journal.c 63 KB

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