journal.c 65 KB

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