write.c 58 KB

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