pagelist.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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->release = release;
  48. hdr->completion_ops = desc->pg_completion_ops;
  49. if (hdr->completion_ops->init_hdr)
  50. hdr->completion_ops->init_hdr(hdr);
  51. }
  52. EXPORT_SYMBOL_GPL(nfs_pgheader_init);
  53. void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
  54. {
  55. spin_lock(&hdr->lock);
  56. if (pos < hdr->io_start + hdr->good_bytes) {
  57. set_bit(NFS_IOHDR_ERROR, &hdr->flags);
  58. clear_bit(NFS_IOHDR_EOF, &hdr->flags);
  59. hdr->good_bytes = pos - hdr->io_start;
  60. hdr->error = error;
  61. }
  62. spin_unlock(&hdr->lock);
  63. }
  64. static inline struct nfs_page *
  65. nfs_page_alloc(void)
  66. {
  67. struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO);
  68. if (p)
  69. INIT_LIST_HEAD(&p->wb_list);
  70. return p;
  71. }
  72. static inline void
  73. nfs_page_free(struct nfs_page *p)
  74. {
  75. kmem_cache_free(nfs_page_cachep, p);
  76. }
  77. /**
  78. * nfs_create_request - Create an NFS read/write request.
  79. * @ctx: open context to use
  80. * @inode: inode to which the request is attached
  81. * @page: page to write
  82. * @offset: starting offset within the page for the write
  83. * @count: number of bytes to read/write
  84. *
  85. * The page must be locked by the caller. This makes sure we never
  86. * create two different requests for the same page.
  87. * User should ensure it is safe to sleep in this function.
  88. */
  89. struct nfs_page *
  90. nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
  91. struct page *page,
  92. unsigned int offset, unsigned int count)
  93. {
  94. struct nfs_page *req;
  95. /* try to allocate the request struct */
  96. req = nfs_page_alloc();
  97. if (req == NULL)
  98. return ERR_PTR(-ENOMEM);
  99. /* get lock context early so we can deal with alloc failures */
  100. req->wb_lock_context = nfs_get_lock_context(ctx);
  101. if (req->wb_lock_context == NULL) {
  102. nfs_page_free(req);
  103. return ERR_PTR(-ENOMEM);
  104. }
  105. /* Initialize the request struct. Initially, we assume a
  106. * long write-back delay. This will be adjusted in
  107. * update_nfs_request below if the region is not locked. */
  108. req->wb_page = page;
  109. req->wb_index = page_file_index(page);
  110. page_cache_get(page);
  111. req->wb_offset = offset;
  112. req->wb_pgbase = offset;
  113. req->wb_bytes = count;
  114. req->wb_context = get_nfs_open_context(ctx);
  115. kref_init(&req->wb_kref);
  116. return req;
  117. }
  118. /**
  119. * nfs_unlock_request - Unlock request and wake up sleepers.
  120. * @req:
  121. */
  122. void nfs_unlock_request(struct nfs_page *req)
  123. {
  124. if (!NFS_WBACK_BUSY(req)) {
  125. printk(KERN_ERR "NFS: Invalid unlock attempted\n");
  126. BUG();
  127. }
  128. smp_mb__before_clear_bit();
  129. clear_bit(PG_BUSY, &req->wb_flags);
  130. smp_mb__after_clear_bit();
  131. wake_up_bit(&req->wb_flags, PG_BUSY);
  132. }
  133. /**
  134. * nfs_unlock_and_release_request - Unlock request and release the nfs_page
  135. * @req:
  136. */
  137. void nfs_unlock_and_release_request(struct nfs_page *req)
  138. {
  139. nfs_unlock_request(req);
  140. nfs_release_request(req);
  141. }
  142. /*
  143. * nfs_clear_request - Free up all resources allocated to the request
  144. * @req:
  145. *
  146. * Release page and open context resources associated with a read/write
  147. * request after it has completed.
  148. */
  149. static void nfs_clear_request(struct nfs_page *req)
  150. {
  151. struct page *page = req->wb_page;
  152. struct nfs_open_context *ctx = req->wb_context;
  153. struct nfs_lock_context *l_ctx = req->wb_lock_context;
  154. if (page != NULL) {
  155. page_cache_release(page);
  156. req->wb_page = NULL;
  157. }
  158. if (l_ctx != NULL) {
  159. nfs_put_lock_context(l_ctx);
  160. req->wb_lock_context = NULL;
  161. }
  162. if (ctx != NULL) {
  163. put_nfs_open_context(ctx);
  164. req->wb_context = NULL;
  165. }
  166. }
  167. /**
  168. * nfs_release_request - Release the count on an NFS read/write request
  169. * @req: request to release
  170. *
  171. * Note: Should never be called with the spinlock held!
  172. */
  173. static void nfs_free_request(struct kref *kref)
  174. {
  175. struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
  176. /* Release struct file and open context */
  177. nfs_clear_request(req);
  178. nfs_page_free(req);
  179. }
  180. void nfs_release_request(struct nfs_page *req)
  181. {
  182. kref_put(&req->wb_kref, nfs_free_request);
  183. }
  184. static int nfs_wait_bit_uninterruptible(void *word)
  185. {
  186. io_schedule();
  187. return 0;
  188. }
  189. /**
  190. * nfs_wait_on_request - Wait for a request to complete.
  191. * @req: request to wait upon.
  192. *
  193. * Interruptible by fatal signals only.
  194. * The user is responsible for holding a count on the request.
  195. */
  196. int
  197. nfs_wait_on_request(struct nfs_page *req)
  198. {
  199. return wait_on_bit(&req->wb_flags, PG_BUSY,
  200. nfs_wait_bit_uninterruptible,
  201. TASK_UNINTERRUPTIBLE);
  202. }
  203. bool nfs_generic_pg_test(struct nfs_pageio_descriptor *desc, struct nfs_page *prev, struct nfs_page *req)
  204. {
  205. /*
  206. * FIXME: ideally we should be able to coalesce all requests
  207. * that are not block boundary aligned, but currently this
  208. * is problematic for the case of bsize < PAGE_CACHE_SIZE,
  209. * since nfs_flush_multi and nfs_pagein_multi assume you
  210. * can have only one struct nfs_page.
  211. */
  212. if (desc->pg_bsize < PAGE_SIZE)
  213. return 0;
  214. return desc->pg_count + req->wb_bytes <= desc->pg_bsize;
  215. }
  216. EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
  217. /**
  218. * nfs_pageio_init - initialise a page io descriptor
  219. * @desc: pointer to descriptor
  220. * @inode: pointer to inode
  221. * @doio: pointer to io function
  222. * @bsize: io block size
  223. * @io_flags: extra parameters for the io function
  224. */
  225. void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
  226. struct inode *inode,
  227. const struct nfs_pageio_ops *pg_ops,
  228. const struct nfs_pgio_completion_ops *compl_ops,
  229. size_t bsize,
  230. int io_flags)
  231. {
  232. INIT_LIST_HEAD(&desc->pg_list);
  233. desc->pg_bytes_written = 0;
  234. desc->pg_count = 0;
  235. desc->pg_bsize = bsize;
  236. desc->pg_base = 0;
  237. desc->pg_moreio = 0;
  238. desc->pg_recoalesce = 0;
  239. desc->pg_inode = inode;
  240. desc->pg_ops = pg_ops;
  241. desc->pg_completion_ops = compl_ops;
  242. desc->pg_ioflags = io_flags;
  243. desc->pg_error = 0;
  244. desc->pg_lseg = NULL;
  245. desc->pg_dreq = NULL;
  246. }
  247. EXPORT_SYMBOL_GPL(nfs_pageio_init);
  248. /**
  249. * nfs_can_coalesce_requests - test two requests for compatibility
  250. * @prev: pointer to nfs_page
  251. * @req: pointer to nfs_page
  252. *
  253. * The nfs_page structures 'prev' and 'req' are compared to ensure that the
  254. * page data area they describe is contiguous, and that their RPC
  255. * credentials, NFSv4 open state, and lockowners are the same.
  256. *
  257. * Return 'true' if this is the case, else return 'false'.
  258. */
  259. static bool nfs_can_coalesce_requests(struct nfs_page *prev,
  260. struct nfs_page *req,
  261. struct nfs_pageio_descriptor *pgio)
  262. {
  263. if (req->wb_context->cred != prev->wb_context->cred)
  264. return false;
  265. if (req->wb_lock_context->lockowner != prev->wb_lock_context->lockowner)
  266. return false;
  267. if (req->wb_context->state != prev->wb_context->state)
  268. return false;
  269. if (req->wb_pgbase != 0)
  270. return false;
  271. if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
  272. return false;
  273. if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
  274. return false;
  275. return pgio->pg_ops->pg_test(pgio, prev, req);
  276. }
  277. /**
  278. * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
  279. * @desc: destination io descriptor
  280. * @req: request
  281. *
  282. * Returns true if the request 'req' was successfully coalesced into the
  283. * existing list of pages 'desc'.
  284. */
  285. static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
  286. struct nfs_page *req)
  287. {
  288. if (desc->pg_count != 0) {
  289. struct nfs_page *prev;
  290. prev = nfs_list_entry(desc->pg_list.prev);
  291. if (!nfs_can_coalesce_requests(prev, req, desc))
  292. return 0;
  293. } else {
  294. if (desc->pg_ops->pg_init)
  295. desc->pg_ops->pg_init(desc, req);
  296. desc->pg_base = req->wb_pgbase;
  297. }
  298. nfs_list_remove_request(req);
  299. nfs_list_add_request(req, &desc->pg_list);
  300. desc->pg_count += req->wb_bytes;
  301. return 1;
  302. }
  303. /*
  304. * Helper for nfs_pageio_add_request and nfs_pageio_complete
  305. */
  306. static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
  307. {
  308. if (!list_empty(&desc->pg_list)) {
  309. int error = desc->pg_ops->pg_doio(desc);
  310. if (error < 0)
  311. desc->pg_error = error;
  312. else
  313. desc->pg_bytes_written += desc->pg_count;
  314. }
  315. if (list_empty(&desc->pg_list)) {
  316. desc->pg_count = 0;
  317. desc->pg_base = 0;
  318. }
  319. }
  320. /**
  321. * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
  322. * @desc: destination io descriptor
  323. * @req: request
  324. *
  325. * Returns true if the request 'req' was successfully coalesced into the
  326. * existing list of pages 'desc'.
  327. */
  328. static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  329. struct nfs_page *req)
  330. {
  331. while (!nfs_pageio_do_add_request(desc, req)) {
  332. desc->pg_moreio = 1;
  333. nfs_pageio_doio(desc);
  334. if (desc->pg_error < 0)
  335. return 0;
  336. desc->pg_moreio = 0;
  337. if (desc->pg_recoalesce)
  338. return 0;
  339. }
  340. return 1;
  341. }
  342. static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
  343. {
  344. LIST_HEAD(head);
  345. do {
  346. list_splice_init(&desc->pg_list, &head);
  347. desc->pg_bytes_written -= desc->pg_count;
  348. desc->pg_count = 0;
  349. desc->pg_base = 0;
  350. desc->pg_recoalesce = 0;
  351. while (!list_empty(&head)) {
  352. struct nfs_page *req;
  353. req = list_first_entry(&head, struct nfs_page, wb_list);
  354. nfs_list_remove_request(req);
  355. if (__nfs_pageio_add_request(desc, req))
  356. continue;
  357. if (desc->pg_error < 0)
  358. return 0;
  359. break;
  360. }
  361. } while (desc->pg_recoalesce);
  362. return 1;
  363. }
  364. int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  365. struct nfs_page *req)
  366. {
  367. int ret;
  368. do {
  369. ret = __nfs_pageio_add_request(desc, req);
  370. if (ret)
  371. break;
  372. if (desc->pg_error < 0)
  373. break;
  374. ret = nfs_do_recoalesce(desc);
  375. } while (ret);
  376. return ret;
  377. }
  378. EXPORT_SYMBOL_GPL(nfs_pageio_add_request);
  379. /**
  380. * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
  381. * @desc: pointer to io descriptor
  382. */
  383. void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
  384. {
  385. for (;;) {
  386. nfs_pageio_doio(desc);
  387. if (!desc->pg_recoalesce)
  388. break;
  389. if (!nfs_do_recoalesce(desc))
  390. break;
  391. }
  392. }
  393. EXPORT_SYMBOL_GPL(nfs_pageio_complete);
  394. /**
  395. * nfs_pageio_cond_complete - Conditional I/O completion
  396. * @desc: pointer to io descriptor
  397. * @index: page index
  398. *
  399. * It is important to ensure that processes don't try to take locks
  400. * on non-contiguous ranges of pages as that might deadlock. This
  401. * function should be called before attempting to wait on a locked
  402. * nfs_page. It will complete the I/O if the page index 'index'
  403. * is not contiguous with the existing list of pages in 'desc'.
  404. */
  405. void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
  406. {
  407. if (!list_empty(&desc->pg_list)) {
  408. struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
  409. if (index != prev->wb_index + 1)
  410. nfs_pageio_complete(desc);
  411. }
  412. }
  413. int __init nfs_init_nfspagecache(void)
  414. {
  415. nfs_page_cachep = kmem_cache_create("nfs_page",
  416. sizeof(struct nfs_page),
  417. 0, SLAB_HWCACHE_ALIGN,
  418. NULL);
  419. if (nfs_page_cachep == NULL)
  420. return -ENOMEM;
  421. return 0;
  422. }
  423. void nfs_destroy_nfspagecache(void)
  424. {
  425. kmem_cache_destroy(nfs_page_cachep);
  426. }