journal.c 57 KB

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