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_version_get(hammer2_inode_t *ip, void *data);
46 static int hammer2_ioctl_remote_get(hammer2_inode_t *ip, void *data);
47 static int hammer2_ioctl_remote_add(hammer2_inode_t *ip, void *data);
48 static int hammer2_ioctl_remote_del(hammer2_inode_t *ip, void *data);
49 static int hammer2_ioctl_remote_rep(hammer2_inode_t *ip, void *data);
50 static int hammer2_ioctl_socket_get(hammer2_inode_t *ip, void *data);
51 static int hammer2_ioctl_socket_set(hammer2_inode_t *ip, void *data);
52 static int hammer2_ioctl_pfs_get(hammer2_inode_t *ip, void *data);
53 static int hammer2_ioctl_pfs_lookup(hammer2_inode_t *ip, void *data);
54 static int hammer2_ioctl_pfs_create(hammer2_inode_t *ip, void *data);
55 static int hammer2_ioctl_pfs_delete(hammer2_inode_t *ip, void *data);
56 static int hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data);
57 static int hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data);
60 hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data, int fflag,
66 * Standard root cred checks, will be selectively ignored below
67 * for ioctls that do not require root creds.
69 error = priv_check_cred(cred, PRIV_HAMMER_IOCTL, 0);
72 case HAMMER2IOC_VERSION_GET:
73 error = hammer2_ioctl_version_get(ip, data);
75 case HAMMER2IOC_REMOTE_GET:
77 error = hammer2_ioctl_remote_get(ip, data);
79 case HAMMER2IOC_REMOTE_ADD:
81 error = hammer2_ioctl_remote_add(ip, data);
83 case HAMMER2IOC_REMOTE_DEL:
85 error = hammer2_ioctl_remote_del(ip, data);
87 case HAMMER2IOC_REMOTE_REP:
89 error = hammer2_ioctl_remote_rep(ip, data);
91 case HAMMER2IOC_SOCKET_GET:
93 error = hammer2_ioctl_socket_get(ip, data);
95 case HAMMER2IOC_SOCKET_SET:
97 error = hammer2_ioctl_socket_set(ip, data);
99 case HAMMER2IOC_PFS_GET:
101 error = hammer2_ioctl_pfs_get(ip, data);
103 case HAMMER2IOC_PFS_LOOKUP:
105 error = hammer2_ioctl_pfs_lookup(ip, data);
107 case HAMMER2IOC_PFS_CREATE:
109 error = hammer2_ioctl_pfs_create(ip, data);
111 case HAMMER2IOC_PFS_DELETE:
113 error = hammer2_ioctl_pfs_delete(ip, data);
115 case HAMMER2IOC_INODE_GET:
116 error = hammer2_ioctl_inode_get(ip, data);
118 case HAMMER2IOC_INODE_SET:
120 error = hammer2_ioctl_inode_set(ip, data);
130 * Retrieve version and basic info
133 hammer2_ioctl_version_get(hammer2_inode_t *ip, void *data)
135 hammer2_mount_t *hmp = ip->hmp;
136 hammer2_ioc_version_t *version = data;
138 version->version = hmp->voldata.version;
143 * Retrieve information about a remote
146 hammer2_ioctl_remote_get(hammer2_inode_t *ip, void *data)
148 hammer2_mount_t *hmp = ip->hmp;
149 hammer2_ioc_remote_t *remote = data;
150 int copyid = remote->copyid;
152 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
155 hammer2_voldata_lock(hmp);
156 remote->copy1 = hmp->voldata.copyinfo[copyid];
157 hammer2_voldata_unlock(hmp);
160 * Adjust nextid (GET only)
162 while (++copyid < HAMMER2_COPYID_COUNT &&
163 hmp->voldata.copyinfo[copyid].copyid == 0) {
166 if (copyid == HAMMER2_COPYID_COUNT)
169 remote->nextid = copyid;
175 * Add new remote entry
178 hammer2_ioctl_remote_add(hammer2_inode_t *ip, void *data)
180 hammer2_mount_t *hmp = ip->hmp;
181 hammer2_ioc_remote_t *remote = data;
182 int copyid = remote->copyid;
185 if (copyid >= HAMMER2_COPYID_COUNT)
188 hammer2_voldata_lock(hmp);
190 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
191 if (hmp->voldata.copyinfo[copyid].copyid == 0)
194 if (copyid == HAMMER2_COPYID_COUNT) {
199 hammer2_modify_volume(hmp);
200 kprintf("copyid %d\n", copyid);
201 remote->copy1.copyid = copyid;
202 hmp->voldata.copyinfo[copyid] = remote->copy1;
204 hammer2_voldata_unlock(hmp);
209 * Delete existing remote entry
212 hammer2_ioctl_remote_del(hammer2_inode_t *ip, void *data)
214 hammer2_mount_t *hmp = ip->hmp;
215 hammer2_ioc_remote_t *remote = data;
216 int copyid = remote->copyid;
219 if (copyid >= HAMMER2_COPYID_COUNT)
221 remote->copy1.path[sizeof(remote->copy1.path) - 1] = 0;
222 hammer2_voldata_lock(hmp);
224 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
225 if (hmp->voldata.copyinfo[copyid].copyid == 0)
227 if (strcmp(remote->copy1.path,
228 hmp->voldata.copyinfo[copyid].path) == 0) {
232 if (copyid == HAMMER2_COPYID_COUNT) {
237 hammer2_modify_volume(hmp);
238 hmp->voldata.copyinfo[copyid].copyid = 0;
240 hammer2_voldata_unlock(hmp);
245 * Replace existing remote entry
248 hammer2_ioctl_remote_rep(hammer2_inode_t *ip, void *data)
250 hammer2_mount_t *hmp = ip->hmp;
251 hammer2_ioc_remote_t *remote = data;
252 int copyid = remote->copyid;
254 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
257 hammer2_voldata_lock(hmp);
258 hammer2_voldata_unlock(hmp);
264 * Retrieve communications socket
267 hammer2_ioctl_socket_get(hammer2_inode_t *ip, void *data)
273 * Set communications socket for connection
276 hammer2_ioctl_socket_set(hammer2_inode_t *ip, void *data)
278 hammer2_mount_t *hmp = ip->hmp;
279 hammer2_ioc_remote_t *remote = data;
280 int copyid = remote->copyid;
282 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
285 hammer2_voldata_lock(hmp);
286 hammer2_voldata_unlock(hmp);
292 * Used to scan PFSs, which are directories under the super-root.
295 hammer2_ioctl_pfs_get(hammer2_inode_t *ip, void *data)
297 hammer2_mount_t *hmp = ip->hmp;
298 hammer2_ioc_pfs_t *pfs = data;
299 hammer2_chain_t *parent;
300 hammer2_chain_t *chain;
301 hammer2_inode_t *xip;
304 parent = hmp->schain;
305 error = hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
310 * Search for the first key or specific key. Remember that keys
311 * can be returned in any order.
313 if (pfs->name_key == 0) {
314 chain = hammer2_chain_lookup(hmp, &parent,
315 0, (hammer2_key_t)-1, 0);
317 chain = hammer2_chain_lookup(hmp, &parent,
318 pfs->name_key, pfs->name_key, 0);
320 while (chain && chain->bref.type != HAMMER2_BREF_TYPE_INODE) {
321 chain = hammer2_chain_next(hmp, &parent, chain,
322 0, (hammer2_key_t)-1, 0);
326 * Load the data being returned by the ioctl.
329 pfs->name_key = xip->ip_data.name_key;
330 pfs->pfs_type = xip->ip_data.pfs_type;
331 pfs->pfs_clid = xip->ip_data.pfs_clid;
332 pfs->pfs_fsid = xip->ip_data.pfs_fsid;
333 KKASSERT(xip->ip_data.name_len < sizeof(pfs->name));
334 bcopy(xip->ip_data.filename, pfs->name,
335 xip->ip_data.name_len);
336 pfs->name[xip->ip_data.name_len] = 0;
339 * Calculate the next field
342 chain = hammer2_chain_next(hmp, &parent, chain,
343 0, (hammer2_key_t)-1, 0);
344 } while (chain && chain->bref.type != HAMMER2_BREF_TYPE_INODE);
346 pfs->name_next = chain->u.ip->ip_data.name_key;
347 hammer2_chain_unlock(hmp, chain);
349 pfs->name_next = (hammer2_key_t)-1;
352 pfs->name_next = (hammer2_key_t)-1;
356 hammer2_chain_unlock(hmp, parent);
361 * Find a specific PFS by name
364 hammer2_ioctl_pfs_lookup(hammer2_inode_t *ip, void *data)
366 hammer2_mount_t *hmp = ip->hmp;
367 hammer2_ioc_pfs_t *pfs = data;
368 hammer2_chain_t *parent;
369 hammer2_chain_t *chain;
370 hammer2_inode_t *xip;
375 parent = hmp->schain;
376 error = hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS |
377 HAMMER2_RESOLVE_SHARED);
381 pfs->name[sizeof(pfs->name) - 1] = 0;
382 len = strlen(pfs->name);
383 lhc = hammer2_dirhash(pfs->name, len);
385 chain = hammer2_chain_lookup(hmp, &parent,
386 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
387 HAMMER2_LOOKUP_SHARED);
389 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
391 len == chain->data->ipdata.name_len &&
392 bcmp(pfs->name, chain->data->ipdata.filename, len) == 0) {
395 chain = hammer2_chain_next(hmp, &parent, chain,
396 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
397 HAMMER2_LOOKUP_SHARED);
401 * Load the data being returned by the ioctl.
405 pfs->name_key = xip->ip_data.name_key;
406 pfs->pfs_type = xip->ip_data.pfs_type;
407 pfs->pfs_clid = xip->ip_data.pfs_clid;
408 pfs->pfs_fsid = xip->ip_data.pfs_fsid;
410 hammer2_chain_unlock(hmp, chain);
415 hammer2_chain_unlock(hmp, parent);
420 * Create a new PFS under the super-root
423 hammer2_ioctl_pfs_create(hammer2_inode_t *ip, void *data)
425 hammer2_mount_t *hmp = ip->hmp;
426 hammer2_ioc_pfs_t *pfs = data;
427 hammer2_inode_t *nip = NULL;
430 pfs->name[sizeof(pfs->name) - 1] = 0; /* ensure 0-termination */
431 error = hammer2_inode_create(hmp->schain->u.ip, NULL, NULL,
432 pfs->name, strlen(pfs->name),
435 hammer2_chain_modify(hmp, &nip->chain, 0);
436 nip->ip_data.pfs_type = pfs->pfs_type;
437 nip->ip_data.pfs_clid = pfs->pfs_clid;
438 nip->ip_data.pfs_fsid = pfs->pfs_fsid;
439 hammer2_chain_unlock(hmp, &nip->chain);
445 * Destroy an existing PFS under the super-root
448 hammer2_ioctl_pfs_delete(hammer2_inode_t *ip, void *data)
450 hammer2_mount_t *hmp = ip->hmp;
451 hammer2_ioc_pfs_t *pfs = data;
454 error = hammer2_unlink_file(hmp->schain->u.ip,
455 pfs->name, strlen(pfs->name),
461 * Retrieve the raw inode structure
464 hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data)
466 hammer2_ioc_inode_t *ino = data;
468 hammer2_inode_lock_sh(ip);
469 ino->ip_data = ip->ip_data;
471 hammer2_inode_unlock_sh(ip);
476 hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data)
478 hammer2_ioc_inode_t *ino = data;
481 hammer2_inode_lock_ex(ip);
482 if (ino->flags & HAMMER2IOC_INODE_FLAG_IQUOTA) {
484 if (ino->flags & HAMMER2IOC_INODE_FLAG_DQUOTA) {
486 if (ino->flags & HAMMER2IOC_INODE_FLAG_COPIES) {
488 hammer2_inode_unlock_ex(ip);