journal.c 73 KB

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