write.c 38 KB

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