pagelist.c 37 KB

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