journal.c 55 KB

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