transaction.c 67 KB

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