journal.c (aa7eb8e78d8ecd6cd0475d86ea8385ff9cb47ece) journal.c (60ad4466821a96913a9b567115e194ed1087c2d7)
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

--- 2064 unchanged lines hidden (view full) ---

2073 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2074 * there.
2075 *
2076 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2077 *
2078 * When a buffer has its BH_JBD bit set it is immune from being released by
2079 * core kernel code, mainly via ->b_count.
2080 *
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

--- 2064 unchanged lines hidden (view full) ---

2073 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2074 * there.
2075 *
2076 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2077 *
2078 * When a buffer has its BH_JBD bit set it is immune from being released by
2079 * core kernel code, mainly via ->b_count.
2080 *
2081 * A journal_head may be detached from its buffer_head when the journal_head's
2082 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
2083 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
2084 * journal_head can be dropped if needed.
2081 * A journal_head is detached from its buffer_head when the journal_head's
2082 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2083 * transaction (b_cp_transaction) hold their references to b_jcount.
2085 *
2086 * Various places in the kernel want to attach a journal_head to a buffer_head
2087 * _before_ attaching the journal_head to a transaction. To protect the
2088 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2089 * journal_head's b_jcount refcount by one. The caller must call
2090 * jbd2_journal_put_journal_head() to undo this.
2091 *
2092 * So the typical usage would be:
2093 *
2094 * (Attach a journal_head if needed. Increments b_jcount)
2095 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2096 * ...
2084 *
2085 * Various places in the kernel want to attach a journal_head to a buffer_head
2086 * _before_ attaching the journal_head to a transaction. To protect the
2087 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2088 * journal_head's b_jcount refcount by one. The caller must call
2089 * jbd2_journal_put_journal_head() to undo this.
2090 *
2091 * So the typical usage would be:
2092 *
2093 * (Attach a journal_head if needed. Increments b_jcount)
2094 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2095 * ...
2096 * (Get another reference for transaction)
2097 * jbd2_journal_grab_journal_head(bh);
2097 * jh->b_transaction = xxx;
2098 * jh->b_transaction = xxx;
2099 * (Put original reference)
2098 * jbd2_journal_put_journal_head(jh);
2100 * jbd2_journal_put_journal_head(jh);
2099 *
2100 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2101 * because it has a non-zero b_transaction.
2102 */
2103
2104/*
2105 * Give a buffer_head a journal_head.
2106 *
2101 */
2102
2103/*
2104 * Give a buffer_head a journal_head.
2105 *
2107 * Doesn't need the journal lock.
2108 * May sleep.
2109 */
2110struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2111{
2112 struct journal_head *jh;
2113 struct journal_head *new_jh = NULL;
2114
2115repeat:

--- 47 unchanged lines hidden (view full) ---

2163 return jh;
2164}
2165
2166static void __journal_remove_journal_head(struct buffer_head *bh)
2167{
2168 struct journal_head *jh = bh2jh(bh);
2169
2170 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2106 * May sleep.
2107 */
2108struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2109{
2110 struct journal_head *jh;
2111 struct journal_head *new_jh = NULL;
2112
2113repeat:

--- 47 unchanged lines hidden (view full) ---

2161 return jh;
2162}
2163
2164static void __journal_remove_journal_head(struct buffer_head *bh)
2165{
2166 struct journal_head *jh = bh2jh(bh);
2167
2168 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2171
2172 get_bh(bh);
2173 if (jh->b_jcount == 0) {
2174 if (jh->b_transaction == NULL &&
2175 jh->b_next_transaction == NULL &&
2176 jh->b_cp_transaction == NULL) {
2177 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2178 J_ASSERT_BH(bh, buffer_jbd(bh));
2179 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2180 BUFFER_TRACE(bh, "remove journal_head");
2181 if (jh->b_frozen_data) {
2182 printk(KERN_WARNING "%s: freeing "
2183 "b_frozen_data\n",
2184 __func__);
2185 jbd2_free(jh->b_frozen_data, bh->b_size);
2186 }
2187 if (jh->b_committed_data) {
2188 printk(KERN_WARNING "%s: freeing "
2189 "b_committed_data\n",
2190 __func__);
2191 jbd2_free(jh->b_committed_data, bh->b_size);
2192 }
2193 bh->b_private = NULL;
2194 jh->b_bh = NULL; /* debug, really */
2195 clear_buffer_jbd(bh);
2196 __brelse(bh);
2197 journal_free_journal_head(jh);
2198 } else {
2199 BUFFER_TRACE(bh, "journal_head was locked");
2200 }
2169 J_ASSERT_JH(jh, jh->b_transaction == NULL);
2170 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2171 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2172 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2173 J_ASSERT_BH(bh, buffer_jbd(bh));
2174 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2175 BUFFER_TRACE(bh, "remove journal_head");
2176 if (jh->b_frozen_data) {
2177 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2178 jbd2_free(jh->b_frozen_data, bh->b_size);
2201 }
2179 }
2180 if (jh->b_committed_data) {
2181 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2182 jbd2_free(jh->b_committed_data, bh->b_size);
2183 }
2184 bh->b_private = NULL;
2185 jh->b_bh = NULL; /* debug, really */
2186 clear_buffer_jbd(bh);
2187 journal_free_journal_head(jh);
2202}
2203
2204/*
2188}
2189
2190/*
2205 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
2206 * and has a zero b_jcount then remove and release its journal_head. If we did
2207 * see that the buffer is not used by any transaction we also "logically"
2208 * decrement ->b_count.
2209 *
2210 * We in fact take an additional increment on ->b_count as a convenience,
2211 * because the caller usually wants to do additional things with the bh
2212 * after calling here.
2213 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
2214 * time. Once the caller has run __brelse(), the buffer is eligible for
2215 * reaping by try_to_free_buffers().
2216 */
2217void jbd2_journal_remove_journal_head(struct buffer_head *bh)
2218{
2219 jbd_lock_bh_journal_head(bh);
2220 __journal_remove_journal_head(bh);
2221 jbd_unlock_bh_journal_head(bh);
2222}
2223
2224/*
2225 * Drop a reference on the passed journal_head. If it fell to zero then try to
2191 * Drop a reference on the passed journal_head. If it fell to zero then
2226 * release the journal_head from the buffer_head.
2227 */
2228void jbd2_journal_put_journal_head(struct journal_head *jh)
2229{
2230 struct buffer_head *bh = jh2bh(jh);
2231
2232 jbd_lock_bh_journal_head(bh);
2233 J_ASSERT_JH(jh, jh->b_jcount > 0);
2234 --jh->b_jcount;
2192 * release the journal_head from the buffer_head.
2193 */
2194void jbd2_journal_put_journal_head(struct journal_head *jh)
2195{
2196 struct buffer_head *bh = jh2bh(jh);
2197
2198 jbd_lock_bh_journal_head(bh);
2199 J_ASSERT_JH(jh, jh->b_jcount > 0);
2200 --jh->b_jcount;
2235 if (!jh->b_jcount && !jh->b_transaction) {
2201 if (!jh->b_jcount) {
2236 __journal_remove_journal_head(bh);
2202 __journal_remove_journal_head(bh);
2203 jbd_unlock_bh_journal_head(bh);
2237 __brelse(bh);
2204 __brelse(bh);
2238 }
2239 jbd_unlock_bh_journal_head(bh);
2205 } else
2206 jbd_unlock_bh_journal_head(bh);
2240}
2241
2242/*
2243 * Initialize jbd inode head
2244 */
2245void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2246{
2247 jinode->i_transaction = NULL;

--- 170 unchanged lines hidden (view full) ---

2418 if (n)
2419 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2420#endif
2421 jbd2_remove_debugfs_entry();
2422 jbd2_remove_jbd_stats_proc_entry();
2423 jbd2_journal_destroy_caches();
2424}
2425
2207}
2208
2209/*
2210 * Initialize jbd inode head
2211 */
2212void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2213{
2214 jinode->i_transaction = NULL;

--- 170 unchanged lines hidden (view full) ---

2385 if (n)
2386 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2387#endif
2388 jbd2_remove_debugfs_entry();
2389 jbd2_remove_jbd_stats_proc_entry();
2390 jbd2_journal_destroy_caches();
2391}
2392
2426/*
2427 * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
2428 * tracing infrastructure to map a dev_t to a device name.
2429 *
2430 * The caller should use rcu_read_lock() in order to make sure the
2431 * device name stays valid until its done with it. We use
2432 * rcu_read_lock() as well to make sure we're safe in case the caller
2433 * gets sloppy, and because rcu_read_lock() is cheap and can be safely
2434 * nested.
2435 */
2436struct devname_cache {
2437 struct rcu_head rcu;
2438 dev_t device;
2439 char devname[BDEVNAME_SIZE];
2440};
2441#define CACHE_SIZE_BITS 6
2442static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
2443static DEFINE_SPINLOCK(devname_cache_lock);
2444
2445static void free_devcache(struct rcu_head *rcu)
2446{
2447 kfree(rcu);
2448}
2449
2450const char *jbd2_dev_to_name(dev_t device)
2451{
2452 int i = hash_32(device, CACHE_SIZE_BITS);
2453 char *ret;
2454 struct block_device *bd;
2455 static struct devname_cache *new_dev;
2456
2457 rcu_read_lock();
2458 if (devcache[i] && devcache[i]->device == device) {
2459 ret = devcache[i]->devname;
2460 rcu_read_unlock();
2461 return ret;
2462 }
2463 rcu_read_unlock();
2464
2465 new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
2466 if (!new_dev)
2467 return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
2468 bd = bdget(device);
2469 spin_lock(&devname_cache_lock);
2470 if (devcache[i]) {
2471 if (devcache[i]->device == device) {
2472 kfree(new_dev);
2473 bdput(bd);
2474 ret = devcache[i]->devname;
2475 spin_unlock(&devname_cache_lock);
2476 return ret;
2477 }
2478 call_rcu(&devcache[i]->rcu, free_devcache);
2479 }
2480 devcache[i] = new_dev;
2481 devcache[i]->device = device;
2482 if (bd) {
2483 bdevname(bd, devcache[i]->devname);
2484 bdput(bd);
2485 } else
2486 __bdevname(device, devcache[i]->devname);
2487 ret = devcache[i]->devname;
2488 spin_unlock(&devname_cache_lock);
2489 return ret;
2490}
2491EXPORT_SYMBOL(jbd2_dev_to_name);
2492
2493MODULE_LICENSE("GPL");
2494module_init(journal_init);
2495module_exit(journal_exit);
2496
2393MODULE_LICENSE("GPL");
2394module_init(journal_init);
2395module_exit(journal_exit);
2396