pagelist.c 27 KB

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