write.c 50 KB

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