transaction.c 65 KB

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