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