write.c 41 KB

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