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