write.c 40 KB

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