write.c 44 KB

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