blob: c3bdf8c594800a49101d2974d91172bcd4686871 [file] [log] [blame]
Vishal Verma9e0e2522015-12-24 19:20:32 -07001#ifndef _LINUX_BADBLOCKS_H
2#define _LINUX_BADBLOCKS_H
3
4#include <linux/seqlock.h>
Dan Williams16263ff2016-01-04 23:50:23 -08005#include <linux/device.h>
Vishal Verma9e0e2522015-12-24 19:20:32 -07006#include <linux/kernel.h>
7#include <linux/stddef.h>
8#include <linux/types.h>
9
10#define BB_LEN_MASK (0x00000000000001FFULL)
11#define BB_OFFSET_MASK (0x7FFFFFFFFFFFFE00ULL)
12#define BB_ACK_MASK (0x8000000000000000ULL)
13#define BB_MAX_LEN 512
14#define BB_OFFSET(x) (((x) & BB_OFFSET_MASK) >> 9)
15#define BB_LEN(x) (((x) & BB_LEN_MASK) + 1)
16#define BB_ACK(x) (!!((x) & BB_ACK_MASK))
17#define BB_MAKE(a, l, ack) (((a)<<9) | ((l)-1) | ((u64)(!!(ack)) << 63))
18
19/* Bad block numbers are stored sorted in a single page.
20 * 64bits is used for each block or extent.
21 * 54 bits are sector number, 9 bits are extent size,
22 * 1 bit is an 'acknowledged' flag.
23 */
24#define MAX_BADBLOCKS (PAGE_SIZE/8)
25
26struct badblocks {
Dan Williams16263ff2016-01-04 23:50:23 -080027 struct device *dev; /* set by devm_init_badblocks */
Vishal Verma9e0e2522015-12-24 19:20:32 -070028 int count; /* count of bad blocks */
29 int unacked_exist; /* there probably are unacknowledged
30 * bad blocks. This is only cleared
31 * when a read discovers none
32 */
33 int shift; /* shift from sectors to block size
34 * a -ve shift means badblocks are
35 * disabled.*/
36 u64 *page; /* badblock list */
37 int changed;
38 seqlock_t lock;
39 sector_t sector;
40 sector_t size; /* in sectors */
41};
42
43int badblocks_check(struct badblocks *bb, sector_t s, int sectors,
44 sector_t *first_bad, int *bad_sectors);
45int badblocks_set(struct badblocks *bb, sector_t s, int sectors,
46 int acknowledged);
47int badblocks_clear(struct badblocks *bb, sector_t s, int sectors);
48void ack_all_badblocks(struct badblocks *bb);
49ssize_t badblocks_show(struct badblocks *bb, char *page, int unack);
50ssize_t badblocks_store(struct badblocks *bb, const char *page, size_t len,
51 int unack);
52int badblocks_init(struct badblocks *bb, int enable);
Dan Williamsd3b407f2016-01-06 12:19:22 -080053void badblocks_exit(struct badblocks *bb);
Dan Williams16263ff2016-01-04 23:50:23 -080054struct device;
55int devm_init_badblocks(struct device *dev, struct badblocks *bb);
56static inline void devm_exit_badblocks(struct device *dev, struct badblocks *bb)
57{
58 if (bb->dev != dev) {
59 dev_WARN_ONCE(dev, 1, "%s: badblocks instance not associated\n",
60 __func__);
61 return;
62 }
63 badblocks_exit(bb);
64}
Vishal Verma9e0e2522015-12-24 19:20:32 -070065#endif