write.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/nfs/write.c
  4. *
  5. * Write file data over NFS.
  6. *
  7. * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
  8. */
  9. #include <linux/types.h>
  10. #include <linux/slab.h>
  11. #include <linux/mm.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/file.h>
  14. #include <linux/writeback.h>
  15. #include <linux/swap.h>
  16. #include <linux/migrate.h>
  17. #include <linux/sunrpc/clnt.h>
  18. #include <linux/nfs_fs.h>
  19. #include <linux/nfs_mount.h>
  20. #include <linux/nfs_page.h>
  21. #include <linux/backing-dev.h>
  22. #include <linux/export.h>
  23. #include <linux/freezer.h>
  24. #include <linux/wait.h>
  25. #include <linux/iversion.h>
  26. #include <linux/filelock.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/sched/mm.h>
  29. #include "delegation.h"
  30. #include "internal.h"
  31. #include "iostat.h"
  32. #include "nfs4_fs.h"
  33. #include "fscache.h"
  34. #include "pnfs.h"
  35. #include "nfstrace.h"
  36. #define NFSDBG_FACILITY NFSDBG_PAGECACHE
  37. #define MIN_POOL_WRITE (32)
  38. #define MIN_POOL_COMMIT (4)
  39. struct nfs_io_completion {
  40. void (*complete)(void *data);
  41. void *data;
  42. struct kref refcount;
  43. };
  44. /*
  45. * Local function declarations
  46. */
  47. static void nfs_redirty_request(struct nfs_page *req);
  48. static const struct rpc_call_ops nfs_commit_ops;
  49. static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
  50. static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
  51. static const struct nfs_rw_ops nfs_rw_write_ops;
  52. static void nfs_inode_remove_request(struct nfs_page *req);
  53. static void nfs_clear_request_commit(struct nfs_commit_info *cinfo,
  54. struct nfs_page *req);
  55. static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
  56. struct inode *inode);
  57. static struct kmem_cache *nfs_wdata_cachep;
  58. static mempool_t *nfs_wdata_mempool;
  59. static struct kmem_cache *nfs_cdata_cachep;
  60. static mempool_t *nfs_commit_mempool;
  61. struct nfs_commit_data *nfs_commitdata_alloc(void)
  62. {
  63. struct nfs_commit_data *p;
  64. p = kmem_cache_zalloc(nfs_cdata_cachep, nfs_io_gfp_mask());
  65. if (!p) {
  66. p = mempool_alloc(nfs_commit_mempool, GFP_NOWAIT);
  67. if (!p)
  68. return NULL;
  69. memset(p, 0, sizeof(*p));
  70. }
  71. INIT_LIST_HEAD(&p->pages);
  72. return p;
  73. }
  74. EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
  75. void nfs_commit_free(struct nfs_commit_data *p)
  76. {
  77. mempool_free(p, nfs_commit_mempool);
  78. }
  79. EXPORT_SYMBOL_GPL(nfs_commit_free);
  80. static struct nfs_pgio_header *nfs_writehdr_alloc(void)
  81. {
  82. struct nfs_pgio_header *p;
  83. p = kmem_cache_zalloc(nfs_wdata_cachep, nfs_io_gfp_mask());
  84. if (!p) {
  85. p = mempool_alloc(nfs_wdata_mempool, GFP_NOWAIT);
  86. if (!p)
  87. return NULL;
  88. memset(p, 0, sizeof(*p));
  89. }
  90. p->rw_mode = FMODE_WRITE;
  91. return p;
  92. }
  93. static void nfs_writehdr_free(struct nfs_pgio_header *hdr)
  94. {
  95. mempool_free(hdr, nfs_wdata_mempool);
  96. }
  97. static struct nfs_io_completion *nfs_io_completion_alloc(gfp_t gfp_flags)
  98. {
  99. return kmalloc_obj(struct nfs_io_completion, gfp_flags);
  100. }
  101. static void nfs_io_completion_init(struct nfs_io_completion *ioc,
  102. void (*complete)(void *), void *data)
  103. {
  104. ioc->complete = complete;
  105. ioc->data = data;
  106. kref_init(&ioc->refcount);
  107. }
  108. static void nfs_io_completion_release(struct kref *kref)
  109. {
  110. struct nfs_io_completion *ioc = container_of(kref,
  111. struct nfs_io_completion, refcount);
  112. ioc->complete(ioc->data);
  113. kfree(ioc);
  114. }
  115. static void nfs_io_completion_get(struct nfs_io_completion *ioc)
  116. {
  117. if (ioc != NULL)
  118. kref_get(&ioc->refcount);
  119. }
  120. static void nfs_io_completion_put(struct nfs_io_completion *ioc)
  121. {
  122. if (ioc != NULL)
  123. kref_put(&ioc->refcount, nfs_io_completion_release);
  124. }
  125. static void
  126. nfs_page_set_inode_ref(struct nfs_page *req, struct inode *inode)
  127. {
  128. if (!test_and_set_bit(PG_INODE_REF, &req->wb_flags)) {
  129. kref_get(&req->wb_kref);
  130. atomic_long_inc(&NFS_I(inode)->nrequests);
  131. }
  132. }
  133. static void nfs_cancel_remove_inode(struct nfs_page *req, struct inode *inode)
  134. {
  135. if (test_and_clear_bit(PG_REMOVE, &req->wb_flags))
  136. nfs_page_set_inode_ref(req, inode);
  137. }
  138. /**
  139. * nfs_folio_find_head_request - find head request associated with a folio
  140. * @folio: pointer to folio
  141. *
  142. * must be called while holding the inode lock.
  143. *
  144. * returns matching head request with reference held, or NULL if not found.
  145. */
  146. static struct nfs_page *nfs_folio_find_head_request(struct folio *folio)
  147. {
  148. struct address_space *mapping = folio->mapping;
  149. struct nfs_page *req;
  150. if (!folio_test_private(folio))
  151. return NULL;
  152. spin_lock(&mapping->i_private_lock);
  153. req = folio->private;
  154. if (req) {
  155. WARN_ON_ONCE(req->wb_head != req);
  156. kref_get(&req->wb_kref);
  157. }
  158. spin_unlock(&mapping->i_private_lock);
  159. return req;
  160. }
  161. /* Adjust the file length if we're writing beyond the end */
  162. static void nfs_grow_file(struct folio *folio, unsigned int offset,
  163. unsigned int count)
  164. {
  165. struct inode *inode = folio->mapping->host;
  166. loff_t end, i_size;
  167. pgoff_t end_index;
  168. spin_lock(&inode->i_lock);
  169. i_size = i_size_read(inode);
  170. end_index = ((i_size - 1) >> folio_shift(folio)) << folio_order(folio);
  171. if (i_size > 0 && folio->index < end_index)
  172. goto out;
  173. end = folio_pos(folio) + (loff_t)offset + (loff_t)count;
  174. if (i_size >= end)
  175. goto out;
  176. trace_nfs_size_grow(inode, end);
  177. i_size_write(inode, end);
  178. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
  179. nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
  180. out:
  181. /* Atomically update timestamps if they are delegated to us. */
  182. nfs_update_delegated_mtime_locked(inode);
  183. spin_unlock(&inode->i_lock);
  184. nfs_fscache_invalidate(inode, 0);
  185. }
  186. /* A writeback failed: mark the page as bad, and invalidate the page cache */
  187. static void nfs_set_pageerror(struct address_space *mapping)
  188. {
  189. struct inode *inode = mapping->host;
  190. nfs_zap_mapping(mapping->host, mapping);
  191. /* Force file size revalidation */
  192. spin_lock(&inode->i_lock);
  193. nfs_set_cache_invalid(inode, NFS_INO_REVAL_FORCED |
  194. NFS_INO_INVALID_CHANGE |
  195. NFS_INO_INVALID_SIZE);
  196. spin_unlock(&inode->i_lock);
  197. }
  198. static void nfs_mapping_set_error(struct folio *folio, int error)
  199. {
  200. struct address_space *mapping = folio->mapping;
  201. filemap_set_wb_err(mapping, error);
  202. if (mapping->host)
  203. errseq_set(&mapping->host->i_sb->s_wb_err,
  204. error == -ENOSPC ? -ENOSPC : -EIO);
  205. nfs_set_pageerror(mapping);
  206. }
  207. /*
  208. * nfs_page_covers_folio
  209. * @req: struct nfs_page
  210. *
  211. * Return true if the request covers the whole folio.
  212. * Note that the caller should ensure all subrequests have been joined
  213. */
  214. static bool nfs_page_group_covers_page(struct nfs_page *req)
  215. {
  216. unsigned int len = nfs_folio_length(nfs_page_to_folio(req));
  217. return req->wb_pgbase == 0 && req->wb_bytes == len;
  218. }
  219. /* We can set the PG_uptodate flag if we see that a write request
  220. * covers the full page.
  221. */
  222. static void nfs_mark_uptodate(struct nfs_page *req)
  223. {
  224. struct folio *folio = nfs_page_to_folio(req);
  225. if (folio_test_uptodate(folio))
  226. return;
  227. if (!nfs_page_group_covers_page(req))
  228. return;
  229. folio_mark_uptodate(folio);
  230. }
  231. static int wb_priority(struct writeback_control *wbc)
  232. {
  233. int ret = 0;
  234. if (wbc->sync_mode == WB_SYNC_ALL)
  235. ret = FLUSH_COND_STABLE;
  236. return ret;
  237. }
  238. /*
  239. * NFS congestion control
  240. */
  241. int nfs_congestion_kb;
  242. #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
  243. #define NFS_CONGESTION_OFF_THRESH \
  244. (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
  245. static void nfs_folio_set_writeback(struct folio *folio)
  246. {
  247. struct nfs_server *nfss = NFS_SERVER(folio->mapping->host);
  248. folio_start_writeback(folio);
  249. if (atomic_long_inc_return(&nfss->writeback) > NFS_CONGESTION_ON_THRESH)
  250. nfss->write_congested = 1;
  251. }
  252. static void nfs_folio_end_writeback(struct folio *folio)
  253. {
  254. struct nfs_server *nfss = NFS_SERVER(folio->mapping->host);
  255. folio_end_writeback_no_dropbehind(folio);
  256. if (atomic_long_dec_return(&nfss->writeback) <
  257. NFS_CONGESTION_OFF_THRESH) {
  258. nfss->write_congested = 0;
  259. wake_up_all(&nfss->write_congestion_wait);
  260. }
  261. }
  262. static void nfs_page_end_writeback(struct nfs_page *req)
  263. {
  264. if (nfs_page_group_sync_on_bit(req, PG_WB_END)) {
  265. nfs_unlock_request(req);
  266. nfs_folio_end_writeback(nfs_page_to_folio(req));
  267. } else
  268. nfs_unlock_request(req);
  269. }
  270. /*
  271. * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
  272. *
  273. * @destroy_list - request list (using wb_this_page) terminated by @old_head
  274. * @old_head - the old head of the list
  275. *
  276. * All subrequests must be locked and removed from all lists, so at this point
  277. * they are only "active" in this function, and possibly in nfs_wait_on_request
  278. * with a reference held by some other context.
  279. */
  280. static void
  281. nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
  282. struct nfs_page *old_head,
  283. struct inode *inode)
  284. {
  285. while (destroy_list) {
  286. struct nfs_page *subreq = destroy_list;
  287. destroy_list = (subreq->wb_this_page == old_head) ?
  288. NULL : subreq->wb_this_page;
  289. /* Note: lock subreq in order to change subreq->wb_head */
  290. nfs_page_set_headlock(subreq);
  291. WARN_ON_ONCE(old_head != subreq->wb_head);
  292. /* make sure old group is not used */
  293. subreq->wb_this_page = subreq;
  294. subreq->wb_head = subreq;
  295. clear_bit(PG_REMOVE, &subreq->wb_flags);
  296. /* Note: races with nfs_page_group_destroy() */
  297. if (!kref_read(&subreq->wb_kref)) {
  298. /* Check if we raced with nfs_page_group_destroy() */
  299. if (test_and_clear_bit(PG_TEARDOWN, &subreq->wb_flags)) {
  300. nfs_page_clear_headlock(subreq);
  301. nfs_free_request(subreq);
  302. } else
  303. nfs_page_clear_headlock(subreq);
  304. continue;
  305. }
  306. nfs_page_clear_headlock(subreq);
  307. nfs_release_request(old_head);
  308. if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags)) {
  309. nfs_release_request(subreq);
  310. atomic_long_dec(&NFS_I(inode)->nrequests);
  311. }
  312. /* subreq is now totally disconnected from page group or any
  313. * write / commit lists. last chance to wake any waiters */
  314. nfs_unlock_and_release_request(subreq);
  315. }
  316. }
  317. /*
  318. * nfs_join_page_group - destroy subrequests of the head req
  319. * @head: the page used to lookup the "page group" of nfs_page structures
  320. * @inode: Inode to which the request belongs.
  321. *
  322. * This function joins all sub requests to the head request by first
  323. * locking all requests in the group, cancelling any pending operations
  324. * and finally updating the head request to cover the whole range covered by
  325. * the (former) group. All subrequests are removed from any write or commit
  326. * lists, unlinked from the group and destroyed.
  327. */
  328. void nfs_join_page_group(struct nfs_page *head, struct nfs_commit_info *cinfo,
  329. struct inode *inode)
  330. {
  331. struct nfs_page *subreq;
  332. struct nfs_page *destroy_list = NULL;
  333. unsigned int pgbase, off, bytes;
  334. pgbase = head->wb_pgbase;
  335. bytes = head->wb_bytes;
  336. off = head->wb_offset;
  337. for (subreq = head->wb_this_page; subreq != head;
  338. subreq = subreq->wb_this_page) {
  339. /* Subrequests should always form a contiguous range */
  340. if (pgbase > subreq->wb_pgbase) {
  341. off -= pgbase - subreq->wb_pgbase;
  342. bytes += pgbase - subreq->wb_pgbase;
  343. pgbase = subreq->wb_pgbase;
  344. }
  345. bytes = max(subreq->wb_pgbase + subreq->wb_bytes
  346. - pgbase, bytes);
  347. }
  348. /* Set the head request's range to cover the former page group */
  349. head->wb_pgbase = pgbase;
  350. head->wb_bytes = bytes;
  351. head->wb_offset = off;
  352. /* Now that all requests are locked, make sure they aren't on any list.
  353. * Commit list removal accounting is done after locks are dropped */
  354. subreq = head;
  355. do {
  356. nfs_clear_request_commit(cinfo, subreq);
  357. subreq = subreq->wb_this_page;
  358. } while (subreq != head);
  359. /* unlink subrequests from head, destroy them later */
  360. if (head->wb_this_page != head) {
  361. /* destroy list will be terminated by head */
  362. destroy_list = head->wb_this_page;
  363. head->wb_this_page = head;
  364. }
  365. nfs_destroy_unlinked_subrequests(destroy_list, head, inode);
  366. }
  367. /**
  368. * nfs_wait_on_request - Wait for a request to complete.
  369. * @req: request to wait upon.
  370. *
  371. * Interruptible by fatal signals only.
  372. * The user is responsible for holding a count on the request.
  373. */
  374. static int nfs_wait_on_request(struct nfs_page *req)
  375. {
  376. if (!test_bit(PG_BUSY, &req->wb_flags))
  377. return 0;
  378. set_bit(PG_CONTENDED2, &req->wb_flags);
  379. smp_mb__after_atomic();
  380. return wait_on_bit_io(&req->wb_flags, PG_BUSY,
  381. TASK_UNINTERRUPTIBLE);
  382. }
  383. /*
  384. * nfs_unroll_locks - unlock all newly locked reqs and wait on @req
  385. * @head: head request of page group, must be holding head lock
  386. * @req: request that couldn't lock and needs to wait on the req bit lock
  387. *
  388. * This is a helper function for nfs_lock_and_join_requests
  389. * returns 0 on success, < 0 on error.
  390. */
  391. static void
  392. nfs_unroll_locks(struct nfs_page *head, struct nfs_page *req)
  393. {
  394. struct nfs_page *tmp;
  395. /* relinquish all the locks successfully grabbed this run */
  396. for (tmp = head->wb_this_page ; tmp != req; tmp = tmp->wb_this_page) {
  397. if (!kref_read(&tmp->wb_kref))
  398. continue;
  399. nfs_unlock_and_release_request(tmp);
  400. }
  401. }
  402. /*
  403. * nfs_page_group_lock_subreq - try to lock a subrequest
  404. * @head: head request of page group
  405. * @subreq: request to lock
  406. *
  407. * This is a helper function for nfs_lock_and_join_requests which
  408. * must be called with the head request and page group both locked.
  409. * On error, it returns with the page group unlocked.
  410. */
  411. static int
  412. nfs_page_group_lock_subreq(struct nfs_page *head, struct nfs_page *subreq)
  413. {
  414. int ret;
  415. if (!kref_get_unless_zero(&subreq->wb_kref))
  416. return 0;
  417. while (!nfs_lock_request(subreq)) {
  418. nfs_page_group_unlock(head);
  419. ret = nfs_wait_on_request(subreq);
  420. if (!ret)
  421. ret = nfs_page_group_lock(head);
  422. if (ret < 0) {
  423. nfs_unroll_locks(head, subreq);
  424. nfs_release_request(subreq);
  425. return ret;
  426. }
  427. }
  428. return 0;
  429. }
  430. /*
  431. * nfs_lock_and_join_requests - join all subreqs to the head req
  432. * @folio: the folio used to lookup the "page group" of nfs_page structures
  433. *
  434. * This function joins all sub requests to the head request by first
  435. * locking all requests in the group, cancelling any pending operations
  436. * and finally updating the head request to cover the whole range covered by
  437. * the (former) group. All subrequests are removed from any write or commit
  438. * lists, unlinked from the group and destroyed.
  439. *
  440. * Returns a locked, referenced pointer to the head request - which after
  441. * this call is guaranteed to be the only request associated with the page.
  442. * Returns NULL if no requests are found for @folio, or a ERR_PTR if an
  443. * error was encountered.
  444. */
  445. static struct nfs_page *nfs_lock_and_join_requests(struct folio *folio)
  446. {
  447. struct inode *inode = folio->mapping->host;
  448. struct nfs_page *head, *subreq;
  449. struct nfs_commit_info cinfo;
  450. int ret;
  451. /*
  452. * A reference is taken only on the head request which acts as a
  453. * reference to the whole page group - the group will not be destroyed
  454. * until the head reference is released.
  455. */
  456. retry:
  457. head = nfs_folio_find_head_request(folio);
  458. if (!head)
  459. return NULL;
  460. while (!nfs_lock_request(head)) {
  461. ret = nfs_wait_on_request(head);
  462. if (ret < 0) {
  463. nfs_release_request(head);
  464. return ERR_PTR(ret);
  465. }
  466. }
  467. ret = nfs_page_group_lock(head);
  468. if (ret < 0)
  469. goto out_unlock;
  470. /* Ensure that nobody removed the request before we locked it */
  471. if (head != folio->private) {
  472. nfs_page_group_unlock(head);
  473. nfs_unlock_and_release_request(head);
  474. goto retry;
  475. }
  476. nfs_cancel_remove_inode(head, inode);
  477. /* lock each request in the page group */
  478. for (subreq = head->wb_this_page;
  479. subreq != head;
  480. subreq = subreq->wb_this_page) {
  481. ret = nfs_page_group_lock_subreq(head, subreq);
  482. if (ret < 0)
  483. goto out_unlock;
  484. }
  485. nfs_page_group_unlock(head);
  486. nfs_init_cinfo_from_inode(&cinfo, inode);
  487. nfs_join_page_group(head, &cinfo, inode);
  488. return head;
  489. out_unlock:
  490. nfs_unlock_and_release_request(head);
  491. return ERR_PTR(ret);
  492. }
  493. static void nfs_write_error(struct nfs_page *req, int error)
  494. {
  495. trace_nfs_write_error(nfs_page_to_inode(req), req, error);
  496. nfs_mapping_set_error(nfs_page_to_folio(req), error);
  497. nfs_inode_remove_request(req);
  498. nfs_page_end_writeback(req);
  499. nfs_release_request(req);
  500. }
  501. /*
  502. * Find an associated nfs write request, and prepare to flush it out
  503. * May return an error if the user signalled nfs_wait_on_request().
  504. */
  505. static int nfs_do_writepage(struct folio *folio, struct writeback_control *wbc,
  506. struct nfs_pageio_descriptor *pgio)
  507. {
  508. struct nfs_page *req;
  509. int ret;
  510. nfs_pageio_cond_complete(pgio, folio->index);
  511. req = nfs_lock_and_join_requests(folio);
  512. if (!req)
  513. return 0;
  514. if (IS_ERR(req))
  515. return PTR_ERR(req);
  516. trace_nfs_do_writepage(req);
  517. nfs_folio_set_writeback(folio);
  518. WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
  519. /* If there is a fatal error that covers this write, just exit */
  520. ret = pgio->pg_error;
  521. if (nfs_error_is_fatal_on_server(ret))
  522. goto out_launder;
  523. if (!nfs_pageio_add_request(pgio, req)) {
  524. ret = pgio->pg_error;
  525. /*
  526. * Remove the problematic req upon fatal errors on the server
  527. */
  528. if (nfs_error_is_fatal_on_server(ret))
  529. goto out_launder;
  530. folio_redirty_for_writepage(wbc, folio);
  531. nfs_redirty_request(req);
  532. pgio->pg_error = 0;
  533. return ret;
  534. }
  535. nfs_add_stats(folio->mapping->host, NFSIOS_WRITEPAGES, 1);
  536. return 0;
  537. out_launder:
  538. nfs_write_error(req, ret);
  539. return 0;
  540. }
  541. /*
  542. * Write an mmapped page to the server.
  543. */
  544. static int nfs_writepage_locked(struct folio *folio,
  545. struct writeback_control *wbc)
  546. {
  547. struct nfs_pageio_descriptor pgio;
  548. struct inode *inode = folio->mapping->host;
  549. int err;
  550. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
  551. nfs_pageio_init_write(&pgio, inode, 0, false,
  552. &nfs_async_write_completion_ops);
  553. err = nfs_do_writepage(folio, wbc, &pgio);
  554. pgio.pg_error = 0;
  555. nfs_pageio_complete(&pgio);
  556. return err;
  557. }
  558. static void nfs_io_completion_commit(void *inode)
  559. {
  560. nfs_commit_inode(inode, 0);
  561. }
  562. int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  563. {
  564. struct inode *inode = mapping->host;
  565. struct nfs_pageio_descriptor pgio;
  566. struct nfs_io_completion *ioc = NULL;
  567. unsigned int mntflags = NFS_SERVER(inode)->flags;
  568. struct nfs_server *nfss = NFS_SERVER(inode);
  569. int priority = 0;
  570. int err;
  571. trace_nfs_writepages(inode, wbc->range_start, wbc->range_end - wbc->range_start);
  572. /* Wait with writeback until write congestion eases */
  573. if (wbc->sync_mode == WB_SYNC_NONE && nfss->write_congested) {
  574. err = wait_event_killable(nfss->write_congestion_wait,
  575. nfss->write_congested == 0);
  576. if (err)
  577. goto out_err;
  578. }
  579. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
  580. if (!(mntflags & NFS_MOUNT_WRITE_EAGER) || wbc->for_kupdate ||
  581. wbc->for_background || wbc->for_sync) {
  582. ioc = nfs_io_completion_alloc(GFP_KERNEL);
  583. if (ioc)
  584. nfs_io_completion_init(ioc, nfs_io_completion_commit,
  585. inode);
  586. priority = wb_priority(wbc);
  587. }
  588. do {
  589. struct folio *folio = NULL;
  590. nfs_pageio_init_write(&pgio, inode, priority, false,
  591. &nfs_async_write_completion_ops);
  592. pgio.pg_io_completion = ioc;
  593. while ((folio = writeback_iter(mapping, wbc, folio, &err))) {
  594. err = nfs_do_writepage(folio, wbc, &pgio);
  595. folio_unlock(folio);
  596. }
  597. pgio.pg_error = 0;
  598. nfs_pageio_complete(&pgio);
  599. if (err == -EAGAIN && mntflags & NFS_MOUNT_SOFTERR)
  600. break;
  601. } while (err < 0 && !nfs_error_is_fatal(err));
  602. nfs_io_completion_put(ioc);
  603. if (err > 0)
  604. err = 0;
  605. out_err:
  606. trace_nfs_writepages_done(inode, wbc->range_start, wbc->range_end - wbc->range_start, err);
  607. return err;
  608. }
  609. /*
  610. * Insert a write request into an inode
  611. */
  612. static void nfs_inode_add_request(struct nfs_page *req)
  613. {
  614. struct folio *folio = nfs_page_to_folio(req);
  615. struct address_space *mapping = folio->mapping;
  616. struct nfs_inode *nfsi = NFS_I(mapping->host);
  617. WARN_ON_ONCE(req->wb_this_page != req);
  618. /* Lock the request! */
  619. nfs_lock_request(req);
  620. spin_lock(&mapping->i_private_lock);
  621. set_bit(PG_MAPPED, &req->wb_flags);
  622. folio_set_private(folio);
  623. folio->private = req;
  624. spin_unlock(&mapping->i_private_lock);
  625. atomic_long_inc(&nfsi->nrequests);
  626. /* this a head request for a page group - mark it as having an
  627. * extra reference so sub groups can follow suit.
  628. * This flag also informs pgio layer when to bump nrequests when
  629. * adding subrequests. */
  630. WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
  631. kref_get(&req->wb_kref);
  632. }
  633. /*
  634. * Remove a write request from an inode
  635. */
  636. static void nfs_inode_remove_request(struct nfs_page *req)
  637. {
  638. struct nfs_inode *nfsi = NFS_I(nfs_page_to_inode(req));
  639. nfs_page_group_lock(req);
  640. if (nfs_page_group_sync_on_bit_locked(req, PG_REMOVE)) {
  641. struct folio *folio = nfs_page_to_folio(req->wb_head);
  642. struct address_space *mapping = folio->mapping;
  643. spin_lock(&mapping->i_private_lock);
  644. if (likely(folio)) {
  645. folio->private = NULL;
  646. folio_clear_private(folio);
  647. clear_bit(PG_MAPPED, &req->wb_head->wb_flags);
  648. }
  649. spin_unlock(&mapping->i_private_lock);
  650. folio_end_dropbehind(folio);
  651. }
  652. nfs_page_group_unlock(req);
  653. if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags)) {
  654. atomic_long_dec(&nfsi->nrequests);
  655. nfs_release_request(req);
  656. }
  657. }
  658. static void nfs_mark_request_dirty(struct nfs_page *req)
  659. {
  660. struct folio *folio = nfs_page_to_folio(req);
  661. if (folio)
  662. filemap_dirty_folio(folio_mapping(folio), folio);
  663. }
  664. /**
  665. * nfs_request_add_commit_list_locked - add request to a commit list
  666. * @req: pointer to a struct nfs_page
  667. * @dst: commit list head
  668. * @cinfo: holds list lock and accounting info
  669. *
  670. * This sets the PG_CLEAN bit, updates the cinfo count of
  671. * number of outstanding requests requiring a commit as well as
  672. * the MM page stats.
  673. *
  674. * The caller must hold NFS_I(cinfo->inode)->commit_mutex, and the
  675. * nfs_page lock.
  676. */
  677. void
  678. nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst,
  679. struct nfs_commit_info *cinfo)
  680. {
  681. set_bit(PG_CLEAN, &req->wb_flags);
  682. nfs_list_add_request(req, dst);
  683. atomic_long_inc(&cinfo->mds->ncommit);
  684. }
  685. EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked);
  686. /**
  687. * nfs_request_add_commit_list - add request to a commit list
  688. * @req: pointer to a struct nfs_page
  689. * @cinfo: holds list lock and accounting info
  690. *
  691. * This sets the PG_CLEAN bit, updates the cinfo count of
  692. * number of outstanding requests requiring a commit as well as
  693. * the MM page stats.
  694. *
  695. * The caller must _not_ hold the cinfo->lock, but must be
  696. * holding the nfs_page lock.
  697. */
  698. void
  699. nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo)
  700. {
  701. mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
  702. nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo);
  703. mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
  704. nfs_folio_mark_unstable(nfs_page_to_folio(req), cinfo);
  705. }
  706. EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
  707. /**
  708. * nfs_request_remove_commit_list - Remove request from a commit list
  709. * @req: pointer to a nfs_page
  710. * @cinfo: holds list lock and accounting info
  711. *
  712. * This clears the PG_CLEAN bit, and updates the cinfo's count of
  713. * number of outstanding requests requiring a commit
  714. * It does not update the MM page stats.
  715. *
  716. * The caller _must_ hold the cinfo->lock and the nfs_page lock.
  717. */
  718. void
  719. nfs_request_remove_commit_list(struct nfs_page *req,
  720. struct nfs_commit_info *cinfo)
  721. {
  722. if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
  723. return;
  724. nfs_list_remove_request(req);
  725. atomic_long_dec(&cinfo->mds->ncommit);
  726. }
  727. EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
  728. static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
  729. struct inode *inode)
  730. {
  731. cinfo->inode = inode;
  732. cinfo->mds = &NFS_I(inode)->commit_info;
  733. cinfo->ds = pnfs_get_ds_info(inode);
  734. cinfo->dreq = NULL;
  735. cinfo->completion_ops = &nfs_commit_completion_ops;
  736. }
  737. void nfs_init_cinfo(struct nfs_commit_info *cinfo,
  738. struct inode *inode,
  739. struct nfs_direct_req *dreq)
  740. {
  741. if (dreq)
  742. nfs_init_cinfo_from_dreq(cinfo, dreq);
  743. else
  744. nfs_init_cinfo_from_inode(cinfo, inode);
  745. }
  746. EXPORT_SYMBOL_GPL(nfs_init_cinfo);
  747. /*
  748. * Add a request to the inode's commit list.
  749. */
  750. void
  751. nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
  752. struct nfs_commit_info *cinfo, u32 ds_commit_idx)
  753. {
  754. if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx))
  755. return;
  756. nfs_request_add_commit_list(req, cinfo);
  757. }
  758. static void nfs_folio_clear_commit(struct folio *folio)
  759. {
  760. if (folio) {
  761. long nr = folio_nr_pages(folio);
  762. node_stat_mod_folio(folio, NR_WRITEBACK, -nr);
  763. wb_stat_mod(&inode_to_bdi(folio->mapping->host)->wb,
  764. WB_WRITEBACK, -nr);
  765. }
  766. }
  767. /* Called holding the request lock on @req */
  768. static void nfs_clear_request_commit(struct nfs_commit_info *cinfo,
  769. struct nfs_page *req)
  770. {
  771. if (test_bit(PG_CLEAN, &req->wb_flags)) {
  772. struct nfs_open_context *ctx = nfs_req_openctx(req);
  773. struct inode *inode = d_inode(ctx->dentry);
  774. mutex_lock(&NFS_I(inode)->commit_mutex);
  775. if (!pnfs_clear_request_commit(req, cinfo)) {
  776. nfs_request_remove_commit_list(req, cinfo);
  777. }
  778. mutex_unlock(&NFS_I(inode)->commit_mutex);
  779. nfs_folio_clear_commit(nfs_page_to_folio(req));
  780. }
  781. }
  782. int nfs_write_need_commit(struct nfs_pgio_header *hdr)
  783. {
  784. if (hdr->verf.committed == NFS_DATA_SYNC)
  785. return hdr->lseg == NULL;
  786. return hdr->verf.committed != NFS_FILE_SYNC;
  787. }
  788. static void nfs_async_write_init(struct nfs_pgio_header *hdr)
  789. {
  790. nfs_io_completion_get(hdr->io_completion);
  791. }
  792. static void nfs_write_completion(struct nfs_pgio_header *hdr)
  793. {
  794. struct nfs_commit_info cinfo;
  795. unsigned long bytes = 0;
  796. if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
  797. goto out;
  798. nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
  799. while (!list_empty(&hdr->pages)) {
  800. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  801. bytes += req->wb_bytes;
  802. nfs_list_remove_request(req);
  803. if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
  804. (hdr->good_bytes < bytes)) {
  805. trace_nfs_comp_error(hdr->inode, req, hdr->error);
  806. nfs_mapping_set_error(nfs_page_to_folio(req),
  807. hdr->error);
  808. goto remove_req;
  809. }
  810. if (nfs_write_need_commit(hdr)) {
  811. /* Reset wb_nio, since the write was successful. */
  812. req->wb_nio = 0;
  813. memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
  814. nfs_mark_request_commit(req, hdr->lseg, &cinfo,
  815. hdr->ds_commit_idx);
  816. goto next;
  817. }
  818. remove_req:
  819. nfs_inode_remove_request(req);
  820. next:
  821. nfs_page_end_writeback(req);
  822. nfs_release_request(req);
  823. }
  824. out:
  825. nfs_io_completion_put(hdr->io_completion);
  826. hdr->release(hdr);
  827. }
  828. unsigned long
  829. nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
  830. {
  831. return atomic_long_read(&cinfo->mds->ncommit);
  832. }
  833. /* NFS_I(cinfo->inode)->commit_mutex held by caller */
  834. int
  835. nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
  836. struct nfs_commit_info *cinfo, int max)
  837. {
  838. struct nfs_page *req, *tmp;
  839. int ret = 0;
  840. list_for_each_entry_safe(req, tmp, src, wb_list) {
  841. kref_get(&req->wb_kref);
  842. if (!nfs_lock_request(req)) {
  843. nfs_release_request(req);
  844. continue;
  845. }
  846. nfs_request_remove_commit_list(req, cinfo);
  847. clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
  848. nfs_list_add_request(req, dst);
  849. ret++;
  850. if ((ret == max) && !cinfo->dreq)
  851. break;
  852. cond_resched();
  853. }
  854. return ret;
  855. }
  856. EXPORT_SYMBOL_GPL(nfs_scan_commit_list);
  857. /*
  858. * nfs_scan_commit - Scan an inode for commit requests
  859. * @inode: NFS inode to scan
  860. * @dst: mds destination list
  861. * @cinfo: mds and ds lists of reqs ready to commit
  862. *
  863. * Moves requests from the inode's 'commit' request list.
  864. * The requests are *not* checked to ensure that they form a contiguous set.
  865. */
  866. int
  867. nfs_scan_commit(struct inode *inode, struct list_head *dst,
  868. struct nfs_commit_info *cinfo)
  869. {
  870. int ret = 0;
  871. if (!atomic_long_read(&cinfo->mds->ncommit))
  872. return 0;
  873. mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
  874. if (atomic_long_read(&cinfo->mds->ncommit) > 0) {
  875. const int max = INT_MAX;
  876. ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
  877. cinfo, max);
  878. ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
  879. }
  880. mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
  881. return ret;
  882. }
  883. /*
  884. * Search for an existing write request, and attempt to update
  885. * it to reflect a new dirty region on a given page.
  886. *
  887. * If the attempt fails, then the existing request is flushed out
  888. * to disk.
  889. */
  890. static struct nfs_page *nfs_try_to_update_request(struct folio *folio,
  891. unsigned int offset,
  892. unsigned int bytes)
  893. {
  894. struct nfs_page *req;
  895. unsigned int rqend;
  896. unsigned int end;
  897. int error;
  898. trace_nfs_try_to_update_request(folio_inode(folio), offset, bytes);
  899. end = offset + bytes;
  900. req = nfs_lock_and_join_requests(folio);
  901. if (IS_ERR_OR_NULL(req))
  902. goto out;
  903. rqend = req->wb_offset + req->wb_bytes;
  904. /*
  905. * Tell the caller to flush out the request if
  906. * the offsets are non-contiguous.
  907. * Note: nfs_flush_incompatible() will already
  908. * have flushed out requests having wrong owners.
  909. */
  910. if (offset > rqend || end < req->wb_offset)
  911. goto out_flushme;
  912. /* Okay, the request matches. Update the region */
  913. if (offset < req->wb_offset) {
  914. req->wb_offset = offset;
  915. req->wb_pgbase = offset;
  916. }
  917. if (end > rqend)
  918. req->wb_bytes = end - req->wb_offset;
  919. else
  920. req->wb_bytes = rqend - req->wb_offset;
  921. req->wb_nio = 0;
  922. out:
  923. trace_nfs_try_to_update_request_done(folio_inode(folio), offset, bytes,
  924. PTR_ERR_OR_ZERO(req));
  925. return req;
  926. out_flushme:
  927. /*
  928. * Note: we mark the request dirty here because
  929. * nfs_lock_and_join_requests() cannot preserve
  930. * commit flags, so we have to replay the write.
  931. */
  932. nfs_mark_request_dirty(req);
  933. nfs_unlock_and_release_request(req);
  934. error = nfs_wb_folio(folio->mapping->host, folio);
  935. trace_nfs_try_to_update_request_done(folio_inode(folio), offset, bytes, error);
  936. return (error < 0) ? ERR_PTR(error) : NULL;
  937. }
  938. /*
  939. * Try to update an existing write request, or create one if there is none.
  940. *
  941. * Note: Should always be called with the Page Lock held to prevent races
  942. * if we have to add a new request. Also assumes that the caller has
  943. * already called nfs_flush_incompatible() if necessary.
  944. */
  945. static struct nfs_page *nfs_setup_write_request(struct nfs_open_context *ctx,
  946. struct folio *folio,
  947. unsigned int offset,
  948. unsigned int bytes)
  949. {
  950. struct nfs_page *req;
  951. req = nfs_try_to_update_request(folio, offset, bytes);
  952. if (req != NULL)
  953. goto out;
  954. req = nfs_page_create_from_folio(ctx, folio, offset, bytes);
  955. if (IS_ERR(req))
  956. goto out;
  957. nfs_inode_add_request(req);
  958. out:
  959. return req;
  960. }
  961. static int nfs_writepage_setup(struct nfs_open_context *ctx,
  962. struct folio *folio, unsigned int offset,
  963. unsigned int count)
  964. {
  965. struct nfs_page *req;
  966. req = nfs_setup_write_request(ctx, folio, offset, count);
  967. if (IS_ERR(req))
  968. return PTR_ERR(req);
  969. trace_nfs_writepage_setup(req);
  970. /* Update file length */
  971. nfs_grow_file(folio, offset, count);
  972. nfs_mark_uptodate(req);
  973. nfs_mark_request_dirty(req);
  974. nfs_unlock_and_release_request(req);
  975. return 0;
  976. }
  977. int nfs_flush_incompatible(struct file *file, struct folio *folio)
  978. {
  979. struct nfs_open_context *ctx = nfs_file_open_context(file);
  980. struct nfs_lock_context *l_ctx;
  981. struct file_lock_context *flctx = locks_inode_context(file_inode(file));
  982. struct nfs_page *req;
  983. int do_flush, status;
  984. /*
  985. * Look for a request corresponding to this page. If there
  986. * is one, and it belongs to another file, we flush it out
  987. * before we try to copy anything into the page. Do this
  988. * due to the lack of an ACCESS-type call in NFSv2.
  989. * Also do the same if we find a request from an existing
  990. * dropped page.
  991. */
  992. do {
  993. req = nfs_folio_find_head_request(folio);
  994. if (req == NULL)
  995. return 0;
  996. l_ctx = req->wb_lock_context;
  997. do_flush = nfs_page_to_folio(req) != folio ||
  998. !nfs_match_open_context(nfs_req_openctx(req), ctx);
  999. if (l_ctx && flctx &&
  1000. !(list_empty_careful(&flctx->flc_posix) &&
  1001. list_empty_careful(&flctx->flc_flock))) {
  1002. do_flush |= l_ctx->lockowner != current->files;
  1003. }
  1004. nfs_release_request(req);
  1005. if (!do_flush)
  1006. return 0;
  1007. status = nfs_wb_folio(folio->mapping->host, folio);
  1008. } while (status == 0);
  1009. return status;
  1010. }
  1011. /*
  1012. * Avoid buffered writes when a open context credential's key would
  1013. * expire soon.
  1014. *
  1015. * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
  1016. *
  1017. * Return 0 and set a credential flag which triggers the inode to flush
  1018. * and performs NFS_FILE_SYNC writes if the key will expired within
  1019. * RPC_KEY_EXPIRE_TIMEO.
  1020. */
  1021. int
  1022. nfs_key_timeout_notify(struct file *filp, struct inode *inode)
  1023. {
  1024. struct nfs_open_context *ctx = nfs_file_open_context(filp);
  1025. if (nfs_ctx_key_to_expire(ctx, inode) &&
  1026. !rcu_access_pointer(ctx->ll_cred))
  1027. /* Already expired! */
  1028. return -EACCES;
  1029. return 0;
  1030. }
  1031. /*
  1032. * Test if the open context credential key is marked to expire soon.
  1033. */
  1034. bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode)
  1035. {
  1036. struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
  1037. struct rpc_cred *cred, *new, *old = NULL;
  1038. struct auth_cred acred = {
  1039. .cred = ctx->cred,
  1040. };
  1041. bool ret = false;
  1042. rcu_read_lock();
  1043. cred = rcu_dereference(ctx->ll_cred);
  1044. if (cred && !(cred->cr_ops->crkey_timeout &&
  1045. cred->cr_ops->crkey_timeout(cred)))
  1046. goto out;
  1047. rcu_read_unlock();
  1048. new = auth->au_ops->lookup_cred(auth, &acred, 0);
  1049. if (new == cred) {
  1050. put_rpccred(new);
  1051. return true;
  1052. }
  1053. if (IS_ERR_OR_NULL(new)) {
  1054. new = NULL;
  1055. ret = true;
  1056. } else if (new->cr_ops->crkey_timeout &&
  1057. new->cr_ops->crkey_timeout(new))
  1058. ret = true;
  1059. rcu_read_lock();
  1060. old = rcu_dereference_protected(xchg(&ctx->ll_cred,
  1061. RCU_INITIALIZER(new)), 1);
  1062. out:
  1063. rcu_read_unlock();
  1064. put_rpccred(old);
  1065. return ret;
  1066. }
  1067. /*
  1068. * If the page cache is marked as unsafe or invalid, then we can't rely on
  1069. * the PageUptodate() flag. In this case, we will need to turn off
  1070. * write optimisations that depend on the page contents being correct.
  1071. */
  1072. static bool nfs_folio_write_uptodate(struct folio *folio, unsigned int pagelen)
  1073. {
  1074. struct inode *inode = folio->mapping->host;
  1075. struct nfs_inode *nfsi = NFS_I(inode);
  1076. if (nfs_have_delegated_attributes(inode))
  1077. goto out;
  1078. if (nfsi->cache_validity &
  1079. (NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE))
  1080. return false;
  1081. smp_rmb();
  1082. if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags) && pagelen != 0)
  1083. return false;
  1084. out:
  1085. if (nfsi->cache_validity & NFS_INO_INVALID_DATA && pagelen != 0)
  1086. return false;
  1087. return folio_test_uptodate(folio) != 0;
  1088. }
  1089. static bool
  1090. is_whole_file_wrlock(struct file_lock *fl)
  1091. {
  1092. return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX &&
  1093. lock_is_write(fl);
  1094. }
  1095. /* If we know the page is up to date, and we're not using byte range locks (or
  1096. * if we have the whole file locked for writing), it may be more efficient to
  1097. * extend the write to cover the entire page in order to avoid fragmentation
  1098. * inefficiencies.
  1099. *
  1100. * If the file is opened for synchronous writes then we can just skip the rest
  1101. * of the checks.
  1102. */
  1103. static int nfs_can_extend_write(struct file *file, struct folio *folio,
  1104. unsigned int pagelen)
  1105. {
  1106. struct inode *inode = file_inode(file);
  1107. struct file_lock_context *flctx = locks_inode_context(inode);
  1108. struct file_lock *fl;
  1109. int ret;
  1110. unsigned int mntflags = NFS_SERVER(inode)->flags;
  1111. if (mntflags & NFS_MOUNT_NO_ALIGNWRITE)
  1112. return 0;
  1113. if (file->f_flags & O_DSYNC)
  1114. return 0;
  1115. if (!nfs_folio_write_uptodate(folio, pagelen))
  1116. return 0;
  1117. if (nfs_have_write_delegation(inode))
  1118. return 1;
  1119. if (!flctx || (list_empty_careful(&flctx->flc_flock) &&
  1120. list_empty_careful(&flctx->flc_posix)))
  1121. return 1;
  1122. /* Check to see if there are whole file write locks */
  1123. ret = 0;
  1124. spin_lock(&flctx->flc_lock);
  1125. if (!list_empty(&flctx->flc_posix)) {
  1126. fl = list_first_entry(&flctx->flc_posix, struct file_lock,
  1127. c.flc_list);
  1128. if (is_whole_file_wrlock(fl))
  1129. ret = 1;
  1130. } else if (!list_empty(&flctx->flc_flock)) {
  1131. fl = list_first_entry(&flctx->flc_flock, struct file_lock,
  1132. c.flc_list);
  1133. if (lock_is_write(fl))
  1134. ret = 1;
  1135. }
  1136. spin_unlock(&flctx->flc_lock);
  1137. return ret;
  1138. }
  1139. /*
  1140. * Update and possibly write a cached page of an NFS file.
  1141. *
  1142. * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
  1143. * things with a page scheduled for an RPC call (e.g. invalidate it).
  1144. */
  1145. int nfs_update_folio(struct file *file, struct folio *folio,
  1146. unsigned int offset, unsigned int count)
  1147. {
  1148. struct nfs_open_context *ctx = nfs_file_open_context(file);
  1149. struct address_space *mapping = folio->mapping;
  1150. struct inode *inode = mapping->host;
  1151. unsigned int pagelen = nfs_folio_length(folio);
  1152. int status = 0;
  1153. nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
  1154. trace_nfs_update_folio(inode, offset, count);
  1155. dprintk("NFS: nfs_update_folio(%pD2 %d@%lld)\n", file, count,
  1156. (long long)(folio_pos(folio) + offset));
  1157. if (!count)
  1158. goto out;
  1159. if (nfs_can_extend_write(file, folio, pagelen)) {
  1160. unsigned int end = count + offset;
  1161. offset = round_down(offset, PAGE_SIZE);
  1162. if (end < pagelen)
  1163. end = min(round_up(end, PAGE_SIZE), pagelen);
  1164. count = end - offset;
  1165. }
  1166. status = nfs_writepage_setup(ctx, folio, offset, count);
  1167. if (status < 0)
  1168. nfs_set_pageerror(mapping);
  1169. out:
  1170. trace_nfs_update_folio_done(inode, offset, count, status);
  1171. dprintk("NFS: nfs_update_folio returns %d (isize %lld)\n",
  1172. status, (long long)i_size_read(inode));
  1173. return status;
  1174. }
  1175. static int flush_task_priority(int how)
  1176. {
  1177. switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
  1178. case FLUSH_HIGHPRI:
  1179. return RPC_PRIORITY_HIGH;
  1180. case FLUSH_LOWPRI:
  1181. return RPC_PRIORITY_LOW;
  1182. }
  1183. return RPC_PRIORITY_NORMAL;
  1184. }
  1185. static void nfs_initiate_write(struct nfs_pgio_header *hdr,
  1186. struct rpc_message *msg,
  1187. const struct nfs_rpc_ops *rpc_ops,
  1188. struct rpc_task_setup *task_setup_data, int how)
  1189. {
  1190. int priority = flush_task_priority(how);
  1191. if (IS_SWAPFILE(hdr->inode))
  1192. task_setup_data->flags |= RPC_TASK_SWAPPER;
  1193. task_setup_data->priority = priority;
  1194. rpc_ops->write_setup(hdr, msg, &task_setup_data->rpc_client);
  1195. trace_nfs_initiate_write(hdr);
  1196. }
  1197. /* If a nfs_flush_* function fails, it should remove reqs from @head and
  1198. * call this on each, which will prepare them to be retried on next
  1199. * writeback using standard nfs.
  1200. */
  1201. static void nfs_redirty_request(struct nfs_page *req)
  1202. {
  1203. struct nfs_inode *nfsi = NFS_I(nfs_page_to_inode(req));
  1204. /* Bump the transmission count */
  1205. req->wb_nio++;
  1206. nfs_mark_request_dirty(req);
  1207. atomic_long_inc(&nfsi->redirtied_pages);
  1208. nfs_page_end_writeback(req);
  1209. nfs_release_request(req);
  1210. }
  1211. static void nfs_async_write_error(struct list_head *head, int error)
  1212. {
  1213. struct nfs_page *req;
  1214. while (!list_empty(head)) {
  1215. req = nfs_list_entry(head->next);
  1216. nfs_list_remove_request(req);
  1217. if (nfs_error_is_fatal_on_server(error))
  1218. nfs_write_error(req, error);
  1219. else
  1220. nfs_redirty_request(req);
  1221. }
  1222. }
  1223. static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
  1224. {
  1225. nfs_async_write_error(&hdr->pages, 0);
  1226. }
  1227. static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
  1228. .init_hdr = nfs_async_write_init,
  1229. .error_cleanup = nfs_async_write_error,
  1230. .completion = nfs_write_completion,
  1231. .reschedule_io = nfs_async_write_reschedule_io,
  1232. };
  1233. void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
  1234. struct inode *inode, int ioflags, bool force_mds,
  1235. const struct nfs_pgio_completion_ops *compl_ops)
  1236. {
  1237. struct nfs_server *server = NFS_SERVER(inode);
  1238. const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
  1239. #if IS_ENABLED(CONFIG_NFS_V4)
  1240. if (server->pnfs_curr_ld && !force_mds)
  1241. pg_ops = server->pnfs_curr_ld->pg_write_ops;
  1242. #endif
  1243. nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
  1244. server->wsize, ioflags);
  1245. }
  1246. EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
  1247. void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
  1248. {
  1249. struct nfs_pgio_mirror *mirror;
  1250. if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
  1251. pgio->pg_ops->pg_cleanup(pgio);
  1252. pgio->pg_ops = &nfs_pgio_rw_ops;
  1253. nfs_pageio_stop_mirroring(pgio);
  1254. mirror = &pgio->pg_mirrors[0];
  1255. mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
  1256. }
  1257. EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
  1258. void nfs_commit_prepare(struct rpc_task *task, void *calldata)
  1259. {
  1260. struct nfs_commit_data *data = calldata;
  1261. NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
  1262. }
  1263. static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr,
  1264. struct nfs_fattr *fattr)
  1265. {
  1266. struct nfs_pgio_args *argp = &hdr->args;
  1267. struct nfs_pgio_res *resp = &hdr->res;
  1268. u64 size = argp->offset + resp->count;
  1269. if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
  1270. fattr->size = size;
  1271. if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) {
  1272. fattr->valid &= ~NFS_ATTR_FATTR_SIZE;
  1273. return;
  1274. }
  1275. if (size != fattr->size)
  1276. return;
  1277. /* Set attribute barrier */
  1278. nfs_fattr_set_barrier(fattr);
  1279. /* ...and update size */
  1280. fattr->valid |= NFS_ATTR_FATTR_SIZE;
  1281. }
  1282. void nfs_writeback_update_inode(struct nfs_pgio_header *hdr)
  1283. {
  1284. struct nfs_fattr *fattr = &hdr->fattr;
  1285. struct inode *inode = hdr->inode;
  1286. if (nfs_have_delegated_mtime(inode)) {
  1287. spin_lock(&inode->i_lock);
  1288. nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS);
  1289. spin_unlock(&inode->i_lock);
  1290. return;
  1291. }
  1292. spin_lock(&inode->i_lock);
  1293. nfs_writeback_check_extend(hdr, fattr);
  1294. nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
  1295. spin_unlock(&inode->i_lock);
  1296. }
  1297. EXPORT_SYMBOL_GPL(nfs_writeback_update_inode);
  1298. /*
  1299. * This function is called when the WRITE call is complete.
  1300. */
  1301. static int nfs_writeback_done(struct rpc_task *task,
  1302. struct nfs_pgio_header *hdr,
  1303. struct inode *inode)
  1304. {
  1305. int status;
  1306. /*
  1307. * ->write_done will attempt to use post-op attributes to detect
  1308. * conflicting writes by other clients. A strict interpretation
  1309. * of close-to-open would allow us to continue caching even if
  1310. * another writer had changed the file, but some applications
  1311. * depend on tighter cache coherency when writing.
  1312. */
  1313. status = NFS_PROTO(inode)->write_done(task, hdr);
  1314. if (status != 0)
  1315. return status;
  1316. nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
  1317. trace_nfs_writeback_done(task, hdr);
  1318. if (task->tk_status >= 0) {
  1319. enum nfs3_stable_how committed = hdr->res.verf->committed;
  1320. if (committed == NFS_UNSTABLE) {
  1321. /*
  1322. * We have some uncommitted data on the server at
  1323. * this point, so ensure that we keep track of that
  1324. * fact irrespective of what later writes do.
  1325. */
  1326. set_bit(NFS_IOHDR_UNSTABLE_WRITES, &hdr->flags);
  1327. }
  1328. if (committed < hdr->args.stable) {
  1329. /* We tried a write call, but the server did not
  1330. * commit data to stable storage even though we
  1331. * requested it.
  1332. * Note: There is a known bug in Tru64 < 5.0 in which
  1333. * the server reports NFS_DATA_SYNC, but performs
  1334. * NFS_FILE_SYNC. We therefore implement this checking
  1335. * as a dprintk() in order to avoid filling syslog.
  1336. */
  1337. static unsigned long complain;
  1338. /* Note this will print the MDS for a DS write */
  1339. if (time_before(complain, jiffies)) {
  1340. dprintk("NFS: faulty NFS server %s:"
  1341. " (committed = %d) != (stable = %d)\n",
  1342. NFS_SERVER(inode)->nfs_client->cl_hostname,
  1343. committed, hdr->args.stable);
  1344. complain = jiffies + 300 * HZ;
  1345. }
  1346. }
  1347. }
  1348. /* Deal with the suid/sgid bit corner case */
  1349. if (nfs_should_remove_suid(inode)) {
  1350. spin_lock(&inode->i_lock);
  1351. nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE
  1352. | NFS_INO_REVAL_FORCED);
  1353. spin_unlock(&inode->i_lock);
  1354. }
  1355. return 0;
  1356. }
  1357. /*
  1358. * This function is called when the WRITE call is complete.
  1359. */
  1360. static void nfs_writeback_result(struct rpc_task *task,
  1361. struct nfs_pgio_header *hdr)
  1362. {
  1363. struct nfs_pgio_args *argp = &hdr->args;
  1364. struct nfs_pgio_res *resp = &hdr->res;
  1365. if (resp->count < argp->count) {
  1366. static unsigned long complain;
  1367. /* This a short write! */
  1368. nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
  1369. /* Has the server at least made some progress? */
  1370. if (resp->count == 0) {
  1371. if (time_before(complain, jiffies)) {
  1372. printk(KERN_WARNING
  1373. "NFS: Server wrote zero bytes, expected %u.\n",
  1374. argp->count);
  1375. complain = jiffies + 300 * HZ;
  1376. }
  1377. nfs_set_pgio_error(hdr, -EIO, argp->offset);
  1378. task->tk_status = -EIO;
  1379. return;
  1380. }
  1381. /* For non rpc-based layout drivers, retry-through-MDS */
  1382. if (!task->tk_ops) {
  1383. hdr->pnfs_error = -EAGAIN;
  1384. return;
  1385. }
  1386. /* Was this an NFSv2 write or an NFSv3 stable write? */
  1387. if (resp->verf->committed != NFS_UNSTABLE) {
  1388. /* Resend from where the server left off */
  1389. hdr->mds_offset += resp->count;
  1390. argp->offset += resp->count;
  1391. argp->pgbase += resp->count;
  1392. argp->count -= resp->count;
  1393. } else {
  1394. /* Resend as a stable write in order to avoid
  1395. * headaches in the case of a server crash.
  1396. */
  1397. argp->stable = NFS_FILE_SYNC;
  1398. }
  1399. resp->count = 0;
  1400. resp->verf->committed = 0;
  1401. rpc_restart_call_prepare(task);
  1402. }
  1403. }
  1404. static int wait_on_commit(struct nfs_mds_commit_info *cinfo)
  1405. {
  1406. return wait_var_event_killable(&cinfo->rpcs_out,
  1407. !atomic_read(&cinfo->rpcs_out));
  1408. }
  1409. void nfs_commit_begin(struct nfs_mds_commit_info *cinfo)
  1410. {
  1411. atomic_inc(&cinfo->rpcs_out);
  1412. }
  1413. bool nfs_commit_end(struct nfs_mds_commit_info *cinfo)
  1414. {
  1415. if (atomic_dec_and_test(&cinfo->rpcs_out)) {
  1416. wake_up_var(&cinfo->rpcs_out);
  1417. return true;
  1418. }
  1419. return false;
  1420. }
  1421. void nfs_commitdata_release(struct nfs_commit_data *data)
  1422. {
  1423. put_nfs_open_context(data->context);
  1424. nfs_commit_free(data);
  1425. }
  1426. EXPORT_SYMBOL_GPL(nfs_commitdata_release);
  1427. int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
  1428. const struct nfs_rpc_ops *nfs_ops,
  1429. const struct rpc_call_ops *call_ops,
  1430. int how, int flags,
  1431. struct nfsd_file *localio)
  1432. {
  1433. struct rpc_task *task;
  1434. int priority = flush_task_priority(how);
  1435. struct rpc_message msg = {
  1436. .rpc_argp = &data->args,
  1437. .rpc_resp = &data->res,
  1438. .rpc_cred = data->cred,
  1439. };
  1440. struct rpc_task_setup task_setup_data = {
  1441. .task = &data->task,
  1442. .rpc_client = clnt,
  1443. .rpc_message = &msg,
  1444. .callback_ops = call_ops,
  1445. .callback_data = data,
  1446. .workqueue = nfsiod_workqueue,
  1447. .flags = RPC_TASK_ASYNC | flags,
  1448. .priority = priority,
  1449. };
  1450. if (nfs_server_capable(data->inode, NFS_CAP_MOVEABLE))
  1451. task_setup_data.flags |= RPC_TASK_MOVEABLE;
  1452. /* Set up the initial task struct. */
  1453. nfs_ops->commit_setup(data, &msg, &task_setup_data.rpc_client);
  1454. trace_nfs_initiate_commit(data);
  1455. dprintk("NFS: initiated commit call\n");
  1456. if (localio)
  1457. return nfs_local_commit(localio, data, call_ops, how);
  1458. task = rpc_run_task(&task_setup_data);
  1459. if (IS_ERR(task))
  1460. return PTR_ERR(task);
  1461. if (how & FLUSH_SYNC)
  1462. rpc_wait_for_completion_task(task);
  1463. rpc_put_task(task);
  1464. return 0;
  1465. }
  1466. EXPORT_SYMBOL_GPL(nfs_initiate_commit);
  1467. static loff_t nfs_get_lwb(struct list_head *head)
  1468. {
  1469. loff_t lwb = 0;
  1470. struct nfs_page *req;
  1471. list_for_each_entry(req, head, wb_list)
  1472. if (lwb < (req_offset(req) + req->wb_bytes))
  1473. lwb = req_offset(req) + req->wb_bytes;
  1474. return lwb;
  1475. }
  1476. /*
  1477. * Set up the argument/result storage required for the RPC call.
  1478. */
  1479. void nfs_init_commit(struct nfs_commit_data *data,
  1480. struct list_head *head,
  1481. struct pnfs_layout_segment *lseg,
  1482. struct nfs_commit_info *cinfo)
  1483. {
  1484. struct nfs_page *first;
  1485. struct nfs_open_context *ctx;
  1486. struct inode *inode;
  1487. /* Set up the RPC argument and reply structs
  1488. * NB: take care not to mess about with data->commit et al. */
  1489. if (head)
  1490. list_splice_init(head, &data->pages);
  1491. first = nfs_list_entry(data->pages.next);
  1492. ctx = nfs_req_openctx(first);
  1493. inode = d_inode(ctx->dentry);
  1494. data->inode = inode;
  1495. data->cred = ctx->cred;
  1496. data->lseg = lseg; /* reference transferred */
  1497. /* only set lwb for pnfs commit */
  1498. if (lseg)
  1499. data->lwb = nfs_get_lwb(&data->pages);
  1500. data->mds_ops = &nfs_commit_ops;
  1501. data->completion_ops = cinfo->completion_ops;
  1502. data->dreq = cinfo->dreq;
  1503. data->args.fh = NFS_FH(data->inode);
  1504. /* Note: we always request a commit of the entire inode */
  1505. data->args.offset = 0;
  1506. data->args.count = 0;
  1507. data->context = get_nfs_open_context(ctx);
  1508. data->res.fattr = &data->fattr;
  1509. data->res.verf = &data->verf;
  1510. nfs_fattr_init(&data->fattr);
  1511. nfs_commit_begin(cinfo->mds);
  1512. }
  1513. EXPORT_SYMBOL_GPL(nfs_init_commit);
  1514. void nfs_retry_commit(struct list_head *page_list,
  1515. struct pnfs_layout_segment *lseg,
  1516. struct nfs_commit_info *cinfo,
  1517. u32 ds_commit_idx)
  1518. {
  1519. struct nfs_page *req;
  1520. while (!list_empty(page_list)) {
  1521. req = nfs_list_entry(page_list->next);
  1522. nfs_list_remove_request(req);
  1523. nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx);
  1524. nfs_folio_clear_commit(nfs_page_to_folio(req));
  1525. nfs_unlock_and_release_request(req);
  1526. }
  1527. }
  1528. EXPORT_SYMBOL_GPL(nfs_retry_commit);
  1529. static void nfs_commit_resched_write(struct nfs_commit_info *cinfo,
  1530. struct nfs_page *req)
  1531. {
  1532. struct folio *folio = nfs_page_to_folio(req);
  1533. filemap_dirty_folio(folio_mapping(folio), folio);
  1534. }
  1535. /*
  1536. * Commit dirty pages
  1537. */
  1538. static int
  1539. nfs_commit_list(struct inode *inode, struct list_head *head, int how,
  1540. struct nfs_commit_info *cinfo)
  1541. {
  1542. struct nfs_commit_data *data;
  1543. struct nfsd_file *localio;
  1544. unsigned short task_flags = 0;
  1545. /* another commit raced with us */
  1546. if (list_empty(head))
  1547. return 0;
  1548. data = nfs_commitdata_alloc();
  1549. if (!data) {
  1550. nfs_retry_commit(head, NULL, cinfo, -1);
  1551. return -ENOMEM;
  1552. }
  1553. /* Set up the argument struct */
  1554. nfs_init_commit(data, head, NULL, cinfo);
  1555. if (NFS_SERVER(inode)->nfs_client->cl_minorversion)
  1556. task_flags = RPC_TASK_MOVEABLE;
  1557. localio = nfs_local_open_fh(NFS_SERVER(inode)->nfs_client, data->cred,
  1558. data->args.fh, &data->context->nfl,
  1559. data->context->mode);
  1560. return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode),
  1561. data->mds_ops, how,
  1562. RPC_TASK_CRED_NOREF | task_flags, localio);
  1563. }
  1564. /*
  1565. * COMMIT call returned
  1566. */
  1567. static void nfs_commit_done(struct rpc_task *task, void *calldata)
  1568. {
  1569. struct nfs_commit_data *data = calldata;
  1570. /* Call the NFS version-specific code */
  1571. NFS_PROTO(data->inode)->commit_done(task, data);
  1572. trace_nfs_commit_done(task, data);
  1573. }
  1574. static void nfs_commit_release_pages(struct nfs_commit_data *data)
  1575. {
  1576. const struct nfs_writeverf *verf = data->res.verf;
  1577. struct nfs_page *req;
  1578. int status = data->task.tk_status;
  1579. struct nfs_commit_info cinfo;
  1580. struct folio *folio;
  1581. while (!list_empty(&data->pages)) {
  1582. req = nfs_list_entry(data->pages.next);
  1583. nfs_list_remove_request(req);
  1584. folio = nfs_page_to_folio(req);
  1585. nfs_folio_clear_commit(folio);
  1586. dprintk("NFS: commit (%s/%llu %d@%lld)",
  1587. nfs_req_openctx(req)->dentry->d_sb->s_id,
  1588. (unsigned long long)NFS_FILEID(d_inode(nfs_req_openctx(req)->dentry)),
  1589. req->wb_bytes,
  1590. (long long)req_offset(req));
  1591. if (status < 0) {
  1592. if (folio) {
  1593. trace_nfs_commit_error(data->inode, req,
  1594. status);
  1595. nfs_mapping_set_error(folio, status);
  1596. nfs_inode_remove_request(req);
  1597. }
  1598. dprintk(", error = %d\n", status);
  1599. goto next;
  1600. }
  1601. /* Okay, COMMIT succeeded, apparently. Check the verifier
  1602. * returned by the server against all stored verfs. */
  1603. if (nfs_write_match_verf(verf, req)) {
  1604. /* We have a match */
  1605. if (folio)
  1606. nfs_inode_remove_request(req);
  1607. dprintk(" OK\n");
  1608. goto next;
  1609. }
  1610. /* We have a mismatch. Write the page again */
  1611. dprintk(" mismatch\n");
  1612. nfs_mark_request_dirty(req);
  1613. atomic_long_inc(&NFS_I(data->inode)->redirtied_pages);
  1614. next:
  1615. nfs_unlock_and_release_request(req);
  1616. /* Latency breaker */
  1617. cond_resched();
  1618. }
  1619. nfs_init_cinfo(&cinfo, data->inode, data->dreq);
  1620. nfs_commit_end(cinfo.mds);
  1621. }
  1622. static void nfs_commit_release(void *calldata)
  1623. {
  1624. struct nfs_commit_data *data = calldata;
  1625. data->completion_ops->completion(data);
  1626. nfs_commitdata_release(calldata);
  1627. }
  1628. static const struct rpc_call_ops nfs_commit_ops = {
  1629. .rpc_call_prepare = nfs_commit_prepare,
  1630. .rpc_call_done = nfs_commit_done,
  1631. .rpc_release = nfs_commit_release,
  1632. };
  1633. static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
  1634. .completion = nfs_commit_release_pages,
  1635. .resched_write = nfs_commit_resched_write,
  1636. };
  1637. int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
  1638. int how, struct nfs_commit_info *cinfo)
  1639. {
  1640. int status;
  1641. status = pnfs_commit_list(inode, head, how, cinfo);
  1642. if (status == PNFS_NOT_ATTEMPTED)
  1643. status = nfs_commit_list(inode, head, how, cinfo);
  1644. return status;
  1645. }
  1646. static int __nfs_commit_inode(struct inode *inode, int how,
  1647. struct writeback_control *wbc)
  1648. {
  1649. LIST_HEAD(head);
  1650. struct nfs_commit_info cinfo;
  1651. int may_wait = how & FLUSH_SYNC;
  1652. int ret, nscan;
  1653. how &= ~FLUSH_SYNC;
  1654. nfs_init_cinfo_from_inode(&cinfo, inode);
  1655. nfs_commit_begin(cinfo.mds);
  1656. for (;;) {
  1657. ret = nscan = nfs_scan_commit(inode, &head, &cinfo);
  1658. if (ret <= 0)
  1659. break;
  1660. ret = nfs_generic_commit_list(inode, &head, how, &cinfo);
  1661. if (ret < 0)
  1662. break;
  1663. ret = 0;
  1664. if (wbc && wbc->sync_mode == WB_SYNC_NONE) {
  1665. if (nscan < wbc->nr_to_write)
  1666. wbc->nr_to_write -= nscan;
  1667. else
  1668. wbc->nr_to_write = 0;
  1669. }
  1670. if (nscan < INT_MAX)
  1671. break;
  1672. cond_resched();
  1673. }
  1674. nfs_commit_end(cinfo.mds);
  1675. if (ret || !may_wait)
  1676. return ret;
  1677. return wait_on_commit(cinfo.mds);
  1678. }
  1679. int nfs_commit_inode(struct inode *inode, int how)
  1680. {
  1681. return __nfs_commit_inode(inode, how, NULL);
  1682. }
  1683. EXPORT_SYMBOL_GPL(nfs_commit_inode);
  1684. int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  1685. {
  1686. struct nfs_inode *nfsi = NFS_I(inode);
  1687. int flags = FLUSH_SYNC;
  1688. int ret = 0;
  1689. if (wbc->sync_mode == WB_SYNC_NONE) {
  1690. /* no commits means nothing needs to be done */
  1691. if (!atomic_long_read(&nfsi->commit_info.ncommit))
  1692. goto check_requests_outstanding;
  1693. /* Don't commit yet if this is a non-blocking flush and there
  1694. * are a lot of outstanding writes for this mapping.
  1695. */
  1696. if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
  1697. goto out_mark_dirty;
  1698. /* don't wait for the COMMIT response */
  1699. flags = 0;
  1700. }
  1701. ret = __nfs_commit_inode(inode, flags, wbc);
  1702. if (!ret) {
  1703. if (flags & FLUSH_SYNC)
  1704. return 0;
  1705. } else if (atomic_long_read(&nfsi->commit_info.ncommit))
  1706. goto out_mark_dirty;
  1707. check_requests_outstanding:
  1708. if (!atomic_read(&nfsi->commit_info.rpcs_out))
  1709. return ret;
  1710. out_mark_dirty:
  1711. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  1712. return ret;
  1713. }
  1714. EXPORT_SYMBOL_GPL(nfs_write_inode);
  1715. /*
  1716. * Wrapper for filemap_write_and_wait_range()
  1717. *
  1718. * Needed for pNFS in order to ensure data becomes visible to the
  1719. * client.
  1720. */
  1721. int nfs_filemap_write_and_wait_range(struct address_space *mapping,
  1722. loff_t lstart, loff_t lend)
  1723. {
  1724. int ret;
  1725. ret = filemap_write_and_wait_range(mapping, lstart, lend);
  1726. if (ret == 0)
  1727. ret = pnfs_sync_inode(mapping->host, true);
  1728. return ret;
  1729. }
  1730. EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range);
  1731. /*
  1732. * flush the inode to disk.
  1733. */
  1734. int nfs_wb_all(struct inode *inode)
  1735. {
  1736. int ret;
  1737. trace_nfs_writeback_inode_enter(inode);
  1738. ret = filemap_write_and_wait(inode->i_mapping);
  1739. if (ret)
  1740. goto out;
  1741. ret = nfs_commit_inode(inode, FLUSH_SYNC);
  1742. if (ret < 0)
  1743. goto out;
  1744. pnfs_sync_inode(inode, true);
  1745. ret = 0;
  1746. out:
  1747. trace_nfs_writeback_inode_exit(inode, ret);
  1748. return ret;
  1749. }
  1750. EXPORT_SYMBOL_GPL(nfs_wb_all);
  1751. int nfs_wb_folio_cancel(struct inode *inode, struct folio *folio)
  1752. {
  1753. struct nfs_page *req;
  1754. int ret = 0;
  1755. folio_wait_writeback(folio);
  1756. /* blocking call to cancel all requests and join to a single (head)
  1757. * request */
  1758. req = nfs_lock_and_join_requests(folio);
  1759. if (IS_ERR(req)) {
  1760. ret = PTR_ERR(req);
  1761. } else if (req) {
  1762. /* all requests from this folio have been cancelled by
  1763. * nfs_lock_and_join_requests, so just remove the head
  1764. * request from the inode / page_private pointer and
  1765. * release it */
  1766. nfs_inode_remove_request(req);
  1767. nfs_unlock_and_release_request(req);
  1768. folio_cancel_dirty(folio);
  1769. }
  1770. return ret;
  1771. }
  1772. /**
  1773. * nfs_wb_folio_reclaim - Write back all requests on one page
  1774. * @inode: pointer to page
  1775. * @folio: pointer to folio
  1776. *
  1777. * Assumes that the folio has been locked by the caller
  1778. */
  1779. int nfs_wb_folio_reclaim(struct inode *inode, struct folio *folio)
  1780. {
  1781. loff_t range_start = folio_pos(folio);
  1782. size_t len = folio_size(folio);
  1783. struct writeback_control wbc = {
  1784. .sync_mode = WB_SYNC_ALL,
  1785. .nr_to_write = 0,
  1786. .range_start = range_start,
  1787. .range_end = range_start + len - 1,
  1788. .for_sync = 1,
  1789. };
  1790. int ret;
  1791. if (folio_test_writeback(folio))
  1792. return -EBUSY;
  1793. if (folio_clear_dirty_for_io(folio)) {
  1794. trace_nfs_writeback_folio_reclaim(inode, range_start, len);
  1795. ret = nfs_writepage_locked(folio, &wbc);
  1796. trace_nfs_writeback_folio_reclaim_done(inode, range_start, len,
  1797. ret);
  1798. return ret;
  1799. }
  1800. nfs_commit_inode(inode, 0);
  1801. return 0;
  1802. }
  1803. /**
  1804. * nfs_wb_folio - Write back all requests on one page
  1805. * @inode: pointer to page
  1806. * @folio: pointer to folio
  1807. *
  1808. * Assumes that the folio has been locked by the caller, and will
  1809. * not unlock it.
  1810. */
  1811. int nfs_wb_folio(struct inode *inode, struct folio *folio)
  1812. {
  1813. loff_t range_start = folio_pos(folio);
  1814. size_t len = folio_size(folio);
  1815. struct writeback_control wbc = {
  1816. .sync_mode = WB_SYNC_ALL,
  1817. .nr_to_write = 0,
  1818. .range_start = range_start,
  1819. .range_end = range_start + len - 1,
  1820. };
  1821. int ret;
  1822. trace_nfs_writeback_folio(inode, range_start, len);
  1823. for (;;) {
  1824. folio_wait_writeback(folio);
  1825. if (folio_clear_dirty_for_io(folio)) {
  1826. ret = nfs_writepage_locked(folio, &wbc);
  1827. if (ret < 0)
  1828. goto out_error;
  1829. continue;
  1830. }
  1831. ret = 0;
  1832. if (!folio_test_private(folio))
  1833. break;
  1834. ret = nfs_commit_inode(inode, FLUSH_SYNC);
  1835. if (ret < 0)
  1836. goto out_error;
  1837. }
  1838. out_error:
  1839. trace_nfs_writeback_folio_done(inode, range_start, len, ret);
  1840. return ret;
  1841. }
  1842. #ifdef CONFIG_MIGRATION
  1843. int nfs_migrate_folio(struct address_space *mapping, struct folio *dst,
  1844. struct folio *src, enum migrate_mode mode)
  1845. {
  1846. /*
  1847. * If the private flag is set, the folio is currently associated with
  1848. * an in-progress read or write request. Don't try to migrate it.
  1849. *
  1850. * FIXME: we could do this in principle, but we'll need a way to ensure
  1851. * that we can safely release the inode reference while holding
  1852. * the folio lock.
  1853. */
  1854. if (folio_test_private(src)) {
  1855. if (mode == MIGRATE_SYNC)
  1856. nfs_wb_folio(src->mapping->host, src);
  1857. if (folio_test_private(src))
  1858. return -EBUSY;
  1859. }
  1860. if (folio_test_private_2(src)) { /* [DEPRECATED] */
  1861. if (mode == MIGRATE_ASYNC)
  1862. return -EBUSY;
  1863. folio_wait_private_2(src);
  1864. }
  1865. return migrate_folio(mapping, dst, src, mode);
  1866. }
  1867. #endif
  1868. int __init nfs_init_writepagecache(void)
  1869. {
  1870. nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
  1871. sizeof(struct nfs_pgio_header),
  1872. 0, SLAB_HWCACHE_ALIGN,
  1873. NULL);
  1874. if (nfs_wdata_cachep == NULL)
  1875. return -ENOMEM;
  1876. nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
  1877. nfs_wdata_cachep);
  1878. if (nfs_wdata_mempool == NULL)
  1879. goto out_destroy_write_cache;
  1880. nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
  1881. sizeof(struct nfs_commit_data),
  1882. 0, SLAB_HWCACHE_ALIGN,
  1883. NULL);
  1884. if (nfs_cdata_cachep == NULL)
  1885. goto out_destroy_write_mempool;
  1886. nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
  1887. nfs_cdata_cachep);
  1888. if (nfs_commit_mempool == NULL)
  1889. goto out_destroy_commit_cache;
  1890. /*
  1891. * NFS congestion size, scale with available memory.
  1892. *
  1893. * 64MB: 8192k
  1894. * 128MB: 11585k
  1895. * 256MB: 16384k
  1896. * 512MB: 23170k
  1897. * 1GB: 32768k
  1898. * 2GB: 46340k
  1899. * 4GB: 65536k
  1900. * 8GB: 92681k
  1901. * 16GB: 131072k
  1902. *
  1903. * This allows larger machines to have larger/more transfers.
  1904. * Limit the default to 256M
  1905. */
  1906. nfs_congestion_kb = (16*int_sqrt(totalram_pages())) << (PAGE_SHIFT-10);
  1907. if (nfs_congestion_kb > 256*1024)
  1908. nfs_congestion_kb = 256*1024;
  1909. return 0;
  1910. out_destroy_commit_cache:
  1911. kmem_cache_destroy(nfs_cdata_cachep);
  1912. out_destroy_write_mempool:
  1913. mempool_destroy(nfs_wdata_mempool);
  1914. out_destroy_write_cache:
  1915. kmem_cache_destroy(nfs_wdata_cachep);
  1916. return -ENOMEM;
  1917. }
  1918. void nfs_destroy_writepagecache(void)
  1919. {
  1920. mempool_destroy(nfs_commit_mempool);
  1921. kmem_cache_destroy(nfs_cdata_cachep);
  1922. mempool_destroy(nfs_wdata_mempool);
  1923. kmem_cache_destroy(nfs_wdata_cachep);
  1924. }
  1925. static const struct nfs_rw_ops nfs_rw_write_ops = {
  1926. .rw_alloc_header = nfs_writehdr_alloc,
  1927. .rw_free_header = nfs_writehdr_free,
  1928. .rw_done = nfs_writeback_done,
  1929. .rw_result = nfs_writeback_result,
  1930. .rw_initiate = nfs_initiate_write,
  1931. };