write.c 47 KB

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