journal.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082
  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. * The only transaction we can possibly wait upon is the
  389. * currently running transaction (if it exists). Otherwise,
  390. * the target tid must be an old one.
  391. */
  392. if (journal->j_running_transaction &&
  393. journal->j_running_transaction->t_tid == target) {
  394. /*
  395. * We want a new commit: OK, mark the request and wakeup the
  396. * commit thread. We do _not_ do the commit ourselves.
  397. */
  398. journal->j_commit_request = target;
  399. jbd_debug(1, "JBD: requesting commit %d/%d\n",
  400. journal->j_commit_request,
  401. journal->j_commit_sequence);
  402. wake_up(&journal->j_wait_commit);
  403. return 1;
  404. } else if (!tid_geq(journal->j_commit_request, target))
  405. /* This should never happen, but if it does, preserve
  406. the evidence before kjournald goes into a loop and
  407. increments j_commit_sequence beyond all recognition. */
  408. WARN_ONCE(1, "jbd: bad log_start_commit: %u %u %u %u\n",
  409. journal->j_commit_request, journal->j_commit_sequence,
  410. target, journal->j_running_transaction ?
  411. journal->j_running_transaction->t_tid : 0);
  412. return 0;
  413. }
  414. int log_start_commit(journal_t *journal, tid_t tid)
  415. {
  416. int ret;
  417. spin_lock(&journal->j_state_lock);
  418. ret = __log_start_commit(journal, tid);
  419. spin_unlock(&journal->j_state_lock);
  420. return ret;
  421. }
  422. /*
  423. * Force and wait upon a commit if the calling process is not within
  424. * transaction. This is used for forcing out undo-protected data which contains
  425. * bitmaps, when the fs is running out of space.
  426. *
  427. * We can only force the running transaction if we don't have an active handle;
  428. * otherwise, we will deadlock.
  429. *
  430. * Returns true if a transaction was started.
  431. */
  432. int journal_force_commit_nested(journal_t *journal)
  433. {
  434. transaction_t *transaction = NULL;
  435. tid_t tid;
  436. spin_lock(&journal->j_state_lock);
  437. if (journal->j_running_transaction && !current->journal_info) {
  438. transaction = journal->j_running_transaction;
  439. __log_start_commit(journal, transaction->t_tid);
  440. } else if (journal->j_committing_transaction)
  441. transaction = journal->j_committing_transaction;
  442. if (!transaction) {
  443. spin_unlock(&journal->j_state_lock);
  444. return 0; /* Nothing to retry */
  445. }
  446. tid = transaction->t_tid;
  447. spin_unlock(&journal->j_state_lock);
  448. log_wait_commit(journal, tid);
  449. return 1;
  450. }
  451. /*
  452. * Start a commit of the current running transaction (if any). Returns true
  453. * if a transaction is going to be committed (or is currently already
  454. * committing), and fills its tid in at *ptid
  455. */
  456. int journal_start_commit(journal_t *journal, tid_t *ptid)
  457. {
  458. int ret = 0;
  459. spin_lock(&journal->j_state_lock);
  460. if (journal->j_running_transaction) {
  461. tid_t tid = journal->j_running_transaction->t_tid;
  462. __log_start_commit(journal, tid);
  463. /* There's a running transaction and we've just made sure
  464. * it's commit has been scheduled. */
  465. if (ptid)
  466. *ptid = tid;
  467. ret = 1;
  468. } else if (journal->j_committing_transaction) {
  469. /*
  470. * If ext3_write_super() recently started a commit, then we
  471. * have to wait for completion of that transaction
  472. */
  473. if (ptid)
  474. *ptid = journal->j_committing_transaction->t_tid;
  475. ret = 1;
  476. }
  477. spin_unlock(&journal->j_state_lock);
  478. return ret;
  479. }
  480. /*
  481. * Wait for a specified commit to complete.
  482. * The caller may not hold the journal lock.
  483. */
  484. int log_wait_commit(journal_t *journal, tid_t tid)
  485. {
  486. int err = 0;
  487. #ifdef CONFIG_JBD_DEBUG
  488. spin_lock(&journal->j_state_lock);
  489. if (!tid_geq(journal->j_commit_request, tid)) {
  490. printk(KERN_EMERG
  491. "%s: error: j_commit_request=%d, tid=%d\n",
  492. __func__, journal->j_commit_request, tid);
  493. }
  494. spin_unlock(&journal->j_state_lock);
  495. #endif
  496. spin_lock(&journal->j_state_lock);
  497. while (tid_gt(tid, journal->j_commit_sequence)) {
  498. jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
  499. tid, journal->j_commit_sequence);
  500. wake_up(&journal->j_wait_commit);
  501. spin_unlock(&journal->j_state_lock);
  502. wait_event(journal->j_wait_done_commit,
  503. !tid_gt(tid, journal->j_commit_sequence));
  504. spin_lock(&journal->j_state_lock);
  505. }
  506. spin_unlock(&journal->j_state_lock);
  507. if (unlikely(is_journal_aborted(journal))) {
  508. printk(KERN_EMERG "journal commit I/O error\n");
  509. err = -EIO;
  510. }
  511. return err;
  512. }
  513. /*
  514. * Return 1 if a given transaction has not yet sent barrier request
  515. * connected with a transaction commit. If 0 is returned, transaction
  516. * may or may not have sent the barrier. Used to avoid sending barrier
  517. * twice in common cases.
  518. */
  519. int journal_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
  520. {
  521. int ret = 0;
  522. transaction_t *commit_trans;
  523. if (!(journal->j_flags & JFS_BARRIER))
  524. return 0;
  525. spin_lock(&journal->j_state_lock);
  526. /* Transaction already committed? */
  527. if (tid_geq(journal->j_commit_sequence, tid))
  528. goto out;
  529. /*
  530. * Transaction is being committed and we already proceeded to
  531. * writing commit record?
  532. */
  533. commit_trans = journal->j_committing_transaction;
  534. if (commit_trans && commit_trans->t_tid == tid &&
  535. commit_trans->t_state >= T_COMMIT_RECORD)
  536. goto out;
  537. ret = 1;
  538. out:
  539. spin_unlock(&journal->j_state_lock);
  540. return ret;
  541. }
  542. EXPORT_SYMBOL(journal_trans_will_send_data_barrier);
  543. /*
  544. * Log buffer allocation routines:
  545. */
  546. int journal_next_log_block(journal_t *journal, unsigned int *retp)
  547. {
  548. unsigned int blocknr;
  549. spin_lock(&journal->j_state_lock);
  550. J_ASSERT(journal->j_free > 1);
  551. blocknr = journal->j_head;
  552. journal->j_head++;
  553. journal->j_free--;
  554. if (journal->j_head == journal->j_last)
  555. journal->j_head = journal->j_first;
  556. spin_unlock(&journal->j_state_lock);
  557. return journal_bmap(journal, blocknr, retp);
  558. }
  559. /*
  560. * Conversion of logical to physical block numbers for the journal
  561. *
  562. * On external journals the journal blocks are identity-mapped, so
  563. * this is a no-op. If needed, we can use j_blk_offset - everything is
  564. * ready.
  565. */
  566. int journal_bmap(journal_t *journal, unsigned int blocknr,
  567. unsigned int *retp)
  568. {
  569. int err = 0;
  570. unsigned int ret;
  571. if (journal->j_inode) {
  572. ret = bmap(journal->j_inode, blocknr);
  573. if (ret)
  574. *retp = ret;
  575. else {
  576. char b[BDEVNAME_SIZE];
  577. printk(KERN_ALERT "%s: journal block not found "
  578. "at offset %u on %s\n",
  579. __func__,
  580. blocknr,
  581. bdevname(journal->j_dev, b));
  582. err = -EIO;
  583. __journal_abort_soft(journal, err);
  584. }
  585. } else {
  586. *retp = blocknr; /* +journal->j_blk_offset */
  587. }
  588. return err;
  589. }
  590. /*
  591. * We play buffer_head aliasing tricks to write data/metadata blocks to
  592. * the journal without copying their contents, but for journal
  593. * descriptor blocks we do need to generate bona fide buffers.
  594. *
  595. * After the caller of journal_get_descriptor_buffer() has finished modifying
  596. * the buffer's contents they really should run flush_dcache_page(bh->b_page).
  597. * But we don't bother doing that, so there will be coherency problems with
  598. * mmaps of blockdevs which hold live JBD-controlled filesystems.
  599. */
  600. struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
  601. {
  602. struct buffer_head *bh;
  603. unsigned int blocknr;
  604. int err;
  605. err = journal_next_log_block(journal, &blocknr);
  606. if (err)
  607. return NULL;
  608. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  609. if (!bh)
  610. return NULL;
  611. lock_buffer(bh);
  612. memset(bh->b_data, 0, journal->j_blocksize);
  613. set_buffer_uptodate(bh);
  614. unlock_buffer(bh);
  615. BUFFER_TRACE(bh, "return this buffer");
  616. return journal_add_journal_head(bh);
  617. }
  618. /*
  619. * Management for journal control blocks: functions to create and
  620. * destroy journal_t structures, and to initialise and read existing
  621. * journal blocks from disk. */
  622. /* First: create and setup a journal_t object in memory. We initialise
  623. * very few fields yet: that has to wait until we have created the
  624. * journal structures from from scratch, or loaded them from disk. */
  625. static journal_t * journal_init_common (void)
  626. {
  627. journal_t *journal;
  628. int err;
  629. journal = kzalloc(sizeof(*journal), GFP_KERNEL);
  630. if (!journal)
  631. goto fail;
  632. init_waitqueue_head(&journal->j_wait_transaction_locked);
  633. init_waitqueue_head(&journal->j_wait_logspace);
  634. init_waitqueue_head(&journal->j_wait_done_commit);
  635. init_waitqueue_head(&journal->j_wait_checkpoint);
  636. init_waitqueue_head(&journal->j_wait_commit);
  637. init_waitqueue_head(&journal->j_wait_updates);
  638. mutex_init(&journal->j_barrier);
  639. mutex_init(&journal->j_checkpoint_mutex);
  640. spin_lock_init(&journal->j_revoke_lock);
  641. spin_lock_init(&journal->j_list_lock);
  642. spin_lock_init(&journal->j_state_lock);
  643. journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
  644. /* The journal is marked for error until we succeed with recovery! */
  645. journal->j_flags = JFS_ABORT;
  646. /* Set up a default-sized revoke table for the new mount. */
  647. err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
  648. if (err) {
  649. kfree(journal);
  650. goto fail;
  651. }
  652. return journal;
  653. fail:
  654. return NULL;
  655. }
  656. /* journal_init_dev and journal_init_inode:
  657. *
  658. * Create a journal structure assigned some fixed set of disk blocks to
  659. * the journal. We don't actually touch those disk blocks yet, but we
  660. * need to set up all of the mapping information to tell the journaling
  661. * system where the journal blocks are.
  662. *
  663. */
  664. /**
  665. * journal_t * journal_init_dev() - creates and initialises a journal structure
  666. * @bdev: Block device on which to create the journal
  667. * @fs_dev: Device which hold journalled filesystem for this journal.
  668. * @start: Block nr Start of journal.
  669. * @len: Length of the journal in blocks.
  670. * @blocksize: blocksize of journalling device
  671. *
  672. * Returns: a newly created journal_t *
  673. *
  674. * journal_init_dev creates a journal which maps a fixed contiguous
  675. * range of blocks on an arbitrary block device.
  676. *
  677. */
  678. journal_t * journal_init_dev(struct block_device *bdev,
  679. struct block_device *fs_dev,
  680. int start, int len, int blocksize)
  681. {
  682. journal_t *journal = journal_init_common();
  683. struct buffer_head *bh;
  684. int n;
  685. if (!journal)
  686. return NULL;
  687. /* journal descriptor can store up to n blocks -bzzz */
  688. journal->j_blocksize = blocksize;
  689. n = journal->j_blocksize / sizeof(journal_block_tag_t);
  690. journal->j_wbufsize = n;
  691. journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
  692. if (!journal->j_wbuf) {
  693. printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
  694. __func__);
  695. goto out_err;
  696. }
  697. journal->j_dev = bdev;
  698. journal->j_fs_dev = fs_dev;
  699. journal->j_blk_offset = start;
  700. journal->j_maxlen = len;
  701. bh = __getblk(journal->j_dev, start, journal->j_blocksize);
  702. if (!bh) {
  703. printk(KERN_ERR
  704. "%s: Cannot get buffer for journal superblock\n",
  705. __func__);
  706. goto out_err;
  707. }
  708. journal->j_sb_buffer = bh;
  709. journal->j_superblock = (journal_superblock_t *)bh->b_data;
  710. return journal;
  711. out_err:
  712. kfree(journal->j_wbuf);
  713. kfree(journal);
  714. return NULL;
  715. }
  716. /**
  717. * journal_t * journal_init_inode () - creates a journal which maps to a inode.
  718. * @inode: An inode to create the journal in
  719. *
  720. * journal_init_inode creates a journal which maps an on-disk inode as
  721. * the journal. The inode must exist already, must support bmap() and
  722. * must have all data blocks preallocated.
  723. */
  724. journal_t * journal_init_inode (struct inode *inode)
  725. {
  726. struct buffer_head *bh;
  727. journal_t *journal = journal_init_common();
  728. int err;
  729. int n;
  730. unsigned int blocknr;
  731. if (!journal)
  732. return NULL;
  733. journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
  734. journal->j_inode = inode;
  735. jbd_debug(1,
  736. "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
  737. journal, inode->i_sb->s_id, inode->i_ino,
  738. (long long) inode->i_size,
  739. inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
  740. journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
  741. journal->j_blocksize = inode->i_sb->s_blocksize;
  742. /* journal descriptor can store up to n blocks -bzzz */
  743. n = journal->j_blocksize / sizeof(journal_block_tag_t);
  744. journal->j_wbufsize = n;
  745. journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
  746. if (!journal->j_wbuf) {
  747. printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
  748. __func__);
  749. goto out_err;
  750. }
  751. err = journal_bmap(journal, 0, &blocknr);
  752. /* If that failed, give up */
  753. if (err) {
  754. printk(KERN_ERR "%s: Cannot locate journal superblock\n",
  755. __func__);
  756. goto out_err;
  757. }
  758. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  759. if (!bh) {
  760. printk(KERN_ERR
  761. "%s: Cannot get buffer for journal superblock\n",
  762. __func__);
  763. goto out_err;
  764. }
  765. journal->j_sb_buffer = bh;
  766. journal->j_superblock = (journal_superblock_t *)bh->b_data;
  767. return journal;
  768. out_err:
  769. kfree(journal->j_wbuf);
  770. kfree(journal);
  771. return NULL;
  772. }
  773. /*
  774. * If the journal init or create aborts, we need to mark the journal
  775. * superblock as being NULL to prevent the journal destroy from writing
  776. * back a bogus superblock.
  777. */
  778. static void journal_fail_superblock (journal_t *journal)
  779. {
  780. struct buffer_head *bh = journal->j_sb_buffer;
  781. brelse(bh);
  782. journal->j_sb_buffer = NULL;
  783. }
  784. /*
  785. * Given a journal_t structure, initialise the various fields for
  786. * startup of a new journaling session. We use this both when creating
  787. * a journal, and after recovering an old journal to reset it for
  788. * subsequent use.
  789. */
  790. static int journal_reset(journal_t *journal)
  791. {
  792. journal_superblock_t *sb = journal->j_superblock;
  793. unsigned int first, last;
  794. first = be32_to_cpu(sb->s_first);
  795. last = be32_to_cpu(sb->s_maxlen);
  796. if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
  797. printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
  798. first, last);
  799. journal_fail_superblock(journal);
  800. return -EINVAL;
  801. }
  802. journal->j_first = first;
  803. journal->j_last = last;
  804. journal->j_head = first;
  805. journal->j_tail = first;
  806. journal->j_free = last - first;
  807. journal->j_tail_sequence = journal->j_transaction_sequence;
  808. journal->j_commit_sequence = journal->j_transaction_sequence - 1;
  809. journal->j_commit_request = journal->j_commit_sequence;
  810. journal->j_max_transaction_buffers = journal->j_maxlen / 4;
  811. /* Add the dynamic fields and write it to disk. */
  812. journal_update_superblock(journal, 1);
  813. return journal_start_thread(journal);
  814. }
  815. /**
  816. * int journal_create() - Initialise the new journal file
  817. * @journal: Journal to create. This structure must have been initialised
  818. *
  819. * Given a journal_t structure which tells us which disk blocks we can
  820. * use, create a new journal superblock and initialise all of the
  821. * journal fields from scratch.
  822. **/
  823. int journal_create(journal_t *journal)
  824. {
  825. unsigned int blocknr;
  826. struct buffer_head *bh;
  827. journal_superblock_t *sb;
  828. int i, err;
  829. if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
  830. printk (KERN_ERR "Journal length (%d blocks) too short.\n",
  831. journal->j_maxlen);
  832. journal_fail_superblock(journal);
  833. return -EINVAL;
  834. }
  835. if (journal->j_inode == NULL) {
  836. /*
  837. * We don't know what block to start at!
  838. */
  839. printk(KERN_EMERG
  840. "%s: creation of journal on external device!\n",
  841. __func__);
  842. BUG();
  843. }
  844. /* Zero out the entire journal on disk. We cannot afford to
  845. have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
  846. jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
  847. for (i = 0; i < journal->j_maxlen; i++) {
  848. err = journal_bmap(journal, i, &blocknr);
  849. if (err)
  850. return err;
  851. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  852. if (unlikely(!bh))
  853. return -ENOMEM;
  854. lock_buffer(bh);
  855. memset (bh->b_data, 0, journal->j_blocksize);
  856. BUFFER_TRACE(bh, "marking dirty");
  857. mark_buffer_dirty(bh);
  858. BUFFER_TRACE(bh, "marking uptodate");
  859. set_buffer_uptodate(bh);
  860. unlock_buffer(bh);
  861. __brelse(bh);
  862. }
  863. sync_blockdev(journal->j_dev);
  864. jbd_debug(1, "JBD: journal cleared.\n");
  865. /* OK, fill in the initial static fields in the new superblock */
  866. sb = journal->j_superblock;
  867. sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
  868. sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
  869. sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
  870. sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
  871. sb->s_first = cpu_to_be32(1);
  872. journal->j_transaction_sequence = 1;
  873. journal->j_flags &= ~JFS_ABORT;
  874. journal->j_format_version = 2;
  875. return journal_reset(journal);
  876. }
  877. /**
  878. * void journal_update_superblock() - Update journal sb on disk.
  879. * @journal: The journal to update.
  880. * @wait: Set to '0' if you don't want to wait for IO completion.
  881. *
  882. * Update a journal's dynamic superblock fields and write it to disk,
  883. * optionally waiting for the IO to complete.
  884. */
  885. void journal_update_superblock(journal_t *journal, int wait)
  886. {
  887. journal_superblock_t *sb = journal->j_superblock;
  888. struct buffer_head *bh = journal->j_sb_buffer;
  889. /*
  890. * As a special case, if the on-disk copy is already marked as needing
  891. * no recovery (s_start == 0) and there are no outstanding transactions
  892. * in the filesystem, then we can safely defer the superblock update
  893. * until the next commit by setting JFS_FLUSHED. This avoids
  894. * attempting a write to a potential-readonly device.
  895. */
  896. if (sb->s_start == 0 && journal->j_tail_sequence ==
  897. journal->j_transaction_sequence) {
  898. jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
  899. "(start %u, seq %d, errno %d)\n",
  900. journal->j_tail, journal->j_tail_sequence,
  901. journal->j_errno);
  902. goto out;
  903. }
  904. if (buffer_write_io_error(bh)) {
  905. char b[BDEVNAME_SIZE];
  906. /*
  907. * Oh, dear. A previous attempt to write the journal
  908. * superblock failed. This could happen because the
  909. * USB device was yanked out. Or it could happen to
  910. * be a transient write error and maybe the block will
  911. * be remapped. Nothing we can do but to retry the
  912. * write and hope for the best.
  913. */
  914. printk(KERN_ERR "JBD: previous I/O error detected "
  915. "for journal superblock update for %s.\n",
  916. journal_dev_name(journal, b));
  917. clear_buffer_write_io_error(bh);
  918. set_buffer_uptodate(bh);
  919. }
  920. spin_lock(&journal->j_state_lock);
  921. jbd_debug(1,"JBD: updating superblock (start %u, seq %d, errno %d)\n",
  922. journal->j_tail, journal->j_tail_sequence, journal->j_errno);
  923. sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
  924. sb->s_start = cpu_to_be32(journal->j_tail);
  925. sb->s_errno = cpu_to_be32(journal->j_errno);
  926. spin_unlock(&journal->j_state_lock);
  927. BUFFER_TRACE(bh, "marking dirty");
  928. mark_buffer_dirty(bh);
  929. if (wait) {
  930. sync_dirty_buffer(bh);
  931. if (buffer_write_io_error(bh)) {
  932. char b[BDEVNAME_SIZE];
  933. printk(KERN_ERR "JBD: I/O error detected "
  934. "when updating journal superblock for %s.\n",
  935. journal_dev_name(journal, b));
  936. clear_buffer_write_io_error(bh);
  937. set_buffer_uptodate(bh);
  938. }
  939. } else
  940. write_dirty_buffer(bh, WRITE);
  941. out:
  942. /* If we have just flushed the log (by marking s_start==0), then
  943. * any future commit will have to be careful to update the
  944. * superblock again to re-record the true start of the log. */
  945. spin_lock(&journal->j_state_lock);
  946. if (sb->s_start)
  947. journal->j_flags &= ~JFS_FLUSHED;
  948. else
  949. journal->j_flags |= JFS_FLUSHED;
  950. spin_unlock(&journal->j_state_lock);
  951. }
  952. /*
  953. * Read the superblock for a given journal, performing initial
  954. * validation of the format.
  955. */
  956. static int journal_get_superblock(journal_t *journal)
  957. {
  958. struct buffer_head *bh;
  959. journal_superblock_t *sb;
  960. int err = -EIO;
  961. bh = journal->j_sb_buffer;
  962. J_ASSERT(bh != NULL);
  963. if (!buffer_uptodate(bh)) {
  964. ll_rw_block(READ, 1, &bh);
  965. wait_on_buffer(bh);
  966. if (!buffer_uptodate(bh)) {
  967. printk (KERN_ERR
  968. "JBD: IO error reading journal superblock\n");
  969. goto out;
  970. }
  971. }
  972. sb = journal->j_superblock;
  973. err = -EINVAL;
  974. if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
  975. sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
  976. printk(KERN_WARNING "JBD: no valid journal superblock found\n");
  977. goto out;
  978. }
  979. switch(be32_to_cpu(sb->s_header.h_blocktype)) {
  980. case JFS_SUPERBLOCK_V1:
  981. journal->j_format_version = 1;
  982. break;
  983. case JFS_SUPERBLOCK_V2:
  984. journal->j_format_version = 2;
  985. break;
  986. default:
  987. printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
  988. goto out;
  989. }
  990. if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
  991. journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
  992. else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
  993. printk (KERN_WARNING "JBD: journal file too short\n");
  994. goto out;
  995. }
  996. if (be32_to_cpu(sb->s_first) == 0 ||
  997. be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
  998. printk(KERN_WARNING
  999. "JBD: Invalid start block of journal: %u\n",
  1000. be32_to_cpu(sb->s_first));
  1001. goto out;
  1002. }
  1003. return 0;
  1004. out:
  1005. journal_fail_superblock(journal);
  1006. return err;
  1007. }
  1008. /*
  1009. * Load the on-disk journal superblock and read the key fields into the
  1010. * journal_t.
  1011. */
  1012. static int load_superblock(journal_t *journal)
  1013. {
  1014. int err;
  1015. journal_superblock_t *sb;
  1016. err = journal_get_superblock(journal);
  1017. if (err)
  1018. return err;
  1019. sb = journal->j_superblock;
  1020. journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
  1021. journal->j_tail = be32_to_cpu(sb->s_start);
  1022. journal->j_first = be32_to_cpu(sb->s_first);
  1023. journal->j_last = be32_to_cpu(sb->s_maxlen);
  1024. journal->j_errno = be32_to_cpu(sb->s_errno);
  1025. return 0;
  1026. }
  1027. /**
  1028. * int journal_load() - Read journal from disk.
  1029. * @journal: Journal to act on.
  1030. *
  1031. * Given a journal_t structure which tells us which disk blocks contain
  1032. * a journal, read the journal from disk to initialise the in-memory
  1033. * structures.
  1034. */
  1035. int journal_load(journal_t *journal)
  1036. {
  1037. int err;
  1038. journal_superblock_t *sb;
  1039. err = load_superblock(journal);
  1040. if (err)
  1041. return err;
  1042. sb = journal->j_superblock;
  1043. /* If this is a V2 superblock, then we have to check the
  1044. * features flags on it. */
  1045. if (journal->j_format_version >= 2) {
  1046. if ((sb->s_feature_ro_compat &
  1047. ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
  1048. (sb->s_feature_incompat &
  1049. ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
  1050. printk (KERN_WARNING
  1051. "JBD: Unrecognised features on journal\n");
  1052. return -EINVAL;
  1053. }
  1054. }
  1055. /* Let the recovery code check whether it needs to recover any
  1056. * data from the journal. */
  1057. if (journal_recover(journal))
  1058. goto recovery_error;
  1059. /* OK, we've finished with the dynamic journal bits:
  1060. * reinitialise the dynamic contents of the superblock in memory
  1061. * and reset them on disk. */
  1062. if (journal_reset(journal))
  1063. goto recovery_error;
  1064. journal->j_flags &= ~JFS_ABORT;
  1065. journal->j_flags |= JFS_LOADED;
  1066. return 0;
  1067. recovery_error:
  1068. printk (KERN_WARNING "JBD: recovery failed\n");
  1069. return -EIO;
  1070. }
  1071. /**
  1072. * void journal_destroy() - Release a journal_t structure.
  1073. * @journal: Journal to act on.
  1074. *
  1075. * Release a journal_t structure once it is no longer in use by the
  1076. * journaled object.
  1077. * Return <0 if we couldn't clean up the journal.
  1078. */
  1079. int journal_destroy(journal_t *journal)
  1080. {
  1081. int err = 0;
  1082. /* Wait for the commit thread to wake up and die. */
  1083. journal_kill_thread(journal);
  1084. /* Force a final log commit */
  1085. if (journal->j_running_transaction)
  1086. journal_commit_transaction(journal);
  1087. /* Force any old transactions to disk */
  1088. /* Totally anal locking here... */
  1089. spin_lock(&journal->j_list_lock);
  1090. while (journal->j_checkpoint_transactions != NULL) {
  1091. spin_unlock(&journal->j_list_lock);
  1092. log_do_checkpoint(journal);
  1093. spin_lock(&journal->j_list_lock);
  1094. }
  1095. J_ASSERT(journal->j_running_transaction == NULL);
  1096. J_ASSERT(journal->j_committing_transaction == NULL);
  1097. J_ASSERT(journal->j_checkpoint_transactions == NULL);
  1098. spin_unlock(&journal->j_list_lock);
  1099. if (journal->j_sb_buffer) {
  1100. if (!is_journal_aborted(journal)) {
  1101. /* We can now mark the journal as empty. */
  1102. journal->j_tail = 0;
  1103. journal->j_tail_sequence =
  1104. ++journal->j_transaction_sequence;
  1105. journal_update_superblock(journal, 1);
  1106. } else {
  1107. err = -EIO;
  1108. }
  1109. brelse(journal->j_sb_buffer);
  1110. }
  1111. if (journal->j_inode)
  1112. iput(journal->j_inode);
  1113. if (journal->j_revoke)
  1114. journal_destroy_revoke(journal);
  1115. kfree(journal->j_wbuf);
  1116. kfree(journal);
  1117. return err;
  1118. }
  1119. /**
  1120. *int journal_check_used_features () - Check if features specified are used.
  1121. * @journal: Journal to check.
  1122. * @compat: bitmask of compatible features
  1123. * @ro: bitmask of features that force read-only mount
  1124. * @incompat: bitmask of incompatible features
  1125. *
  1126. * Check whether the journal uses all of a given set of
  1127. * features. Return true (non-zero) if it does.
  1128. **/
  1129. int journal_check_used_features (journal_t *journal, unsigned long compat,
  1130. unsigned long ro, unsigned long incompat)
  1131. {
  1132. journal_superblock_t *sb;
  1133. if (!compat && !ro && !incompat)
  1134. return 1;
  1135. if (journal->j_format_version == 1)
  1136. return 0;
  1137. sb = journal->j_superblock;
  1138. if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
  1139. ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
  1140. ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
  1141. return 1;
  1142. return 0;
  1143. }
  1144. /**
  1145. * int journal_check_available_features() - Check feature set in journalling layer
  1146. * @journal: Journal to check.
  1147. * @compat: bitmask of compatible features
  1148. * @ro: bitmask of features that force read-only mount
  1149. * @incompat: bitmask of incompatible features
  1150. *
  1151. * Check whether the journaling code supports the use of
  1152. * all of a given set of features on this journal. Return true
  1153. * (non-zero) if it can. */
  1154. int journal_check_available_features (journal_t *journal, unsigned long compat,
  1155. unsigned long ro, unsigned long incompat)
  1156. {
  1157. if (!compat && !ro && !incompat)
  1158. return 1;
  1159. /* We can support any known requested features iff the
  1160. * superblock is in version 2. Otherwise we fail to support any
  1161. * extended sb features. */
  1162. if (journal->j_format_version != 2)
  1163. return 0;
  1164. if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
  1165. (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
  1166. (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
  1167. return 1;
  1168. return 0;
  1169. }
  1170. /**
  1171. * int journal_set_features () - Mark a given journal feature in the superblock
  1172. * @journal: Journal to act on.
  1173. * @compat: bitmask of compatible features
  1174. * @ro: bitmask of features that force read-only mount
  1175. * @incompat: bitmask of incompatible features
  1176. *
  1177. * Mark a given journal feature as present on the
  1178. * superblock. Returns true if the requested features could be set.
  1179. *
  1180. */
  1181. int journal_set_features (journal_t *journal, unsigned long compat,
  1182. unsigned long ro, unsigned long incompat)
  1183. {
  1184. journal_superblock_t *sb;
  1185. if (journal_check_used_features(journal, compat, ro, incompat))
  1186. return 1;
  1187. if (!journal_check_available_features(journal, compat, ro, incompat))
  1188. return 0;
  1189. jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
  1190. compat, ro, incompat);
  1191. sb = journal->j_superblock;
  1192. sb->s_feature_compat |= cpu_to_be32(compat);
  1193. sb->s_feature_ro_compat |= cpu_to_be32(ro);
  1194. sb->s_feature_incompat |= cpu_to_be32(incompat);
  1195. return 1;
  1196. }
  1197. /**
  1198. * int journal_update_format () - Update on-disk journal structure.
  1199. * @journal: Journal to act on.
  1200. *
  1201. * Given an initialised but unloaded journal struct, poke about in the
  1202. * on-disk structure to update it to the most recent supported version.
  1203. */
  1204. int journal_update_format (journal_t *journal)
  1205. {
  1206. journal_superblock_t *sb;
  1207. int err;
  1208. err = journal_get_superblock(journal);
  1209. if (err)
  1210. return err;
  1211. sb = journal->j_superblock;
  1212. switch (be32_to_cpu(sb->s_header.h_blocktype)) {
  1213. case JFS_SUPERBLOCK_V2:
  1214. return 0;
  1215. case JFS_SUPERBLOCK_V1:
  1216. return journal_convert_superblock_v1(journal, sb);
  1217. default:
  1218. break;
  1219. }
  1220. return -EINVAL;
  1221. }
  1222. static int journal_convert_superblock_v1(journal_t *journal,
  1223. journal_superblock_t *sb)
  1224. {
  1225. int offset, blocksize;
  1226. struct buffer_head *bh;
  1227. printk(KERN_WARNING
  1228. "JBD: Converting superblock from version 1 to 2.\n");
  1229. /* Pre-initialise new fields to zero */
  1230. offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
  1231. blocksize = be32_to_cpu(sb->s_blocksize);
  1232. memset(&sb->s_feature_compat, 0, blocksize-offset);
  1233. sb->s_nr_users = cpu_to_be32(1);
  1234. sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
  1235. journal->j_format_version = 2;
  1236. bh = journal->j_sb_buffer;
  1237. BUFFER_TRACE(bh, "marking dirty");
  1238. mark_buffer_dirty(bh);
  1239. sync_dirty_buffer(bh);
  1240. return 0;
  1241. }
  1242. /**
  1243. * int journal_flush () - Flush journal
  1244. * @journal: Journal to act on.
  1245. *
  1246. * Flush all data for a given journal to disk and empty the journal.
  1247. * Filesystems can use this when remounting readonly to ensure that
  1248. * recovery does not need to happen on remount.
  1249. */
  1250. int journal_flush(journal_t *journal)
  1251. {
  1252. int err = 0;
  1253. transaction_t *transaction = NULL;
  1254. unsigned int old_tail;
  1255. spin_lock(&journal->j_state_lock);
  1256. /* Force everything buffered to the log... */
  1257. if (journal->j_running_transaction) {
  1258. transaction = journal->j_running_transaction;
  1259. __log_start_commit(journal, transaction->t_tid);
  1260. } else if (journal->j_committing_transaction)
  1261. transaction = journal->j_committing_transaction;
  1262. /* Wait for the log commit to complete... */
  1263. if (transaction) {
  1264. tid_t tid = transaction->t_tid;
  1265. spin_unlock(&journal->j_state_lock);
  1266. log_wait_commit(journal, tid);
  1267. } else {
  1268. spin_unlock(&journal->j_state_lock);
  1269. }
  1270. /* ...and flush everything in the log out to disk. */
  1271. spin_lock(&journal->j_list_lock);
  1272. while (!err && journal->j_checkpoint_transactions != NULL) {
  1273. spin_unlock(&journal->j_list_lock);
  1274. mutex_lock(&journal->j_checkpoint_mutex);
  1275. err = log_do_checkpoint(journal);
  1276. mutex_unlock(&journal->j_checkpoint_mutex);
  1277. spin_lock(&journal->j_list_lock);
  1278. }
  1279. spin_unlock(&journal->j_list_lock);
  1280. if (is_journal_aborted(journal))
  1281. return -EIO;
  1282. cleanup_journal_tail(journal);
  1283. /* Finally, mark the journal as really needing no recovery.
  1284. * This sets s_start==0 in the underlying superblock, which is
  1285. * the magic code for a fully-recovered superblock. Any future
  1286. * commits of data to the journal will restore the current
  1287. * s_start value. */
  1288. spin_lock(&journal->j_state_lock);
  1289. old_tail = journal->j_tail;
  1290. journal->j_tail = 0;
  1291. spin_unlock(&journal->j_state_lock);
  1292. journal_update_superblock(journal, 1);
  1293. spin_lock(&journal->j_state_lock);
  1294. journal->j_tail = old_tail;
  1295. J_ASSERT(!journal->j_running_transaction);
  1296. J_ASSERT(!journal->j_committing_transaction);
  1297. J_ASSERT(!journal->j_checkpoint_transactions);
  1298. J_ASSERT(journal->j_head == journal->j_tail);
  1299. J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
  1300. spin_unlock(&journal->j_state_lock);
  1301. return 0;
  1302. }
  1303. /**
  1304. * int journal_wipe() - Wipe journal contents
  1305. * @journal: Journal to act on.
  1306. * @write: flag (see below)
  1307. *
  1308. * Wipe out all of the contents of a journal, safely. This will produce
  1309. * a warning if the journal contains any valid recovery information.
  1310. * Must be called between journal_init_*() and journal_load().
  1311. *
  1312. * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
  1313. * we merely suppress recovery.
  1314. */
  1315. int journal_wipe(journal_t *journal, int write)
  1316. {
  1317. int err = 0;
  1318. J_ASSERT (!(journal->j_flags & JFS_LOADED));
  1319. err = load_superblock(journal);
  1320. if (err)
  1321. return err;
  1322. if (!journal->j_tail)
  1323. goto no_recovery;
  1324. printk (KERN_WARNING "JBD: %s recovery information on journal\n",
  1325. write ? "Clearing" : "Ignoring");
  1326. err = journal_skip_recovery(journal);
  1327. if (write)
  1328. journal_update_superblock(journal, 1);
  1329. no_recovery:
  1330. return err;
  1331. }
  1332. /*
  1333. * journal_dev_name: format a character string to describe on what
  1334. * device this journal is present.
  1335. */
  1336. static const char *journal_dev_name(journal_t *journal, char *buffer)
  1337. {
  1338. struct block_device *bdev;
  1339. if (journal->j_inode)
  1340. bdev = journal->j_inode->i_sb->s_bdev;
  1341. else
  1342. bdev = journal->j_dev;
  1343. return bdevname(bdev, buffer);
  1344. }
  1345. /*
  1346. * Journal abort has very specific semantics, which we describe
  1347. * for journal abort.
  1348. *
  1349. * Two internal function, which provide abort to te jbd layer
  1350. * itself are here.
  1351. */
  1352. /*
  1353. * Quick version for internal journal use (doesn't lock the journal).
  1354. * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
  1355. * and don't attempt to make any other journal updates.
  1356. */
  1357. static void __journal_abort_hard(journal_t *journal)
  1358. {
  1359. transaction_t *transaction;
  1360. char b[BDEVNAME_SIZE];
  1361. if (journal->j_flags & JFS_ABORT)
  1362. return;
  1363. printk(KERN_ERR "Aborting journal on device %s.\n",
  1364. journal_dev_name(journal, b));
  1365. spin_lock(&journal->j_state_lock);
  1366. journal->j_flags |= JFS_ABORT;
  1367. transaction = journal->j_running_transaction;
  1368. if (transaction)
  1369. __log_start_commit(journal, transaction->t_tid);
  1370. spin_unlock(&journal->j_state_lock);
  1371. }
  1372. /* Soft abort: record the abort error status in the journal superblock,
  1373. * but don't do any other IO. */
  1374. static void __journal_abort_soft (journal_t *journal, int errno)
  1375. {
  1376. if (journal->j_flags & JFS_ABORT)
  1377. return;
  1378. if (!journal->j_errno)
  1379. journal->j_errno = errno;
  1380. __journal_abort_hard(journal);
  1381. if (errno)
  1382. journal_update_superblock(journal, 1);
  1383. }
  1384. /**
  1385. * void journal_abort () - Shutdown the journal immediately.
  1386. * @journal: the journal to shutdown.
  1387. * @errno: an error number to record in the journal indicating
  1388. * the reason for the shutdown.
  1389. *
  1390. * Perform a complete, immediate shutdown of the ENTIRE
  1391. * journal (not of a single transaction). This operation cannot be
  1392. * undone without closing and reopening the journal.
  1393. *
  1394. * The journal_abort function is intended to support higher level error
  1395. * recovery mechanisms such as the ext2/ext3 remount-readonly error
  1396. * mode.
  1397. *
  1398. * Journal abort has very specific semantics. Any existing dirty,
  1399. * unjournaled buffers in the main filesystem will still be written to
  1400. * disk by bdflush, but the journaling mechanism will be suspended
  1401. * immediately and no further transaction commits will be honoured.
  1402. *
  1403. * Any dirty, journaled buffers will be written back to disk without
  1404. * hitting the journal. Atomicity cannot be guaranteed on an aborted
  1405. * filesystem, but we _do_ attempt to leave as much data as possible
  1406. * behind for fsck to use for cleanup.
  1407. *
  1408. * Any attempt to get a new transaction handle on a journal which is in
  1409. * ABORT state will just result in an -EROFS error return. A
  1410. * journal_stop on an existing handle will return -EIO if we have
  1411. * entered abort state during the update.
  1412. *
  1413. * Recursive transactions are not disturbed by journal abort until the
  1414. * final journal_stop, which will receive the -EIO error.
  1415. *
  1416. * Finally, the journal_abort call allows the caller to supply an errno
  1417. * which will be recorded (if possible) in the journal superblock. This
  1418. * allows a client to record failure conditions in the middle of a
  1419. * transaction without having to complete the transaction to record the
  1420. * failure to disk. ext3_error, for example, now uses this
  1421. * functionality.
  1422. *
  1423. * Errors which originate from within the journaling layer will NOT
  1424. * supply an errno; a null errno implies that absolutely no further
  1425. * writes are done to the journal (unless there are any already in
  1426. * progress).
  1427. *
  1428. */
  1429. void journal_abort(journal_t *journal, int errno)
  1430. {
  1431. __journal_abort_soft(journal, errno);
  1432. }
  1433. /**
  1434. * int journal_errno () - returns the journal's error state.
  1435. * @journal: journal to examine.
  1436. *
  1437. * This is the errno numbet set with journal_abort(), the last
  1438. * time the journal was mounted - if the journal was stopped
  1439. * without calling abort this will be 0.
  1440. *
  1441. * If the journal has been aborted on this mount time -EROFS will
  1442. * be returned.
  1443. */
  1444. int journal_errno(journal_t *journal)
  1445. {
  1446. int err;
  1447. spin_lock(&journal->j_state_lock);
  1448. if (journal->j_flags & JFS_ABORT)
  1449. err = -EROFS;
  1450. else
  1451. err = journal->j_errno;
  1452. spin_unlock(&journal->j_state_lock);
  1453. return err;
  1454. }
  1455. /**
  1456. * int journal_clear_err () - clears the journal's error state
  1457. * @journal: journal to act on.
  1458. *
  1459. * An error must be cleared or Acked to take a FS out of readonly
  1460. * mode.
  1461. */
  1462. int journal_clear_err(journal_t *journal)
  1463. {
  1464. int err = 0;
  1465. spin_lock(&journal->j_state_lock);
  1466. if (journal->j_flags & JFS_ABORT)
  1467. err = -EROFS;
  1468. else
  1469. journal->j_errno = 0;
  1470. spin_unlock(&journal->j_state_lock);
  1471. return err;
  1472. }
  1473. /**
  1474. * void journal_ack_err() - Ack journal err.
  1475. * @journal: journal to act on.
  1476. *
  1477. * An error must be cleared or Acked to take a FS out of readonly
  1478. * mode.
  1479. */
  1480. void journal_ack_err(journal_t *journal)
  1481. {
  1482. spin_lock(&journal->j_state_lock);
  1483. if (journal->j_errno)
  1484. journal->j_flags |= JFS_ACK_ERR;
  1485. spin_unlock(&journal->j_state_lock);
  1486. }
  1487. int journal_blocks_per_page(struct inode *inode)
  1488. {
  1489. return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  1490. }
  1491. /*
  1492. * Journal_head storage management
  1493. */
  1494. static struct kmem_cache *journal_head_cache;
  1495. #ifdef CONFIG_JBD_DEBUG
  1496. static atomic_t nr_journal_heads = ATOMIC_INIT(0);
  1497. #endif
  1498. static int journal_init_journal_head_cache(void)
  1499. {
  1500. int retval;
  1501. J_ASSERT(journal_head_cache == NULL);
  1502. journal_head_cache = kmem_cache_create("journal_head",
  1503. sizeof(struct journal_head),
  1504. 0, /* offset */
  1505. SLAB_TEMPORARY, /* flags */
  1506. NULL); /* ctor */
  1507. retval = 0;
  1508. if (!journal_head_cache) {
  1509. retval = -ENOMEM;
  1510. printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
  1511. }
  1512. return retval;
  1513. }
  1514. static void journal_destroy_journal_head_cache(void)
  1515. {
  1516. if (journal_head_cache) {
  1517. kmem_cache_destroy(journal_head_cache);
  1518. journal_head_cache = NULL;
  1519. }
  1520. }
  1521. /*
  1522. * journal_head splicing and dicing
  1523. */
  1524. static struct journal_head *journal_alloc_journal_head(void)
  1525. {
  1526. struct journal_head *ret;
  1527. #ifdef CONFIG_JBD_DEBUG
  1528. atomic_inc(&nr_journal_heads);
  1529. #endif
  1530. ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
  1531. if (ret == NULL) {
  1532. jbd_debug(1, "out of memory for journal_head\n");
  1533. printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n",
  1534. __func__);
  1535. while (ret == NULL) {
  1536. yield();
  1537. ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
  1538. }
  1539. }
  1540. return ret;
  1541. }
  1542. static void journal_free_journal_head(struct journal_head *jh)
  1543. {
  1544. #ifdef CONFIG_JBD_DEBUG
  1545. atomic_dec(&nr_journal_heads);
  1546. memset(jh, JBD_POISON_FREE, sizeof(*jh));
  1547. #endif
  1548. kmem_cache_free(journal_head_cache, jh);
  1549. }
  1550. /*
  1551. * A journal_head is attached to a buffer_head whenever JBD has an
  1552. * interest in the buffer.
  1553. *
  1554. * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
  1555. * is set. This bit is tested in core kernel code where we need to take
  1556. * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
  1557. * there.
  1558. *
  1559. * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
  1560. *
  1561. * When a buffer has its BH_JBD bit set it is immune from being released by
  1562. * core kernel code, mainly via ->b_count.
  1563. *
  1564. * A journal_head may be detached from its buffer_head when the journal_head's
  1565. * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
  1566. * Various places in JBD call journal_remove_journal_head() to indicate that the
  1567. * journal_head can be dropped if needed.
  1568. *
  1569. * Various places in the kernel want to attach a journal_head to a buffer_head
  1570. * _before_ attaching the journal_head to a transaction. To protect the
  1571. * journal_head in this situation, journal_add_journal_head elevates the
  1572. * journal_head's b_jcount refcount by one. The caller must call
  1573. * journal_put_journal_head() to undo this.
  1574. *
  1575. * So the typical usage would be:
  1576. *
  1577. * (Attach a journal_head if needed. Increments b_jcount)
  1578. * struct journal_head *jh = journal_add_journal_head(bh);
  1579. * ...
  1580. * jh->b_transaction = xxx;
  1581. * journal_put_journal_head(jh);
  1582. *
  1583. * Now, the journal_head's b_jcount is zero, but it is safe from being released
  1584. * because it has a non-zero b_transaction.
  1585. */
  1586. /*
  1587. * Give a buffer_head a journal_head.
  1588. *
  1589. * Doesn't need the journal lock.
  1590. * May sleep.
  1591. */
  1592. struct journal_head *journal_add_journal_head(struct buffer_head *bh)
  1593. {
  1594. struct journal_head *jh;
  1595. struct journal_head *new_jh = NULL;
  1596. repeat:
  1597. if (!buffer_jbd(bh)) {
  1598. new_jh = journal_alloc_journal_head();
  1599. memset(new_jh, 0, sizeof(*new_jh));
  1600. }
  1601. jbd_lock_bh_journal_head(bh);
  1602. if (buffer_jbd(bh)) {
  1603. jh = bh2jh(bh);
  1604. } else {
  1605. J_ASSERT_BH(bh,
  1606. (atomic_read(&bh->b_count) > 0) ||
  1607. (bh->b_page && bh->b_page->mapping));
  1608. if (!new_jh) {
  1609. jbd_unlock_bh_journal_head(bh);
  1610. goto repeat;
  1611. }
  1612. jh = new_jh;
  1613. new_jh = NULL; /* We consumed it */
  1614. set_buffer_jbd(bh);
  1615. bh->b_private = jh;
  1616. jh->b_bh = bh;
  1617. get_bh(bh);
  1618. BUFFER_TRACE(bh, "added journal_head");
  1619. }
  1620. jh->b_jcount++;
  1621. jbd_unlock_bh_journal_head(bh);
  1622. if (new_jh)
  1623. journal_free_journal_head(new_jh);
  1624. return bh->b_private;
  1625. }
  1626. /*
  1627. * Grab a ref against this buffer_head's journal_head. If it ended up not
  1628. * having a journal_head, return NULL
  1629. */
  1630. struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
  1631. {
  1632. struct journal_head *jh = NULL;
  1633. jbd_lock_bh_journal_head(bh);
  1634. if (buffer_jbd(bh)) {
  1635. jh = bh2jh(bh);
  1636. jh->b_jcount++;
  1637. }
  1638. jbd_unlock_bh_journal_head(bh);
  1639. return jh;
  1640. }
  1641. static void __journal_remove_journal_head(struct buffer_head *bh)
  1642. {
  1643. struct journal_head *jh = bh2jh(bh);
  1644. J_ASSERT_JH(jh, jh->b_jcount >= 0);
  1645. get_bh(bh);
  1646. if (jh->b_jcount == 0) {
  1647. if (jh->b_transaction == NULL &&
  1648. jh->b_next_transaction == NULL &&
  1649. jh->b_cp_transaction == NULL) {
  1650. J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
  1651. J_ASSERT_BH(bh, buffer_jbd(bh));
  1652. J_ASSERT_BH(bh, jh2bh(jh) == bh);
  1653. BUFFER_TRACE(bh, "remove journal_head");
  1654. if (jh->b_frozen_data) {
  1655. printk(KERN_WARNING "%s: freeing "
  1656. "b_frozen_data\n",
  1657. __func__);
  1658. jbd_free(jh->b_frozen_data, bh->b_size);
  1659. }
  1660. if (jh->b_committed_data) {
  1661. printk(KERN_WARNING "%s: freeing "
  1662. "b_committed_data\n",
  1663. __func__);
  1664. jbd_free(jh->b_committed_data, bh->b_size);
  1665. }
  1666. bh->b_private = NULL;
  1667. jh->b_bh = NULL; /* debug, really */
  1668. clear_buffer_jbd(bh);
  1669. __brelse(bh);
  1670. journal_free_journal_head(jh);
  1671. } else {
  1672. BUFFER_TRACE(bh, "journal_head was locked");
  1673. }
  1674. }
  1675. }
  1676. /*
  1677. * journal_remove_journal_head(): if the buffer isn't attached to a transaction
  1678. * and has a zero b_jcount then remove and release its journal_head. If we did
  1679. * see that the buffer is not used by any transaction we also "logically"
  1680. * decrement ->b_count.
  1681. *
  1682. * We in fact take an additional increment on ->b_count as a convenience,
  1683. * because the caller usually wants to do additional things with the bh
  1684. * after calling here.
  1685. * The caller of journal_remove_journal_head() *must* run __brelse(bh) at some
  1686. * time. Once the caller has run __brelse(), the buffer is eligible for
  1687. * reaping by try_to_free_buffers().
  1688. */
  1689. void journal_remove_journal_head(struct buffer_head *bh)
  1690. {
  1691. jbd_lock_bh_journal_head(bh);
  1692. __journal_remove_journal_head(bh);
  1693. jbd_unlock_bh_journal_head(bh);
  1694. }
  1695. /*
  1696. * Drop a reference on the passed journal_head. If it fell to zero then try to
  1697. * release the journal_head from the buffer_head.
  1698. */
  1699. void journal_put_journal_head(struct journal_head *jh)
  1700. {
  1701. struct buffer_head *bh = jh2bh(jh);
  1702. jbd_lock_bh_journal_head(bh);
  1703. J_ASSERT_JH(jh, jh->b_jcount > 0);
  1704. --jh->b_jcount;
  1705. if (!jh->b_jcount && !jh->b_transaction) {
  1706. __journal_remove_journal_head(bh);
  1707. __brelse(bh);
  1708. }
  1709. jbd_unlock_bh_journal_head(bh);
  1710. }
  1711. /*
  1712. * debugfs tunables
  1713. */
  1714. #ifdef CONFIG_JBD_DEBUG
  1715. u8 journal_enable_debug __read_mostly;
  1716. EXPORT_SYMBOL(journal_enable_debug);
  1717. static struct dentry *jbd_debugfs_dir;
  1718. static struct dentry *jbd_debug;
  1719. static void __init jbd_create_debugfs_entry(void)
  1720. {
  1721. jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
  1722. if (jbd_debugfs_dir)
  1723. jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
  1724. jbd_debugfs_dir,
  1725. &journal_enable_debug);
  1726. }
  1727. static void __exit jbd_remove_debugfs_entry(void)
  1728. {
  1729. debugfs_remove(jbd_debug);
  1730. debugfs_remove(jbd_debugfs_dir);
  1731. }
  1732. #else
  1733. static inline void jbd_create_debugfs_entry(void)
  1734. {
  1735. }
  1736. static inline void jbd_remove_debugfs_entry(void)
  1737. {
  1738. }
  1739. #endif
  1740. struct kmem_cache *jbd_handle_cache;
  1741. static int __init journal_init_handle_cache(void)
  1742. {
  1743. jbd_handle_cache = kmem_cache_create("journal_handle",
  1744. sizeof(handle_t),
  1745. 0, /* offset */
  1746. SLAB_TEMPORARY, /* flags */
  1747. NULL); /* ctor */
  1748. if (jbd_handle_cache == NULL) {
  1749. printk(KERN_EMERG "JBD: failed to create handle cache\n");
  1750. return -ENOMEM;
  1751. }
  1752. return 0;
  1753. }
  1754. static void journal_destroy_handle_cache(void)
  1755. {
  1756. if (jbd_handle_cache)
  1757. kmem_cache_destroy(jbd_handle_cache);
  1758. }
  1759. /*
  1760. * Module startup and shutdown
  1761. */
  1762. static int __init journal_init_caches(void)
  1763. {
  1764. int ret;
  1765. ret = journal_init_revoke_caches();
  1766. if (ret == 0)
  1767. ret = journal_init_journal_head_cache();
  1768. if (ret == 0)
  1769. ret = journal_init_handle_cache();
  1770. return ret;
  1771. }
  1772. static void journal_destroy_caches(void)
  1773. {
  1774. journal_destroy_revoke_caches();
  1775. journal_destroy_journal_head_cache();
  1776. journal_destroy_handle_cache();
  1777. }
  1778. static int __init journal_init(void)
  1779. {
  1780. int ret;
  1781. BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
  1782. ret = journal_init_caches();
  1783. if (ret != 0)
  1784. journal_destroy_caches();
  1785. jbd_create_debugfs_entry();
  1786. return ret;
  1787. }
  1788. static void __exit journal_exit(void)
  1789. {
  1790. #ifdef CONFIG_JBD_DEBUG
  1791. int n = atomic_read(&nr_journal_heads);
  1792. if (n)
  1793. printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
  1794. #endif
  1795. jbd_remove_debugfs_entry();
  1796. journal_destroy_caches();
  1797. }
  1798. MODULE_LICENSE("GPL");
  1799. module_init(journal_init);
  1800. module_exit(journal_exit);