2 * Copyright (c) 2011-2013 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_delete(hammer2_inode_t *ip, void *data);
57 static int hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data);
58 static int hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data);
61 hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data, int fflag,
67 * Standard root cred checks, will be selectively ignored below
68 * for ioctls that do not require root creds.
70 error = priv_check_cred(cred, PRIV_HAMMER_IOCTL, 0);
73 case HAMMER2IOC_VERSION_GET:
74 error = hammer2_ioctl_version_get(ip, data);
76 case HAMMER2IOC_RECLUSTER:
78 error = hammer2_ioctl_recluster(ip, data);
80 case HAMMER2IOC_REMOTE_SCAN:
82 error = hammer2_ioctl_remote_scan(ip, data);
84 case HAMMER2IOC_REMOTE_ADD:
86 error = hammer2_ioctl_remote_add(ip, data);
88 case HAMMER2IOC_REMOTE_DEL:
90 error = hammer2_ioctl_remote_del(ip, data);
92 case HAMMER2IOC_REMOTE_REP:
94 error = hammer2_ioctl_remote_rep(ip, data);
96 case HAMMER2IOC_SOCKET_GET:
98 error = hammer2_ioctl_socket_get(ip, data);
100 case HAMMER2IOC_SOCKET_SET:
102 error = hammer2_ioctl_socket_set(ip, data);
104 case HAMMER2IOC_PFS_GET:
106 error = hammer2_ioctl_pfs_get(ip, data);
108 case HAMMER2IOC_PFS_LOOKUP:
110 error = hammer2_ioctl_pfs_lookup(ip, data);
112 case HAMMER2IOC_PFS_CREATE:
114 error = hammer2_ioctl_pfs_create(ip, data);
116 case HAMMER2IOC_PFS_DELETE:
118 error = hammer2_ioctl_pfs_delete(ip, data);
120 case HAMMER2IOC_INODE_GET:
121 error = hammer2_ioctl_inode_get(ip, data);
123 case HAMMER2IOC_INODE_SET:
125 error = hammer2_ioctl_inode_set(ip, data);
135 * Retrieve version and basic info
138 hammer2_ioctl_version_get(hammer2_inode_t *ip, void *data)
140 hammer2_mount_t *hmp = ip->hmp;
141 hammer2_ioc_version_t *version = data;
143 version->version = hmp->voldata.version;
148 hammer2_ioctl_recluster(hammer2_inode_t *ip, void *data)
150 hammer2_ioc_recluster_t *recl = data;
153 fp = holdfp(curproc->p_fd, recl->fd, -1);
155 kprintf("reconnect to cluster\n");
156 hammer2_cluster_reconnect(ip->pmp, fp);
164 * Retrieve information about a remote
167 hammer2_ioctl_remote_scan(hammer2_inode_t *ip, void *data)
169 hammer2_mount_t *hmp = ip->hmp;
170 hammer2_ioc_remote_t *remote = data;
171 int copyid = remote->copyid;
173 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
176 hammer2_voldata_lock(hmp);
177 remote->copy1 = hmp->voldata.copyinfo[copyid];
178 hammer2_voldata_unlock(hmp, 0);
181 * Adjust nextid (GET only)
183 while (++copyid < HAMMER2_COPYID_COUNT &&
184 hmp->voldata.copyinfo[copyid].copyid == 0) {
187 if (copyid == HAMMER2_COPYID_COUNT)
190 remote->nextid = copyid;
196 * Add new remote entry
199 hammer2_ioctl_remote_add(hammer2_inode_t *ip, void *data)
201 hammer2_mount_t *hmp = ip->hmp;
202 hammer2_pfsmount_t *pmp = ip->pmp;
203 hammer2_ioc_remote_t *remote = data;
204 int copyid = remote->copyid;
207 if (copyid >= HAMMER2_COPYID_COUNT)
210 hammer2_voldata_lock(hmp);
212 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
213 if (hmp->voldata.copyinfo[copyid].copyid == 0)
216 if (copyid == HAMMER2_COPYID_COUNT) {
221 hammer2_modify_volume(hmp);
222 remote->copy1.copyid = copyid;
223 hmp->voldata.copyinfo[copyid] = remote->copy1;
224 hammer2_volconf_update(pmp, copyid);
226 hammer2_voldata_unlock(hmp, 1);
231 * Delete existing remote entry
234 hammer2_ioctl_remote_del(hammer2_inode_t *ip, void *data)
236 hammer2_mount_t *hmp = ip->hmp;
237 hammer2_pfsmount_t *pmp = ip->pmp;
238 hammer2_ioc_remote_t *remote = data;
239 int copyid = remote->copyid;
242 if (copyid >= HAMMER2_COPYID_COUNT)
244 remote->copy1.path[sizeof(remote->copy1.path) - 1] = 0;
245 hammer2_voldata_lock(hmp);
247 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
248 if (hmp->voldata.copyinfo[copyid].copyid == 0)
250 if (strcmp(remote->copy1.path,
251 hmp->voldata.copyinfo[copyid].path) == 0) {
255 if (copyid == HAMMER2_COPYID_COUNT) {
260 hammer2_modify_volume(hmp);
261 hmp->voldata.copyinfo[copyid].copyid = 0;
262 hammer2_volconf_update(pmp, copyid);
264 hammer2_voldata_unlock(hmp, 1);
269 * Replace existing remote entry
272 hammer2_ioctl_remote_rep(hammer2_inode_t *ip, void *data)
274 hammer2_mount_t *hmp = ip->hmp;
275 hammer2_ioc_remote_t *remote = data;
276 int copyid = remote->copyid;
278 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
281 hammer2_voldata_lock(hmp);
282 /*hammer2_volconf_update(pmp, copyid);*/
283 hammer2_voldata_unlock(hmp, 1);
289 * Retrieve communications socket
292 hammer2_ioctl_socket_get(hammer2_inode_t *ip, void *data)
298 * Set communications socket for connection
301 hammer2_ioctl_socket_set(hammer2_inode_t *ip, void *data)
303 hammer2_mount_t *hmp = ip->hmp;
304 hammer2_ioc_remote_t *remote = data;
305 int copyid = remote->copyid;
307 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
310 hammer2_voldata_lock(hmp);
311 hammer2_voldata_unlock(hmp, 0);
317 * Used to scan PFSs, which are directories under the super-root.
320 hammer2_ioctl_pfs_get(hammer2_inode_t *ip, void *data)
322 hammer2_inode_data_t *ipdata;
323 hammer2_mount_t *hmp;
324 hammer2_ioc_pfs_t *pfs;
325 hammer2_chain_t *parent;
326 hammer2_chain_t *chain;
332 parent = hammer2_chain_lookup_init(hmp->schain, 0);
335 * Search for the first key or specific key. Remember that keys
336 * can be returned in any order.
338 if (pfs->name_key == 0) {
339 chain = hammer2_chain_lookup(&parent,
340 0, (hammer2_key_t)-1, 0);
342 chain = hammer2_chain_lookup(&parent,
343 pfs->name_key, pfs->name_key, 0);
345 while (chain && chain->bref.type != HAMMER2_BREF_TYPE_INODE) {
346 chain = hammer2_chain_next(&parent, chain,
347 0, (hammer2_key_t)-1, 0);
351 * Load the data being returned by the ioctl.
353 ipdata = &chain->data->ipdata;
354 pfs->name_key = ipdata->name_key;
355 pfs->pfs_type = ipdata->pfs_type;
356 pfs->pfs_clid = ipdata->pfs_clid;
357 pfs->pfs_fsid = ipdata->pfs_fsid;
358 KKASSERT(ipdata->name_len < sizeof(pfs->name));
359 bcopy(ipdata->filename, pfs->name, ipdata->name_len);
360 pfs->name[ipdata->name_len] = 0;
361 ipdata = NULL; /* safety */
364 * Calculate the next field
367 chain = hammer2_chain_next(&parent, chain,
368 0, (hammer2_key_t)-1, 0);
369 } while (chain && chain->bref.type != HAMMER2_BREF_TYPE_INODE);
371 pfs->name_next = chain->data->ipdata.name_key;
372 hammer2_chain_unlock(chain);
374 pfs->name_next = (hammer2_key_t)-1;
377 pfs->name_next = (hammer2_key_t)-1;
380 hammer2_chain_lookup_done(parent);
386 * Find a specific PFS by name
389 hammer2_ioctl_pfs_lookup(hammer2_inode_t *ip, void *data)
391 hammer2_inode_data_t *ipdata;
392 hammer2_mount_t *hmp;
393 hammer2_ioc_pfs_t *pfs;
394 hammer2_chain_t *parent;
395 hammer2_chain_t *chain;
403 parent = hammer2_chain_lookup_init(hmp->schain, HAMMER2_LOOKUP_SHARED);
405 pfs->name[sizeof(pfs->name) - 1] = 0;
406 len = strlen(pfs->name);
407 lhc = hammer2_dirhash(pfs->name, len);
409 chain = hammer2_chain_lookup(&parent,
410 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
411 HAMMER2_LOOKUP_SHARED);
413 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
414 len == chain->data->ipdata.name_len &&
415 bcmp(pfs->name, chain->data->ipdata.filename, len) == 0) {
418 chain = hammer2_chain_next(&parent, chain,
419 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
420 HAMMER2_LOOKUP_SHARED);
424 * Load the data being returned by the ioctl.
427 ipdata = &chain->data->ipdata;
428 pfs->name_key = ipdata->name_key;
429 pfs->pfs_type = ipdata->pfs_type;
430 pfs->pfs_clid = ipdata->pfs_clid;
431 pfs->pfs_fsid = ipdata->pfs_fsid;
434 hammer2_chain_unlock(chain);
438 hammer2_chain_lookup_done(parent);
443 * Create a new PFS under the super-root
446 hammer2_ioctl_pfs_create(hammer2_inode_t *ip, void *data)
448 hammer2_inode_data_t *nipdata;
449 hammer2_mount_t *hmp;
450 hammer2_ioc_pfs_t *pfs;
451 hammer2_inode_t *nip;
452 hammer2_trans_t trans;
459 pfs->name[sizeof(pfs->name) - 1] = 0; /* ensure 0-termination */
461 hammer2_trans_init(hmp, &trans);
462 nip = hammer2_inode_create(&trans, hmp->sroot, NULL, NULL,
463 pfs->name, strlen(pfs->name),
466 nipdata = hammer2_chain_modify_ip(&trans, nip, 0);
467 nipdata->pfs_type = pfs->pfs_type;
468 nipdata->pfs_clid = pfs->pfs_clid;
469 nipdata->pfs_fsid = pfs->pfs_fsid;
470 hammer2_inode_unlock_ex(nip);
472 hammer2_trans_done(&trans);
477 * Destroy an existing PFS under the super-root
480 hammer2_ioctl_pfs_delete(hammer2_inode_t *ip, void *data)
482 hammer2_mount_t *hmp = ip->hmp;
483 hammer2_ioc_pfs_t *pfs = data;
484 hammer2_trans_t trans;
487 hammer2_trans_init(hmp, &trans);
488 error = hammer2_unlink_file(&trans, hmp->sroot,
489 pfs->name, strlen(pfs->name),
491 hammer2_trans_done(&trans);
497 * Retrieve the raw inode structure
500 hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data)
502 hammer2_ioc_inode_t *ino = data;
504 hammer2_inode_lock_sh(ip);
505 ino->ip_data = ip->chain->data->ipdata;
507 hammer2_inode_unlock_sh(ip);
513 hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data)
515 hammer2_ioc_inode_t *ino = data;
518 hammer2_inode_lock_ex(ip);
519 if (ino->flags & HAMMER2IOC_INODE_FLAG_IQUOTA) {
521 if (ino->flags & HAMMER2IOC_INODE_FLAG_DQUOTA) {
523 if (ino->flags & HAMMER2IOC_INODE_FLAG_COPIES) {
525 hammer2_inode_unlock_ex(ip);