journal.c 72 KB

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