write.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974
  1. /*
  2. * linux/fs/nfs/write.c
  3. *
  4. * Write file data over NFS.
  5. *
  6. * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/slab.h>
  10. #include <linux/mm.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/file.h>
  13. #include <linux/writeback.h>
  14. #include <linux/swap.h>
  15. #include <linux/migrate.h>
  16. #include <linux/sunrpc/clnt.h>
  17. #include <linux/nfs_fs.h>
  18. #include <linux/nfs_mount.h>
  19. #include <linux/nfs_page.h>
  20. #include <linux/backing-dev.h>
  21. #include <linux/export.h>
  22. #include <asm/uaccess.h>
  23. #include "delegation.h"
  24. #include "internal.h"
  25. #include "iostat.h"
  26. #include "nfs4_fs.h"
  27. #include "fscache.h"
  28. #include "pnfs.h"
  29. #include "nfstrace.h"
  30. #define NFSDBG_FACILITY NFSDBG_PAGECACHE
  31. #define MIN_POOL_WRITE (32)
  32. #define MIN_POOL_COMMIT (4)
  33. /*
  34. * Local function declarations
  35. */
  36. static void nfs_redirty_request(struct nfs_page *req);
  37. static const struct rpc_call_ops nfs_write_common_ops;
  38. static const struct rpc_call_ops nfs_commit_ops;
  39. static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
  40. static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
  41. static struct kmem_cache *nfs_wdata_cachep;
  42. static mempool_t *nfs_wdata_mempool;
  43. static struct kmem_cache *nfs_cdata_cachep;
  44. static mempool_t *nfs_commit_mempool;
  45. struct nfs_commit_data *nfs_commitdata_alloc(void)
  46. {
  47. struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
  48. if (p) {
  49. memset(p, 0, sizeof(*p));
  50. INIT_LIST_HEAD(&p->pages);
  51. }
  52. return p;
  53. }
  54. EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
  55. void nfs_commit_free(struct nfs_commit_data *p)
  56. {
  57. mempool_free(p, nfs_commit_mempool);
  58. }
  59. EXPORT_SYMBOL_GPL(nfs_commit_free);
  60. struct nfs_write_header *nfs_writehdr_alloc(void)
  61. {
  62. struct nfs_write_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO);
  63. if (p) {
  64. struct nfs_pgio_header *hdr = &p->header;
  65. memset(p, 0, sizeof(*p));
  66. INIT_LIST_HEAD(&hdr->pages);
  67. INIT_LIST_HEAD(&hdr->rpc_list);
  68. spin_lock_init(&hdr->lock);
  69. atomic_set(&hdr->refcnt, 0);
  70. hdr->verf = &p->verf;
  71. }
  72. return p;
  73. }
  74. EXPORT_SYMBOL_GPL(nfs_writehdr_alloc);
  75. static struct nfs_write_data *nfs_writedata_alloc(struct nfs_pgio_header *hdr,
  76. unsigned int pagecount)
  77. {
  78. struct nfs_write_data *data, *prealloc;
  79. prealloc = &container_of(hdr, struct nfs_write_header, header)->rpc_data;
  80. if (prealloc->header == NULL)
  81. data = prealloc;
  82. else
  83. data = kzalloc(sizeof(*data), GFP_KERNEL);
  84. if (!data)
  85. goto out;
  86. if (nfs_pgarray_set(&data->pages, pagecount)) {
  87. data->header = hdr;
  88. atomic_inc(&hdr->refcnt);
  89. } else {
  90. if (data != prealloc)
  91. kfree(data);
  92. data = NULL;
  93. }
  94. out:
  95. return data;
  96. }
  97. void nfs_writehdr_free(struct nfs_pgio_header *hdr)
  98. {
  99. struct nfs_write_header *whdr = container_of(hdr, struct nfs_write_header, header);
  100. mempool_free(whdr, nfs_wdata_mempool);
  101. }
  102. EXPORT_SYMBOL_GPL(nfs_writehdr_free);
  103. void nfs_writedata_release(struct nfs_write_data *wdata)
  104. {
  105. struct nfs_pgio_header *hdr = wdata->header;
  106. struct nfs_write_header *write_header = container_of(hdr, struct nfs_write_header, header);
  107. put_nfs_open_context(wdata->args.context);
  108. if (wdata->pages.pagevec != wdata->pages.page_array)
  109. kfree(wdata->pages.pagevec);
  110. if (wdata == &write_header->rpc_data) {
  111. wdata->header = NULL;
  112. wdata = NULL;
  113. }
  114. if (atomic_dec_and_test(&hdr->refcnt))
  115. hdr->completion_ops->completion(hdr);
  116. /* Note: we only free the rpc_task after callbacks are done.
  117. * See the comment in rpc_free_task() for why
  118. */
  119. kfree(wdata);
  120. }
  121. EXPORT_SYMBOL_GPL(nfs_writedata_release);
  122. static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
  123. {
  124. ctx->error = error;
  125. smp_wmb();
  126. set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  127. }
  128. static struct nfs_page *
  129. nfs_page_find_request_locked(struct nfs_inode *nfsi, struct page *page)
  130. {
  131. struct nfs_page *req = NULL;
  132. if (PagePrivate(page))
  133. req = (struct nfs_page *)page_private(page);
  134. else if (unlikely(PageSwapCache(page))) {
  135. struct nfs_page *freq, *t;
  136. /* Linearly search the commit list for the correct req */
  137. list_for_each_entry_safe(freq, t, &nfsi->commit_info.list, wb_list) {
  138. if (freq->wb_page == page) {
  139. req = freq;
  140. break;
  141. }
  142. }
  143. }
  144. if (req)
  145. kref_get(&req->wb_kref);
  146. return req;
  147. }
  148. static struct nfs_page *nfs_page_find_request(struct page *page)
  149. {
  150. struct inode *inode = page_file_mapping(page)->host;
  151. struct nfs_page *req = NULL;
  152. spin_lock(&inode->i_lock);
  153. req = nfs_page_find_request_locked(NFS_I(inode), page);
  154. spin_unlock(&inode->i_lock);
  155. return req;
  156. }
  157. /* Adjust the file length if we're writing beyond the end */
  158. static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
  159. {
  160. struct inode *inode = page_file_mapping(page)->host;
  161. loff_t end, i_size;
  162. pgoff_t end_index;
  163. spin_lock(&inode->i_lock);
  164. i_size = i_size_read(inode);
  165. end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
  166. if (i_size > 0 && page_file_index(page) < end_index)
  167. goto out;
  168. end = page_file_offset(page) + ((loff_t)offset+count);
  169. if (i_size >= end)
  170. goto out;
  171. i_size_write(inode, end);
  172. nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
  173. out:
  174. spin_unlock(&inode->i_lock);
  175. }
  176. /* A writeback failed: mark the page as bad, and invalidate the page cache */
  177. static void nfs_set_pageerror(struct page *page)
  178. {
  179. nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
  180. }
  181. /* We can set the PG_uptodate flag if we see that a write request
  182. * covers the full page.
  183. */
  184. static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
  185. {
  186. if (PageUptodate(page))
  187. return;
  188. if (base != 0)
  189. return;
  190. if (count != nfs_page_length(page))
  191. return;
  192. SetPageUptodate(page);
  193. }
  194. static int wb_priority(struct writeback_control *wbc)
  195. {
  196. if (wbc->for_reclaim)
  197. return FLUSH_HIGHPRI | FLUSH_STABLE;
  198. if (wbc->for_kupdate || wbc->for_background)
  199. return FLUSH_LOWPRI | FLUSH_COND_STABLE;
  200. return FLUSH_COND_STABLE;
  201. }
  202. /*
  203. * NFS congestion control
  204. */
  205. int nfs_congestion_kb;
  206. #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
  207. #define NFS_CONGESTION_OFF_THRESH \
  208. (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
  209. static void nfs_set_page_writeback(struct page *page)
  210. {
  211. struct nfs_server *nfss = NFS_SERVER(page_file_mapping(page)->host);
  212. int ret = test_set_page_writeback(page);
  213. WARN_ON_ONCE(ret != 0);
  214. if (atomic_long_inc_return(&nfss->writeback) >
  215. NFS_CONGESTION_ON_THRESH) {
  216. set_bdi_congested(&nfss->backing_dev_info,
  217. BLK_RW_ASYNC);
  218. }
  219. }
  220. static void nfs_end_page_writeback(struct page *page)
  221. {
  222. struct inode *inode = page_file_mapping(page)->host;
  223. struct nfs_server *nfss = NFS_SERVER(inode);
  224. end_page_writeback(page);
  225. if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
  226. clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
  227. }
  228. static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
  229. {
  230. struct inode *inode = page_file_mapping(page)->host;
  231. struct nfs_page *req;
  232. int ret;
  233. spin_lock(&inode->i_lock);
  234. for (;;) {
  235. req = nfs_page_find_request_locked(NFS_I(inode), page);
  236. if (req == NULL)
  237. break;
  238. if (nfs_lock_request(req))
  239. break;
  240. /* Note: If we hold the page lock, as is the case in nfs_writepage,
  241. * then the call to nfs_lock_request() will always
  242. * succeed provided that someone hasn't already marked the
  243. * request as dirty (in which case we don't care).
  244. */
  245. spin_unlock(&inode->i_lock);
  246. if (!nonblock)
  247. ret = nfs_wait_on_request(req);
  248. else
  249. ret = -EAGAIN;
  250. nfs_release_request(req);
  251. if (ret != 0)
  252. return ERR_PTR(ret);
  253. spin_lock(&inode->i_lock);
  254. }
  255. spin_unlock(&inode->i_lock);
  256. return req;
  257. }
  258. /*
  259. * Find an associated nfs write request, and prepare to flush it out
  260. * May return an error if the user signalled nfs_wait_on_request().
  261. */
  262. static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
  263. struct page *page, bool nonblock)
  264. {
  265. struct nfs_page *req;
  266. int ret = 0;
  267. req = nfs_find_and_lock_request(page, nonblock);
  268. if (!req)
  269. goto out;
  270. ret = PTR_ERR(req);
  271. if (IS_ERR(req))
  272. goto out;
  273. nfs_set_page_writeback(page);
  274. WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
  275. ret = 0;
  276. if (!nfs_pageio_add_request(pgio, req)) {
  277. nfs_redirty_request(req);
  278. ret = pgio->pg_error;
  279. }
  280. out:
  281. return ret;
  282. }
  283. static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
  284. {
  285. struct inode *inode = page_file_mapping(page)->host;
  286. int ret;
  287. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
  288. nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
  289. nfs_pageio_cond_complete(pgio, page_file_index(page));
  290. ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
  291. if (ret == -EAGAIN) {
  292. redirty_page_for_writepage(wbc, page);
  293. ret = 0;
  294. }
  295. return ret;
  296. }
  297. /*
  298. * Write an mmapped page to the server.
  299. */
  300. static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
  301. {
  302. struct nfs_pageio_descriptor pgio;
  303. int err;
  304. nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc),
  305. false, &nfs_async_write_completion_ops);
  306. err = nfs_do_writepage(page, wbc, &pgio);
  307. nfs_pageio_complete(&pgio);
  308. if (err < 0)
  309. return err;
  310. if (pgio.pg_error < 0)
  311. return pgio.pg_error;
  312. return 0;
  313. }
  314. int nfs_writepage(struct page *page, struct writeback_control *wbc)
  315. {
  316. int ret;
  317. ret = nfs_writepage_locked(page, wbc);
  318. unlock_page(page);
  319. return ret;
  320. }
  321. static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
  322. {
  323. int ret;
  324. ret = nfs_do_writepage(page, wbc, data);
  325. unlock_page(page);
  326. return ret;
  327. }
  328. int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  329. {
  330. struct inode *inode = mapping->host;
  331. unsigned long *bitlock = &NFS_I(inode)->flags;
  332. struct nfs_pageio_descriptor pgio;
  333. int err;
  334. /* Stop dirtying of new pages while we sync */
  335. err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
  336. nfs_wait_bit_killable, TASK_KILLABLE);
  337. if (err)
  338. goto out_err;
  339. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
  340. nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
  341. &nfs_async_write_completion_ops);
  342. err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
  343. nfs_pageio_complete(&pgio);
  344. clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
  345. smp_mb__after_clear_bit();
  346. wake_up_bit(bitlock, NFS_INO_FLUSHING);
  347. if (err < 0)
  348. goto out_err;
  349. err = pgio.pg_error;
  350. if (err < 0)
  351. goto out_err;
  352. return 0;
  353. out_err:
  354. return err;
  355. }
  356. /*
  357. * Insert a write request into an inode
  358. */
  359. static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
  360. {
  361. struct nfs_inode *nfsi = NFS_I(inode);
  362. /* Lock the request! */
  363. nfs_lock_request(req);
  364. spin_lock(&inode->i_lock);
  365. if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
  366. inode->i_version++;
  367. /*
  368. * Swap-space should not get truncated. Hence no need to plug the race
  369. * with invalidate/truncate.
  370. */
  371. if (likely(!PageSwapCache(req->wb_page))) {
  372. set_bit(PG_MAPPED, &req->wb_flags);
  373. SetPagePrivate(req->wb_page);
  374. set_page_private(req->wb_page, (unsigned long)req);
  375. }
  376. nfsi->npages++;
  377. kref_get(&req->wb_kref);
  378. spin_unlock(&inode->i_lock);
  379. }
  380. /*
  381. * Remove a write request from an inode
  382. */
  383. static void nfs_inode_remove_request(struct nfs_page *req)
  384. {
  385. struct inode *inode = req->wb_context->dentry->d_inode;
  386. struct nfs_inode *nfsi = NFS_I(inode);
  387. spin_lock(&inode->i_lock);
  388. if (likely(!PageSwapCache(req->wb_page))) {
  389. set_page_private(req->wb_page, 0);
  390. ClearPagePrivate(req->wb_page);
  391. clear_bit(PG_MAPPED, &req->wb_flags);
  392. }
  393. nfsi->npages--;
  394. spin_unlock(&inode->i_lock);
  395. nfs_release_request(req);
  396. }
  397. static void
  398. nfs_mark_request_dirty(struct nfs_page *req)
  399. {
  400. __set_page_dirty_nobuffers(req->wb_page);
  401. }
  402. #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
  403. /**
  404. * nfs_request_add_commit_list - add request to a commit list
  405. * @req: pointer to a struct nfs_page
  406. * @dst: commit list head
  407. * @cinfo: holds list lock and accounting info
  408. *
  409. * This sets the PG_CLEAN bit, updates the cinfo count of
  410. * number of outstanding requests requiring a commit as well as
  411. * the MM page stats.
  412. *
  413. * The caller must _not_ hold the cinfo->lock, but must be
  414. * holding the nfs_page lock.
  415. */
  416. void
  417. nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
  418. struct nfs_commit_info *cinfo)
  419. {
  420. set_bit(PG_CLEAN, &(req)->wb_flags);
  421. spin_lock(cinfo->lock);
  422. nfs_list_add_request(req, dst);
  423. cinfo->mds->ncommit++;
  424. spin_unlock(cinfo->lock);
  425. if (!cinfo->dreq) {
  426. inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  427. inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
  428. BDI_RECLAIMABLE);
  429. __mark_inode_dirty(req->wb_context->dentry->d_inode,
  430. I_DIRTY_DATASYNC);
  431. }
  432. }
  433. EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
  434. /**
  435. * nfs_request_remove_commit_list - Remove request from a commit list
  436. * @req: pointer to a nfs_page
  437. * @cinfo: holds list lock and accounting info
  438. *
  439. * This clears the PG_CLEAN bit, and updates the cinfo's count of
  440. * number of outstanding requests requiring a commit
  441. * It does not update the MM page stats.
  442. *
  443. * The caller _must_ hold the cinfo->lock and the nfs_page lock.
  444. */
  445. void
  446. nfs_request_remove_commit_list(struct nfs_page *req,
  447. struct nfs_commit_info *cinfo)
  448. {
  449. if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
  450. return;
  451. nfs_list_remove_request(req);
  452. cinfo->mds->ncommit--;
  453. }
  454. EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
  455. static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
  456. struct inode *inode)
  457. {
  458. cinfo->lock = &inode->i_lock;
  459. cinfo->mds = &NFS_I(inode)->commit_info;
  460. cinfo->ds = pnfs_get_ds_info(inode);
  461. cinfo->dreq = NULL;
  462. cinfo->completion_ops = &nfs_commit_completion_ops;
  463. }
  464. void nfs_init_cinfo(struct nfs_commit_info *cinfo,
  465. struct inode *inode,
  466. struct nfs_direct_req *dreq)
  467. {
  468. if (dreq)
  469. nfs_init_cinfo_from_dreq(cinfo, dreq);
  470. else
  471. nfs_init_cinfo_from_inode(cinfo, inode);
  472. }
  473. EXPORT_SYMBOL_GPL(nfs_init_cinfo);
  474. /*
  475. * Add a request to the inode's commit list.
  476. */
  477. void
  478. nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
  479. struct nfs_commit_info *cinfo)
  480. {
  481. if (pnfs_mark_request_commit(req, lseg, cinfo))
  482. return;
  483. nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
  484. }
  485. static void
  486. nfs_clear_page_commit(struct page *page)
  487. {
  488. dec_zone_page_state(page, NR_UNSTABLE_NFS);
  489. dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE);
  490. }
  491. static void
  492. nfs_clear_request_commit(struct nfs_page *req)
  493. {
  494. if (test_bit(PG_CLEAN, &req->wb_flags)) {
  495. struct inode *inode = req->wb_context->dentry->d_inode;
  496. struct nfs_commit_info cinfo;
  497. nfs_init_cinfo_from_inode(&cinfo, inode);
  498. if (!pnfs_clear_request_commit(req, &cinfo)) {
  499. spin_lock(cinfo.lock);
  500. nfs_request_remove_commit_list(req, &cinfo);
  501. spin_unlock(cinfo.lock);
  502. }
  503. nfs_clear_page_commit(req->wb_page);
  504. }
  505. }
  506. static inline
  507. int nfs_write_need_commit(struct nfs_write_data *data)
  508. {
  509. if (data->verf.committed == NFS_DATA_SYNC)
  510. return data->header->lseg == NULL;
  511. return data->verf.committed != NFS_FILE_SYNC;
  512. }
  513. #else
  514. static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
  515. struct inode *inode)
  516. {
  517. }
  518. void nfs_init_cinfo(struct nfs_commit_info *cinfo,
  519. struct inode *inode,
  520. struct nfs_direct_req *dreq)
  521. {
  522. }
  523. void
  524. nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
  525. struct nfs_commit_info *cinfo)
  526. {
  527. }
  528. static void
  529. nfs_clear_request_commit(struct nfs_page *req)
  530. {
  531. }
  532. static inline
  533. int nfs_write_need_commit(struct nfs_write_data *data)
  534. {
  535. return 0;
  536. }
  537. #endif
  538. static void nfs_write_completion(struct nfs_pgio_header *hdr)
  539. {
  540. struct nfs_commit_info cinfo;
  541. unsigned long bytes = 0;
  542. if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
  543. goto out;
  544. nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
  545. while (!list_empty(&hdr->pages)) {
  546. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  547. bytes += req->wb_bytes;
  548. nfs_list_remove_request(req);
  549. if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
  550. (hdr->good_bytes < bytes)) {
  551. nfs_set_pageerror(req->wb_page);
  552. nfs_context_set_write_error(req->wb_context, hdr->error);
  553. goto remove_req;
  554. }
  555. if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
  556. nfs_mark_request_dirty(req);
  557. goto next;
  558. }
  559. if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
  560. memcpy(&req->wb_verf, &hdr->verf->verifier, sizeof(req->wb_verf));
  561. nfs_mark_request_commit(req, hdr->lseg, &cinfo);
  562. goto next;
  563. }
  564. remove_req:
  565. nfs_inode_remove_request(req);
  566. next:
  567. nfs_unlock_request(req);
  568. nfs_end_page_writeback(req->wb_page);
  569. nfs_release_request(req);
  570. }
  571. out:
  572. hdr->release(hdr);
  573. }
  574. #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
  575. static unsigned long
  576. nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
  577. {
  578. return cinfo->mds->ncommit;
  579. }
  580. /* cinfo->lock held by caller */
  581. int
  582. nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
  583. struct nfs_commit_info *cinfo, int max)
  584. {
  585. struct nfs_page *req, *tmp;
  586. int ret = 0;
  587. list_for_each_entry_safe(req, tmp, src, wb_list) {
  588. if (!nfs_lock_request(req))
  589. continue;
  590. kref_get(&req->wb_kref);
  591. if (cond_resched_lock(cinfo->lock))
  592. list_safe_reset_next(req, tmp, wb_list);
  593. nfs_request_remove_commit_list(req, cinfo);
  594. nfs_list_add_request(req, dst);
  595. ret++;
  596. if ((ret == max) && !cinfo->dreq)
  597. break;
  598. }
  599. return ret;
  600. }
  601. /*
  602. * nfs_scan_commit - Scan an inode for commit requests
  603. * @inode: NFS inode to scan
  604. * @dst: mds destination list
  605. * @cinfo: mds and ds lists of reqs ready to commit
  606. *
  607. * Moves requests from the inode's 'commit' request list.
  608. * The requests are *not* checked to ensure that they form a contiguous set.
  609. */
  610. int
  611. nfs_scan_commit(struct inode *inode, struct list_head *dst,
  612. struct nfs_commit_info *cinfo)
  613. {
  614. int ret = 0;
  615. spin_lock(cinfo->lock);
  616. if (cinfo->mds->ncommit > 0) {
  617. const int max = INT_MAX;
  618. ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
  619. cinfo, max);
  620. ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
  621. }
  622. spin_unlock(cinfo->lock);
  623. return ret;
  624. }
  625. #else
  626. static unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
  627. {
  628. return 0;
  629. }
  630. int nfs_scan_commit(struct inode *inode, struct list_head *dst,
  631. struct nfs_commit_info *cinfo)
  632. {
  633. return 0;
  634. }
  635. #endif
  636. /*
  637. * Search for an existing write request, and attempt to update
  638. * it to reflect a new dirty region on a given page.
  639. *
  640. * If the attempt fails, then the existing request is flushed out
  641. * to disk.
  642. */
  643. static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
  644. struct page *page,
  645. unsigned int offset,
  646. unsigned int bytes)
  647. {
  648. struct nfs_page *req;
  649. unsigned int rqend;
  650. unsigned int end;
  651. int error;
  652. if (!PagePrivate(page))
  653. return NULL;
  654. end = offset + bytes;
  655. spin_lock(&inode->i_lock);
  656. for (;;) {
  657. req = nfs_page_find_request_locked(NFS_I(inode), page);
  658. if (req == NULL)
  659. goto out_unlock;
  660. rqend = req->wb_offset + req->wb_bytes;
  661. /*
  662. * Tell the caller to flush out the request if
  663. * the offsets are non-contiguous.
  664. * Note: nfs_flush_incompatible() will already
  665. * have flushed out requests having wrong owners.
  666. */
  667. if (offset > rqend
  668. || end < req->wb_offset)
  669. goto out_flushme;
  670. if (nfs_lock_request(req))
  671. break;
  672. /* The request is locked, so wait and then retry */
  673. spin_unlock(&inode->i_lock);
  674. error = nfs_wait_on_request(req);
  675. nfs_release_request(req);
  676. if (error != 0)
  677. goto out_err;
  678. spin_lock(&inode->i_lock);
  679. }
  680. /* Okay, the request matches. Update the region */
  681. if (offset < req->wb_offset) {
  682. req->wb_offset = offset;
  683. req->wb_pgbase = offset;
  684. }
  685. if (end > rqend)
  686. req->wb_bytes = end - req->wb_offset;
  687. else
  688. req->wb_bytes = rqend - req->wb_offset;
  689. out_unlock:
  690. spin_unlock(&inode->i_lock);
  691. if (req)
  692. nfs_clear_request_commit(req);
  693. return req;
  694. out_flushme:
  695. spin_unlock(&inode->i_lock);
  696. nfs_release_request(req);
  697. error = nfs_wb_page(inode, page);
  698. out_err:
  699. return ERR_PTR(error);
  700. }
  701. /*
  702. * Try to update an existing write request, or create one if there is none.
  703. *
  704. * Note: Should always be called with the Page Lock held to prevent races
  705. * if we have to add a new request. Also assumes that the caller has
  706. * already called nfs_flush_incompatible() if necessary.
  707. */
  708. static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
  709. struct page *page, unsigned int offset, unsigned int bytes)
  710. {
  711. struct inode *inode = page_file_mapping(page)->host;
  712. struct nfs_page *req;
  713. req = nfs_try_to_update_request(inode, page, offset, bytes);
  714. if (req != NULL)
  715. goto out;
  716. req = nfs_create_request(ctx, inode, page, offset, bytes);
  717. if (IS_ERR(req))
  718. goto out;
  719. nfs_inode_add_request(inode, req);
  720. out:
  721. return req;
  722. }
  723. static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
  724. unsigned int offset, unsigned int count)
  725. {
  726. struct nfs_page *req;
  727. req = nfs_setup_write_request(ctx, page, offset, count);
  728. if (IS_ERR(req))
  729. return PTR_ERR(req);
  730. /* Update file length */
  731. nfs_grow_file(page, offset, count);
  732. nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
  733. nfs_mark_request_dirty(req);
  734. nfs_unlock_and_release_request(req);
  735. return 0;
  736. }
  737. int nfs_flush_incompatible(struct file *file, struct page *page)
  738. {
  739. struct nfs_open_context *ctx = nfs_file_open_context(file);
  740. struct nfs_lock_context *l_ctx;
  741. struct nfs_page *req;
  742. int do_flush, status;
  743. /*
  744. * Look for a request corresponding to this page. If there
  745. * is one, and it belongs to another file, we flush it out
  746. * before we try to copy anything into the page. Do this
  747. * due to the lack of an ACCESS-type call in NFSv2.
  748. * Also do the same if we find a request from an existing
  749. * dropped page.
  750. */
  751. do {
  752. req = nfs_page_find_request(page);
  753. if (req == NULL)
  754. return 0;
  755. l_ctx = req->wb_lock_context;
  756. do_flush = req->wb_page != page || req->wb_context != ctx;
  757. if (l_ctx && ctx->dentry->d_inode->i_flock != NULL) {
  758. do_flush |= l_ctx->lockowner.l_owner != current->files
  759. || l_ctx->lockowner.l_pid != current->tgid;
  760. }
  761. nfs_release_request(req);
  762. if (!do_flush)
  763. return 0;
  764. status = nfs_wb_page(page_file_mapping(page)->host, page);
  765. } while (status == 0);
  766. return status;
  767. }
  768. /*
  769. * Avoid buffered writes when a open context credential's key would
  770. * expire soon.
  771. *
  772. * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
  773. *
  774. * Return 0 and set a credential flag which triggers the inode to flush
  775. * and performs NFS_FILE_SYNC writes if the key will expired within
  776. * RPC_KEY_EXPIRE_TIMEO.
  777. */
  778. int
  779. nfs_key_timeout_notify(struct file *filp, struct inode *inode)
  780. {
  781. struct nfs_open_context *ctx = nfs_file_open_context(filp);
  782. struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
  783. return rpcauth_key_timeout_notify(auth, ctx->cred);
  784. }
  785. /*
  786. * Test if the open context credential key is marked to expire soon.
  787. */
  788. bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx)
  789. {
  790. return rpcauth_cred_key_to_expire(ctx->cred);
  791. }
  792. /*
  793. * If the page cache is marked as unsafe or invalid, then we can't rely on
  794. * the PageUptodate() flag. In this case, we will need to turn off
  795. * write optimisations that depend on the page contents being correct.
  796. */
  797. static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
  798. {
  799. struct nfs_inode *nfsi = NFS_I(inode);
  800. if (nfs_have_delegated_attributes(inode))
  801. goto out;
  802. if (nfsi->cache_validity & (NFS_INO_INVALID_DATA|NFS_INO_REVAL_PAGECACHE))
  803. return false;
  804. smp_rmb();
  805. if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
  806. return false;
  807. out:
  808. return PageUptodate(page) != 0;
  809. }
  810. /* If we know the page is up to date, and we're not using byte range locks (or
  811. * if we have the whole file locked for writing), it may be more efficient to
  812. * extend the write to cover the entire page in order to avoid fragmentation
  813. * inefficiencies.
  814. *
  815. * If the file is opened for synchronous writes then we can just skip the rest
  816. * of the checks.
  817. */
  818. static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
  819. {
  820. if (file->f_flags & O_DSYNC)
  821. return 0;
  822. if (!nfs_write_pageuptodate(page, inode))
  823. return 0;
  824. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
  825. return 1;
  826. if (inode->i_flock == NULL || (inode->i_flock->fl_start == 0 &&
  827. inode->i_flock->fl_end == OFFSET_MAX &&
  828. inode->i_flock->fl_type != F_RDLCK))
  829. return 1;
  830. return 0;
  831. }
  832. /*
  833. * Update and possibly write a cached page of an NFS file.
  834. *
  835. * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
  836. * things with a page scheduled for an RPC call (e.g. invalidate it).
  837. */
  838. int nfs_updatepage(struct file *file, struct page *page,
  839. unsigned int offset, unsigned int count)
  840. {
  841. struct nfs_open_context *ctx = nfs_file_open_context(file);
  842. struct inode *inode = page_file_mapping(page)->host;
  843. int status = 0;
  844. nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
  845. dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n",
  846. file, count, (long long)(page_file_offset(page) + offset));
  847. if (nfs_can_extend_write(file, page, inode)) {
  848. count = max(count + offset, nfs_page_length(page));
  849. offset = 0;
  850. }
  851. status = nfs_writepage_setup(ctx, page, offset, count);
  852. if (status < 0)
  853. nfs_set_pageerror(page);
  854. else
  855. __set_page_dirty_nobuffers(page);
  856. dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
  857. status, (long long)i_size_read(inode));
  858. return status;
  859. }
  860. static int flush_task_priority(int how)
  861. {
  862. switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
  863. case FLUSH_HIGHPRI:
  864. return RPC_PRIORITY_HIGH;
  865. case FLUSH_LOWPRI:
  866. return RPC_PRIORITY_LOW;
  867. }
  868. return RPC_PRIORITY_NORMAL;
  869. }
  870. int nfs_initiate_write(struct rpc_clnt *clnt,
  871. struct nfs_write_data *data,
  872. const struct rpc_call_ops *call_ops,
  873. int how, int flags)
  874. {
  875. struct inode *inode = data->header->inode;
  876. int priority = flush_task_priority(how);
  877. struct rpc_task *task;
  878. struct rpc_message msg = {
  879. .rpc_argp = &data->args,
  880. .rpc_resp = &data->res,
  881. .rpc_cred = data->header->cred,
  882. };
  883. struct rpc_task_setup task_setup_data = {
  884. .rpc_client = clnt,
  885. .task = &data->task,
  886. .rpc_message = &msg,
  887. .callback_ops = call_ops,
  888. .callback_data = data,
  889. .workqueue = nfsiod_workqueue,
  890. .flags = RPC_TASK_ASYNC | flags,
  891. .priority = priority,
  892. };
  893. int ret = 0;
  894. /* Set up the initial task struct. */
  895. NFS_PROTO(inode)->write_setup(data, &msg);
  896. dprintk("NFS: %5u initiated write call "
  897. "(req %s/%llu, %u bytes @ offset %llu)\n",
  898. data->task.tk_pid,
  899. inode->i_sb->s_id,
  900. (unsigned long long)NFS_FILEID(inode),
  901. data->args.count,
  902. (unsigned long long)data->args.offset);
  903. nfs4_state_protect_write(NFS_SERVER(inode)->nfs_client,
  904. &task_setup_data.rpc_client, &msg, data);
  905. task = rpc_run_task(&task_setup_data);
  906. if (IS_ERR(task)) {
  907. ret = PTR_ERR(task);
  908. goto out;
  909. }
  910. if (how & FLUSH_SYNC) {
  911. ret = rpc_wait_for_completion_task(task);
  912. if (ret == 0)
  913. ret = task->tk_status;
  914. }
  915. rpc_put_task(task);
  916. out:
  917. return ret;
  918. }
  919. EXPORT_SYMBOL_GPL(nfs_initiate_write);
  920. /*
  921. * Set up the argument/result storage required for the RPC call.
  922. */
  923. static void nfs_write_rpcsetup(struct nfs_write_data *data,
  924. unsigned int count, unsigned int offset,
  925. int how, struct nfs_commit_info *cinfo)
  926. {
  927. struct nfs_page *req = data->header->req;
  928. /* Set up the RPC argument and reply structs
  929. * NB: take care not to mess about with data->commit et al. */
  930. data->args.fh = NFS_FH(data->header->inode);
  931. data->args.offset = req_offset(req) + offset;
  932. /* pnfs_set_layoutcommit needs this */
  933. data->mds_offset = data->args.offset;
  934. data->args.pgbase = req->wb_pgbase + offset;
  935. data->args.pages = data->pages.pagevec;
  936. data->args.count = count;
  937. data->args.context = get_nfs_open_context(req->wb_context);
  938. data->args.lock_context = req->wb_lock_context;
  939. data->args.stable = NFS_UNSTABLE;
  940. switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
  941. case 0:
  942. break;
  943. case FLUSH_COND_STABLE:
  944. if (nfs_reqs_to_commit(cinfo))
  945. break;
  946. default:
  947. data->args.stable = NFS_FILE_SYNC;
  948. }
  949. data->res.fattr = &data->fattr;
  950. data->res.count = count;
  951. data->res.verf = &data->verf;
  952. nfs_fattr_init(&data->fattr);
  953. }
  954. static int nfs_do_write(struct nfs_write_data *data,
  955. const struct rpc_call_ops *call_ops,
  956. int how)
  957. {
  958. struct inode *inode = data->header->inode;
  959. return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how, 0);
  960. }
  961. static int nfs_do_multiple_writes(struct list_head *head,
  962. const struct rpc_call_ops *call_ops,
  963. int how)
  964. {
  965. struct nfs_write_data *data;
  966. int ret = 0;
  967. while (!list_empty(head)) {
  968. int ret2;
  969. data = list_first_entry(head, struct nfs_write_data, list);
  970. list_del_init(&data->list);
  971. ret2 = nfs_do_write(data, call_ops, how);
  972. if (ret == 0)
  973. ret = ret2;
  974. }
  975. return ret;
  976. }
  977. /* If a nfs_flush_* function fails, it should remove reqs from @head and
  978. * call this on each, which will prepare them to be retried on next
  979. * writeback using standard nfs.
  980. */
  981. static void nfs_redirty_request(struct nfs_page *req)
  982. {
  983. nfs_mark_request_dirty(req);
  984. nfs_unlock_request(req);
  985. nfs_end_page_writeback(req->wb_page);
  986. nfs_release_request(req);
  987. }
  988. static void nfs_async_write_error(struct list_head *head)
  989. {
  990. struct nfs_page *req;
  991. while (!list_empty(head)) {
  992. req = nfs_list_entry(head->next);
  993. nfs_list_remove_request(req);
  994. nfs_redirty_request(req);
  995. }
  996. }
  997. static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
  998. .error_cleanup = nfs_async_write_error,
  999. .completion = nfs_write_completion,
  1000. };
  1001. static void nfs_flush_error(struct nfs_pageio_descriptor *desc,
  1002. struct nfs_pgio_header *hdr)
  1003. {
  1004. set_bit(NFS_IOHDR_REDO, &hdr->flags);
  1005. while (!list_empty(&hdr->rpc_list)) {
  1006. struct nfs_write_data *data = list_first_entry(&hdr->rpc_list,
  1007. struct nfs_write_data, list);
  1008. list_del(&data->list);
  1009. nfs_writedata_release(data);
  1010. }
  1011. desc->pg_completion_ops->error_cleanup(&desc->pg_list);
  1012. }
  1013. /*
  1014. * Generate multiple small requests to write out a single
  1015. * contiguous dirty area on one page.
  1016. */
  1017. static int nfs_flush_multi(struct nfs_pageio_descriptor *desc,
  1018. struct nfs_pgio_header *hdr)
  1019. {
  1020. struct nfs_page *req = hdr->req;
  1021. struct page *page = req->wb_page;
  1022. struct nfs_write_data *data;
  1023. size_t wsize = desc->pg_bsize, nbytes;
  1024. unsigned int offset;
  1025. int requests = 0;
  1026. struct nfs_commit_info cinfo;
  1027. nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
  1028. if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
  1029. (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) ||
  1030. desc->pg_count > wsize))
  1031. desc->pg_ioflags &= ~FLUSH_COND_STABLE;
  1032. offset = 0;
  1033. nbytes = desc->pg_count;
  1034. do {
  1035. size_t len = min(nbytes, wsize);
  1036. data = nfs_writedata_alloc(hdr, 1);
  1037. if (!data) {
  1038. nfs_flush_error(desc, hdr);
  1039. return -ENOMEM;
  1040. }
  1041. data->pages.pagevec[0] = page;
  1042. nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo);
  1043. list_add(&data->list, &hdr->rpc_list);
  1044. requests++;
  1045. nbytes -= len;
  1046. offset += len;
  1047. } while (nbytes != 0);
  1048. nfs_list_remove_request(req);
  1049. nfs_list_add_request(req, &hdr->pages);
  1050. desc->pg_rpc_callops = &nfs_write_common_ops;
  1051. return 0;
  1052. }
  1053. /*
  1054. * Create an RPC task for the given write request and kick it.
  1055. * The page must have been locked by the caller.
  1056. *
  1057. * It may happen that the page we're passed is not marked dirty.
  1058. * This is the case if nfs_updatepage detects a conflicting request
  1059. * that has been written but not committed.
  1060. */
  1061. static int nfs_flush_one(struct nfs_pageio_descriptor *desc,
  1062. struct nfs_pgio_header *hdr)
  1063. {
  1064. struct nfs_page *req;
  1065. struct page **pages;
  1066. struct nfs_write_data *data;
  1067. struct list_head *head = &desc->pg_list;
  1068. struct nfs_commit_info cinfo;
  1069. data = nfs_writedata_alloc(hdr, nfs_page_array_len(desc->pg_base,
  1070. desc->pg_count));
  1071. if (!data) {
  1072. nfs_flush_error(desc, hdr);
  1073. return -ENOMEM;
  1074. }
  1075. nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
  1076. pages = data->pages.pagevec;
  1077. while (!list_empty(head)) {
  1078. req = nfs_list_entry(head->next);
  1079. nfs_list_remove_request(req);
  1080. nfs_list_add_request(req, &hdr->pages);
  1081. *pages++ = req->wb_page;
  1082. }
  1083. if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
  1084. (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
  1085. desc->pg_ioflags &= ~FLUSH_COND_STABLE;
  1086. /* Set up the argument struct */
  1087. nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
  1088. list_add(&data->list, &hdr->rpc_list);
  1089. desc->pg_rpc_callops = &nfs_write_common_ops;
  1090. return 0;
  1091. }
  1092. int nfs_generic_flush(struct nfs_pageio_descriptor *desc,
  1093. struct nfs_pgio_header *hdr)
  1094. {
  1095. if (desc->pg_bsize < PAGE_CACHE_SIZE)
  1096. return nfs_flush_multi(desc, hdr);
  1097. return nfs_flush_one(desc, hdr);
  1098. }
  1099. EXPORT_SYMBOL_GPL(nfs_generic_flush);
  1100. static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
  1101. {
  1102. struct nfs_write_header *whdr;
  1103. struct nfs_pgio_header *hdr;
  1104. int ret;
  1105. whdr = nfs_writehdr_alloc();
  1106. if (!whdr) {
  1107. desc->pg_completion_ops->error_cleanup(&desc->pg_list);
  1108. return -ENOMEM;
  1109. }
  1110. hdr = &whdr->header;
  1111. nfs_pgheader_init(desc, hdr, nfs_writehdr_free);
  1112. atomic_inc(&hdr->refcnt);
  1113. ret = nfs_generic_flush(desc, hdr);
  1114. if (ret == 0)
  1115. ret = nfs_do_multiple_writes(&hdr->rpc_list,
  1116. desc->pg_rpc_callops,
  1117. desc->pg_ioflags);
  1118. if (atomic_dec_and_test(&hdr->refcnt))
  1119. hdr->completion_ops->completion(hdr);
  1120. return ret;
  1121. }
  1122. static const struct nfs_pageio_ops nfs_pageio_write_ops = {
  1123. .pg_test = nfs_generic_pg_test,
  1124. .pg_doio = nfs_generic_pg_writepages,
  1125. };
  1126. void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
  1127. struct inode *inode, int ioflags, bool force_mds,
  1128. const struct nfs_pgio_completion_ops *compl_ops)
  1129. {
  1130. struct nfs_server *server = NFS_SERVER(inode);
  1131. const struct nfs_pageio_ops *pg_ops = &nfs_pageio_write_ops;
  1132. #ifdef CONFIG_NFS_V4_1
  1133. if (server->pnfs_curr_ld && !force_mds)
  1134. pg_ops = server->pnfs_curr_ld->pg_write_ops;
  1135. #endif
  1136. nfs_pageio_init(pgio, inode, pg_ops, compl_ops, server->wsize, ioflags);
  1137. }
  1138. EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
  1139. void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
  1140. {
  1141. pgio->pg_ops = &nfs_pageio_write_ops;
  1142. pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
  1143. }
  1144. EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
  1145. void nfs_write_prepare(struct rpc_task *task, void *calldata)
  1146. {
  1147. struct nfs_write_data *data = calldata;
  1148. int err;
  1149. err = NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data);
  1150. if (err)
  1151. rpc_exit(task, err);
  1152. }
  1153. void nfs_commit_prepare(struct rpc_task *task, void *calldata)
  1154. {
  1155. struct nfs_commit_data *data = calldata;
  1156. NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
  1157. }
  1158. /*
  1159. * Handle a write reply that flushes a whole page.
  1160. *
  1161. * FIXME: There is an inherent race with invalidate_inode_pages and
  1162. * writebacks since the page->count is kept > 1 for as long
  1163. * as the page has a write request pending.
  1164. */
  1165. static void nfs_writeback_done_common(struct rpc_task *task, void *calldata)
  1166. {
  1167. struct nfs_write_data *data = calldata;
  1168. nfs_writeback_done(task, data);
  1169. }
  1170. static void nfs_writeback_release_common(void *calldata)
  1171. {
  1172. struct nfs_write_data *data = calldata;
  1173. struct nfs_pgio_header *hdr = data->header;
  1174. int status = data->task.tk_status;
  1175. if ((status >= 0) && nfs_write_need_commit(data)) {
  1176. spin_lock(&hdr->lock);
  1177. if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags))
  1178. ; /* Do nothing */
  1179. else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags))
  1180. memcpy(hdr->verf, &data->verf, sizeof(*hdr->verf));
  1181. else if (memcmp(hdr->verf, &data->verf, sizeof(*hdr->verf)))
  1182. set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags);
  1183. spin_unlock(&hdr->lock);
  1184. }
  1185. nfs_writedata_release(data);
  1186. }
  1187. static const struct rpc_call_ops nfs_write_common_ops = {
  1188. .rpc_call_prepare = nfs_write_prepare,
  1189. .rpc_call_done = nfs_writeback_done_common,
  1190. .rpc_release = nfs_writeback_release_common,
  1191. };
  1192. /*
  1193. * Special version of should_remove_suid() that ignores capabilities.
  1194. */
  1195. static int nfs_should_remove_suid(const struct inode *inode)
  1196. {
  1197. umode_t mode = inode->i_mode;
  1198. int kill = 0;
  1199. /* suid always must be killed */
  1200. if (unlikely(mode & S_ISUID))
  1201. kill = ATTR_KILL_SUID;
  1202. /*
  1203. * sgid without any exec bits is just a mandatory locking mark; leave
  1204. * it alone. If some exec bits are set, it's a real sgid; kill it.
  1205. */
  1206. if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
  1207. kill |= ATTR_KILL_SGID;
  1208. if (unlikely(kill && S_ISREG(mode)))
  1209. return kill;
  1210. return 0;
  1211. }
  1212. /*
  1213. * This function is called when the WRITE call is complete.
  1214. */
  1215. void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
  1216. {
  1217. struct nfs_writeargs *argp = &data->args;
  1218. struct nfs_writeres *resp = &data->res;
  1219. struct inode *inode = data->header->inode;
  1220. int status;
  1221. dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
  1222. task->tk_pid, task->tk_status);
  1223. /*
  1224. * ->write_done will attempt to use post-op attributes to detect
  1225. * conflicting writes by other clients. A strict interpretation
  1226. * of close-to-open would allow us to continue caching even if
  1227. * another writer had changed the file, but some applications
  1228. * depend on tighter cache coherency when writing.
  1229. */
  1230. status = NFS_PROTO(inode)->write_done(task, data);
  1231. if (status != 0)
  1232. return;
  1233. nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
  1234. #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
  1235. if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
  1236. /* We tried a write call, but the server did not
  1237. * commit data to stable storage even though we
  1238. * requested it.
  1239. * Note: There is a known bug in Tru64 < 5.0 in which
  1240. * the server reports NFS_DATA_SYNC, but performs
  1241. * NFS_FILE_SYNC. We therefore implement this checking
  1242. * as a dprintk() in order to avoid filling syslog.
  1243. */
  1244. static unsigned long complain;
  1245. /* Note this will print the MDS for a DS write */
  1246. if (time_before(complain, jiffies)) {
  1247. dprintk("NFS: faulty NFS server %s:"
  1248. " (committed = %d) != (stable = %d)\n",
  1249. NFS_SERVER(inode)->nfs_client->cl_hostname,
  1250. resp->verf->committed, argp->stable);
  1251. complain = jiffies + 300 * HZ;
  1252. }
  1253. }
  1254. #endif
  1255. if (task->tk_status < 0) {
  1256. nfs_set_pgio_error(data->header, task->tk_status, argp->offset);
  1257. return;
  1258. }
  1259. /* Deal with the suid/sgid bit corner case */
  1260. if (nfs_should_remove_suid(inode))
  1261. nfs_mark_for_revalidate(inode);
  1262. if (resp->count < argp->count) {
  1263. static unsigned long complain;
  1264. /* This a short write! */
  1265. nfs_inc_stats(inode, NFSIOS_SHORTWRITE);
  1266. /* Has the server at least made some progress? */
  1267. if (resp->count == 0) {
  1268. if (time_before(complain, jiffies)) {
  1269. printk(KERN_WARNING
  1270. "NFS: Server wrote zero bytes, expected %u.\n",
  1271. argp->count);
  1272. complain = jiffies + 300 * HZ;
  1273. }
  1274. nfs_set_pgio_error(data->header, -EIO, argp->offset);
  1275. task->tk_status = -EIO;
  1276. return;
  1277. }
  1278. /* Was this an NFSv2 write or an NFSv3 stable write? */
  1279. if (resp->verf->committed != NFS_UNSTABLE) {
  1280. /* Resend from where the server left off */
  1281. data->mds_offset += resp->count;
  1282. argp->offset += resp->count;
  1283. argp->pgbase += resp->count;
  1284. argp->count -= resp->count;
  1285. } else {
  1286. /* Resend as a stable write in order to avoid
  1287. * headaches in the case of a server crash.
  1288. */
  1289. argp->stable = NFS_FILE_SYNC;
  1290. }
  1291. rpc_restart_call_prepare(task);
  1292. }
  1293. }
  1294. #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
  1295. static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
  1296. {
  1297. int ret;
  1298. if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
  1299. return 1;
  1300. if (!may_wait)
  1301. return 0;
  1302. ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
  1303. NFS_INO_COMMIT,
  1304. nfs_wait_bit_killable,
  1305. TASK_KILLABLE);
  1306. return (ret < 0) ? ret : 1;
  1307. }
  1308. static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
  1309. {
  1310. clear_bit(NFS_INO_COMMIT, &nfsi->flags);
  1311. smp_mb__after_clear_bit();
  1312. wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
  1313. }
  1314. void nfs_commitdata_release(struct nfs_commit_data *data)
  1315. {
  1316. put_nfs_open_context(data->context);
  1317. nfs_commit_free(data);
  1318. }
  1319. EXPORT_SYMBOL_GPL(nfs_commitdata_release);
  1320. int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
  1321. const struct rpc_call_ops *call_ops,
  1322. int how, int flags)
  1323. {
  1324. struct rpc_task *task;
  1325. int priority = flush_task_priority(how);
  1326. struct rpc_message msg = {
  1327. .rpc_argp = &data->args,
  1328. .rpc_resp = &data->res,
  1329. .rpc_cred = data->cred,
  1330. };
  1331. struct rpc_task_setup task_setup_data = {
  1332. .task = &data->task,
  1333. .rpc_client = clnt,
  1334. .rpc_message = &msg,
  1335. .callback_ops = call_ops,
  1336. .callback_data = data,
  1337. .workqueue = nfsiod_workqueue,
  1338. .flags = RPC_TASK_ASYNC | flags,
  1339. .priority = priority,
  1340. };
  1341. /* Set up the initial task struct. */
  1342. NFS_PROTO(data->inode)->commit_setup(data, &msg);
  1343. dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
  1344. nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
  1345. NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
  1346. task = rpc_run_task(&task_setup_data);
  1347. if (IS_ERR(task))
  1348. return PTR_ERR(task);
  1349. if (how & FLUSH_SYNC)
  1350. rpc_wait_for_completion_task(task);
  1351. rpc_put_task(task);
  1352. return 0;
  1353. }
  1354. EXPORT_SYMBOL_GPL(nfs_initiate_commit);
  1355. /*
  1356. * Set up the argument/result storage required for the RPC call.
  1357. */
  1358. void nfs_init_commit(struct nfs_commit_data *data,
  1359. struct list_head *head,
  1360. struct pnfs_layout_segment *lseg,
  1361. struct nfs_commit_info *cinfo)
  1362. {
  1363. struct nfs_page *first = nfs_list_entry(head->next);
  1364. struct inode *inode = first->wb_context->dentry->d_inode;
  1365. /* Set up the RPC argument and reply structs
  1366. * NB: take care not to mess about with data->commit et al. */
  1367. list_splice_init(head, &data->pages);
  1368. data->inode = inode;
  1369. data->cred = first->wb_context->cred;
  1370. data->lseg = lseg; /* reference transferred */
  1371. data->mds_ops = &nfs_commit_ops;
  1372. data->completion_ops = cinfo->completion_ops;
  1373. data->dreq = cinfo->dreq;
  1374. data->args.fh = NFS_FH(data->inode);
  1375. /* Note: we always request a commit of the entire inode */
  1376. data->args.offset = 0;
  1377. data->args.count = 0;
  1378. data->context = get_nfs_open_context(first->wb_context);
  1379. data->res.fattr = &data->fattr;
  1380. data->res.verf = &data->verf;
  1381. nfs_fattr_init(&data->fattr);
  1382. }
  1383. EXPORT_SYMBOL_GPL(nfs_init_commit);
  1384. void nfs_retry_commit(struct list_head *page_list,
  1385. struct pnfs_layout_segment *lseg,
  1386. struct nfs_commit_info *cinfo)
  1387. {
  1388. struct nfs_page *req;
  1389. while (!list_empty(page_list)) {
  1390. req = nfs_list_entry(page_list->next);
  1391. nfs_list_remove_request(req);
  1392. nfs_mark_request_commit(req, lseg, cinfo);
  1393. if (!cinfo->dreq) {
  1394. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  1395. dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
  1396. BDI_RECLAIMABLE);
  1397. }
  1398. nfs_unlock_and_release_request(req);
  1399. }
  1400. }
  1401. EXPORT_SYMBOL_GPL(nfs_retry_commit);
  1402. /*
  1403. * Commit dirty pages
  1404. */
  1405. static int
  1406. nfs_commit_list(struct inode *inode, struct list_head *head, int how,
  1407. struct nfs_commit_info *cinfo)
  1408. {
  1409. struct nfs_commit_data *data;
  1410. data = nfs_commitdata_alloc();
  1411. if (!data)
  1412. goto out_bad;
  1413. /* Set up the argument struct */
  1414. nfs_init_commit(data, head, NULL, cinfo);
  1415. atomic_inc(&cinfo->mds->rpcs_out);
  1416. return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops,
  1417. how, 0);
  1418. out_bad:
  1419. nfs_retry_commit(head, NULL, cinfo);
  1420. cinfo->completion_ops->error_cleanup(NFS_I(inode));
  1421. return -ENOMEM;
  1422. }
  1423. /*
  1424. * COMMIT call returned
  1425. */
  1426. static void nfs_commit_done(struct rpc_task *task, void *calldata)
  1427. {
  1428. struct nfs_commit_data *data = calldata;
  1429. dprintk("NFS: %5u nfs_commit_done (status %d)\n",
  1430. task->tk_pid, task->tk_status);
  1431. /* Call the NFS version-specific code */
  1432. NFS_PROTO(data->inode)->commit_done(task, data);
  1433. }
  1434. static void nfs_commit_release_pages(struct nfs_commit_data *data)
  1435. {
  1436. struct nfs_page *req;
  1437. int status = data->task.tk_status;
  1438. struct nfs_commit_info cinfo;
  1439. while (!list_empty(&data->pages)) {
  1440. req = nfs_list_entry(data->pages.next);
  1441. nfs_list_remove_request(req);
  1442. nfs_clear_page_commit(req->wb_page);
  1443. dprintk("NFS: commit (%s/%llu %d@%lld)",
  1444. req->wb_context->dentry->d_sb->s_id,
  1445. (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  1446. req->wb_bytes,
  1447. (long long)req_offset(req));
  1448. if (status < 0) {
  1449. nfs_context_set_write_error(req->wb_context, status);
  1450. nfs_inode_remove_request(req);
  1451. dprintk(", error = %d\n", status);
  1452. goto next;
  1453. }
  1454. /* Okay, COMMIT succeeded, apparently. Check the verifier
  1455. * returned by the server against all stored verfs. */
  1456. if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) {
  1457. /* We have a match */
  1458. nfs_inode_remove_request(req);
  1459. dprintk(" OK\n");
  1460. goto next;
  1461. }
  1462. /* We have a mismatch. Write the page again */
  1463. dprintk(" mismatch\n");
  1464. nfs_mark_request_dirty(req);
  1465. set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
  1466. next:
  1467. nfs_unlock_and_release_request(req);
  1468. }
  1469. nfs_init_cinfo(&cinfo, data->inode, data->dreq);
  1470. if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
  1471. nfs_commit_clear_lock(NFS_I(data->inode));
  1472. }
  1473. static void nfs_commit_release(void *calldata)
  1474. {
  1475. struct nfs_commit_data *data = calldata;
  1476. data->completion_ops->completion(data);
  1477. nfs_commitdata_release(calldata);
  1478. }
  1479. static const struct rpc_call_ops nfs_commit_ops = {
  1480. .rpc_call_prepare = nfs_commit_prepare,
  1481. .rpc_call_done = nfs_commit_done,
  1482. .rpc_release = nfs_commit_release,
  1483. };
  1484. static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
  1485. .completion = nfs_commit_release_pages,
  1486. .error_cleanup = nfs_commit_clear_lock,
  1487. };
  1488. int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
  1489. int how, struct nfs_commit_info *cinfo)
  1490. {
  1491. int status;
  1492. status = pnfs_commit_list(inode, head, how, cinfo);
  1493. if (status == PNFS_NOT_ATTEMPTED)
  1494. status = nfs_commit_list(inode, head, how, cinfo);
  1495. return status;
  1496. }
  1497. int nfs_commit_inode(struct inode *inode, int how)
  1498. {
  1499. LIST_HEAD(head);
  1500. struct nfs_commit_info cinfo;
  1501. int may_wait = how & FLUSH_SYNC;
  1502. int res;
  1503. res = nfs_commit_set_lock(NFS_I(inode), may_wait);
  1504. if (res <= 0)
  1505. goto out_mark_dirty;
  1506. nfs_init_cinfo_from_inode(&cinfo, inode);
  1507. res = nfs_scan_commit(inode, &head, &cinfo);
  1508. if (res) {
  1509. int error;
  1510. error = nfs_generic_commit_list(inode, &head, how, &cinfo);
  1511. if (error < 0)
  1512. return error;
  1513. if (!may_wait)
  1514. goto out_mark_dirty;
  1515. error = wait_on_bit(&NFS_I(inode)->flags,
  1516. NFS_INO_COMMIT,
  1517. nfs_wait_bit_killable,
  1518. TASK_KILLABLE);
  1519. if (error < 0)
  1520. return error;
  1521. } else
  1522. nfs_commit_clear_lock(NFS_I(inode));
  1523. return res;
  1524. /* Note: If we exit without ensuring that the commit is complete,
  1525. * we must mark the inode as dirty. Otherwise, future calls to
  1526. * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
  1527. * that the data is on the disk.
  1528. */
  1529. out_mark_dirty:
  1530. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  1531. return res;
  1532. }
  1533. static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
  1534. {
  1535. struct nfs_inode *nfsi = NFS_I(inode);
  1536. int flags = FLUSH_SYNC;
  1537. int ret = 0;
  1538. /* no commits means nothing needs to be done */
  1539. if (!nfsi->commit_info.ncommit)
  1540. return ret;
  1541. if (wbc->sync_mode == WB_SYNC_NONE) {
  1542. /* Don't commit yet if this is a non-blocking flush and there
  1543. * are a lot of outstanding writes for this mapping.
  1544. */
  1545. if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
  1546. goto out_mark_dirty;
  1547. /* don't wait for the COMMIT response */
  1548. flags = 0;
  1549. }
  1550. ret = nfs_commit_inode(inode, flags);
  1551. if (ret >= 0) {
  1552. if (wbc->sync_mode == WB_SYNC_NONE) {
  1553. if (ret < wbc->nr_to_write)
  1554. wbc->nr_to_write -= ret;
  1555. else
  1556. wbc->nr_to_write = 0;
  1557. }
  1558. return 0;
  1559. }
  1560. out_mark_dirty:
  1561. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  1562. return ret;
  1563. }
  1564. #else
  1565. static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
  1566. {
  1567. return 0;
  1568. }
  1569. #endif
  1570. int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  1571. {
  1572. return nfs_commit_unstable_pages(inode, wbc);
  1573. }
  1574. EXPORT_SYMBOL_GPL(nfs_write_inode);
  1575. /*
  1576. * flush the inode to disk.
  1577. */
  1578. int nfs_wb_all(struct inode *inode)
  1579. {
  1580. struct writeback_control wbc = {
  1581. .sync_mode = WB_SYNC_ALL,
  1582. .nr_to_write = LONG_MAX,
  1583. .range_start = 0,
  1584. .range_end = LLONG_MAX,
  1585. };
  1586. int ret;
  1587. trace_nfs_writeback_inode_enter(inode);
  1588. ret = sync_inode(inode, &wbc);
  1589. trace_nfs_writeback_inode_exit(inode, ret);
  1590. return ret;
  1591. }
  1592. EXPORT_SYMBOL_GPL(nfs_wb_all);
  1593. int nfs_wb_page_cancel(struct inode *inode, struct page *page)
  1594. {
  1595. struct nfs_page *req;
  1596. int ret = 0;
  1597. for (;;) {
  1598. wait_on_page_writeback(page);
  1599. req = nfs_page_find_request(page);
  1600. if (req == NULL)
  1601. break;
  1602. if (nfs_lock_request(req)) {
  1603. nfs_clear_request_commit(req);
  1604. nfs_inode_remove_request(req);
  1605. /*
  1606. * In case nfs_inode_remove_request has marked the
  1607. * page as being dirty
  1608. */
  1609. cancel_dirty_page(page, PAGE_CACHE_SIZE);
  1610. nfs_unlock_and_release_request(req);
  1611. break;
  1612. }
  1613. ret = nfs_wait_on_request(req);
  1614. nfs_release_request(req);
  1615. if (ret < 0)
  1616. break;
  1617. }
  1618. return ret;
  1619. }
  1620. /*
  1621. * Write back all requests on one page - we do this before reading it.
  1622. */
  1623. int nfs_wb_page(struct inode *inode, struct page *page)
  1624. {
  1625. loff_t range_start = page_file_offset(page);
  1626. loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
  1627. struct writeback_control wbc = {
  1628. .sync_mode = WB_SYNC_ALL,
  1629. .nr_to_write = 0,
  1630. .range_start = range_start,
  1631. .range_end = range_end,
  1632. };
  1633. int ret;
  1634. trace_nfs_writeback_page_enter(inode);
  1635. for (;;) {
  1636. wait_on_page_writeback(page);
  1637. if (clear_page_dirty_for_io(page)) {
  1638. ret = nfs_writepage_locked(page, &wbc);
  1639. if (ret < 0)
  1640. goto out_error;
  1641. continue;
  1642. }
  1643. ret = 0;
  1644. if (!PagePrivate(page))
  1645. break;
  1646. ret = nfs_commit_inode(inode, FLUSH_SYNC);
  1647. if (ret < 0)
  1648. goto out_error;
  1649. }
  1650. out_error:
  1651. trace_nfs_writeback_page_exit(inode, ret);
  1652. return ret;
  1653. }
  1654. #ifdef CONFIG_MIGRATION
  1655. int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
  1656. struct page *page, enum migrate_mode mode)
  1657. {
  1658. /*
  1659. * If PagePrivate is set, then the page is currently associated with
  1660. * an in-progress read or write request. Don't try to migrate it.
  1661. *
  1662. * FIXME: we could do this in principle, but we'll need a way to ensure
  1663. * that we can safely release the inode reference while holding
  1664. * the page lock.
  1665. */
  1666. if (PagePrivate(page))
  1667. return -EBUSY;
  1668. if (!nfs_fscache_release_page(page, GFP_KERNEL))
  1669. return -EBUSY;
  1670. return migrate_page(mapping, newpage, page, mode);
  1671. }
  1672. #endif
  1673. int __init nfs_init_writepagecache(void)
  1674. {
  1675. nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
  1676. sizeof(struct nfs_write_header),
  1677. 0, SLAB_HWCACHE_ALIGN,
  1678. NULL);
  1679. if (nfs_wdata_cachep == NULL)
  1680. return -ENOMEM;
  1681. nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
  1682. nfs_wdata_cachep);
  1683. if (nfs_wdata_mempool == NULL)
  1684. goto out_destroy_write_cache;
  1685. nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
  1686. sizeof(struct nfs_commit_data),
  1687. 0, SLAB_HWCACHE_ALIGN,
  1688. NULL);
  1689. if (nfs_cdata_cachep == NULL)
  1690. goto out_destroy_write_mempool;
  1691. nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
  1692. nfs_cdata_cachep);
  1693. if (nfs_commit_mempool == NULL)
  1694. goto out_destroy_commit_cache;
  1695. /*
  1696. * NFS congestion size, scale with available memory.
  1697. *
  1698. * 64MB: 8192k
  1699. * 128MB: 11585k
  1700. * 256MB: 16384k
  1701. * 512MB: 23170k
  1702. * 1GB: 32768k
  1703. * 2GB: 46340k
  1704. * 4GB: 65536k
  1705. * 8GB: 92681k
  1706. * 16GB: 131072k
  1707. *
  1708. * This allows larger machines to have larger/more transfers.
  1709. * Limit the default to 256M
  1710. */
  1711. nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
  1712. if (nfs_congestion_kb > 256*1024)
  1713. nfs_congestion_kb = 256*1024;
  1714. return 0;
  1715. out_destroy_commit_cache:
  1716. kmem_cache_destroy(nfs_cdata_cachep);
  1717. out_destroy_write_mempool:
  1718. mempool_destroy(nfs_wdata_mempool);
  1719. out_destroy_write_cache:
  1720. kmem_cache_destroy(nfs_wdata_cachep);
  1721. return -ENOMEM;
  1722. }
  1723. void nfs_destroy_writepagecache(void)
  1724. {
  1725. mempool_destroy(nfs_commit_mempool);
  1726. kmem_cache_destroy(nfs_cdata_cachep);
  1727. mempool_destroy(nfs_wdata_mempool);
  1728. kmem_cache_destroy(nfs_wdata_cachep);
  1729. }