bsd-family-tree: Sync with FreeBSD (adds OpenBSD 5.3 & DragonFly 3.4.1).
[dragonfly.git] / sys / vfs / hammer2 / hammer2_ioctl.c
CommitLineData
2910a90c 1/*
0dea3156 2 * Copyright (c) 2011-2013 The DragonFly Project. All rights reserved.
2910a90c
MD
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
ae183399 45static int hammer2_ioctl_version_get(hammer2_inode_t *ip, void *data);
1a34728c
MD
46static int hammer2_ioctl_recluster(hammer2_inode_t *ip, void *data);
47static int hammer2_ioctl_remote_scan(hammer2_inode_t *ip, void *data);
ae183399
MD
48static int hammer2_ioctl_remote_add(hammer2_inode_t *ip, void *data);
49static int hammer2_ioctl_remote_del(hammer2_inode_t *ip, void *data);
50static int hammer2_ioctl_remote_rep(hammer2_inode_t *ip, void *data);
51static int hammer2_ioctl_socket_get(hammer2_inode_t *ip, void *data);
52static int hammer2_ioctl_socket_set(hammer2_inode_t *ip, void *data);
53static int hammer2_ioctl_pfs_get(hammer2_inode_t *ip, void *data);
458ecb1b 54static int hammer2_ioctl_pfs_lookup(hammer2_inode_t *ip, void *data);
ae183399
MD
55static int hammer2_ioctl_pfs_create(hammer2_inode_t *ip, void *data);
56static int hammer2_ioctl_pfs_delete(hammer2_inode_t *ip, void *data);
344d4f82
MD
57static int hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data);
58static int hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data);
2910a90c
MD
59
60int
61hammer2_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) {
ae183399
MD
73 case HAMMER2IOC_VERSION_GET:
74 error = hammer2_ioctl_version_get(ip, data);
2910a90c 75 break;
1a34728c 76 case HAMMER2IOC_RECLUSTER:
2910a90c 77 if (error == 0)
1a34728c
MD
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);
2910a90c 83 break;
ae183399 84 case HAMMER2IOC_REMOTE_ADD:
2910a90c 85 if (error == 0)
ae183399 86 error = hammer2_ioctl_remote_add(ip, data);
2910a90c 87 break;
ae183399 88 case HAMMER2IOC_REMOTE_DEL:
2910a90c 89 if (error == 0)
ae183399 90 error = hammer2_ioctl_remote_del(ip, data);
2910a90c 91 break;
ae183399 92 case HAMMER2IOC_REMOTE_REP:
2910a90c 93 if (error == 0)
ae183399 94 error = hammer2_ioctl_remote_rep(ip, data);
2910a90c 95 break;
ae183399 96 case HAMMER2IOC_SOCKET_GET:
2910a90c 97 if (error == 0)
ae183399 98 error = hammer2_ioctl_socket_get(ip, data);
2910a90c 99 break;
ae183399
MD
100 case HAMMER2IOC_SOCKET_SET:
101 if (error == 0)
102 error = hammer2_ioctl_socket_set(ip, data);
103 break;
104 case HAMMER2IOC_PFS_GET:
2910a90c 105 if (error == 0)
ae183399
MD
106 error = hammer2_ioctl_pfs_get(ip, data);
107 break;
458ecb1b
MD
108 case HAMMER2IOC_PFS_LOOKUP:
109 if (error == 0)
110 error = hammer2_ioctl_pfs_lookup(ip, data);
111 break;
ae183399
MD
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);
2910a90c 119 break;
344d4f82
MD
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;
2910a90c
MD
127 default:
128 error = EOPNOTSUPP;
129 break;
130 }
131 return (error);
132}
133
134/*
135 * Retrieve version and basic info
136 */
137static int
ae183399 138hammer2_ioctl_version_get(hammer2_inode_t *ip, void *data)
2910a90c
MD
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
1a34728c
MD
147static int
148hammer2_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
2910a90c
MD
163/*
164 * Retrieve information about a remote
165 */
166static int
1a34728c 167hammer2_ioctl_remote_scan(hammer2_inode_t *ip, void *data)
2910a90c
MD
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];
0dea3156 178 hammer2_voldata_unlock(hmp, 0);
2910a90c
MD
179
180 /*
181 * Adjust nextid (GET only)
182 */
183 while (++copyid < HAMMER2_COPYID_COUNT &&
184 hmp->voldata.copyinfo[copyid].copyid == 0) {
1a34728c 185 ;
2910a90c
MD
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 */
198static int
ae183399 199hammer2_ioctl_remote_add(hammer2_inode_t *ip, void *data)
2910a90c
MD
200{
201 hammer2_mount_t *hmp = ip->hmp;
1a34728c 202 hammer2_pfsmount_t *pmp = ip->pmp;
2910a90c
MD
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);
2910a90c
MD
222 remote->copy1.copyid = copyid;
223 hmp->voldata.copyinfo[copyid] = remote->copy1;
1a34728c 224 hammer2_volconf_update(pmp, copyid);
2910a90c 225failed:
0dea3156 226 hammer2_voldata_unlock(hmp, 1);
2910a90c
MD
227 return (error);
228}
229
230/*
231 * Delete existing remote entry
232 */
233static int
ae183399 234hammer2_ioctl_remote_del(hammer2_inode_t *ip, void *data)
2910a90c
MD
235{
236 hammer2_mount_t *hmp = ip->hmp;
1a34728c 237 hammer2_pfsmount_t *pmp = ip->pmp;
2910a90c
MD
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;
1a34728c 262 hammer2_volconf_update(pmp, copyid);
2910a90c 263failed:
0dea3156 264 hammer2_voldata_unlock(hmp, 1);
2910a90c
MD
265 return (error);
266}
267
268/*
269 * Replace existing remote entry
270 */
271static int
ae183399 272hammer2_ioctl_remote_rep(hammer2_inode_t *ip, void *data)
2910a90c
MD
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);
1a34728c 282 /*hammer2_volconf_update(pmp, copyid);*/
0dea3156 283 hammer2_voldata_unlock(hmp, 1);
2910a90c
MD
284
285 return(0);
286}
287
288/*
289 * Retrieve communications socket
290 */
291static int
ae183399 292hammer2_ioctl_socket_get(hammer2_inode_t *ip, void *data)
2910a90c
MD
293{
294 return (EOPNOTSUPP);
295}
296
297/*
298 * Set communications socket for connection
299 */
300static int
ae183399 301hammer2_ioctl_socket_set(hammer2_inode_t *ip, void *data)
2910a90c
MD
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);
0dea3156 311 hammer2_voldata_unlock(hmp, 0);
2910a90c
MD
312
313 return(0);
314}
ae183399
MD
315
316/*
317 * Used to scan PFSs, which are directories under the super-root.
318 */
319static int
320hammer2_ioctl_pfs_get(hammer2_inode_t *ip, void *data)
321{
476d2aad
MD
322 hammer2_inode_data_t *ipdata;
323 hammer2_mount_t *hmp;
324 hammer2_ioc_pfs_t *pfs;
ae183399
MD
325 hammer2_chain_t *parent;
326 hammer2_chain_t *chain;
476d2aad 327 int error;
ae183399 328
476d2aad
MD
329 error = 0;
330 hmp = ip->hmp;
331 pfs = data;
0dea3156 332 parent = hammer2_chain_lookup_init(hmp->schain, 0);
ae183399
MD
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) {
0dea3156 339 chain = hammer2_chain_lookup(&parent,
ae183399
MD
340 0, (hammer2_key_t)-1, 0);
341 } else {
0dea3156 342 chain = hammer2_chain_lookup(&parent,
ae183399
MD
343 pfs->name_key, pfs->name_key, 0);
344 }
345 while (chain && chain->bref.type != HAMMER2_BREF_TYPE_INODE) {
0dea3156
MD
346 chain = hammer2_chain_next(&parent, chain,
347 0, (hammer2_key_t)-1, 0);
ae183399
MD
348 }
349 if (chain) {
350 /*
351 * Load the data being returned by the ioctl.
352 */
476d2aad
MD
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 */
ae183399
MD
362
363 /*
364 * Calculate the next field
365 */
366 do {
0dea3156
MD
367 chain = hammer2_chain_next(&parent, chain,
368 0, (hammer2_key_t)-1, 0);
ae183399
MD
369 } while (chain && chain->bref.type != HAMMER2_BREF_TYPE_INODE);
370 if (chain) {
476d2aad 371 pfs->name_next = chain->data->ipdata.name_key;
0dea3156 372 hammer2_chain_unlock(chain);
ae183399
MD
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 }
0dea3156
MD
380 hammer2_chain_lookup_done(parent);
381
ae183399
MD
382 return (error);
383}
384
458ecb1b
MD
385/*
386 * Find a specific PFS by name
387 */
388static int
389hammer2_ioctl_pfs_lookup(hammer2_inode_t *ip, void *data)
390{
476d2aad
MD
391 hammer2_inode_data_t *ipdata;
392 hammer2_mount_t *hmp;
393 hammer2_ioc_pfs_t *pfs;
458ecb1b
MD
394 hammer2_chain_t *parent;
395 hammer2_chain_t *chain;
458ecb1b 396 hammer2_key_t lhc;
476d2aad 397 int error;
458ecb1b
MD
398 size_t len;
399
476d2aad
MD
400 error = 0;
401 hmp = ip->hmp;
402 pfs = data;
0dea3156 403 parent = hammer2_chain_lookup_init(hmp->schain, HAMMER2_LOOKUP_SHARED);
458ecb1b
MD
404
405 pfs->name[sizeof(pfs->name) - 1] = 0;
406 len = strlen(pfs->name);
407 lhc = hammer2_dirhash(pfs->name, len);
408
0dea3156 409 chain = hammer2_chain_lookup(&parent,
458ecb1b
MD
410 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
411 HAMMER2_LOOKUP_SHARED);
412 while (chain) {
413 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
458ecb1b
MD
414 len == chain->data->ipdata.name_len &&
415 bcmp(pfs->name, chain->data->ipdata.filename, len) == 0) {
416 break;
417 }
0dea3156 418 chain = hammer2_chain_next(&parent, chain,
458ecb1b
MD
419 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
420 HAMMER2_LOOKUP_SHARED);
421 }
422
423 /*
424 * Load the data being returned by the ioctl.
425 */
426 if (chain) {
476d2aad
MD
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;
432 ipdata = NULL;
458ecb1b 433
0dea3156 434 hammer2_chain_unlock(chain);
458ecb1b
MD
435 } else {
436 error = ENOENT;
437 }
0dea3156 438 hammer2_chain_lookup_done(parent);
458ecb1b
MD
439 return (error);
440}
441
ae183399
MD
442/*
443 * Create a new PFS under the super-root
444 */
445static int
446hammer2_ioctl_pfs_create(hammer2_inode_t *ip, void *data)
447{
476d2aad
MD
448 hammer2_inode_data_t *nipdata;
449 hammer2_mount_t *hmp;
450 hammer2_ioc_pfs_t *pfs;
451 hammer2_inode_t *nip;
0dea3156 452 hammer2_trans_t trans;
ae183399
MD
453 int error;
454
476d2aad
MD
455 hmp = ip->hmp;
456 pfs = data;
457 nip = NULL;
458
ae183399 459 pfs->name[sizeof(pfs->name) - 1] = 0; /* ensure 0-termination */
10252dc7 460
d001f460 461 hammer2_trans_init(hmp, &trans, 0);
0dea3156 462 nip = hammer2_inode_create(&trans, hmp->sroot, NULL, NULL,
ae183399 463 pfs->name, strlen(pfs->name),
0dea3156 464 &error);
ae183399 465 if (error == 0) {
4a59bd3e
MD
466 nipdata = hammer2_chain_modify_ip(&trans, nip,
467 HAMMER2_MODIFY_ASSERTNOCOPY);
476d2aad
MD
468 nipdata->pfs_type = pfs->pfs_type;
469 nipdata->pfs_clid = pfs->pfs_clid;
470 nipdata->pfs_fsid = pfs->pfs_fsid;
0dea3156 471 hammer2_inode_unlock_ex(nip);
ae183399 472 }
0dea3156 473 hammer2_trans_done(&trans);
ae183399
MD
474 return (error);
475}
476
477/*
478 * Destroy an existing PFS under the super-root
479 */
480static int
481hammer2_ioctl_pfs_delete(hammer2_inode_t *ip, void *data)
482{
483 hammer2_mount_t *hmp = ip->hmp;
484 hammer2_ioc_pfs_t *pfs = data;
0dea3156 485 hammer2_trans_t trans;
ae183399
MD
486 int error;
487
d001f460 488 hammer2_trans_init(hmp, &trans, 0);
0dea3156 489 error = hammer2_unlink_file(&trans, hmp->sroot,
9797e933
MD
490 pfs->name, strlen(pfs->name),
491 0, NULL);
0dea3156
MD
492 hammer2_trans_done(&trans);
493
ae183399
MD
494 return (error);
495}
344d4f82
MD
496
497/*
498 * Retrieve the raw inode structure
499 */
500static int
501hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data)
502{
503 hammer2_ioc_inode_t *ino = data;
504
0dea3156
MD
505 hammer2_inode_lock_sh(ip);
506 ino->ip_data = ip->chain->data->ipdata;
26b047fa 507 ino->kdata = ip;
0dea3156
MD
508 hammer2_inode_unlock_sh(ip);
509
344d4f82
MD
510 return (0);
511}
512
513static int
514hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data)
515{
516 hammer2_ioc_inode_t *ino = data;
517 int error = EINVAL;
518
0dea3156 519 hammer2_inode_lock_ex(ip);
344d4f82
MD
520 if (ino->flags & HAMMER2IOC_INODE_FLAG_IQUOTA) {
521 }
522 if (ino->flags & HAMMER2IOC_INODE_FLAG_DQUOTA) {
523 }
524 if (ino->flags & HAMMER2IOC_INODE_FLAG_COPIES) {
525 }
0dea3156
MD
526 hammer2_inode_unlock_ex(ip);
527
344d4f82
MD
528 return (error);
529}