pagelist.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. #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.
  48. * User should ensure it is safe to sleep in this function.
  49. */
  50. struct nfs_page *
  51. nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
  52. struct page *page,
  53. unsigned int offset, unsigned int count)
  54. {
  55. struct nfs_server *server = NFS_SERVER(inode);
  56. struct nfs_page *req;
  57. for (;;) {
  58. /* try to allocate the request struct */
  59. req = nfs_page_alloc();
  60. if (req != NULL)
  61. break;
  62. if (signalled() && (server->flags & NFS_MOUNT_INTR))
  63. return ERR_PTR(-ERESTARTSYS);
  64. yield();
  65. }
  66. /* Initialize the request struct. Initially, we assume a
  67. * long write-back delay. This will be adjusted in
  68. * update_nfs_request below if the region is not locked. */
  69. req->wb_page = page;
  70. atomic_set(&req->wb_complete, 0);
  71. req->wb_index = page->index;
  72. page_cache_get(page);
  73. BUG_ON(PagePrivate(page));
  74. BUG_ON(!PageLocked(page));
  75. BUG_ON(page->mapping->host != inode);
  76. req->wb_offset = offset;
  77. req->wb_pgbase = offset;
  78. req->wb_bytes = count;
  79. atomic_set(&req->wb_count, 1);
  80. req->wb_context = get_nfs_open_context(ctx);
  81. return req;
  82. }
  83. /**
  84. * nfs_unlock_request - Unlock request and wake up sleepers.
  85. * @req:
  86. */
  87. void nfs_unlock_request(struct nfs_page *req)
  88. {
  89. if (!NFS_WBACK_BUSY(req)) {
  90. printk(KERN_ERR "NFS: Invalid unlock attempted\n");
  91. BUG();
  92. }
  93. smp_mb__before_clear_bit();
  94. clear_bit(PG_BUSY, &req->wb_flags);
  95. smp_mb__after_clear_bit();
  96. wake_up_bit(&req->wb_flags, PG_BUSY);
  97. nfs_release_request(req);
  98. }
  99. /**
  100. * nfs_set_page_writeback_locked - Lock a request for writeback
  101. * @req:
  102. */
  103. int nfs_set_page_writeback_locked(struct nfs_page *req)
  104. {
  105. struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
  106. if (!nfs_lock_request(req))
  107. return 0;
  108. radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
  109. return 1;
  110. }
  111. /**
  112. * nfs_clear_page_writeback - Unlock request and wake up sleepers
  113. */
  114. void nfs_clear_page_writeback(struct nfs_page *req)
  115. {
  116. struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
  117. if (req->wb_page != NULL) {
  118. spin_lock(&nfsi->req_lock);
  119. radix_tree_tag_clear(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
  120. spin_unlock(&nfsi->req_lock);
  121. }
  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 resources associated with a write request after it
  129. * has completed.
  130. */
  131. void nfs_clear_request(struct nfs_page *req)
  132. {
  133. struct page *page = req->wb_page;
  134. if (page != NULL) {
  135. page_cache_release(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. static int nfs_wait_bit_interruptible(void *word)
  160. {
  161. int ret = 0;
  162. if (signal_pending(current))
  163. ret = -ERESTARTSYS;
  164. else
  165. schedule();
  166. return ret;
  167. }
  168. /**
  169. * nfs_wait_on_request - Wait for a request to complete.
  170. * @req: request to wait upon.
  171. *
  172. * Interruptible by signals only if mounted with intr flag.
  173. * The user is responsible for holding a count on the request.
  174. */
  175. int
  176. nfs_wait_on_request(struct nfs_page *req)
  177. {
  178. struct rpc_clnt *clnt = NFS_CLIENT(req->wb_context->dentry->d_inode);
  179. sigset_t oldmask;
  180. int ret = 0;
  181. if (!test_bit(PG_BUSY, &req->wb_flags))
  182. goto out;
  183. /*
  184. * Note: the call to rpc_clnt_sigmask() suffices to ensure that we
  185. * are not interrupted if intr flag is not set
  186. */
  187. rpc_clnt_sigmask(clnt, &oldmask);
  188. ret = out_of_line_wait_on_bit(&req->wb_flags, PG_BUSY,
  189. nfs_wait_bit_interruptible, TASK_INTERRUPTIBLE);
  190. rpc_clnt_sigunmask(clnt, &oldmask);
  191. out:
  192. return ret;
  193. }
  194. /**
  195. * nfs_pageio_init - initialise a page io descriptor
  196. * @desc: pointer to descriptor
  197. * @inode: pointer to inode
  198. * @doio: pointer to io function
  199. * @bsize: io block size
  200. * @io_flags: extra parameters for the io function
  201. */
  202. void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
  203. struct inode *inode,
  204. int (*doio)(struct inode *, struct list_head *, unsigned int, size_t, int),
  205. size_t bsize,
  206. int io_flags)
  207. {
  208. INIT_LIST_HEAD(&desc->pg_list);
  209. desc->pg_bytes_written = 0;
  210. desc->pg_count = 0;
  211. desc->pg_bsize = bsize;
  212. desc->pg_base = 0;
  213. desc->pg_inode = inode;
  214. desc->pg_doio = doio;
  215. desc->pg_ioflags = io_flags;
  216. desc->pg_error = 0;
  217. }
  218. /**
  219. * nfs_can_coalesce_requests - test two requests for compatibility
  220. * @prev: pointer to nfs_page
  221. * @req: pointer to nfs_page
  222. *
  223. * The nfs_page structures 'prev' and 'req' are compared to ensure that the
  224. * page data area they describe is contiguous, and that their RPC
  225. * credentials, NFSv4 open state, and lockowners are the same.
  226. *
  227. * Return 'true' if this is the case, else return 'false'.
  228. */
  229. static int nfs_can_coalesce_requests(struct nfs_page *prev,
  230. struct nfs_page *req)
  231. {
  232. if (req->wb_context->cred != prev->wb_context->cred)
  233. return 0;
  234. if (req->wb_context->lockowner != prev->wb_context->lockowner)
  235. return 0;
  236. if (req->wb_context->state != prev->wb_context->state)
  237. return 0;
  238. if (req->wb_index != (prev->wb_index + 1))
  239. return 0;
  240. if (req->wb_pgbase != 0)
  241. return 0;
  242. if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
  243. return 0;
  244. return 1;
  245. }
  246. /**
  247. * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
  248. * @desc: destination io descriptor
  249. * @req: request
  250. *
  251. * Returns true if the request 'req' was successfully coalesced into the
  252. * existing list of pages 'desc'.
  253. */
  254. static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
  255. struct nfs_page *req)
  256. {
  257. size_t newlen = req->wb_bytes;
  258. if (desc->pg_count != 0) {
  259. struct nfs_page *prev;
  260. /*
  261. * FIXME: ideally we should be able to coalesce all requests
  262. * that are not block boundary aligned, but currently this
  263. * is problematic for the case of bsize < PAGE_CACHE_SIZE,
  264. * since nfs_flush_multi and nfs_pagein_multi assume you
  265. * can have only one struct nfs_page.
  266. */
  267. if (desc->pg_bsize < PAGE_SIZE)
  268. return 0;
  269. newlen += desc->pg_count;
  270. if (newlen > desc->pg_bsize)
  271. return 0;
  272. prev = nfs_list_entry(desc->pg_list.prev);
  273. if (!nfs_can_coalesce_requests(prev, req))
  274. return 0;
  275. } else
  276. desc->pg_base = req->wb_pgbase;
  277. nfs_list_remove_request(req);
  278. nfs_list_add_request(req, &desc->pg_list);
  279. desc->pg_count = newlen;
  280. return 1;
  281. }
  282. /*
  283. * Helper for nfs_pageio_add_request and nfs_pageio_complete
  284. */
  285. static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
  286. {
  287. if (!list_empty(&desc->pg_list)) {
  288. int error = desc->pg_doio(desc->pg_inode,
  289. &desc->pg_list,
  290. nfs_page_array_len(desc->pg_base,
  291. desc->pg_count),
  292. desc->pg_count,
  293. desc->pg_ioflags);
  294. if (error < 0)
  295. desc->pg_error = error;
  296. else
  297. desc->pg_bytes_written += desc->pg_count;
  298. }
  299. if (list_empty(&desc->pg_list)) {
  300. desc->pg_count = 0;
  301. desc->pg_base = 0;
  302. }
  303. }
  304. /**
  305. * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
  306. * @desc: destination io descriptor
  307. * @req: request
  308. *
  309. * Returns true if the request 'req' was successfully coalesced into the
  310. * existing list of pages 'desc'.
  311. */
  312. int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  313. struct nfs_page *req)
  314. {
  315. while (!nfs_pageio_do_add_request(desc, req)) {
  316. nfs_pageio_doio(desc);
  317. if (desc->pg_error < 0)
  318. return 0;
  319. }
  320. return 1;
  321. }
  322. /**
  323. * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
  324. * @desc: pointer to io descriptor
  325. */
  326. void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
  327. {
  328. nfs_pageio_doio(desc);
  329. }
  330. #define NFS_SCAN_MAXENTRIES 16
  331. /**
  332. * nfs_scan_list - Scan a list for matching requests
  333. * @nfsi: NFS inode
  334. * @head: One of the NFS inode request lists
  335. * @dst: Destination list
  336. * @idx_start: lower bound of page->index to scan
  337. * @npages: idx_start + npages sets the upper bound to scan.
  338. *
  339. * Moves elements from one of the inode request lists.
  340. * If the number of requests is set to 0, the entire address_space
  341. * starting at index idx_start, is scanned.
  342. * The requests are *not* checked to ensure that they form a contiguous set.
  343. * You must be holding the inode's req_lock when calling this function
  344. */
  345. int nfs_scan_list(struct nfs_inode *nfsi, struct list_head *head,
  346. struct list_head *dst, pgoff_t idx_start,
  347. unsigned int npages)
  348. {
  349. struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
  350. struct nfs_page *req;
  351. pgoff_t idx_end;
  352. int found, i;
  353. int res;
  354. res = 0;
  355. if (npages == 0)
  356. idx_end = ~0;
  357. else
  358. idx_end = idx_start + npages - 1;
  359. for (;;) {
  360. found = radix_tree_gang_lookup(&nfsi->nfs_page_tree,
  361. (void **)&pgvec[0], idx_start,
  362. NFS_SCAN_MAXENTRIES);
  363. if (found <= 0)
  364. break;
  365. for (i = 0; i < found; i++) {
  366. req = pgvec[i];
  367. if (req->wb_index > idx_end)
  368. goto out;
  369. idx_start = req->wb_index + 1;
  370. if (req->wb_list_head != head)
  371. continue;
  372. if (nfs_set_page_writeback_locked(req)) {
  373. nfs_list_remove_request(req);
  374. nfs_list_add_request(req, dst);
  375. res++;
  376. }
  377. }
  378. }
  379. out:
  380. return res;
  381. }
  382. int __init nfs_init_nfspagecache(void)
  383. {
  384. nfs_page_cachep = kmem_cache_create("nfs_page",
  385. sizeof(struct nfs_page),
  386. 0, SLAB_HWCACHE_ALIGN,
  387. NULL, NULL);
  388. if (nfs_page_cachep == NULL)
  389. return -ENOMEM;
  390. return 0;
  391. }
  392. void nfs_destroy_nfspagecache(void)
  393. {
  394. kmem_cache_destroy(nfs_page_cachep);
  395. }