1 /* 2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved. 3 * 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> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 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 17 * distribution. 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. 21 * 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 33 * SUCH DAMAGE. 34 */ 35 /* 36 * Ioctl Functions. 37 * 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. 41 */ 42 43 #include "hammer2.h" 44 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); 59 60 int 61 hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data, int fflag, 62 struct ucred *cred) 63 { 64 int error; 65 66 /* 67 * Standard root cred checks, will be selectively ignored below 68 * for ioctls that do not require root creds. 69 */ 70 error = priv_check_cred(cred, PRIV_HAMMER_IOCTL, 0); 71 72 switch(com) { 73 case HAMMER2IOC_VERSION_GET: 74 error = hammer2_ioctl_version_get(ip, data); 75 break; 76 case HAMMER2IOC_RECLUSTER: 77 if (error == 0) 78 error = hammer2_ioctl_recluster(ip, data); 79 break; 80 case HAMMER2IOC_REMOTE_SCAN: 81 if (error == 0) 82 error = hammer2_ioctl_remote_scan(ip, data); 83 break; 84 case HAMMER2IOC_REMOTE_ADD: 85 if (error == 0) 86 error = hammer2_ioctl_remote_add(ip, data); 87 break; 88 case HAMMER2IOC_REMOTE_DEL: 89 if (error == 0) 90 error = hammer2_ioctl_remote_del(ip, data); 91 break; 92 case HAMMER2IOC_REMOTE_REP: 93 if (error == 0) 94 error = hammer2_ioctl_remote_rep(ip, data); 95 break; 96 case HAMMER2IOC_SOCKET_GET: 97 if (error == 0) 98 error = hammer2_ioctl_socket_get(ip, data); 99 break; 100 case HAMMER2IOC_SOCKET_SET: 101 if (error == 0) 102 error = hammer2_ioctl_socket_set(ip, data); 103 break; 104 case HAMMER2IOC_PFS_GET: 105 if (error == 0) 106 error = hammer2_ioctl_pfs_get(ip, data); 107 break; 108 case HAMMER2IOC_PFS_LOOKUP: 109 if (error == 0) 110 error = hammer2_ioctl_pfs_lookup(ip, data); 111 break; 112 case HAMMER2IOC_PFS_CREATE: 113 if (error == 0) 114 error = hammer2_ioctl_pfs_create(ip, data); 115 break; 116 case HAMMER2IOC_PFS_DELETE: 117 if (error == 0) 118 error = hammer2_ioctl_pfs_delete(ip, data); 119 break; 120 case HAMMER2IOC_INODE_GET: 121 error = hammer2_ioctl_inode_get(ip, data); 122 break; 123 case HAMMER2IOC_INODE_SET: 124 if (error == 0) 125 error = hammer2_ioctl_inode_set(ip, data); 126 break; 127 default: 128 error = EOPNOTSUPP; 129 break; 130 } 131 return (error); 132 } 133 134 /* 135 * Retrieve version and basic info 136 */ 137 static int 138 hammer2_ioctl_version_get(hammer2_inode_t *ip, void *data) 139 { 140 hammer2_mount_t *hmp = ip->hmp; 141 hammer2_ioc_version_t *version = data; 142 143 version->version = hmp->voldata.version; 144 return 0; 145 } 146 147 static int 148 hammer2_ioctl_recluster(hammer2_inode_t *ip, void *data) 149 { 150 hammer2_ioc_recluster_t *recl = data; 151 struct file *fp; 152 153 fp = holdfp(curproc->p_fd, recl->fd, -1); 154 if (fp) { 155 kprintf("reconnect to cluster\n"); 156 hammer2_cluster_reconnect(ip->pmp, fp); 157 return 0; 158 } else { 159 return EINVAL; 160 } 161 } 162 163 /* 164 * Retrieve information about a remote 165 */ 166 static int 167 hammer2_ioctl_remote_scan(hammer2_inode_t *ip, void *data) 168 { 169 hammer2_mount_t *hmp = ip->hmp; 170 hammer2_ioc_remote_t *remote = data; 171 int copyid = remote->copyid; 172 173 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT) 174 return (EINVAL); 175 176 hammer2_voldata_lock(hmp); 177 remote->copy1 = hmp->voldata.copyinfo[copyid]; 178 hammer2_voldata_unlock(hmp); 179 180 /* 181 * Adjust nextid (GET only) 182 */ 183 while (++copyid < HAMMER2_COPYID_COUNT && 184 hmp->voldata.copyinfo[copyid].copyid == 0) { 185 ; 186 } 187 if (copyid == HAMMER2_COPYID_COUNT) 188 remote->nextid = -1; 189 else 190 remote->nextid = copyid; 191 192 return(0); 193 } 194 195 /* 196 * Add new remote entry 197 */ 198 static int 199 hammer2_ioctl_remote_add(hammer2_inode_t *ip, void *data) 200 { 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; 205 int error = 0; 206 207 if (copyid >= HAMMER2_COPYID_COUNT) 208 return (EINVAL); 209 210 hammer2_voldata_lock(hmp); 211 if (copyid < 0) { 212 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) { 213 if (hmp->voldata.copyinfo[copyid].copyid == 0) 214 break; 215 } 216 if (copyid == HAMMER2_COPYID_COUNT) { 217 error = ENOSPC; 218 goto failed; 219 } 220 } 221 hammer2_modify_volume(hmp); 222 remote->copy1.copyid = copyid; 223 hmp->voldata.copyinfo[copyid] = remote->copy1; 224 hammer2_volconf_update(pmp, copyid); 225 failed: 226 hammer2_voldata_unlock(hmp); 227 return (error); 228 } 229 230 /* 231 * Delete existing remote entry 232 */ 233 static int 234 hammer2_ioctl_remote_del(hammer2_inode_t *ip, void *data) 235 { 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; 240 int error = 0; 241 242 if (copyid >= HAMMER2_COPYID_COUNT) 243 return (EINVAL); 244 remote->copy1.path[sizeof(remote->copy1.path) - 1] = 0; 245 hammer2_voldata_lock(hmp); 246 if (copyid < 0) { 247 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) { 248 if (hmp->voldata.copyinfo[copyid].copyid == 0) 249 continue; 250 if (strcmp(remote->copy1.path, 251 hmp->voldata.copyinfo[copyid].path) == 0) { 252 break; 253 } 254 } 255 if (copyid == HAMMER2_COPYID_COUNT) { 256 error = ENOENT; 257 goto failed; 258 } 259 } 260 hammer2_modify_volume(hmp); 261 hmp->voldata.copyinfo[copyid].copyid = 0; 262 hammer2_volconf_update(pmp, copyid); 263 failed: 264 hammer2_voldata_unlock(hmp); 265 return (error); 266 } 267 268 /* 269 * Replace existing remote entry 270 */ 271 static int 272 hammer2_ioctl_remote_rep(hammer2_inode_t *ip, void *data) 273 { 274 hammer2_mount_t *hmp = ip->hmp; 275 hammer2_ioc_remote_t *remote = data; 276 int copyid = remote->copyid; 277 278 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT) 279 return (EINVAL); 280 281 hammer2_voldata_lock(hmp); 282 /*hammer2_volconf_update(pmp, copyid);*/ 283 hammer2_voldata_unlock(hmp); 284 285 return(0); 286 } 287 288 /* 289 * Retrieve communications socket 290 */ 291 static int 292 hammer2_ioctl_socket_get(hammer2_inode_t *ip, void *data) 293 { 294 return (EOPNOTSUPP); 295 } 296 297 /* 298 * Set communications socket for connection 299 */ 300 static int 301 hammer2_ioctl_socket_set(hammer2_inode_t *ip, void *data) 302 { 303 hammer2_mount_t *hmp = ip->hmp; 304 hammer2_ioc_remote_t *remote = data; 305 int copyid = remote->copyid; 306 307 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT) 308 return (EINVAL); 309 310 hammer2_voldata_lock(hmp); 311 hammer2_voldata_unlock(hmp); 312 313 return(0); 314 } 315 316 /* 317 * Used to scan PFSs, which are directories under the super-root. 318 */ 319 static int 320 hammer2_ioctl_pfs_get(hammer2_inode_t *ip, void *data) 321 { 322 hammer2_mount_t *hmp = ip->hmp; 323 hammer2_ioc_pfs_t *pfs = data; 324 hammer2_chain_t *parent; 325 hammer2_chain_t *chain; 326 hammer2_inode_t *xip; 327 int error = 0; 328 329 parent = hmp->schain; 330 error = hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS); 331 if (error) 332 goto done; 333 334 /* 335 * Search for the first key or specific key. Remember that keys 336 * can be returned in any order. 337 */ 338 if (pfs->name_key == 0) { 339 chain = hammer2_chain_lookup(hmp, &parent, 340 0, (hammer2_key_t)-1, 0); 341 } else { 342 chain = hammer2_chain_lookup(hmp, &parent, 343 pfs->name_key, pfs->name_key, 0); 344 } 345 while (chain && chain->bref.type != HAMMER2_BREF_TYPE_INODE) { 346 chain = hammer2_chain_next(hmp, &parent, chain, 347 0, (hammer2_key_t)-1, 0); 348 } 349 if (chain) { 350 /* 351 * Load the data being returned by the ioctl. 352 */ 353 xip = chain->u.ip; 354 pfs->name_key = xip->ip_data.name_key; 355 pfs->pfs_type = xip->ip_data.pfs_type; 356 pfs->pfs_clid = xip->ip_data.pfs_clid; 357 pfs->pfs_fsid = xip->ip_data.pfs_fsid; 358 KKASSERT(xip->ip_data.name_len < sizeof(pfs->name)); 359 bcopy(xip->ip_data.filename, pfs->name, 360 xip->ip_data.name_len); 361 pfs->name[xip->ip_data.name_len] = 0; 362 363 /* 364 * Calculate the next field 365 */ 366 do { 367 chain = hammer2_chain_next(hmp, &parent, chain, 368 0, (hammer2_key_t)-1, 0); 369 } while (chain && chain->bref.type != HAMMER2_BREF_TYPE_INODE); 370 if (chain) { 371 pfs->name_next = chain->u.ip->ip_data.name_key; 372 hammer2_chain_unlock(hmp, chain); 373 } else { 374 pfs->name_next = (hammer2_key_t)-1; 375 } 376 } else { 377 pfs->name_next = (hammer2_key_t)-1; 378 error = ENOENT; 379 } 380 done: 381 hammer2_chain_unlock(hmp, parent); 382 return (error); 383 } 384 385 /* 386 * Find a specific PFS by name 387 */ 388 static int 389 hammer2_ioctl_pfs_lookup(hammer2_inode_t *ip, void *data) 390 { 391 hammer2_mount_t *hmp = ip->hmp; 392 hammer2_ioc_pfs_t *pfs = data; 393 hammer2_chain_t *parent; 394 hammer2_chain_t *chain; 395 hammer2_inode_t *xip; 396 hammer2_key_t lhc; 397 int error = 0; 398 size_t len; 399 400 parent = hmp->schain; 401 error = hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS | 402 HAMMER2_RESOLVE_SHARED); 403 if (error) 404 goto done; 405 406 pfs->name[sizeof(pfs->name) - 1] = 0; 407 len = strlen(pfs->name); 408 lhc = hammer2_dirhash(pfs->name, len); 409 410 chain = hammer2_chain_lookup(hmp, &parent, 411 lhc, lhc + HAMMER2_DIRHASH_LOMASK, 412 HAMMER2_LOOKUP_SHARED); 413 while (chain) { 414 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && 415 chain->u.ip && 416 len == chain->data->ipdata.name_len && 417 bcmp(pfs->name, chain->data->ipdata.filename, len) == 0) { 418 break; 419 } 420 chain = hammer2_chain_next(hmp, &parent, chain, 421 lhc, lhc + HAMMER2_DIRHASH_LOMASK, 422 HAMMER2_LOOKUP_SHARED); 423 } 424 425 /* 426 * Load the data being returned by the ioctl. 427 */ 428 if (chain) { 429 xip = chain->u.ip; 430 pfs->name_key = xip->ip_data.name_key; 431 pfs->pfs_type = xip->ip_data.pfs_type; 432 pfs->pfs_clid = xip->ip_data.pfs_clid; 433 pfs->pfs_fsid = xip->ip_data.pfs_fsid; 434 435 hammer2_chain_unlock(hmp, chain); 436 } else { 437 error = ENOENT; 438 } 439 done: 440 hammer2_chain_unlock(hmp, parent); 441 return (error); 442 } 443 444 /* 445 * Create a new PFS under the super-root 446 */ 447 static int 448 hammer2_ioctl_pfs_create(hammer2_inode_t *ip, void *data) 449 { 450 hammer2_mount_t *hmp = ip->hmp; 451 hammer2_ioc_pfs_t *pfs = data; 452 hammer2_inode_t *nip = NULL; 453 int error; 454 455 pfs->name[sizeof(pfs->name) - 1] = 0; /* ensure 0-termination */ 456 error = hammer2_inode_create(hmp->schain->u.ip, NULL, NULL, 457 pfs->name, strlen(pfs->name), 458 &nip); 459 if (error == 0) { 460 hammer2_chain_modify(hmp, &nip->chain, 0); 461 nip->ip_data.pfs_type = pfs->pfs_type; 462 nip->ip_data.pfs_clid = pfs->pfs_clid; 463 nip->ip_data.pfs_fsid = pfs->pfs_fsid; 464 hammer2_chain_unlock(hmp, &nip->chain); 465 } 466 return (error); 467 } 468 469 /* 470 * Destroy an existing PFS under the super-root 471 */ 472 static int 473 hammer2_ioctl_pfs_delete(hammer2_inode_t *ip, void *data) 474 { 475 hammer2_mount_t *hmp = ip->hmp; 476 hammer2_ioc_pfs_t *pfs = data; 477 int error; 478 479 error = hammer2_unlink_file(hmp->schain->u.ip, 480 pfs->name, strlen(pfs->name), 481 0, NULL); 482 return (error); 483 } 484 485 /* 486 * Retrieve the raw inode structure 487 */ 488 static int 489 hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data) 490 { 491 hammer2_ioc_inode_t *ino = data; 492 493 hammer2_inode_lock_sh(ip); 494 ino->ip_data = ip->ip_data; 495 ino->kdata = ip; 496 hammer2_inode_unlock_sh(ip); 497 return (0); 498 } 499 500 static int 501 hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data) 502 { 503 hammer2_ioc_inode_t *ino = data; 504 int error = EINVAL; 505 506 hammer2_inode_lock_ex(ip); 507 if (ino->flags & HAMMER2IOC_INODE_FLAG_IQUOTA) { 508 } 509 if (ino->flags & HAMMER2IOC_INODE_FLAG_DQUOTA) { 510 } 511 if (ino->flags & HAMMER2IOC_INODE_FLAG_COPIES) { 512 } 513 hammer2_inode_unlock_ex(ip); 514 return (error); 515 } 516