pagelist.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. static int nfs_wait_bit_killable(void *word)
  160. {
  161. int ret = 0;
  162. if (fatal_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 fatal signals only.
  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. int ret = 0;
  179. if (!test_bit(PG_BUSY, &req->wb_flags))
  180. goto out;
  181. ret = out_of_line_wait_on_bit(&req->wb_flags, PG_BUSY,
  182. nfs_wait_bit_killable, TASK_KILLABLE);
  183. out:
  184. return ret;
  185. }
  186. /**
  187. * nfs_pageio_init - initialise a page io descriptor
  188. * @desc: pointer to descriptor
  189. * @inode: pointer to inode
  190. * @doio: pointer to io function
  191. * @bsize: io block size
  192. * @io_flags: extra parameters for the io function
  193. */
  194. void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
  195. struct inode *inode,
  196. int (*doio)(struct inode *, struct list_head *, unsigned int, size_t, int),
  197. size_t bsize,
  198. int io_flags)
  199. {
  200. INIT_LIST_HEAD(&desc->pg_list);
  201. desc->pg_bytes_written = 0;
  202. desc->pg_count = 0;
  203. desc->pg_bsize = bsize;
  204. desc->pg_base = 0;
  205. desc->pg_inode = inode;
  206. desc->pg_doio = doio;
  207. desc->pg_ioflags = io_flags;
  208. desc->pg_error = 0;
  209. }
  210. /**
  211. * nfs_can_coalesce_requests - test two requests for compatibility
  212. * @prev: pointer to nfs_page
  213. * @req: pointer to nfs_page
  214. *
  215. * The nfs_page structures 'prev' and 'req' are compared to ensure that the
  216. * page data area they describe is contiguous, and that their RPC
  217. * credentials, NFSv4 open state, and lockowners are the same.
  218. *
  219. * Return 'true' if this is the case, else return 'false'.
  220. */
  221. static int nfs_can_coalesce_requests(struct nfs_page *prev,
  222. struct nfs_page *req)
  223. {
  224. if (req->wb_context->cred != prev->wb_context->cred)
  225. return 0;
  226. if (req->wb_context->lockowner != prev->wb_context->lockowner)
  227. return 0;
  228. if (req->wb_context->state != prev->wb_context->state)
  229. return 0;
  230. if (req->wb_index != (prev->wb_index + 1))
  231. return 0;
  232. if (req->wb_pgbase != 0)
  233. return 0;
  234. if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
  235. return 0;
  236. return 1;
  237. }
  238. /**
  239. * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
  240. * @desc: destination io descriptor
  241. * @req: request
  242. *
  243. * Returns true if the request 'req' was successfully coalesced into the
  244. * existing list of pages 'desc'.
  245. */
  246. static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
  247. struct nfs_page *req)
  248. {
  249. size_t newlen = req->wb_bytes;
  250. if (desc->pg_count != 0) {
  251. struct nfs_page *prev;
  252. /*
  253. * FIXME: ideally we should be able to coalesce all requests
  254. * that are not block boundary aligned, but currently this
  255. * is problematic for the case of bsize < PAGE_CACHE_SIZE,
  256. * since nfs_flush_multi and nfs_pagein_multi assume you
  257. * can have only one struct nfs_page.
  258. */
  259. if (desc->pg_bsize < PAGE_SIZE)
  260. return 0;
  261. newlen += desc->pg_count;
  262. if (newlen > desc->pg_bsize)
  263. return 0;
  264. prev = nfs_list_entry(desc->pg_list.prev);
  265. if (!nfs_can_coalesce_requests(prev, req))
  266. return 0;
  267. } else
  268. desc->pg_base = req->wb_pgbase;
  269. nfs_list_remove_request(req);
  270. nfs_list_add_request(req, &desc->pg_list);
  271. desc->pg_count = newlen;
  272. return 1;
  273. }
  274. /*
  275. * Helper for nfs_pageio_add_request and nfs_pageio_complete
  276. */
  277. static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
  278. {
  279. if (!list_empty(&desc->pg_list)) {
  280. int error = desc->pg_doio(desc->pg_inode,
  281. &desc->pg_list,
  282. nfs_page_array_len(desc->pg_base,
  283. desc->pg_count),
  284. desc->pg_count,
  285. desc->pg_ioflags);
  286. if (error < 0)
  287. desc->pg_error = error;
  288. else
  289. desc->pg_bytes_written += desc->pg_count;
  290. }
  291. if (list_empty(&desc->pg_list)) {
  292. desc->pg_count = 0;
  293. desc->pg_base = 0;
  294. }
  295. }
  296. /**
  297. * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
  298. * @desc: destination io descriptor
  299. * @req: request
  300. *
  301. * Returns true if the request 'req' was successfully coalesced into the
  302. * existing list of pages 'desc'.
  303. */
  304. int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  305. struct nfs_page *req)
  306. {
  307. while (!nfs_pageio_do_add_request(desc, req)) {
  308. nfs_pageio_doio(desc);
  309. if (desc->pg_error < 0)
  310. return 0;
  311. }
  312. return 1;
  313. }
  314. /**
  315. * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
  316. * @desc: pointer to io descriptor
  317. */
  318. void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
  319. {
  320. nfs_pageio_doio(desc);
  321. }
  322. /**
  323. * nfs_pageio_cond_complete - Conditional I/O completion
  324. * @desc: pointer to io descriptor
  325. * @index: page index
  326. *
  327. * It is important to ensure that processes don't try to take locks
  328. * on non-contiguous ranges of pages as that might deadlock. This
  329. * function should be called before attempting to wait on a locked
  330. * nfs_page. It will complete the I/O if the page index 'index'
  331. * is not contiguous with the existing list of pages in 'desc'.
  332. */
  333. void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
  334. {
  335. if (!list_empty(&desc->pg_list)) {
  336. struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
  337. if (index != prev->wb_index + 1)
  338. nfs_pageio_doio(desc);
  339. }
  340. }
  341. #define NFS_SCAN_MAXENTRIES 16
  342. /**
  343. * nfs_scan_list - Scan a list for matching requests
  344. * @nfsi: NFS inode
  345. * @dst: Destination list
  346. * @idx_start: lower bound of page->index to scan
  347. * @npages: idx_start + npages sets the upper bound to scan.
  348. * @tag: tag to scan for
  349. *
  350. * Moves elements from one of the inode request lists.
  351. * If the number of requests is set to 0, the entire address_space
  352. * starting at index idx_start, is scanned.
  353. * The requests are *not* checked to ensure that they form a contiguous set.
  354. * You must be holding the inode's i_lock when calling this function
  355. */
  356. int nfs_scan_list(struct nfs_inode *nfsi,
  357. struct list_head *dst, pgoff_t idx_start,
  358. unsigned int npages, int tag)
  359. {
  360. struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
  361. struct nfs_page *req;
  362. pgoff_t idx_end;
  363. int found, i;
  364. int res;
  365. res = 0;
  366. if (npages == 0)
  367. idx_end = ~0;
  368. else
  369. idx_end = idx_start + npages - 1;
  370. for (;;) {
  371. found = radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree,
  372. (void **)&pgvec[0], idx_start,
  373. NFS_SCAN_MAXENTRIES, tag);
  374. if (found <= 0)
  375. break;
  376. for (i = 0; i < found; i++) {
  377. req = pgvec[i];
  378. if (req->wb_index > idx_end)
  379. goto out;
  380. idx_start = req->wb_index + 1;
  381. if (nfs_set_page_tag_locked(req)) {
  382. kref_get(&req->wb_kref);
  383. nfs_list_remove_request(req);
  384. radix_tree_tag_clear(&nfsi->nfs_page_tree,
  385. req->wb_index, tag);
  386. nfs_list_add_request(req, dst);
  387. res++;
  388. if (res == INT_MAX)
  389. goto out;
  390. }
  391. }
  392. /* for latency reduction */
  393. cond_resched_lock(&nfsi->vfs_inode.i_lock);
  394. }
  395. out:
  396. return res;
  397. }
  398. int __init nfs_init_nfspagecache(void)
  399. {
  400. nfs_page_cachep = kmem_cache_create("nfs_page",
  401. sizeof(struct nfs_page),
  402. 0, SLAB_HWCACHE_ALIGN,
  403. NULL);
  404. if (nfs_page_cachep == NULL)
  405. return -ENOMEM;
  406. return 0;
  407. }
  408. void nfs_destroy_nfspagecache(void)
  409. {
  410. kmem_cache_destroy(nfs_page_cachep);
  411. }