write.c 41 KB

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