journal.c 65 KB

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