journal.c 64 KB

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