journal.c 71 KB

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