journal.c 65 KB

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