journal.c 64 KB

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