write.c 40 KB

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