write.c 47 KB

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