journal.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122
  1. /*
  2. * linux/fs/jbd/journal.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 journal-writing code; part of the ext2fs
  13. * journaling system.
  14. *
  15. * This file manages journals: areas of disk reserved for logging
  16. * transactional updates. This includes the kernel journaling thread
  17. * which is responsible for scheduling updates to the log.
  18. *
  19. * We do not actually manage the physical storage of the journal in this
  20. * file: that is left to a per-journal policy function, which allows us
  21. * to store the journal within a filesystem-specified area for ext2
  22. * journaling (ext2 can use a reserved inode for storing the log).
  23. */
  24. #include <linux/module.h>
  25. #include <linux/time.h>
  26. #include <linux/fs.h>
  27. #include <linux/jbd.h>
  28. #include <linux/errno.h>
  29. #include <linux/slab.h>
  30. #include <linux/init.h>
  31. #include <linux/mm.h>
  32. #include <linux/freezer.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/kthread.h>
  35. #include <linux/poison.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/debugfs.h>
  38. #include <linux/ratelimit.h>
  39. #define CREATE_TRACE_POINTS
  40. #include <trace/events/jbd.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/page.h>
  43. EXPORT_SYMBOL(journal_start);
  44. EXPORT_SYMBOL(journal_restart);
  45. EXPORT_SYMBOL(journal_extend);
  46. EXPORT_SYMBOL(journal_stop);
  47. EXPORT_SYMBOL(journal_lock_updates);
  48. EXPORT_SYMBOL(journal_unlock_updates);
  49. EXPORT_SYMBOL(journal_get_write_access);
  50. EXPORT_SYMBOL(journal_get_create_access);
  51. EXPORT_SYMBOL(journal_get_undo_access);
  52. EXPORT_SYMBOL(journal_dirty_data);
  53. EXPORT_SYMBOL(journal_dirty_metadata);
  54. EXPORT_SYMBOL(journal_release_buffer);
  55. EXPORT_SYMBOL(journal_forget);
  56. #if 0
  57. EXPORT_SYMBOL(journal_sync_buffer);
  58. #endif
  59. EXPORT_SYMBOL(journal_flush);
  60. EXPORT_SYMBOL(journal_revoke);
  61. EXPORT_SYMBOL(journal_init_dev);
  62. EXPORT_SYMBOL(journal_init_inode);
  63. EXPORT_SYMBOL(journal_update_format);
  64. EXPORT_SYMBOL(journal_check_used_features);
  65. EXPORT_SYMBOL(journal_check_available_features);
  66. EXPORT_SYMBOL(journal_set_features);
  67. EXPORT_SYMBOL(journal_create);
  68. EXPORT_SYMBOL(journal_load);
  69. EXPORT_SYMBOL(journal_destroy);
  70. EXPORT_SYMBOL(journal_abort);
  71. EXPORT_SYMBOL(journal_errno);
  72. EXPORT_SYMBOL(journal_ack_err);
  73. EXPORT_SYMBOL(journal_clear_err);
  74. EXPORT_SYMBOL(log_wait_commit);
  75. EXPORT_SYMBOL(log_start_commit);
  76. EXPORT_SYMBOL(journal_start_commit);
  77. EXPORT_SYMBOL(journal_force_commit_nested);
  78. EXPORT_SYMBOL(journal_wipe);
  79. EXPORT_SYMBOL(journal_blocks_per_page);
  80. EXPORT_SYMBOL(journal_invalidatepage);
  81. EXPORT_SYMBOL(journal_try_to_free_buffers);
  82. EXPORT_SYMBOL(journal_force_commit);
  83. static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
  84. static void __journal_abort_soft (journal_t *journal, int errno);
  85. static const char *journal_dev_name(journal_t *journal, char *buffer);
  86. /*
  87. * Helper function used to manage commit timeouts
  88. */
  89. static void commit_timeout(unsigned long __data)
  90. {
  91. struct task_struct * p = (struct task_struct *) __data;
  92. wake_up_process(p);
  93. }
  94. /*
  95. * kjournald: The main thread function used to manage a logging device
  96. * journal.
  97. *
  98. * This kernel thread is responsible for two things:
  99. *
  100. * 1) COMMIT: Every so often we need to commit the current state of the
  101. * filesystem to disk. The journal thread is responsible for writing
  102. * all of the metadata buffers to disk.
  103. *
  104. * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
  105. * of the data in that part of the log has been rewritten elsewhere on
  106. * the disk. Flushing these old buffers to reclaim space in the log is
  107. * known as checkpointing, and this thread is responsible for that job.
  108. */
  109. static int kjournald(void *arg)
  110. {
  111. journal_t *journal = arg;
  112. transaction_t *transaction;
  113. /*
  114. * Set up an interval timer which can be used to trigger a commit wakeup
  115. * after the commit interval expires
  116. */
  117. setup_timer(&journal->j_commit_timer, commit_timeout,
  118. (unsigned long)current);
  119. set_freezable();
  120. /* Record that the journal thread is running */
  121. journal->j_task = current;
  122. wake_up(&journal->j_wait_done_commit);
  123. printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
  124. journal->j_commit_interval / HZ);
  125. /*
  126. * And now, wait forever for commit wakeup events.
  127. */
  128. spin_lock(&journal->j_state_lock);
  129. loop:
  130. if (journal->j_flags & JFS_UNMOUNT)
  131. goto end_loop;
  132. jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
  133. journal->j_commit_sequence, journal->j_commit_request);
  134. if (journal->j_commit_sequence != journal->j_commit_request) {
  135. jbd_debug(1, "OK, requests differ\n");
  136. spin_unlock(&journal->j_state_lock);
  137. del_timer_sync(&journal->j_commit_timer);
  138. journal_commit_transaction(journal);
  139. spin_lock(&journal->j_state_lock);
  140. goto loop;
  141. }
  142. wake_up(&journal->j_wait_done_commit);
  143. if (freezing(current)) {
  144. /*
  145. * The simpler the better. Flushing journal isn't a
  146. * good idea, because that depends on threads that may
  147. * be already stopped.
  148. */
  149. jbd_debug(1, "Now suspending kjournald\n");
  150. spin_unlock(&journal->j_state_lock);
  151. try_to_freeze();
  152. spin_lock(&journal->j_state_lock);
  153. } else {
  154. /*
  155. * We assume on resume that commits are already there,
  156. * so we don't sleep
  157. */
  158. DEFINE_WAIT(wait);
  159. int should_sleep = 1;
  160. prepare_to_wait(&journal->j_wait_commit, &wait,
  161. TASK_INTERRUPTIBLE);
  162. if (journal->j_commit_sequence != journal->j_commit_request)
  163. should_sleep = 0;
  164. transaction = journal->j_running_transaction;
  165. if (transaction && time_after_eq(jiffies,
  166. transaction->t_expires))
  167. should_sleep = 0;
  168. if (journal->j_flags & JFS_UNMOUNT)
  169. should_sleep = 0;
  170. if (should_sleep) {
  171. spin_unlock(&journal->j_state_lock);
  172. schedule();
  173. spin_lock(&journal->j_state_lock);
  174. }
  175. finish_wait(&journal->j_wait_commit, &wait);
  176. }
  177. jbd_debug(1, "kjournald wakes\n");
  178. /*
  179. * Were we woken up by a commit wakeup event?
  180. */
  181. transaction = journal->j_running_transaction;
  182. if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
  183. journal->j_commit_request = transaction->t_tid;
  184. jbd_debug(1, "woke because of timeout\n");
  185. }
  186. goto loop;
  187. end_loop:
  188. spin_unlock(&journal->j_state_lock);
  189. del_timer_sync(&journal->j_commit_timer);
  190. journal->j_task = NULL;
  191. wake_up(&journal->j_wait_done_commit);
  192. jbd_debug(1, "Journal thread exiting.\n");
  193. return 0;
  194. }
  195. static int journal_start_thread(journal_t *journal)
  196. {
  197. struct task_struct *t;
  198. t = kthread_run(kjournald, journal, "kjournald");
  199. if (IS_ERR(t))
  200. return PTR_ERR(t);
  201. wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
  202. return 0;
  203. }
  204. static void journal_kill_thread(journal_t *journal)
  205. {
  206. spin_lock(&journal->j_state_lock);
  207. journal->j_flags |= JFS_UNMOUNT;
  208. while (journal->j_task) {
  209. wake_up(&journal->j_wait_commit);
  210. spin_unlock(&journal->j_state_lock);
  211. wait_event(journal->j_wait_done_commit,
  212. journal->j_task == NULL);
  213. spin_lock(&journal->j_state_lock);
  214. }
  215. spin_unlock(&journal->j_state_lock);
  216. }
  217. /*
  218. * journal_write_metadata_buffer: write a metadata buffer to the journal.
  219. *
  220. * Writes a metadata buffer to a given disk block. The actual IO is not
  221. * performed but a new buffer_head is constructed which labels the data
  222. * to be written with the correct destination disk block.
  223. *
  224. * Any magic-number escaping which needs to be done will cause a
  225. * copy-out here. If the buffer happens to start with the
  226. * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
  227. * magic number is only written to the log for descripter blocks. In
  228. * this case, we copy the data and replace the first word with 0, and we
  229. * return a result code which indicates that this buffer needs to be
  230. * marked as an escaped buffer in the corresponding log descriptor
  231. * block. The missing word can then be restored when the block is read
  232. * during recovery.
  233. *
  234. * If the source buffer has already been modified by a new transaction
  235. * since we took the last commit snapshot, we use the frozen copy of
  236. * that data for IO. If we end up using the existing buffer_head's data
  237. * for the write, then we *have* to lock the buffer to prevent anyone
  238. * else from using and possibly modifying it while the IO is in
  239. * progress.
  240. *
  241. * The function returns a pointer to the buffer_heads to be used for IO.
  242. *
  243. * We assume that the journal has already been locked in this function.
  244. *
  245. * Return value:
  246. * <0: Error
  247. * >=0: Finished OK
  248. *
  249. * On success:
  250. * Bit 0 set == escape performed on the data
  251. * Bit 1 set == buffer copy-out performed (kfree the data after IO)
  252. */
  253. int journal_write_metadata_buffer(transaction_t *transaction,
  254. struct journal_head *jh_in,
  255. struct journal_head **jh_out,
  256. unsigned int blocknr)
  257. {
  258. int need_copy_out = 0;
  259. int done_copy_out = 0;
  260. int do_escape = 0;
  261. char *mapped_data;
  262. struct buffer_head *new_bh;
  263. struct journal_head *new_jh;
  264. struct page *new_page;
  265. unsigned int new_offset;
  266. struct buffer_head *bh_in = jh2bh(jh_in);
  267. journal_t *journal = transaction->t_journal;
  268. /*
  269. * The buffer really shouldn't be locked: only the current committing
  270. * transaction is allowed to write it, so nobody else is allowed
  271. * to do any IO.
  272. *
  273. * akpm: except if we're journalling data, and write() output is
  274. * also part of a shared mapping, and another thread has
  275. * decided to launch a writepage() against this buffer.
  276. */
  277. J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
  278. new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
  279. /* keep subsequent assertions sane */
  280. new_bh->b_state = 0;
  281. init_buffer(new_bh, NULL, NULL);
  282. atomic_set(&new_bh->b_count, 1);
  283. new_jh = journal_add_journal_head(new_bh); /* This sleeps */
  284. /*
  285. * If a new transaction has already done a buffer copy-out, then
  286. * we use that version of the data for the commit.
  287. */
  288. jbd_lock_bh_state(bh_in);
  289. repeat:
  290. if (jh_in->b_frozen_data) {
  291. done_copy_out = 1;
  292. new_page = virt_to_page(jh_in->b_frozen_data);
  293. new_offset = offset_in_page(jh_in->b_frozen_data);
  294. } else {
  295. new_page = jh2bh(jh_in)->b_page;
  296. new_offset = offset_in_page(jh2bh(jh_in)->b_data);
  297. }
  298. mapped_data = kmap_atomic(new_page);
  299. /*
  300. * Check for escaping
  301. */
  302. if (*((__be32 *)(mapped_data + new_offset)) ==
  303. cpu_to_be32(JFS_MAGIC_NUMBER)) {
  304. need_copy_out = 1;
  305. do_escape = 1;
  306. }
  307. kunmap_atomic(mapped_data);
  308. /*
  309. * Do we need to do a data copy?
  310. */
  311. if (need_copy_out && !done_copy_out) {
  312. char *tmp;
  313. jbd_unlock_bh_state(bh_in);
  314. tmp = jbd_alloc(bh_in->b_size, GFP_NOFS);
  315. jbd_lock_bh_state(bh_in);
  316. if (jh_in->b_frozen_data) {
  317. jbd_free(tmp, bh_in->b_size);
  318. goto repeat;
  319. }
  320. jh_in->b_frozen_data = tmp;
  321. mapped_data = kmap_atomic(new_page);
  322. memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
  323. kunmap_atomic(mapped_data);
  324. new_page = virt_to_page(tmp);
  325. new_offset = offset_in_page(tmp);
  326. done_copy_out = 1;
  327. }
  328. /*
  329. * Did we need to do an escaping? Now we've done all the
  330. * copying, we can finally do so.
  331. */
  332. if (do_escape) {
  333. mapped_data = kmap_atomic(new_page);
  334. *((unsigned int *)(mapped_data + new_offset)) = 0;
  335. kunmap_atomic(mapped_data);
  336. }
  337. set_bh_page(new_bh, new_page, new_offset);
  338. new_jh->b_transaction = NULL;
  339. new_bh->b_size = jh2bh(jh_in)->b_size;
  340. new_bh->b_bdev = transaction->t_journal->j_dev;
  341. new_bh->b_blocknr = blocknr;
  342. set_buffer_mapped(new_bh);
  343. set_buffer_dirty(new_bh);
  344. *jh_out = new_jh;
  345. /*
  346. * The to-be-written buffer needs to get moved to the io queue,
  347. * and the original buffer whose contents we are shadowing or
  348. * copying is moved to the transaction's shadow queue.
  349. */
  350. JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
  351. spin_lock(&journal->j_list_lock);
  352. __journal_file_buffer(jh_in, transaction, BJ_Shadow);
  353. spin_unlock(&journal->j_list_lock);
  354. jbd_unlock_bh_state(bh_in);
  355. JBUFFER_TRACE(new_jh, "file as BJ_IO");
  356. journal_file_buffer(new_jh, transaction, BJ_IO);
  357. return do_escape | (done_copy_out << 1);
  358. }
  359. /*
  360. * Allocation code for the journal file. Manage the space left in the
  361. * journal, so that we can begin checkpointing when appropriate.
  362. */
  363. /*
  364. * __log_space_left: Return the number of free blocks left in the journal.
  365. *
  366. * Called with the journal already locked.
  367. *
  368. * Called under j_state_lock
  369. */
  370. int __log_space_left(journal_t *journal)
  371. {
  372. int left = journal->j_free;
  373. assert_spin_locked(&journal->j_state_lock);
  374. /*
  375. * Be pessimistic here about the number of those free blocks which
  376. * might be required for log descriptor control blocks.
  377. */
  378. #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
  379. left -= MIN_LOG_RESERVED_BLOCKS;
  380. if (left <= 0)
  381. return 0;
  382. left -= (left >> 3);
  383. return left;
  384. }
  385. /*
  386. * Called under j_state_lock. Returns true if a transaction commit was started.
  387. */
  388. int __log_start_commit(journal_t *journal, tid_t target)
  389. {
  390. /*
  391. * The only transaction we can possibly wait upon is the
  392. * currently running transaction (if it exists). Otherwise,
  393. * the target tid must be an old one.
  394. */
  395. if (journal->j_running_transaction &&
  396. journal->j_running_transaction->t_tid == target) {
  397. /*
  398. * We want a new commit: OK, mark the request and wakeup the
  399. * commit thread. We do _not_ do the commit ourselves.
  400. */
  401. journal->j_commit_request = target;
  402. jbd_debug(1, "JBD: requesting commit %d/%d\n",
  403. journal->j_commit_request,
  404. journal->j_commit_sequence);
  405. wake_up(&journal->j_wait_commit);
  406. return 1;
  407. } else if (!tid_geq(journal->j_commit_request, target))
  408. /* This should never happen, but if it does, preserve
  409. the evidence before kjournald goes into a loop and
  410. increments j_commit_sequence beyond all recognition. */
  411. WARN_ONCE(1, "jbd: bad log_start_commit: %u %u %u %u\n",
  412. journal->j_commit_request, journal->j_commit_sequence,
  413. target, journal->j_running_transaction ?
  414. journal->j_running_transaction->t_tid : 0);
  415. return 0;
  416. }
  417. int log_start_commit(journal_t *journal, tid_t tid)
  418. {
  419. int ret;
  420. spin_lock(&journal->j_state_lock);
  421. ret = __log_start_commit(journal, tid);
  422. spin_unlock(&journal->j_state_lock);
  423. return ret;
  424. }
  425. /*
  426. * Force and wait upon a commit if the calling process is not within
  427. * transaction. This is used for forcing out undo-protected data which contains
  428. * bitmaps, when the fs is running out of space.
  429. *
  430. * We can only force the running transaction if we don't have an active handle;
  431. * otherwise, we will deadlock.
  432. *
  433. * Returns true if a transaction was started.
  434. */
  435. int journal_force_commit_nested(journal_t *journal)
  436. {
  437. transaction_t *transaction = NULL;
  438. tid_t tid;
  439. spin_lock(&journal->j_state_lock);
  440. if (journal->j_running_transaction && !current->journal_info) {
  441. transaction = journal->j_running_transaction;
  442. __log_start_commit(journal, transaction->t_tid);
  443. } else if (journal->j_committing_transaction)
  444. transaction = journal->j_committing_transaction;
  445. if (!transaction) {
  446. spin_unlock(&journal->j_state_lock);
  447. return 0; /* Nothing to retry */
  448. }
  449. tid = transaction->t_tid;
  450. spin_unlock(&journal->j_state_lock);
  451. log_wait_commit(journal, tid);
  452. return 1;
  453. }
  454. /*
  455. * Start a commit of the current running transaction (if any). Returns true
  456. * if a transaction is going to be committed (or is currently already
  457. * committing), and fills its tid in at *ptid
  458. */
  459. int journal_start_commit(journal_t *journal, tid_t *ptid)
  460. {
  461. int ret = 0;
  462. spin_lock(&journal->j_state_lock);
  463. if (journal->j_running_transaction) {
  464. tid_t tid = journal->j_running_transaction->t_tid;
  465. __log_start_commit(journal, tid);
  466. /* There's a running transaction and we've just made sure
  467. * it's commit has been scheduled. */
  468. if (ptid)
  469. *ptid = tid;
  470. ret = 1;
  471. } else if (journal->j_committing_transaction) {
  472. /*
  473. * If commit has been started, then we have to wait for
  474. * completion of that transaction.
  475. */
  476. if (ptid)
  477. *ptid = journal->j_committing_transaction->t_tid;
  478. ret = 1;
  479. }
  480. spin_unlock(&journal->j_state_lock);
  481. return ret;
  482. }
  483. /*
  484. * Wait for a specified commit to complete.
  485. * The caller may not hold the journal lock.
  486. */
  487. int log_wait_commit(journal_t *journal, tid_t tid)
  488. {
  489. int err = 0;
  490. #ifdef CONFIG_JBD_DEBUG
  491. spin_lock(&journal->j_state_lock);
  492. if (!tid_geq(journal->j_commit_request, tid)) {
  493. printk(KERN_EMERG
  494. "%s: error: j_commit_request=%d, tid=%d\n",
  495. __func__, journal->j_commit_request, tid);
  496. }
  497. spin_unlock(&journal->j_state_lock);
  498. #endif
  499. spin_lock(&journal->j_state_lock);
  500. if (!tid_geq(journal->j_commit_waited, tid))
  501. journal->j_commit_waited = tid;
  502. while (tid_gt(tid, journal->j_commit_sequence)) {
  503. jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
  504. tid, journal->j_commit_sequence);
  505. wake_up(&journal->j_wait_commit);
  506. spin_unlock(&journal->j_state_lock);
  507. wait_event(journal->j_wait_done_commit,
  508. !tid_gt(tid, journal->j_commit_sequence));
  509. spin_lock(&journal->j_state_lock);
  510. }
  511. spin_unlock(&journal->j_state_lock);
  512. if (unlikely(is_journal_aborted(journal))) {
  513. printk(KERN_EMERG "journal commit I/O error\n");
  514. err = -EIO;
  515. }
  516. return err;
  517. }
  518. /*
  519. * Return 1 if a given transaction has not yet sent barrier request
  520. * connected with a transaction commit. If 0 is returned, transaction
  521. * may or may not have sent the barrier. Used to avoid sending barrier
  522. * twice in common cases.
  523. */
  524. int journal_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
  525. {
  526. int ret = 0;
  527. transaction_t *commit_trans;
  528. if (!(journal->j_flags & JFS_BARRIER))
  529. return 0;
  530. spin_lock(&journal->j_state_lock);
  531. /* Transaction already committed? */
  532. if (tid_geq(journal->j_commit_sequence, tid))
  533. goto out;
  534. /*
  535. * Transaction is being committed and we already proceeded to
  536. * writing commit record?
  537. */
  538. commit_trans = journal->j_committing_transaction;
  539. if (commit_trans && commit_trans->t_tid == tid &&
  540. commit_trans->t_state >= T_COMMIT_RECORD)
  541. goto out;
  542. ret = 1;
  543. out:
  544. spin_unlock(&journal->j_state_lock);
  545. return ret;
  546. }
  547. EXPORT_SYMBOL(journal_trans_will_send_data_barrier);
  548. /*
  549. * Log buffer allocation routines:
  550. */
  551. int journal_next_log_block(journal_t *journal, unsigned int *retp)
  552. {
  553. unsigned int blocknr;
  554. spin_lock(&journal->j_state_lock);
  555. J_ASSERT(journal->j_free > 1);
  556. blocknr = journal->j_head;
  557. journal->j_head++;
  558. journal->j_free--;
  559. if (journal->j_head == journal->j_last)
  560. journal->j_head = journal->j_first;
  561. spin_unlock(&journal->j_state_lock);
  562. return journal_bmap(journal, blocknr, retp);
  563. }
  564. /*
  565. * Conversion of logical to physical block numbers for the journal
  566. *
  567. * On external journals the journal blocks are identity-mapped, so
  568. * this is a no-op. If needed, we can use j_blk_offset - everything is
  569. * ready.
  570. */
  571. int journal_bmap(journal_t *journal, unsigned int blocknr,
  572. unsigned int *retp)
  573. {
  574. int err = 0;
  575. unsigned int ret;
  576. if (journal->j_inode) {
  577. ret = bmap(journal->j_inode, blocknr);
  578. if (ret)
  579. *retp = ret;
  580. else {
  581. char b[BDEVNAME_SIZE];
  582. printk(KERN_ALERT "%s: journal block not found "
  583. "at offset %u on %s\n",
  584. __func__,
  585. blocknr,
  586. bdevname(journal->j_dev, b));
  587. err = -EIO;
  588. __journal_abort_soft(journal, err);
  589. }
  590. } else {
  591. *retp = blocknr; /* +journal->j_blk_offset */
  592. }
  593. return err;
  594. }
  595. /*
  596. * We play buffer_head aliasing tricks to write data/metadata blocks to
  597. * the journal without copying their contents, but for journal
  598. * descriptor blocks we do need to generate bona fide buffers.
  599. *
  600. * After the caller of journal_get_descriptor_buffer() has finished modifying
  601. * the buffer's contents they really should run flush_dcache_page(bh->b_page).
  602. * But we don't bother doing that, so there will be coherency problems with
  603. * mmaps of blockdevs which hold live JBD-controlled filesystems.
  604. */
  605. struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
  606. {
  607. struct buffer_head *bh;
  608. unsigned int blocknr;
  609. int err;
  610. err = journal_next_log_block(journal, &blocknr);
  611. if (err)
  612. return NULL;
  613. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  614. if (!bh)
  615. return NULL;
  616. lock_buffer(bh);
  617. memset(bh->b_data, 0, journal->j_blocksize);
  618. set_buffer_uptodate(bh);
  619. unlock_buffer(bh);
  620. BUFFER_TRACE(bh, "return this buffer");
  621. return journal_add_journal_head(bh);
  622. }
  623. /*
  624. * Management for journal control blocks: functions to create and
  625. * destroy journal_t structures, and to initialise and read existing
  626. * journal blocks from disk. */
  627. /* First: create and setup a journal_t object in memory. We initialise
  628. * very few fields yet: that has to wait until we have created the
  629. * journal structures from from scratch, or loaded them from disk. */
  630. static journal_t * journal_init_common (void)
  631. {
  632. journal_t *journal;
  633. int err;
  634. journal = kzalloc(sizeof(*journal), GFP_KERNEL);
  635. if (!journal)
  636. goto fail;
  637. init_waitqueue_head(&journal->j_wait_transaction_locked);
  638. init_waitqueue_head(&journal->j_wait_logspace);
  639. init_waitqueue_head(&journal->j_wait_done_commit);
  640. init_waitqueue_head(&journal->j_wait_checkpoint);
  641. init_waitqueue_head(&journal->j_wait_commit);
  642. init_waitqueue_head(&journal->j_wait_updates);
  643. mutex_init(&journal->j_checkpoint_mutex);
  644. spin_lock_init(&journal->j_revoke_lock);
  645. spin_lock_init(&journal->j_list_lock);
  646. spin_lock_init(&journal->j_state_lock);
  647. journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
  648. /* The journal is marked for error until we succeed with recovery! */
  649. journal->j_flags = JFS_ABORT;
  650. /* Set up a default-sized revoke table for the new mount. */
  651. err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
  652. if (err) {
  653. kfree(journal);
  654. goto fail;
  655. }
  656. return journal;
  657. fail:
  658. return NULL;
  659. }
  660. /* journal_init_dev and journal_init_inode:
  661. *
  662. * Create a journal structure assigned some fixed set of disk blocks to
  663. * the journal. We don't actually touch those disk blocks yet, but we
  664. * need to set up all of the mapping information to tell the journaling
  665. * system where the journal blocks are.
  666. *
  667. */
  668. /**
  669. * journal_t * journal_init_dev() - creates and initialises a journal structure
  670. * @bdev: Block device on which to create the journal
  671. * @fs_dev: Device which hold journalled filesystem for this journal.
  672. * @start: Block nr Start of journal.
  673. * @len: Length of the journal in blocks.
  674. * @blocksize: blocksize of journalling device
  675. *
  676. * Returns: a newly created journal_t *
  677. *
  678. * journal_init_dev creates a journal which maps a fixed contiguous
  679. * range of blocks on an arbitrary block device.
  680. *
  681. */
  682. journal_t * journal_init_dev(struct block_device *bdev,
  683. struct block_device *fs_dev,
  684. int start, int len, int blocksize)
  685. {
  686. journal_t *journal = journal_init_common();
  687. struct buffer_head *bh;
  688. int n;
  689. if (!journal)
  690. return NULL;
  691. /* journal descriptor can store up to n blocks -bzzz */
  692. journal->j_blocksize = blocksize;
  693. n = journal->j_blocksize / sizeof(journal_block_tag_t);
  694. journal->j_wbufsize = n;
  695. journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
  696. if (!journal->j_wbuf) {
  697. printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
  698. __func__);
  699. goto out_err;
  700. }
  701. journal->j_dev = bdev;
  702. journal->j_fs_dev = fs_dev;
  703. journal->j_blk_offset = start;
  704. journal->j_maxlen = len;
  705. bh = __getblk(journal->j_dev, start, journal->j_blocksize);
  706. if (!bh) {
  707. printk(KERN_ERR
  708. "%s: Cannot get buffer for journal superblock\n",
  709. __func__);
  710. goto out_err;
  711. }
  712. journal->j_sb_buffer = bh;
  713. journal->j_superblock = (journal_superblock_t *)bh->b_data;
  714. return journal;
  715. out_err:
  716. kfree(journal->j_wbuf);
  717. kfree(journal);
  718. return NULL;
  719. }
  720. /**
  721. * journal_t * journal_init_inode () - creates a journal which maps to a inode.
  722. * @inode: An inode to create the journal in
  723. *
  724. * journal_init_inode creates a journal which maps an on-disk inode as
  725. * the journal. The inode must exist already, must support bmap() and
  726. * must have all data blocks preallocated.
  727. */
  728. journal_t * journal_init_inode (struct inode *inode)
  729. {
  730. struct buffer_head *bh;
  731. journal_t *journal = journal_init_common();
  732. int err;
  733. int n;
  734. unsigned int blocknr;
  735. if (!journal)
  736. return NULL;
  737. journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
  738. journal->j_inode = inode;
  739. jbd_debug(1,
  740. "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
  741. journal, inode->i_sb->s_id, inode->i_ino,
  742. (long long) inode->i_size,
  743. inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
  744. journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
  745. journal->j_blocksize = inode->i_sb->s_blocksize;
  746. /* journal descriptor can store up to n blocks -bzzz */
  747. n = journal->j_blocksize / sizeof(journal_block_tag_t);
  748. journal->j_wbufsize = n;
  749. journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
  750. if (!journal->j_wbuf) {
  751. printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
  752. __func__);
  753. goto out_err;
  754. }
  755. err = journal_bmap(journal, 0, &blocknr);
  756. /* If that failed, give up */
  757. if (err) {
  758. printk(KERN_ERR "%s: Cannot locate journal superblock\n",
  759. __func__);
  760. goto out_err;
  761. }
  762. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  763. if (!bh) {
  764. printk(KERN_ERR
  765. "%s: Cannot get buffer for journal superblock\n",
  766. __func__);
  767. goto out_err;
  768. }
  769. journal->j_sb_buffer = bh;
  770. journal->j_superblock = (journal_superblock_t *)bh->b_data;
  771. return journal;
  772. out_err:
  773. kfree(journal->j_wbuf);
  774. kfree(journal);
  775. return NULL;
  776. }
  777. /*
  778. * If the journal init or create aborts, we need to mark the journal
  779. * superblock as being NULL to prevent the journal destroy from writing
  780. * back a bogus superblock.
  781. */
  782. static void journal_fail_superblock (journal_t *journal)
  783. {
  784. struct buffer_head *bh = journal->j_sb_buffer;
  785. brelse(bh);
  786. journal->j_sb_buffer = NULL;
  787. }
  788. /*
  789. * Given a journal_t structure, initialise the various fields for
  790. * startup of a new journaling session. We use this both when creating
  791. * a journal, and after recovering an old journal to reset it for
  792. * subsequent use.
  793. */
  794. static int journal_reset(journal_t *journal)
  795. {
  796. journal_superblock_t *sb = journal->j_superblock;
  797. unsigned int first, last;
  798. first = be32_to_cpu(sb->s_first);
  799. last = be32_to_cpu(sb->s_maxlen);
  800. if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
  801. printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
  802. first, last);
  803. journal_fail_superblock(journal);
  804. return -EINVAL;
  805. }
  806. journal->j_first = first;
  807. journal->j_last = last;
  808. journal->j_head = first;
  809. journal->j_tail = first;
  810. journal->j_free = last - first;
  811. journal->j_tail_sequence = journal->j_transaction_sequence;
  812. journal->j_commit_sequence = journal->j_transaction_sequence - 1;
  813. journal->j_commit_request = journal->j_commit_sequence;
  814. journal->j_max_transaction_buffers = journal->j_maxlen / 4;
  815. /*
  816. * As a special case, if the on-disk copy is already marked as needing
  817. * no recovery (s_start == 0), then we can safely defer the superblock
  818. * update until the next commit by setting JFS_FLUSHED. This avoids
  819. * attempting a write to a potential-readonly device.
  820. */
  821. if (sb->s_start == 0) {
  822. jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
  823. "(start %u, seq %d, errno %d)\n",
  824. journal->j_tail, journal->j_tail_sequence,
  825. journal->j_errno);
  826. journal->j_flags |= JFS_FLUSHED;
  827. } else {
  828. /* Lock here to make assertions happy... */
  829. mutex_lock(&journal->j_checkpoint_mutex);
  830. /*
  831. * Update log tail information. We use WRITE_FUA since new
  832. * transaction will start reusing journal space and so we
  833. * must make sure information about current log tail is on
  834. * disk before that.
  835. */
  836. journal_update_sb_log_tail(journal,
  837. journal->j_tail_sequence,
  838. journal->j_tail,
  839. WRITE_FUA);
  840. mutex_unlock(&journal->j_checkpoint_mutex);
  841. }
  842. return journal_start_thread(journal);
  843. }
  844. /**
  845. * int journal_create() - Initialise the new journal file
  846. * @journal: Journal to create. This structure must have been initialised
  847. *
  848. * Given a journal_t structure which tells us which disk blocks we can
  849. * use, create a new journal superblock and initialise all of the
  850. * journal fields from scratch.
  851. **/
  852. int journal_create(journal_t *journal)
  853. {
  854. unsigned int blocknr;
  855. struct buffer_head *bh;
  856. journal_superblock_t *sb;
  857. int i, err;
  858. if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
  859. printk (KERN_ERR "Journal length (%d blocks) too short.\n",
  860. journal->j_maxlen);
  861. journal_fail_superblock(journal);
  862. return -EINVAL;
  863. }
  864. if (journal->j_inode == NULL) {
  865. /*
  866. * We don't know what block to start at!
  867. */
  868. printk(KERN_EMERG
  869. "%s: creation of journal on external device!\n",
  870. __func__);
  871. BUG();
  872. }
  873. /* Zero out the entire journal on disk. We cannot afford to
  874. have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
  875. jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
  876. for (i = 0; i < journal->j_maxlen; i++) {
  877. err = journal_bmap(journal, i, &blocknr);
  878. if (err)
  879. return err;
  880. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  881. if (unlikely(!bh))
  882. return -ENOMEM;
  883. lock_buffer(bh);
  884. memset (bh->b_data, 0, journal->j_blocksize);
  885. BUFFER_TRACE(bh, "marking dirty");
  886. mark_buffer_dirty(bh);
  887. BUFFER_TRACE(bh, "marking uptodate");
  888. set_buffer_uptodate(bh);
  889. unlock_buffer(bh);
  890. __brelse(bh);
  891. }
  892. sync_blockdev(journal->j_dev);
  893. jbd_debug(1, "JBD: journal cleared.\n");
  894. /* OK, fill in the initial static fields in the new superblock */
  895. sb = journal->j_superblock;
  896. sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
  897. sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
  898. sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
  899. sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
  900. sb->s_first = cpu_to_be32(1);
  901. journal->j_transaction_sequence = 1;
  902. journal->j_flags &= ~JFS_ABORT;
  903. journal->j_format_version = 2;
  904. return journal_reset(journal);
  905. }
  906. static void journal_write_superblock(journal_t *journal, int write_op)
  907. {
  908. struct buffer_head *bh = journal->j_sb_buffer;
  909. int ret;
  910. trace_journal_write_superblock(journal, write_op);
  911. if (!(journal->j_flags & JFS_BARRIER))
  912. write_op &= ~(REQ_FUA | REQ_FLUSH);
  913. lock_buffer(bh);
  914. if (buffer_write_io_error(bh)) {
  915. char b[BDEVNAME_SIZE];
  916. /*
  917. * Oh, dear. A previous attempt to write the journal
  918. * superblock failed. This could happen because the
  919. * USB device was yanked out. Or it could happen to
  920. * be a transient write error and maybe the block will
  921. * be remapped. Nothing we can do but to retry the
  922. * write and hope for the best.
  923. */
  924. printk(KERN_ERR "JBD: previous I/O error detected "
  925. "for journal superblock update for %s.\n",
  926. journal_dev_name(journal, b));
  927. clear_buffer_write_io_error(bh);
  928. set_buffer_uptodate(bh);
  929. }
  930. get_bh(bh);
  931. bh->b_end_io = end_buffer_write_sync;
  932. ret = submit_bh(write_op, bh);
  933. wait_on_buffer(bh);
  934. if (buffer_write_io_error(bh)) {
  935. clear_buffer_write_io_error(bh);
  936. set_buffer_uptodate(bh);
  937. ret = -EIO;
  938. }
  939. if (ret) {
  940. char b[BDEVNAME_SIZE];
  941. printk(KERN_ERR "JBD: Error %d detected "
  942. "when updating journal superblock for %s.\n",
  943. ret, journal_dev_name(journal, b));
  944. }
  945. }
  946. /**
  947. * journal_update_sb_log_tail() - Update log tail in journal sb on disk.
  948. * @journal: The journal to update.
  949. * @tail_tid: TID of the new transaction at the tail of the log
  950. * @tail_block: The first block of the transaction at the tail of the log
  951. * @write_op: With which operation should we write the journal sb
  952. *
  953. * Update a journal's superblock information about log tail and write it to
  954. * disk, waiting for the IO to complete.
  955. */
  956. void journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
  957. unsigned int tail_block, int write_op)
  958. {
  959. journal_superblock_t *sb = journal->j_superblock;
  960. BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
  961. jbd_debug(1,"JBD: updating superblock (start %u, seq %u)\n",
  962. tail_block, tail_tid);
  963. sb->s_sequence = cpu_to_be32(tail_tid);
  964. sb->s_start = cpu_to_be32(tail_block);
  965. journal_write_superblock(journal, write_op);
  966. /* Log is no longer empty */
  967. spin_lock(&journal->j_state_lock);
  968. WARN_ON(!sb->s_sequence);
  969. journal->j_flags &= ~JFS_FLUSHED;
  970. spin_unlock(&journal->j_state_lock);
  971. }
  972. /**
  973. * mark_journal_empty() - Mark on disk journal as empty.
  974. * @journal: The journal to update.
  975. *
  976. * Update a journal's dynamic superblock fields to show that journal is empty.
  977. * Write updated superblock to disk waiting for IO to complete.
  978. */
  979. static void mark_journal_empty(journal_t *journal)
  980. {
  981. journal_superblock_t *sb = journal->j_superblock;
  982. BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
  983. spin_lock(&journal->j_state_lock);
  984. /* Is it already empty? */
  985. if (sb->s_start == 0) {
  986. spin_unlock(&journal->j_state_lock);
  987. return;
  988. }
  989. jbd_debug(1, "JBD: Marking journal as empty (seq %d)\n",
  990. journal->j_tail_sequence);
  991. sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
  992. sb->s_start = cpu_to_be32(0);
  993. spin_unlock(&journal->j_state_lock);
  994. journal_write_superblock(journal, WRITE_FUA);
  995. spin_lock(&journal->j_state_lock);
  996. /* Log is empty */
  997. journal->j_flags |= JFS_FLUSHED;
  998. spin_unlock(&journal->j_state_lock);
  999. }
  1000. /**
  1001. * journal_update_sb_errno() - Update error in the journal.
  1002. * @journal: The journal to update.
  1003. *
  1004. * Update a journal's errno. Write updated superblock to disk waiting for IO
  1005. * to complete.
  1006. */
  1007. static void journal_update_sb_errno(journal_t *journal)
  1008. {
  1009. journal_superblock_t *sb = journal->j_superblock;
  1010. spin_lock(&journal->j_state_lock);
  1011. jbd_debug(1, "JBD: updating superblock error (errno %d)\n",
  1012. journal->j_errno);
  1013. sb->s_errno = cpu_to_be32(journal->j_errno);
  1014. spin_unlock(&journal->j_state_lock);
  1015. journal_write_superblock(journal, WRITE_SYNC);
  1016. }
  1017. /*
  1018. * Read the superblock for a given journal, performing initial
  1019. * validation of the format.
  1020. */
  1021. static int journal_get_superblock(journal_t *journal)
  1022. {
  1023. struct buffer_head *bh;
  1024. journal_superblock_t *sb;
  1025. int err = -EIO;
  1026. bh = journal->j_sb_buffer;
  1027. J_ASSERT(bh != NULL);
  1028. if (!buffer_uptodate(bh)) {
  1029. ll_rw_block(READ, 1, &bh);
  1030. wait_on_buffer(bh);
  1031. if (!buffer_uptodate(bh)) {
  1032. printk (KERN_ERR
  1033. "JBD: IO error reading journal superblock\n");
  1034. goto out;
  1035. }
  1036. }
  1037. sb = journal->j_superblock;
  1038. err = -EINVAL;
  1039. if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
  1040. sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
  1041. printk(KERN_WARNING "JBD: no valid journal superblock found\n");
  1042. goto out;
  1043. }
  1044. switch(be32_to_cpu(sb->s_header.h_blocktype)) {
  1045. case JFS_SUPERBLOCK_V1:
  1046. journal->j_format_version = 1;
  1047. break;
  1048. case JFS_SUPERBLOCK_V2:
  1049. journal->j_format_version = 2;
  1050. break;
  1051. default:
  1052. printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
  1053. goto out;
  1054. }
  1055. if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
  1056. journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
  1057. else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
  1058. printk (KERN_WARNING "JBD: journal file too short\n");
  1059. goto out;
  1060. }
  1061. if (be32_to_cpu(sb->s_first) == 0 ||
  1062. be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
  1063. printk(KERN_WARNING
  1064. "JBD: Invalid start block of journal: %u\n",
  1065. be32_to_cpu(sb->s_first));
  1066. goto out;
  1067. }
  1068. return 0;
  1069. out:
  1070. journal_fail_superblock(journal);
  1071. return err;
  1072. }
  1073. /*
  1074. * Load the on-disk journal superblock and read the key fields into the
  1075. * journal_t.
  1076. */
  1077. static int load_superblock(journal_t *journal)
  1078. {
  1079. int err;
  1080. journal_superblock_t *sb;
  1081. err = journal_get_superblock(journal);
  1082. if (err)
  1083. return err;
  1084. sb = journal->j_superblock;
  1085. journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
  1086. journal->j_tail = be32_to_cpu(sb->s_start);
  1087. journal->j_first = be32_to_cpu(sb->s_first);
  1088. journal->j_last = be32_to_cpu(sb->s_maxlen);
  1089. journal->j_errno = be32_to_cpu(sb->s_errno);
  1090. return 0;
  1091. }
  1092. /**
  1093. * int journal_load() - Read journal from disk.
  1094. * @journal: Journal to act on.
  1095. *
  1096. * Given a journal_t structure which tells us which disk blocks contain
  1097. * a journal, read the journal from disk to initialise the in-memory
  1098. * structures.
  1099. */
  1100. int journal_load(journal_t *journal)
  1101. {
  1102. int err;
  1103. journal_superblock_t *sb;
  1104. err = load_superblock(journal);
  1105. if (err)
  1106. return err;
  1107. sb = journal->j_superblock;
  1108. /* If this is a V2 superblock, then we have to check the
  1109. * features flags on it. */
  1110. if (journal->j_format_version >= 2) {
  1111. if ((sb->s_feature_ro_compat &
  1112. ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
  1113. (sb->s_feature_incompat &
  1114. ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
  1115. printk (KERN_WARNING
  1116. "JBD: Unrecognised features on journal\n");
  1117. return -EINVAL;
  1118. }
  1119. }
  1120. /* Let the recovery code check whether it needs to recover any
  1121. * data from the journal. */
  1122. if (journal_recover(journal))
  1123. goto recovery_error;
  1124. /* OK, we've finished with the dynamic journal bits:
  1125. * reinitialise the dynamic contents of the superblock in memory
  1126. * and reset them on disk. */
  1127. if (journal_reset(journal))
  1128. goto recovery_error;
  1129. journal->j_flags &= ~JFS_ABORT;
  1130. journal->j_flags |= JFS_LOADED;
  1131. return 0;
  1132. recovery_error:
  1133. printk (KERN_WARNING "JBD: recovery failed\n");
  1134. return -EIO;
  1135. }
  1136. /**
  1137. * void journal_destroy() - Release a journal_t structure.
  1138. * @journal: Journal to act on.
  1139. *
  1140. * Release a journal_t structure once it is no longer in use by the
  1141. * journaled object.
  1142. * Return <0 if we couldn't clean up the journal.
  1143. */
  1144. int journal_destroy(journal_t *journal)
  1145. {
  1146. int err = 0;
  1147. /* Wait for the commit thread to wake up and die. */
  1148. journal_kill_thread(journal);
  1149. /* Force a final log commit */
  1150. if (journal->j_running_transaction)
  1151. journal_commit_transaction(journal);
  1152. /* Force any old transactions to disk */
  1153. /* We cannot race with anybody but must keep assertions happy */
  1154. mutex_lock(&journal->j_checkpoint_mutex);
  1155. /* Totally anal locking here... */
  1156. spin_lock(&journal->j_list_lock);
  1157. while (journal->j_checkpoint_transactions != NULL) {
  1158. spin_unlock(&journal->j_list_lock);
  1159. log_do_checkpoint(journal);
  1160. spin_lock(&journal->j_list_lock);
  1161. }
  1162. J_ASSERT(journal->j_running_transaction == NULL);
  1163. J_ASSERT(journal->j_committing_transaction == NULL);
  1164. J_ASSERT(journal->j_checkpoint_transactions == NULL);
  1165. spin_unlock(&journal->j_list_lock);
  1166. if (journal->j_sb_buffer) {
  1167. if (!is_journal_aborted(journal)) {
  1168. journal->j_tail_sequence =
  1169. ++journal->j_transaction_sequence;
  1170. mark_journal_empty(journal);
  1171. } else
  1172. err = -EIO;
  1173. brelse(journal->j_sb_buffer);
  1174. }
  1175. mutex_unlock(&journal->j_checkpoint_mutex);
  1176. if (journal->j_inode)
  1177. iput(journal->j_inode);
  1178. if (journal->j_revoke)
  1179. journal_destroy_revoke(journal);
  1180. kfree(journal->j_wbuf);
  1181. kfree(journal);
  1182. return err;
  1183. }
  1184. /**
  1185. *int journal_check_used_features () - Check if features specified are used.
  1186. * @journal: Journal to check.
  1187. * @compat: bitmask of compatible features
  1188. * @ro: bitmask of features that force read-only mount
  1189. * @incompat: bitmask of incompatible features
  1190. *
  1191. * Check whether the journal uses all of a given set of
  1192. * features. Return true (non-zero) if it does.
  1193. **/
  1194. int journal_check_used_features (journal_t *journal, unsigned long compat,
  1195. unsigned long ro, unsigned long incompat)
  1196. {
  1197. journal_superblock_t *sb;
  1198. if (!compat && !ro && !incompat)
  1199. return 1;
  1200. if (journal->j_format_version == 1)
  1201. return 0;
  1202. sb = journal->j_superblock;
  1203. if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
  1204. ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
  1205. ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
  1206. return 1;
  1207. return 0;
  1208. }
  1209. /**
  1210. * int journal_check_available_features() - Check feature set in journalling layer
  1211. * @journal: Journal to check.
  1212. * @compat: bitmask of compatible features
  1213. * @ro: bitmask of features that force read-only mount
  1214. * @incompat: bitmask of incompatible features
  1215. *
  1216. * Check whether the journaling code supports the use of
  1217. * all of a given set of features on this journal. Return true
  1218. * (non-zero) if it can. */
  1219. int journal_check_available_features (journal_t *journal, unsigned long compat,
  1220. unsigned long ro, unsigned long incompat)
  1221. {
  1222. if (!compat && !ro && !incompat)
  1223. return 1;
  1224. /* We can support any known requested features iff the
  1225. * superblock is in version 2. Otherwise we fail to support any
  1226. * extended sb features. */
  1227. if (journal->j_format_version != 2)
  1228. return 0;
  1229. if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
  1230. (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
  1231. (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
  1232. return 1;
  1233. return 0;
  1234. }
  1235. /**
  1236. * int journal_set_features () - Mark a given journal feature in the superblock
  1237. * @journal: Journal to act on.
  1238. * @compat: bitmask of compatible features
  1239. * @ro: bitmask of features that force read-only mount
  1240. * @incompat: bitmask of incompatible features
  1241. *
  1242. * Mark a given journal feature as present on the
  1243. * superblock. Returns true if the requested features could be set.
  1244. *
  1245. */
  1246. int journal_set_features (journal_t *journal, unsigned long compat,
  1247. unsigned long ro, unsigned long incompat)
  1248. {
  1249. journal_superblock_t *sb;
  1250. if (journal_check_used_features(journal, compat, ro, incompat))
  1251. return 1;
  1252. if (!journal_check_available_features(journal, compat, ro, incompat))
  1253. return 0;
  1254. jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
  1255. compat, ro, incompat);
  1256. sb = journal->j_superblock;
  1257. sb->s_feature_compat |= cpu_to_be32(compat);
  1258. sb->s_feature_ro_compat |= cpu_to_be32(ro);
  1259. sb->s_feature_incompat |= cpu_to_be32(incompat);
  1260. return 1;
  1261. }
  1262. /**
  1263. * int journal_update_format () - Update on-disk journal structure.
  1264. * @journal: Journal to act on.
  1265. *
  1266. * Given an initialised but unloaded journal struct, poke about in the
  1267. * on-disk structure to update it to the most recent supported version.
  1268. */
  1269. int journal_update_format (journal_t *journal)
  1270. {
  1271. journal_superblock_t *sb;
  1272. int err;
  1273. err = journal_get_superblock(journal);
  1274. if (err)
  1275. return err;
  1276. sb = journal->j_superblock;
  1277. switch (be32_to_cpu(sb->s_header.h_blocktype)) {
  1278. case JFS_SUPERBLOCK_V2:
  1279. return 0;
  1280. case JFS_SUPERBLOCK_V1:
  1281. return journal_convert_superblock_v1(journal, sb);
  1282. default:
  1283. break;
  1284. }
  1285. return -EINVAL;
  1286. }
  1287. static int journal_convert_superblock_v1(journal_t *journal,
  1288. journal_superblock_t *sb)
  1289. {
  1290. int offset, blocksize;
  1291. struct buffer_head *bh;
  1292. printk(KERN_WARNING
  1293. "JBD: Converting superblock from version 1 to 2.\n");
  1294. /* Pre-initialise new fields to zero */
  1295. offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
  1296. blocksize = be32_to_cpu(sb->s_blocksize);
  1297. memset(&sb->s_feature_compat, 0, blocksize-offset);
  1298. sb->s_nr_users = cpu_to_be32(1);
  1299. sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
  1300. journal->j_format_version = 2;
  1301. bh = journal->j_sb_buffer;
  1302. BUFFER_TRACE(bh, "marking dirty");
  1303. mark_buffer_dirty(bh);
  1304. sync_dirty_buffer(bh);
  1305. return 0;
  1306. }
  1307. /**
  1308. * int journal_flush () - Flush journal
  1309. * @journal: Journal to act on.
  1310. *
  1311. * Flush all data for a given journal to disk and empty the journal.
  1312. * Filesystems can use this when remounting readonly to ensure that
  1313. * recovery does not need to happen on remount.
  1314. */
  1315. int journal_flush(journal_t *journal)
  1316. {
  1317. int err = 0;
  1318. transaction_t *transaction = NULL;
  1319. spin_lock(&journal->j_state_lock);
  1320. /* Force everything buffered to the log... */
  1321. if (journal->j_running_transaction) {
  1322. transaction = journal->j_running_transaction;
  1323. __log_start_commit(journal, transaction->t_tid);
  1324. } else if (journal->j_committing_transaction)
  1325. transaction = journal->j_committing_transaction;
  1326. /* Wait for the log commit to complete... */
  1327. if (transaction) {
  1328. tid_t tid = transaction->t_tid;
  1329. spin_unlock(&journal->j_state_lock);
  1330. log_wait_commit(journal, tid);
  1331. } else {
  1332. spin_unlock(&journal->j_state_lock);
  1333. }
  1334. /* ...and flush everything in the log out to disk. */
  1335. spin_lock(&journal->j_list_lock);
  1336. while (!err && journal->j_checkpoint_transactions != NULL) {
  1337. spin_unlock(&journal->j_list_lock);
  1338. mutex_lock(&journal->j_checkpoint_mutex);
  1339. err = log_do_checkpoint(journal);
  1340. mutex_unlock(&journal->j_checkpoint_mutex);
  1341. spin_lock(&journal->j_list_lock);
  1342. }
  1343. spin_unlock(&journal->j_list_lock);
  1344. if (is_journal_aborted(journal))
  1345. return -EIO;
  1346. mutex_lock(&journal->j_checkpoint_mutex);
  1347. cleanup_journal_tail(journal);
  1348. /* Finally, mark the journal as really needing no recovery.
  1349. * This sets s_start==0 in the underlying superblock, which is
  1350. * the magic code for a fully-recovered superblock. Any future
  1351. * commits of data to the journal will restore the current
  1352. * s_start value. */
  1353. mark_journal_empty(journal);
  1354. mutex_unlock(&journal->j_checkpoint_mutex);
  1355. spin_lock(&journal->j_state_lock);
  1356. J_ASSERT(!journal->j_running_transaction);
  1357. J_ASSERT(!journal->j_committing_transaction);
  1358. J_ASSERT(!journal->j_checkpoint_transactions);
  1359. J_ASSERT(journal->j_head == journal->j_tail);
  1360. J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
  1361. spin_unlock(&journal->j_state_lock);
  1362. return 0;
  1363. }
  1364. /**
  1365. * int journal_wipe() - Wipe journal contents
  1366. * @journal: Journal to act on.
  1367. * @write: flag (see below)
  1368. *
  1369. * Wipe out all of the contents of a journal, safely. This will produce
  1370. * a warning if the journal contains any valid recovery information.
  1371. * Must be called between journal_init_*() and journal_load().
  1372. *
  1373. * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
  1374. * we merely suppress recovery.
  1375. */
  1376. int journal_wipe(journal_t *journal, int write)
  1377. {
  1378. int err = 0;
  1379. J_ASSERT (!(journal->j_flags & JFS_LOADED));
  1380. err = load_superblock(journal);
  1381. if (err)
  1382. return err;
  1383. if (!journal->j_tail)
  1384. goto no_recovery;
  1385. printk (KERN_WARNING "JBD: %s recovery information on journal\n",
  1386. write ? "Clearing" : "Ignoring");
  1387. err = journal_skip_recovery(journal);
  1388. if (write) {
  1389. /* Lock to make assertions happy... */
  1390. mutex_lock(&journal->j_checkpoint_mutex);
  1391. mark_journal_empty(journal);
  1392. mutex_unlock(&journal->j_checkpoint_mutex);
  1393. }
  1394. no_recovery:
  1395. return err;
  1396. }
  1397. /*
  1398. * journal_dev_name: format a character string to describe on what
  1399. * device this journal is present.
  1400. */
  1401. static const char *journal_dev_name(journal_t *journal, char *buffer)
  1402. {
  1403. struct block_device *bdev;
  1404. if (journal->j_inode)
  1405. bdev = journal->j_inode->i_sb->s_bdev;
  1406. else
  1407. bdev = journal->j_dev;
  1408. return bdevname(bdev, buffer);
  1409. }
  1410. /*
  1411. * Journal abort has very specific semantics, which we describe
  1412. * for journal abort.
  1413. *
  1414. * Two internal function, which provide abort to te jbd layer
  1415. * itself are here.
  1416. */
  1417. /*
  1418. * Quick version for internal journal use (doesn't lock the journal).
  1419. * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
  1420. * and don't attempt to make any other journal updates.
  1421. */
  1422. static void __journal_abort_hard(journal_t *journal)
  1423. {
  1424. transaction_t *transaction;
  1425. char b[BDEVNAME_SIZE];
  1426. if (journal->j_flags & JFS_ABORT)
  1427. return;
  1428. printk(KERN_ERR "Aborting journal on device %s.\n",
  1429. journal_dev_name(journal, b));
  1430. spin_lock(&journal->j_state_lock);
  1431. journal->j_flags |= JFS_ABORT;
  1432. transaction = journal->j_running_transaction;
  1433. if (transaction)
  1434. __log_start_commit(journal, transaction->t_tid);
  1435. spin_unlock(&journal->j_state_lock);
  1436. }
  1437. /* Soft abort: record the abort error status in the journal superblock,
  1438. * but don't do any other IO. */
  1439. static void __journal_abort_soft (journal_t *journal, int errno)
  1440. {
  1441. if (journal->j_flags & JFS_ABORT)
  1442. return;
  1443. if (!journal->j_errno)
  1444. journal->j_errno = errno;
  1445. __journal_abort_hard(journal);
  1446. if (errno)
  1447. journal_update_sb_errno(journal);
  1448. }
  1449. /**
  1450. * void journal_abort () - Shutdown the journal immediately.
  1451. * @journal: the journal to shutdown.
  1452. * @errno: an error number to record in the journal indicating
  1453. * the reason for the shutdown.
  1454. *
  1455. * Perform a complete, immediate shutdown of the ENTIRE
  1456. * journal (not of a single transaction). This operation cannot be
  1457. * undone without closing and reopening the journal.
  1458. *
  1459. * The journal_abort function is intended to support higher level error
  1460. * recovery mechanisms such as the ext2/ext3 remount-readonly error
  1461. * mode.
  1462. *
  1463. * Journal abort has very specific semantics. Any existing dirty,
  1464. * unjournaled buffers in the main filesystem will still be written to
  1465. * disk by bdflush, but the journaling mechanism will be suspended
  1466. * immediately and no further transaction commits will be honoured.
  1467. *
  1468. * Any dirty, journaled buffers will be written back to disk without
  1469. * hitting the journal. Atomicity cannot be guaranteed on an aborted
  1470. * filesystem, but we _do_ attempt to leave as much data as possible
  1471. * behind for fsck to use for cleanup.
  1472. *
  1473. * Any attempt to get a new transaction handle on a journal which is in
  1474. * ABORT state will just result in an -EROFS error return. A
  1475. * journal_stop on an existing handle will return -EIO if we have
  1476. * entered abort state during the update.
  1477. *
  1478. * Recursive transactions are not disturbed by journal abort until the
  1479. * final journal_stop, which will receive the -EIO error.
  1480. *
  1481. * Finally, the journal_abort call allows the caller to supply an errno
  1482. * which will be recorded (if possible) in the journal superblock. This
  1483. * allows a client to record failure conditions in the middle of a
  1484. * transaction without having to complete the transaction to record the
  1485. * failure to disk. ext3_error, for example, now uses this
  1486. * functionality.
  1487. *
  1488. * Errors which originate from within the journaling layer will NOT
  1489. * supply an errno; a null errno implies that absolutely no further
  1490. * writes are done to the journal (unless there are any already in
  1491. * progress).
  1492. *
  1493. */
  1494. void journal_abort(journal_t *journal, int errno)
  1495. {
  1496. __journal_abort_soft(journal, errno);
  1497. }
  1498. /**
  1499. * int journal_errno () - returns the journal's error state.
  1500. * @journal: journal to examine.
  1501. *
  1502. * This is the errno numbet set with journal_abort(), the last
  1503. * time the journal was mounted - if the journal was stopped
  1504. * without calling abort this will be 0.
  1505. *
  1506. * If the journal has been aborted on this mount time -EROFS will
  1507. * be returned.
  1508. */
  1509. int journal_errno(journal_t *journal)
  1510. {
  1511. int err;
  1512. spin_lock(&journal->j_state_lock);
  1513. if (journal->j_flags & JFS_ABORT)
  1514. err = -EROFS;
  1515. else
  1516. err = journal->j_errno;
  1517. spin_unlock(&journal->j_state_lock);
  1518. return err;
  1519. }
  1520. /**
  1521. * int journal_clear_err () - clears the journal's error state
  1522. * @journal: journal to act on.
  1523. *
  1524. * An error must be cleared or Acked to take a FS out of readonly
  1525. * mode.
  1526. */
  1527. int journal_clear_err(journal_t *journal)
  1528. {
  1529. int err = 0;
  1530. spin_lock(&journal->j_state_lock);
  1531. if (journal->j_flags & JFS_ABORT)
  1532. err = -EROFS;
  1533. else
  1534. journal->j_errno = 0;
  1535. spin_unlock(&journal->j_state_lock);
  1536. return err;
  1537. }
  1538. /**
  1539. * void journal_ack_err() - Ack journal err.
  1540. * @journal: journal to act on.
  1541. *
  1542. * An error must be cleared or Acked to take a FS out of readonly
  1543. * mode.
  1544. */
  1545. void journal_ack_err(journal_t *journal)
  1546. {
  1547. spin_lock(&journal->j_state_lock);
  1548. if (journal->j_errno)
  1549. journal->j_flags |= JFS_ACK_ERR;
  1550. spin_unlock(&journal->j_state_lock);
  1551. }
  1552. int journal_blocks_per_page(struct inode *inode)
  1553. {
  1554. return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  1555. }
  1556. /*
  1557. * Journal_head storage management
  1558. */
  1559. static struct kmem_cache *journal_head_cache;
  1560. #ifdef CONFIG_JBD_DEBUG
  1561. static atomic_t nr_journal_heads = ATOMIC_INIT(0);
  1562. #endif
  1563. static int journal_init_journal_head_cache(void)
  1564. {
  1565. int retval;
  1566. J_ASSERT(journal_head_cache == NULL);
  1567. journal_head_cache = kmem_cache_create("journal_head",
  1568. sizeof(struct journal_head),
  1569. 0, /* offset */
  1570. SLAB_TEMPORARY, /* flags */
  1571. NULL); /* ctor */
  1572. retval = 0;
  1573. if (!journal_head_cache) {
  1574. retval = -ENOMEM;
  1575. printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
  1576. }
  1577. return retval;
  1578. }
  1579. static void journal_destroy_journal_head_cache(void)
  1580. {
  1581. if (journal_head_cache) {
  1582. kmem_cache_destroy(journal_head_cache);
  1583. journal_head_cache = NULL;
  1584. }
  1585. }
  1586. /*
  1587. * journal_head splicing and dicing
  1588. */
  1589. static struct journal_head *journal_alloc_journal_head(void)
  1590. {
  1591. struct journal_head *ret;
  1592. #ifdef CONFIG_JBD_DEBUG
  1593. atomic_inc(&nr_journal_heads);
  1594. #endif
  1595. ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
  1596. if (ret == NULL) {
  1597. jbd_debug(1, "out of memory for journal_head\n");
  1598. printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n",
  1599. __func__);
  1600. while (ret == NULL) {
  1601. yield();
  1602. ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
  1603. }
  1604. }
  1605. return ret;
  1606. }
  1607. static void journal_free_journal_head(struct journal_head *jh)
  1608. {
  1609. #ifdef CONFIG_JBD_DEBUG
  1610. atomic_dec(&nr_journal_heads);
  1611. memset(jh, JBD_POISON_FREE, sizeof(*jh));
  1612. #endif
  1613. kmem_cache_free(journal_head_cache, jh);
  1614. }
  1615. /*
  1616. * A journal_head is attached to a buffer_head whenever JBD has an
  1617. * interest in the buffer.
  1618. *
  1619. * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
  1620. * is set. This bit is tested in core kernel code where we need to take
  1621. * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
  1622. * there.
  1623. *
  1624. * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
  1625. *
  1626. * When a buffer has its BH_JBD bit set it is immune from being released by
  1627. * core kernel code, mainly via ->b_count.
  1628. *
  1629. * A journal_head is detached from its buffer_head when the journal_head's
  1630. * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
  1631. * transaction (b_cp_transaction) hold their references to b_jcount.
  1632. *
  1633. * Various places in the kernel want to attach a journal_head to a buffer_head
  1634. * _before_ attaching the journal_head to a transaction. To protect the
  1635. * journal_head in this situation, journal_add_journal_head elevates the
  1636. * journal_head's b_jcount refcount by one. The caller must call
  1637. * journal_put_journal_head() to undo this.
  1638. *
  1639. * So the typical usage would be:
  1640. *
  1641. * (Attach a journal_head if needed. Increments b_jcount)
  1642. * struct journal_head *jh = journal_add_journal_head(bh);
  1643. * ...
  1644. * (Get another reference for transaction)
  1645. * journal_grab_journal_head(bh);
  1646. * jh->b_transaction = xxx;
  1647. * (Put original reference)
  1648. * journal_put_journal_head(jh);
  1649. */
  1650. /*
  1651. * Give a buffer_head a journal_head.
  1652. *
  1653. * May sleep.
  1654. */
  1655. struct journal_head *journal_add_journal_head(struct buffer_head *bh)
  1656. {
  1657. struct journal_head *jh;
  1658. struct journal_head *new_jh = NULL;
  1659. repeat:
  1660. if (!buffer_jbd(bh)) {
  1661. new_jh = journal_alloc_journal_head();
  1662. memset(new_jh, 0, sizeof(*new_jh));
  1663. }
  1664. jbd_lock_bh_journal_head(bh);
  1665. if (buffer_jbd(bh)) {
  1666. jh = bh2jh(bh);
  1667. } else {
  1668. J_ASSERT_BH(bh,
  1669. (atomic_read(&bh->b_count) > 0) ||
  1670. (bh->b_page && bh->b_page->mapping));
  1671. if (!new_jh) {
  1672. jbd_unlock_bh_journal_head(bh);
  1673. goto repeat;
  1674. }
  1675. jh = new_jh;
  1676. new_jh = NULL; /* We consumed it */
  1677. set_buffer_jbd(bh);
  1678. bh->b_private = jh;
  1679. jh->b_bh = bh;
  1680. get_bh(bh);
  1681. BUFFER_TRACE(bh, "added journal_head");
  1682. }
  1683. jh->b_jcount++;
  1684. jbd_unlock_bh_journal_head(bh);
  1685. if (new_jh)
  1686. journal_free_journal_head(new_jh);
  1687. return bh->b_private;
  1688. }
  1689. /*
  1690. * Grab a ref against this buffer_head's journal_head. If it ended up not
  1691. * having a journal_head, return NULL
  1692. */
  1693. struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
  1694. {
  1695. struct journal_head *jh = NULL;
  1696. jbd_lock_bh_journal_head(bh);
  1697. if (buffer_jbd(bh)) {
  1698. jh = bh2jh(bh);
  1699. jh->b_jcount++;
  1700. }
  1701. jbd_unlock_bh_journal_head(bh);
  1702. return jh;
  1703. }
  1704. static void __journal_remove_journal_head(struct buffer_head *bh)
  1705. {
  1706. struct journal_head *jh = bh2jh(bh);
  1707. J_ASSERT_JH(jh, jh->b_jcount >= 0);
  1708. J_ASSERT_JH(jh, jh->b_transaction == NULL);
  1709. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  1710. J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
  1711. J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
  1712. J_ASSERT_BH(bh, buffer_jbd(bh));
  1713. J_ASSERT_BH(bh, jh2bh(jh) == bh);
  1714. BUFFER_TRACE(bh, "remove journal_head");
  1715. if (jh->b_frozen_data) {
  1716. printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
  1717. jbd_free(jh->b_frozen_data, bh->b_size);
  1718. }
  1719. if (jh->b_committed_data) {
  1720. printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
  1721. jbd_free(jh->b_committed_data, bh->b_size);
  1722. }
  1723. bh->b_private = NULL;
  1724. jh->b_bh = NULL; /* debug, really */
  1725. clear_buffer_jbd(bh);
  1726. journal_free_journal_head(jh);
  1727. }
  1728. /*
  1729. * Drop a reference on the passed journal_head. If it fell to zero then
  1730. * release the journal_head from the buffer_head.
  1731. */
  1732. void journal_put_journal_head(struct journal_head *jh)
  1733. {
  1734. struct buffer_head *bh = jh2bh(jh);
  1735. jbd_lock_bh_journal_head(bh);
  1736. J_ASSERT_JH(jh, jh->b_jcount > 0);
  1737. --jh->b_jcount;
  1738. if (!jh->b_jcount) {
  1739. __journal_remove_journal_head(bh);
  1740. jbd_unlock_bh_journal_head(bh);
  1741. __brelse(bh);
  1742. } else
  1743. jbd_unlock_bh_journal_head(bh);
  1744. }
  1745. /*
  1746. * debugfs tunables
  1747. */
  1748. #ifdef CONFIG_JBD_DEBUG
  1749. u8 journal_enable_debug __read_mostly;
  1750. EXPORT_SYMBOL(journal_enable_debug);
  1751. static struct dentry *jbd_debugfs_dir;
  1752. static struct dentry *jbd_debug;
  1753. static void __init jbd_create_debugfs_entry(void)
  1754. {
  1755. jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
  1756. if (jbd_debugfs_dir)
  1757. jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
  1758. jbd_debugfs_dir,
  1759. &journal_enable_debug);
  1760. }
  1761. static void __exit jbd_remove_debugfs_entry(void)
  1762. {
  1763. debugfs_remove(jbd_debug);
  1764. debugfs_remove(jbd_debugfs_dir);
  1765. }
  1766. #else
  1767. static inline void jbd_create_debugfs_entry(void)
  1768. {
  1769. }
  1770. static inline void jbd_remove_debugfs_entry(void)
  1771. {
  1772. }
  1773. #endif
  1774. struct kmem_cache *jbd_handle_cache;
  1775. static int __init journal_init_handle_cache(void)
  1776. {
  1777. jbd_handle_cache = kmem_cache_create("journal_handle",
  1778. sizeof(handle_t),
  1779. 0, /* offset */
  1780. SLAB_TEMPORARY, /* flags */
  1781. NULL); /* ctor */
  1782. if (jbd_handle_cache == NULL) {
  1783. printk(KERN_EMERG "JBD: failed to create handle cache\n");
  1784. return -ENOMEM;
  1785. }
  1786. return 0;
  1787. }
  1788. static void journal_destroy_handle_cache(void)
  1789. {
  1790. if (jbd_handle_cache)
  1791. kmem_cache_destroy(jbd_handle_cache);
  1792. }
  1793. /*
  1794. * Module startup and shutdown
  1795. */
  1796. static int __init journal_init_caches(void)
  1797. {
  1798. int ret;
  1799. ret = journal_init_revoke_caches();
  1800. if (ret == 0)
  1801. ret = journal_init_journal_head_cache();
  1802. if (ret == 0)
  1803. ret = journal_init_handle_cache();
  1804. return ret;
  1805. }
  1806. static void journal_destroy_caches(void)
  1807. {
  1808. journal_destroy_revoke_caches();
  1809. journal_destroy_journal_head_cache();
  1810. journal_destroy_handle_cache();
  1811. }
  1812. static int __init journal_init(void)
  1813. {
  1814. int ret;
  1815. BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
  1816. ret = journal_init_caches();
  1817. if (ret != 0)
  1818. journal_destroy_caches();
  1819. jbd_create_debugfs_entry();
  1820. return ret;
  1821. }
  1822. static void __exit journal_exit(void)
  1823. {
  1824. #ifdef CONFIG_JBD_DEBUG
  1825. int n = atomic_read(&nr_journal_heads);
  1826. if (n)
  1827. printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
  1828. #endif
  1829. jbd_remove_debugfs_entry();
  1830. journal_destroy_caches();
  1831. }
  1832. MODULE_LICENSE("GPL");
  1833. module_init(journal_init);
  1834. module_exit(journal_exit);