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