transaction.c 65 KB

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