journal.c 58 KB

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