journal.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443
  1. /*
  2. * linux/fs/jbd2/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/jbd2.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/seq_file.h>
  39. #include <linux/math64.h>
  40. #include <linux/hash.h>
  41. #include <linux/log2.h>
  42. #include <linux/vmalloc.h>
  43. #include <linux/backing-dev.h>
  44. #include <linux/bitops.h>
  45. #include <linux/ratelimit.h>
  46. #define CREATE_TRACE_POINTS
  47. #include <trace/events/jbd2.h>
  48. #include <asm/uaccess.h>
  49. #include <asm/page.h>
  50. #include <asm/system.h>
  51. EXPORT_SYMBOL(jbd2_journal_extend);
  52. EXPORT_SYMBOL(jbd2_journal_stop);
  53. EXPORT_SYMBOL(jbd2_journal_lock_updates);
  54. EXPORT_SYMBOL(jbd2_journal_unlock_updates);
  55. EXPORT_SYMBOL(jbd2_journal_get_write_access);
  56. EXPORT_SYMBOL(jbd2_journal_get_create_access);
  57. EXPORT_SYMBOL(jbd2_journal_get_undo_access);
  58. EXPORT_SYMBOL(jbd2_journal_set_triggers);
  59. EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
  60. EXPORT_SYMBOL(jbd2_journal_release_buffer);
  61. EXPORT_SYMBOL(jbd2_journal_forget);
  62. #if 0
  63. EXPORT_SYMBOL(journal_sync_buffer);
  64. #endif
  65. EXPORT_SYMBOL(jbd2_journal_flush);
  66. EXPORT_SYMBOL(jbd2_journal_revoke);
  67. EXPORT_SYMBOL(jbd2_journal_init_dev);
  68. EXPORT_SYMBOL(jbd2_journal_init_inode);
  69. EXPORT_SYMBOL(jbd2_journal_update_format);
  70. EXPORT_SYMBOL(jbd2_journal_check_used_features);
  71. EXPORT_SYMBOL(jbd2_journal_check_available_features);
  72. EXPORT_SYMBOL(jbd2_journal_set_features);
  73. EXPORT_SYMBOL(jbd2_journal_load);
  74. EXPORT_SYMBOL(jbd2_journal_destroy);
  75. EXPORT_SYMBOL(jbd2_journal_abort);
  76. EXPORT_SYMBOL(jbd2_journal_errno);
  77. EXPORT_SYMBOL(jbd2_journal_ack_err);
  78. EXPORT_SYMBOL(jbd2_journal_clear_err);
  79. EXPORT_SYMBOL(jbd2_log_wait_commit);
  80. EXPORT_SYMBOL(jbd2_log_start_commit);
  81. EXPORT_SYMBOL(jbd2_journal_start_commit);
  82. EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
  83. EXPORT_SYMBOL(jbd2_journal_wipe);
  84. EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
  85. EXPORT_SYMBOL(jbd2_journal_invalidatepage);
  86. EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
  87. EXPORT_SYMBOL(jbd2_journal_force_commit);
  88. EXPORT_SYMBOL(jbd2_journal_file_inode);
  89. EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
  90. EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
  91. EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
  92. EXPORT_SYMBOL(jbd2_inode_cache);
  93. static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
  94. static void __journal_abort_soft (journal_t *journal, int errno);
  95. static int jbd2_journal_create_slab(size_t slab_size);
  96. /*
  97. * Helper function used to manage commit timeouts
  98. */
  99. static void commit_timeout(unsigned long __data)
  100. {
  101. struct task_struct * p = (struct task_struct *) __data;
  102. wake_up_process(p);
  103. }
  104. /*
  105. * kjournald2: The main thread function used to manage a logging device
  106. * journal.
  107. *
  108. * This kernel thread is responsible for two things:
  109. *
  110. * 1) COMMIT: Every so often we need to commit the current state of the
  111. * filesystem to disk. The journal thread is responsible for writing
  112. * all of the metadata buffers to disk.
  113. *
  114. * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
  115. * of the data in that part of the log has been rewritten elsewhere on
  116. * the disk. Flushing these old buffers to reclaim space in the log is
  117. * known as checkpointing, and this thread is responsible for that job.
  118. */
  119. static int kjournald2(void *arg)
  120. {
  121. journal_t *journal = arg;
  122. transaction_t *transaction;
  123. /*
  124. * Set up an interval timer which can be used to trigger a commit wakeup
  125. * after the commit interval expires
  126. */
  127. setup_timer(&journal->j_commit_timer, commit_timeout,
  128. (unsigned long)current);
  129. /* Record that the journal thread is running */
  130. journal->j_task = current;
  131. wake_up(&journal->j_wait_done_commit);
  132. /*
  133. * And now, wait forever for commit wakeup events.
  134. */
  135. write_lock(&journal->j_state_lock);
  136. loop:
  137. if (journal->j_flags & JBD2_UNMOUNT)
  138. goto end_loop;
  139. jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
  140. journal->j_commit_sequence, journal->j_commit_request);
  141. if (journal->j_commit_sequence != journal->j_commit_request) {
  142. jbd_debug(1, "OK, requests differ\n");
  143. write_unlock(&journal->j_state_lock);
  144. del_timer_sync(&journal->j_commit_timer);
  145. jbd2_journal_commit_transaction(journal);
  146. write_lock(&journal->j_state_lock);
  147. goto loop;
  148. }
  149. wake_up(&journal->j_wait_done_commit);
  150. if (freezing(current)) {
  151. /*
  152. * The simpler the better. Flushing journal isn't a
  153. * good idea, because that depends on threads that may
  154. * be already stopped.
  155. */
  156. jbd_debug(1, "Now suspending kjournald2\n");
  157. write_unlock(&journal->j_state_lock);
  158. refrigerator();
  159. write_lock(&journal->j_state_lock);
  160. } else {
  161. /*
  162. * We assume on resume that commits are already there,
  163. * so we don't sleep
  164. */
  165. DEFINE_WAIT(wait);
  166. int should_sleep = 1;
  167. prepare_to_wait(&journal->j_wait_commit, &wait,
  168. TASK_INTERRUPTIBLE);
  169. if (journal->j_commit_sequence != journal->j_commit_request)
  170. should_sleep = 0;
  171. transaction = journal->j_running_transaction;
  172. if (transaction && time_after_eq(jiffies,
  173. transaction->t_expires))
  174. should_sleep = 0;
  175. if (journal->j_flags & JBD2_UNMOUNT)
  176. should_sleep = 0;
  177. if (should_sleep) {
  178. write_unlock(&journal->j_state_lock);
  179. schedule();
  180. write_lock(&journal->j_state_lock);
  181. }
  182. finish_wait(&journal->j_wait_commit, &wait);
  183. }
  184. jbd_debug(1, "kjournald2 wakes\n");
  185. /*
  186. * Were we woken up by a commit wakeup event?
  187. */
  188. transaction = journal->j_running_transaction;
  189. if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
  190. journal->j_commit_request = transaction->t_tid;
  191. jbd_debug(1, "woke because of timeout\n");
  192. }
  193. goto loop;
  194. end_loop:
  195. write_unlock(&journal->j_state_lock);
  196. del_timer_sync(&journal->j_commit_timer);
  197. journal->j_task = NULL;
  198. wake_up(&journal->j_wait_done_commit);
  199. jbd_debug(1, "Journal thread exiting.\n");
  200. return 0;
  201. }
  202. static int jbd2_journal_start_thread(journal_t *journal)
  203. {
  204. struct task_struct *t;
  205. t = kthread_run(kjournald2, journal, "jbd2/%s",
  206. journal->j_devname);
  207. if (IS_ERR(t))
  208. return PTR_ERR(t);
  209. wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
  210. return 0;
  211. }
  212. static void journal_kill_thread(journal_t *journal)
  213. {
  214. write_lock(&journal->j_state_lock);
  215. journal->j_flags |= JBD2_UNMOUNT;
  216. while (journal->j_task) {
  217. wake_up(&journal->j_wait_commit);
  218. write_unlock(&journal->j_state_lock);
  219. wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
  220. write_lock(&journal->j_state_lock);
  221. }
  222. write_unlock(&journal->j_state_lock);
  223. }
  224. /*
  225. * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
  226. *
  227. * Writes a metadata buffer to a given disk block. The actual IO is not
  228. * performed but a new buffer_head is constructed which labels the data
  229. * to be written with the correct destination disk block.
  230. *
  231. * Any magic-number escaping which needs to be done will cause a
  232. * copy-out here. If the buffer happens to start with the
  233. * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
  234. * magic number is only written to the log for descripter blocks. In
  235. * this case, we copy the data and replace the first word with 0, and we
  236. * return a result code which indicates that this buffer needs to be
  237. * marked as an escaped buffer in the corresponding log descriptor
  238. * block. The missing word can then be restored when the block is read
  239. * during recovery.
  240. *
  241. * If the source buffer has already been modified by a new transaction
  242. * since we took the last commit snapshot, we use the frozen copy of
  243. * that data for IO. If we end up using the existing buffer_head's data
  244. * for the write, then we *have* to lock the buffer to prevent anyone
  245. * else from using and possibly modifying it while the IO is in
  246. * progress.
  247. *
  248. * The function returns a pointer to the buffer_heads to be used for IO.
  249. *
  250. * We assume that the journal has already been locked in this function.
  251. *
  252. * Return value:
  253. * <0: Error
  254. * >=0: Finished OK
  255. *
  256. * On success:
  257. * Bit 0 set == escape performed on the data
  258. * Bit 1 set == buffer copy-out performed (kfree the data after IO)
  259. */
  260. int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
  261. struct journal_head *jh_in,
  262. struct journal_head **jh_out,
  263. unsigned long long blocknr)
  264. {
  265. int need_copy_out = 0;
  266. int done_copy_out = 0;
  267. int do_escape = 0;
  268. char *mapped_data;
  269. struct buffer_head *new_bh;
  270. struct journal_head *new_jh;
  271. struct page *new_page;
  272. unsigned int new_offset;
  273. struct buffer_head *bh_in = jh2bh(jh_in);
  274. journal_t *journal = transaction->t_journal;
  275. /*
  276. * The buffer really shouldn't be locked: only the current committing
  277. * transaction is allowed to write it, so nobody else is allowed
  278. * to do any IO.
  279. *
  280. * akpm: except if we're journalling data, and write() output is
  281. * also part of a shared mapping, and another thread has
  282. * decided to launch a writepage() against this buffer.
  283. */
  284. J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
  285. retry_alloc:
  286. new_bh = alloc_buffer_head(GFP_NOFS);
  287. if (!new_bh) {
  288. /*
  289. * Failure is not an option, but __GFP_NOFAIL is going
  290. * away; so we retry ourselves here.
  291. */
  292. congestion_wait(BLK_RW_ASYNC, HZ/50);
  293. goto retry_alloc;
  294. }
  295. /* keep subsequent assertions sane */
  296. new_bh->b_state = 0;
  297. init_buffer(new_bh, NULL, NULL);
  298. atomic_set(&new_bh->b_count, 1);
  299. new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
  300. /*
  301. * If a new transaction has already done a buffer copy-out, then
  302. * we use that version of the data for the commit.
  303. */
  304. jbd_lock_bh_state(bh_in);
  305. repeat:
  306. if (jh_in->b_frozen_data) {
  307. done_copy_out = 1;
  308. new_page = virt_to_page(jh_in->b_frozen_data);
  309. new_offset = offset_in_page(jh_in->b_frozen_data);
  310. } else {
  311. new_page = jh2bh(jh_in)->b_page;
  312. new_offset = offset_in_page(jh2bh(jh_in)->b_data);
  313. }
  314. mapped_data = kmap_atomic(new_page, KM_USER0);
  315. /*
  316. * Fire data frozen trigger if data already wasn't frozen. Do this
  317. * before checking for escaping, as the trigger may modify the magic
  318. * offset. If a copy-out happens afterwards, it will have the correct
  319. * data in the buffer.
  320. */
  321. if (!done_copy_out)
  322. jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
  323. jh_in->b_triggers);
  324. /*
  325. * Check for escaping
  326. */
  327. if (*((__be32 *)(mapped_data + new_offset)) ==
  328. cpu_to_be32(JBD2_MAGIC_NUMBER)) {
  329. need_copy_out = 1;
  330. do_escape = 1;
  331. }
  332. kunmap_atomic(mapped_data, KM_USER0);
  333. /*
  334. * Do we need to do a data copy?
  335. */
  336. if (need_copy_out && !done_copy_out) {
  337. char *tmp;
  338. jbd_unlock_bh_state(bh_in);
  339. tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
  340. if (!tmp) {
  341. jbd2_journal_put_journal_head(new_jh);
  342. return -ENOMEM;
  343. }
  344. jbd_lock_bh_state(bh_in);
  345. if (jh_in->b_frozen_data) {
  346. jbd2_free(tmp, bh_in->b_size);
  347. goto repeat;
  348. }
  349. jh_in->b_frozen_data = tmp;
  350. mapped_data = kmap_atomic(new_page, KM_USER0);
  351. memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
  352. kunmap_atomic(mapped_data, KM_USER0);
  353. new_page = virt_to_page(tmp);
  354. new_offset = offset_in_page(tmp);
  355. done_copy_out = 1;
  356. /*
  357. * This isn't strictly necessary, as we're using frozen
  358. * data for the escaping, but it keeps consistency with
  359. * b_frozen_data usage.
  360. */
  361. jh_in->b_frozen_triggers = jh_in->b_triggers;
  362. }
  363. /*
  364. * Did we need to do an escaping? Now we've done all the
  365. * copying, we can finally do so.
  366. */
  367. if (do_escape) {
  368. mapped_data = kmap_atomic(new_page, KM_USER0);
  369. *((unsigned int *)(mapped_data + new_offset)) = 0;
  370. kunmap_atomic(mapped_data, KM_USER0);
  371. }
  372. set_bh_page(new_bh, new_page, new_offset);
  373. new_jh->b_transaction = NULL;
  374. new_bh->b_size = jh2bh(jh_in)->b_size;
  375. new_bh->b_bdev = transaction->t_journal->j_dev;
  376. new_bh->b_blocknr = blocknr;
  377. set_buffer_mapped(new_bh);
  378. set_buffer_dirty(new_bh);
  379. *jh_out = new_jh;
  380. /*
  381. * The to-be-written buffer needs to get moved to the io queue,
  382. * and the original buffer whose contents we are shadowing or
  383. * copying is moved to the transaction's shadow queue.
  384. */
  385. JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
  386. spin_lock(&journal->j_list_lock);
  387. __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
  388. spin_unlock(&journal->j_list_lock);
  389. jbd_unlock_bh_state(bh_in);
  390. JBUFFER_TRACE(new_jh, "file as BJ_IO");
  391. jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
  392. return do_escape | (done_copy_out << 1);
  393. }
  394. /*
  395. * Allocation code for the journal file. Manage the space left in the
  396. * journal, so that we can begin checkpointing when appropriate.
  397. */
  398. /*
  399. * __jbd2_log_space_left: Return the number of free blocks left in the journal.
  400. *
  401. * Called with the journal already locked.
  402. *
  403. * Called under j_state_lock
  404. */
  405. int __jbd2_log_space_left(journal_t *journal)
  406. {
  407. int left = journal->j_free;
  408. /* assert_spin_locked(&journal->j_state_lock); */
  409. /*
  410. * Be pessimistic here about the number of those free blocks which
  411. * might be required for log descriptor control blocks.
  412. */
  413. #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
  414. left -= MIN_LOG_RESERVED_BLOCKS;
  415. if (left <= 0)
  416. return 0;
  417. left -= (left >> 3);
  418. return left;
  419. }
  420. /*
  421. * Called with j_state_lock locked for writing.
  422. * Returns true if a transaction commit was started.
  423. */
  424. int __jbd2_log_start_commit(journal_t *journal, tid_t target)
  425. {
  426. /*
  427. * Are we already doing a recent enough commit?
  428. */
  429. if (!tid_geq(journal->j_commit_request, target)) {
  430. /*
  431. * We want a new commit: OK, mark the request and wakeup the
  432. * commit thread. We do _not_ do the commit ourselves.
  433. */
  434. journal->j_commit_request = target;
  435. jbd_debug(1, "JBD: requesting commit %d/%d\n",
  436. journal->j_commit_request,
  437. journal->j_commit_sequence);
  438. wake_up(&journal->j_wait_commit);
  439. return 1;
  440. }
  441. return 0;
  442. }
  443. int jbd2_log_start_commit(journal_t *journal, tid_t tid)
  444. {
  445. int ret;
  446. write_lock(&journal->j_state_lock);
  447. ret = __jbd2_log_start_commit(journal, tid);
  448. write_unlock(&journal->j_state_lock);
  449. return ret;
  450. }
  451. /*
  452. * Force and wait upon a commit if the calling process is not within
  453. * transaction. This is used for forcing out undo-protected data which contains
  454. * bitmaps, when the fs is running out of space.
  455. *
  456. * We can only force the running transaction if we don't have an active handle;
  457. * otherwise, we will deadlock.
  458. *
  459. * Returns true if a transaction was started.
  460. */
  461. int jbd2_journal_force_commit_nested(journal_t *journal)
  462. {
  463. transaction_t *transaction = NULL;
  464. tid_t tid;
  465. int need_to_start = 0;
  466. read_lock(&journal->j_state_lock);
  467. if (journal->j_running_transaction && !current->journal_info) {
  468. transaction = journal->j_running_transaction;
  469. if (!tid_geq(journal->j_commit_request, transaction->t_tid))
  470. need_to_start = 1;
  471. } else if (journal->j_committing_transaction)
  472. transaction = journal->j_committing_transaction;
  473. if (!transaction) {
  474. read_unlock(&journal->j_state_lock);
  475. return 0; /* Nothing to retry */
  476. }
  477. tid = transaction->t_tid;
  478. read_unlock(&journal->j_state_lock);
  479. if (need_to_start)
  480. jbd2_log_start_commit(journal, tid);
  481. jbd2_log_wait_commit(journal, tid);
  482. return 1;
  483. }
  484. /*
  485. * Start a commit of the current running transaction (if any). Returns true
  486. * if a transaction is going to be committed (or is currently already
  487. * committing), and fills its tid in at *ptid
  488. */
  489. int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
  490. {
  491. int ret = 0;
  492. write_lock(&journal->j_state_lock);
  493. if (journal->j_running_transaction) {
  494. tid_t tid = journal->j_running_transaction->t_tid;
  495. __jbd2_log_start_commit(journal, tid);
  496. /* There's a running transaction and we've just made sure
  497. * it's commit has been scheduled. */
  498. if (ptid)
  499. *ptid = tid;
  500. ret = 1;
  501. } else if (journal->j_committing_transaction) {
  502. /*
  503. * If ext3_write_super() recently started a commit, then we
  504. * have to wait for completion of that transaction
  505. */
  506. if (ptid)
  507. *ptid = journal->j_committing_transaction->t_tid;
  508. ret = 1;
  509. }
  510. write_unlock(&journal->j_state_lock);
  511. return ret;
  512. }
  513. /*
  514. * Wait for a specified commit to complete.
  515. * The caller may not hold the journal lock.
  516. */
  517. int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
  518. {
  519. int err = 0;
  520. read_lock(&journal->j_state_lock);
  521. #ifdef CONFIG_JBD2_DEBUG
  522. if (!tid_geq(journal->j_commit_request, tid)) {
  523. printk(KERN_EMERG
  524. "%s: error: j_commit_request=%d, tid=%d\n",
  525. __func__, journal->j_commit_request, tid);
  526. }
  527. #endif
  528. while (tid_gt(tid, journal->j_commit_sequence)) {
  529. jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
  530. tid, journal->j_commit_sequence);
  531. wake_up(&journal->j_wait_commit);
  532. read_unlock(&journal->j_state_lock);
  533. wait_event(journal->j_wait_done_commit,
  534. !tid_gt(tid, journal->j_commit_sequence));
  535. read_lock(&journal->j_state_lock);
  536. }
  537. read_unlock(&journal->j_state_lock);
  538. if (unlikely(is_journal_aborted(journal))) {
  539. printk(KERN_EMERG "journal commit I/O error\n");
  540. err = -EIO;
  541. }
  542. return err;
  543. }
  544. /*
  545. * Log buffer allocation routines:
  546. */
  547. int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
  548. {
  549. unsigned long blocknr;
  550. write_lock(&journal->j_state_lock);
  551. J_ASSERT(journal->j_free > 1);
  552. blocknr = journal->j_head;
  553. journal->j_head++;
  554. journal->j_free--;
  555. if (journal->j_head == journal->j_last)
  556. journal->j_head = journal->j_first;
  557. write_unlock(&journal->j_state_lock);
  558. return jbd2_journal_bmap(journal, blocknr, retp);
  559. }
  560. /*
  561. * Conversion of logical to physical block numbers for the journal
  562. *
  563. * On external journals the journal blocks are identity-mapped, so
  564. * this is a no-op. If needed, we can use j_blk_offset - everything is
  565. * ready.
  566. */
  567. int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
  568. unsigned long long *retp)
  569. {
  570. int err = 0;
  571. unsigned long long ret;
  572. if (journal->j_inode) {
  573. ret = bmap(journal->j_inode, blocknr);
  574. if (ret)
  575. *retp = ret;
  576. else {
  577. printk(KERN_ALERT "%s: journal block not found "
  578. "at offset %lu on %s\n",
  579. __func__, blocknr, journal->j_devname);
  580. err = -EIO;
  581. __journal_abort_soft(journal, err);
  582. }
  583. } else {
  584. *retp = blocknr; /* +journal->j_blk_offset */
  585. }
  586. return err;
  587. }
  588. /*
  589. * We play buffer_head aliasing tricks to write data/metadata blocks to
  590. * the journal without copying their contents, but for journal
  591. * descriptor blocks we do need to generate bona fide buffers.
  592. *
  593. * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
  594. * the buffer's contents they really should run flush_dcache_page(bh->b_page).
  595. * But we don't bother doing that, so there will be coherency problems with
  596. * mmaps of blockdevs which hold live JBD-controlled filesystems.
  597. */
  598. struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
  599. {
  600. struct buffer_head *bh;
  601. unsigned long long blocknr;
  602. int err;
  603. err = jbd2_journal_next_log_block(journal, &blocknr);
  604. if (err)
  605. return NULL;
  606. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  607. if (!bh)
  608. return NULL;
  609. lock_buffer(bh);
  610. memset(bh->b_data, 0, journal->j_blocksize);
  611. set_buffer_uptodate(bh);
  612. unlock_buffer(bh);
  613. BUFFER_TRACE(bh, "return this buffer");
  614. return jbd2_journal_add_journal_head(bh);
  615. }
  616. struct jbd2_stats_proc_session {
  617. journal_t *journal;
  618. struct transaction_stats_s *stats;
  619. int start;
  620. int max;
  621. };
  622. static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
  623. {
  624. return *pos ? NULL : SEQ_START_TOKEN;
  625. }
  626. static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
  627. {
  628. return NULL;
  629. }
  630. static int jbd2_seq_info_show(struct seq_file *seq, void *v)
  631. {
  632. struct jbd2_stats_proc_session *s = seq->private;
  633. if (v != SEQ_START_TOKEN)
  634. return 0;
  635. seq_printf(seq, "%lu transaction, each up to %u blocks\n",
  636. s->stats->ts_tid,
  637. s->journal->j_max_transaction_buffers);
  638. if (s->stats->ts_tid == 0)
  639. return 0;
  640. seq_printf(seq, "average: \n %ums waiting for transaction\n",
  641. jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
  642. seq_printf(seq, " %ums running transaction\n",
  643. jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
  644. seq_printf(seq, " %ums transaction was being locked\n",
  645. jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
  646. seq_printf(seq, " %ums flushing data (in ordered mode)\n",
  647. jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
  648. seq_printf(seq, " %ums logging transaction\n",
  649. jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
  650. seq_printf(seq, " %lluus average transaction commit time\n",
  651. div_u64(s->journal->j_average_commit_time, 1000));
  652. seq_printf(seq, " %lu handles per transaction\n",
  653. s->stats->run.rs_handle_count / s->stats->ts_tid);
  654. seq_printf(seq, " %lu blocks per transaction\n",
  655. s->stats->run.rs_blocks / s->stats->ts_tid);
  656. seq_printf(seq, " %lu logged blocks per transaction\n",
  657. s->stats->run.rs_blocks_logged / s->stats->ts_tid);
  658. return 0;
  659. }
  660. static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
  661. {
  662. }
  663. static const struct seq_operations jbd2_seq_info_ops = {
  664. .start = jbd2_seq_info_start,
  665. .next = jbd2_seq_info_next,
  666. .stop = jbd2_seq_info_stop,
  667. .show = jbd2_seq_info_show,
  668. };
  669. static int jbd2_seq_info_open(struct inode *inode, struct file *file)
  670. {
  671. journal_t *journal = PDE(inode)->data;
  672. struct jbd2_stats_proc_session *s;
  673. int rc, size;
  674. s = kmalloc(sizeof(*s), GFP_KERNEL);
  675. if (s == NULL)
  676. return -ENOMEM;
  677. size = sizeof(struct transaction_stats_s);
  678. s->stats = kmalloc(size, GFP_KERNEL);
  679. if (s->stats == NULL) {
  680. kfree(s);
  681. return -ENOMEM;
  682. }
  683. spin_lock(&journal->j_history_lock);
  684. memcpy(s->stats, &journal->j_stats, size);
  685. s->journal = journal;
  686. spin_unlock(&journal->j_history_lock);
  687. rc = seq_open(file, &jbd2_seq_info_ops);
  688. if (rc == 0) {
  689. struct seq_file *m = file->private_data;
  690. m->private = s;
  691. } else {
  692. kfree(s->stats);
  693. kfree(s);
  694. }
  695. return rc;
  696. }
  697. static int jbd2_seq_info_release(struct inode *inode, struct file *file)
  698. {
  699. struct seq_file *seq = file->private_data;
  700. struct jbd2_stats_proc_session *s = seq->private;
  701. kfree(s->stats);
  702. kfree(s);
  703. return seq_release(inode, file);
  704. }
  705. static const struct file_operations jbd2_seq_info_fops = {
  706. .owner = THIS_MODULE,
  707. .open = jbd2_seq_info_open,
  708. .read = seq_read,
  709. .llseek = seq_lseek,
  710. .release = jbd2_seq_info_release,
  711. };
  712. static struct proc_dir_entry *proc_jbd2_stats;
  713. static void jbd2_stats_proc_init(journal_t *journal)
  714. {
  715. journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
  716. if (journal->j_proc_entry) {
  717. proc_create_data("info", S_IRUGO, journal->j_proc_entry,
  718. &jbd2_seq_info_fops, journal);
  719. }
  720. }
  721. static void jbd2_stats_proc_exit(journal_t *journal)
  722. {
  723. remove_proc_entry("info", journal->j_proc_entry);
  724. remove_proc_entry(journal->j_devname, proc_jbd2_stats);
  725. }
  726. /*
  727. * Management for journal control blocks: functions to create and
  728. * destroy journal_t structures, and to initialise and read existing
  729. * journal blocks from disk. */
  730. /* First: create and setup a journal_t object in memory. We initialise
  731. * very few fields yet: that has to wait until we have created the
  732. * journal structures from from scratch, or loaded them from disk. */
  733. static journal_t * journal_init_common (void)
  734. {
  735. journal_t *journal;
  736. int err;
  737. journal = kzalloc(sizeof(*journal), GFP_KERNEL);
  738. if (!journal)
  739. return NULL;
  740. init_waitqueue_head(&journal->j_wait_transaction_locked);
  741. init_waitqueue_head(&journal->j_wait_logspace);
  742. init_waitqueue_head(&journal->j_wait_done_commit);
  743. init_waitqueue_head(&journal->j_wait_checkpoint);
  744. init_waitqueue_head(&journal->j_wait_commit);
  745. init_waitqueue_head(&journal->j_wait_updates);
  746. mutex_init(&journal->j_barrier);
  747. mutex_init(&journal->j_checkpoint_mutex);
  748. spin_lock_init(&journal->j_revoke_lock);
  749. spin_lock_init(&journal->j_list_lock);
  750. rwlock_init(&journal->j_state_lock);
  751. journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
  752. journal->j_min_batch_time = 0;
  753. journal->j_max_batch_time = 15000; /* 15ms */
  754. /* The journal is marked for error until we succeed with recovery! */
  755. journal->j_flags = JBD2_ABORT;
  756. /* Set up a default-sized revoke table for the new mount. */
  757. err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
  758. if (err) {
  759. kfree(journal);
  760. return NULL;
  761. }
  762. spin_lock_init(&journal->j_history_lock);
  763. return journal;
  764. }
  765. /* jbd2_journal_init_dev and jbd2_journal_init_inode:
  766. *
  767. * Create a journal structure assigned some fixed set of disk blocks to
  768. * the journal. We don't actually touch those disk blocks yet, but we
  769. * need to set up all of the mapping information to tell the journaling
  770. * system where the journal blocks are.
  771. *
  772. */
  773. /**
  774. * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
  775. * @bdev: Block device on which to create the journal
  776. * @fs_dev: Device which hold journalled filesystem for this journal.
  777. * @start: Block nr Start of journal.
  778. * @len: Length of the journal in blocks.
  779. * @blocksize: blocksize of journalling device
  780. *
  781. * Returns: a newly created journal_t *
  782. *
  783. * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
  784. * range of blocks on an arbitrary block device.
  785. *
  786. */
  787. journal_t * jbd2_journal_init_dev(struct block_device *bdev,
  788. struct block_device *fs_dev,
  789. unsigned long long start, int len, int blocksize)
  790. {
  791. journal_t *journal = journal_init_common();
  792. struct buffer_head *bh;
  793. char *p;
  794. int n;
  795. if (!journal)
  796. return NULL;
  797. /* journal descriptor can store up to n blocks -bzzz */
  798. journal->j_blocksize = blocksize;
  799. journal->j_dev = bdev;
  800. journal->j_fs_dev = fs_dev;
  801. journal->j_blk_offset = start;
  802. journal->j_maxlen = len;
  803. bdevname(journal->j_dev, journal->j_devname);
  804. p = journal->j_devname;
  805. while ((p = strchr(p, '/')))
  806. *p = '!';
  807. jbd2_stats_proc_init(journal);
  808. n = journal->j_blocksize / sizeof(journal_block_tag_t);
  809. journal->j_wbufsize = n;
  810. journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
  811. if (!journal->j_wbuf) {
  812. printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
  813. __func__);
  814. goto out_err;
  815. }
  816. bh = __getblk(journal->j_dev, start, journal->j_blocksize);
  817. if (!bh) {
  818. printk(KERN_ERR
  819. "%s: Cannot get buffer for journal superblock\n",
  820. __func__);
  821. goto out_err;
  822. }
  823. journal->j_sb_buffer = bh;
  824. journal->j_superblock = (journal_superblock_t *)bh->b_data;
  825. return journal;
  826. out_err:
  827. kfree(journal->j_wbuf);
  828. jbd2_stats_proc_exit(journal);
  829. kfree(journal);
  830. return NULL;
  831. }
  832. /**
  833. * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
  834. * @inode: An inode to create the journal in
  835. *
  836. * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
  837. * the journal. The inode must exist already, must support bmap() and
  838. * must have all data blocks preallocated.
  839. */
  840. journal_t * jbd2_journal_init_inode (struct inode *inode)
  841. {
  842. struct buffer_head *bh;
  843. journal_t *journal = journal_init_common();
  844. char *p;
  845. int err;
  846. int n;
  847. unsigned long long blocknr;
  848. if (!journal)
  849. return NULL;
  850. journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
  851. journal->j_inode = inode;
  852. bdevname(journal->j_dev, journal->j_devname);
  853. p = journal->j_devname;
  854. while ((p = strchr(p, '/')))
  855. *p = '!';
  856. p = journal->j_devname + strlen(journal->j_devname);
  857. sprintf(p, "-%lu", journal->j_inode->i_ino);
  858. jbd_debug(1,
  859. "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
  860. journal, inode->i_sb->s_id, inode->i_ino,
  861. (long long) inode->i_size,
  862. inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
  863. journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
  864. journal->j_blocksize = inode->i_sb->s_blocksize;
  865. jbd2_stats_proc_init(journal);
  866. /* journal descriptor can store up to n blocks -bzzz */
  867. n = journal->j_blocksize / sizeof(journal_block_tag_t);
  868. journal->j_wbufsize = n;
  869. journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
  870. if (!journal->j_wbuf) {
  871. printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
  872. __func__);
  873. goto out_err;
  874. }
  875. err = jbd2_journal_bmap(journal, 0, &blocknr);
  876. /* If that failed, give up */
  877. if (err) {
  878. printk(KERN_ERR "%s: Cannot locate journal superblock\n",
  879. __func__);
  880. goto out_err;
  881. }
  882. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  883. if (!bh) {
  884. printk(KERN_ERR
  885. "%s: Cannot get buffer for journal superblock\n",
  886. __func__);
  887. goto out_err;
  888. }
  889. journal->j_sb_buffer = bh;
  890. journal->j_superblock = (journal_superblock_t *)bh->b_data;
  891. return journal;
  892. out_err:
  893. kfree(journal->j_wbuf);
  894. jbd2_stats_proc_exit(journal);
  895. kfree(journal);
  896. return NULL;
  897. }
  898. /*
  899. * If the journal init or create aborts, we need to mark the journal
  900. * superblock as being NULL to prevent the journal destroy from writing
  901. * back a bogus superblock.
  902. */
  903. static void journal_fail_superblock (journal_t *journal)
  904. {
  905. struct buffer_head *bh = journal->j_sb_buffer;
  906. brelse(bh);
  907. journal->j_sb_buffer = NULL;
  908. }
  909. /*
  910. * Given a journal_t structure, initialise the various fields for
  911. * startup of a new journaling session. We use this both when creating
  912. * a journal, and after recovering an old journal to reset it for
  913. * subsequent use.
  914. */
  915. static int journal_reset(journal_t *journal)
  916. {
  917. journal_superblock_t *sb = journal->j_superblock;
  918. unsigned long long first, last;
  919. first = be32_to_cpu(sb->s_first);
  920. last = be32_to_cpu(sb->s_maxlen);
  921. if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
  922. printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n",
  923. first, last);
  924. journal_fail_superblock(journal);
  925. return -EINVAL;
  926. }
  927. journal->j_first = first;
  928. journal->j_last = last;
  929. journal->j_head = first;
  930. journal->j_tail = first;
  931. journal->j_free = last - first;
  932. journal->j_tail_sequence = journal->j_transaction_sequence;
  933. journal->j_commit_sequence = journal->j_transaction_sequence - 1;
  934. journal->j_commit_request = journal->j_commit_sequence;
  935. journal->j_max_transaction_buffers = journal->j_maxlen / 4;
  936. /* Add the dynamic fields and write it to disk. */
  937. jbd2_journal_update_superblock(journal, 1);
  938. return jbd2_journal_start_thread(journal);
  939. }
  940. /**
  941. * void jbd2_journal_update_superblock() - Update journal sb on disk.
  942. * @journal: The journal to update.
  943. * @wait: Set to '0' if you don't want to wait for IO completion.
  944. *
  945. * Update a journal's dynamic superblock fields and write it to disk,
  946. * optionally waiting for the IO to complete.
  947. */
  948. void jbd2_journal_update_superblock(journal_t *journal, int wait)
  949. {
  950. journal_superblock_t *sb = journal->j_superblock;
  951. struct buffer_head *bh = journal->j_sb_buffer;
  952. /*
  953. * As a special case, if the on-disk copy is already marked as needing
  954. * no recovery (s_start == 0) and there are no outstanding transactions
  955. * in the filesystem, then we can safely defer the superblock update
  956. * until the next commit by setting JBD2_FLUSHED. This avoids
  957. * attempting a write to a potential-readonly device.
  958. */
  959. if (sb->s_start == 0 && journal->j_tail_sequence ==
  960. journal->j_transaction_sequence) {
  961. jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
  962. "(start %ld, seq %d, errno %d)\n",
  963. journal->j_tail, journal->j_tail_sequence,
  964. journal->j_errno);
  965. goto out;
  966. }
  967. if (buffer_write_io_error(bh)) {
  968. /*
  969. * Oh, dear. A previous attempt to write the journal
  970. * superblock failed. This could happen because the
  971. * USB device was yanked out. Or it could happen to
  972. * be a transient write error and maybe the block will
  973. * be remapped. Nothing we can do but to retry the
  974. * write and hope for the best.
  975. */
  976. printk(KERN_ERR "JBD2: previous I/O error detected "
  977. "for journal superblock update for %s.\n",
  978. journal->j_devname);
  979. clear_buffer_write_io_error(bh);
  980. set_buffer_uptodate(bh);
  981. }
  982. read_lock(&journal->j_state_lock);
  983. jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
  984. journal->j_tail, journal->j_tail_sequence, journal->j_errno);
  985. sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
  986. sb->s_start = cpu_to_be32(journal->j_tail);
  987. sb->s_errno = cpu_to_be32(journal->j_errno);
  988. read_unlock(&journal->j_state_lock);
  989. BUFFER_TRACE(bh, "marking dirty");
  990. mark_buffer_dirty(bh);
  991. if (wait) {
  992. sync_dirty_buffer(bh);
  993. if (buffer_write_io_error(bh)) {
  994. printk(KERN_ERR "JBD2: I/O error detected "
  995. "when updating journal superblock for %s.\n",
  996. journal->j_devname);
  997. clear_buffer_write_io_error(bh);
  998. set_buffer_uptodate(bh);
  999. }
  1000. } else
  1001. write_dirty_buffer(bh, WRITE);
  1002. out:
  1003. /* If we have just flushed the log (by marking s_start==0), then
  1004. * any future commit will have to be careful to update the
  1005. * superblock again to re-record the true start of the log. */
  1006. write_lock(&journal->j_state_lock);
  1007. if (sb->s_start)
  1008. journal->j_flags &= ~JBD2_FLUSHED;
  1009. else
  1010. journal->j_flags |= JBD2_FLUSHED;
  1011. write_unlock(&journal->j_state_lock);
  1012. }
  1013. /*
  1014. * Read the superblock for a given journal, performing initial
  1015. * validation of the format.
  1016. */
  1017. static int journal_get_superblock(journal_t *journal)
  1018. {
  1019. struct buffer_head *bh;
  1020. journal_superblock_t *sb;
  1021. int err = -EIO;
  1022. bh = journal->j_sb_buffer;
  1023. J_ASSERT(bh != NULL);
  1024. if (!buffer_uptodate(bh)) {
  1025. ll_rw_block(READ, 1, &bh);
  1026. wait_on_buffer(bh);
  1027. if (!buffer_uptodate(bh)) {
  1028. printk (KERN_ERR
  1029. "JBD: IO error reading journal superblock\n");
  1030. goto out;
  1031. }
  1032. }
  1033. sb = journal->j_superblock;
  1034. err = -EINVAL;
  1035. if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
  1036. sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
  1037. printk(KERN_WARNING "JBD: no valid journal superblock found\n");
  1038. goto out;
  1039. }
  1040. switch(be32_to_cpu(sb->s_header.h_blocktype)) {
  1041. case JBD2_SUPERBLOCK_V1:
  1042. journal->j_format_version = 1;
  1043. break;
  1044. case JBD2_SUPERBLOCK_V2:
  1045. journal->j_format_version = 2;
  1046. break;
  1047. default:
  1048. printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
  1049. goto out;
  1050. }
  1051. if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
  1052. journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
  1053. else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
  1054. printk (KERN_WARNING "JBD: journal file too short\n");
  1055. goto out;
  1056. }
  1057. return 0;
  1058. out:
  1059. journal_fail_superblock(journal);
  1060. return err;
  1061. }
  1062. /*
  1063. * Load the on-disk journal superblock and read the key fields into the
  1064. * journal_t.
  1065. */
  1066. static int load_superblock(journal_t *journal)
  1067. {
  1068. int err;
  1069. journal_superblock_t *sb;
  1070. err = journal_get_superblock(journal);
  1071. if (err)
  1072. return err;
  1073. sb = journal->j_superblock;
  1074. journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
  1075. journal->j_tail = be32_to_cpu(sb->s_start);
  1076. journal->j_first = be32_to_cpu(sb->s_first);
  1077. journal->j_last = be32_to_cpu(sb->s_maxlen);
  1078. journal->j_errno = be32_to_cpu(sb->s_errno);
  1079. return 0;
  1080. }
  1081. /**
  1082. * int jbd2_journal_load() - Read journal from disk.
  1083. * @journal: Journal to act on.
  1084. *
  1085. * Given a journal_t structure which tells us which disk blocks contain
  1086. * a journal, read the journal from disk to initialise the in-memory
  1087. * structures.
  1088. */
  1089. int jbd2_journal_load(journal_t *journal)
  1090. {
  1091. int err;
  1092. journal_superblock_t *sb;
  1093. err = load_superblock(journal);
  1094. if (err)
  1095. return err;
  1096. sb = journal->j_superblock;
  1097. /* If this is a V2 superblock, then we have to check the
  1098. * features flags on it. */
  1099. if (journal->j_format_version >= 2) {
  1100. if ((sb->s_feature_ro_compat &
  1101. ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
  1102. (sb->s_feature_incompat &
  1103. ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
  1104. printk (KERN_WARNING
  1105. "JBD: Unrecognised features on journal\n");
  1106. return -EINVAL;
  1107. }
  1108. }
  1109. /*
  1110. * Create a slab for this blocksize
  1111. */
  1112. err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
  1113. if (err)
  1114. return err;
  1115. /* Let the recovery code check whether it needs to recover any
  1116. * data from the journal. */
  1117. if (jbd2_journal_recover(journal))
  1118. goto recovery_error;
  1119. if (journal->j_failed_commit) {
  1120. printk(KERN_ERR "JBD2: journal transaction %u on %s "
  1121. "is corrupt.\n", journal->j_failed_commit,
  1122. journal->j_devname);
  1123. return -EIO;
  1124. }
  1125. /* OK, we've finished with the dynamic journal bits:
  1126. * reinitialise the dynamic contents of the superblock in memory
  1127. * and reset them on disk. */
  1128. if (journal_reset(journal))
  1129. goto recovery_error;
  1130. journal->j_flags &= ~JBD2_ABORT;
  1131. journal->j_flags |= JBD2_LOADED;
  1132. return 0;
  1133. recovery_error:
  1134. printk (KERN_WARNING "JBD: recovery failed\n");
  1135. return -EIO;
  1136. }
  1137. /**
  1138. * void jbd2_journal_destroy() - Release a journal_t structure.
  1139. * @journal: Journal to act on.
  1140. *
  1141. * Release a journal_t structure once it is no longer in use by the
  1142. * journaled object.
  1143. * Return <0 if we couldn't clean up the journal.
  1144. */
  1145. int jbd2_journal_destroy(journal_t *journal)
  1146. {
  1147. int err = 0;
  1148. /* Wait for the commit thread to wake up and die. */
  1149. journal_kill_thread(journal);
  1150. /* Force a final log commit */
  1151. if (journal->j_running_transaction)
  1152. jbd2_journal_commit_transaction(journal);
  1153. /* Force any old transactions to disk */
  1154. /* Totally anal locking here... */
  1155. spin_lock(&journal->j_list_lock);
  1156. while (journal->j_checkpoint_transactions != NULL) {
  1157. spin_unlock(&journal->j_list_lock);
  1158. mutex_lock(&journal->j_checkpoint_mutex);
  1159. jbd2_log_do_checkpoint(journal);
  1160. mutex_unlock(&journal->j_checkpoint_mutex);
  1161. spin_lock(&journal->j_list_lock);
  1162. }
  1163. J_ASSERT(journal->j_running_transaction == NULL);
  1164. J_ASSERT(journal->j_committing_transaction == NULL);
  1165. J_ASSERT(journal->j_checkpoint_transactions == NULL);
  1166. spin_unlock(&journal->j_list_lock);
  1167. if (journal->j_sb_buffer) {
  1168. if (!is_journal_aborted(journal)) {
  1169. /* We can now mark the journal as empty. */
  1170. journal->j_tail = 0;
  1171. journal->j_tail_sequence =
  1172. ++journal->j_transaction_sequence;
  1173. jbd2_journal_update_superblock(journal, 1);
  1174. } else {
  1175. err = -EIO;
  1176. }
  1177. brelse(journal->j_sb_buffer);
  1178. }
  1179. if (journal->j_proc_entry)
  1180. jbd2_stats_proc_exit(journal);
  1181. if (journal->j_inode)
  1182. iput(journal->j_inode);
  1183. if (journal->j_revoke)
  1184. jbd2_journal_destroy_revoke(journal);
  1185. kfree(journal->j_wbuf);
  1186. kfree(journal);
  1187. return err;
  1188. }
  1189. /**
  1190. *int jbd2_journal_check_used_features () - Check if features specified are used.
  1191. * @journal: Journal to check.
  1192. * @compat: bitmask of compatible features
  1193. * @ro: bitmask of features that force read-only mount
  1194. * @incompat: bitmask of incompatible features
  1195. *
  1196. * Check whether the journal uses all of a given set of
  1197. * features. Return true (non-zero) if it does.
  1198. **/
  1199. int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
  1200. unsigned long ro, unsigned long incompat)
  1201. {
  1202. journal_superblock_t *sb;
  1203. if (!compat && !ro && !incompat)
  1204. return 1;
  1205. /* Load journal superblock if it is not loaded yet. */
  1206. if (journal->j_format_version == 0 &&
  1207. journal_get_superblock(journal) != 0)
  1208. return 0;
  1209. if (journal->j_format_version == 1)
  1210. return 0;
  1211. sb = journal->j_superblock;
  1212. if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
  1213. ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
  1214. ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
  1215. return 1;
  1216. return 0;
  1217. }
  1218. /**
  1219. * int jbd2_journal_check_available_features() - Check feature set in journalling layer
  1220. * @journal: Journal to check.
  1221. * @compat: bitmask of compatible features
  1222. * @ro: bitmask of features that force read-only mount
  1223. * @incompat: bitmask of incompatible features
  1224. *
  1225. * Check whether the journaling code supports the use of
  1226. * all of a given set of features on this journal. Return true
  1227. * (non-zero) if it can. */
  1228. int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
  1229. unsigned long ro, unsigned long incompat)
  1230. {
  1231. if (!compat && !ro && !incompat)
  1232. return 1;
  1233. /* We can support any known requested features iff the
  1234. * superblock is in version 2. Otherwise we fail to support any
  1235. * extended sb features. */
  1236. if (journal->j_format_version != 2)
  1237. return 0;
  1238. if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
  1239. (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
  1240. (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
  1241. return 1;
  1242. return 0;
  1243. }
  1244. /**
  1245. * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
  1246. * @journal: Journal to act on.
  1247. * @compat: bitmask of compatible features
  1248. * @ro: bitmask of features that force read-only mount
  1249. * @incompat: bitmask of incompatible features
  1250. *
  1251. * Mark a given journal feature as present on the
  1252. * superblock. Returns true if the requested features could be set.
  1253. *
  1254. */
  1255. int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
  1256. unsigned long ro, unsigned long incompat)
  1257. {
  1258. journal_superblock_t *sb;
  1259. if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
  1260. return 1;
  1261. if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
  1262. return 0;
  1263. jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
  1264. compat, ro, incompat);
  1265. sb = journal->j_superblock;
  1266. sb->s_feature_compat |= cpu_to_be32(compat);
  1267. sb->s_feature_ro_compat |= cpu_to_be32(ro);
  1268. sb->s_feature_incompat |= cpu_to_be32(incompat);
  1269. return 1;
  1270. }
  1271. /*
  1272. * jbd2_journal_clear_features () - Clear a given journal feature in the
  1273. * superblock
  1274. * @journal: Journal to act on.
  1275. * @compat: bitmask of compatible features
  1276. * @ro: bitmask of features that force read-only mount
  1277. * @incompat: bitmask of incompatible features
  1278. *
  1279. * Clear a given journal feature as present on the
  1280. * superblock.
  1281. */
  1282. void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
  1283. unsigned long ro, unsigned long incompat)
  1284. {
  1285. journal_superblock_t *sb;
  1286. jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
  1287. compat, ro, incompat);
  1288. sb = journal->j_superblock;
  1289. sb->s_feature_compat &= ~cpu_to_be32(compat);
  1290. sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
  1291. sb->s_feature_incompat &= ~cpu_to_be32(incompat);
  1292. }
  1293. EXPORT_SYMBOL(jbd2_journal_clear_features);
  1294. /**
  1295. * int jbd2_journal_update_format () - Update on-disk journal structure.
  1296. * @journal: Journal to act on.
  1297. *
  1298. * Given an initialised but unloaded journal struct, poke about in the
  1299. * on-disk structure to update it to the most recent supported version.
  1300. */
  1301. int jbd2_journal_update_format (journal_t *journal)
  1302. {
  1303. journal_superblock_t *sb;
  1304. int err;
  1305. err = journal_get_superblock(journal);
  1306. if (err)
  1307. return err;
  1308. sb = journal->j_superblock;
  1309. switch (be32_to_cpu(sb->s_header.h_blocktype)) {
  1310. case JBD2_SUPERBLOCK_V2:
  1311. return 0;
  1312. case JBD2_SUPERBLOCK_V1:
  1313. return journal_convert_superblock_v1(journal, sb);
  1314. default:
  1315. break;
  1316. }
  1317. return -EINVAL;
  1318. }
  1319. static int journal_convert_superblock_v1(journal_t *journal,
  1320. journal_superblock_t *sb)
  1321. {
  1322. int offset, blocksize;
  1323. struct buffer_head *bh;
  1324. printk(KERN_WARNING
  1325. "JBD: Converting superblock from version 1 to 2.\n");
  1326. /* Pre-initialise new fields to zero */
  1327. offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
  1328. blocksize = be32_to_cpu(sb->s_blocksize);
  1329. memset(&sb->s_feature_compat, 0, blocksize-offset);
  1330. sb->s_nr_users = cpu_to_be32(1);
  1331. sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
  1332. journal->j_format_version = 2;
  1333. bh = journal->j_sb_buffer;
  1334. BUFFER_TRACE(bh, "marking dirty");
  1335. mark_buffer_dirty(bh);
  1336. sync_dirty_buffer(bh);
  1337. return 0;
  1338. }
  1339. /**
  1340. * int jbd2_journal_flush () - Flush journal
  1341. * @journal: Journal to act on.
  1342. *
  1343. * Flush all data for a given journal to disk and empty the journal.
  1344. * Filesystems can use this when remounting readonly to ensure that
  1345. * recovery does not need to happen on remount.
  1346. */
  1347. int jbd2_journal_flush(journal_t *journal)
  1348. {
  1349. int err = 0;
  1350. transaction_t *transaction = NULL;
  1351. unsigned long old_tail;
  1352. write_lock(&journal->j_state_lock);
  1353. /* Force everything buffered to the log... */
  1354. if (journal->j_running_transaction) {
  1355. transaction = journal->j_running_transaction;
  1356. __jbd2_log_start_commit(journal, transaction->t_tid);
  1357. } else if (journal->j_committing_transaction)
  1358. transaction = journal->j_committing_transaction;
  1359. /* Wait for the log commit to complete... */
  1360. if (transaction) {
  1361. tid_t tid = transaction->t_tid;
  1362. write_unlock(&journal->j_state_lock);
  1363. jbd2_log_wait_commit(journal, tid);
  1364. } else {
  1365. write_unlock(&journal->j_state_lock);
  1366. }
  1367. /* ...and flush everything in the log out to disk. */
  1368. spin_lock(&journal->j_list_lock);
  1369. while (!err && journal->j_checkpoint_transactions != NULL) {
  1370. spin_unlock(&journal->j_list_lock);
  1371. mutex_lock(&journal->j_checkpoint_mutex);
  1372. err = jbd2_log_do_checkpoint(journal);
  1373. mutex_unlock(&journal->j_checkpoint_mutex);
  1374. spin_lock(&journal->j_list_lock);
  1375. }
  1376. spin_unlock(&journal->j_list_lock);
  1377. if (is_journal_aborted(journal))
  1378. return -EIO;
  1379. jbd2_cleanup_journal_tail(journal);
  1380. /* Finally, mark the journal as really needing no recovery.
  1381. * This sets s_start==0 in the underlying superblock, which is
  1382. * the magic code for a fully-recovered superblock. Any future
  1383. * commits of data to the journal will restore the current
  1384. * s_start value. */
  1385. write_lock(&journal->j_state_lock);
  1386. old_tail = journal->j_tail;
  1387. journal->j_tail = 0;
  1388. write_unlock(&journal->j_state_lock);
  1389. jbd2_journal_update_superblock(journal, 1);
  1390. write_lock(&journal->j_state_lock);
  1391. journal->j_tail = old_tail;
  1392. J_ASSERT(!journal->j_running_transaction);
  1393. J_ASSERT(!journal->j_committing_transaction);
  1394. J_ASSERT(!journal->j_checkpoint_transactions);
  1395. J_ASSERT(journal->j_head == journal->j_tail);
  1396. J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
  1397. write_unlock(&journal->j_state_lock);
  1398. return 0;
  1399. }
  1400. /**
  1401. * int jbd2_journal_wipe() - Wipe journal contents
  1402. * @journal: Journal to act on.
  1403. * @write: flag (see below)
  1404. *
  1405. * Wipe out all of the contents of a journal, safely. This will produce
  1406. * a warning if the journal contains any valid recovery information.
  1407. * Must be called between journal_init_*() and jbd2_journal_load().
  1408. *
  1409. * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
  1410. * we merely suppress recovery.
  1411. */
  1412. int jbd2_journal_wipe(journal_t *journal, int write)
  1413. {
  1414. int err = 0;
  1415. J_ASSERT (!(journal->j_flags & JBD2_LOADED));
  1416. err = load_superblock(journal);
  1417. if (err)
  1418. return err;
  1419. if (!journal->j_tail)
  1420. goto no_recovery;
  1421. printk (KERN_WARNING "JBD: %s recovery information on journal\n",
  1422. write ? "Clearing" : "Ignoring");
  1423. err = jbd2_journal_skip_recovery(journal);
  1424. if (write)
  1425. jbd2_journal_update_superblock(journal, 1);
  1426. no_recovery:
  1427. return err;
  1428. }
  1429. /*
  1430. * Journal abort has very specific semantics, which we describe
  1431. * for journal abort.
  1432. *
  1433. * Two internal functions, which provide abort to the jbd layer
  1434. * itself are here.
  1435. */
  1436. /*
  1437. * Quick version for internal journal use (doesn't lock the journal).
  1438. * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
  1439. * and don't attempt to make any other journal updates.
  1440. */
  1441. void __jbd2_journal_abort_hard(journal_t *journal)
  1442. {
  1443. transaction_t *transaction;
  1444. if (journal->j_flags & JBD2_ABORT)
  1445. return;
  1446. printk(KERN_ERR "Aborting journal on device %s.\n",
  1447. journal->j_devname);
  1448. write_lock(&journal->j_state_lock);
  1449. journal->j_flags |= JBD2_ABORT;
  1450. transaction = journal->j_running_transaction;
  1451. if (transaction)
  1452. __jbd2_log_start_commit(journal, transaction->t_tid);
  1453. write_unlock(&journal->j_state_lock);
  1454. }
  1455. /* Soft abort: record the abort error status in the journal superblock,
  1456. * but don't do any other IO. */
  1457. static void __journal_abort_soft (journal_t *journal, int errno)
  1458. {
  1459. if (journal->j_flags & JBD2_ABORT)
  1460. return;
  1461. if (!journal->j_errno)
  1462. journal->j_errno = errno;
  1463. __jbd2_journal_abort_hard(journal);
  1464. if (errno)
  1465. jbd2_journal_update_superblock(journal, 1);
  1466. }
  1467. /**
  1468. * void jbd2_journal_abort () - Shutdown the journal immediately.
  1469. * @journal: the journal to shutdown.
  1470. * @errno: an error number to record in the journal indicating
  1471. * the reason for the shutdown.
  1472. *
  1473. * Perform a complete, immediate shutdown of the ENTIRE
  1474. * journal (not of a single transaction). This operation cannot be
  1475. * undone without closing and reopening the journal.
  1476. *
  1477. * The jbd2_journal_abort function is intended to support higher level error
  1478. * recovery mechanisms such as the ext2/ext3 remount-readonly error
  1479. * mode.
  1480. *
  1481. * Journal abort has very specific semantics. Any existing dirty,
  1482. * unjournaled buffers in the main filesystem will still be written to
  1483. * disk by bdflush, but the journaling mechanism will be suspended
  1484. * immediately and no further transaction commits will be honoured.
  1485. *
  1486. * Any dirty, journaled buffers will be written back to disk without
  1487. * hitting the journal. Atomicity cannot be guaranteed on an aborted
  1488. * filesystem, but we _do_ attempt to leave as much data as possible
  1489. * behind for fsck to use for cleanup.
  1490. *
  1491. * Any attempt to get a new transaction handle on a journal which is in
  1492. * ABORT state will just result in an -EROFS error return. A
  1493. * jbd2_journal_stop on an existing handle will return -EIO if we have
  1494. * entered abort state during the update.
  1495. *
  1496. * Recursive transactions are not disturbed by journal abort until the
  1497. * final jbd2_journal_stop, which will receive the -EIO error.
  1498. *
  1499. * Finally, the jbd2_journal_abort call allows the caller to supply an errno
  1500. * which will be recorded (if possible) in the journal superblock. This
  1501. * allows a client to record failure conditions in the middle of a
  1502. * transaction without having to complete the transaction to record the
  1503. * failure to disk. ext3_error, for example, now uses this
  1504. * functionality.
  1505. *
  1506. * Errors which originate from within the journaling layer will NOT
  1507. * supply an errno; a null errno implies that absolutely no further
  1508. * writes are done to the journal (unless there are any already in
  1509. * progress).
  1510. *
  1511. */
  1512. void jbd2_journal_abort(journal_t *journal, int errno)
  1513. {
  1514. __journal_abort_soft(journal, errno);
  1515. }
  1516. /**
  1517. * int jbd2_journal_errno () - returns the journal's error state.
  1518. * @journal: journal to examine.
  1519. *
  1520. * This is the errno number set with jbd2_journal_abort(), the last
  1521. * time the journal was mounted - if the journal was stopped
  1522. * without calling abort this will be 0.
  1523. *
  1524. * If the journal has been aborted on this mount time -EROFS will
  1525. * be returned.
  1526. */
  1527. int jbd2_journal_errno(journal_t *journal)
  1528. {
  1529. int err;
  1530. read_lock(&journal->j_state_lock);
  1531. if (journal->j_flags & JBD2_ABORT)
  1532. err = -EROFS;
  1533. else
  1534. err = journal->j_errno;
  1535. read_unlock(&journal->j_state_lock);
  1536. return err;
  1537. }
  1538. /**
  1539. * int jbd2_journal_clear_err () - clears the journal's error state
  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. int jbd2_journal_clear_err(journal_t *journal)
  1546. {
  1547. int err = 0;
  1548. write_lock(&journal->j_state_lock);
  1549. if (journal->j_flags & JBD2_ABORT)
  1550. err = -EROFS;
  1551. else
  1552. journal->j_errno = 0;
  1553. write_unlock(&journal->j_state_lock);
  1554. return err;
  1555. }
  1556. /**
  1557. * void jbd2_journal_ack_err() - Ack journal err.
  1558. * @journal: journal to act on.
  1559. *
  1560. * An error must be cleared or acked to take a FS out of readonly
  1561. * mode.
  1562. */
  1563. void jbd2_journal_ack_err(journal_t *journal)
  1564. {
  1565. write_lock(&journal->j_state_lock);
  1566. if (journal->j_errno)
  1567. journal->j_flags |= JBD2_ACK_ERR;
  1568. write_unlock(&journal->j_state_lock);
  1569. }
  1570. int jbd2_journal_blocks_per_page(struct inode *inode)
  1571. {
  1572. return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  1573. }
  1574. /*
  1575. * helper functions to deal with 32 or 64bit block numbers.
  1576. */
  1577. size_t journal_tag_bytes(journal_t *journal)
  1578. {
  1579. if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
  1580. return JBD2_TAG_SIZE64;
  1581. else
  1582. return JBD2_TAG_SIZE32;
  1583. }
  1584. /*
  1585. * JBD memory management
  1586. *
  1587. * These functions are used to allocate block-sized chunks of memory
  1588. * used for making copies of buffer_head data. Very often it will be
  1589. * page-sized chunks of data, but sometimes it will be in
  1590. * sub-page-size chunks. (For example, 16k pages on Power systems
  1591. * with a 4k block file system.) For blocks smaller than a page, we
  1592. * use a SLAB allocator. There are slab caches for each block size,
  1593. * which are allocated at mount time, if necessary, and we only free
  1594. * (all of) the slab caches when/if the jbd2 module is unloaded. For
  1595. * this reason we don't need to a mutex to protect access to
  1596. * jbd2_slab[] allocating or releasing memory; only in
  1597. * jbd2_journal_create_slab().
  1598. */
  1599. #define JBD2_MAX_SLABS 8
  1600. static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
  1601. static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
  1602. "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
  1603. "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
  1604. };
  1605. static void jbd2_journal_destroy_slabs(void)
  1606. {
  1607. int i;
  1608. for (i = 0; i < JBD2_MAX_SLABS; i++) {
  1609. if (jbd2_slab[i])
  1610. kmem_cache_destroy(jbd2_slab[i]);
  1611. jbd2_slab[i] = NULL;
  1612. }
  1613. }
  1614. static int jbd2_journal_create_slab(size_t size)
  1615. {
  1616. static DEFINE_MUTEX(jbd2_slab_create_mutex);
  1617. int i = order_base_2(size) - 10;
  1618. size_t slab_size;
  1619. if (size == PAGE_SIZE)
  1620. return 0;
  1621. if (i >= JBD2_MAX_SLABS)
  1622. return -EINVAL;
  1623. if (unlikely(i < 0))
  1624. i = 0;
  1625. mutex_lock(&jbd2_slab_create_mutex);
  1626. if (jbd2_slab[i]) {
  1627. mutex_unlock(&jbd2_slab_create_mutex);
  1628. return 0; /* Already created */
  1629. }
  1630. slab_size = 1 << (i+10);
  1631. jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
  1632. slab_size, 0, NULL);
  1633. mutex_unlock(&jbd2_slab_create_mutex);
  1634. if (!jbd2_slab[i]) {
  1635. printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
  1636. return -ENOMEM;
  1637. }
  1638. return 0;
  1639. }
  1640. static struct kmem_cache *get_slab(size_t size)
  1641. {
  1642. int i = order_base_2(size) - 10;
  1643. BUG_ON(i >= JBD2_MAX_SLABS);
  1644. if (unlikely(i < 0))
  1645. i = 0;
  1646. BUG_ON(jbd2_slab[i] == NULL);
  1647. return jbd2_slab[i];
  1648. }
  1649. void *jbd2_alloc(size_t size, gfp_t flags)
  1650. {
  1651. void *ptr;
  1652. BUG_ON(size & (size-1)); /* Must be a power of 2 */
  1653. flags |= __GFP_REPEAT;
  1654. if (size == PAGE_SIZE)
  1655. ptr = (void *)__get_free_pages(flags, 0);
  1656. else if (size > PAGE_SIZE) {
  1657. int order = get_order(size);
  1658. if (order < 3)
  1659. ptr = (void *)__get_free_pages(flags, order);
  1660. else
  1661. ptr = vmalloc(size);
  1662. } else
  1663. ptr = kmem_cache_alloc(get_slab(size), flags);
  1664. /* Check alignment; SLUB has gotten this wrong in the past,
  1665. * and this can lead to user data corruption! */
  1666. BUG_ON(((unsigned long) ptr) & (size-1));
  1667. return ptr;
  1668. }
  1669. void jbd2_free(void *ptr, size_t size)
  1670. {
  1671. if (size == PAGE_SIZE) {
  1672. free_pages((unsigned long)ptr, 0);
  1673. return;
  1674. }
  1675. if (size > PAGE_SIZE) {
  1676. int order = get_order(size);
  1677. if (order < 3)
  1678. free_pages((unsigned long)ptr, order);
  1679. else
  1680. vfree(ptr);
  1681. return;
  1682. }
  1683. kmem_cache_free(get_slab(size), ptr);
  1684. };
  1685. /*
  1686. * Journal_head storage management
  1687. */
  1688. static struct kmem_cache *jbd2_journal_head_cache;
  1689. #ifdef CONFIG_JBD2_DEBUG
  1690. static atomic_t nr_journal_heads = ATOMIC_INIT(0);
  1691. #endif
  1692. static int journal_init_jbd2_journal_head_cache(void)
  1693. {
  1694. int retval;
  1695. J_ASSERT(jbd2_journal_head_cache == NULL);
  1696. jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
  1697. sizeof(struct journal_head),
  1698. 0, /* offset */
  1699. SLAB_TEMPORARY, /* flags */
  1700. NULL); /* ctor */
  1701. retval = 0;
  1702. if (!jbd2_journal_head_cache) {
  1703. retval = -ENOMEM;
  1704. printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
  1705. }
  1706. return retval;
  1707. }
  1708. static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
  1709. {
  1710. if (jbd2_journal_head_cache) {
  1711. kmem_cache_destroy(jbd2_journal_head_cache);
  1712. jbd2_journal_head_cache = NULL;
  1713. }
  1714. }
  1715. /*
  1716. * journal_head splicing and dicing
  1717. */
  1718. static struct journal_head *journal_alloc_journal_head(void)
  1719. {
  1720. struct journal_head *ret;
  1721. #ifdef CONFIG_JBD2_DEBUG
  1722. atomic_inc(&nr_journal_heads);
  1723. #endif
  1724. ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
  1725. if (!ret) {
  1726. jbd_debug(1, "out of memory for journal_head\n");
  1727. pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
  1728. while (!ret) {
  1729. yield();
  1730. ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
  1731. }
  1732. }
  1733. return ret;
  1734. }
  1735. static void journal_free_journal_head(struct journal_head *jh)
  1736. {
  1737. #ifdef CONFIG_JBD2_DEBUG
  1738. atomic_dec(&nr_journal_heads);
  1739. memset(jh, JBD2_POISON_FREE, sizeof(*jh));
  1740. #endif
  1741. kmem_cache_free(jbd2_journal_head_cache, jh);
  1742. }
  1743. /*
  1744. * A journal_head is attached to a buffer_head whenever JBD has an
  1745. * interest in the buffer.
  1746. *
  1747. * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
  1748. * is set. This bit is tested in core kernel code where we need to take
  1749. * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
  1750. * there.
  1751. *
  1752. * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
  1753. *
  1754. * When a buffer has its BH_JBD bit set it is immune from being released by
  1755. * core kernel code, mainly via ->b_count.
  1756. *
  1757. * A journal_head may be detached from its buffer_head when the journal_head's
  1758. * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
  1759. * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
  1760. * journal_head can be dropped if needed.
  1761. *
  1762. * Various places in the kernel want to attach a journal_head to a buffer_head
  1763. * _before_ attaching the journal_head to a transaction. To protect the
  1764. * journal_head in this situation, jbd2_journal_add_journal_head elevates the
  1765. * journal_head's b_jcount refcount by one. The caller must call
  1766. * jbd2_journal_put_journal_head() to undo this.
  1767. *
  1768. * So the typical usage would be:
  1769. *
  1770. * (Attach a journal_head if needed. Increments b_jcount)
  1771. * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  1772. * ...
  1773. * jh->b_transaction = xxx;
  1774. * jbd2_journal_put_journal_head(jh);
  1775. *
  1776. * Now, the journal_head's b_jcount is zero, but it is safe from being released
  1777. * because it has a non-zero b_transaction.
  1778. */
  1779. /*
  1780. * Give a buffer_head a journal_head.
  1781. *
  1782. * Doesn't need the journal lock.
  1783. * May sleep.
  1784. */
  1785. struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
  1786. {
  1787. struct journal_head *jh;
  1788. struct journal_head *new_jh = NULL;
  1789. repeat:
  1790. if (!buffer_jbd(bh)) {
  1791. new_jh = journal_alloc_journal_head();
  1792. memset(new_jh, 0, sizeof(*new_jh));
  1793. }
  1794. jbd_lock_bh_journal_head(bh);
  1795. if (buffer_jbd(bh)) {
  1796. jh = bh2jh(bh);
  1797. } else {
  1798. J_ASSERT_BH(bh,
  1799. (atomic_read(&bh->b_count) > 0) ||
  1800. (bh->b_page && bh->b_page->mapping));
  1801. if (!new_jh) {
  1802. jbd_unlock_bh_journal_head(bh);
  1803. goto repeat;
  1804. }
  1805. jh = new_jh;
  1806. new_jh = NULL; /* We consumed it */
  1807. set_buffer_jbd(bh);
  1808. bh->b_private = jh;
  1809. jh->b_bh = bh;
  1810. get_bh(bh);
  1811. BUFFER_TRACE(bh, "added journal_head");
  1812. }
  1813. jh->b_jcount++;
  1814. jbd_unlock_bh_journal_head(bh);
  1815. if (new_jh)
  1816. journal_free_journal_head(new_jh);
  1817. return bh->b_private;
  1818. }
  1819. /*
  1820. * Grab a ref against this buffer_head's journal_head. If it ended up not
  1821. * having a journal_head, return NULL
  1822. */
  1823. struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
  1824. {
  1825. struct journal_head *jh = NULL;
  1826. jbd_lock_bh_journal_head(bh);
  1827. if (buffer_jbd(bh)) {
  1828. jh = bh2jh(bh);
  1829. jh->b_jcount++;
  1830. }
  1831. jbd_unlock_bh_journal_head(bh);
  1832. return jh;
  1833. }
  1834. static void __journal_remove_journal_head(struct buffer_head *bh)
  1835. {
  1836. struct journal_head *jh = bh2jh(bh);
  1837. J_ASSERT_JH(jh, jh->b_jcount >= 0);
  1838. get_bh(bh);
  1839. if (jh->b_jcount == 0) {
  1840. if (jh->b_transaction == NULL &&
  1841. jh->b_next_transaction == NULL &&
  1842. jh->b_cp_transaction == NULL) {
  1843. J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
  1844. J_ASSERT_BH(bh, buffer_jbd(bh));
  1845. J_ASSERT_BH(bh, jh2bh(jh) == bh);
  1846. BUFFER_TRACE(bh, "remove journal_head");
  1847. if (jh->b_frozen_data) {
  1848. printk(KERN_WARNING "%s: freeing "
  1849. "b_frozen_data\n",
  1850. __func__);
  1851. jbd2_free(jh->b_frozen_data, bh->b_size);
  1852. }
  1853. if (jh->b_committed_data) {
  1854. printk(KERN_WARNING "%s: freeing "
  1855. "b_committed_data\n",
  1856. __func__);
  1857. jbd2_free(jh->b_committed_data, bh->b_size);
  1858. }
  1859. bh->b_private = NULL;
  1860. jh->b_bh = NULL; /* debug, really */
  1861. clear_buffer_jbd(bh);
  1862. __brelse(bh);
  1863. journal_free_journal_head(jh);
  1864. } else {
  1865. BUFFER_TRACE(bh, "journal_head was locked");
  1866. }
  1867. }
  1868. }
  1869. /*
  1870. * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
  1871. * and has a zero b_jcount then remove and release its journal_head. If we did
  1872. * see that the buffer is not used by any transaction we also "logically"
  1873. * decrement ->b_count.
  1874. *
  1875. * We in fact take an additional increment on ->b_count as a convenience,
  1876. * because the caller usually wants to do additional things with the bh
  1877. * after calling here.
  1878. * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
  1879. * time. Once the caller has run __brelse(), the buffer is eligible for
  1880. * reaping by try_to_free_buffers().
  1881. */
  1882. void jbd2_journal_remove_journal_head(struct buffer_head *bh)
  1883. {
  1884. jbd_lock_bh_journal_head(bh);
  1885. __journal_remove_journal_head(bh);
  1886. jbd_unlock_bh_journal_head(bh);
  1887. }
  1888. /*
  1889. * Drop a reference on the passed journal_head. If it fell to zero then try to
  1890. * release the journal_head from the buffer_head.
  1891. */
  1892. void jbd2_journal_put_journal_head(struct journal_head *jh)
  1893. {
  1894. struct buffer_head *bh = jh2bh(jh);
  1895. jbd_lock_bh_journal_head(bh);
  1896. J_ASSERT_JH(jh, jh->b_jcount > 0);
  1897. --jh->b_jcount;
  1898. if (!jh->b_jcount && !jh->b_transaction) {
  1899. __journal_remove_journal_head(bh);
  1900. __brelse(bh);
  1901. }
  1902. jbd_unlock_bh_journal_head(bh);
  1903. }
  1904. /*
  1905. * Initialize jbd inode head
  1906. */
  1907. void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
  1908. {
  1909. jinode->i_transaction = NULL;
  1910. jinode->i_next_transaction = NULL;
  1911. jinode->i_vfs_inode = inode;
  1912. jinode->i_flags = 0;
  1913. INIT_LIST_HEAD(&jinode->i_list);
  1914. }
  1915. /*
  1916. * Function to be called before we start removing inode from memory (i.e.,
  1917. * clear_inode() is a fine place to be called from). It removes inode from
  1918. * transaction's lists.
  1919. */
  1920. void jbd2_journal_release_jbd_inode(journal_t *journal,
  1921. struct jbd2_inode *jinode)
  1922. {
  1923. if (!journal)
  1924. return;
  1925. restart:
  1926. spin_lock(&journal->j_list_lock);
  1927. /* Is commit writing out inode - we have to wait */
  1928. if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
  1929. wait_queue_head_t *wq;
  1930. DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
  1931. wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
  1932. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  1933. spin_unlock(&journal->j_list_lock);
  1934. schedule();
  1935. finish_wait(wq, &wait.wait);
  1936. goto restart;
  1937. }
  1938. if (jinode->i_transaction) {
  1939. list_del(&jinode->i_list);
  1940. jinode->i_transaction = NULL;
  1941. }
  1942. spin_unlock(&journal->j_list_lock);
  1943. }
  1944. /*
  1945. * debugfs tunables
  1946. */
  1947. #ifdef CONFIG_JBD2_DEBUG
  1948. u8 jbd2_journal_enable_debug __read_mostly;
  1949. EXPORT_SYMBOL(jbd2_journal_enable_debug);
  1950. #define JBD2_DEBUG_NAME "jbd2-debug"
  1951. static struct dentry *jbd2_debugfs_dir;
  1952. static struct dentry *jbd2_debug;
  1953. static void __init jbd2_create_debugfs_entry(void)
  1954. {
  1955. jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
  1956. if (jbd2_debugfs_dir)
  1957. jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
  1958. S_IRUGO | S_IWUSR,
  1959. jbd2_debugfs_dir,
  1960. &jbd2_journal_enable_debug);
  1961. }
  1962. static void __exit jbd2_remove_debugfs_entry(void)
  1963. {
  1964. debugfs_remove(jbd2_debug);
  1965. debugfs_remove(jbd2_debugfs_dir);
  1966. }
  1967. #else
  1968. static void __init jbd2_create_debugfs_entry(void)
  1969. {
  1970. }
  1971. static void __exit jbd2_remove_debugfs_entry(void)
  1972. {
  1973. }
  1974. #endif
  1975. #ifdef CONFIG_PROC_FS
  1976. #define JBD2_STATS_PROC_NAME "fs/jbd2"
  1977. static void __init jbd2_create_jbd_stats_proc_entry(void)
  1978. {
  1979. proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
  1980. }
  1981. static void __exit jbd2_remove_jbd_stats_proc_entry(void)
  1982. {
  1983. if (proc_jbd2_stats)
  1984. remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
  1985. }
  1986. #else
  1987. #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
  1988. #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
  1989. #endif
  1990. struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
  1991. static int __init journal_init_handle_cache(void)
  1992. {
  1993. jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
  1994. if (jbd2_handle_cache == NULL) {
  1995. printk(KERN_EMERG "JBD2: failed to create handle cache\n");
  1996. return -ENOMEM;
  1997. }
  1998. jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
  1999. if (jbd2_inode_cache == NULL) {
  2000. printk(KERN_EMERG "JBD2: failed to create inode cache\n");
  2001. kmem_cache_destroy(jbd2_handle_cache);
  2002. return -ENOMEM;
  2003. }
  2004. return 0;
  2005. }
  2006. static void jbd2_journal_destroy_handle_cache(void)
  2007. {
  2008. if (jbd2_handle_cache)
  2009. kmem_cache_destroy(jbd2_handle_cache);
  2010. if (jbd2_inode_cache)
  2011. kmem_cache_destroy(jbd2_inode_cache);
  2012. }
  2013. /*
  2014. * Module startup and shutdown
  2015. */
  2016. static int __init journal_init_caches(void)
  2017. {
  2018. int ret;
  2019. ret = jbd2_journal_init_revoke_caches();
  2020. if (ret == 0)
  2021. ret = journal_init_jbd2_journal_head_cache();
  2022. if (ret == 0)
  2023. ret = journal_init_handle_cache();
  2024. return ret;
  2025. }
  2026. static void jbd2_journal_destroy_caches(void)
  2027. {
  2028. jbd2_journal_destroy_revoke_caches();
  2029. jbd2_journal_destroy_jbd2_journal_head_cache();
  2030. jbd2_journal_destroy_handle_cache();
  2031. jbd2_journal_destroy_slabs();
  2032. }
  2033. static int __init journal_init(void)
  2034. {
  2035. int ret;
  2036. BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
  2037. ret = journal_init_caches();
  2038. if (ret == 0) {
  2039. jbd2_create_debugfs_entry();
  2040. jbd2_create_jbd_stats_proc_entry();
  2041. } else {
  2042. jbd2_journal_destroy_caches();
  2043. }
  2044. return ret;
  2045. }
  2046. static void __exit journal_exit(void)
  2047. {
  2048. #ifdef CONFIG_JBD2_DEBUG
  2049. int n = atomic_read(&nr_journal_heads);
  2050. if (n)
  2051. printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
  2052. #endif
  2053. jbd2_remove_debugfs_entry();
  2054. jbd2_remove_jbd_stats_proc_entry();
  2055. jbd2_journal_destroy_caches();
  2056. }
  2057. /*
  2058. * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
  2059. * tracing infrastructure to map a dev_t to a device name.
  2060. *
  2061. * The caller should use rcu_read_lock() in order to make sure the
  2062. * device name stays valid until its done with it. We use
  2063. * rcu_read_lock() as well to make sure we're safe in case the caller
  2064. * gets sloppy, and because rcu_read_lock() is cheap and can be safely
  2065. * nested.
  2066. */
  2067. struct devname_cache {
  2068. struct rcu_head rcu;
  2069. dev_t device;
  2070. char devname[BDEVNAME_SIZE];
  2071. };
  2072. #define CACHE_SIZE_BITS 6
  2073. static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
  2074. static DEFINE_SPINLOCK(devname_cache_lock);
  2075. static void free_devcache(struct rcu_head *rcu)
  2076. {
  2077. kfree(rcu);
  2078. }
  2079. const char *jbd2_dev_to_name(dev_t device)
  2080. {
  2081. int i = hash_32(device, CACHE_SIZE_BITS);
  2082. char *ret;
  2083. struct block_device *bd;
  2084. static struct devname_cache *new_dev;
  2085. rcu_read_lock();
  2086. if (devcache[i] && devcache[i]->device == device) {
  2087. ret = devcache[i]->devname;
  2088. rcu_read_unlock();
  2089. return ret;
  2090. }
  2091. rcu_read_unlock();
  2092. new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
  2093. if (!new_dev)
  2094. return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
  2095. spin_lock(&devname_cache_lock);
  2096. if (devcache[i]) {
  2097. if (devcache[i]->device == device) {
  2098. kfree(new_dev);
  2099. ret = devcache[i]->devname;
  2100. spin_unlock(&devname_cache_lock);
  2101. return ret;
  2102. }
  2103. call_rcu(&devcache[i]->rcu, free_devcache);
  2104. }
  2105. devcache[i] = new_dev;
  2106. devcache[i]->device = device;
  2107. bd = bdget(device);
  2108. if (bd) {
  2109. bdevname(bd, devcache[i]->devname);
  2110. bdput(bd);
  2111. } else
  2112. __bdevname(device, devcache[i]->devname);
  2113. ret = devcache[i]->devname;
  2114. spin_unlock(&devname_cache_lock);
  2115. return ret;
  2116. }
  2117. EXPORT_SYMBOL(jbd2_dev_to_name);
  2118. MODULE_LICENSE("GPL");
  2119. module_init(journal_init);
  2120. module_exit(journal_exit);