write.c 40 KB

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