journal.c 63 KB

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