transaction.c 79 KB

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