journal.c 54 KB

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