journal.c 54 KB

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