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;
174 fp = holdfp(curproc->p_fd, recl->fd, -1);
176 kprintf("reconnect to cluster\n");
177 hammer2_cluster_reconnect(ip->pmp, fp);
185 * Retrieve information about a remote
188 hammer2_ioctl_remote_scan(hammer2_inode_t *ip, void *data)
190 hammer2_mount_t *hmp = ip->pmp->iroot->cluster.focus->hmp;
191 hammer2_ioc_remote_t *remote = data;
192 int copyid = remote->copyid;
194 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
197 hammer2_voldata_lock(hmp);
198 remote->copy1 = hmp->voldata.copyinfo[copyid];
199 hammer2_voldata_unlock(hmp);
202 * Adjust nextid (GET only)
204 while (++copyid < HAMMER2_COPYID_COUNT &&
205 hmp->voldata.copyinfo[copyid].copyid == 0) {
208 if (copyid == HAMMER2_COPYID_COUNT)
211 remote->nextid = copyid;
217 * Add new remote entry
220 hammer2_ioctl_remote_add(hammer2_inode_t *ip, void *data)
222 hammer2_ioc_remote_t *remote = data;
223 hammer2_pfsmount_t *pmp = ip->pmp;
224 hammer2_mount_t *hmp;
225 int copyid = remote->copyid;
228 if (copyid >= HAMMER2_COPYID_COUNT)
231 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
232 hammer2_voldata_lock(hmp);
234 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
235 if (hmp->voldata.copyinfo[copyid].copyid == 0)
238 if (copyid == HAMMER2_COPYID_COUNT) {
243 hammer2_voldata_modify(hmp);
244 remote->copy1.copyid = copyid;
245 hmp->voldata.copyinfo[copyid] = remote->copy1;
246 hammer2_volconf_update(pmp, copyid);
248 hammer2_voldata_unlock(hmp);
253 * Delete existing remote entry
256 hammer2_ioctl_remote_del(hammer2_inode_t *ip, void *data)
258 hammer2_ioc_remote_t *remote = data;
259 hammer2_pfsmount_t *pmp = ip->pmp;
260 hammer2_mount_t *hmp;
261 int copyid = remote->copyid;
264 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
265 if (copyid >= HAMMER2_COPYID_COUNT)
267 remote->copy1.path[sizeof(remote->copy1.path) - 1] = 0;
268 hammer2_voldata_lock(hmp);
270 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
271 if (hmp->voldata.copyinfo[copyid].copyid == 0)
273 if (strcmp(remote->copy1.path,
274 hmp->voldata.copyinfo[copyid].path) == 0) {
278 if (copyid == HAMMER2_COPYID_COUNT) {
283 hammer2_voldata_modify(hmp);
284 hmp->voldata.copyinfo[copyid].copyid = 0;
285 hammer2_volconf_update(pmp, copyid);
287 hammer2_voldata_unlock(hmp);
292 * Replace existing remote entry
295 hammer2_ioctl_remote_rep(hammer2_inode_t *ip, void *data)
297 hammer2_ioc_remote_t *remote = data;
298 hammer2_mount_t *hmp;
299 int copyid = remote->copyid;
301 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
303 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
306 hammer2_voldata_lock(hmp);
307 hammer2_voldata_modify(hmp);
308 /*hammer2_volconf_update(pmp, copyid);*/
309 hammer2_voldata_unlock(hmp);
315 * Retrieve communications socket
318 hammer2_ioctl_socket_get(hammer2_inode_t *ip, void *data)
324 * Set communications socket for connection
327 hammer2_ioctl_socket_set(hammer2_inode_t *ip, void *data)
329 hammer2_ioc_remote_t *remote = data;
330 hammer2_mount_t *hmp;
331 int copyid = remote->copyid;
333 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
334 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
337 hammer2_voldata_lock(hmp);
338 hammer2_voldata_unlock(hmp);
344 * Used to scan and retrieve PFS information. PFS's are directories under
347 * To scan PFSs pass name_key=0. The function will scan for the next
348 * PFS and set all fields, as well as set name_next to the next key.
349 * When no PFSs remain, name_next is set to (hammer2_key_t)-1.
351 * To retrieve the PFS associated with the file descriptor, pass
352 * name_key set to (hammer2_key_t)-1.
355 hammer2_ioctl_pfs_get(hammer2_inode_t *ip, void *data)
357 const hammer2_inode_data_t *ipdata;
358 hammer2_mount_t *hmp;
359 hammer2_ioc_pfs_t *pfs;
360 hammer2_cluster_t *cparent;
361 hammer2_cluster_t *rcluster;
362 hammer2_cluster_t *cluster;
363 hammer2_key_t key_next;
368 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
370 cparent = hammer2_inode_lock_ex(hmp->spmp->iroot);
371 rcluster = hammer2_inode_lock_ex(ip->pmp->iroot);
374 * Search for the first key or specific key. Remember that keys
375 * can be returned in any order.
377 if (pfs->name_key == 0) {
378 cluster = hammer2_cluster_lookup(cparent, &key_next,
379 0, (hammer2_key_t)-1,
381 } else if (pfs->name_key == (hammer2_key_t)-1) {
382 ipdata = &hammer2_cluster_data(rcluster)->ipdata;
383 cluster = hammer2_cluster_lookup(cparent, &key_next,
387 ipdata = NULL; /* safety */
389 cluster = hammer2_cluster_lookup(cparent, &key_next,
390 pfs->name_key, pfs->name_key,
393 hammer2_inode_unlock_ex(ip->pmp->iroot, rcluster);
396 hammer2_cluster_type(cluster) != HAMMER2_BREF_TYPE_INODE) {
397 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
398 key_next, (hammer2_key_t)-1,
403 * Load the data being returned by the ioctl.
405 ipdata = &hammer2_cluster_data(cluster)->ipdata;
406 pfs->name_key = ipdata->name_key;
407 pfs->pfs_type = ipdata->pfs_type;
408 pfs->pfs_clid = ipdata->pfs_clid;
409 pfs->pfs_fsid = ipdata->pfs_fsid;
410 KKASSERT(ipdata->name_len < sizeof(pfs->name));
411 bcopy(ipdata->filename, pfs->name, ipdata->name_len);
412 pfs->name[ipdata->name_len] = 0;
413 ipdata = NULL; /* safety */
416 * Calculate the next field
419 cluster = hammer2_cluster_next(cparent, cluster,
421 0, (hammer2_key_t)-1,
424 hammer2_cluster_type(cluster) !=
425 HAMMER2_BREF_TYPE_INODE);
427 ipdata = &hammer2_cluster_data(cluster)->ipdata;
428 pfs->name_next = ipdata->name_key;
429 hammer2_cluster_unlock(cluster);
431 pfs->name_next = (hammer2_key_t)-1;
434 pfs->name_next = (hammer2_key_t)-1;
437 hammer2_inode_unlock_ex(hmp->spmp->iroot, cparent);
443 * Find a specific PFS by name
446 hammer2_ioctl_pfs_lookup(hammer2_inode_t *ip, void *data)
448 const hammer2_inode_data_t *ipdata;
449 hammer2_mount_t *hmp;
450 hammer2_ioc_pfs_t *pfs;
451 hammer2_cluster_t *cparent;
452 hammer2_cluster_t *cluster;
453 hammer2_key_t key_next;
460 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
462 cparent = hammer2_inode_lock_sh(hmp->spmp->iroot);
464 pfs->name[sizeof(pfs->name) - 1] = 0;
465 len = strlen(pfs->name);
466 lhc = hammer2_dirhash(pfs->name, len);
468 cluster = hammer2_cluster_lookup(cparent, &key_next,
469 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
470 HAMMER2_LOOKUP_SHARED, &ddflag);
472 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE) {
473 ipdata = &hammer2_cluster_data(cluster)->ipdata;
474 if (ipdata->name_len == len &&
475 bcmp(ipdata->filename, pfs->name, len) == 0) {
478 ipdata = NULL; /* safety */
480 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
482 lhc + HAMMER2_DIRHASH_LOMASK,
483 HAMMER2_LOOKUP_SHARED);
487 * Load the data being returned by the ioctl.
490 ipdata = &hammer2_cluster_data(cluster)->ipdata;
491 pfs->name_key = ipdata->name_key;
492 pfs->pfs_type = ipdata->pfs_type;
493 pfs->pfs_clid = ipdata->pfs_clid;
494 pfs->pfs_fsid = ipdata->pfs_fsid;
497 hammer2_cluster_unlock(cluster);
501 hammer2_inode_unlock_sh(hmp->spmp->iroot, cparent);
507 * Create a new PFS under the super-root
510 hammer2_ioctl_pfs_create(hammer2_inode_t *ip, void *data)
512 hammer2_inode_data_t *nipdata;
513 hammer2_mount_t *hmp;
514 hammer2_ioc_pfs_t *pfs;
515 hammer2_inode_t *nip;
516 hammer2_cluster_t *ncluster;
517 hammer2_trans_t trans;
520 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
524 if (pfs->name[0] == 0)
526 pfs->name[sizeof(pfs->name) - 1] = 0; /* ensure 0-termination */
528 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_NEWINODE);
529 nip = hammer2_inode_create(&trans, hmp->spmp->iroot, NULL, NULL,
530 pfs->name, strlen(pfs->name),
533 nipdata = hammer2_cluster_modify_ip(&trans, nip, ncluster,
534 HAMMER2_MODIFY_ASSERTNOCOPY);
535 nipdata->pfs_type = pfs->pfs_type;
536 nipdata->pfs_clid = pfs->pfs_clid;
537 nipdata->pfs_fsid = pfs->pfs_fsid;
540 * Do not allow compression on PFS's with the special name
541 * "boot", the boot loader can't decompress (yet).
543 if (strcmp(pfs->name, "boot") == 0)
544 nipdata->comp_algo = HAMMER2_COMP_AUTOZERO;
545 hammer2_cluster_modsync(ncluster);
546 hammer2_inode_unlock_ex(nip, ncluster);
548 hammer2_trans_done(&trans);
554 * Destroy an existing PFS under the super-root
557 hammer2_ioctl_pfs_delete(hammer2_inode_t *ip, void *data)
559 hammer2_mount_t *hmp;
560 hammer2_ioc_pfs_t *pfs = data;
561 hammer2_trans_t trans;
564 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
565 hammer2_trans_init(&trans, ip->pmp, 0);
566 error = hammer2_unlink_file(&trans, hmp->spmp->iroot,
567 pfs->name, strlen(pfs->name),
569 hammer2_trans_done(&trans);
575 hammer2_ioctl_pfs_snapshot(hammer2_inode_t *ip, void *data)
577 hammer2_ioc_pfs_t *pfs = data;
578 hammer2_trans_t trans;
579 hammer2_cluster_t *cparent;
582 if (pfs->name[0] == 0)
584 if (pfs->name[sizeof(pfs->name)-1] != 0)
587 hammer2_vfs_sync(ip->pmp->mp, MNT_WAIT);
589 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_NEWINODE);
590 cparent = hammer2_inode_lock_ex(ip);
591 error = hammer2_cluster_snapshot(&trans, cparent, pfs);
592 hammer2_inode_unlock_ex(ip, cparent);
593 hammer2_trans_done(&trans);
599 * Retrieve the raw inode structure
602 hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data)
604 const hammer2_inode_data_t *ipdata;
605 hammer2_ioc_inode_t *ino;
606 hammer2_cluster_t *cparent;
610 cparent = hammer2_inode_lock_sh(ip);
611 ipdata = &hammer2_cluster_data(cparent)->ipdata;
612 ino->ip_data = *ipdata;
614 hammer2_inode_unlock_sh(ip, cparent);
620 * Set various parameters in an inode which cannot be set through
621 * normal filesystem VNOPS.
624 hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data)
626 const hammer2_inode_data_t *ripdata;
627 hammer2_inode_data_t *wipdata;
628 hammer2_ioc_inode_t *ino = data;
629 hammer2_cluster_t *cparent;
630 hammer2_trans_t trans;
634 hammer2_trans_init(&trans, ip->pmp, 0);
635 cparent = hammer2_inode_lock_ex(ip);
636 ripdata = &hammer2_cluster_data(cparent)->ipdata;
638 if (ino->ip_data.comp_algo != ripdata->comp_algo) {
639 wipdata = hammer2_cluster_modify_ip(&trans, ip, cparent, 0);
640 wipdata->comp_algo = ino->ip_data.comp_algo;
641 ripdata = wipdata; /* safety */
646 /* Ignore these flags for now...*/
647 if (ino->flags & HAMMER2IOC_INODE_FLAG_IQUOTA) {
649 if (ino->flags & HAMMER2IOC_INODE_FLAG_DQUOTA) {
651 if (ino->flags & HAMMER2IOC_INODE_FLAG_COPIES) {
654 hammer2_cluster_modsync(cparent);
655 hammer2_trans_done(&trans);
656 hammer2_inode_unlock_ex(ip, cparent);
663 hammer2_ioctl_debug_dump(hammer2_inode_t *ip)
665 hammer2_chain_t *chain;
669 for (i = 0; i < ip->cluster.nchains; ++i) {
670 chain = ip->cluster.array[i];
673 hammer2_dump_chain(chain, 0, &count, 'i');