journal.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  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. return 0;
  997. out:
  998. journal_fail_superblock(journal);
  999. return err;
  1000. }
  1001. /*
  1002. * Load the on-disk journal superblock and read the key fields into the
  1003. * journal_t.
  1004. */
  1005. static int load_superblock(journal_t *journal)
  1006. {
  1007. int err;
  1008. journal_superblock_t *sb;
  1009. err = journal_get_superblock(journal);
  1010. if (err)
  1011. return err;
  1012. sb = journal->j_superblock;
  1013. journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
  1014. journal->j_tail = be32_to_cpu(sb->s_start);
  1015. journal->j_first = be32_to_cpu(sb->s_first);
  1016. journal->j_last = be32_to_cpu(sb->s_maxlen);
  1017. journal->j_errno = be32_to_cpu(sb->s_errno);
  1018. return 0;
  1019. }
  1020. /**
  1021. * int journal_load() - Read journal from disk.
  1022. * @journal: Journal to act on.
  1023. *
  1024. * Given a journal_t structure which tells us which disk blocks contain
  1025. * a journal, read the journal from disk to initialise the in-memory
  1026. * structures.
  1027. */
  1028. int journal_load(journal_t *journal)
  1029. {
  1030. int err;
  1031. journal_superblock_t *sb;
  1032. err = load_superblock(journal);
  1033. if (err)
  1034. return err;
  1035. sb = journal->j_superblock;
  1036. /* If this is a V2 superblock, then we have to check the
  1037. * features flags on it. */
  1038. if (journal->j_format_version >= 2) {
  1039. if ((sb->s_feature_ro_compat &
  1040. ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
  1041. (sb->s_feature_incompat &
  1042. ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
  1043. printk (KERN_WARNING
  1044. "JBD: Unrecognised features on journal\n");
  1045. return -EINVAL;
  1046. }
  1047. }
  1048. /* Let the recovery code check whether it needs to recover any
  1049. * data from the journal. */
  1050. if (journal_recover(journal))
  1051. goto recovery_error;
  1052. /* OK, we've finished with the dynamic journal bits:
  1053. * reinitialise the dynamic contents of the superblock in memory
  1054. * and reset them on disk. */
  1055. if (journal_reset(journal))
  1056. goto recovery_error;
  1057. journal->j_flags &= ~JFS_ABORT;
  1058. journal->j_flags |= JFS_LOADED;
  1059. return 0;
  1060. recovery_error:
  1061. printk (KERN_WARNING "JBD: recovery failed\n");
  1062. return -EIO;
  1063. }
  1064. /**
  1065. * void journal_destroy() - Release a journal_t structure.
  1066. * @journal: Journal to act on.
  1067. *
  1068. * Release a journal_t structure once it is no longer in use by the
  1069. * journaled object.
  1070. * Return <0 if we couldn't clean up the journal.
  1071. */
  1072. int journal_destroy(journal_t *journal)
  1073. {
  1074. int err = 0;
  1075. /* Wait for the commit thread to wake up and die. */
  1076. journal_kill_thread(journal);
  1077. /* Force a final log commit */
  1078. if (journal->j_running_transaction)
  1079. journal_commit_transaction(journal);
  1080. /* Force any old transactions to disk */
  1081. /* Totally anal locking here... */
  1082. spin_lock(&journal->j_list_lock);
  1083. while (journal->j_checkpoint_transactions != NULL) {
  1084. spin_unlock(&journal->j_list_lock);
  1085. log_do_checkpoint(journal);
  1086. spin_lock(&journal->j_list_lock);
  1087. }
  1088. J_ASSERT(journal->j_running_transaction == NULL);
  1089. J_ASSERT(journal->j_committing_transaction == NULL);
  1090. J_ASSERT(journal->j_checkpoint_transactions == NULL);
  1091. spin_unlock(&journal->j_list_lock);
  1092. if (journal->j_sb_buffer) {
  1093. if (!is_journal_aborted(journal)) {
  1094. /* We can now mark the journal as empty. */
  1095. journal->j_tail = 0;
  1096. journal->j_tail_sequence =
  1097. ++journal->j_transaction_sequence;
  1098. journal_update_superblock(journal, 1);
  1099. } else {
  1100. err = -EIO;
  1101. }
  1102. brelse(journal->j_sb_buffer);
  1103. }
  1104. if (journal->j_inode)
  1105. iput(journal->j_inode);
  1106. if (journal->j_revoke)
  1107. journal_destroy_revoke(journal);
  1108. kfree(journal->j_wbuf);
  1109. kfree(journal);
  1110. return err;
  1111. }
  1112. /**
  1113. *int journal_check_used_features () - Check if features specified are used.
  1114. * @journal: Journal to check.
  1115. * @compat: bitmask of compatible features
  1116. * @ro: bitmask of features that force read-only mount
  1117. * @incompat: bitmask of incompatible features
  1118. *
  1119. * Check whether the journal uses all of a given set of
  1120. * features. Return true (non-zero) if it does.
  1121. **/
  1122. int journal_check_used_features (journal_t *journal, unsigned long compat,
  1123. unsigned long ro, unsigned long incompat)
  1124. {
  1125. journal_superblock_t *sb;
  1126. if (!compat && !ro && !incompat)
  1127. return 1;
  1128. if (journal->j_format_version == 1)
  1129. return 0;
  1130. sb = journal->j_superblock;
  1131. if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
  1132. ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
  1133. ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
  1134. return 1;
  1135. return 0;
  1136. }
  1137. /**
  1138. * int journal_check_available_features() - Check feature set in journalling layer
  1139. * @journal: Journal to check.
  1140. * @compat: bitmask of compatible features
  1141. * @ro: bitmask of features that force read-only mount
  1142. * @incompat: bitmask of incompatible features
  1143. *
  1144. * Check whether the journaling code supports the use of
  1145. * all of a given set of features on this journal. Return true
  1146. * (non-zero) if it can. */
  1147. int journal_check_available_features (journal_t *journal, unsigned long compat,
  1148. unsigned long ro, unsigned long incompat)
  1149. {
  1150. if (!compat && !ro && !incompat)
  1151. return 1;
  1152. /* We can support any known requested features iff the
  1153. * superblock is in version 2. Otherwise we fail to support any
  1154. * extended sb features. */
  1155. if (journal->j_format_version != 2)
  1156. return 0;
  1157. if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
  1158. (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
  1159. (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
  1160. return 1;
  1161. return 0;
  1162. }
  1163. /**
  1164. * int journal_set_features () - Mark a given journal feature in the superblock
  1165. * @journal: Journal to act on.
  1166. * @compat: bitmask of compatible features
  1167. * @ro: bitmask of features that force read-only mount
  1168. * @incompat: bitmask of incompatible features
  1169. *
  1170. * Mark a given journal feature as present on the
  1171. * superblock. Returns true if the requested features could be set.
  1172. *
  1173. */
  1174. int journal_set_features (journal_t *journal, unsigned long compat,
  1175. unsigned long ro, unsigned long incompat)
  1176. {
  1177. journal_superblock_t *sb;
  1178. if (journal_check_used_features(journal, compat, ro, incompat))
  1179. return 1;
  1180. if (!journal_check_available_features(journal, compat, ro, incompat))
  1181. return 0;
  1182. jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
  1183. compat, ro, incompat);
  1184. sb = journal->j_superblock;
  1185. sb->s_feature_compat |= cpu_to_be32(compat);
  1186. sb->s_feature_ro_compat |= cpu_to_be32(ro);
  1187. sb->s_feature_incompat |= cpu_to_be32(incompat);
  1188. return 1;
  1189. }
  1190. /**
  1191. * int journal_update_format () - Update on-disk journal structure.
  1192. * @journal: Journal to act on.
  1193. *
  1194. * Given an initialised but unloaded journal struct, poke about in the
  1195. * on-disk structure to update it to the most recent supported version.
  1196. */
  1197. int journal_update_format (journal_t *journal)
  1198. {
  1199. journal_superblock_t *sb;
  1200. int err;
  1201. err = journal_get_superblock(journal);
  1202. if (err)
  1203. return err;
  1204. sb = journal->j_superblock;
  1205. switch (be32_to_cpu(sb->s_header.h_blocktype)) {
  1206. case JFS_SUPERBLOCK_V2:
  1207. return 0;
  1208. case JFS_SUPERBLOCK_V1:
  1209. return journal_convert_superblock_v1(journal, sb);
  1210. default:
  1211. break;
  1212. }
  1213. return -EINVAL;
  1214. }
  1215. static int journal_convert_superblock_v1(journal_t *journal,
  1216. journal_superblock_t *sb)
  1217. {
  1218. int offset, blocksize;
  1219. struct buffer_head *bh;
  1220. printk(KERN_WARNING
  1221. "JBD: Converting superblock from version 1 to 2.\n");
  1222. /* Pre-initialise new fields to zero */
  1223. offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
  1224. blocksize = be32_to_cpu(sb->s_blocksize);
  1225. memset(&sb->s_feature_compat, 0, blocksize-offset);
  1226. sb->s_nr_users = cpu_to_be32(1);
  1227. sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
  1228. journal->j_format_version = 2;
  1229. bh = journal->j_sb_buffer;
  1230. BUFFER_TRACE(bh, "marking dirty");
  1231. mark_buffer_dirty(bh);
  1232. sync_dirty_buffer(bh);
  1233. return 0;
  1234. }
  1235. /**
  1236. * int journal_flush () - Flush journal
  1237. * @journal: Journal to act on.
  1238. *
  1239. * Flush all data for a given journal to disk and empty the journal.
  1240. * Filesystems can use this when remounting readonly to ensure that
  1241. * recovery does not need to happen on remount.
  1242. */
  1243. int journal_flush(journal_t *journal)
  1244. {
  1245. int err = 0;
  1246. transaction_t *transaction = NULL;
  1247. unsigned int old_tail;
  1248. spin_lock(&journal->j_state_lock);
  1249. /* Force everything buffered to the log... */
  1250. if (journal->j_running_transaction) {
  1251. transaction = journal->j_running_transaction;
  1252. __log_start_commit(journal, transaction->t_tid);
  1253. } else if (journal->j_committing_transaction)
  1254. transaction = journal->j_committing_transaction;
  1255. /* Wait for the log commit to complete... */
  1256. if (transaction) {
  1257. tid_t tid = transaction->t_tid;
  1258. spin_unlock(&journal->j_state_lock);
  1259. log_wait_commit(journal, tid);
  1260. } else {
  1261. spin_unlock(&journal->j_state_lock);
  1262. }
  1263. /* ...and flush everything in the log out to disk. */
  1264. spin_lock(&journal->j_list_lock);
  1265. while (!err && journal->j_checkpoint_transactions != NULL) {
  1266. spin_unlock(&journal->j_list_lock);
  1267. mutex_lock(&journal->j_checkpoint_mutex);
  1268. err = log_do_checkpoint(journal);
  1269. mutex_unlock(&journal->j_checkpoint_mutex);
  1270. spin_lock(&journal->j_list_lock);
  1271. }
  1272. spin_unlock(&journal->j_list_lock);
  1273. if (is_journal_aborted(journal))
  1274. return -EIO;
  1275. cleanup_journal_tail(journal);
  1276. /* Finally, mark the journal as really needing no recovery.
  1277. * This sets s_start==0 in the underlying superblock, which is
  1278. * the magic code for a fully-recovered superblock. Any future
  1279. * commits of data to the journal will restore the current
  1280. * s_start value. */
  1281. spin_lock(&journal->j_state_lock);
  1282. old_tail = journal->j_tail;
  1283. journal->j_tail = 0;
  1284. spin_unlock(&journal->j_state_lock);
  1285. journal_update_superblock(journal, 1);
  1286. spin_lock(&journal->j_state_lock);
  1287. journal->j_tail = old_tail;
  1288. J_ASSERT(!journal->j_running_transaction);
  1289. J_ASSERT(!journal->j_committing_transaction);
  1290. J_ASSERT(!journal->j_checkpoint_transactions);
  1291. J_ASSERT(journal->j_head == journal->j_tail);
  1292. J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
  1293. spin_unlock(&journal->j_state_lock);
  1294. return 0;
  1295. }
  1296. /**
  1297. * int journal_wipe() - Wipe journal contents
  1298. * @journal: Journal to act on.
  1299. * @write: flag (see below)
  1300. *
  1301. * Wipe out all of the contents of a journal, safely. This will produce
  1302. * a warning if the journal contains any valid recovery information.
  1303. * Must be called between journal_init_*() and journal_load().
  1304. *
  1305. * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
  1306. * we merely suppress recovery.
  1307. */
  1308. int journal_wipe(journal_t *journal, int write)
  1309. {
  1310. int err = 0;
  1311. J_ASSERT (!(journal->j_flags & JFS_LOADED));
  1312. err = load_superblock(journal);
  1313. if (err)
  1314. return err;
  1315. if (!journal->j_tail)
  1316. goto no_recovery;
  1317. printk (KERN_WARNING "JBD: %s recovery information on journal\n",
  1318. write ? "Clearing" : "Ignoring");
  1319. err = journal_skip_recovery(journal);
  1320. if (write)
  1321. journal_update_superblock(journal, 1);
  1322. no_recovery:
  1323. return err;
  1324. }
  1325. /*
  1326. * journal_dev_name: format a character string to describe on what
  1327. * device this journal is present.
  1328. */
  1329. static const char *journal_dev_name(journal_t *journal, char *buffer)
  1330. {
  1331. struct block_device *bdev;
  1332. if (journal->j_inode)
  1333. bdev = journal->j_inode->i_sb->s_bdev;
  1334. else
  1335. bdev = journal->j_dev;
  1336. return bdevname(bdev, buffer);
  1337. }
  1338. /*
  1339. * Journal abort has very specific semantics, which we describe
  1340. * for journal abort.
  1341. *
  1342. * Two internal function, which provide abort to te jbd layer
  1343. * itself are here.
  1344. */
  1345. /*
  1346. * Quick version for internal journal use (doesn't lock the journal).
  1347. * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
  1348. * and don't attempt to make any other journal updates.
  1349. */
  1350. static void __journal_abort_hard(journal_t *journal)
  1351. {
  1352. transaction_t *transaction;
  1353. char b[BDEVNAME_SIZE];
  1354. if (journal->j_flags & JFS_ABORT)
  1355. return;
  1356. printk(KERN_ERR "Aborting journal on device %s.\n",
  1357. journal_dev_name(journal, b));
  1358. spin_lock(&journal->j_state_lock);
  1359. journal->j_flags |= JFS_ABORT;
  1360. transaction = journal->j_running_transaction;
  1361. if (transaction)
  1362. __log_start_commit(journal, transaction->t_tid);
  1363. spin_unlock(&journal->j_state_lock);
  1364. }
  1365. /* Soft abort: record the abort error status in the journal superblock,
  1366. * but don't do any other IO. */
  1367. static void __journal_abort_soft (journal_t *journal, int errno)
  1368. {
  1369. if (journal->j_flags & JFS_ABORT)
  1370. return;
  1371. if (!journal->j_errno)
  1372. journal->j_errno = errno;
  1373. __journal_abort_hard(journal);
  1374. if (errno)
  1375. journal_update_superblock(journal, 1);
  1376. }
  1377. /**
  1378. * void journal_abort () - Shutdown the journal immediately.
  1379. * @journal: the journal to shutdown.
  1380. * @errno: an error number to record in the journal indicating
  1381. * the reason for the shutdown.
  1382. *
  1383. * Perform a complete, immediate shutdown of the ENTIRE
  1384. * journal (not of a single transaction). This operation cannot be
  1385. * undone without closing and reopening the journal.
  1386. *
  1387. * The journal_abort function is intended to support higher level error
  1388. * recovery mechanisms such as the ext2/ext3 remount-readonly error
  1389. * mode.
  1390. *
  1391. * Journal abort has very specific semantics. Any existing dirty,
  1392. * unjournaled buffers in the main filesystem will still be written to
  1393. * disk by bdflush, but the journaling mechanism will be suspended
  1394. * immediately and no further transaction commits will be honoured.
  1395. *
  1396. * Any dirty, journaled buffers will be written back to disk without
  1397. * hitting the journal. Atomicity cannot be guaranteed on an aborted
  1398. * filesystem, but we _do_ attempt to leave as much data as possible
  1399. * behind for fsck to use for cleanup.
  1400. *
  1401. * Any attempt to get a new transaction handle on a journal which is in
  1402. * ABORT state will just result in an -EROFS error return. A
  1403. * journal_stop on an existing handle will return -EIO if we have
  1404. * entered abort state during the update.
  1405. *
  1406. * Recursive transactions are not disturbed by journal abort until the
  1407. * final journal_stop, which will receive the -EIO error.
  1408. *
  1409. * Finally, the journal_abort call allows the caller to supply an errno
  1410. * which will be recorded (if possible) in the journal superblock. This
  1411. * allows a client to record failure conditions in the middle of a
  1412. * transaction without having to complete the transaction to record the
  1413. * failure to disk. ext3_error, for example, now uses this
  1414. * functionality.
  1415. *
  1416. * Errors which originate from within the journaling layer will NOT
  1417. * supply an errno; a null errno implies that absolutely no further
  1418. * writes are done to the journal (unless there are any already in
  1419. * progress).
  1420. *
  1421. */
  1422. void journal_abort(journal_t *journal, int errno)
  1423. {
  1424. __journal_abort_soft(journal, errno);
  1425. }
  1426. /**
  1427. * int journal_errno () - returns the journal's error state.
  1428. * @journal: journal to examine.
  1429. *
  1430. * This is the errno numbet set with journal_abort(), the last
  1431. * time the journal was mounted - if the journal was stopped
  1432. * without calling abort this will be 0.
  1433. *
  1434. * If the journal has been aborted on this mount time -EROFS will
  1435. * be returned.
  1436. */
  1437. int journal_errno(journal_t *journal)
  1438. {
  1439. int err;
  1440. spin_lock(&journal->j_state_lock);
  1441. if (journal->j_flags & JFS_ABORT)
  1442. err = -EROFS;
  1443. else
  1444. err = journal->j_errno;
  1445. spin_unlock(&journal->j_state_lock);
  1446. return err;
  1447. }
  1448. /**
  1449. * int journal_clear_err () - clears the journal's error state
  1450. * @journal: journal to act on.
  1451. *
  1452. * An error must be cleared or Acked to take a FS out of readonly
  1453. * mode.
  1454. */
  1455. int journal_clear_err(journal_t *journal)
  1456. {
  1457. int err = 0;
  1458. spin_lock(&journal->j_state_lock);
  1459. if (journal->j_flags & JFS_ABORT)
  1460. err = -EROFS;
  1461. else
  1462. journal->j_errno = 0;
  1463. spin_unlock(&journal->j_state_lock);
  1464. return err;
  1465. }
  1466. /**
  1467. * void journal_ack_err() - Ack journal err.
  1468. * @journal: journal to act on.
  1469. *
  1470. * An error must be cleared or Acked to take a FS out of readonly
  1471. * mode.
  1472. */
  1473. void journal_ack_err(journal_t *journal)
  1474. {
  1475. spin_lock(&journal->j_state_lock);
  1476. if (journal->j_errno)
  1477. journal->j_flags |= JFS_ACK_ERR;
  1478. spin_unlock(&journal->j_state_lock);
  1479. }
  1480. int journal_blocks_per_page(struct inode *inode)
  1481. {
  1482. return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  1483. }
  1484. /*
  1485. * Journal_head storage management
  1486. */
  1487. static struct kmem_cache *journal_head_cache;
  1488. #ifdef CONFIG_JBD_DEBUG
  1489. static atomic_t nr_journal_heads = ATOMIC_INIT(0);
  1490. #endif
  1491. static int journal_init_journal_head_cache(void)
  1492. {
  1493. int retval;
  1494. J_ASSERT(journal_head_cache == NULL);
  1495. journal_head_cache = kmem_cache_create("journal_head",
  1496. sizeof(struct journal_head),
  1497. 0, /* offset */
  1498. SLAB_TEMPORARY, /* flags */
  1499. NULL); /* ctor */
  1500. retval = 0;
  1501. if (!journal_head_cache) {
  1502. retval = -ENOMEM;
  1503. printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
  1504. }
  1505. return retval;
  1506. }
  1507. static void journal_destroy_journal_head_cache(void)
  1508. {
  1509. if (journal_head_cache) {
  1510. kmem_cache_destroy(journal_head_cache);
  1511. journal_head_cache = NULL;
  1512. }
  1513. }
  1514. /*
  1515. * journal_head splicing and dicing
  1516. */
  1517. static struct journal_head *journal_alloc_journal_head(void)
  1518. {
  1519. struct journal_head *ret;
  1520. #ifdef CONFIG_JBD_DEBUG
  1521. atomic_inc(&nr_journal_heads);
  1522. #endif
  1523. ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
  1524. if (ret == NULL) {
  1525. jbd_debug(1, "out of memory for journal_head\n");
  1526. printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n",
  1527. __func__);
  1528. while (ret == NULL) {
  1529. yield();
  1530. ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
  1531. }
  1532. }
  1533. return ret;
  1534. }
  1535. static void journal_free_journal_head(struct journal_head *jh)
  1536. {
  1537. #ifdef CONFIG_JBD_DEBUG
  1538. atomic_dec(&nr_journal_heads);
  1539. memset(jh, JBD_POISON_FREE, sizeof(*jh));
  1540. #endif
  1541. kmem_cache_free(journal_head_cache, jh);
  1542. }
  1543. /*
  1544. * A journal_head is attached to a buffer_head whenever JBD has an
  1545. * interest in the buffer.
  1546. *
  1547. * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
  1548. * is set. This bit is tested in core kernel code where we need to take
  1549. * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
  1550. * there.
  1551. *
  1552. * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
  1553. *
  1554. * When a buffer has its BH_JBD bit set it is immune from being released by
  1555. * core kernel code, mainly via ->b_count.
  1556. *
  1557. * A journal_head may be detached from its buffer_head when the journal_head's
  1558. * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
  1559. * Various places in JBD call journal_remove_journal_head() to indicate that the
  1560. * journal_head can be dropped if needed.
  1561. *
  1562. * Various places in the kernel want to attach a journal_head to a buffer_head
  1563. * _before_ attaching the journal_head to a transaction. To protect the
  1564. * journal_head in this situation, journal_add_journal_head elevates the
  1565. * journal_head's b_jcount refcount by one. The caller must call
  1566. * journal_put_journal_head() to undo this.
  1567. *
  1568. * So the typical usage would be:
  1569. *
  1570. * (Attach a journal_head if needed. Increments b_jcount)
  1571. * struct journal_head *jh = journal_add_journal_head(bh);
  1572. * ...
  1573. * jh->b_transaction = xxx;
  1574. * journal_put_journal_head(jh);
  1575. *
  1576. * Now, the journal_head's b_jcount is zero, but it is safe from being released
  1577. * because it has a non-zero b_transaction.
  1578. */
  1579. /*
  1580. * Give a buffer_head a journal_head.
  1581. *
  1582. * Doesn't need the journal lock.
  1583. * May sleep.
  1584. */
  1585. struct journal_head *journal_add_journal_head(struct buffer_head *bh)
  1586. {
  1587. struct journal_head *jh;
  1588. struct journal_head *new_jh = NULL;
  1589. repeat:
  1590. if (!buffer_jbd(bh)) {
  1591. new_jh = journal_alloc_journal_head();
  1592. memset(new_jh, 0, sizeof(*new_jh));
  1593. }
  1594. jbd_lock_bh_journal_head(bh);
  1595. if (buffer_jbd(bh)) {
  1596. jh = bh2jh(bh);
  1597. } else {
  1598. J_ASSERT_BH(bh,
  1599. (atomic_read(&bh->b_count) > 0) ||
  1600. (bh->b_page && bh->b_page->mapping));
  1601. if (!new_jh) {
  1602. jbd_unlock_bh_journal_head(bh);
  1603. goto repeat;
  1604. }
  1605. jh = new_jh;
  1606. new_jh = NULL; /* We consumed it */
  1607. set_buffer_jbd(bh);
  1608. bh->b_private = jh;
  1609. jh->b_bh = bh;
  1610. get_bh(bh);
  1611. BUFFER_TRACE(bh, "added journal_head");
  1612. }
  1613. jh->b_jcount++;
  1614. jbd_unlock_bh_journal_head(bh);
  1615. if (new_jh)
  1616. journal_free_journal_head(new_jh);
  1617. return bh->b_private;
  1618. }
  1619. /*
  1620. * Grab a ref against this buffer_head's journal_head. If it ended up not
  1621. * having a journal_head, return NULL
  1622. */
  1623. struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
  1624. {
  1625. struct journal_head *jh = NULL;
  1626. jbd_lock_bh_journal_head(bh);
  1627. if (buffer_jbd(bh)) {
  1628. jh = bh2jh(bh);
  1629. jh->b_jcount++;
  1630. }
  1631. jbd_unlock_bh_journal_head(bh);
  1632. return jh;
  1633. }
  1634. static void __journal_remove_journal_head(struct buffer_head *bh)
  1635. {
  1636. struct journal_head *jh = bh2jh(bh);
  1637. J_ASSERT_JH(jh, jh->b_jcount >= 0);
  1638. get_bh(bh);
  1639. if (jh->b_jcount == 0) {
  1640. if (jh->b_transaction == NULL &&
  1641. jh->b_next_transaction == NULL &&
  1642. jh->b_cp_transaction == NULL) {
  1643. J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
  1644. J_ASSERT_BH(bh, buffer_jbd(bh));
  1645. J_ASSERT_BH(bh, jh2bh(jh) == bh);
  1646. BUFFER_TRACE(bh, "remove journal_head");
  1647. if (jh->b_frozen_data) {
  1648. printk(KERN_WARNING "%s: freeing "
  1649. "b_frozen_data\n",
  1650. __func__);
  1651. jbd_free(jh->b_frozen_data, bh->b_size);
  1652. }
  1653. if (jh->b_committed_data) {
  1654. printk(KERN_WARNING "%s: freeing "
  1655. "b_committed_data\n",
  1656. __func__);
  1657. jbd_free(jh->b_committed_data, bh->b_size);
  1658. }
  1659. bh->b_private = NULL;
  1660. jh->b_bh = NULL; /* debug, really */
  1661. clear_buffer_jbd(bh);
  1662. __brelse(bh);
  1663. journal_free_journal_head(jh);
  1664. } else {
  1665. BUFFER_TRACE(bh, "journal_head was locked");
  1666. }
  1667. }
  1668. }
  1669. /*
  1670. * journal_remove_journal_head(): if the buffer isn't attached to a transaction
  1671. * and has a zero b_jcount then remove and release its journal_head. If we did
  1672. * see that the buffer is not used by any transaction we also "logically"
  1673. * decrement ->b_count.
  1674. *
  1675. * We in fact take an additional increment on ->b_count as a convenience,
  1676. * because the caller usually wants to do additional things with the bh
  1677. * after calling here.
  1678. * The caller of journal_remove_journal_head() *must* run __brelse(bh) at some
  1679. * time. Once the caller has run __brelse(), the buffer is eligible for
  1680. * reaping by try_to_free_buffers().
  1681. */
  1682. void journal_remove_journal_head(struct buffer_head *bh)
  1683. {
  1684. jbd_lock_bh_journal_head(bh);
  1685. __journal_remove_journal_head(bh);
  1686. jbd_unlock_bh_journal_head(bh);
  1687. }
  1688. /*
  1689. * Drop a reference on the passed journal_head. If it fell to zero then try to
  1690. * release the journal_head from the buffer_head.
  1691. */
  1692. void journal_put_journal_head(struct journal_head *jh)
  1693. {
  1694. struct buffer_head *bh = jh2bh(jh);
  1695. jbd_lock_bh_journal_head(bh);
  1696. J_ASSERT_JH(jh, jh->b_jcount > 0);
  1697. --jh->b_jcount;
  1698. if (!jh->b_jcount && !jh->b_transaction) {
  1699. __journal_remove_journal_head(bh);
  1700. __brelse(bh);
  1701. }
  1702. jbd_unlock_bh_journal_head(bh);
  1703. }
  1704. /*
  1705. * debugfs tunables
  1706. */
  1707. #ifdef CONFIG_JBD_DEBUG
  1708. u8 journal_enable_debug __read_mostly;
  1709. EXPORT_SYMBOL(journal_enable_debug);
  1710. static struct dentry *jbd_debugfs_dir;
  1711. static struct dentry *jbd_debug;
  1712. static void __init jbd_create_debugfs_entry(void)
  1713. {
  1714. jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
  1715. if (jbd_debugfs_dir)
  1716. jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
  1717. jbd_debugfs_dir,
  1718. &journal_enable_debug);
  1719. }
  1720. static void __exit jbd_remove_debugfs_entry(void)
  1721. {
  1722. debugfs_remove(jbd_debug);
  1723. debugfs_remove(jbd_debugfs_dir);
  1724. }
  1725. #else
  1726. static inline void jbd_create_debugfs_entry(void)
  1727. {
  1728. }
  1729. static inline void jbd_remove_debugfs_entry(void)
  1730. {
  1731. }
  1732. #endif
  1733. struct kmem_cache *jbd_handle_cache;
  1734. static int __init journal_init_handle_cache(void)
  1735. {
  1736. jbd_handle_cache = kmem_cache_create("journal_handle",
  1737. sizeof(handle_t),
  1738. 0, /* offset */
  1739. SLAB_TEMPORARY, /* flags */
  1740. NULL); /* ctor */
  1741. if (jbd_handle_cache == NULL) {
  1742. printk(KERN_EMERG "JBD: failed to create handle cache\n");
  1743. return -ENOMEM;
  1744. }
  1745. return 0;
  1746. }
  1747. static void journal_destroy_handle_cache(void)
  1748. {
  1749. if (jbd_handle_cache)
  1750. kmem_cache_destroy(jbd_handle_cache);
  1751. }
  1752. /*
  1753. * Module startup and shutdown
  1754. */
  1755. static int __init journal_init_caches(void)
  1756. {
  1757. int ret;
  1758. ret = journal_init_revoke_caches();
  1759. if (ret == 0)
  1760. ret = journal_init_journal_head_cache();
  1761. if (ret == 0)
  1762. ret = journal_init_handle_cache();
  1763. return ret;
  1764. }
  1765. static void journal_destroy_caches(void)
  1766. {
  1767. journal_destroy_revoke_caches();
  1768. journal_destroy_journal_head_cache();
  1769. journal_destroy_handle_cache();
  1770. }
  1771. static int __init journal_init(void)
  1772. {
  1773. int ret;
  1774. BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
  1775. ret = journal_init_caches();
  1776. if (ret != 0)
  1777. journal_destroy_caches();
  1778. jbd_create_debugfs_entry();
  1779. return ret;
  1780. }
  1781. static void __exit journal_exit(void)
  1782. {
  1783. #ifdef CONFIG_JBD_DEBUG
  1784. int n = atomic_read(&nr_journal_heads);
  1785. if (n)
  1786. printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
  1787. #endif
  1788. jbd_remove_debugfs_entry();
  1789. journal_destroy_caches();
  1790. }
  1791. MODULE_LICENSE("GPL");
  1792. module_init(journal_init);
  1793. module_exit(journal_exit);