xref: /linux/fs/orangefs/orangefs-bufmap.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2575e9461SMike Marshall /*
3575e9461SMike Marshall  * (C) 2001 Clemson University and The University of Chicago
4575e9461SMike Marshall  *
5575e9461SMike Marshall  * See COPYING in top-level directory.
6575e9461SMike Marshall  */
7575e9461SMike Marshall #include "protocol.h"
8575e9461SMike Marshall #include "orangefs-kernel.h"
9575e9461SMike Marshall #include "orangefs-bufmap.h"
10575e9461SMike Marshall 
11ea2c9c9fSAl Viro struct slot_map {
12ea2c9c9fSAl Viro 	int c;
13ea2c9c9fSAl Viro 	wait_queue_head_t q;
14ea2c9c9fSAl Viro 	int count;
15ea2c9c9fSAl Viro 	unsigned long *map;
16ea2c9c9fSAl Viro };
17ea2c9c9fSAl Viro 
18ea2c9c9fSAl Viro static struct slot_map rw_map = {
19ea2c9c9fSAl Viro 	.c = -1,
20ea2c9c9fSAl Viro 	.q = __WAIT_QUEUE_HEAD_INITIALIZER(rw_map.q)
21ea2c9c9fSAl Viro };
22ea2c9c9fSAl Viro static struct slot_map readdir_map = {
23ea2c9c9fSAl Viro 	.c = -1,
24ea2c9c9fSAl Viro 	.q = __WAIT_QUEUE_HEAD_INITIALIZER(readdir_map.q)
25ea2c9c9fSAl Viro };
26ea2c9c9fSAl Viro 
27ea2c9c9fSAl Viro 
install(struct slot_map * m,int count,unsigned long * map)28ea2c9c9fSAl Viro static void install(struct slot_map *m, int count, unsigned long *map)
29ea2c9c9fSAl Viro {
30ea2c9c9fSAl Viro 	spin_lock(&m->q.lock);
31ea2c9c9fSAl Viro 	m->c = m->count = count;
32ea2c9c9fSAl Viro 	m->map = map;
33ea2c9c9fSAl Viro 	wake_up_all_locked(&m->q);
34ea2c9c9fSAl Viro 	spin_unlock(&m->q.lock);
35ea2c9c9fSAl Viro }
36ea2c9c9fSAl Viro 
mark_killed(struct slot_map * m)37ea2c9c9fSAl Viro static void mark_killed(struct slot_map *m)
38ea2c9c9fSAl Viro {
39ea2c9c9fSAl Viro 	spin_lock(&m->q.lock);
40ea2c9c9fSAl Viro 	m->c -= m->count + 1;
41ea2c9c9fSAl Viro 	spin_unlock(&m->q.lock);
42ea2c9c9fSAl Viro }
43ea2c9c9fSAl Viro 
run_down(struct slot_map * m)44ea2c9c9fSAl Viro static void run_down(struct slot_map *m)
45ea2c9c9fSAl Viro {
46ea2c9c9fSAl Viro 	DEFINE_WAIT(wait);
47ea2c9c9fSAl Viro 	spin_lock(&m->q.lock);
48ea2c9c9fSAl Viro 	if (m->c != -1) {
49ea2c9c9fSAl Viro 		for (;;) {
502055da97SIngo Molnar 			if (likely(list_empty(&wait.entry)))
51ac6424b9SIngo Molnar 				__add_wait_queue_entry_tail(&m->q, &wait);
52ea2c9c9fSAl Viro 			set_current_state(TASK_UNINTERRUPTIBLE);
53ea2c9c9fSAl Viro 
54ea2c9c9fSAl Viro 			if (m->c == -1)
55ea2c9c9fSAl Viro 				break;
56ea2c9c9fSAl Viro 
57ea2c9c9fSAl Viro 			spin_unlock(&m->q.lock);
58ea2c9c9fSAl Viro 			schedule();
59ea2c9c9fSAl Viro 			spin_lock(&m->q.lock);
60ea2c9c9fSAl Viro 		}
61ea2c9c9fSAl Viro 		__remove_wait_queue(&m->q, &wait);
62ea2c9c9fSAl Viro 		__set_current_state(TASK_RUNNING);
63ea2c9c9fSAl Viro 	}
64ea2c9c9fSAl Viro 	m->map = NULL;
65ea2c9c9fSAl Viro 	spin_unlock(&m->q.lock);
66ea2c9c9fSAl Viro }
67ea2c9c9fSAl Viro 
put(struct slot_map * m,int slot)68ea2c9c9fSAl Viro static void put(struct slot_map *m, int slot)
69ea2c9c9fSAl Viro {
70ea2c9c9fSAl Viro 	int v;
71ea2c9c9fSAl Viro 	spin_lock(&m->q.lock);
72ea2c9c9fSAl Viro 	__clear_bit(slot, m->map);
73ea2c9c9fSAl Viro 	v = ++m->c;
74c2676ef8SDavid Reynolds 	if (v > 0)
75ea2c9c9fSAl Viro 		wake_up_locked(&m->q);
76c2676ef8SDavid Reynolds 	if (unlikely(v == -1))     /* finished dying */
77ea2c9c9fSAl Viro 		wake_up_all_locked(&m->q);
78ea2c9c9fSAl Viro 	spin_unlock(&m->q.lock);
79ea2c9c9fSAl Viro }
80ea2c9c9fSAl Viro 
wait_for_free(struct slot_map * m)81ea2c9c9fSAl Viro static int wait_for_free(struct slot_map *m)
82ea2c9c9fSAl Viro {
83ea2c9c9fSAl Viro 	long left = slot_timeout_secs * HZ;
84ea2c9c9fSAl Viro 	DEFINE_WAIT(wait);
85ea2c9c9fSAl Viro 
86ea2c9c9fSAl Viro 	do {
87ea2c9c9fSAl Viro 		long n = left, t;
882055da97SIngo Molnar 		if (likely(list_empty(&wait.entry)))
89ac6424b9SIngo Molnar 			__add_wait_queue_entry_tail_exclusive(&m->q, &wait);
90ea2c9c9fSAl Viro 		set_current_state(TASK_INTERRUPTIBLE);
91ea2c9c9fSAl Viro 
92ea2c9c9fSAl Viro 		if (m->c > 0)
93ea2c9c9fSAl Viro 			break;
94ea2c9c9fSAl Viro 
95ea2c9c9fSAl Viro 		if (m->c < 0) {
96ea2c9c9fSAl Viro 			/* we are waiting for map to be installed */
97ea2c9c9fSAl Viro 			/* it would better be there soon, or we go away */
98ea2c9c9fSAl Viro 			if (n > ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ)
99ea2c9c9fSAl Viro 				n = ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ;
100ea2c9c9fSAl Viro 		}
101ea2c9c9fSAl Viro 		spin_unlock(&m->q.lock);
102ea2c9c9fSAl Viro 		t = schedule_timeout(n);
103ea2c9c9fSAl Viro 		spin_lock(&m->q.lock);
104ea2c9c9fSAl Viro 		if (unlikely(!t) && n != left && m->c < 0)
105ea2c9c9fSAl Viro 			left = t;
106ea2c9c9fSAl Viro 		else
107ea2c9c9fSAl Viro 			left = t + (left - n);
10808d405c8SDavidlohr Bueso 		if (signal_pending(current))
109ea2c9c9fSAl Viro 			left = -EINTR;
110ea2c9c9fSAl Viro 	} while (left > 0);
111ea2c9c9fSAl Viro 
1122055da97SIngo Molnar 	if (!list_empty(&wait.entry))
1132055da97SIngo Molnar 		list_del(&wait.entry);
114ea2c9c9fSAl Viro 	else if (left <= 0 && waitqueue_active(&m->q))
115ea2c9c9fSAl Viro 		__wake_up_locked_key(&m->q, TASK_INTERRUPTIBLE, NULL);
116ea2c9c9fSAl Viro 	__set_current_state(TASK_RUNNING);
117ea2c9c9fSAl Viro 
118ea2c9c9fSAl Viro 	if (likely(left > 0))
119ea2c9c9fSAl Viro 		return 0;
120ea2c9c9fSAl Viro 
121ea2c9c9fSAl Viro 	return left < 0 ? -EINTR : -ETIMEDOUT;
122ea2c9c9fSAl Viro }
123ea2c9c9fSAl Viro 
get(struct slot_map * m)124ea2c9c9fSAl Viro static int get(struct slot_map *m)
125ea2c9c9fSAl Viro {
126ea2c9c9fSAl Viro 	int res = 0;
127ea2c9c9fSAl Viro 	spin_lock(&m->q.lock);
128ea2c9c9fSAl Viro 	if (unlikely(m->c <= 0))
129ea2c9c9fSAl Viro 		res = wait_for_free(m);
130ea2c9c9fSAl Viro 	if (likely(!res)) {
131ea2c9c9fSAl Viro 		m->c--;
132ea2c9c9fSAl Viro 		res = find_first_zero_bit(m->map, m->count);
133ea2c9c9fSAl Viro 		__set_bit(res, m->map);
134ea2c9c9fSAl Viro 	}
135ea2c9c9fSAl Viro 	spin_unlock(&m->q.lock);
136ea2c9c9fSAl Viro 	return res;
137ea2c9c9fSAl Viro }
138575e9461SMike Marshall 
139bf89f584SMartin Brandenburg /* used to describe mapped buffers */
140bf89f584SMartin Brandenburg struct orangefs_bufmap_desc {
141817e9b4dSMike Marshall 	void __user *uaddr;		/* user space address pointer */
142bf89f584SMartin Brandenburg 	struct page **page_array;	/* array of mapped pages */
143bf89f584SMartin Brandenburg 	int array_count;		/* size of above arrays */
144bf89f584SMartin Brandenburg 	struct list_head list_link;
145bf89f584SMartin Brandenburg };
146bf89f584SMartin Brandenburg 
147575e9461SMike Marshall static struct orangefs_bufmap {
148575e9461SMike Marshall 	int desc_size;
149575e9461SMike Marshall 	int desc_shift;
150575e9461SMike Marshall 	int desc_count;
151575e9461SMike Marshall 	int total_size;
152575e9461SMike Marshall 	int page_count;
153575e9461SMike Marshall 
154575e9461SMike Marshall 	struct page **page_array;
155575e9461SMike Marshall 	struct orangefs_bufmap_desc *desc_array;
156575e9461SMike Marshall 
157575e9461SMike Marshall 	/* array to track usage of buffer descriptors */
158ea2c9c9fSAl Viro 	unsigned long *buffer_index_array;
159575e9461SMike Marshall 
160575e9461SMike Marshall 	/* array to track usage of buffer descriptors for readdir */
161ea2c9c9fSAl Viro #define N DIV_ROUND_UP(ORANGEFS_READDIR_DEFAULT_DESC_COUNT, BITS_PER_LONG)
162ea2c9c9fSAl Viro 	unsigned long readdir_index_array[N];
163ea2c9c9fSAl Viro #undef N
164575e9461SMike Marshall } *__orangefs_bufmap;
165575e9461SMike Marshall 
166575e9461SMike Marshall static DEFINE_SPINLOCK(orangefs_bufmap_lock);
167575e9461SMike Marshall 
168575e9461SMike Marshall static void
orangefs_bufmap_unmap(struct orangefs_bufmap * bufmap)169575e9461SMike Marshall orangefs_bufmap_unmap(struct orangefs_bufmap *bufmap)
170575e9461SMike Marshall {
1710df55645SJohn Hubbard 	unpin_user_pages(bufmap->page_array, bufmap->page_count);
172575e9461SMike Marshall }
173575e9461SMike Marshall 
174575e9461SMike Marshall static void
orangefs_bufmap_free(struct orangefs_bufmap * bufmap)175575e9461SMike Marshall orangefs_bufmap_free(struct orangefs_bufmap *bufmap)
176575e9461SMike Marshall {
177575e9461SMike Marshall 	kfree(bufmap->page_array);
178575e9461SMike Marshall 	kfree(bufmap->desc_array);
17940a74870SChristophe JAILLET 	bitmap_free(bufmap->buffer_index_array);
180575e9461SMike Marshall 	kfree(bufmap);
181575e9461SMike Marshall }
182575e9461SMike Marshall 
183b09d10dfSMartin Brandenburg /*
184b09d10dfSMartin Brandenburg  * XXX: Can the size and shift change while the caller gives up the
185b09d10dfSMartin Brandenburg  * XXX: lock between calling this and doing something useful?
186b09d10dfSMartin Brandenburg  */
187b09d10dfSMartin Brandenburg 
orangefs_bufmap_size_query(void)188765a75b3SMartin Brandenburg int orangefs_bufmap_size_query(void)
189575e9461SMike Marshall {
190b09d10dfSMartin Brandenburg 	struct orangefs_bufmap *bufmap;
191b09d10dfSMartin Brandenburg 	int size = 0;
19217804184SAl Viro 	spin_lock(&orangefs_bufmap_lock);
19317804184SAl Viro 	bufmap = __orangefs_bufmap;
19417804184SAl Viro 	if (bufmap)
195b09d10dfSMartin Brandenburg 		size = bufmap->desc_size;
19617804184SAl Viro 	spin_unlock(&orangefs_bufmap_lock);
197575e9461SMike Marshall 	return size;
198575e9461SMike Marshall }
199575e9461SMike Marshall 
orangefs_bufmap_shift_query(void)200765a75b3SMartin Brandenburg int orangefs_bufmap_shift_query(void)
201575e9461SMike Marshall {
202b09d10dfSMartin Brandenburg 	struct orangefs_bufmap *bufmap;
203b09d10dfSMartin Brandenburg 	int shift = 0;
20417804184SAl Viro 	spin_lock(&orangefs_bufmap_lock);
20517804184SAl Viro 	bufmap = __orangefs_bufmap;
20617804184SAl Viro 	if (bufmap)
207b09d10dfSMartin Brandenburg 		shift = bufmap->desc_shift;
20817804184SAl Viro 	spin_unlock(&orangefs_bufmap_lock);
209575e9461SMike Marshall 	return shift;
210575e9461SMike Marshall }
211575e9461SMike Marshall 
212575e9461SMike Marshall static DECLARE_WAIT_QUEUE_HEAD(bufmap_waitq);
213575e9461SMike Marshall static DECLARE_WAIT_QUEUE_HEAD(readdir_waitq);
214575e9461SMike Marshall 
215575e9461SMike Marshall static struct orangefs_bufmap *
orangefs_bufmap_alloc(struct ORANGEFS_dev_map_desc * user_desc)216575e9461SMike Marshall orangefs_bufmap_alloc(struct ORANGEFS_dev_map_desc *user_desc)
217575e9461SMike Marshall {
218575e9461SMike Marshall 	struct orangefs_bufmap *bufmap;
219575e9461SMike Marshall 
220575e9461SMike Marshall 	bufmap = kzalloc(sizeof(*bufmap), GFP_KERNEL);
221575e9461SMike Marshall 	if (!bufmap)
222575e9461SMike Marshall 		goto out;
223575e9461SMike Marshall 
224575e9461SMike Marshall 	bufmap->total_size = user_desc->total_size;
225575e9461SMike Marshall 	bufmap->desc_count = user_desc->count;
226575e9461SMike Marshall 	bufmap->desc_size = user_desc->size;
227575e9461SMike Marshall 	bufmap->desc_shift = ilog2(bufmap->desc_size);
228575e9461SMike Marshall 
22940a74870SChristophe JAILLET 	bufmap->buffer_index_array = bitmap_zalloc(bufmap->desc_count, GFP_KERNEL);
23007a25853SMarkus Elfring 	if (!bufmap->buffer_index_array)
231575e9461SMike Marshall 		goto out_free_bufmap;
232575e9461SMike Marshall 
233575e9461SMike Marshall 	bufmap->desc_array =
234575e9461SMike Marshall 		kcalloc(bufmap->desc_count, sizeof(struct orangefs_bufmap_desc),
235575e9461SMike Marshall 			GFP_KERNEL);
23607a25853SMarkus Elfring 	if (!bufmap->desc_array)
237575e9461SMike Marshall 		goto out_free_index_array;
238575e9461SMike Marshall 
239575e9461SMike Marshall 	bufmap->page_count = bufmap->total_size / PAGE_SIZE;
240575e9461SMike Marshall 
241575e9461SMike Marshall 	/* allocate storage to track our page mappings */
242575e9461SMike Marshall 	bufmap->page_array =
243575e9461SMike Marshall 		kcalloc(bufmap->page_count, sizeof(struct page *), GFP_KERNEL);
244575e9461SMike Marshall 	if (!bufmap->page_array)
245575e9461SMike Marshall 		goto out_free_desc_array;
246575e9461SMike Marshall 
247575e9461SMike Marshall 	return bufmap;
248575e9461SMike Marshall 
249575e9461SMike Marshall out_free_desc_array:
250575e9461SMike Marshall 	kfree(bufmap->desc_array);
251575e9461SMike Marshall out_free_index_array:
25240a74870SChristophe JAILLET 	bitmap_free(bufmap->buffer_index_array);
253575e9461SMike Marshall out_free_bufmap:
254575e9461SMike Marshall 	kfree(bufmap);
255575e9461SMike Marshall out:
256575e9461SMike Marshall 	return NULL;
257575e9461SMike Marshall }
258575e9461SMike Marshall 
259575e9461SMike Marshall static int
orangefs_bufmap_map(struct orangefs_bufmap * bufmap,struct ORANGEFS_dev_map_desc * user_desc)260575e9461SMike Marshall orangefs_bufmap_map(struct orangefs_bufmap *bufmap,
261575e9461SMike Marshall 		struct ORANGEFS_dev_map_desc *user_desc)
262575e9461SMike Marshall {
263575e9461SMike Marshall 	int pages_per_desc = bufmap->desc_size / PAGE_SIZE;
264575e9461SMike Marshall 	int offset = 0, ret, i;
265575e9461SMike Marshall 
266575e9461SMike Marshall 	/* map the pages */
2670df55645SJohn Hubbard 	ret = pin_user_pages_fast((unsigned long)user_desc->ptr,
26873b0140bSIra Weiny 			     bufmap->page_count, FOLL_WRITE, bufmap->page_array);
269575e9461SMike Marshall 
270575e9461SMike Marshall 	if (ret < 0)
271575e9461SMike Marshall 		return ret;
272575e9461SMike Marshall 
273575e9461SMike Marshall 	if (ret != bufmap->page_count) {
274575e9461SMike Marshall 		gossip_err("orangefs error: asked for %d pages, only got %d.\n",
275575e9461SMike Marshall 				bufmap->page_count, ret);
276575e9461SMike Marshall 
277*86b3d5f6SMatthew Wilcox (Oracle) 		for (i = 0; i < ret; i++)
2780df55645SJohn Hubbard 			unpin_user_page(bufmap->page_array[i]);
279575e9461SMike Marshall 		return -ENOMEM;
280575e9461SMike Marshall 	}
281575e9461SMike Marshall 
282575e9461SMike Marshall 	/*
283575e9461SMike Marshall 	 * ideally we want to get kernel space pointers for each page, but
284575e9461SMike Marshall 	 * we can't kmap that many pages at once if highmem is being used.
285575e9461SMike Marshall 	 * so instead, we just kmap/kunmap the page address each time the
286575e9461SMike Marshall 	 * kaddr is needed.
287575e9461SMike Marshall 	 */
288575e9461SMike Marshall 	for (i = 0; i < bufmap->page_count; i++)
289575e9461SMike Marshall 		flush_dcache_page(bufmap->page_array[i]);
290575e9461SMike Marshall 
291575e9461SMike Marshall 	/* build a list of available descriptors */
292575e9461SMike Marshall 	for (offset = 0, i = 0; i < bufmap->desc_count; i++) {
293575e9461SMike Marshall 		bufmap->desc_array[i].page_array = &bufmap->page_array[offset];
294575e9461SMike Marshall 		bufmap->desc_array[i].array_count = pages_per_desc;
295575e9461SMike Marshall 		bufmap->desc_array[i].uaddr =
296575e9461SMike Marshall 		    (user_desc->ptr + (i * pages_per_desc * PAGE_SIZE));
297575e9461SMike Marshall 		offset += pages_per_desc;
298575e9461SMike Marshall 	}
299575e9461SMike Marshall 
300575e9461SMike Marshall 	return 0;
301575e9461SMike Marshall }
302575e9461SMike Marshall 
303575e9461SMike Marshall /*
304575e9461SMike Marshall  * orangefs_bufmap_initialize()
305575e9461SMike Marshall  *
306575e9461SMike Marshall  * initializes the mapped buffer interface
307575e9461SMike Marshall  *
308575e9461SMike Marshall  * returns 0 on success, -errno on failure
309575e9461SMike Marshall  */
orangefs_bufmap_initialize(struct ORANGEFS_dev_map_desc * user_desc)310575e9461SMike Marshall int orangefs_bufmap_initialize(struct ORANGEFS_dev_map_desc *user_desc)
311575e9461SMike Marshall {
312575e9461SMike Marshall 	struct orangefs_bufmap *bufmap;
313575e9461SMike Marshall 	int ret = -EINVAL;
314575e9461SMike Marshall 
315575e9461SMike Marshall 	gossip_debug(GOSSIP_BUFMAP_DEBUG,
316575e9461SMike Marshall 		     "orangefs_bufmap_initialize: called (ptr ("
317575e9461SMike Marshall 		     "%p) sz (%d) cnt(%d).\n",
318575e9461SMike Marshall 		     user_desc->ptr,
319575e9461SMike Marshall 		     user_desc->size,
320575e9461SMike Marshall 		     user_desc->count);
321575e9461SMike Marshall 
322eb82fbcfSDan Carpenter 	if (user_desc->total_size < 0 ||
323eb82fbcfSDan Carpenter 	    user_desc->size < 0 ||
324eb82fbcfSDan Carpenter 	    user_desc->count < 0)
325eb82fbcfSDan Carpenter 		goto out;
326eb82fbcfSDan Carpenter 
327575e9461SMike Marshall 	/*
328575e9461SMike Marshall 	 * sanity check alignment and size of buffer that caller wants to
329575e9461SMike Marshall 	 * work with
330575e9461SMike Marshall 	 */
331575e9461SMike Marshall 	if (PAGE_ALIGN((unsigned long)user_desc->ptr) !=
332575e9461SMike Marshall 	    (unsigned long)user_desc->ptr) {
333575e9461SMike Marshall 		gossip_err("orangefs error: memory alignment (front). %p\n",
334575e9461SMike Marshall 			   user_desc->ptr);
335575e9461SMike Marshall 		goto out;
336575e9461SMike Marshall 	}
337575e9461SMike Marshall 
338575e9461SMike Marshall 	if (PAGE_ALIGN(((unsigned long)user_desc->ptr + user_desc->total_size))
339575e9461SMike Marshall 	    != (unsigned long)(user_desc->ptr + user_desc->total_size)) {
340575e9461SMike Marshall 		gossip_err("orangefs error: memory alignment (back).(%p + %d)\n",
341575e9461SMike Marshall 			   user_desc->ptr,
342575e9461SMike Marshall 			   user_desc->total_size);
343575e9461SMike Marshall 		goto out;
344575e9461SMike Marshall 	}
345575e9461SMike Marshall 
346575e9461SMike Marshall 	if (user_desc->total_size != (user_desc->size * user_desc->count)) {
347575e9461SMike Marshall 		gossip_err("orangefs error: user provided an oddly sized buffer: (%d, %d, %d)\n",
348575e9461SMike Marshall 			   user_desc->total_size,
349575e9461SMike Marshall 			   user_desc->size,
350575e9461SMike Marshall 			   user_desc->count);
351575e9461SMike Marshall 		goto out;
352575e9461SMike Marshall 	}
353575e9461SMike Marshall 
354575e9461SMike Marshall 	if ((user_desc->size % PAGE_SIZE) != 0) {
355575e9461SMike Marshall 		gossip_err("orangefs error: bufmap size not page size divisible (%d).\n",
356575e9461SMike Marshall 			   user_desc->size);
357575e9461SMike Marshall 		goto out;
358575e9461SMike Marshall 	}
359575e9461SMike Marshall 
360575e9461SMike Marshall 	ret = -ENOMEM;
361575e9461SMike Marshall 	bufmap = orangefs_bufmap_alloc(user_desc);
362575e9461SMike Marshall 	if (!bufmap)
363575e9461SMike Marshall 		goto out;
364575e9461SMike Marshall 
365575e9461SMike Marshall 	ret = orangefs_bufmap_map(bufmap, user_desc);
366575e9461SMike Marshall 	if (ret)
367575e9461SMike Marshall 		goto out_free_bufmap;
368575e9461SMike Marshall 
369575e9461SMike Marshall 
370575e9461SMike Marshall 	spin_lock(&orangefs_bufmap_lock);
371575e9461SMike Marshall 	if (__orangefs_bufmap) {
372575e9461SMike Marshall 		spin_unlock(&orangefs_bufmap_lock);
373575e9461SMike Marshall 		gossip_err("orangefs: error: bufmap already initialized.\n");
374ea2c9c9fSAl Viro 		ret = -EINVAL;
375575e9461SMike Marshall 		goto out_unmap_bufmap;
376575e9461SMike Marshall 	}
377575e9461SMike Marshall 	__orangefs_bufmap = bufmap;
378ea2c9c9fSAl Viro 	install(&rw_map,
379ea2c9c9fSAl Viro 		bufmap->desc_count,
380ea2c9c9fSAl Viro 		bufmap->buffer_index_array);
381ea2c9c9fSAl Viro 	install(&readdir_map,
382ea2c9c9fSAl Viro 		ORANGEFS_READDIR_DEFAULT_DESC_COUNT,
383ea2c9c9fSAl Viro 		bufmap->readdir_index_array);
384575e9461SMike Marshall 	spin_unlock(&orangefs_bufmap_lock);
385575e9461SMike Marshall 
386575e9461SMike Marshall 	gossip_debug(GOSSIP_BUFMAP_DEBUG,
387575e9461SMike Marshall 		     "orangefs_bufmap_initialize: exiting normally\n");
388575e9461SMike Marshall 	return 0;
389575e9461SMike Marshall 
390575e9461SMike Marshall out_unmap_bufmap:
391575e9461SMike Marshall 	orangefs_bufmap_unmap(bufmap);
392575e9461SMike Marshall out_free_bufmap:
393575e9461SMike Marshall 	orangefs_bufmap_free(bufmap);
394575e9461SMike Marshall out:
395575e9461SMike Marshall 	return ret;
396575e9461SMike Marshall }
397575e9461SMike Marshall 
398575e9461SMike Marshall /*
399575e9461SMike Marshall  * orangefs_bufmap_finalize()
400575e9461SMike Marshall  *
401575e9461SMike Marshall  * shuts down the mapped buffer interface and releases any resources
402575e9461SMike Marshall  * associated with it
403575e9461SMike Marshall  *
404575e9461SMike Marshall  * no return value
405575e9461SMike Marshall  */
orangefs_bufmap_finalize(void)406575e9461SMike Marshall void orangefs_bufmap_finalize(void)
407575e9461SMike Marshall {
408ea2c9c9fSAl Viro 	struct orangefs_bufmap *bufmap = __orangefs_bufmap;
409ea2c9c9fSAl Viro 	if (!bufmap)
410ea2c9c9fSAl Viro 		return;
411575e9461SMike Marshall 	gossip_debug(GOSSIP_BUFMAP_DEBUG, "orangefs_bufmap_finalize: called\n");
412ea2c9c9fSAl Viro 	mark_killed(&rw_map);
413ea2c9c9fSAl Viro 	mark_killed(&readdir_map);
414575e9461SMike Marshall 	gossip_debug(GOSSIP_BUFMAP_DEBUG,
415575e9461SMike Marshall 		     "orangefs_bufmap_finalize: exiting normally\n");
416575e9461SMike Marshall }
417575e9461SMike Marshall 
orangefs_bufmap_run_down(void)418ea2c9c9fSAl Viro void orangefs_bufmap_run_down(void)
419575e9461SMike Marshall {
420ea2c9c9fSAl Viro 	struct orangefs_bufmap *bufmap = __orangefs_bufmap;
421ea2c9c9fSAl Viro 	if (!bufmap)
422575e9461SMike Marshall 		return;
423ea2c9c9fSAl Viro 	run_down(&rw_map);
424ea2c9c9fSAl Viro 	run_down(&readdir_map);
425ea2c9c9fSAl Viro 	spin_lock(&orangefs_bufmap_lock);
426ea2c9c9fSAl Viro 	__orangefs_bufmap = NULL;
427ea2c9c9fSAl Viro 	spin_unlock(&orangefs_bufmap_lock);
428ea2c9c9fSAl Viro 	orangefs_bufmap_unmap(bufmap);
429ea2c9c9fSAl Viro 	orangefs_bufmap_free(bufmap);
430575e9461SMike Marshall }
431575e9461SMike Marshall 
432575e9461SMike Marshall /*
433575e9461SMike Marshall  * orangefs_bufmap_get()
434575e9461SMike Marshall  *
435575e9461SMike Marshall  * gets a free mapped buffer descriptor, will sleep until one becomes
436575e9461SMike Marshall  * available if necessary
437575e9461SMike Marshall  *
438b8a99a8fSAl Viro  * returns slot on success, -errno on failure
439575e9461SMike Marshall  */
orangefs_bufmap_get(void)440b8a99a8fSAl Viro int orangefs_bufmap_get(void)
441575e9461SMike Marshall {
442b8a99a8fSAl Viro 	return get(&rw_map);
443575e9461SMike Marshall }
444575e9461SMike Marshall 
445575e9461SMike Marshall /*
446575e9461SMike Marshall  * orangefs_bufmap_put()
447575e9461SMike Marshall  *
448575e9461SMike Marshall  * returns a mapped buffer descriptor to the collection
449575e9461SMike Marshall  *
450575e9461SMike Marshall  * no return value
451575e9461SMike Marshall  */
orangefs_bufmap_put(int buffer_index)4521357d06dSAl Viro void orangefs_bufmap_put(int buffer_index)
453575e9461SMike Marshall {
454ea2c9c9fSAl Viro 	put(&rw_map, buffer_index);
455575e9461SMike Marshall }
456575e9461SMike Marshall 
457575e9461SMike Marshall /*
4587d221485SMartin Brandenburg  * orangefs_readdir_index_get()
459575e9461SMike Marshall  *
460575e9461SMike Marshall  * gets a free descriptor, will sleep until one becomes
461575e9461SMike Marshall  * available if necessary.
462575e9461SMike Marshall  * Although the readdir buffers are not mapped into kernel space
463575e9461SMike Marshall  * we could do that at a later point of time. Regardless, these
464575e9461SMike Marshall  * indices are used by the client-core.
465575e9461SMike Marshall  *
466b8a99a8fSAl Viro  * returns slot on success, -errno on failure
467575e9461SMike Marshall  */
orangefs_readdir_index_get(void)468b8a99a8fSAl Viro int orangefs_readdir_index_get(void)
469575e9461SMike Marshall {
470b8a99a8fSAl Viro 	return get(&readdir_map);
471575e9461SMike Marshall }
472575e9461SMike Marshall 
orangefs_readdir_index_put(int buffer_index)47382d37f19SAl Viro void orangefs_readdir_index_put(int buffer_index)
474575e9461SMike Marshall {
475ea2c9c9fSAl Viro 	put(&readdir_map, buffer_index);
476575e9461SMike Marshall }
477575e9461SMike Marshall 
478b5e376eaSMike Marshall /*
479b5e376eaSMike Marshall  * we've been handed an iovec, we need to copy it to
480b5e376eaSMike Marshall  * the shared memory descriptor at "buffer_index".
481b5e376eaSMike Marshall  */
orangefs_bufmap_copy_from_iovec(struct iov_iter * iter,int buffer_index,size_t size)482bf6bf606SAl Viro int orangefs_bufmap_copy_from_iovec(struct iov_iter *iter,
483575e9461SMike Marshall 				int buffer_index,
484575e9461SMike Marshall 				size_t size)
485575e9461SMike Marshall {
486bf6bf606SAl Viro 	struct orangefs_bufmap_desc *to;
487575e9461SMike Marshall 	int i;
488575e9461SMike Marshall 
489575e9461SMike Marshall 	gossip_debug(GOSSIP_BUFMAP_DEBUG,
490575e9461SMike Marshall 		     "%s: buffer_index:%d: size:%zu:\n",
491575e9461SMike Marshall 		     __func__, buffer_index, size);
492575e9461SMike Marshall 
493bf6bf606SAl Viro 	to = &__orangefs_bufmap->desc_array[buffer_index];
494575e9461SMike Marshall 	for (i = 0; size; i++) {
495575e9461SMike Marshall 		struct page *page = to->page_array[i];
496575e9461SMike Marshall 		size_t n = size;
497575e9461SMike Marshall 		if (n > PAGE_SIZE)
498575e9461SMike Marshall 			n = PAGE_SIZE;
499890559e3SAl Viro 		if (copy_page_from_iter(page, 0, n, iter) != n)
500575e9461SMike Marshall 			return -EFAULT;
501575e9461SMike Marshall 		size -= n;
502575e9461SMike Marshall 	}
503575e9461SMike Marshall 	return 0;
504575e9461SMike Marshall }
505575e9461SMike Marshall 
506575e9461SMike Marshall /*
507b5e376eaSMike Marshall  * we've been handed an iovec, we need to fill it from
508b5e376eaSMike Marshall  * the shared memory descriptor at "buffer_index".
509575e9461SMike Marshall  */
orangefs_bufmap_copy_to_iovec(struct iov_iter * iter,int buffer_index,size_t size)510bf6bf606SAl Viro int orangefs_bufmap_copy_to_iovec(struct iov_iter *iter,
511575e9461SMike Marshall 				    int buffer_index,
512575e9461SMike Marshall 				    size_t size)
513575e9461SMike Marshall {
514bf6bf606SAl Viro 	struct orangefs_bufmap_desc *from;
515575e9461SMike Marshall 	int i;
516575e9461SMike Marshall 
517bf6bf606SAl Viro 	from = &__orangefs_bufmap->desc_array[buffer_index];
518575e9461SMike Marshall 	gossip_debug(GOSSIP_BUFMAP_DEBUG,
519575e9461SMike Marshall 		     "%s: buffer_index:%d: size:%zu:\n",
520575e9461SMike Marshall 		     __func__, buffer_index, size);
521575e9461SMike Marshall 
522575e9461SMike Marshall 
523575e9461SMike Marshall 	for (i = 0; size; i++) {
524575e9461SMike Marshall 		struct page *page = from->page_array[i];
525575e9461SMike Marshall 		size_t n = size;
526575e9461SMike Marshall 		if (n > PAGE_SIZE)
527575e9461SMike Marshall 			n = PAGE_SIZE;
528575e9461SMike Marshall 		n = copy_page_to_iter(page, 0, n, iter);
529575e9461SMike Marshall 		if (!n)
530575e9461SMike Marshall 			return -EFAULT;
531575e9461SMike Marshall 		size -= n;
532575e9461SMike Marshall 	}
533575e9461SMike Marshall 	return 0;
534575e9461SMike Marshall }
535dd59a647SMike Marshall 
orangefs_bufmap_page_fill(void * page_to,int buffer_index,int slot_index)536dd59a647SMike Marshall void orangefs_bufmap_page_fill(void *page_to,
537dd59a647SMike Marshall 				int buffer_index,
538dd59a647SMike Marshall 				int slot_index)
539dd59a647SMike Marshall {
540dd59a647SMike Marshall 	struct orangefs_bufmap_desc *from;
541dd59a647SMike Marshall 	void *page_from;
542dd59a647SMike Marshall 
543dd59a647SMike Marshall 	from = &__orangefs_bufmap->desc_array[buffer_index];
544dd59a647SMike Marshall 	page_from = kmap_atomic(from->page_array[slot_index]);
545dd59a647SMike Marshall 	memcpy(page_to, page_from, PAGE_SIZE);
546dd59a647SMike Marshall 	kunmap_atomic(page_from);
547dd59a647SMike Marshall }
548