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