write.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  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/sunrpc/clnt.h>
  15. #include <linux/nfs_fs.h>
  16. #include <linux/nfs_mount.h>
  17. #include <linux/nfs_page.h>
  18. #include <linux/backing-dev.h>
  19. #include <asm/uaccess.h>
  20. #include <linux/smp_lock.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_mark_request_dirty(struct nfs_page *req);
  34. static int nfs_wait_on_write_congestion(struct address_space *, int);
  35. static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
  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. static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
  43. struct nfs_write_data *nfs_commit_alloc(void)
  44. {
  45. struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
  46. if (p) {
  47. memset(p, 0, sizeof(*p));
  48. INIT_LIST_HEAD(&p->pages);
  49. }
  50. return p;
  51. }
  52. void nfs_commit_rcu_free(struct rcu_head *head)
  53. {
  54. struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
  55. if (p && (p->pagevec != &p->page_array[0]))
  56. kfree(p->pagevec);
  57. mempool_free(p, nfs_commit_mempool);
  58. }
  59. void nfs_commit_free(struct nfs_write_data *wdata)
  60. {
  61. call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
  62. }
  63. struct nfs_write_data *nfs_writedata_alloc(size_t len)
  64. {
  65. unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  66. struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
  67. if (p) {
  68. memset(p, 0, sizeof(*p));
  69. INIT_LIST_HEAD(&p->pages);
  70. p->npages = pagecount;
  71. if (pagecount <= ARRAY_SIZE(p->page_array))
  72. p->pagevec = p->page_array;
  73. else {
  74. p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
  75. if (!p->pagevec) {
  76. mempool_free(p, nfs_wdata_mempool);
  77. p = NULL;
  78. }
  79. }
  80. }
  81. return p;
  82. }
  83. static void nfs_writedata_rcu_free(struct rcu_head *head)
  84. {
  85. struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
  86. if (p && (p->pagevec != &p->page_array[0]))
  87. kfree(p->pagevec);
  88. mempool_free(p, nfs_wdata_mempool);
  89. }
  90. static void nfs_writedata_free(struct nfs_write_data *wdata)
  91. {
  92. call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
  93. }
  94. void nfs_writedata_release(void *wdata)
  95. {
  96. nfs_writedata_free(wdata);
  97. }
  98. static struct nfs_page *nfs_page_find_request_locked(struct page *page)
  99. {
  100. struct nfs_page *req = NULL;
  101. if (PagePrivate(page)) {
  102. req = (struct nfs_page *)page_private(page);
  103. if (req != NULL)
  104. atomic_inc(&req->wb_count);
  105. }
  106. return req;
  107. }
  108. static struct nfs_page *nfs_page_find_request(struct page *page)
  109. {
  110. struct nfs_page *req = NULL;
  111. spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
  112. spin_lock(req_lock);
  113. req = nfs_page_find_request_locked(page);
  114. spin_unlock(req_lock);
  115. return req;
  116. }
  117. /* Adjust the file length if we're writing beyond the end */
  118. static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
  119. {
  120. struct inode *inode = page->mapping->host;
  121. loff_t end, i_size = i_size_read(inode);
  122. unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
  123. if (i_size > 0 && page->index < end_index)
  124. return;
  125. end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
  126. if (i_size >= end)
  127. return;
  128. nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
  129. i_size_write(inode, end);
  130. }
  131. /* A writeback failed: mark the page as bad, and invalidate the page cache */
  132. static void nfs_set_pageerror(struct page *page)
  133. {
  134. SetPageError(page);
  135. nfs_zap_mapping(page->mapping->host, page->mapping);
  136. }
  137. /* We can set the PG_uptodate flag if we see that a write request
  138. * covers the full page.
  139. */
  140. static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
  141. {
  142. if (PageUptodate(page))
  143. return;
  144. if (base != 0)
  145. return;
  146. if (count != nfs_page_length(page))
  147. return;
  148. if (count != PAGE_CACHE_SIZE)
  149. memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
  150. SetPageUptodate(page);
  151. }
  152. static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
  153. unsigned int offset, unsigned int count)
  154. {
  155. struct nfs_page *req;
  156. int ret;
  157. for (;;) {
  158. req = nfs_update_request(ctx, page, offset, count);
  159. if (!IS_ERR(req))
  160. break;
  161. ret = PTR_ERR(req);
  162. if (ret != -EBUSY)
  163. return ret;
  164. ret = nfs_wb_page(page->mapping->host, page);
  165. if (ret != 0)
  166. return ret;
  167. }
  168. /* Update file length */
  169. nfs_grow_file(page, offset, count);
  170. /* Set the PG_uptodate flag? */
  171. nfs_mark_uptodate(page, offset, count);
  172. nfs_unlock_request(req);
  173. return 0;
  174. }
  175. static int wb_priority(struct writeback_control *wbc)
  176. {
  177. if (wbc->for_reclaim)
  178. return FLUSH_HIGHPRI;
  179. if (wbc->for_kupdate)
  180. return FLUSH_LOWPRI;
  181. return 0;
  182. }
  183. /*
  184. * Find an associated nfs write request, and prepare to flush it out
  185. * Returns 1 if there was no write request, or if the request was
  186. * already tagged by nfs_set_page_dirty.Returns 0 if the request
  187. * was not tagged.
  188. * May also return an error if the user signalled nfs_wait_on_request().
  189. */
  190. static int nfs_page_mark_flush(struct page *page)
  191. {
  192. struct nfs_page *req;
  193. spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
  194. int ret;
  195. spin_lock(req_lock);
  196. for(;;) {
  197. req = nfs_page_find_request_locked(page);
  198. if (req == NULL) {
  199. spin_unlock(req_lock);
  200. return 1;
  201. }
  202. if (nfs_lock_request_dontget(req))
  203. break;
  204. /* Note: If we hold the page lock, as is the case in nfs_writepage,
  205. * then the call to nfs_lock_request_dontget() will always
  206. * succeed provided that someone hasn't already marked the
  207. * request as dirty (in which case we don't care).
  208. */
  209. spin_unlock(req_lock);
  210. ret = nfs_wait_on_request(req);
  211. nfs_release_request(req);
  212. if (ret != 0)
  213. return ret;
  214. spin_lock(req_lock);
  215. }
  216. spin_unlock(req_lock);
  217. if (test_and_set_bit(PG_FLUSHING, &req->wb_flags) == 0) {
  218. nfs_mark_request_dirty(req);
  219. set_page_writeback(page);
  220. }
  221. ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
  222. nfs_unlock_request(req);
  223. return ret;
  224. }
  225. /*
  226. * Write an mmapped page to the server.
  227. */
  228. static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
  229. {
  230. struct nfs_open_context *ctx;
  231. struct inode *inode = page->mapping->host;
  232. unsigned offset;
  233. int err;
  234. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
  235. nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
  236. err = nfs_page_mark_flush(page);
  237. if (err <= 0)
  238. goto out;
  239. err = 0;
  240. offset = nfs_page_length(page);
  241. if (!offset)
  242. goto out;
  243. ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
  244. if (ctx == NULL) {
  245. err = -EBADF;
  246. goto out;
  247. }
  248. err = nfs_writepage_setup(ctx, page, 0, offset);
  249. put_nfs_open_context(ctx);
  250. if (err != 0)
  251. goto out;
  252. err = nfs_page_mark_flush(page);
  253. if (err > 0)
  254. err = 0;
  255. out:
  256. if (!wbc->for_writepages)
  257. nfs_flush_mapping(page->mapping, wbc, FLUSH_STABLE|wb_priority(wbc));
  258. return err;
  259. }
  260. int nfs_writepage(struct page *page, struct writeback_control *wbc)
  261. {
  262. int err;
  263. err = nfs_writepage_locked(page, wbc);
  264. unlock_page(page);
  265. return err;
  266. }
  267. /*
  268. * Note: causes nfs_update_request() to block on the assumption
  269. * that the writeback is generated due to memory pressure.
  270. */
  271. int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  272. {
  273. struct backing_dev_info *bdi = mapping->backing_dev_info;
  274. struct inode *inode = mapping->host;
  275. int err;
  276. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
  277. err = generic_writepages(mapping, wbc);
  278. if (err)
  279. return err;
  280. while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
  281. if (wbc->nonblocking)
  282. return 0;
  283. nfs_wait_on_write_congestion(mapping, 0);
  284. }
  285. err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
  286. if (err < 0)
  287. goto out;
  288. nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
  289. err = 0;
  290. out:
  291. clear_bit(BDI_write_congested, &bdi->state);
  292. wake_up_all(&nfs_write_congestion);
  293. congestion_end(WRITE);
  294. return err;
  295. }
  296. /*
  297. * Insert a write request into an inode
  298. */
  299. static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
  300. {
  301. struct nfs_inode *nfsi = NFS_I(inode);
  302. int error;
  303. error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
  304. BUG_ON(error == -EEXIST);
  305. if (error)
  306. return error;
  307. if (!nfsi->npages) {
  308. igrab(inode);
  309. nfs_begin_data_update(inode);
  310. if (nfs_have_delegation(inode, FMODE_WRITE))
  311. nfsi->change_attr++;
  312. }
  313. SetPagePrivate(req->wb_page);
  314. set_page_private(req->wb_page, (unsigned long)req);
  315. nfsi->npages++;
  316. atomic_inc(&req->wb_count);
  317. return 0;
  318. }
  319. /*
  320. * Insert a write request into an inode
  321. */
  322. static void nfs_inode_remove_request(struct nfs_page *req)
  323. {
  324. struct inode *inode = req->wb_context->dentry->d_inode;
  325. struct nfs_inode *nfsi = NFS_I(inode);
  326. BUG_ON (!NFS_WBACK_BUSY(req));
  327. spin_lock(&nfsi->req_lock);
  328. set_page_private(req->wb_page, 0);
  329. ClearPagePrivate(req->wb_page);
  330. radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
  331. nfsi->npages--;
  332. if (!nfsi->npages) {
  333. spin_unlock(&nfsi->req_lock);
  334. nfs_end_data_update(inode);
  335. iput(inode);
  336. } else
  337. spin_unlock(&nfsi->req_lock);
  338. nfs_clear_request(req);
  339. nfs_release_request(req);
  340. }
  341. /*
  342. * Add a request to the inode's dirty list.
  343. */
  344. static void
  345. nfs_mark_request_dirty(struct nfs_page *req)
  346. {
  347. struct inode *inode = req->wb_context->dentry->d_inode;
  348. struct nfs_inode *nfsi = NFS_I(inode);
  349. spin_lock(&nfsi->req_lock);
  350. radix_tree_tag_set(&nfsi->nfs_page_tree,
  351. req->wb_index, NFS_PAGE_TAG_DIRTY);
  352. nfs_list_add_request(req, &nfsi->dirty);
  353. nfsi->ndirty++;
  354. spin_unlock(&nfsi->req_lock);
  355. __mark_inode_dirty(inode, I_DIRTY_PAGES);
  356. }
  357. static void
  358. nfs_redirty_request(struct nfs_page *req)
  359. {
  360. clear_bit(PG_FLUSHING, &req->wb_flags);
  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. return test_bit(PG_FLUSHING, &req->wb_flags) == 0;
  370. }
  371. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  372. /*
  373. * Add a request to the inode's commit list.
  374. */
  375. static void
  376. nfs_mark_request_commit(struct nfs_page *req)
  377. {
  378. struct inode *inode = req->wb_context->dentry->d_inode;
  379. struct nfs_inode *nfsi = NFS_I(inode);
  380. spin_lock(&nfsi->req_lock);
  381. nfs_list_add_request(req, &nfsi->commit);
  382. nfsi->ncommit++;
  383. spin_unlock(&nfsi->req_lock);
  384. inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  385. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  386. }
  387. #endif
  388. /*
  389. * Wait for a request to complete.
  390. *
  391. * Interruptible by signals only if mounted with intr flag.
  392. */
  393. static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
  394. {
  395. struct nfs_inode *nfsi = NFS_I(inode);
  396. struct nfs_page *req;
  397. unsigned long idx_end, next;
  398. unsigned int res = 0;
  399. int error;
  400. if (npages == 0)
  401. idx_end = ~0;
  402. else
  403. idx_end = idx_start + npages - 1;
  404. next = idx_start;
  405. while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
  406. if (req->wb_index > idx_end)
  407. break;
  408. next = req->wb_index + 1;
  409. BUG_ON(!NFS_WBACK_BUSY(req));
  410. atomic_inc(&req->wb_count);
  411. spin_unlock(&nfsi->req_lock);
  412. error = nfs_wait_on_request(req);
  413. nfs_release_request(req);
  414. spin_lock(&nfsi->req_lock);
  415. if (error < 0)
  416. return error;
  417. res++;
  418. }
  419. return res;
  420. }
  421. static void nfs_cancel_dirty_list(struct list_head *head)
  422. {
  423. struct nfs_page *req;
  424. while(!list_empty(head)) {
  425. req = nfs_list_entry(head->next);
  426. nfs_list_remove_request(req);
  427. nfs_inode_remove_request(req);
  428. nfs_clear_page_writeback(req);
  429. }
  430. }
  431. static void nfs_cancel_commit_list(struct list_head *head)
  432. {
  433. struct nfs_page *req;
  434. while(!list_empty(head)) {
  435. req = nfs_list_entry(head->next);
  436. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  437. nfs_list_remove_request(req);
  438. nfs_inode_remove_request(req);
  439. nfs_unlock_request(req);
  440. }
  441. }
  442. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  443. /*
  444. * nfs_scan_commit - Scan an inode for commit requests
  445. * @inode: NFS inode to scan
  446. * @dst: destination list
  447. * @idx_start: lower bound of page->index to scan.
  448. * @npages: idx_start + npages sets the upper bound to scan.
  449. *
  450. * Moves requests from the inode's 'commit' request list.
  451. * The requests are *not* checked to ensure that they form a contiguous set.
  452. */
  453. static int
  454. nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
  455. {
  456. struct nfs_inode *nfsi = NFS_I(inode);
  457. int res = 0;
  458. if (nfsi->ncommit != 0) {
  459. res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
  460. nfsi->ncommit -= res;
  461. if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
  462. printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
  463. }
  464. return res;
  465. }
  466. #else
  467. static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
  468. {
  469. return 0;
  470. }
  471. #endif
  472. static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
  473. {
  474. struct backing_dev_info *bdi = mapping->backing_dev_info;
  475. DEFINE_WAIT(wait);
  476. int ret = 0;
  477. might_sleep();
  478. if (!bdi_write_congested(bdi))
  479. return 0;
  480. nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
  481. if (intr) {
  482. struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
  483. sigset_t oldset;
  484. rpc_clnt_sigmask(clnt, &oldset);
  485. prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
  486. if (bdi_write_congested(bdi)) {
  487. if (signalled())
  488. ret = -ERESTARTSYS;
  489. else
  490. schedule();
  491. }
  492. rpc_clnt_sigunmask(clnt, &oldset);
  493. } else {
  494. prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
  495. if (bdi_write_congested(bdi))
  496. schedule();
  497. }
  498. finish_wait(&nfs_write_congestion, &wait);
  499. return ret;
  500. }
  501. /*
  502. * Try to update any existing write request, or create one if there is none.
  503. * In order to match, the request's credentials must match those of
  504. * the calling process.
  505. *
  506. * Note: Should always be called with the Page Lock held!
  507. */
  508. static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
  509. struct page *page, unsigned int offset, unsigned int bytes)
  510. {
  511. struct inode *inode = page->mapping->host;
  512. struct nfs_inode *nfsi = NFS_I(inode);
  513. struct nfs_page *req, *new = NULL;
  514. unsigned long rqend, end;
  515. end = offset + bytes;
  516. if (nfs_wait_on_write_congestion(page->mapping, NFS_SERVER(inode)->flags & NFS_MOUNT_INTR))
  517. return ERR_PTR(-ERESTARTSYS);
  518. for (;;) {
  519. /* Loop over all inode entries and see if we find
  520. * A request for the page we wish to update
  521. */
  522. spin_lock(&nfsi->req_lock);
  523. req = nfs_page_find_request_locked(page);
  524. if (req) {
  525. if (!nfs_lock_request_dontget(req)) {
  526. int error;
  527. spin_unlock(&nfsi->req_lock);
  528. error = nfs_wait_on_request(req);
  529. nfs_release_request(req);
  530. if (error < 0) {
  531. if (new)
  532. nfs_release_request(new);
  533. return ERR_PTR(error);
  534. }
  535. continue;
  536. }
  537. spin_unlock(&nfsi->req_lock);
  538. if (new)
  539. nfs_release_request(new);
  540. break;
  541. }
  542. if (new) {
  543. int error;
  544. nfs_lock_request_dontget(new);
  545. error = nfs_inode_add_request(inode, new);
  546. if (error) {
  547. spin_unlock(&nfsi->req_lock);
  548. nfs_unlock_request(new);
  549. return ERR_PTR(error);
  550. }
  551. spin_unlock(&nfsi->req_lock);
  552. return new;
  553. }
  554. spin_unlock(&nfsi->req_lock);
  555. new = nfs_create_request(ctx, inode, page, offset, bytes);
  556. if (IS_ERR(new))
  557. return new;
  558. }
  559. /* We have a request for our page.
  560. * If the creds don't match, or the
  561. * page addresses don't match,
  562. * tell the caller to wait on the conflicting
  563. * request.
  564. */
  565. rqend = req->wb_offset + req->wb_bytes;
  566. if (req->wb_context != ctx
  567. || req->wb_page != page
  568. || !nfs_dirty_request(req)
  569. || offset > rqend || end < req->wb_offset) {
  570. nfs_unlock_request(req);
  571. return ERR_PTR(-EBUSY);
  572. }
  573. /* Okay, the request matches. Update the region */
  574. if (offset < req->wb_offset) {
  575. req->wb_offset = offset;
  576. req->wb_pgbase = offset;
  577. req->wb_bytes = rqend - req->wb_offset;
  578. }
  579. if (end > rqend)
  580. req->wb_bytes = end - req->wb_offset;
  581. return req;
  582. }
  583. int nfs_flush_incompatible(struct file *file, struct page *page)
  584. {
  585. struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
  586. struct nfs_page *req;
  587. int do_flush, status;
  588. /*
  589. * Look for a request corresponding to this page. If there
  590. * is one, and it belongs to another file, we flush it out
  591. * before we try to copy anything into the page. Do this
  592. * due to the lack of an ACCESS-type call in NFSv2.
  593. * Also do the same if we find a request from an existing
  594. * dropped page.
  595. */
  596. do {
  597. req = nfs_page_find_request(page);
  598. if (req == NULL)
  599. return 0;
  600. do_flush = req->wb_page != page || req->wb_context != ctx
  601. || !nfs_dirty_request(req);
  602. nfs_release_request(req);
  603. if (!do_flush)
  604. return 0;
  605. status = nfs_wb_page(page->mapping->host, page);
  606. } while (status == 0);
  607. return status;
  608. }
  609. /*
  610. * Update and possibly write a cached page of an NFS file.
  611. *
  612. * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
  613. * things with a page scheduled for an RPC call (e.g. invalidate it).
  614. */
  615. int nfs_updatepage(struct file *file, struct page *page,
  616. unsigned int offset, unsigned int count)
  617. {
  618. struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
  619. struct inode *inode = page->mapping->host;
  620. int status = 0;
  621. nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
  622. dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
  623. file->f_path.dentry->d_parent->d_name.name,
  624. file->f_path.dentry->d_name.name, count,
  625. (long long)(page_offset(page) +offset));
  626. /* If we're not using byte range locks, and we know the page
  627. * is entirely in cache, it may be more efficient to avoid
  628. * fragmenting write requests.
  629. */
  630. if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
  631. count = max(count + offset, nfs_page_length(page));
  632. offset = 0;
  633. }
  634. status = nfs_writepage_setup(ctx, page, offset, count);
  635. __set_page_dirty_nobuffers(page);
  636. dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
  637. status, (long long)i_size_read(inode));
  638. if (status < 0)
  639. nfs_set_pageerror(page);
  640. return status;
  641. }
  642. static void nfs_writepage_release(struct nfs_page *req)
  643. {
  644. end_page_writeback(req->wb_page);
  645. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  646. if (!PageError(req->wb_page)) {
  647. if (NFS_NEED_RESCHED(req)) {
  648. nfs_redirty_request(req);
  649. goto out;
  650. } else if (NFS_NEED_COMMIT(req)) {
  651. nfs_mark_request_commit(req);
  652. goto out;
  653. }
  654. }
  655. nfs_inode_remove_request(req);
  656. out:
  657. nfs_clear_commit(req);
  658. nfs_clear_reschedule(req);
  659. #else
  660. nfs_inode_remove_request(req);
  661. #endif
  662. nfs_clear_page_writeback(req);
  663. }
  664. static inline int flush_task_priority(int how)
  665. {
  666. switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
  667. case FLUSH_HIGHPRI:
  668. return RPC_PRIORITY_HIGH;
  669. case FLUSH_LOWPRI:
  670. return RPC_PRIORITY_LOW;
  671. }
  672. return RPC_PRIORITY_NORMAL;
  673. }
  674. /*
  675. * Set up the argument/result storage required for the RPC call.
  676. */
  677. static void nfs_write_rpcsetup(struct nfs_page *req,
  678. struct nfs_write_data *data,
  679. const struct rpc_call_ops *call_ops,
  680. unsigned int count, unsigned int offset,
  681. int how)
  682. {
  683. struct inode *inode;
  684. int flags;
  685. /* Set up the RPC argument and reply structs
  686. * NB: take care not to mess about with data->commit et al. */
  687. data->req = req;
  688. data->inode = inode = req->wb_context->dentry->d_inode;
  689. data->cred = req->wb_context->cred;
  690. data->args.fh = NFS_FH(inode);
  691. data->args.offset = req_offset(req) + offset;
  692. data->args.pgbase = req->wb_pgbase + offset;
  693. data->args.pages = data->pagevec;
  694. data->args.count = count;
  695. data->args.context = req->wb_context;
  696. data->res.fattr = &data->fattr;
  697. data->res.count = count;
  698. data->res.verf = &data->verf;
  699. nfs_fattr_init(&data->fattr);
  700. /* Set up the initial task struct. */
  701. flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  702. rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
  703. NFS_PROTO(inode)->write_setup(data, how);
  704. data->task.tk_priority = flush_task_priority(how);
  705. data->task.tk_cookie = (unsigned long)inode;
  706. dprintk("NFS: %5u initiated write call "
  707. "(req %s/%Ld, %u bytes @ offset %Lu)\n",
  708. data->task.tk_pid,
  709. inode->i_sb->s_id,
  710. (long long)NFS_FILEID(inode),
  711. count,
  712. (unsigned long long)data->args.offset);
  713. }
  714. static void nfs_execute_write(struct nfs_write_data *data)
  715. {
  716. struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
  717. sigset_t oldset;
  718. rpc_clnt_sigmask(clnt, &oldset);
  719. rpc_execute(&data->task);
  720. rpc_clnt_sigunmask(clnt, &oldset);
  721. }
  722. /*
  723. * Generate multiple small requests to write out a single
  724. * contiguous dirty area on one page.
  725. */
  726. static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
  727. {
  728. struct nfs_page *req = nfs_list_entry(head->next);
  729. struct page *page = req->wb_page;
  730. struct nfs_write_data *data;
  731. size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
  732. unsigned int offset;
  733. int requests = 0;
  734. LIST_HEAD(list);
  735. nfs_list_remove_request(req);
  736. nbytes = req->wb_bytes;
  737. do {
  738. size_t len = min(nbytes, wsize);
  739. data = nfs_writedata_alloc(len);
  740. if (!data)
  741. goto out_bad;
  742. list_add(&data->pages, &list);
  743. requests++;
  744. nbytes -= len;
  745. } while (nbytes != 0);
  746. atomic_set(&req->wb_complete, requests);
  747. ClearPageError(page);
  748. offset = 0;
  749. nbytes = req->wb_bytes;
  750. do {
  751. data = list_entry(list.next, struct nfs_write_data, pages);
  752. list_del_init(&data->pages);
  753. data->pagevec[0] = page;
  754. if (nbytes > wsize) {
  755. nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
  756. wsize, offset, how);
  757. offset += wsize;
  758. nbytes -= wsize;
  759. } else {
  760. nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
  761. nbytes, offset, how);
  762. nbytes = 0;
  763. }
  764. nfs_execute_write(data);
  765. } while (nbytes != 0);
  766. return 0;
  767. out_bad:
  768. while (!list_empty(&list)) {
  769. data = list_entry(list.next, struct nfs_write_data, pages);
  770. list_del(&data->pages);
  771. nfs_writedata_release(data);
  772. }
  773. nfs_redirty_request(req);
  774. nfs_clear_page_writeback(req);
  775. return -ENOMEM;
  776. }
  777. /*
  778. * Create an RPC task for the given write request and kick it.
  779. * The page must have been locked by the caller.
  780. *
  781. * It may happen that the page we're passed is not marked dirty.
  782. * This is the case if nfs_updatepage detects a conflicting request
  783. * that has been written but not committed.
  784. */
  785. static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
  786. {
  787. struct nfs_page *req;
  788. struct page **pages;
  789. struct nfs_write_data *data;
  790. unsigned int count;
  791. data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
  792. if (!data)
  793. goto out_bad;
  794. pages = data->pagevec;
  795. count = 0;
  796. while (!list_empty(head)) {
  797. req = nfs_list_entry(head->next);
  798. nfs_list_remove_request(req);
  799. nfs_list_add_request(req, &data->pages);
  800. ClearPageError(req->wb_page);
  801. *pages++ = req->wb_page;
  802. count += req->wb_bytes;
  803. }
  804. req = nfs_list_entry(data->pages.next);
  805. /* Set up the argument struct */
  806. nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
  807. nfs_execute_write(data);
  808. return 0;
  809. out_bad:
  810. while (!list_empty(head)) {
  811. struct nfs_page *req = nfs_list_entry(head->next);
  812. nfs_list_remove_request(req);
  813. nfs_redirty_request(req);
  814. nfs_clear_page_writeback(req);
  815. }
  816. return -ENOMEM;
  817. }
  818. static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
  819. {
  820. LIST_HEAD(one_request);
  821. int (*flush_one)(struct inode *, struct list_head *, int);
  822. struct nfs_page *req;
  823. int wpages = NFS_SERVER(inode)->wpages;
  824. int wsize = NFS_SERVER(inode)->wsize;
  825. int error;
  826. flush_one = nfs_flush_one;
  827. if (wsize < PAGE_CACHE_SIZE)
  828. flush_one = nfs_flush_multi;
  829. /* For single writes, FLUSH_STABLE is more efficient */
  830. if (npages <= wpages && npages == NFS_I(inode)->npages
  831. && nfs_list_entry(head->next)->wb_bytes <= wsize)
  832. how |= FLUSH_STABLE;
  833. do {
  834. nfs_coalesce_requests(head, &one_request, wpages);
  835. req = nfs_list_entry(one_request.next);
  836. error = flush_one(inode, &one_request, how);
  837. if (error < 0)
  838. goto out_err;
  839. } while (!list_empty(head));
  840. return 0;
  841. out_err:
  842. while (!list_empty(head)) {
  843. req = nfs_list_entry(head->next);
  844. nfs_list_remove_request(req);
  845. nfs_redirty_request(req);
  846. nfs_clear_page_writeback(req);
  847. }
  848. return error;
  849. }
  850. /*
  851. * Handle a write reply that flushed part of a page.
  852. */
  853. static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
  854. {
  855. struct nfs_write_data *data = calldata;
  856. struct nfs_page *req = data->req;
  857. struct page *page = req->wb_page;
  858. dprintk("NFS: write (%s/%Ld %d@%Ld)",
  859. req->wb_context->dentry->d_inode->i_sb->s_id,
  860. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  861. req->wb_bytes,
  862. (long long)req_offset(req));
  863. if (nfs_writeback_done(task, data) != 0)
  864. return;
  865. if (task->tk_status < 0) {
  866. nfs_set_pageerror(page);
  867. req->wb_context->error = task->tk_status;
  868. dprintk(", error = %d\n", task->tk_status);
  869. } else {
  870. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  871. if (data->verf.committed < NFS_FILE_SYNC) {
  872. if (!NFS_NEED_COMMIT(req)) {
  873. nfs_defer_commit(req);
  874. memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
  875. dprintk(" defer commit\n");
  876. } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
  877. nfs_defer_reschedule(req);
  878. dprintk(" server reboot detected\n");
  879. }
  880. } else
  881. #endif
  882. dprintk(" OK\n");
  883. }
  884. if (atomic_dec_and_test(&req->wb_complete))
  885. nfs_writepage_release(req);
  886. }
  887. static const struct rpc_call_ops nfs_write_partial_ops = {
  888. .rpc_call_done = nfs_writeback_done_partial,
  889. .rpc_release = nfs_writedata_release,
  890. };
  891. /*
  892. * Handle a write reply that flushes a whole page.
  893. *
  894. * FIXME: There is an inherent race with invalidate_inode_pages and
  895. * writebacks since the page->count is kept > 1 for as long
  896. * as the page has a write request pending.
  897. */
  898. static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
  899. {
  900. struct nfs_write_data *data = calldata;
  901. struct nfs_page *req;
  902. struct page *page;
  903. if (nfs_writeback_done(task, data) != 0)
  904. return;
  905. /* Update attributes as result of writeback. */
  906. while (!list_empty(&data->pages)) {
  907. req = nfs_list_entry(data->pages.next);
  908. nfs_list_remove_request(req);
  909. page = req->wb_page;
  910. dprintk("NFS: write (%s/%Ld %d@%Ld)",
  911. req->wb_context->dentry->d_inode->i_sb->s_id,
  912. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  913. req->wb_bytes,
  914. (long long)req_offset(req));
  915. if (task->tk_status < 0) {
  916. nfs_set_pageerror(page);
  917. req->wb_context->error = task->tk_status;
  918. end_page_writeback(page);
  919. nfs_inode_remove_request(req);
  920. dprintk(", error = %d\n", task->tk_status);
  921. goto next;
  922. }
  923. end_page_writeback(page);
  924. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  925. if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
  926. nfs_inode_remove_request(req);
  927. dprintk(" OK\n");
  928. goto next;
  929. }
  930. memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
  931. nfs_mark_request_commit(req);
  932. dprintk(" marked for commit\n");
  933. #else
  934. nfs_inode_remove_request(req);
  935. #endif
  936. next:
  937. nfs_clear_page_writeback(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;
  1030. struct inode *inode;
  1031. int flags;
  1032. /* Set up the RPC argument and reply structs
  1033. * NB: take care not to mess about with data->commit et al. */
  1034. list_splice_init(head, &data->pages);
  1035. first = nfs_list_entry(data->pages.next);
  1036. inode = first->wb_context->dentry->d_inode;
  1037. data->inode = inode;
  1038. data->cred = first->wb_context->cred;
  1039. data->args.fh = NFS_FH(data->inode);
  1040. /* Note: we always request a commit of the entire inode */
  1041. data->args.offset = 0;
  1042. data->args.count = 0;
  1043. data->res.count = 0;
  1044. data->res.fattr = &data->fattr;
  1045. data->res.verf = &data->verf;
  1046. nfs_fattr_init(&data->fattr);
  1047. /* Set up the initial task struct. */
  1048. flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  1049. rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
  1050. NFS_PROTO(inode)->commit_setup(data, how);
  1051. data->task.tk_priority = flush_task_priority(how);
  1052. data->task.tk_cookie = (unsigned long)inode;
  1053. dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
  1054. }
  1055. /*
  1056. * Commit dirty pages
  1057. */
  1058. static int
  1059. nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1060. {
  1061. struct nfs_write_data *data;
  1062. struct nfs_page *req;
  1063. data = nfs_commit_alloc();
  1064. if (!data)
  1065. goto out_bad;
  1066. /* Set up the argument struct */
  1067. nfs_commit_rpcsetup(head, data, how);
  1068. nfs_execute_write(data);
  1069. return 0;
  1070. out_bad:
  1071. while (!list_empty(head)) {
  1072. req = nfs_list_entry(head->next);
  1073. nfs_list_remove_request(req);
  1074. nfs_mark_request_commit(req);
  1075. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  1076. nfs_clear_page_writeback(req);
  1077. }
  1078. return -ENOMEM;
  1079. }
  1080. /*
  1081. * COMMIT call returned
  1082. */
  1083. static void nfs_commit_done(struct rpc_task *task, void *calldata)
  1084. {
  1085. struct nfs_write_data *data = calldata;
  1086. struct nfs_page *req;
  1087. dprintk("NFS: %5u nfs_commit_done (status %d)\n",
  1088. task->tk_pid, task->tk_status);
  1089. /* Call the NFS version-specific code */
  1090. if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
  1091. return;
  1092. while (!list_empty(&data->pages)) {
  1093. req = nfs_list_entry(data->pages.next);
  1094. nfs_list_remove_request(req);
  1095. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  1096. dprintk("NFS: commit (%s/%Ld %d@%Ld)",
  1097. req->wb_context->dentry->d_inode->i_sb->s_id,
  1098. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  1099. req->wb_bytes,
  1100. (long long)req_offset(req));
  1101. if (task->tk_status < 0) {
  1102. req->wb_context->error = task->tk_status;
  1103. nfs_inode_remove_request(req);
  1104. dprintk(", error = %d\n", task->tk_status);
  1105. goto next;
  1106. }
  1107. /* Okay, COMMIT succeeded, apparently. Check the verifier
  1108. * returned by the server against all stored verfs. */
  1109. if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
  1110. /* We have a match */
  1111. nfs_inode_remove_request(req);
  1112. dprintk(" OK\n");
  1113. goto next;
  1114. }
  1115. /* We have a mismatch. Write the page again */
  1116. dprintk(" mismatch\n");
  1117. nfs_redirty_request(req);
  1118. next:
  1119. nfs_clear_page_writeback(req);
  1120. }
  1121. }
  1122. static const struct rpc_call_ops nfs_commit_ops = {
  1123. .rpc_call_done = nfs_commit_done,
  1124. .rpc_release = nfs_commit_release,
  1125. };
  1126. #else
  1127. static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1128. {
  1129. return 0;
  1130. }
  1131. #endif
  1132. static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
  1133. {
  1134. struct nfs_inode *nfsi = NFS_I(mapping->host);
  1135. LIST_HEAD(head);
  1136. long res;
  1137. spin_lock(&nfsi->req_lock);
  1138. res = nfs_scan_dirty(mapping, wbc, &head);
  1139. spin_unlock(&nfsi->req_lock);
  1140. if (res) {
  1141. int error = nfs_flush_list(mapping->host, &head, res, how);
  1142. if (error < 0)
  1143. return error;
  1144. }
  1145. return res;
  1146. }
  1147. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1148. int nfs_commit_inode(struct inode *inode, int how)
  1149. {
  1150. struct nfs_inode *nfsi = NFS_I(inode);
  1151. LIST_HEAD(head);
  1152. int res;
  1153. spin_lock(&nfsi->req_lock);
  1154. res = nfs_scan_commit(inode, &head, 0, 0);
  1155. spin_unlock(&nfsi->req_lock);
  1156. if (res) {
  1157. int error = nfs_commit_list(inode, &head, how);
  1158. if (error < 0)
  1159. return error;
  1160. }
  1161. return res;
  1162. }
  1163. #endif
  1164. long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
  1165. {
  1166. struct inode *inode = mapping->host;
  1167. struct nfs_inode *nfsi = NFS_I(inode);
  1168. unsigned long idx_start, idx_end;
  1169. unsigned int npages = 0;
  1170. LIST_HEAD(head);
  1171. int nocommit = how & FLUSH_NOCOMMIT;
  1172. long pages, ret;
  1173. /* FIXME */
  1174. if (wbc->range_cyclic)
  1175. idx_start = 0;
  1176. else {
  1177. idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
  1178. idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
  1179. if (idx_end > idx_start) {
  1180. unsigned long l_npages = 1 + idx_end - idx_start;
  1181. npages = l_npages;
  1182. if (sizeof(npages) != sizeof(l_npages) &&
  1183. (unsigned long)npages != l_npages)
  1184. npages = 0;
  1185. }
  1186. }
  1187. how &= ~FLUSH_NOCOMMIT;
  1188. spin_lock(&nfsi->req_lock);
  1189. do {
  1190. wbc->pages_skipped = 0;
  1191. ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
  1192. if (ret != 0)
  1193. continue;
  1194. pages = nfs_scan_dirty(mapping, wbc, &head);
  1195. if (pages != 0) {
  1196. spin_unlock(&nfsi->req_lock);
  1197. if (how & FLUSH_INVALIDATE) {
  1198. nfs_cancel_dirty_list(&head);
  1199. ret = pages;
  1200. } else
  1201. ret = nfs_flush_list(inode, &head, pages, how);
  1202. spin_lock(&nfsi->req_lock);
  1203. continue;
  1204. }
  1205. if (wbc->pages_skipped != 0)
  1206. continue;
  1207. if (nocommit)
  1208. break;
  1209. pages = nfs_scan_commit(inode, &head, idx_start, npages);
  1210. if (pages == 0) {
  1211. if (wbc->pages_skipped != 0)
  1212. continue;
  1213. break;
  1214. }
  1215. if (how & FLUSH_INVALIDATE) {
  1216. spin_unlock(&nfsi->req_lock);
  1217. nfs_cancel_commit_list(&head);
  1218. ret = pages;
  1219. spin_lock(&nfsi->req_lock);
  1220. continue;
  1221. }
  1222. pages += nfs_scan_commit(inode, &head, 0, 0);
  1223. spin_unlock(&nfsi->req_lock);
  1224. ret = nfs_commit_list(inode, &head, how);
  1225. spin_lock(&nfsi->req_lock);
  1226. } while (ret >= 0);
  1227. spin_unlock(&nfsi->req_lock);
  1228. return ret;
  1229. }
  1230. /*
  1231. * flush the inode to disk.
  1232. */
  1233. int nfs_wb_all(struct inode *inode)
  1234. {
  1235. struct address_space *mapping = inode->i_mapping;
  1236. struct writeback_control wbc = {
  1237. .bdi = mapping->backing_dev_info,
  1238. .sync_mode = WB_SYNC_ALL,
  1239. .nr_to_write = LONG_MAX,
  1240. .for_writepages = 1,
  1241. .range_cyclic = 1,
  1242. };
  1243. int ret;
  1244. ret = generic_writepages(mapping, &wbc);
  1245. if (ret < 0)
  1246. goto out;
  1247. ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
  1248. if (ret >= 0)
  1249. return 0;
  1250. out:
  1251. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  1252. return ret;
  1253. }
  1254. int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
  1255. {
  1256. struct writeback_control wbc = {
  1257. .bdi = mapping->backing_dev_info,
  1258. .sync_mode = WB_SYNC_ALL,
  1259. .nr_to_write = LONG_MAX,
  1260. .range_start = range_start,
  1261. .range_end = range_end,
  1262. .for_writepages = 1,
  1263. };
  1264. int ret;
  1265. if (!(how & FLUSH_NOWRITEPAGE)) {
  1266. ret = generic_writepages(mapping, &wbc);
  1267. if (ret < 0)
  1268. goto out;
  1269. }
  1270. ret = nfs_sync_mapping_wait(mapping, &wbc, how);
  1271. if (ret >= 0)
  1272. return 0;
  1273. out:
  1274. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  1275. return ret;
  1276. }
  1277. int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
  1278. {
  1279. loff_t range_start = page_offset(page);
  1280. loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
  1281. struct writeback_control wbc = {
  1282. .bdi = page->mapping->backing_dev_info,
  1283. .sync_mode = WB_SYNC_ALL,
  1284. .nr_to_write = LONG_MAX,
  1285. .range_start = range_start,
  1286. .range_end = range_end,
  1287. };
  1288. int ret;
  1289. BUG_ON(!PageLocked(page));
  1290. if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
  1291. ret = nfs_writepage_locked(page, &wbc);
  1292. if (ret < 0)
  1293. goto out;
  1294. }
  1295. if (!PagePrivate(page))
  1296. return 0;
  1297. ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
  1298. if (ret >= 0)
  1299. return 0;
  1300. out:
  1301. __mark_inode_dirty(inode, I_DIRTY_PAGES);
  1302. return ret;
  1303. }
  1304. /*
  1305. * Write back all requests on one page - we do this before reading it.
  1306. */
  1307. int nfs_wb_page(struct inode *inode, struct page* page)
  1308. {
  1309. return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
  1310. }
  1311. int nfs_set_page_dirty(struct page *page)
  1312. {
  1313. struct nfs_page *req;
  1314. req = nfs_page_find_request(page);
  1315. if (req != NULL) {
  1316. /* Mark any existing write requests for flushing */
  1317. set_bit(PG_NEED_FLUSH, &req->wb_flags);
  1318. nfs_release_request(req);
  1319. }
  1320. return __set_page_dirty_nobuffers(page);
  1321. }
  1322. int __init nfs_init_writepagecache(void)
  1323. {
  1324. nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
  1325. sizeof(struct nfs_write_data),
  1326. 0, SLAB_HWCACHE_ALIGN,
  1327. NULL, NULL);
  1328. if (nfs_wdata_cachep == NULL)
  1329. return -ENOMEM;
  1330. nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
  1331. nfs_wdata_cachep);
  1332. if (nfs_wdata_mempool == NULL)
  1333. return -ENOMEM;
  1334. nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
  1335. nfs_wdata_cachep);
  1336. if (nfs_commit_mempool == NULL)
  1337. return -ENOMEM;
  1338. return 0;
  1339. }
  1340. void nfs_destroy_writepagecache(void)
  1341. {
  1342. mempool_destroy(nfs_commit_mempool);
  1343. mempool_destroy(nfs_wdata_mempool);
  1344. kmem_cache_destroy(nfs_wdata_cachep);
  1345. }