write.c 47 KB

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