write.c 51 KB

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