transaction.c 65 KB

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