write.c 48 KB

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