write.c 38 KB

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