orangefs-bufmap.c 13 KB

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