pagelist.c 33 KB

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