transaction.c 63 KB

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