journal.c 66 KB

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