write.c 55 KB

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