write.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154
  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 <linux/freezer.h>
  23. #include <linux/wait.h>
  24. #include <linux/uaccess.h>
  25. #include "delegation.h"
  26. #include "internal.h"
  27. #include "iostat.h"
  28. #include "nfs4_fs.h"
  29. #include "fscache.h"
  30. #include "pnfs.h"
  31. #include "nfstrace.h"
  32. #define NFSDBG_FACILITY NFSDBG_PAGECACHE
  33. #define MIN_POOL_WRITE (32)
  34. #define MIN_POOL_COMMIT (4)
  35. struct nfs_io_completion {
  36. void (*complete)(void *data);
  37. void *data;
  38. struct kref refcount;
  39. };
  40. /*
  41. * Local function declarations
  42. */
  43. static void nfs_redirty_request(struct nfs_page *req);
  44. static const struct rpc_call_ops nfs_commit_ops;
  45. static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
  46. static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
  47. static const struct nfs_rw_ops nfs_rw_write_ops;
  48. static void nfs_clear_request_commit(struct nfs_page *req);
  49. static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
  50. struct inode *inode);
  51. static struct nfs_page *
  52. nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
  53. struct page *page);
  54. static struct kmem_cache *nfs_wdata_cachep;
  55. static mempool_t *nfs_wdata_mempool;
  56. static struct kmem_cache *nfs_cdata_cachep;
  57. static mempool_t *nfs_commit_mempool;
  58. struct nfs_commit_data *nfs_commitdata_alloc(bool never_fail)
  59. {
  60. struct nfs_commit_data *p;
  61. if (never_fail)
  62. p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
  63. else {
  64. /* It is OK to do some reclaim, not no safe to wait
  65. * for anything to be returned to the pool.
  66. * mempool_alloc() cannot handle that particular combination,
  67. * so we need two separate attempts.
  68. */
  69. p = mempool_alloc(nfs_commit_mempool, GFP_NOWAIT);
  70. if (!p)
  71. p = kmem_cache_alloc(nfs_cdata_cachep, GFP_NOIO |
  72. __GFP_NOWARN | __GFP_NORETRY);
  73. if (!p)
  74. return NULL;
  75. }
  76. memset(p, 0, sizeof(*p));
  77. INIT_LIST_HEAD(&p->pages);
  78. return p;
  79. }
  80. EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
  81. void nfs_commit_free(struct nfs_commit_data *p)
  82. {
  83. mempool_free(p, nfs_commit_mempool);
  84. }
  85. EXPORT_SYMBOL_GPL(nfs_commit_free);
  86. static struct nfs_pgio_header *nfs_writehdr_alloc(void)
  87. {
  88. struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO);
  89. if (p) {
  90. memset(p, 0, sizeof(*p));
  91. p->rw_mode = FMODE_WRITE;
  92. }
  93. return p;
  94. }
  95. static void nfs_writehdr_free(struct nfs_pgio_header *hdr)
  96. {
  97. mempool_free(hdr, nfs_wdata_mempool);
  98. }
  99. static struct nfs_io_completion *nfs_io_completion_alloc(gfp_t gfp_flags)
  100. {
  101. return kmalloc(sizeof(struct nfs_io_completion), gfp_flags);
  102. }
  103. static void nfs_io_completion_init(struct nfs_io_completion *ioc,
  104. void (*complete)(void *), void *data)
  105. {
  106. ioc->complete = complete;
  107. ioc->data = data;
  108. kref_init(&ioc->refcount);
  109. }
  110. static void nfs_io_completion_release(struct kref *kref)
  111. {
  112. struct nfs_io_completion *ioc = container_of(kref,
  113. struct nfs_io_completion, refcount);
  114. ioc->complete(ioc->data);
  115. kfree(ioc);
  116. }
  117. static void nfs_io_completion_get(struct nfs_io_completion *ioc)
  118. {
  119. if (ioc != NULL)
  120. kref_get(&ioc->refcount);
  121. }
  122. static void nfs_io_completion_put(struct nfs_io_completion *ioc)
  123. {
  124. if (ioc != NULL)
  125. kref_put(&ioc->refcount, nfs_io_completion_release);
  126. }
  127. static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
  128. {
  129. ctx->error = error;
  130. smp_wmb();
  131. set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  132. }
  133. /*
  134. * nfs_page_find_head_request_locked - find head request associated with @page
  135. *
  136. * must be called while holding the inode lock.
  137. *
  138. * returns matching head request with reference held, or NULL if not found.
  139. */
  140. static struct nfs_page *
  141. nfs_page_find_head_request_locked(struct nfs_inode *nfsi, struct page *page)
  142. {
  143. struct nfs_page *req = NULL;
  144. if (PagePrivate(page))
  145. req = (struct nfs_page *)page_private(page);
  146. else if (unlikely(PageSwapCache(page)))
  147. req = nfs_page_search_commits_for_head_request_locked(nfsi,
  148. page);
  149. if (req) {
  150. WARN_ON_ONCE(req->wb_head != req);
  151. kref_get(&req->wb_kref);
  152. }
  153. return req;
  154. }
  155. /*
  156. * nfs_page_find_head_request - find head request associated with @page
  157. *
  158. * returns matching head request with reference held, or NULL if not found.
  159. */
  160. static struct nfs_page *nfs_page_find_head_request(struct page *page)
  161. {
  162. struct inode *inode = page_file_mapping(page)->host;
  163. struct nfs_page *req = NULL;
  164. spin_lock(&inode->i_lock);
  165. req = nfs_page_find_head_request_locked(NFS_I(inode), page);
  166. spin_unlock(&inode->i_lock);
  167. return req;
  168. }
  169. /* Adjust the file length if we're writing beyond the end */
  170. static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
  171. {
  172. struct inode *inode = page_file_mapping(page)->host;
  173. loff_t end, i_size;
  174. pgoff_t end_index;
  175. spin_lock(&inode->i_lock);
  176. i_size = i_size_read(inode);
  177. end_index = (i_size - 1) >> PAGE_SHIFT;
  178. if (i_size > 0 && page_index(page) < end_index)
  179. goto out;
  180. end = page_file_offset(page) + ((loff_t)offset+count);
  181. if (i_size >= end)
  182. goto out;
  183. i_size_write(inode, end);
  184. nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
  185. out:
  186. spin_unlock(&inode->i_lock);
  187. }
  188. /* A writeback failed: mark the page as bad, and invalidate the page cache */
  189. static void nfs_set_pageerror(struct page *page)
  190. {
  191. nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
  192. }
  193. /*
  194. * nfs_page_group_search_locked
  195. * @head - head request of page group
  196. * @page_offset - offset into page
  197. *
  198. * Search page group with head @head to find a request that contains the
  199. * page offset @page_offset.
  200. *
  201. * Returns a pointer to the first matching nfs request, or NULL if no
  202. * match is found.
  203. *
  204. * Must be called with the page group lock held
  205. */
  206. static struct nfs_page *
  207. nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
  208. {
  209. struct nfs_page *req;
  210. WARN_ON_ONCE(head != head->wb_head);
  211. WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_head->wb_flags));
  212. req = head;
  213. do {
  214. if (page_offset >= req->wb_pgbase &&
  215. page_offset < (req->wb_pgbase + req->wb_bytes))
  216. return req;
  217. req = req->wb_this_page;
  218. } while (req != head);
  219. return NULL;
  220. }
  221. /*
  222. * nfs_page_group_covers_page
  223. * @head - head request of page group
  224. *
  225. * Return true if the page group with head @head covers the whole page,
  226. * returns false otherwise
  227. */
  228. static bool nfs_page_group_covers_page(struct nfs_page *req)
  229. {
  230. struct nfs_page *tmp;
  231. unsigned int pos = 0;
  232. unsigned int len = nfs_page_length(req->wb_page);
  233. nfs_page_group_lock(req, false);
  234. do {
  235. tmp = nfs_page_group_search_locked(req->wb_head, pos);
  236. if (tmp) {
  237. /* no way this should happen */
  238. WARN_ON_ONCE(tmp->wb_pgbase != pos);
  239. pos += tmp->wb_bytes - (pos - tmp->wb_pgbase);
  240. }
  241. } while (tmp && pos < len);
  242. nfs_page_group_unlock(req);
  243. WARN_ON_ONCE(pos > len);
  244. return pos == len;
  245. }
  246. /* We can set the PG_uptodate flag if we see that a write request
  247. * covers the full page.
  248. */
  249. static void nfs_mark_uptodate(struct nfs_page *req)
  250. {
  251. if (PageUptodate(req->wb_page))
  252. return;
  253. if (!nfs_page_group_covers_page(req))
  254. return;
  255. SetPageUptodate(req->wb_page);
  256. }
  257. static int wb_priority(struct writeback_control *wbc)
  258. {
  259. int ret = 0;
  260. if (wbc->sync_mode == WB_SYNC_ALL)
  261. ret = FLUSH_COND_STABLE;
  262. return ret;
  263. }
  264. /*
  265. * NFS congestion control
  266. */
  267. int nfs_congestion_kb;
  268. #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
  269. #define NFS_CONGESTION_OFF_THRESH \
  270. (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
  271. static void nfs_set_page_writeback(struct page *page)
  272. {
  273. struct inode *inode = page_file_mapping(page)->host;
  274. struct nfs_server *nfss = NFS_SERVER(inode);
  275. int ret = test_set_page_writeback(page);
  276. WARN_ON_ONCE(ret != 0);
  277. if (atomic_long_inc_return(&nfss->writeback) >
  278. NFS_CONGESTION_ON_THRESH)
  279. set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
  280. }
  281. static void nfs_end_page_writeback(struct nfs_page *req)
  282. {
  283. struct inode *inode = page_file_mapping(req->wb_page)->host;
  284. struct nfs_server *nfss = NFS_SERVER(inode);
  285. if (!nfs_page_group_sync_on_bit(req, PG_WB_END))
  286. return;
  287. end_page_writeback(req->wb_page);
  288. if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
  289. clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
  290. }
  291. /* nfs_page_group_clear_bits
  292. * @req - an nfs request
  293. * clears all page group related bits from @req
  294. */
  295. static void
  296. nfs_page_group_clear_bits(struct nfs_page *req)
  297. {
  298. clear_bit(PG_TEARDOWN, &req->wb_flags);
  299. clear_bit(PG_UNLOCKPAGE, &req->wb_flags);
  300. clear_bit(PG_UPTODATE, &req->wb_flags);
  301. clear_bit(PG_WB_END, &req->wb_flags);
  302. clear_bit(PG_REMOVE, &req->wb_flags);
  303. }
  304. /*
  305. * nfs_unroll_locks_and_wait - unlock all newly locked reqs and wait on @req
  306. *
  307. * this is a helper function for nfs_lock_and_join_requests
  308. *
  309. * @inode - inode associated with request page group, must be holding inode lock
  310. * @head - head request of page group, must be holding head lock
  311. * @req - request that couldn't lock and needs to wait on the req bit lock
  312. *
  313. * NOTE: this must be called holding page_group bit lock and inode spin lock
  314. * and BOTH will be released before returning.
  315. *
  316. * returns 0 on success, < 0 on error.
  317. */
  318. static int
  319. nfs_unroll_locks_and_wait(struct inode *inode, struct nfs_page *head,
  320. struct nfs_page *req)
  321. __releases(&inode->i_lock)
  322. {
  323. struct nfs_page *tmp;
  324. int ret;
  325. /* relinquish all the locks successfully grabbed this run */
  326. for (tmp = head ; tmp != req; tmp = tmp->wb_this_page)
  327. nfs_unlock_request(tmp);
  328. WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
  329. /* grab a ref on the request that will be waited on */
  330. kref_get(&req->wb_kref);
  331. nfs_page_group_unlock(head);
  332. spin_unlock(&inode->i_lock);
  333. /* release ref from nfs_page_find_head_request_locked */
  334. nfs_release_request(head);
  335. ret = nfs_wait_on_request(req);
  336. nfs_release_request(req);
  337. return ret;
  338. }
  339. /*
  340. * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
  341. *
  342. * @destroy_list - request list (using wb_this_page) terminated by @old_head
  343. * @old_head - the old head of the list
  344. *
  345. * All subrequests must be locked and removed from all lists, so at this point
  346. * they are only "active" in this function, and possibly in nfs_wait_on_request
  347. * with a reference held by some other context.
  348. */
  349. static void
  350. nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
  351. struct nfs_page *old_head)
  352. {
  353. while (destroy_list) {
  354. struct nfs_page *subreq = destroy_list;
  355. destroy_list = (subreq->wb_this_page == old_head) ?
  356. NULL : subreq->wb_this_page;
  357. WARN_ON_ONCE(old_head != subreq->wb_head);
  358. /* make sure old group is not used */
  359. subreq->wb_head = subreq;
  360. subreq->wb_this_page = subreq;
  361. /* subreq is now totally disconnected from page group or any
  362. * write / commit lists. last chance to wake any waiters */
  363. nfs_unlock_request(subreq);
  364. if (!test_bit(PG_TEARDOWN, &subreq->wb_flags)) {
  365. /* release ref on old head request */
  366. nfs_release_request(old_head);
  367. nfs_page_group_clear_bits(subreq);
  368. /* release the PG_INODE_REF reference */
  369. if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags))
  370. nfs_release_request(subreq);
  371. else
  372. WARN_ON_ONCE(1);
  373. } else {
  374. WARN_ON_ONCE(test_bit(PG_CLEAN, &subreq->wb_flags));
  375. /* zombie requests have already released the last
  376. * reference and were waiting on the rest of the
  377. * group to complete. Since it's no longer part of a
  378. * group, simply free the request */
  379. nfs_page_group_clear_bits(subreq);
  380. nfs_free_request(subreq);
  381. }
  382. }
  383. }
  384. /*
  385. * nfs_lock_and_join_requests - join all subreqs to the head req and return
  386. * a locked reference, cancelling any pending
  387. * operations for this page.
  388. *
  389. * @page - the page used to lookup the "page group" of nfs_page structures
  390. *
  391. * This function joins all sub requests to the head request by first
  392. * locking all requests in the group, cancelling any pending operations
  393. * and finally updating the head request to cover the whole range covered by
  394. * the (former) group. All subrequests are removed from any write or commit
  395. * lists, unlinked from the group and destroyed.
  396. *
  397. * Returns a locked, referenced pointer to the head request - which after
  398. * this call is guaranteed to be the only request associated with the page.
  399. * Returns NULL if no requests are found for @page, or a ERR_PTR if an
  400. * error was encountered.
  401. */
  402. static struct nfs_page *
  403. nfs_lock_and_join_requests(struct page *page)
  404. {
  405. struct inode *inode = page_file_mapping(page)->host;
  406. struct nfs_page *head, *subreq;
  407. struct nfs_page *destroy_list = NULL;
  408. unsigned int total_bytes;
  409. int ret;
  410. try_again:
  411. total_bytes = 0;
  412. WARN_ON_ONCE(destroy_list);
  413. spin_lock(&inode->i_lock);
  414. /*
  415. * A reference is taken only on the head request which acts as a
  416. * reference to the whole page group - the group will not be destroyed
  417. * until the head reference is released.
  418. */
  419. head = nfs_page_find_head_request_locked(NFS_I(inode), page);
  420. if (!head) {
  421. spin_unlock(&inode->i_lock);
  422. return NULL;
  423. }
  424. /* holding inode lock, so always make a non-blocking call to try the
  425. * page group lock */
  426. ret = nfs_page_group_lock(head, true);
  427. if (ret < 0) {
  428. spin_unlock(&inode->i_lock);
  429. nfs_page_group_lock_wait(head);
  430. nfs_release_request(head);
  431. goto try_again;
  432. }
  433. /* lock each request in the page group */
  434. subreq = head;
  435. do {
  436. /*
  437. * Subrequests are always contiguous, non overlapping
  438. * and in order - but may be repeated (mirrored writes).
  439. */
  440. if (subreq->wb_offset == (head->wb_offset + total_bytes)) {
  441. /* keep track of how many bytes this group covers */
  442. total_bytes += subreq->wb_bytes;
  443. } else if (WARN_ON_ONCE(subreq->wb_offset < head->wb_offset ||
  444. ((subreq->wb_offset + subreq->wb_bytes) >
  445. (head->wb_offset + total_bytes)))) {
  446. nfs_page_group_unlock(head);
  447. spin_unlock(&inode->i_lock);
  448. return ERR_PTR(-EIO);
  449. }
  450. if (!nfs_lock_request(subreq)) {
  451. /* releases page group bit lock and
  452. * inode spin lock and all references */
  453. ret = nfs_unroll_locks_and_wait(inode, head,
  454. subreq);
  455. if (ret == 0)
  456. goto try_again;
  457. return ERR_PTR(ret);
  458. }
  459. subreq = subreq->wb_this_page;
  460. } while (subreq != head);
  461. /* Now that all requests are locked, make sure they aren't on any list.
  462. * Commit list removal accounting is done after locks are dropped */
  463. subreq = head;
  464. do {
  465. nfs_clear_request_commit(subreq);
  466. subreq = subreq->wb_this_page;
  467. } while (subreq != head);
  468. /* unlink subrequests from head, destroy them later */
  469. if (head->wb_this_page != head) {
  470. /* destroy list will be terminated by head */
  471. destroy_list = head->wb_this_page;
  472. head->wb_this_page = head;
  473. /* change head request to cover whole range that
  474. * the former page group covered */
  475. head->wb_bytes = total_bytes;
  476. }
  477. /*
  478. * prepare head request to be added to new pgio descriptor
  479. */
  480. nfs_page_group_clear_bits(head);
  481. /*
  482. * some part of the group was still on the inode list - otherwise
  483. * the group wouldn't be involved in async write.
  484. * grab a reference for the head request, iff it needs one.
  485. */
  486. if (!test_and_set_bit(PG_INODE_REF, &head->wb_flags))
  487. kref_get(&head->wb_kref);
  488. nfs_page_group_unlock(head);
  489. /* drop lock to clean uprequests on destroy list */
  490. spin_unlock(&inode->i_lock);
  491. nfs_destroy_unlinked_subrequests(destroy_list, head);
  492. /* still holds ref on head from nfs_page_find_head_request_locked
  493. * and still has lock on head from lock loop */
  494. return head;
  495. }
  496. static void nfs_write_error_remove_page(struct nfs_page *req)
  497. {
  498. nfs_unlock_request(req);
  499. nfs_end_page_writeback(req);
  500. generic_error_remove_page(page_file_mapping(req->wb_page),
  501. req->wb_page);
  502. nfs_release_request(req);
  503. }
  504. static bool
  505. nfs_error_is_fatal_on_server(int err)
  506. {
  507. switch (err) {
  508. case 0:
  509. case -ERESTARTSYS:
  510. case -EINTR:
  511. return false;
  512. }
  513. return nfs_error_is_fatal(err);
  514. }
  515. /*
  516. * Find an associated nfs write request, and prepare to flush it out
  517. * May return an error if the user signalled nfs_wait_on_request().
  518. */
  519. static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
  520. struct page *page)
  521. {
  522. struct nfs_page *req;
  523. int ret = 0;
  524. req = nfs_lock_and_join_requests(page);
  525. if (!req)
  526. goto out;
  527. ret = PTR_ERR(req);
  528. if (IS_ERR(req))
  529. goto out;
  530. nfs_set_page_writeback(page);
  531. WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
  532. ret = 0;
  533. /* If there is a fatal error that covers this write, just exit */
  534. if (nfs_error_is_fatal_on_server(req->wb_context->error))
  535. goto out_launder;
  536. if (!nfs_pageio_add_request(pgio, req)) {
  537. ret = pgio->pg_error;
  538. /*
  539. * Remove the problematic req upon fatal errors on the server
  540. */
  541. if (nfs_error_is_fatal(ret)) {
  542. nfs_context_set_write_error(req->wb_context, ret);
  543. if (nfs_error_is_fatal_on_server(ret))
  544. goto out_launder;
  545. }
  546. nfs_redirty_request(req);
  547. ret = -EAGAIN;
  548. } else
  549. nfs_add_stats(page_file_mapping(page)->host,
  550. NFSIOS_WRITEPAGES, 1);
  551. out:
  552. return ret;
  553. out_launder:
  554. nfs_write_error_remove_page(req);
  555. return ret;
  556. }
  557. static int nfs_do_writepage(struct page *page, struct writeback_control *wbc,
  558. struct nfs_pageio_descriptor *pgio)
  559. {
  560. int ret;
  561. nfs_pageio_cond_complete(pgio, page_index(page));
  562. ret = nfs_page_async_flush(pgio, page);
  563. if (ret == -EAGAIN) {
  564. redirty_page_for_writepage(wbc, page);
  565. ret = 0;
  566. }
  567. return ret;
  568. }
  569. /*
  570. * Write an mmapped page to the server.
  571. */
  572. static int nfs_writepage_locked(struct page *page,
  573. struct writeback_control *wbc)
  574. {
  575. struct nfs_pageio_descriptor pgio;
  576. struct inode *inode = page_file_mapping(page)->host;
  577. int err;
  578. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
  579. nfs_pageio_init_write(&pgio, inode, 0,
  580. false, &nfs_async_write_completion_ops);
  581. err = nfs_do_writepage(page, wbc, &pgio);
  582. nfs_pageio_complete(&pgio);
  583. if (err < 0)
  584. return err;
  585. if (pgio.pg_error < 0)
  586. return pgio.pg_error;
  587. return 0;
  588. }
  589. int nfs_writepage(struct page *page, struct writeback_control *wbc)
  590. {
  591. int ret;
  592. ret = nfs_writepage_locked(page, wbc);
  593. unlock_page(page);
  594. return ret;
  595. }
  596. static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
  597. {
  598. int ret;
  599. ret = nfs_do_writepage(page, wbc, data);
  600. unlock_page(page);
  601. return ret;
  602. }
  603. static void nfs_io_completion_commit(void *inode)
  604. {
  605. nfs_commit_inode(inode, 0);
  606. }
  607. int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  608. {
  609. struct inode *inode = mapping->host;
  610. struct nfs_pageio_descriptor pgio;
  611. struct nfs_io_completion *ioc = nfs_io_completion_alloc(GFP_NOFS);
  612. int err;
  613. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
  614. if (ioc)
  615. nfs_io_completion_init(ioc, nfs_io_completion_commit, inode);
  616. nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
  617. &nfs_async_write_completion_ops);
  618. pgio.pg_io_completion = ioc;
  619. err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
  620. nfs_pageio_complete(&pgio);
  621. nfs_io_completion_put(ioc);
  622. if (err < 0)
  623. goto out_err;
  624. err = pgio.pg_error;
  625. if (err < 0)
  626. goto out_err;
  627. return 0;
  628. out_err:
  629. return err;
  630. }
  631. /*
  632. * Insert a write request into an inode
  633. */
  634. static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
  635. {
  636. struct nfs_inode *nfsi = NFS_I(inode);
  637. WARN_ON_ONCE(req->wb_this_page != req);
  638. /* Lock the request! */
  639. nfs_lock_request(req);
  640. spin_lock(&inode->i_lock);
  641. if (!nfsi->nrequests &&
  642. NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
  643. inode->i_version++;
  644. /*
  645. * Swap-space should not get truncated. Hence no need to plug the race
  646. * with invalidate/truncate.
  647. */
  648. if (likely(!PageSwapCache(req->wb_page))) {
  649. set_bit(PG_MAPPED, &req->wb_flags);
  650. SetPagePrivate(req->wb_page);
  651. set_page_private(req->wb_page, (unsigned long)req);
  652. }
  653. nfsi->nrequests++;
  654. /* this a head request for a page group - mark it as having an
  655. * extra reference so sub groups can follow suit.
  656. * This flag also informs pgio layer when to bump nrequests when
  657. * adding subrequests. */
  658. WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
  659. kref_get(&req->wb_kref);
  660. spin_unlock(&inode->i_lock);
  661. }
  662. /*
  663. * Remove a write request from an inode
  664. */
  665. static void nfs_inode_remove_request(struct nfs_page *req)
  666. {
  667. struct inode *inode = d_inode(req->wb_context->dentry);
  668. struct nfs_inode *nfsi = NFS_I(inode);
  669. struct nfs_page *head;
  670. if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
  671. head = req->wb_head;
  672. spin_lock(&inode->i_lock);
  673. if (likely(head->wb_page && !PageSwapCache(head->wb_page))) {
  674. set_page_private(head->wb_page, 0);
  675. ClearPagePrivate(head->wb_page);
  676. clear_bit(PG_MAPPED, &head->wb_flags);
  677. }
  678. nfsi->nrequests--;
  679. spin_unlock(&inode->i_lock);
  680. } else {
  681. spin_lock(&inode->i_lock);
  682. nfsi->nrequests--;
  683. spin_unlock(&inode->i_lock);
  684. }
  685. if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags))
  686. nfs_release_request(req);
  687. }
  688. static void
  689. nfs_mark_request_dirty(struct nfs_page *req)
  690. {
  691. if (req->wb_page)
  692. __set_page_dirty_nobuffers(req->wb_page);
  693. }
  694. /*
  695. * nfs_page_search_commits_for_head_request_locked
  696. *
  697. * Search through commit lists on @inode for the head request for @page.
  698. * Must be called while holding the inode (which is cinfo) lock.
  699. *
  700. * Returns the head request if found, or NULL if not found.
  701. */
  702. static struct nfs_page *
  703. nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
  704. struct page *page)
  705. {
  706. struct nfs_page *freq, *t;
  707. struct nfs_commit_info cinfo;
  708. struct inode *inode = &nfsi->vfs_inode;
  709. nfs_init_cinfo_from_inode(&cinfo, inode);
  710. /* search through pnfs commit lists */
  711. freq = pnfs_search_commit_reqs(inode, &cinfo, page);
  712. if (freq)
  713. return freq->wb_head;
  714. /* Linearly search the commit list for the correct request */
  715. list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) {
  716. if (freq->wb_page == page)
  717. return freq->wb_head;
  718. }
  719. return NULL;
  720. }
  721. /**
  722. * nfs_request_add_commit_list_locked - add request to a commit list
  723. * @req: pointer to a struct nfs_page
  724. * @dst: commit list head
  725. * @cinfo: holds list lock and accounting info
  726. *
  727. * This sets the PG_CLEAN bit, updates the cinfo count of
  728. * number of outstanding requests requiring a commit as well as
  729. * the MM page stats.
  730. *
  731. * The caller must hold cinfo->inode->i_lock, and the nfs_page lock.
  732. */
  733. void
  734. nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst,
  735. struct nfs_commit_info *cinfo)
  736. {
  737. set_bit(PG_CLEAN, &req->wb_flags);
  738. nfs_list_add_request(req, dst);
  739. cinfo->mds->ncommit++;
  740. }
  741. EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked);
  742. /**
  743. * nfs_request_add_commit_list - add request to a commit list
  744. * @req: pointer to a struct nfs_page
  745. * @dst: commit list head
  746. * @cinfo: holds list lock and accounting info
  747. *
  748. * This sets the PG_CLEAN bit, updates the cinfo count of
  749. * number of outstanding requests requiring a commit as well as
  750. * the MM page stats.
  751. *
  752. * The caller must _not_ hold the cinfo->lock, but must be
  753. * holding the nfs_page lock.
  754. */
  755. void
  756. nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo)
  757. {
  758. spin_lock(&cinfo->inode->i_lock);
  759. nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo);
  760. spin_unlock(&cinfo->inode->i_lock);
  761. if (req->wb_page)
  762. nfs_mark_page_unstable(req->wb_page, cinfo);
  763. }
  764. EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
  765. /**
  766. * nfs_request_remove_commit_list - Remove request from a commit list
  767. * @req: pointer to a nfs_page
  768. * @cinfo: holds list lock and accounting info
  769. *
  770. * This clears the PG_CLEAN bit, and updates the cinfo's count of
  771. * number of outstanding requests requiring a commit
  772. * It does not update the MM page stats.
  773. *
  774. * The caller _must_ hold the cinfo->lock and the nfs_page lock.
  775. */
  776. void
  777. nfs_request_remove_commit_list(struct nfs_page *req,
  778. struct nfs_commit_info *cinfo)
  779. {
  780. if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
  781. return;
  782. nfs_list_remove_request(req);
  783. cinfo->mds->ncommit--;
  784. }
  785. EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
  786. static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
  787. struct inode *inode)
  788. {
  789. cinfo->inode = inode;
  790. cinfo->mds = &NFS_I(inode)->commit_info;
  791. cinfo->ds = pnfs_get_ds_info(inode);
  792. cinfo->dreq = NULL;
  793. cinfo->completion_ops = &nfs_commit_completion_ops;
  794. }
  795. void nfs_init_cinfo(struct nfs_commit_info *cinfo,
  796. struct inode *inode,
  797. struct nfs_direct_req *dreq)
  798. {
  799. if (dreq)
  800. nfs_init_cinfo_from_dreq(cinfo, dreq);
  801. else
  802. nfs_init_cinfo_from_inode(cinfo, inode);
  803. }
  804. EXPORT_SYMBOL_GPL(nfs_init_cinfo);
  805. /*
  806. * Add a request to the inode's commit list.
  807. */
  808. void
  809. nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
  810. struct nfs_commit_info *cinfo, u32 ds_commit_idx)
  811. {
  812. if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx))
  813. return;
  814. nfs_request_add_commit_list(req, cinfo);
  815. }
  816. static void
  817. nfs_clear_page_commit(struct page *page)
  818. {
  819. dec_node_page_state(page, NR_UNSTABLE_NFS);
  820. dec_wb_stat(&inode_to_bdi(page_file_mapping(page)->host)->wb,
  821. WB_RECLAIMABLE);
  822. }
  823. /* Called holding inode (/cinfo) lock */
  824. static void
  825. nfs_clear_request_commit(struct nfs_page *req)
  826. {
  827. if (test_bit(PG_CLEAN, &req->wb_flags)) {
  828. struct inode *inode = d_inode(req->wb_context->dentry);
  829. struct nfs_commit_info cinfo;
  830. nfs_init_cinfo_from_inode(&cinfo, inode);
  831. if (!pnfs_clear_request_commit(req, &cinfo)) {
  832. nfs_request_remove_commit_list(req, &cinfo);
  833. }
  834. nfs_clear_page_commit(req->wb_page);
  835. }
  836. }
  837. int nfs_write_need_commit(struct nfs_pgio_header *hdr)
  838. {
  839. if (hdr->verf.committed == NFS_DATA_SYNC)
  840. return hdr->lseg == NULL;
  841. return hdr->verf.committed != NFS_FILE_SYNC;
  842. }
  843. static void nfs_async_write_init(struct nfs_pgio_header *hdr)
  844. {
  845. nfs_io_completion_get(hdr->io_completion);
  846. }
  847. static void nfs_write_completion(struct nfs_pgio_header *hdr)
  848. {
  849. struct nfs_commit_info cinfo;
  850. unsigned long bytes = 0;
  851. if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
  852. goto out;
  853. nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
  854. while (!list_empty(&hdr->pages)) {
  855. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  856. bytes += req->wb_bytes;
  857. nfs_list_remove_request(req);
  858. if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
  859. (hdr->good_bytes < bytes)) {
  860. nfs_set_pageerror(req->wb_page);
  861. nfs_context_set_write_error(req->wb_context, hdr->error);
  862. goto remove_req;
  863. }
  864. if (nfs_write_need_commit(hdr)) {
  865. memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
  866. nfs_mark_request_commit(req, hdr->lseg, &cinfo,
  867. hdr->pgio_mirror_idx);
  868. goto next;
  869. }
  870. remove_req:
  871. nfs_inode_remove_request(req);
  872. next:
  873. nfs_unlock_request(req);
  874. nfs_end_page_writeback(req);
  875. nfs_release_request(req);
  876. }
  877. out:
  878. nfs_io_completion_put(hdr->io_completion);
  879. hdr->release(hdr);
  880. }
  881. unsigned long
  882. nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
  883. {
  884. return cinfo->mds->ncommit;
  885. }
  886. /* cinfo->inode->i_lock held by caller */
  887. int
  888. nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
  889. struct nfs_commit_info *cinfo, int max)
  890. {
  891. struct nfs_page *req, *tmp;
  892. int ret = 0;
  893. list_for_each_entry_safe(req, tmp, src, wb_list) {
  894. if (!nfs_lock_request(req))
  895. continue;
  896. kref_get(&req->wb_kref);
  897. if (cond_resched_lock(&cinfo->inode->i_lock))
  898. list_safe_reset_next(req, tmp, wb_list);
  899. nfs_request_remove_commit_list(req, cinfo);
  900. nfs_list_add_request(req, dst);
  901. ret++;
  902. if ((ret == max) && !cinfo->dreq)
  903. break;
  904. }
  905. return ret;
  906. }
  907. /*
  908. * nfs_scan_commit - Scan an inode for commit requests
  909. * @inode: NFS inode to scan
  910. * @dst: mds destination list
  911. * @cinfo: mds and ds lists of reqs ready to commit
  912. *
  913. * Moves requests from the inode's 'commit' request list.
  914. * The requests are *not* checked to ensure that they form a contiguous set.
  915. */
  916. int
  917. nfs_scan_commit(struct inode *inode, struct list_head *dst,
  918. struct nfs_commit_info *cinfo)
  919. {
  920. int ret = 0;
  921. spin_lock(&cinfo->inode->i_lock);
  922. if (cinfo->mds->ncommit > 0) {
  923. const int max = INT_MAX;
  924. ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
  925. cinfo, max);
  926. ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
  927. }
  928. spin_unlock(&cinfo->inode->i_lock);
  929. return ret;
  930. }
  931. /*
  932. * Search for an existing write request, and attempt to update
  933. * it to reflect a new dirty region on a given page.
  934. *
  935. * If the attempt fails, then the existing request is flushed out
  936. * to disk.
  937. */
  938. static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
  939. struct page *page,
  940. unsigned int offset,
  941. unsigned int bytes)
  942. {
  943. struct nfs_page *req;
  944. unsigned int rqend;
  945. unsigned int end;
  946. int error;
  947. if (!PagePrivate(page))
  948. return NULL;
  949. end = offset + bytes;
  950. spin_lock(&inode->i_lock);
  951. for (;;) {
  952. req = nfs_page_find_head_request_locked(NFS_I(inode), page);
  953. if (req == NULL)
  954. goto out_unlock;
  955. /* should be handled by nfs_flush_incompatible */
  956. WARN_ON_ONCE(req->wb_head != req);
  957. WARN_ON_ONCE(req->wb_this_page != req);
  958. rqend = req->wb_offset + req->wb_bytes;
  959. /*
  960. * Tell the caller to flush out the request if
  961. * the offsets are non-contiguous.
  962. * Note: nfs_flush_incompatible() will already
  963. * have flushed out requests having wrong owners.
  964. */
  965. if (offset > rqend
  966. || end < req->wb_offset)
  967. goto out_flushme;
  968. if (nfs_lock_request(req))
  969. break;
  970. /* The request is locked, so wait and then retry */
  971. spin_unlock(&inode->i_lock);
  972. error = nfs_wait_on_request(req);
  973. nfs_release_request(req);
  974. if (error != 0)
  975. goto out_err;
  976. spin_lock(&inode->i_lock);
  977. }
  978. /* Okay, the request matches. Update the region */
  979. if (offset < req->wb_offset) {
  980. req->wb_offset = offset;
  981. req->wb_pgbase = offset;
  982. }
  983. if (end > rqend)
  984. req->wb_bytes = end - req->wb_offset;
  985. else
  986. req->wb_bytes = rqend - req->wb_offset;
  987. out_unlock:
  988. if (req)
  989. nfs_clear_request_commit(req);
  990. spin_unlock(&inode->i_lock);
  991. return req;
  992. out_flushme:
  993. spin_unlock(&inode->i_lock);
  994. nfs_release_request(req);
  995. error = nfs_wb_page(inode, page);
  996. out_err:
  997. return ERR_PTR(error);
  998. }
  999. /*
  1000. * Try to update an existing write request, or create one if there is none.
  1001. *
  1002. * Note: Should always be called with the Page Lock held to prevent races
  1003. * if we have to add a new request. Also assumes that the caller has
  1004. * already called nfs_flush_incompatible() if necessary.
  1005. */
  1006. static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
  1007. struct page *page, unsigned int offset, unsigned int bytes)
  1008. {
  1009. struct inode *inode = page_file_mapping(page)->host;
  1010. struct nfs_page *req;
  1011. req = nfs_try_to_update_request(inode, page, offset, bytes);
  1012. if (req != NULL)
  1013. goto out;
  1014. req = nfs_create_request(ctx, page, NULL, offset, bytes);
  1015. if (IS_ERR(req))
  1016. goto out;
  1017. nfs_inode_add_request(inode, req);
  1018. out:
  1019. return req;
  1020. }
  1021. static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
  1022. unsigned int offset, unsigned int count)
  1023. {
  1024. struct nfs_page *req;
  1025. req = nfs_setup_write_request(ctx, page, offset, count);
  1026. if (IS_ERR(req))
  1027. return PTR_ERR(req);
  1028. /* Update file length */
  1029. nfs_grow_file(page, offset, count);
  1030. nfs_mark_uptodate(req);
  1031. nfs_mark_request_dirty(req);
  1032. nfs_unlock_and_release_request(req);
  1033. return 0;
  1034. }
  1035. int nfs_flush_incompatible(struct file *file, struct page *page)
  1036. {
  1037. struct nfs_open_context *ctx = nfs_file_open_context(file);
  1038. struct nfs_lock_context *l_ctx;
  1039. struct file_lock_context *flctx = file_inode(file)->i_flctx;
  1040. struct nfs_page *req;
  1041. int do_flush, status;
  1042. /*
  1043. * Look for a request corresponding to this page. If there
  1044. * is one, and it belongs to another file, we flush it out
  1045. * before we try to copy anything into the page. Do this
  1046. * due to the lack of an ACCESS-type call in NFSv2.
  1047. * Also do the same if we find a request from an existing
  1048. * dropped page.
  1049. */
  1050. do {
  1051. req = nfs_page_find_head_request(page);
  1052. if (req == NULL)
  1053. return 0;
  1054. l_ctx = req->wb_lock_context;
  1055. do_flush = req->wb_page != page ||
  1056. !nfs_match_open_context(req->wb_context, ctx);
  1057. /* for now, flush if more than 1 request in page_group */
  1058. do_flush |= req->wb_this_page != req;
  1059. if (l_ctx && flctx &&
  1060. !(list_empty_careful(&flctx->flc_posix) &&
  1061. list_empty_careful(&flctx->flc_flock))) {
  1062. do_flush |= l_ctx->lockowner != current->files;
  1063. }
  1064. nfs_release_request(req);
  1065. if (!do_flush)
  1066. return 0;
  1067. status = nfs_wb_page(page_file_mapping(page)->host, page);
  1068. } while (status == 0);
  1069. return status;
  1070. }
  1071. /*
  1072. * Avoid buffered writes when a open context credential's key would
  1073. * expire soon.
  1074. *
  1075. * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
  1076. *
  1077. * Return 0 and set a credential flag which triggers the inode to flush
  1078. * and performs NFS_FILE_SYNC writes if the key will expired within
  1079. * RPC_KEY_EXPIRE_TIMEO.
  1080. */
  1081. int
  1082. nfs_key_timeout_notify(struct file *filp, struct inode *inode)
  1083. {
  1084. struct nfs_open_context *ctx = nfs_file_open_context(filp);
  1085. struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
  1086. return rpcauth_key_timeout_notify(auth, ctx->cred);
  1087. }
  1088. /*
  1089. * Test if the open context credential key is marked to expire soon.
  1090. */
  1091. bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode)
  1092. {
  1093. struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
  1094. return rpcauth_cred_key_to_expire(auth, ctx->cred);
  1095. }
  1096. /*
  1097. * If the page cache is marked as unsafe or invalid, then we can't rely on
  1098. * the PageUptodate() flag. In this case, we will need to turn off
  1099. * write optimisations that depend on the page contents being correct.
  1100. */
  1101. static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
  1102. {
  1103. struct nfs_inode *nfsi = NFS_I(inode);
  1104. if (nfs_have_delegated_attributes(inode))
  1105. goto out;
  1106. if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  1107. return false;
  1108. smp_rmb();
  1109. if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
  1110. return false;
  1111. out:
  1112. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  1113. return false;
  1114. return PageUptodate(page) != 0;
  1115. }
  1116. static bool
  1117. is_whole_file_wrlock(struct file_lock *fl)
  1118. {
  1119. return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX &&
  1120. fl->fl_type == F_WRLCK;
  1121. }
  1122. /* If we know the page is up to date, and we're not using byte range locks (or
  1123. * if we have the whole file locked for writing), it may be more efficient to
  1124. * extend the write to cover the entire page in order to avoid fragmentation
  1125. * inefficiencies.
  1126. *
  1127. * If the file is opened for synchronous writes then we can just skip the rest
  1128. * of the checks.
  1129. */
  1130. static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
  1131. {
  1132. int ret;
  1133. struct file_lock_context *flctx = inode->i_flctx;
  1134. struct file_lock *fl;
  1135. if (file->f_flags & O_DSYNC)
  1136. return 0;
  1137. if (!nfs_write_pageuptodate(page, inode))
  1138. return 0;
  1139. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
  1140. return 1;
  1141. if (!flctx || (list_empty_careful(&flctx->flc_flock) &&
  1142. list_empty_careful(&flctx->flc_posix)))
  1143. return 1;
  1144. /* Check to see if there are whole file write locks */
  1145. ret = 0;
  1146. spin_lock(&flctx->flc_lock);
  1147. if (!list_empty(&flctx->flc_posix)) {
  1148. fl = list_first_entry(&flctx->flc_posix, struct file_lock,
  1149. fl_list);
  1150. if (is_whole_file_wrlock(fl))
  1151. ret = 1;
  1152. } else if (!list_empty(&flctx->flc_flock)) {
  1153. fl = list_first_entry(&flctx->flc_flock, struct file_lock,
  1154. fl_list);
  1155. if (fl->fl_type == F_WRLCK)
  1156. ret = 1;
  1157. }
  1158. spin_unlock(&flctx->flc_lock);
  1159. return ret;
  1160. }
  1161. /*
  1162. * Update and possibly write a cached page of an NFS file.
  1163. *
  1164. * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
  1165. * things with a page scheduled for an RPC call (e.g. invalidate it).
  1166. */
  1167. int nfs_updatepage(struct file *file, struct page *page,
  1168. unsigned int offset, unsigned int count)
  1169. {
  1170. struct nfs_open_context *ctx = nfs_file_open_context(file);
  1171. struct inode *inode = page_file_mapping(page)->host;
  1172. int status = 0;
  1173. nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
  1174. dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n",
  1175. file, count, (long long)(page_file_offset(page) + offset));
  1176. if (!count)
  1177. goto out;
  1178. if (nfs_can_extend_write(file, page, inode)) {
  1179. count = max(count + offset, nfs_page_length(page));
  1180. offset = 0;
  1181. }
  1182. status = nfs_writepage_setup(ctx, page, offset, count);
  1183. if (status < 0)
  1184. nfs_set_pageerror(page);
  1185. else
  1186. __set_page_dirty_nobuffers(page);
  1187. out:
  1188. dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
  1189. status, (long long)i_size_read(inode));
  1190. return status;
  1191. }
  1192. static int flush_task_priority(int how)
  1193. {
  1194. switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
  1195. case FLUSH_HIGHPRI:
  1196. return RPC_PRIORITY_HIGH;
  1197. case FLUSH_LOWPRI:
  1198. return RPC_PRIORITY_LOW;
  1199. }
  1200. return RPC_PRIORITY_NORMAL;
  1201. }
  1202. static void nfs_initiate_write(struct nfs_pgio_header *hdr,
  1203. struct rpc_message *msg,
  1204. const struct nfs_rpc_ops *rpc_ops,
  1205. struct rpc_task_setup *task_setup_data, int how)
  1206. {
  1207. int priority = flush_task_priority(how);
  1208. task_setup_data->priority = priority;
  1209. rpc_ops->write_setup(hdr, msg);
  1210. nfs4_state_protect_write(NFS_SERVER(hdr->inode)->nfs_client,
  1211. &task_setup_data->rpc_client, msg, hdr);
  1212. }
  1213. /* If a nfs_flush_* function fails, it should remove reqs from @head and
  1214. * call this on each, which will prepare them to be retried on next
  1215. * writeback using standard nfs.
  1216. */
  1217. static void nfs_redirty_request(struct nfs_page *req)
  1218. {
  1219. nfs_mark_request_dirty(req);
  1220. set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
  1221. nfs_unlock_request(req);
  1222. nfs_end_page_writeback(req);
  1223. nfs_release_request(req);
  1224. }
  1225. static void nfs_async_write_error(struct list_head *head)
  1226. {
  1227. struct nfs_page *req;
  1228. while (!list_empty(head)) {
  1229. req = nfs_list_entry(head->next);
  1230. nfs_list_remove_request(req);
  1231. nfs_redirty_request(req);
  1232. }
  1233. }
  1234. static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
  1235. {
  1236. nfs_async_write_error(&hdr->pages);
  1237. }
  1238. static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
  1239. .init_hdr = nfs_async_write_init,
  1240. .error_cleanup = nfs_async_write_error,
  1241. .completion = nfs_write_completion,
  1242. .reschedule_io = nfs_async_write_reschedule_io,
  1243. };
  1244. void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
  1245. struct inode *inode, int ioflags, bool force_mds,
  1246. const struct nfs_pgio_completion_ops *compl_ops)
  1247. {
  1248. struct nfs_server *server = NFS_SERVER(inode);
  1249. const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
  1250. #ifdef CONFIG_NFS_V4_1
  1251. if (server->pnfs_curr_ld && !force_mds)
  1252. pg_ops = server->pnfs_curr_ld->pg_write_ops;
  1253. #endif
  1254. nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
  1255. server->wsize, ioflags, GFP_NOIO);
  1256. }
  1257. EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
  1258. void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
  1259. {
  1260. struct nfs_pgio_mirror *mirror;
  1261. if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
  1262. pgio->pg_ops->pg_cleanup(pgio);
  1263. pgio->pg_ops = &nfs_pgio_rw_ops;
  1264. nfs_pageio_stop_mirroring(pgio);
  1265. mirror = &pgio->pg_mirrors[0];
  1266. mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
  1267. }
  1268. EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
  1269. void nfs_commit_prepare(struct rpc_task *task, void *calldata)
  1270. {
  1271. struct nfs_commit_data *data = calldata;
  1272. NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
  1273. }
  1274. /*
  1275. * Special version of should_remove_suid() that ignores capabilities.
  1276. */
  1277. static int nfs_should_remove_suid(const struct inode *inode)
  1278. {
  1279. umode_t mode = inode->i_mode;
  1280. int kill = 0;
  1281. /* suid always must be killed */
  1282. if (unlikely(mode & S_ISUID))
  1283. kill = ATTR_KILL_SUID;
  1284. /*
  1285. * sgid without any exec bits is just a mandatory locking mark; leave
  1286. * it alone. If some exec bits are set, it's a real sgid; kill it.
  1287. */
  1288. if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
  1289. kill |= ATTR_KILL_SGID;
  1290. if (unlikely(kill && S_ISREG(mode)))
  1291. return kill;
  1292. return 0;
  1293. }
  1294. static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr,
  1295. struct nfs_fattr *fattr)
  1296. {
  1297. struct nfs_pgio_args *argp = &hdr->args;
  1298. struct nfs_pgio_res *resp = &hdr->res;
  1299. u64 size = argp->offset + resp->count;
  1300. if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
  1301. fattr->size = size;
  1302. if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) {
  1303. fattr->valid &= ~NFS_ATTR_FATTR_SIZE;
  1304. return;
  1305. }
  1306. if (size != fattr->size)
  1307. return;
  1308. /* Set attribute barrier */
  1309. nfs_fattr_set_barrier(fattr);
  1310. /* ...and update size */
  1311. fattr->valid |= NFS_ATTR_FATTR_SIZE;
  1312. }
  1313. void nfs_writeback_update_inode(struct nfs_pgio_header *hdr)
  1314. {
  1315. struct nfs_fattr *fattr = &hdr->fattr;
  1316. struct inode *inode = hdr->inode;
  1317. spin_lock(&inode->i_lock);
  1318. nfs_writeback_check_extend(hdr, fattr);
  1319. nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
  1320. spin_unlock(&inode->i_lock);
  1321. }
  1322. EXPORT_SYMBOL_GPL(nfs_writeback_update_inode);
  1323. /*
  1324. * This function is called when the WRITE call is complete.
  1325. */
  1326. static int nfs_writeback_done(struct rpc_task *task,
  1327. struct nfs_pgio_header *hdr,
  1328. struct inode *inode)
  1329. {
  1330. int status;
  1331. /*
  1332. * ->write_done will attempt to use post-op attributes to detect
  1333. * conflicting writes by other clients. A strict interpretation
  1334. * of close-to-open would allow us to continue caching even if
  1335. * another writer had changed the file, but some applications
  1336. * depend on tighter cache coherency when writing.
  1337. */
  1338. status = NFS_PROTO(inode)->write_done(task, hdr);
  1339. if (status != 0)
  1340. return status;
  1341. nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
  1342. if (hdr->res.verf->committed < hdr->args.stable &&
  1343. task->tk_status >= 0) {
  1344. /* We tried a write call, but the server did not
  1345. * commit data to stable storage even though we
  1346. * requested it.
  1347. * Note: There is a known bug in Tru64 < 5.0 in which
  1348. * the server reports NFS_DATA_SYNC, but performs
  1349. * NFS_FILE_SYNC. We therefore implement this checking
  1350. * as a dprintk() in order to avoid filling syslog.
  1351. */
  1352. static unsigned long complain;
  1353. /* Note this will print the MDS for a DS write */
  1354. if (time_before(complain, jiffies)) {
  1355. dprintk("NFS: faulty NFS server %s:"
  1356. " (committed = %d) != (stable = %d)\n",
  1357. NFS_SERVER(inode)->nfs_client->cl_hostname,
  1358. hdr->res.verf->committed, hdr->args.stable);
  1359. complain = jiffies + 300 * HZ;
  1360. }
  1361. }
  1362. /* Deal with the suid/sgid bit corner case */
  1363. if (nfs_should_remove_suid(inode))
  1364. nfs_mark_for_revalidate(inode);
  1365. return 0;
  1366. }
  1367. /*
  1368. * This function is called when the WRITE call is complete.
  1369. */
  1370. static void nfs_writeback_result(struct rpc_task *task,
  1371. struct nfs_pgio_header *hdr)
  1372. {
  1373. struct nfs_pgio_args *argp = &hdr->args;
  1374. struct nfs_pgio_res *resp = &hdr->res;
  1375. if (resp->count < argp->count) {
  1376. static unsigned long complain;
  1377. /* This a short write! */
  1378. nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
  1379. /* Has the server at least made some progress? */
  1380. if (resp->count == 0) {
  1381. if (time_before(complain, jiffies)) {
  1382. printk(KERN_WARNING
  1383. "NFS: Server wrote zero bytes, expected %u.\n",
  1384. argp->count);
  1385. complain = jiffies + 300 * HZ;
  1386. }
  1387. nfs_set_pgio_error(hdr, -EIO, argp->offset);
  1388. task->tk_status = -EIO;
  1389. return;
  1390. }
  1391. /* For non rpc-based layout drivers, retry-through-MDS */
  1392. if (!task->tk_ops) {
  1393. hdr->pnfs_error = -EAGAIN;
  1394. return;
  1395. }
  1396. /* Was this an NFSv2 write or an NFSv3 stable write? */
  1397. if (resp->verf->committed != NFS_UNSTABLE) {
  1398. /* Resend from where the server left off */
  1399. hdr->mds_offset += resp->count;
  1400. argp->offset += resp->count;
  1401. argp->pgbase += resp->count;
  1402. argp->count -= resp->count;
  1403. } else {
  1404. /* Resend as a stable write in order to avoid
  1405. * headaches in the case of a server crash.
  1406. */
  1407. argp->stable = NFS_FILE_SYNC;
  1408. }
  1409. rpc_restart_call_prepare(task);
  1410. }
  1411. }
  1412. static int wait_on_commit(struct nfs_mds_commit_info *cinfo)
  1413. {
  1414. return wait_on_atomic_t(&cinfo->rpcs_out,
  1415. nfs_wait_atomic_killable, TASK_KILLABLE);
  1416. }
  1417. static void nfs_commit_begin(struct nfs_mds_commit_info *cinfo)
  1418. {
  1419. atomic_inc(&cinfo->rpcs_out);
  1420. }
  1421. static void nfs_commit_end(struct nfs_mds_commit_info *cinfo)
  1422. {
  1423. if (atomic_dec_and_test(&cinfo->rpcs_out))
  1424. wake_up_atomic_t(&cinfo->rpcs_out);
  1425. }
  1426. void nfs_commitdata_release(struct nfs_commit_data *data)
  1427. {
  1428. put_nfs_open_context(data->context);
  1429. nfs_commit_free(data);
  1430. }
  1431. EXPORT_SYMBOL_GPL(nfs_commitdata_release);
  1432. int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
  1433. const struct nfs_rpc_ops *nfs_ops,
  1434. const struct rpc_call_ops *call_ops,
  1435. int how, int flags)
  1436. {
  1437. struct rpc_task *task;
  1438. int priority = flush_task_priority(how);
  1439. struct rpc_message msg = {
  1440. .rpc_argp = &data->args,
  1441. .rpc_resp = &data->res,
  1442. .rpc_cred = data->cred,
  1443. };
  1444. struct rpc_task_setup task_setup_data = {
  1445. .task = &data->task,
  1446. .rpc_client = clnt,
  1447. .rpc_message = &msg,
  1448. .callback_ops = call_ops,
  1449. .callback_data = data,
  1450. .workqueue = nfsiod_workqueue,
  1451. .flags = RPC_TASK_ASYNC | flags,
  1452. .priority = priority,
  1453. };
  1454. /* Set up the initial task struct. */
  1455. nfs_ops->commit_setup(data, &msg);
  1456. dprintk("NFS: initiated commit call\n");
  1457. nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
  1458. NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
  1459. task = rpc_run_task(&task_setup_data);
  1460. if (IS_ERR(task))
  1461. return PTR_ERR(task);
  1462. if (how & FLUSH_SYNC)
  1463. rpc_wait_for_completion_task(task);
  1464. rpc_put_task(task);
  1465. return 0;
  1466. }
  1467. EXPORT_SYMBOL_GPL(nfs_initiate_commit);
  1468. static loff_t nfs_get_lwb(struct list_head *head)
  1469. {
  1470. loff_t lwb = 0;
  1471. struct nfs_page *req;
  1472. list_for_each_entry(req, head, wb_list)
  1473. if (lwb < (req_offset(req) + req->wb_bytes))
  1474. lwb = req_offset(req) + req->wb_bytes;
  1475. return lwb;
  1476. }
  1477. /*
  1478. * Set up the argument/result storage required for the RPC call.
  1479. */
  1480. void nfs_init_commit(struct nfs_commit_data *data,
  1481. struct list_head *head,
  1482. struct pnfs_layout_segment *lseg,
  1483. struct nfs_commit_info *cinfo)
  1484. {
  1485. struct nfs_page *first = nfs_list_entry(head->next);
  1486. struct inode *inode = d_inode(first->wb_context->dentry);
  1487. /* Set up the RPC argument and reply structs
  1488. * NB: take care not to mess about with data->commit et al. */
  1489. list_splice_init(head, &data->pages);
  1490. data->inode = inode;
  1491. data->cred = first->wb_context->cred;
  1492. data->lseg = lseg; /* reference transferred */
  1493. /* only set lwb for pnfs commit */
  1494. if (lseg)
  1495. data->lwb = nfs_get_lwb(&data->pages);
  1496. data->mds_ops = &nfs_commit_ops;
  1497. data->completion_ops = cinfo->completion_ops;
  1498. data->dreq = cinfo->dreq;
  1499. data->args.fh = NFS_FH(data->inode);
  1500. /* Note: we always request a commit of the entire inode */
  1501. data->args.offset = 0;
  1502. data->args.count = 0;
  1503. data->context = get_nfs_open_context(first->wb_context);
  1504. data->res.fattr = &data->fattr;
  1505. data->res.verf = &data->verf;
  1506. nfs_fattr_init(&data->fattr);
  1507. }
  1508. EXPORT_SYMBOL_GPL(nfs_init_commit);
  1509. void nfs_retry_commit(struct list_head *page_list,
  1510. struct pnfs_layout_segment *lseg,
  1511. struct nfs_commit_info *cinfo,
  1512. u32 ds_commit_idx)
  1513. {
  1514. struct nfs_page *req;
  1515. while (!list_empty(page_list)) {
  1516. req = nfs_list_entry(page_list->next);
  1517. nfs_list_remove_request(req);
  1518. nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx);
  1519. if (!cinfo->dreq)
  1520. nfs_clear_page_commit(req->wb_page);
  1521. nfs_unlock_and_release_request(req);
  1522. }
  1523. }
  1524. EXPORT_SYMBOL_GPL(nfs_retry_commit);
  1525. static void
  1526. nfs_commit_resched_write(struct nfs_commit_info *cinfo,
  1527. struct nfs_page *req)
  1528. {
  1529. __set_page_dirty_nobuffers(req->wb_page);
  1530. }
  1531. /*
  1532. * Commit dirty pages
  1533. */
  1534. static int
  1535. nfs_commit_list(struct inode *inode, struct list_head *head, int how,
  1536. struct nfs_commit_info *cinfo)
  1537. {
  1538. struct nfs_commit_data *data;
  1539. /* another commit raced with us */
  1540. if (list_empty(head))
  1541. return 0;
  1542. data = nfs_commitdata_alloc(true);
  1543. /* Set up the argument struct */
  1544. nfs_init_commit(data, head, NULL, cinfo);
  1545. atomic_inc(&cinfo->mds->rpcs_out);
  1546. return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode),
  1547. data->mds_ops, how, 0);
  1548. }
  1549. /*
  1550. * COMMIT call returned
  1551. */
  1552. static void nfs_commit_done(struct rpc_task *task, void *calldata)
  1553. {
  1554. struct nfs_commit_data *data = calldata;
  1555. dprintk("NFS: %5u nfs_commit_done (status %d)\n",
  1556. task->tk_pid, task->tk_status);
  1557. /* Call the NFS version-specific code */
  1558. NFS_PROTO(data->inode)->commit_done(task, data);
  1559. }
  1560. static void nfs_commit_release_pages(struct nfs_commit_data *data)
  1561. {
  1562. struct nfs_page *req;
  1563. int status = data->task.tk_status;
  1564. struct nfs_commit_info cinfo;
  1565. struct nfs_server *nfss;
  1566. while (!list_empty(&data->pages)) {
  1567. req = nfs_list_entry(data->pages.next);
  1568. nfs_list_remove_request(req);
  1569. if (req->wb_page)
  1570. nfs_clear_page_commit(req->wb_page);
  1571. dprintk("NFS: commit (%s/%llu %d@%lld)",
  1572. req->wb_context->dentry->d_sb->s_id,
  1573. (unsigned long long)NFS_FILEID(d_inode(req->wb_context->dentry)),
  1574. req->wb_bytes,
  1575. (long long)req_offset(req));
  1576. if (status < 0) {
  1577. nfs_context_set_write_error(req->wb_context, status);
  1578. if (req->wb_page)
  1579. nfs_inode_remove_request(req);
  1580. dprintk_cont(", error = %d\n", status);
  1581. goto next;
  1582. }
  1583. /* Okay, COMMIT succeeded, apparently. Check the verifier
  1584. * returned by the server against all stored verfs. */
  1585. if (!nfs_write_verifier_cmp(&req->wb_verf, &data->verf.verifier)) {
  1586. /* We have a match */
  1587. if (req->wb_page)
  1588. nfs_inode_remove_request(req);
  1589. dprintk_cont(" OK\n");
  1590. goto next;
  1591. }
  1592. /* We have a mismatch. Write the page again */
  1593. dprintk_cont(" mismatch\n");
  1594. nfs_mark_request_dirty(req);
  1595. set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
  1596. next:
  1597. nfs_unlock_and_release_request(req);
  1598. }
  1599. nfss = NFS_SERVER(data->inode);
  1600. if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
  1601. clear_bdi_congested(inode_to_bdi(data->inode), BLK_RW_ASYNC);
  1602. nfs_init_cinfo(&cinfo, data->inode, data->dreq);
  1603. nfs_commit_end(cinfo.mds);
  1604. }
  1605. static void nfs_commit_release(void *calldata)
  1606. {
  1607. struct nfs_commit_data *data = calldata;
  1608. data->completion_ops->completion(data);
  1609. nfs_commitdata_release(calldata);
  1610. }
  1611. static const struct rpc_call_ops nfs_commit_ops = {
  1612. .rpc_call_prepare = nfs_commit_prepare,
  1613. .rpc_call_done = nfs_commit_done,
  1614. .rpc_release = nfs_commit_release,
  1615. };
  1616. static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
  1617. .completion = nfs_commit_release_pages,
  1618. .resched_write = nfs_commit_resched_write,
  1619. };
  1620. int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
  1621. int how, struct nfs_commit_info *cinfo)
  1622. {
  1623. int status;
  1624. status = pnfs_commit_list(inode, head, how, cinfo);
  1625. if (status == PNFS_NOT_ATTEMPTED)
  1626. status = nfs_commit_list(inode, head, how, cinfo);
  1627. return status;
  1628. }
  1629. int nfs_commit_inode(struct inode *inode, int how)
  1630. {
  1631. LIST_HEAD(head);
  1632. struct nfs_commit_info cinfo;
  1633. int may_wait = how & FLUSH_SYNC;
  1634. int error = 0;
  1635. int res;
  1636. nfs_init_cinfo_from_inode(&cinfo, inode);
  1637. nfs_commit_begin(cinfo.mds);
  1638. res = nfs_scan_commit(inode, &head, &cinfo);
  1639. if (res)
  1640. error = nfs_generic_commit_list(inode, &head, how, &cinfo);
  1641. nfs_commit_end(cinfo.mds);
  1642. if (error < 0)
  1643. goto out_error;
  1644. if (!may_wait)
  1645. goto out_mark_dirty;
  1646. error = wait_on_commit(cinfo.mds);
  1647. if (error < 0)
  1648. return error;
  1649. return res;
  1650. out_error:
  1651. res = error;
  1652. /* Note: If we exit without ensuring that the commit is complete,
  1653. * we must mark the inode as dirty. Otherwise, future calls to
  1654. * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
  1655. * that the data is on the disk.
  1656. */
  1657. out_mark_dirty:
  1658. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  1659. return res;
  1660. }
  1661. EXPORT_SYMBOL_GPL(nfs_commit_inode);
  1662. int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  1663. {
  1664. struct nfs_inode *nfsi = NFS_I(inode);
  1665. int flags = FLUSH_SYNC;
  1666. int ret = 0;
  1667. /* no commits means nothing needs to be done */
  1668. if (!nfsi->commit_info.ncommit)
  1669. return ret;
  1670. if (wbc->sync_mode == WB_SYNC_NONE) {
  1671. /* Don't commit yet if this is a non-blocking flush and there
  1672. * are a lot of outstanding writes for this mapping.
  1673. */
  1674. if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
  1675. goto out_mark_dirty;
  1676. /* don't wait for the COMMIT response */
  1677. flags = 0;
  1678. }
  1679. ret = nfs_commit_inode(inode, flags);
  1680. if (ret >= 0) {
  1681. if (wbc->sync_mode == WB_SYNC_NONE) {
  1682. if (ret < wbc->nr_to_write)
  1683. wbc->nr_to_write -= ret;
  1684. else
  1685. wbc->nr_to_write = 0;
  1686. }
  1687. return 0;
  1688. }
  1689. out_mark_dirty:
  1690. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  1691. return ret;
  1692. }
  1693. EXPORT_SYMBOL_GPL(nfs_write_inode);
  1694. /*
  1695. * Wrapper for filemap_write_and_wait_range()
  1696. *
  1697. * Needed for pNFS in order to ensure data becomes visible to the
  1698. * client.
  1699. */
  1700. int nfs_filemap_write_and_wait_range(struct address_space *mapping,
  1701. loff_t lstart, loff_t lend)
  1702. {
  1703. int ret;
  1704. ret = filemap_write_and_wait_range(mapping, lstart, lend);
  1705. if (ret == 0)
  1706. ret = pnfs_sync_inode(mapping->host, true);
  1707. return ret;
  1708. }
  1709. EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range);
  1710. /*
  1711. * flush the inode to disk.
  1712. */
  1713. int nfs_wb_all(struct inode *inode)
  1714. {
  1715. int ret;
  1716. trace_nfs_writeback_inode_enter(inode);
  1717. ret = filemap_write_and_wait(inode->i_mapping);
  1718. if (ret)
  1719. goto out;
  1720. ret = nfs_commit_inode(inode, FLUSH_SYNC);
  1721. if (ret < 0)
  1722. goto out;
  1723. pnfs_sync_inode(inode, true);
  1724. ret = 0;
  1725. out:
  1726. trace_nfs_writeback_inode_exit(inode, ret);
  1727. return ret;
  1728. }
  1729. EXPORT_SYMBOL_GPL(nfs_wb_all);
  1730. int nfs_wb_page_cancel(struct inode *inode, struct page *page)
  1731. {
  1732. struct nfs_page *req;
  1733. int ret = 0;
  1734. wait_on_page_writeback(page);
  1735. /* blocking call to cancel all requests and join to a single (head)
  1736. * request */
  1737. req = nfs_lock_and_join_requests(page);
  1738. if (IS_ERR(req)) {
  1739. ret = PTR_ERR(req);
  1740. } else if (req) {
  1741. /* all requests from this page have been cancelled by
  1742. * nfs_lock_and_join_requests, so just remove the head
  1743. * request from the inode / page_private pointer and
  1744. * release it */
  1745. nfs_inode_remove_request(req);
  1746. nfs_unlock_and_release_request(req);
  1747. }
  1748. return ret;
  1749. }
  1750. /*
  1751. * Write back all requests on one page - we do this before reading it.
  1752. */
  1753. int nfs_wb_page(struct inode *inode, struct page *page)
  1754. {
  1755. loff_t range_start = page_file_offset(page);
  1756. loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
  1757. struct writeback_control wbc = {
  1758. .sync_mode = WB_SYNC_ALL,
  1759. .nr_to_write = 0,
  1760. .range_start = range_start,
  1761. .range_end = range_end,
  1762. };
  1763. int ret;
  1764. trace_nfs_writeback_page_enter(inode);
  1765. for (;;) {
  1766. wait_on_page_writeback(page);
  1767. if (clear_page_dirty_for_io(page)) {
  1768. ret = nfs_writepage_locked(page, &wbc);
  1769. if (ret < 0)
  1770. goto out_error;
  1771. continue;
  1772. }
  1773. ret = 0;
  1774. if (!PagePrivate(page))
  1775. break;
  1776. ret = nfs_commit_inode(inode, FLUSH_SYNC);
  1777. if (ret < 0)
  1778. goto out_error;
  1779. }
  1780. out_error:
  1781. trace_nfs_writeback_page_exit(inode, ret);
  1782. return ret;
  1783. }
  1784. #ifdef CONFIG_MIGRATION
  1785. int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
  1786. struct page *page, enum migrate_mode mode)
  1787. {
  1788. /*
  1789. * If PagePrivate is set, then the page is currently associated with
  1790. * an in-progress read or write request. Don't try to migrate it.
  1791. *
  1792. * FIXME: we could do this in principle, but we'll need a way to ensure
  1793. * that we can safely release the inode reference while holding
  1794. * the page lock.
  1795. */
  1796. if (PagePrivate(page))
  1797. return -EBUSY;
  1798. if (!nfs_fscache_release_page(page, GFP_KERNEL))
  1799. return -EBUSY;
  1800. return migrate_page(mapping, newpage, page, mode);
  1801. }
  1802. #endif
  1803. int __init nfs_init_writepagecache(void)
  1804. {
  1805. nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
  1806. sizeof(struct nfs_pgio_header),
  1807. 0, SLAB_HWCACHE_ALIGN,
  1808. NULL);
  1809. if (nfs_wdata_cachep == NULL)
  1810. return -ENOMEM;
  1811. nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
  1812. nfs_wdata_cachep);
  1813. if (nfs_wdata_mempool == NULL)
  1814. goto out_destroy_write_cache;
  1815. nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
  1816. sizeof(struct nfs_commit_data),
  1817. 0, SLAB_HWCACHE_ALIGN,
  1818. NULL);
  1819. if (nfs_cdata_cachep == NULL)
  1820. goto out_destroy_write_mempool;
  1821. nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
  1822. nfs_cdata_cachep);
  1823. if (nfs_commit_mempool == NULL)
  1824. goto out_destroy_commit_cache;
  1825. /*
  1826. * NFS congestion size, scale with available memory.
  1827. *
  1828. * 64MB: 8192k
  1829. * 128MB: 11585k
  1830. * 256MB: 16384k
  1831. * 512MB: 23170k
  1832. * 1GB: 32768k
  1833. * 2GB: 46340k
  1834. * 4GB: 65536k
  1835. * 8GB: 92681k
  1836. * 16GB: 131072k
  1837. *
  1838. * This allows larger machines to have larger/more transfers.
  1839. * Limit the default to 256M
  1840. */
  1841. nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
  1842. if (nfs_congestion_kb > 256*1024)
  1843. nfs_congestion_kb = 256*1024;
  1844. return 0;
  1845. out_destroy_commit_cache:
  1846. kmem_cache_destroy(nfs_cdata_cachep);
  1847. out_destroy_write_mempool:
  1848. mempool_destroy(nfs_wdata_mempool);
  1849. out_destroy_write_cache:
  1850. kmem_cache_destroy(nfs_wdata_cachep);
  1851. return -ENOMEM;
  1852. }
  1853. void nfs_destroy_writepagecache(void)
  1854. {
  1855. mempool_destroy(nfs_commit_mempool);
  1856. kmem_cache_destroy(nfs_cdata_cachep);
  1857. mempool_destroy(nfs_wdata_mempool);
  1858. kmem_cache_destroy(nfs_wdata_cachep);
  1859. }
  1860. static const struct nfs_rw_ops nfs_rw_write_ops = {
  1861. .rw_alloc_header = nfs_writehdr_alloc,
  1862. .rw_free_header = nfs_writehdr_free,
  1863. .rw_done = nfs_writeback_done,
  1864. .rw_result = nfs_writeback_result,
  1865. .rw_initiate = nfs_initiate_write,
  1866. };