write.c 47 KB

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