journal.c 75 KB

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