write.c 56 KB

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