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