write.c 45 KB

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