pagelist.c 11 KB

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