journal.c 66 KB

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