pagelist.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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. if (page) {
  300. req->wb_index = page_index(page);
  301. get_page(page);
  302. }
  303. req->wb_offset = offset;
  304. req->wb_pgbase = offset;
  305. req->wb_bytes = count;
  306. req->wb_context = get_nfs_open_context(ctx);
  307. kref_init(&req->wb_kref);
  308. nfs_page_group_init(req, last);
  309. return req;
  310. }
  311. /**
  312. * nfs_unlock_request - Unlock request and wake up sleepers.
  313. * @req:
  314. */
  315. void nfs_unlock_request(struct nfs_page *req)
  316. {
  317. if (!NFS_WBACK_BUSY(req)) {
  318. printk(KERN_ERR "NFS: Invalid unlock attempted\n");
  319. BUG();
  320. }
  321. smp_mb__before_atomic();
  322. clear_bit(PG_BUSY, &req->wb_flags);
  323. smp_mb__after_atomic();
  324. wake_up_bit(&req->wb_flags, PG_BUSY);
  325. }
  326. /**
  327. * nfs_unlock_and_release_request - Unlock request and release the nfs_page
  328. * @req:
  329. */
  330. void nfs_unlock_and_release_request(struct nfs_page *req)
  331. {
  332. nfs_unlock_request(req);
  333. nfs_release_request(req);
  334. }
  335. /*
  336. * nfs_clear_request - Free up all resources allocated to the request
  337. * @req:
  338. *
  339. * Release page and open context resources associated with a read/write
  340. * request after it has completed.
  341. */
  342. static void nfs_clear_request(struct nfs_page *req)
  343. {
  344. struct page *page = req->wb_page;
  345. struct nfs_open_context *ctx = req->wb_context;
  346. struct nfs_lock_context *l_ctx = req->wb_lock_context;
  347. if (page != NULL) {
  348. put_page(page);
  349. req->wb_page = NULL;
  350. }
  351. if (l_ctx != NULL) {
  352. if (atomic_dec_and_test(&l_ctx->io_count))
  353. wake_up_atomic_t(&l_ctx->io_count);
  354. nfs_put_lock_context(l_ctx);
  355. req->wb_lock_context = NULL;
  356. }
  357. if (ctx != NULL) {
  358. put_nfs_open_context(ctx);
  359. req->wb_context = NULL;
  360. }
  361. }
  362. /**
  363. * nfs_release_request - Release the count on an NFS read/write request
  364. * @req: request to release
  365. *
  366. * Note: Should never be called with the spinlock held!
  367. */
  368. void nfs_free_request(struct nfs_page *req)
  369. {
  370. WARN_ON_ONCE(req->wb_this_page != req);
  371. /* extra debug: make sure no sync bits are still set */
  372. WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
  373. WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags));
  374. WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags));
  375. WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags));
  376. WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags));
  377. /* Release struct file and open context */
  378. nfs_clear_request(req);
  379. nfs_page_free(req);
  380. }
  381. void nfs_release_request(struct nfs_page *req)
  382. {
  383. kref_put(&req->wb_kref, nfs_page_group_destroy);
  384. }
  385. /**
  386. * nfs_wait_on_request - Wait for a request to complete.
  387. * @req: request to wait upon.
  388. *
  389. * Interruptible by fatal signals only.
  390. * The user is responsible for holding a count on the request.
  391. */
  392. int
  393. nfs_wait_on_request(struct nfs_page *req)
  394. {
  395. return wait_on_bit_io(&req->wb_flags, PG_BUSY,
  396. TASK_UNINTERRUPTIBLE);
  397. }
  398. /*
  399. * nfs_generic_pg_test - determine if requests can be coalesced
  400. * @desc: pointer to descriptor
  401. * @prev: previous request in desc, or NULL
  402. * @req: this request
  403. *
  404. * Returns zero if @req can be coalesced into @desc, otherwise it returns
  405. * the size of the request.
  406. */
  407. size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
  408. struct nfs_page *prev, struct nfs_page *req)
  409. {
  410. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  411. if (mirror->pg_count > mirror->pg_bsize) {
  412. /* should never happen */
  413. WARN_ON_ONCE(1);
  414. return 0;
  415. }
  416. /*
  417. * Limit the request size so that we can still allocate a page array
  418. * for it without upsetting the slab allocator.
  419. */
  420. if (((mirror->pg_count + req->wb_bytes) >> PAGE_SHIFT) *
  421. sizeof(struct page *) > PAGE_SIZE)
  422. return 0;
  423. return min(mirror->pg_bsize - mirror->pg_count, (size_t)req->wb_bytes);
  424. }
  425. EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
  426. struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *ops)
  427. {
  428. struct nfs_pgio_header *hdr = ops->rw_alloc_header();
  429. if (hdr) {
  430. INIT_LIST_HEAD(&hdr->pages);
  431. spin_lock_init(&hdr->lock);
  432. hdr->rw_ops = ops;
  433. }
  434. return hdr;
  435. }
  436. EXPORT_SYMBOL_GPL(nfs_pgio_header_alloc);
  437. /*
  438. * nfs_pgio_header_free - Free a read or write header
  439. * @hdr: The header to free
  440. */
  441. void nfs_pgio_header_free(struct nfs_pgio_header *hdr)
  442. {
  443. hdr->rw_ops->rw_free_header(hdr);
  444. }
  445. EXPORT_SYMBOL_GPL(nfs_pgio_header_free);
  446. /**
  447. * nfs_pgio_data_destroy - make @hdr suitable for reuse
  448. *
  449. * Frees memory and releases refs from nfs_generic_pgio, so that it may
  450. * be called again.
  451. *
  452. * @hdr: A header that has had nfs_generic_pgio called
  453. */
  454. void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr)
  455. {
  456. if (hdr->args.context)
  457. put_nfs_open_context(hdr->args.context);
  458. if (hdr->page_array.pagevec != hdr->page_array.page_array)
  459. kfree(hdr->page_array.pagevec);
  460. }
  461. EXPORT_SYMBOL_GPL(nfs_pgio_data_destroy);
  462. /**
  463. * nfs_pgio_rpcsetup - Set up arguments for a pageio call
  464. * @hdr: The pageio hdr
  465. * @count: Number of bytes to read
  466. * @offset: Initial offset
  467. * @how: How to commit data (writes only)
  468. * @cinfo: Commit information for the call (writes only)
  469. */
  470. static void nfs_pgio_rpcsetup(struct nfs_pgio_header *hdr,
  471. unsigned int count, unsigned int offset,
  472. int how, struct nfs_commit_info *cinfo)
  473. {
  474. struct nfs_page *req = hdr->req;
  475. /* Set up the RPC argument and reply structs
  476. * NB: take care not to mess about with hdr->commit et al. */
  477. hdr->args.fh = NFS_FH(hdr->inode);
  478. hdr->args.offset = req_offset(req) + offset;
  479. /* pnfs_set_layoutcommit needs this */
  480. hdr->mds_offset = hdr->args.offset;
  481. hdr->args.pgbase = req->wb_pgbase + offset;
  482. hdr->args.pages = hdr->page_array.pagevec;
  483. hdr->args.count = count;
  484. hdr->args.context = get_nfs_open_context(req->wb_context);
  485. hdr->args.lock_context = req->wb_lock_context;
  486. hdr->args.stable = NFS_UNSTABLE;
  487. switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
  488. case 0:
  489. break;
  490. case FLUSH_COND_STABLE:
  491. if (nfs_reqs_to_commit(cinfo))
  492. break;
  493. default:
  494. hdr->args.stable = NFS_FILE_SYNC;
  495. }
  496. hdr->res.fattr = &hdr->fattr;
  497. hdr->res.count = count;
  498. hdr->res.eof = 0;
  499. hdr->res.verf = &hdr->verf;
  500. nfs_fattr_init(&hdr->fattr);
  501. }
  502. /**
  503. * nfs_pgio_prepare - Prepare pageio hdr to go over the wire
  504. * @task: The current task
  505. * @calldata: pageio header to prepare
  506. */
  507. static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
  508. {
  509. struct nfs_pgio_header *hdr = calldata;
  510. int err;
  511. err = NFS_PROTO(hdr->inode)->pgio_rpc_prepare(task, hdr);
  512. if (err)
  513. rpc_exit(task, err);
  514. }
  515. int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
  516. struct rpc_cred *cred, const struct nfs_rpc_ops *rpc_ops,
  517. const struct rpc_call_ops *call_ops, int how, int flags)
  518. {
  519. struct rpc_task *task;
  520. struct rpc_message msg = {
  521. .rpc_argp = &hdr->args,
  522. .rpc_resp = &hdr->res,
  523. .rpc_cred = cred,
  524. };
  525. struct rpc_task_setup task_setup_data = {
  526. .rpc_client = clnt,
  527. .task = &hdr->task,
  528. .rpc_message = &msg,
  529. .callback_ops = call_ops,
  530. .callback_data = hdr,
  531. .workqueue = nfsiod_workqueue,
  532. .flags = RPC_TASK_ASYNC | flags,
  533. };
  534. int ret = 0;
  535. hdr->rw_ops->rw_initiate(hdr, &msg, rpc_ops, &task_setup_data, how);
  536. dprintk("NFS: initiated pgio call "
  537. "(req %s/%llu, %u bytes @ offset %llu)\n",
  538. hdr->inode->i_sb->s_id,
  539. (unsigned long long)NFS_FILEID(hdr->inode),
  540. hdr->args.count,
  541. (unsigned long long)hdr->args.offset);
  542. task = rpc_run_task(&task_setup_data);
  543. if (IS_ERR(task)) {
  544. ret = PTR_ERR(task);
  545. goto out;
  546. }
  547. if (how & FLUSH_SYNC) {
  548. ret = rpc_wait_for_completion_task(task);
  549. if (ret == 0)
  550. ret = task->tk_status;
  551. }
  552. rpc_put_task(task);
  553. out:
  554. return ret;
  555. }
  556. EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
  557. /**
  558. * nfs_pgio_error - Clean up from a pageio error
  559. * @desc: IO descriptor
  560. * @hdr: pageio header
  561. */
  562. static void nfs_pgio_error(struct nfs_pgio_header *hdr)
  563. {
  564. set_bit(NFS_IOHDR_REDO, &hdr->flags);
  565. nfs_pgio_data_destroy(hdr);
  566. hdr->completion_ops->completion(hdr);
  567. }
  568. /**
  569. * nfs_pgio_release - Release pageio data
  570. * @calldata: The pageio header to release
  571. */
  572. static void nfs_pgio_release(void *calldata)
  573. {
  574. struct nfs_pgio_header *hdr = calldata;
  575. nfs_pgio_data_destroy(hdr);
  576. hdr->completion_ops->completion(hdr);
  577. }
  578. static void nfs_pageio_mirror_init(struct nfs_pgio_mirror *mirror,
  579. unsigned int bsize)
  580. {
  581. INIT_LIST_HEAD(&mirror->pg_list);
  582. mirror->pg_bytes_written = 0;
  583. mirror->pg_count = 0;
  584. mirror->pg_bsize = bsize;
  585. mirror->pg_base = 0;
  586. mirror->pg_recoalesce = 0;
  587. }
  588. /**
  589. * nfs_pageio_init - initialise a page io descriptor
  590. * @desc: pointer to descriptor
  591. * @inode: pointer to inode
  592. * @pg_ops: pointer to pageio operations
  593. * @compl_ops: pointer to pageio completion operations
  594. * @rw_ops: pointer to nfs read/write operations
  595. * @bsize: io block size
  596. * @io_flags: extra parameters for the io function
  597. */
  598. void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
  599. struct inode *inode,
  600. const struct nfs_pageio_ops *pg_ops,
  601. const struct nfs_pgio_completion_ops *compl_ops,
  602. const struct nfs_rw_ops *rw_ops,
  603. size_t bsize,
  604. int io_flags)
  605. {
  606. struct nfs_pgio_mirror *new;
  607. int i;
  608. desc->pg_moreio = 0;
  609. desc->pg_inode = inode;
  610. desc->pg_ops = pg_ops;
  611. desc->pg_completion_ops = compl_ops;
  612. desc->pg_rw_ops = rw_ops;
  613. desc->pg_ioflags = io_flags;
  614. desc->pg_error = 0;
  615. desc->pg_lseg = NULL;
  616. desc->pg_dreq = NULL;
  617. desc->pg_layout_private = NULL;
  618. desc->pg_bsize = bsize;
  619. desc->pg_mirror_count = 1;
  620. desc->pg_mirror_idx = 0;
  621. if (pg_ops->pg_get_mirror_count) {
  622. /* until we have a request, we don't have an lseg and no
  623. * idea how many mirrors there will be */
  624. new = kcalloc(NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX,
  625. sizeof(struct nfs_pgio_mirror), GFP_KERNEL);
  626. desc->pg_mirrors_dynamic = new;
  627. desc->pg_mirrors = new;
  628. for (i = 0; i < NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX; i++)
  629. nfs_pageio_mirror_init(&desc->pg_mirrors[i], bsize);
  630. } else {
  631. desc->pg_mirrors_dynamic = NULL;
  632. desc->pg_mirrors = desc->pg_mirrors_static;
  633. nfs_pageio_mirror_init(&desc->pg_mirrors[0], bsize);
  634. }
  635. }
  636. EXPORT_SYMBOL_GPL(nfs_pageio_init);
  637. /**
  638. * nfs_pgio_result - Basic pageio error handling
  639. * @task: The task that ran
  640. * @calldata: Pageio header to check
  641. */
  642. static void nfs_pgio_result(struct rpc_task *task, void *calldata)
  643. {
  644. struct nfs_pgio_header *hdr = calldata;
  645. struct inode *inode = hdr->inode;
  646. dprintk("NFS: %s: %5u, (status %d)\n", __func__,
  647. task->tk_pid, task->tk_status);
  648. if (hdr->rw_ops->rw_done(task, hdr, inode) != 0)
  649. return;
  650. if (task->tk_status < 0)
  651. nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset);
  652. else
  653. hdr->rw_ops->rw_result(task, hdr);
  654. }
  655. /*
  656. * Create an RPC task for the given read or write request and kick it.
  657. * The page must have been locked by the caller.
  658. *
  659. * It may happen that the page we're passed is not marked dirty.
  660. * This is the case if nfs_updatepage detects a conflicting request
  661. * that has been written but not committed.
  662. */
  663. int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
  664. struct nfs_pgio_header *hdr)
  665. {
  666. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  667. struct nfs_page *req;
  668. struct page **pages,
  669. *last_page;
  670. struct list_head *head = &mirror->pg_list;
  671. struct nfs_commit_info cinfo;
  672. unsigned int pagecount, pageused;
  673. pagecount = nfs_page_array_len(mirror->pg_base, mirror->pg_count);
  674. if (!nfs_pgarray_set(&hdr->page_array, pagecount)) {
  675. nfs_pgio_error(hdr);
  676. desc->pg_error = -ENOMEM;
  677. return desc->pg_error;
  678. }
  679. nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
  680. pages = hdr->page_array.pagevec;
  681. last_page = NULL;
  682. pageused = 0;
  683. while (!list_empty(head)) {
  684. req = nfs_list_entry(head->next);
  685. nfs_list_remove_request(req);
  686. nfs_list_add_request(req, &hdr->pages);
  687. if (!last_page || last_page != req->wb_page) {
  688. pageused++;
  689. if (pageused > pagecount)
  690. break;
  691. *pages++ = last_page = req->wb_page;
  692. }
  693. }
  694. if (WARN_ON_ONCE(pageused != pagecount)) {
  695. nfs_pgio_error(hdr);
  696. desc->pg_error = -EINVAL;
  697. return desc->pg_error;
  698. }
  699. if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
  700. (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
  701. desc->pg_ioflags &= ~FLUSH_COND_STABLE;
  702. /* Set up the argument struct */
  703. nfs_pgio_rpcsetup(hdr, mirror->pg_count, 0, desc->pg_ioflags, &cinfo);
  704. desc->pg_rpc_callops = &nfs_pgio_common_ops;
  705. return 0;
  706. }
  707. EXPORT_SYMBOL_GPL(nfs_generic_pgio);
  708. static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
  709. {
  710. struct nfs_pgio_header *hdr;
  711. int ret;
  712. hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
  713. if (!hdr) {
  714. desc->pg_error = -ENOMEM;
  715. return desc->pg_error;
  716. }
  717. nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
  718. ret = nfs_generic_pgio(desc, hdr);
  719. if (ret == 0)
  720. ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
  721. hdr,
  722. hdr->cred,
  723. NFS_PROTO(hdr->inode),
  724. desc->pg_rpc_callops,
  725. desc->pg_ioflags, 0);
  726. return ret;
  727. }
  728. /*
  729. * nfs_pageio_setup_mirroring - determine if mirroring is to be used
  730. * by calling the pg_get_mirror_count op
  731. */
  732. static int nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor *pgio,
  733. struct nfs_page *req)
  734. {
  735. int mirror_count = 1;
  736. if (!pgio->pg_ops->pg_get_mirror_count)
  737. return 0;
  738. mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
  739. if (pgio->pg_error < 0)
  740. return pgio->pg_error;
  741. if (!mirror_count || mirror_count > NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX)
  742. return -EINVAL;
  743. if (WARN_ON_ONCE(!pgio->pg_mirrors_dynamic))
  744. return -EINVAL;
  745. pgio->pg_mirror_count = mirror_count;
  746. return 0;
  747. }
  748. /*
  749. * nfs_pageio_stop_mirroring - stop using mirroring (set mirror count to 1)
  750. */
  751. void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio)
  752. {
  753. pgio->pg_mirror_count = 1;
  754. pgio->pg_mirror_idx = 0;
  755. }
  756. static void nfs_pageio_cleanup_mirroring(struct nfs_pageio_descriptor *pgio)
  757. {
  758. pgio->pg_mirror_count = 1;
  759. pgio->pg_mirror_idx = 0;
  760. pgio->pg_mirrors = pgio->pg_mirrors_static;
  761. kfree(pgio->pg_mirrors_dynamic);
  762. pgio->pg_mirrors_dynamic = NULL;
  763. }
  764. static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
  765. const struct nfs_lock_context *l2)
  766. {
  767. return l1->lockowner == l2->lockowner;
  768. }
  769. /**
  770. * nfs_can_coalesce_requests - test two requests for compatibility
  771. * @prev: pointer to nfs_page
  772. * @req: pointer to nfs_page
  773. *
  774. * The nfs_page structures 'prev' and 'req' are compared to ensure that the
  775. * page data area they describe is contiguous, and that their RPC
  776. * credentials, NFSv4 open state, and lockowners are the same.
  777. *
  778. * Return 'true' if this is the case, else return 'false'.
  779. */
  780. static bool nfs_can_coalesce_requests(struct nfs_page *prev,
  781. struct nfs_page *req,
  782. struct nfs_pageio_descriptor *pgio)
  783. {
  784. size_t size;
  785. struct file_lock_context *flctx;
  786. if (prev) {
  787. if (!nfs_match_open_context(req->wb_context, prev->wb_context))
  788. return false;
  789. flctx = d_inode(req->wb_context->dentry)->i_flctx;
  790. if (flctx != NULL &&
  791. !(list_empty_careful(&flctx->flc_posix) &&
  792. list_empty_careful(&flctx->flc_flock)) &&
  793. !nfs_match_lock_context(req->wb_lock_context,
  794. prev->wb_lock_context))
  795. return false;
  796. if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
  797. return false;
  798. if (req->wb_page == prev->wb_page) {
  799. if (req->wb_pgbase != prev->wb_pgbase + prev->wb_bytes)
  800. return false;
  801. } else {
  802. if (req->wb_pgbase != 0 ||
  803. prev->wb_pgbase + prev->wb_bytes != PAGE_SIZE)
  804. return false;
  805. }
  806. }
  807. size = pgio->pg_ops->pg_test(pgio, prev, req);
  808. WARN_ON_ONCE(size > req->wb_bytes);
  809. if (size && size < req->wb_bytes)
  810. req->wb_bytes = size;
  811. return size > 0;
  812. }
  813. /**
  814. * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
  815. * @desc: destination io descriptor
  816. * @req: request
  817. *
  818. * Returns true if the request 'req' was successfully coalesced into the
  819. * existing list of pages 'desc'.
  820. */
  821. static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
  822. struct nfs_page *req)
  823. {
  824. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  825. struct nfs_page *prev = NULL;
  826. if (mirror->pg_count != 0) {
  827. prev = nfs_list_entry(mirror->pg_list.prev);
  828. } else {
  829. if (desc->pg_ops->pg_init)
  830. desc->pg_ops->pg_init(desc, req);
  831. if (desc->pg_error < 0)
  832. return 0;
  833. mirror->pg_base = req->wb_pgbase;
  834. }
  835. if (!nfs_can_coalesce_requests(prev, req, desc))
  836. return 0;
  837. nfs_list_remove_request(req);
  838. nfs_list_add_request(req, &mirror->pg_list);
  839. mirror->pg_count += req->wb_bytes;
  840. return 1;
  841. }
  842. /*
  843. * Helper for nfs_pageio_add_request and nfs_pageio_complete
  844. */
  845. static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
  846. {
  847. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  848. if (!list_empty(&mirror->pg_list)) {
  849. int error = desc->pg_ops->pg_doio(desc);
  850. if (error < 0)
  851. desc->pg_error = error;
  852. else
  853. mirror->pg_bytes_written += mirror->pg_count;
  854. }
  855. if (list_empty(&mirror->pg_list)) {
  856. mirror->pg_count = 0;
  857. mirror->pg_base = 0;
  858. }
  859. }
  860. /**
  861. * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
  862. * @desc: destination io descriptor
  863. * @req: request
  864. *
  865. * This may split a request into subrequests which are all part of the
  866. * same page group.
  867. *
  868. * Returns true if the request 'req' was successfully coalesced into the
  869. * existing list of pages 'desc'.
  870. */
  871. static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  872. struct nfs_page *req)
  873. {
  874. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  875. struct nfs_page *subreq;
  876. unsigned int bytes_left = 0;
  877. unsigned int offset, pgbase;
  878. nfs_page_group_lock(req, false);
  879. subreq = req;
  880. bytes_left = subreq->wb_bytes;
  881. offset = subreq->wb_offset;
  882. pgbase = subreq->wb_pgbase;
  883. do {
  884. if (!nfs_pageio_do_add_request(desc, subreq)) {
  885. /* make sure pg_test call(s) did nothing */
  886. WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
  887. WARN_ON_ONCE(subreq->wb_offset != offset);
  888. WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
  889. nfs_page_group_unlock(req);
  890. desc->pg_moreio = 1;
  891. nfs_pageio_doio(desc);
  892. if (desc->pg_error < 0)
  893. return 0;
  894. if (mirror->pg_recoalesce)
  895. return 0;
  896. /* retry add_request for this subreq */
  897. nfs_page_group_lock(req, false);
  898. continue;
  899. }
  900. /* check for buggy pg_test call(s) */
  901. WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
  902. WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
  903. WARN_ON_ONCE(subreq->wb_bytes == 0);
  904. bytes_left -= subreq->wb_bytes;
  905. offset += subreq->wb_bytes;
  906. pgbase += subreq->wb_bytes;
  907. if (bytes_left) {
  908. subreq = nfs_create_request(req->wb_context,
  909. req->wb_page,
  910. subreq, pgbase, bytes_left);
  911. if (IS_ERR(subreq))
  912. goto err_ptr;
  913. nfs_lock_request(subreq);
  914. subreq->wb_offset = offset;
  915. subreq->wb_index = req->wb_index;
  916. }
  917. } while (bytes_left > 0);
  918. nfs_page_group_unlock(req);
  919. return 1;
  920. err_ptr:
  921. desc->pg_error = PTR_ERR(subreq);
  922. nfs_page_group_unlock(req);
  923. return 0;
  924. }
  925. static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
  926. {
  927. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  928. LIST_HEAD(head);
  929. do {
  930. list_splice_init(&mirror->pg_list, &head);
  931. mirror->pg_bytes_written -= mirror->pg_count;
  932. mirror->pg_count = 0;
  933. mirror->pg_base = 0;
  934. mirror->pg_recoalesce = 0;
  935. while (!list_empty(&head)) {
  936. struct nfs_page *req;
  937. req = list_first_entry(&head, struct nfs_page, wb_list);
  938. nfs_list_remove_request(req);
  939. if (__nfs_pageio_add_request(desc, req))
  940. continue;
  941. if (desc->pg_error < 0) {
  942. list_splice_tail(&head, &mirror->pg_list);
  943. mirror->pg_recoalesce = 1;
  944. return 0;
  945. }
  946. break;
  947. }
  948. } while (mirror->pg_recoalesce);
  949. return 1;
  950. }
  951. static int nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor *desc,
  952. struct nfs_page *req)
  953. {
  954. int ret;
  955. do {
  956. ret = __nfs_pageio_add_request(desc, req);
  957. if (ret)
  958. break;
  959. if (desc->pg_error < 0)
  960. break;
  961. ret = nfs_do_recoalesce(desc);
  962. } while (ret);
  963. return ret;
  964. }
  965. int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  966. struct nfs_page *req)
  967. {
  968. u32 midx;
  969. unsigned int pgbase, offset, bytes;
  970. struct nfs_page *dupreq, *lastreq;
  971. pgbase = req->wb_pgbase;
  972. offset = req->wb_offset;
  973. bytes = req->wb_bytes;
  974. nfs_pageio_setup_mirroring(desc, req);
  975. if (desc->pg_error < 0)
  976. goto out_failed;
  977. for (midx = 0; midx < desc->pg_mirror_count; midx++) {
  978. if (midx) {
  979. nfs_page_group_lock(req, false);
  980. /* find the last request */
  981. for (lastreq = req->wb_head;
  982. lastreq->wb_this_page != req->wb_head;
  983. lastreq = lastreq->wb_this_page)
  984. ;
  985. dupreq = nfs_create_request(req->wb_context,
  986. req->wb_page, lastreq, pgbase, bytes);
  987. if (IS_ERR(dupreq)) {
  988. nfs_page_group_unlock(req);
  989. desc->pg_error = PTR_ERR(dupreq);
  990. goto out_failed;
  991. }
  992. nfs_lock_request(dupreq);
  993. nfs_page_group_unlock(req);
  994. dupreq->wb_offset = offset;
  995. dupreq->wb_index = req->wb_index;
  996. } else
  997. dupreq = req;
  998. if (nfs_pgio_has_mirroring(desc))
  999. desc->pg_mirror_idx = midx;
  1000. if (!nfs_pageio_add_request_mirror(desc, dupreq))
  1001. goto out_failed;
  1002. }
  1003. return 1;
  1004. out_failed:
  1005. /*
  1006. * We might have failed before sending any reqs over wire.
  1007. * Clean up rest of the reqs in mirror pg_list.
  1008. */
  1009. if (desc->pg_error) {
  1010. struct nfs_pgio_mirror *mirror;
  1011. void (*func)(struct list_head *);
  1012. /* remember fatal errors */
  1013. if (nfs_error_is_fatal(desc->pg_error))
  1014. mapping_set_error(desc->pg_inode->i_mapping,
  1015. desc->pg_error);
  1016. func = desc->pg_completion_ops->error_cleanup;
  1017. for (midx = 0; midx < desc->pg_mirror_count; midx++) {
  1018. mirror = &desc->pg_mirrors[midx];
  1019. func(&mirror->pg_list);
  1020. }
  1021. }
  1022. return 0;
  1023. }
  1024. /*
  1025. * nfs_pageio_complete_mirror - Complete I/O on the current mirror of an
  1026. * nfs_pageio_descriptor
  1027. * @desc: pointer to io descriptor
  1028. * @mirror_idx: pointer to mirror index
  1029. */
  1030. static void nfs_pageio_complete_mirror(struct nfs_pageio_descriptor *desc,
  1031. u32 mirror_idx)
  1032. {
  1033. struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[mirror_idx];
  1034. u32 restore_idx = desc->pg_mirror_idx;
  1035. if (nfs_pgio_has_mirroring(desc))
  1036. desc->pg_mirror_idx = mirror_idx;
  1037. for (;;) {
  1038. nfs_pageio_doio(desc);
  1039. if (!mirror->pg_recoalesce)
  1040. break;
  1041. if (!nfs_do_recoalesce(desc))
  1042. break;
  1043. }
  1044. desc->pg_mirror_idx = restore_idx;
  1045. }
  1046. /*
  1047. * nfs_pageio_resend - Transfer requests to new descriptor and resend
  1048. * @hdr - the pgio header to move request from
  1049. * @desc - the pageio descriptor to add requests to
  1050. *
  1051. * Try to move each request (nfs_page) from @hdr to @desc then attempt
  1052. * to send them.
  1053. *
  1054. * Returns 0 on success and < 0 on error.
  1055. */
  1056. int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
  1057. struct nfs_pgio_header *hdr)
  1058. {
  1059. LIST_HEAD(failed);
  1060. desc->pg_dreq = hdr->dreq;
  1061. while (!list_empty(&hdr->pages)) {
  1062. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  1063. nfs_list_remove_request(req);
  1064. if (!nfs_pageio_add_request(desc, req))
  1065. nfs_list_add_request(req, &failed);
  1066. }
  1067. nfs_pageio_complete(desc);
  1068. if (!list_empty(&failed)) {
  1069. list_move(&failed, &hdr->pages);
  1070. return desc->pg_error < 0 ? desc->pg_error : -EIO;
  1071. }
  1072. return 0;
  1073. }
  1074. EXPORT_SYMBOL_GPL(nfs_pageio_resend);
  1075. /**
  1076. * nfs_pageio_complete - Complete I/O then cleanup an nfs_pageio_descriptor
  1077. * @desc: pointer to io descriptor
  1078. */
  1079. void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
  1080. {
  1081. u32 midx;
  1082. for (midx = 0; midx < desc->pg_mirror_count; midx++)
  1083. nfs_pageio_complete_mirror(desc, midx);
  1084. if (desc->pg_ops->pg_cleanup)
  1085. desc->pg_ops->pg_cleanup(desc);
  1086. nfs_pageio_cleanup_mirroring(desc);
  1087. }
  1088. /**
  1089. * nfs_pageio_cond_complete - Conditional I/O completion
  1090. * @desc: pointer to io descriptor
  1091. * @index: page index
  1092. *
  1093. * It is important to ensure that processes don't try to take locks
  1094. * on non-contiguous ranges of pages as that might deadlock. This
  1095. * function should be called before attempting to wait on a locked
  1096. * nfs_page. It will complete the I/O if the page index 'index'
  1097. * is not contiguous with the existing list of pages in 'desc'.
  1098. */
  1099. void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
  1100. {
  1101. struct nfs_pgio_mirror *mirror;
  1102. struct nfs_page *prev;
  1103. u32 midx;
  1104. for (midx = 0; midx < desc->pg_mirror_count; midx++) {
  1105. mirror = &desc->pg_mirrors[midx];
  1106. if (!list_empty(&mirror->pg_list)) {
  1107. prev = nfs_list_entry(mirror->pg_list.prev);
  1108. if (index != prev->wb_index + 1)
  1109. nfs_pageio_complete_mirror(desc, midx);
  1110. }
  1111. }
  1112. }
  1113. int __init nfs_init_nfspagecache(void)
  1114. {
  1115. nfs_page_cachep = kmem_cache_create("nfs_page",
  1116. sizeof(struct nfs_page),
  1117. 0, SLAB_HWCACHE_ALIGN,
  1118. NULL);
  1119. if (nfs_page_cachep == NULL)
  1120. return -ENOMEM;
  1121. return 0;
  1122. }
  1123. void nfs_destroy_nfspagecache(void)
  1124. {
  1125. kmem_cache_destroy(nfs_page_cachep);
  1126. }
  1127. static const struct rpc_call_ops nfs_pgio_common_ops = {
  1128. .rpc_call_prepare = nfs_pgio_prepare,
  1129. .rpc_call_done = nfs_pgio_result,
  1130. .rpc_release = nfs_pgio_release,
  1131. };
  1132. const struct nfs_pageio_ops nfs_pgio_rw_ops = {
  1133. .pg_test = nfs_generic_pg_test,
  1134. .pg_doio = nfs_generic_pg_pgios,
  1135. };