write.c 56 KB

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