pagelist.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. #define NFS_PARANOIA 1
  20. static kmem_cache_t *nfs_page_cachep;
  21. static inline struct nfs_page *
  22. nfs_page_alloc(void)
  23. {
  24. struct nfs_page *p;
  25. p = kmem_cache_alloc(nfs_page_cachep, SLAB_KERNEL);
  26. if (p) {
  27. memset(p, 0, sizeof(*p));
  28. INIT_LIST_HEAD(&p->wb_list);
  29. }
  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. * @file: file descriptor 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, and avoids
  47. * a possible deadlock when we reach the hard limit on the number
  48. * of dirty pages.
  49. * User should ensure it is safe to sleep in this function.
  50. */
  51. struct nfs_page *
  52. nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
  53. struct page *page,
  54. unsigned int offset, unsigned int count)
  55. {
  56. struct nfs_server *server = NFS_SERVER(inode);
  57. struct nfs_page *req;
  58. /* Deal with hard limits. */
  59. for (;;) {
  60. /* try to allocate the request struct */
  61. req = nfs_page_alloc();
  62. if (req != NULL)
  63. break;
  64. /* Try to free up at least one request in order to stay
  65. * below the hard limit
  66. */
  67. if (signalled() && (server->flags & NFS_MOUNT_INTR))
  68. return ERR_PTR(-ERESTARTSYS);
  69. yield();
  70. }
  71. /* Initialize the request struct. Initially, we assume a
  72. * long write-back delay. This will be adjusted in
  73. * update_nfs_request below if the region is not locked. */
  74. req->wb_page = page;
  75. atomic_set(&req->wb_complete, 0);
  76. req->wb_index = page->index;
  77. page_cache_get(page);
  78. BUG_ON(PagePrivate(page));
  79. BUG_ON(!PageLocked(page));
  80. BUG_ON(page->mapping->host != inode);
  81. req->wb_offset = offset;
  82. req->wb_pgbase = offset;
  83. req->wb_bytes = count;
  84. atomic_set(&req->wb_count, 1);
  85. req->wb_context = get_nfs_open_context(ctx);
  86. return req;
  87. }
  88. /**
  89. * nfs_unlock_request - Unlock request and wake up sleepers.
  90. * @req:
  91. */
  92. void nfs_unlock_request(struct nfs_page *req)
  93. {
  94. if (!NFS_WBACK_BUSY(req)) {
  95. printk(KERN_ERR "NFS: Invalid unlock attempted\n");
  96. BUG();
  97. }
  98. smp_mb__before_clear_bit();
  99. clear_bit(PG_BUSY, &req->wb_flags);
  100. smp_mb__after_clear_bit();
  101. wake_up_bit(&req->wb_flags, PG_BUSY);
  102. nfs_release_request(req);
  103. }
  104. /**
  105. * nfs_set_page_writeback_locked - Lock a request for writeback
  106. * @req:
  107. */
  108. int nfs_set_page_writeback_locked(struct nfs_page *req)
  109. {
  110. struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
  111. if (!nfs_lock_request(req))
  112. return 0;
  113. radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
  114. return 1;
  115. }
  116. /**
  117. * nfs_clear_page_writeback - Unlock request and wake up sleepers
  118. */
  119. void nfs_clear_page_writeback(struct nfs_page *req)
  120. {
  121. struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
  122. if (req->wb_page != NULL) {
  123. spin_lock(&nfsi->req_lock);
  124. radix_tree_tag_clear(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
  125. spin_unlock(&nfsi->req_lock);
  126. }
  127. nfs_unlock_request(req);
  128. }
  129. /**
  130. * nfs_clear_request - Free up all resources allocated to the request
  131. * @req:
  132. *
  133. * Release page resources associated with a write request after it
  134. * has completed.
  135. */
  136. void nfs_clear_request(struct nfs_page *req)
  137. {
  138. struct page *page = req->wb_page;
  139. if (page != NULL) {
  140. page_cache_release(page);
  141. req->wb_page = NULL;
  142. }
  143. }
  144. /**
  145. * nfs_release_request - Release the count on an NFS read/write request
  146. * @req: request to release
  147. *
  148. * Note: Should never be called with the spinlock held!
  149. */
  150. void
  151. nfs_release_request(struct nfs_page *req)
  152. {
  153. if (!atomic_dec_and_test(&req->wb_count))
  154. return;
  155. #ifdef NFS_PARANOIA
  156. BUG_ON (!list_empty(&req->wb_list));
  157. BUG_ON (NFS_WBACK_BUSY(req));
  158. #endif
  159. /* Release struct file or cached credential */
  160. nfs_clear_request(req);
  161. put_nfs_open_context(req->wb_context);
  162. nfs_page_free(req);
  163. }
  164. static int nfs_wait_bit_interruptible(void *word)
  165. {
  166. int ret = 0;
  167. if (signal_pending(current))
  168. ret = -ERESTARTSYS;
  169. else
  170. schedule();
  171. return ret;
  172. }
  173. /**
  174. * nfs_wait_on_request - Wait for a request to complete.
  175. * @req: request to wait upon.
  176. *
  177. * Interruptible by signals only if mounted with intr flag.
  178. * The user is responsible for holding a count on the request.
  179. */
  180. int
  181. nfs_wait_on_request(struct nfs_page *req)
  182. {
  183. struct rpc_clnt *clnt = NFS_CLIENT(req->wb_context->dentry->d_inode);
  184. sigset_t oldmask;
  185. int ret = 0;
  186. if (!test_bit(PG_BUSY, &req->wb_flags))
  187. goto out;
  188. /*
  189. * Note: the call to rpc_clnt_sigmask() suffices to ensure that we
  190. * are not interrupted if intr flag is not set
  191. */
  192. rpc_clnt_sigmask(clnt, &oldmask);
  193. ret = out_of_line_wait_on_bit(&req->wb_flags, PG_BUSY,
  194. nfs_wait_bit_interruptible, TASK_INTERRUPTIBLE);
  195. rpc_clnt_sigunmask(clnt, &oldmask);
  196. out:
  197. return ret;
  198. }
  199. /**
  200. * nfs_coalesce_requests - Split coalesced requests out from a list.
  201. * @head: source list
  202. * @dst: destination list
  203. * @nmax: maximum number of requests to coalesce
  204. *
  205. * Moves a maximum of 'nmax' elements from one list to another.
  206. * The elements are checked to ensure that they form a contiguous set
  207. * of pages, and that the RPC credentials are the same.
  208. */
  209. int
  210. nfs_coalesce_requests(struct list_head *head, struct list_head *dst,
  211. unsigned int nmax)
  212. {
  213. struct nfs_page *req = NULL;
  214. unsigned int npages = 0;
  215. while (!list_empty(head)) {
  216. struct nfs_page *prev = req;
  217. req = nfs_list_entry(head->next);
  218. if (prev) {
  219. if (req->wb_context->cred != prev->wb_context->cred)
  220. break;
  221. if (req->wb_context->lockowner != prev->wb_context->lockowner)
  222. break;
  223. if (req->wb_context->state != prev->wb_context->state)
  224. break;
  225. if (req->wb_index != (prev->wb_index + 1))
  226. break;
  227. if (req->wb_pgbase != 0)
  228. break;
  229. }
  230. nfs_list_remove_request(req);
  231. nfs_list_add_request(req, dst);
  232. npages++;
  233. if (req->wb_pgbase + req->wb_bytes != PAGE_CACHE_SIZE)
  234. break;
  235. if (npages >= nmax)
  236. break;
  237. }
  238. return npages;
  239. }
  240. #define NFS_SCAN_MAXENTRIES 16
  241. /**
  242. * nfs_scan_lock_dirty - Scan the radix tree for dirty requests
  243. * @nfsi: NFS inode
  244. * @dst: Destination list
  245. * @idx_start: lower bound of page->index to scan
  246. * @npages: idx_start + npages sets the upper bound to scan.
  247. *
  248. * Moves elements from one of the inode request lists.
  249. * If the number of requests is set to 0, the entire address_space
  250. * starting at index idx_start, is scanned.
  251. * The requests are *not* checked to ensure that they form a contiguous set.
  252. * You must be holding the inode's req_lock when calling this function
  253. */
  254. int
  255. nfs_scan_lock_dirty(struct nfs_inode *nfsi, struct list_head *dst,
  256. unsigned long idx_start, unsigned int npages)
  257. {
  258. struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
  259. struct nfs_page *req;
  260. unsigned long idx_end;
  261. int found, i;
  262. int res;
  263. res = 0;
  264. if (npages == 0)
  265. idx_end = ~0;
  266. else
  267. idx_end = idx_start + npages - 1;
  268. for (;;) {
  269. found = radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree,
  270. (void **)&pgvec[0], idx_start, NFS_SCAN_MAXENTRIES,
  271. NFS_PAGE_TAG_DIRTY);
  272. if (found <= 0)
  273. break;
  274. for (i = 0; i < found; i++) {
  275. req = pgvec[i];
  276. if (req->wb_index > idx_end)
  277. goto out;
  278. idx_start = req->wb_index + 1;
  279. if (nfs_set_page_writeback_locked(req)) {
  280. radix_tree_tag_clear(&nfsi->nfs_page_tree,
  281. req->wb_index, NFS_PAGE_TAG_DIRTY);
  282. nfs_list_remove_request(req);
  283. nfs_list_add_request(req, dst);
  284. dec_zone_page_state(req->wb_page, NR_FILE_DIRTY);
  285. res++;
  286. }
  287. }
  288. }
  289. out:
  290. return res;
  291. }
  292. /**
  293. * nfs_scan_list - Scan a list for matching requests
  294. * @nfsi: NFS inode
  295. * @head: One of the NFS inode request lists
  296. * @dst: Destination list
  297. * @idx_start: lower bound of page->index to scan
  298. * @npages: idx_start + npages sets the upper bound to scan.
  299. *
  300. * Moves elements from one of the inode request lists.
  301. * If the number of requests is set to 0, the entire address_space
  302. * starting at index idx_start, is scanned.
  303. * The requests are *not* checked to ensure that they form a contiguous set.
  304. * You must be holding the inode's req_lock when calling this function
  305. */
  306. int nfs_scan_list(struct nfs_inode *nfsi, struct list_head *head,
  307. struct list_head *dst, unsigned long idx_start,
  308. unsigned int npages)
  309. {
  310. struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
  311. struct nfs_page *req;
  312. unsigned long idx_end;
  313. int found, i;
  314. int res;
  315. res = 0;
  316. if (npages == 0)
  317. idx_end = ~0;
  318. else
  319. idx_end = idx_start + npages - 1;
  320. for (;;) {
  321. found = radix_tree_gang_lookup(&nfsi->nfs_page_tree,
  322. (void **)&pgvec[0], idx_start,
  323. NFS_SCAN_MAXENTRIES);
  324. if (found <= 0)
  325. break;
  326. for (i = 0; i < found; i++) {
  327. req = pgvec[i];
  328. if (req->wb_index > idx_end)
  329. goto out;
  330. idx_start = req->wb_index + 1;
  331. if (req->wb_list_head != head)
  332. continue;
  333. if (nfs_set_page_writeback_locked(req)) {
  334. nfs_list_remove_request(req);
  335. nfs_list_add_request(req, dst);
  336. res++;
  337. }
  338. }
  339. }
  340. out:
  341. return res;
  342. }
  343. int __init nfs_init_nfspagecache(void)
  344. {
  345. nfs_page_cachep = kmem_cache_create("nfs_page",
  346. sizeof(struct nfs_page),
  347. 0, SLAB_HWCACHE_ALIGN,
  348. NULL, NULL);
  349. if (nfs_page_cachep == NULL)
  350. return -ENOMEM;
  351. return 0;
  352. }
  353. void nfs_destroy_nfspagecache(void)
  354. {
  355. kmem_cache_destroy(nfs_page_cachep);
  356. }