pagelist.c 11 KB

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