write.c 37 KB

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