write.c 42 KB

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