write.c 37 KB

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