transaction.c 65 KB

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