write.c 38 KB

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