orangefs-bufmap.c 12.9 KB
Newer Older
M
Mike Marshall 已提交
1 2 3 4 5 6
/*
 * (C) 2001 Clemson University and The University of Chicago
 *
 * See COPYING in top-level directory.
 */
#include "protocol.h"
7 8
#include "orangefs-kernel.h"
#include "orangefs-bufmap.h"
M
Mike Marshall 已提交
9

A
Al Viro 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
struct slot_map {
	int c;
	wait_queue_head_t q;
	int count;
	unsigned long *map;
};

static struct slot_map rw_map = {
	.c = -1,
	.q = __WAIT_QUEUE_HEAD_INITIALIZER(rw_map.q)
};
static struct slot_map readdir_map = {
	.c = -1,
	.q = __WAIT_QUEUE_HEAD_INITIALIZER(readdir_map.q)
};


static void install(struct slot_map *m, int count, unsigned long *map)
{
	spin_lock(&m->q.lock);
	m->c = m->count = count;
	m->map = map;
	wake_up_all_locked(&m->q);
	spin_unlock(&m->q.lock);
}

static void mark_killed(struct slot_map *m)
{
	spin_lock(&m->q.lock);
	m->c -= m->count + 1;
	spin_unlock(&m->q.lock);
}

static void run_down(struct slot_map *m)
{
	DEFINE_WAIT(wait);
	spin_lock(&m->q.lock);
	if (m->c != -1) {
		for (;;) {
			if (likely(list_empty(&wait.task_list)))
				__add_wait_queue_tail(&m->q, &wait);
			set_current_state(TASK_UNINTERRUPTIBLE);

			if (m->c == -1)
				break;

			spin_unlock(&m->q.lock);
			schedule();
			spin_lock(&m->q.lock);
		}
		__remove_wait_queue(&m->q, &wait);
		__set_current_state(TASK_RUNNING);
	}
	m->map = NULL;
	spin_unlock(&m->q.lock);
}

static void put(struct slot_map *m, int slot)
{
	int v;
	spin_lock(&m->q.lock);
	__clear_bit(slot, m->map);
	v = ++m->c;
	if (unlikely(v == 1))	/* no free slots -> one free slot */
		wake_up_locked(&m->q);
	else if (unlikely(v == -1))	/* finished dying */
		wake_up_all_locked(&m->q);
	spin_unlock(&m->q.lock);
}

static int wait_for_free(struct slot_map *m)
{
	long left = slot_timeout_secs * HZ;
	DEFINE_WAIT(wait);

	do {
		long n = left, t;
		if (likely(list_empty(&wait.task_list)))
			__add_wait_queue_tail_exclusive(&m->q, &wait);
		set_current_state(TASK_INTERRUPTIBLE);

		if (m->c > 0)
			break;

		if (m->c < 0) {
			/* we are waiting for map to be installed */
			/* it would better be there soon, or we go away */
			if (n > ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ)
				n = ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ;
		}
		spin_unlock(&m->q.lock);
		t = schedule_timeout(n);
		spin_lock(&m->q.lock);
		if (unlikely(!t) && n != left && m->c < 0)
			left = t;
		else
			left = t + (left - n);
		if (unlikely(signal_pending(current)))
			left = -EINTR;
	} while (left > 0);

	if (!list_empty(&wait.task_list))
		list_del(&wait.task_list);
	else if (left <= 0 && waitqueue_active(&m->q))
		__wake_up_locked_key(&m->q, TASK_INTERRUPTIBLE, NULL);
	__set_current_state(TASK_RUNNING);

	if (likely(left > 0))
		return 0;

	return left < 0 ? -EINTR : -ETIMEDOUT;
}

static int get(struct slot_map *m)
{
	int res = 0;
	spin_lock(&m->q.lock);
	if (unlikely(m->c <= 0))
		res = wait_for_free(m);
	if (likely(!res)) {
		m->c--;
		res = find_first_zero_bit(m->map, m->count);
		__set_bit(res, m->map);
	}
	spin_unlock(&m->q.lock);
	return res;
}
M
Mike Marshall 已提交
137

138 139 140 141 142 143 144 145
/* used to describe mapped buffers */
struct orangefs_bufmap_desc {
	void *uaddr;			/* user space address pointer */
	struct page **page_array;	/* array of mapped pages */
	int array_count;		/* size of above arrays */
	struct list_head list_link;
};

146
static struct orangefs_bufmap {
M
Mike Marshall 已提交
147 148 149 150 151 152 153
	int desc_size;
	int desc_shift;
	int desc_count;
	int total_size;
	int page_count;

