transaction.c 65 KB

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