]>
Commit | Line | Data |
---|---|---|
14560b7f KZ |
1 | /* |
2 | * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. | |
67b8731f | 3 | * Written by Milan Broz <gmazyland@gmail.com> |
14560b7f KZ |
4 | * Karel Zak <kzak@redhat.com> |
5 | */ | |
6 | #ifndef UTIL_LINUX_LSBLK_H | |
7 | #define UTIL_LINUX_LSBLK_H | |
8 | ||
9 | #include <stdint.h> | |
10 | #include <inttypes.h> | |
6529212d | 11 | #include <sys/stat.h> |
14560b7f KZ |
12 | #include <sys/statvfs.h> |
13 | ||
14 | #include <libsmartcols.h> | |
c6648d2d | 15 | #include <libmount.h> |
14560b7f KZ |
16 | |
17 | #include "c.h" | |
5bb395f4 | 18 | #include "list.h" |
14560b7f KZ |
19 | #include "debug.h" |
20 | ||
21 | #define LSBLK_DEBUG_INIT (1 << 1) | |
22 | #define LSBLK_DEBUG_FILTER (1 << 2) | |
23 | #define LSBLK_DEBUG_DEV (1 << 3) | |
3bf2a36c | 24 | #define LSBLK_DEBUG_TREE (1 << 4) |
0986f29a | 25 | #define LSBLK_DEBUG_DEP (1 << 5) |
14560b7f KZ |
26 | #define LSBLK_DEBUG_ALL 0xFFFF |
27 | ||
28 | UL_DEBUG_DECLARE_MASK(lsblk); | |
29 | #define DBG(m, x) __UL_DBG(lsblk, LSBLK_DEBUG_, m, x) | |
30 | #define ON_DBG(m, x) __UL_DBG_CALL(lsblk, LSBLK_DEBUG_, m, x) | |
31 | ||
32 | #define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(lsblk) | |
33 | #include "debugobj.h" | |
34 | ||
35 | struct lsblk { | |
36 | struct libscols_table *table; /* output table */ | |
37 | struct libscols_column *sort_col;/* sort output by this column */ | |
b9c088f2 KZ |
38 | |
39 | int sort_id; /* id of the sort column */ | |
40 | int tree_id; /* od of column used for tree */ | |
14560b7f | 41 | |
dc4662f0 KZ |
42 | int dedup_id; |
43 | ||
9f55de78 KZ |
44 | struct libscols_filter *filter; |
45 | struct libscols_filter *hlighter; | |
c3317cdc | 46 | const char *hlighter_seq; |
94ebe1d3 | 47 | |
0398951e KZ |
48 | struct libscols_filter **ct_filters; /* array of counters' filters */ |
49 | size_t ncts; /* number of ct filters */ | |
50 | ||
14560b7f KZ |
51 | const char *sysroot; |
52 | int flags; /* LSBLK_* */ | |
53 | ||
54 | unsigned int all_devices:1; /* print all devices, including empty */ | |
55 | unsigned int bytes:1; /* print SIZE in bytes */ | |
56 | unsigned int inverse:1; /* print inverse dependencies */ | |
0bd05f5e | 57 | unsigned int merge:1; /* merge sub-trees */ |
14560b7f KZ |
58 | unsigned int nodeps:1; /* don't print slaves/holders */ |
59 | unsigned int scsi:1; /* print only device with HCTL (SCSI) */ | |
97afabf7 | 60 | unsigned int nvme:1; /* print NVMe device only */ |
1b3e8f5d | 61 | unsigned int virtio:1; /* print virtio device only */ |
14560b7f KZ |
62 | unsigned int paths:1; /* print devnames with "/dev" prefix */ |
63 | unsigned int sort_hidden:1; /* sort column not between output columns */ | |
af5fa636 | 64 | unsigned int rawdata : 1; /* has rawdata in cell userdata */ |
dc4662f0 | 65 | unsigned int dedup_hidden :1; /* deduplication column not between output columns */ |
14560b7f | 66 | unsigned int force_tree_order:1;/* sort lines by parent->tree relation */ |
554e866b | 67 | unsigned int noempty:1; /* hide empty devices */ |
14560b7f KZ |
68 | }; |
69 | ||
70 | extern struct lsblk *lsblk; /* global handler */ | |
71 | ||
baad6dcc | 72 | struct lsblk_devprop { |
6f74ede5 | 73 | /* udev / blkid based */ |
baad6dcc | 74 | char *fstype; /* detected fs, NULL or "?" if cannot detect */ |
9cca1ef2 | 75 | char *fsversion; /* filesystem version */ |
baad6dcc KZ |
76 | char *uuid; /* filesystem UUID (or stack uuid) */ |
77 | char *ptuuid; /* partition table UUID */ | |
78 | char *pttype; /* partition table type */ | |
79 | char *label; /* filesystem label */ | |
80 | char *parttype; /* partition type UUID */ | |
81 | char *partuuid; /* partition UUID */ | |
82 | char *partlabel; /* partition label */ | |
83 | char *partflags; /* partition flags */ | |
e4ed8665 | 84 | char *partn; /* partition number */ |
baad6dcc KZ |
85 | char *wwn; /* storage WWN */ |
86 | char *serial; /* disk serial number */ | |
87 | char *model; /* disk model */ | |
e054895f | 88 | char *idlink; /* /dev/disk/by-id/<name> */ |
8e89dd8b | 89 | char *revision; /* firmware revision/version */ |
6f74ede5 KZ |
90 | |
91 | /* lsblk specific (for --sysroot only) */ | |
92 | char *owner; /* user name */ | |
93 | char *group; /* group name */ | |
94 | char *mode; /* access mode in ls(1)-like notation */ | |
baad6dcc KZ |
95 | }; |
96 | ||
5bb395f4 KZ |
97 | /* Device dependence |
98 | * | |
99 | * Note that the same device may be slave/holder for more another devices. It | |
100 | * means we need to allocate list member rather than use @child directly. | |
101 | */ | |
102 | struct lsblk_devdep { | |
0986f29a KZ |
103 | struct list_head ls_childs; /* item in parent->childs */ |
104 | struct list_head ls_parents; /* item in child->parents */ | |
105 | ||
5bb395f4 | 106 | struct lsblk_device *child; |
0986f29a | 107 | struct lsblk_device *parent; |
5bb395f4 KZ |
108 | }; |
109 | ||
fed34a1e | 110 | struct lsblk_device { |
5bb395f4 KZ |
111 | int refcount; |
112 | ||
0986f29a KZ |
113 | struct list_head childs; /* list with lsblk_devdep */ |
114 | struct list_head parents; | |
5bb395f4 KZ |
115 | struct list_head ls_roots; /* item in devtree->roots list */ |
116 | struct list_head ls_devices; /* item in devtree->devices list */ | |
14560b7f | 117 | |
10501add | 118 | struct lsblk_device *wholedisk; /* for partitions */ |
5bb395f4 | 119 | |
0bd05f5e KZ |
120 | struct libscols_line *scols_line; |
121 | ||
5bb395f4 | 122 | struct lsblk_devprop *properties; |
14560b7f KZ |
123 | struct stat st; |
124 | ||
125 | char *name; /* kernel name in /sys/block */ | |
126 | char *dm_name; /* DM name (dm/block) */ | |
127 | ||
128 | char *filename; /* path to device node */ | |
dc4662f0 | 129 | char *dedupkey; /* de-duplication key */ |
14560b7f KZ |
130 | |
131 | struct path_cxt *sysfs; | |
132 | ||
c6648d2d KZ |
133 | struct libmnt_fs **fss; /* filesystems attached to the device */ |
134 | size_t nfss; /* number of items in fss[] */ | |
135 | ||
14560b7f KZ |
136 | struct statvfs fsstat; /* statvfs() result */ |
137 | ||
138 | int npartitions; /* # of partitions this device has */ | |
139 | int nholders; /* # of devices mapped directly to this device | |
140 | * /sys/block/.../holders */ | |
141 | int nslaves; /* # of devices this device maps to */ | |
142 | int maj, min; /* devno */ | |
8a2f175c KZ |
143 | |
144 | uint64_t discard_granularity; /* sunknown:-1, yes:1, not:0 */ | |
14560b7f KZ |
145 | |
146 | uint64_t size; /* device size */ | |
81a8936c | 147 | int removable; /* unknown:-1, yes:1, not:0 */ |
14560b7f | 148 | |
82dae67d | 149 | unsigned int is_mounted : 1, |
baad6dcc | 150 | is_swap : 1, |
0bd05f5e | 151 | is_printed : 1, |
baad6dcc | 152 | udev_requested : 1, |
7408a5d9 KZ |
153 | blkid_requested : 1, |
154 | file_requested : 1; | |
14560b7f KZ |
155 | }; |
156 | ||
10501add | 157 | #define device_is_partition(_x) ((_x)->wholedisk != NULL) |
5bb395f4 | 158 | |
94ebe1d3 KZ |
159 | /* Unfortunately, pktcdvd dependence on block device is not defined by |
160 | * slave/holder symlinks. The struct lsblk_devnomap represents one line in | |
161 | * /sys/class/pktcdvd/device_map | |
162 | */ | |
163 | struct lsblk_devnomap { | |
164 | dev_t slave; /* packet device devno */ | |
165 | dev_t holder; /* block device devno */ | |
166 | ||
167 | struct list_head ls_devnomap; | |
168 | }; | |
169 | ||
170 | ||
5bb395f4 | 171 | /* |
11026083 | 172 | * Note that lsblk tree uses bottom devices (devices without slaves) as root |
5bb395f4 KZ |
173 | * of the tree, and partitions are interpreted as a dependence too; it means: |
174 | * sda -> sda1 -> md0 | |
175 | * | |
176 | * The flag 'is_inverted' turns the tree over (root is device without holders): | |
177 | * md0 -> sda1 -> sda | |
178 | */ | |
179 | struct lsblk_devtree { | |
180 | int refcount; | |
181 | ||
182 | struct list_head roots; /* tree root devices */ | |
183 | struct list_head devices; /* all devices */ | |
94ebe1d3 | 184 | struct list_head pktcdvd_map; /* devnomap->ls_devnomap */ |
5bb395f4 | 185 | |
94ebe1d3 KZ |
186 | unsigned int is_inverse : 1, /* inverse tree */ |
187 | pktcdvd_read : 1; | |
5bb395f4 KZ |
188 | }; |
189 | ||
190 | ||
191 | /* | |
192 | * Generic iterator | |
193 | */ | |
194 | struct lsblk_iter { | |
195 | struct list_head *p; /* current position */ | |
196 | struct list_head *head; /* start position */ | |
197 | int direction; /* LSBLK_ITER_{FOR,BACK}WARD */ | |
198 | }; | |
199 | ||
200 | #define LSBLK_ITER_FORWARD 0 | |
201 | #define LSBLK_ITER_BACKWARD 1 | |
202 | ||
203 | #define IS_ITER_FORWARD(_i) ((_i)->direction == LSBLK_ITER_FORWARD) | |
204 | #define IS_ITER_BACKWARD(_i) ((_i)->direction == LSBLK_ITER_BACKWARD) | |
205 | ||
206 | #define LSBLK_ITER_INIT(itr, list) \ | |
207 | do { \ | |
208 | (itr)->p = IS_ITER_FORWARD(itr) ? \ | |
209 | (list)->next : (list)->prev; \ | |
210 | (itr)->head = (list); \ | |
211 | } while(0) | |
212 | ||
213 | #define LSBLK_ITER_ITERATE(itr, res, restype, member) \ | |
214 | do { \ | |
215 | res = list_entry((itr)->p, restype, member); \ | |
216 | (itr)->p = IS_ITER_FORWARD(itr) ? \ | |
217 | (itr)->p->next : (itr)->p->prev; \ | |
218 | } while(0) | |
219 | ||
220 | ||
04be258d | 221 | /* lsblk-mnt.c */ |
e0c016f1 KZ |
222 | extern void lsblk_mnt_init(void); |
223 | extern void lsblk_mnt_deinit(void); | |
224 | ||
c6648d2d KZ |
225 | extern void lsblk_device_free_filesystems(struct lsblk_device *dev); |
226 | extern const char *lsblk_device_get_mountpoint(struct lsblk_device *dev); | |
227 | extern struct libmnt_fs **lsblk_device_get_filesystems(struct lsblk_device *dev, size_t *n); | |
cfb715ed | 228 | |
ccafadb7 KZ |
229 | /* lsblk-properties.c */ |
230 | extern void lsblk_device_free_properties(struct lsblk_devprop *p); | |
fed34a1e | 231 | extern struct lsblk_devprop *lsblk_device_get_properties(struct lsblk_device *dev); |
ccafadb7 KZ |
232 | extern void lsblk_properties_deinit(void); |
233 | ||
107e9559 KZ |
234 | extern const char *lsblk_parttype_code_to_string(const char *code, const char *pttype); |
235 | ||
5bb395f4 KZ |
236 | /* lsblk-devtree.c */ |
237 | void lsblk_reset_iter(struct lsblk_iter *itr, int direction); | |
10501add | 238 | struct lsblk_device *lsblk_new_device(void); |
5bb395f4 KZ |
239 | void lsblk_ref_device(struct lsblk_device *dev); |
240 | void lsblk_unref_device(struct lsblk_device *dev); | |
e2d7870b | 241 | int lsblk_device_new_dependence(struct lsblk_device *parent, struct lsblk_device *child); |
0986f29a | 242 | int lsblk_device_has_child(struct lsblk_device *dev, struct lsblk_device *child); |
5bb395f4 KZ |
243 | int lsblk_device_next_child(struct lsblk_device *dev, |
244 | struct lsblk_iter *itr, | |
245 | struct lsblk_device **child); | |
246 | ||
94ebe1d3 KZ |
247 | dev_t lsblk_devtree_pktcdvd_get_mate(struct lsblk_devtree *tr, dev_t devno, int is_slave); |
248 | ||
0bd05f5e KZ |
249 | int lsblk_device_is_last_parent(struct lsblk_device *dev, struct lsblk_device *parent); |
250 | int lsblk_device_next_parent( | |
251 | struct lsblk_device *dev, | |
252 | struct lsblk_iter *itr, | |
253 | struct lsblk_device **parent); | |
254 | ||
5bb395f4 KZ |
255 | struct lsblk_devtree *lsblk_new_devtree(void); |
256 | void lsblk_ref_devtree(struct lsblk_devtree *tr); | |
257 | void lsblk_unref_devtree(struct lsblk_devtree *tr); | |
258 | int lsblk_devtree_add_root(struct lsblk_devtree *tr, struct lsblk_device *dev); | |
94ebe1d3 | 259 | int lsblk_devtree_remove_root(struct lsblk_devtree *tr, struct lsblk_device *dev); |
5bb395f4 KZ |
260 | int lsblk_devtree_next_root(struct lsblk_devtree *tr, |
261 | struct lsblk_iter *itr, | |
262 | struct lsblk_device **dev); | |
263 | int lsblk_devtree_add_device(struct lsblk_devtree *tr, struct lsblk_device *dev); | |
264 | int lsblk_devtree_next_device(struct lsblk_devtree *tr, | |
265 | struct lsblk_iter *itr, | |
266 | struct lsblk_device **dev); | |
21d4a48c | 267 | int lsblk_devtree_has_device(struct lsblk_devtree *tr, struct lsblk_device *dev); |
5bb395f4 | 268 | struct lsblk_device *lsblk_devtree_get_device(struct lsblk_devtree *tr, const char *name); |
21d4a48c | 269 | int lsblk_devtree_remove_device(struct lsblk_devtree *tr, struct lsblk_device *dev); |
dc4662f0 | 270 | int lsblk_devtree_deduplicate_devices(struct lsblk_devtree *tr); |
5bb395f4 | 271 | |
14560b7f | 272 | #endif /* UTIL_LINUX_LSBLK_H */ |