pagelist.c 11 KB

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