write.c 41 KB

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