transaction.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * linux/fs/jbd2/transaction.c
  4. *
  5. * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
  6. *
  7. * Copyright 1998 Red Hat corp --- All Rights Reserved
  8. *
  9. * Generic filesystem transaction handling code; part of the ext2fs
  10. * journaling system.
  11. *
  12. * This file manages transactions (compound commits managed by the
  13. * journaling code) and handles (individual atomic operations by the
  14. * filesystem).
  15. */
  16. #include <linux/time.h>
  17. #include <linux/fs.h>
  18. #include <linux/jbd2.h>
  19. #include <linux/errno.h>
  20. #include <linux/slab.h>
  21. #include <linux/timer.h>
  22. #include <linux/mm.h>
  23. #include <linux/highmem.h>
  24. #include <linux/hrtimer.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/bug.h>
  27. #include <linux/module.h>
  28. #include <linux/sched/mm.h>
  29. #include <trace/events/jbd2.h>
  30. static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
  31. static void __jbd2_journal_unfile_buffer(struct journal_head *jh);
  32. static struct kmem_cache *transaction_cache;
  33. int __init jbd2_journal_init_transaction_cache(void)
  34. {
  35. J_ASSERT(!transaction_cache);
  36. transaction_cache = kmem_cache_create("jbd2_transaction_s",
  37. sizeof(transaction_t),
  38. 0,
  39. SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY,
  40. NULL);
  41. if (!transaction_cache) {
  42. pr_emerg("JBD2: failed to create transaction cache\n");
  43. return -ENOMEM;
  44. }
  45. return 0;
  46. }
  47. void jbd2_journal_destroy_transaction_cache(void)
  48. {
  49. kmem_cache_destroy(transaction_cache);
  50. transaction_cache = NULL;
  51. }
  52. void jbd2_journal_free_transaction(transaction_t *transaction)
  53. {
  54. if (unlikely(ZERO_OR_NULL_PTR(transaction)))
  55. return;
  56. kmem_cache_free(transaction_cache, transaction);
  57. }
  58. /*
  59. * jbd2_get_transaction: obtain a new transaction_t object.
  60. *
  61. * Simply initialise a new transaction. Initialize it in
  62. * RUNNING state and add it to the current journal (which should not
  63. * have an existing running transaction: we only make a new transaction
  64. * once we have started to commit the old one).
  65. *
  66. * Preconditions:
  67. * The journal MUST be locked. We don't perform atomic mallocs on the
  68. * new transaction and we can't block without protecting against other
  69. * processes trying to touch the journal while it is in transition.
  70. *
  71. */
  72. static void jbd2_get_transaction(journal_t *journal,
  73. transaction_t *transaction)
  74. {
  75. transaction->t_journal = journal;
  76. transaction->t_state = T_RUNNING;
  77. transaction->t_start_time = ktime_get();
  78. transaction->t_tid = journal->j_transaction_sequence++;
  79. transaction->t_expires = jiffies + journal->j_commit_interval;
  80. atomic_set(&transaction->t_updates, 0);
  81. atomic_set(&transaction->t_outstanding_credits,
  82. journal->j_transaction_overhead_buffers +
  83. atomic_read(&journal->j_reserved_credits));
  84. atomic_set(&transaction->t_outstanding_revokes, 0);
  85. atomic_set(&transaction->t_handle_count, 0);
  86. INIT_LIST_HEAD(&transaction->t_inode_list);
  87. INIT_LIST_HEAD(&transaction->t_private_list);
  88. /* Set up the commit timer for the new transaction. */
  89. journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
  90. add_timer(&journal->j_commit_timer);
  91. J_ASSERT(journal->j_running_transaction == NULL);
  92. journal->j_running_transaction = transaction;
  93. transaction->t_max_wait = 0;
  94. transaction->t_start = jiffies;
  95. transaction->t_requested = 0;
  96. }
  97. /*
  98. * Handle management.
  99. *
  100. * A handle_t is an object which represents a single atomic update to a
  101. * filesystem, and which tracks all of the modifications which form part
  102. * of that one update.
  103. */
  104. /*
  105. * Update transaction's maximum wait time, if debugging is enabled.
  106. *
  107. * t_max_wait is carefully updated here with use of atomic compare exchange.
  108. * Note that there could be multiplre threads trying to do this simultaneously
  109. * hence using cmpxchg to avoid any use of locks in this case.
  110. * With this t_max_wait can be updated w/o enabling jbd2_journal_enable_debug.
  111. */
  112. static inline void update_t_max_wait(transaction_t *transaction,
  113. unsigned long ts)
  114. {
  115. unsigned long oldts, newts;
  116. if (time_after(transaction->t_start, ts)) {
  117. newts = jbd2_time_diff(ts, transaction->t_start);
  118. oldts = READ_ONCE(transaction->t_max_wait);
  119. while (oldts < newts)
  120. oldts = cmpxchg(&transaction->t_max_wait, oldts, newts);
  121. }
  122. }
  123. /*
  124. * Wait until running transaction passes to T_FLUSH state and new transaction
  125. * can thus be started. Also starts the commit if needed. The function expects
  126. * running transaction to exist and releases j_state_lock.
  127. */
  128. static void wait_transaction_locked(journal_t *journal)
  129. __releases(journal->j_state_lock)
  130. {
  131. DEFINE_WAIT(wait);
  132. int need_to_start;
  133. tid_t tid = journal->j_running_transaction->t_tid;
  134. prepare_to_wait_exclusive(&journal->j_wait_transaction_locked, &wait,
  135. TASK_UNINTERRUPTIBLE);
  136. need_to_start = !tid_geq(journal->j_commit_request, tid);
  137. read_unlock(&journal->j_state_lock);
  138. if (need_to_start)
  139. jbd2_log_start_commit(journal, tid);
  140. jbd2_might_wait_for_commit(journal);
  141. schedule();
  142. finish_wait(&journal->j_wait_transaction_locked, &wait);
  143. }
  144. /*
  145. * Wait until running transaction transitions from T_SWITCH to T_FLUSH
  146. * state and new transaction can thus be started. The function releases
  147. * j_state_lock.
  148. */
  149. static void wait_transaction_switching(journal_t *journal)
  150. __releases(journal->j_state_lock)
  151. {
  152. DEFINE_WAIT(wait);
  153. if (WARN_ON(!journal->j_running_transaction ||
  154. journal->j_running_transaction->t_state != T_SWITCH)) {
  155. read_unlock(&journal->j_state_lock);
  156. return;
  157. }
  158. prepare_to_wait_exclusive(&journal->j_wait_transaction_locked, &wait,
  159. TASK_UNINTERRUPTIBLE);
  160. read_unlock(&journal->j_state_lock);
  161. /*
  162. * We don't call jbd2_might_wait_for_commit() here as there's no
  163. * waiting for outstanding handles happening anymore in T_SWITCH state
  164. * and handling of reserved handles actually relies on that for
  165. * correctness.
  166. */
  167. schedule();
  168. finish_wait(&journal->j_wait_transaction_locked, &wait);
  169. }
  170. static void sub_reserved_credits(journal_t *journal, int blocks)
  171. {
  172. atomic_sub(blocks, &journal->j_reserved_credits);
  173. wake_up(&journal->j_wait_reserved);
  174. }
  175. /* Maximum number of blocks for user transaction payload */
  176. static int jbd2_max_user_trans_buffers(journal_t *journal)
  177. {
  178. return journal->j_max_transaction_buffers -
  179. journal->j_transaction_overhead_buffers;
  180. }
  181. /*
  182. * Wait until we can add credits for handle to the running transaction. Called
  183. * with j_state_lock held for reading. Returns 0 if handle joined the running
  184. * transaction. Returns 1 if we had to wait, j_state_lock is dropped, and
  185. * caller must retry.
  186. *
  187. * Note: because j_state_lock may be dropped depending on the return
  188. * value, we need to fake out sparse so ti doesn't complain about a
  189. * locking imbalance. Callers of add_transaction_credits will need to
  190. * make a similar accomodation.
  191. */
  192. static int add_transaction_credits(journal_t *journal, int blocks,
  193. int rsv_blocks)
  194. __must_hold(&journal->j_state_lock)
  195. {
  196. transaction_t *t = journal->j_running_transaction;
  197. int needed;
  198. int total = blocks + rsv_blocks;
  199. /*
  200. * If the current transaction is locked down for commit, wait
  201. * for the lock to be released.
  202. */
  203. if (t->t_state != T_RUNNING) {
  204. WARN_ON_ONCE(t->t_state >= T_FLUSH);
  205. wait_transaction_locked(journal);
  206. __acquire(&journal->j_state_lock); /* fake out sparse */
  207. return 1;
  208. }
  209. /*
  210. * If there is not enough space left in the log to write all
  211. * potential buffers requested by this operation, we need to
  212. * stall pending a log checkpoint to free some more log space.
  213. */
  214. needed = atomic_add_return(total, &t->t_outstanding_credits);
  215. if (needed > journal->j_max_transaction_buffers) {
  216. /*
  217. * If the current transaction is already too large,
  218. * then start to commit it: we can then go back and
  219. * attach this handle to a new transaction.
  220. */
  221. atomic_sub(total, &t->t_outstanding_credits);
  222. /*
  223. * Is the number of reserved credits in the current transaction too
  224. * big to fit this handle? Wait until reserved credits are freed.
  225. */
  226. if (atomic_read(&journal->j_reserved_credits) + total >
  227. jbd2_max_user_trans_buffers(journal)) {
  228. read_unlock(&journal->j_state_lock);
  229. jbd2_might_wait_for_commit(journal);
  230. wait_event(journal->j_wait_reserved,
  231. atomic_read(&journal->j_reserved_credits) + total <=
  232. jbd2_max_user_trans_buffers(journal));
  233. __acquire(&journal->j_state_lock); /* fake out sparse */
  234. return 1;
  235. }
  236. wait_transaction_locked(journal);
  237. __acquire(&journal->j_state_lock); /* fake out sparse */
  238. return 1;
  239. }
  240. /*
  241. * The commit code assumes that it can get enough log space
  242. * without forcing a checkpoint. This is *critical* for
  243. * correctness: a checkpoint of a buffer which is also
  244. * associated with a committing transaction creates a deadlock,
  245. * so commit simply cannot force through checkpoints.
  246. *
  247. * We must therefore ensure the necessary space in the journal
  248. * *before* starting to dirty potentially checkpointed buffers
  249. * in the new transaction.
  250. */
  251. if (jbd2_log_space_left(journal) < journal->j_max_transaction_buffers) {
  252. atomic_sub(total, &t->t_outstanding_credits);
  253. read_unlock(&journal->j_state_lock);
  254. jbd2_might_wait_for_commit(journal);
  255. write_lock(&journal->j_state_lock);
  256. if (jbd2_log_space_left(journal) <
  257. journal->j_max_transaction_buffers)
  258. __jbd2_log_wait_for_space(journal);
  259. write_unlock(&journal->j_state_lock);
  260. __acquire(&journal->j_state_lock); /* fake out sparse */
  261. return 1;
  262. }
  263. /* No reservation? We are done... */
  264. if (!rsv_blocks)
  265. return 0;
  266. needed = atomic_add_return(rsv_blocks, &journal->j_reserved_credits);
  267. /* We allow at most half of a transaction to be reserved */
  268. if (needed > jbd2_max_user_trans_buffers(journal) / 2) {
  269. sub_reserved_credits(journal, rsv_blocks);
  270. atomic_sub(total, &t->t_outstanding_credits);
  271. read_unlock(&journal->j_state_lock);
  272. jbd2_might_wait_for_commit(journal);
  273. wait_event(journal->j_wait_reserved,
  274. atomic_read(&journal->j_reserved_credits) + rsv_blocks
  275. <= jbd2_max_user_trans_buffers(journal) / 2);
  276. __acquire(&journal->j_state_lock); /* fake out sparse */
  277. return 1;
  278. }
  279. return 0;
  280. }
  281. /*
  282. * start_this_handle: Given a handle, deal with any locking or stalling
  283. * needed to make sure that there is enough journal space for the handle
  284. * to begin. Attach the handle to a transaction and set up the
  285. * transaction's buffer credits.
  286. */
  287. static int start_this_handle(journal_t *journal, handle_t *handle,
  288. gfp_t gfp_mask)
  289. {
  290. transaction_t *transaction, *new_transaction = NULL;
  291. int blocks = handle->h_total_credits;
  292. int rsv_blocks = 0;
  293. unsigned long ts = jiffies;
  294. if (handle->h_rsv_handle)
  295. rsv_blocks = handle->h_rsv_handle->h_total_credits;
  296. /*
  297. * Limit the number of reserved credits to 1/2 of maximum transaction
  298. * size and limit the number of total credits to not exceed maximum
  299. * transaction size per operation.
  300. */
  301. if (rsv_blocks > jbd2_max_user_trans_buffers(journal) / 2 ||
  302. rsv_blocks + blocks > jbd2_max_user_trans_buffers(journal)) {
  303. printk(KERN_ERR "JBD2: %s wants too many credits "
  304. "credits:%d rsv_credits:%d max:%d\n",
  305. current->comm, blocks, rsv_blocks,
  306. jbd2_max_user_trans_buffers(journal));
  307. WARN_ON(1);
  308. return -ENOSPC;
  309. }
  310. alloc_transaction:
  311. /*
  312. * This check is racy but it is just an optimization of allocating new
  313. * transaction early if there are high chances we'll need it. If we
  314. * guess wrong, we'll retry or free unused transaction.
  315. */
  316. if (!data_race(journal->j_running_transaction)) {
  317. /*
  318. * If __GFP_FS is not present, then we may be being called from
  319. * inside the fs writeback layer, so we MUST NOT fail.
  320. */
  321. if ((gfp_mask & __GFP_FS) == 0)
  322. gfp_mask |= __GFP_NOFAIL;
  323. new_transaction = kmem_cache_zalloc(transaction_cache,
  324. gfp_mask);
  325. if (!new_transaction)
  326. return -ENOMEM;
  327. }
  328. jbd2_debug(3, "New handle %p going live.\n", handle);
  329. /*
  330. * We need to hold j_state_lock until t_updates has been incremented,
  331. * for proper journal barrier handling
  332. */
  333. repeat:
  334. read_lock(&journal->j_state_lock);
  335. BUG_ON(journal->j_flags & JBD2_UNMOUNT);
  336. if (is_journal_aborted(journal) ||
  337. (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
  338. read_unlock(&journal->j_state_lock);
  339. jbd2_journal_free_transaction(new_transaction);
  340. return -EROFS;
  341. }
  342. /*
  343. * Wait on the journal's transaction barrier if necessary. Specifically
  344. * we allow reserved handles to proceed because otherwise commit could
  345. * deadlock on page writeback not being able to complete.
  346. */
  347. if (!handle->h_reserved && journal->j_barrier_count) {
  348. read_unlock(&journal->j_state_lock);
  349. wait_event(journal->j_wait_transaction_locked,
  350. journal->j_barrier_count == 0);
  351. goto repeat;
  352. }
  353. if (!journal->j_running_transaction) {
  354. read_unlock(&journal->j_state_lock);
  355. if (!new_transaction)
  356. goto alloc_transaction;
  357. write_lock(&journal->j_state_lock);
  358. if (!journal->j_running_transaction &&
  359. (handle->h_reserved || !journal->j_barrier_count)) {
  360. jbd2_get_transaction(journal, new_transaction);
  361. new_transaction = NULL;
  362. }
  363. write_unlock(&journal->j_state_lock);
  364. goto repeat;
  365. }
  366. transaction = journal->j_running_transaction;
  367. if (!handle->h_reserved) {
  368. /* We may have dropped j_state_lock - restart in that case */
  369. if (add_transaction_credits(journal, blocks, rsv_blocks)) {
  370. /*
  371. * add_transaction_credits releases
  372. * j_state_lock on a non-zero return
  373. */
  374. __release(&journal->j_state_lock);
  375. goto repeat;
  376. }
  377. } else {
  378. /*
  379. * We have handle reserved so we are allowed to join T_LOCKED
  380. * transaction and we don't have to check for transaction size
  381. * and journal space. But we still have to wait while running
  382. * transaction is being switched to a committing one as it
  383. * won't wait for any handles anymore.
  384. */
  385. if (transaction->t_state == T_SWITCH) {
  386. wait_transaction_switching(journal);
  387. goto repeat;
  388. }
  389. sub_reserved_credits(journal, blocks);
  390. handle->h_reserved = 0;
  391. }
  392. /* OK, account for the buffers that this operation expects to
  393. * use and add the handle to the running transaction.
  394. */
  395. update_t_max_wait(transaction, ts);
  396. handle->h_transaction = transaction;
  397. handle->h_requested_credits = blocks;
  398. handle->h_revoke_credits_requested = handle->h_revoke_credits;
  399. handle->h_start_jiffies = jiffies;
  400. atomic_inc(&transaction->t_updates);
  401. atomic_inc(&transaction->t_handle_count);
  402. jbd2_debug(4, "Handle %p given %d credits (total %d, free %lu)\n",
  403. handle, blocks,
  404. atomic_read(&transaction->t_outstanding_credits),
  405. jbd2_log_space_left(journal));
  406. read_unlock(&journal->j_state_lock);
  407. current->journal_info = handle;
  408. rwsem_acquire_read(&journal->j_trans_commit_map, 0, 0, _THIS_IP_);
  409. jbd2_journal_free_transaction(new_transaction);
  410. /*
  411. * Ensure that no allocations done while the transaction is open are
  412. * going to recurse back to the fs layer.
  413. */
  414. handle->saved_alloc_context = memalloc_nofs_save();
  415. return 0;
  416. }
  417. /* Allocate a new handle. This should probably be in a slab... */
  418. static handle_t *new_handle(int nblocks)
  419. {
  420. handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
  421. if (!handle)
  422. return NULL;
  423. handle->h_total_credits = nblocks;
  424. handle->h_ref = 1;
  425. return handle;
  426. }
  427. handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int rsv_blocks,
  428. int revoke_records, gfp_t gfp_mask,
  429. unsigned int type, unsigned int line_no)
  430. {
  431. handle_t *handle = journal_current_handle();
  432. int err;
  433. if (!journal)
  434. return ERR_PTR(-EROFS);
  435. if (handle) {
  436. J_ASSERT(handle->h_transaction->t_journal == journal);
  437. handle->h_ref++;
  438. return handle;
  439. }
  440. nblocks += DIV_ROUND_UP(revoke_records,
  441. journal->j_revoke_records_per_block);
  442. handle = new_handle(nblocks);
  443. if (!handle)
  444. return ERR_PTR(-ENOMEM);
  445. if (rsv_blocks) {
  446. handle_t *rsv_handle;
  447. rsv_handle = new_handle(rsv_blocks);
  448. if (!rsv_handle) {
  449. jbd2_free_handle(handle);
  450. return ERR_PTR(-ENOMEM);
  451. }
  452. rsv_handle->h_reserved = 1;
  453. rsv_handle->h_journal = journal;
  454. handle->h_rsv_handle = rsv_handle;
  455. }
  456. handle->h_revoke_credits = revoke_records;
  457. err = start_this_handle(journal, handle, gfp_mask);
  458. if (err < 0) {
  459. if (handle->h_rsv_handle)
  460. jbd2_free_handle(handle->h_rsv_handle);
  461. jbd2_free_handle(handle);
  462. return ERR_PTR(err);
  463. }
  464. handle->h_type = type;
  465. handle->h_line_no = line_no;
  466. trace_jbd2_handle_start(journal->j_fs_dev->bd_dev,
  467. handle->h_transaction->t_tid, type,
  468. line_no, nblocks);
  469. return handle;
  470. }
  471. EXPORT_SYMBOL(jbd2__journal_start);
  472. /**
  473. * jbd2_journal_start() - Obtain a new handle.
  474. * @journal: Journal to start transaction on.
  475. * @nblocks: number of block buffer we might modify
  476. *
  477. * We make sure that the transaction can guarantee at least nblocks of
  478. * modified buffers in the log. We block until the log can guarantee
  479. * that much space. Additionally, if rsv_blocks > 0, we also create another
  480. * handle with rsv_blocks reserved blocks in the journal. This handle is
  481. * stored in h_rsv_handle. It is not attached to any particular transaction
  482. * and thus doesn't block transaction commit. If the caller uses this reserved
  483. * handle, it has to set h_rsv_handle to NULL as otherwise jbd2_journal_stop()
  484. * on the parent handle will dispose the reserved one. Reserved handle has to
  485. * be converted to a normal handle using jbd2_journal_start_reserved() before
  486. * it can be used.
  487. *
  488. * Return a pointer to a newly allocated handle, or an ERR_PTR() value
  489. * on failure.
  490. */
  491. handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
  492. {
  493. return jbd2__journal_start(journal, nblocks, 0, 0, GFP_NOFS, 0, 0);
  494. }
  495. EXPORT_SYMBOL(jbd2_journal_start);
  496. static void __jbd2_journal_unreserve_handle(handle_t *handle, transaction_t *t)
  497. {
  498. journal_t *journal = handle->h_journal;
  499. WARN_ON(!handle->h_reserved);
  500. sub_reserved_credits(journal, handle->h_total_credits);
  501. if (t)
  502. atomic_sub(handle->h_total_credits, &t->t_outstanding_credits);
  503. }
  504. void jbd2_journal_free_reserved(handle_t *handle)
  505. {
  506. journal_t *journal = handle->h_journal;
  507. /* Get j_state_lock to pin running transaction if it exists */
  508. read_lock(&journal->j_state_lock);
  509. __jbd2_journal_unreserve_handle(handle, journal->j_running_transaction);
  510. read_unlock(&journal->j_state_lock);
  511. jbd2_free_handle(handle);
  512. }
  513. EXPORT_SYMBOL(jbd2_journal_free_reserved);
  514. /**
  515. * jbd2_journal_start_reserved() - start reserved handle
  516. * @handle: handle to start
  517. * @type: for handle statistics
  518. * @line_no: for handle statistics
  519. *
  520. * Start handle that has been previously reserved with jbd2_journal_reserve().
  521. * This attaches @handle to the running transaction (or creates one if there's
  522. * not transaction running). Unlike jbd2_journal_start() this function cannot
  523. * block on journal commit, checkpointing, or similar stuff. It can block on
  524. * memory allocation or frozen journal though.
  525. *
  526. * Return 0 on success, non-zero on error - handle is freed in that case.
  527. */
  528. int jbd2_journal_start_reserved(handle_t *handle, unsigned int type,
  529. unsigned int line_no)
  530. {
  531. journal_t *journal = handle->h_journal;
  532. int ret = -EIO;
  533. if (WARN_ON(!handle->h_reserved)) {
  534. /* Someone passed in normal handle? Just stop it. */
  535. jbd2_journal_stop(handle);
  536. return ret;
  537. }
  538. /*
  539. * Usefulness of mixing of reserved and unreserved handles is
  540. * questionable. So far nobody seems to need it so just error out.
  541. */
  542. if (WARN_ON(current->journal_info)) {
  543. jbd2_journal_free_reserved(handle);
  544. return ret;
  545. }
  546. handle->h_journal = NULL;
  547. /*
  548. * GFP_NOFS is here because callers are likely from writeback or
  549. * similarly constrained call sites
  550. */
  551. ret = start_this_handle(journal, handle, GFP_NOFS);
  552. if (ret < 0) {
  553. handle->h_journal = journal;
  554. jbd2_journal_free_reserved(handle);
  555. return ret;
  556. }
  557. handle->h_type = type;
  558. handle->h_line_no = line_no;
  559. trace_jbd2_handle_start(journal->j_fs_dev->bd_dev,
  560. handle->h_transaction->t_tid, type,
  561. line_no, handle->h_total_credits);
  562. return 0;
  563. }
  564. EXPORT_SYMBOL(jbd2_journal_start_reserved);
  565. /**
  566. * jbd2_journal_extend() - extend buffer credits.
  567. * @handle: handle to 'extend'
  568. * @nblocks: nr blocks to try to extend by.
  569. * @revoke_records: number of revoke records to try to extend by.
  570. *
  571. * Some transactions, such as large extends and truncates, can be done
  572. * atomically all at once or in several stages. The operation requests
  573. * a credit for a number of buffer modifications in advance, but can
  574. * extend its credit if it needs more.
  575. *
  576. * jbd2_journal_extend tries to give the running handle more buffer credits.
  577. * It does not guarantee that allocation - this is a best-effort only.
  578. * The calling process MUST be able to deal cleanly with a failure to
  579. * extend here.
  580. *
  581. * Return 0 on success, non-zero on failure.
  582. *
  583. * return code < 0 implies an error
  584. * return code > 0 implies normal transaction-full status.
  585. */
  586. int jbd2_journal_extend(handle_t *handle, int nblocks, int revoke_records)
  587. {
  588. transaction_t *transaction = handle->h_transaction;
  589. journal_t *journal;
  590. int result;
  591. int wanted;
  592. if (is_handle_aborted(handle))
  593. return -EROFS;
  594. journal = transaction->t_journal;
  595. result = 1;
  596. read_lock(&journal->j_state_lock);
  597. /* Don't extend a locked-down transaction! */
  598. if (transaction->t_state != T_RUNNING) {
  599. jbd2_debug(3, "denied handle %p %d blocks: "
  600. "transaction not running\n", handle, nblocks);
  601. goto error_out;
  602. }
  603. nblocks += DIV_ROUND_UP(
  604. handle->h_revoke_credits_requested + revoke_records,
  605. journal->j_revoke_records_per_block) -
  606. DIV_ROUND_UP(
  607. handle->h_revoke_credits_requested,
  608. journal->j_revoke_records_per_block);
  609. wanted = atomic_add_return(nblocks,
  610. &transaction->t_outstanding_credits);
  611. if (wanted > journal->j_max_transaction_buffers) {
  612. jbd2_debug(3, "denied handle %p %d blocks: "
  613. "transaction too large\n", handle, nblocks);
  614. atomic_sub(nblocks, &transaction->t_outstanding_credits);
  615. goto error_out;
  616. }
  617. trace_jbd2_handle_extend(journal->j_fs_dev->bd_dev,
  618. transaction->t_tid,
  619. handle->h_type, handle->h_line_no,
  620. handle->h_total_credits,
  621. nblocks);
  622. handle->h_total_credits += nblocks;
  623. handle->h_requested_credits += nblocks;
  624. handle->h_revoke_credits += revoke_records;
  625. handle->h_revoke_credits_requested += revoke_records;
  626. result = 0;
  627. jbd2_debug(3, "extended handle %p by %d\n", handle, nblocks);
  628. error_out:
  629. read_unlock(&journal->j_state_lock);
  630. return result;
  631. }
  632. static void stop_this_handle(handle_t *handle)
  633. {
  634. transaction_t *transaction = handle->h_transaction;
  635. journal_t *journal = transaction->t_journal;
  636. int revokes;
  637. J_ASSERT(journal_current_handle() == handle);
  638. J_ASSERT(atomic_read(&transaction->t_updates) > 0);
  639. current->journal_info = NULL;
  640. /*
  641. * Subtract necessary revoke descriptor blocks from handle credits. We
  642. * take care to account only for revoke descriptor blocks the
  643. * transaction will really need as large sequences of transactions with
  644. * small numbers of revokes are relatively common.
  645. */
  646. revokes = handle->h_revoke_credits_requested - handle->h_revoke_credits;
  647. if (revokes) {
  648. int t_revokes, revoke_descriptors;
  649. int rr_per_blk = journal->j_revoke_records_per_block;
  650. WARN_ON_ONCE(DIV_ROUND_UP(revokes, rr_per_blk)
  651. > handle->h_total_credits);
  652. t_revokes = atomic_add_return(revokes,
  653. &transaction->t_outstanding_revokes);
  654. revoke_descriptors =
  655. DIV_ROUND_UP(t_revokes, rr_per_blk) -
  656. DIV_ROUND_UP(t_revokes - revokes, rr_per_blk);
  657. handle->h_total_credits -= revoke_descriptors;
  658. }
  659. atomic_sub(handle->h_total_credits,
  660. &transaction->t_outstanding_credits);
  661. if (handle->h_rsv_handle)
  662. __jbd2_journal_unreserve_handle(handle->h_rsv_handle,
  663. transaction);
  664. if (atomic_dec_and_test(&transaction->t_updates))
  665. wake_up(&journal->j_wait_updates);
  666. rwsem_release(&journal->j_trans_commit_map, _THIS_IP_);
  667. /*
  668. * Scope of the GFP_NOFS context is over here and so we can restore the
  669. * original alloc context.
  670. */
  671. memalloc_nofs_restore(handle->saved_alloc_context);
  672. }
  673. /**
  674. * jbd2__journal_restart() - restart a handle .
  675. * @handle: handle to restart
  676. * @nblocks: nr credits requested
  677. * @revoke_records: number of revoke record credits requested
  678. * @gfp_mask: memory allocation flags (for start_this_handle)
  679. *
  680. * Restart a handle for a multi-transaction filesystem
  681. * operation.
  682. *
  683. * If the jbd2_journal_extend() call above fails to grant new buffer credits
  684. * to a running handle, a call to jbd2_journal_restart will commit the
  685. * handle's transaction so far and reattach the handle to a new
  686. * transaction capable of guaranteeing the requested number of
  687. * credits. We preserve reserved handle if there's any attached to the
  688. * passed in handle.
  689. */
  690. int jbd2__journal_restart(handle_t *handle, int nblocks, int revoke_records,
  691. gfp_t gfp_mask)
  692. {
  693. transaction_t *transaction = handle->h_transaction;
  694. journal_t *journal;
  695. tid_t tid;
  696. int need_to_start;
  697. int ret;
  698. /* If we've had an abort of any type, don't even think about
  699. * actually doing the restart! */
  700. if (is_handle_aborted(handle))
  701. return 0;
  702. journal = transaction->t_journal;
  703. tid = transaction->t_tid;
  704. /*
  705. * First unlink the handle from its current transaction, and start the
  706. * commit on that.
  707. */
  708. jbd2_debug(2, "restarting handle %p\n", handle);
  709. stop_this_handle(handle);
  710. handle->h_transaction = NULL;
  711. /*
  712. * TODO: If we use READ_ONCE / WRITE_ONCE for j_commit_request we can
  713. * get rid of pointless j_state_lock traffic like this.
  714. */
  715. read_lock(&journal->j_state_lock);
  716. need_to_start = !tid_geq(journal->j_commit_request, tid);
  717. read_unlock(&journal->j_state_lock);
  718. if (need_to_start)
  719. jbd2_log_start_commit(journal, tid);
  720. handle->h_total_credits = nblocks +
  721. DIV_ROUND_UP(revoke_records,
  722. journal->j_revoke_records_per_block);
  723. handle->h_revoke_credits = revoke_records;
  724. ret = start_this_handle(journal, handle, gfp_mask);
  725. trace_jbd2_handle_restart(journal->j_fs_dev->bd_dev,
  726. ret ? 0 : handle->h_transaction->t_tid,
  727. handle->h_type, handle->h_line_no,
  728. handle->h_total_credits);
  729. return ret;
  730. }
  731. EXPORT_SYMBOL(jbd2__journal_restart);
  732. int jbd2_journal_restart(handle_t *handle, int nblocks)
  733. {
  734. return jbd2__journal_restart(handle, nblocks, 0, GFP_NOFS);
  735. }
  736. EXPORT_SYMBOL(jbd2_journal_restart);
  737. /*
  738. * Waits for any outstanding t_updates to finish.
  739. * This is called with write j_state_lock held.
  740. */
  741. void jbd2_journal_wait_updates(journal_t *journal)
  742. {
  743. DEFINE_WAIT(wait);
  744. while (1) {
  745. /*
  746. * Note that the running transaction can get freed under us if
  747. * this transaction is getting committed in
  748. * jbd2_journal_commit_transaction() ->
  749. * jbd2_journal_free_transaction(). This can only happen when we
  750. * release j_state_lock -> schedule() -> acquire j_state_lock.
  751. * Hence we should everytime retrieve new j_running_transaction
  752. * value (after j_state_lock release acquire cycle), else it may
  753. * lead to use-after-free of old freed transaction.
  754. */
  755. transaction_t *transaction = journal->j_running_transaction;
  756. if (!transaction)
  757. break;
  758. prepare_to_wait(&journal->j_wait_updates, &wait,
  759. TASK_UNINTERRUPTIBLE);
  760. if (!atomic_read(&transaction->t_updates)) {
  761. finish_wait(&journal->j_wait_updates, &wait);
  762. break;
  763. }
  764. write_unlock(&journal->j_state_lock);
  765. schedule();
  766. finish_wait(&journal->j_wait_updates, &wait);
  767. write_lock(&journal->j_state_lock);
  768. }
  769. }
  770. /**
  771. * jbd2_journal_lock_updates () - establish a transaction barrier.
  772. * @journal: Journal to establish a barrier on.
  773. *
  774. * This locks out any further updates from being started, and blocks
  775. * until all existing updates have completed, returning only once the
  776. * journal is in a quiescent state with no updates running.
  777. *
  778. * The journal lock should not be held on entry.
  779. */
  780. void jbd2_journal_lock_updates(journal_t *journal)
  781. {
  782. jbd2_might_wait_for_commit(journal);
  783. write_lock(&journal->j_state_lock);
  784. ++journal->j_barrier_count;
  785. /* Wait until there are no reserved handles */
  786. if (atomic_read(&journal->j_reserved_credits)) {
  787. write_unlock(&journal->j_state_lock);
  788. wait_event(journal->j_wait_reserved,
  789. atomic_read(&journal->j_reserved_credits) == 0);
  790. write_lock(&journal->j_state_lock);
  791. }
  792. /* Wait until there are no running t_updates */
  793. jbd2_journal_wait_updates(journal);
  794. write_unlock(&journal->j_state_lock);
  795. /*
  796. * We have now established a barrier against other normal updates, but
  797. * we also need to barrier against other jbd2_journal_lock_updates() calls
  798. * to make sure that we serialise special journal-locked operations
  799. * too.
  800. */
  801. mutex_lock(&journal->j_barrier);
  802. }
  803. /**
  804. * jbd2_journal_unlock_updates () - release barrier
  805. * @journal: Journal to release the barrier on.
  806. *
  807. * Release a transaction barrier obtained with jbd2_journal_lock_updates().
  808. *
  809. * Should be called without the journal lock held.
  810. */
  811. void jbd2_journal_unlock_updates (journal_t *journal)
  812. {
  813. J_ASSERT(journal->j_barrier_count != 0);
  814. mutex_unlock(&journal->j_barrier);
  815. write_lock(&journal->j_state_lock);
  816. --journal->j_barrier_count;
  817. write_unlock(&journal->j_state_lock);
  818. wake_up_all(&journal->j_wait_transaction_locked);
  819. }
  820. static void warn_dirty_buffer(struct buffer_head *bh)
  821. {
  822. printk(KERN_WARNING
  823. "JBD2: Spotted dirty metadata buffer (dev = %pg, blocknr = %llu). "
  824. "There's a risk of filesystem corruption in case of system "
  825. "crash.\n",
  826. bh->b_bdev, (unsigned long long)bh->b_blocknr);
  827. }
  828. /* Call t_frozen trigger and copy buffer data into jh->b_frozen_data. */
  829. static void jbd2_freeze_jh_data(struct journal_head *jh)
  830. {
  831. char *source;
  832. struct buffer_head *bh = jh2bh(jh);
  833. J_EXPECT_JH(jh, buffer_uptodate(bh), "Possible IO failure.\n");
  834. source = kmap_local_folio(bh->b_folio, bh_offset(bh));
  835. /* Fire data frozen trigger just before we copy the data */
  836. jbd2_buffer_frozen_trigger(jh, source, jh->b_triggers);
  837. memcpy(jh->b_frozen_data, source, bh->b_size);
  838. kunmap_local(source);
  839. /*
  840. * Now that the frozen data is saved off, we need to store any matching
  841. * triggers.
  842. */
  843. jh->b_frozen_triggers = jh->b_triggers;
  844. }
  845. /*
  846. * If the buffer is already part of the current transaction, then there
  847. * is nothing we need to do. If it is already part of a prior
  848. * transaction which we are still committing to disk, then we need to
  849. * make sure that we do not overwrite the old copy: we do copy-out to
  850. * preserve the copy going to disk. We also account the buffer against
  851. * the handle's metadata buffer credits (unless the buffer is already
  852. * part of the transaction, that is).
  853. *
  854. */
  855. static int
  856. do_get_write_access(handle_t *handle, struct journal_head *jh,
  857. int force_copy)
  858. {
  859. struct buffer_head *bh;
  860. transaction_t *transaction = handle->h_transaction;
  861. journal_t *journal;
  862. int error;
  863. char *frozen_buffer = NULL;
  864. unsigned long start_lock, time_lock;
  865. journal = transaction->t_journal;
  866. jbd2_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
  867. JBUFFER_TRACE(jh, "entry");
  868. repeat:
  869. bh = jh2bh(jh);
  870. /* @@@ Need to check for errors here at some point. */
  871. start_lock = jiffies;
  872. lock_buffer(bh);
  873. spin_lock(&jh->b_state_lock);
  874. /* If it takes too long to lock the buffer, trace it */
  875. time_lock = jbd2_time_diff(start_lock, jiffies);
  876. if (time_lock > HZ/10)
  877. trace_jbd2_lock_buffer_stall(bh->b_bdev->bd_dev,
  878. jiffies_to_msecs(time_lock));
  879. /* We now hold the buffer lock so it is safe to query the buffer
  880. * state. Is the buffer dirty?
  881. *
  882. * If so, there are two possibilities. The buffer may be
  883. * non-journaled, and undergoing a quite legitimate writeback.
  884. * Otherwise, it is journaled, and we don't expect dirty buffers
  885. * in that state (the buffers should be marked JBD_Dirty
  886. * instead.) So either the IO is being done under our own
  887. * control and this is a bug, or it's a third party IO such as
  888. * dump(8) (which may leave the buffer scheduled for read ---
  889. * ie. locked but not dirty) or tune2fs (which may actually have
  890. * the buffer dirtied, ugh.) */
  891. if (buffer_dirty(bh) && jh->b_transaction) {
  892. warn_dirty_buffer(bh);
  893. /*
  894. * We need to clean the dirty flag and we must do it under the
  895. * buffer lock to be sure we don't race with running write-out.
  896. */
  897. JBUFFER_TRACE(jh, "Journalling dirty buffer");
  898. clear_buffer_dirty(bh);
  899. /*
  900. * The buffer is going to be added to BJ_Reserved list now and
  901. * nothing guarantees jbd2_journal_dirty_metadata() will be
  902. * ever called for it. So we need to set jbddirty bit here to
  903. * make sure the buffer is dirtied and written out when the
  904. * journaling machinery is done with it.
  905. */
  906. set_buffer_jbddirty(bh);
  907. }
  908. error = -EROFS;
  909. if (is_handle_aborted(handle)) {
  910. spin_unlock(&jh->b_state_lock);
  911. unlock_buffer(bh);
  912. goto out;
  913. }
  914. error = 0;
  915. /*
  916. * The buffer is already part of this transaction if b_transaction or
  917. * b_next_transaction points to it
  918. */
  919. if (jh->b_transaction == transaction ||
  920. jh->b_next_transaction == transaction) {
  921. unlock_buffer(bh);
  922. goto done;
  923. }
  924. /*
  925. * this is the first time this transaction is touching this buffer,
  926. * reset the modified flag
  927. */
  928. jh->b_modified = 0;
  929. /*
  930. * If the buffer is not journaled right now, we need to make sure it
  931. * doesn't get written to disk before the caller actually commits the
  932. * new data
  933. */
  934. if (!jh->b_transaction) {
  935. JBUFFER_TRACE(jh, "no transaction");
  936. J_ASSERT_JH(jh, !jh->b_next_transaction);
  937. JBUFFER_TRACE(jh, "file as BJ_Reserved");
  938. /*
  939. * Make sure all stores to jh (b_modified, b_frozen_data) are
  940. * visible before attaching it to the running transaction.
  941. * Paired with barrier in jbd2_write_access_granted()
  942. */
  943. smp_wmb();
  944. spin_lock(&journal->j_list_lock);
  945. if (test_clear_buffer_dirty(bh)) {
  946. /*
  947. * Execute buffer dirty clearing and jh->b_transaction
  948. * assignment under journal->j_list_lock locked to
  949. * prevent bh being removed from checkpoint list if
  950. * the buffer is in an intermediate state (not dirty
  951. * and jh->b_transaction is NULL).
  952. */
  953. JBUFFER_TRACE(jh, "Journalling dirty buffer");
  954. set_buffer_jbddirty(bh);
  955. }
  956. __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
  957. spin_unlock(&journal->j_list_lock);
  958. unlock_buffer(bh);
  959. goto done;
  960. }
  961. unlock_buffer(bh);
  962. /*
  963. * If there is already a copy-out version of this buffer, then we don't
  964. * need to make another one
  965. */
  966. if (jh->b_frozen_data) {
  967. JBUFFER_TRACE(jh, "has frozen data");
  968. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  969. goto attach_next;
  970. }
  971. JBUFFER_TRACE(jh, "owned by older transaction");
  972. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  973. J_ASSERT_JH(jh, jh->b_transaction == journal->j_committing_transaction);
  974. /*
  975. * There is one case we have to be very careful about. If the
  976. * committing transaction is currently writing this buffer out to disk
  977. * and has NOT made a copy-out, then we cannot modify the buffer
  978. * contents at all right now. The essence of copy-out is that it is
  979. * the extra copy, not the primary copy, which gets journaled. If the
  980. * primary copy is already going to disk then we cannot do copy-out
  981. * here.
  982. */
  983. if (buffer_shadow(bh)) {
  984. JBUFFER_TRACE(jh, "on shadow: sleep");
  985. spin_unlock(&jh->b_state_lock);
  986. wait_on_bit_io(&bh->b_state, BH_Shadow, TASK_UNINTERRUPTIBLE);
  987. goto repeat;
  988. }
  989. /*
  990. * Only do the copy if the currently-owning transaction still needs it.
  991. * If buffer isn't on BJ_Metadata list, the committing transaction is
  992. * past that stage (here we use the fact that BH_Shadow is set under
  993. * bh_state lock together with refiling to BJ_Shadow list and at this
  994. * point we know the buffer doesn't have BH_Shadow set).
  995. *
  996. * Subtle point, though: if this is a get_undo_access, then we will be
  997. * relying on the frozen_data to contain the new value of the
  998. * committed_data record after the transaction, so we HAVE to force the
  999. * frozen_data copy in that case.
  1000. */
  1001. if (jh->b_jlist == BJ_Metadata || force_copy) {
  1002. JBUFFER_TRACE(jh, "generate frozen data");
  1003. if (!frozen_buffer) {
  1004. JBUFFER_TRACE(jh, "allocate memory for buffer");
  1005. spin_unlock(&jh->b_state_lock);
  1006. frozen_buffer = jbd2_alloc(jh2bh(jh)->b_size,
  1007. GFP_NOFS | __GFP_NOFAIL);
  1008. goto repeat;
  1009. }
  1010. jh->b_frozen_data = frozen_buffer;
  1011. frozen_buffer = NULL;
  1012. jbd2_freeze_jh_data(jh);
  1013. }
  1014. attach_next:
  1015. /*
  1016. * Make sure all stores to jh (b_modified, b_frozen_data) are visible
  1017. * before attaching it to the running transaction. Paired with barrier
  1018. * in jbd2_write_access_granted()
  1019. */
  1020. smp_wmb();
  1021. jh->b_next_transaction = transaction;
  1022. done:
  1023. spin_unlock(&jh->b_state_lock);
  1024. /*
  1025. * If we are about to journal a buffer, then any revoke pending on it is
  1026. * no longer valid
  1027. */
  1028. jbd2_journal_cancel_revoke(handle, jh);
  1029. out:
  1030. if (unlikely(frozen_buffer)) /* It's usually NULL */
  1031. jbd2_free(frozen_buffer, bh->b_size);
  1032. JBUFFER_TRACE(jh, "exit");
  1033. return error;
  1034. }
  1035. /* Fast check whether buffer is already attached to the required transaction */
  1036. static bool jbd2_write_access_granted(handle_t *handle, struct buffer_head *bh,
  1037. bool undo)
  1038. {
  1039. struct journal_head *jh;
  1040. bool ret = false;
  1041. /* Dirty buffers require special handling... */
  1042. if (buffer_dirty(bh))
  1043. return false;
  1044. /*
  1045. * RCU protects us from dereferencing freed pages. So the checks we do
  1046. * are guaranteed not to oops. However the jh slab object can get freed
  1047. * & reallocated while we work with it. So we have to be careful. When
  1048. * we see jh attached to the running transaction, we know it must stay
  1049. * so until the transaction is committed. Thus jh won't be freed and
  1050. * will be attached to the same bh while we run. However it can
  1051. * happen jh gets freed, reallocated, and attached to the transaction
  1052. * just after we get pointer to it from bh. So we have to be careful
  1053. * and recheck jh still belongs to our bh before we return success.
  1054. */
  1055. rcu_read_lock();
  1056. if (!buffer_jbd(bh))
  1057. goto out;
  1058. /* This should be bh2jh() but that doesn't work with inline functions */
  1059. jh = READ_ONCE(bh->b_private);
  1060. if (!jh)
  1061. goto out;
  1062. /* For undo access buffer must have data copied */
  1063. if (undo && !jh->b_committed_data)
  1064. goto out;
  1065. if (READ_ONCE(jh->b_transaction) != handle->h_transaction &&
  1066. READ_ONCE(jh->b_next_transaction) != handle->h_transaction)
  1067. goto out;
  1068. /*
  1069. * There are two reasons for the barrier here:
  1070. * 1) Make sure to fetch b_bh after we did previous checks so that we
  1071. * detect when jh went through free, realloc, attach to transaction
  1072. * while we were checking. Paired with implicit barrier in that path.
  1073. * 2) So that access to bh done after jbd2_write_access_granted()
  1074. * doesn't get reordered and see inconsistent state of concurrent
  1075. * do_get_write_access().
  1076. */
  1077. smp_mb();
  1078. if (unlikely(jh->b_bh != bh))
  1079. goto out;
  1080. ret = true;
  1081. out:
  1082. rcu_read_unlock();
  1083. return ret;
  1084. }
  1085. /**
  1086. * jbd2_journal_get_write_access() - notify intent to modify a buffer
  1087. * for metadata (not data) update.
  1088. * @handle: transaction to add buffer modifications to
  1089. * @bh: bh to be used for metadata writes
  1090. *
  1091. * Returns: error code or 0 on success.
  1092. *
  1093. * In full data journalling mode the buffer may be of type BJ_AsyncData,
  1094. * because we're ``write()ing`` a buffer which is also part of a shared mapping.
  1095. */
  1096. int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
  1097. {
  1098. struct journal_head *jh;
  1099. journal_t *journal;
  1100. int rc;
  1101. if (is_handle_aborted(handle))
  1102. return -EROFS;
  1103. journal = handle->h_transaction->t_journal;
  1104. if (jbd2_check_fs_dev_write_error(journal)) {
  1105. /*
  1106. * If the fs dev has writeback errors, it may have failed
  1107. * to async write out metadata buffers in the background.
  1108. * In this case, we could read old data from disk and write
  1109. * it out again, which may lead to on-disk filesystem
  1110. * inconsistency. Aborting journal can avoid it happen.
  1111. */
  1112. jbd2_journal_abort(journal, -EIO);
  1113. return -EIO;
  1114. }
  1115. if (jbd2_write_access_granted(handle, bh, false))
  1116. return 0;
  1117. jh = jbd2_journal_add_journal_head(bh);
  1118. /* We do not want to get caught playing with fields which the
  1119. * log thread also manipulates. Make sure that the buffer
  1120. * completes any outstanding IO before proceeding. */
  1121. rc = do_get_write_access(handle, jh, 0);
  1122. jbd2_journal_put_journal_head(jh);
  1123. return rc;
  1124. }
  1125. /*
  1126. * When the user wants to journal a newly created buffer_head
  1127. * (ie. getblk() returned a new buffer and we are going to populate it
  1128. * manually rather than reading off disk), then we need to keep the
  1129. * buffer_head locked until it has been completely filled with new
  1130. * data. In this case, we should be able to make the assertion that
  1131. * the bh is not already part of an existing transaction.
  1132. *
  1133. * The buffer should already be locked by the caller by this point.
  1134. * There is no lock ranking violation: it was a newly created,
  1135. * unlocked buffer beforehand. */
  1136. /**
  1137. * jbd2_journal_get_create_access () - notify intent to use newly created bh
  1138. * @handle: transaction to new buffer to
  1139. * @bh: new buffer.
  1140. *
  1141. * Call this if you create a new bh.
  1142. */
  1143. int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
  1144. {
  1145. transaction_t *transaction = handle->h_transaction;
  1146. journal_t *journal;
  1147. struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  1148. int err;
  1149. jbd2_debug(5, "journal_head %p\n", jh);
  1150. err = -EROFS;
  1151. if (is_handle_aborted(handle))
  1152. goto out;
  1153. journal = transaction->t_journal;
  1154. err = 0;
  1155. JBUFFER_TRACE(jh, "entry");
  1156. /*
  1157. * The buffer may already belong to this transaction due to pre-zeroing
  1158. * in the filesystem's new_block code. It may also be on the previous,
  1159. * committing transaction's lists, but it HAS to be in Forget state in
  1160. * that case: the transaction must have deleted the buffer for it to be
  1161. * reused here.
  1162. */
  1163. spin_lock(&jh->b_state_lock);
  1164. J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
  1165. jh->b_transaction == NULL ||
  1166. (jh->b_transaction == journal->j_committing_transaction &&
  1167. jh->b_jlist == BJ_Forget)));
  1168. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  1169. J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
  1170. if (jh->b_transaction == NULL) {
  1171. /*
  1172. * Previous jbd2_journal_forget() could have left the buffer
  1173. * with jbddirty bit set because it was being committed. When
  1174. * the commit finished, we've filed the buffer for
  1175. * checkpointing and marked it dirty. Now we are reallocating
  1176. * the buffer so the transaction freeing it must have
  1177. * committed and so it's safe to clear the dirty bit.
  1178. */
  1179. clear_buffer_dirty(jh2bh(jh));
  1180. /* first access by this transaction */
  1181. jh->b_modified = 0;
  1182. JBUFFER_TRACE(jh, "file as BJ_Reserved");
  1183. spin_lock(&journal->j_list_lock);
  1184. __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
  1185. spin_unlock(&journal->j_list_lock);
  1186. } else if (jh->b_transaction == journal->j_committing_transaction) {
  1187. /* first access by this transaction */
  1188. jh->b_modified = 0;
  1189. JBUFFER_TRACE(jh, "set next transaction");
  1190. spin_lock(&journal->j_list_lock);
  1191. jh->b_next_transaction = transaction;
  1192. spin_unlock(&journal->j_list_lock);
  1193. }
  1194. spin_unlock(&jh->b_state_lock);
  1195. /*
  1196. * akpm: I added this. ext3_alloc_branch can pick up new indirect
  1197. * blocks which contain freed but then revoked metadata. We need
  1198. * to cancel the revoke in case we end up freeing it yet again
  1199. * and the reallocating as data - this would cause a second revoke,
  1200. * which hits an assertion error.
  1201. */
  1202. JBUFFER_TRACE(jh, "cancelling revoke");
  1203. jbd2_journal_cancel_revoke(handle, jh);
  1204. out:
  1205. jbd2_journal_put_journal_head(jh);
  1206. return err;
  1207. }
  1208. /**
  1209. * jbd2_journal_get_undo_access() - Notify intent to modify metadata with
  1210. * non-rewindable consequences
  1211. * @handle: transaction
  1212. * @bh: buffer to undo
  1213. *
  1214. * Sometimes there is a need to distinguish between metadata which has
  1215. * been committed to disk and that which has not. The ext3fs code uses
  1216. * this for freeing and allocating space, we have to make sure that we
  1217. * do not reuse freed space until the deallocation has been committed,
  1218. * since if we overwrote that space we would make the delete
  1219. * un-rewindable in case of a crash.
  1220. *
  1221. * To deal with that, jbd2_journal_get_undo_access requests write access to a
  1222. * buffer for parts of non-rewindable operations such as delete
  1223. * operations on the bitmaps. The journaling code must keep a copy of
  1224. * the buffer's contents prior to the undo_access call until such time
  1225. * as we know that the buffer has definitely been committed to disk.
  1226. *
  1227. * We never need to know which transaction the committed data is part
  1228. * of, buffers touched here are guaranteed to be dirtied later and so
  1229. * will be committed to a new transaction in due course, at which point
  1230. * we can discard the old committed data pointer.
  1231. *
  1232. * Returns error number or 0 on success.
  1233. */
  1234. int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
  1235. {
  1236. int err;
  1237. struct journal_head *jh;
  1238. char *committed_data = NULL;
  1239. if (is_handle_aborted(handle))
  1240. return -EROFS;
  1241. if (jbd2_write_access_granted(handle, bh, true))
  1242. return 0;
  1243. jh = jbd2_journal_add_journal_head(bh);
  1244. JBUFFER_TRACE(jh, "entry");
  1245. /*
  1246. * Do this first --- it can drop the journal lock, so we want to
  1247. * make sure that obtaining the committed_data is done
  1248. * atomically wrt. completion of any outstanding commits.
  1249. */
  1250. err = do_get_write_access(handle, jh, 1);
  1251. if (err)
  1252. goto out;
  1253. repeat:
  1254. if (!jh->b_committed_data)
  1255. committed_data = jbd2_alloc(jh2bh(jh)->b_size,
  1256. GFP_NOFS|__GFP_NOFAIL);
  1257. spin_lock(&jh->b_state_lock);
  1258. if (!jh->b_committed_data) {
  1259. /* Copy out the current buffer contents into the
  1260. * preserved, committed copy. */
  1261. JBUFFER_TRACE(jh, "generate b_committed data");
  1262. if (!committed_data) {
  1263. spin_unlock(&jh->b_state_lock);
  1264. goto repeat;
  1265. }
  1266. jh->b_committed_data = committed_data;
  1267. committed_data = NULL;
  1268. memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
  1269. }
  1270. spin_unlock(&jh->b_state_lock);
  1271. out:
  1272. jbd2_journal_put_journal_head(jh);
  1273. if (unlikely(committed_data))
  1274. jbd2_free(committed_data, bh->b_size);
  1275. return err;
  1276. }
  1277. /**
  1278. * jbd2_journal_set_triggers() - Add triggers for commit writeout
  1279. * @bh: buffer to trigger on
  1280. * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
  1281. *
  1282. * Set any triggers on this journal_head. This is always safe, because
  1283. * triggers for a committing buffer will be saved off, and triggers for
  1284. * a running transaction will match the buffer in that transaction.
  1285. *
  1286. * Call with NULL to clear the triggers.
  1287. */
  1288. void jbd2_journal_set_triggers(struct buffer_head *bh,
  1289. struct jbd2_buffer_trigger_type *type)
  1290. {
  1291. struct journal_head *jh = jbd2_journal_grab_journal_head(bh);
  1292. if (WARN_ON_ONCE(!jh))
  1293. return;
  1294. jh->b_triggers = type;
  1295. jbd2_journal_put_journal_head(jh);
  1296. }
  1297. void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
  1298. struct jbd2_buffer_trigger_type *triggers)
  1299. {
  1300. struct buffer_head *bh = jh2bh(jh);
  1301. if (!triggers || !triggers->t_frozen)
  1302. return;
  1303. triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
  1304. }
  1305. void jbd2_buffer_abort_trigger(struct journal_head *jh,
  1306. struct jbd2_buffer_trigger_type *triggers)
  1307. {
  1308. if (!triggers || !triggers->t_abort)
  1309. return;
  1310. triggers->t_abort(triggers, jh2bh(jh));
  1311. }
  1312. /**
  1313. * jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
  1314. * @handle: transaction to add buffer to.
  1315. * @bh: buffer to mark
  1316. *
  1317. * mark dirty metadata which needs to be journaled as part of the current
  1318. * transaction.
  1319. *
  1320. * The buffer must have previously had jbd2_journal_get_write_access()
  1321. * called so that it has a valid journal_head attached to the buffer
  1322. * head.
  1323. *
  1324. * The buffer is placed on the transaction's metadata list and is marked
  1325. * as belonging to the transaction.
  1326. *
  1327. * Returns error number or 0 on success.
  1328. *
  1329. * Special care needs to be taken if the buffer already belongs to the
  1330. * current committing transaction (in which case we should have frozen
  1331. * data present for that commit). In that case, we don't relink the
  1332. * buffer: that only gets done when the old transaction finally
  1333. * completes its commit.
  1334. */
  1335. int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
  1336. {
  1337. transaction_t *transaction = handle->h_transaction;
  1338. journal_t *journal;
  1339. struct journal_head *jh;
  1340. int ret = 0;
  1341. if (!buffer_jbd(bh))
  1342. return -EUCLEAN;
  1343. /*
  1344. * We don't grab jh reference here since the buffer must be part
  1345. * of the running transaction.
  1346. */
  1347. jh = bh2jh(bh);
  1348. jbd2_debug(5, "journal_head %p\n", jh);
  1349. JBUFFER_TRACE(jh, "entry");
  1350. /*
  1351. * This and the following assertions are unreliable since we may see jh
  1352. * in inconsistent state unless we grab bh_state lock. But this is
  1353. * crucial to catch bugs so let's do a reliable check until the
  1354. * lockless handling is fully proven.
  1355. */
  1356. if (data_race(jh->b_transaction != transaction &&
  1357. jh->b_next_transaction != transaction)) {
  1358. spin_lock(&jh->b_state_lock);
  1359. J_ASSERT_JH(jh, jh->b_transaction == transaction ||
  1360. jh->b_next_transaction == transaction);
  1361. spin_unlock(&jh->b_state_lock);
  1362. }
  1363. if (data_race(jh->b_modified == 1)) {
  1364. /* If it's in our transaction it must be in BJ_Metadata list. */
  1365. if (data_race(jh->b_transaction == transaction &&
  1366. jh->b_jlist != BJ_Metadata)) {
  1367. spin_lock(&jh->b_state_lock);
  1368. if (jh->b_transaction == transaction &&
  1369. jh->b_jlist != BJ_Metadata)
  1370. pr_err("JBD2: assertion failure: h_type=%u "
  1371. "h_line_no=%u block_no=%llu jlist=%u\n",
  1372. handle->h_type, handle->h_line_no,
  1373. (unsigned long long) bh->b_blocknr,
  1374. jh->b_jlist);
  1375. J_ASSERT_JH(jh, jh->b_transaction != transaction ||
  1376. jh->b_jlist == BJ_Metadata);
  1377. spin_unlock(&jh->b_state_lock);
  1378. }
  1379. goto out;
  1380. }
  1381. spin_lock(&jh->b_state_lock);
  1382. if (is_handle_aborted(handle)) {
  1383. /*
  1384. * Check journal aborting with @jh->b_state_lock locked,
  1385. * since 'jh->b_transaction' could be replaced with
  1386. * 'jh->b_next_transaction' during old transaction
  1387. * committing if journal aborted, which may fail
  1388. * assertion on 'jh->b_frozen_data == NULL'.
  1389. */
  1390. ret = -EROFS;
  1391. goto out_unlock_bh;
  1392. }
  1393. journal = transaction->t_journal;
  1394. if (jh->b_modified == 0) {
  1395. /*
  1396. * This buffer's got modified and becoming part
  1397. * of the transaction. This needs to be done
  1398. * once a transaction -bzzz
  1399. */
  1400. if (WARN_ON_ONCE(jbd2_handle_buffer_credits(handle) <= 0)) {
  1401. ret = -ENOSPC;
  1402. goto out_unlock_bh;
  1403. }
  1404. jh->b_modified = 1;
  1405. handle->h_total_credits--;
  1406. }
  1407. /*
  1408. * fastpath, to avoid expensive locking. If this buffer is already
  1409. * on the running transaction's metadata list there is nothing to do.
  1410. * Nobody can take it off again because there is a handle open.
  1411. * I _think_ we're OK here with SMP barriers - a mistaken decision will
  1412. * result in this test being false, so we go in and take the locks.
  1413. */
  1414. if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
  1415. JBUFFER_TRACE(jh, "fastpath");
  1416. if (unlikely(jh->b_transaction !=
  1417. journal->j_running_transaction)) {
  1418. printk(KERN_ERR "JBD2: %s: "
  1419. "jh->b_transaction (%llu, %p, %u) != "
  1420. "journal->j_running_transaction (%p, %u)\n",
  1421. journal->j_devname,
  1422. (unsigned long long) bh->b_blocknr,
  1423. jh->b_transaction,
  1424. jh->b_transaction ? jh->b_transaction->t_tid : 0,
  1425. journal->j_running_transaction,
  1426. journal->j_running_transaction ?
  1427. journal->j_running_transaction->t_tid : 0);
  1428. ret = -EINVAL;
  1429. }
  1430. goto out_unlock_bh;
  1431. }
  1432. set_buffer_jbddirty(bh);
  1433. /*
  1434. * Metadata already on the current transaction list doesn't
  1435. * need to be filed. Metadata on another transaction's list must
  1436. * be committing, and will be refiled once the commit completes:
  1437. * leave it alone for now.
  1438. */
  1439. if (jh->b_transaction != transaction) {
  1440. JBUFFER_TRACE(jh, "already on other transaction");
  1441. if (unlikely(((jh->b_transaction !=
  1442. journal->j_committing_transaction)) ||
  1443. (jh->b_next_transaction != transaction))) {
  1444. printk(KERN_ERR "jbd2_journal_dirty_metadata: %s: "
  1445. "bad jh for block %llu: "
  1446. "transaction (%p, %u), "
  1447. "jh->b_transaction (%p, %u), "
  1448. "jh->b_next_transaction (%p, %u), jlist %u\n",
  1449. journal->j_devname,
  1450. (unsigned long long) bh->b_blocknr,
  1451. transaction, transaction->t_tid,
  1452. jh->b_transaction,
  1453. jh->b_transaction ?
  1454. jh->b_transaction->t_tid : 0,
  1455. jh->b_next_transaction,
  1456. jh->b_next_transaction ?
  1457. jh->b_next_transaction->t_tid : 0,
  1458. jh->b_jlist);
  1459. WARN_ON(1);
  1460. ret = -EINVAL;
  1461. }
  1462. /* And this case is illegal: we can't reuse another
  1463. * transaction's data buffer, ever. */
  1464. goto out_unlock_bh;
  1465. }
  1466. /* That test should have eliminated the following case: */
  1467. J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
  1468. JBUFFER_TRACE(jh, "file as BJ_Metadata");
  1469. spin_lock(&journal->j_list_lock);
  1470. __jbd2_journal_file_buffer(jh, transaction, BJ_Metadata);
  1471. spin_unlock(&journal->j_list_lock);
  1472. out_unlock_bh:
  1473. spin_unlock(&jh->b_state_lock);
  1474. out:
  1475. JBUFFER_TRACE(jh, "exit");
  1476. return ret;
  1477. }
  1478. /**
  1479. * jbd2_journal_forget() - bforget() for potentially-journaled buffers.
  1480. * @handle: transaction handle
  1481. * @bh: bh to 'forget'
  1482. *
  1483. * We can only do the bforget if there are no commits pending against the
  1484. * buffer. If the buffer is dirty in the current running transaction we
  1485. * can safely unlink it.
  1486. *
  1487. * bh may not be a journalled buffer at all - it may be a non-JBD
  1488. * buffer which came off the hashtable. Check for this.
  1489. *
  1490. * Decrements bh->b_count by one.
  1491. *
  1492. * Allow this call even if the handle has aborted --- it may be part of
  1493. * the caller's cleanup after an abort.
  1494. */
  1495. int jbd2_journal_forget(handle_t *handle, struct buffer_head *bh)
  1496. {
  1497. transaction_t *transaction = handle->h_transaction;
  1498. journal_t *journal;
  1499. struct journal_head *jh;
  1500. int drop_reserve = 0;
  1501. int err = 0;
  1502. int was_modified = 0;
  1503. int wait_for_writeback = 0;
  1504. if (is_handle_aborted(handle))
  1505. return -EROFS;
  1506. journal = transaction->t_journal;
  1507. BUFFER_TRACE(bh, "entry");
  1508. jh = jbd2_journal_grab_journal_head(bh);
  1509. if (!jh) {
  1510. __bforget(bh);
  1511. return 0;
  1512. }
  1513. spin_lock(&jh->b_state_lock);
  1514. /* Critical error: attempting to delete a bitmap buffer, maybe?
  1515. * Don't do any jbd operations, and return an error. */
  1516. if (!J_EXPECT_JH(jh, !jh->b_committed_data,
  1517. "inconsistent data on disk")) {
  1518. err = -EIO;
  1519. goto drop;
  1520. }
  1521. /* keep track of whether or not this transaction modified us */
  1522. was_modified = jh->b_modified;
  1523. /*
  1524. * The buffer's going from the transaction, we must drop
  1525. * all references -bzzz
  1526. */
  1527. jh->b_modified = 0;
  1528. if (jh->b_transaction == transaction) {
  1529. J_ASSERT_JH(jh, !jh->b_frozen_data);
  1530. /* If we are forgetting a buffer which is already part
  1531. * of this transaction, then we can just drop it from
  1532. * the transaction immediately. */
  1533. clear_buffer_dirty(bh);
  1534. clear_buffer_jbddirty(bh);
  1535. JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
  1536. /*
  1537. * we only want to drop a reference if this transaction
  1538. * modified the buffer
  1539. */
  1540. if (was_modified)
  1541. drop_reserve = 1;
  1542. /*
  1543. * We are no longer going to journal this buffer.
  1544. * However, the commit of this transaction is still
  1545. * important to the buffer: the delete that we are now
  1546. * processing might obsolete an old log entry, so by
  1547. * committing, we can satisfy the buffer's checkpoint.
  1548. *
  1549. * So, if we have a checkpoint on the buffer, we should
  1550. * now refile the buffer on our BJ_Forget list so that
  1551. * we know to remove the checkpoint after we commit.
  1552. */
  1553. spin_lock(&journal->j_list_lock);
  1554. if (jh->b_cp_transaction) {
  1555. __jbd2_journal_temp_unlink_buffer(jh);
  1556. __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
  1557. } else {
  1558. __jbd2_journal_unfile_buffer(jh);
  1559. jbd2_journal_put_journal_head(jh);
  1560. }
  1561. spin_unlock(&journal->j_list_lock);
  1562. } else if (jh->b_transaction) {
  1563. J_ASSERT_JH(jh, (jh->b_transaction ==
  1564. journal->j_committing_transaction));
  1565. /* However, if the buffer is still owned by a prior
  1566. * (committing) transaction, we can't drop it yet... */
  1567. JBUFFER_TRACE(jh, "belongs to older transaction");
  1568. /* ... but we CAN drop it from the new transaction through
  1569. * marking the buffer as freed and set j_next_transaction to
  1570. * the new transaction, so that not only the commit code
  1571. * knows it should clear dirty bits when it is done with the
  1572. * buffer, but also the buffer can be checkpointed only
  1573. * after the new transaction commits. */
  1574. set_buffer_freed(bh);
  1575. if (!jh->b_next_transaction) {
  1576. spin_lock(&journal->j_list_lock);
  1577. jh->b_next_transaction = transaction;
  1578. spin_unlock(&journal->j_list_lock);
  1579. } else {
  1580. J_ASSERT(jh->b_next_transaction == transaction);
  1581. /*
  1582. * only drop a reference if this transaction modified
  1583. * the buffer
  1584. */
  1585. if (was_modified)
  1586. drop_reserve = 1;
  1587. }
  1588. } else {
  1589. /*
  1590. * Finally, if the buffer is not belongs to any
  1591. * transaction, we can just drop it now if it has no
  1592. * checkpoint.
  1593. */
  1594. spin_lock(&journal->j_list_lock);
  1595. if (!jh->b_cp_transaction) {
  1596. JBUFFER_TRACE(jh, "belongs to none transaction");
  1597. spin_unlock(&journal->j_list_lock);
  1598. goto drop;
  1599. }
  1600. /*
  1601. * Otherwise, if the buffer has been written to disk,
  1602. * it is safe to remove the checkpoint and drop it.
  1603. */
  1604. if (jbd2_journal_try_remove_checkpoint(jh) >= 0) {
  1605. spin_unlock(&journal->j_list_lock);
  1606. goto drop;
  1607. }
  1608. /*
  1609. * The buffer has not yet been written to disk. We should
  1610. * either clear the buffer or ensure that the ongoing I/O
  1611. * is completed, and attach this buffer to current
  1612. * transaction so that the buffer can be checkpointed only
  1613. * after the current transaction commits.
  1614. */
  1615. clear_buffer_dirty(bh);
  1616. wait_for_writeback = 1;
  1617. __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
  1618. spin_unlock(&journal->j_list_lock);
  1619. }
  1620. drop:
  1621. __brelse(bh);
  1622. spin_unlock(&jh->b_state_lock);
  1623. if (wait_for_writeback)
  1624. wait_on_buffer(bh);
  1625. jbd2_journal_put_journal_head(jh);
  1626. if (drop_reserve) {
  1627. /* no need to reserve log space for this block -bzzz */
  1628. handle->h_total_credits++;
  1629. }
  1630. return err;
  1631. }
  1632. /**
  1633. * jbd2_journal_stop() - complete a transaction
  1634. * @handle: transaction to complete.
  1635. *
  1636. * All done for a particular handle.
  1637. *
  1638. * There is not much action needed here. We just return any remaining
  1639. * buffer credits to the transaction and remove the handle. The only
  1640. * complication is that we need to start a commit operation if the
  1641. * filesystem is marked for synchronous update.
  1642. *
  1643. * jbd2_journal_stop itself will not usually return an error, but it may
  1644. * do so in unusual circumstances. In particular, expect it to
  1645. * return -EIO if a jbd2_journal_abort has been executed since the
  1646. * transaction began.
  1647. */
  1648. int jbd2_journal_stop(handle_t *handle)
  1649. {
  1650. transaction_t *transaction = handle->h_transaction;
  1651. journal_t *journal;
  1652. int err = 0, wait_for_commit = 0;
  1653. tid_t tid;
  1654. pid_t pid;
  1655. if (--handle->h_ref > 0) {
  1656. jbd2_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
  1657. handle->h_ref);
  1658. if (is_handle_aborted(handle))
  1659. return -EIO;
  1660. return 0;
  1661. }
  1662. if (!transaction) {
  1663. /*
  1664. * Handle is already detached from the transaction so there is
  1665. * nothing to do other than free the handle.
  1666. */
  1667. memalloc_nofs_restore(handle->saved_alloc_context);
  1668. goto free_and_exit;
  1669. }
  1670. journal = transaction->t_journal;
  1671. tid = transaction->t_tid;
  1672. if (is_handle_aborted(handle))
  1673. err = -EIO;
  1674. jbd2_debug(4, "Handle %p going down\n", handle);
  1675. trace_jbd2_handle_stats(journal->j_fs_dev->bd_dev,
  1676. tid, handle->h_type, handle->h_line_no,
  1677. jiffies - handle->h_start_jiffies,
  1678. handle->h_sync, handle->h_requested_credits,
  1679. (handle->h_requested_credits -
  1680. handle->h_total_credits));
  1681. /*
  1682. * Implement synchronous transaction batching. If the handle
  1683. * was synchronous, don't force a commit immediately. Let's
  1684. * yield and let another thread piggyback onto this
  1685. * transaction. Keep doing that while new threads continue to
  1686. * arrive. It doesn't cost much - we're about to run a commit
  1687. * and sleep on IO anyway. Speeds up many-threaded, many-dir
  1688. * operations by 30x or more...
  1689. *
  1690. * We try and optimize the sleep time against what the
  1691. * underlying disk can do, instead of having a static sleep
  1692. * time. This is useful for the case where our storage is so
  1693. * fast that it is more optimal to go ahead and force a flush
  1694. * and wait for the transaction to be committed than it is to
  1695. * wait for an arbitrary amount of time for new writers to
  1696. * join the transaction. We achieve this by measuring how
  1697. * long it takes to commit a transaction, and compare it with
  1698. * how long this transaction has been running, and if run time
  1699. * < commit time then we sleep for the delta and commit. This
  1700. * greatly helps super fast disks that would see slowdowns as
  1701. * more threads started doing fsyncs.
  1702. *
  1703. * But don't do this if this process was the most recent one
  1704. * to perform a synchronous write. We do this to detect the
  1705. * case where a single process is doing a stream of sync
  1706. * writes. No point in waiting for joiners in that case.
  1707. *
  1708. * Setting max_batch_time to 0 disables this completely.
  1709. */
  1710. pid = current->pid;
  1711. if (handle->h_sync && journal->j_last_sync_writer != pid &&
  1712. journal->j_max_batch_time) {
  1713. u64 commit_time, trans_time;
  1714. journal->j_last_sync_writer = pid;
  1715. read_lock(&journal->j_state_lock);
  1716. commit_time = journal->j_average_commit_time;
  1717. read_unlock(&journal->j_state_lock);
  1718. trans_time = ktime_to_ns(ktime_sub(ktime_get(),
  1719. transaction->t_start_time));
  1720. commit_time = max_t(u64, commit_time,
  1721. 1000*journal->j_min_batch_time);
  1722. commit_time = min_t(u64, commit_time,
  1723. 1000*journal->j_max_batch_time);
  1724. if (trans_time < commit_time) {
  1725. ktime_t expires = ktime_add_ns(ktime_get(),
  1726. commit_time);
  1727. set_current_state(TASK_UNINTERRUPTIBLE);
  1728. schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
  1729. }
  1730. }
  1731. if (handle->h_sync)
  1732. transaction->t_synchronous_commit = 1;
  1733. /*
  1734. * If the handle is marked SYNC, we need to set another commit
  1735. * going! We also want to force a commit if the transaction is too
  1736. * old now.
  1737. */
  1738. if (handle->h_sync ||
  1739. time_after_eq(jiffies, transaction->t_expires)) {
  1740. /* Do this even for aborted journals: an abort still
  1741. * completes the commit thread, it just doesn't write
  1742. * anything to disk. */
  1743. jbd2_debug(2, "transaction too old, requesting commit for "
  1744. "handle %p\n", handle);
  1745. /* This is non-blocking */
  1746. jbd2_log_start_commit(journal, tid);
  1747. /*
  1748. * Special case: JBD2_SYNC synchronous updates require us
  1749. * to wait for the commit to complete.
  1750. */
  1751. if (handle->h_sync && !(current->flags & PF_MEMALLOC))
  1752. wait_for_commit = 1;
  1753. }
  1754. /*
  1755. * Once stop_this_handle() drops t_updates, the transaction could start
  1756. * committing on us and eventually disappear. So we must not
  1757. * dereference transaction pointer again after calling
  1758. * stop_this_handle().
  1759. */
  1760. stop_this_handle(handle);
  1761. if (wait_for_commit)
  1762. err = jbd2_log_wait_commit(journal, tid);
  1763. free_and_exit:
  1764. if (handle->h_rsv_handle)
  1765. jbd2_free_handle(handle->h_rsv_handle);
  1766. jbd2_free_handle(handle);
  1767. return err;
  1768. }
  1769. /*
  1770. *
  1771. * List management code snippets: various functions for manipulating the
  1772. * transaction buffer lists.
  1773. *
  1774. */
  1775. /*
  1776. * Append a buffer to a transaction list, given the transaction's list head
  1777. * pointer.
  1778. *
  1779. * j_list_lock is held.
  1780. *
  1781. * jh->b_state_lock is held.
  1782. */
  1783. static inline void
  1784. __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
  1785. {
  1786. if (!*list) {
  1787. jh->b_tnext = jh->b_tprev = jh;
  1788. *list = jh;
  1789. } else {
  1790. /* Insert at the tail of the list to preserve order */
  1791. struct journal_head *first = *list, *last = first->b_tprev;
  1792. jh->b_tprev = last;
  1793. jh->b_tnext = first;
  1794. last->b_tnext = first->b_tprev = jh;
  1795. }
  1796. }
  1797. /*
  1798. * Remove a buffer from a transaction list, given the transaction's list
  1799. * head pointer.
  1800. *
  1801. * Called with j_list_lock held, and the journal may not be locked.
  1802. *
  1803. * jh->b_state_lock is held.
  1804. */
  1805. static inline void
  1806. __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
  1807. {
  1808. if (*list == jh) {
  1809. *list = jh->b_tnext;
  1810. if (*list == jh)
  1811. *list = NULL;
  1812. }
  1813. jh->b_tprev->b_tnext = jh->b_tnext;
  1814. jh->b_tnext->b_tprev = jh->b_tprev;
  1815. }
  1816. /*
  1817. * Remove a buffer from the appropriate transaction list.
  1818. *
  1819. * Note that this function can *change* the value of
  1820. * bh->b_transaction->t_buffers, t_forget, t_shadow_list, t_log_list or
  1821. * t_reserved_list. If the caller is holding onto a copy of one of these
  1822. * pointers, it could go bad. Generally the caller needs to re-read the
  1823. * pointer from the transaction_t.
  1824. *
  1825. * Called under j_list_lock.
  1826. */
  1827. static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
  1828. {
  1829. struct journal_head **list = NULL;
  1830. transaction_t *transaction;
  1831. struct buffer_head *bh = jh2bh(jh);
  1832. lockdep_assert_held(&jh->b_state_lock);
  1833. transaction = jh->b_transaction;
  1834. if (transaction)
  1835. assert_spin_locked(&transaction->t_journal->j_list_lock);
  1836. J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
  1837. if (jh->b_jlist != BJ_None)
  1838. J_ASSERT_JH(jh, transaction != NULL);
  1839. switch (jh->b_jlist) {
  1840. case BJ_None:
  1841. return;
  1842. case BJ_Metadata:
  1843. transaction->t_nr_buffers--;
  1844. J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
  1845. list = &transaction->t_buffers;
  1846. break;
  1847. case BJ_Forget:
  1848. list = &transaction->t_forget;
  1849. break;
  1850. case BJ_Shadow:
  1851. list = &transaction->t_shadow_list;
  1852. break;
  1853. case BJ_Reserved:
  1854. list = &transaction->t_reserved_list;
  1855. break;
  1856. }
  1857. __blist_del_buffer(list, jh);
  1858. jh->b_jlist = BJ_None;
  1859. if (transaction && is_journal_aborted(transaction->t_journal))
  1860. clear_buffer_jbddirty(bh);
  1861. else if (test_clear_buffer_jbddirty(bh))
  1862. mark_buffer_dirty(bh); /* Expose it to the VM */
  1863. }
  1864. /*
  1865. * Remove buffer from all transactions. The caller is responsible for dropping
  1866. * the jh reference that belonged to the transaction.
  1867. *
  1868. * Called with bh_state lock and j_list_lock
  1869. */
  1870. static void __jbd2_journal_unfile_buffer(struct journal_head *jh)
  1871. {
  1872. J_ASSERT_JH(jh, jh->b_transaction != NULL);
  1873. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  1874. __jbd2_journal_temp_unlink_buffer(jh);
  1875. jh->b_transaction = NULL;
  1876. }
  1877. void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
  1878. {
  1879. struct buffer_head *bh = jh2bh(jh);
  1880. /* Get reference so that buffer cannot be freed before we unlock it */
  1881. get_bh(bh);
  1882. spin_lock(&jh->b_state_lock);
  1883. spin_lock(&journal->j_list_lock);
  1884. __jbd2_journal_unfile_buffer(jh);
  1885. spin_unlock(&journal->j_list_lock);
  1886. spin_unlock(&jh->b_state_lock);
  1887. jbd2_journal_put_journal_head(jh);
  1888. __brelse(bh);
  1889. }
  1890. /**
  1891. * jbd2_journal_try_to_free_buffers() - try to free page buffers.
  1892. * @journal: journal for operation
  1893. * @folio: Folio to detach data from.
  1894. *
  1895. * For all the buffers on this page,
  1896. * if they are fully written out ordered data, move them onto BUF_CLEAN
  1897. * so try_to_free_buffers() can reap them.
  1898. *
  1899. * This function returns non-zero if we wish try_to_free_buffers()
  1900. * to be called. We do this if the page is releasable by try_to_free_buffers().
  1901. * We also do it if the page has locked or dirty buffers and the caller wants
  1902. * us to perform sync or async writeout.
  1903. *
  1904. * This complicates JBD locking somewhat. We aren't protected by the
  1905. * BKL here. We wish to remove the buffer from its committing or
  1906. * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
  1907. *
  1908. * This may *change* the value of transaction_t->t_datalist, so anyone
  1909. * who looks at t_datalist needs to lock against this function.
  1910. *
  1911. * Even worse, someone may be doing a jbd2_journal_dirty_data on this
  1912. * buffer. So we need to lock against that. jbd2_journal_dirty_data()
  1913. * will come out of the lock with the buffer dirty, which makes it
  1914. * ineligible for release here.
  1915. *
  1916. * Who else is affected by this? hmm... Really the only contender
  1917. * is do_get_write_access() - it could be looking at the buffer while
  1918. * journal_try_to_free_buffer() is changing its state. But that
  1919. * cannot happen because we never reallocate freed data as metadata
  1920. * while the data is part of a transaction. Yes?
  1921. *
  1922. * Return false on failure, true on success
  1923. */
  1924. bool jbd2_journal_try_to_free_buffers(journal_t *journal, struct folio *folio)
  1925. {
  1926. struct buffer_head *head;
  1927. struct buffer_head *bh;
  1928. bool ret = false;
  1929. J_ASSERT(folio_test_locked(folio));
  1930. head = folio_buffers(folio);
  1931. bh = head;
  1932. do {
  1933. struct journal_head *jh;
  1934. /*
  1935. * We take our own ref against the journal_head here to avoid
  1936. * having to add tons of locking around each instance of
  1937. * jbd2_journal_put_journal_head().
  1938. */
  1939. jh = jbd2_journal_grab_journal_head(bh);
  1940. if (!jh)
  1941. continue;
  1942. spin_lock(&jh->b_state_lock);
  1943. if (!jh->b_transaction && !jh->b_next_transaction) {
  1944. spin_lock(&journal->j_list_lock);
  1945. /* Remove written-back checkpointed metadata buffer */
  1946. if (jh->b_cp_transaction != NULL)
  1947. jbd2_journal_try_remove_checkpoint(jh);
  1948. spin_unlock(&journal->j_list_lock);
  1949. }
  1950. spin_unlock(&jh->b_state_lock);
  1951. jbd2_journal_put_journal_head(jh);
  1952. if (buffer_jbd(bh))
  1953. goto busy;
  1954. } while ((bh = bh->b_this_page) != head);
  1955. ret = try_to_free_buffers(folio);
  1956. busy:
  1957. return ret;
  1958. }
  1959. /*
  1960. * This buffer is no longer needed. If it is on an older transaction's
  1961. * checkpoint list we need to record it on this transaction's forget list
  1962. * to pin this buffer (and hence its checkpointing transaction) down until
  1963. * this transaction commits. If the buffer isn't on a checkpoint list, we
  1964. * release it.
  1965. * Returns non-zero if JBD no longer has an interest in the buffer.
  1966. *
  1967. * Called under j_list_lock.
  1968. *
  1969. * Called under jh->b_state_lock.
  1970. */
  1971. static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
  1972. {
  1973. int may_free = 1;
  1974. struct buffer_head *bh = jh2bh(jh);
  1975. if (jh->b_cp_transaction) {
  1976. JBUFFER_TRACE(jh, "on running+cp transaction");
  1977. __jbd2_journal_temp_unlink_buffer(jh);
  1978. /*
  1979. * We don't want to write the buffer anymore, clear the
  1980. * bit so that we don't confuse checks in
  1981. * __journal_file_buffer
  1982. */
  1983. clear_buffer_dirty(bh);
  1984. __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
  1985. may_free = 0;
  1986. } else {
  1987. JBUFFER_TRACE(jh, "on running transaction");
  1988. __jbd2_journal_unfile_buffer(jh);
  1989. jbd2_journal_put_journal_head(jh);
  1990. }
  1991. return may_free;
  1992. }
  1993. /*
  1994. * jbd2_journal_invalidate_folio
  1995. *
  1996. * This code is tricky. It has a number of cases to deal with.
  1997. *
  1998. * There are two invariants which this code relies on:
  1999. *
  2000. * i_size must be updated on disk before we start calling invalidate_folio
  2001. * on the data.
  2002. *
  2003. * This is done in ext3 by defining an ext3_setattr method which
  2004. * updates i_size before truncate gets going. By maintaining this
  2005. * invariant, we can be sure that it is safe to throw away any buffers
  2006. * attached to the current transaction: once the transaction commits,
  2007. * we know that the data will not be needed.
  2008. *
  2009. * Note however that we can *not* throw away data belonging to the
  2010. * previous, committing transaction!
  2011. *
  2012. * Any disk blocks which *are* part of the previous, committing
  2013. * transaction (and which therefore cannot be discarded immediately) are
  2014. * not going to be reused in the new running transaction
  2015. *
  2016. * The bitmap committed_data images guarantee this: any block which is
  2017. * allocated in one transaction and removed in the next will be marked
  2018. * as in-use in the committed_data bitmap, so cannot be reused until
  2019. * the next transaction to delete the block commits. This means that
  2020. * leaving committing buffers dirty is quite safe: the disk blocks
  2021. * cannot be reallocated to a different file and so buffer aliasing is
  2022. * not possible.
  2023. *
  2024. *
  2025. * The above applies mainly to ordered data mode. In writeback mode we
  2026. * don't make guarantees about the order in which data hits disk --- in
  2027. * particular we don't guarantee that new dirty data is flushed before
  2028. * transaction commit --- so it is always safe just to discard data
  2029. * immediately in that mode. --sct
  2030. */
  2031. /*
  2032. * The journal_unmap_buffer helper function returns zero if the buffer
  2033. * concerned remains pinned as an anonymous buffer belonging to an older
  2034. * transaction.
  2035. *
  2036. * We're outside-transaction here. Either or both of j_running_transaction
  2037. * and j_committing_transaction may be NULL.
  2038. */
  2039. static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh,
  2040. int partial_page)
  2041. {
  2042. transaction_t *transaction;
  2043. struct journal_head *jh;
  2044. int may_free = 1;
  2045. BUFFER_TRACE(bh, "entry");
  2046. /*
  2047. * It is safe to proceed here without the j_list_lock because the
  2048. * buffers cannot be stolen by try_to_free_buffers as long as we are
  2049. * holding the page lock. --sct
  2050. */
  2051. jh = jbd2_journal_grab_journal_head(bh);
  2052. if (!jh)
  2053. goto zap_buffer_unlocked;
  2054. /* OK, we have data buffer in journaled mode */
  2055. write_lock(&journal->j_state_lock);
  2056. spin_lock(&jh->b_state_lock);
  2057. spin_lock(&journal->j_list_lock);
  2058. /*
  2059. * We cannot remove the buffer from checkpoint lists until the
  2060. * transaction adding inode to orphan list (let's call it T)
  2061. * is committed. Otherwise if the transaction changing the
  2062. * buffer would be cleaned from the journal before T is
  2063. * committed, a crash will cause that the correct contents of
  2064. * the buffer will be lost. On the other hand we have to
  2065. * clear the buffer dirty bit at latest at the moment when the
  2066. * transaction marking the buffer as freed in the filesystem
  2067. * structures is committed because from that moment on the
  2068. * block can be reallocated and used by a different page.
  2069. * Since the block hasn't been freed yet but the inode has
  2070. * already been added to orphan list, it is safe for us to add
  2071. * the buffer to BJ_Forget list of the newest transaction.
  2072. *
  2073. * Also we have to clear buffer_mapped flag of a truncated buffer
  2074. * because the buffer_head may be attached to the page straddling
  2075. * i_size (can happen only when blocksize < pagesize) and thus the
  2076. * buffer_head can be reused when the file is extended again. So we end
  2077. * up keeping around invalidated buffers attached to transactions'
  2078. * BJ_Forget list just to stop checkpointing code from cleaning up
  2079. * the transaction this buffer was modified in.
  2080. */
  2081. transaction = jh->b_transaction;
  2082. if (transaction == NULL) {
  2083. /* First case: not on any transaction. If it
  2084. * has no checkpoint link, then we can zap it:
  2085. * it's a writeback-mode buffer so we don't care
  2086. * if it hits disk safely. */
  2087. if (!jh->b_cp_transaction) {
  2088. JBUFFER_TRACE(jh, "not on any transaction: zap");
  2089. goto zap_buffer;
  2090. }
  2091. if (!buffer_dirty(bh)) {
  2092. /* bdflush has written it. We can drop it now */
  2093. __jbd2_journal_remove_checkpoint(jh);
  2094. goto zap_buffer;
  2095. }
  2096. /* OK, it must be in the journal but still not
  2097. * written fully to disk: it's metadata or
  2098. * journaled data... */
  2099. if (journal->j_running_transaction) {
  2100. /* ... and once the current transaction has
  2101. * committed, the buffer won't be needed any
  2102. * longer. */
  2103. JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
  2104. may_free = __dispose_buffer(jh,
  2105. journal->j_running_transaction);
  2106. goto zap_buffer;
  2107. } else {
  2108. /* There is no currently-running transaction. So the
  2109. * orphan record which we wrote for this file must have
  2110. * passed into commit. We must attach this buffer to
  2111. * the committing transaction, if it exists. */
  2112. if (journal->j_committing_transaction) {
  2113. JBUFFER_TRACE(jh, "give to committing trans");
  2114. may_free = __dispose_buffer(jh,
  2115. journal->j_committing_transaction);
  2116. goto zap_buffer;
  2117. } else {
  2118. /* The orphan record's transaction has
  2119. * committed. We can cleanse this buffer */
  2120. clear_buffer_jbddirty(bh);
  2121. __jbd2_journal_remove_checkpoint(jh);
  2122. goto zap_buffer;
  2123. }
  2124. }
  2125. } else if (transaction == journal->j_committing_transaction) {
  2126. JBUFFER_TRACE(jh, "on committing transaction");
  2127. /*
  2128. * The buffer is committing, we simply cannot touch
  2129. * it. If the page is straddling i_size we have to wait
  2130. * for commit and try again.
  2131. */
  2132. if (partial_page) {
  2133. spin_unlock(&journal->j_list_lock);
  2134. spin_unlock(&jh->b_state_lock);
  2135. write_unlock(&journal->j_state_lock);
  2136. jbd2_journal_put_journal_head(jh);
  2137. /* Already zapped buffer? Nothing to do... */
  2138. if (!bh->b_bdev)
  2139. return 0;
  2140. return -EBUSY;
  2141. }
  2142. /*
  2143. * OK, buffer won't be reachable after truncate. We just clear
  2144. * b_modified to not confuse transaction credit accounting, and
  2145. * set j_next_transaction to the running transaction (if there
  2146. * is one) and mark buffer as freed so that commit code knows
  2147. * it should clear dirty bits when it is done with the buffer.
  2148. */
  2149. set_buffer_freed(bh);
  2150. if (journal->j_running_transaction && buffer_jbddirty(bh))
  2151. jh->b_next_transaction = journal->j_running_transaction;
  2152. jh->b_modified = 0;
  2153. spin_unlock(&journal->j_list_lock);
  2154. spin_unlock(&jh->b_state_lock);
  2155. write_unlock(&journal->j_state_lock);
  2156. jbd2_journal_put_journal_head(jh);
  2157. return 0;
  2158. } else {
  2159. /* Good, the buffer belongs to the running transaction.
  2160. * We are writing our own transaction's data, not any
  2161. * previous one's, so it is safe to throw it away
  2162. * (remember that we expect the filesystem to have set
  2163. * i_size already for this truncate so recovery will not
  2164. * expose the disk blocks we are discarding here.) */
  2165. J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
  2166. JBUFFER_TRACE(jh, "on running transaction");
  2167. may_free = __dispose_buffer(jh, transaction);
  2168. }
  2169. zap_buffer:
  2170. /*
  2171. * This is tricky. Although the buffer is truncated, it may be reused
  2172. * if blocksize < pagesize and it is attached to the page straddling
  2173. * EOF. Since the buffer might have been added to BJ_Forget list of the
  2174. * running transaction, journal_get_write_access() won't clear
  2175. * b_modified and credit accounting gets confused. So clear b_modified
  2176. * here.
  2177. */
  2178. jh->b_modified = 0;
  2179. spin_unlock(&journal->j_list_lock);
  2180. spin_unlock(&jh->b_state_lock);
  2181. write_unlock(&journal->j_state_lock);
  2182. jbd2_journal_put_journal_head(jh);
  2183. zap_buffer_unlocked:
  2184. clear_buffer_dirty(bh);
  2185. J_ASSERT_BH(bh, !buffer_jbddirty(bh));
  2186. clear_buffer_mapped(bh);
  2187. clear_buffer_req(bh);
  2188. clear_buffer_new(bh);
  2189. clear_buffer_delay(bh);
  2190. clear_buffer_unwritten(bh);
  2191. bh->b_bdev = NULL;
  2192. return may_free;
  2193. }
  2194. /**
  2195. * jbd2_journal_invalidate_folio()
  2196. * @journal: journal to use for flush...
  2197. * @folio: folio to flush
  2198. * @offset: start of the range to invalidate
  2199. * @length: length of the range to invalidate
  2200. *
  2201. * Reap page buffers containing data after in the specified range in page.
  2202. * Can return -EBUSY if buffers are part of the committing transaction and
  2203. * the page is straddling i_size. Caller then has to wait for current commit
  2204. * and try again.
  2205. */
  2206. int jbd2_journal_invalidate_folio(journal_t *journal, struct folio *folio,
  2207. size_t offset, size_t length)
  2208. {
  2209. struct buffer_head *head, *bh, *next;
  2210. unsigned int stop = offset + length;
  2211. unsigned int curr_off = 0;
  2212. int partial_page = (offset || length < folio_size(folio));
  2213. int may_free = 1;
  2214. int ret = 0;
  2215. if (!folio_test_locked(folio))
  2216. BUG();
  2217. head = folio_buffers(folio);
  2218. if (!head)
  2219. return 0;
  2220. BUG_ON(stop > folio_size(folio) || stop < length);
  2221. /* We will potentially be playing with lists other than just the
  2222. * data lists (especially for journaled data mode), so be
  2223. * cautious in our locking. */
  2224. bh = head;
  2225. do {
  2226. unsigned int next_off = curr_off + bh->b_size;
  2227. next = bh->b_this_page;
  2228. if (next_off > stop)
  2229. return 0;
  2230. if (offset <= curr_off) {
  2231. /* This block is wholly outside the truncation point */
  2232. lock_buffer(bh);
  2233. ret = journal_unmap_buffer(journal, bh, partial_page);
  2234. unlock_buffer(bh);
  2235. if (ret < 0)
  2236. return ret;
  2237. may_free &= ret;
  2238. }
  2239. curr_off = next_off;
  2240. bh = next;
  2241. } while (bh != head);
  2242. if (!partial_page) {
  2243. if (may_free && try_to_free_buffers(folio))
  2244. J_ASSERT(!folio_buffers(folio));
  2245. }
  2246. return 0;
  2247. }
  2248. /*
  2249. * File a buffer on the given transaction list.
  2250. */
  2251. void __jbd2_journal_file_buffer(struct journal_head *jh,
  2252. transaction_t *transaction, int jlist)
  2253. {
  2254. struct journal_head **list = NULL;
  2255. int was_dirty = 0;
  2256. struct buffer_head *bh = jh2bh(jh);
  2257. lockdep_assert_held(&jh->b_state_lock);
  2258. assert_spin_locked(&transaction->t_journal->j_list_lock);
  2259. J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
  2260. J_ASSERT_JH(jh, jh->b_transaction == transaction ||
  2261. jh->b_transaction == NULL);
  2262. if (jh->b_transaction && jh->b_jlist == jlist)
  2263. return;
  2264. if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
  2265. jlist == BJ_Shadow || jlist == BJ_Forget) {
  2266. /*
  2267. * For metadata buffers, we track dirty bit in buffer_jbddirty
  2268. * instead of buffer_dirty. We should not see a dirty bit set
  2269. * here because we clear it in do_get_write_access but e.g.
  2270. * tune2fs can modify the sb and set the dirty bit at any time
  2271. * so we try to gracefully handle that.
  2272. */
  2273. if (buffer_dirty(bh))
  2274. warn_dirty_buffer(bh);
  2275. if (test_clear_buffer_dirty(bh) ||
  2276. test_clear_buffer_jbddirty(bh))
  2277. was_dirty = 1;
  2278. }
  2279. if (jh->b_transaction)
  2280. __jbd2_journal_temp_unlink_buffer(jh);
  2281. else
  2282. jbd2_journal_grab_journal_head(bh);
  2283. jh->b_transaction = transaction;
  2284. switch (jlist) {
  2285. case BJ_None:
  2286. J_ASSERT_JH(jh, !jh->b_committed_data);
  2287. J_ASSERT_JH(jh, !jh->b_frozen_data);
  2288. return;
  2289. case BJ_Metadata:
  2290. transaction->t_nr_buffers++;
  2291. list = &transaction->t_buffers;
  2292. break;
  2293. case BJ_Forget:
  2294. list = &transaction->t_forget;
  2295. break;
  2296. case BJ_Shadow:
  2297. list = &transaction->t_shadow_list;
  2298. break;
  2299. case BJ_Reserved:
  2300. list = &transaction->t_reserved_list;
  2301. break;
  2302. }
  2303. __blist_add_buffer(list, jh);
  2304. jh->b_jlist = jlist;
  2305. if (was_dirty)
  2306. set_buffer_jbddirty(bh);
  2307. }
  2308. void jbd2_journal_file_buffer(struct journal_head *jh,
  2309. transaction_t *transaction, int jlist)
  2310. {
  2311. spin_lock(&jh->b_state_lock);
  2312. spin_lock(&transaction->t_journal->j_list_lock);
  2313. __jbd2_journal_file_buffer(jh, transaction, jlist);
  2314. spin_unlock(&transaction->t_journal->j_list_lock);
  2315. spin_unlock(&jh->b_state_lock);
  2316. }
  2317. /*
  2318. * Remove a buffer from its current buffer list in preparation for
  2319. * dropping it from its current transaction entirely. If the buffer has
  2320. * already started to be used by a subsequent transaction, refile the
  2321. * buffer on that transaction's metadata list.
  2322. *
  2323. * Called under j_list_lock
  2324. * Called under jh->b_state_lock
  2325. *
  2326. * When this function returns true, there's no next transaction to refile to
  2327. * and the caller has to drop jh reference through
  2328. * jbd2_journal_put_journal_head().
  2329. */
  2330. bool __jbd2_journal_refile_buffer(struct journal_head *jh)
  2331. {
  2332. int was_dirty, jlist;
  2333. struct buffer_head *bh = jh2bh(jh);
  2334. lockdep_assert_held(&jh->b_state_lock);
  2335. if (jh->b_transaction)
  2336. assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
  2337. /* If the buffer is now unused, just drop it. */
  2338. if (jh->b_next_transaction == NULL) {
  2339. __jbd2_journal_unfile_buffer(jh);
  2340. return true;
  2341. }
  2342. /*
  2343. * It has been modified by a later transaction: add it to the new
  2344. * transaction's metadata list.
  2345. */
  2346. was_dirty = test_clear_buffer_jbddirty(bh);
  2347. __jbd2_journal_temp_unlink_buffer(jh);
  2348. /*
  2349. * b_transaction must be set, otherwise the new b_transaction won't
  2350. * be holding jh reference
  2351. */
  2352. J_ASSERT_JH(jh, jh->b_transaction != NULL);
  2353. /*
  2354. * We set b_transaction here because b_next_transaction will inherit
  2355. * our jh reference and thus __jbd2_journal_file_buffer() must not
  2356. * take a new one.
  2357. */
  2358. WRITE_ONCE(jh->b_transaction, jh->b_next_transaction);
  2359. WRITE_ONCE(jh->b_next_transaction, NULL);
  2360. if (buffer_freed(bh))
  2361. jlist = BJ_Forget;
  2362. else if (jh->b_modified)
  2363. jlist = BJ_Metadata;
  2364. else
  2365. jlist = BJ_Reserved;
  2366. __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
  2367. J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
  2368. if (was_dirty)
  2369. set_buffer_jbddirty(bh);
  2370. return false;
  2371. }
  2372. /*
  2373. * __jbd2_journal_refile_buffer() with necessary locking added. We take our
  2374. * bh reference so that we can safely unlock bh.
  2375. *
  2376. * The jh and bh may be freed by this call.
  2377. */
  2378. void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
  2379. {
  2380. bool drop;
  2381. spin_lock(&jh->b_state_lock);
  2382. spin_lock(&journal->j_list_lock);
  2383. drop = __jbd2_journal_refile_buffer(jh);
  2384. spin_unlock(&jh->b_state_lock);
  2385. spin_unlock(&journal->j_list_lock);
  2386. if (drop)
  2387. jbd2_journal_put_journal_head(jh);
  2388. }
  2389. /*
  2390. * File inode in the inode list of the handle's transaction
  2391. */
  2392. static int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode,
  2393. unsigned long flags, loff_t start_byte, loff_t end_byte)
  2394. {
  2395. transaction_t *transaction = handle->h_transaction;
  2396. journal_t *journal;
  2397. if (is_handle_aborted(handle))
  2398. return -EROFS;
  2399. journal = transaction->t_journal;
  2400. jbd2_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
  2401. transaction->t_tid);
  2402. spin_lock(&journal->j_list_lock);
  2403. jinode->i_flags |= flags;
  2404. if (jinode->i_dirty_end) {
  2405. jinode->i_dirty_start = min(jinode->i_dirty_start, start_byte);
  2406. jinode->i_dirty_end = max(jinode->i_dirty_end, end_byte);
  2407. } else {
  2408. jinode->i_dirty_start = start_byte;
  2409. jinode->i_dirty_end = end_byte;
  2410. }
  2411. /* Is inode already attached where we need it? */
  2412. if (jinode->i_transaction == transaction ||
  2413. jinode->i_next_transaction == transaction)
  2414. goto done;
  2415. /*
  2416. * We only ever set this variable to 1 so the test is safe. Since
  2417. * t_need_data_flush is likely to be set, we do the test to save some
  2418. * cacheline bouncing
  2419. */
  2420. if (!transaction->t_need_data_flush)
  2421. transaction->t_need_data_flush = 1;
  2422. /* On some different transaction's list - should be
  2423. * the committing one */
  2424. if (jinode->i_transaction) {
  2425. J_ASSERT(jinode->i_next_transaction == NULL);
  2426. J_ASSERT(jinode->i_transaction ==
  2427. journal->j_committing_transaction);
  2428. jinode->i_next_transaction = transaction;
  2429. goto done;
  2430. }
  2431. /* Not on any transaction list... */
  2432. J_ASSERT(!jinode->i_next_transaction);
  2433. jinode->i_transaction = transaction;
  2434. list_add(&jinode->i_list, &transaction->t_inode_list);
  2435. done:
  2436. spin_unlock(&journal->j_list_lock);
  2437. return 0;
  2438. }
  2439. int jbd2_journal_inode_ranged_write(handle_t *handle,
  2440. struct jbd2_inode *jinode, loff_t start_byte, loff_t length)
  2441. {
  2442. return jbd2_journal_file_inode(handle, jinode,
  2443. JI_WRITE_DATA | JI_WAIT_DATA, start_byte,
  2444. start_byte + length - 1);
  2445. }
  2446. int jbd2_journal_inode_ranged_wait(handle_t *handle, struct jbd2_inode *jinode,
  2447. loff_t start_byte, loff_t length)
  2448. {
  2449. return jbd2_journal_file_inode(handle, jinode, JI_WAIT_DATA,
  2450. start_byte, start_byte + length - 1);
  2451. }
  2452. /*
  2453. * File truncate and transaction commit interact with each other in a
  2454. * non-trivial way. If a transaction writing data block A is
  2455. * committing, we cannot discard the data by truncate until we have
  2456. * written them. Otherwise if we crashed after the transaction with
  2457. * write has committed but before the transaction with truncate has
  2458. * committed, we could see stale data in block A. This function is a
  2459. * helper to solve this problem. It starts writeout of the truncated
  2460. * part in case it is in the committing transaction.
  2461. *
  2462. * Filesystem code must call this function when inode is journaled in
  2463. * ordered mode before truncation happens and after the inode has been
  2464. * placed on orphan list with the new inode size. The second condition
  2465. * avoids the race that someone writes new data and we start
  2466. * committing the transaction after this function has been called but
  2467. * before a transaction for truncate is started (and furthermore it
  2468. * allows us to optimize the case where the addition to orphan list
  2469. * happens in the same transaction as write --- we don't have to write
  2470. * any data in such case).
  2471. */
  2472. int jbd2_journal_begin_ordered_truncate(journal_t *journal,
  2473. struct jbd2_inode *jinode,
  2474. loff_t new_size)
  2475. {
  2476. transaction_t *inode_trans, *commit_trans;
  2477. int ret = 0;
  2478. /* This is a quick check to avoid locking if not necessary */
  2479. if (!jinode->i_transaction)
  2480. goto out;
  2481. /* Locks are here just to force reading of recent values, it is
  2482. * enough that the transaction was not committing before we started
  2483. * a transaction adding the inode to orphan list */
  2484. read_lock(&journal->j_state_lock);
  2485. commit_trans = journal->j_committing_transaction;
  2486. read_unlock(&journal->j_state_lock);
  2487. spin_lock(&journal->j_list_lock);
  2488. inode_trans = jinode->i_transaction;
  2489. spin_unlock(&journal->j_list_lock);
  2490. if (inode_trans == commit_trans) {
  2491. ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
  2492. new_size, LLONG_MAX);
  2493. if (ret)
  2494. jbd2_journal_abort(journal, ret);
  2495. }
  2496. out:
  2497. return ret;
  2498. }