journal.c 54 KB

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