pagelist.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  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 = kzalloc_objs(struct page *, pagecount,
  796. gfp_flags);
  797. if (!pg_array->pagevec) {
  798. pg_array->npages = 0;
  799. nfs_pgio_error(hdr);
  800. desc->pg_error = -ENOMEM;
  801. return desc->pg_error;
  802. }
  803. }
  804. nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
  805. pages = hdr->page_array.pagevec;
  806. last_page = NULL;
  807. pageused = 0;
  808. while (!list_empty(head)) {
  809. struct nfs_page_iter_page i;
  810. struct page *page;
  811. req = nfs_list_entry(head->next);
  812. nfs_list_move_request(req, &hdr->pages);
  813. if (req->wb_pgbase == 0)
  814. last_page = NULL;
  815. nfs_page_iter_page_init(&i, req);
  816. while ((page = nfs_page_iter_page_get(&i)) != NULL) {
  817. if (last_page != page) {
  818. pageused++;
  819. if (pageused > pagecount)
  820. goto full;
  821. *pages++ = last_page = page;
  822. }
  823. }
  824. }
  825. full:
  826. if (WARN_ON_ONCE(pageused != pagecount)) {
  827. nfs_pgio_error(hdr);
  828. desc->pg_error = -EINVAL;
  829. return desc->pg_error;
  830. }
  831. if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
  832. (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
  833. desc->pg_ioflags &= ~FLUSH_COND_STABLE;
  834. /* Set up the argument struct */
  835. nfs_pgio_rpcsetup(hdr, pg_base, mirror->pg_count, desc->pg_ioflags,
  836. &cinfo);
  837. desc->pg_rpc_callops = &nfs_pgio_common_ops;
  838. return 0;
  839. }
  840. EXPORT_SYMBOL_GPL(nfs_generic_pgio);
  841. static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
  842. {
  843. struct nfs_pgio_header *hdr;
  844. int ret;
  845. unsigned short task_flags = 0;
  846. hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
  847. if (!hdr) {
  848. desc->pg_error = -ENOMEM;
  849. return desc->pg_error;
  850. }
  851. nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
  852. ret = nfs_generic_pgio(desc, hdr);
  853. if (ret == 0) {
  854. struct nfs_client *clp = NFS_SERVER(hdr->inode)->nfs_client;
  855. struct nfsd_file *localio =
  856. nfs_local_open_fh(clp, hdr->cred, hdr->args.fh,
  857. &hdr->args.context->nfl,
  858. hdr->args.context->mode);
  859. if (NFS_SERVER(hdr->inode)->nfs_client->cl_minorversion)
  860. task_flags = RPC_TASK_MOVEABLE;
  861. ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
  862. hdr,
  863. hdr->cred,
  864. NFS_PROTO(hdr->inode),
  865. desc->pg_rpc_callops,
  866. desc->pg_ioflags,
  867. RPC_TASK_CRED_NOREF | task_flags,
  868. localio);
  869. }
  870. return ret;
  871. }
  872. static struct nfs_pgio_mirror *
  873. nfs_pageio_alloc_mirrors(struct nfs_pageio_descriptor *desc,
  874. unsigned int mirror_count)
  875. {
  876. struct nfs_pgio_mirror *ret;
  877. unsigned int i;
  878. kfree(desc->pg_mirrors_dynamic);
  879. desc->pg_mirrors_dynamic = NULL;
  880. if (mirror_count == 1)
  881. return desc->pg_mirrors_static;
  882. ret = kmalloc_objs(*ret, mirror_count, nfs_io_gfp_mask());
  883. if (ret != NULL) {
  884. for (i = 0; i < mirror_count; i++)
  885. nfs_pageio_mirror_init(&ret[i], desc->pg_bsize);
  886. desc->pg_mirrors_dynamic = ret;
  887. }
  888. return ret;
  889. }
  890. /*
  891. * nfs_pageio_setup_mirroring - determine if mirroring is to be used
  892. * by calling the pg_get_mirror_count op
  893. */
  894. static void nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor *pgio,
  895. struct nfs_page *req)
  896. {
  897. unsigned int mirror_count = 1;
  898. if (pgio->pg_ops->pg_get_mirror_count)
  899. mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
  900. if (mirror_count == pgio->pg_mirror_count || pgio->pg_error < 0)
  901. return;
  902. if (!mirror_count || mirror_count > NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX) {
  903. pgio->pg_error = -EINVAL;
  904. return;
  905. }
  906. pgio->pg_mirrors = nfs_pageio_alloc_mirrors(pgio, mirror_count);
  907. if (pgio->pg_mirrors == NULL) {
  908. pgio->pg_error = -ENOMEM;
  909. pgio->pg_mirrors = pgio->pg_mirrors_static;
  910. mirror_count = 1;
  911. }
  912. pgio->pg_mirror_count = mirror_count;
  913. }
  914. static void nfs_pageio_cleanup_mirroring(struct nfs_pageio_descriptor *pgio)
  915. {
  916. pgio->pg_mirror_count = 1;
  917. pgio->pg_mirror_idx = 0;
  918. pgio->pg_mirrors = pgio->pg_mirrors_static;
  919. kfree(pgio->pg_mirrors_dynamic);
  920. pgio->pg_mirrors_dynamic = NULL;
  921. }
  922. static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
  923. const struct nfs_lock_context *l2)
  924. {
  925. return l1->lockowner == l2->lockowner;
  926. }
  927. static bool nfs_page_is_contiguous(const struct nfs_page *prev,
  928. const struct nfs_page *req)
  929. {
  930. size_t prev_end = prev->wb_pgbase + prev->wb_bytes;
  931. if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
  932. return false;
  933. if (req->wb_pgbase == 0)
  934. return prev_end == nfs_page_max_length(prev);
  935. if (req->wb_pgbase == prev_end) {
  936. struct folio *folio = nfs_page_to_folio(req);
  937. if (folio)
  938. return folio == nfs_page_to_folio(prev);
  939. return req->wb_page == prev->wb_page;
  940. }
  941. return false;
  942. }
  943. /**
  944. * nfs_coalesce_size - test two requests for compatibility
  945. * @prev: pointer to nfs_page
  946. * @req: pointer to nfs_page
  947. * @pgio: pointer to nfs_pagio_descriptor
  948. *
  949. * The nfs_page structures 'prev' and 'req' are compared to ensure that the
  950. * page data area they describe is contiguous, and that their RPC
  951. * credentials, NFSv4 open state, and lockowners are the same.
  952. *
  953. * Returns size of the request that can be coalesced
  954. */
  955. static unsigned int nfs_coalesce_size(struct nfs_page *prev,
  956. struct nfs_page *req,
  957. struct nfs_pageio_descriptor *pgio)
  958. {
  959. struct file_lock_context *flctx;
  960. if (prev) {
  961. if (!nfs_match_open_context(nfs_req_openctx(req), nfs_req_openctx(prev)))
  962. return 0;
  963. flctx = locks_inode_context(d_inode(nfs_req_openctx(req)->dentry));
  964. if (flctx != NULL &&
  965. !(list_empty_careful(&flctx->flc_posix) &&
  966. list_empty_careful(&flctx->flc_flock)) &&
  967. !nfs_match_lock_context(req->wb_lock_context,
  968. prev->wb_lock_context))
  969. return 0;
  970. if (!nfs_page_is_contiguous(prev, req))
  971. return 0;
  972. }
  973. return pgio->pg_ops->pg_test(pgio, prev, req);
  974. }
  975. /**
  976. * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
  977. * @desc: destination io descriptor
  978. * @req: request
  979. *
  980. * If the request 'req' was successfully coalesced into the existing list
  981. * of pages 'desc', it returns the size of req.
  982. */
  983. static unsigned int
  984. nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
  985. struct nfs_page *req)
  986. {
  987. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  988. struct nfs_page *prev = NULL;
  989. unsigned int size;
  990. if (list_empty(&mirror->pg_list)) {
  991. if (desc->pg_ops->pg_init)
  992. desc->pg_ops->pg_init(desc, req);
  993. if (desc->pg_error < 0)
  994. return 0;
  995. mirror->pg_base = req->wb_pgbase;
  996. mirror->pg_count = 0;
  997. mirror->pg_recoalesce = 0;
  998. } else
  999. prev = nfs_list_entry(mirror->pg_list.prev);
  1000. if (desc->pg_maxretrans && req->wb_nio > desc->pg_maxretrans) {
  1001. if (NFS_SERVER(desc->pg_inode)->flags & NFS_MOUNT_SOFTERR)
  1002. desc->pg_error = -ETIMEDOUT;
  1003. else
  1004. desc->pg_error = -EIO;
  1005. return 0;
  1006. }
  1007. size = nfs_coalesce_size(prev, req, desc);
  1008. if (size < req->wb_bytes)
  1009. return size;
  1010. nfs_list_move_request(req, &mirror->pg_list);
  1011. mirror->pg_count += req->wb_bytes;
  1012. return req->wb_bytes;
  1013. }
  1014. /*
  1015. * Helper for nfs_pageio_add_request and nfs_pageio_complete
  1016. */
  1017. static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
  1018. {
  1019. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  1020. if (!list_empty(&mirror->pg_list)) {
  1021. int error = desc->pg_ops->pg_doio(desc);
  1022. if (error < 0)
  1023. desc->pg_error = error;
  1024. if (list_empty(&mirror->pg_list))
  1025. mirror->pg_bytes_written += mirror->pg_count;
  1026. }
  1027. }
  1028. static void
  1029. nfs_pageio_cleanup_request(struct nfs_pageio_descriptor *desc,
  1030. struct nfs_page *req)
  1031. {
  1032. LIST_HEAD(head);
  1033. nfs_list_move_request(req, &head);
  1034. desc->pg_completion_ops->error_cleanup(&head, desc->pg_error);
  1035. }
  1036. /**
  1037. * __nfs_pageio_add_request - Attempt to coalesce a request into a page list.
  1038. * @desc: destination io descriptor
  1039. * @req: request
  1040. *
  1041. * This may split a request into subrequests which are all part of the
  1042. * same page group. If so, it will submit @req as the last one, to ensure
  1043. * the pointer to @req is still valid in case of failure.
  1044. *
  1045. * Returns true if the request 'req' was successfully coalesced into the
  1046. * existing list of pages 'desc'.
  1047. */
  1048. static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  1049. struct nfs_page *req)
  1050. {
  1051. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  1052. struct nfs_page *subreq;
  1053. unsigned int size, subreq_size;
  1054. nfs_page_group_lock(req);
  1055. subreq = req;
  1056. subreq_size = subreq->wb_bytes;
  1057. for(;;) {
  1058. size = nfs_pageio_do_add_request(desc, subreq);
  1059. if (size == subreq_size) {
  1060. /* We successfully submitted a request */
  1061. if (subreq == req)
  1062. break;
  1063. req->wb_pgbase += size;
  1064. req->wb_bytes -= size;
  1065. req->wb_offset += size;
  1066. subreq_size = req->wb_bytes;
  1067. subreq = req;
  1068. continue;
  1069. }
  1070. if (WARN_ON_ONCE(subreq != req)) {
  1071. nfs_page_group_unlock(req);
  1072. nfs_pageio_cleanup_request(desc, subreq);
  1073. subreq = req;
  1074. subreq_size = req->wb_bytes;
  1075. nfs_page_group_lock(req);
  1076. }
  1077. if (!size) {
  1078. /* Can't coalesce any more, so do I/O */
  1079. nfs_page_group_unlock(req);
  1080. desc->pg_moreio = 1;
  1081. nfs_pageio_doio(desc);
  1082. if (desc->pg_error < 0 || mirror->pg_recoalesce)
  1083. return 0;
  1084. /* retry add_request for this subreq */
  1085. nfs_page_group_lock(req);
  1086. continue;
  1087. }
  1088. subreq = nfs_create_subreq(req, req->wb_pgbase,
  1089. req->wb_offset, size);
  1090. if (IS_ERR(subreq))
  1091. goto err_ptr;
  1092. subreq_size = size;
  1093. }
  1094. nfs_page_group_unlock(req);
  1095. return 1;
  1096. err_ptr:
  1097. desc->pg_error = PTR_ERR(subreq);
  1098. nfs_page_group_unlock(req);
  1099. return 0;
  1100. }
  1101. static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
  1102. {
  1103. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  1104. LIST_HEAD(head);
  1105. do {
  1106. list_splice_init(&mirror->pg_list, &head);
  1107. mirror->pg_recoalesce = 0;
  1108. while (!list_empty(&head)) {
  1109. struct nfs_page *req;
  1110. req = list_first_entry(&head, struct nfs_page, wb_list);
  1111. if (__nfs_pageio_add_request(desc, req))
  1112. continue;
  1113. if (desc->pg_error < 0) {
  1114. list_splice_tail(&head, &mirror->pg_list);
  1115. mirror->pg_recoalesce = 1;
  1116. return 0;
  1117. }
  1118. break;
  1119. }
  1120. } while (mirror->pg_recoalesce);
  1121. return 1;
  1122. }
  1123. static int nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor *desc,
  1124. struct nfs_page *req)
  1125. {
  1126. int ret;
  1127. do {
  1128. ret = __nfs_pageio_add_request(desc, req);
  1129. if (ret)
  1130. break;
  1131. if (desc->pg_error < 0)
  1132. break;
  1133. ret = nfs_do_recoalesce(desc);
  1134. } while (ret);
  1135. return ret;
  1136. }
  1137. static void nfs_pageio_error_cleanup(struct nfs_pageio_descriptor *desc)
  1138. {
  1139. u32 midx;
  1140. struct nfs_pgio_mirror *mirror;
  1141. if (!desc->pg_error)
  1142. return;
  1143. for (midx = 0; midx < desc->pg_mirror_count; midx++) {
  1144. mirror = nfs_pgio_get_mirror(desc, midx);
  1145. desc->pg_completion_ops->error_cleanup(&mirror->pg_list,
  1146. desc->pg_error);
  1147. }
  1148. }
  1149. int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  1150. struct nfs_page *req)
  1151. {
  1152. u32 midx;
  1153. unsigned int pgbase, offset, bytes;
  1154. struct nfs_page *dupreq;
  1155. pgbase = req->wb_pgbase;
  1156. offset = req->wb_offset;
  1157. bytes = req->wb_bytes;
  1158. nfs_pageio_setup_mirroring(desc, req);
  1159. if (desc->pg_error < 0)
  1160. goto out_failed;
  1161. /* Create the mirror instances first, and fire them off */
  1162. for (midx = 1; midx < desc->pg_mirror_count; midx++) {
  1163. nfs_page_group_lock(req);
  1164. dupreq = nfs_create_subreq(req,
  1165. pgbase, offset, bytes);
  1166. nfs_page_group_unlock(req);
  1167. if (IS_ERR(dupreq)) {
  1168. desc->pg_error = PTR_ERR(dupreq);
  1169. goto out_failed;
  1170. }
  1171. nfs_pgio_set_current_mirror(desc, midx);
  1172. if (!nfs_pageio_add_request_mirror(desc, dupreq))
  1173. goto out_cleanup_subreq;
  1174. }
  1175. nfs_pgio_set_current_mirror(desc, 0);
  1176. if (!nfs_pageio_add_request_mirror(desc, req))
  1177. goto out_failed;
  1178. return 1;
  1179. out_cleanup_subreq:
  1180. nfs_pageio_cleanup_request(desc, dupreq);
  1181. out_failed:
  1182. nfs_pageio_error_cleanup(desc);
  1183. return 0;
  1184. }
  1185. /*
  1186. * nfs_pageio_complete_mirror - Complete I/O on the current mirror of an
  1187. * nfs_pageio_descriptor
  1188. * @desc: pointer to io descriptor
  1189. * @mirror_idx: pointer to mirror index
  1190. */
  1191. static void nfs_pageio_complete_mirror(struct nfs_pageio_descriptor *desc,
  1192. u32 mirror_idx)
  1193. {
  1194. struct nfs_pgio_mirror *mirror;
  1195. u32 restore_idx;
  1196. restore_idx = nfs_pgio_set_current_mirror(desc, mirror_idx);
  1197. mirror = nfs_pgio_current_mirror(desc);
  1198. for (;;) {
  1199. nfs_pageio_doio(desc);
  1200. if (desc->pg_error < 0 || !mirror->pg_recoalesce)
  1201. break;
  1202. if (!nfs_do_recoalesce(desc))
  1203. break;
  1204. }
  1205. nfs_pgio_set_current_mirror(desc, restore_idx);
  1206. }
  1207. /*
  1208. * nfs_pageio_resend - Transfer requests to new descriptor and resend
  1209. * @hdr - the pgio header to move request from
  1210. * @desc - the pageio descriptor to add requests to
  1211. *
  1212. * Try to move each request (nfs_page) from @hdr to @desc then attempt
  1213. * to send them.
  1214. *
  1215. * Returns 0 on success and < 0 on error.
  1216. */
  1217. int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
  1218. struct nfs_pgio_header *hdr)
  1219. {
  1220. LIST_HEAD(pages);
  1221. desc->pg_io_completion = hdr->io_completion;
  1222. desc->pg_dreq = hdr->dreq;
  1223. nfs_netfs_set_pageio_descriptor(desc, hdr);
  1224. list_splice_init(&hdr->pages, &pages);
  1225. while (!list_empty(&pages)) {
  1226. struct nfs_page *req = nfs_list_entry(pages.next);
  1227. if (!nfs_pageio_add_request(desc, req))
  1228. break;
  1229. }
  1230. nfs_pageio_complete(desc);
  1231. if (!list_empty(&pages)) {
  1232. int err = desc->pg_error < 0 ? desc->pg_error : -EIO;
  1233. hdr->completion_ops->error_cleanup(&pages, err);
  1234. nfs_set_pgio_error(hdr, err, hdr->io_start);
  1235. return err;
  1236. }
  1237. return 0;
  1238. }
  1239. EXPORT_SYMBOL_GPL(nfs_pageio_resend);
  1240. /**
  1241. * nfs_pageio_complete - Complete I/O then cleanup an nfs_pageio_descriptor
  1242. * @desc: pointer to io descriptor
  1243. */
  1244. void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
  1245. {
  1246. u32 midx;
  1247. for (midx = 0; midx < desc->pg_mirror_count; midx++)
  1248. nfs_pageio_complete_mirror(desc, midx);
  1249. if (desc->pg_error < 0)
  1250. nfs_pageio_error_cleanup(desc);
  1251. if (desc->pg_ops->pg_cleanup)
  1252. desc->pg_ops->pg_cleanup(desc);
  1253. nfs_pageio_cleanup_mirroring(desc);
  1254. }
  1255. /**
  1256. * nfs_pageio_cond_complete - Conditional I/O completion
  1257. * @desc: pointer to io descriptor
  1258. * @index: page index
  1259. *
  1260. * It is important to ensure that processes don't try to take locks
  1261. * on non-contiguous ranges of pages as that might deadlock. This
  1262. * function should be called before attempting to wait on a locked
  1263. * nfs_page. It will complete the I/O if the page index 'index'
  1264. * is not contiguous with the existing list of pages in 'desc'.
  1265. */
  1266. void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
  1267. {
  1268. struct nfs_pgio_mirror *mirror;
  1269. struct nfs_page *prev;
  1270. struct folio *folio;
  1271. u32 midx;
  1272. for (midx = 0; midx < desc->pg_mirror_count; midx++) {
  1273. mirror = nfs_pgio_get_mirror(desc, midx);
  1274. if (!list_empty(&mirror->pg_list)) {
  1275. prev = nfs_list_entry(mirror->pg_list.prev);
  1276. folio = nfs_page_to_folio(prev);
  1277. if (folio) {
  1278. if (index == folio_next_index(folio))
  1279. continue;
  1280. } else if (index == prev->wb_index + 1)
  1281. continue;
  1282. /*
  1283. * We will submit more requests after these. Indicate
  1284. * this to the underlying layers.
  1285. */
  1286. desc->pg_moreio = 1;
  1287. nfs_pageio_complete(desc);
  1288. break;
  1289. }
  1290. }
  1291. }
  1292. /*
  1293. * nfs_pageio_stop_mirroring - stop using mirroring (set mirror count to 1)
  1294. */
  1295. void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio)
  1296. {
  1297. nfs_pageio_complete(pgio);
  1298. }
  1299. int __init nfs_init_nfspagecache(void)
  1300. {
  1301. nfs_page_cachep = kmem_cache_create("nfs_page",
  1302. sizeof(struct nfs_page),
  1303. 0, SLAB_HWCACHE_ALIGN,
  1304. NULL);
  1305. if (nfs_page_cachep == NULL)
  1306. return -ENOMEM;
  1307. return 0;
  1308. }
  1309. void nfs_destroy_nfspagecache(void)
  1310. {
  1311. kmem_cache_destroy(nfs_page_cachep);
  1312. }
  1313. static const struct rpc_call_ops nfs_pgio_common_ops = {
  1314. .rpc_call_prepare = nfs_pgio_prepare,
  1315. .rpc_call_done = nfs_pgio_result,
  1316. .rpc_release = nfs_pgio_release,
  1317. };
  1318. const struct nfs_pageio_ops nfs_pgio_rw_ops = {
  1319. .pg_test = nfs_generic_pg_test,
  1320. .pg_doio = nfs_generic_pg_pgios,
  1321. };