pagelist.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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/nfs.h>
  16. #include <linux/nfs3.h>
  17. #include <linux/nfs4.h>
  18. #include <linux/nfs_page.h>
  19. #include <linux/nfs_fs.h>
  20. #include <linux/nfs_mount.h>
  21. #include <linux/export.h>
  22. #include "internal.h"
  23. #include "pnfs.h"
  24. #define NFSDBG_FACILITY NFSDBG_PAGECACHE
  25. static struct kmem_cache *nfs_page_cachep;
  26. static const struct rpc_call_ops nfs_pgio_common_ops;
  27. static bool nfs_pgarray_set(struct nfs_page_array *p, unsigned int pagecount)
  28. {
  29. p->npages = pagecount;
  30. if (pagecount <= ARRAY_SIZE(p->page_array))
  31. p->pagevec = p->page_array;
  32. else {
  33. p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL);
  34. if (!p->pagevec)
  35. p->npages = 0;
  36. }
  37. return p->pagevec != NULL;
  38. }
  39. void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
  40. struct nfs_pgio_header *hdr,
  41. void (*release)(struct nfs_pgio_header *hdr))
  42. {
  43. hdr->req = nfs_list_entry(desc->pg_list.next);
  44. hdr->inode = desc->pg_inode;
  45. hdr->cred = hdr->req->wb_context->cred;
  46. hdr->io_start = req_offset(hdr->req);
  47. hdr->good_bytes = desc->pg_count;
  48. hdr->dreq = desc->pg_dreq;
  49. hdr->layout_private = desc->pg_layout_private;
  50. hdr->release = release;
  51. hdr->completion_ops = desc->pg_completion_ops;
  52. if (hdr->completion_ops->init_hdr)
  53. hdr->completion_ops->init_hdr(hdr);
  54. }
  55. EXPORT_SYMBOL_GPL(nfs_pgheader_init);
  56. void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
  57. {
  58. spin_lock(&hdr->lock);
  59. if (pos < hdr->io_start + hdr->good_bytes) {
  60. set_bit(NFS_IOHDR_ERROR, &hdr->flags);
  61. clear_bit(NFS_IOHDR_EOF, &hdr->flags);
  62. hdr->good_bytes = pos - hdr->io_start;
  63. hdr->error = error;
  64. }
  65. spin_unlock(&hdr->lock);
  66. }
  67. static inline struct nfs_page *
  68. nfs_page_alloc(void)
  69. {
  70. struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO);
  71. if (p)
  72. INIT_LIST_HEAD(&p->wb_list);
  73. return p;
  74. }
  75. static inline void
  76. nfs_page_free(struct nfs_page *p)
  77. {
  78. kmem_cache_free(nfs_page_cachep, p);
  79. }
  80. static void
  81. nfs_iocounter_inc(struct nfs_io_counter *c)
  82. {
  83. atomic_inc(&c->io_count);
  84. }
  85. static void
  86. nfs_iocounter_dec(struct nfs_io_counter *c)
  87. {
  88. if (atomic_dec_and_test(&c->io_count)) {
  89. clear_bit(NFS_IO_INPROGRESS, &c->flags);
  90. smp_mb__after_atomic();
  91. wake_up_bit(&c->flags, NFS_IO_INPROGRESS);
  92. }
  93. }
  94. static int
  95. __nfs_iocounter_wait(struct nfs_io_counter *c)
  96. {
  97. wait_queue_head_t *wq = bit_waitqueue(&c->flags, NFS_IO_INPROGRESS);
  98. DEFINE_WAIT_BIT(q, &c->flags, NFS_IO_INPROGRESS);
  99. int ret = 0;
  100. do {
  101. prepare_to_wait(wq, &q.wait, TASK_KILLABLE);
  102. set_bit(NFS_IO_INPROGRESS, &c->flags);
  103. if (atomic_read(&c->io_count) == 0)
  104. break;
  105. ret = nfs_wait_bit_killable(&q.key);
  106. } while (atomic_read(&c->io_count) != 0 && !ret);
  107. finish_wait(wq, &q.wait);
  108. return ret;
  109. }
  110. /**
  111. * nfs_iocounter_wait - wait for i/o to complete
  112. * @c: nfs_io_counter to use
  113. *
  114. * returns -ERESTARTSYS if interrupted by a fatal signal.
  115. * Otherwise returns 0 once the io_count hits 0.
  116. */
  117. int
  118. nfs_iocounter_wait(struct nfs_io_counter *c)
  119. {
  120. if (atomic_read(&c->io_count) == 0)
  121. return 0;
  122. return __nfs_iocounter_wait(c);
  123. }
  124. /*
  125. * nfs_page_group_lock - lock the head of the page group
  126. * @req - request in group that is to be locked
  127. * @nonblock - if true don't block waiting for lock
  128. *
  129. * this lock must be held if modifying the page group list
  130. *
  131. * return 0 on success, < 0 on error: -EDELAY if nonblocking or the
  132. * result from wait_on_bit_lock
  133. *
  134. * NOTE: calling with nonblock=false should always have set the
  135. * lock bit (see fs/buffer.c and other uses of wait_on_bit_lock
  136. * with TASK_UNINTERRUPTIBLE), so there is no need to check the result.
  137. */
  138. int
  139. nfs_page_group_lock(struct nfs_page *req, bool nonblock)
  140. {
  141. struct nfs_page *head = req->wb_head;
  142. WARN_ON_ONCE(head != head->wb_head);
  143. if (!test_and_set_bit(PG_HEADLOCK, &head->wb_flags))
  144. return 0;
  145. if (!nonblock)
  146. return wait_on_bit_lock(&head->wb_flags, PG_HEADLOCK,
  147. TASK_UNINTERRUPTIBLE);
  148. return -EAGAIN;
  149. }
  150. /*
  151. * nfs_page_group_lock_wait - wait for the lock to clear, but don't grab it
  152. * @req - a request in the group
  153. *
  154. * This is a blocking call to wait for the group lock to be cleared.
  155. */
  156. void
  157. nfs_page_group_lock_wait(struct nfs_page *req)
  158. {
  159. struct nfs_page *head = req->wb_head;
  160. WARN_ON_ONCE(head != head->wb_head);
  161. wait_on_bit(&head->wb_flags, PG_HEADLOCK,
  162. TASK_UNINTERRUPTIBLE);
  163. }
  164. /*
  165. * nfs_page_group_unlock - unlock the head of the page group
  166. * @req - request in group that is to be unlocked
  167. */
  168. void
  169. nfs_page_group_unlock(struct nfs_page *req)
  170. {
  171. struct nfs_page *head = req->wb_head;
  172. WARN_ON_ONCE(head != head->wb_head);
  173. smp_mb__before_atomic();
  174. clear_bit(PG_HEADLOCK, &head->wb_flags);
  175. smp_mb__after_atomic();
  176. wake_up_bit(&head->wb_flags, PG_HEADLOCK);
  177. }
  178. /*
  179. * nfs_page_group_sync_on_bit_locked
  180. *
  181. * must be called with page group lock held
  182. */
  183. static bool
  184. nfs_page_group_sync_on_bit_locked(struct nfs_page *req, unsigned int bit)
  185. {
  186. struct nfs_page *head = req->wb_head;
  187. struct nfs_page *tmp;
  188. WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_flags));
  189. WARN_ON_ONCE(test_and_set_bit(bit, &req->wb_flags));
  190. tmp = req->wb_this_page;
  191. while (tmp != req) {
  192. if (!test_bit(bit, &tmp->wb_flags))
  193. return false;
  194. tmp = tmp->wb_this_page;
  195. }
  196. /* true! reset all bits */
  197. tmp = req;
  198. do {
  199. clear_bit(bit, &tmp->wb_flags);
  200. tmp = tmp->wb_this_page;
  201. } while (tmp != req);
  202. return true;
  203. }
  204. /*
  205. * nfs_page_group_sync_on_bit - set bit on current request, but only
  206. * return true if the bit is set for all requests in page group
  207. * @req - request in page group
  208. * @bit - PG_* bit that is used to sync page group
  209. */
  210. bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit)
  211. {
  212. bool ret;
  213. nfs_page_group_lock(req, false);
  214. ret = nfs_page_group_sync_on_bit_locked(req, bit);
  215. nfs_page_group_unlock(req);
  216. return ret;
  217. }
  218. /*
  219. * nfs_page_group_init - Initialize the page group linkage for @req
  220. * @req - a new nfs request
  221. * @prev - the previous request in page group, or NULL if @req is the first
  222. * or only request in the group (the head).
  223. */
  224. static inline void
  225. nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev)
  226. {
  227. WARN_ON_ONCE(prev == req);
  228. if (!prev) {
  229. /* a head request */
  230. req->wb_head = req;
  231. req->wb_this_page = req;
  232. } else {
  233. /* a subrequest */
  234. WARN_ON_ONCE(prev->wb_this_page != prev->wb_head);
  235. WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags));
  236. req->wb_head = prev->wb_head;
  237. req->wb_this_page = prev->wb_this_page;
  238. prev->wb_this_page = req;
  239. /* All subrequests take a ref on the head request until
  240. * nfs_page_group_destroy is called */
  241. kref_get(&req->wb_head->wb_kref);
  242. /* grab extra ref if head request has extra ref from
  243. * the write/commit path to handle handoff between write
  244. * and commit lists */
  245. if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags)) {
  246. set_bit(PG_INODE_REF, &req->wb_flags);
  247. kref_get(&req->wb_kref);
  248. }
  249. }
  250. }
  251. /*
  252. * nfs_page_group_destroy - sync the destruction of page groups
  253. * @req - request that no longer needs the page group
  254. *
  255. * releases the page group reference from each member once all
  256. * members have called this function.
  257. */
  258. static void
  259. nfs_page_group_destroy(struct kref *kref)
  260. {
  261. struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
  262. struct nfs_page *tmp, *next;
  263. /* subrequests must release the ref on the head request */
  264. if (req->wb_head != req)
  265. nfs_release_request(req->wb_head);
  266. if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN))
  267. return;
  268. tmp = req;
  269. do {
  270. next = tmp->wb_this_page;
  271. /* unlink and free */
  272. tmp->wb_this_page = tmp;
  273. tmp->wb_head = tmp;
  274. nfs_free_request(tmp);
  275. tmp = next;
  276. } while (tmp != req);
  277. }
  278. /**
  279. * nfs_create_request - Create an NFS read/write request.
  280. * @ctx: open context to use
  281. * @page: page to write
  282. * @last: last nfs request created for this page group or NULL if head
  283. * @offset: starting offset within the page for the write
  284. * @count: number of bytes to read/write
  285. *
  286. * The page must be locked by the caller. This makes sure we never
  287. * create two different requests for the same page.
  288. * User should ensure it is safe to sleep in this function.
  289. */
  290. struct nfs_page *
  291. nfs_create_request(struct nfs_open_context *ctx, struct page *page,
  292. struct nfs_page *last, unsigned int offset,
  293. unsigned int count)
  294. {
  295. struct nfs_page *req;
  296. struct nfs_lock_context *l_ctx;
  297. if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
  298. return ERR_PTR(-EBADF);
  299. /* try to allocate the request struct */
  300. req = nfs_page_alloc();
  301. if (req == NULL)
  302. return ERR_PTR(-ENOMEM);
  303. /* get lock context early so we can deal with alloc failures */
  304. l_ctx = nfs_get_lock_context(ctx);
  305. if (IS_ERR(l_ctx)) {
  306. nfs_page_free(req);
  307. return ERR_CAST(l_ctx);
  308. }
  309. req->wb_lock_context = l_ctx;
  310. nfs_iocounter_inc(&l_ctx->io_count);
  311. /* Initialize the request struct. Initially, we assume a
  312. * long write-back delay. This will be adjusted in
  313. * update_nfs_request below if the region is not locked. */
  314. req->wb_page = page;
  315. req->wb_index = page_file_index(page);
  316. page_cache_get(page);
  317. req->wb_offset = offset;
  318. req->wb_pgbase = offset;
  319. req->wb_bytes = count;
  320. req->wb_context = get_nfs_open_context(ctx);
  321. kref_init(&req->wb_kref);
  322. nfs_page_group_init(req, last);
  323. return req;
  324. }
  325. /**
  326. * nfs_unlock_request - Unlock request and wake up sleepers.
  327. * @req:
  328. */
  329. void nfs_unlock_request(struct nfs_page *req)
  330. {
  331. if (!NFS_WBACK_BUSY(req)) {
  332. printk(KERN_ERR "NFS: Invalid unlock attempted\n");
  333. BUG();
  334. }
  335. smp_mb__before_atomic();
  336. clear_bit(PG_BUSY, &req->wb_flags);
  337. smp_mb__after_atomic();
  338. wake_up_bit(&req->wb_flags, PG_BUSY);
  339. }
  340. /**
  341. * nfs_unlock_and_release_request - Unlock request and release the nfs_page
  342. * @req:
  343. */
  344. void nfs_unlock_and_release_request(struct nfs_page *req)
  345. {
  346. nfs_unlock_request(req);
  347. nfs_release_request(req);
  348. }
  349. /*
  350. * nfs_clear_request - Free up all resources allocated to the request
  351. * @req:
  352. *
  353. * Release page and open context resources associated with a read/write
  354. * request after it has completed.
  355. */
  356. static void nfs_clear_request(struct nfs_page *req)
  357. {
  358. struct page *page = req->wb_page;
  359. struct nfs_open_context *ctx = req->wb_context;
  360. struct nfs_lock_context *l_ctx = req->wb_lock_context;
  361. if (page != NULL) {
  362. page_cache_release(page);
  363. req->wb_page = NULL;
  364. }
  365. if (l_ctx != NULL) {
  366. nfs_iocounter_dec(&l_ctx->io_count);
  367. nfs_put_lock_context(l_ctx);
  368. req->wb_lock_context = NULL;
  369. }
  370. if (ctx != NULL) {
  371. put_nfs_open_context(ctx);
  372. req->wb_context = NULL;
  373. }
  374. }
  375. /**
  376. * nfs_release_request - Release the count on an NFS read/write request
  377. * @req: request to release
  378. *
  379. * Note: Should never be called with the spinlock held!
  380. */
  381. void nfs_free_request(struct nfs_page *req)
  382. {
  383. WARN_ON_ONCE(req->wb_this_page != req);
  384. /* extra debug: make sure no sync bits are still set */
  385. WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
  386. WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags));
  387. WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags));
  388. WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags));
  389. WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags));
  390. /* Release struct file and open context */
  391. nfs_clear_request(req);
  392. nfs_page_free(req);
  393. }
  394. void nfs_release_request(struct nfs_page *req)
  395. {
  396. kref_put(&req->wb_kref, nfs_page_group_destroy);
  397. }
  398. /**
  399. * nfs_wait_on_request - Wait for a request to complete.
  400. * @req: request to wait upon.
  401. *
  402. * Interruptible by fatal signals only.
  403. * The user is responsible for holding a count on the request.
  404. */
  405. int
  406. nfs_wait_on_request(struct nfs_page *req)
  407. {
  408. return wait_on_bit_io(&req->wb_flags, PG_BUSY,
  409. TASK_UNINTERRUPTIBLE);
  410. }
  411. /*
  412. * nfs_generic_pg_test - determine if requests can be coalesced
  413. * @desc: pointer to descriptor
  414. * @prev: previous request in desc, or NULL
  415. * @req: this request
  416. *
  417. * Returns zero if @req can be coalesced into @desc, otherwise it returns
  418. * the size of the request.
  419. */
  420. size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
  421. struct nfs_page *prev, struct nfs_page *req)
  422. {
  423. if (desc->pg_count > desc->pg_bsize) {
  424. /* should never happen */
  425. WARN_ON_ONCE(1);
  426. return 0;
  427. }
  428. return min(desc->pg_bsize - desc->pg_count, (size_t)req->wb_bytes);
  429. }
  430. EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
  431. struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *ops)
  432. {
  433. struct nfs_pgio_header *hdr = ops->rw_alloc_header();
  434. if (hdr) {
  435. INIT_LIST_HEAD(&hdr->pages);
  436. spin_lock_init(&hdr->lock);
  437. hdr->rw_ops = ops;
  438. }
  439. return hdr;
  440. }
  441. EXPORT_SYMBOL_GPL(nfs_pgio_header_alloc);
  442. /*
  443. * nfs_pgio_header_free - Free a read or write header
  444. * @hdr: The header to free
  445. */
  446. void nfs_pgio_header_free(struct nfs_pgio_header *hdr)
  447. {
  448. hdr->rw_ops->rw_free_header(hdr);
  449. }
  450. EXPORT_SYMBOL_GPL(nfs_pgio_header_free);
  451. /**
  452. * nfs_pgio_data_destroy - make @hdr suitable for reuse
  453. *
  454. * Frees memory and releases refs from nfs_generic_pgio, so that it may
  455. * be called again.
  456. *
  457. * @hdr: A header that has had nfs_generic_pgio called
  458. */
  459. void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr)
  460. {
  461. put_nfs_open_context(hdr->args.context);
  462. if (hdr->page_array.pagevec != hdr->page_array.page_array)
  463. kfree(hdr->page_array.pagevec);
  464. }
  465. EXPORT_SYMBOL_GPL(nfs_pgio_data_destroy);
  466. /**
  467. * nfs_pgio_rpcsetup - Set up arguments for a pageio call
  468. * @hdr: The pageio hdr
  469. * @count: Number of bytes to read
  470. * @offset: Initial offset
  471. * @how: How to commit data (writes only)
  472. * @cinfo: Commit information for the call (writes only)
  473. */
  474. static void nfs_pgio_rpcsetup(struct nfs_pgio_header *hdr,
  475. unsigned int count, unsigned int offset,
  476. int how, struct nfs_commit_info *cinfo)
  477. {
  478. struct nfs_page *req = hdr->req;
  479. /* Set up the RPC argument and reply structs
  480. * NB: take care not to mess about with hdr->commit et al. */
  481. hdr->args.fh = NFS_FH(hdr->inode);
  482. hdr->args.offset = req_offset(req) + offset;
  483. /* pnfs_set_layoutcommit needs this */
  484. hdr->mds_offset = hdr->args.offset;
  485. hdr->args.pgbase = req->wb_pgbase + offset;
  486. hdr->args.pages = hdr->page_array.pagevec;
  487. hdr->args.count = count;
  488. hdr->args.context = get_nfs_open_context(req->wb_context);
  489. hdr->args.lock_context = req->wb_lock_context;
  490. hdr->args.stable = NFS_UNSTABLE;
  491. switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
  492. case 0:
  493. break;
  494. case FLUSH_COND_STABLE:
  495. if (nfs_reqs_to_commit(cinfo))
  496. break;
  497. default:
  498. hdr->args.stable = NFS_FILE_SYNC;
  499. }
  500. hdr->res.fattr = &hdr->fattr;
  501. hdr->res.count = count;
  502. hdr->res.eof = 0;
  503. hdr->res.verf = &hdr->verf;
  504. nfs_fattr_init(&hdr->fattr);
  505. }
  506. /**
  507. * nfs_pgio_prepare - Prepare pageio hdr to go over the wire
  508. * @task: The current task
  509. * @calldata: pageio header to prepare
  510. */
  511. static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
  512. {
  513. struct nfs_pgio_header *hdr = calldata;
  514. int err;
  515. err = NFS_PROTO(hdr->inode)->pgio_rpc_prepare(task, hdr);
  516. if (err)
  517. rpc_exit(task, err);
  518. }
  519. int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
  520. const struct rpc_call_ops *call_ops, int how, int flags)
  521. {
  522. struct rpc_task *task;
  523. struct rpc_message msg = {
  524. .rpc_argp = &hdr->args,
  525. .rpc_resp = &hdr->res,
  526. .rpc_cred = hdr->cred,
  527. };
  528. struct rpc_task_setup task_setup_data = {
  529. .rpc_client = clnt,
  530. .task = &hdr->task,
  531. .rpc_message = &msg,
  532. .callback_ops = call_ops,
  533. .callback_data = hdr,
  534. .workqueue = nfsiod_workqueue,
  535. .flags = RPC_TASK_ASYNC | flags,
  536. };
  537. int ret = 0;
  538. hdr->rw_ops->rw_initiate(hdr, &msg, &task_setup_data, how);
  539. dprintk("NFS: %5u initiated pgio call "
  540. "(req %s/%llu, %u bytes @ offset %llu)\n",
  541. hdr->task.tk_pid,
  542. hdr->inode->i_sb->s_id,
  543. (unsigned long long)NFS_FILEID(hdr->inode),
  544. hdr->args.count,
  545. (unsigned long long)hdr->args.offset);
  546. task = rpc_run_task(&task_setup_data);
  547. if (IS_ERR(task)) {
  548. ret = PTR_ERR(task);
  549. goto out;
  550. }
  551. if (how & FLUSH_SYNC) {
  552. ret = rpc_wait_for_completion_task(task);
  553. if (ret == 0)
  554. ret = task->tk_status;
  555. }
  556. rpc_put_task(task);
  557. out:
  558. return ret;
  559. }
  560. EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
  561. /**
  562. * nfs_pgio_error - Clean up from a pageio error
  563. * @desc: IO descriptor
  564. * @hdr: pageio header
  565. */
  566. static int nfs_pgio_error(struct nfs_pageio_descriptor *desc,
  567. struct nfs_pgio_header *hdr)
  568. {
  569. set_bit(NFS_IOHDR_REDO, &hdr->flags);
  570. nfs_pgio_data_destroy(hdr);
  571. hdr->completion_ops->completion(hdr);
  572. desc->pg_completion_ops->error_cleanup(&desc->pg_list);
  573. return -ENOMEM;
  574. }
  575. /**
  576. * nfs_pgio_release - Release pageio data
  577. * @calldata: The pageio header to release
  578. */
  579. static void nfs_pgio_release(void *calldata)
  580. {
  581. struct nfs_pgio_header *hdr = calldata;
  582. if (hdr->rw_ops->rw_release)
  583. hdr->rw_ops->rw_release(hdr);
  584. nfs_pgio_data_destroy(hdr);
  585. hdr->completion_ops->completion(hdr);
  586. }
  587. /**
  588. * nfs_pageio_init - initialise a page io descriptor
  589. * @desc: pointer to descriptor
  590. * @inode: pointer to inode
  591. * @doio: pointer to io function
  592. * @bsize: io block size
  593. * @io_flags: extra parameters for the io function
  594. */
  595. void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
  596. struct inode *inode,
  597. const struct nfs_pageio_ops *pg_ops,
  598. const struct nfs_pgio_completion_ops *compl_ops,
  599. const struct nfs_rw_ops *rw_ops,
  600. size_t bsize,
  601. int io_flags)
  602. {
  603. INIT_LIST_HEAD(&desc->pg_list);
  604. desc->pg_bytes_written = 0;
  605. desc->pg_count = 0;
  606. desc->pg_bsize = bsize;
  607. desc->pg_base = 0;
  608. desc->pg_moreio = 0;
  609. desc->pg_recoalesce = 0;
  610. desc->pg_inode = inode;
  611. desc->pg_ops = pg_ops;
  612. desc->pg_completion_ops = compl_ops;
  613. desc->pg_rw_ops = rw_ops;
  614. desc->pg_ioflags = io_flags;
  615. desc->pg_error = 0;
  616. desc->pg_lseg = NULL;
  617. desc->pg_dreq = NULL;
  618. desc->pg_layout_private = NULL;
  619. }
  620. EXPORT_SYMBOL_GPL(nfs_pageio_init);
  621. /**
  622. * nfs_pgio_result - Basic pageio error handling
  623. * @task: The task that ran
  624. * @calldata: Pageio header to check
  625. */
  626. static void nfs_pgio_result(struct rpc_task *task, void *calldata)
  627. {
  628. struct nfs_pgio_header *hdr = calldata;
  629. struct inode *inode = hdr->inode;
  630. dprintk("NFS: %s: %5u, (status %d)\n", __func__,
  631. task->tk_pid, task->tk_status);
  632. if (hdr->rw_ops->rw_done(task, hdr, inode) != 0)
  633. return;
  634. if (task->tk_status < 0)
  635. nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset);
  636. else
  637. hdr->rw_ops->rw_result(task, hdr);
  638. }
  639. /*
  640. * Create an RPC task for the given read or write request and kick it.
  641. * The page must have been locked by the caller.
  642. *
  643. * It may happen that the page we're passed is not marked dirty.
  644. * This is the case if nfs_updatepage detects a conflicting request
  645. * that has been written but not committed.
  646. */
  647. int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
  648. struct nfs_pgio_header *hdr)
  649. {
  650. struct nfs_page *req;
  651. struct page **pages,
  652. *last_page;
  653. struct list_head *head = &desc->pg_list;
  654. struct nfs_commit_info cinfo;
  655. unsigned int pagecount, pageused;
  656. pagecount = nfs_page_array_len(desc->pg_base, desc->pg_count);
  657. if (!nfs_pgarray_set(&hdr->page_array, pagecount))
  658. return nfs_pgio_error(desc, hdr);
  659. nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
  660. pages = hdr->page_array.pagevec;
  661. last_page = NULL;
  662. pageused = 0;
  663. while (!list_empty(head)) {
  664. req = nfs_list_entry(head->next);
  665. nfs_list_remove_request(req);
  666. nfs_list_add_request(req, &hdr->pages);
  667. if (WARN_ON_ONCE(pageused >= pagecount))
  668. return nfs_pgio_error(desc, hdr);
  669. if (!last_page || last_page != req->wb_page) {
  670. *pages++ = last_page = req->wb_page;
  671. pageused++;
  672. }
  673. }
  674. if (WARN_ON_ONCE(pageused != pagecount))
  675. return nfs_pgio_error(desc, hdr);
  676. if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
  677. (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
  678. desc->pg_ioflags &= ~FLUSH_COND_STABLE;
  679. /* Set up the argument struct */
  680. nfs_pgio_rpcsetup(hdr, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
  681. desc->pg_rpc_callops = &nfs_pgio_common_ops;
  682. return 0;
  683. }
  684. EXPORT_SYMBOL_GPL(nfs_generic_pgio);
  685. static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
  686. {
  687. struct nfs_pgio_header *hdr;
  688. int ret;
  689. hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
  690. if (!hdr) {
  691. desc->pg_completion_ops->error_cleanup(&desc->pg_list);
  692. return -ENOMEM;
  693. }
  694. nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
  695. ret = nfs_generic_pgio(desc, hdr);
  696. if (ret == 0)
  697. ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
  698. hdr, desc->pg_rpc_callops,
  699. desc->pg_ioflags, 0);
  700. return ret;
  701. }
  702. static bool nfs_match_open_context(const struct nfs_open_context *ctx1,
  703. const struct nfs_open_context *ctx2)
  704. {
  705. return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state;
  706. }
  707. static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
  708. const struct nfs_lock_context *l2)
  709. {
  710. return l1->lockowner.l_owner == l2->lockowner.l_owner
  711. && l1->lockowner.l_pid == l2->lockowner.l_pid;
  712. }
  713. /**
  714. * nfs_can_coalesce_requests - test two requests for compatibility
  715. * @prev: pointer to nfs_page
  716. * @req: pointer to nfs_page
  717. *
  718. * The nfs_page structures 'prev' and 'req' are compared to ensure that the
  719. * page data area they describe is contiguous, and that their RPC
  720. * credentials, NFSv4 open state, and lockowners are the same.
  721. *
  722. * Return 'true' if this is the case, else return 'false'.
  723. */
  724. static bool nfs_can_coalesce_requests(struct nfs_page *prev,
  725. struct nfs_page *req,
  726. struct nfs_pageio_descriptor *pgio)
  727. {
  728. size_t size;
  729. if (prev) {
  730. if (!nfs_match_open_context(req->wb_context, prev->wb_context))
  731. return false;
  732. if (req->wb_context->dentry->d_inode->i_flock != NULL &&
  733. !nfs_match_lock_context(req->wb_lock_context,
  734. prev->wb_lock_context))
  735. return false;
  736. if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
  737. return false;
  738. if (req->wb_page == prev->wb_page) {
  739. if (req->wb_pgbase != prev->wb_pgbase + prev->wb_bytes)
  740. return false;
  741. } else {
  742. if (req->wb_pgbase != 0 ||
  743. prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
  744. return false;
  745. }
  746. }
  747. size = pgio->pg_ops->pg_test(pgio, prev, req);
  748. WARN_ON_ONCE(size > req->wb_bytes);
  749. if (size && size < req->wb_bytes)
  750. req->wb_bytes = size;
  751. return size > 0;
  752. }
  753. /**
  754. * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
  755. * @desc: destination io descriptor
  756. * @req: request
  757. *
  758. * Returns true if the request 'req' was successfully coalesced into the
  759. * existing list of pages 'desc'.
  760. */
  761. static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
  762. struct nfs_page *req)
  763. {
  764. struct nfs_page *prev = NULL;
  765. if (desc->pg_count != 0) {
  766. prev = nfs_list_entry(desc->pg_list.prev);
  767. } else {
  768. if (desc->pg_ops->pg_init)
  769. desc->pg_ops->pg_init(desc, req);
  770. desc->pg_base = req->wb_pgbase;
  771. }
  772. if (!nfs_can_coalesce_requests(prev, req, desc))
  773. return 0;
  774. nfs_list_remove_request(req);
  775. nfs_list_add_request(req, &desc->pg_list);
  776. desc->pg_count += req->wb_bytes;
  777. return 1;
  778. }
  779. /*
  780. * Helper for nfs_pageio_add_request and nfs_pageio_complete
  781. */
  782. static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
  783. {
  784. if (!list_empty(&desc->pg_list)) {
  785. int error = desc->pg_ops->pg_doio(desc);
  786. if (error < 0)
  787. desc->pg_error = error;
  788. else
  789. desc->pg_bytes_written += desc->pg_count;
  790. }
  791. if (list_empty(&desc->pg_list)) {
  792. desc->pg_count = 0;
  793. desc->pg_base = 0;
  794. }
  795. }
  796. /**
  797. * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
  798. * @desc: destination io descriptor
  799. * @req: request
  800. *
  801. * This may split a request into subrequests which are all part of the
  802. * same page group.
  803. *
  804. * Returns true if the request 'req' was successfully coalesced into the
  805. * existing list of pages 'desc'.
  806. */
  807. static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  808. struct nfs_page *req)
  809. {
  810. struct nfs_page *subreq;
  811. unsigned int bytes_left = 0;
  812. unsigned int offset, pgbase;
  813. nfs_page_group_lock(req, false);
  814. subreq = req;
  815. bytes_left = subreq->wb_bytes;
  816. offset = subreq->wb_offset;
  817. pgbase = subreq->wb_pgbase;
  818. do {
  819. if (!nfs_pageio_do_add_request(desc, subreq)) {
  820. /* make sure pg_test call(s) did nothing */
  821. WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
  822. WARN_ON_ONCE(subreq->wb_offset != offset);
  823. WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
  824. nfs_page_group_unlock(req);
  825. desc->pg_moreio = 1;
  826. nfs_pageio_doio(desc);
  827. if (desc->pg_error < 0)
  828. return 0;
  829. if (desc->pg_recoalesce)
  830. return 0;
  831. /* retry add_request for this subreq */
  832. nfs_page_group_lock(req, false);
  833. continue;
  834. }
  835. /* check for buggy pg_test call(s) */
  836. WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
  837. WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
  838. WARN_ON_ONCE(subreq->wb_bytes == 0);
  839. bytes_left -= subreq->wb_bytes;
  840. offset += subreq->wb_bytes;
  841. pgbase += subreq->wb_bytes;
  842. if (bytes_left) {
  843. subreq = nfs_create_request(req->wb_context,
  844. req->wb_page,
  845. subreq, pgbase, bytes_left);
  846. if (IS_ERR(subreq))
  847. goto err_ptr;
  848. nfs_lock_request(subreq);
  849. subreq->wb_offset = offset;
  850. subreq->wb_index = req->wb_index;
  851. }
  852. } while (bytes_left > 0);
  853. nfs_page_group_unlock(req);
  854. return 1;
  855. err_ptr:
  856. desc->pg_error = PTR_ERR(subreq);
  857. nfs_page_group_unlock(req);
  858. return 0;
  859. }
  860. static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
  861. {
  862. LIST_HEAD(head);
  863. do {
  864. list_splice_init(&desc->pg_list, &head);
  865. desc->pg_bytes_written -= desc->pg_count;
  866. desc->pg_count = 0;
  867. desc->pg_base = 0;
  868. desc->pg_recoalesce = 0;
  869. desc->pg_moreio = 0;
  870. while (!list_empty(&head)) {
  871. struct nfs_page *req;
  872. req = list_first_entry(&head, struct nfs_page, wb_list);
  873. nfs_list_remove_request(req);
  874. if (__nfs_pageio_add_request(desc, req))
  875. continue;
  876. if (desc->pg_error < 0)
  877. return 0;
  878. break;
  879. }
  880. } while (desc->pg_recoalesce);
  881. return 1;
  882. }
  883. int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  884. struct nfs_page *req)
  885. {
  886. int ret;
  887. do {
  888. ret = __nfs_pageio_add_request(desc, req);
  889. if (ret)
  890. break;
  891. if (desc->pg_error < 0)
  892. break;
  893. ret = nfs_do_recoalesce(desc);
  894. } while (ret);
  895. return ret;
  896. }
  897. /*
  898. * nfs_pageio_resend - Transfer requests to new descriptor and resend
  899. * @hdr - the pgio header to move request from
  900. * @desc - the pageio descriptor to add requests to
  901. *
  902. * Try to move each request (nfs_page) from @hdr to @desc then attempt
  903. * to send them.
  904. *
  905. * Returns 0 on success and < 0 on error.
  906. */
  907. int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
  908. struct nfs_pgio_header *hdr)
  909. {
  910. LIST_HEAD(failed);
  911. desc->pg_dreq = hdr->dreq;
  912. while (!list_empty(&hdr->pages)) {
  913. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  914. nfs_list_remove_request(req);
  915. if (!nfs_pageio_add_request(desc, req))
  916. nfs_list_add_request(req, &failed);
  917. }
  918. nfs_pageio_complete(desc);
  919. if (!list_empty(&failed)) {
  920. list_move(&failed, &hdr->pages);
  921. return -EIO;
  922. }
  923. return 0;
  924. }
  925. EXPORT_SYMBOL_GPL(nfs_pageio_resend);
  926. /**
  927. * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
  928. * @desc: pointer to io descriptor
  929. */
  930. void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
  931. {
  932. for (;;) {
  933. nfs_pageio_doio(desc);
  934. if (!desc->pg_recoalesce)
  935. break;
  936. if (!nfs_do_recoalesce(desc))
  937. break;
  938. }
  939. }
  940. /**
  941. * nfs_pageio_cond_complete - Conditional I/O completion
  942. * @desc: pointer to io descriptor
  943. * @index: page index
  944. *
  945. * It is important to ensure that processes don't try to take locks
  946. * on non-contiguous ranges of pages as that might deadlock. This
  947. * function should be called before attempting to wait on a locked
  948. * nfs_page. It will complete the I/O if the page index 'index'
  949. * is not contiguous with the existing list of pages in 'desc'.
  950. */
  951. void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
  952. {
  953. if (!list_empty(&desc->pg_list)) {
  954. struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
  955. if (index != prev->wb_index + 1)
  956. nfs_pageio_complete(desc);
  957. }
  958. }
  959. int __init nfs_init_nfspagecache(void)
  960. {
  961. nfs_page_cachep = kmem_cache_create("nfs_page",
  962. sizeof(struct nfs_page),
  963. 0, SLAB_HWCACHE_ALIGN,
  964. NULL);
  965. if (nfs_page_cachep == NULL)
  966. return -ENOMEM;
  967. return 0;
  968. }
  969. void nfs_destroy_nfspagecache(void)
  970. {
  971. kmem_cache_destroy(nfs_page_cachep);
  972. }
  973. static const struct rpc_call_ops nfs_pgio_common_ops = {
  974. .rpc_call_prepare = nfs_pgio_prepare,
  975. .rpc_call_done = nfs_pgio_result,
  976. .rpc_release = nfs_pgio_release,
  977. };
  978. const struct nfs_pageio_ops nfs_pgio_rw_ops = {
  979. .pg_test = nfs_generic_pg_test,
  980. .pg_doio = nfs_generic_pg_pgios,
  981. };