transaction.c 62 KB

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