write.c 40 KB

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