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);
62 hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data, int fflag,
68 * Standard root cred checks, will be selectively ignored below
69 * for ioctls that do not require root creds.
71 error = priv_check_cred(cred, PRIV_HAMMER_IOCTL, 0);
74 case HAMMER2IOC_VERSION_GET:
75 error = hammer2_ioctl_version_get(ip, data);
77 case HAMMER2IOC_RECLUSTER:
79 error = hammer2_ioctl_recluster(ip, data);
81 case HAMMER2IOC_REMOTE_SCAN:
83 error = hammer2_ioctl_remote_scan(ip, data);
85 case HAMMER2IOC_REMOTE_ADD:
87 error = hammer2_ioctl_remote_add(ip, data);
89 case HAMMER2IOC_REMOTE_DEL:
91 error = hammer2_ioctl_remote_del(ip, data);
93 case HAMMER2IOC_REMOTE_REP:
95 error = hammer2_ioctl_remote_rep(ip, data);
97 case HAMMER2IOC_SOCKET_GET:
99 error = hammer2_ioctl_socket_get(ip, data);
101 case HAMMER2IOC_SOCKET_SET:
103 error = hammer2_ioctl_socket_set(ip, data);
105 case HAMMER2IOC_PFS_GET:
107 error = hammer2_ioctl_pfs_get(ip, data);
109 case HAMMER2IOC_PFS_LOOKUP:
111 error = hammer2_ioctl_pfs_lookup(ip, data);
113 case HAMMER2IOC_PFS_CREATE:
115 error = hammer2_ioctl_pfs_create(ip, data);
117 case HAMMER2IOC_PFS_DELETE:
119 error = hammer2_ioctl_pfs_delete(ip, data);
121 case HAMMER2IOC_PFS_SNAPSHOT:
123 error = hammer2_ioctl_pfs_snapshot(ip, data);
125 case HAMMER2IOC_INODE_GET:
126 error = hammer2_ioctl_inode_get(ip, data);
128 case HAMMER2IOC_INODE_SET:
130 error = hammer2_ioctl_inode_set(ip, data);
140 * Retrieve version and basic info
143 hammer2_ioctl_version_get(hammer2_inode_t *ip, void *data)
145 hammer2_mount_t *hmp = ip->hmp;
146 hammer2_ioc_version_t *version = data;
148 version->version = hmp->voldata.version;
153 hammer2_ioctl_recluster(hammer2_inode_t *ip, void *data)
155 hammer2_ioc_recluster_t *recl = data;
158 fp = holdfp(curproc->p_fd, recl->fd, -1);
160 kprintf("reconnect to cluster\n");
161 hammer2_cluster_reconnect(ip->pmp, fp);
169 * Retrieve information about a remote
172 hammer2_ioctl_remote_scan(hammer2_inode_t *ip, void *data)
174 hammer2_mount_t *hmp = ip->hmp;
175 hammer2_ioc_remote_t *remote = data;
176 int copyid = remote->copyid;
178 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
181 hammer2_voldata_lock(hmp);
182 remote->copy1 = hmp->voldata.copyinfo[copyid];
183 hammer2_voldata_unlock(hmp, 0);
186 * Adjust nextid (GET only)
188 while (++copyid < HAMMER2_COPYID_COUNT &&
189 hmp->voldata.copyinfo[copyid].copyid == 0) {
192 if (copyid == HAMMER2_COPYID_COUNT)
195 remote->nextid = copyid;
201 * Add new remote entry
204 hammer2_ioctl_remote_add(hammer2_inode_t *ip, void *data)
206 hammer2_mount_t *hmp = ip->hmp;
207 hammer2_pfsmount_t *pmp = ip->pmp;
208 hammer2_ioc_remote_t *remote = data;
209 int copyid = remote->copyid;
212 if (copyid >= HAMMER2_COPYID_COUNT)
215 hammer2_voldata_lock(hmp);
217 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
218 if (hmp->voldata.copyinfo[copyid].copyid == 0)
221 if (copyid == HAMMER2_COPYID_COUNT) {
226 hammer2_modify_volume(hmp);
227 remote->copy1.copyid = copyid;
228 hmp->voldata.copyinfo[copyid] = remote->copy1;
229 hammer2_volconf_update(pmp, copyid);
231 hammer2_voldata_unlock(hmp, 1);
236 * Delete existing remote entry
239 hammer2_ioctl_remote_del(hammer2_inode_t *ip, void *data)
241 hammer2_mount_t *hmp = ip->hmp;
242 hammer2_pfsmount_t *pmp = ip->pmp;
243 hammer2_ioc_remote_t *remote = data;
244 int copyid = remote->copyid;
247 if (copyid >= HAMMER2_COPYID_COUNT)
249 remote->copy1.path[sizeof(remote->copy1.path) - 1] = 0;
250 hammer2_voldata_lock(hmp);
252 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
253 if (hmp->voldata.copyinfo[copyid].copyid == 0)
255 if (strcmp(remote->copy1.path,
256 hmp->voldata.copyinfo[copyid].path) == 0) {
260 if (copyid == HAMMER2_COPYID_COUNT) {
265 hammer2_modify_volume(hmp);
266 hmp->voldata.copyinfo[copyid].copyid = 0;
267 hammer2_volconf_update(pmp, copyid);
269 hammer2_voldata_unlock(hmp, 1);
274 * Replace existing remote entry
277 hammer2_ioctl_remote_rep(hammer2_inode_t *ip, void *data)
279 hammer2_mount_t *hmp = ip->hmp;
280 hammer2_ioc_remote_t *remote = data;
281 int copyid = remote->copyid;
283 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
286 hammer2_voldata_lock(hmp);
287 /*hammer2_volconf_update(pmp, copyid);*/
288 hammer2_voldata_unlock(hmp, 1);
294 * Retrieve communications socket
297 hammer2_ioctl_socket_get(hammer2_inode_t *ip, void *data)
303 * Set communications socket for connection
306 hammer2_ioctl_socket_set(hammer2_inode_t *ip, void *data)
308 hammer2_mount_t *hmp = ip->hmp;
309 hammer2_ioc_remote_t *remote = data;
310 int copyid = remote->copyid;
312 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
315 hammer2_voldata_lock(hmp);
316 hammer2_voldata_unlock(hmp, 0);
322 * Used to scan and retrieve PFS information. PFS's are directories under
325 * To scan PFSs pass name_key=0. The function will scan for the next
326 * PFS and set all fields, as well as set name_next to the next key.
327 * When no PFSs remain, name_next is set to (hammer2_key_t)-1.
329 * To retrieve the PFS associated with the file descriptor, pass
330 * name_key set to (hammer2_key_t)-1.
333 hammer2_ioctl_pfs_get(hammer2_inode_t *ip, void *data)
335 hammer2_inode_data_t *ipdata;
336 hammer2_mount_t *hmp;
337 hammer2_ioc_pfs_t *pfs;
338 hammer2_chain_t *parent;
339 hammer2_chain_t *chain;
340 hammer2_chain_t *rchain;
346 parent = hammer2_chain_lookup_init(hmp->schain, 0);
347 rchain = ip->pmp->rchain;
350 * Search for the first key or specific key. Remember that keys
351 * can be returned in any order.
353 if (pfs->name_key == 0) {
354 chain = hammer2_chain_lookup(&parent,
355 0, (hammer2_key_t)-1, 0);
356 } else if (pfs->name_key == (hammer2_key_t)-1) {
357 chain = hammer2_chain_lookup(&parent,
358 rchain->data->ipdata.name_key,
359 rchain->data->ipdata.name_key,
362 chain = hammer2_chain_lookup(&parent,
363 pfs->name_key, pfs->name_key, 0);
365 while (chain && chain->bref.type != HAMMER2_BREF_TYPE_INODE) {
366 chain = hammer2_chain_next(&parent, chain,
367 0, (hammer2_key_t)-1, 0);
371 * Load the data being returned by the ioctl.
373 ipdata = &chain->data->ipdata;
374 pfs->name_key = ipdata->name_key;
375 pfs->pfs_type = ipdata->pfs_type;
376 pfs->pfs_clid = ipdata->pfs_clid;
377 pfs->pfs_fsid = ipdata->pfs_fsid;
378 KKASSERT(ipdata->name_len < sizeof(pfs->name));
379 bcopy(ipdata->filename, pfs->name, ipdata->name_len);
380 pfs->name[ipdata->name_len] = 0;
381 ipdata = NULL; /* safety */
384 * Calculate the next field
387 chain = hammer2_chain_next(&parent, chain,
388 0, (hammer2_key_t)-1, 0);
389 } while (chain && chain->bref.type != HAMMER2_BREF_TYPE_INODE);
391 pfs->name_next = chain->data->ipdata.name_key;
392 hammer2_chain_unlock(chain);
394 pfs->name_next = (hammer2_key_t)-1;
397 pfs->name_next = (hammer2_key_t)-1;
400 hammer2_chain_lookup_done(parent);
406 * Find a specific PFS by name
409 hammer2_ioctl_pfs_lookup(hammer2_inode_t *ip, void *data)
411 hammer2_inode_data_t *ipdata;
412 hammer2_mount_t *hmp;
413 hammer2_ioc_pfs_t *pfs;
414 hammer2_chain_t *parent;
415 hammer2_chain_t *chain;
423 parent = hammer2_chain_lookup_init(hmp->schain, HAMMER2_LOOKUP_SHARED);
425 pfs->name[sizeof(pfs->name) - 1] = 0;
426 len = strlen(pfs->name);
427 lhc = hammer2_dirhash(pfs->name, len);
429 chain = hammer2_chain_lookup(&parent,
430 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
431 HAMMER2_LOOKUP_SHARED);
433 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
434 len == chain->data->ipdata.name_len &&
435 bcmp(pfs->name, chain->data->ipdata.filename, len) == 0) {
438 chain = hammer2_chain_next(&parent, chain,
439 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
440 HAMMER2_LOOKUP_SHARED);
444 * Load the data being returned by the ioctl.
447 ipdata = &chain->data->ipdata;
448 pfs->name_key = ipdata->name_key;
449 pfs->pfs_type = ipdata->pfs_type;
450 pfs->pfs_clid = ipdata->pfs_clid;
451 pfs->pfs_fsid = ipdata->pfs_fsid;
454 hammer2_chain_unlock(chain);
458 hammer2_chain_lookup_done(parent);
463 * Create a new PFS under the super-root
466 hammer2_ioctl_pfs_create(hammer2_inode_t *ip, void *data)
468 hammer2_inode_data_t *nipdata;
469 hammer2_mount_t *hmp;
470 hammer2_ioc_pfs_t *pfs;
471 hammer2_inode_t *nip;
472 hammer2_trans_t trans;
479 if (pfs->name[0] == 0)
481 pfs->name[sizeof(pfs->name) - 1] = 0; /* ensure 0-termination */
483 hammer2_trans_init(hmp, &trans, 0);
484 nip = hammer2_inode_create(&trans, hmp->sroot, NULL, NULL,
485 pfs->name, strlen(pfs->name),
488 nipdata = hammer2_chain_modify_ip(&trans, nip,
489 HAMMER2_MODIFY_ASSERTNOCOPY);
490 nipdata->pfs_type = pfs->pfs_type;
491 nipdata->pfs_clid = pfs->pfs_clid;
492 nipdata->pfs_fsid = pfs->pfs_fsid;
493 hammer2_inode_unlock_ex(nip);
495 hammer2_trans_done(&trans);
500 * Destroy an existing PFS under the super-root
503 hammer2_ioctl_pfs_delete(hammer2_inode_t *ip, void *data)
505 hammer2_mount_t *hmp = ip->hmp;
506 hammer2_ioc_pfs_t *pfs = data;
507 hammer2_trans_t trans;
510 hammer2_trans_init(hmp, &trans, 0);
511 error = hammer2_unlink_file(&trans, hmp->sroot,
512 pfs->name, strlen(pfs->name),
514 hammer2_trans_done(&trans);
520 hammer2_ioctl_pfs_snapshot(hammer2_inode_t *ip, void *data)
522 hammer2_mount_t *hmp = ip->hmp;
523 hammer2_ioc_pfs_t *pfs = data;
524 hammer2_trans_t trans;
527 if (pfs->name[0] == 0)
529 if (pfs->name[sizeof(pfs->name)-1] != 0)
532 hammer2_trans_init(hmp, &trans, 0);
533 hammer2_inode_lock_ex(ip);
534 error = hammer2_chain_snapshot(&trans, ip, pfs);
535 hammer2_inode_unlock_ex(ip);
536 hammer2_trans_done(&trans);
542 * Retrieve the raw inode structure
545 hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data)
547 hammer2_ioc_inode_t *ino = data;
549 hammer2_inode_lock_sh(ip);
550 ino->ip_data = ip->chain->data->ipdata;
552 hammer2_inode_unlock_sh(ip);
558 hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data)
560 hammer2_ioc_inode_t *ino = data;
563 hammer2_inode_lock_ex(ip);
564 if (ino->flags & HAMMER2IOC_INODE_FLAG_IQUOTA) {
566 if (ino->flags & HAMMER2IOC_INODE_FLAG_DQUOTA) {
568 if (ino->flags & HAMMER2IOC_INODE_FLAG_COPIES) {
570 hammer2_inode_unlock_ex(ip);