journal.c 65 KB

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