pagelist.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. /*
  429. * Limit the request size so that we can still allocate a page array
  430. * for it without upsetting the slab allocator.
  431. */
  432. if (((desc->pg_count + req->wb_bytes) >> PAGE_SHIFT) *
  433. sizeof(struct page) > PAGE_SIZE)
  434. return 0;
  435. return min(desc->pg_bsize - desc->pg_count, (size_t)req->wb_bytes);
  436. }
  437. EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
  438. struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *ops)
  439. {
  440. struct nfs_pgio_header *hdr = ops->rw_alloc_header();
  441. if (hdr) {
  442. INIT_LIST_HEAD(&hdr->pages);
  443. spin_lock_init(&hdr->lock);
  444. hdr->rw_ops = ops;
  445. }
  446. return hdr;
  447. }
  448. EXPORT_SYMBOL_GPL(nfs_pgio_header_alloc);
  449. /*
  450. * nfs_pgio_header_free - Free a read or write header
  451. * @hdr: The header to free
  452. */
  453. void nfs_pgio_header_free(struct nfs_pgio_header *hdr)
  454. {
  455. hdr->rw_ops->rw_free_header(hdr);
  456. }
  457. EXPORT_SYMBOL_GPL(nfs_pgio_header_free);
  458. /**
  459. * nfs_pgio_data_destroy - make @hdr suitable for reuse
  460. *
  461. * Frees memory and releases refs from nfs_generic_pgio, so that it may
  462. * be called again.
  463. *
  464. * @hdr: A header that has had nfs_generic_pgio called
  465. */
  466. void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr)
  467. {
  468. if (hdr->args.context)
  469. put_nfs_open_context(hdr->args.context);
  470. if (hdr->page_array.pagevec != hdr->page_array.page_array)
  471. kfree(hdr->page_array.pagevec);
  472. }
  473. EXPORT_SYMBOL_GPL(nfs_pgio_data_destroy);
  474. /**
  475. * nfs_pgio_rpcsetup - Set up arguments for a pageio call
  476. * @hdr: The pageio hdr
  477. * @count: Number of bytes to read
  478. * @offset: Initial offset
  479. * @how: How to commit data (writes only)
  480. * @cinfo: Commit information for the call (writes only)
  481. */
  482. static void nfs_pgio_rpcsetup(struct nfs_pgio_header *hdr,
  483. unsigned int count, unsigned int offset,
  484. int how, struct nfs_commit_info *cinfo)
  485. {
  486. struct nfs_page *req = hdr->req;
  487. /* Set up the RPC argument and reply structs
  488. * NB: take care not to mess about with hdr->commit et al. */
  489. hdr->args.fh = NFS_FH(hdr->inode);
  490. hdr->args.offset = req_offset(req) + offset;
  491. /* pnfs_set_layoutcommit needs this */
  492. hdr->mds_offset = hdr->args.offset;
  493. hdr->args.pgbase = req->wb_pgbase + offset;
  494. hdr->args.pages = hdr->page_array.pagevec;
  495. hdr->args.count = count;
  496. hdr->args.context = get_nfs_open_context(req->wb_context);
  497. hdr->args.lock_context = req->wb_lock_context;
  498. hdr->args.stable = NFS_UNSTABLE;
  499. switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
  500. case 0:
  501. break;
  502. case FLUSH_COND_STABLE:
  503. if (nfs_reqs_to_commit(cinfo))
  504. break;
  505. default:
  506. hdr->args.stable = NFS_FILE_SYNC;
  507. }
  508. hdr->res.fattr = &hdr->fattr;
  509. hdr->res.count = count;
  510. hdr->res.eof = 0;
  511. hdr->res.verf = &hdr->verf;
  512. nfs_fattr_init(&hdr->fattr);
  513. }
  514. /**
  515. * nfs_pgio_prepare - Prepare pageio hdr to go over the wire
  516. * @task: The current task
  517. * @calldata: pageio header to prepare
  518. */
  519. static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
  520. {
  521. struct nfs_pgio_header *hdr = calldata;
  522. int err;
  523. err = NFS_PROTO(hdr->inode)->pgio_rpc_prepare(task, hdr);
  524. if (err)
  525. rpc_exit(task, err);
  526. }
  527. int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
  528. const struct rpc_call_ops *call_ops, int how, int flags)
  529. {
  530. struct rpc_task *task;
  531. struct rpc_message msg = {
  532. .rpc_argp = &hdr->args,
  533. .rpc_resp = &hdr->res,
  534. .rpc_cred = hdr->cred,
  535. };
  536. struct rpc_task_setup task_setup_data = {
  537. .rpc_client = clnt,
  538. .task = &hdr->task,
  539. .rpc_message = &msg,
  540. .callback_ops = call_ops,
  541. .callback_data = hdr,
  542. .workqueue = nfsiod_workqueue,
  543. .flags = RPC_TASK_ASYNC | flags,
  544. };
  545. int ret = 0;
  546. hdr->rw_ops->rw_initiate(hdr, &msg, &task_setup_data, how);
  547. dprintk("NFS: %5u initiated pgio call "
  548. "(req %s/%llu, %u bytes @ offset %llu)\n",
  549. hdr->task.tk_pid,
  550. hdr->inode->i_sb->s_id,
  551. (unsigned long long)NFS_FILEID(hdr->inode),
  552. hdr->args.count,
  553. (unsigned long long)hdr->args.offset);
  554. task = rpc_run_task(&task_setup_data);
  555. if (IS_ERR(task)) {
  556. ret = PTR_ERR(task);
  557. goto out;
  558. }
  559. if (how & FLUSH_SYNC) {
  560. ret = rpc_wait_for_completion_task(task);
  561. if (ret == 0)
  562. ret = task->tk_status;
  563. }
  564. rpc_put_task(task);
  565. out:
  566. return ret;
  567. }
  568. EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
  569. /**
  570. * nfs_pgio_error - Clean up from a pageio error
  571. * @desc: IO descriptor
  572. * @hdr: pageio header
  573. */
  574. static int nfs_pgio_error(struct nfs_pageio_descriptor *desc,
  575. struct nfs_pgio_header *hdr)
  576. {
  577. set_bit(NFS_IOHDR_REDO, &hdr->flags);
  578. nfs_pgio_data_destroy(hdr);
  579. hdr->completion_ops->completion(hdr);
  580. desc->pg_completion_ops->error_cleanup(&desc->pg_list);
  581. return -ENOMEM;
  582. }
  583. /**
  584. * nfs_pgio_release - Release pageio data
  585. * @calldata: The pageio header to release
  586. */
  587. static void nfs_pgio_release(void *calldata)
  588. {
  589. struct nfs_pgio_header *hdr = calldata;
  590. if (hdr->rw_ops->rw_release)
  591. hdr->rw_ops->rw_release(hdr);
  592. nfs_pgio_data_destroy(hdr);
  593. hdr->completion_ops->completion(hdr);
  594. }
  595. /**
  596. * nfs_pageio_init - initialise a page io descriptor
  597. * @desc: pointer to descriptor
  598. * @inode: pointer to inode
  599. * @doio: pointer to io function
  600. * @bsize: io block size
  601. * @io_flags: extra parameters for the io function
  602. */
  603. void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
  604. struct inode *inode,
  605. const struct nfs_pageio_ops *pg_ops,
  606. const struct nfs_pgio_completion_ops *compl_ops,
  607. const struct nfs_rw_ops *rw_ops,
  608. size_t bsize,
  609. int io_flags)
  610. {
  611. INIT_LIST_HEAD(&desc->pg_list);
  612. desc->pg_bytes_written = 0;
  613. desc->pg_count = 0;
  614. desc->pg_bsize = bsize;
  615. desc->pg_base = 0;
  616. desc->pg_moreio = 0;
  617. desc->pg_recoalesce = 0;
  618. desc->pg_inode = inode;
  619. desc->pg_ops = pg_ops;
  620. desc->pg_completion_ops = compl_ops;
  621. desc->pg_rw_ops = rw_ops;
  622. desc->pg_ioflags = io_flags;
  623. desc->pg_error = 0;
  624. desc->pg_lseg = NULL;
  625. desc->pg_dreq = NULL;
  626. desc->pg_layout_private = NULL;
  627. }
  628. EXPORT_SYMBOL_GPL(nfs_pageio_init);
  629. /**
  630. * nfs_pgio_result - Basic pageio error handling
  631. * @task: The task that ran
  632. * @calldata: Pageio header to check
  633. */
  634. static void nfs_pgio_result(struct rpc_task *task, void *calldata)
  635. {
  636. struct nfs_pgio_header *hdr = calldata;
  637. struct inode *inode = hdr->inode;
  638. dprintk("NFS: %s: %5u, (status %d)\n", __func__,
  639. task->tk_pid, task->tk_status);
  640. if (hdr->rw_ops->rw_done(task, hdr, inode) != 0)
  641. return;
  642. if (task->tk_status < 0)
  643. nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset);
  644. else
  645. hdr->rw_ops->rw_result(task, hdr);
  646. }
  647. /*
  648. * Create an RPC task for the given read or write request and kick it.
  649. * The page must have been locked by the caller.
  650. *
  651. * It may happen that the page we're passed is not marked dirty.
  652. * This is the case if nfs_updatepage detects a conflicting request
  653. * that has been written but not committed.
  654. */
  655. int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
  656. struct nfs_pgio_header *hdr)
  657. {
  658. struct nfs_page *req;
  659. struct page **pages,
  660. *last_page;
  661. struct list_head *head = &desc->pg_list;
  662. struct nfs_commit_info cinfo;
  663. unsigned int pagecount, pageused;
  664. pagecount = nfs_page_array_len(desc->pg_base, desc->pg_count);
  665. if (!nfs_pgarray_set(&hdr->page_array, pagecount))
  666. return nfs_pgio_error(desc, hdr);
  667. nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
  668. pages = hdr->page_array.pagevec;
  669. last_page = NULL;
  670. pageused = 0;
  671. while (!list_empty(head)) {
  672. req = nfs_list_entry(head->next);
  673. nfs_list_remove_request(req);
  674. nfs_list_add_request(req, &hdr->pages);
  675. if (!last_page || last_page != req->wb_page) {
  676. pageused++;
  677. if (pageused > pagecount)
  678. break;
  679. *pages++ = last_page = req->wb_page;
  680. }
  681. }
  682. if (WARN_ON_ONCE(pageused != pagecount))
  683. return nfs_pgio_error(desc, hdr);
  684. if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
  685. (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
  686. desc->pg_ioflags &= ~FLUSH_COND_STABLE;
  687. /* Set up the argument struct */
  688. nfs_pgio_rpcsetup(hdr, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
  689. desc->pg_rpc_callops = &nfs_pgio_common_ops;
  690. return 0;
  691. }
  692. EXPORT_SYMBOL_GPL(nfs_generic_pgio);
  693. static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
  694. {
  695. struct nfs_pgio_header *hdr;
  696. int ret;
  697. hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
  698. if (!hdr) {
  699. desc->pg_completion_ops->error_cleanup(&desc->pg_list);
  700. return -ENOMEM;
  701. }
  702. nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
  703. ret = nfs_generic_pgio(desc, hdr);
  704. if (ret == 0)
  705. ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
  706. hdr, desc->pg_rpc_callops,
  707. desc->pg_ioflags, 0);
  708. return ret;
  709. }
  710. static bool nfs_match_open_context(const struct nfs_open_context *ctx1,
  711. const struct nfs_open_context *ctx2)
  712. {
  713. return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state;
  714. }
  715. static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
  716. const struct nfs_lock_context *l2)
  717. {
  718. return l1->lockowner.l_owner == l2->lockowner.l_owner
  719. && l1->lockowner.l_pid == l2->lockowner.l_pid;
  720. }
  721. /**
  722. * nfs_can_coalesce_requests - test two requests for compatibility
  723. * @prev: pointer to nfs_page
  724. * @req: pointer to nfs_page
  725. *
  726. * The nfs_page structures 'prev' and 'req' are compared to ensure that the
  727. * page data area they describe is contiguous, and that their RPC
  728. * credentials, NFSv4 open state, and lockowners are the same.
  729. *
  730. * Return 'true' if this is the case, else return 'false'.
  731. */
  732. static bool nfs_can_coalesce_requests(struct nfs_page *prev,
  733. struct nfs_page *req,
  734. struct nfs_pageio_descriptor *pgio)
  735. {
  736. size_t size;
  737. if (prev) {
  738. if (!nfs_match_open_context(req->wb_context, prev->wb_context))
  739. return false;
  740. if (req->wb_context->dentry->d_inode->i_flock != NULL &&
  741. !nfs_match_lock_context(req->wb_lock_context,
  742. prev->wb_lock_context))
  743. return false;
  744. if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
  745. return false;
  746. if (req->wb_page == prev->wb_page) {
  747. if (req->wb_pgbase != prev->wb_pgbase + prev->wb_bytes)
  748. return false;
  749. } else {
  750. if (req->wb_pgbase != 0 ||
  751. prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
  752. return false;
  753. }
  754. }
  755. size = pgio->pg_ops->pg_test(pgio, prev, req);
  756. WARN_ON_ONCE(size > req->wb_bytes);
  757. if (size && size < req->wb_bytes)
  758. req->wb_bytes = size;
  759. return size > 0;
  760. }
  761. /**
  762. * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
  763. * @desc: destination io descriptor
  764. * @req: request
  765. *
  766. * Returns true if the request 'req' was successfully coalesced into the
  767. * existing list of pages 'desc'.
  768. */
  769. static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
  770. struct nfs_page *req)
  771. {
  772. struct nfs_page *prev = NULL;
  773. if (desc->pg_count != 0) {
  774. prev = nfs_list_entry(desc->pg_list.prev);
  775. } else {
  776. if (desc->pg_ops->pg_init)
  777. desc->pg_ops->pg_init(desc, req);
  778. desc->pg_base = req->wb_pgbase;
  779. }
  780. if (!nfs_can_coalesce_requests(prev, req, desc))
  781. return 0;
  782. nfs_list_remove_request(req);
  783. nfs_list_add_request(req, &desc->pg_list);
  784. desc->pg_count += req->wb_bytes;
  785. return 1;
  786. }
  787. /*
  788. * Helper for nfs_pageio_add_request and nfs_pageio_complete
  789. */
  790. static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
  791. {
  792. if (!list_empty(&desc->pg_list)) {
  793. int error = desc->pg_ops->pg_doio(desc);
  794. if (error < 0)
  795. desc->pg_error = error;
  796. else
  797. desc->pg_bytes_written += desc->pg_count;
  798. }
  799. if (list_empty(&desc->pg_list)) {
  800. desc->pg_count = 0;
  801. desc->pg_base = 0;
  802. }
  803. }
  804. /**
  805. * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
  806. * @desc: destination io descriptor
  807. * @req: request
  808. *
  809. * This may split a request into subrequests which are all part of the
  810. * same page group.
  811. *
  812. * Returns true if the request 'req' was successfully coalesced into the
  813. * existing list of pages 'desc'.
  814. */
  815. static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  816. struct nfs_page *req)
  817. {
  818. struct nfs_page *subreq;
  819. unsigned int bytes_left = 0;
  820. unsigned int offset, pgbase;
  821. nfs_page_group_lock(req, false);
  822. subreq = req;
  823. bytes_left = subreq->wb_bytes;
  824. offset = subreq->wb_offset;
  825. pgbase = subreq->wb_pgbase;
  826. do {
  827. if (!nfs_pageio_do_add_request(desc, subreq)) {
  828. /* make sure pg_test call(s) did nothing */
  829. WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
  830. WARN_ON_ONCE(subreq->wb_offset != offset);
  831. WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
  832. nfs_page_group_unlock(req);
  833. desc->pg_moreio = 1;
  834. nfs_pageio_doio(desc);
  835. if (desc->pg_error < 0)
  836. return 0;
  837. if (desc->pg_recoalesce)
  838. return 0;
  839. /* retry add_request for this subreq */
  840. nfs_page_group_lock(req, false);
  841. continue;
  842. }
  843. /* check for buggy pg_test call(s) */
  844. WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
  845. WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
  846. WARN_ON_ONCE(subreq->wb_bytes == 0);
  847. bytes_left -= subreq->wb_bytes;
  848. offset += subreq->wb_bytes;
  849. pgbase += subreq->wb_bytes;
  850. if (bytes_left) {
  851. subreq = nfs_create_request(req->wb_context,
  852. req->wb_page,
  853. subreq, pgbase, bytes_left);
  854. if (IS_ERR(subreq))
  855. goto err_ptr;
  856. nfs_lock_request(subreq);
  857. subreq->wb_offset = offset;
  858. subreq->wb_index = req->wb_index;
  859. }
  860. } while (bytes_left > 0);
  861. nfs_page_group_unlock(req);
  862. return 1;
  863. err_ptr:
  864. desc->pg_error = PTR_ERR(subreq);
  865. nfs_page_group_unlock(req);
  866. return 0;
  867. }
  868. static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
  869. {
  870. LIST_HEAD(head);
  871. do {
  872. list_splice_init(&desc->pg_list, &head);
  873. desc->pg_bytes_written -= desc->pg_count;
  874. desc->pg_count = 0;
  875. desc->pg_base = 0;
  876. desc->pg_recoalesce = 0;
  877. desc->pg_moreio = 0;
  878. while (!list_empty(&head)) {
  879. struct nfs_page *req;
  880. req = list_first_entry(&head, struct nfs_page, wb_list);
  881. nfs_list_remove_request(req);
  882. if (__nfs_pageio_add_request(desc, req))
  883. continue;
  884. if (desc->pg_error < 0)
  885. return 0;
  886. break;
  887. }
  888. } while (desc->pg_recoalesce);
  889. return 1;
  890. }
  891. int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  892. struct nfs_page *req)
  893. {
  894. int ret;
  895. do {
  896. ret = __nfs_pageio_add_request(desc, req);
  897. if (ret)
  898. break;
  899. if (desc->pg_error < 0)
  900. break;
  901. ret = nfs_do_recoalesce(desc);
  902. } while (ret);
  903. return ret;
  904. }
  905. /*
  906. * nfs_pageio_resend - Transfer requests to new descriptor and resend
  907. * @hdr - the pgio header to move request from
  908. * @desc - the pageio descriptor to add requests to
  909. *
  910. * Try to move each request (nfs_page) from @hdr to @desc then attempt
  911. * to send them.
  912. *
  913. * Returns 0 on success and < 0 on error.
  914. */
  915. int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
  916. struct nfs_pgio_header *hdr)
  917. {
  918. LIST_HEAD(failed);
  919. desc->pg_dreq = hdr->dreq;
  920. while (!list_empty(&hdr->pages)) {
  921. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  922. nfs_list_remove_request(req);
  923. if (!nfs_pageio_add_request(desc, req))
  924. nfs_list_add_request(req, &failed);
  925. }
  926. nfs_pageio_complete(desc);
  927. if (!list_empty(&failed)) {
  928. list_move(&failed, &hdr->pages);
  929. return -EIO;
  930. }
  931. return 0;
  932. }
  933. EXPORT_SYMBOL_GPL(nfs_pageio_resend);
  934. /**
  935. * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
  936. * @desc: pointer to io descriptor
  937. */
  938. void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
  939. {
  940. for (;;) {
  941. nfs_pageio_doio(desc);
  942. if (!desc->pg_recoalesce)
  943. break;
  944. if (!nfs_do_recoalesce(desc))
  945. break;
  946. }
  947. }
  948. /**
  949. * nfs_pageio_cond_complete - Conditional I/O completion
  950. * @desc: pointer to io descriptor
  951. * @index: page index
  952. *
  953. * It is important to ensure that processes don't try to take locks
  954. * on non-contiguous ranges of pages as that might deadlock. This
  955. * function should be called before attempting to wait on a locked
  956. * nfs_page. It will complete the I/O if the page index 'index'
  957. * is not contiguous with the existing list of pages in 'desc'.
  958. */
  959. void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
  960. {
  961. if (!list_empty(&desc->pg_list)) {
  962. struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
  963. if (index != prev->wb_index + 1)
  964. nfs_pageio_complete(desc);
  965. }
  966. }
  967. int __init nfs_init_nfspagecache(void)
  968. {
  969. nfs_page_cachep = kmem_cache_create("nfs_page",
  970. sizeof(struct nfs_page),
  971. 0, SLAB_HWCACHE_ALIGN,
  972. NULL);
  973. if (nfs_page_cachep == NULL)
  974. return -ENOMEM;
  975. return 0;
  976. }
  977. void nfs_destroy_nfspagecache(void)
  978. {
  979. kmem_cache_destroy(nfs_page_cachep);
  980. }
  981. static const struct rpc_call_ops nfs_pgio_common_ops = {
  982. .rpc_call_prepare = nfs_pgio_prepare,
  983. .rpc_call_done = nfs_pgio_result,
  984. .rpc_release = nfs_pgio_release,
  985. };
  986. const struct nfs_pageio_ops nfs_pgio_rw_ops = {
  987. .pg_test = nfs_generic_pg_test,
  988. .pg_doio = nfs_generic_pg_pgios,
  989. };