write.c 41 KB

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