Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * (C) 2001 Clemson University and The University of Chicago
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * See COPYING in top-level directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include "protocol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include "orangefs-kernel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "orangefs-bufmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) struct slot_map {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 	int c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 	wait_queue_head_t q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	unsigned long *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static struct slot_map rw_map = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	.c = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	.q = __WAIT_QUEUE_HEAD_INITIALIZER(rw_map.q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static struct slot_map readdir_map = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	.c = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	.q = __WAIT_QUEUE_HEAD_INITIALIZER(readdir_map.q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static void install(struct slot_map *m, int count, unsigned long *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	spin_lock(&m->q.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	m->c = m->count = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	m->map = map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	wake_up_all_locked(&m->q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	spin_unlock(&m->q.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static void mark_killed(struct slot_map *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	spin_lock(&m->q.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	m->c -= m->count + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	spin_unlock(&m->q.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static void run_down(struct slot_map *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	DEFINE_WAIT(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	spin_lock(&m->q.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (m->c != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			if (likely(list_empty(&wait.entry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 				__add_wait_queue_entry_tail(&m->q, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			set_current_state(TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 			if (m->c == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			spin_unlock(&m->q.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 			schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 			spin_lock(&m->q.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		__remove_wait_queue(&m->q, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		__set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	m->map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	spin_unlock(&m->q.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static void put(struct slot_map *m, int slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	int v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	spin_lock(&m->q.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	__clear_bit(slot, m->map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	v = ++m->c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (v > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		wake_up_locked(&m->q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (unlikely(v == -1))     /* finished dying */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		wake_up_all_locked(&m->q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	spin_unlock(&m->q.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) static int wait_for_free(struct slot_map *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	long left = slot_timeout_secs * HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	DEFINE_WAIT(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		long n = left, t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		if (likely(list_empty(&wait.entry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			__add_wait_queue_entry_tail_exclusive(&m->q, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		if (m->c > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		if (m->c < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			/* we are waiting for map to be installed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			/* it would better be there soon, or we go away */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			if (n > ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 				n = ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		spin_unlock(&m->q.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		t = schedule_timeout(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		spin_lock(&m->q.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		if (unlikely(!t) && n != left && m->c < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			left = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			left = t + (left - n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		if (signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			left = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	} while (left > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	if (!list_empty(&wait.entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		list_del(&wait.entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	else if (left <= 0 && waitqueue_active(&m->q))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		__wake_up_locked_key(&m->q, TASK_INTERRUPTIBLE, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	__set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (likely(left > 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	return left < 0 ? -EINTR : -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static int get(struct slot_map *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	spin_lock(&m->q.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	if (unlikely(m->c <= 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		res = wait_for_free(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (likely(!res)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		m->c--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		res = find_first_zero_bit(m->map, m->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		__set_bit(res, m->map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	spin_unlock(&m->q.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* used to describe mapped buffers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct orangefs_bufmap_desc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	void __user *uaddr;		/* user space address pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	struct page **page_array;	/* array of mapped pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	int array_count;		/* size of above arrays */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	struct list_head list_link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static struct orangefs_bufmap {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	int desc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	int desc_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	int desc_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	int total_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	int page_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	struct page **page_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	struct orangefs_bufmap_desc *desc_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	/* array to track usage of buffer descriptors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	unsigned long *buffer_index_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	/* array to track usage of buffer descriptors for readdir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) #define N DIV_ROUND_UP(ORANGEFS_READDIR_DEFAULT_DESC_COUNT, BITS_PER_LONG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	unsigned long readdir_index_array[N];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) #undef N
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) } *__orangefs_bufmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static DEFINE_SPINLOCK(orangefs_bufmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) orangefs_bufmap_unmap(struct orangefs_bufmap *bufmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	unpin_user_pages(bufmap->page_array, bufmap->page_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) orangefs_bufmap_free(struct orangefs_bufmap *bufmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	kfree(bufmap->page_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	kfree(bufmap->desc_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	bitmap_free(bufmap->buffer_index_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	kfree(bufmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  * XXX: Can the size and shift change while the caller gives up the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  * XXX: lock between calling this and doing something useful?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int orangefs_bufmap_size_query(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	struct orangefs_bufmap *bufmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	int size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	spin_lock(&orangefs_bufmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	bufmap = __orangefs_bufmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (bufmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		size = bufmap->desc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	spin_unlock(&orangefs_bufmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) int orangefs_bufmap_shift_query(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	struct orangefs_bufmap *bufmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	int shift = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	spin_lock(&orangefs_bufmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	bufmap = __orangefs_bufmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	if (bufmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		shift = bufmap->desc_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	spin_unlock(&orangefs_bufmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	return shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static DECLARE_WAIT_QUEUE_HEAD(bufmap_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static DECLARE_WAIT_QUEUE_HEAD(readdir_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static struct orangefs_bufmap *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) orangefs_bufmap_alloc(struct ORANGEFS_dev_map_desc *user_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	struct orangefs_bufmap *bufmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	bufmap = kzalloc(sizeof(*bufmap), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (!bufmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	bufmap->total_size = user_desc->total_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	bufmap->desc_count = user_desc->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	bufmap->desc_size = user_desc->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	bufmap->desc_shift = ilog2(bufmap->desc_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	bufmap->buffer_index_array = bitmap_zalloc(bufmap->desc_count, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (!bufmap->buffer_index_array)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		goto out_free_bufmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	bufmap->desc_array =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		kcalloc(bufmap->desc_count, sizeof(struct orangefs_bufmap_desc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (!bufmap->desc_array)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		goto out_free_index_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	bufmap->page_count = bufmap->total_size / PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	/* allocate storage to track our page mappings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	bufmap->page_array =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		kcalloc(bufmap->page_count, sizeof(struct page *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (!bufmap->page_array)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		goto out_free_desc_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	return bufmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) out_free_desc_array:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	kfree(bufmap->desc_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) out_free_index_array:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	bitmap_free(bufmap->buffer_index_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) out_free_bufmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	kfree(bufmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) orangefs_bufmap_map(struct orangefs_bufmap *bufmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		struct ORANGEFS_dev_map_desc *user_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	int pages_per_desc = bufmap->desc_size / PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	int offset = 0, ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	/* map the pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	ret = pin_user_pages_fast((unsigned long)user_desc->ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			     bufmap->page_count, FOLL_WRITE, bufmap->page_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (ret != bufmap->page_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		gossip_err("orangefs error: asked for %d pages, only got %d.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 				bufmap->page_count, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		for (i = 0; i < ret; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			SetPageError(bufmap->page_array[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			unpin_user_page(bufmap->page_array[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	 * ideally we want to get kernel space pointers for each page, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	 * we can't kmap that many pages at once if highmem is being used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	 * so instead, we just kmap/kunmap the page address each time the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	 * kaddr is needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	for (i = 0; i < bufmap->page_count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		flush_dcache_page(bufmap->page_array[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	/* build a list of available descriptors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	for (offset = 0, i = 0; i < bufmap->desc_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		bufmap->desc_array[i].page_array = &bufmap->page_array[offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		bufmap->desc_array[i].array_count = pages_per_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		bufmap->desc_array[i].uaddr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		    (user_desc->ptr + (i * pages_per_desc * PAGE_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		offset += pages_per_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)  * orangefs_bufmap_initialize()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  * initializes the mapped buffer interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)  * returns 0 on success, -errno on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) int orangefs_bufmap_initialize(struct ORANGEFS_dev_map_desc *user_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	struct orangefs_bufmap *bufmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	gossip_debug(GOSSIP_BUFMAP_DEBUG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		     "orangefs_bufmap_initialize: called (ptr ("
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		     "%p) sz (%d) cnt(%d).\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		     user_desc->ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		     user_desc->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		     user_desc->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	if (user_desc->total_size < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	    user_desc->size < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	    user_desc->count < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	 * sanity check alignment and size of buffer that caller wants to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	 * work with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	if (PAGE_ALIGN((unsigned long)user_desc->ptr) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	    (unsigned long)user_desc->ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		gossip_err("orangefs error: memory alignment (front). %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			   user_desc->ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	if (PAGE_ALIGN(((unsigned long)user_desc->ptr + user_desc->total_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	    != (unsigned long)(user_desc->ptr + user_desc->total_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		gossip_err("orangefs error: memory alignment (back).(%p + %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			   user_desc->ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			   user_desc->total_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (user_desc->total_size != (user_desc->size * user_desc->count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		gossip_err("orangefs error: user provided an oddly sized buffer: (%d, %d, %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			   user_desc->total_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			   user_desc->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			   user_desc->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if ((user_desc->size % PAGE_SIZE) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		gossip_err("orangefs error: bufmap size not page size divisible (%d).\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 			   user_desc->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	bufmap = orangefs_bufmap_alloc(user_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	if (!bufmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	ret = orangefs_bufmap_map(bufmap, user_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		goto out_free_bufmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	spin_lock(&orangefs_bufmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	if (__orangefs_bufmap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		spin_unlock(&orangefs_bufmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		gossip_err("orangefs: error: bufmap already initialized.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		goto out_unmap_bufmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	__orangefs_bufmap = bufmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	install(&rw_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		bufmap->desc_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		bufmap->buffer_index_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	install(&readdir_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		ORANGEFS_READDIR_DEFAULT_DESC_COUNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		bufmap->readdir_index_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	spin_unlock(&orangefs_bufmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	gossip_debug(GOSSIP_BUFMAP_DEBUG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		     "orangefs_bufmap_initialize: exiting normally\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) out_unmap_bufmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	orangefs_bufmap_unmap(bufmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) out_free_bufmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	orangefs_bufmap_free(bufmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)  * orangefs_bufmap_finalize()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)  * shuts down the mapped buffer interface and releases any resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)  * associated with it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)  * no return value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) void orangefs_bufmap_finalize(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	struct orangefs_bufmap *bufmap = __orangefs_bufmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	if (!bufmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	gossip_debug(GOSSIP_BUFMAP_DEBUG, "orangefs_bufmap_finalize: called\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	mark_killed(&rw_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	mark_killed(&readdir_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	gossip_debug(GOSSIP_BUFMAP_DEBUG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		     "orangefs_bufmap_finalize: exiting normally\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) void orangefs_bufmap_run_down(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	struct orangefs_bufmap *bufmap = __orangefs_bufmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	if (!bufmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	run_down(&rw_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	run_down(&readdir_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	spin_lock(&orangefs_bufmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	__orangefs_bufmap = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	spin_unlock(&orangefs_bufmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	orangefs_bufmap_unmap(bufmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	orangefs_bufmap_free(bufmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)  * orangefs_bufmap_get()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)  * gets a free mapped buffer descriptor, will sleep until one becomes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)  * available if necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)  * returns slot on success, -errno on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) int orangefs_bufmap_get(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	return get(&rw_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)  * orangefs_bufmap_put()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)  * returns a mapped buffer descriptor to the collection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)  * no return value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) void orangefs_bufmap_put(int buffer_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	put(&rw_map, buffer_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)  * orangefs_readdir_index_get()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)  * gets a free descriptor, will sleep until one becomes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)  * available if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)  * Although the readdir buffers are not mapped into kernel space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)  * we could do that at a later point of time. Regardless, these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)  * indices are used by the client-core.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)  * returns slot on success, -errno on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) int orangefs_readdir_index_get(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	return get(&readdir_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) void orangefs_readdir_index_put(int buffer_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	put(&readdir_map, buffer_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)  * we've been handed an iovec, we need to copy it to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)  * the shared memory descriptor at "buffer_index".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) int orangefs_bufmap_copy_from_iovec(struct iov_iter *iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 				int buffer_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 				size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	struct orangefs_bufmap_desc *to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	gossip_debug(GOSSIP_BUFMAP_DEBUG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		     "%s: buffer_index:%d: size:%zu:\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		     __func__, buffer_index, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	to = &__orangefs_bufmap->desc_array[buffer_index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	for (i = 0; size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		struct page *page = to->page_array[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		size_t n = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		if (n > PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 			n = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		if (copy_page_from_iter(page, 0, n, iter) != n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		size -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)  * we've been handed an iovec, we need to fill it from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)  * the shared memory descriptor at "buffer_index".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) int orangefs_bufmap_copy_to_iovec(struct iov_iter *iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 				    int buffer_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 				    size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	struct orangefs_bufmap_desc *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	from = &__orangefs_bufmap->desc_array[buffer_index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	gossip_debug(GOSSIP_BUFMAP_DEBUG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		     "%s: buffer_index:%d: size:%zu:\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		     __func__, buffer_index, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	for (i = 0; size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		struct page *page = from->page_array[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		size_t n = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		if (n > PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 			n = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		n = copy_page_to_iter(page, 0, n, iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		if (!n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		size -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) void orangefs_bufmap_page_fill(void *page_to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 				int buffer_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 				int slot_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	struct orangefs_bufmap_desc *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	void *page_from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	from = &__orangefs_bufmap->desc_array[buffer_index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	page_from = kmap_atomic(from->page_array[slot_index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	memcpy(page_to, page_from, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	kunmap_atomic(page_from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) }