write.c 42 KB

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