write.c 48 KB

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