transaction.c 78 KB

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