pagelist.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 "internal.h"
  20. static struct kmem_cache *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, GFP_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.
  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_server *server = NFS_SERVER(inode);
  55. struct nfs_page *req;
  56. for (;;) {
  57. /* try to allocate the request struct */
  58. req = nfs_page_alloc();
  59. if (req != NULL)
  60. break;
  61. if (signalled() && (server->flags & NFS_MOUNT_INTR))
  62. return ERR_PTR(-ERESTARTSYS);
  63. yield();
  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. atomic_set(&req->wb_count, 1);
  79. req->wb_context = get_nfs_open_context(ctx);
  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_writeback_locked - Lock a request for writeback
  100. * @req:
  101. */
  102. int nfs_set_page_writeback_locked(struct nfs_page *req)
  103. {
  104. struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
  105. if (!nfs_lock_request(req))
  106. return 0;
  107. radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
  108. return 1;
  109. }
  110. /**
  111. * nfs_clear_page_writeback - Unlock request and wake up sleepers
  112. */
  113. void nfs_clear_page_writeback(struct nfs_page *req)
  114. {
  115. struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
  116. if (req->wb_page != NULL) {
  117. spin_lock(&nfsi->req_lock);
  118. radix_tree_tag_clear(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
  119. spin_unlock(&nfsi->req_lock);
  120. }
  121. nfs_unlock_request(req);
  122. }
  123. /**
  124. * nfs_clear_request - Free up all resources allocated to the request
  125. * @req:
  126. *
  127. * Release page resources associated with a write request after it
  128. * has completed.
  129. */
  130. void nfs_clear_request(struct nfs_page *req)
  131. {
  132. struct page *page = req->wb_page;
  133. if (page != NULL) {
  134. page_cache_release(page);
  135. req->wb_page = NULL;
  136. }
  137. }
  138. /**
  139. * nfs_release_request - Release the count on an NFS read/write request
  140. * @req: request to release
  141. *
  142. * Note: Should never be called with the spinlock held!
  143. */
  144. void
  145. nfs_release_request(struct nfs_page *req)
  146. {
  147. if (!atomic_dec_and_test(&req->wb_count))
  148. return;
  149. /* Release struct file or cached credential */
  150. nfs_clear_request(req);
  151. put_nfs_open_context(req->wb_context);
  152. nfs_page_free(req);
  153. }
  154. static int nfs_wait_bit_interruptible(void *word)
  155. {
  156. int ret = 0;
  157. if (signal_pending(current))
  158. ret = -ERESTARTSYS;
  159. else
  160. schedule();
  161. return ret;
  162. }
  163. /**
  164. * nfs_wait_on_request - Wait for a request to complete.
  165. * @req: request to wait upon.
  166. *
  167. * Interruptible by signals only if mounted with intr flag.
  168. * The user is responsible for holding a count on the request.
  169. */
  170. int
  171. nfs_wait_on_request(struct nfs_page *req)
  172. {
  173. struct rpc_clnt *clnt = NFS_CLIENT(req->wb_context->dentry->d_inode);
  174. sigset_t oldmask;
  175. int ret = 0;
  176. if (!test_bit(PG_BUSY, &req->wb_flags))
  177. goto out;
  178. /*
  179. * Note: the call to rpc_clnt_sigmask() suffices to ensure that we
  180. * are not interrupted if intr flag is not set
  181. */
  182. rpc_clnt_sigmask(clnt, &oldmask);
  183. ret = out_of_line_wait_on_bit(&req->wb_flags, PG_BUSY,
  184. nfs_wait_bit_interruptible, TASK_INTERRUPTIBLE);
  185. rpc_clnt_sigunmask(clnt, &oldmask);
  186. out:
  187. return ret;
  188. }
  189. /**
  190. * nfs_pageio_init - initialise a page io descriptor
  191. * @desc: pointer to descriptor
  192. * @inode: pointer to inode
  193. * @doio: pointer to io function
  194. * @bsize: io block size
  195. * @io_flags: extra parameters for the io function
  196. */
  197. void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
  198. struct inode *inode,
  199. int (*doio)(struct inode *, struct list_head *, unsigned int, size_t, int),
  200. size_t bsize,
  201. int io_flags)
  202. {
  203. INIT_LIST_HEAD(&desc->pg_list);
  204. desc->pg_bytes_written = 0;
  205. desc->pg_count = 0;
  206. desc->pg_bsize = bsize;
  207. desc->pg_base = 0;
  208. desc->pg_inode = inode;
  209. desc->pg_doio = doio;
  210. desc->pg_ioflags = io_flags;
  211. desc->pg_error = 0;
  212. }
  213. /**
  214. * nfs_can_coalesce_requests - test two requests for compatibility
  215. * @prev: pointer to nfs_page
  216. * @req: pointer to nfs_page
  217. *
  218. * The nfs_page structures 'prev' and 'req' are compared to ensure that the
  219. * page data area they describe is contiguous, and that their RPC
  220. * credentials, NFSv4 open state, and lockowners are the same.
  221. *
  222. * Return 'true' if this is the case, else return 'false'.
  223. */
  224. static int nfs_can_coalesce_requests(struct nfs_page *prev,
  225. struct nfs_page *req)
  226. {
  227. if (req->wb_context->cred != prev->wb_context->cred)
  228. return 0;
  229. if (req->wb_context->lockowner != prev->wb_context->lockowner)
  230. return 0;
  231. if (req->wb_context->state != prev->wb_context->state)
  232. return 0;
  233. if (req->wb_index != (prev->wb_index + 1))
  234. return 0;
  235. if (req->wb_pgbase != 0)
  236. return 0;
  237. if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
  238. return 0;
  239. return 1;
  240. }
  241. /**
  242. * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
  243. * @desc: destination io descriptor
  244. * @req: request
  245. *
  246. * Returns true if the request 'req' was successfully coalesced into the
  247. * existing list of pages 'desc'.
  248. */
  249. static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
  250. struct nfs_page *req)
  251. {
  252. size_t newlen = req->wb_bytes;
  253. if (desc->pg_count != 0) {
  254. struct nfs_page *prev;
  255. /*
  256. * FIXME: ideally we should be able to coalesce all requests
  257. * that are not block boundary aligned, but currently this
  258. * is problematic for the case of bsize < PAGE_CACHE_SIZE,
  259. * since nfs_flush_multi and nfs_pagein_multi assume you
  260. * can have only one struct nfs_page.
  261. */
  262. if (desc->pg_bsize < PAGE_SIZE)
  263. return 0;
  264. newlen += desc->pg_count;
  265. if (newlen > desc->pg_bsize)
  266. return 0;
  267. prev = nfs_list_entry(desc->pg_list.prev);
  268. if (!nfs_can_coalesce_requests(prev, req))
  269. return 0;
  270. } else
  271. desc->pg_base = req->wb_pgbase;
  272. nfs_list_remove_request(req);
  273. nfs_list_add_request(req, &desc->pg_list);
  274. desc->pg_count = newlen;
  275. return 1;
  276. }
  277. /*
  278. * Helper for nfs_pageio_add_request and nfs_pageio_complete
  279. */
  280. static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
  281. {
  282. if (!list_empty(&desc->pg_list)) {
  283. int error = desc->pg_doio(desc->pg_inode,
  284. &desc->pg_list,
  285. nfs_page_array_len(desc->pg_base,
  286. desc->pg_count),
  287. desc->pg_count,
  288. desc->pg_ioflags);
  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. 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. nfs_pageio_doio(desc);
  312. if (desc->pg_error < 0)
  313. return 0;
  314. }
  315. return 1;
  316. }
  317. /**
  318. * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
  319. * @desc: pointer to io descriptor
  320. */
  321. void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
  322. {
  323. nfs_pageio_doio(desc);
  324. }
  325. #define NFS_SCAN_MAXENTRIES 16
  326. /**
  327. * nfs_scan_list - Scan a list for matching requests
  328. * @nfsi: NFS inode
  329. * @head: One of the NFS inode request lists
  330. * @dst: Destination list
  331. * @idx_start: lower bound of page->index to scan
  332. * @npages: idx_start + npages sets the upper bound to scan.
  333. *
  334. * Moves elements from one of the inode request lists.
  335. * If the number of requests is set to 0, the entire address_space
  336. * starting at index idx_start, is scanned.
  337. * The requests are *not* checked to ensure that they form a contiguous set.
  338. * You must be holding the inode's req_lock when calling this function
  339. */
  340. int nfs_scan_list(struct nfs_inode *nfsi, struct list_head *head,
  341. struct list_head *dst, pgoff_t idx_start,
  342. unsigned int npages)
  343. {
  344. struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
  345. struct nfs_page *req;
  346. pgoff_t idx_end;
  347. int found, i;
  348. int res;
  349. res = 0;
  350. if (npages == 0)
  351. idx_end = ~0;
  352. else
  353. idx_end = idx_start + npages - 1;
  354. for (;;) {
  355. found = radix_tree_gang_lookup(&nfsi->nfs_page_tree,
  356. (void **)&pgvec[0], idx_start,
  357. NFS_SCAN_MAXENTRIES);
  358. if (found <= 0)
  359. break;
  360. for (i = 0; i < found; i++) {
  361. req = pgvec[i];
  362. if (req->wb_index > idx_end)
  363. goto out;
  364. idx_start = req->wb_index + 1;
  365. if (req->wb_list_head != head)
  366. continue;
  367. if (nfs_set_page_writeback_locked(req)) {
  368. nfs_list_remove_request(req);
  369. nfs_list_add_request(req, dst);
  370. res++;
  371. }
  372. }
  373. }
  374. out:
  375. return res;
  376. }
  377. int __init nfs_init_nfspagecache(void)
  378. {
  379. nfs_page_cachep = kmem_cache_create("nfs_page",
  380. sizeof(struct nfs_page),
  381. 0, SLAB_HWCACHE_ALIGN,
  382. NULL, NULL);
  383. if (nfs_page_cachep == NULL)
  384. return -ENOMEM;
  385. return 0;
  386. }
  387. void nfs_destroy_nfspagecache(void)
  388. {
  389. kmem_cache_destroy(nfs_page_cachep);
  390. }