write.c 42 KB

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