transaction.c 85 KB

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