write.c 39 KB

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