journal.c 75 KB

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