write.c 40 KB

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