md.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. md_k.h : kernel internal structure of the Linux MD driver
  3. Copyright (C) 1996-98 Ingo Molnar, Gadi Oxman
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. You should have received a copy of the GNU General Public License
  9. (for example /usr/src/linux/COPYING); if not, write to the Free
  10. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  11. */
  12. #ifndef _MD_MD_H
  13. #define _MD_MD_H
  14. #include <linux/blkdev.h>
  15. #include <linux/kobject.h>
  16. #include <linux/list.h>
  17. #include <linux/mm.h>
  18. #include <linux/mutex.h>
  19. #include <linux/timer.h>
  20. #include <linux/wait.h>
  21. #include <linux/workqueue.h>
  22. #define MaxSector (~(sector_t)0)
  23. typedef struct mddev_s mddev_t;
  24. typedef struct mdk_rdev_s mdk_rdev_t;
  25. /*
  26. * MD's 'extended' device
  27. */
  28. struct mdk_rdev_s
  29. {
  30. struct list_head same_set; /* RAID devices within the same set */
  31. sector_t sectors; /* Device size (in 512bytes sectors) */
  32. mddev_t *mddev; /* RAID array if running */
  33. int last_events; /* IO event timestamp */
  34. /*
  35. * If meta_bdev is non-NULL, it means that a separate device is
  36. * being used to store the metadata (superblock/bitmap) which
  37. * would otherwise be contained on the same device as the data (bdev).
  38. */
  39. struct block_device *meta_bdev;
  40. struct block_device *bdev; /* block device handle */
  41. struct page *sb_page;
  42. int sb_loaded;
  43. __u64 sb_events;
  44. sector_t data_offset; /* start of data in array */
  45. sector_t sb_start; /* offset of the super block (in 512byte sectors) */
  46. int sb_size; /* bytes in the superblock */
  47. int preferred_minor; /* autorun support */
  48. struct kobject kobj;
  49. /* A device can be in one of three states based on two flags:
  50. * Not working: faulty==1 in_sync==0
  51. * Fully working: faulty==0 in_sync==1
  52. * Working, but not
  53. * in sync with array
  54. * faulty==0 in_sync==0
  55. *
  56. * It can never have faulty==1, in_sync==1
  57. * This reduces the burden of testing multiple flags in many cases
  58. */
  59. unsigned long flags;
  60. #define Faulty 1 /* device is known to have a fault */
  61. #define In_sync 2 /* device is in_sync with rest of array */
  62. #define WriteMostly 4 /* Avoid reading if at all possible */
  63. #define AutoDetected 7 /* added by auto-detect */
  64. #define Blocked 8 /* An error occurred on an externally
  65. * managed array, don't allow writes
  66. * until it is cleared */
  67. wait_queue_head_t blocked_wait;
  68. int desc_nr; /* descriptor index in the superblock */
  69. int raid_disk; /* role of device in array */
  70. int new_raid_disk; /* role that the device will have in
  71. * the array after a level-change completes.
  72. */
  73. int saved_raid_disk; /* role that device used to have in the
  74. * array and could again if we did a partial
  75. * resync from the bitmap
  76. */
  77. sector_t recovery_offset;/* If this device has been partially
  78. * recovered, this is where we were
  79. * up to.
  80. */
  81. atomic_t nr_pending; /* number of pending requests.
  82. * only maintained for arrays that
  83. * support hot removal
  84. */
  85. atomic_t read_errors; /* number of consecutive read errors that
  86. * we have tried to ignore.
  87. */
  88. struct timespec last_read_error; /* monotonic time since our
  89. * last read error
  90. */
  91. atomic_t corrected_errors; /* number of corrected read errors,
  92. * for reporting to userspace and storing
  93. * in superblock.
  94. */
  95. struct work_struct del_work; /* used for delayed sysfs removal */
  96. struct sysfs_dirent *sysfs_state; /* handle for 'state'
  97. * sysfs entry */
  98. };
  99. struct mddev_s
  100. {
  101. void *private;
  102. struct mdk_personality *pers;
  103. dev_t unit;
  104. int md_minor;
  105. struct list_head disks;
  106. unsigned long flags;
  107. #define MD_CHANGE_DEVS 0 /* Some device status has changed */
  108. #define MD_CHANGE_CLEAN 1 /* transition to or from 'clean' */
  109. #define MD_CHANGE_PENDING 2 /* switch from 'clean' to 'active' in progress */
  110. #define MD_ARRAY_FIRST_USE 3 /* First use of array, needs initialization */
  111. int suspended;
  112. atomic_t active_io;
  113. int ro;
  114. int sysfs_active; /* set when sysfs deletes
  115. * are happening, so run/
  116. * takeover/stop are not safe
  117. */
  118. int ready; /* See when safe to pass
  119. * IO requests down */
  120. struct gendisk *gendisk;
  121. struct kobject kobj;
  122. int hold_active;
  123. #define UNTIL_IOCTL 1
  124. #define UNTIL_STOP 2
  125. /* Superblock information */
  126. int major_version,
  127. minor_version,
  128. patch_version;
  129. int persistent;
  130. int external; /* metadata is
  131. * managed externally */
  132. char metadata_type[17]; /* externally set*/
  133. int chunk_sectors;
  134. time_t ctime, utime;
  135. int level, layout;
  136. char clevel[16];
  137. int raid_disks;
  138. int max_disks;
  139. sector_t dev_sectors; /* used size of
  140. * component devices */
  141. sector_t array_sectors; /* exported array size */
  142. int external_size; /* size managed
  143. * externally */
  144. __u64 events;
  145. /* If the last 'event' was simply a clean->dirty transition, and
  146. * we didn't write it to the spares, then it is safe and simple
  147. * to just decrement the event count on a dirty->clean transition.
  148. * So we record that possibility here.
  149. */
  150. int can_decrease_events;
  151. char uuid[16];
  152. /* If the array is being reshaped, we need to record the
  153. * new shape and an indication of where we are up to.
  154. * This is written to the superblock.
  155. * If reshape_position is MaxSector, then no reshape is happening (yet).
  156. */
  157. sector_t reshape_position;
  158. int delta_disks, new_level, new_layout;
  159. int new_chunk_sectors;
  160. atomic_t plug_cnt; /* If device is expecting
  161. * more bios soon.
  162. */
  163. struct mdk_thread_s *thread; /* management thread */
  164. struct mdk_thread_s *sync_thread; /* doing resync or reconstruct */
  165. sector_t curr_resync; /* last block scheduled */
  166. /* As resync requests can complete out of order, we cannot easily track
  167. * how much resync has been completed. So we occasionally pause until
  168. * everything completes, then set curr_resync_completed to curr_resync.
  169. * As such it may be well behind the real resync mark, but it is a value
  170. * we are certain of.
  171. */
  172. sector_t curr_resync_completed;
  173. unsigned long resync_mark; /* a recent timestamp */
  174. sector_t resync_mark_cnt;/* blocks written at resync_mark */
  175. sector_t curr_mark_cnt; /* blocks scheduled now */
  176. sector_t resync_max_sectors; /* may be set by personality */
  177. sector_t resync_mismatches; /* count of sectors where
  178. * parity/replica mismatch found
  179. */
  180. /* allow user-space to request suspension of IO to regions of the array */
  181. sector_t suspend_lo;
  182. sector_t suspend_hi;
  183. /* if zero, use the system-wide default */
  184. int sync_speed_min;
  185. int sync_speed_max;
  186. /* resync even though the same disks are shared among md-devices */
  187. int parallel_resync;
  188. int ok_start_degraded;
  189. /* recovery/resync flags
  190. * NEEDED: we might need to start a resync/recover
  191. * RUNNING: a thread is running, or about to be started
  192. * SYNC: actually doing a resync, not a recovery
  193. * RECOVER: doing recovery, or need to try it.
  194. * INTR: resync needs to be aborted for some reason
  195. * DONE: thread is done and is waiting to be reaped
  196. * REQUEST: user-space has requested a sync (used with SYNC)
  197. * CHECK: user-space request for check-only, no repair
  198. * RESHAPE: A reshape is happening
  199. *
  200. * If neither SYNC or RESHAPE are set, then it is a recovery.
  201. */
  202. #define MD_RECOVERY_RUNNING 0
  203. #define MD_RECOVERY_SYNC 1
  204. #define MD_RECOVERY_RECOVER 2
  205. #define MD_RECOVERY_INTR 3
  206. #define MD_RECOVERY_DONE 4
  207. #define MD_RECOVERY_NEEDED 5
  208. #define MD_RECOVERY_REQUESTED 6
  209. #define MD_RECOVERY_CHECK 7
  210. #define MD_RECOVERY_RESHAPE 8
  211. #define MD_RECOVERY_FROZEN 9
  212. unsigned long recovery;
  213. int recovery_disabled; /* if we detect that recovery
  214. * will always fail, set this
  215. * so we don't loop trying */
  216. int in_sync; /* know to not need resync */
  217. /* 'open_mutex' avoids races between 'md_open' and 'do_md_stop', so
  218. * that we are never stopping an array while it is open.
  219. * 'reconfig_mutex' protects all other reconfiguration.
  220. * These locks are separate due to conflicting interactions
  221. * with bdev->bd_mutex.
  222. * Lock ordering is:
  223. * reconfig_mutex -> bd_mutex : e.g. do_md_run -> revalidate_disk
  224. * bd_mutex -> open_mutex: e.g. __blkdev_get -> md_open
  225. */
  226. struct mutex open_mutex;
  227. struct mutex reconfig_mutex;
  228. atomic_t active; /* general refcount */
  229. atomic_t openers; /* number of active opens */
  230. int changed; /* True if we might need to
  231. * reread partition info */
  232. int degraded; /* whether md should consider
  233. * adding a spare
  234. */
  235. atomic_t recovery_active; /* blocks scheduled, but not written */
  236. wait_queue_head_t recovery_wait;
  237. sector_t recovery_cp;
  238. sector_t resync_min; /* user requested sync
  239. * starts here */
  240. sector_t resync_max; /* resync should pause
  241. * when it gets here */
  242. struct sysfs_dirent *sysfs_state; /* handle for 'array_state'
  243. * file in sysfs.
  244. */
  245. struct sysfs_dirent *sysfs_action; /* handle for 'sync_action' */
  246. struct work_struct del_work; /* used for delayed sysfs removal */
  247. spinlock_t write_lock;
  248. wait_queue_head_t sb_wait; /* for waiting on superblock updates */
  249. atomic_t pending_writes; /* number of active superblock writes */
  250. unsigned int safemode; /* if set, update "clean" superblock
  251. * when no writes pending.
  252. */
  253. unsigned int safemode_delay;
  254. struct timer_list safemode_timer;
  255. atomic_t writes_pending;
  256. struct request_queue *queue; /* for plugging ... */
  257. struct bitmap *bitmap; /* the bitmap for the device */
  258. struct {
  259. struct file *file; /* the bitmap file */
  260. loff_t offset; /* offset from superblock of
  261. * start of bitmap. May be
  262. * negative, but not '0'
  263. * For external metadata, offset
  264. * from start of device.
  265. */
  266. loff_t default_offset; /* this is the offset to use when
  267. * hot-adding a bitmap. It should
  268. * eventually be settable by sysfs.
  269. */
  270. /* When md is serving under dm, it might use a
  271. * dirty_log to store the bits.
  272. */
  273. struct dm_dirty_log *log;
  274. struct mutex mutex;
  275. unsigned long chunksize;
  276. unsigned long daemon_sleep; /* how many jiffies between updates? */
  277. unsigned long max_write_behind; /* write-behind mode */
  278. int external;
  279. } bitmap_info;
  280. atomic_t max_corr_read_errors; /* max read retries */
  281. struct list_head all_mddevs;
  282. struct attribute_group *to_remove;
  283. struct bio_set *bio_set;
  284. /* Generic flush handling.
  285. * The last to finish preflush schedules a worker to submit
  286. * the rest of the request (without the REQ_FLUSH flag).
  287. */
  288. struct bio *flush_bio;
  289. atomic_t flush_pending;
  290. struct work_struct flush_work;
  291. struct work_struct event_work; /* used by dm to report failure event */
  292. void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
  293. };
  294. static inline void rdev_dec_pending(mdk_rdev_t *rdev, mddev_t *mddev)
  295. {
  296. int faulty = test_bit(Faulty, &rdev->flags);
  297. if (atomic_dec_and_test(&rdev->nr_pending) && faulty)
  298. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  299. }
  300. static inline void md_sync_acct(struct block_device *bdev, unsigned long nr_sectors)
  301. {
  302. atomic_add(nr_sectors, &bdev->bd_contains->bd_disk->sync_io);
  303. }
  304. struct mdk_personality
  305. {
  306. char *name;
  307. int level;
  308. struct list_head list;
  309. struct module *owner;
  310. int (*make_request)(mddev_t *mddev, struct bio *bio);
  311. int (*run)(mddev_t *mddev);
  312. int (*stop)(mddev_t *mddev);
  313. void (*status)(struct seq_file *seq, mddev_t *mddev);
  314. /* error_handler must set ->faulty and clear ->in_sync
  315. * if appropriate, and should abort recovery if needed
  316. */
  317. void (*error_handler)(mddev_t *mddev, mdk_rdev_t *rdev);
  318. int (*hot_add_disk) (mddev_t *mddev, mdk_rdev_t *rdev);
  319. int (*hot_remove_disk) (mddev_t *mddev, int number);
  320. int (*spare_active) (mddev_t *mddev);
  321. sector_t (*sync_request)(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster);
  322. int (*resize) (mddev_t *mddev, sector_t sectors);
  323. sector_t (*size) (mddev_t *mddev, sector_t sectors, int raid_disks);
  324. int (*check_reshape) (mddev_t *mddev);
  325. int (*start_reshape) (mddev_t *mddev);
  326. void (*finish_reshape) (mddev_t *mddev);
  327. /* quiesce moves between quiescence states
  328. * 0 - fully active
  329. * 1 - no new requests allowed
  330. * others - reserved
  331. */
  332. void (*quiesce) (mddev_t *mddev, int state);
  333. /* takeover is used to transition an array from one
  334. * personality to another. The new personality must be able
  335. * to handle the data in the current layout.
  336. * e.g. 2drive raid1 -> 2drive raid5
  337. * ndrive raid5 -> degraded n+1drive raid6 with special layout
  338. * If the takeover succeeds, a new 'private' structure is returned.
  339. * This needs to be installed and then ->run used to activate the
  340. * array.
  341. */
  342. void *(*takeover) (mddev_t *mddev);
  343. };
  344. struct md_sysfs_entry {
  345. struct attribute attr;
  346. ssize_t (*show)(mddev_t *, char *);
  347. ssize_t (*store)(mddev_t *, const char *, size_t);
  348. };
  349. extern struct attribute_group md_bitmap_group;
  350. static inline struct sysfs_dirent *sysfs_get_dirent_safe(struct sysfs_dirent *sd, char *name)
  351. {
  352. if (sd)
  353. return sysfs_get_dirent(sd, NULL, name);
  354. return sd;
  355. }
  356. static inline void sysfs_notify_dirent_safe(struct sysfs_dirent *sd)
  357. {
  358. if (sd)
  359. sysfs_notify_dirent(sd);
  360. }
  361. static inline char * mdname (mddev_t * mddev)
  362. {
  363. return mddev->gendisk ? mddev->gendisk->disk_name : "mdX";
  364. }
  365. /*
  366. * iterates through some rdev ringlist. It's safe to remove the
  367. * current 'rdev'. Dont touch 'tmp' though.
  368. */
  369. #define rdev_for_each_list(rdev, tmp, head) \
  370. list_for_each_entry_safe(rdev, tmp, head, same_set)
  371. /*
  372. * iterates through the 'same array disks' ringlist
  373. */
  374. #define rdev_for_each(rdev, tmp, mddev) \
  375. list_for_each_entry_safe(rdev, tmp, &((mddev)->disks), same_set)
  376. #define rdev_for_each_rcu(rdev, mddev) \
  377. list_for_each_entry_rcu(rdev, &((mddev)->disks), same_set)
  378. typedef struct mdk_thread_s {
  379. void (*run) (mddev_t *mddev);
  380. mddev_t *mddev;
  381. wait_queue_head_t wqueue;
  382. unsigned long flags;
  383. struct task_struct *tsk;
  384. unsigned long timeout;
  385. } mdk_thread_t;
  386. #define THREAD_WAKEUP 0
  387. #define __wait_event_lock_irq(wq, condition, lock, cmd) \
  388. do { \
  389. wait_queue_t __wait; \
  390. init_waitqueue_entry(&__wait, current); \
  391. \
  392. add_wait_queue(&wq, &__wait); \
  393. for (;;) { \
  394. set_current_state(TASK_UNINTERRUPTIBLE); \
  395. if (condition) \
  396. break; \
  397. spin_unlock_irq(&lock); \
  398. cmd; \
  399. schedule(); \
  400. spin_lock_irq(&lock); \
  401. } \
  402. current->state = TASK_RUNNING; \
  403. remove_wait_queue(&wq, &__wait); \
  404. } while (0)
  405. #define wait_event_lock_irq(wq, condition, lock, cmd) \
  406. do { \
  407. if (condition) \
  408. break; \
  409. __wait_event_lock_irq(wq, condition, lock, cmd); \
  410. } while (0)
  411. static inline void safe_put_page(struct page *p)
  412. {
  413. if (p) put_page(p);
  414. }
  415. extern int register_md_personality(struct mdk_personality *p);
  416. extern int unregister_md_personality(struct mdk_personality *p);
  417. extern mdk_thread_t * md_register_thread(void (*run) (mddev_t *mddev),
  418. mddev_t *mddev, const char *name);
  419. extern void md_unregister_thread(mdk_thread_t **threadp);
  420. extern void md_wakeup_thread(mdk_thread_t *thread);
  421. extern void md_check_recovery(mddev_t *mddev);
  422. extern void md_write_start(mddev_t *mddev, struct bio *bi);
  423. extern void md_write_end(mddev_t *mddev);
  424. extern void md_done_sync(mddev_t *mddev, int blocks, int ok);
  425. extern void md_error(mddev_t *mddev, mdk_rdev_t *rdev);
  426. extern int mddev_congested(mddev_t *mddev, int bits);
  427. extern void md_flush_request(mddev_t *mddev, struct bio *bio);
  428. extern void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
  429. sector_t sector, int size, struct page *page);
  430. extern void md_super_wait(mddev_t *mddev);
  431. extern int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
  432. struct page *page, int rw, bool metadata_op);
  433. extern void md_do_sync(mddev_t *mddev);
  434. extern void md_new_event(mddev_t *mddev);
  435. extern int md_allow_write(mddev_t *mddev);
  436. extern void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev);
  437. extern void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors);
  438. extern int md_check_no_bitmap(mddev_t *mddev);
  439. extern int md_integrity_register(mddev_t *mddev);
  440. extern void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev);
  441. extern int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale);
  442. extern void restore_bitmap_write_access(struct file *file);
  443. extern void mddev_init(mddev_t *mddev);
  444. extern int md_run(mddev_t *mddev);
  445. extern void md_stop(mddev_t *mddev);
  446. extern void md_stop_writes(mddev_t *mddev);
  447. extern void md_rdev_init(mdk_rdev_t *rdev);
  448. extern void mddev_suspend(mddev_t *mddev);
  449. extern void mddev_resume(mddev_t *mddev);
  450. extern struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
  451. mddev_t *mddev);
  452. extern struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
  453. mddev_t *mddev);
  454. extern int mddev_check_plugged(mddev_t *mddev);
  455. #endif /* _MD_MD_H */