write.c 38 KB

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