transaction.c 66 KB

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