write.c 47 KB

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