pagelist.c 13 KB

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