write.c 48 KB

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