write.c 47 KB

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