write.c 51 KB

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