journal.c 54 KB

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