write.c 39 KB

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