]> git.ipfire.org Git - thirdparty/util-linux.git/blame - misc-utils/lsblk.h
lsblk: make process_partitions() more readable
[thirdparty/util-linux.git] / misc-utils / lsblk.h
CommitLineData
14560b7f
KZ
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>
6529212d 11#include <sys/stat.h>
14560b7f
KZ
12#include <sys/statvfs.h>
13
14#include <libsmartcols.h>
15
16#include "c.h"
5bb395f4 17#include "list.h"
14560b7f
KZ
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)
3bf2a36c 23#define LSBLK_DEBUG_TREE (1 << 4)
14560b7f
KZ
24#define LSBLK_DEBUG_ALL 0xFFFF
25
26UL_DEBUG_DECLARE_MASK(lsblk);
27#define DBG(m, x) __UL_DBG(lsblk, LSBLK_DEBUG_, m, x)
28#define ON_DBG(m, x) __UL_DBG_CALL(lsblk, LSBLK_DEBUG_, m, x)
29
30#define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(lsblk)
31#include "debugobj.h"
32
33struct lsblk {
34 struct libscols_table *table; /* output table */
35 struct libscols_column *sort_col;/* sort output by this column */
36 int sort_id;
37
38 const char *sysroot;
39 int flags; /* LSBLK_* */
40
41 unsigned int all_devices:1; /* print all devices, including empty */
42 unsigned int bytes:1; /* print SIZE in bytes */
43 unsigned int inverse:1; /* print inverse dependencies */
44 unsigned int nodeps:1; /* don't print slaves/holders */
45 unsigned int scsi:1; /* print only device with HCTL (SCSI) */
46 unsigned int paths:1; /* print devnames with "/dev" prefix */
47 unsigned int sort_hidden:1; /* sort column not between output columns */
48 unsigned int force_tree_order:1;/* sort lines by parent->tree relation */
49};
50
51extern struct lsblk *lsblk; /* global handler */
52
baad6dcc
KZ
53struct lsblk_devprop {
54 char *fstype; /* detected fs, NULL or "?" if cannot detect */
55 char *uuid; /* filesystem UUID (or stack uuid) */
56 char *ptuuid; /* partition table UUID */
57 char *pttype; /* partition table type */
58 char *label; /* filesystem label */
59 char *parttype; /* partition type UUID */
60 char *partuuid; /* partition UUID */
61 char *partlabel; /* partition label */
62 char *partflags; /* partition flags */
63 char *wwn; /* storage WWN */
64 char *serial; /* disk serial number */
65 char *model; /* disk model */
66};
67
5bb395f4
KZ
68/* Device dependence
69 *
70 * Note that the same device may be slave/holder for more another devices. It
71 * means we need to allocate list member rather than use @child directly.
72 */
73struct lsblk_devdep {
74 struct list_head ls_deps; /* item in parent->deps */
75 struct lsblk_device *child;
76};
77
fed34a1e 78struct lsblk_device {
5bb395f4
KZ
79 int refcount;
80
81 struct list_head deps; /* list with lsblk_devdep */
82 struct list_head ls_roots; /* item in devtree->roots list */
83 struct list_head ls_devices; /* item in devtree->devices list */
14560b7f 84
5bb395f4
KZ
85 struct lsblk_devtree *tree;
86
87 struct lsblk_devprop *properties;
88 struct libscols_line *scols_line;
89
14560b7f
KZ
90 struct stat st;
91
92 char *name; /* kernel name in /sys/block */
93 char *dm_name; /* DM name (dm/block) */
94
95 char *filename; /* path to device node */
96
97 struct path_cxt *sysfs;
98
99 int partition; /* is partition? TRUE/FALSE */
100
14560b7f
KZ
101 char *mountpoint; /* device mountpoint */
102 struct statvfs fsstat; /* statvfs() result */
103
104 int npartitions; /* # of partitions this device has */
105 int nholders; /* # of devices mapped directly to this device
106 * /sys/block/.../holders */
107 int nslaves; /* # of devices this device maps to */
108 int maj, min; /* devno */
109 int discard; /* supports discard */
110
111 uint64_t size; /* device size */
112
82dae67d 113 unsigned int is_mounted : 1,
baad6dcc
KZ
114 is_swap : 1,
115 udev_requested : 1,
116 blkid_requested : 1;
14560b7f
KZ
117};
118
5bb395f4
KZ
119
120/*
121 * Note that lsblk tree uses botton devices (devices without slaves) as root
122 * of the tree, and partitions are interpreted as a dependence too; it means:
123 * sda -> sda1 -> md0
124 *
125 * The flag 'is_inverted' turns the tree over (root is device without holders):
126 * md0 -> sda1 -> sda
127 */
128struct lsblk_devtree {
129 int refcount;
130
131 struct list_head roots; /* tree root devices */
132 struct list_head devices; /* all devices */
133
134 unsigned int is_inverse : 1; /* inverse tree */
135};
136
137
138/*
139 * Generic iterator
140 */
141struct lsblk_iter {
142 struct list_head *p; /* current position */
143 struct list_head *head; /* start position */
144 int direction; /* LSBLK_ITER_{FOR,BACK}WARD */
145};
146
147#define LSBLK_ITER_FORWARD 0
148#define LSBLK_ITER_BACKWARD 1
149
150#define IS_ITER_FORWARD(_i) ((_i)->direction == LSBLK_ITER_FORWARD)
151#define IS_ITER_BACKWARD(_i) ((_i)->direction == LSBLK_ITER_BACKWARD)
152
153#define LSBLK_ITER_INIT(itr, list) \
154 do { \
155 (itr)->p = IS_ITER_FORWARD(itr) ? \
156 (list)->next : (list)->prev; \
157 (itr)->head = (list); \
158 } while(0)
159
160#define LSBLK_ITER_ITERATE(itr, res, restype, member) \
161 do { \
162 res = list_entry((itr)->p, restype, member); \
163 (itr)->p = IS_ITER_FORWARD(itr) ? \
164 (itr)->p->next : (itr)->p->prev; \
165 } while(0)
166
167
04be258d 168/* lsblk-mnt.c */
e0c016f1
KZ
169extern void lsblk_mnt_init(void);
170extern void lsblk_mnt_deinit(void);
171
fed34a1e 172extern char *lsblk_device_get_mountpoint(struct lsblk_device *dev);
cfb715ed 173
ccafadb7
KZ
174/* lsblk-properties.c */
175extern void lsblk_device_free_properties(struct lsblk_devprop *p);
fed34a1e 176extern struct lsblk_devprop *lsblk_device_get_properties(struct lsblk_device *dev);
ccafadb7
KZ
177extern void lsblk_properties_deinit(void);
178
5bb395f4
KZ
179/* lsblk-devtree.c */
180void lsblk_reset_iter(struct lsblk_iter *itr, int direction);
181struct lsblk_device *lsblk_new_device(struct lsblk_devtree *tree);
182void lsblk_ref_device(struct lsblk_device *dev);
183void lsblk_unref_device(struct lsblk_device *dev);
e2d7870b
KZ
184int lsblk_device_new_dependence(struct lsblk_device *parent, struct lsblk_device *child);
185int lsblk_device_has_dependence(struct lsblk_device *dev, struct lsblk_device *child);
5bb395f4
KZ
186int lsblk_device_next_child(struct lsblk_device *dev,
187 struct lsblk_iter *itr,
188 struct lsblk_device **child);
189
190struct lsblk_devtree *lsblk_new_devtree(void);
191void lsblk_ref_devtree(struct lsblk_devtree *tr);
192void lsblk_unref_devtree(struct lsblk_devtree *tr);
193int lsblk_devtree_add_root(struct lsblk_devtree *tr, struct lsblk_device *dev);
194int lsblk_devtree_next_root(struct lsblk_devtree *tr,
195 struct lsblk_iter *itr,
196 struct lsblk_device **dev);
197int lsblk_devtree_add_device(struct lsblk_devtree *tr, struct lsblk_device *dev);
198int lsblk_devtree_next_device(struct lsblk_devtree *tr,
199 struct lsblk_iter *itr,
200 struct lsblk_device **dev);
21d4a48c 201int lsblk_devtree_has_device(struct lsblk_devtree *tr, struct lsblk_device *dev);
5bb395f4 202struct lsblk_device *lsblk_devtree_get_device(struct lsblk_devtree *tr, const char *name);
21d4a48c 203int lsblk_devtree_remove_device(struct lsblk_devtree *tr, struct lsblk_device *dev);
5bb395f4 204
14560b7f 205#endif /* UTIL_LINUX_LSBLK_H */