write.c 41 KB

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