transaction.c 79 KB

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