write.c 38 KB

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