pagelist.c 38 KB

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