write.c 40 KB

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