Re: [PATCH] Fix setting bio flags in drivers (sd_dif/floppy).

From: Muthu Kumar
Date: Thu Mar 01 2012 - 15:23:14 EST


>>                       kunmap_atomic(sdt, KM_USER0);
>>               }
>>
>> -             bio->bi_flags |= BIO_MAPPED_INTEGRITY;
>> +             bio->bi_flags |= (1 << BIO_MAPPED_INTEGRITY);
>>       }
>>
>>       return 0;
>
> urgh.  This isn't the first time.
>
> It's too easy for people to make this mistake.  I'm not sure what a
> good fix would be - I don't think sparse can save us with __bitwise or
> similar.
>
> The approach we took in buffer_head.h with BH_Foo and BUFFER_FNS
> accessors worked pretty well.
>

Does this look good? BTW, I used the non-atomic variants __set/__clear
to match the behavior of current usage.
If this looks good, I can send the proper patch as attachment (so no
line wraps :)


diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h
index 4053cbd..8b2fbbb 100644
--- a/include/linux/blk_types.h
+++ b/include/linux/blk_types.h
@@ -8,6 +8,7 @@
#ifdef CONFIG_BLOCK

#include <linux/types.h>
+#include <linux/bitops.h>

struct bio_set;
struct bio;
@@ -98,6 +99,43 @@ struct bio {
#define bio_flagged(bio, flag) ((bio)->bi_flags & (1 << (flag)))

/*
+ * Macro tricks similar to setting b_state in buffer_head.h.
+ * To set uptodate flag in a bio:
+ * bio_flags_set_uptodate(bio);
+ * To clear
+ * bio_flags_clear_uptodate(bio);
+ * To test if it is set
+ * bio_flagged_uptodate(bio);
+ */
+#define BIO_FLAGS_FNS(bit, name) \
+static inline void bio_flags_set_##name(struct bio *bio) \
+{ \
+ __set_bit(BIO_##bit, &(bio)->bi_flags); \
+} \
+static inline void bio_flags_clear_##name(struct bio *bio) \
+{ \
+ __clear_bit(BIO_##bit, &(bio)->bi_flags); \
+} \
+static inline int bio_flagged_##name(const struct bio *bio) \
+{ \
+ return test_bit(BIO_##bit, &(bio)->bi_flags); \
+} \
+
+BIO_FLAGS_FNS(UPTODATE, uptodate)
+BIO_FLAGS_FNS(RW_BLOCK, rw_block)
+BIO_FLAGS_FNS(EOF, eof)
+BIO_FLAGS_FNS(SEG_VALID, seg_valid)
+BIO_FLAGS_FNS(CLONED, cloned)
+BIO_FLAGS_FNS(BOUNCED, bounced)
+BIO_FLAGS_FNS(USER_MAPPED, user_mapped)
+BIO_FLAGS_FNS(EOPNOTSUPP, eopnotsupp)
+BIO_FLAGS_FNS(NULL_MAPPED, null_mapped)
+BIO_FLAGS_FNS(FS_INTEGRITY, fs_integrity)
+BIO_FLAGS_FNS(QUIET, quiet)
+BIO_FLAGS_FNS(MAPPED_INTEGRITY, mapped_integrity)
+
+/*
* top 4 bits of bio flags indicate the pool this bio came from
*/
#define BIO_POOL_BITS (4)
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/