2 * Copyright (c) 2011-2015 The DragonFly Project. All rights reserved.
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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
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.
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);
61 //static int hammer2_ioctl_inode_comp_set(hammer2_inode_t *ip, void *data);
62 //static int hammer2_ioctl_inode_comp_rec_set(hammer2_inode_t *ip, void *data);
63 //static int hammer2_ioctl_inode_comp_rec_set2(hammer2_inode_t *ip, void *data);
64 static int hammer2_ioctl_bulkfree_scan(hammer2_inode_t *ip, void *data);
67 hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data, int fflag,
73 * Standard root cred checks, will be selectively ignored below
74 * for ioctls that do not require root creds.
76 error = priv_check_cred(cred, PRIV_HAMMER_IOCTL, 0);
79 case HAMMER2IOC_VERSION_GET:
80 error = hammer2_ioctl_version_get(ip, data);
82 case HAMMER2IOC_RECLUSTER:
84 error = hammer2_ioctl_recluster(ip, data);
86 case HAMMER2IOC_REMOTE_SCAN:
88 error = hammer2_ioctl_remote_scan(ip, data);
90 case HAMMER2IOC_REMOTE_ADD:
92 error = hammer2_ioctl_remote_add(ip, data);
94 case HAMMER2IOC_REMOTE_DEL:
96 error = hammer2_ioctl_remote_del(ip, data);
98 case HAMMER2IOC_REMOTE_REP:
100 error = hammer2_ioctl_remote_rep(ip, data);
102 case HAMMER2IOC_SOCKET_GET:
104 error = hammer2_ioctl_socket_get(ip, data);
106 case HAMMER2IOC_SOCKET_SET:
108 error = hammer2_ioctl_socket_set(ip, data);
110 case HAMMER2IOC_PFS_GET:
112 error = hammer2_ioctl_pfs_get(ip, data);
114 case HAMMER2IOC_PFS_LOOKUP:
116 error = hammer2_ioctl_pfs_lookup(ip, data);
118 case HAMMER2IOC_PFS_CREATE:
120 error = hammer2_ioctl_pfs_create(ip, data);
122 case HAMMER2IOC_PFS_DELETE:
124 error = hammer2_ioctl_pfs_delete(ip, data);
126 case HAMMER2IOC_PFS_SNAPSHOT:
128 error = hammer2_ioctl_pfs_snapshot(ip, data);
130 case HAMMER2IOC_INODE_GET:
131 error = hammer2_ioctl_inode_get(ip, data);
133 case HAMMER2IOC_INODE_SET:
135 error = hammer2_ioctl_inode_set(ip, data);
137 case HAMMER2IOC_BULKFREE_SCAN:
138 error = hammer2_ioctl_bulkfree_scan(ip, data);
140 /*case HAMMER2IOC_INODE_COMP_SET:
141 error = hammer2_ioctl_inode_comp_set(ip, data);
143 case HAMMER2IOC_INODE_COMP_REC_SET:
144 error = hammer2_ioctl_inode_comp_rec_set(ip, data);
146 case HAMMER2IOC_INODE_COMP_REC_SET2:
147 error = hammer2_ioctl_inode_comp_rec_set2(ip, data);
149 case HAMMER2IOC_DEBUG_DUMP:
150 error = hammer2_ioctl_debug_dump(ip);
160 * Retrieve version and basic info
163 hammer2_ioctl_version_get(hammer2_inode_t *ip, void *data)
165 hammer2_mount_t *hmp = ip->pmp->iroot->cluster.focus->hmp;
166 hammer2_ioc_version_t *version = data;
168 version->version = hmp->voldata.version;
173 hammer2_ioctl_recluster(hammer2_inode_t *ip, void *data)
175 hammer2_ioc_recluster_t *recl = data;
177 hammer2_cluster_t *cluster;
180 fp = holdfp(curproc->p_fd, recl->fd, -1);
182 kprintf("reconnect to cluster: XXX ");
183 cluster = &ip->pmp->iroot->cluster;
184 if (cluster->nchains != 1 || cluster->focus == NULL) {
185 kprintf("not a local device mount\n");
188 hammer2_cluster_reconnect(cluster->focus->hmp, fp);
199 * Retrieve information about a remote
202 hammer2_ioctl_remote_scan(hammer2_inode_t *ip, void *data)
204 hammer2_mount_t *hmp = ip->pmp->iroot->cluster.focus->hmp;
205 hammer2_ioc_remote_t *remote = data;
206 int copyid = remote->copyid;
208 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
211 hammer2_voldata_lock(hmp);
212 remote->copy1 = hmp->voldata.copyinfo[copyid];
213 hammer2_voldata_unlock(hmp);
216 * Adjust nextid (GET only)
218 while (++copyid < HAMMER2_COPYID_COUNT &&
219 hmp->voldata.copyinfo[copyid].copyid == 0) {
222 if (copyid == HAMMER2_COPYID_COUNT)
225 remote->nextid = copyid;
231 * Add new remote entry
234 hammer2_ioctl_remote_add(hammer2_inode_t *ip, void *data)
236 hammer2_ioc_remote_t *remote = data;
237 hammer2_pfsmount_t *pmp = ip->pmp;
238 hammer2_mount_t *hmp;
239 int copyid = remote->copyid;
242 if (copyid >= HAMMER2_COPYID_COUNT)
245 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
246 hammer2_voldata_lock(hmp);
248 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
249 if (hmp->voldata.copyinfo[copyid].copyid == 0)
252 if (copyid == HAMMER2_COPYID_COUNT) {
257 hammer2_voldata_modify(hmp);
258 remote->copy1.copyid = copyid;
259 hmp->voldata.copyinfo[copyid] = remote->copy1;
260 hammer2_volconf_update(hmp, copyid);
262 hammer2_voldata_unlock(hmp);
267 * Delete existing remote entry
270 hammer2_ioctl_remote_del(hammer2_inode_t *ip, void *data)
272 hammer2_ioc_remote_t *remote = data;
273 hammer2_pfsmount_t *pmp = ip->pmp;
274 hammer2_mount_t *hmp;
275 int copyid = remote->copyid;
278 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
279 if (copyid >= HAMMER2_COPYID_COUNT)
281 remote->copy1.path[sizeof(remote->copy1.path) - 1] = 0;
282 hammer2_voldata_lock(hmp);
284 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
285 if (hmp->voldata.copyinfo[copyid].copyid == 0)
287 if (strcmp(remote->copy1.path,
288 hmp->voldata.copyinfo[copyid].path) == 0) {
292 if (copyid == HAMMER2_COPYID_COUNT) {
297 hammer2_voldata_modify(hmp);
298 hmp->voldata.copyinfo[copyid].copyid = 0;
299 hammer2_volconf_update(hmp, copyid);
301 hammer2_voldata_unlock(hmp);
306 * Replace existing remote entry
309 hammer2_ioctl_remote_rep(hammer2_inode_t *ip, void *data)
311 hammer2_ioc_remote_t *remote = data;
312 hammer2_mount_t *hmp;
313 int copyid = remote->copyid;
315 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
317 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
320 hammer2_voldata_lock(hmp);
321 hammer2_voldata_modify(hmp);
322 /*hammer2_volconf_update(hmp, copyid);*/
323 hammer2_voldata_unlock(hmp);
329 * Retrieve communications socket
332 hammer2_ioctl_socket_get(hammer2_inode_t *ip, void *data)
338 * Set communications socket for connection
341 hammer2_ioctl_socket_set(hammer2_inode_t *ip, void *data)
343 hammer2_ioc_remote_t *remote = data;
344 hammer2_mount_t *hmp;
345 int copyid = remote->copyid;
347 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
348 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
351 hammer2_voldata_lock(hmp);
352 hammer2_voldata_unlock(hmp);
358 * Used to scan and retrieve PFS information. PFS's are directories under
361 * To scan PFSs pass name_key=0. The function will scan for the next
362 * PFS and set all fields, as well as set name_next to the next key.
363 * When no PFSs remain, name_next is set to (hammer2_key_t)-1.
365 * To retrieve the PFS associated with the file descriptor, pass
366 * name_key set to (hammer2_key_t)-1.
369 hammer2_ioctl_pfs_get(hammer2_inode_t *ip, void *data)
371 const hammer2_inode_data_t *ripdata;
372 hammer2_mount_t *hmp;
373 hammer2_ioc_pfs_t *pfs;
374 hammer2_cluster_t *cparent;
375 hammer2_cluster_t *rcluster;
376 hammer2_cluster_t *cluster;
377 hammer2_key_t key_next;
382 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
384 cparent = hammer2_inode_lock_ex(hmp->spmp->iroot);
385 rcluster = hammer2_inode_lock_ex(ip->pmp->iroot);
388 * Search for the first key or specific key. Remember that keys
389 * can be returned in any order.
391 if (pfs->name_key == 0) {
392 cluster = hammer2_cluster_lookup(cparent, &key_next,
393 0, (hammer2_key_t)-1,
395 } else if (pfs->name_key == (hammer2_key_t)-1) {
396 ripdata = &hammer2_cluster_rdata(rcluster)->ipdata;
397 cluster = hammer2_cluster_lookup(cparent, &key_next,
401 ripdata = NULL; /* safety */
403 cluster = hammer2_cluster_lookup(cparent, &key_next,
404 pfs->name_key, pfs->name_key,
407 hammer2_inode_unlock_ex(ip->pmp->iroot, rcluster);
410 hammer2_cluster_type(cluster) != HAMMER2_BREF_TYPE_INODE) {
411 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
412 key_next, (hammer2_key_t)-1,
417 * Load the data being returned by the ioctl.
419 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
420 pfs->name_key = ripdata->name_key;
421 pfs->pfs_type = ripdata->pfs_type;
422 pfs->pfs_clid = ripdata->pfs_clid;
423 pfs->pfs_fsid = ripdata->pfs_fsid;
424 KKASSERT(ripdata->name_len < sizeof(pfs->name));
425 bcopy(ripdata->filename, pfs->name, ripdata->name_len);
426 pfs->name[ripdata->name_len] = 0;
427 ripdata = NULL; /* safety */
430 * Calculate the next field
433 cluster = hammer2_cluster_next(cparent, cluster,
435 0, (hammer2_key_t)-1,
438 hammer2_cluster_type(cluster) !=
439 HAMMER2_BREF_TYPE_INODE);
441 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
442 pfs->name_next = ripdata->name_key;
443 hammer2_cluster_unlock(cluster);
445 pfs->name_next = (hammer2_key_t)-1;
448 pfs->name_next = (hammer2_key_t)-1;
451 hammer2_inode_unlock_ex(hmp->spmp->iroot, cparent);
457 * Find a specific PFS by name
460 hammer2_ioctl_pfs_lookup(hammer2_inode_t *ip, void *data)
462 const hammer2_inode_data_t *ripdata;
463 hammer2_mount_t *hmp;
464 hammer2_ioc_pfs_t *pfs;
465 hammer2_cluster_t *cparent;
466 hammer2_cluster_t *cluster;
467 hammer2_key_t key_next;
474 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
476 cparent = hammer2_inode_lock_sh(hmp->spmp->iroot);
478 pfs->name[sizeof(pfs->name) - 1] = 0;
479 len = strlen(pfs->name);
480 lhc = hammer2_dirhash(pfs->name, len);
482 cluster = hammer2_cluster_lookup(cparent, &key_next,
483 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
484 HAMMER2_LOOKUP_SHARED, &ddflag);
486 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE) {
487 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
488 if (ripdata->name_len == len &&
489 bcmp(ripdata->filename, pfs->name, len) == 0) {
492 ripdata = NULL; /* safety */
494 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
496 lhc + HAMMER2_DIRHASH_LOMASK,
497 HAMMER2_LOOKUP_SHARED);
501 * Load the data being returned by the ioctl.
504 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
505 pfs->name_key = ripdata->name_key;
506 pfs->pfs_type = ripdata->pfs_type;
507 pfs->pfs_clid = ripdata->pfs_clid;
508 pfs->pfs_fsid = ripdata->pfs_fsid;
511 hammer2_cluster_unlock(cluster);
515 hammer2_inode_unlock_sh(hmp->spmp->iroot, cparent);
521 * Create a new PFS under the super-root
524 hammer2_ioctl_pfs_create(hammer2_inode_t *ip, void *data)
526 hammer2_inode_data_t *nipdata;
527 hammer2_mount_t *hmp;
528 hammer2_ioc_pfs_t *pfs;
529 hammer2_inode_t *nip;
530 hammer2_cluster_t *ncluster;
531 hammer2_trans_t trans;
534 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
538 if (pfs->name[0] == 0)
540 pfs->name[sizeof(pfs->name) - 1] = 0; /* ensure 0-termination */
542 if (hammer2_ioctl_pfs_lookup(ip, pfs) == 0)
545 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_NEWINODE);
546 nip = hammer2_inode_create(&trans, hmp->spmp->iroot, NULL, NULL,
547 pfs->name, strlen(pfs->name),
550 nipdata = hammer2_cluster_modify_ip(&trans, nip, ncluster, 0);
551 nipdata->pfs_type = pfs->pfs_type;
552 nipdata->pfs_clid = pfs->pfs_clid;
553 nipdata->pfs_fsid = pfs->pfs_fsid;
556 * Do not allow compression on PFS's with the special name
557 * "boot", the boot loader can't decompress (yet).
559 if (strcmp(pfs->name, "boot") == 0)
560 nipdata->comp_algo = HAMMER2_ENC_ALGO(
561 HAMMER2_COMP_AUTOZERO);
562 hammer2_cluster_modsync(ncluster);
563 hammer2_inode_unlock_ex(nip, ncluster);
565 hammer2_trans_done(&trans);
571 * Destroy an existing PFS under the super-root
574 hammer2_ioctl_pfs_delete(hammer2_inode_t *ip, void *data)
576 hammer2_mount_t *hmp;
577 hammer2_ioc_pfs_t *pfs = data;
578 hammer2_trans_t trans;
581 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
582 hammer2_trans_init(&trans, ip->pmp, 0);
583 error = hammer2_unlink_file(&trans, hmp->spmp->iroot,
584 pfs->name, strlen(pfs->name),
586 hammer2_trans_done(&trans);
592 hammer2_ioctl_pfs_snapshot(hammer2_inode_t *ip, void *data)
594 hammer2_ioc_pfs_t *pfs = data;
595 hammer2_trans_t trans;
596 hammer2_cluster_t *cparent;
599 if (pfs->name[0] == 0)
601 if (pfs->name[sizeof(pfs->name)-1] != 0)
604 hammer2_vfs_sync(ip->pmp->mp, MNT_WAIT);
606 hammer2_trans_init(&trans, ip->pmp,
607 HAMMER2_TRANS_ISFLUSH | HAMMER2_TRANS_NEWINODE);
608 cparent = hammer2_inode_lock_ex(ip);
609 error = hammer2_cluster_snapshot(&trans, cparent, pfs);
610 hammer2_inode_unlock_ex(ip, cparent);
611 hammer2_trans_done(&trans);
617 * Retrieve the raw inode structure
620 hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data)
622 const hammer2_inode_data_t *ripdata;
623 hammer2_ioc_inode_t *ino;
624 hammer2_cluster_t *cparent;
628 cparent = hammer2_inode_lock_sh(ip);
629 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
630 ino->ip_data = *ripdata;
632 hammer2_inode_unlock_sh(ip, cparent);
638 * Set various parameters in an inode which cannot be set through
639 * normal filesystem VNOPS.
642 hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data)
644 const hammer2_inode_data_t *ripdata;
645 hammer2_inode_data_t *wipdata;
646 hammer2_ioc_inode_t *ino = data;
647 hammer2_cluster_t *cparent;
648 hammer2_trans_t trans;
652 hammer2_trans_init(&trans, ip->pmp, 0);
653 cparent = hammer2_inode_lock_ex(ip);
654 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
656 if (ino->ip_data.check_algo != ripdata->check_algo) {
657 wipdata = hammer2_cluster_modify_ip(&trans, ip, cparent, 0);
658 wipdata->check_algo = ino->ip_data.check_algo;
659 ripdata = wipdata; /* safety */
660 hammer2_cluster_setmethod_check(&trans, cparent,
661 wipdata->check_algo);
664 if (ino->ip_data.comp_algo != ripdata->comp_algo) {
665 wipdata = hammer2_cluster_modify_ip(&trans, ip, cparent, 0);
666 wipdata->comp_algo = ino->ip_data.comp_algo;
667 ripdata = wipdata; /* safety */
672 /* Ignore these flags for now...*/
673 if (ino->flags & HAMMER2IOC_INODE_FLAG_IQUOTA) {
675 if (ino->flags & HAMMER2IOC_INODE_FLAG_DQUOTA) {
677 if (ino->flags & HAMMER2IOC_INODE_FLAG_COPIES) {
680 hammer2_cluster_modsync(cparent);
681 hammer2_trans_done(&trans);
682 hammer2_inode_unlock_ex(ip, cparent);
689 hammer2_ioctl_debug_dump(hammer2_inode_t *ip)
691 hammer2_chain_t *chain;
695 for (i = 0; i < ip->cluster.nchains; ++i) {
696 chain = ip->cluster.array[i].chain;
699 hammer2_dump_chain(chain, 0, &count, 'i');
706 hammer2_ioctl_bulkfree_scan(hammer2_inode_t *ip, void *data)
708 hammer2_ioc_bulkfree_t *bfi = data;
709 hammer2_mount_t *hmp = ip->pmp->iroot->cluster.focus->hmp;
712 /* XXX run local cluster targets only */
713 error = hammer2_bulkfree_pass(hmp, bfi);