pagelist.c 13 KB

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