pagelist.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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/sunrpc/clnt.h>
  14. #include <linux/nfs3.h>
  15. #include <linux/nfs4.h>
  16. #include <linux/nfs_page.h>
  17. #include <linux/nfs_fs.h>
  18. #include <linux/nfs_mount.h>
  19. #include <linux/writeback.h>
  20. #define NFS_PARANOIA 1
  21. static struct kmem_cache *nfs_page_cachep;
  22. static inline struct nfs_page *
  23. nfs_page_alloc(void)
  24. {
  25. struct nfs_page *p;
  26. p = kmem_cache_alloc(nfs_page_cachep, GFP_KERNEL);
  27. if (p) {
  28. memset(p, 0, sizeof(*p));
  29. INIT_LIST_HEAD(&p->wb_list);
  30. }
  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. * @file: file descriptor 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, and avoids
  48. * a possible deadlock when we reach the hard limit on the number
  49. * of dirty pages.
  50. * User should ensure it is safe to sleep in this function.
  51. */
  52. struct nfs_page *
  53. nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
  54. struct page *page,
  55. unsigned int offset, unsigned int count)
  56. {
  57. struct nfs_server *server = NFS_SERVER(inode);
  58. struct nfs_page *req;
  59. /* Deal with hard limits. */
  60. for (;;) {
  61. /* try to allocate the request struct */
  62. req = nfs_page_alloc();
  63. if (req != NULL)
  64. break;
  65. /* Try to free up at least one request in order to stay
  66. * below the hard limit
  67. */
  68. if (signalled() && (server->flags & NFS_MOUNT_INTR))
  69. return ERR_PTR(-ERESTARTSYS);
  70. yield();
  71. }
  72. /* Initialize the request struct. Initially, we assume a
  73. * long write-back delay. This will be adjusted in
  74. * update_nfs_request below if the region is not locked. */
  75. req->wb_page = page;
  76. atomic_set(&req->wb_complete, 0);
  77. req->wb_index = page->index;
  78. page_cache_get(page);
  79. BUG_ON(PagePrivate(page));
  80. BUG_ON(!PageLocked(page));
  81. BUG_ON(page->mapping->host != inode);
  82. req->wb_offset = offset;
  83. req->wb_pgbase = offset;
  84. req->wb_bytes = count;
  85. atomic_set(&req->wb_count, 1);
  86. req->wb_context = get_nfs_open_context(ctx);
  87. return req;
  88. }
  89. /**
  90. * nfs_unlock_request - Unlock request and wake up sleepers.
  91. * @req:
  92. */
  93. void nfs_unlock_request(struct nfs_page *req)
  94. {
  95. if (!NFS_WBACK_BUSY(req)) {
  96. printk(KERN_ERR "NFS: Invalid unlock attempted\n");
  97. BUG();
  98. }
  99. smp_mb__before_clear_bit();
  100. clear_bit(PG_BUSY, &req->wb_flags);
  101. smp_mb__after_clear_bit();
  102. wake_up_bit(&req->wb_flags, PG_BUSY);
  103. nfs_release_request(req);
  104. }
  105. /**
  106. * nfs_set_page_writeback_locked - Lock a request for writeback
  107. * @req:
  108. */
  109. int nfs_set_page_writeback_locked(struct nfs_page *req)
  110. {
  111. struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
  112. if (!nfs_lock_request(req))
  113. return 0;
  114. radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
  115. return 1;
  116. }
  117. /**
  118. * nfs_clear_page_writeback - Unlock request and wake up sleepers
  119. */
  120. void nfs_clear_page_writeback(struct nfs_page *req)
  121. {
  122. struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
  123. if (req->wb_page != NULL) {
  124. spin_lock(&nfsi->req_lock);
  125. radix_tree_tag_clear(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
  126. spin_unlock(&nfsi->req_lock);
  127. }
  128. nfs_unlock_request(req);
  129. }
  130. /**
  131. * nfs_clear_request - Free up all resources allocated to the request
  132. * @req:
  133. *
  134. * Release page resources associated with a write request after it
  135. * has completed.
  136. */
  137. void nfs_clear_request(struct nfs_page *req)
  138. {
  139. struct page *page = req->wb_page;
  140. if (page != NULL) {
  141. page_cache_release(page);
  142. req->wb_page = NULL;
  143. }
  144. }
  145. /**
  146. * nfs_release_request - Release the count on an NFS read/write request
  147. * @req: request to release
  148. *
  149. * Note: Should never be called with the spinlock held!
  150. */
  151. void
  152. nfs_release_request(struct nfs_page *req)
  153. {
  154. if (!atomic_dec_and_test(&req->wb_count))
  155. return;
  156. #ifdef NFS_PARANOIA
  157. BUG_ON (!list_empty(&req->wb_list));
  158. BUG_ON (NFS_WBACK_BUSY(req));
  159. #endif
  160. /* Release struct file or cached credential */
  161. nfs_clear_request(req);
  162. put_nfs_open_context(req->wb_context);
  163. nfs_page_free(req);
  164. }
  165. static int nfs_wait_bit_interruptible(void *word)
  166. {
  167. int ret = 0;
  168. if (signal_pending(current))
  169. ret = -ERESTARTSYS;
  170. else
  171. schedule();
  172. return ret;
  173. }
  174. /**
  175. * nfs_wait_on_request - Wait for a request to complete.
  176. * @req: request to wait upon.
  177. *
  178. * Interruptible by signals only if mounted with intr flag.
  179. * The user is responsible for holding a count on the request.
  180. */
  181. int
  182. nfs_wait_on_request(struct nfs_page *req)
  183. {
  184. struct rpc_clnt *clnt = NFS_CLIENT(req->wb_context->dentry->d_inode);
  185. sigset_t oldmask;
  186. int ret = 0;
  187. if (!test_bit(PG_BUSY, &req->wb_flags))
  188. goto out;
  189. /*
  190. * Note: the call to rpc_clnt_sigmask() suffices to ensure that we
  191. * are not interrupted if intr flag is not set
  192. */
  193. rpc_clnt_sigmask(clnt, &oldmask);
  194. ret = out_of_line_wait_on_bit(&req->wb_flags, PG_BUSY,
  195. nfs_wait_bit_interruptible, TASK_INTERRUPTIBLE);
  196. rpc_clnt_sigunmask(clnt, &oldmask);
  197. out:
  198. return ret;
  199. }
  200. /**
  201. * nfs_pageio_init - initialise a page io descriptor
  202. * @desc: pointer to descriptor
  203. * @iosize: io block size
  204. */
  205. void nfs_pageio_init(struct nfs_pageio_descriptor *desc, unsigned int bsize)
  206. {
  207. INIT_LIST_HEAD(&desc->pg_list);
  208. desc->pg_count = 0;
  209. desc->pg_bsize = bsize;
  210. desc->pg_base = 0;
  211. }
  212. /**
  213. * nfs_can_coalesce_requests - test two requests for compatibility
  214. * @prev: pointer to nfs_page
  215. * @req: pointer to nfs_page
  216. *
  217. * The nfs_page structures 'prev' and 'req' are compared to ensure that the
  218. * page data area they describe is contiguous, and that their RPC
  219. * credentials, NFSv4 open state, and lockowners are the same.
  220. *
  221. * Return 'true' if this is the case, else return 'false'.
  222. */
  223. static int nfs_can_coalesce_requests(struct nfs_page *prev,
  224. struct nfs_page *req)
  225. {
  226. if (req->wb_context->cred != prev->wb_context->cred)
  227. return 0;
  228. if (req->wb_context->lockowner != prev->wb_context->lockowner)
  229. return 0;
  230. if (req->wb_context->state != prev->wb_context->state)
  231. return 0;
  232. if (req->wb_index != (prev->wb_index + 1))
  233. return 0;
  234. if (req->wb_pgbase != 0)
  235. return 0;
  236. if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
  237. return 0;
  238. return 1;
  239. }
  240. /**
  241. * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
  242. * @desc: destination io descriptor
  243. * @req: request
  244. *
  245. * Returns true if the request 'req' was successfully coalesced into the
  246. * existing list of pages 'desc'.
  247. */
  248. static int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  249. struct nfs_page *req)
  250. {
  251. size_t newlen = req->wb_bytes;
  252. if (desc->pg_count != 0) {
  253. struct nfs_page *prev;
  254. /*
  255. * FIXME: ideally we should be able to coalesce all requests
  256. * that are not block boundary aligned, but currently this
  257. * is problematic for the case of bsize < PAGE_CACHE_SIZE,
  258. * since nfs_flush_multi and nfs_pagein_multi assume you
  259. * can have only one struct nfs_page.
  260. */
  261. newlen += desc->pg_count;
  262. if (desc->pg_base + newlen > desc->pg_bsize)
  263. return 0;
  264. prev = nfs_list_entry(desc->pg_list.prev);
  265. if (!nfs_can_coalesce_requests(prev, req))
  266. return 0;
  267. } else
  268. desc->pg_base = req->wb_pgbase;
  269. nfs_list_remove_request(req);
  270. nfs_list_add_request(req, &desc->pg_list);
  271. desc->pg_count = newlen;
  272. return 1;
  273. }
  274. /**
  275. * nfs_pageio_add_list - Split coalesced requests out from a list.
  276. * @desc: destination io descriptor
  277. * @head: source list
  278. *
  279. * Moves a maximum of 'nmax' elements from one list to another.
  280. * The elements are checked to ensure that they form a contiguous set
  281. * of pages, and that the RPC credentials are the same.
  282. */
  283. void nfs_pageio_add_list(struct nfs_pageio_descriptor *desc,
  284. struct list_head *head)
  285. {
  286. while (!list_empty(head)) {
  287. struct nfs_page *req = nfs_list_entry(head->next);
  288. if (!nfs_pageio_add_request(desc, req))
  289. break;
  290. }
  291. }
  292. #define NFS_SCAN_MAXENTRIES 16
  293. /**
  294. * nfs_scan_dirty - Scan the radix tree for dirty requests
  295. * @mapping: pointer to address space
  296. * @wbc: writeback_control structure
  297. * @dst: Destination list
  298. *
  299. * Moves elements from one of the inode request lists.
  300. * If the number of requests is set to 0, the entire address_space
  301. * starting at index idx_start, is scanned.
  302. * The requests are *not* checked to ensure that they form a contiguous set.
  303. * You must be holding the inode's req_lock when calling this function
  304. */
  305. long nfs_scan_dirty(struct address_space *mapping,
  306. struct writeback_control *wbc,
  307. struct list_head *dst)
  308. {
  309. struct nfs_inode *nfsi = NFS_I(mapping->host);
  310. struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
  311. struct nfs_page *req;
  312. pgoff_t idx_start, idx_end;
  313. long res = 0;
  314. int found, i;
  315. if (nfsi->ndirty == 0)
  316. return 0;
  317. if (wbc->range_cyclic) {
  318. idx_start = 0;
  319. idx_end = ULONG_MAX;
  320. } else if (wbc->range_end == 0) {
  321. idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
  322. idx_end = ULONG_MAX;
  323. } else {
  324. idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
  325. idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
  326. }
  327. for (;;) {
  328. unsigned int toscan = NFS_SCAN_MAXENTRIES;
  329. found = radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree,
  330. (void **)&pgvec[0], idx_start, toscan,
  331. NFS_PAGE_TAG_DIRTY);
  332. /* Did we make progress? */
  333. if (found <= 0)
  334. break;
  335. for (i = 0; i < found; i++) {
  336. req = pgvec[i];
  337. if (!wbc->range_cyclic && req->wb_index > idx_end)
  338. goto out;
  339. /* Try to lock request and mark it for writeback */
  340. if (!nfs_set_page_writeback_locked(req))
  341. goto next;
  342. radix_tree_tag_clear(&nfsi->nfs_page_tree,
  343. req->wb_index, NFS_PAGE_TAG_DIRTY);
  344. nfsi->ndirty--;
  345. nfs_list_remove_request(req);
  346. nfs_list_add_request(req, dst);
  347. res++;
  348. if (res == LONG_MAX)
  349. goto out;
  350. next:
  351. idx_start = req->wb_index + 1;
  352. }
  353. }
  354. out:
  355. WARN_ON ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty));
  356. return res;
  357. }
  358. /**
  359. * nfs_scan_list - Scan a list for matching requests
  360. * @nfsi: NFS inode
  361. * @head: One of the NFS inode request lists
  362. * @dst: Destination list
  363. * @idx_start: lower bound of page->index to scan
  364. * @npages: idx_start + npages sets the upper bound to scan.
  365. *
  366. * Moves elements from one of the inode request lists.
  367. * If the number of requests is set to 0, the entire address_space
  368. * starting at index idx_start, is scanned.
  369. * The requests are *not* checked to ensure that they form a contiguous set.
  370. * You must be holding the inode's req_lock when calling this function
  371. */
  372. int nfs_scan_list(struct nfs_inode *nfsi, struct list_head *head,
  373. struct list_head *dst, unsigned long idx_start,
  374. unsigned int npages)
  375. {
  376. struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
  377. struct nfs_page *req;
  378. unsigned long idx_end;
  379. int found, i;
  380. int res;
  381. res = 0;
  382. if (npages == 0)
  383. idx_end = ~0;
  384. else
  385. idx_end = idx_start + npages - 1;
  386. for (;;) {
  387. found = radix_tree_gang_lookup(&nfsi->nfs_page_tree,
  388. (void **)&pgvec[0], idx_start,
  389. NFS_SCAN_MAXENTRIES);
  390. if (found <= 0)
  391. break;
  392. for (i = 0; i < found; i++) {
  393. req = pgvec[i];
  394. if (req->wb_index > idx_end)
  395. goto out;
  396. idx_start = req->wb_index + 1;
  397. if (req->wb_list_head != head)
  398. continue;
  399. if (nfs_set_page_writeback_locked(req)) {
  400. nfs_list_remove_request(req);
  401. nfs_list_add_request(req, dst);
  402. res++;
  403. }
  404. }
  405. }
  406. out:
  407. return res;
  408. }
  409. int __init nfs_init_nfspagecache(void)
  410. {
  411. nfs_page_cachep = kmem_cache_create("nfs_page",
  412. sizeof(struct nfs_page),
  413. 0, SLAB_HWCACHE_ALIGN,
  414. NULL, NULL);
  415. if (nfs_page_cachep == NULL)
  416. return -ENOMEM;
  417. return 0;
  418. }
  419. void nfs_destroy_nfspagecache(void)
  420. {
  421. kmem_cache_destroy(nfs_page_cachep);
  422. }