journal.c 65 KB

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