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_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_inode_comp_set(hammer2_inode_t *ip, void *data);
61 //static int hammer2_ioctl_inode_comp_rec_set(hammer2_inode_t *ip, void *data);
62 //static int hammer2_ioctl_inode_comp_rec_set2(hammer2_inode_t *ip, void *data);
65 hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data, int fflag,
71 * Standard root cred checks, will be selectively ignored below
72 * for ioctls that do not require root creds.
74 error = priv_check_cred(cred, PRIV_HAMMER_IOCTL, 0);
77 case HAMMER2IOC_VERSION_GET:
78 error = hammer2_ioctl_version_get(ip, data);
80 case HAMMER2IOC_RECLUSTER:
82 error = hammer2_ioctl_recluster(ip, data);
84 case HAMMER2IOC_REMOTE_SCAN:
86 error = hammer2_ioctl_remote_scan(ip, data);
88 case HAMMER2IOC_REMOTE_ADD:
90 error = hammer2_ioctl_remote_add(ip, data);
92 case HAMMER2IOC_REMOTE_DEL:
94 error = hammer2_ioctl_remote_del(ip, data);
96 case HAMMER2IOC_REMOTE_REP:
98 error = hammer2_ioctl_remote_rep(ip, data);
100 case HAMMER2IOC_SOCKET_GET:
102 error = hammer2_ioctl_socket_get(ip, data);
104 case HAMMER2IOC_SOCKET_SET:
106 error = hammer2_ioctl_socket_set(ip, data);
108 case HAMMER2IOC_PFS_GET:
110 error = hammer2_ioctl_pfs_get(ip, data);
112 case HAMMER2IOC_PFS_LOOKUP:
114 error = hammer2_ioctl_pfs_lookup(ip, data);
116 case HAMMER2IOC_PFS_CREATE:
118 error = hammer2_ioctl_pfs_create(ip, data);
120 case HAMMER2IOC_PFS_DELETE:
122 error = hammer2_ioctl_pfs_delete(ip, data);
124 case HAMMER2IOC_PFS_SNAPSHOT:
126 error = hammer2_ioctl_pfs_snapshot(ip, data);
128 case HAMMER2IOC_INODE_GET:
129 error = hammer2_ioctl_inode_get(ip, data);
131 case HAMMER2IOC_INODE_SET:
133 error = hammer2_ioctl_inode_set(ip, data);
135 /*case HAMMER2IOC_INODE_COMP_SET:
136 error = hammer2_ioctl_inode_comp_set(ip, data);
138 case HAMMER2IOC_INODE_COMP_REC_SET:
139 error = hammer2_ioctl_inode_comp_rec_set(ip, data);
141 case HAMMER2IOC_INODE_COMP_REC_SET2:
142 error = hammer2_ioctl_inode_comp_rec_set2(ip, data);
152 * Retrieve version and basic info
155 hammer2_ioctl_version_get(hammer2_inode_t *ip, void *data)
157 hammer2_mount_t *hmp = ip->pmp->cluster.chains[0]->hmp;
158 hammer2_ioc_version_t *version = data;
160 version->version = hmp->voldata.version;
165 hammer2_ioctl_recluster(hammer2_inode_t *ip, void *data)
167 hammer2_ioc_recluster_t *recl = data;
170 fp = holdfp(curproc->p_fd, recl->fd, -1);
172 kprintf("reconnect to cluster\n");
173 hammer2_cluster_reconnect(ip->pmp, fp);
181 * Retrieve information about a remote
184 hammer2_ioctl_remote_scan(hammer2_inode_t *ip, void *data)
186 hammer2_mount_t *hmp = ip->pmp->cluster.chains[0]->hmp;
187 hammer2_ioc_remote_t *remote = data;
188 int copyid = remote->copyid;
190 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
193 hammer2_voldata_lock(hmp);
194 remote->copy1 = hmp->voldata.copyinfo[copyid];
195 hammer2_voldata_unlock(hmp, 0);
198 * Adjust nextid (GET only)
200 while (++copyid < HAMMER2_COPYID_COUNT &&
201 hmp->voldata.copyinfo[copyid].copyid == 0) {
204 if (copyid == HAMMER2_COPYID_COUNT)
207 remote->nextid = copyid;
213 * Add new remote entry
216 hammer2_ioctl_remote_add(hammer2_inode_t *ip, void *data)
218 hammer2_ioc_remote_t *remote = data;
219 hammer2_pfsmount_t *pmp = ip->pmp;
220 hammer2_mount_t *hmp;
221 int copyid = remote->copyid;
224 if (copyid >= HAMMER2_COPYID_COUNT)
227 hmp = pmp->cluster.chains[0]->hmp; /* XXX */
228 hammer2_voldata_lock(hmp);
230 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
231 if (hmp->voldata.copyinfo[copyid].copyid == 0)
234 if (copyid == HAMMER2_COPYID_COUNT) {
239 hammer2_modify_volume(hmp);
240 remote->copy1.copyid = copyid;
241 hmp->voldata.copyinfo[copyid] = remote->copy1;
242 hammer2_volconf_update(pmp, copyid);
244 hammer2_voldata_unlock(hmp, 1);
249 * Delete existing remote entry
252 hammer2_ioctl_remote_del(hammer2_inode_t *ip, void *data)
254 hammer2_ioc_remote_t *remote = data;
255 hammer2_pfsmount_t *pmp = ip->pmp;
256 hammer2_mount_t *hmp;
257 int copyid = remote->copyid;
260 hmp = pmp->cluster.chains[0]->hmp; /* XXX */
261 if (copyid >= HAMMER2_COPYID_COUNT)
263 remote->copy1.path[sizeof(remote->copy1.path) - 1] = 0;
264 hammer2_voldata_lock(hmp);
266 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
267 if (hmp->voldata.copyinfo[copyid].copyid == 0)
269 if (strcmp(remote->copy1.path,
270 hmp->voldata.copyinfo[copyid].path) == 0) {
274 if (copyid == HAMMER2_COPYID_COUNT) {
279 hammer2_modify_volume(hmp);
280 hmp->voldata.copyinfo[copyid].copyid = 0;
281 hammer2_volconf_update(pmp, copyid);
283 hammer2_voldata_unlock(hmp, 1);
288 * Replace existing remote entry
291 hammer2_ioctl_remote_rep(hammer2_inode_t *ip, void *data)
293 hammer2_ioc_remote_t *remote = data;
294 hammer2_mount_t *hmp;
295 int copyid = remote->copyid;
297 hmp = ip->pmp->cluster.chains[0]->hmp; /* XXX */
299 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
302 hammer2_voldata_lock(hmp);
303 /*hammer2_volconf_update(pmp, copyid);*/
304 hammer2_voldata_unlock(hmp, 1);
310 * Retrieve communications socket
313 hammer2_ioctl_socket_get(hammer2_inode_t *ip, void *data)
319 * Set communications socket for connection
322 hammer2_ioctl_socket_set(hammer2_inode_t *ip, void *data)
324 hammer2_ioc_remote_t *remote = data;
325 hammer2_mount_t *hmp;
326 int copyid = remote->copyid;
328 hmp = ip->pmp->cluster.chains[0]->hmp; /* XXX */
329 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
332 hammer2_voldata_lock(hmp);
333 hammer2_voldata_unlock(hmp, 0);
339 * Used to scan and retrieve PFS information. PFS's are directories under
342 * To scan PFSs pass name_key=0. The function will scan for the next
343 * PFS and set all fields, as well as set name_next to the next key.
344 * When no PFSs remain, name_next is set to (hammer2_key_t)-1.
346 * To retrieve the PFS associated with the file descriptor, pass
347 * name_key set to (hammer2_key_t)-1.
350 hammer2_ioctl_pfs_get(hammer2_inode_t *ip, void *data)
352 hammer2_inode_data_t *ipdata;
353 hammer2_mount_t *hmp;
354 hammer2_ioc_pfs_t *pfs;
355 hammer2_chain_t *parent;
356 hammer2_chain_t *chain;
357 hammer2_chain_t *rchain;
358 hammer2_key_t key_next;
360 int cache_index = -1;
363 hmp = ip->pmp->cluster.chains[0]->hmp; /* XXX */
365 parent = hammer2_inode_lock_ex(hmp->sroot);
366 rchain = hammer2_inode_lock_ex(ip->pmp->iroot);
369 * Search for the first key or specific key. Remember that keys
370 * can be returned in any order.
372 if (pfs->name_key == 0) {
373 chain = hammer2_chain_lookup(&parent, &key_next,
374 0, (hammer2_key_t)-1,
376 } else if (pfs->name_key == (hammer2_key_t)-1) {
377 chain = hammer2_chain_lookup(&parent, &key_next,
378 rchain->data->ipdata.name_key,
379 rchain->data->ipdata.name_key,
382 chain = hammer2_chain_lookup(&parent, &key_next,
383 pfs->name_key, pfs->name_key,
386 hammer2_inode_unlock_ex(ip->pmp->iroot, rchain);
388 while (chain && chain->bref.type != HAMMER2_BREF_TYPE_INODE) {
389 chain = hammer2_chain_next(&parent, chain, &key_next,
390 key_next, (hammer2_key_t)-1,
395 * Load the data being returned by the ioctl.
397 ipdata = &chain->data->ipdata;
398 pfs->name_key = ipdata->name_key;
399 pfs->pfs_type = ipdata->pfs_type;
400 pfs->pfs_clid = ipdata->pfs_clid;
401 pfs->pfs_fsid = ipdata->pfs_fsid;
402 KKASSERT(ipdata->name_len < sizeof(pfs->name));
403 bcopy(ipdata->filename, pfs->name, ipdata->name_len);
404 pfs->name[ipdata->name_len] = 0;
405 ipdata = NULL; /* safety */
408 * Calculate the next field
411 chain = hammer2_chain_next(&parent, chain, &key_next,
412 0, (hammer2_key_t)-1,
414 } while (chain && chain->bref.type != HAMMER2_BREF_TYPE_INODE);
416 pfs->name_next = chain->data->ipdata.name_key;
417 hammer2_chain_unlock(chain);
419 pfs->name_next = (hammer2_key_t)-1;
422 pfs->name_next = (hammer2_key_t)-1;
425 hammer2_inode_unlock_ex(hmp->sroot, parent);
431 * Find a specific PFS by name
434 hammer2_ioctl_pfs_lookup(hammer2_inode_t *ip, void *data)
436 hammer2_inode_data_t *ipdata;
437 hammer2_mount_t *hmp;
438 hammer2_ioc_pfs_t *pfs;
439 hammer2_chain_t *parent;
440 hammer2_chain_t *chain;
441 hammer2_key_t key_next;
444 int cache_index = -1;
448 hmp = ip->pmp->cluster.chains[0]->hmp; /* XXX */
450 parent = hammer2_inode_lock_sh(hmp->sroot);
452 pfs->name[sizeof(pfs->name) - 1] = 0;
453 len = strlen(pfs->name);
454 lhc = hammer2_dirhash(pfs->name, len);
456 chain = hammer2_chain_lookup(&parent, &key_next,
457 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
458 &cache_index, HAMMER2_LOOKUP_SHARED);
460 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
461 len == chain->data->ipdata.name_len &&
462 bcmp(pfs->name, chain->data->ipdata.filename, len) == 0) {
465 chain = hammer2_chain_next(&parent, chain, &key_next,
467 lhc + HAMMER2_DIRHASH_LOMASK,
468 &cache_index, HAMMER2_LOOKUP_SHARED);
472 * Load the data being returned by the ioctl.
475 ipdata = &chain->data->ipdata;
476 pfs->name_key = ipdata->name_key;
477 pfs->pfs_type = ipdata->pfs_type;
478 pfs->pfs_clid = ipdata->pfs_clid;
479 pfs->pfs_fsid = ipdata->pfs_fsid;
482 hammer2_chain_unlock(chain);
486 hammer2_inode_unlock_sh(hmp->sroot, parent);
492 * Create a new PFS under the super-root
495 hammer2_ioctl_pfs_create(hammer2_inode_t *ip, void *data)
497 hammer2_inode_data_t *nipdata;
498 hammer2_mount_t *hmp;
499 hammer2_ioc_pfs_t *pfs;
500 hammer2_inode_t *nip;
501 hammer2_chain_t *nchain;
502 hammer2_trans_t trans;
505 hmp = ip->pmp->cluster.chains[0]->hmp; /* XXX */
509 if (pfs->name[0] == 0)
511 pfs->name[sizeof(pfs->name) - 1] = 0; /* ensure 0-termination */
513 hammer2_trans_init(&trans, ip->pmp, 0);
514 nip = hammer2_inode_create(&trans, hmp->sroot, NULL, NULL,
515 pfs->name, strlen(pfs->name),
518 nipdata = hammer2_chain_modify_ip(&trans, nip, &nchain,
519 HAMMER2_MODIFY_ASSERTNOCOPY);
520 nipdata->pfs_type = pfs->pfs_type;
521 nipdata->pfs_clid = pfs->pfs_clid;
522 nipdata->pfs_fsid = pfs->pfs_fsid;
525 * Do not allow compression on PFS's with the special name
526 * "boot", the boot loader can't decompress (yet).
528 if (strcmp(pfs->name, "boot") == 0)
529 nipdata->comp_algo = HAMMER2_COMP_AUTOZERO;
530 hammer2_inode_unlock_ex(nip, nchain);
532 hammer2_trans_done(&trans);
537 * Destroy an existing PFS under the super-root
540 hammer2_ioctl_pfs_delete(hammer2_inode_t *ip, void *data)
542 hammer2_mount_t *hmp;
543 hammer2_ioc_pfs_t *pfs = data;
544 hammer2_trans_t trans;
547 hmp = ip->pmp->cluster.chains[0]->hmp; /* XXX */
548 hammer2_trans_init(&trans, ip->pmp, 0);
549 error = hammer2_unlink_file(&trans, hmp->sroot,
550 pfs->name, strlen(pfs->name),
552 hammer2_trans_done(&trans);
558 hammer2_ioctl_pfs_snapshot(hammer2_inode_t *ip, void *data)
560 hammer2_ioc_pfs_t *pfs = data;
561 hammer2_trans_t trans;
562 hammer2_chain_t *parent;
565 if (pfs->name[0] == 0)
567 if (pfs->name[sizeof(pfs->name)-1] != 0)
570 hammer2_trans_init(&trans, ip->pmp, 0);
571 parent = hammer2_inode_lock_ex(ip);
572 error = hammer2_chain_snapshot(&trans, parent, pfs);
573 hammer2_inode_unlock_ex(ip, parent);
574 hammer2_trans_done(&trans);
580 * Retrieve the raw inode structure
583 hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data)
585 hammer2_ioc_inode_t *ino = data;
586 hammer2_chain_t *parent;
588 parent = hammer2_inode_lock_sh(ip);
589 ino->ip_data = ip->chain->data->ipdata;
591 hammer2_inode_unlock_sh(ip, parent);
597 * Set various parameters in an inode which cannot be set through
598 * normal filesystem VNOPS.
601 hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data)
603 hammer2_inode_data_t *ipdata;
604 hammer2_ioc_inode_t *ino = data;
605 hammer2_chain_t *chain;
606 hammer2_trans_t trans;
609 hammer2_trans_init(&trans, ip->pmp, 0);
610 chain = hammer2_inode_lock_ex(ip);
612 if (ino->ip_data.comp_algo != chain->data->ipdata.comp_algo) {
613 ipdata = hammer2_chain_modify_ip(&trans, ip, &chain, 0);
614 ipdata->comp_algo = ino->ip_data.comp_algo;
618 /* Ignore these flags for now...*/
619 if (ino->flags & HAMMER2IOC_INODE_FLAG_IQUOTA) {
621 if (ino->flags & HAMMER2IOC_INODE_FLAG_DQUOTA) {
623 if (ino->flags & HAMMER2IOC_INODE_FLAG_COPIES) {
625 hammer2_trans_done(&trans);
626 hammer2_inode_unlock_ex(ip, chain);