journal.c 66 KB

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