journal.c 56 KB

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