write.c 40 KB

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