write.c 40 KB

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