xref: /dragonfly/sys/vfs/hammer2/hammer2_ioctl.c (revision f0e61bb7)
1 /*
2  * Copyright (c) 2011-2020 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 /*
36  * Ioctl Functions.
37  *
38  * WARNING! The ioctl functions which manipulate the connection state need
39  *	    to be able to run without deadlock on the volume's chain lock.
40  *	    Most of these functions use a separate lock.
41  */
42 
43 #include "hammer2.h"
44 
45 #include <sys/kern_syscall.h>
46 
47 static int hammer2_ioctl_version_get(hammer2_inode_t *ip, void *data);
48 static int hammer2_ioctl_recluster(hammer2_inode_t *ip, void *data);
49 static int hammer2_ioctl_remote_scan(hammer2_inode_t *ip, void *data);
50 static int hammer2_ioctl_remote_add(hammer2_inode_t *ip, void *data);
51 static int hammer2_ioctl_remote_del(hammer2_inode_t *ip, void *data);
52 static int hammer2_ioctl_remote_rep(hammer2_inode_t *ip, void *data);
53 static int hammer2_ioctl_socket_get(hammer2_inode_t *ip, void *data);
54 static int hammer2_ioctl_socket_set(hammer2_inode_t *ip, void *data);
55 static int hammer2_ioctl_pfs_get(hammer2_inode_t *ip, void *data);
56 static int hammer2_ioctl_pfs_lookup(hammer2_inode_t *ip, void *data);
57 static int hammer2_ioctl_pfs_create(hammer2_inode_t *ip, void *data);
58 static int hammer2_ioctl_pfs_snapshot(hammer2_inode_t *ip, void *data);
59 static int hammer2_ioctl_pfs_delete(hammer2_inode_t *ip, void *data);
60 static int hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data);
61 static int hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data);
62 static int hammer2_ioctl_debug_dump(hammer2_inode_t *ip, u_int flags);
63 static int hammer2_ioctl_emerg_mode(hammer2_inode_t *ip, u_int mode);
64 static int hammer2_ioctl_growfs(hammer2_inode_t *ip, void *data,
65 			struct ucred *cred);
66 //static int hammer2_ioctl_inode_comp_set(hammer2_inode_t *ip, void *data);
67 //static int hammer2_ioctl_inode_comp_rec_set(hammer2_inode_t *ip, void *data);
68 //static int hammer2_ioctl_inode_comp_rec_set2(hammer2_inode_t *ip, void *data);
69 static int hammer2_ioctl_bulkfree_scan(hammer2_inode_t *ip, void *data);
70 static int hammer2_ioctl_destroy(hammer2_inode_t *ip, void *data);
71 static int hammer2_ioctl_volume_list(hammer2_inode_t *ip, void *data);
72 
73 int
74 hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data, int fflag,
75 	      struct ucred *cred)
76 {
77 	int error;
78 
79 	/*
80 	 * Standard root cred checks, will be selectively ignored below
81 	 * for ioctls that do not require root creds.
82 	 */
83 	error = priv_check_cred(cred, PRIV_HAMMER_IOCTL, 0);
84 
85 	switch(com) {
86 	case HAMMER2IOC_VERSION_GET:
87 		error = hammer2_ioctl_version_get(ip, data);
88 		break;
89 	case HAMMER2IOC_RECLUSTER:
90 		if (error == 0)
91 			error = hammer2_ioctl_recluster(ip, data);
92 		break;
93 	case HAMMER2IOC_REMOTE_SCAN:
94 		if (error == 0)
95 			error = hammer2_ioctl_remote_scan(ip, data);
96 		break;
97 	case HAMMER2IOC_REMOTE_ADD:
98 		if (error == 0)
99 			error = hammer2_ioctl_remote_add(ip, data);
100 		break;
101 	case HAMMER2IOC_REMOTE_DEL:
102 		if (error == 0)
103 			error = hammer2_ioctl_remote_del(ip, data);
104 		break;
105 	case HAMMER2IOC_REMOTE_REP:
106 		if (error == 0)
107 			error = hammer2_ioctl_remote_rep(ip, data);
108 		break;
109 	case HAMMER2IOC_SOCKET_GET:
110 		if (error == 0)
111 			error = hammer2_ioctl_socket_get(ip, data);
112 		break;
113 	case HAMMER2IOC_SOCKET_SET:
114 		if (error == 0)
115 			error = hammer2_ioctl_socket_set(ip, data);
116 		break;
117 	case HAMMER2IOC_PFS_GET:
118 		if (error == 0)
119 			error = hammer2_ioctl_pfs_get(ip, data);
120 		break;
121 	case HAMMER2IOC_PFS_LOOKUP:
122 		if (error == 0)
123 			error = hammer2_ioctl_pfs_lookup(ip, data);
124 		break;
125 	case HAMMER2IOC_PFS_CREATE:
126 		if (error == 0)
127 			error = hammer2_ioctl_pfs_create(ip, data);
128 		break;
129 	case HAMMER2IOC_PFS_DELETE:
130 		if (error == 0)
131 			error = hammer2_ioctl_pfs_delete(ip, data);
132 		break;
133 	case HAMMER2IOC_PFS_SNAPSHOT:
134 		if (error == 0)
135 			error = hammer2_ioctl_pfs_snapshot(ip, data);
136 		break;
137 	case HAMMER2IOC_INODE_GET:
138 		error = hammer2_ioctl_inode_get(ip, data);
139 		break;
140 	case HAMMER2IOC_INODE_SET:
141 		if (error == 0)
142 			error = hammer2_ioctl_inode_set(ip, data);
143 		break;
144 	case HAMMER2IOC_BULKFREE_SCAN:
145 		error = hammer2_ioctl_bulkfree_scan(ip, data);
146 		break;
147 	case HAMMER2IOC_BULKFREE_ASYNC:
148 		error = hammer2_ioctl_bulkfree_scan(ip, NULL);
149 		break;
150 	case HAMMER2IOC_DESTROY:
151 		if (error == 0)
152 			error = hammer2_ioctl_destroy(ip, data);
153 		break;
154 	case HAMMER2IOC_DEBUG_DUMP:
155 		error = hammer2_ioctl_debug_dump(ip, *(u_int *)data);
156 		break;
157 	case HAMMER2IOC_EMERG_MODE:
158 		if (error == 0)
159 			error = hammer2_ioctl_emerg_mode(ip, *(u_int *)data);
160 		break;
161 	case HAMMER2IOC_GROWFS:
162 		if (error == 0)
163 			error = hammer2_ioctl_growfs(ip, data, cred);
164 		break;
165 	case HAMMER2IOC_VOLUME_LIST:
166 		if (error == 0)
167 			error = hammer2_ioctl_volume_list(ip, data);
168 		break;
169 	default:
170 		error = EOPNOTSUPP;
171 		break;
172 	}
173 	return (error);
174 }
175 
176 /*
177  * Retrieve version and basic info
178  */
179 static int
180 hammer2_ioctl_version_get(hammer2_inode_t *ip, void *data)
181 {
182 	hammer2_ioc_version_t *version = data;
183 	hammer2_dev_t *hmp;
184 
185 	hmp = ip->pmp->pfs_hmps[0];
186 	if (hmp)
187 		version->version = hmp->voldata.version;
188 	else
189 		version->version = -1;
190 	return 0;
191 }
192 
193 static int
194 hammer2_ioctl_recluster(hammer2_inode_t *ip, void *data)
195 {
196 	hammer2_ioc_recluster_t *recl = data;
197 	struct vnode *vproot;
198 	struct file *fp;
199 	hammer2_cluster_t *cluster;
200 	int error;
201 
202 	fp = holdfp(curthread, recl->fd, -1);
203 	if (fp) {
204 		error = VFS_ROOT(ip->pmp->mp, &vproot);
205 		if (error == 0) {
206 			cluster = &ip->pmp->iroot->cluster;
207 			kprintf("reconnect to cluster: nc=%d focus=%p\n",
208 				cluster->nchains, cluster->focus);
209 			if (cluster->nchains != 1 || cluster->focus == NULL) {
210 				kprintf("not a local device mount\n");
211 				error = EINVAL;
212 			} else {
213 				hammer2_cluster_reconnect(cluster->focus->hmp,
214 							  fp);
215 				kprintf("ok\n");
216 				error = 0;
217 			}
218 			vput(vproot);
219 		}
220 	} else {
221 		error = EINVAL;
222 	}
223 	return error;
224 }
225 
226 /*
227  * Retrieve information about a remote
228  */
229 static int
230 hammer2_ioctl_remote_scan(hammer2_inode_t *ip, void *data)
231 {
232 	hammer2_dev_t *hmp;
233 	hammer2_ioc_remote_t *remote = data;
234 	int copyid = remote->copyid;
235 
236 	hmp = ip->pmp->pfs_hmps[0];
237 	if (hmp == NULL)
238 		return (EINVAL);
239 
240 	if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
241 		return (EINVAL);
242 
243 	hammer2_voldata_lock(hmp);
244 	remote->copy1 = hmp->voldata.copyinfo[copyid];
245 	hammer2_voldata_unlock(hmp);
246 
247 	/*
248 	 * Adjust nextid (GET only)
249 	 */
250 	while (++copyid < HAMMER2_COPYID_COUNT &&
251 	       hmp->voldata.copyinfo[copyid].copyid == 0) {
252 		;
253 	}
254 	if (copyid == HAMMER2_COPYID_COUNT)
255 		remote->nextid = -1;
256 	else
257 		remote->nextid = copyid;
258 
259 	return(0);
260 }
261 
262 /*
263  * Add new remote entry
264  */
265 static int
266 hammer2_ioctl_remote_add(hammer2_inode_t *ip, void *data)
267 {
268 	hammer2_ioc_remote_t *remote = data;
269 	hammer2_pfs_t *pmp = ip->pmp;
270 	hammer2_dev_t *hmp;
271 	int copyid = remote->copyid;
272 	int error = 0;
273 
274 	hmp = pmp->pfs_hmps[0];
275 	if (hmp == NULL)
276 		return (EINVAL);
277 	if (copyid >= HAMMER2_COPYID_COUNT)
278 		return (EINVAL);
279 
280 	hammer2_voldata_lock(hmp);
281 	if (copyid < 0) {
282 		for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
283 			if (hmp->voldata.copyinfo[copyid].copyid == 0)
284 				break;
285 		}
286 		if (copyid == HAMMER2_COPYID_COUNT) {
287 			error = ENOSPC;
288 			goto failed;
289 		}
290 	}
291 	hammer2_voldata_modify(hmp);
292 	remote->copy1.copyid = copyid;
293 	hmp->voldata.copyinfo[copyid] = remote->copy1;
294 	hammer2_volconf_update(hmp, copyid);
295 failed:
296 	hammer2_voldata_unlock(hmp);
297 	return (error);
298 }
299 
300 /*
301  * Delete existing remote entry
302  */
303 static int
304 hammer2_ioctl_remote_del(hammer2_inode_t *ip, void *data)
305 {
306 	hammer2_ioc_remote_t *remote = data;
307 	hammer2_pfs_t *pmp = ip->pmp;
308 	hammer2_dev_t *hmp;
309 	int copyid = remote->copyid;
310 	int error = 0;
311 
312 	hmp = pmp->pfs_hmps[0];
313 	if (hmp == NULL)
314 		return (EINVAL);
315 	if (copyid >= HAMMER2_COPYID_COUNT)
316 		return (EINVAL);
317 	remote->copy1.path[sizeof(remote->copy1.path) - 1] = 0;
318 	hammer2_voldata_lock(hmp);
319 	if (copyid < 0) {
320 		for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
321 			if (hmp->voldata.copyinfo[copyid].copyid == 0)
322 				continue;
323 			if (strcmp(remote->copy1.path,
324 			    hmp->voldata.copyinfo[copyid].path) == 0) {
325 				break;
326 			}
327 		}
328 		if (copyid == HAMMER2_COPYID_COUNT) {
329 			error = ENOENT;
330 			goto failed;
331 		}
332 	}
333 	hammer2_voldata_modify(hmp);
334 	hmp->voldata.copyinfo[copyid].copyid = 0;
335 	hammer2_volconf_update(hmp, copyid);
336 failed:
337 	hammer2_voldata_unlock(hmp);
338 	return (error);
339 }
340 
341 /*
342  * Replace existing remote entry
343  */
344 static int
345 hammer2_ioctl_remote_rep(hammer2_inode_t *ip, void *data)
346 {
347 	hammer2_ioc_remote_t *remote = data;
348 	hammer2_dev_t *hmp;
349 	int copyid = remote->copyid;
350 
351 	hmp = ip->pmp->pfs_hmps[0];
352 	if (hmp == NULL)
353 		return (EINVAL);
354 	if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
355 		return (EINVAL);
356 
357 	hammer2_voldata_lock(hmp);
358 	hammer2_voldata_modify(hmp);
359 	/*hammer2_volconf_update(hmp, copyid);*/
360 	hammer2_voldata_unlock(hmp);
361 
362 	return(0);
363 }
364 
365 /*
366  * Retrieve communications socket
367  */
368 static int
369 hammer2_ioctl_socket_get(hammer2_inode_t *ip, void *data)
370 {
371 	return (EOPNOTSUPP);
372 }
373 
374 /*
375  * Set communications socket for connection
376  */
377 static int
378 hammer2_ioctl_socket_set(hammer2_inode_t *ip, void *data)
379 {
380 	hammer2_ioc_remote_t *remote = data;
381 	hammer2_dev_t *hmp;
382 	int copyid = remote->copyid;
383 
384 	hmp = ip->pmp->pfs_hmps[0];
385 	if (hmp == NULL)
386 		return (EINVAL);
387 	if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
388 		return (EINVAL);
389 
390 	hammer2_voldata_lock(hmp);
391 	hammer2_voldata_unlock(hmp);
392 
393 	return(0);
394 }
395 
396 /*
397  * Used to scan and retrieve PFS information.  PFS's are directories under
398  * the super-root.
399  *
400  * To scan PFSs pass name_key=0.  The function will scan for the next
401  * PFS and set all fields, as well as set name_next to the next key.
402  * When no PFSs remain, name_next is set to (hammer2_key_t)-1.
403  *
404  * To retrieve a particular PFS by key, specify the key but note that
405  * the ioctl will return the lowest key >= specified_key, so the caller
406  * must verify the key.
407  *
408  * To retrieve the PFS associated with the file descriptor, pass
409  * name_key set to (hammer2_key_t)-1.
410  */
411 static int
412 hammer2_ioctl_pfs_get(hammer2_inode_t *ip, void *data)
413 {
414 	const hammer2_inode_data_t *ripdata;
415 	hammer2_dev_t *hmp;
416 	hammer2_ioc_pfs_t *pfs;
417 	hammer2_chain_t *parent;
418 	hammer2_chain_t *chain;
419 	hammer2_key_t key_next;
420 	hammer2_key_t save_key;
421 	int error;
422 
423 	hmp = ip->pmp->pfs_hmps[0];
424 	if (hmp == NULL)
425 		return (EINVAL);
426 
427 	pfs = data;
428 	save_key = pfs->name_key;
429 	error = 0;
430 
431 	/*
432 	 * Setup
433 	 */
434 	if (save_key == (hammer2_key_t)-1) {
435 		hammer2_inode_lock(ip->pmp->iroot, 0);
436 		parent = NULL;
437 		chain = hammer2_inode_chain(ip->pmp->iroot, 0,
438 					    HAMMER2_RESOLVE_ALWAYS |
439 					    HAMMER2_RESOLVE_SHARED);
440 	} else {
441 		hammer2_inode_lock(hmp->spmp->iroot, 0);
442 		parent = hammer2_inode_chain(hmp->spmp->iroot, 0,
443 					    HAMMER2_RESOLVE_ALWAYS |
444 					    HAMMER2_RESOLVE_SHARED);
445 		chain = hammer2_chain_lookup(&parent, &key_next,
446 					    pfs->name_key, HAMMER2_KEY_MAX,
447 					    &error,
448 					    HAMMER2_LOOKUP_SHARED);
449 	}
450 
451 	/*
452 	 * Locate next PFS
453 	 */
454 	while (chain) {
455 		if (chain->bref.type == HAMMER2_BREF_TYPE_INODE)
456 			break;
457 		if (parent == NULL) {
458 			hammer2_chain_unlock(chain);
459 			hammer2_chain_drop(chain);
460 			chain = NULL;
461 			break;
462 		}
463 		chain = hammer2_chain_next(&parent, chain, &key_next,
464 					    key_next, HAMMER2_KEY_MAX,
465 					    &error,
466 					    HAMMER2_LOOKUP_SHARED);
467 	}
468 	error = hammer2_error_to_errno(error);
469 
470 	/*
471 	 * Load the data being returned by the ioctl.
472 	 */
473 	if (chain && chain->error == 0) {
474 		ripdata = &chain->data->ipdata;
475 		pfs->name_key = ripdata->meta.name_key;
476 		pfs->pfs_type = ripdata->meta.pfs_type;
477 		pfs->pfs_subtype = ripdata->meta.pfs_subtype;
478 		pfs->pfs_clid = ripdata->meta.pfs_clid;
479 		pfs->pfs_fsid = ripdata->meta.pfs_fsid;
480 		KKASSERT(ripdata->meta.name_len < sizeof(pfs->name));
481 		bcopy(ripdata->filename, pfs->name, ripdata->meta.name_len);
482 		pfs->name[ripdata->meta.name_len] = 0;
483 		ripdata = NULL;	/* safety */
484 
485 		/*
486 		 * Calculate name_next, if any.  We are only accessing
487 		 * chain->bref so we can ignore chain->error (if the key
488 		 * is used later it will error then).
489 		 */
490 		if (parent == NULL) {
491 			pfs->name_next = (hammer2_key_t)-1;
492 		} else {
493 			chain = hammer2_chain_next(&parent, chain, &key_next,
494 						    key_next, HAMMER2_KEY_MAX,
495 						    &error,
496 						    HAMMER2_LOOKUP_SHARED);
497 			if (chain)
498 				pfs->name_next = chain->bref.key;
499 			else
500 				pfs->name_next = (hammer2_key_t)-1;
501 		}
502 	} else {
503 		pfs->name_next = (hammer2_key_t)-1;
504 		error = ENOENT;
505 	}
506 
507 	/*
508 	 * Cleanup
509 	 */
510 	if (chain) {
511 		hammer2_chain_unlock(chain);
512 		hammer2_chain_drop(chain);
513 	}
514 	if (parent) {
515 		hammer2_chain_unlock(parent);
516 		hammer2_chain_drop(parent);
517 	}
518 	if (save_key == (hammer2_key_t)-1) {
519 		hammer2_inode_unlock(ip->pmp->iroot);
520 	} else {
521 		hammer2_inode_unlock(hmp->spmp->iroot);
522 	}
523 
524 	return (error);
525 }
526 
527 /*
528  * Find a specific PFS by name
529  */
530 static int
531 hammer2_ioctl_pfs_lookup(hammer2_inode_t *ip, void *data)
532 {
533 	const hammer2_inode_data_t *ripdata;
534 	hammer2_dev_t *hmp;
535 	hammer2_ioc_pfs_t *pfs;
536 	hammer2_chain_t *parent;
537 	hammer2_chain_t *chain;
538 	hammer2_key_t key_next;
539 	hammer2_key_t lhc;
540 	int error;
541 	size_t len;
542 
543 	hmp = ip->pmp->pfs_hmps[0];
544 	if (hmp == NULL)
545 		return (EINVAL);
546 
547 	pfs = data;
548 	error = 0;
549 
550 	hammer2_inode_lock(hmp->spmp->iroot, HAMMER2_RESOLVE_SHARED);
551 	parent = hammer2_inode_chain(hmp->spmp->iroot, 0,
552 				     HAMMER2_RESOLVE_ALWAYS |
553 				     HAMMER2_RESOLVE_SHARED);
554 
555 	pfs->name[sizeof(pfs->name) - 1] = 0;
556 	len = strlen(pfs->name);
557 	lhc = hammer2_dirhash(pfs->name, len);
558 
559 	chain = hammer2_chain_lookup(&parent, &key_next,
560 					 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
561 					 &error, HAMMER2_LOOKUP_SHARED);
562 	while (chain) {
563 		if (hammer2_chain_dirent_test(chain, pfs->name, len))
564 			break;
565 		chain = hammer2_chain_next(&parent, chain, &key_next,
566 					   key_next,
567 					   lhc + HAMMER2_DIRHASH_LOMASK,
568 					   &error, HAMMER2_LOOKUP_SHARED);
569 	}
570 	error = hammer2_error_to_errno(error);
571 
572 	/*
573 	 * Load the data being returned by the ioctl.
574 	 */
575 	if (chain && chain->error == 0) {
576 		KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
577 		ripdata = &chain->data->ipdata;
578 		pfs->name_key = ripdata->meta.name_key;
579 		pfs->pfs_type = ripdata->meta.pfs_type;
580 		pfs->pfs_subtype = ripdata->meta.pfs_subtype;
581 		pfs->pfs_clid = ripdata->meta.pfs_clid;
582 		pfs->pfs_fsid = ripdata->meta.pfs_fsid;
583 		ripdata = NULL;
584 
585 		hammer2_chain_unlock(chain);
586 		hammer2_chain_drop(chain);
587 	} else if (error == 0) {
588 		error = ENOENT;
589 	}
590 	if (parent) {
591 		hammer2_chain_unlock(parent);
592 		hammer2_chain_drop(parent);
593 	}
594 	hammer2_inode_unlock(hmp->spmp->iroot);
595 
596 	return (error);
597 }
598 
599 /*
600  * Create a new PFS under the super-root
601  */
602 static int
603 hammer2_ioctl_pfs_create(hammer2_inode_t *ip, void *data)
604 {
605 	hammer2_inode_data_t *nipdata;
606 	hammer2_chain_t *nchain;
607 	hammer2_dev_t *hmp;
608 	hammer2_dev_t *force_local;
609 	hammer2_ioc_pfs_t *pfs;
610 	hammer2_inode_t *nip;
611 	hammer2_tid_t mtid;
612 	int error;
613 
614 	hmp = ip->pmp->pfs_hmps[0];	/* XXX */
615 	if (hmp == NULL)
616 		return (EINVAL);
617 
618 	pfs = data;
619 	nip = NULL;
620 
621 	if (pfs->name[0] == 0)
622 		return(EINVAL);
623 	pfs->name[sizeof(pfs->name) - 1] = 0;	/* ensure 0-termination */
624 
625 	if (hammer2_ioctl_pfs_lookup(ip, pfs) == 0)
626 		return(EEXIST);
627 
628 	hammer2_trans_init(hmp->spmp, HAMMER2_TRANS_ISFLUSH);
629 	mtid = hammer2_trans_sub(hmp->spmp);
630 	nip = hammer2_inode_create_pfs(hmp->spmp, pfs->name, strlen(pfs->name),
631 				       &error);
632 	if (error == 0) {
633 		atomic_set_int(&nip->flags, HAMMER2_INODE_NOSIDEQ);
634 		hammer2_inode_modify(nip);
635 		nchain = hammer2_inode_chain(nip, 0, HAMMER2_RESOLVE_ALWAYS);
636 		error = hammer2_chain_modify(nchain, mtid, 0, 0);
637 		KKASSERT(error == 0);
638 		nipdata = &nchain->data->ipdata;
639 
640 		nip->meta.pfs_type = pfs->pfs_type;
641 		nip->meta.pfs_subtype = pfs->pfs_subtype;
642 		nip->meta.pfs_clid = pfs->pfs_clid;
643 		nip->meta.pfs_fsid = pfs->pfs_fsid;
644 		nip->meta.op_flags |= HAMMER2_OPFLAG_PFSROOT;
645 
646 		/*
647 		 * Set default compression and check algorithm.  This
648 		 * can be changed later.
649 		 *
650 		 * Do not allow compression on PFS's with the special name
651 		 * "boot", the boot loader can't decompress (yet).
652 		 */
653 		nip->meta.comp_algo =
654 			HAMMER2_ENC_ALGO(HAMMER2_COMP_NEWFS_DEFAULT);
655 		nip->meta.check_algo =
656 			HAMMER2_ENC_ALGO( HAMMER2_CHECK_XXHASH64);
657 
658 		if (strcasecmp(pfs->name, "boot") == 0) {
659 			nip->meta.comp_algo =
660 				HAMMER2_ENC_ALGO(HAMMER2_COMP_AUTOZERO);
661 		}
662 
663 		/*
664 		 * Super-root isn't mounted, fsync it
665 		 */
666 		hammer2_chain_unlock(nchain);
667 		hammer2_inode_ref(nip);
668 		hammer2_inode_unlock(nip);
669 		hammer2_inode_chain_sync(nip);
670 		hammer2_inode_chain_flush(nip, HAMMER2_XOP_INODE_STOP |
671 					       HAMMER2_XOP_FSSYNC);
672 		hammer2_inode_drop(nip);
673 		/* nip is dead */
674 
675 		/*
676 		 * We still have a ref on the chain, relock and associate
677 		 * with an appropriate PFS.
678 		 */
679 		force_local = (hmp->hflags & HMNT2_LOCAL) ? hmp : NULL;
680 
681 		hammer2_chain_lock(nchain, HAMMER2_RESOLVE_ALWAYS);
682 		nipdata = &nchain->data->ipdata;
683 		kprintf("ADD LOCAL PFS (IOCTL): %s\n", nipdata->filename);
684 		hammer2_pfsalloc(nchain, nipdata, force_local);
685 
686 		hammer2_chain_unlock(nchain);
687 		hammer2_chain_drop(nchain);
688 	}
689 	hammer2_trans_done(hmp->spmp, HAMMER2_TRANS_ISFLUSH |
690 				      HAMMER2_TRANS_SIDEQ);
691 
692 	return (error);
693 }
694 
695 /*
696  * Destroy an existing PFS under the super-root
697  */
698 static int
699 hammer2_ioctl_pfs_delete(hammer2_inode_t *ip, void *data)
700 {
701 	hammer2_ioc_pfs_t *pfs = data;
702 	hammer2_dev_t	*hmp;
703 	hammer2_pfs_t	*spmp;
704 	hammer2_pfs_t	*pmp;
705 	hammer2_xop_unlink_t *xop;
706 	hammer2_inode_t *dip;
707 	int error;
708 	int i;
709 
710 	/*
711 	 * The PFS should be probed, so we should be able to
712 	 * locate it.  We only delete the PFS from the
713 	 * specific H2 block device (hmp), not all of
714 	 * them.  We must remove the PFS from the cluster
715 	 * before we can destroy it.
716 	 */
717 	hmp = ip->pmp->pfs_hmps[0];
718 	if (hmp == NULL)
719 		return (EINVAL);
720 
721 	pfs->name[sizeof(pfs->name) - 1] = 0;	/* ensure termination */
722 
723 	lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
724 
725 	TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
726 		for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
727 			if (pmp->pfs_hmps[i] != hmp)
728 				continue;
729 			if (pmp->pfs_names[i] &&
730 			    strcmp(pmp->pfs_names[i], pfs->name) == 0) {
731 				break;
732 			}
733 		}
734 		if (i != HAMMER2_MAXCLUSTER)
735 			break;
736 	}
737 
738 	if (pmp == NULL) {
739 		lockmgr(&hammer2_mntlk, LK_RELEASE);
740 		return ENOENT;
741 	}
742 	if (pmp->mp) {
743 		lockmgr(&hammer2_mntlk, LK_RELEASE);
744 		return EBUSY;
745 	}
746 
747 	/*
748 	 * Ok, we found the pmp and we have the index.  Permanently remove
749 	 * the PFS from the cluster
750 	 */
751 	kprintf("FOUND PFS %s CLINDEX %d\n", pfs->name, i);
752 	hammer2_pfsdealloc(pmp, i, 1);
753 
754 	lockmgr(&hammer2_mntlk, LK_RELEASE);
755 
756 	/*
757 	 * Now destroy the PFS under its device using the per-device
758 	 * super-root.
759 	 */
760 	spmp = hmp->spmp;
761 	dip = spmp->iroot;
762 	hammer2_trans_init(spmp, 0);
763 	hammer2_inode_lock(dip, 0);
764 
765 	xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
766 	hammer2_xop_setname(&xop->head, pfs->name, strlen(pfs->name));
767 	xop->isdir = 2;
768 	xop->dopermanent = H2DOPERM_PERMANENT | H2DOPERM_FORCE;
769 	hammer2_xop_start(&xop->head, &hammer2_unlink_desc);
770 
771 	error = hammer2_xop_collect(&xop->head, 0);
772 
773 	hammer2_inode_unlock(dip);
774 
775 #if 0
776 	if (error == 0) {
777 	        ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1);
778 	        hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
779 	        if (ip) {
780 	                hammer2_inode_unlink_finisher(ip, NULL);
781 	                hammer2_inode_unlock(ip);
782 	        }
783 	} else {
784 	        hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
785 	}
786 #endif
787 	hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
788 
789 	hammer2_trans_done(spmp, HAMMER2_TRANS_SIDEQ);
790 
791 	return (hammer2_error_to_errno(error));
792 }
793 
794 static int
795 hammer2_ioctl_pfs_snapshot(hammer2_inode_t *ip, void *data)
796 {
797 	hammer2_ioc_pfs_t *pfs = data;
798 	hammer2_dev_t	*hmp;
799 	hammer2_pfs_t	*pmp;
800 	hammer2_chain_t	*chain;
801 	hammer2_inode_t *nip;
802 	hammer2_tid_t	mtid;
803 	size_t name_len;
804 	int error;
805 #if 0
806 	uuid_t opfs_clid;
807 #endif
808 
809 	if (pfs->name[0] == 0)
810 		return(EINVAL);
811 	if (pfs->name[sizeof(pfs->name)-1] != 0)
812 		return(EINVAL);
813 
814 	pmp = ip->pmp;
815 	ip = pmp->iroot;
816 
817 	hmp = pmp->pfs_hmps[0];
818 	if (hmp == NULL)
819 		return (EINVAL);
820 
821 	lockmgr(&hmp->bulklk, LK_EXCLUSIVE);
822 
823 	/*
824 	 * NOSYNC is for debugging.  We skip the filesystem sync and use
825 	 * a normal transaction (which is less likely to stall).  used for
826 	 * testing filesystem consistency.
827 	 *
828 	 * In normal mode we sync the filesystem and use a flush transaction.
829 	 */
830 	if (pfs->pfs_flags & HAMMER2_PFSFLAGS_NOSYNC) {
831 		hammer2_trans_init(pmp, 0);
832 	} else {
833 		hammer2_vfs_sync(pmp->mp, MNT_WAIT);
834 		hammer2_trans_init(pmp, HAMMER2_TRANS_ISFLUSH);
835 	}
836 	mtid = hammer2_trans_sub(pmp);
837 	hammer2_inode_lock(ip, 0);
838 	hammer2_inode_modify(ip);
839 	ip->meta.pfs_lsnap_tid = mtid;
840 
841 	/* XXX cluster it! */
842 	chain = hammer2_inode_chain(ip, 0, HAMMER2_RESOLVE_ALWAYS);
843 
844 	name_len = strlen(pfs->name);
845 	hmp = chain->hmp;
846 
847 	/*
848 	 * Create the snapshot directory under the super-root
849 	 *
850 	 * Set PFS type, generate a unique filesystem id, and generate
851 	 * a cluster id.  Use the same clid when snapshotting a PFS root,
852 	 * which theoretically allows the snapshot to be used as part of
853 	 * the same cluster (perhaps as a cache).
854 	 *
855 	 * Note that pfs_lsnap_tid must be set in the snapshot as well,
856 	 * ensuring that any nocrc/nocomp file data modifications force
857 	 * a copy-on-write.
858 	 *
859 	 * Copy the (flushed) blockref array.  Theoretically we could use
860 	 * chain_duplicate() but it becomes difficult to disentangle
861 	 * the shared core so for now just brute-force it.
862 	 */
863 	hammer2_chain_unlock(chain);
864 	nip = hammer2_inode_create_pfs(hmp->spmp, pfs->name, name_len, &error);
865 	hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
866 
867 	if (nip) {
868 		hammer2_dev_t *force_local;
869 		hammer2_chain_t *nchain;
870 		hammer2_inode_data_t *wipdata;
871 		hammer2_tid_t starting_inum;
872 
873 		atomic_set_int(&nip->flags, HAMMER2_INODE_NOSIDEQ);
874 		hammer2_inode_modify(nip);
875 		nchain = hammer2_inode_chain(nip, 0, HAMMER2_RESOLVE_ALWAYS);
876 		error = hammer2_chain_modify(nchain, mtid, 0, 0);
877 		KKASSERT(error == 0);
878 		wipdata = &nchain->data->ipdata;
879 
880 		starting_inum = ip->pmp->inode_tid + 1;
881 		nip->meta.pfs_inum = starting_inum;
882 		nip->meta.pfs_type = HAMMER2_PFSTYPE_MASTER;
883 		nip->meta.pfs_subtype = HAMMER2_PFSSUBTYPE_SNAPSHOT;
884 		nip->meta.op_flags |= HAMMER2_OPFLAG_PFSROOT;
885 		nip->meta.pfs_lsnap_tid = mtid;
886 		nchain->bref.embed.stats = chain->bref.embed.stats;
887 
888 		kern_uuidgen(&nip->meta.pfs_fsid, 1);
889 
890 #if 0
891 		/*
892 		 * Give the snapshot its own private cluster id.  As a
893 		 * snapshot no further synchronization with the original
894 		 * cluster will be done.
895 		 */
896 		if (chain->flags & HAMMER2_CHAIN_PFSBOUNDARY)
897 			nip->meta.pfs_clid = opfs_clid;
898 		else
899 			kern_uuidgen(&nip->meta.pfs_clid, 1);
900 #endif
901 		kern_uuidgen(&nip->meta.pfs_clid, 1);
902 		nchain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
903 
904 		/* XXX hack blockset copy */
905 		/* XXX doesn't work with real cluster */
906 		wipdata->meta = nip->meta;
907 		hammer2_spin_ex(&pmp->inum_spin);
908 		wipdata->u.blockset = pmp->pfs_iroot_blocksets[0];
909 		hammer2_spin_unex(&pmp->inum_spin);
910 
911 		KKASSERT(wipdata == &nchain->data->ipdata);
912 
913 		hammer2_chain_unlock(nchain);
914 		hammer2_inode_ref(nip);
915 		hammer2_inode_unlock(nip);
916 		hammer2_inode_chain_sync(nip);
917 		hammer2_inode_chain_flush(nip, HAMMER2_XOP_INODE_STOP |
918 					       HAMMER2_XOP_FSSYNC);
919 					       /* XXX | HAMMER2_XOP_VOLHDR */
920 		hammer2_inode_drop(nip);
921 		/* nip is dead */
922 
923 		force_local = (hmp->hflags & HMNT2_LOCAL) ? hmp : NULL;
924 
925 		hammer2_chain_lock(nchain, HAMMER2_RESOLVE_ALWAYS);
926 		wipdata = &nchain->data->ipdata;
927 		kprintf("SNAPSHOT LOCAL PFS (IOCTL): %s\n", wipdata->filename);
928 		hammer2_pfsalloc(nchain, wipdata, force_local);
929 		nchain->pmp->inode_tid = starting_inum;
930 
931 		hammer2_chain_unlock(nchain);
932 		hammer2_chain_drop(nchain);
933 	}
934 
935 	hammer2_chain_unlock(chain);
936 	hammer2_chain_drop(chain);
937 
938 	hammer2_inode_unlock(ip);
939 	if (pfs->pfs_flags & HAMMER2_PFSFLAGS_NOSYNC) {
940 		hammer2_trans_done(pmp, 0);
941 	} else {
942 		hammer2_trans_done(pmp, HAMMER2_TRANS_ISFLUSH |
943 					HAMMER2_TRANS_SIDEQ);
944 	}
945 
946 	lockmgr(&hmp->bulklk, LK_RELEASE);
947 
948 	return (hammer2_error_to_errno(error));
949 }
950 
951 /*
952  * Retrieve the raw inode structure, non-inclusive of node-specific data.
953  */
954 static int
955 hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data)
956 {
957 	hammer2_ioc_inode_t *ino = data;
958 
959 	hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
960 	ino->data_count = hammer2_inode_data_count(ip);
961 	ino->inode_count = hammer2_inode_inode_count(ip);
962 
963 	bzero(&ino->ip_data, sizeof(ino->ip_data));
964 	ino->ip_data.meta = ip->meta;
965 	hammer2_inode_unlock(ip);
966 
967 	return 0;
968 }
969 
970 /*
971  * Set various parameters in an inode which cannot be set through
972  * normal filesystem VNOPS.
973  */
974 static int
975 hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data)
976 {
977 	hammer2_ioc_inode_t *ino = data;
978 
979 	hammer2_trans_init(ip->pmp, 0);
980 	hammer2_inode_lock(ip, 0);
981 
982 	if ((ino->flags & HAMMER2IOC_INODE_FLAG_CHECK) &&
983 	    ip->meta.check_algo != ino->ip_data.meta.check_algo) {
984 		hammer2_inode_modify(ip);
985 		ip->meta.check_algo = ino->ip_data.meta.check_algo;
986 	}
987 	if ((ino->flags & HAMMER2IOC_INODE_FLAG_COMP) &&
988 	    ip->meta.comp_algo != ino->ip_data.meta.comp_algo) {
989 		hammer2_inode_modify(ip);
990 		ip->meta.comp_algo = ino->ip_data.meta.comp_algo;
991 	}
992 
993 	/* Ignore these flags for now...*/
994 	if ((ino->flags & HAMMER2IOC_INODE_FLAG_IQUOTA) &&
995 	    ip->meta.inode_quota != ino->ip_data.meta.inode_quota) {
996 		hammer2_inode_modify(ip);
997 		ip->meta.inode_quota = ino->ip_data.meta.inode_quota;
998 	}
999 	if ((ino->flags & HAMMER2IOC_INODE_FLAG_DQUOTA) &&
1000 	    ip->meta.data_quota != ino->ip_data.meta.data_quota) {
1001 		hammer2_inode_modify(ip);
1002 		ip->meta.data_quota = ino->ip_data.meta.data_quota;
1003 	}
1004 	if ((ino->flags & HAMMER2IOC_INODE_FLAG_COPIES) &&
1005 	    ip->meta.ncopies != ino->ip_data.meta.ncopies) {
1006 		hammer2_inode_modify(ip);
1007 		ip->meta.ncopies = ino->ip_data.meta.ncopies;
1008 	}
1009 	hammer2_inode_unlock(ip);
1010 	hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ);
1011 
1012 	return (0);
1013 }
1014 
1015 static
1016 int
1017 hammer2_ioctl_debug_dump(hammer2_inode_t *ip, u_int flags)
1018 {
1019 	hammer2_chain_t *chain;
1020 	int count = 100000;
1021 	int i;
1022 
1023 	for (i = 0; i < ip->cluster.nchains; ++i) {
1024 		chain = ip->cluster.array[i].chain;
1025 		if (chain == NULL)
1026 			continue;
1027 		kprintf("cluster #%d\n", i);
1028 		hammer2_dump_chain(chain, 0, 0, &count, 'i', flags);
1029 	}
1030 	return 0;
1031 }
1032 
1033 /*
1034  * Turn on or off emergency mode on a filesystem.
1035  */
1036 static
1037 int
1038 hammer2_ioctl_emerg_mode(hammer2_inode_t *ip, u_int mode)
1039 {
1040 	hammer2_pfs_t *pmp;
1041 	hammer2_dev_t *hmp;
1042 	int i;
1043 
1044 	pmp = ip->pmp;
1045 	if (mode) {
1046 		kprintf("hammer2: WARNING: Emergency mode enabled\n");
1047 		atomic_set_int(&pmp->flags, HAMMER2_PMPF_EMERG);
1048 	} else {
1049 		kprintf("hammer2: WARNING: Emergency mode disabled\n");
1050 		atomic_clear_int(&pmp->flags, HAMMER2_PMPF_EMERG);
1051 	}
1052 	for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
1053 		hmp = pmp->pfs_hmps[i];
1054 		if (hmp == NULL)
1055 			continue;
1056 		if (mode)
1057 			atomic_set_int(&hmp->hflags, HMNT2_EMERG);
1058 		else
1059 			atomic_clear_int(&hmp->hflags, HMNT2_EMERG);
1060 	}
1061 	return 0;
1062 }
1063 
1064 /*
1065  * Do a bulkfree scan on media related to the PFS.  This routine will
1066  * flush all PFSs associated with the media before doing the bulkfree
1067  * scan.
1068  *
1069  * This version can only run on non-clustered media.  A new ioctl or a
1070  * temporary mount of @LOCAL will be needed to run on clustered media.
1071  */
1072 static
1073 int
1074 hammer2_ioctl_bulkfree_scan(hammer2_inode_t *ip, void *data)
1075 {
1076 	hammer2_ioc_bulkfree_t *bfi = data;
1077 	hammer2_dev_t	*hmp;
1078 	hammer2_pfs_t	*pmp;
1079 	hammer2_chain_t *vchain;
1080 	int error;
1081 	int didsnap;
1082 
1083 	pmp = ip->pmp;
1084 	ip = pmp->iroot;
1085 
1086 	hmp = pmp->pfs_hmps[0];
1087 	if (hmp == NULL)
1088 		return (EINVAL);
1089 	if (bfi == NULL)
1090 		return (EINVAL);
1091 
1092 	/*
1093 	 * Bulkfree has to be serialized to guarantee at least one sync
1094 	 * inbetween bulkfrees.
1095 	 */
1096 	error = lockmgr(&hmp->bflock, LK_EXCLUSIVE | LK_PCATCH);
1097 	if (error)
1098 		return error;
1099 
1100 	/*
1101 	 * Sync all mounts related to the media
1102 	 */
1103 	lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
1104 	TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
1105 		int etmp;
1106 		int i;
1107 
1108 		for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
1109 			if (pmp->pfs_hmps[i] != hmp)
1110 				continue;
1111 			etmp = hammer2_vfs_sync_pmp(pmp, MNT_WAIT);
1112 			if (etmp && (error == 0 || error == ENOSPC))
1113 				error = etmp;
1114 			break;
1115 		}
1116 	}
1117 	lockmgr(&hammer2_mntlk, LK_RELEASE);
1118 
1119 	if (error && error != ENOSPC)
1120 		goto failed;
1121 
1122 	/*
1123 	 * If we have an ENOSPC error we have to bulkfree on the live
1124 	 * topology.  Otherwise we can bulkfree on a snapshot.
1125 	 */
1126 	if (error) {
1127 		kprintf("hammer2: WARNING! Bulkfree forced to use live "
1128 			"topology due to ENOSPC\n");
1129 		vchain = &hmp->vchain;
1130 		hammer2_chain_ref(vchain);
1131 		didsnap = 0;
1132 	} else {
1133 		vchain = hammer2_chain_bulksnap(hmp);
1134 		didsnap = 1;
1135 	}
1136 
1137 	/*
1138 	 * Normal bulkfree operations do not require a transaction because
1139 	 * they operate on a snapshot, and so can run concurrently with
1140 	 * any operation except another bulkfree.
1141 	 *
1142 	 * If we are running bulkfree on the live topology we have to be
1143 	 * in a FLUSH transaction.
1144 	 */
1145 	if (didsnap == 0)
1146 		hammer2_trans_init(hmp->spmp, HAMMER2_TRANS_ISFLUSH);
1147 
1148 	if (bfi) {
1149 		hammer2_thr_freeze(&hmp->bfthr);
1150 		error = hammer2_bulkfree_pass(hmp, vchain, bfi);
1151 		hammer2_thr_unfreeze(&hmp->bfthr);
1152 	}
1153 	if (didsnap) {
1154 		hammer2_chain_bulkdrop(vchain);
1155 	} else {
1156 		hammer2_chain_drop(vchain);
1157 		hammer2_trans_done(hmp->spmp, HAMMER2_TRANS_ISFLUSH |
1158 					HAMMER2_TRANS_SIDEQ);
1159 	}
1160 	error = hammer2_error_to_errno(error);
1161 
1162 failed:
1163 	lockmgr(&hmp->bflock, LK_RELEASE);
1164 	return error;
1165 }
1166 
1167 /*
1168  * Unconditionally delete meta-data in a hammer2 filesystem
1169  */
1170 static
1171 int
1172 hammer2_ioctl_destroy(hammer2_inode_t *ip, void *data)
1173 {
1174 	hammer2_ioc_destroy_t *iocd = data;
1175 	hammer2_pfs_t *pmp = ip->pmp;
1176 	int error;
1177 
1178 	if (pmp->ronly) {
1179 		error = EROFS;
1180 		return error;
1181 	}
1182 
1183 	switch(iocd->cmd) {
1184 	case HAMMER2_DELETE_FILE:
1185 		/*
1186 		 * Destroy a bad directory entry by name.  Caller must
1187 		 * pass the directory as fd.
1188 		 */
1189 		{
1190 		hammer2_xop_unlink_t *xop;
1191 
1192 		if (iocd->path[sizeof(iocd->path)-1]) {
1193 			error = EINVAL;
1194 			break;
1195 		}
1196 		if (ip->meta.type != HAMMER2_OBJTYPE_DIRECTORY) {
1197 			error = EINVAL;
1198 			break;
1199 		}
1200 		hammer2_pfs_memory_wait(pmp);
1201 		hammer2_trans_init(pmp, 0);
1202 		hammer2_inode_lock(ip, 0);
1203 
1204 		xop = hammer2_xop_alloc(ip, HAMMER2_XOP_MODIFYING);
1205 		hammer2_xop_setname(&xop->head, iocd->path, strlen(iocd->path));
1206 		xop->isdir = -1;
1207 		xop->dopermanent = H2DOPERM_PERMANENT |
1208 				   H2DOPERM_FORCE |
1209 				   H2DOPERM_IGNINO;
1210 		hammer2_xop_start(&xop->head, &hammer2_unlink_desc);
1211 
1212 		error = hammer2_xop_collect(&xop->head, 0);
1213 		error = hammer2_error_to_errno(error);
1214 		hammer2_inode_unlock(ip);
1215 		hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1216 		hammer2_trans_done(pmp, HAMMER2_TRANS_SIDEQ);
1217 		}
1218 		break;
1219 	case HAMMER2_DELETE_INUM:
1220 		/*
1221 		 * Destroy a bad inode by inode number.
1222 		 */
1223 		{
1224 		hammer2_xop_lookup_t *xop;
1225 
1226 		if (iocd->inum < 1) {
1227 			error = EINVAL;
1228 			break;
1229 		}
1230 		hammer2_pfs_memory_wait(pmp);
1231 		hammer2_trans_init(pmp, 0);
1232 
1233 		xop = hammer2_xop_alloc(pmp->iroot, HAMMER2_XOP_MODIFYING);
1234 		xop->lhc = iocd->inum;
1235 		hammer2_xop_start(&xop->head, &hammer2_delete_desc);
1236 		error = hammer2_xop_collect(&xop->head, 0);
1237 		error = hammer2_error_to_errno(error);
1238 		hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1239 		hammer2_trans_done(pmp, HAMMER2_TRANS_SIDEQ);
1240 		}
1241 		break;
1242 	default:
1243 		error = EINVAL;
1244 		break;
1245 	}
1246 	return error;
1247 }
1248 
1249 /*
1250  * Grow a filesystem into its partition size
1251  */
1252 static int
1253 hammer2_ioctl_growfs(hammer2_inode_t *ip, void *data, struct ucred *cred)
1254 {
1255 	hammer2_ioc_growfs_t *grow = data;
1256 	hammer2_dev_t *hmp;
1257 	hammer2_off_t size, delta;
1258 	hammer2_tid_t mtid;
1259 	struct partinfo part;
1260 	struct vattr_lite va;
1261 	struct buf *bp;
1262 	int error;
1263 	int i;
1264 
1265 	hmp = ip->pmp->pfs_hmps[0];
1266 
1267 	if (hmp->nvolumes > 1) {
1268 		kprintf("hammer2: growfs currently unsupported "
1269 			"with multiple volumes\n");
1270 		return EOPNOTSUPP;
1271 	}
1272 	KKASSERT(hmp->total_size == hmp->voldata.volu_size);
1273 
1274 	/*
1275 	 * Extract from disklabel
1276 	 */
1277 	if (VOP_IOCTL(hmp->devvp, DIOCGPART, (void *)&part, 0, cred, NULL) == 0) {
1278 		size = part.media_size;
1279 		kprintf("hammer2: growfs partition-auto to %016jx\n",
1280 			(intmax_t)size);
1281 	} else if (VOP_GETATTR_LITE(hmp->devvp, &va) == 0) {
1282 		size = va.va_size;
1283 		kprintf("hammer2: growfs fstat-auto to %016jx\n",
1284 			(intmax_t)size);
1285 	} else {
1286 		return EINVAL;
1287 	}
1288 
1289 	/*
1290 	 * Expand to devvp size unless specified.
1291 	 */
1292 	grow->modified = 0;
1293 	if (grow->size == 0) {
1294 		grow->size = size;
1295 	} else if (grow->size > size) {
1296 		kprintf("hammer2: growfs size %016jx exceeds device size "
1297 			"%016jx\n",
1298 			(intmax_t)grow->size, (intmax_t)size);
1299 		return EINVAL;
1300 	}
1301 
1302 	/*
1303 	 * This is typically ~8MB alignment to avoid edge cases accessing
1304 	 * reserved blocks at the base of each 2GB zone.
1305 	 */
1306 	grow->size &= ~HAMMER2_VOLUME_ALIGNMASK64;
1307 	delta = grow->size - hmp->voldata.volu_size;
1308 
1309 	/*
1310 	 * Maximum allowed size is 2^63
1311 	 */
1312 	if (grow->size > 0x7FFFFFFFFFFFFFFFLU) {
1313 		kprintf("hammer2: growfs failure, limit is 2^63 - 1 bytes\n");
1314 		return EINVAL;
1315 	}
1316 
1317 	/*
1318 	 * We can't shrink a filesystem
1319 	 */
1320 	if (grow->size < hmp->voldata.volu_size) {
1321 		kprintf("hammer2: growfs failure, "
1322 			"would shrink from %016jx to %016jx\n",
1323 			(intmax_t)hmp->voldata.volu_size,
1324 			(intmax_t)grow->size);
1325 		return EINVAL;
1326 	}
1327 
1328 	if (delta == 0) {
1329 		kprintf("hammer2: growfs - size did not change\n");
1330 		return 0;
1331 	}
1332 
1333 	/*
1334 	 * Clear any new volume header backups that we extend into.
1335 	 * Skip volume headers that are already part of the filesystem.
1336 	 */
1337 	for (i = 0; i < HAMMER2_NUM_VOLHDRS; ++i) {
1338 		if (i * HAMMER2_ZONE_BYTES64 < hmp->voldata.volu_size)
1339 			continue;
1340 		if (i * HAMMER2_ZONE_BYTES64 >= grow->size)
1341 			break;
1342 		kprintf("hammer2: growfs - clear volhdr %d ", i);
1343 		error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
1344 			      HAMMER2_VOLUME_BYTES, &bp);
1345 		if (error) {
1346 			brelse(bp);
1347 			kprintf("I/O error %d\n", error);
1348 			return EINVAL;
1349 		}
1350 		bzero(bp->b_data, HAMMER2_VOLUME_BYTES);
1351 		error = bwrite(bp);
1352 		if (error) {
1353 			kprintf("I/O error %d\n", error);
1354 			return EINVAL;
1355 		}
1356 		kprintf("\n");
1357 	}
1358 
1359 	hammer2_trans_init(hmp->spmp, HAMMER2_TRANS_ISFLUSH);
1360 	mtid = hammer2_trans_sub(hmp->spmp);
1361 
1362 	kprintf("hammer2: growfs - expand by %016jx to %016jx mtid %016jx\n",
1363 		(intmax_t)delta, (intmax_t)grow->size, (intmax_t)mtid);
1364 
1365 
1366 	hammer2_voldata_lock(hmp);
1367 	hammer2_voldata_modify(hmp);
1368 
1369 	/*
1370 	 * NOTE: Just adjusting total_size for a single-volume filesystem
1371 	 *	 or for the last volume in a multi-volume filesystem, is
1372 	 *	 fine.  But we can't grow any other partition in a multi-volume
1373 	 *	 filesystem.  For now we just punt (at the top) on any
1374 	 *	 multi-volume filesystem.
1375 	 */
1376 	hmp->voldata.volu_size = grow->size;
1377 	hmp->voldata.total_size += delta;
1378 	hmp->voldata.allocator_size += delta;
1379 	hmp->voldata.allocator_free += delta;
1380 	hmp->total_size += delta;
1381 	hmp->volumes[0].size += delta;	/* note: indexes first (only) volume */
1382 
1383 	hammer2_voldata_unlock(hmp);
1384 
1385 	hammer2_trans_done(hmp->spmp, HAMMER2_TRANS_ISFLUSH |
1386 				      HAMMER2_TRANS_SIDEQ);
1387 	grow->modified = 1;
1388 
1389 	/*
1390 	 * Flush the mess right here and now.  We could just let the
1391 	 * filesystem syncer do it, but this was a sensitive operation
1392 	 * so don't take any chances.
1393 	 */
1394 	hammer2_vfs_sync(ip->pmp->mp, MNT_WAIT);
1395 
1396 	return 0;
1397 }
1398 
1399 /*
1400  * Get a list of volumes.
1401  */
1402 static int
1403 hammer2_ioctl_volume_list(hammer2_inode_t *ip, void *data)
1404 {
1405 	hammer2_ioc_volume_list_t *vollist = data;
1406 	hammer2_ioc_volume_t entry;
1407 	hammer2_volume_t *vol;
1408 	hammer2_dev_t *hmp;
1409 	hammer2_pfs_t *pmp;
1410 	int i, error = 0, cnt = 0;
1411 
1412 	pmp = ip->pmp;
1413 	hmp = pmp->pfs_hmps[0];
1414 	if (hmp == NULL)
1415 		return (EINVAL);
1416 
1417 	hammer2_voldata_lock(hmp);
1418 	for (i = 0; i < hmp->nvolumes; ++i) {
1419 		if (cnt >= vollist->nvolumes)
1420 			break;
1421 		vol = &hmp->volumes[i];
1422 		bzero(&entry, sizeof(entry));
1423 		/* copy hammer2_volume_t fields */
1424 		entry.id = vol->id;
1425 		bcopy(vol->dev->path, entry.path, sizeof(entry.path));
1426 		entry.offset = vol->offset;
1427 		entry.size = vol->size;
1428 		error = copyout(&entry, &vollist->volumes[cnt], sizeof(entry));
1429 		if (error)
1430 			goto failed;
1431 		cnt++;
1432 	}
1433 	vollist->nvolumes = cnt;
1434 	vollist->version = hmp->voldata.version;
1435 	bcopy(pmp->pfs_names[0], vollist->pfs_name, sizeof(vollist->pfs_name));
1436 failed:
1437 	hammer2_voldata_unlock(hmp);
1438 
1439 	return error;
1440 }
1441