write.c 43 KB

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