write.c 44 KB

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