journal.c 74 KB

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