write.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  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/sunrpc/clnt.h>
  16. #include <linux/nfs_fs.h>
  17. #include <linux/nfs_mount.h>
  18. #include <linux/nfs_page.h>
  19. #include <linux/backing-dev.h>
  20. #include <asm/uaccess.h>
  21. #include "delegation.h"
  22. #include "internal.h"
  23. #include "iostat.h"
  24. #define NFSDBG_FACILITY NFSDBG_PAGECACHE
  25. #define MIN_POOL_WRITE (32)
  26. #define MIN_POOL_COMMIT (4)
  27. /*
  28. * Local function declarations
  29. */
  30. static struct nfs_page * nfs_update_request(struct nfs_open_context*,
  31. struct page *,
  32. unsigned int, unsigned int);
  33. static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
  34. struct inode *inode, int ioflags);
  35. static void nfs_redirty_request(struct nfs_page *req);
  36. static const struct rpc_call_ops nfs_write_partial_ops;
  37. static const struct rpc_call_ops nfs_write_full_ops;
  38. static const struct rpc_call_ops nfs_commit_ops;
  39. static struct kmem_cache *nfs_wdata_cachep;
  40. static mempool_t *nfs_wdata_mempool;
  41. static mempool_t *nfs_commit_mempool;
  42. struct nfs_write_data *nfs_commitdata_alloc(void)
  43. {
  44. struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
  45. if (p) {
  46. memset(p, 0, sizeof(*p));
  47. INIT_LIST_HEAD(&p->pages);
  48. }
  49. return p;
  50. }
  51. void nfs_commit_free(struct nfs_write_data *p)
  52. {
  53. if (p && (p->pagevec != &p->page_array[0]))
  54. kfree(p->pagevec);
  55. mempool_free(p, nfs_commit_mempool);
  56. }
  57. struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
  58. {
  59. struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
  60. if (p) {
  61. memset(p, 0, sizeof(*p));
  62. INIT_LIST_HEAD(&p->pages);
  63. p->npages = pagecount;
  64. if (pagecount <= ARRAY_SIZE(p->page_array))
  65. p->pagevec = p->page_array;
  66. else {
  67. p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
  68. if (!p->pagevec) {
  69. mempool_free(p, nfs_wdata_mempool);
  70. p = NULL;
  71. }
  72. }
  73. }
  74. return p;
  75. }
  76. static void nfs_writedata_free(struct nfs_write_data *p)
  77. {
  78. if (p && (p->pagevec != &p->page_array[0]))
  79. kfree(p->pagevec);
  80. mempool_free(p, nfs_wdata_mempool);
  81. }
  82. void nfs_writedata_release(void *data)
  83. {
  84. struct nfs_write_data *wdata = data;
  85. put_nfs_open_context(wdata->args.context);
  86. nfs_writedata_free(wdata);
  87. }
  88. static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
  89. {
  90. ctx->error = error;
  91. smp_wmb();
  92. set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  93. }
  94. static struct nfs_page *nfs_page_find_request_locked(struct page *page)
  95. {
  96. struct nfs_page *req = NULL;
  97. if (PagePrivate(page)) {
  98. req = (struct nfs_page *)page_private(page);
  99. if (req != NULL)
  100. kref_get(&req->wb_kref);
  101. }
  102. return req;
  103. }
  104. static struct nfs_page *nfs_page_find_request(struct page *page)
  105. {
  106. struct inode *inode = page->mapping->host;
  107. struct nfs_page *req = NULL;
  108. spin_lock(&inode->i_lock);
  109. req = nfs_page_find_request_locked(page);
  110. spin_unlock(&inode->i_lock);
  111. return req;
  112. }
  113. /* Adjust the file length if we're writing beyond the end */
  114. static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
  115. {
  116. struct inode *inode = page->mapping->host;
  117. loff_t end, i_size = i_size_read(inode);
  118. pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
  119. if (i_size > 0 && page->index < end_index)
  120. return;
  121. end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
  122. if (i_size >= end)
  123. return;
  124. nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
  125. i_size_write(inode, end);
  126. }
  127. /* A writeback failed: mark the page as bad, and invalidate the page cache */
  128. static void nfs_set_pageerror(struct page *page)
  129. {
  130. SetPageError(page);
  131. nfs_zap_mapping(page->mapping->host, page->mapping);
  132. }
  133. /* We can set the PG_uptodate flag if we see that a write request
  134. * covers the full page.
  135. */
  136. static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
  137. {
  138. if (PageUptodate(page))
  139. return;
  140. if (base != 0)
  141. return;
  142. if (count != nfs_page_length(page))
  143. return;
  144. SetPageUptodate(page);
  145. }
  146. static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
  147. unsigned int offset, unsigned int count)
  148. {
  149. struct nfs_page *req;
  150. int ret;
  151. for (;;) {
  152. req = nfs_update_request(ctx, page, offset, count);
  153. if (!IS_ERR(req))
  154. break;
  155. ret = PTR_ERR(req);
  156. if (ret != -EBUSY)
  157. return ret;
  158. ret = nfs_wb_page(page->mapping->host, page);
  159. if (ret != 0)
  160. return ret;
  161. }
  162. /* Update file length */
  163. nfs_grow_file(page, offset, count);
  164. nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
  165. nfs_clear_page_tag_locked(req);
  166. return 0;
  167. }
  168. static int wb_priority(struct writeback_control *wbc)
  169. {
  170. if (wbc->for_reclaim)
  171. return FLUSH_HIGHPRI | FLUSH_STABLE;
  172. if (wbc->for_kupdate)
  173. return FLUSH_LOWPRI;
  174. return 0;
  175. }
  176. /*
  177. * NFS congestion control
  178. */
  179. int nfs_congestion_kb;
  180. #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
  181. #define NFS_CONGESTION_OFF_THRESH \
  182. (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
  183. static int nfs_set_page_writeback(struct page *page)
  184. {
  185. int ret = test_set_page_writeback(page);
  186. if (!ret) {
  187. struct inode *inode = page->mapping->host;
  188. struct nfs_server *nfss = NFS_SERVER(inode);
  189. if (atomic_long_inc_return(&nfss->writeback) >
  190. NFS_CONGESTION_ON_THRESH)
  191. set_bdi_congested(&nfss->backing_dev_info, WRITE);
  192. }
  193. return ret;
  194. }
  195. static void nfs_end_page_writeback(struct page *page)
  196. {
  197. struct inode *inode = page->mapping->host;
  198. struct nfs_server *nfss = NFS_SERVER(inode);
  199. end_page_writeback(page);
  200. if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
  201. clear_bdi_congested(&nfss->backing_dev_info, WRITE);
  202. }
  203. /*
  204. * Find an associated nfs write request, and prepare to flush it out
  205. * May return an error if the user signalled nfs_wait_on_request().
  206. */
  207. static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
  208. struct page *page)
  209. {
  210. struct inode *inode = page->mapping->host;
  211. struct nfs_page *req;
  212. int ret;
  213. spin_lock(&inode->i_lock);
  214. for(;;) {
  215. req = nfs_page_find_request_locked(page);
  216. if (req == NULL) {
  217. spin_unlock(&inode->i_lock);
  218. return 0;
  219. }
  220. if (nfs_set_page_tag_locked(req))
  221. break;
  222. /* Note: If we hold the page lock, as is the case in nfs_writepage,
  223. * then the call to nfs_set_page_tag_locked() will always
  224. * succeed provided that someone hasn't already marked the
  225. * request as dirty (in which case we don't care).
  226. */
  227. spin_unlock(&inode->i_lock);
  228. ret = nfs_wait_on_request(req);
  229. nfs_release_request(req);
  230. if (ret != 0)
  231. return ret;
  232. spin_lock(&inode->i_lock);
  233. }
  234. if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
  235. /* This request is marked for commit */
  236. spin_unlock(&inode->i_lock);
  237. nfs_clear_page_tag_locked(req);
  238. nfs_pageio_complete(pgio);
  239. return 0;
  240. }
  241. if (nfs_set_page_writeback(page) != 0) {
  242. spin_unlock(&inode->i_lock);
  243. BUG();
  244. }
  245. spin_unlock(&inode->i_lock);
  246. if (!nfs_pageio_add_request(pgio, req)) {
  247. nfs_redirty_request(req);
  248. return pgio->pg_error;
  249. }
  250. return 0;
  251. }
  252. static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
  253. {
  254. struct inode *inode = page->mapping->host;
  255. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
  256. nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
  257. nfs_pageio_cond_complete(pgio, page->index);
  258. return nfs_page_async_flush(pgio, page);
  259. }
  260. /*
  261. * Write an mmapped page to the server.
  262. */
  263. static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
  264. {
  265. struct nfs_pageio_descriptor pgio;
  266. int err;
  267. nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
  268. err = nfs_do_writepage(page, wbc, &pgio);
  269. nfs_pageio_complete(&pgio);
  270. if (err < 0)
  271. return err;
  272. if (pgio.pg_error < 0)
  273. return pgio.pg_error;
  274. return 0;
  275. }
  276. int nfs_writepage(struct page *page, struct writeback_control *wbc)
  277. {
  278. int ret;
  279. ret = nfs_writepage_locked(page, wbc);
  280. unlock_page(page);
  281. return ret;
  282. }
  283. static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
  284. {
  285. int ret;
  286. ret = nfs_do_writepage(page, wbc, data);
  287. unlock_page(page);
  288. return ret;
  289. }
  290. int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  291. {
  292. struct inode *inode = mapping->host;
  293. struct nfs_pageio_descriptor pgio;
  294. int err;
  295. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
  296. nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
  297. err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
  298. nfs_pageio_complete(&pgio);
  299. if (err < 0)
  300. return err;
  301. if (pgio.pg_error < 0)
  302. return pgio.pg_error;
  303. return 0;
  304. }
  305. /*
  306. * Insert a write request into an inode
  307. */
  308. static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
  309. {
  310. struct nfs_inode *nfsi = NFS_I(inode);
  311. int error;
  312. error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
  313. BUG_ON(error);
  314. if (!nfsi->npages) {
  315. igrab(inode);
  316. if (nfs_have_delegation(inode, FMODE_WRITE))
  317. nfsi->change_attr++;
  318. }
  319. SetPagePrivate(req->wb_page);
  320. set_page_private(req->wb_page, (unsigned long)req);
  321. nfsi->npages++;
  322. kref_get(&req->wb_kref);
  323. radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
  324. NFS_PAGE_TAG_LOCKED);
  325. }
  326. /*
  327. * Remove a write request from an inode
  328. */
  329. static void nfs_inode_remove_request(struct nfs_page *req)
  330. {
  331. struct inode *inode = req->wb_context->path.dentry->d_inode;
  332. struct nfs_inode *nfsi = NFS_I(inode);
  333. BUG_ON (!NFS_WBACK_BUSY(req));
  334. spin_lock(&inode->i_lock);
  335. set_page_private(req->wb_page, 0);
  336. ClearPagePrivate(req->wb_page);
  337. radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
  338. nfsi->npages--;
  339. if (!nfsi->npages) {
  340. spin_unlock(&inode->i_lock);
  341. iput(inode);
  342. } else
  343. spin_unlock(&inode->i_lock);
  344. nfs_clear_request(req);
  345. nfs_release_request(req);
  346. }
  347. static void
  348. nfs_mark_request_dirty(struct nfs_page *req)
  349. {
  350. __set_page_dirty_nobuffers(req->wb_page);
  351. }
  352. /*
  353. * Check if a request is dirty
  354. */
  355. static inline int
  356. nfs_dirty_request(struct nfs_page *req)
  357. {
  358. struct page *page = req->wb_page;
  359. if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
  360. return 0;
  361. return !PageWriteback(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. nfsi->ncommit++;
  374. set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
  375. radix_tree_tag_set(&nfsi->nfs_page_tree,
  376. req->wb_index,
  377. NFS_PAGE_TAG_COMMIT);
  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 inline
  384. int nfs_write_need_commit(struct nfs_write_data *data)
  385. {
  386. return data->verf.committed != NFS_FILE_SYNC;
  387. }
  388. static inline
  389. int nfs_reschedule_unstable_write(struct nfs_page *req)
  390. {
  391. if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
  392. nfs_mark_request_commit(req);
  393. return 1;
  394. }
  395. if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
  396. nfs_mark_request_dirty(req);
  397. return 1;
  398. }
  399. return 0;
  400. }
  401. #else
  402. static inline void
  403. nfs_mark_request_commit(struct nfs_page *req)
  404. {
  405. }
  406. static inline
  407. int nfs_write_need_commit(struct nfs_write_data *data)
  408. {
  409. return 0;
  410. }
  411. static inline
  412. int nfs_reschedule_unstable_write(struct nfs_page *req)
  413. {
  414. return 0;
  415. }
  416. #endif
  417. /*
  418. * Wait for a request to complete.
  419. *
  420. * Interruptible by fatal signals only.
  421. */
  422. static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
  423. {
  424. struct nfs_inode *nfsi = NFS_I(inode);
  425. struct nfs_page *req;
  426. pgoff_t idx_end, next;
  427. unsigned int res = 0;
  428. int error;
  429. if (npages == 0)
  430. idx_end = ~0;
  431. else
  432. idx_end = idx_start + npages - 1;
  433. next = idx_start;
  434. while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
  435. if (req->wb_index > idx_end)
  436. break;
  437. next = req->wb_index + 1;
  438. BUG_ON(!NFS_WBACK_BUSY(req));
  439. kref_get(&req->wb_kref);
  440. spin_unlock(&inode->i_lock);
  441. error = nfs_wait_on_request(req);
  442. nfs_release_request(req);
  443. spin_lock(&inode->i_lock);
  444. if (error < 0)
  445. return error;
  446. res++;
  447. }
  448. return res;
  449. }
  450. static void nfs_cancel_commit_list(struct list_head *head)
  451. {
  452. struct nfs_page *req;
  453. while(!list_empty(head)) {
  454. req = nfs_list_entry(head->next);
  455. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  456. dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
  457. BDI_RECLAIMABLE);
  458. nfs_list_remove_request(req);
  459. clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
  460. nfs_inode_remove_request(req);
  461. nfs_unlock_request(req);
  462. }
  463. }
  464. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  465. /*
  466. * nfs_scan_commit - Scan an inode for commit requests
  467. * @inode: NFS inode to scan
  468. * @dst: destination list
  469. * @idx_start: lower bound of page->index to scan.
  470. * @npages: idx_start + npages sets the upper bound to scan.
  471. *
  472. * Moves requests from the inode's 'commit' request list.
  473. * The requests are *not* checked to ensure that they form a contiguous set.
  474. */
  475. static int
  476. nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
  477. {
  478. struct nfs_inode *nfsi = NFS_I(inode);
  479. int res = 0;
  480. if (nfsi->ncommit != 0) {
  481. res = nfs_scan_list(nfsi, dst, idx_start, npages,
  482. NFS_PAGE_TAG_COMMIT);
  483. nfsi->ncommit -= res;
  484. }
  485. return res;
  486. }
  487. #else
  488. static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
  489. {
  490. return 0;
  491. }
  492. #endif
  493. /*
  494. * Try to update any existing write request, or create one if there is none.
  495. * In order to match, the request's credentials must match those of
  496. * the calling process.
  497. *
  498. * Note: Should always be called with the Page Lock held!
  499. */
  500. static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
  501. struct page *page, unsigned int offset, unsigned int bytes)
  502. {
  503. struct address_space *mapping = page->mapping;
  504. struct inode *inode = mapping->host;
  505. struct nfs_page *req, *new = NULL;
  506. pgoff_t rqend, end;
  507. end = offset + bytes;
  508. for (;;) {
  509. /* Loop over all inode entries and see if we find
  510. * A request for the page we wish to update
  511. */
  512. spin_lock(&inode->i_lock);
  513. req = nfs_page_find_request_locked(page);
  514. if (req) {
  515. if (!nfs_set_page_tag_locked(req)) {
  516. int error;
  517. spin_unlock(&inode->i_lock);
  518. error = nfs_wait_on_request(req);
  519. nfs_release_request(req);
  520. if (error < 0) {
  521. if (new) {
  522. radix_tree_preload_end();
  523. nfs_release_request(new);
  524. }
  525. return ERR_PTR(error);
  526. }
  527. continue;
  528. }
  529. spin_unlock(&inode->i_lock);
  530. if (new) {
  531. radix_tree_preload_end();
  532. nfs_release_request(new);
  533. }
  534. break;
  535. }
  536. if (new) {
  537. nfs_lock_request_dontget(new);
  538. nfs_inode_add_request(inode, new);
  539. spin_unlock(&inode->i_lock);
  540. radix_tree_preload_end();
  541. req = new;
  542. goto out;
  543. }
  544. spin_unlock(&inode->i_lock);
  545. new = nfs_create_request(ctx, inode, page, offset, bytes);
  546. if (IS_ERR(new))
  547. return new;
  548. if (radix_tree_preload(GFP_NOFS)) {
  549. nfs_release_request(new);
  550. return ERR_PTR(-ENOMEM);
  551. }
  552. }
  553. /* We have a request for our page.
  554. * If the creds don't match, or the
  555. * page addresses don't match,
  556. * tell the caller to wait on the conflicting
  557. * request.
  558. */
  559. rqend = req->wb_offset + req->wb_bytes;
  560. if (req->wb_context != ctx
  561. || req->wb_page != page
  562. || !nfs_dirty_request(req)
  563. || offset > rqend || end < req->wb_offset) {
  564. nfs_clear_page_tag_locked(req);
  565. return ERR_PTR(-EBUSY);
  566. }
  567. /* Okay, the request matches. Update the region */
  568. if (offset < req->wb_offset) {
  569. req->wb_offset = offset;
  570. req->wb_pgbase = offset;
  571. req->wb_bytes = max(end, rqend) - req->wb_offset;
  572. goto out;
  573. }
  574. if (end > rqend)
  575. req->wb_bytes = end - req->wb_offset;
  576. out:
  577. return req;
  578. }
  579. int nfs_flush_incompatible(struct file *file, struct page *page)
  580. {
  581. struct nfs_open_context *ctx = nfs_file_open_context(file);
  582. struct nfs_page *req;
  583. int do_flush, status;
  584. /*
  585. * Look for a request corresponding to this page. If there
  586. * is one, and it belongs to another file, we flush it out
  587. * before we try to copy anything into the page. Do this
  588. * due to the lack of an ACCESS-type call in NFSv2.
  589. * Also do the same if we find a request from an existing
  590. * dropped page.
  591. */
  592. do {
  593. req = nfs_page_find_request(page);
  594. if (req == NULL)
  595. return 0;
  596. do_flush = req->wb_page != page || req->wb_context != ctx
  597. || !nfs_dirty_request(req);
  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@%Ld)\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_SYNC)) {
  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 %Ld)\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_I(inode)->ncommit)
  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/%Ld, %u bytes @ offset %Lu)\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. struct nfs_page *req = data->req;
  849. dprintk("NFS: write (%s/%Ld %d@%Ld)",
  850. req->wb_context->path.dentry->d_inode->i_sb->s_id,
  851. (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
  852. req->wb_bytes,
  853. (long long)req_offset(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. static const struct rpc_call_ops nfs_write_partial_ops = {
  890. .rpc_call_done = nfs_writeback_done_partial,
  891. .rpc_release = nfs_writeback_release_partial,
  892. };
  893. /*
  894. * Handle a write reply that flushes a whole page.
  895. *
  896. * FIXME: There is an inherent race with invalidate_inode_pages and
  897. * writebacks since the page->count is kept > 1 for as long
  898. * as the page has a write request pending.
  899. */
  900. static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
  901. {
  902. struct nfs_write_data *data = calldata;
  903. nfs_writeback_done(task, data);
  904. }
  905. static void nfs_writeback_release_full(void *calldata)
  906. {
  907. struct nfs_write_data *data = calldata;
  908. int status = data->task.tk_status;
  909. /* Update attributes as result of writeback. */
  910. while (!list_empty(&data->pages)) {
  911. struct nfs_page *req = nfs_list_entry(data->pages.next);
  912. struct page *page = req->wb_page;
  913. nfs_list_remove_request(req);
  914. dprintk("NFS: write (%s/%Ld %d@%Ld)",
  915. req->wb_context->path.dentry->d_inode->i_sb->s_id,
  916. (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
  917. req->wb_bytes,
  918. (long long)req_offset(req));
  919. if (status < 0) {
  920. nfs_set_pageerror(page);
  921. nfs_context_set_write_error(req->wb_context, status);
  922. dprintk(", error = %d\n", status);
  923. goto remove_request;
  924. }
  925. if (nfs_write_need_commit(data)) {
  926. memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
  927. nfs_mark_request_commit(req);
  928. nfs_end_page_writeback(page);
  929. dprintk(" marked for commit\n");
  930. goto next;
  931. }
  932. dprintk(" OK\n");
  933. remove_request:
  934. nfs_end_page_writeback(page);
  935. nfs_inode_remove_request(req);
  936. next:
  937. nfs_clear_page_tag_locked(req);
  938. }
  939. nfs_writedata_release(calldata);
  940. }
  941. static const struct rpc_call_ops nfs_write_full_ops = {
  942. .rpc_call_done = nfs_writeback_done_full,
  943. .rpc_release = nfs_writeback_release_full,
  944. };
  945. /*
  946. * This function is called when the WRITE call is complete.
  947. */
  948. int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
  949. {
  950. struct nfs_writeargs *argp = &data->args;
  951. struct nfs_writeres *resp = &data->res;
  952. int status;
  953. dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
  954. task->tk_pid, task->tk_status);
  955. /*
  956. * ->write_done will attempt to use post-op attributes to detect
  957. * conflicting writes by other clients. A strict interpretation
  958. * of close-to-open would allow us to continue caching even if
  959. * another writer had changed the file, but some applications
  960. * depend on tighter cache coherency when writing.
  961. */
  962. status = NFS_PROTO(data->inode)->write_done(task, data);
  963. if (status != 0)
  964. return status;
  965. nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
  966. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  967. if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
  968. /* We tried a write call, but the server did not
  969. * commit data to stable storage even though we
  970. * requested it.
  971. * Note: There is a known bug in Tru64 < 5.0 in which
  972. * the server reports NFS_DATA_SYNC, but performs
  973. * NFS_FILE_SYNC. We therefore implement this checking
  974. * as a dprintk() in order to avoid filling syslog.
  975. */
  976. static unsigned long complain;
  977. if (time_before(complain, jiffies)) {
  978. dprintk("NFS: faulty NFS server %s:"
  979. " (committed = %d) != (stable = %d)\n",
  980. NFS_SERVER(data->inode)->nfs_client->cl_hostname,
  981. resp->verf->committed, argp->stable);
  982. complain = jiffies + 300 * HZ;
  983. }
  984. }
  985. #endif
  986. /* Is this a short write? */
  987. if (task->tk_status >= 0 && resp->count < argp->count) {
  988. static unsigned long complain;
  989. nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
  990. /* Has the server at least made some progress? */
  991. if (resp->count != 0) {
  992. /* Was this an NFSv2 write or an NFSv3 stable write? */
  993. if (resp->verf->committed != NFS_UNSTABLE) {
  994. /* Resend from where the server left off */
  995. argp->offset += resp->count;
  996. argp->pgbase += resp->count;
  997. argp->count -= resp->count;
  998. } else {
  999. /* Resend as a stable write in order to avoid
  1000. * headaches in the case of a server crash.
  1001. */
  1002. argp->stable = NFS_FILE_SYNC;
  1003. }
  1004. rpc_restart_call(task);
  1005. return -EAGAIN;
  1006. }
  1007. if (time_before(complain, jiffies)) {
  1008. printk(KERN_WARNING
  1009. "NFS: Server wrote zero bytes, expected %u.\n",
  1010. argp->count);
  1011. complain = jiffies + 300 * HZ;
  1012. }
  1013. /* Can't do anything about it except throw an error. */
  1014. task->tk_status = -EIO;
  1015. }
  1016. return 0;
  1017. }
  1018. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1019. void nfs_commitdata_release(void *data)
  1020. {
  1021. struct nfs_write_data *wdata = data;
  1022. put_nfs_open_context(wdata->args.context);
  1023. nfs_commit_free(wdata);
  1024. }
  1025. /*
  1026. * Set up the argument/result storage required for the RPC call.
  1027. */
  1028. static int nfs_commit_rpcsetup(struct list_head *head,
  1029. struct nfs_write_data *data,
  1030. int how)
  1031. {
  1032. struct nfs_page *first = nfs_list_entry(head->next);
  1033. struct inode *inode = first->wb_context->path.dentry->d_inode;
  1034. int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  1035. int priority = flush_task_priority(how);
  1036. struct rpc_task *task;
  1037. struct rpc_message msg = {
  1038. .rpc_argp = &data->args,
  1039. .rpc_resp = &data->res,
  1040. .rpc_cred = first->wb_context->cred,
  1041. };
  1042. struct rpc_task_setup task_setup_data = {
  1043. .task = &data->task,
  1044. .rpc_client = NFS_CLIENT(inode),
  1045. .rpc_message = &msg,
  1046. .callback_ops = &nfs_commit_ops,
  1047. .callback_data = data,
  1048. .workqueue = nfsiod_workqueue,
  1049. .flags = flags,
  1050. .priority = priority,
  1051. };
  1052. /* Set up the RPC argument and reply structs
  1053. * NB: take care not to mess about with data->commit et al. */
  1054. list_splice_init(head, &data->pages);
  1055. data->inode = inode;
  1056. data->cred = msg.rpc_cred;
  1057. data->args.fh = NFS_FH(data->inode);
  1058. /* Note: we always request a commit of the entire inode */
  1059. data->args.offset = 0;
  1060. data->args.count = 0;
  1061. data->args.context = get_nfs_open_context(first->wb_context);
  1062. data->res.count = 0;
  1063. data->res.fattr = &data->fattr;
  1064. data->res.verf = &data->verf;
  1065. nfs_fattr_init(&data->fattr);
  1066. /* Set up the initial task struct. */
  1067. NFS_PROTO(inode)->commit_setup(data, &msg);
  1068. dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
  1069. task = rpc_run_task(&task_setup_data);
  1070. if (IS_ERR(task))
  1071. return PTR_ERR(task);
  1072. rpc_put_task(task);
  1073. return 0;
  1074. }
  1075. /*
  1076. * Commit dirty pages
  1077. */
  1078. static int
  1079. nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1080. {
  1081. struct nfs_write_data *data;
  1082. struct nfs_page *req;
  1083. data = nfs_commitdata_alloc();
  1084. if (!data)
  1085. goto out_bad;
  1086. /* Set up the argument struct */
  1087. return nfs_commit_rpcsetup(head, data, how);
  1088. out_bad:
  1089. while (!list_empty(head)) {
  1090. req = nfs_list_entry(head->next);
  1091. nfs_list_remove_request(req);
  1092. nfs_mark_request_commit(req);
  1093. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  1094. dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
  1095. BDI_RECLAIMABLE);
  1096. nfs_clear_page_tag_locked(req);
  1097. }
  1098. return -ENOMEM;
  1099. }
  1100. /*
  1101. * COMMIT call returned
  1102. */
  1103. static void nfs_commit_done(struct rpc_task *task, void *calldata)
  1104. {
  1105. struct nfs_write_data *data = calldata;
  1106. dprintk("NFS: %5u nfs_commit_done (status %d)\n",
  1107. task->tk_pid, task->tk_status);
  1108. /* Call the NFS version-specific code */
  1109. if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
  1110. return;
  1111. }
  1112. static void nfs_commit_release(void *calldata)
  1113. {
  1114. struct nfs_write_data *data = calldata;
  1115. struct nfs_page *req;
  1116. int status = data->task.tk_status;
  1117. while (!list_empty(&data->pages)) {
  1118. req = nfs_list_entry(data->pages.next);
  1119. nfs_list_remove_request(req);
  1120. clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
  1121. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  1122. dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
  1123. BDI_RECLAIMABLE);
  1124. dprintk("NFS: commit (%s/%Ld %d@%Ld)",
  1125. req->wb_context->path.dentry->d_inode->i_sb->s_id,
  1126. (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
  1127. req->wb_bytes,
  1128. (long long)req_offset(req));
  1129. if (status < 0) {
  1130. nfs_context_set_write_error(req->wb_context, status);
  1131. nfs_inode_remove_request(req);
  1132. dprintk(", error = %d\n", status);
  1133. goto next;
  1134. }
  1135. /* Okay, COMMIT succeeded, apparently. Check the verifier
  1136. * returned by the server against all stored verfs. */
  1137. if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
  1138. /* We have a match */
  1139. nfs_inode_remove_request(req);
  1140. dprintk(" OK\n");
  1141. goto next;
  1142. }
  1143. /* We have a mismatch. Write the page again */
  1144. dprintk(" mismatch\n");
  1145. nfs_mark_request_dirty(req);
  1146. next:
  1147. nfs_clear_page_tag_locked(req);
  1148. }
  1149. nfs_commitdata_release(calldata);
  1150. }
  1151. static const struct rpc_call_ops nfs_commit_ops = {
  1152. .rpc_call_done = nfs_commit_done,
  1153. .rpc_release = nfs_commit_release,
  1154. };
  1155. int nfs_commit_inode(struct inode *inode, int how)
  1156. {
  1157. LIST_HEAD(head);
  1158. int res;
  1159. spin_lock(&inode->i_lock);
  1160. res = nfs_scan_commit(inode, &head, 0, 0);
  1161. spin_unlock(&inode->i_lock);
  1162. if (res) {
  1163. int error = nfs_commit_list(inode, &head, how);
  1164. if (error < 0)
  1165. return error;
  1166. }
  1167. return res;
  1168. }
  1169. #else
  1170. static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1171. {
  1172. return 0;
  1173. }
  1174. #endif
  1175. long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
  1176. {
  1177. struct inode *inode = mapping->host;
  1178. pgoff_t idx_start, idx_end;
  1179. unsigned int npages = 0;
  1180. LIST_HEAD(head);
  1181. int nocommit = how & FLUSH_NOCOMMIT;
  1182. long pages, ret;
  1183. /* FIXME */
  1184. if (wbc->range_cyclic)
  1185. idx_start = 0;
  1186. else {
  1187. idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
  1188. idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
  1189. if (idx_end > idx_start) {
  1190. pgoff_t l_npages = 1 + idx_end - idx_start;
  1191. npages = l_npages;
  1192. if (sizeof(npages) != sizeof(l_npages) &&
  1193. (pgoff_t)npages != l_npages)
  1194. npages = 0;
  1195. }
  1196. }
  1197. how &= ~FLUSH_NOCOMMIT;
  1198. spin_lock(&inode->i_lock);
  1199. do {
  1200. ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
  1201. if (ret != 0)
  1202. continue;
  1203. if (nocommit)
  1204. break;
  1205. pages = nfs_scan_commit(inode, &head, idx_start, npages);
  1206. if (pages == 0)
  1207. break;
  1208. if (how & FLUSH_INVALIDATE) {
  1209. spin_unlock(&inode->i_lock);
  1210. nfs_cancel_commit_list(&head);
  1211. ret = pages;
  1212. spin_lock(&inode->i_lock);
  1213. continue;
  1214. }
  1215. pages += nfs_scan_commit(inode, &head, 0, 0);
  1216. spin_unlock(&inode->i_lock);
  1217. ret = nfs_commit_list(inode, &head, how);
  1218. spin_lock(&inode->i_lock);
  1219. } while (ret >= 0);
  1220. spin_unlock(&inode->i_lock);
  1221. return ret;
  1222. }
  1223. static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
  1224. {
  1225. int ret;
  1226. ret = nfs_writepages(mapping, wbc);
  1227. if (ret < 0)
  1228. goto out;
  1229. ret = nfs_sync_mapping_wait(mapping, wbc, how);
  1230. if (ret < 0)
  1231. goto out;
  1232. return 0;
  1233. out:
  1234. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  1235. return ret;
  1236. }
  1237. /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
  1238. static int nfs_write_mapping(struct address_space *mapping, int how)
  1239. {
  1240. struct writeback_control wbc = {
  1241. .bdi = mapping->backing_dev_info,
  1242. .sync_mode = WB_SYNC_NONE,
  1243. .nr_to_write = LONG_MAX,
  1244. .for_writepages = 1,
  1245. .range_cyclic = 1,
  1246. };
  1247. int ret;
  1248. ret = __nfs_write_mapping(mapping, &wbc, how);
  1249. if (ret < 0)
  1250. return ret;
  1251. wbc.sync_mode = WB_SYNC_ALL;
  1252. return __nfs_write_mapping(mapping, &wbc, how);
  1253. }
  1254. /*
  1255. * flush the inode to disk.
  1256. */
  1257. int nfs_wb_all(struct inode *inode)
  1258. {
  1259. return nfs_write_mapping(inode->i_mapping, 0);
  1260. }
  1261. int nfs_wb_nocommit(struct inode *inode)
  1262. {
  1263. return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
  1264. }
  1265. int nfs_wb_page_cancel(struct inode *inode, struct page *page)
  1266. {
  1267. struct nfs_page *req;
  1268. loff_t range_start = page_offset(page);
  1269. loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
  1270. struct writeback_control wbc = {
  1271. .bdi = page->mapping->backing_dev_info,
  1272. .sync_mode = WB_SYNC_ALL,
  1273. .nr_to_write = LONG_MAX,
  1274. .range_start = range_start,
  1275. .range_end = range_end,
  1276. };
  1277. int ret = 0;
  1278. BUG_ON(!PageLocked(page));
  1279. for (;;) {
  1280. req = nfs_page_find_request(page);
  1281. if (req == NULL)
  1282. goto out;
  1283. if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
  1284. nfs_release_request(req);
  1285. break;
  1286. }
  1287. if (nfs_lock_request_dontget(req)) {
  1288. nfs_inode_remove_request(req);
  1289. /*
  1290. * In case nfs_inode_remove_request has marked the
  1291. * page as being dirty
  1292. */
  1293. cancel_dirty_page(page, PAGE_CACHE_SIZE);
  1294. nfs_unlock_request(req);
  1295. break;
  1296. }
  1297. ret = nfs_wait_on_request(req);
  1298. if (ret < 0)
  1299. goto out;
  1300. }
  1301. if (!PagePrivate(page))
  1302. return 0;
  1303. ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
  1304. out:
  1305. return ret;
  1306. }
  1307. static int nfs_wb_page_priority(struct inode *inode, struct page *page,
  1308. int how)
  1309. {
  1310. loff_t range_start = page_offset(page);
  1311. loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
  1312. struct writeback_control wbc = {
  1313. .bdi = page->mapping->backing_dev_info,
  1314. .sync_mode = WB_SYNC_ALL,
  1315. .nr_to_write = LONG_MAX,
  1316. .range_start = range_start,
  1317. .range_end = range_end,
  1318. };
  1319. int ret;
  1320. do {
  1321. if (clear_page_dirty_for_io(page)) {
  1322. ret = nfs_writepage_locked(page, &wbc);
  1323. if (ret < 0)
  1324. goto out_error;
  1325. } else if (!PagePrivate(page))
  1326. break;
  1327. ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
  1328. if (ret < 0)
  1329. goto out_error;
  1330. } while (PagePrivate(page));
  1331. return 0;
  1332. out_error:
  1333. __mark_inode_dirty(inode, I_DIRTY_PAGES);
  1334. return ret;
  1335. }
  1336. /*
  1337. * Write back all requests on one page - we do this before reading it.
  1338. */
  1339. int nfs_wb_page(struct inode *inode, struct page* page)
  1340. {
  1341. return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
  1342. }
  1343. int __init nfs_init_writepagecache(void)
  1344. {
  1345. nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
  1346. sizeof(struct nfs_write_data),
  1347. 0, SLAB_HWCACHE_ALIGN,
  1348. NULL);
  1349. if (nfs_wdata_cachep == NULL)
  1350. return -ENOMEM;
  1351. nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
  1352. nfs_wdata_cachep);
  1353. if (nfs_wdata_mempool == NULL)
  1354. return -ENOMEM;
  1355. nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
  1356. nfs_wdata_cachep);
  1357. if (nfs_commit_mempool == NULL)
  1358. return -ENOMEM;
  1359. /*
  1360. * NFS congestion size, scale with available memory.
  1361. *
  1362. * 64MB: 8192k
  1363. * 128MB: 11585k
  1364. * 256MB: 16384k
  1365. * 512MB: 23170k
  1366. * 1GB: 32768k
  1367. * 2GB: 46340k
  1368. * 4GB: 65536k
  1369. * 8GB: 92681k
  1370. * 16GB: 131072k
  1371. *
  1372. * This allows larger machines to have larger/more transfers.
  1373. * Limit the default to 256M
  1374. */
  1375. nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
  1376. if (nfs_congestion_kb > 256*1024)
  1377. nfs_congestion_kb = 256*1024;
  1378. return 0;
  1379. }
  1380. void nfs_destroy_writepagecache(void)
  1381. {
  1382. mempool_destroy(nfs_commit_mempool);
  1383. mempool_destroy(nfs_wdata_mempool);
  1384. kmem_cache_destroy(nfs_wdata_cachep);
  1385. }