write.c 38 KB

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