journal.c 56 KB

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