write.c 47 KB

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