write.c 45 KB

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