transaction.c 68 KB

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