write.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  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. page_cache_get(page);
  174. if (atomic_long_inc_return(&nfss->writeback) >
  175. NFS_CONGESTION_ON_THRESH) {
  176. set_bdi_congested(&nfss->backing_dev_info,
  177. BLK_RW_ASYNC);
  178. }
  179. }
  180. return ret;
  181. }
  182. static void nfs_end_page_writeback(struct page *page)
  183. {
  184. struct inode *inode = page->mapping->host;
  185. struct nfs_server *nfss = NFS_SERVER(inode);
  186. end_page_writeback(page);
  187. page_cache_release(page);
  188. if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
  189. clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
  190. }
  191. static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
  192. {
  193. struct inode *inode = page->mapping->host;
  194. struct nfs_page *req;
  195. int ret;
  196. spin_lock(&inode->i_lock);
  197. for (;;) {
  198. req = nfs_page_find_request_locked(page);
  199. if (req == NULL)
  200. break;
  201. if (nfs_set_page_tag_locked(req))
  202. break;
  203. /* Note: If we hold the page lock, as is the case in nfs_writepage,
  204. * then the call to nfs_set_page_tag_locked() will always
  205. * succeed provided that someone hasn't already marked the
  206. * request as dirty (in which case we don't care).
  207. */
  208. spin_unlock(&inode->i_lock);
  209. if (!nonblock)
  210. ret = nfs_wait_on_request(req);
  211. else
  212. ret = -EAGAIN;
  213. nfs_release_request(req);
  214. if (ret != 0)
  215. return ERR_PTR(ret);
  216. spin_lock(&inode->i_lock);
  217. }
  218. spin_unlock(&inode->i_lock);
  219. return req;
  220. }
  221. /*
  222. * Find an associated nfs write request, and prepare to flush it out
  223. * May return an error if the user signalled nfs_wait_on_request().
  224. */
  225. static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
  226. struct page *page, bool nonblock)
  227. {
  228. struct nfs_page *req;
  229. int ret = 0;
  230. req = nfs_find_and_lock_request(page, nonblock);
  231. if (!req)
  232. goto out;
  233. ret = PTR_ERR(req);
  234. if (IS_ERR(req))
  235. goto out;
  236. ret = nfs_set_page_writeback(page);
  237. BUG_ON(ret != 0);
  238. BUG_ON(test_bit(PG_CLEAN, &req->wb_flags));
  239. if (!nfs_pageio_add_request(pgio, req)) {
  240. nfs_redirty_request(req);
  241. ret = pgio->pg_error;
  242. }
  243. out:
  244. return ret;
  245. }
  246. static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
  247. {
  248. struct inode *inode = page->mapping->host;
  249. int ret;
  250. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
  251. nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
  252. nfs_pageio_cond_complete(pgio, page->index);
  253. ret = nfs_page_async_flush(pgio, page,
  254. wbc->sync_mode == WB_SYNC_NONE ||
  255. wbc->nonblocking != 0);
  256. if (ret == -EAGAIN) {
  257. redirty_page_for_writepage(wbc, page);
  258. ret = 0;
  259. }
  260. return ret;
  261. }
  262. /*
  263. * Write an mmapped page to the server.
  264. */
  265. static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
  266. {
  267. struct nfs_pageio_descriptor pgio;
  268. int err;
  269. nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
  270. err = nfs_do_writepage(page, wbc, &pgio);
  271. nfs_pageio_complete(&pgio);
  272. if (err < 0)
  273. return err;
  274. if (pgio.pg_error < 0)
  275. return pgio.pg_error;
  276. return 0;
  277. }
  278. int nfs_writepage(struct page *page, struct writeback_control *wbc)
  279. {
  280. int ret;
  281. ret = nfs_writepage_locked(page, wbc);
  282. unlock_page(page);
  283. return ret;
  284. }
  285. static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
  286. {
  287. int ret;
  288. ret = nfs_do_writepage(page, wbc, data);
  289. unlock_page(page);
  290. return ret;
  291. }
  292. int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  293. {
  294. struct inode *inode = mapping->host;
  295. unsigned long *bitlock = &NFS_I(inode)->flags;
  296. struct nfs_pageio_descriptor pgio;
  297. int err;
  298. /* Stop dirtying of new pages while we sync */
  299. err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
  300. nfs_wait_bit_killable, TASK_KILLABLE);
  301. if (err)
  302. goto out_err;
  303. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
  304. nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
  305. err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
  306. nfs_pageio_complete(&pgio);
  307. clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
  308. smp_mb__after_clear_bit();
  309. wake_up_bit(bitlock, NFS_INO_FLUSHING);
  310. if (err < 0)
  311. goto out_err;
  312. err = pgio.pg_error;
  313. if (err < 0)
  314. goto out_err;
  315. return 0;
  316. out_err:
  317. return err;
  318. }
  319. /*
  320. * Insert a write request into an inode
  321. */
  322. static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
  323. {
  324. struct nfs_inode *nfsi = NFS_I(inode);
  325. int error;
  326. error = radix_tree_preload(GFP_NOFS);
  327. if (error != 0)
  328. goto out;
  329. /* Lock the request! */
  330. nfs_lock_request_dontget(req);
  331. spin_lock(&inode->i_lock);
  332. error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
  333. BUG_ON(error);
  334. if (!nfsi->npages) {
  335. igrab(inode);
  336. if (nfs_have_delegation(inode, FMODE_WRITE))
  337. nfsi->change_attr++;
  338. }
  339. SetPagePrivate(req->wb_page);
  340. set_page_private(req->wb_page, (unsigned long)req);
  341. nfsi->npages++;
  342. kref_get(&req->wb_kref);
  343. radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
  344. NFS_PAGE_TAG_LOCKED);
  345. spin_unlock(&inode->i_lock);
  346. radix_tree_preload_end();
  347. out:
  348. return error;
  349. }
  350. /*
  351. * Remove a write request from an inode
  352. */
  353. static void nfs_inode_remove_request(struct nfs_page *req)
  354. {
  355. struct inode *inode = req->wb_context->path.dentry->d_inode;
  356. struct nfs_inode *nfsi = NFS_I(inode);
  357. BUG_ON (!NFS_WBACK_BUSY(req));
  358. spin_lock(&inode->i_lock);
  359. set_page_private(req->wb_page, 0);
  360. ClearPagePrivate(req->wb_page);
  361. radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
  362. nfsi->npages--;
  363. if (!nfsi->npages) {
  364. spin_unlock(&inode->i_lock);
  365. iput(inode);
  366. } else
  367. spin_unlock(&inode->i_lock);
  368. nfs_clear_request(req);
  369. nfs_release_request(req);
  370. }
  371. static void
  372. nfs_mark_request_dirty(struct nfs_page *req)
  373. {
  374. __set_page_dirty_nobuffers(req->wb_page);
  375. __mark_inode_dirty(req->wb_page->mapping->host, I_DIRTY_DATASYNC);
  376. }
  377. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  378. /*
  379. * Add a request to the inode's commit list.
  380. */
  381. static void
  382. nfs_mark_request_commit(struct nfs_page *req)
  383. {
  384. struct inode *inode = req->wb_context->path.dentry->d_inode;
  385. struct nfs_inode *nfsi = NFS_I(inode);
  386. spin_lock(&inode->i_lock);
  387. set_bit(PG_CLEAN, &(req)->wb_flags);
  388. radix_tree_tag_set(&nfsi->nfs_page_tree,
  389. req->wb_index,
  390. NFS_PAGE_TAG_COMMIT);
  391. nfsi->ncommit++;
  392. spin_unlock(&inode->i_lock);
  393. inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  394. inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
  395. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  396. }
  397. static int
  398. nfs_clear_request_commit(struct nfs_page *req)
  399. {
  400. struct page *page = req->wb_page;
  401. if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
  402. dec_zone_page_state(page, NR_UNSTABLE_NFS);
  403. dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
  404. return 1;
  405. }
  406. return 0;
  407. }
  408. static inline
  409. int nfs_write_need_commit(struct nfs_write_data *data)
  410. {
  411. return data->verf.committed != NFS_FILE_SYNC;
  412. }
  413. static inline
  414. int nfs_reschedule_unstable_write(struct nfs_page *req)
  415. {
  416. if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
  417. nfs_mark_request_commit(req);
  418. return 1;
  419. }
  420. if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
  421. nfs_mark_request_dirty(req);
  422. return 1;
  423. }
  424. return 0;
  425. }
  426. #else
  427. static inline void
  428. nfs_mark_request_commit(struct nfs_page *req)
  429. {
  430. }
  431. static inline int
  432. nfs_clear_request_commit(struct nfs_page *req)
  433. {
  434. return 0;
  435. }
  436. static inline
  437. int nfs_write_need_commit(struct nfs_write_data *data)
  438. {
  439. return 0;
  440. }
  441. static inline
  442. int nfs_reschedule_unstable_write(struct nfs_page *req)
  443. {
  444. return 0;
  445. }
  446. #endif
  447. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  448. static int
  449. nfs_need_commit(struct nfs_inode *nfsi)
  450. {
  451. return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT);
  452. }
  453. /*
  454. * nfs_scan_commit - Scan an inode for commit requests
  455. * @inode: NFS inode to scan
  456. * @dst: destination list
  457. * @idx_start: lower bound of page->index to scan.
  458. * @npages: idx_start + npages sets the upper bound to scan.
  459. *
  460. * Moves requests from the inode's 'commit' request list.
  461. * The requests are *not* checked to ensure that they form a contiguous set.
  462. */
  463. static int
  464. nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
  465. {
  466. struct nfs_inode *nfsi = NFS_I(inode);
  467. int ret;
  468. if (!nfs_need_commit(nfsi))
  469. return 0;
  470. ret = nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT);
  471. if (ret > 0)
  472. nfsi->ncommit -= ret;
  473. if (nfs_need_commit(NFS_I(inode)))
  474. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  475. return ret;
  476. }
  477. #else
  478. static inline int nfs_need_commit(struct nfs_inode *nfsi)
  479. {
  480. return 0;
  481. }
  482. static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
  483. {
  484. return 0;
  485. }
  486. #endif
  487. /*
  488. * Search for an existing write request, and attempt to update
  489. * it to reflect a new dirty region on a given page.
  490. *
  491. * If the attempt fails, then the existing request is flushed out
  492. * to disk.
  493. */
  494. static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
  495. struct page *page,
  496. unsigned int offset,
  497. unsigned int bytes)
  498. {
  499. struct nfs_page *req;
  500. unsigned int rqend;
  501. unsigned int end;
  502. int error;
  503. if (!PagePrivate(page))
  504. return NULL;
  505. end = offset + bytes;
  506. spin_lock(&inode->i_lock);
  507. for (;;) {
  508. req = nfs_page_find_request_locked(page);
  509. if (req == NULL)
  510. goto out_unlock;
  511. rqend = req->wb_offset + req->wb_bytes;
  512. /*
  513. * Tell the caller to flush out the request if
  514. * the offsets are non-contiguous.
  515. * Note: nfs_flush_incompatible() will already
  516. * have flushed out requests having wrong owners.
  517. */
  518. if (offset > rqend
  519. || end < req->wb_offset)
  520. goto out_flushme;
  521. if (nfs_set_page_tag_locked(req))
  522. break;
  523. /* The request is locked, so wait and then retry */
  524. spin_unlock(&inode->i_lock);
  525. error = nfs_wait_on_request(req);
  526. nfs_release_request(req);
  527. if (error != 0)
  528. goto out_err;
  529. spin_lock(&inode->i_lock);
  530. }
  531. if (nfs_clear_request_commit(req) &&
  532. radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
  533. req->wb_index, NFS_PAGE_TAG_COMMIT) != NULL)
  534. NFS_I(inode)->ncommit--;
  535. /* Okay, the request matches. Update the region */
  536. if (offset < req->wb_offset) {
  537. req->wb_offset = offset;
  538. req->wb_pgbase = offset;
  539. }
  540. if (end > rqend)
  541. req->wb_bytes = end - req->wb_offset;
  542. else
  543. req->wb_bytes = rqend - req->wb_offset;
  544. out_unlock:
  545. spin_unlock(&inode->i_lock);
  546. return req;
  547. out_flushme:
  548. spin_unlock(&inode->i_lock);
  549. nfs_release_request(req);
  550. error = nfs_wb_page(inode, page);
  551. out_err:
  552. return ERR_PTR(error);
  553. }
  554. /*
  555. * Try to update an existing write request, or create one if there is none.
  556. *
  557. * Note: Should always be called with the Page Lock held to prevent races
  558. * if we have to add a new request. Also assumes that the caller has
  559. * already called nfs_flush_incompatible() if necessary.
  560. */
  561. static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
  562. struct page *page, unsigned int offset, unsigned int bytes)
  563. {
  564. struct inode *inode = page->mapping->host;
  565. struct nfs_page *req;
  566. int error;
  567. req = nfs_try_to_update_request(inode, page, offset, bytes);
  568. if (req != NULL)
  569. goto out;
  570. req = nfs_create_request(ctx, inode, page, offset, bytes);
  571. if (IS_ERR(req))
  572. goto out;
  573. error = nfs_inode_add_request(inode, req);
  574. if (error != 0) {
  575. nfs_release_request(req);
  576. req = ERR_PTR(error);
  577. }
  578. out:
  579. return req;
  580. }
  581. static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
  582. unsigned int offset, unsigned int count)
  583. {
  584. struct nfs_page *req;
  585. req = nfs_setup_write_request(ctx, page, offset, count);
  586. if (IS_ERR(req))
  587. return PTR_ERR(req);
  588. nfs_mark_request_dirty(req);
  589. /* Update file length */
  590. nfs_grow_file(page, offset, count);
  591. nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
  592. nfs_mark_request_dirty(req);
  593. nfs_clear_page_tag_locked(req);
  594. return 0;
  595. }
  596. int nfs_flush_incompatible(struct file *file, struct page *page)
  597. {
  598. struct nfs_open_context *ctx = nfs_file_open_context(file);
  599. struct nfs_page *req;
  600. int do_flush, status;
  601. /*
  602. * Look for a request corresponding to this page. If there
  603. * is one, and it belongs to another file, we flush it out
  604. * before we try to copy anything into the page. Do this
  605. * due to the lack of an ACCESS-type call in NFSv2.
  606. * Also do the same if we find a request from an existing
  607. * dropped page.
  608. */
  609. do {
  610. req = nfs_page_find_request(page);
  611. if (req == NULL)
  612. return 0;
  613. do_flush = req->wb_page != page || req->wb_context != ctx;
  614. nfs_release_request(req);
  615. if (!do_flush)
  616. return 0;
  617. status = nfs_wb_page(page->mapping->host, page);
  618. } while (status == 0);
  619. return status;
  620. }
  621. /*
  622. * If the page cache is marked as unsafe or invalid, then we can't rely on
  623. * the PageUptodate() flag. In this case, we will need to turn off
  624. * write optimisations that depend on the page contents being correct.
  625. */
  626. static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
  627. {
  628. return PageUptodate(page) &&
  629. !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
  630. }
  631. /*
  632. * Update and possibly write a cached page of an NFS file.
  633. *
  634. * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
  635. * things with a page scheduled for an RPC call (e.g. invalidate it).
  636. */
  637. int nfs_updatepage(struct file *file, struct page *page,
  638. unsigned int offset, unsigned int count)
  639. {
  640. struct nfs_open_context *ctx = nfs_file_open_context(file);
  641. struct inode *inode = page->mapping->host;
  642. int status = 0;
  643. nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
  644. dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n",
  645. file->f_path.dentry->d_parent->d_name.name,
  646. file->f_path.dentry->d_name.name, count,
  647. (long long)(page_offset(page) + offset));
  648. /* If we're not using byte range locks, and we know the page
  649. * is up to date, it may be more efficient to extend the write
  650. * to cover the entire page in order to avoid fragmentation
  651. * inefficiencies.
  652. */
  653. if (nfs_write_pageuptodate(page, inode) &&
  654. inode->i_flock == NULL &&
  655. !(file->f_flags & O_DSYNC)) {
  656. count = max(count + offset, nfs_page_length(page));
  657. offset = 0;
  658. }
  659. status = nfs_writepage_setup(ctx, page, offset, count);
  660. if (status < 0)
  661. nfs_set_pageerror(page);
  662. dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
  663. status, (long long)i_size_read(inode));
  664. return status;
  665. }
  666. static void nfs_writepage_release(struct nfs_page *req)
  667. {
  668. struct page *page = req->wb_page;
  669. if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req))
  670. nfs_inode_remove_request(req);
  671. nfs_clear_page_tag_locked(req);
  672. nfs_end_page_writeback(page);
  673. }
  674. static int flush_task_priority(int how)
  675. {
  676. switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
  677. case FLUSH_HIGHPRI:
  678. return RPC_PRIORITY_HIGH;
  679. case FLUSH_LOWPRI:
  680. return RPC_PRIORITY_LOW;
  681. }
  682. return RPC_PRIORITY_NORMAL;
  683. }
  684. /*
  685. * Set up the argument/result storage required for the RPC call.
  686. */
  687. static int nfs_write_rpcsetup(struct nfs_page *req,
  688. struct nfs_write_data *data,
  689. const struct rpc_call_ops *call_ops,
  690. unsigned int count, unsigned int offset,
  691. int how)
  692. {
  693. struct inode *inode = req->wb_context->path.dentry->d_inode;
  694. int priority = flush_task_priority(how);
  695. struct rpc_task *task;
  696. struct rpc_message msg = {
  697. .rpc_argp = &data->args,
  698. .rpc_resp = &data->res,
  699. .rpc_cred = req->wb_context->cred,
  700. };
  701. struct rpc_task_setup task_setup_data = {
  702. .rpc_client = NFS_CLIENT(inode),
  703. .task = &data->task,
  704. .rpc_message = &msg,
  705. .callback_ops = call_ops,
  706. .callback_data = data,
  707. .workqueue = nfsiod_workqueue,
  708. .flags = RPC_TASK_ASYNC,
  709. .priority = priority,
  710. };
  711. int ret = 0;
  712. /* Set up the RPC argument and reply structs
  713. * NB: take care not to mess about with data->commit et al. */
  714. data->req = req;
  715. data->inode = inode = req->wb_context->path.dentry->d_inode;
  716. data->cred = msg.rpc_cred;
  717. data->args.fh = NFS_FH(inode);
  718. data->args.offset = req_offset(req) + offset;
  719. data->args.pgbase = req->wb_pgbase + offset;
  720. data->args.pages = data->pagevec;
  721. data->args.count = count;
  722. data->args.context = get_nfs_open_context(req->wb_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_release(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. struct nfs_client *clp = (NFS_SERVER(data->inode))->nfs_client;
  916. if (nfs4_setup_sequence(clp, &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. /* Don't commit yet if this is a non-blocking flush and there are
  1250. * lots of outstanding writes for this mapping.
  1251. */
  1252. if (wbc->sync_mode == WB_SYNC_NONE &&
  1253. nfsi->ncommit <= (nfsi->npages >> 1))
  1254. goto out_mark_dirty;
  1255. if (wbc->nonblocking || wbc->for_background)
  1256. flags = 0;
  1257. ret = nfs_commit_inode(inode, flags);
  1258. if (ret >= 0) {
  1259. if (wbc->sync_mode == WB_SYNC_NONE) {
  1260. if (ret < wbc->nr_to_write)
  1261. wbc->nr_to_write -= ret;
  1262. else
  1263. wbc->nr_to_write = 0;
  1264. }
  1265. return 0;
  1266. }
  1267. out_mark_dirty:
  1268. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  1269. return ret;
  1270. }
  1271. #else
  1272. int nfs_commit_inode(struct inode *inode, int how)
  1273. {
  1274. return 0;
  1275. }
  1276. static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
  1277. {
  1278. return 0;
  1279. }
  1280. #endif
  1281. int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  1282. {
  1283. return nfs_commit_unstable_pages(inode, wbc);
  1284. }
  1285. /*
  1286. * flush the inode to disk.
  1287. */
  1288. int nfs_wb_all(struct inode *inode)
  1289. {
  1290. struct writeback_control wbc = {
  1291. .sync_mode = WB_SYNC_ALL,
  1292. .nr_to_write = LONG_MAX,
  1293. .range_start = 0,
  1294. .range_end = LLONG_MAX,
  1295. };
  1296. return sync_inode(inode, &wbc);
  1297. }
  1298. int nfs_wb_page_cancel(struct inode *inode, struct page *page)
  1299. {
  1300. struct nfs_page *req;
  1301. int ret = 0;
  1302. BUG_ON(!PageLocked(page));
  1303. for (;;) {
  1304. wait_on_page_writeback(page);
  1305. req = nfs_page_find_request(page);
  1306. if (req == NULL)
  1307. break;
  1308. if (nfs_lock_request_dontget(req)) {
  1309. nfs_inode_remove_request(req);
  1310. /*
  1311. * In case nfs_inode_remove_request has marked the
  1312. * page as being dirty
  1313. */
  1314. cancel_dirty_page(page, PAGE_CACHE_SIZE);
  1315. nfs_unlock_request(req);
  1316. break;
  1317. }
  1318. ret = nfs_wait_on_request(req);
  1319. nfs_release_request(req);
  1320. if (ret < 0)
  1321. break;
  1322. }
  1323. return ret;
  1324. }
  1325. /*
  1326. * Write back all requests on one page - we do this before reading it.
  1327. */
  1328. int nfs_wb_page(struct inode *inode, struct page *page)
  1329. {
  1330. loff_t range_start = page_offset(page);
  1331. loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
  1332. struct writeback_control wbc = {
  1333. .sync_mode = WB_SYNC_ALL,
  1334. .nr_to_write = 0,
  1335. .range_start = range_start,
  1336. .range_end = range_end,
  1337. };
  1338. int ret;
  1339. for (;;) {
  1340. wait_on_page_writeback(page);
  1341. if (clear_page_dirty_for_io(page)) {
  1342. ret = nfs_writepage_locked(page, &wbc);
  1343. if (ret < 0)
  1344. goto out_error;
  1345. continue;
  1346. }
  1347. if (!PagePrivate(page))
  1348. break;
  1349. ret = nfs_commit_inode(inode, FLUSH_SYNC);
  1350. if (ret < 0)
  1351. goto out_error;
  1352. }
  1353. return 0;
  1354. out_error:
  1355. return ret;
  1356. }
  1357. #ifdef CONFIG_MIGRATION
  1358. int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
  1359. struct page *page)
  1360. {
  1361. struct nfs_page *req;
  1362. int ret;
  1363. nfs_fscache_release_page(page, GFP_KERNEL);
  1364. req = nfs_find_and_lock_request(page, false);
  1365. ret = PTR_ERR(req);
  1366. if (IS_ERR(req))
  1367. goto out;
  1368. ret = migrate_page(mapping, newpage, page);
  1369. if (!req)
  1370. goto out;
  1371. if (ret)
  1372. goto out_unlock;
  1373. page_cache_get(newpage);
  1374. spin_lock(&mapping->host->i_lock);
  1375. req->wb_page = newpage;
  1376. SetPagePrivate(newpage);
  1377. set_page_private(newpage, (unsigned long)req);
  1378. ClearPagePrivate(page);
  1379. set_page_private(page, 0);
  1380. spin_unlock(&mapping->host->i_lock);
  1381. page_cache_release(page);
  1382. out_unlock:
  1383. nfs_clear_page_tag_locked(req);
  1384. out:
  1385. return ret;
  1386. }
  1387. #endif
  1388. int __init nfs_init_writepagecache(void)
  1389. {
  1390. nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
  1391. sizeof(struct nfs_write_data),
  1392. 0, SLAB_HWCACHE_ALIGN,
  1393. NULL);
  1394. if (nfs_wdata_cachep == NULL)
  1395. return -ENOMEM;
  1396. nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
  1397. nfs_wdata_cachep);
  1398. if (nfs_wdata_mempool == NULL)
  1399. return -ENOMEM;
  1400. nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
  1401. nfs_wdata_cachep);
  1402. if (nfs_commit_mempool == NULL)
  1403. return -ENOMEM;
  1404. /*
  1405. * NFS congestion size, scale with available memory.
  1406. *
  1407. * 64MB: 8192k
  1408. * 128MB: 11585k
  1409. * 256MB: 16384k
  1410. * 512MB: 23170k
  1411. * 1GB: 32768k
  1412. * 2GB: 46340k
  1413. * 4GB: 65536k
  1414. * 8GB: 92681k
  1415. * 16GB: 131072k
  1416. *
  1417. * This allows larger machines to have larger/more transfers.
  1418. * Limit the default to 256M
  1419. */
  1420. nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
  1421. if (nfs_congestion_kb > 256*1024)
  1422. nfs_congestion_kb = 256*1024;
  1423. return 0;
  1424. }
  1425. void nfs_destroy_writepagecache(void)
  1426. {
  1427. mempool_destroy(nfs_commit_mempool);
  1428. mempool_destroy(nfs_wdata_mempool);
  1429. kmem_cache_destroy(nfs_wdata_cachep);
  1430. }