journal.c 76 KB

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