write.c 54 KB

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