journal.c 58 KB

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