pagelist.c 33 KB

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