write.c 40 KB

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