write.c 39 KB

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