blob: c2c07bfa647199139f78522c5c87da3e170e6cee [file] [log] [blame]
Heinz Mauelshagen3bd94002023-01-25 21:00:44 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07004 * Copyright (C) 2004 - 2006 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This file is released under the GPL.
7 */
8
Mike Snitzer4cc96132016-05-12 16:28:10 -04009#include "dm-core.h"
Tushar Sugandhi91ccbba2021-07-12 17:48:58 -070010#include "dm-ima.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/vmalloc.h>
13#include <linux/miscdevice.h>
Ingo Molnar5b3cc152017-02-02 20:43:54 +010014#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/wait.h>
17#include <linux/slab.h>
Mikulas Patockab82096a2021-03-10 05:18:03 -050018#include <linux/rbtree.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/dm-ioctl.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080020#include <linux/hdreg.h>
Milan Broz76c072b2008-02-08 02:09:56 +000021#include <linux/compat.h>
Jordy Zomercd9c88d2022-01-29 15:58:39 +010022#include <linux/nospec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080024#include <linux/uaccess.h>
Tushar Sugandhi91ccbba2021-07-12 17:48:58 -070025#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Alasdair G Kergon72d94862006-06-26 00:27:35 -070027#define DM_MSG_PREFIX "ioctl"
Mike Snitzer86ab1b82024-02-07 11:42:53 -050028#define DM_DRIVER_EMAIL "dm-devel@lists.linux.dev"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Mikulas Patocka93e64422017-01-16 16:05:59 -050030struct dm_file {
31 /*
32 * poll will wait until the global event number is greater than
33 * this value.
34 */
Heinz Mauelshagen86a32382023-01-25 21:14:58 +010035 volatile unsigned int global_event_nr;
Mikulas Patocka93e64422017-01-16 16:05:59 -050036};
37
Heinz Mauelshagena4a82ce2023-01-26 15:48:30 +010038/*
39 *---------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * The ioctl interface needs to be able to look up devices by
41 * name or uuid.
Heinz Mauelshagena4a82ce2023-01-26 15:48:30 +010042 *---------------------------------------------------------------
43 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044struct hash_cell {
Mikulas Patockab82096a2021-03-10 05:18:03 -050045 struct rb_node name_node;
46 struct rb_node uuid_node;
47 bool name_set;
48 bool uuid_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 char *name;
51 char *uuid;
52 struct mapped_device *md;
53 struct dm_table *new_map;
54};
55
56struct vers_iter {
Heinz Mauelshagen8ca817c2023-02-01 22:31:43 +010057 size_t param_size;
58 struct dm_target_versions *vers, *old_vers;
59 char *end;
60 uint32_t flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
63
Mikulas Patockab82096a2021-03-10 05:18:03 -050064static struct rb_root name_rb_tree = RB_ROOT;
65static struct rb_root uuid_rb_tree = RB_ROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Mikulas Patocka2c140a22013-11-01 18:27:41 -040067static void dm_hash_remove_all(bool keep_open_devices, bool mark_deferred, bool only_deferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
70 * Guards access to both hash tables.
71 */
72static DECLARE_RWSEM(_hash_lock);
73
Mikulas Patocka60769052009-12-10 23:51:52 +000074/*
75 * Protects use of mdptr to obtain hash cell name and uuid from mapped device.
76 */
77static DEFINE_MUTEX(dm_hash_cells_mutex);
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static void dm_hash_exit(void)
80{
Mikulas Patocka2c140a22013-11-01 18:27:41 -040081 dm_hash_remove_all(false, false, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Heinz Mauelshagena4a82ce2023-01-26 15:48:30 +010084/*
85 *---------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * Code for looking up a device by name
Heinz Mauelshagena4a82ce2023-01-26 15:48:30 +010087 *---------------------------------------------------------------
88 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static struct hash_cell *__get_name_cell(const char *str)
90{
Mikulas Patockab82096a2021-03-10 05:18:03 -050091 struct rb_node *n = name_rb_tree.rb_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Mikulas Patockab82096a2021-03-10 05:18:03 -050093 while (n) {
94 struct hash_cell *hc = container_of(n, struct hash_cell, name_node);
Heinz Mauelshagen0ef0b472023-02-01 23:42:29 +010095 int c;
96
97 c = strcmp(hc->name, str);
Mikulas Patockab82096a2021-03-10 05:18:03 -050098 if (!c) {
Jeff Mahoney7ec75f22006-06-26 00:27:24 -070099 dm_get(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700101 }
Mikulas Patockab82096a2021-03-10 05:18:03 -0500102 n = c >= 0 ? n->rb_left : n->rb_right;
103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 return NULL;
106}
107
108static struct hash_cell *__get_uuid_cell(const char *str)
109{
Mikulas Patockab82096a2021-03-10 05:18:03 -0500110 struct rb_node *n = uuid_rb_tree.rb_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Mikulas Patockab82096a2021-03-10 05:18:03 -0500112 while (n) {
113 struct hash_cell *hc = container_of(n, struct hash_cell, uuid_node);
Heinz Mauelshagen0ef0b472023-02-01 23:42:29 +0100114 int c;
115
116 c = strcmp(hc->uuid, str);
Mikulas Patockab82096a2021-03-10 05:18:03 -0500117 if (!c) {
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700118 dm_get(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700120 }
Mikulas Patockab82096a2021-03-10 05:18:03 -0500121 n = c >= 0 ? n->rb_left : n->rb_right;
122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 return NULL;
125}
126
Mikulas Patockab82096a2021-03-10 05:18:03 -0500127static void __unlink_name(struct hash_cell *hc)
128{
129 if (hc->name_set) {
130 hc->name_set = false;
131 rb_erase(&hc->name_node, &name_rb_tree);
132 }
133}
134
135static void __unlink_uuid(struct hash_cell *hc)
136{
137 if (hc->uuid_set) {
138 hc->uuid_set = false;
139 rb_erase(&hc->uuid_node, &uuid_rb_tree);
140 }
141}
142
143static void __link_name(struct hash_cell *new_hc)
144{
145 struct rb_node **n, *parent;
146
147 __unlink_name(new_hc);
148
149 new_hc->name_set = true;
150
151 n = &name_rb_tree.rb_node;
152 parent = NULL;
153
154 while (*n) {
155 struct hash_cell *hc = container_of(*n, struct hash_cell, name_node);
Heinz Mauelshagen0ef0b472023-02-01 23:42:29 +0100156 int c;
157
158 c = strcmp(hc->name, new_hc->name);
Mikulas Patockab82096a2021-03-10 05:18:03 -0500159 BUG_ON(!c);
160 parent = *n;
161 n = c >= 0 ? &hc->name_node.rb_left : &hc->name_node.rb_right;
162 }
163
164 rb_link_node(&new_hc->name_node, parent, n);
165 rb_insert_color(&new_hc->name_node, &name_rb_tree);
166}
167
168static void __link_uuid(struct hash_cell *new_hc)
169{
170 struct rb_node **n, *parent;
171
172 __unlink_uuid(new_hc);
173
174 new_hc->uuid_set = true;
175
176 n = &uuid_rb_tree.rb_node;
177 parent = NULL;
178
179 while (*n) {
180 struct hash_cell *hc = container_of(*n, struct hash_cell, uuid_node);
Heinz Mauelshagen0ef0b472023-02-01 23:42:29 +0100181 int c;
182
183 c = strcmp(hc->uuid, new_hc->uuid);
Mikulas Patockab82096a2021-03-10 05:18:03 -0500184 BUG_ON(!c);
185 parent = *n;
186 n = c > 0 ? &hc->uuid_node.rb_left : &hc->uuid_node.rb_right;
187 }
188
189 rb_link_node(&new_hc->uuid_node, parent, n);
190 rb_insert_color(&new_hc->uuid_node, &uuid_rb_tree);
191}
192
Mikulas Patockaba2e19b2011-08-02 12:32:06 +0100193static struct hash_cell *__get_dev_cell(uint64_t dev)
194{
195 struct mapped_device *md;
196 struct hash_cell *hc;
197
198 md = dm_get_md(huge_decode_dev(dev));
199 if (!md)
200 return NULL;
201
202 hc = dm_get_mdptr(md);
203 if (!hc) {
204 dm_put(md);
205 return NULL;
206 }
207
208 return hc;
209}
210
Heinz Mauelshagena4a82ce2023-01-26 15:48:30 +0100211/*
212 *---------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 * Inserting, removing and renaming a device.
Heinz Mauelshagena4a82ce2023-01-26 15:48:30 +0100214 *---------------------------------------------------------------
215 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static struct hash_cell *alloc_cell(const char *name, const char *uuid,
217 struct mapped_device *md)
218{
219 struct hash_cell *hc;
220
221 hc = kmalloc(sizeof(*hc), GFP_KERNEL);
222 if (!hc)
223 return NULL;
224
Paulo Marques543537b2005-06-23 00:09:02 -0700225 hc->name = kstrdup(name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (!hc->name) {
227 kfree(hc);
228 return NULL;
229 }
230
231 if (!uuid)
232 hc->uuid = NULL;
233
234 else {
Paulo Marques543537b2005-06-23 00:09:02 -0700235 hc->uuid = kstrdup(uuid, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (!hc->uuid) {
237 kfree(hc->name);
238 kfree(hc);
239 return NULL;
240 }
241 }
242
Mikulas Patockab82096a2021-03-10 05:18:03 -0500243 hc->name_set = hc->uuid_set = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 hc->md = md;
245 hc->new_map = NULL;
246 return hc;
247}
248
249static void free_cell(struct hash_cell *hc)
250{
251 if (hc) {
252 kfree(hc->name);
253 kfree(hc->uuid);
254 kfree(hc);
255 }
256}
257
258/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * The kdev_t and uuid of a device can never change once it is
260 * initially inserted.
261 */
262static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
263{
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700264 struct hash_cell *cell, *hc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 /*
267 * Allocate the new cells.
268 */
269 cell = alloc_cell(name, uuid, md);
270 if (!cell)
271 return -ENOMEM;
272
273 /*
274 * Insert the cell into both hash tables.
275 */
276 down_write(&_hash_lock);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700277 hc = __get_name_cell(name);
278 if (hc) {
279 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 goto bad;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Mikulas Patockab82096a2021-03-10 05:18:03 -0500283 __link_name(cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (uuid) {
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700286 hc = __get_uuid_cell(uuid);
287 if (hc) {
Mikulas Patockab82096a2021-03-10 05:18:03 -0500288 __unlink_name(cell);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700289 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 goto bad;
291 }
Mikulas Patockab82096a2021-03-10 05:18:03 -0500292 __link_uuid(cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 dm_get(md);
Mikulas Patocka60769052009-12-10 23:51:52 +0000295 mutex_lock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 dm_set_mdptr(md, cell);
Mikulas Patocka60769052009-12-10 23:51:52 +0000297 mutex_unlock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 up_write(&_hash_lock);
299
300 return 0;
301
302 bad:
303 up_write(&_hash_lock);
304 free_cell(cell);
305 return -EBUSY;
306}
307
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100308static struct dm_table *__hash_remove(struct hash_cell *hc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
goggin, edward269fd2a2005-09-27 21:45:44 -0700310 struct dm_table *table;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100311 int srcu_idx;
goggin, edward269fd2a2005-09-27 21:45:44 -0700312
Mike Snitzer69868be2023-02-17 13:08:17 -0500313 lockdep_assert_held(&_hash_lock);
314
Mikulas Patockab82096a2021-03-10 05:18:03 -0500315 /* remove from the dev trees */
316 __unlink_name(hc);
317 __unlink_uuid(hc);
Mikulas Patocka60769052009-12-10 23:51:52 +0000318 mutex_lock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 dm_set_mdptr(hc->md, NULL);
Mikulas Patocka60769052009-12-10 23:51:52 +0000320 mutex_unlock(&dm_hash_cells_mutex);
goggin, edward269fd2a2005-09-27 21:45:44 -0700321
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100322 table = dm_get_live_table(hc->md, &srcu_idx);
323 if (table)
goggin, edward269fd2a2005-09-27 21:45:44 -0700324 dm_table_event(table);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100325 dm_put_live_table(hc->md, srcu_idx);
goggin, edward269fd2a2005-09-27 21:45:44 -0700326
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100327 table = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (hc->new_map)
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100329 table = hc->new_map;
Mike Anderson1134e5ae2006-03-27 01:17:54 -0800330 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 free_cell(hc);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100332
333 return table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400336static void dm_hash_remove_all(bool keep_open_devices, bool mark_deferred, bool only_deferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Mikulas Patockab82096a2021-03-10 05:18:03 -0500338 int dev_skipped;
339 struct rb_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct hash_cell *hc;
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100341 struct mapped_device *md;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100342 struct dm_table *t;
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100343
344retry:
345 dev_skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 down_write(&_hash_lock);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700348
Mikulas Patockab82096a2021-03-10 05:18:03 -0500349 for (n = rb_first(&name_rb_tree); n; n = rb_next(n)) {
350 hc = container_of(n, struct hash_cell, name_node);
351 md = hc->md;
352 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700353
Mikulas Patockab82096a2021-03-10 05:18:03 -0500354 if (keep_open_devices &&
355 dm_lock_for_deletion(md, mark_deferred, only_deferred)) {
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100356 dm_put(md);
Mikulas Patockab82096a2021-03-10 05:18:03 -0500357 dev_skipped++;
358 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
Mikulas Patockab82096a2021-03-10 05:18:03 -0500360
361 t = __hash_remove(hc);
362
363 up_write(&_hash_lock);
364
365 if (t) {
366 dm_sync_table(md);
367 dm_table_destroy(t);
368 }
Tushar Sugandhi84010e52021-07-12 17:49:00 -0700369 dm_ima_measure_on_device_remove(md, true);
Mikulas Patockab82096a2021-03-10 05:18:03 -0500370 dm_put(md);
371 if (likely(keep_open_devices))
372 dm_destroy(md);
373 else
374 dm_destroy_immediate(md);
375
376 /*
377 * Some mapped devices may be using other mapped
378 * devices, so repeat until we make no further
379 * progress. If a new mapped device is created
380 * here it will also get removed.
381 */
382 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 up_write(&_hash_lock);
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100386
387 if (dev_skipped)
388 DMWARN("remove_all left %d open device(s)", dev_skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
Peter Jones84c89552011-01-13 19:59:47 +0000391/*
392 * Set the uuid of a hash_cell that isn't already set.
393 */
394static void __set_cell_uuid(struct hash_cell *hc, char *new_uuid)
395{
396 mutex_lock(&dm_hash_cells_mutex);
397 hc->uuid = new_uuid;
398 mutex_unlock(&dm_hash_cells_mutex);
399
Mikulas Patockab82096a2021-03-10 05:18:03 -0500400 __link_uuid(hc);
Peter Jones84c89552011-01-13 19:59:47 +0000401}
402
403/*
404 * Changes the name of a hash_cell and returns the old name for
405 * the caller to free.
406 */
407static char *__change_cell_name(struct hash_cell *hc, char *new_name)
408{
409 char *old_name;
410
411 /*
412 * Rename and move the name cell.
413 */
Mikulas Patockab82096a2021-03-10 05:18:03 -0500414 __unlink_name(hc);
Peter Jones84c89552011-01-13 19:59:47 +0000415 old_name = hc->name;
416
417 mutex_lock(&dm_hash_cells_mutex);
418 hc->name = new_name;
419 mutex_unlock(&dm_hash_cells_mutex);
420
Mikulas Patockab82096a2021-03-10 05:18:03 -0500421 __link_name(hc);
Peter Jones84c89552011-01-13 19:59:47 +0000422
423 return old_name;
424}
425
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100426static struct mapped_device *dm_hash_rename(struct dm_ioctl *param,
427 const char *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Peter Jones84c89552011-01-13 19:59:47 +0000429 char *new_data, *old_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct hash_cell *hc;
goggin, edward81f17772006-01-06 00:20:01 -0800431 struct dm_table *table;
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100432 struct mapped_device *md;
Heinz Mauelshagen86a32382023-01-25 21:14:58 +0100433 unsigned int change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100434 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /*
437 * duplicate new.
438 */
Peter Jones84c89552011-01-13 19:59:47 +0000439 new_data = kstrdup(new, GFP_KERNEL);
440 if (!new_data)
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100441 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 down_write(&_hash_lock);
444
445 /*
446 * Is new free ?
447 */
Peter Jones84c89552011-01-13 19:59:47 +0000448 if (change_uuid)
449 hc = __get_uuid_cell(new);
450 else
451 hc = __get_name_cell(new);
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (hc) {
Heinz Mauelshagen2e84fec2023-02-03 18:55:47 +0100454 DMERR("Unable to change %s on mapped device %s to one that already exists: %s",
Mikulas Patocka43e6c112022-08-24 07:25:57 -0400455 change_uuid ? "uuid" : "name",
456 param->name, new);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700457 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 up_write(&_hash_lock);
Peter Jones84c89552011-01-13 19:59:47 +0000459 kfree(new_data);
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100460 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462
463 /*
464 * Is there such a device as 'old' ?
465 */
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100466 hc = __get_name_cell(param->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (!hc) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -0400468 DMERR("Unable to rename non-existent device, %s to %s%s",
469 param->name, change_uuid ? "uuid " : "", new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 up_write(&_hash_lock);
Peter Jones84c89552011-01-13 19:59:47 +0000471 kfree(new_data);
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100472 return ERR_PTR(-ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
475 /*
Peter Jones84c89552011-01-13 19:59:47 +0000476 * Does this device already have a uuid?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 */
Peter Jones84c89552011-01-13 19:59:47 +0000478 if (change_uuid && hc->uuid) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -0400479 DMERR("Unable to change uuid of mapped device %s to %s "
480 "because uuid is already set to %s",
481 param->name, new, hc->uuid);
Peter Jones84c89552011-01-13 19:59:47 +0000482 dm_put(hc->md);
483 up_write(&_hash_lock);
484 kfree(new_data);
485 return ERR_PTR(-EINVAL);
486 }
487
488 if (change_uuid)
489 __set_cell_uuid(hc, new_data);
490 else
491 old_name = __change_cell_name(hc, new_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
goggin, edward81f17772006-01-06 00:20:01 -0800493 /*
494 * Wake up any dm event waiters.
495 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100496 table = dm_get_live_table(hc->md, &srcu_idx);
497 if (table)
goggin, edward81f17772006-01-06 00:20:01 -0800498 dm_table_event(table);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100499 dm_put_live_table(hc->md, srcu_idx);
goggin, edward81f17772006-01-06 00:20:01 -0800500
Mikulas Patocka7533afa2023-02-07 08:33:06 -0500501 if (!dm_kobject_uevent(hc->md, KOBJ_CHANGE, param->event_nr, false))
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100502 param->flags |= DM_UEVENT_GENERATED_FLAG;
Alasdair G Kergon69267a32007-12-13 14:15:57 +0000503
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100504 md = hc->md;
Tushar Sugandhi7d1d1df2021-07-12 17:49:02 -0700505
506 dm_ima_measure_on_device_rename(md);
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 up_write(&_hash_lock);
509 kfree(old_name);
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100510
511 return md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400514void dm_deferred_remove(void)
515{
516 dm_hash_remove_all(true, false, true);
517}
518
Heinz Mauelshagena4a82ce2023-01-26 15:48:30 +0100519/*
520 *---------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 * Implementation of the ioctl commands
Heinz Mauelshagena4a82ce2023-01-26 15:48:30 +0100522 *---------------------------------------------------------------
523 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/*
525 * All the ioctl commands get dispatched to functions with this
526 * prototype.
527 */
Mikulas Patockafc1841e2017-05-05 11:12:52 -0700528typedef int (*ioctl_fn)(struct file *filp, struct dm_ioctl *param, size_t param_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Mikulas Patockafc1841e2017-05-05 11:12:52 -0700530static int remove_all(struct file *filp, struct dm_ioctl *param, size_t param_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400532 dm_hash_remove_all(true, !!(param->flags & DM_DEFERRED_REMOVE), false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 param->data_size = 0;
534 return 0;
535}
536
537/*
538 * Round up the ptr to an 8-byte boundary.
539 */
540#define ALIGN_MASK 7
Mikulas Patocka62e08242017-09-20 07:29:49 -0400541static inline size_t align_val(size_t val)
542{
543 return (val + ALIGN_MASK) & ~ALIGN_MASK;
544}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545static inline void *align_ptr(void *ptr)
546{
Mikulas Patocka62e08242017-09-20 07:29:49 -0400547 return (void *)align_val((size_t)ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550/*
551 * Retrieves the data payload buffer from an already allocated
552 * struct dm_ioctl.
553 */
554static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
555 size_t *len)
556{
557 param->data_start = align_ptr(param + 1) - (void *) param;
558
559 if (param->data_start < param_size)
560 *len = param_size - param->data_start;
561 else
562 *len = 0;
563
564 return ((void *) param) + param->data_start;
565}
566
Mikulas Patockac9090852021-03-11 13:27:29 -0500567static bool filter_device(struct hash_cell *hc, const char *pfx_name, const char *pfx_uuid)
568{
569 const char *val;
570 size_t val_len, pfx_len;
571
572 val = hc->name;
573 val_len = strlen(val);
574 pfx_len = strnlen(pfx_name, DM_NAME_LEN);
575 if (pfx_len > val_len)
576 return false;
577 if (memcmp(val, pfx_name, pfx_len))
578 return false;
579
580 val = hc->uuid ? hc->uuid : "";
581 val_len = strlen(val);
582 pfx_len = strnlen(pfx_uuid, DM_UUID_LEN);
583 if (pfx_len > val_len)
584 return false;
585 if (memcmp(val, pfx_uuid, pfx_len))
586 return false;
587
588 return true;
589}
590
Mikulas Patockafc1841e2017-05-05 11:12:52 -0700591static int list_devices(struct file *filp, struct dm_ioctl *param, size_t param_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Mikulas Patockab82096a2021-03-10 05:18:03 -0500593 struct rb_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 struct hash_cell *hc;
595 size_t len, needed = 0;
596 struct gendisk *disk;
Mikulas Patocka62e08242017-09-20 07:29:49 -0400597 struct dm_name_list *orig_nl, *nl, *old_nl = NULL;
Mikulas Patocka23d70c52017-01-16 16:07:01 -0500598 uint32_t *event_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 down_write(&_hash_lock);
601
602 /*
603 * Loop through all the devices working out how much
604 * space we need.
605 */
Mikulas Patockab82096a2021-03-10 05:18:03 -0500606 for (n = rb_first(&name_rb_tree); n; n = rb_next(n)) {
607 hc = container_of(n, struct hash_cell, name_node);
Mikulas Patockac9090852021-03-11 13:27:29 -0500608 if (!filter_device(hc, param->name, param->uuid))
609 continue;
Mikulas Patockab82096a2021-03-10 05:18:03 -0500610 needed += align_val(offsetof(struct dm_name_list, name) + strlen(hc->name) + 1);
Mikulas Patocka8b638082021-03-12 09:07:30 -0500611 needed += align_val(sizeof(uint32_t) * 2);
612 if (param->flags & DM_UUID_FLAG && hc->uuid)
613 needed += align_val(strlen(hc->uuid) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615
616 /*
617 * Grab our output buffer.
618 */
Mikulas Patocka62e08242017-09-20 07:29:49 -0400619 nl = orig_nl = get_result_buffer(param, param_size, &len);
Mikulas Patocka4edbe1d2021-03-26 14:32:32 -0400620 if (len < needed || len < sizeof(nl->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 param->flags |= DM_BUFFER_FULL_FLAG;
622 goto out;
623 }
624 param->data_size = param->data_start + needed;
625
626 nl->dev = 0; /* Flags no data */
627
628 /*
629 * Now loop through filling out the names.
630 */
Mikulas Patockab82096a2021-03-10 05:18:03 -0500631 for (n = rb_first(&name_rb_tree); n; n = rb_next(n)) {
Mikulas Patocka8b638082021-03-12 09:07:30 -0500632 void *uuid_ptr;
Heinz Mauelshagen0ef0b472023-02-01 23:42:29 +0100633
Mikulas Patockab82096a2021-03-10 05:18:03 -0500634 hc = container_of(n, struct hash_cell, name_node);
Mikulas Patockac9090852021-03-11 13:27:29 -0500635 if (!filter_device(hc, param->name, param->uuid))
636 continue;
Mikulas Patockab82096a2021-03-10 05:18:03 -0500637 if (old_nl)
638 old_nl->next = (uint32_t) ((void *) nl -
639 (void *) old_nl);
640 disk = dm_disk(hc->md);
641 nl->dev = huge_encode_dev(disk_devt(disk));
642 nl->next = 0;
643 strcpy(nl->name, hc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Mikulas Patockab82096a2021-03-10 05:18:03 -0500645 old_nl = nl;
646 event_nr = align_ptr(nl->name + strlen(hc->name) + 1);
Mikulas Patocka8b638082021-03-12 09:07:30 -0500647 event_nr[0] = dm_get_event_nr(hc->md);
648 event_nr[1] = 0;
649 uuid_ptr = align_ptr(event_nr + 2);
650 if (param->flags & DM_UUID_FLAG) {
651 if (hc->uuid) {
652 event_nr[1] |= DM_NAME_LIST_FLAG_HAS_UUID;
653 strcpy(uuid_ptr, hc->uuid);
654 uuid_ptr = align_ptr(uuid_ptr + strlen(hc->uuid) + 1);
655 } else {
656 event_nr[1] |= DM_NAME_LIST_FLAG_DOESNT_HAVE_UUID;
657 }
658 }
659 nl = uuid_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Mikulas Patocka62e08242017-09-20 07:29:49 -0400661 /*
662 * If mismatch happens, security may be compromised due to buffer
663 * overflow, so it's better to crash.
664 */
665 BUG_ON((char *)nl - (char *)orig_nl != needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 out:
668 up_write(&_hash_lock);
669 return 0;
670}
671
672static void list_version_get_needed(struct target_type *tt, void *needed_param)
673{
Heinz Mauelshagen8ca817c2023-02-01 22:31:43 +0100674 size_t *needed = needed_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Heinz Mauelshagen8ca817c2023-02-01 22:31:43 +0100676 *needed += sizeof(struct dm_target_versions);
677 *needed += strlen(tt->name) + 1;
678 *needed += ALIGN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681static void list_version_get_info(struct target_type *tt, void *param)
682{
Heinz Mauelshagen8ca817c2023-02-01 22:31:43 +0100683 struct vers_iter *info = param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Heinz Mauelshagen8ca817c2023-02-01 22:31:43 +0100685 /* Check space - it might have changed since the first iteration */
686 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 > info->end) {
687 info->flags = DM_BUFFER_FULL_FLAG;
688 return;
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Heinz Mauelshagen8ca817c2023-02-01 22:31:43 +0100691 if (info->old_vers)
692 info->old_vers->next = (uint32_t) ((void *)info->vers - (void *)info->old_vers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Heinz Mauelshagen8ca817c2023-02-01 22:31:43 +0100694 info->vers->version[0] = tt->version[0];
695 info->vers->version[1] = tt->version[1];
696 info->vers->version[2] = tt->version[2];
697 info->vers->next = 0;
698 strcpy(info->vers->name, tt->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Heinz Mauelshagen8ca817c2023-02-01 22:31:43 +0100700 info->old_vers = info->vers;
701 info->vers = align_ptr((void *)(info->vers + 1) + strlen(tt->name) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Mikulas Patockaafa179e2019-09-16 05:55:42 -0400704static int __list_versions(struct dm_ioctl *param, size_t param_size, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 size_t len, needed = 0;
707 struct dm_target_versions *vers;
708 struct vers_iter iter_info;
Mikulas Patockaafa179e2019-09-16 05:55:42 -0400709 struct target_type *tt = NULL;
710
711 if (name) {
712 tt = dm_get_target_type(name);
713 if (!tt)
714 return -EINVAL;
715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 /*
718 * Loop through all the devices working out how much
719 * space we need.
720 */
Mikulas Patockaafa179e2019-09-16 05:55:42 -0400721 if (!tt)
722 dm_target_iterate(list_version_get_needed, &needed);
723 else
724 list_version_get_needed(tt, &needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 /*
727 * Grab our output buffer.
728 */
729 vers = get_result_buffer(param, param_size, &len);
730 if (len < needed) {
731 param->flags |= DM_BUFFER_FULL_FLAG;
732 goto out;
733 }
734 param->data_size = param->data_start + needed;
735
736 iter_info.param_size = param_size;
737 iter_info.old_vers = NULL;
738 iter_info.vers = vers;
739 iter_info.flags = 0;
Mikulas Patocka4fe1ec92022-11-01 16:53:35 -0400740 iter_info.end = (char *)vers + needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 /*
743 * Now loop through filling out the names & versions.
744 */
Mikulas Patockaafa179e2019-09-16 05:55:42 -0400745 if (!tt)
746 dm_target_iterate(list_version_get_info, &iter_info);
747 else
748 list_version_get_info(tt, &iter_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 param->flags |= iter_info.flags;
750
751 out:
Mikulas Patockaafa179e2019-09-16 05:55:42 -0400752 if (tt)
753 dm_put_target_type(tt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return 0;
755}
756
Mikulas Patockaafa179e2019-09-16 05:55:42 -0400757static int list_versions(struct file *filp, struct dm_ioctl *param, size_t param_size)
758{
759 return __list_versions(param, param_size, NULL);
760}
761
762static int get_target_version(struct file *filp, struct dm_ioctl *param, size_t param_size)
763{
764 return __list_versions(param, param_size, param->name);
765}
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767static int check_name(const char *name)
768{
769 if (strchr(name, '/')) {
Demi Marie Obenoura85f1a92023-06-03 10:52:43 -0400770 DMERR("device name cannot contain '/'");
771 return -EINVAL;
772 }
773
Demi Marie Obenour81ca2db2023-06-03 10:52:44 -0400774 if (strcmp(name, DM_CONTROL_NODE) == 0 ||
775 strcmp(name, ".") == 0 ||
776 strcmp(name, "..") == 0) {
777 DMERR("device name cannot be \"%s\", \".\", or \"..\"", DM_CONTROL_NODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return -EINVAL;
779 }
780
781 return 0;
782}
783
784/*
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000785 * On successful return, the caller must not attempt to acquire
Junxiao Bi88e2f902014-12-24 14:48:12 +0800786 * _hash_lock without first calling dm_put_live_table, because dm_table_destroy
787 * waits for this dm_put_live_table and could be called under this lock.
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000788 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100789static struct dm_table *dm_get_inactive_table(struct mapped_device *md, int *srcu_idx)
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000790{
791 struct hash_cell *hc;
792 struct dm_table *table = NULL;
793
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100794 /* increment rcu count, we don't care about the table pointer */
795 dm_get_live_table(md, srcu_idx);
796
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000797 down_read(&_hash_lock);
798 hc = dm_get_mdptr(md);
Hou Taoa2f998a2022-12-16 12:23:53 +0800799 if (!hc) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -0400800 DMERR("device has been removed from the dev hash table.");
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000801 goto out;
802 }
803
804 table = hc->new_map;
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000805
806out:
807 up_read(&_hash_lock);
808
809 return table;
810}
811
812static struct dm_table *dm_get_live_or_inactive_table(struct mapped_device *md,
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100813 struct dm_ioctl *param,
814 int *srcu_idx)
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000815{
816 return (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) ?
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100817 dm_get_inactive_table(md, srcu_idx) : dm_get_live_table(md, srcu_idx);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000818}
819
820/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 * Fills in a dm_ioctl structure, ready for sending back to
822 * userland.
823 */
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100824static void __dev_status(struct mapped_device *md, struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 struct gendisk *disk = dm_disk(md);
827 struct dm_table *table;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100828 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
Mike Snitzerffcc3932014-10-28 18:34:52 -0400831 DM_ACTIVE_PRESENT_FLAG | DM_INTERNAL_SUSPEND_FLAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000833 if (dm_suspended_md(md))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 param->flags |= DM_SUSPEND_FLAG;
835
Mike Snitzerffcc3932014-10-28 18:34:52 -0400836 if (dm_suspended_internally_md(md))
837 param->flags |= DM_INTERNAL_SUSPEND_FLAG;
838
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400839 if (dm_test_deferred_remove_flag(md))
840 param->flags |= DM_DEFERRED_REMOVE;
841
Tejun Heof331c022008-09-03 09:01:48 +0200842 param->dev = huge_encode_dev(disk_devt(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700844 /*
845 * Yes, this will be out of date by the time it gets back
846 * to userland, but it is still very useful for
847 * debugging.
848 */
849 param->open_count = dm_open_count(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 param->event_nr = dm_get_event_nr(md);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000852 param->target_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100854 table = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (table) {
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000856 if (!(param->flags & DM_QUERY_INACTIVE_TABLE_FLAG)) {
857 if (get_disk_ro(disk))
858 param->flags |= DM_READONLY_FLAG;
Mike Snitzer2aec3772022-07-05 14:00:36 -0400859 param->target_count = table->num_targets;
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000860 }
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000861
862 param->flags |= DM_ACTIVE_PRESENT_FLAG;
863 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100864 dm_put_live_table(md, srcu_idx);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000865
866 if (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100867 int srcu_idx;
Heinz Mauelshagen0ef0b472023-02-01 23:42:29 +0100868
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100869 table = dm_get_inactive_table(md, &srcu_idx);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000870 if (table) {
Christoph Hellwig05bdb992023-06-08 13:02:55 +0200871 if (!(dm_table_get_mode(table) & BLK_OPEN_WRITE))
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000872 param->flags |= DM_READONLY_FLAG;
Mike Snitzer2aec3772022-07-05 14:00:36 -0400873 param->target_count = table->num_targets;
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000874 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100875 dm_put_live_table(md, srcu_idx);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Mikulas Patockafc1841e2017-05-05 11:12:52 -0700879static int dev_create(struct file *filp, struct dm_ioctl *param, size_t param_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700881 int r, m = DM_ANY_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct mapped_device *md;
883
884 r = check_name(param->name);
885 if (r)
886 return r;
887
888 if (param->flags & DM_PERSISTENT_DEV_FLAG)
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700889 m = MINOR(huge_decode_dev(param->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700891 r = dm_create(m, &md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (r)
893 return r;
894
895 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +0100896 if (r) {
897 dm_put(md);
898 dm_destroy(md);
899 return r;
900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
903
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100904 __dev_status(md, param);
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 dm_put(md);
907
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +0100908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911/*
912 * Always use UUID for lookups if it's present, otherwise use name or dev.
913 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800914static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100916 struct hash_cell *hc = NULL;
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800917
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100918 if (*param->uuid) {
Mikulas Patockadbdcc902022-03-20 17:12:46 -0400919 if (*param->name || param->dev) {
920 DMERR("Invalid ioctl structure: uuid %s, name %s, dev %llx",
921 param->uuid, param->name, (unsigned long long)param->dev);
Mikulas Patocka759dea22011-08-02 12:32:06 +0100922 return NULL;
Mikulas Patockadbdcc902022-03-20 17:12:46 -0400923 }
Mikulas Patocka759dea22011-08-02 12:32:06 +0100924
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100925 hc = __get_uuid_cell(param->uuid);
926 if (!hc)
927 return NULL;
Mikulas Patockaba2e19b2011-08-02 12:32:06 +0100928 } else if (*param->name) {
Mikulas Patockadbdcc902022-03-20 17:12:46 -0400929 if (param->dev) {
930 DMERR("Invalid ioctl structure: name %s, dev %llx",
931 param->name, (unsigned long long)param->dev);
Mikulas Patocka759dea22011-08-02 12:32:06 +0100932 return NULL;
Mikulas Patockadbdcc902022-03-20 17:12:46 -0400933 }
Mikulas Patocka759dea22011-08-02 12:32:06 +0100934
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100935 hc = __get_name_cell(param->name);
936 if (!hc)
937 return NULL;
Mikulas Patockaba2e19b2011-08-02 12:32:06 +0100938 } else if (param->dev) {
939 hc = __get_dev_cell(param->dev);
940 if (!hc)
941 return NULL;
942 } else
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100943 return NULL;
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800944
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100945 /*
946 * Sneakily write in both the name and the uuid
947 * while we have the cell.
948 */
Heinz Mauelshagen22a8b842023-02-07 23:04:01 +0100949 strscpy(param->name, hc->name, sizeof(param->name));
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100950 if (hc->uuid)
Heinz Mauelshagen22a8b842023-02-07 23:04:01 +0100951 strscpy(param->uuid, hc->uuid, sizeof(param->uuid));
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100952 else
953 param->uuid[0] = '\0';
954
955 if (hc->new_map)
956 param->flags |= DM_INACTIVE_PRESENT_FLAG;
957 else
958 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
959
960 return hc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Arjan van de Ven858119e2006-01-14 13:20:43 -0800963static struct mapped_device *find_device(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 struct hash_cell *hc;
966 struct mapped_device *md = NULL;
967
968 down_read(&_hash_lock);
969 hc = __find_device_hash_cell(param);
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100970 if (hc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 up_read(&_hash_lock);
973
974 return md;
975}
976
Mikulas Patockafc1841e2017-05-05 11:12:52 -0700977static int dev_remove(struct file *filp, struct dm_ioctl *param, size_t param_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 struct hash_cell *hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700980 struct mapped_device *md;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700981 int r;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100982 struct dm_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 down_write(&_hash_lock);
985 hc = __find_device_hash_cell(param);
986
987 if (!hc) {
Milan Broz810b4922011-01-13 19:59:55 +0000988 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 up_write(&_hash_lock);
990 return -ENXIO;
991 }
992
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700993 md = hc->md;
994
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700995 /*
996 * Ensure the device is not open and nothing further can open it.
997 */
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400998 r = dm_lock_for_deletion(md, !!(param->flags & DM_DEFERRED_REMOVE), false);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700999 if (r) {
Mikulas Patocka2c140a22013-11-01 18:27:41 -04001000 if (r == -EBUSY && param->flags & DM_DEFERRED_REMOVE) {
1001 up_write(&_hash_lock);
1002 dm_put(md);
1003 return 0;
1004 }
Milan Broz810b4922011-01-13 19:59:55 +00001005 DMDEBUG_LIMIT("unable to remove open device %s", hc->name);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -07001006 up_write(&_hash_lock);
1007 dm_put(md);
1008 return r;
1009 }
1010
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001011 t = __hash_remove(hc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 up_write(&_hash_lock);
Milan Broz60935eb2009-06-22 10:12:30 +01001013
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001014 if (t) {
1015 dm_sync_table(md);
1016 dm_table_destroy(t);
1017 }
1018
Mikulas Patocka2c140a22013-11-01 18:27:41 -04001019 param->flags &= ~DM_DEFERRED_REMOVE;
1020
Tushar Sugandhi84010e52021-07-12 17:49:00 -07001021 dm_ima_measure_on_device_remove(md, false);
1022
Mikulas Patocka7533afa2023-02-07 08:33:06 -05001023 if (!dm_kobject_uevent(md, KOBJ_REMOVE, param->event_nr, false))
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00001024 param->flags |= DM_UEVENT_GENERATED_FLAG;
Milan Broz60935eb2009-06-22 10:12:30 +01001025
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001026 dm_put(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +01001027 dm_destroy(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return 0;
1029}
1030
1031/*
1032 * Check a string doesn't overrun the chunk of
1033 * memory we copied from userland.
1034 */
1035static int invalid_str(char *str, void *end)
1036{
1037 while ((void *) str < end)
1038 if (!*str++)
1039 return 0;
1040
1041 return -EINVAL;
1042}
1043
Mikulas Patockafc1841e2017-05-05 11:12:52 -07001044static int dev_rename(struct file *filp, struct dm_ioctl *param, size_t param_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 int r;
Peter Jones84c89552011-01-13 19:59:47 +00001047 char *new_data = (char *) param + param->data_start;
Peter Rajnoha856a6f12010-08-12 04:13:53 +01001048 struct mapped_device *md;
Heinz Mauelshagen86a32382023-01-25 21:14:58 +01001049 unsigned int change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Peter Jones84c89552011-01-13 19:59:47 +00001051 if (new_data < param->data ||
Alasdair Kergonc2b04822013-08-29 16:37:45 +01001052 invalid_str(new_data, (void *) param + param_size) || !*new_data ||
Peter Jones84c89552011-01-13 19:59:47 +00001053 strlen(new_data) > (change_uuid ? DM_UUID_LEN - 1 : DM_NAME_LEN - 1)) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04001054 DMERR("Invalid new mapped device name or uuid string supplied.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return -EINVAL;
1056 }
1057
Peter Jones84c89552011-01-13 19:59:47 +00001058 if (!change_uuid) {
1059 r = check_name(new_data);
1060 if (r)
1061 return r;
1062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Peter Jones84c89552011-01-13 19:59:47 +00001064 md = dm_hash_rename(param, new_data);
Peter Rajnoha856a6f12010-08-12 04:13:53 +01001065 if (IS_ERR(md))
1066 return PTR_ERR(md);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00001067
Peter Rajnoha856a6f12010-08-12 04:13:53 +01001068 __dev_status(md, param);
1069 dm_put(md);
1070
1071 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Mikulas Patockafc1841e2017-05-05 11:12:52 -07001074static int dev_set_geometry(struct file *filp, struct dm_ioctl *param, size_t param_size)
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001075{
1076 int r = -EINVAL, x;
1077 struct mapped_device *md;
1078 struct hd_geometry geometry;
1079 unsigned long indata[4];
1080 char *geostr = (char *) param + param->data_start;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001081 char dummy;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001082
1083 md = find_device(param);
1084 if (!md)
1085 return -ENXIO;
1086
Alasdair G Kergon27238b22008-02-08 02:09:53 +00001087 if (geostr < param->data ||
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001088 invalid_str(geostr, (void *) param + param_size)) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04001089 DMERR("Invalid geometry supplied.");
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001090 goto out;
1091 }
1092
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001093 x = sscanf(geostr, "%lu %lu %lu %lu%c", indata,
1094 indata + 1, indata + 2, indata + 3, &dummy);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001095
1096 if (x != 4) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04001097 DMERR("Unable to interpret geometry settings.");
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001098 goto out;
1099 }
1100
Sergey Shtylyov151d8122023-01-17 23:59:55 +03001101 if (indata[0] > 65535 || indata[1] > 255 || indata[2] > 255) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04001102 DMERR("Geometry exceeds range limits.");
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001103 goto out;
1104 }
1105
1106 geometry.cylinders = indata[0];
1107 geometry.heads = indata[1];
1108 geometry.sectors = indata[2];
1109 geometry.start = indata[3];
1110
1111 r = dm_set_geometry(md, &geometry);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001112
1113 param->data_size = 0;
1114
1115out:
1116 dm_put(md);
1117 return r;
1118}
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120static int do_suspend(struct dm_ioctl *param)
1121{
1122 int r = 0;
Heinz Mauelshagen86a32382023-01-25 21:14:58 +01001123 unsigned int suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 struct mapped_device *md;
1125
1126 md = find_device(param);
1127 if (!md)
1128 return -ENXIO;
1129
Alasdair G Kergon6da487d2006-01-06 00:20:07 -08001130 if (param->flags & DM_SKIP_LOCKFS_FLAG)
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08001131 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
Kiyoshi Ueda81fdb092006-12-08 02:41:07 -08001132 if (param->flags & DM_NOFLUSH_FLAG)
1133 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
Alasdair G Kergon6da487d2006-01-06 00:20:07 -08001134
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001135 if (!dm_suspended_md(md)) {
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08001136 r = dm_suspend(md, suspend_flags);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001137 if (r)
1138 goto out;
1139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001141 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001143out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return r;
1147}
1148
1149static int do_resume(struct dm_ioctl *param)
1150{
1151 int r = 0;
Heinz Mauelshagen86a32382023-01-25 21:14:58 +01001152 unsigned int suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 struct hash_cell *hc;
1154 struct mapped_device *md;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001155 struct dm_table *new_map, *old_map = NULL;
Mikulas Patocka7533afa2023-02-07 08:33:06 -05001156 bool need_resize_uevent = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 down_write(&_hash_lock);
1159
1160 hc = __find_device_hash_cell(param);
1161 if (!hc) {
Milan Broz810b4922011-01-13 19:59:55 +00001162 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 up_write(&_hash_lock);
1164 return -ENXIO;
1165 }
1166
1167 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 new_map = hc->new_map;
1170 hc->new_map = NULL;
1171 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1172
1173 up_write(&_hash_lock);
1174
1175 /* Do we need to load a new map ? */
1176 if (new_map) {
Mikulas Patocka7533afa2023-02-07 08:33:06 -05001177 sector_t old_size, new_size;
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 /* Suspend if it isn't already suspended */
Li Lingfeng27609042023-06-01 14:14:23 +08001180 if (param->flags & DM_SKIP_LOCKFS_FLAG)
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08001181 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
Kiyoshi Ueda81fdb092006-12-08 02:41:07 -08001182 if (param->flags & DM_NOFLUSH_FLAG)
1183 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00001184 if (!dm_suspended_md(md))
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08001185 dm_suspend(md, suspend_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Mikulas Patocka7533afa2023-02-07 08:33:06 -05001187 old_size = dm_get_size(md);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001188 old_map = dm_swap_table(md, new_map);
1189 if (IS_ERR(old_map)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001190 dm_sync_table(md);
Mikulas Patockad5816872009-01-06 03:05:10 +00001191 dm_table_destroy(new_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 dm_put(md);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001193 return PTR_ERR(old_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
Mikulas Patocka7533afa2023-02-07 08:33:06 -05001195 new_size = dm_get_size(md);
1196 if (old_size && new_size && old_size != new_size)
1197 need_resize_uevent = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Christoph Hellwig05bdb992023-06-08 13:02:55 +02001199 if (dm_table_get_mode(new_map) & BLK_OPEN_WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 set_disk_ro(dm_disk(md), 0);
1201 else
1202 set_disk_ro(dm_disk(md), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204
Mike Snitzer0f3649a92010-03-06 02:32:24 +00001205 if (dm_suspended_md(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 r = dm_resume(md);
Tushar Sugandhi8eb6fab2021-07-12 17:48:59 -07001207 if (!r) {
1208 dm_ima_measure_on_device_resume(md, new_map ? true : false);
1209
Mikulas Patocka7533afa2023-02-07 08:33:06 -05001210 if (!dm_kobject_uevent(md, KOBJ_CHANGE, param->event_nr, need_resize_uevent))
Tushar Sugandhi8eb6fab2021-07-12 17:48:59 -07001211 param->flags |= DM_UEVENT_GENERATED_FLAG;
1212 }
Mike Snitzer0f3649a92010-03-06 02:32:24 +00001213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001215 /*
1216 * Since dm_swap_table synchronizes RCU, nobody should be in
1217 * read-side critical section already.
1218 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001219 if (old_map)
1220 dm_table_destroy(old_map);
Milan Broz60935eb2009-06-22 10:12:30 +01001221
Mike Snitzer0f3649a92010-03-06 02:32:24 +00001222 if (!r)
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001223 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 dm_put(md);
1226 return r;
1227}
1228
1229/*
1230 * Set or unset the suspension state of a device.
1231 * If the device already is in the requested state we just return its status.
1232 */
Mikulas Patockafc1841e2017-05-05 11:12:52 -07001233static int dev_suspend(struct file *filp, struct dm_ioctl *param, size_t param_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 if (param->flags & DM_SUSPEND_FLAG)
1236 return do_suspend(param);
1237
1238 return do_resume(param);
1239}
1240
1241/*
1242 * Copies device info back to user space, used by
1243 * the create and info ioctls.
1244 */
Mikulas Patockafc1841e2017-05-05 11:12:52 -07001245static int dev_status(struct file *filp, struct dm_ioctl *param, size_t param_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 struct mapped_device *md;
1248
1249 md = find_device(param);
1250 if (!md)
1251 return -ENXIO;
1252
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001253 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001255
1256 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
1259/*
1260 * Build up the status struct for each target
1261 */
1262static void retrieve_status(struct dm_table *table,
1263 struct dm_ioctl *param, size_t param_size)
1264{
1265 unsigned int i, num_targets;
1266 struct dm_target_spec *spec;
1267 char *outbuf, *outptr;
1268 status_type_t type;
1269 size_t remaining, len, used = 0;
Heinz Mauelshagen86a32382023-01-25 21:14:58 +01001270 unsigned int status_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 outptr = outbuf = get_result_buffer(param, param_size, &len);
1273
1274 if (param->flags & DM_STATUS_TABLE_FLAG)
1275 type = STATUSTYPE_TABLE;
Tushar Sugandhi91ccbba2021-07-12 17:48:58 -07001276 else if (param->flags & DM_IMA_MEASUREMENT_FLAG)
1277 type = STATUSTYPE_IMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 else
1279 type = STATUSTYPE_INFO;
1280
1281 /* Get all the target info */
Mike Snitzer2aec3772022-07-05 14:00:36 -04001282 num_targets = table->num_targets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 for (i = 0; i < num_targets; i++) {
1284 struct dm_target *ti = dm_table_get_target(table, i);
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00001285 size_t l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 remaining = len - (outptr - outbuf);
1288 if (remaining <= sizeof(struct dm_target_spec)) {
1289 param->flags |= DM_BUFFER_FULL_FLAG;
1290 break;
1291 }
1292
1293 spec = (struct dm_target_spec *) outptr;
1294
1295 spec->status = 0;
1296 spec->sector_start = ti->begin;
1297 spec->length = ti->len;
Justin Stitt0ffb6452023-09-25 06:54:51 +00001298 strscpy_pad(spec->target_type, ti->type->name,
1299 sizeof(spec->target_type));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 outptr += sizeof(struct dm_target_spec);
1302 remaining = len - (outptr - outbuf);
1303 if (remaining <= 0) {
1304 param->flags |= DM_BUFFER_FULL_FLAG;
1305 break;
1306 }
1307
1308 /* Get the status/table string from the target driver */
1309 if (ti->type->status) {
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01001310 if (param->flags & DM_NOFLUSH_FLAG)
1311 status_flags |= DM_STATUS_NOFLUSH_FLAG;
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00001312 ti->type->status(ti, type, status_flags, outptr, remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 } else
1314 outptr[0] = '\0';
1315
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00001316 l = strlen(outptr) + 1;
1317 if (l == remaining) {
1318 param->flags |= DM_BUFFER_FULL_FLAG;
1319 break;
1320 }
1321
1322 outptr += l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 used = param->data_start + (outptr - outbuf);
1324
1325 outptr = align_ptr(outptr);
1326 spec->next = outptr - outbuf;
1327 }
1328
1329 if (used)
1330 param->data_size = used;
1331
1332 param->target_count = num_targets;
1333}
1334
1335/*
1336 * Wait for a device to report an event
1337 */
Mikulas Patockafc1841e2017-05-05 11:12:52 -07001338static int dev_wait(struct file *filp, struct dm_ioctl *param, size_t param_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001340 int r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 struct mapped_device *md;
1342 struct dm_table *table;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001343 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 md = find_device(param);
1346 if (!md)
1347 return -ENXIO;
1348
1349 /*
1350 * Wait for a notification event
1351 */
1352 if (dm_wait_event(md, param->event_nr)) {
1353 r = -ERESTARTSYS;
1354 goto out;
1355 }
1356
1357 /*
1358 * The userland program is going to want to know what
1359 * changed to trigger the event, so we may as well tell
1360 * him and save an ioctl.
1361 */
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001362 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001364 table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
1365 if (table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 retrieve_status(table, param, param_size);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001367 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001369out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return r;
1373}
1374
Mikulas Patockafc1841e2017-05-05 11:12:52 -07001375/*
1376 * Remember the global event number and make it possible to poll
1377 * for further events.
1378 */
1379static int dev_arm_poll(struct file *filp, struct dm_ioctl *param, size_t param_size)
1380{
1381 struct dm_file *priv = filp->private_data;
1382
1383 priv->global_event_nr = atomic_read(&dm_global_event_nr);
1384
1385 return 0;
1386}
1387
Christoph Hellwig05bdb992023-06-08 13:02:55 +02001388static inline blk_mode_t get_mode(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Christoph Hellwig05bdb992023-06-08 13:02:55 +02001390 blk_mode_t mode = BLK_OPEN_READ | BLK_OPEN_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 if (param->flags & DM_READONLY_FLAG)
Christoph Hellwig05bdb992023-06-08 13:02:55 +02001393 mode = BLK_OPEN_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 return mode;
1396}
1397
Demi Marie Obenour10655c72023-06-03 10:52:41 -04001398static int next_target(struct dm_target_spec *last, uint32_t next, const char *end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 struct dm_target_spec **spec, char **target_params)
1400{
Demi Marie Obenourb60528d2023-06-03 10:52:39 -04001401 static_assert(__alignof__(struct dm_target_spec) <= 8,
1402 "struct dm_target_spec must not require more than 8-byte alignment");
1403
Demi Marie Obenour13f4a692023-06-03 10:52:40 -04001404 /*
1405 * Number of bytes remaining, starting with last. This is always
1406 * sizeof(struct dm_target_spec) or more, as otherwise *last was
1407 * out of bounds already.
1408 */
Demi Marie Obenour10655c72023-06-03 10:52:41 -04001409 size_t remaining = end - (char *)last;
Demi Marie Obenour13f4a692023-06-03 10:52:40 -04001410
1411 /*
1412 * There must be room for both the next target spec and the
1413 * NUL-terminator of the target itself.
1414 */
1415 if (remaining - sizeof(struct dm_target_spec) <= next) {
1416 DMERR("Target spec extends beyond end of parameters");
1417 return -EINVAL;
1418 }
1419
Demi Marie Obenourb60528d2023-06-03 10:52:39 -04001420 if (next % __alignof__(struct dm_target_spec)) {
1421 DMERR("Next dm_target_spec (offset %u) is not %zu-byte aligned",
1422 next, __alignof__(struct dm_target_spec));
1423 return -EINVAL;
1424 }
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
1427 *target_params = (char *) (*spec + 1);
1428
Demi Marie Obenour10655c72023-06-03 10:52:41 -04001429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
1432static int populate_table(struct dm_table *table,
1433 struct dm_ioctl *param, size_t param_size)
1434{
1435 int r;
1436 unsigned int i = 0;
1437 struct dm_target_spec *spec = (struct dm_target_spec *) param;
1438 uint32_t next = param->data_start;
Demi Marie Obenour10655c72023-06-03 10:52:41 -04001439 const char *const end = (const char *) param + param_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 char *target_params;
Demi Marie Obenour10655c72023-06-03 10:52:41 -04001441 size_t min_size = sizeof(struct dm_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 if (!param->target_count) {
Heinz Mauelshagen1c131882023-02-06 23:42:32 +01001444 DMERR("%s: no targets specified", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return -EINVAL;
1446 }
1447
1448 for (i = 0; i < param->target_count; i++) {
Demi Marie Obenour10655c72023-06-03 10:52:41 -04001449 const char *nul_terminator;
1450
1451 if (next < min_size) {
1452 DMERR("%s: next target spec (offset %u) overlaps %s",
1453 __func__, next, i ? "previous target" : "'struct dm_ioctl'");
1454 return -EINVAL;
1455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 r = next_target(spec, next, end, &spec, &target_params);
1458 if (r) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04001459 DMERR("unable to find target");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return r;
1461 }
1462
Demi Marie Obenour10655c72023-06-03 10:52:41 -04001463 nul_terminator = memchr(target_params, 0, (size_t)(end - target_params));
1464 if (nul_terminator == NULL) {
1465 DMERR("%s: target parameters not NUL-terminated", __func__);
1466 return -EINVAL;
1467 }
1468
1469 /* Add 1 for NUL terminator */
1470 min_size = (size_t)(nul_terminator - (const char *)spec) + 1;
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 r = dm_table_add_target(table, spec->target_type,
1473 (sector_t) spec->sector_start,
1474 (sector_t) spec->length,
1475 target_params);
1476 if (r) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04001477 DMERR("error adding target to table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return r;
1479 }
1480
1481 next = spec->next;
1482 }
1483
1484 return dm_table_complete(table);
1485}
1486
Bart Van Assche7e0d5742017-04-27 10:11:23 -07001487static bool is_valid_type(enum dm_queue_mode cur, enum dm_queue_mode new)
Toshi Kanib5ab4a92016-06-28 13:37:15 -06001488{
1489 if (cur == new ||
1490 (cur == DM_TYPE_BIO_BASED && new == DM_TYPE_DAX_BIO_BASED))
1491 return true;
1492
1493 return false;
1494}
1495
Mikulas Patockafc1841e2017-05-05 11:12:52 -07001496static int table_load(struct file *filp, struct dm_ioctl *param, size_t param_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 int r;
1499 struct hash_cell *hc;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001500 struct dm_table *t, *old_map = NULL;
Mike Anderson1134e5ae2006-03-27 01:17:54 -08001501 struct mapped_device *md;
Alasdair G Kergon36a04562011-10-31 20:19:04 +00001502 struct target_type *immutable_target_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Mike Anderson1134e5ae2006-03-27 01:17:54 -08001504 md = find_device(param);
1505 if (!md)
1506 return -ENXIO;
1507
1508 r = dm_table_create(&t, get_mode(param), param->target_count, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (r)
Mike Snitzerf11c1c52013-08-27 20:03:00 -04001510 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Mike Snitzer00c4fc32013-08-27 18:57:03 -04001512 /* Protect md->type and md->queue against concurrent table loads. */
1513 dm_lock_md_type(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 r = populate_table(t, param, param_size);
Mike Snitzerf11c1c52013-08-27 20:03:00 -04001515 if (r)
1516 goto err_unlock_md_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Tushar Sugandhi91ccbba2021-07-12 17:48:58 -07001518 dm_ima_measure_on_table_load(t, STATUSTYPE_IMA);
1519
Alasdair G Kergon36a04562011-10-31 20:19:04 +00001520 immutable_target_type = dm_get_immutable_target_type(md);
1521 if (immutable_target_type &&
Mike Snitzerf083b092016-02-06 18:38:46 -05001522 (immutable_target_type != dm_table_get_immutable_target_type(t)) &&
1523 !dm_table_get_wildcard_target(t)) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04001524 DMERR("can't replace immutable target type %s",
1525 immutable_target_type->name);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00001526 r = -EINVAL;
Mike Snitzerf11c1c52013-08-27 20:03:00 -04001527 goto err_unlock_md_type;
Alasdair G Kergon36a04562011-10-31 20:19:04 +00001528 }
1529
Christoph Hellwig3e6180f2015-04-30 10:10:36 -04001530 if (dm_get_md_type(md) == DM_TYPE_NONE) {
Christoph Hellwig3e6180f2015-04-30 10:10:36 -04001531 /* setup md->queue to reflect md's type (may block) */
Mike Snitzer591ddcfc2016-01-31 12:05:42 -05001532 r = dm_setup_md_queue(md, t);
Christoph Hellwig3e6180f2015-04-30 10:10:36 -04001533 if (r) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04001534 DMERR("unable to set up device queue for new table.");
Christoph Hellwig3e6180f2015-04-30 10:10:36 -04001535 goto err_unlock_md_type;
1536 }
Toshi Kanib5ab4a92016-06-28 13:37:15 -06001537 } else if (!is_valid_type(dm_get_md_type(md), dm_table_get_type(t))) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04001538 DMERR("can't change device type (old=%u vs new=%u) after initial table load.",
1539 dm_get_md_type(md), dm_table_get_type(t));
Mike Snitzera5664da2010-08-12 04:14:01 +01001540 r = -EINVAL;
Mike Snitzerf11c1c52013-08-27 20:03:00 -04001541 goto err_unlock_md_type;
Mike Snitzera5664da2010-08-12 04:14:01 +01001542 }
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001543
Mike Snitzera5664da2010-08-12 04:14:01 +01001544 dm_unlock_md_type(md);
1545
1546 /* stage inactive table */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 down_write(&_hash_lock);
Mike Anderson1134e5ae2006-03-27 01:17:54 -08001548 hc = dm_get_mdptr(md);
Hou Taoa2f998a2022-12-16 12:23:53 +08001549 if (!hc) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04001550 DMERR("device has been removed from the dev hash table.");
Mike Anderson1134e5ae2006-03-27 01:17:54 -08001551 up_write(&_hash_lock);
1552 r = -ENXIO;
Mike Snitzerf11c1c52013-08-27 20:03:00 -04001553 goto err_destroy_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
1555
1556 if (hc->new_map)
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001557 old_map = hc->new_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 hc->new_map = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 up_write(&_hash_lock);
Mike Anderson1134e5ae2006-03-27 01:17:54 -08001560
1561 param->flags |= DM_INACTIVE_PRESENT_FLAG;
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001562 __dev_status(md, param);
Mike Anderson1134e5ae2006-03-27 01:17:54 -08001563
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001564 if (old_map) {
1565 dm_sync_table(md);
1566 dm_table_destroy(old_map);
1567 }
1568
Mike Anderson1134e5ae2006-03-27 01:17:54 -08001569 dm_put(md);
1570
Mike Snitzerf11c1c52013-08-27 20:03:00 -04001571 return 0;
1572
1573err_unlock_md_type:
1574 dm_unlock_md_type(md);
1575err_destroy_table:
1576 dm_table_destroy(t);
1577err:
1578 dm_put(md);
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 return r;
1581}
1582
Mikulas Patockafc1841e2017-05-05 11:12:52 -07001583static int table_clear(struct file *filp, struct dm_ioctl *param, size_t param_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 struct hash_cell *hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001586 struct mapped_device *md;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001587 struct dm_table *old_map = NULL;
Tushar Sugandhi99169b92021-07-12 17:49:01 -07001588 bool has_new_map = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 down_write(&_hash_lock);
1591
1592 hc = __find_device_hash_cell(param);
1593 if (!hc) {
Milan Broz810b4922011-01-13 19:59:55 +00001594 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 up_write(&_hash_lock);
1596 return -ENXIO;
1597 }
1598
1599 if (hc->new_map) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001600 old_map = hc->new_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 hc->new_map = NULL;
Tushar Sugandhi99169b92021-07-12 17:49:01 -07001602 has_new_map = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
1604
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001605 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 up_write(&_hash_lock);
Mike Snitzer3d32aaa2023-04-17 11:59:56 -04001607
1608 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1609 __dev_status(md, param);
1610
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001611 if (old_map) {
1612 dm_sync_table(md);
1613 dm_table_destroy(old_map);
1614 }
Tushar Sugandhi99169b92021-07-12 17:49:01 -07001615 dm_ima_measure_on_table_clear(md, has_new_map);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001616 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001617
1618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
1621/*
1622 * Retrieves a list of devices used by a particular dm device.
1623 */
1624static void retrieve_deps(struct dm_table *table,
1625 struct dm_ioctl *param, size_t param_size)
1626{
1627 unsigned int count = 0;
1628 struct list_head *tmp;
1629 size_t len, needed;
Mikulas Patocka82b15192008-10-10 13:37:09 +01001630 struct dm_dev_internal *dd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 struct dm_target_deps *deps;
1632
Mikulas Patockaf6007dc2023-08-09 12:44:20 +02001633 down_read(&table->devices_lock);
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 deps = get_result_buffer(param, param_size, &len);
1636
1637 /*
1638 * Count the devices.
1639 */
Heinz Mauelshagen43be9c72023-01-30 21:43:57 +01001640 list_for_each(tmp, dm_table_get_devices(table))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 count++;
1642
1643 /*
1644 * Check we have enough space.
1645 */
Gustavo A. R. Silvada899622020-06-17 12:31:45 -04001646 needed = struct_size(deps, dev, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (len < needed) {
1648 param->flags |= DM_BUFFER_FULL_FLAG;
Mikulas Patockaf6007dc2023-08-09 12:44:20 +02001649 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
1651
1652 /*
1653 * Fill in the devices.
1654 */
1655 deps->count = count;
1656 count = 0;
Heinz Mauelshagen43be9c72023-01-30 21:43:57 +01001657 list_for_each_entry(dd, dm_table_get_devices(table), list)
Benjamin Marzinski86f11522014-08-13 13:53:43 -05001658 deps->dev[count++] = huge_encode_dev(dd->dm_dev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 param->data_size = param->data_start + needed;
Mikulas Patockaf6007dc2023-08-09 12:44:20 +02001661
1662out:
1663 up_read(&table->devices_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
Mikulas Patockafc1841e2017-05-05 11:12:52 -07001666static int table_deps(struct file *filp, struct dm_ioctl *param, size_t param_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 struct mapped_device *md;
1669 struct dm_table *table;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001670 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 md = find_device(param);
1673 if (!md)
1674 return -ENXIO;
1675
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001676 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001678 table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
1679 if (table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 retrieve_deps(table, param, param_size);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001681 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001684
1685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
1688/*
1689 * Return the status of a device as a text string for each
1690 * target.
1691 */
Mikulas Patockafc1841e2017-05-05 11:12:52 -07001692static int table_status(struct file *filp, struct dm_ioctl *param, size_t param_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 struct mapped_device *md;
1695 struct dm_table *table;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001696 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 md = find_device(param);
1699 if (!md)
1700 return -ENXIO;
1701
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001702 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001704 table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
1705 if (table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 retrieve_status(table, param, param_size);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001707 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001710
1711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
Mikulas Patockaa2606242013-03-01 22:45:49 +00001714/*
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001715 * Process device-mapper dependent messages. Messages prefixed with '@'
1716 * are processed by the DM core. All others are delivered to the target.
Mikulas Patockaa2606242013-03-01 22:45:49 +00001717 * Returns a number <= 1 if message was processed by device mapper.
1718 * Returns 2 if message should be delivered to the target.
1719 */
Heinz Mauelshagen86a32382023-01-25 21:14:58 +01001720static int message_for_md(struct mapped_device *md, unsigned int argc, char **argv,
1721 char *result, unsigned int maxlen)
Mikulas Patockaa2606242013-03-01 22:45:49 +00001722{
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001723 int r;
1724
1725 if (**argv != '@')
1726 return 2; /* no '@' prefix, deliver to target */
1727
Mikulas Patocka2c140a22013-11-01 18:27:41 -04001728 if (!strcasecmp(argv[0], "@cancel_deferred_remove")) {
1729 if (argc != 1) {
1730 DMERR("Invalid arguments for @cancel_deferred_remove");
1731 return -EINVAL;
1732 }
1733 return dm_cancel_deferred_remove(md);
1734 }
1735
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001736 r = dm_stats_message(md, argc, argv, result, maxlen);
1737 if (r < 2)
1738 return r;
1739
1740 DMERR("Unsupported message sent to DM core: %s", argv[0]);
1741 return -EINVAL;
Mikulas Patockaa2606242013-03-01 22:45:49 +00001742}
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744/*
1745 * Pass a message to the target that's at the supplied device offset.
1746 */
Mikulas Patockafc1841e2017-05-05 11:12:52 -07001747static int target_message(struct file *filp, struct dm_ioctl *param, size_t param_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
1749 int r, argc;
1750 char **argv;
1751 struct mapped_device *md;
1752 struct dm_table *table;
1753 struct dm_target *ti;
1754 struct dm_target_msg *tmsg = (void *) param + param->data_start;
Mikulas Patockaa2606242013-03-01 22:45:49 +00001755 size_t maxlen;
1756 char *result = get_result_buffer(param, param_size, &maxlen);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001757 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 md = find_device(param);
1760 if (!md)
1761 return -ENXIO;
1762
Milan Broz027d50f2007-10-19 22:38:36 +01001763 if (tmsg < (struct dm_target_msg *) param->data ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 invalid_str(tmsg->message, (void *) param + param_size)) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04001765 DMERR("Invalid target message parameters.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 r = -EINVAL;
1767 goto out;
1768 }
1769
1770 r = dm_split_args(&argc, &argv, tmsg->message);
1771 if (r) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04001772 DMERR("Failed to split target message parameters");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 goto out;
1774 }
1775
Alasdair G Kergon2ca4c922011-08-02 12:32:03 +01001776 if (!argc) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04001777 DMERR("Empty message received.");
Qinglang Miao4d7659b2020-11-28 18:19:59 +08001778 r = -EINVAL;
Jesper Juhl902c6a92012-03-07 19:09:34 +00001779 goto out_argv;
Alasdair G Kergon2ca4c922011-08-02 12:32:03 +01001780 }
1781
Mikulas Patockaa2606242013-03-01 22:45:49 +00001782 r = message_for_md(md, argc, argv, result, maxlen);
1783 if (r <= 1)
1784 goto out_argv;
1785
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001786 table = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (!table)
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001788 goto out_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Mike Andersonc50abeb2009-12-10 23:52:20 +00001790 if (dm_deleting_md(md)) {
1791 r = -ENXIO;
1792 goto out_table;
1793 }
1794
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001795 ti = dm_table_find_target(table, tmsg->sector);
Mikulas Patocka123d87d2019-08-23 09:55:26 -04001796 if (!ti) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04001797 DMERR("Target message sector outside device.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 r = -EINVAL;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001799 } else if (ti->type->message)
Mike Snitzer1eb5fa82018-02-28 15:59:59 -05001800 r = ti->type->message(ti, argc, argv, result, maxlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 else {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04001802 DMERR("Target type does not support messages");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 r = -EINVAL;
1804 }
1805
Mike Andersonc50abeb2009-12-10 23:52:20 +00001806 out_table:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001807 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 out_argv:
1809 kfree(argv);
1810 out:
Mikulas Patockaa2606242013-03-01 22:45:49 +00001811 if (r >= 0)
1812 __dev_status(md, param);
1813
1814 if (r == 1) {
1815 param->flags |= DM_DATA_OUT_FLAG;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001816 if (dm_message_test_buffer_overflow(result, maxlen))
Mikulas Patockaa2606242013-03-01 22:45:49 +00001817 param->flags |= DM_BUFFER_FULL_FLAG;
1818 else
1819 param->data_size = param->data_start + strlen(result) + 1;
1820 r = 0;
1821 }
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 dm_put(md);
1824 return r;
1825}
1826
Mikulas Patockae2914cc22013-03-01 22:45:48 +00001827/*
1828 * The ioctl parameter block consists of two parts, a dm_ioctl struct
1829 * followed by a data buffer. This flag is set if the second part,
1830 * which has a variable size, is not used by the function processing
1831 * the ioctl.
1832 */
Mikulas Patocka62e08242017-09-20 07:29:49 -04001833#define IOCTL_FLAGS_NO_PARAMS 1
1834#define IOCTL_FLAGS_ISSUE_GLOBAL_EVENT 2
Mikulas Patockae2914cc22013-03-01 22:45:48 +00001835
Heinz Mauelshagena4a82ce2023-01-26 15:48:30 +01001836/*
1837 *---------------------------------------------------------------
1838 * Implementation of open/close/ioctl on the special char device.
1839 *---------------------------------------------------------------
1840 */
Mikulas Patockae2914cc22013-03-01 22:45:48 +00001841static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Eric Biggerscf0dec62017-06-22 11:33:47 -07001843 static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 int cmd;
Mikulas Patockae2914cc22013-03-01 22:45:48 +00001845 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 ioctl_fn fn;
1847 } _ioctls[] = {
Mikulas Patockae2914cc22013-03-01 22:45:48 +00001848 {DM_VERSION_CMD, 0, NULL}, /* version is dealt with elsewhere */
Mikulas Patocka62e08242017-09-20 07:29:49 -04001849 {DM_REMOVE_ALL_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, remove_all},
Mikulas Patockae2914cc22013-03-01 22:45:48 +00001850 {DM_LIST_DEVICES_CMD, 0, list_devices},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Mikulas Patocka62e08242017-09-20 07:29:49 -04001852 {DM_DEV_CREATE_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_create},
1853 {DM_DEV_REMOVE_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_remove},
1854 {DM_DEV_RENAME_CMD, IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_rename},
Mikulas Patockae2914cc22013-03-01 22:45:48 +00001855 {DM_DEV_SUSPEND_CMD, IOCTL_FLAGS_NO_PARAMS, dev_suspend},
1856 {DM_DEV_STATUS_CMD, IOCTL_FLAGS_NO_PARAMS, dev_status},
1857 {DM_DEV_WAIT_CMD, 0, dev_wait},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Mikulas Patockae2914cc22013-03-01 22:45:48 +00001859 {DM_TABLE_LOAD_CMD, 0, table_load},
1860 {DM_TABLE_CLEAR_CMD, IOCTL_FLAGS_NO_PARAMS, table_clear},
1861 {DM_TABLE_DEPS_CMD, 0, table_deps},
1862 {DM_TABLE_STATUS_CMD, 0, table_status},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Mikulas Patockae2914cc22013-03-01 22:45:48 +00001864 {DM_LIST_VERSIONS_CMD, 0, list_versions},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Mikulas Patockae2914cc22013-03-01 22:45:48 +00001866 {DM_TARGET_MSG_CMD, 0, target_message},
Mikulas Patockafc1841e2017-05-05 11:12:52 -07001867 {DM_DEV_SET_GEOMETRY_CMD, 0, dev_set_geometry},
Mikulas Patockab52c3de2022-11-01 16:54:27 -04001868 {DM_DEV_ARM_POLL_CMD, IOCTL_FLAGS_NO_PARAMS, dev_arm_poll},
1869 {DM_GET_TARGET_VERSION_CMD, 0, get_target_version},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 };
1871
Mikulas Patockae2914cc22013-03-01 22:45:48 +00001872 if (unlikely(cmd >= ARRAY_SIZE(_ioctls)))
1873 return NULL;
1874
Jordy Zomercd9c88d2022-01-29 15:58:39 +01001875 cmd = array_index_nospec(cmd, ARRAY_SIZE(_ioctls));
Mikulas Patockae2914cc22013-03-01 22:45:48 +00001876 *ioctl_flags = _ioctls[cmd].flags;
1877 return _ioctls[cmd].fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
1879
1880/*
1881 * As well as checking the version compatibility this always
1882 * copies the kernel interface version out.
1883 */
Demi Marie Obenour249bed82023-06-03 10:52:42 -04001884static int check_version(unsigned int cmd, struct dm_ioctl __user *user,
1885 struct dm_ioctl *kernel_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 int r = 0;
1888
Demi Marie Obenour249bed82023-06-03 10:52:42 -04001889 /* Make certain version is first member of dm_ioctl struct */
1890 BUILD_BUG_ON(offsetof(struct dm_ioctl, version) != 0);
1891
1892 if (copy_from_user(kernel_params->version, user->version, sizeof(kernel_params->version)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return -EFAULT;
1894
Demi Marie Obenour249bed82023-06-03 10:52:42 -04001895 if ((kernel_params->version[0] != DM_VERSION_MAJOR) ||
1896 (kernel_params->version[1] > DM_VERSION_MINOR)) {
Heinz Mauelshagen2e84fec2023-02-03 18:55:47 +01001897 DMERR("ioctl interface mismatch: kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
Mikulas Patocka43e6c112022-08-24 07:25:57 -04001898 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1899 DM_VERSION_PATCHLEVEL,
Demi Marie Obenour249bed82023-06-03 10:52:42 -04001900 kernel_params->version[0],
1901 kernel_params->version[1],
1902 kernel_params->version[2],
1903 cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 r = -EINVAL;
1905 }
1906
1907 /*
1908 * Fill in the kernel version.
1909 */
Demi Marie Obenour249bed82023-06-03 10:52:42 -04001910 kernel_params->version[0] = DM_VERSION_MAJOR;
1911 kernel_params->version[1] = DM_VERSION_MINOR;
1912 kernel_params->version[2] = DM_VERSION_PATCHLEVEL;
1913 if (copy_to_user(user->version, kernel_params->version, sizeof(kernel_params->version)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return -EFAULT;
1915
1916 return r;
1917}
1918
Bart Van Assche028b39e2016-06-28 16:36:46 +02001919#define DM_PARAMS_MALLOC 0x0001 /* Params allocated with kvmalloc() */
Mikulas Patocka9c5091f2012-12-21 20:23:36 +00001920#define DM_WIPE_BUFFER 0x0010 /* Wipe input buffer before returning from ioctl */
1921
1922static void free_params(struct dm_ioctl *param, size_t param_size, int param_flags)
1923{
1924 if (param_flags & DM_WIPE_BUFFER)
1925 memset(param, 0, param_size);
1926
Bart Van Assche028b39e2016-06-28 16:36:46 +02001927 if (param_flags & DM_PARAMS_MALLOC)
1928 kvfree(param);
Mikulas Patocka9c5091f2012-12-21 20:23:36 +00001929}
1930
Mikulas Patocka02cde502013-03-01 22:45:49 +00001931static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernel,
Wenwen Wang800a7342018-10-03 11:43:59 -05001932 int ioctl_flags, struct dm_ioctl **param, int *param_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Mikulas Patocka02cde502013-03-01 22:45:49 +00001934 struct dm_ioctl *dmi;
Milan Brozf8681202011-03-24 13:54:30 +00001935 int secure_data;
Bart Van Assche60807582016-11-18 14:28:00 -08001936 const size_t minimum_data_size = offsetof(struct dm_ioctl, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Demi Marie Obenour249bed82023-06-03 10:52:42 -04001938 /* check_version() already copied version from userspace, avoid TOCTOU */
1939 if (copy_from_user((char *)param_kernel + sizeof(param_kernel->version),
1940 (char __user *)user + sizeof(param_kernel->version),
1941 minimum_data_size - sizeof(param_kernel->version)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return -EFAULT;
1943
Mikulas Patockabd504bc2024-01-09 15:57:56 +01001944 if (unlikely(param_kernel->data_size < minimum_data_size) ||
1945 unlikely(param_kernel->data_size > DM_MAX_TARGETS * DM_MAX_TARGET_PARAMS)) {
Mikulas Patockadbdcc902022-03-20 17:12:46 -04001946 DMERR("Invalid data size in the ioctl structure: %u",
1947 param_kernel->data_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return -EINVAL;
Mikulas Patockadbdcc902022-03-20 17:12:46 -04001949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Mikulas Patocka02cde502013-03-01 22:45:49 +00001951 secure_data = param_kernel->flags & DM_SECURE_DATA_FLAG;
Milan Brozf8681202011-03-24 13:54:30 +00001952
Mikulas Patocka9c5091f2012-12-21 20:23:36 +00001953 *param_flags = secure_data ? DM_WIPE_BUFFER : 0;
1954
Mikulas Patocka02cde502013-03-01 22:45:49 +00001955 if (ioctl_flags & IOCTL_FLAGS_NO_PARAMS) {
1956 dmi = param_kernel;
1957 dmi->data_size = minimum_data_size;
1958 goto data_copied;
1959 }
1960
Mikulas Patocka5023e5c2012-12-21 20:23:36 +00001961 /*
Junaid Shahid8c1e21622017-05-18 12:00:51 -07001962 * Use __GFP_HIGH to avoid low memory issues when a device is
1963 * suspended and the ioctl is needed to resume it.
Mikulas Patocka9c5091f2012-12-21 20:23:36 +00001964 * Use kmalloc() rather than vmalloc() when we can.
Mikulas Patocka5023e5c2012-12-21 20:23:36 +00001965 */
Mikulas Patocka9c5091f2012-12-21 20:23:36 +00001966 dmi = NULL;
Mikulas Patockae2c789c2023-06-26 16:48:40 +02001967 dmi = kvmalloc(param_kernel->data_size, GFP_NOIO | __GFP_HIGH);
Mikulas Patocka02cde502013-03-01 22:45:49 +00001968
1969 if (!dmi) {
1970 if (secure_data && clear_user(user, param_kernel->data_size))
Milan Brozf8681202011-03-24 13:54:30 +00001971 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return -ENOMEM;
Milan Brozf8681202011-03-24 13:54:30 +00001973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Bart Van Assche028b39e2016-06-28 16:36:46 +02001975 *param_flags |= DM_PARAMS_MALLOC;
1976
Wenwen Wang800a7342018-10-03 11:43:59 -05001977 /* Copy from param_kernel (which was already copied from user) */
1978 memcpy(dmi, param_kernel, minimum_data_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Wenwen Wang800a7342018-10-03 11:43:59 -05001980 if (copy_from_user(&dmi->data, (char __user *)user + minimum_data_size,
1981 param_kernel->data_size - minimum_data_size))
1982 goto bad;
Mikulas Patocka02cde502013-03-01 22:45:49 +00001983data_copied:
Milan Brozf8681202011-03-24 13:54:30 +00001984 /* Wipe the user buffer so we do not return it to userspace */
Mikulas Patocka02cde502013-03-01 22:45:49 +00001985 if (secure_data && clear_user(user, param_kernel->data_size))
Milan Brozf8681202011-03-24 13:54:30 +00001986 goto bad;
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 *param = dmi;
1989 return 0;
Milan Broz6bb43b52011-03-24 13:54:28 +00001990
1991bad:
Mikulas Patocka02cde502013-03-01 22:45:49 +00001992 free_params(dmi, param_kernel->data_size, *param_flags);
Mikulas Patocka9c5091f2012-12-21 20:23:36 +00001993
Milan Broz6bb43b52011-03-24 13:54:28 +00001994 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995}
1996
1997static int validate_params(uint cmd, struct dm_ioctl *param)
1998{
1999 /* Always clear this flag */
2000 param->flags &= ~DM_BUFFER_FULL_FLAG;
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00002001 param->flags &= ~DM_UEVENT_GENERATED_FLAG;
Milan Brozf8681202011-03-24 13:54:30 +00002002 param->flags &= ~DM_SECURE_DATA_FLAG;
Mikulas Patockaa2606242013-03-01 22:45:49 +00002003 param->flags &= ~DM_DATA_OUT_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 /* Ignores parameters */
2006 if (cmd == DM_REMOVE_ALL_CMD ||
2007 cmd == DM_LIST_DEVICES_CMD ||
2008 cmd == DM_LIST_VERSIONS_CMD)
2009 return 0;
2010
Matthias Kaehlckee36215d2017-04-17 11:05:03 -07002011 if (cmd == DM_DEV_CREATE_CMD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (!*param->name) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04002013 DMERR("name not supplied when creating device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return -EINVAL;
2015 }
Matthias Kaehlckee36215d2017-04-17 11:05:03 -07002016 } else if (*param->uuid && *param->name) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04002017 DMERR("only supply one of name or uuid, cmd(%u)", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return -EINVAL;
2019 }
2020
2021 /* Ensure strings are terminated */
2022 param->name[DM_NAME_LEN - 1] = '\0';
2023 param->uuid[DM_UUID_LEN - 1] = '\0';
2024
2025 return 0;
2026}
2027
Mikulas Patockafc1841e2017-05-05 11:12:52 -07002028static int ctl_ioctl(struct file *file, uint command, struct dm_ioctl __user *user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 int r = 0;
Mikulas Patockae2914cc22013-03-01 22:45:48 +00002031 int ioctl_flags;
Mikulas Patocka9c5091f2012-12-21 20:23:36 +00002032 int param_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 unsigned int cmd;
Kees Cook3f649ab2020-06-03 13:09:38 -07002034 struct dm_ioctl *param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 ioctl_fn fn = NULL;
Milan Broz6bb43b52011-03-24 13:54:28 +00002036 size_t input_param_size;
Mikulas Patocka02cde502013-03-01 22:45:49 +00002037 struct dm_ioctl param_kernel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 /* only root can play with this */
2040 if (!capable(CAP_SYS_ADMIN))
2041 return -EACCES;
2042
2043 if (_IOC_TYPE(command) != DM_IOCTL)
2044 return -ENOTTY;
2045
2046 cmd = _IOC_NR(command);
2047
2048 /*
2049 * Check the interface version passed in. This also
2050 * writes out the kernel's interface version.
2051 */
Demi Marie Obenour249bed82023-06-03 10:52:42 -04002052 r = check_version(cmd, user, &param_kernel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (r)
2054 return r;
2055
2056 /*
2057 * Nothing more to do for the version command.
2058 */
2059 if (cmd == DM_VERSION_CMD)
2060 return 0;
2061
Mikulas Patockae2914cc22013-03-01 22:45:48 +00002062 fn = lookup_ioctl(cmd, &ioctl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (!fn) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04002064 DMERR("dm_ctl_ioctl: unknown command 0x%x", command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return -ENOTTY;
2066 }
2067
2068 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 * Copy the parameters into kernel space.
2070 */
Mikulas Patocka02cde502013-03-01 22:45:49 +00002071 r = copy_params(user, &param_kernel, ioctl_flags, &param, &param_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Alasdair G Kergondab6a422006-02-01 03:04:52 -08002073 if (r)
2074 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Milan Brozf8681202011-03-24 13:54:30 +00002076 input_param_size = param->data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 r = validate_params(cmd, param);
2078 if (r)
2079 goto out;
2080
Adrian Salido4617f562017-04-27 10:32:55 -07002081 param->data_size = offsetof(struct dm_ioctl, data);
Mikulas Patockafc1841e2017-05-05 11:12:52 -07002082 r = fn(file, param, input_param_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Mikulas Patockae2914cc22013-03-01 22:45:48 +00002084 if (unlikely(param->flags & DM_BUFFER_FULL_FLAG) &&
2085 unlikely(ioctl_flags & IOCTL_FLAGS_NO_PARAMS))
2086 DMERR("ioctl %d tried to output some data but has IOCTL_FLAGS_NO_PARAMS set", cmd);
2087
Mikulas Patocka62e08242017-09-20 07:29:49 -04002088 if (!r && ioctl_flags & IOCTL_FLAGS_ISSUE_GLOBAL_EVENT)
2089 dm_issue_global_event();
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 /*
2092 * Copy the results back to userland.
2093 */
2094 if (!r && copy_to_user(user, param, param->data_size))
2095 r = -EFAULT;
2096
Milan Broz6bb43b52011-03-24 13:54:28 +00002097out:
Mikulas Patocka9c5091f2012-12-21 20:23:36 +00002098 free_params(param, input_param_size, param_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return r;
2100}
2101
Alasdair G Kergon27238b22008-02-08 02:09:53 +00002102static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
2103{
Mikulas Patockafc1841e2017-05-05 11:12:52 -07002104 return (long)ctl_ioctl(file, command, (struct dm_ioctl __user *)u);
Alasdair G Kergon27238b22008-02-08 02:09:53 +00002105}
2106
Milan Broz76c072b2008-02-08 02:09:56 +00002107#ifdef CONFIG_COMPAT
2108static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
2109{
2110 return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
2111}
2112#else
2113#define dm_compat_ctl_ioctl NULL
2114#endif
2115
Mikulas Patocka93e64422017-01-16 16:05:59 -05002116static int dm_open(struct inode *inode, struct file *filp)
2117{
2118 int r;
2119 struct dm_file *priv;
2120
2121 r = nonseekable_open(inode, filp);
2122 if (unlikely(r))
2123 return r;
2124
2125 priv = filp->private_data = kmalloc(sizeof(struct dm_file), GFP_KERNEL);
2126 if (!priv)
2127 return -ENOMEM;
2128
2129 priv->global_event_nr = atomic_read(&dm_global_event_nr);
2130
2131 return 0;
2132}
2133
2134static int dm_release(struct inode *inode, struct file *filp)
2135{
2136 kfree(filp->private_data);
2137 return 0;
2138}
2139
Al Viroafc9a422017-07-03 06:39:46 -04002140static __poll_t dm_poll(struct file *filp, poll_table *wait)
Mikulas Patocka93e64422017-01-16 16:05:59 -05002141{
2142 struct dm_file *priv = filp->private_data;
Al Viroafc9a422017-07-03 06:39:46 -04002143 __poll_t mask = 0;
Mikulas Patocka93e64422017-01-16 16:05:59 -05002144
2145 poll_wait(filp, &dm_global_eventq, wait);
2146
2147 if ((int)(atomic_read(&dm_global_event_nr) - priv->global_event_nr) > 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002148 mask |= EPOLLIN;
Mikulas Patocka93e64422017-01-16 16:05:59 -05002149
2150 return mask;
2151}
2152
Arjan van de Venfa027c22007-02-12 00:55:33 -08002153static const struct file_operations _ctl_fops = {
Mikulas Patocka93e64422017-01-16 16:05:59 -05002154 .open = dm_open,
2155 .release = dm_release,
2156 .poll = dm_poll,
Alasdair G Kergon27238b22008-02-08 02:09:53 +00002157 .unlocked_ioctl = dm_ctl_ioctl,
Milan Broz76c072b2008-02-08 02:09:56 +00002158 .compat_ioctl = dm_compat_ctl_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002160 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161};
2162
2163static struct miscdevice _dm_misc = {
Peter Rajnoha7e507eb2010-08-12 04:14:05 +01002164 .minor = MAPPER_CTRL_MINOR,
Heinz Mauelshagen8ca817c2023-02-01 22:31:43 +01002165 .name = DM_NAME,
Peter Rajnoha7e507eb2010-08-12 04:14:05 +01002166 .nodename = DM_DIR "/" DM_CONTROL_NODE,
Heinz Mauelshagen8ca817c2023-02-01 22:31:43 +01002167 .fops = &_ctl_fops
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168};
2169
Peter Rajnoha7e507eb2010-08-12 04:14:05 +01002170MODULE_ALIAS_MISCDEV(MAPPER_CTRL_MINOR);
2171MODULE_ALIAS("devname:" DM_DIR "/" DM_CONTROL_NODE);
2172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173/*
2174 * Create misc character device and link to DM_DIR/control.
2175 */
2176int __init dm_interface_init(void)
2177{
2178 int r;
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 r = misc_register(&_dm_misc);
2181 if (r) {
2182 DMERR("misc_register failed for control device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return r;
2184 }
2185
2186 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
2187 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
2188 DM_DRIVER_EMAIL);
2189 return 0;
2190}
2191
2192void dm_interface_exit(void)
2193{
Greg Kroah-Hartmanf368ed62015-07-30 15:59:57 -07002194 misc_deregister(&_dm_misc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 dm_hash_exit();
2196}
Mike Anderson96a1f7d2007-10-19 22:47:59 +01002197
2198/**
2199 * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
2200 * @md: Pointer to mapped_device
2201 * @name: Buffer (size DM_NAME_LEN) for name
2202 * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
2203 */
2204int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
2205{
2206 int r = 0;
2207 struct hash_cell *hc;
2208
2209 if (!md)
2210 return -ENXIO;
2211
Mikulas Patocka60769052009-12-10 23:51:52 +00002212 mutex_lock(&dm_hash_cells_mutex);
Mike Anderson96a1f7d2007-10-19 22:47:59 +01002213 hc = dm_get_mdptr(md);
Hou Taoa2f998a2022-12-16 12:23:53 +08002214 if (!hc) {
Mike Anderson96a1f7d2007-10-19 22:47:59 +01002215 r = -ENXIO;
2216 goto out;
2217 }
2218
Milan Broz23d39f62009-01-06 03:05:04 +00002219 if (name)
2220 strcpy(name, hc->name);
2221 if (uuid)
2222 strcpy(uuid, hc->uuid ? : "");
Mike Anderson96a1f7d2007-10-19 22:47:59 +01002223
2224out:
Mikulas Patocka60769052009-12-10 23:51:52 +00002225 mutex_unlock(&dm_hash_cells_mutex);
Mike Anderson96a1f7d2007-10-19 22:47:59 +01002226
2227 return r;
2228}
Mike Snitzer61931c02020-10-01 15:00:56 -04002229EXPORT_SYMBOL_GPL(dm_copy_name_and_uuid);
Helen Koike6bbc9232019-02-21 17:33:34 -03002230
2231/**
2232 * dm_early_create - create a mapped device in early boot.
2233 *
2234 * @dmi: Contains main information of the device mapping to be created.
2235 * @spec_array: array of pointers to struct dm_target_spec. Describes the
2236 * mapping table of the device.
2237 * @target_params_array: array of strings with the parameters to a specific
2238 * target.
2239 *
2240 * Instead of having the struct dm_target_spec and the parameters for every
2241 * target embedded at the end of struct dm_ioctl (as performed in a normal
2242 * ioctl), pass them as arguments, so the caller doesn't need to serialize them.
2243 * The size of the spec_array and target_params_array is given by
2244 * @dmi->target_count.
2245 * This function is supposed to be called in early boot, so locking mechanisms
2246 * to protect against concurrent loads are not required.
2247 */
2248int __init dm_early_create(struct dm_ioctl *dmi,
2249 struct dm_target_spec **spec_array,
2250 char **target_params_array)
2251{
2252 int r, m = DM_ANY_MINOR;
2253 struct dm_table *t, *old_map;
2254 struct mapped_device *md;
2255 unsigned int i;
2256
2257 if (!dmi->target_count)
2258 return -EINVAL;
2259
2260 r = check_name(dmi->name);
2261 if (r)
2262 return r;
2263
2264 if (dmi->flags & DM_PERSISTENT_DEV_FLAG)
2265 m = MINOR(huge_decode_dev(dmi->dev));
2266
2267 /* alloc dm device */
2268 r = dm_create(m, &md);
2269 if (r)
2270 return r;
2271
2272 /* hash insert */
2273 r = dm_hash_insert(dmi->name, *dmi->uuid ? dmi->uuid : NULL, md);
2274 if (r)
2275 goto err_destroy_dm;
2276
2277 /* alloc table */
2278 r = dm_table_create(&t, get_mode(dmi), dmi->target_count, md);
2279 if (r)
Helen Koike0f41fcf2019-05-15 13:50:54 -03002280 goto err_hash_remove;
Helen Koike6bbc9232019-02-21 17:33:34 -03002281
2282 /* add targets */
2283 for (i = 0; i < dmi->target_count; i++) {
2284 r = dm_table_add_target(t, spec_array[i]->target_type,
2285 (sector_t) spec_array[i]->sector_start,
2286 (sector_t) spec_array[i]->length,
2287 target_params_array[i]);
2288 if (r) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04002289 DMERR("error adding target to table");
Helen Koike6bbc9232019-02-21 17:33:34 -03002290 goto err_destroy_table;
2291 }
2292 }
2293
2294 /* finish table */
2295 r = dm_table_complete(t);
2296 if (r)
2297 goto err_destroy_table;
2298
Helen Koike6bbc9232019-02-21 17:33:34 -03002299 /* setup md->queue to reflect md's type (may block) */
2300 r = dm_setup_md_queue(md, t);
2301 if (r) {
Mikulas Patocka43e6c112022-08-24 07:25:57 -04002302 DMERR("unable to set up device queue for new table.");
Helen Koike6bbc9232019-02-21 17:33:34 -03002303 goto err_destroy_table;
2304 }
2305
2306 /* Set new map */
2307 dm_suspend(md, 0);
2308 old_map = dm_swap_table(md, t);
2309 if (IS_ERR(old_map)) {
2310 r = PTR_ERR(old_map);
2311 goto err_destroy_table;
2312 }
2313 set_disk_ro(dm_disk(md), !!(dmi->flags & DM_READONLY_FLAG));
2314
2315 /* resume device */
2316 r = dm_resume(md);
2317 if (r)
2318 goto err_destroy_table;
2319
2320 DMINFO("%s (%s) is ready", md->disk->disk_name, dmi->name);
2321 dm_put(md);
2322 return 0;
2323
2324err_destroy_table:
2325 dm_table_destroy(t);
Helen Koike0f41fcf2019-05-15 13:50:54 -03002326err_hash_remove:
Mike Snitzer69868be2023-02-17 13:08:17 -05002327 down_write(&_hash_lock);
Helen Koike0f41fcf2019-05-15 13:50:54 -03002328 (void) __hash_remove(__get_name_cell(dmi->name));
Mike Snitzer69868be2023-02-17 13:08:17 -05002329 up_write(&_hash_lock);
Helen Koike0f41fcf2019-05-15 13:50:54 -03002330 /* release reference from __get_name_cell */
2331 dm_put(md);
Helen Koike6bbc9232019-02-21 17:33:34 -03002332err_destroy_dm:
2333 dm_put(md);
2334 dm_destroy(md);
2335 return r;
2336}