write.c 38 KB

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