pagelist.c 11 KB

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