	struct page **page_array;
154
	struct orangefs_bufmap_desc *desc_array;
M
Mike Marshall 已提交
155 156

	/* array to track usage of buffer descriptors */
A
Al Viro 已提交
157
	unsigned long *buffer_index_array;
M
Mike Marshall 已提交
158 159

	/* array to track usage of buffer descriptors for readdir */
A
Al Viro 已提交
160 161 162
#define N DIV_ROUND_UP(ORANGEFS_READDIR_DEFAULT_DESC_COUNT, BITS_PER_LONG)
	unsigned long readdir_index_array[N];
#undef N
163
} *__orangefs_bufmap;
M
Mike Marshall 已提交
164

165
static DEFINE_SPINLOCK(orangefs_bufmap_lock);
M
Mike Marshall 已提交
166 167

static void
168
orangefs_bufmap_unmap(struct orangefs_bufmap *bufmap)
M
Mike Marshall 已提交
169 170 171 172 173 174 175 176
{
	int i;

	for (i = 0; i < bufmap->page_count; i++)
		page_cache_release(bufmap->page_array[i]);
}

static void
177
orangefs_bufmap_free(struct orangefs_bufmap *bufmap)
M
Mike Marshall 已提交
178 179 180 181 182 183 184
{
	kfree(bufmap->page_array);
	kfree(bufmap->desc_array);
	kfree(bufmap->buffer_index_array);
	kfree(bufmap);
}

185 186 187 188 189
/*
 * XXX: Can the size and shift change while the caller gives up the 
 * XXX: lock between calling this and doing something useful?
 */

190
int orangefs_bufmap_size_query(void)
M
Mike Marshall 已提交
191
{
192 193
	struct orangefs_bufmap *bufmap;
	int size = 0;
194 195 196
	spin_lock(&orangefs_bufmap_lock);
	bufmap = __orangefs_bufmap;
	if (bufmap)
197
		size = bufmap->desc_size;
198
	spin_unlock(&orangefs_bufmap_lock);
M
Mike Marshall 已提交
199 200 201
	return size;
}

202
int orangefs_bufmap_shift_query(void)
M
Mike Marshall 已提交
203
{
204 205
	struct orangefs_bufmap *bufmap;
	int shift = 0;
206 207 208
	spin_lock(&orangefs_bufmap_lock);
	bufmap = __orangefs_bufmap;
	if (bufmap)
209
		shift = bufmap->desc_shift;
210
	spin_unlock(&orangefs_bufmap_lock);
M
Mike Marshall 已提交
211 212 213 214 215 216 217
	return shift;
}

static DECLARE_WAIT_QUEUE_HEAD(bufmap_waitq);
static DECLARE_WAIT_QUEUE_HEAD(readdir_waitq);

/*
218
 * orangefs_get_bufmap_init
M
Mike Marshall 已提交
219 220 221 222 223 224
 *
 * If bufmap_init is 1, then the shared memory system, including the
 * buffer_index_array, is available.  Otherwise, it is not.
 *
 * returns the value of bufmap_init
 */
225
int orangefs_get_bufmap_init(void)
M
Mike Marshall 已提交
226
{
227
	return __orangefs_bufmap ? 1 : 0;
M
Mike Marshall 已提交
228 229 230
}


231 232
static struct orangefs_bufmap *
orangefs_bufmap_alloc(struct ORANGEFS_dev_map_desc *user_desc)
M
Mike Marshall 已提交
233
{
234
	struct orangefs_bufmap *bufmap;
M
Mike Marshall 已提交
235 236 237 238 239 240 241 242 243 244 245

	bufmap = kzalloc(sizeof(*bufmap), GFP_KERNEL);
	if (!bufmap)
		goto out;

	bufmap->total_size = user_desc->total_size;
	bufmap->desc_count = user_desc->count;
	bufmap->desc_size = user_desc->size;
	bufmap->desc_shift = ilog2(bufmap->desc_size);

	bufmap->buffer_index_array =
A
Al Viro 已提交
246
		kzalloc(DIV_ROUND_UP(bufmap->desc_count, BITS_PER_LONG), GFP_KERNEL);
M
Mike Marshall 已提交
247
	if (!bufmap->buffer_index_array) {
248
		gossip_err("orangefs: could not allocate %d buffer indices\n",
M
Mike Marshall 已提交
249 250 251 252 253
				bufmap->desc_count);
		goto out_free_bufmap;
	}

	bufmap->desc_array =
254
		kcalloc(bufmap->desc_count, sizeof(struct orangefs_bufmap_desc),
M
Mike Marshall 已提交
255 256
			GFP_KERNEL);
	if (!bufmap->desc_array) {
257
		gossip_err("orangefs: could not allocate %d descriptors\n",
M
Mike Marshall 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
				bufmap->desc_count);
		goto out_free_index_array;
	}

	bufmap->page_count = bufmap->total_size / PAGE_SIZE;

	/* allocate storage to track our page mappings */
	bufmap->page_array =
		kcalloc(bufmap->page_count, sizeof(struct page *), GFP_KERNEL);
	if (!bufmap->page_array)
		goto out_free_desc_array;

	return bufmap;

out_free_desc_array:
	kfree(bufmap->desc_array);
out_free_index_array:
	kfree(bufmap->buffer_index_array);
out_free_bufmap:
	kfree(bufmap);
out:
	return NULL;
}

