write.c 40 KB

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