pagelist.c 10 KB

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