write.c 41 KB

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