2 * Copyright (c) 2011-2014 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);
66 hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data, int fflag,
72 * Standard root cred checks, will be selectively ignored below
73 * for ioctls that do not require root creds.
75 error = priv_check_cred(cred, PRIV_HAMMER_IOCTL, 0);
78 case HAMMER2IOC_VERSION_GET:
79 error = hammer2_ioctl_version_get(ip, data);
81 case HAMMER2IOC_RECLUSTER:
83 error = hammer2_ioctl_recluster(ip, data);
85 case HAMMER2IOC_REMOTE_SCAN:
87 error = hammer2_ioctl_remote_scan(ip, data);
89 case HAMMER2IOC_REMOTE_ADD:
91 error = hammer2_ioctl_remote_add(ip, data);
93 case HAMMER2IOC_REMOTE_DEL:
95 error = hammer2_ioctl_remote_del(ip, data);
97 case HAMMER2IOC_REMOTE_REP:
99 error = hammer2_ioctl_remote_rep(ip, data);
101 case HAMMER2IOC_SOCKET_GET:
103 error = hammer2_ioctl_socket_get(ip, data);
105 case HAMMER2IOC_SOCKET_SET:
107 error = hammer2_ioctl_socket_set(ip, data);
109 case HAMMER2IOC_PFS_GET:
111 error = hammer2_ioctl_pfs_get(ip, data);
113 case HAMMER2IOC_PFS_LOOKUP:
115 error = hammer2_ioctl_pfs_lookup(ip, data);
117 case HAMMER2IOC_PFS_CREATE:
119 error = hammer2_ioctl_pfs_create(ip, data);
121 case HAMMER2IOC_PFS_DELETE:
123 error = hammer2_ioctl_pfs_delete(ip, data);
125 case HAMMER2IOC_PFS_SNAPSHOT:
127 error = hammer2_ioctl_pfs_snapshot(ip, data);
129 case HAMMER2IOC_INODE_GET:
130 error = hammer2_ioctl_inode_get(ip, data);
132 case HAMMER2IOC_INODE_SET:
134 error = hammer2_ioctl_inode_set(ip, data);
136 /*case HAMMER2IOC_INODE_COMP_SET:
137 error = hammer2_ioctl_inode_comp_set(ip, data);
139 case HAMMER2IOC_INODE_COMP_REC_SET:
140 error = hammer2_ioctl_inode_comp_rec_set(ip, data);
142 case HAMMER2IOC_INODE_COMP_REC_SET2:
143 error = hammer2_ioctl_inode_comp_rec_set2(ip, data);
145 case HAMMER2IOC_DEBUG_DUMP:
146 error = hammer2_ioctl_debug_dump(ip);
156 * Retrieve version and basic info
159 hammer2_ioctl_version_get(hammer2_inode_t *ip, void *data)
161 hammer2_mount_t *hmp = ip->pmp->iroot->cluster.focus->hmp;
162 hammer2_ioc_version_t *version = data;
164 version->version = hmp->voldata.version;
169 hammer2_ioctl_recluster(hammer2_inode_t *ip, void *data)
171 hammer2_ioc_recluster_t *recl = data;
173 hammer2_cluster_t *cluster;
176 fp = holdfp(curproc->p_fd, recl->fd, -1);
178 kprintf("reconnect to cluster: ");
179 cluster = &ip->pmp->iroot->cluster;
180 if (cluster->nchains != 1 || cluster->focus == NULL) {
181 kprintf("not a local device mount\n");
184 hammer2_cluster_reconnect(cluster->focus->hmp, fp);
195 * Retrieve information about a remote
198 hammer2_ioctl_remote_scan(hammer2_inode_t *ip, void *data)
200 hammer2_mount_t *hmp = ip->pmp->iroot->cluster.focus->hmp;
201 hammer2_ioc_remote_t *remote = data;
202 int copyid = remote->copyid;
204 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
207 hammer2_voldata_lock(hmp);
208 remote->copy1 = hmp->voldata.copyinfo[copyid];
209 hammer2_voldata_unlock(hmp);
212 * Adjust nextid (GET only)
214 while (++copyid < HAMMER2_COPYID_COUNT &&
215 hmp->voldata.copyinfo[copyid].copyid == 0) {
218 if (copyid == HAMMER2_COPYID_COUNT)
221 remote->nextid = copyid;
227 * Add new remote entry
230 hammer2_ioctl_remote_add(hammer2_inode_t *ip, void *data)
232 hammer2_ioc_remote_t *remote = data;
233 hammer2_pfsmount_t *pmp = ip->pmp;
234 hammer2_mount_t *hmp;
235 int copyid = remote->copyid;
238 if (copyid >= HAMMER2_COPYID_COUNT)
241 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
242 hammer2_voldata_lock(hmp);
244 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
245 if (hmp->voldata.copyinfo[copyid].copyid == 0)
248 if (copyid == HAMMER2_COPYID_COUNT) {
253 hammer2_voldata_modify(hmp);
254 remote->copy1.copyid = copyid;
255 hmp->voldata.copyinfo[copyid] = remote->copy1;
256 hammer2_volconf_update(hmp, copyid);
258 hammer2_voldata_unlock(hmp);
263 * Delete existing remote entry
266 hammer2_ioctl_remote_del(hammer2_inode_t *ip, void *data)
268 hammer2_ioc_remote_t *remote = data;
269 hammer2_pfsmount_t *pmp = ip->pmp;
270 hammer2_mount_t *hmp;
271 int copyid = remote->copyid;
274 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
275 if (copyid >= HAMMER2_COPYID_COUNT)
277 remote->copy1.path[sizeof(remote->copy1.path) - 1] = 0;
278 hammer2_voldata_lock(hmp);
280 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
281 if (hmp->voldata.copyinfo[copyid].copyid == 0)
283 if (strcmp(remote->copy1.path,
284 hmp->voldata.copyinfo[copyid].path) == 0) {
288 if (copyid == HAMMER2_COPYID_COUNT) {
293 hammer2_voldata_modify(hmp);
294 hmp->voldata.copyinfo[copyid].copyid = 0;
295 hammer2_volconf_update(hmp, copyid);
297 hammer2_voldata_unlock(hmp);
302 * Replace existing remote entry
305 hammer2_ioctl_remote_rep(hammer2_inode_t *ip, void *data)
307 hammer2_ioc_remote_t *remote = data;
308 hammer2_mount_t *hmp;
309 int copyid = remote->copyid;
311 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
313 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
316 hammer2_voldata_lock(hmp);
317 hammer2_voldata_modify(hmp);
318 /*hammer2_volconf_update(hmp, copyid);*/
319 hammer2_voldata_unlock(hmp);
325 * Retrieve communications socket
328 hammer2_ioctl_socket_get(hammer2_inode_t *ip, void *data)
334 * Set communications socket for connection
337 hammer2_ioctl_socket_set(hammer2_inode_t *ip, void *data)
339 hammer2_ioc_remote_t *remote = data;
340 hammer2_mount_t *hmp;
341 int copyid = remote->copyid;
343 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
344 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
347 hammer2_voldata_lock(hmp);
348 hammer2_voldata_unlock(hmp);
354 * Used to scan and retrieve PFS information. PFS's are directories under
357 * To scan PFSs pass name_key=0. The function will scan for the next
358 * PFS and set all fields, as well as set name_next to the next key.
359 * When no PFSs remain, name_next is set to (hammer2_key_t)-1.
361 * To retrieve the PFS associated with the file descriptor, pass
362 * name_key set to (hammer2_key_t)-1.
365 hammer2_ioctl_pfs_get(hammer2_inode_t *ip, void *data)
367 const hammer2_inode_data_t *ipdata;
368 hammer2_mount_t *hmp;
369 hammer2_ioc_pfs_t *pfs;
370 hammer2_cluster_t *cparent;
371 hammer2_cluster_t *rcluster;
372 hammer2_cluster_t *cluster;
373 hammer2_key_t key_next;
378 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
380 cparent = hammer2_inode_lock_ex(hmp->spmp->iroot);
381 rcluster = hammer2_inode_lock_ex(ip->pmp->iroot);
384 * Search for the first key or specific key. Remember that keys
385 * can be returned in any order.
387 if (pfs->name_key == 0) {
388 cluster = hammer2_cluster_lookup(cparent, &key_next,
389 0, (hammer2_key_t)-1,
391 } else if (pfs->name_key == (hammer2_key_t)-1) {
392 ipdata = &hammer2_cluster_data(rcluster)->ipdata;
393 cluster = hammer2_cluster_lookup(cparent, &key_next,
397 ipdata = NULL; /* safety */
399 cluster = hammer2_cluster_lookup(cparent, &key_next,
400 pfs->name_key, pfs->name_key,
403 hammer2_inode_unlock_ex(ip->pmp->iroot, rcluster);
406 hammer2_cluster_type(cluster) != HAMMER2_BREF_TYPE_INODE) {
407 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
408 key_next, (hammer2_key_t)-1,
413 * Load the data being returned by the ioctl.
415 ipdata = &hammer2_cluster_data(cluster)->ipdata;
416 pfs->name_key = ipdata->name_key;
417 pfs->pfs_type = ipdata->pfs_type;
418 pfs->pfs_clid = ipdata->pfs_clid;
419 pfs->pfs_fsid = ipdata->pfs_fsid;
420 KKASSERT(ipdata->name_len < sizeof(pfs->name));
421 bcopy(ipdata->filename, pfs->name, ipdata->name_len);
422 pfs->name[ipdata->name_len] = 0;
423 ipdata = NULL; /* safety */
426 * Calculate the next field
429 cluster = hammer2_cluster_next(cparent, cluster,
431 0, (hammer2_key_t)-1,
434 hammer2_cluster_type(cluster) !=
435 HAMMER2_BREF_TYPE_INODE);
437 ipdata = &hammer2_cluster_data(cluster)->ipdata;
438 pfs->name_next = ipdata->name_key;
439 hammer2_cluster_unlock(cluster);
441 pfs->name_next = (hammer2_key_t)-1;
444 pfs->name_next = (hammer2_key_t)-1;
447 hammer2_inode_unlock_ex(hmp->spmp->iroot, cparent);
453 * Find a specific PFS by name
456 hammer2_ioctl_pfs_lookup(hammer2_inode_t *ip, void *data)
458 const hammer2_inode_data_t *ipdata;
459 hammer2_mount_t *hmp;
460 hammer2_ioc_pfs_t *pfs;
461 hammer2_cluster_t *cparent;
462 hammer2_cluster_t *cluster;
463 hammer2_key_t key_next;
470 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
472 cparent = hammer2_inode_lock_sh(hmp->spmp->iroot);
474 pfs->name[sizeof(pfs->name) - 1] = 0;
475 len = strlen(pfs->name);
476 lhc = hammer2_dirhash(pfs->name, len);
478 cluster = hammer2_cluster_lookup(cparent, &key_next,
479 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
480 HAMMER2_LOOKUP_SHARED, &ddflag);
482 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE) {
483 ipdata = &hammer2_cluster_data(cluster)->ipdata;
484 if (ipdata->name_len == len &&
485 bcmp(ipdata->filename, pfs->name, len) == 0) {
488 ipdata = NULL; /* safety */
490 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
492 lhc + HAMMER2_DIRHASH_LOMASK,
493 HAMMER2_LOOKUP_SHARED);
497 * Load the data being returned by the ioctl.
500 ipdata = &hammer2_cluster_data(cluster)->ipdata;
501 pfs->name_key = ipdata->name_key;
502 pfs->pfs_type = ipdata->pfs_type;
503 pfs->pfs_clid = ipdata->pfs_clid;
504 pfs->pfs_fsid = ipdata->pfs_fsid;
507 hammer2_cluster_unlock(cluster);
511 hammer2_inode_unlock_sh(hmp->spmp->iroot, cparent);
517 * Create a new PFS under the super-root
520 hammer2_ioctl_pfs_create(hammer2_inode_t *ip, void *data)
522 hammer2_inode_data_t *nipdata;
523 hammer2_mount_t *hmp;
524 hammer2_ioc_pfs_t *pfs;
525 hammer2_inode_t *nip;
526 hammer2_cluster_t *ncluster;
527 hammer2_trans_t trans;
530 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
534 if (pfs->name[0] == 0)
536 pfs->name[sizeof(pfs->name) - 1] = 0; /* ensure 0-termination */
538 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_NEWINODE);
539 nip = hammer2_inode_create(&trans, hmp->spmp->iroot, NULL, NULL,
540 pfs->name, strlen(pfs->name),
543 nipdata = hammer2_cluster_modify_ip(&trans, nip, ncluster, 0);
544 nipdata->pfs_type = pfs->pfs_type;
545 nipdata->pfs_clid = pfs->pfs_clid;
546 nipdata->pfs_fsid = pfs->pfs_fsid;
549 * Do not allow compression on PFS's with the special name
550 * "boot", the boot loader can't decompress (yet).
552 if (strcmp(pfs->name, "boot") == 0)
553 nipdata->comp_algo = HAMMER2_COMP_AUTOZERO;
554 hammer2_cluster_modsync(ncluster);
555 hammer2_inode_unlock_ex(nip, ncluster);
557 hammer2_trans_done(&trans);
563 * Destroy an existing PFS under the super-root
566 hammer2_ioctl_pfs_delete(hammer2_inode_t *ip, void *data)
568 hammer2_mount_t *hmp;
569 hammer2_ioc_pfs_t *pfs = data;
570 hammer2_trans_t trans;
573 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
574 hammer2_trans_init(&trans, ip->pmp, 0);
575 error = hammer2_unlink_file(&trans, hmp->spmp->iroot,
576 pfs->name, strlen(pfs->name),
578 hammer2_trans_done(&trans);
584 hammer2_ioctl_pfs_snapshot(hammer2_inode_t *ip, void *data)
586 hammer2_ioc_pfs_t *pfs = data;
587 hammer2_trans_t trans;
588 hammer2_cluster_t *cparent;
591 if (pfs->name[0] == 0)
593 if (pfs->name[sizeof(pfs->name)-1] != 0)
596 hammer2_vfs_sync(ip->pmp->mp, MNT_WAIT);
598 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_NEWINODE);
599 cparent = hammer2_inode_lock_ex(ip);
600 error = hammer2_cluster_snapshot(&trans, cparent, pfs);
601 hammer2_inode_unlock_ex(ip, cparent);
602 hammer2_trans_done(&trans);
608 * Retrieve the raw inode structure
611 hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data)
613 const hammer2_inode_data_t *ipdata;
614 hammer2_ioc_inode_t *ino;
615 hammer2_cluster_t *cparent;
619 cparent = hammer2_inode_lock_sh(ip);
620 ipdata = &hammer2_cluster_data(cparent)->ipdata;
621 ino->ip_data = *ipdata;
623 hammer2_inode_unlock_sh(ip, cparent);
629 * Set various parameters in an inode which cannot be set through
630 * normal filesystem VNOPS.
633 hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data)
635 const hammer2_inode_data_t *ripdata;
636 hammer2_inode_data_t *wipdata;
637 hammer2_ioc_inode_t *ino = data;
638 hammer2_cluster_t *cparent;
639 hammer2_trans_t trans;
643 hammer2_trans_init(&trans, ip->pmp, 0);
644 cparent = hammer2_inode_lock_ex(ip);
645 ripdata = &hammer2_cluster_data(cparent)->ipdata;
647 if (ino->ip_data.comp_algo != ripdata->comp_algo) {
648 wipdata = hammer2_cluster_modify_ip(&trans, ip, cparent, 0);
649 wipdata->comp_algo = ino->ip_data.comp_algo;
650 ripdata = wipdata; /* safety */
655 /* Ignore these flags for now...*/
656 if (ino->flags & HAMMER2IOC_INODE_FLAG_IQUOTA) {
658 if (ino->flags & HAMMER2IOC_INODE_FLAG_DQUOTA) {
660 if (ino->flags & HAMMER2IOC_INODE_FLAG_COPIES) {
663 hammer2_cluster_modsync(cparent);
664 hammer2_trans_done(&trans);
665 hammer2_inode_unlock_ex(ip, cparent);
672 hammer2_ioctl_debug_dump(hammer2_inode_t *ip)
674 hammer2_chain_t *chain;
678 for (i = 0; i < ip->cluster.nchains; ++i) {
679 chain = ip->cluster.array[i];
682 hammer2_dump_chain(chain, 0, &count, 'i');