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