pagelist.c 11 KB

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