]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/lsblk.h
Merge branch 'libuuid-range' of https://github.com/UQ-RCC/util-linux
[thirdparty/util-linux.git] / misc-utils / lsblk.h
1 /*
2 * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved.
3 * Written by Milan Broz <mbroz@redhat.com>
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>
11 #include <sys/stat.h>
12 #include <sys/statvfs.h>
13
14 #include <libsmartcols.h>
15
16 #include "c.h"
17 #include "list.h"
18 #include "debug.h"
19
20 #define LSBLK_DEBUG_INIT (1 << 1)
21 #define LSBLK_DEBUG_FILTER (1 << 2)
22 #define LSBLK_DEBUG_DEV (1 << 3)
23 #define LSBLK_DEBUG_TREE (1 << 4)
24 #define LSBLK_DEBUG_DEP (1 << 5)
25 #define LSBLK_DEBUG_ALL 0xFFFF
26
27 UL_DEBUG_DECLARE_MASK(lsblk);
28 #define DBG(m, x) __UL_DBG(lsblk, LSBLK_DEBUG_, m, x)
29 #define ON_DBG(m, x) __UL_DBG_CALL(lsblk, LSBLK_DEBUG_, m, x)
30
31 #define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(lsblk)
32 #include "debugobj.h"
33
34 struct lsblk {
35 struct libscols_table *table; /* output table */
36
37 struct libscols_column *sort_col;/* sort output by this column */
38
39 int sort_id; /* id of the sort column */
40 int tree_id; /* od of column used for tree */
41
42 int dedup_id;
43
44 const char *sysroot;
45 int flags; /* LSBLK_* */
46
47 unsigned int all_devices:1; /* print all devices, including empty */
48 unsigned int bytes:1; /* print SIZE in bytes */
49 unsigned int inverse:1; /* print inverse dependencies */
50 unsigned int merge:1; /* merge sub-trees */
51 unsigned int nodeps:1; /* don't print slaves/holders */
52 unsigned int scsi:1; /* print only device with HCTL (SCSI) */
53 unsigned int paths:1; /* print devnames with "/dev" prefix */
54 unsigned int sort_hidden:1; /* sort column not between output columns */
55 unsigned int dedup_hidden :1; /* deduplication column not between output columns */
56 unsigned int force_tree_order:1;/* sort lines by parent->tree relation */
57 };
58
59 extern struct lsblk *lsblk; /* global handler */
60
61 struct lsblk_devprop {
62 /* udev / blkid based */
63 char *fstype; /* detected fs, NULL or "?" if cannot detect */
64 char *fsversion; /* filesystem version */
65 char *uuid; /* filesystem UUID (or stack uuid) */
66 char *ptuuid; /* partition table UUID */
67 char *pttype; /* partition table type */
68 char *label; /* filesystem label */
69 char *parttype; /* partition type UUID */
70 char *partuuid; /* partition UUID */
71 char *partlabel; /* partition label */
72 char *partflags; /* partition flags */
73 char *wwn; /* storage WWN */
74 char *serial; /* disk serial number */
75 char *model; /* disk model */
76
77 /* lsblk specific (for --sysroot only) */
78 char *owner; /* user name */
79 char *group; /* group name */
80 char *mode; /* access mode in ls(1)-like notation */
81 };
82
83 /* Device dependence
84 *
85 * Note that the same device may be slave/holder for more another devices. It
86 * means we need to allocate list member rather than use @child directly.
87 */
88 struct lsblk_devdep {
89 struct list_head ls_childs; /* item in parent->childs */
90 struct list_head ls_parents; /* item in child->parents */
91
92 struct lsblk_device *child;
93 struct lsblk_device *parent;
94 };
95
96 struct lsblk_device {
97 int refcount;
98
99 struct list_head childs; /* list with lsblk_devdep */
100 struct list_head parents;
101 struct list_head ls_roots; /* item in devtree->roots list */
102 struct list_head ls_devices; /* item in devtree->devices list */
103
104 struct lsblk_device *wholedisk; /* for partitions */
105
106 struct libscols_line *scols_line;
107
108 struct lsblk_devprop *properties;
109 struct stat st;
110
111 char *name; /* kernel name in /sys/block */
112 char *dm_name; /* DM name (dm/block) */
113
114 char *filename; /* path to device node */
115 char *dedupkey; /* de-duplication key */
116
117 struct path_cxt *sysfs;
118
119 char *mountpoint; /* device mountpoint */
120 struct statvfs fsstat; /* statvfs() result */
121
122 int npartitions; /* # of partitions this device has */
123 int nholders; /* # of devices mapped directly to this device
124 * /sys/block/.../holders */
125 int nslaves; /* # of devices this device maps to */
126 int maj, min; /* devno */
127
128 uint64_t discard_granularity; /* sunknown:-1, yes:1, not:0 */
129
130 uint64_t size; /* device size */
131 int removable; /* unknown:-1, yes:1, not:0 */
132
133 unsigned int is_mounted : 1,
134 is_swap : 1,
135 is_printed : 1,
136 udev_requested : 1,
137 blkid_requested : 1,
138 file_requested : 1;
139 };
140
141 #define device_is_partition(_x) ((_x)->wholedisk != NULL)
142
143 /*
144 * Note that lsblk tree uses bottom devices (devices without slaves) as root
145 * of the tree, and partitions are interpreted as a dependence too; it means:
146 * sda -> sda1 -> md0
147 *
148 * The flag 'is_inverted' turns the tree over (root is device without holders):
149 * md0 -> sda1 -> sda
150 */
151 struct lsblk_devtree {
152 int refcount;
153
154 struct list_head roots; /* tree root devices */
155 struct list_head devices; /* all devices */
156
157 unsigned int is_inverse : 1; /* inverse tree */
158 };
159
160
161 /*
162 * Generic iterator
163 */
164 struct lsblk_iter {
165 struct list_head *p; /* current position */
166 struct list_head *head; /* start position */
167 int direction; /* LSBLK_ITER_{FOR,BACK}WARD */
168 };
169
170 #define LSBLK_ITER_FORWARD 0
171 #define LSBLK_ITER_BACKWARD 1
172
173 #define IS_ITER_FORWARD(_i) ((_i)->direction == LSBLK_ITER_FORWARD)
174 #define IS_ITER_BACKWARD(_i) ((_i)->direction == LSBLK_ITER_BACKWARD)
175
176 #define LSBLK_ITER_INIT(itr, list) \
177 do { \
178 (itr)->p = IS_ITER_FORWARD(itr) ? \
179 (list)->next : (list)->prev; \
180 (itr)->head = (list); \
181 } while(0)
182
183 #define LSBLK_ITER_ITERATE(itr, res, restype, member) \
184 do { \
185 res = list_entry((itr)->p, restype, member); \
186 (itr)->p = IS_ITER_FORWARD(itr) ? \
187 (itr)->p->next : (itr)->p->prev; \
188 } while(0)
189
190
191 /* lsblk-mnt.c */
192 extern void lsblk_mnt_init(void);
193 extern void lsblk_mnt_deinit(void);
194
195 extern char *lsblk_device_get_mountpoint(struct lsblk_device *dev);
196
197 /* lsblk-properties.c */
198 extern void lsblk_device_free_properties(struct lsblk_devprop *p);
199 extern struct lsblk_devprop *lsblk_device_get_properties(struct lsblk_device *dev);
200 extern void lsblk_properties_deinit(void);
201
202 extern const char *lsblk_parttype_code_to_string(const char *code, const char *pttype);
203
204 /* lsblk-devtree.c */
205 void lsblk_reset_iter(struct lsblk_iter *itr, int direction);
206 struct lsblk_device *lsblk_new_device(void);
207 void lsblk_ref_device(struct lsblk_device *dev);
208 void lsblk_unref_device(struct lsblk_device *dev);
209 int lsblk_device_new_dependence(struct lsblk_device *parent, struct lsblk_device *child);
210 int lsblk_device_has_child(struct lsblk_device *dev, struct lsblk_device *child);
211 int lsblk_device_next_child(struct lsblk_device *dev,
212 struct lsblk_iter *itr,
213 struct lsblk_device **child);
214
215 int lsblk_device_is_last_parent(struct lsblk_device *dev, struct lsblk_device *parent);
216 int lsblk_device_next_parent(
217 struct lsblk_device *dev,
218 struct lsblk_iter *itr,
219 struct lsblk_device **parent);
220
221 struct lsblk_devtree *lsblk_new_devtree(void);
222 void lsblk_ref_devtree(struct lsblk_devtree *tr);
223 void lsblk_unref_devtree(struct lsblk_devtree *tr);
224 int lsblk_devtree_add_root(struct lsblk_devtree *tr, struct lsblk_device *dev);
225 int lsblk_devtree_next_root(struct lsblk_devtree *tr,
226 struct lsblk_iter *itr,
227 struct lsblk_device **dev);
228 int lsblk_devtree_add_device(struct lsblk_devtree *tr, struct lsblk_device *dev);
229 int lsblk_devtree_next_device(struct lsblk_devtree *tr,
230 struct lsblk_iter *itr,
231 struct lsblk_device **dev);
232 int lsblk_devtree_has_device(struct lsblk_devtree *tr, struct lsblk_device *dev);
233 struct lsblk_device *lsblk_devtree_get_device(struct lsblk_devtree *tr, const char *name);
234 int lsblk_devtree_remove_device(struct lsblk_devtree *tr, struct lsblk_device *dev);
235 int lsblk_devtree_deduplicate_devices(struct lsblk_devtree *tr);
236
237 #endif /* UTIL_LINUX_LSBLK_H */