write.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  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 fatal signals only.
  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_segment(req->wb_page, req->wb_bytes, PAGE_CACHE_SIZE);
  585. return req;
  586. }
  587. int nfs_flush_incompatible(struct file *file, struct page *page)
  588. {
  589. struct nfs_open_context *ctx = nfs_file_open_context(file);
  590. struct nfs_page *req;
  591. int do_flush, status;
  592. /*
  593. * Look for a request corresponding to this page. If there
  594. * is one, and it belongs to another file, we flush it out
  595. * before we try to copy anything into the page. Do this
  596. * due to the lack of an ACCESS-type call in NFSv2.
  597. * Also do the same if we find a request from an existing
  598. * dropped page.
  599. */
  600. do {
  601. req = nfs_page_find_request(page);
  602. if (req == NULL)
  603. return 0;
  604. do_flush = req->wb_page != page || req->wb_context != ctx
  605. || !nfs_dirty_request(req);
  606. nfs_release_request(req);
  607. if (!do_flush)
  608. return 0;
  609. status = nfs_wb_page(page->mapping->host, page);
  610. } while (status == 0);
  611. return status;
  612. }
  613. /*
  614. * If the page cache is marked as unsafe or invalid, then we can't rely on
  615. * the PageUptodate() flag. In this case, we will need to turn off
  616. * write optimisations that depend on the page contents being correct.
  617. */
  618. static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
  619. {
  620. return PageUptodate(page) &&
  621. !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
  622. }
  623. /*
  624. * Update and possibly write a cached page of an NFS file.
  625. *
  626. * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
  627. * things with a page scheduled for an RPC call (e.g. invalidate it).
  628. */
  629. int nfs_updatepage(struct file *file, struct page *page,
  630. unsigned int offset, unsigned int count)
  631. {
  632. struct nfs_open_context *ctx = nfs_file_open_context(file);
  633. struct inode *inode = page->mapping->host;
  634. int status = 0;
  635. nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
  636. dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
  637. file->f_path.dentry->d_parent->d_name.name,
  638. file->f_path.dentry->d_name.name, count,
  639. (long long)(page_offset(page) +offset));
  640. /* If we're not using byte range locks, and we know the page
  641. * is up to date, it may be more efficient to extend the write
  642. * to cover the entire page in order to avoid fragmentation
  643. * inefficiencies.
  644. */
  645. if (nfs_write_pageuptodate(page, inode) &&
  646. inode->i_flock == NULL &&
  647. !(file->f_mode & O_SYNC)) {
  648. count = max(count + offset, nfs_page_length(page));
  649. offset = 0;
  650. }
  651. status = nfs_writepage_setup(ctx, page, offset, count);
  652. __set_page_dirty_nobuffers(page);
  653. dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
  654. status, (long long)i_size_read(inode));
  655. if (status < 0)
  656. nfs_set_pageerror(page);
  657. return status;
  658. }
  659. static void nfs_writepage_release(struct nfs_page *req)
  660. {
  661. if (PageError(req->wb_page)) {
  662. nfs_end_page_writeback(req->wb_page);
  663. nfs_inode_remove_request(req);
  664. } else if (!nfs_reschedule_unstable_write(req)) {
  665. /* Set the PG_uptodate flag */
  666. nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
  667. nfs_end_page_writeback(req->wb_page);
  668. nfs_inode_remove_request(req);
  669. } else
  670. nfs_end_page_writeback(req->wb_page);
  671. nfs_clear_page_tag_locked(req);
  672. }
  673. static int flush_task_priority(int how)
  674. {
  675. switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
  676. case FLUSH_HIGHPRI:
  677. return RPC_PRIORITY_HIGH;
  678. case FLUSH_LOWPRI:
  679. return RPC_PRIORITY_LOW;
  680. }
  681. return RPC_PRIORITY_NORMAL;
  682. }
  683. /*
  684. * Set up the argument/result storage required for the RPC call.
  685. */
  686. static void nfs_write_rpcsetup(struct nfs_page *req,
  687. struct nfs_write_data *data,
  688. const struct rpc_call_ops *call_ops,
  689. unsigned int count, unsigned int offset,
  690. int how)
  691. {
  692. struct inode *inode = req->wb_context->path.dentry->d_inode;
  693. int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  694. int priority = flush_task_priority(how);
  695. struct rpc_task *task;
  696. struct rpc_message msg = {
  697. .rpc_argp = &data->args,
  698. .rpc_resp = &data->res,
  699. .rpc_cred = req->wb_context->cred,
  700. };
  701. struct rpc_task_setup task_setup_data = {
  702. .rpc_client = NFS_CLIENT(inode),
  703. .task = &data->task,
  704. .rpc_message = &msg,
  705. .callback_ops = call_ops,
  706. .callback_data = data,
  707. .flags = flags,
  708. .priority = priority,
  709. };
  710. /* Set up the RPC argument and reply structs
  711. * NB: take care not to mess about with data->commit et al. */
  712. data->req = req;
  713. data->inode = inode = req->wb_context->path.dentry->d_inode;
  714. data->cred = msg.rpc_cred;
  715. data->args.fh = NFS_FH(inode);
  716. data->args.offset = req_offset(req) + offset;
  717. data->args.pgbase = req->wb_pgbase + offset;
  718. data->args.pages = data->pagevec;
  719. data->args.count = count;
  720. data->args.context = req->wb_context;
  721. data->args.stable = NFS_UNSTABLE;
  722. if (how & FLUSH_STABLE) {
  723. data->args.stable = NFS_DATA_SYNC;
  724. if (!NFS_I(inode)->ncommit)
  725. data->args.stable = NFS_FILE_SYNC;
  726. }
  727. data->res.fattr = &data->fattr;
  728. data->res.count = count;
  729. data->res.verf = &data->verf;
  730. nfs_fattr_init(&data->fattr);
  731. /* Set up the initial task struct. */
  732. NFS_PROTO(inode)->write_setup(data, &msg);
  733. dprintk("NFS: %5u initiated write call "
  734. "(req %s/%Ld, %u bytes @ offset %Lu)\n",
  735. data->task.tk_pid,
  736. inode->i_sb->s_id,
  737. (long long)NFS_FILEID(inode),
  738. count,
  739. (unsigned long long)data->args.offset);
  740. task = rpc_run_task(&task_setup_data);
  741. if (!IS_ERR(task))
  742. rpc_put_task(task);
  743. }
  744. /*
  745. * Generate multiple small requests to write out a single
  746. * contiguous dirty area on one page.
  747. */
  748. static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
  749. {
  750. struct nfs_page *req = nfs_list_entry(head->next);
  751. struct page *page = req->wb_page;
  752. struct nfs_write_data *data;
  753. size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
  754. unsigned int offset;
  755. int requests = 0;
  756. LIST_HEAD(list);
  757. nfs_list_remove_request(req);
  758. nbytes = count;
  759. do {
  760. size_t len = min(nbytes, wsize);
  761. data = nfs_writedata_alloc(1);
  762. if (!data)
  763. goto out_bad;
  764. list_add(&data->pages, &list);
  765. requests++;
  766. nbytes -= len;
  767. } while (nbytes != 0);
  768. atomic_set(&req->wb_complete, requests);
  769. ClearPageError(page);
  770. offset = 0;
  771. nbytes = count;
  772. do {
  773. data = list_entry(list.next, struct nfs_write_data, pages);
  774. list_del_init(&data->pages);
  775. data->pagevec[0] = page;
  776. if (nbytes < wsize)
  777. wsize = nbytes;
  778. nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
  779. wsize, offset, how);
  780. offset += wsize;
  781. nbytes -= wsize;
  782. } while (nbytes != 0);
  783. return 0;
  784. out_bad:
  785. while (!list_empty(&list)) {
  786. data = list_entry(list.next, struct nfs_write_data, pages);
  787. list_del(&data->pages);
  788. nfs_writedata_release(data);
  789. }
  790. nfs_redirty_request(req);
  791. nfs_end_page_writeback(req->wb_page);
  792. nfs_clear_page_tag_locked(req);
  793. return -ENOMEM;
  794. }
  795. /*
  796. * Create an RPC task for the given write request and kick it.
  797. * The page must have been locked by the caller.
  798. *
  799. * It may happen that the page we're passed is not marked dirty.
  800. * This is the case if nfs_updatepage detects a conflicting request
  801. * that has been written but not committed.
  802. */
  803. static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
  804. {
  805. struct nfs_page *req;
  806. struct page **pages;
  807. struct nfs_write_data *data;
  808. data = nfs_writedata_alloc(npages);
  809. if (!data)
  810. goto out_bad;
  811. pages = data->pagevec;
  812. while (!list_empty(head)) {
  813. req = nfs_list_entry(head->next);
  814. nfs_list_remove_request(req);
  815. nfs_list_add_request(req, &data->pages);
  816. ClearPageError(req->wb_page);
  817. *pages++ = req->wb_page;
  818. }
  819. req = nfs_list_entry(data->pages.next);
  820. /* Set up the argument struct */
  821. nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
  822. return 0;
  823. out_bad:
  824. while (!list_empty(head)) {
  825. req = nfs_list_entry(head->next);
  826. nfs_list_remove_request(req);
  827. nfs_redirty_request(req);
  828. nfs_end_page_writeback(req->wb_page);
  829. nfs_clear_page_tag_locked(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. struct nfs_page *req = data->req;
  849. struct page *page = req->wb_page;
  850. dprintk("NFS: write (%s/%Ld %d@%Ld)",
  851. req->wb_context->path.dentry->d_inode->i_sb->s_id,
  852. (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
  853. req->wb_bytes,
  854. (long long)req_offset(req));
  855. if (nfs_writeback_done(task, data) != 0)
  856. return;
  857. if (task->tk_status < 0) {
  858. nfs_set_pageerror(page);
  859. nfs_context_set_write_error(req->wb_context, task->tk_status);
  860. dprintk(", error = %d\n", task->tk_status);
  861. goto out;
  862. }
  863. if (nfs_write_need_commit(data)) {
  864. struct inode *inode = page->mapping->host;
  865. spin_lock(&inode->i_lock);
  866. if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
  867. /* Do nothing we need to resend the writes */
  868. } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
  869. memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
  870. dprintk(" defer commit\n");
  871. } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
  872. set_bit(PG_NEED_RESCHED, &req->wb_flags);
  873. clear_bit(PG_NEED_COMMIT, &req->wb_flags);
  874. dprintk(" server reboot detected\n");
  875. }
  876. spin_unlock(&inode->i_lock);
  877. } else
  878. dprintk(" OK\n");
  879. out:
  880. if (atomic_dec_and_test(&req->wb_complete))
  881. nfs_writepage_release(req);
  882. }
  883. static const struct rpc_call_ops nfs_write_partial_ops = {
  884. .rpc_call_done = nfs_writeback_done_partial,
  885. .rpc_release = nfs_writedata_release,
  886. };
  887. /*
  888. * Handle a write reply that flushes a whole page.
  889. *
  890. * FIXME: There is an inherent race with invalidate_inode_pages and
  891. * writebacks since the page->count is kept > 1 for as long
  892. * as the page has a write request pending.
  893. */
  894. static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
  895. {
  896. struct nfs_write_data *data = calldata;
  897. struct nfs_page *req;
  898. struct page *page;
  899. if (nfs_writeback_done(task, data) != 0)
  900. return;
  901. /* Update attributes as result of writeback. */
  902. while (!list_empty(&data->pages)) {
  903. req = nfs_list_entry(data->pages.next);
  904. nfs_list_remove_request(req);
  905. page = req->wb_page;
  906. dprintk("NFS: write (%s/%Ld %d@%Ld)",
  907. req->wb_context->path.dentry->d_inode->i_sb->s_id,
  908. (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
  909. req->wb_bytes,
  910. (long long)req_offset(req));
  911. if (task->tk_status < 0) {
  912. nfs_set_pageerror(page);
  913. nfs_context_set_write_error(req->wb_context, task->tk_status);
  914. dprintk(", error = %d\n", task->tk_status);
  915. goto remove_request;
  916. }
  917. if (nfs_write_need_commit(data)) {
  918. memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
  919. nfs_mark_request_commit(req);
  920. nfs_end_page_writeback(page);
  921. dprintk(" marked for commit\n");
  922. goto next;
  923. }
  924. /* Set the PG_uptodate flag? */
  925. nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
  926. dprintk(" OK\n");
  927. remove_request:
  928. nfs_end_page_writeback(page);
  929. nfs_inode_remove_request(req);
  930. next:
  931. nfs_clear_page_tag_locked(req);
  932. }
  933. }
  934. static const struct rpc_call_ops nfs_write_full_ops = {
  935. .rpc_call_done = nfs_writeback_done_full,
  936. .rpc_release = nfs_writedata_release,
  937. };
  938. /*
  939. * This function is called when the WRITE call is complete.
  940. */
  941. int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
  942. {
  943. struct nfs_writeargs *argp = &data->args;
  944. struct nfs_writeres *resp = &data->res;
  945. int status;
  946. dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
  947. task->tk_pid, task->tk_status);
  948. /*
  949. * ->write_done will attempt to use post-op attributes to detect
  950. * conflicting writes by other clients. A strict interpretation
  951. * of close-to-open would allow us to continue caching even if
  952. * another writer had changed the file, but some applications
  953. * depend on tighter cache coherency when writing.
  954. */
  955. status = NFS_PROTO(data->inode)->write_done(task, data);
  956. if (status != 0)
  957. return status;
  958. nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
  959. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  960. if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
  961. /* We tried a write call, but the server did not
  962. * commit data to stable storage even though we
  963. * requested it.
  964. * Note: There is a known bug in Tru64 < 5.0 in which
  965. * the server reports NFS_DATA_SYNC, but performs
  966. * NFS_FILE_SYNC. We therefore implement this checking
  967. * as a dprintk() in order to avoid filling syslog.
  968. */
  969. static unsigned long complain;
  970. if (time_before(complain, jiffies)) {
  971. dprintk("NFS: faulty NFS server %s:"
  972. " (committed = %d) != (stable = %d)\n",
  973. NFS_SERVER(data->inode)->nfs_client->cl_hostname,
  974. resp->verf->committed, argp->stable);
  975. complain = jiffies + 300 * HZ;
  976. }
  977. }
  978. #endif
  979. /* Is this a short write? */
  980. if (task->tk_status >= 0 && resp->count < argp->count) {
  981. static unsigned long complain;
  982. nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
  983. /* Has the server at least made some progress? */
  984. if (resp->count != 0) {
  985. /* Was this an NFSv2 write or an NFSv3 stable write? */
  986. if (resp->verf->committed != NFS_UNSTABLE) {
  987. /* Resend from where the server left off */
  988. argp->offset += resp->count;
  989. argp->pgbase += resp->count;
  990. argp->count -= resp->count;
  991. } else {
  992. /* Resend as a stable write in order to avoid
  993. * headaches in the case of a server crash.
  994. */
  995. argp->stable = NFS_FILE_SYNC;
  996. }
  997. rpc_restart_call(task);
  998. return -EAGAIN;
  999. }
  1000. if (time_before(complain, jiffies)) {
  1001. printk(KERN_WARNING
  1002. "NFS: Server wrote zero bytes, expected %u.\n",
  1003. argp->count);
  1004. complain = jiffies + 300 * HZ;
  1005. }
  1006. /* Can't do anything about it except throw an error. */
  1007. task->tk_status = -EIO;
  1008. }
  1009. return 0;
  1010. }
  1011. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1012. void nfs_commit_release(void *wdata)
  1013. {
  1014. nfs_commit_free(wdata);
  1015. }
  1016. /*
  1017. * Set up the argument/result storage required for the RPC call.
  1018. */
  1019. static void nfs_commit_rpcsetup(struct list_head *head,
  1020. struct nfs_write_data *data,
  1021. int how)
  1022. {
  1023. struct nfs_page *first = nfs_list_entry(head->next);
  1024. struct inode *inode = first->wb_context->path.dentry->d_inode;
  1025. int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  1026. int priority = flush_task_priority(how);
  1027. struct rpc_task *task;
  1028. struct rpc_message msg = {
  1029. .rpc_argp = &data->args,
  1030. .rpc_resp = &data->res,
  1031. .rpc_cred = first->wb_context->cred,
  1032. };
  1033. struct rpc_task_setup task_setup_data = {
  1034. .task = &data->task,
  1035. .rpc_client = NFS_CLIENT(inode),
  1036. .rpc_message = &msg,
  1037. .callback_ops = &nfs_commit_ops,
  1038. .callback_data = data,
  1039. .flags = flags,
  1040. .priority = priority,
  1041. };
  1042. /* Set up the RPC argument and reply structs
  1043. * NB: take care not to mess about with data->commit et al. */
  1044. list_splice_init(head, &data->pages);
  1045. data->inode = inode;
  1046. data->cred = msg.rpc_cred;
  1047. data->args.fh = NFS_FH(data->inode);
  1048. /* Note: we always request a commit of the entire inode */
  1049. data->args.offset = 0;
  1050. data->args.count = 0;
  1051. data->res.count = 0;
  1052. data->res.fattr = &data->fattr;
  1053. data->res.verf = &data->verf;
  1054. nfs_fattr_init(&data->fattr);
  1055. /* Set up the initial task struct. */
  1056. NFS_PROTO(inode)->commit_setup(data, &msg);
  1057. dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
  1058. task = rpc_run_task(&task_setup_data);
  1059. if (!IS_ERR(task))
  1060. rpc_put_task(task);
  1061. }
  1062. /*
  1063. * Commit dirty pages
  1064. */
  1065. static int
  1066. nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1067. {
  1068. struct nfs_write_data *data;
  1069. struct nfs_page *req;
  1070. data = nfs_commit_alloc();
  1071. if (!data)
  1072. goto out_bad;
  1073. /* Set up the argument struct */
  1074. nfs_commit_rpcsetup(head, data, how);
  1075. return 0;
  1076. out_bad:
  1077. while (!list_empty(head)) {
  1078. req = nfs_list_entry(head->next);
  1079. nfs_list_remove_request(req);
  1080. nfs_mark_request_commit(req);
  1081. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  1082. dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
  1083. BDI_RECLAIMABLE);
  1084. nfs_clear_page_tag_locked(req);
  1085. }
  1086. return -ENOMEM;
  1087. }
  1088. /*
  1089. * COMMIT call returned
  1090. */
  1091. static void nfs_commit_done(struct rpc_task *task, void *calldata)
  1092. {
  1093. struct nfs_write_data *data = calldata;
  1094. struct nfs_page *req;
  1095. dprintk("NFS: %5u nfs_commit_done (status %d)\n",
  1096. task->tk_pid, task->tk_status);
  1097. /* Call the NFS version-specific code */
  1098. if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
  1099. return;
  1100. while (!list_empty(&data->pages)) {
  1101. req = nfs_list_entry(data->pages.next);
  1102. nfs_list_remove_request(req);
  1103. clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
  1104. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  1105. dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
  1106. BDI_RECLAIMABLE);
  1107. dprintk("NFS: commit (%s/%Ld %d@%Ld)",
  1108. req->wb_context->path.dentry->d_inode->i_sb->s_id,
  1109. (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
  1110. req->wb_bytes,
  1111. (long long)req_offset(req));
  1112. if (task->tk_status < 0) {
  1113. nfs_context_set_write_error(req->wb_context, task->tk_status);
  1114. nfs_inode_remove_request(req);
  1115. dprintk(", error = %d\n", task->tk_status);
  1116. goto next;
  1117. }
  1118. /* Okay, COMMIT succeeded, apparently. Check the verifier
  1119. * returned by the server against all stored verfs. */
  1120. if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
  1121. /* We have a match */
  1122. /* Set the PG_uptodate flag */
  1123. nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
  1124. req->wb_bytes);
  1125. nfs_inode_remove_request(req);
  1126. dprintk(" OK\n");
  1127. goto next;
  1128. }
  1129. /* We have a mismatch. Write the page again */
  1130. dprintk(" mismatch\n");
  1131. nfs_redirty_request(req);
  1132. next:
  1133. nfs_clear_page_tag_locked(req);
  1134. }
  1135. }
  1136. static const struct rpc_call_ops nfs_commit_ops = {
  1137. .rpc_call_done = nfs_commit_done,
  1138. .rpc_release = nfs_commit_release,
  1139. };
  1140. int nfs_commit_inode(struct inode *inode, int how)
  1141. {
  1142. LIST_HEAD(head);
  1143. int res;
  1144. spin_lock(&inode->i_lock);
  1145. res = nfs_scan_commit(inode, &head, 0, 0);
  1146. spin_unlock(&inode->i_lock);
  1147. if (res) {
  1148. int error = nfs_commit_list(inode, &head, how);
  1149. if (error < 0)
  1150. return error;
  1151. }
  1152. return res;
  1153. }
  1154. #else
  1155. static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1156. {
  1157. return 0;
  1158. }
  1159. #endif
  1160. long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
  1161. {
  1162. struct inode *inode = mapping->host;
  1163. pgoff_t idx_start, idx_end;
  1164. unsigned int npages = 0;
  1165. LIST_HEAD(head);
  1166. int nocommit = how & FLUSH_NOCOMMIT;
  1167. long pages, ret;
  1168. /* FIXME */
  1169. if (wbc->range_cyclic)
  1170. idx_start = 0;
  1171. else {
  1172. idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
  1173. idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
  1174. if (idx_end > idx_start) {
  1175. pgoff_t l_npages = 1 + idx_end - idx_start;
  1176. npages = l_npages;
  1177. if (sizeof(npages) != sizeof(l_npages) &&
  1178. (pgoff_t)npages != l_npages)
  1179. npages = 0;
  1180. }
  1181. }
  1182. how &= ~FLUSH_NOCOMMIT;
  1183. spin_lock(&inode->i_lock);
  1184. do {
  1185. ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
  1186. if (ret != 0)
  1187. continue;
  1188. if (nocommit)
  1189. break;
  1190. pages = nfs_scan_commit(inode, &head, idx_start, npages);
  1191. if (pages == 0)
  1192. break;
  1193. if (how & FLUSH_INVALIDATE) {
  1194. spin_unlock(&inode->i_lock);
  1195. nfs_cancel_commit_list(&head);
  1196. ret = pages;
  1197. spin_lock(&inode->i_lock);
  1198. continue;
  1199. }
  1200. pages += nfs_scan_commit(inode, &head, 0, 0);
  1201. spin_unlock(&inode->i_lock);
  1202. ret = nfs_commit_list(inode, &head, how);
  1203. spin_lock(&inode->i_lock);
  1204. } while (ret >= 0);
  1205. spin_unlock(&inode->i_lock);
  1206. return ret;
  1207. }
  1208. static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
  1209. {
  1210. int ret;
  1211. ret = nfs_writepages(mapping, wbc);
  1212. if (ret < 0)
  1213. goto out;
  1214. ret = nfs_sync_mapping_wait(mapping, wbc, how);
  1215. if (ret < 0)
  1216. goto out;
  1217. return 0;
  1218. out:
  1219. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  1220. return ret;
  1221. }
  1222. /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
  1223. static int nfs_write_mapping(struct address_space *mapping, int how)
  1224. {
  1225. struct writeback_control wbc = {
  1226. .bdi = mapping->backing_dev_info,
  1227. .sync_mode = WB_SYNC_NONE,
  1228. .nr_to_write = LONG_MAX,
  1229. .for_writepages = 1,
  1230. .range_cyclic = 1,
  1231. };
  1232. int ret;
  1233. ret = __nfs_write_mapping(mapping, &wbc, how);
  1234. if (ret < 0)
  1235. return ret;
  1236. wbc.sync_mode = WB_SYNC_ALL;
  1237. return __nfs_write_mapping(mapping, &wbc, how);
  1238. }
  1239. /*
  1240. * flush the inode to disk.
  1241. */
  1242. int nfs_wb_all(struct inode *inode)
  1243. {
  1244. return nfs_write_mapping(inode->i_mapping, 0);
  1245. }
  1246. int nfs_wb_nocommit(struct inode *inode)
  1247. {
  1248. return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
  1249. }
  1250. int nfs_wb_page_cancel(struct inode *inode, struct page *page)
  1251. {
  1252. struct nfs_page *req;
  1253. loff_t range_start = page_offset(page);
  1254. loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
  1255. struct writeback_control wbc = {
  1256. .bdi = page->mapping->backing_dev_info,
  1257. .sync_mode = WB_SYNC_ALL,
  1258. .nr_to_write = LONG_MAX,
  1259. .range_start = range_start,
  1260. .range_end = range_end,
  1261. };
  1262. int ret = 0;
  1263. BUG_ON(!PageLocked(page));
  1264. for (;;) {
  1265. req = nfs_page_find_request(page);
  1266. if (req == NULL)
  1267. goto out;
  1268. if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
  1269. nfs_release_request(req);
  1270. break;
  1271. }
  1272. if (nfs_lock_request_dontget(req)) {
  1273. nfs_inode_remove_request(req);
  1274. /*
  1275. * In case nfs_inode_remove_request has marked the
  1276. * page as being dirty
  1277. */
  1278. cancel_dirty_page(page, PAGE_CACHE_SIZE);
  1279. nfs_unlock_request(req);
  1280. break;
  1281. }
  1282. ret = nfs_wait_on_request(req);
  1283. if (ret < 0)
  1284. goto out;
  1285. }
  1286. if (!PagePrivate(page))
  1287. return 0;
  1288. ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
  1289. out:
  1290. return ret;
  1291. }
  1292. static int nfs_wb_page_priority(struct inode *inode, struct page *page,
  1293. int how)
  1294. {
  1295. loff_t range_start = page_offset(page);
  1296. loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
  1297. struct writeback_control wbc = {
  1298. .bdi = page->mapping->backing_dev_info,
  1299. .sync_mode = WB_SYNC_ALL,
  1300. .nr_to_write = LONG_MAX,
  1301. .range_start = range_start,
  1302. .range_end = range_end,
  1303. };
  1304. int ret;
  1305. BUG_ON(!PageLocked(page));
  1306. if (clear_page_dirty_for_io(page)) {
  1307. ret = nfs_writepage_locked(page, &wbc);
  1308. if (ret < 0)
  1309. goto out;
  1310. }
  1311. if (!PagePrivate(page))
  1312. return 0;
  1313. ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
  1314. if (ret >= 0)
  1315. return 0;
  1316. out:
  1317. __mark_inode_dirty(inode, I_DIRTY_PAGES);
  1318. return ret;
  1319. }
  1320. /*
  1321. * Write back all requests on one page - we do this before reading it.
  1322. */
  1323. int nfs_wb_page(struct inode *inode, struct page* page)
  1324. {
  1325. return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
  1326. }
  1327. int __init nfs_init_writepagecache(void)
  1328. {
  1329. nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
  1330. sizeof(struct nfs_write_data),
  1331. 0, SLAB_HWCACHE_ALIGN,
  1332. NULL);
  1333. if (nfs_wdata_cachep == NULL)
  1334. return -ENOMEM;
  1335. nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
  1336. nfs_wdata_cachep);
  1337. if (nfs_wdata_mempool == NULL)
  1338. return -ENOMEM;
  1339. nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
  1340. nfs_wdata_cachep);
  1341. if (nfs_commit_mempool == NULL)
  1342. return -ENOMEM;
  1343. /*
  1344. * NFS congestion size, scale with available memory.
  1345. *
  1346. * 64MB: 8192k
  1347. * 128MB: 11585k
  1348. * 256MB: 16384k
  1349. * 512MB: 23170k
  1350. * 1GB: 32768k
  1351. * 2GB: 46340k
  1352. * 4GB: 65536k
  1353. * 8GB: 92681k
  1354. * 16GB: 131072k
  1355. *
  1356. * This allows larger machines to have larger/more transfers.
  1357. * Limit the default to 256M
  1358. */
  1359. nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
  1360. if (nfs_congestion_kb > 256*1024)
  1361. nfs_congestion_kb = 256*1024;
  1362. return 0;
  1363. }
  1364. void nfs_destroy_writepagecache(void)
  1365. {
  1366. mempool_destroy(nfs_commit_mempool);
  1367. mempool_destroy(nfs_wdata_mempool);
  1368. kmem_cache_destroy(nfs_wdata_cachep);
  1369. }