journal.c 73 KB

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