journal.c 57 KB

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