write.c 39 KB

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