write.c 45 KB

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