write.c 53 KB

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