journal.c 74 KB

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