pagelist.c 11 KB

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