pagelist.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * linux/fs/nfs/pagelist.c
  3. *
  4. * A set of helper functions for managing NFS read and write requests.
  5. * The main purpose of these routines is to provide support for the
  6. * coalescing of several requests into a single RPC call.
  7. *
  8. * Copyright 2000, 2001 (c) Trond Myklebust <trond.myklebust@fys.uio.no>
  9. *
  10. */
  11. #include <linux/slab.h>
  12. #include <linux/file.h>
  13. #include <linux/sched.h>
  14. #include <linux/sunrpc/clnt.h>
  15. #include <linux/nfs.h>
  16. #include <linux/nfs3.h>
  17. #include <linux/nfs4.h>
  18. #include <linux/nfs_page.h>
  19. #include <linux/nfs_fs.h>
  20. #include <linux/nfs_mount.h>
  21. #include <linux/export.h>
  22. #include "internal.h"
  23. #include "pnfs.h"
  24. static struct kmem_cache *nfs_page_cachep;
  25. bool nfs_pgarray_set(struct nfs_page_array *p, unsigned int pagecount)
  26. {
  27. p->npages = pagecount;
  28. if (pagecount <= ARRAY_SIZE(p->page_array))
  29. p->pagevec = p->page_array;
  30. else {
  31. p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL);
  32. if (!p->pagevec)
  33. p->npages = 0;
  34. }
  35. return p->pagevec != NULL;
  36. }
  37. void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
  38. struct nfs_pgio_header *hdr,
  39. void (*release)(struct nfs_pgio_header *hdr))
  40. {
  41. hdr->req = nfs_list_entry(desc->pg_list.next);
  42. hdr->inode = desc->pg_inode;
  43. hdr->cred = hdr->req->wb_context->cred;
  44. hdr->io_start = req_offset(hdr->req);
  45. hdr->good_bytes = desc->pg_count;
  46. hdr->dreq = desc->pg_dreq;
  47. hdr->layout_private = desc->pg_layout_private;
  48. hdr->release = release;
  49. hdr->completion_ops = desc->pg_completion_ops;
  50. if (hdr->completion_ops->init_hdr)
  51. hdr->completion_ops->init_hdr(hdr);
  52. }
  53. EXPORT_SYMBOL_GPL(nfs_pgheader_init);
  54. void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
  55. {
  56. spin_lock(&hdr->lock);
  57. if (pos < hdr->io_start + hdr->good_bytes) {
  58. set_bit(NFS_IOHDR_ERROR, &hdr->flags);
  59. clear_bit(NFS_IOHDR_EOF, &hdr->flags);
  60. hdr->good_bytes = pos - hdr->io_start;
  61. hdr->error = error;
  62. }
  63. spin_unlock(&hdr->lock);
  64. }
  65. static inline struct nfs_page *
  66. nfs_page_alloc(void)
  67. {
  68. struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO);
  69. if (p)
  70. INIT_LIST_HEAD(&p->wb_list);
  71. return p;
  72. }
  73. static inline void
  74. nfs_page_free(struct nfs_page *p)
  75. {
  76. kmem_cache_free(nfs_page_cachep, p);
  77. }
  78. static void
  79. nfs_iocounter_inc(struct nfs_io_counter *c)
  80. {
  81. atomic_inc(&c->io_count);
  82. }
  83. static void
  84. nfs_iocounter_dec(struct nfs_io_counter *c)
  85. {
  86. if (atomic_dec_and_test(&c->io_count)) {
  87. clear_bit(NFS_IO_INPROGRESS, &c->flags);
  88. smp_mb__after_clear_bit();
  89. wake_up_bit(&c->flags, NFS_IO_INPROGRESS);
  90. }
  91. }
  92. static int
  93. __nfs_iocounter_wait(struct nfs_io_counter *c)
  94. {
  95. wait_queue_head_t *wq = bit_waitqueue(&c->flags, NFS_IO_INPROGRESS);
  96. DEFINE_WAIT_BIT(q, &c->flags, NFS_IO_INPROGRESS);
  97. int ret = 0;
  98. do {
  99. prepare_to_wait(wq, &q.wait, TASK_KILLABLE);
  100. set_bit(NFS_IO_INPROGRESS, &c->flags);
  101. if (atomic_read(&c->io_count) == 0)
  102. break;
  103. ret = nfs_wait_bit_killable(&c->flags);
  104. } while (atomic_read(&c->io_count) != 0);
  105. finish_wait(wq, &q.wait);
  106. return ret;
  107. }
  108. /**
  109. * nfs_iocounter_wait - wait for i/o to complete
  110. * @c: nfs_io_counter to use
  111. *
  112. * returns -ERESTARTSYS if interrupted by a fatal signal.
  113. * Otherwise returns 0 once the io_count hits 0.
  114. */
  115. int
  116. nfs_iocounter_wait(struct nfs_io_counter *c)
  117. {
  118. if (atomic_read(&c->io_count) == 0)
  119. return 0;
  120. return __nfs_iocounter_wait(c);
  121. }
  122. /**
  123. * nfs_create_request - Create an NFS read/write request.
  124. * @ctx: open context to use
  125. * @inode: inode to which the request is attached
  126. * @page: page to write
  127. * @offset: starting offset within the page for the write
  128. * @count: number of bytes to read/write
  129. *
  130. * The page must be locked by the caller. This makes sure we never
  131. * create two different requests for the same page.
  132. * User should ensure it is safe to sleep in this function.
  133. */
  134. struct nfs_page *
  135. nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
  136. struct page *page,
  137. unsigned int offset, unsigned int count)
  138. {
  139. struct nfs_page *req;
  140. struct nfs_lock_context *l_ctx;
  141. if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
  142. return ERR_PTR(-EBADF);
  143. /* try to allocate the request struct */
  144. req = nfs_page_alloc();
  145. if (req == NULL)
  146. return ERR_PTR(-ENOMEM);
  147. /* get lock context early so we can deal with alloc failures */
  148. l_ctx = nfs_get_lock_context(ctx);
  149. if (IS_ERR(l_ctx)) {
  150. nfs_page_free(req);
  151. return ERR_CAST(l_ctx);
  152. }
  153. req->wb_lock_context = l_ctx;
  154. nfs_iocounter_inc(&l_ctx->io_count);
  155. /* Initialize the request struct. Initially, we assume a
  156. * long write-back delay. This will be adjusted in
  157. * update_nfs_request below if the region is not locked. */
  158. req->wb_page = page;
  159. req->wb_index = page_file_index(page);
  160. page_cache_get(page);
  161. req->wb_offset = offset;
  162. req->wb_pgbase = offset;
  163. req->wb_bytes = count;
  164. req->wb_context = get_nfs_open_context(ctx);
  165. kref_init(&req->wb_kref);
  166. return req;
  167. }
  168. /**
  169. * nfs_unlock_request - Unlock request and wake up sleepers.
  170. * @req:
  171. */
  172. void nfs_unlock_request(struct nfs_page *req)
  173. {
  174. if (!NFS_WBACK_BUSY(req)) {
  175. printk(KERN_ERR "NFS: Invalid unlock attempted\n");
  176. BUG();
  177. }
  178. smp_mb__before_clear_bit();
  179. clear_bit(PG_BUSY, &req->wb_flags);
  180. smp_mb__after_clear_bit();
  181. wake_up_bit(&req->wb_flags, PG_BUSY);
  182. }
  183. /**
  184. * nfs_unlock_and_release_request - Unlock request and release the nfs_page
  185. * @req:
  186. */
  187. void nfs_unlock_and_release_request(struct nfs_page *req)
  188. {
  189. nfs_unlock_request(req);
  190. nfs_release_request(req);
  191. }
  192. /*
  193. * nfs_clear_request - Free up all resources allocated to the request
  194. * @req:
  195. *
  196. * Release page and open context resources associated with a read/write
  197. * request after it has completed.
  198. */
  199. static void nfs_clear_request(struct nfs_page *req)
  200. {
  201. struct page *page = req->wb_page;
  202. struct nfs_open_context *ctx = req->wb_context;
  203. struct nfs_lock_context *l_ctx = req->wb_lock_context;
  204. if (page != NULL) {
  205. page_cache_release(page);
  206. req->wb_page = NULL;
  207. }
  208. if (l_ctx != NULL) {
  209. nfs_iocounter_dec(&l_ctx->io_count);
  210. nfs_put_lock_context(l_ctx);
  211. req->wb_lock_context = NULL;
  212. }
  213. if (ctx != NULL) {
  214. put_nfs_open_context(ctx);
  215. req->wb_context = NULL;
  216. }
  217. }
  218. /**
  219. * nfs_release_request - Release the count on an NFS read/write request
  220. * @req: request to release
  221. *
  222. * Note: Should never be called with the spinlock held!
  223. */
  224. static void nfs_free_request(struct kref *kref)
  225. {
  226. struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
  227. /* Release struct file and open context */
  228. nfs_clear_request(req);
  229. nfs_page_free(req);
  230. }
  231. void nfs_release_request(struct nfs_page *req)
  232. {
  233. kref_put(&req->wb_kref, nfs_free_request);
  234. }
  235. static int nfs_wait_bit_uninterruptible(void *word)
  236. {
  237. io_schedule();
  238. return 0;
  239. }
  240. /**
  241. * nfs_wait_on_request - Wait for a request to complete.
  242. * @req: request to wait upon.
  243. *
  244. * Interruptible by fatal signals only.
  245. * The user is responsible for holding a count on the request.
  246. */
  247. int
  248. nfs_wait_on_request(struct nfs_page *req)
  249. {
  250. return wait_on_bit(&req->wb_flags, PG_BUSY,
  251. nfs_wait_bit_uninterruptible,
  252. TASK_UNINTERRUPTIBLE);
  253. }
  254. bool nfs_generic_pg_test(struct nfs_pageio_descriptor *desc, struct nfs_page *prev, struct nfs_page *req)
  255. {
  256. /*
  257. * FIXME: ideally we should be able to coalesce all requests
  258. * that are not block boundary aligned, but currently this
  259. * is problematic for the case of bsize < PAGE_CACHE_SIZE,
  260. * since nfs_flush_multi and nfs_pagein_multi assume you
  261. * can have only one struct nfs_page.
  262. */
  263. if (desc->pg_bsize < PAGE_SIZE)
  264. return 0;
  265. return desc->pg_count + req->wb_bytes <= desc->pg_bsize;
  266. }
  267. EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
  268. /**
  269. * nfs_pageio_init - initialise a page io descriptor
  270. * @desc: pointer to descriptor
  271. * @inode: pointer to inode
  272. * @doio: pointer to io function
  273. * @bsize: io block size
  274. * @io_flags: extra parameters for the io function
  275. */
  276. void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
  277. struct inode *inode,
  278. const struct nfs_pageio_ops *pg_ops,
  279. const struct nfs_pgio_completion_ops *compl_ops,
  280. size_t bsize,
  281. int io_flags)
  282. {
  283. INIT_LIST_HEAD(&desc->pg_list);
  284. desc->pg_bytes_written = 0;
  285. desc->pg_count = 0;
  286. desc->pg_bsize = bsize;
  287. desc->pg_base = 0;
  288. desc->pg_moreio = 0;
  289. desc->pg_recoalesce = 0;
  290. desc->pg_inode = inode;
  291. desc->pg_ops = pg_ops;
  292. desc->pg_completion_ops = compl_ops;
  293. desc->pg_ioflags = io_flags;
  294. desc->pg_error = 0;
  295. desc->pg_lseg = NULL;
  296. desc->pg_dreq = NULL;
  297. desc->pg_layout_private = NULL;
  298. }
  299. EXPORT_SYMBOL_GPL(nfs_pageio_init);
  300. static bool nfs_match_open_context(const struct nfs_open_context *ctx1,
  301. const struct nfs_open_context *ctx2)
  302. {
  303. return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state;
  304. }
  305. static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
  306. const struct nfs_lock_context *l2)
  307. {
  308. return l1->lockowner.l_owner == l2->lockowner.l_owner
  309. && l1->lockowner.l_pid == l2->lockowner.l_pid;
  310. }
  311. /**
  312. * nfs_can_coalesce_requests - test two requests for compatibility
  313. * @prev: pointer to nfs_page
  314. * @req: pointer to nfs_page
  315. *
  316. * The nfs_page structures 'prev' and 'req' are compared to ensure that the
  317. * page data area they describe is contiguous, and that their RPC
  318. * credentials, NFSv4 open state, and lockowners are the same.
  319. *
  320. * Return 'true' if this is the case, else return 'false'.
  321. */
  322. static bool nfs_can_coalesce_requests(struct nfs_page *prev,
  323. struct nfs_page *req,
  324. struct nfs_pageio_descriptor *pgio)
  325. {
  326. if (!nfs_match_open_context(req->wb_context, prev->wb_context))
  327. return false;
  328. if (req->wb_context->dentry->d_inode->i_flock != NULL &&
  329. !nfs_match_lock_context(req->wb_lock_context, prev->wb_lock_context))
  330. return false;
  331. if (req->wb_pgbase != 0)
  332. return false;
  333. if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
  334. return false;
  335. if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
  336. return false;
  337. return pgio->pg_ops->pg_test(pgio, prev, req);
  338. }
  339. /**
  340. * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
  341. * @desc: destination io descriptor
  342. * @req: request
  343. *
  344. * Returns true if the request 'req' was successfully coalesced into the
  345. * existing list of pages 'desc'.
  346. */
  347. static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
  348. struct nfs_page *req)
  349. {
  350. if (desc->pg_count != 0) {
  351. struct nfs_page *prev;
  352. prev = nfs_list_entry(desc->pg_list.prev);
  353. if (!nfs_can_coalesce_requests(prev, req, desc))
  354. return 0;
  355. } else {
  356. if (desc->pg_ops->pg_init)
  357. desc->pg_ops->pg_init(desc, req);
  358. desc->pg_base = req->wb_pgbase;
  359. }
  360. nfs_list_remove_request(req);
  361. nfs_list_add_request(req, &desc->pg_list);
  362. desc->pg_count += req->wb_bytes;
  363. return 1;
  364. }
  365. /*
  366. * Helper for nfs_pageio_add_request and nfs_pageio_complete
  367. */
  368. static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
  369. {
  370. if (!list_empty(&desc->pg_list)) {
  371. int error = desc->pg_ops->pg_doio(desc);
  372. if (error < 0)
  373. desc->pg_error = error;
  374. else
  375. desc->pg_bytes_written += desc->pg_count;
  376. }
  377. if (list_empty(&desc->pg_list)) {
  378. desc->pg_count = 0;
  379. desc->pg_base = 0;
  380. }
  381. }
  382. /**
  383. * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
  384. * @desc: destination io descriptor
  385. * @req: request
  386. *
  387. * Returns true if the request 'req' was successfully coalesced into the
  388. * existing list of pages 'desc'.
  389. */
  390. static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  391. struct nfs_page *req)
  392. {
  393. while (!nfs_pageio_do_add_request(desc, req)) {
  394. desc->pg_moreio = 1;
  395. nfs_pageio_doio(desc);
  396. if (desc->pg_error < 0)
  397. return 0;
  398. desc->pg_moreio = 0;
  399. if (desc->pg_recoalesce)
  400. return 0;
  401. }
  402. return 1;
  403. }
  404. static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
  405. {
  406. LIST_HEAD(head);
  407. do {
  408. list_splice_init(&desc->pg_list, &head);
  409. desc->pg_bytes_written -= desc->pg_count;
  410. desc->pg_count = 0;
  411. desc->pg_base = 0;
  412. desc->pg_recoalesce = 0;
  413. while (!list_empty(&head)) {
  414. struct nfs_page *req;
  415. req = list_first_entry(&head, struct nfs_page, wb_list);
  416. nfs_list_remove_request(req);
  417. if (__nfs_pageio_add_request(desc, req))
  418. continue;
  419. if (desc->pg_error < 0)
  420. return 0;
  421. break;
  422. }
  423. } while (desc->pg_recoalesce);
  424. return 1;
  425. }
  426. int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  427. struct nfs_page *req)
  428. {
  429. int ret;
  430. do {
  431. ret = __nfs_pageio_add_request(desc, req);
  432. if (ret)
  433. break;
  434. if (desc->pg_error < 0)
  435. break;
  436. ret = nfs_do_recoalesce(desc);
  437. } while (ret);
  438. return ret;
  439. }
  440. EXPORT_SYMBOL_GPL(nfs_pageio_add_request);
  441. /**
  442. * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
  443. * @desc: pointer to io descriptor
  444. */
  445. void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
  446. {
  447. for (;;) {
  448. nfs_pageio_doio(desc);
  449. if (!desc->pg_recoalesce)
  450. break;
  451. if (!nfs_do_recoalesce(desc))
  452. break;
  453. }
  454. }
  455. EXPORT_SYMBOL_GPL(nfs_pageio_complete);
  456. /**
  457. * nfs_pageio_cond_complete - Conditional I/O completion
  458. * @desc: pointer to io descriptor
  459. * @index: page index
  460. *
  461. * It is important to ensure that processes don't try to take locks
  462. * on non-contiguous ranges of pages as that might deadlock. This
  463. * function should be called before attempting to wait on a locked
  464. * nfs_page. It will complete the I/O if the page index 'index'
  465. * is not contiguous with the existing list of pages in 'desc'.
  466. */
  467. void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
  468. {
  469. if (!list_empty(&desc->pg_list)) {
  470. struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
  471. if (index != prev->wb_index + 1)
  472. nfs_pageio_complete(desc);
  473. }
  474. }
  475. int __init nfs_init_nfspagecache(void)
  476. {
  477. nfs_page_cachep = kmem_cache_create("nfs_page",
  478. sizeof(struct nfs_page),
  479. 0, SLAB_HWCACHE_ALIGN,
  480. NULL);
  481. if (nfs_page_cachep == NULL)
  482. return -ENOMEM;
  483. return 0;
  484. }
  485. void nfs_destroy_nfspagecache(void)
  486. {
  487. kmem_cache_destroy(nfs_page_cachep);
  488. }