write.c 48 KB

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