journal.c 74 KB

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