2 * Copyright (c) 2011-2012 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_get_version(hammer2_inode_t *ip, void *data);
46 static int hammer2_ioctl_get_remote(hammer2_inode_t *ip, void *data);
47 static int hammer2_ioctl_add_remote(hammer2_inode_t *ip, void *data);
48 static int hammer2_ioctl_del_remote(hammer2_inode_t *ip, void *data);
49 static int hammer2_ioctl_rep_remote(hammer2_inode_t *ip, void *data);
50 static int hammer2_ioctl_get_socket(hammer2_inode_t *ip, void *data);
51 static int hammer2_ioctl_set_socket(hammer2_inode_t *ip, void *data);
54 hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data, int fflag,
60 * Standard root cred checks, will be selectively ignored below
61 * for ioctls that do not require root creds.
63 error = priv_check_cred(cred, PRIV_HAMMER_IOCTL, 0);
66 case HAMMER2IOC_GET_VERSION:
68 * Retrieve version and basic status
70 error = hammer2_ioctl_get_version(ip, data);
72 case HAMMER2IOC_GET_REMOTE:
74 * Retrieve information about a remote
77 error = hammer2_ioctl_get_remote(ip, data);
79 case HAMMER2IOC_ADD_REMOTE:
81 * Add new remote entry.
84 error = hammer2_ioctl_add_remote(ip, data);
86 case HAMMER2IOC_DEL_REMOTE:
88 * Delete existing remote entry
91 error = hammer2_ioctl_del_remote(ip, data);
93 case HAMMER2IOC_REP_REMOTE:
95 * Replace existing remote entry
98 error = hammer2_ioctl_rep_remote(ip, data);
100 case HAMMER2IOC_GET_SOCKET:
102 * Retrieve communications socket
105 error = hammer2_ioctl_get_socket(ip, data);
107 case HAMMER2IOC_SET_SOCKET:
109 * Set communications socket for connection
112 error = hammer2_ioctl_set_socket(ip, data);
122 * Retrieve version and basic info
125 hammer2_ioctl_get_version(hammer2_inode_t *ip, void *data)
127 hammer2_mount_t *hmp = ip->hmp;
128 hammer2_ioc_version_t *version = data;
130 version->version = hmp->voldata.version;
135 * Retrieve information about a remote
138 hammer2_ioctl_get_remote(hammer2_inode_t *ip, void *data)
140 hammer2_mount_t *hmp = ip->hmp;
141 hammer2_ioc_remote_t *remote = data;
142 int copyid = remote->copyid;
144 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
147 hammer2_voldata_lock(hmp);
148 remote->copy1 = hmp->voldata.copyinfo[copyid];
149 hammer2_voldata_unlock(hmp);
152 * Adjust nextid (GET only)
154 while (++copyid < HAMMER2_COPYID_COUNT &&
155 hmp->voldata.copyinfo[copyid].copyid == 0) {
158 if (copyid == HAMMER2_COPYID_COUNT)
161 remote->nextid = copyid;
167 * Add new remote entry
170 hammer2_ioctl_add_remote(hammer2_inode_t *ip, void *data)
172 hammer2_mount_t *hmp = ip->hmp;
173 hammer2_ioc_remote_t *remote = data;
174 int copyid = remote->copyid;
177 if (copyid >= HAMMER2_COPYID_COUNT)
180 hammer2_voldata_lock(hmp);
182 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
183 if (hmp->voldata.copyinfo[copyid].copyid == 0)
186 if (copyid == HAMMER2_COPYID_COUNT) {
191 hammer2_modify_volume(hmp);
192 kprintf("copyid %d\n", copyid);
193 remote->copy1.copyid = copyid;
194 hmp->voldata.copyinfo[copyid] = remote->copy1;
196 hammer2_voldata_unlock(hmp);
201 * Delete existing remote entry
204 hammer2_ioctl_del_remote(hammer2_inode_t *ip, void *data)
206 hammer2_mount_t *hmp = ip->hmp;
207 hammer2_ioc_remote_t *remote = data;
208 int copyid = remote->copyid;
211 if (copyid >= HAMMER2_COPYID_COUNT)
213 remote->copy1.path[sizeof(remote->copy1.path) - 1] = 0;
214 hammer2_voldata_lock(hmp);
216 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
217 if (hmp->voldata.copyinfo[copyid].copyid == 0)
219 if (strcmp(remote->copy1.path,
220 hmp->voldata.copyinfo[copyid].path) == 0) {
224 if (copyid == HAMMER2_COPYID_COUNT) {
229 hammer2_modify_volume(hmp);
230 hmp->voldata.copyinfo[copyid].copyid = 0;
232 hammer2_voldata_unlock(hmp);
237 * Replace existing remote entry
240 hammer2_ioctl_rep_remote(hammer2_inode_t *ip, void *data)
242 hammer2_mount_t *hmp = ip->hmp;
243 hammer2_ioc_remote_t *remote = data;
244 int copyid = remote->copyid;
246 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
249 hammer2_voldata_lock(hmp);
250 hammer2_voldata_unlock(hmp);
256 * Retrieve communications socket
259 hammer2_ioctl_get_socket(hammer2_inode_t *ip, void *data)
265 * Set communications socket for connection
268 hammer2_ioctl_set_socket(hammer2_inode_t *ip, void *data)
270 hammer2_mount_t *hmp = ip->hmp;
271 hammer2_ioc_remote_t *remote = data;
272 int copyid = remote->copyid;
274 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
277 hammer2_voldata_lock(hmp);
278 hammer2_voldata_unlock(hmp);