journal.c 53 KB

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