pagelist.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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/config.h>
  12. #include <linux/slab.h>
  13. #include <linux/file.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. #define NFS_PARANOIA 1
  21. static kmem_cache_t *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, SLAB_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. req->wb_offset = offset;
  80. req->wb_pgbase = offset;
  81. req->wb_bytes = count;
  82. atomic_set(&req->wb_count, 1);
  83. req->wb_context = get_nfs_open_context(ctx);
  84. return req;
  85. }
  86. /**
  87. * nfs_unlock_request - Unlock request and wake up sleepers.
  88. * @req:
  89. */
  90. void nfs_unlock_request(struct nfs_page *req)
  91. {
  92. if (!NFS_WBACK_BUSY(req)) {
  93. printk(KERN_ERR "NFS: Invalid unlock attempted\n");
  94. BUG();
  95. }
  96. smp_mb__before_clear_bit();
  97. clear_bit(PG_BUSY, &req->wb_flags);
  98. smp_mb__after_clear_bit();
  99. wake_up_bit(&req->wb_flags, PG_BUSY);
  100. nfs_release_request(req);
  101. }
  102. /**
  103. * nfs_set_page_writeback_locked - Lock a request for writeback
  104. * @req:
  105. */
  106. int nfs_set_page_writeback_locked(struct nfs_page *req)
  107. {
  108. struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
  109. if (!nfs_lock_request(req))
  110. return 0;
  111. radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
  112. return 1;
  113. }
  114. /**
  115. * nfs_clear_page_writeback - Unlock request and wake up sleepers
  116. */
  117. void nfs_clear_page_writeback(struct nfs_page *req)
  118. {
  119. struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
  120. spin_lock(&nfsi->req_lock);
  121. radix_tree_tag_clear(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
  122. spin_unlock(&nfsi->req_lock);
  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 resources associated with a write request after it
  130. * has completed.
  131. */
  132. void nfs_clear_request(struct nfs_page *req)
  133. {
  134. if (req->wb_page) {
  135. page_cache_release(req->wb_page);
  136. req->wb_page = NULL;
  137. }
  138. }
  139. /**
  140. * nfs_release_request - Release the count on an NFS read/write request
  141. * @req: request to release
  142. *
  143. * Note: Should never be called with the spinlock held!
  144. */
  145. void
  146. nfs_release_request(struct nfs_page *req)
  147. {
  148. if (!atomic_dec_and_test(&req->wb_count))
  149. return;
  150. #ifdef NFS_PARANOIA
  151. BUG_ON (!list_empty(&req->wb_list));
  152. BUG_ON (NFS_WBACK_BUSY(req));
  153. #endif
  154. /* Release struct file or cached credential */
  155. nfs_clear_request(req);
  156. put_nfs_open_context(req->wb_context);
  157. nfs_page_free(req);
  158. }
  159. /**
  160. * nfs_list_add_request - Insert a request into a sorted list
  161. * @req: request
  162. * @head: head of list into which to insert the request.
  163. *
  164. * Note that the wb_list is sorted by page index in order to facilitate
  165. * coalescing of requests.
  166. * We use an insertion sort that is optimized for the case of appended
  167. * writes.
  168. */
  169. void
  170. nfs_list_add_request(struct nfs_page *req, struct list_head *head)
  171. {
  172. struct list_head *pos;
  173. #ifdef NFS_PARANOIA
  174. if (!list_empty(&req->wb_list)) {
  175. printk(KERN_ERR "NFS: Add to list failed!\n");
  176. BUG();
  177. }
  178. #endif
  179. list_for_each_prev(pos, head) {
  180. struct nfs_page *p = nfs_list_entry(pos);
  181. if (p->wb_index < req->wb_index)
  182. break;
  183. }
  184. list_add(&req->wb_list, pos);
  185. req->wb_list_head = head;
  186. }
  187. static int nfs_wait_bit_interruptible(void *word)
  188. {
  189. int ret = 0;
  190. if (signal_pending(current))
  191. ret = -ERESTARTSYS;
  192. else
  193. schedule();
  194. return ret;
  195. }
  196. /**
  197. * nfs_wait_on_request - Wait for a request to complete.
  198. * @req: request to wait upon.
  199. *
  200. * Interruptible by signals only if mounted with intr flag.
  201. * The user is responsible for holding a count on the request.
  202. */
  203. int
  204. nfs_wait_on_request(struct nfs_page *req)
  205. {
  206. struct rpc_clnt *clnt = NFS_CLIENT(req->wb_context->dentry->d_inode);
  207. sigset_t oldmask;
  208. int ret = 0;
  209. if (!test_bit(PG_BUSY, &req->wb_flags))
  210. goto out;
  211. /*
  212. * Note: the call to rpc_clnt_sigmask() suffices to ensure that we
  213. * are not interrupted if intr flag is not set
  214. */
  215. rpc_clnt_sigmask(clnt, &oldmask);
  216. ret = out_of_line_wait_on_bit(&req->wb_flags, PG_BUSY,
  217. nfs_wait_bit_interruptible, TASK_INTERRUPTIBLE);
  218. rpc_clnt_sigunmask(clnt, &oldmask);
  219. out:
  220. return ret;
  221. }
  222. /**
  223. * nfs_coalesce_requests - Split coalesced requests out from a list.
  224. * @head: source list
  225. * @dst: destination list
  226. * @nmax: maximum number of requests to coalesce
  227. *
  228. * Moves a maximum of 'nmax' elements from one list to another.
  229. * The elements are checked to ensure that they form a contiguous set
  230. * of pages, and that the RPC credentials are the same.
  231. */
  232. int
  233. nfs_coalesce_requests(struct list_head *head, struct list_head *dst,
  234. unsigned int nmax)
  235. {
  236. struct nfs_page *req = NULL;
  237. unsigned int npages = 0;
  238. while (!list_empty(head)) {
  239. struct nfs_page *prev = req;
  240. req = nfs_list_entry(head->next);
  241. if (prev) {
  242. if (req->wb_context->cred != prev->wb_context->cred)
  243. break;
  244. if (req->wb_context->lockowner != prev->wb_context->lockowner)
  245. break;
  246. if (req->wb_context->state != prev->wb_context->state)
  247. break;
  248. if (req->wb_index != (prev->wb_index + 1))
  249. break;
  250. if (req->wb_pgbase != 0)
  251. break;
  252. }
  253. nfs_list_remove_request(req);
  254. nfs_list_add_request(req, dst);
  255. npages++;
  256. if (req->wb_pgbase + req->wb_bytes != PAGE_CACHE_SIZE)
  257. break;
  258. if (npages >= nmax)
  259. break;
  260. }
  261. return npages;
  262. }
  263. /**
  264. * nfs_scan_list - Scan a list for matching requests
  265. * @head: One of the NFS inode request lists
  266. * @dst: Destination list
  267. * @idx_start: lower bound of page->index to scan
  268. * @npages: idx_start + npages sets the upper bound to scan.
  269. *
  270. * Moves elements from one of the inode request lists.
  271. * If the number of requests is set to 0, the entire address_space
  272. * starting at index idx_start, is scanned.
  273. * The requests are *not* checked to ensure that they form a contiguous set.
  274. * You must be holding the inode's req_lock when calling this function
  275. */
  276. int
  277. nfs_scan_list(struct list_head *head, struct list_head *dst,
  278. unsigned long idx_start, unsigned int npages)
  279. {
  280. struct list_head *pos, *tmp;
  281. struct nfs_page *req;
  282. unsigned long idx_end;
  283. int res;
  284. res = 0;
  285. if (npages == 0)
  286. idx_end = ~0;
  287. else
  288. idx_end = idx_start + npages - 1;
  289. list_for_each_safe(pos, tmp, head) {
  290. req = nfs_list_entry(pos);
  291. if (req->wb_index < idx_start)
  292. continue;
  293. if (req->wb_index > idx_end)
  294. break;
  295. if (!nfs_set_page_writeback_locked(req))
  296. continue;
  297. nfs_list_remove_request(req);
  298. nfs_list_add_request(req, dst);
  299. res++;
  300. }
  301. return res;
  302. }
  303. int nfs_init_nfspagecache(void)
  304. {
  305. nfs_page_cachep = kmem_cache_create("nfs_page",
  306. sizeof(struct nfs_page),
  307. 0, SLAB_HWCACHE_ALIGN,
  308. NULL, NULL);
  309. if (nfs_page_cachep == NULL)
  310. return -ENOMEM;
  311. return 0;
  312. }
  313. void nfs_destroy_nfspagecache(void)
  314. {
  315. if (kmem_cache_destroy(nfs_page_cachep))
  316. printk(KERN_INFO "nfs_page: not all structures were freed\n");
  317. }