xref: /openbmc/linux/fs/orangefs/orangefs-bufmap.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
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);
179*40a74870SChristophe 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 
229*40a74870SChristophe 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:
252*40a74870SChristophe 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 
277575e9461SMike Marshall 		for (i = 0; i < ret; i++) {
278575e9461SMike Marshall 			SetPageError(bufmap->page_array[i]);
2790df55645SJohn Hubbard 			unpin_user_page(bufmap->page_array[i]);
280575e9461SMike Marshall 		}
281575e9461SMike Marshall 		return -ENOMEM;
282575e9461SMike Marshall 	}
283575e9461SMike Marshall 
284575e9461SMike Marshall 	/*
285575e9461SMike Marshall 	 * ideally we want to get kernel space pointers for each page, but
286575e9461SMike Marshall 	 * we can't kmap that many pages at once if highmem is being used.
287575e9461SMike Marshall 	 * so instead, we just kmap/kunmap the page address each time the
288575e9461SMike Marshall 	 * kaddr is needed.
289575e9461SMike Marshall 	 */
290575e9461SMike Marshall 	for (i = 0; i < bufmap->page_count; i++)
291575e9461SMike Marshall 		flush_dcache_page(bufmap->page_array[i]);
292575e9461SMike Marshall 
293575e9461SMike Marshall 	/* build a list of available descriptors */
294575e9461SMike Marshall 	for (offset = 0, i = 0; i < bufmap->desc_count; i++) {
295575e9461SMike Marshall 		bufmap->desc_array[i].page_array = &bufmap->page_array[offset];
296575e9461SMike Marshall 		bufmap->desc_array[i].array_count = pages_per_desc;
297575e9461SMike Marshall 		bufmap->desc_array[i].uaddr =
298575e9461SMike Marshall 		    (user_desc->ptr + (i * pages_per_desc * PAGE_SIZE));
299575e9461SMike Marshall 		offset += pages_per_desc;
300575e9461SMike Marshall 	}
301575e9461SMike Marshall 
302575e9461SMike Marshall 	return 0;
303575e9461SMike Marshall }
304575e9461SMike Marshall 
305575e9461SMike Marshall /*
306575e9461SMike Marshall  * orangefs_bufmap_initialize()
307575e9461SMike Marshall  *
308575e9461SMike Marshall  * initializes the mapped buffer interface
309575e9461SMike Marshall  *
310575e9461SMike Marshall  * returns 0 on success, -errno on failure
311575e9461SMike Marshall  */
orangefs_bufmap_initialize(struct ORANGEFS_dev_map_desc * user_desc)312575e9461SMike Marshall int orangefs_bufmap_initialize(struct ORANGEFS_dev_map_desc *user_desc)
313575e9461SMike Marshall {
314575e9461SMike Marshall 	struct orangefs_bufmap *bufmap;
315575e9461SMike Marshall 	int ret = -EINVAL;
316575e9461SMike Marshall 
317575e9461SMike Marshall 	gossip_debug(GOSSIP_BUFMAP_DEBUG,
318575e9461SMike Marshall 		     "orangefs_bufmap_initialize: called (ptr ("
319575e9461SMike Marshall 		     "%p) sz (%d) cnt(%d).\n",
320575e9461SMike Marshall 		     user_desc->ptr,
321575e9461SMike Marshall 		     user_desc->size,
322575e9461SMike Marshall 		     user_desc->count);
323575e9461SMike Marshall 
324eb82fbcfSDan Carpenter 	if (user_desc->total_size < 0 ||
325eb82fbcfSDan Carpenter 	    user_desc->size < 0 ||
326eb82fbcfSDan Carpenter 	    user_desc->count < 0)
327eb82fbcfSDan Carpenter 		goto out;
328eb82fbcfSDan Carpenter 
329575e9461SMike Marshall 	/*
330575e9461SMike Marshall 	 * sanity check alignment and size of buffer that caller wants to
331575e9461SMike Marshall 	 * work with
332575e9461SMike Marshall 	 */
333575e9461SMike Marshall 	if (PAGE_ALIGN((unsigned long)user_desc->ptr) !=
334575e9461SMike Marshall 	    (unsigned long)user_desc->ptr) {
335575e9461SMike Marshall 		gossip_err("orangefs error: memory alignment (front). %p\n",
336575e9461SMike Marshall 			   user_desc->ptr);
337575e9461SMike Marshall 		goto out;
338575e9461SMike Marshall 	}
339575e9461SMike Marshall 
340575e9461SMike Marshall 	if (PAGE_ALIGN(((unsigned long)user_desc->ptr + user_desc->total_size))
341575e9461SMike Marshall 	    != (unsigned long)(user_desc->ptr + user_desc->total_size)) {
342575e9461SMike Marshall 		gossip_err("orangefs error: memory alignment (back).(%p + %d)\n",
343575e9461SMike Marshall 			   user_desc->ptr,
344575e9461SMike Marshall 			   user_desc->total_size);
345575e9461SMike Marshall 		goto out;
346575e9461SMike Marshall 	}
347575e9461SMike Marshall 
348575e9461SMike Marshall 	if (user_desc->total_size != (user_desc->size * user_desc->count)) {
349575e9461SMike Marshall 		gossip_err("orangefs error: user provided an oddly sized buffer: (%d, %d, %d)\n",
350575e9461SMike Marshall 			   user_desc->total_size,
351575e9461SMike Marshall 			   user_desc->size,
352575e9461SMike Marshall 			   user_desc->count);
353575e9461SMike Marshall 		goto out;
354575e9461SMike Marshall 	}
355575e9461SMike Marshall 
356575e9461SMike Marshall 	if ((user_desc->size % PAGE_SIZE) != 0) {
357575e9461SMike Marshall 		gossip_err("orangefs error: bufmap size not page size divisible (%d).\n",
358575e9461SMike Marshall 			   user_desc->size);
359575e9461SMike Marshall 		goto out;
360575e9461SMike Marshall 	}
361575e9461SMike Marshall 
362575e9461SMike Marshall 	ret = -ENOMEM;
363575e9461SMike Marshall 	bufmap = orangefs_bufmap_alloc(user_desc);
364575e9461SMike Marshall 	if (!bufmap)
365575e9461SMike Marshall 		goto out;
366575e9461SMike Marshall 
367575e9461SMike Marshall 	ret = orangefs_bufmap_map(bufmap, user_desc);
368575e9461SMike Marshall 	if (ret)
369575e9461SMike Marshall 		goto out_free_bufmap;
370575e9461SMike Marshall 
371575e9461SMike Marshall 
372575e9461SMike Marshall 	spin_lock(&orangefs_bufmap_lock);
373575e9461SMike Marshall 	if (__orangefs_bufmap) {
374575e9461SMike Marshall 		spin_unlock(&orangefs_bufmap_lock);
375575e9461SMike Marshall 		gossip_err("orangefs: error: bufmap already initialized.\n");
376ea2c9c9fSAl Viro 		ret = -EINVAL;
377575e9461SMike Marshall 		goto out_unmap_bufmap;
378575e9461SMike Marshall 	}
379575e9461SMike Marshall 	__orangefs_bufmap = bufmap;
380ea2c9c9fSAl Viro 	install(&rw_map,
381ea2c9c9fSAl Viro 		bufmap->desc_count,
382ea2c9c9fSAl Viro 		bufmap->buffer_index_array);
383ea2c9c9fSAl Viro 	install(&readdir_map,
384ea2c9c9fSAl Viro 		ORANGEFS_READDIR_DEFAULT_DESC_COUNT,
385ea2c9c9fSAl Viro 		bufmap->readdir_index_array);
386575e9461SMike Marshall 	spin_unlock(&orangefs_bufmap_lock);
387575e9461SMike Marshall 
388575e9461SMike Marshall 	gossip_debug(GOSSIP_BUFMAP_DEBUG,
389575e9461SMike Marshall 		     "orangefs_bufmap_initialize: exiting normally\n");
390575e9461SMike Marshall 	return 0;
391575e9461SMike Marshall 
392575e9461SMike Marshall out_unmap_bufmap:
393575e9461SMike Marshall 	orangefs_bufmap_unmap(bufmap);
394575e9461SMike Marshall out_free_bufmap:
395575e9461SMike Marshall 	orangefs_bufmap_free(bufmap);
396575e9461SMike Marshall out:
397575e9461SMike Marshall 	return ret;
398575e9461SMike Marshall }
399575e9461SMike Marshall 
400575e9461SMike Marshall /*
401575e9461SMike Marshall  * orangefs_bufmap_finalize()
402575e9461SMike Marshall  *
403575e9461SMike Marshall  * shuts down the mapped buffer interface and releases any resources
404575e9461SMike Marshall  * associated with it
405575e9461SMike Marshall  *
406575e9461SMike Marshall  * no return value
407575e9461SMike Marshall  */
orangefs_bufmap_finalize(void)408575e9461SMike Marshall void orangefs_bufmap_finalize(void)
409575e9461SMike Marshall {
410ea2c9c9fSAl Viro 	struct orangefs_bufmap *bufmap = __orangefs_bufmap;
411ea2c9c9fSAl Viro 	if (!bufmap)
412ea2c9c9fSAl Viro 		return;
413575e9461SMike Marshall 	gossip_debug(GOSSIP_BUFMAP_DEBUG, "orangefs_bufmap_finalize: called\n");
414ea2c9c9fSAl Viro 	mark_killed(&rw_map);
415ea2c9c9fSAl Viro 	mark_killed(&readdir_map);
416575e9461SMike Marshall 	gossip_debug(GOSSIP_BUFMAP_DEBUG,
417575e9461SMike Marshall 		     "orangefs_bufmap_finalize: exiting normally\n");
418575e9461SMike Marshall }
419575e9461SMike Marshall 
orangefs_bufmap_run_down(void)420ea2c9c9fSAl Viro void orangefs_bufmap_run_down(void)
421575e9461SMike Marshall {
422ea2c9c9fSAl Viro 	struct orangefs_bufmap *bufmap = __orangefs_bufmap;
423ea2c9c9fSAl Viro 	if (!bufmap)
424575e9461SMike Marshall 		return;
425ea2c9c9fSAl Viro 	run_down(&rw_map);
426ea2c9c9fSAl Viro 	run_down(&readdir_map);
427ea2c9c9fSAl Viro 	spin_lock(&orangefs_bufmap_lock);
428ea2c9c9fSAl Viro 	__orangefs_bufmap = NULL;
429ea2c9c9fSAl Viro 	spin_unlock(&orangefs_bufmap_lock);
430ea2c9c9fSAl Viro 	orangefs_bufmap_unmap(bufmap);
431ea2c9c9fSAl Viro 	orangefs_bufmap_free(bufmap);
432575e9461SMike Marshall }
433575e9461SMike Marshall 
434575e9461SMike Marshall /*
435575e9461SMike Marshall  * orangefs_bufmap_get()
436575e9461SMike Marshall  *
437575e9461SMike Marshall  * gets a free mapped buffer descriptor, will sleep until one becomes
438575e9461SMike Marshall  * available if necessary
439575e9461SMike Marshall  *
440b8a99a8fSAl Viro  * returns slot on success, -errno on failure
441575e9461SMike Marshall  */
orangefs_bufmap_get(void)442b8a99a8fSAl Viro int orangefs_bufmap_get(void)
443575e9461SMike Marshall {
444b8a99a8fSAl Viro 	return get(&rw_map);
445575e9461SMike Marshall }
446575e9461SMike Marshall 
447575e9461SMike Marshall /*
448575e9461SMike Marshall  * orangefs_bufmap_put()
449575e9461SMike Marshall  *
450575e9461SMike Marshall  * returns a mapped buffer descriptor to the collection
451575e9461SMike Marshall  *
452575e9461SMike Marshall  * no return value
453575e9461SMike Marshall  */
orangefs_bufmap_put(int buffer_index)4541357d06dSAl Viro void orangefs_bufmap_put(int buffer_index)
455575e9461SMike Marshall {
456ea2c9c9fSAl Viro 	put(&rw_map, buffer_index);
457575e9461SMike Marshall }
458575e9461SMike Marshall 
459575e9461SMike Marshall /*
4607d221485SMartin Brandenburg  * orangefs_readdir_index_get()
461575e9461SMike Marshall  *
462575e9461SMike Marshall  * gets a free descriptor, will sleep until one becomes
463575e9461SMike Marshall  * available if necessary.
464575e9461SMike Marshall  * Although the readdir buffers are not mapped into kernel space
465575e9461SMike Marshall  * we could do that at a later point of time. Regardless, these
466575e9461SMike Marshall  * indices are used by the client-core.
467575e9461SMike Marshall  *
468b8a99a8fSAl Viro  * returns slot on success, -errno on failure
469575e9461SMike Marshall  */
orangefs_readdir_index_get(void)470b8a99a8fSAl Viro int orangefs_readdir_index_get(void)
471575e9461SMike Marshall {
472b8a99a8fSAl Viro 	return get(&readdir_map);
473575e9461SMike Marshall }
474575e9461SMike Marshall 
orangefs_readdir_index_put(int buffer_index)47582d37f19SAl Viro void orangefs_readdir_index_put(int buffer_index)
476575e9461SMike Marshall {
477ea2c9c9fSAl Viro 	put(&readdir_map, buffer_index);
478575e9461SMike Marshall }
479575e9461SMike Marshall 
480b5e376eaSMike Marshall /*
481b5e376eaSMike Marshall  * we've been handed an iovec, we need to copy it to
482b5e376eaSMike Marshall  * the shared memory descriptor at "buffer_index".
483b5e376eaSMike Marshall  */
orangefs_bufmap_copy_from_iovec(struct iov_iter * iter,int buffer_index,size_t size)484bf6bf606SAl Viro int orangefs_bufmap_copy_from_iovec(struct iov_iter *iter,
485575e9461SMike Marshall 				int buffer_index,
486575e9461SMike Marshall 				size_t size)
487575e9461SMike Marshall {
488bf6bf606SAl Viro 	struct orangefs_bufmap_desc *to;
489575e9461SMike Marshall 	int i;
490575e9461SMike Marshall 
491575e9461SMike Marshall 	gossip_debug(GOSSIP_BUFMAP_DEBUG,
492575e9461SMike Marshall 		     "%s: buffer_index:%d: size:%zu:\n",
493575e9461SMike Marshall 		     __func__, buffer_index, size);
494575e9461SMike Marshall 
495bf6bf606SAl Viro 	to = &__orangefs_bufmap->desc_array[buffer_index];
496575e9461SMike Marshall 	for (i = 0; size; i++) {
497575e9461SMike Marshall 		struct page *page = to->page_array[i];
498575e9461SMike Marshall 		size_t n = size;
499575e9461SMike Marshall 		if (n > PAGE_SIZE)
500575e9461SMike Marshall 			n = PAGE_SIZE;
501890559e3SAl Viro 		if (copy_page_from_iter(page, 0, n, iter) != n)
502575e9461SMike Marshall 			return -EFAULT;
503575e9461SMike Marshall 		size -= n;
504575e9461SMike Marshall 	}
505575e9461SMike Marshall 	return 0;
506575e9461SMike Marshall }
507575e9461SMike Marshall 
508575e9461SMike Marshall /*
509b5e376eaSMike Marshall  * we've been handed an iovec, we need to fill it from
510b5e376eaSMike Marshall  * the shared memory descriptor at "buffer_index".
511575e9461SMike Marshall  */
orangefs_bufmap_copy_to_iovec(struct iov_iter * iter,int buffer_index,size_t size)512bf6bf606SAl Viro int orangefs_bufmap_copy_to_iovec(struct iov_iter *iter,
513575e9461SMike Marshall 				    int buffer_index,
514575e9461SMike Marshall 				    size_t size)
515575e9461SMike Marshall {
516bf6bf606SAl Viro 	struct orangefs_bufmap_desc *from;
517575e9461SMike Marshall 	int i;
518575e9461SMike Marshall 
519bf6bf606SAl Viro 	from = &__orangefs_bufmap->desc_array[buffer_index];
520575e9461SMike Marshall 	gossip_debug(GOSSIP_BUFMAP_DEBUG,
521575e9461SMike Marshall 		     "%s: buffer_index:%d: size:%zu:\n",
522575e9461SMike Marshall 		     __func__, buffer_index, size);
523575e9461SMike Marshall 
524575e9461SMike Marshall 
525575e9461SMike Marshall 	for (i = 0; size; i++) {
526575e9461SMike Marshall 		struct page *page = from->page_array[i];
527575e9461SMike Marshall 		size_t n = size;
528575e9461SMike Marshall 		if (n > PAGE_SIZE)
529575e9461SMike Marshall 			n = PAGE_SIZE;
530575e9461SMike Marshall 		n = copy_page_to_iter(page, 0, n, iter);
531575e9461SMike Marshall 		if (!n)
532575e9461SMike Marshall 			return -EFAULT;
533575e9461SMike Marshall 		size -= n;
534575e9461SMike Marshall 	}
535575e9461SMike Marshall 	return 0;
536575e9461SMike Marshall }
537dd59a647SMike Marshall 
orangefs_bufmap_page_fill(void * page_to,int buffer_index,int slot_index)538dd59a647SMike Marshall void orangefs_bufmap_page_fill(void *page_to,
539dd59a647SMike Marshall 				int buffer_index,
540dd59a647SMike Marshall 				int slot_index)
541dd59a647SMike Marshall {
542dd59a647SMike Marshall 	struct orangefs_bufmap_desc *from;
543dd59a647SMike Marshall 	void *page_from;
544dd59a647SMike Marshall 
545dd59a647SMike Marshall 	from = &__orangefs_bufmap->desc_array[buffer_index];
546dd59a647SMike Marshall 	page_from = kmap_atomic(from->page_array[slot_index]);
547dd59a647SMike Marshall 	memcpy(page_to, page_from, PAGE_SIZE);
548dd59a647SMike Marshall 	kunmap_atomic(page_from);
549dd59a647SMike Marshall }
550