journal.c 67 KB

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