write.c 56 KB

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