static int
283 284
orangefs_bufmap_map(struct orangefs_bufmap *bufmap,
		struct ORANGEFS_dev_map_desc *user_desc)
M
Mike Marshall 已提交
285 286 287 288 289
{
	int pages_per_desc = bufmap->desc_size / PAGE_SIZE;
	int offset = 0, ret, i;

	/* map the pages */
290 291
	ret = get_user_pages_fast((unsigned long)user_desc->ptr,
			     bufmap->page_count, 1, bufmap->page_array);
M
Mike Marshall 已提交
292 293 294 295 296

	if (ret < 0)
		return ret;

	if (ret != bufmap->page_count) {
297
		gossip_err("orangefs error: asked for %d pages, only got %d.\n",
M
Mike Marshall 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
				bufmap->page_count, ret);

		for (i = 0; i < ret; i++) {
			SetPageError(bufmap->page_array[i]);
			page_cache_release(bufmap->page_array[i]);
		}
		return -ENOMEM;
	}

	/*
	 * ideally we want to get kernel space pointers for each page, but
	 * we can't kmap that many pages at once if highmem is being used.
	 * so instead, we just kmap/kunmap the page address each time the
	 * kaddr is needed.
	 */
	for (i = 0; i < bufmap->page_count; i++)
		flush_dcache_page(bufmap->page_array[i]);

	/* build a list of available descriptors */
	for (offset = 0, i = 0; i < bufmap->desc_count; i++) {
		bufmap->desc_array[i].page_array = &bufmap->page_array[offset];
		bufmap->desc_array[i].array_count = pages_per_desc;
		bufmap->desc_array[i].uaddr =
		    (user_desc->ptr + (i * pages_per_desc * PAGE_SIZE));
		offset += pages_per_desc;
	}

	return 0;
}

/*
329
 * orangefs_bufmap_initialize()
M
Mike Marshall 已提交
330 331 332 333 334
 *
 * initializes the mapped buffer interface
 *
 * returns 0 on success, -errno on failure
 */
