write.c 54 KB

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