write.c 39 KB

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