write.c 38 KB

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