pagelist.c 33 KB

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