journal.c 55 KB

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