write.c 56 KB

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