335
int orangefs_bufmap_initialize(struct ORANGEFS_dev_map_desc *user_desc)
M
Mike Marshall 已提交
336
{
337
	struct orangefs_bufmap *bufmap;
M
Mike Marshall 已提交
338 339 340
	int ret = -EINVAL;

	gossip_debug(GOSSIP_BUFMAP_DEBUG,
341
		     "orangefs_bufmap_initialize: called (ptr ("
M
Mike Marshall 已提交
342 343 344 345 346 347 348 349 350 351 352
		     "%p) sz (%d) cnt(%d).\n",
		     user_desc->ptr,
		     user_desc->size,
		     user_desc->count);

	/*
	 * sanity check alignment and size of buffer that caller wants to
	 * work with
	 */
	if (PAGE_ALIGN((unsigned long)user_desc->ptr) !=
	    (unsigned long)user_desc->ptr) {
353
		gossip_err("orangefs error: memory alignment (front). %p\n",
M
Mike Marshall 已提交
354 355 356 357 358 359
			   user_desc->ptr);
		goto out;
	}

	if (PAGE_ALIGN(((unsigned long)user_desc->ptr + user_desc->total_size))
	    != (unsigned long)(user_desc->ptr + user_desc->total_size)) {
360
		gossip_err("orangefs error: memory alignment (back).(%p + %d)\n",
M
Mike Marshall 已提交
361 362 363 364 365 366
			   user_desc->ptr,
			   user_desc->total_size);
		goto out;
	}

	if (user_desc->total_size != (user_desc->size * user_desc->count)) {
367
		gossip_err("orangefs error: user provided an oddly sized buffer: (%d, %d, %d)\n",
M
Mike Marshall 已提交
368 369 370 371 372 373 374
			   user_desc->total_size,
			   user_desc->size,
			   user_desc->count);
		goto out;
	}

	if ((user_desc->size % PAGE_SIZE) != 0) {
375
		gossip_err("orangefs error: bufmap size not page size divisible (%d).\n",
M
Mike Marshall 已提交
376 377 378 379 380
			   user_desc->size);
		goto out;
	}

	ret = -ENOMEM;
381
	bufmap = orangefs_bufmap_alloc(user_desc);
M
Mike Marshall 已提交
382 383 384
	if (!bufmap)
		goto out;

385
	ret = orangefs_bufmap_map(bufmap, user_desc);
M
Mike Marshall 已提交
386 387 388 389
	if (ret)
		goto out_free_bufmap;


390 391 392 393
	spin_lock(&orangefs_bufmap_lock);
	if (__orangefs_bufmap) {
		spin_unlock(&orangefs_bufmap_lock);
		gossip_err("orangefs: error: bufmap already initialized.\n");
A
Al Viro 已提交
394
		ret = -EINVAL;
M
Mike Marshall 已提交
395 396
		goto out_unmap_bufmap;
	}
397
	__orangefs_bufmap = bufmap;
A
Al Viro 已提交
398 399 400 401 402 403
	install(&rw_map,
		bufmap->desc_count,
		bufmap->buffer_index_array);
	install(&readdir_map,
		ORANGEFS_READDIR_DEFAULT_DESC_COUNT,
		bufmap->readdir_index_array);
404
	spin_unlock(&orangefs_bufmap_lock);
M
Mike Marshall 已提交
405 406

	gossip_debug(GOSSIP_BUFMAP_DEBUG,
407
		     "orangefs_bufmap_initialize: exiting normally\n");
M
Mike Marshall 已提交
408 409 410
	return 0;

out_unmap_bufmap:
411
	orangefs_bufmap_unmap(bufmap);
M
Mike Marshall 已提交
412
out_free_bufmap:
413
	orangefs_bufmap_free(bufmap);
M
Mike Marshall 已提交
414 415 416 417 418
out:
	return ret;
}

/*
419
 * orangefs_bufmap_finalize()
M
Mike Marshall 已提交
420 421 422 423 424 425
 *
 * shuts down the mapped buffer interface and releases any resources
 * associated with it
 *
 * no return value
 */
426
void orangefs_bufmap_finalize(void)
M
Mike Marshall 已提交
427
{
A
Al Viro 已提交
428 429 430
	struct orangefs_bufmap *bufmap = __orangefs_bufmap;
	if (!bufmap)
		return;
431
	gossip_debug(GOSSIP_BUFMAP_DEBUG, "orangefs_bufmap_finalize: called\n");
A
Al Viro 已提交
432 433
	mark_killed(&rw_map);
	mark_killed(&readdir_map);
M
Mike Marshall 已提交
434
	gossip_debug(GOSSIP_BUFMAP_DEBUG,
435
		     "orangefs_bufmap_finalize: exiting normally\n");
M
Mike Marshall 已提交
436 437
}

A
Al Viro 已提交
438
void orangefs_bufmap_run_down(void)
M
Mike Marshall 已提交
439
{
A
Al Viro 已提交
440 441
	struct orangefs_bufmap *bufmap = __orangefs_bufmap;
	if (!bufmap)
M
Mike Marshall 已提交
442
		return;
A
Al Viro 已提交
443 444 445 446 447 448 449
	run_down(&rw_map);
	run_down(&readdir_map);
	spin_lock(&orangefs_bufmap_lock);
	__orangefs_bufmap = NULL;
	spin_unlock(&orangefs_bufmap_lock);
	orangefs_bufmap_unmap(bufmap);
	orangefs_bufmap_free(bufmap);
M
Mike Marshall 已提交
450 451 452
}

/*
453
 * orangefs_bufmap_get()
M
Mike Marshall 已提交
454 455 456 457 458 459
 *
 * gets a free mapped buffer descriptor, will sleep until one becomes
 * available if necessary
 *
 * returns 0 on success, -errno on failure
 */
