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