write.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  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_CLEAN, &req->wb_flags)) {
  210. spin_unlock(&inode->i_lock);
  211. BUG();
  212. }
  213. if (nfs_set_page_writeback(page) != 0) {
  214. spin_unlock(&inode->i_lock);
  215. BUG();
  216. }
  217. spin_unlock(&inode->i_lock);
  218. if (!nfs_pageio_add_request(pgio, req)) {
  219. nfs_redirty_request(req);
  220. return pgio->pg_error;
  221. }
  222. return 0;
  223. }
  224. static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
  225. {
  226. struct inode *inode = page->mapping->host;
  227. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
  228. nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
  229. nfs_pageio_cond_complete(pgio, page->index);
  230. return nfs_page_async_flush(pgio, page);
  231. }
  232. /*
  233. * Write an mmapped page to the server.
  234. */
  235. static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
  236. {
  237. struct nfs_pageio_descriptor pgio;
  238. int err;
  239. nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
  240. err = nfs_do_writepage(page, wbc, &pgio);
  241. nfs_pageio_complete(&pgio);
  242. if (err < 0)
  243. return err;
  244. if (pgio.pg_error < 0)
  245. return pgio.pg_error;
  246. return 0;
  247. }
  248. int nfs_writepage(struct page *page, struct writeback_control *wbc)
  249. {
  250. int ret;
  251. ret = nfs_writepage_locked(page, wbc);
  252. unlock_page(page);
  253. return ret;
  254. }
  255. static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
  256. {
  257. int ret;
  258. ret = nfs_do_writepage(page, wbc, data);
  259. unlock_page(page);
  260. return ret;
  261. }
  262. int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  263. {
  264. struct inode *inode = mapping->host;
  265. struct nfs_pageio_descriptor pgio;
  266. int err;
  267. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
  268. nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
  269. err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
  270. nfs_pageio_complete(&pgio);
  271. if (err < 0)
  272. return err;
  273. if (pgio.pg_error < 0)
  274. return pgio.pg_error;
  275. return 0;
  276. }
  277. /*
  278. * Insert a write request into an inode
  279. */
  280. static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
  281. {
  282. struct nfs_inode *nfsi = NFS_I(inode);
  283. int error;
  284. error = radix_tree_preload(GFP_NOFS);
  285. if (error != 0)
  286. goto out;
  287. /* Lock the request! */
  288. nfs_lock_request_dontget(req);
  289. spin_lock(&inode->i_lock);
  290. error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
  291. BUG_ON(error);
  292. if (!nfsi->npages) {
  293. igrab(inode);
  294. if (nfs_have_delegation(inode, FMODE_WRITE))
  295. nfsi->change_attr++;
  296. }
  297. SetPagePrivate(req->wb_page);
  298. set_page_private(req->wb_page, (unsigned long)req);
  299. nfsi->npages++;
  300. kref_get(&req->wb_kref);
  301. radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
  302. NFS_PAGE_TAG_LOCKED);
  303. spin_unlock(&inode->i_lock);
  304. radix_tree_preload_end();
  305. out:
  306. return error;
  307. }
  308. /*
  309. * Remove a write request from an inode
  310. */
  311. static void nfs_inode_remove_request(struct nfs_page *req)
  312. {
  313. struct inode *inode = req->wb_context->path.dentry->d_inode;
  314. struct nfs_inode *nfsi = NFS_I(inode);
  315. BUG_ON (!NFS_WBACK_BUSY(req));
  316. spin_lock(&inode->i_lock);
  317. set_page_private(req->wb_page, 0);
  318. ClearPagePrivate(req->wb_page);
  319. radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
  320. nfsi->npages--;
  321. if (!nfsi->npages) {
  322. spin_unlock(&inode->i_lock);
  323. iput(inode);
  324. } else
  325. spin_unlock(&inode->i_lock);
  326. nfs_clear_request(req);
  327. nfs_release_request(req);
  328. }
  329. static void
  330. nfs_mark_request_dirty(struct nfs_page *req)
  331. {
  332. __set_page_dirty_nobuffers(req->wb_page);
  333. }
  334. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  335. /*
  336. * Add a request to the inode's commit list.
  337. */
  338. static void
  339. nfs_mark_request_commit(struct nfs_page *req)
  340. {
  341. struct inode *inode = req->wb_context->path.dentry->d_inode;
  342. struct nfs_inode *nfsi = NFS_I(inode);
  343. spin_lock(&inode->i_lock);
  344. nfsi->ncommit++;
  345. set_bit(PG_CLEAN, &(req)->wb_flags);
  346. radix_tree_tag_set(&nfsi->nfs_page_tree,
  347. req->wb_index,
  348. NFS_PAGE_TAG_COMMIT);
  349. spin_unlock(&inode->i_lock);
  350. inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  351. inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
  352. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  353. }
  354. static int
  355. nfs_clear_request_commit(struct nfs_page *req)
  356. {
  357. struct page *page = req->wb_page;
  358. if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
  359. dec_zone_page_state(page, NR_UNSTABLE_NFS);
  360. dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
  361. return 1;
  362. }
  363. return 0;
  364. }
  365. static inline
  366. int nfs_write_need_commit(struct nfs_write_data *data)
  367. {
  368. return data->verf.committed != NFS_FILE_SYNC;
  369. }
  370. static inline
  371. int nfs_reschedule_unstable_write(struct nfs_page *req)
  372. {
  373. if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
  374. nfs_mark_request_commit(req);
  375. return 1;
  376. }
  377. if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
  378. nfs_mark_request_dirty(req);
  379. return 1;
  380. }
  381. return 0;
  382. }
  383. #else
  384. static inline void
  385. nfs_mark_request_commit(struct nfs_page *req)
  386. {
  387. }
  388. static inline int
  389. nfs_clear_request_commit(struct nfs_page *req)
  390. {
  391. return 0;
  392. }
  393. static inline
  394. int nfs_write_need_commit(struct nfs_write_data *data)
  395. {
  396. return 0;
  397. }
  398. static inline
  399. int nfs_reschedule_unstable_write(struct nfs_page *req)
  400. {
  401. return 0;
  402. }
  403. #endif
  404. /*
  405. * Wait for a request to complete.
  406. *
  407. * Interruptible by fatal signals only.
  408. */
  409. static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
  410. {
  411. struct nfs_inode *nfsi = NFS_I(inode);
  412. struct nfs_page *req;
  413. pgoff_t idx_end, next;
  414. unsigned int res = 0;
  415. int error;
  416. if (npages == 0)
  417. idx_end = ~0;
  418. else
  419. idx_end = idx_start + npages - 1;
  420. next = idx_start;
  421. while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
  422. if (req->wb_index > idx_end)
  423. break;
  424. next = req->wb_index + 1;
  425. BUG_ON(!NFS_WBACK_BUSY(req));
  426. kref_get(&req->wb_kref);
  427. spin_unlock(&inode->i_lock);
  428. error = nfs_wait_on_request(req);
  429. nfs_release_request(req);
  430. spin_lock(&inode->i_lock);
  431. if (error < 0)
  432. return error;
  433. res++;
  434. }
  435. return res;
  436. }
  437. static void nfs_cancel_commit_list(struct list_head *head)
  438. {
  439. struct nfs_page *req;
  440. while(!list_empty(head)) {
  441. req = nfs_list_entry(head->next);
  442. nfs_list_remove_request(req);
  443. nfs_clear_request_commit(req);
  444. nfs_inode_remove_request(req);
  445. nfs_unlock_request(req);
  446. }
  447. }
  448. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  449. /*
  450. * nfs_scan_commit - Scan an inode for commit requests
  451. * @inode: NFS inode to scan
  452. * @dst: destination list
  453. * @idx_start: lower bound of page->index to scan.
  454. * @npages: idx_start + npages sets the upper bound to scan.
  455. *
  456. * Moves requests from the inode's 'commit' request list.
  457. * The requests are *not* checked to ensure that they form a contiguous set.
  458. */
  459. static int
  460. nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
  461. {
  462. struct nfs_inode *nfsi = NFS_I(inode);
  463. int res = 0;
  464. if (nfsi->ncommit != 0) {
  465. res = nfs_scan_list(nfsi, dst, idx_start, npages,
  466. NFS_PAGE_TAG_COMMIT);
  467. nfsi->ncommit -= res;
  468. }
  469. return res;
  470. }
  471. #else
  472. static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
  473. {
  474. return 0;
  475. }
  476. #endif
  477. /*
  478. * Search for an existing write request, and attempt to update
  479. * it to reflect a new dirty region on a given page.
  480. *
  481. * If the attempt fails, then the existing request is flushed out
  482. * to disk.
  483. */
  484. static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
  485. struct page *page,
  486. unsigned int offset,
  487. unsigned int bytes)
  488. {
  489. struct nfs_page *req;
  490. unsigned int rqend;
  491. unsigned int end;
  492. int error;
  493. if (!PagePrivate(page))
  494. return NULL;
  495. end = offset + bytes;
  496. spin_lock(&inode->i_lock);
  497. for (;;) {
  498. req = nfs_page_find_request_locked(page);
  499. if (req == NULL)
  500. goto out_unlock;
  501. rqend = req->wb_offset + req->wb_bytes;
  502. /*
  503. * Tell the caller to flush out the request if
  504. * the offsets are non-contiguous.
  505. * Note: nfs_flush_incompatible() will already
  506. * have flushed out requests having wrong owners.
  507. */
  508. if (offset > rqend
  509. || end < req->wb_offset)
  510. goto out_flushme;
  511. if (nfs_set_page_tag_locked(req))
  512. break;
  513. /* The request is locked, so wait and then retry */
  514. spin_unlock(&inode->i_lock);
  515. error = nfs_wait_on_request(req);
  516. nfs_release_request(req);
  517. if (error != 0)
  518. goto out_err;
  519. spin_lock(&inode->i_lock);
  520. }
  521. if (nfs_clear_request_commit(req))
  522. radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
  523. req->wb_index, NFS_PAGE_TAG_COMMIT);
  524. /* Okay, the request matches. Update the region */
  525. if (offset < req->wb_offset) {
  526. req->wb_offset = offset;
  527. req->wb_pgbase = offset;
  528. }
  529. if (end > rqend)
  530. req->wb_bytes = end - req->wb_offset;
  531. else
  532. req->wb_bytes = rqend - req->wb_offset;
  533. out_unlock:
  534. spin_unlock(&inode->i_lock);
  535. return req;
  536. out_flushme:
  537. spin_unlock(&inode->i_lock);
  538. nfs_release_request(req);
  539. error = nfs_wb_page(inode, page);
  540. out_err:
  541. return ERR_PTR(error);
  542. }
  543. /*
  544. * Try to update an existing write request, or create one if there is none.
  545. *
  546. * Note: Should always be called with the Page Lock held to prevent races
  547. * if we have to add a new request. Also assumes that the caller has
  548. * already called nfs_flush_incompatible() if necessary.
  549. */
  550. static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
  551. struct page *page, unsigned int offset, unsigned int bytes)
  552. {
  553. struct inode *inode = page->mapping->host;
  554. struct nfs_page *req;
  555. int error;
  556. req = nfs_try_to_update_request(inode, page, offset, bytes);
  557. if (req != NULL)
  558. goto out;
  559. req = nfs_create_request(ctx, inode, page, offset, bytes);
  560. if (IS_ERR(req))
  561. goto out;
  562. error = nfs_inode_add_request(inode, req);
  563. if (error != 0) {
  564. nfs_release_request(req);
  565. req = ERR_PTR(error);
  566. }
  567. out:
  568. return req;
  569. }
  570. static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
  571. unsigned int offset, unsigned int count)
  572. {
  573. struct nfs_page *req;
  574. req = nfs_setup_write_request(ctx, page, offset, count);
  575. if (IS_ERR(req))
  576. return PTR_ERR(req);
  577. /* Update file length */
  578. nfs_grow_file(page, offset, count);
  579. nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
  580. nfs_clear_page_tag_locked(req);
  581. return 0;
  582. }
  583. int nfs_flush_incompatible(struct file *file, struct page *page)
  584. {
  585. struct nfs_open_context *ctx = nfs_file_open_context(file);
  586. struct nfs_page *req;
  587. int do_flush, status;
  588. /*
  589. * Look for a request corresponding to this page. If there
  590. * is one, and it belongs to another file, we flush it out
  591. * before we try to copy anything into the page. Do this
  592. * due to the lack of an ACCESS-type call in NFSv2.
  593. * Also do the same if we find a request from an existing
  594. * dropped page.
  595. */
  596. do {
  597. req = nfs_page_find_request(page);
  598. if (req == NULL)
  599. return 0;
  600. do_flush = req->wb_page != page || req->wb_context != ctx;
  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. nfs_clear_request_commit(req);
  1125. dprintk("NFS: commit (%s/%lld %d@%lld)",
  1126. req->wb_context->path.dentry->d_inode->i_sb->s_id,
  1127. (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
  1128. req->wb_bytes,
  1129. (long long)req_offset(req));
  1130. if (status < 0) {
  1131. nfs_context_set_write_error(req->wb_context, status);
  1132. nfs_inode_remove_request(req);
  1133. dprintk(", error = %d\n", status);
  1134. goto next;
  1135. }
  1136. /* Okay, COMMIT succeeded, apparently. Check the verifier
  1137. * returned by the server against all stored verfs. */
  1138. if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
  1139. /* We have a match */
  1140. nfs_inode_remove_request(req);
  1141. dprintk(" OK\n");
  1142. goto next;
  1143. }
  1144. /* We have a mismatch. Write the page again */
  1145. dprintk(" mismatch\n");
  1146. nfs_mark_request_dirty(req);
  1147. next:
  1148. nfs_clear_page_tag_locked(req);
  1149. }
  1150. nfs_commitdata_release(calldata);
  1151. }
  1152. static const struct rpc_call_ops nfs_commit_ops = {
  1153. .rpc_call_done = nfs_commit_done,
  1154. .rpc_release = nfs_commit_release,
  1155. };
  1156. int nfs_commit_inode(struct inode *inode, int how)
  1157. {
  1158. LIST_HEAD(head);
  1159. int res;
  1160. spin_lock(&inode->i_lock);
  1161. res = nfs_scan_commit(inode, &head, 0, 0);
  1162. spin_unlock(&inode->i_lock);
  1163. if (res) {
  1164. int error = nfs_commit_list(inode, &head, how);
  1165. if (error < 0)
  1166. return error;
  1167. }
  1168. return res;
  1169. }
  1170. #else
  1171. static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1172. {
  1173. return 0;
  1174. }
  1175. #endif
  1176. long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
  1177. {
  1178. struct inode *inode = mapping->host;
  1179. pgoff_t idx_start, idx_end;
  1180. unsigned int npages = 0;
  1181. LIST_HEAD(head);
  1182. int nocommit = how & FLUSH_NOCOMMIT;
  1183. long pages, ret;
  1184. /* FIXME */
  1185. if (wbc->range_cyclic)
  1186. idx_start = 0;
  1187. else {
  1188. idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
  1189. idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
  1190. if (idx_end > idx_start) {
  1191. pgoff_t l_npages = 1 + idx_end - idx_start;
  1192. npages = l_npages;
  1193. if (sizeof(npages) != sizeof(l_npages) &&
  1194. (pgoff_t)npages != l_npages)
  1195. npages = 0;
  1196. }
  1197. }
  1198. how &= ~FLUSH_NOCOMMIT;
  1199. spin_lock(&inode->i_lock);
  1200. do {
  1201. ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
  1202. if (ret != 0)
  1203. continue;
  1204. if (nocommit)
  1205. break;
  1206. pages = nfs_scan_commit(inode, &head, idx_start, npages);
  1207. if (pages == 0)
  1208. break;
  1209. if (how & FLUSH_INVALIDATE) {
  1210. spin_unlock(&inode->i_lock);
  1211. nfs_cancel_commit_list(&head);
  1212. ret = pages;
  1213. spin_lock(&inode->i_lock);
  1214. continue;
  1215. }
  1216. pages += nfs_scan_commit(inode, &head, 0, 0);
  1217. spin_unlock(&inode->i_lock);
  1218. ret = nfs_commit_list(inode, &head, how);
  1219. spin_lock(&inode->i_lock);
  1220. } while (ret >= 0);
  1221. spin_unlock(&inode->i_lock);
  1222. return ret;
  1223. }
  1224. static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
  1225. {
  1226. int ret;
  1227. ret = nfs_writepages(mapping, wbc);
  1228. if (ret < 0)
  1229. goto out;
  1230. ret = nfs_sync_mapping_wait(mapping, wbc, how);
  1231. if (ret < 0)
  1232. goto out;
  1233. return 0;
  1234. out:
  1235. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  1236. return ret;
  1237. }
  1238. /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
  1239. static int nfs_write_mapping(struct address_space *mapping, int how)
  1240. {
  1241. struct writeback_control wbc = {
  1242. .bdi = mapping->backing_dev_info,
  1243. .sync_mode = WB_SYNC_NONE,
  1244. .nr_to_write = LONG_MAX,
  1245. .for_writepages = 1,
  1246. .range_cyclic = 1,
  1247. };
  1248. int ret;
  1249. ret = __nfs_write_mapping(mapping, &wbc, how);
  1250. if (ret < 0)
  1251. return ret;
  1252. wbc.sync_mode = WB_SYNC_ALL;
  1253. return __nfs_write_mapping(mapping, &wbc, how);
  1254. }
  1255. /*
  1256. * flush the inode to disk.
  1257. */
  1258. int nfs_wb_all(struct inode *inode)
  1259. {
  1260. return nfs_write_mapping(inode->i_mapping, 0);
  1261. }
  1262. int nfs_wb_nocommit(struct inode *inode)
  1263. {
  1264. return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
  1265. }
  1266. int nfs_wb_page_cancel(struct inode *inode, struct page *page)
  1267. {
  1268. struct nfs_page *req;
  1269. loff_t range_start = page_offset(page);
  1270. loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
  1271. struct writeback_control wbc = {
  1272. .bdi = page->mapping->backing_dev_info,
  1273. .sync_mode = WB_SYNC_ALL,
  1274. .nr_to_write = LONG_MAX,
  1275. .range_start = range_start,
  1276. .range_end = range_end,
  1277. };
  1278. int ret = 0;
  1279. BUG_ON(!PageLocked(page));
  1280. for (;;) {
  1281. req = nfs_page_find_request(page);
  1282. if (req == NULL)
  1283. goto out;
  1284. if (test_bit(PG_CLEAN, &req->wb_flags)) {
  1285. nfs_release_request(req);
  1286. break;
  1287. }
  1288. if (nfs_lock_request_dontget(req)) {
  1289. nfs_inode_remove_request(req);
  1290. /*
  1291. * In case nfs_inode_remove_request has marked the
  1292. * page as being dirty
  1293. */
  1294. cancel_dirty_page(page, PAGE_CACHE_SIZE);
  1295. nfs_unlock_request(req);
  1296. break;
  1297. }
  1298. ret = nfs_wait_on_request(req);
  1299. if (ret < 0)
  1300. goto out;
  1301. }
  1302. if (!PagePrivate(page))
  1303. return 0;
  1304. ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
  1305. out:
  1306. return ret;
  1307. }
  1308. static int nfs_wb_page_priority(struct inode *inode, struct page *page,
  1309. int how)
  1310. {
  1311. loff_t range_start = page_offset(page);
  1312. loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
  1313. struct writeback_control wbc = {
  1314. .bdi = page->mapping->backing_dev_info,
  1315. .sync_mode = WB_SYNC_ALL,
  1316. .nr_to_write = LONG_MAX,
  1317. .range_start = range_start,
  1318. .range_end = range_end,
  1319. };
  1320. int ret;
  1321. do {
  1322. if (clear_page_dirty_for_io(page)) {
  1323. ret = nfs_writepage_locked(page, &wbc);
  1324. if (ret < 0)
  1325. goto out_error;
  1326. } else if (!PagePrivate(page))
  1327. break;
  1328. ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
  1329. if (ret < 0)
  1330. goto out_error;
  1331. } while (PagePrivate(page));
  1332. return 0;
  1333. out_error:
  1334. __mark_inode_dirty(inode, I_DIRTY_PAGES);
  1335. return ret;
  1336. }
  1337. /*
  1338. * Write back all requests on one page - we do this before reading it.
  1339. */
  1340. int nfs_wb_page(struct inode *inode, struct page* page)
  1341. {
  1342. return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
  1343. }
  1344. int __init nfs_init_writepagecache(void)
  1345. {
  1346. nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
  1347. sizeof(struct nfs_write_data),
  1348. 0, SLAB_HWCACHE_ALIGN,
  1349. NULL);
  1350. if (nfs_wdata_cachep == NULL)
  1351. return -ENOMEM;
  1352. nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
  1353. nfs_wdata_cachep);
  1354. if (nfs_wdata_mempool == NULL)
  1355. return -ENOMEM;
  1356. nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
  1357. nfs_wdata_cachep);
  1358. if (nfs_commit_mempool == NULL)
  1359. return -ENOMEM;
  1360. /*
  1361. * NFS congestion size, scale with available memory.
  1362. *
  1363. * 64MB: 8192k
  1364. * 128MB: 11585k
  1365. * 256MB: 16384k
  1366. * 512MB: 23170k
  1367. * 1GB: 32768k
  1368. * 2GB: 46340k
  1369. * 4GB: 65536k
  1370. * 8GB: 92681k
  1371. * 16GB: 131072k
  1372. *
  1373. * This allows larger machines to have larger/more transfers.
  1374. * Limit the default to 256M
  1375. */
  1376. nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
  1377. if (nfs_congestion_kb > 256*1024)
  1378. nfs_congestion_kb = 256*1024;
  1379. return 0;
  1380. }
  1381. void nfs_destroy_writepagecache(void)
  1382. {
  1383. mempool_destroy(nfs_commit_mempool);
  1384. mempool_destroy(nfs_wdata_mempool);
  1385. kmem_cache_destroy(nfs_wdata_cachep);
  1386. }