460
int orangefs_bufmap_get(struct orangefs_bufmap **mapp, int *buffer_index)
M
Mike Marshall 已提交
461
{
A
Al Viro 已提交
462 463 464 465 466
	int ret = get(&rw_map);
	if (ret >= 0) {
		*mapp = __orangefs_bufmap;
		*buffer_index = ret;
		ret = 0;
M
Mike Marshall 已提交
467 468 469 470 471
	}
	return ret;
}

/*
472
 * orangefs_bufmap_put()
M
Mike Marshall 已提交
473 474 475 476 477
 *
 * returns a mapped buffer descriptor to the collection
 *
 * no return value
 */
478
void orangefs_bufmap_put(int buffer_index)
M
Mike Marshall 已提交
479
{
A
Al Viro 已提交
480
	put(&rw_map, buffer_index);
M
Mike Marshall 已提交
481 482 483
}

/*
484
 * orangefs_readdir_index_get()
M
Mike Marshall 已提交
485 486 487 488 489 490 491 492 493
 *
 * gets a free descriptor, will sleep until one becomes
 * available if necessary.
 * Although the readdir buffers are not mapped into kernel space
 * we could do that at a later point of time. Regardless, these
 * indices are used by the client-core.
 *
 * returns 0 on success, -errno on failure
 */
494
int orangefs_readdir_index_get(struct orangefs_bufmap **mapp, int *buffer_index)
M
Mike Marshall 已提交
495
{
A
Al Viro 已提交
496 497 498 499 500
	int ret = get(&readdir_map);
	if (ret >= 0) {
		*mapp = __orangefs_bufmap;
		*buffer_index = ret;
		ret = 0;
M
Mike Marshall 已提交
501 502 503 504
	}
	return ret;
}

505
void orangefs_readdir_index_put(int buffer_index)
M
Mike Marshall 已提交
506
{
A
Al Viro 已提交
507
	put(&readdir_map, buffer_index);
M
Mike Marshall 已提交
508 509
}

M
Mike Marshall 已提交
510 511 512 513
/*
 * we've been handed an iovec, we need to copy it to 
 * the shared memory descriptor at "buffer_index".
 */
514
int orangefs_bufmap_copy_from_iovec(struct iov_iter *iter,
515 516
				int buffer_index,
				size_t size)
M
Mike Marshall 已提交
517
{
518
	struct orangefs_bufmap_desc *to;
M
Mike Marshall 已提交
519
	int i;
M
Mike Marshall 已提交
520 521

	gossip_debug(GOSSIP_BUFMAP_DEBUG,
522
		     "%s: buffer_index:%d: size:%zu:\n",
M
Mike Marshall 已提交
523
		     __func__, buffer_index, size);
M
Mike Marshall 已提交
524

525
	to = &__orangefs_bufmap->desc_array[buffer_index];
M
Mike Marshall 已提交
526
	for (i = 0; size; i++) {
527 528 529 530 531 532 533 534
		struct page *page = to->page_array[i];
		size_t n = size;
		if (n > PAGE_SIZE)
			n = PAGE_SIZE;
		n = copy_page_from_iter(page, 0, n, iter);
		if (!n)
			return -EFAULT;
		size -= n;
M
Mike Marshall 已提交
535
	}
536
	return 0;
M
Mike Marshall 已提交
537 538 539 540

}

/*
M
Mike Marshall 已提交
541 542
 * we've been handed an iovec, we need to fill it from
 * the shared memory descriptor at "buffer_index".
M
Mike Marshall 已提交
543
 */
544
int orangefs_bufmap_copy_to_iovec(struct iov_iter *iter,
545 546
				    int buffer_index,
				    size_t size)
M
Mike Marshall 已提交
547
{
548
	struct orangefs_bufmap_desc *from;
M
Mike Marshall 已提交
549
	int i;
M
Mike Marshall 已提交
550

551
	from = &__orangefs_bufmap->desc_array[buffer_index];
M
Mike Marshall 已提交
552
	gossip_debug(GOSSIP_BUFMAP_DEBUG,
553 554
		     "%s: buffer_index:%d: size:%zu:\n",
		     __func__, buffer_index, size);
M
Mike Marshall 已提交
555 556


557 558 559 560 561 562 563 564 565
	for (i = 0; size; i++) {
		struct page *page = from->page_array[i];
		size_t n = size;
		if (n > PAGE_SIZE)
			n = PAGE_SIZE;
		n = copy_page_to_iter(page, 0, n, iter);
		if (!n)
			return -EFAULT;
		size -= n;
M
Mike Marshall 已提交
566
	}
567
	return 0;
M
Mike Marshall 已提交
568
}