hammer2 - update documentation, begin working on callback I/O
[dragonfly.git] / sys / vfs / hammer2 / hammer2_ioctl.c
CommitLineData
2910a90c 1/*
8138a154 2 * Copyright (c) 2011-2014 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 55static int hammer2_ioctl_pfs_create(hammer2_inode_t *ip, void *data);
a864c5d9 56static int hammer2_ioctl_pfs_snapshot(hammer2_inode_t *ip, void *data);
ae183399 57static int hammer2_ioctl_pfs_delete(hammer2_inode_t *ip, void *data);
344d4f82
MD
58static int hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data);
59static int hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data);
03188ed8 60static int hammer2_ioctl_debug_dump(hammer2_inode_t *ip);
355d67fc
MD
61//static int hammer2_ioctl_inode_comp_set(hammer2_inode_t *ip, void *data);
62//static int hammer2_ioctl_inode_comp_rec_set(hammer2_inode_t *ip, void *data);
63//static int hammer2_ioctl_inode_comp_rec_set2(hammer2_inode_t *ip, void *data);
2910a90c
MD
64
65int
66hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data, int fflag,
67 struct ucred *cred)
68{
69 int error;
70
71 /*
72 * Standard root cred checks, will be selectively ignored below
73 * for ioctls that do not require root creds.
74 */
75 error = priv_check_cred(cred, PRIV_HAMMER_IOCTL, 0);
76
77 switch(com) {
ae183399
MD
78 case HAMMER2IOC_VERSION_GET:
79 error = hammer2_ioctl_version_get(ip, data);
2910a90c 80 break;
1a34728c 81 case HAMMER2IOC_RECLUSTER:
2910a90c 82 if (error == 0)
1a34728c
MD
83 error = hammer2_ioctl_recluster(ip, data);
84 break;
85 case HAMMER2IOC_REMOTE_SCAN:
86 if (error == 0)
87 error = hammer2_ioctl_remote_scan(ip, data);
2910a90c 88 break;
ae183399 89 case HAMMER2IOC_REMOTE_ADD:
2910a90c 90 if (error == 0)
ae183399 91 error = hammer2_ioctl_remote_add(ip, data);
2910a90c 92 break;
ae183399 93 case HAMMER2IOC_REMOTE_DEL:
2910a90c 94 if (error == 0)
ae183399 95 error = hammer2_ioctl_remote_del(ip, data);
2910a90c 96 break;
ae183399 97 case HAMMER2IOC_REMOTE_REP:
2910a90c 98 if (error == 0)
ae183399 99 error = hammer2_ioctl_remote_rep(ip, data);
2910a90c 100 break;
ae183399 101 case HAMMER2IOC_SOCKET_GET:
2910a90c 102 if (error == 0)
ae183399 103 error = hammer2_ioctl_socket_get(ip, data);
2910a90c 104 break;
ae183399
MD
105 case HAMMER2IOC_SOCKET_SET:
106 if (error == 0)
107 error = hammer2_ioctl_socket_set(ip, data);
108 break;
109 case HAMMER2IOC_PFS_GET:
2910a90c 110 if (error == 0)
ae183399
MD
111 error = hammer2_ioctl_pfs_get(ip, data);
112 break;
458ecb1b
MD
113 case HAMMER2IOC_PFS_LOOKUP:
114 if (error == 0)
115 error = hammer2_ioctl_pfs_lookup(ip, data);
116 break;
ae183399
MD
117 case HAMMER2IOC_PFS_CREATE:
118 if (error == 0)
119 error = hammer2_ioctl_pfs_create(ip, data);
120 break;
121 case HAMMER2IOC_PFS_DELETE:
122 if (error == 0)
123 error = hammer2_ioctl_pfs_delete(ip, data);
2910a90c 124 break;
a864c5d9
MD
125 case HAMMER2IOC_PFS_SNAPSHOT:
126 if (error == 0)
127 error = hammer2_ioctl_pfs_snapshot(ip, data);
128 break;
344d4f82
MD
129 case HAMMER2IOC_INODE_GET:
130 error = hammer2_ioctl_inode_get(ip, data);
131 break;
132 case HAMMER2IOC_INODE_SET:
133 if (error == 0)
134 error = hammer2_ioctl_inode_set(ip, data);
135 break;
355d67fc
MD
136 /*case HAMMER2IOC_INODE_COMP_SET:
137 error = hammer2_ioctl_inode_comp_set(ip, data);
138 break;
139 case HAMMER2IOC_INODE_COMP_REC_SET:
140 error = hammer2_ioctl_inode_comp_rec_set(ip, data);
141 break;
142 case HAMMER2IOC_INODE_COMP_REC_SET2:
143 error = hammer2_ioctl_inode_comp_rec_set2(ip, data);
144 break;*/
03188ed8
MD
145 case HAMMER2IOC_DEBUG_DUMP:
146 error = hammer2_ioctl_debug_dump(ip);
147 break;
2910a90c
MD
148 default:
149 error = EOPNOTSUPP;
150 break;
151 }
152 return (error);
153}
154
155/*
156 * Retrieve version and basic info
157 */
158static int
ae183399 159hammer2_ioctl_version_get(hammer2_inode_t *ip, void *data)
2910a90c 160{
58e43599 161 hammer2_mount_t *hmp = ip->pmp->iroot->cluster.focus->hmp;
2910a90c
MD
162 hammer2_ioc_version_t *version = data;
163
164 version->version = hmp->voldata.version;
165 return 0;
166}
167
1a34728c
MD
168static int
169hammer2_ioctl_recluster(hammer2_inode_t *ip, void *data)
170{
171 hammer2_ioc_recluster_t *recl = data;
172 struct file *fp;
063639ba
MD
173 hammer2_cluster_t *cluster;
174 int error;
1a34728c
MD
175
176 fp = holdfp(curproc->p_fd, recl->fd, -1);
177 if (fp) {
063639ba
MD
178 kprintf("reconnect to cluster: ");
179 cluster = &ip->pmp->iroot->cluster;
180 if (cluster->nchains != 1 || cluster->focus == NULL) {
181 kprintf("not a local device mount\n");
182 error = EINVAL;
183 } else {
184 hammer2_cluster_reconnect(cluster->focus->hmp, fp);
185 kprintf("ok\n");
186 error = 0;
187 }
1a34728c 188 } else {
063639ba 189 error = EINVAL;
1a34728c 190 }
063639ba 191 return error;
1a34728c
MD
192}
193
2910a90c
MD
194/*
195 * Retrieve information about a remote
196 */
197static int
1a34728c 198hammer2_ioctl_remote_scan(hammer2_inode_t *ip, void *data)
2910a90c 199{
58e43599 200 hammer2_mount_t *hmp = ip->pmp->iroot->cluster.focus->hmp;
2910a90c
MD
201 hammer2_ioc_remote_t *remote = data;
202 int copyid = remote->copyid;
203
204 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
205 return (EINVAL);
206
207 hammer2_voldata_lock(hmp);
208 remote->copy1 = hmp->voldata.copyinfo[copyid];
50456506 209 hammer2_voldata_unlock(hmp);
2910a90c
MD
210
211 /*
212 * Adjust nextid (GET only)
213 */
214 while (++copyid < HAMMER2_COPYID_COUNT &&
215 hmp->voldata.copyinfo[copyid].copyid == 0) {
1a34728c 216 ;
2910a90c
MD
217 }
218 if (copyid == HAMMER2_COPYID_COUNT)
219 remote->nextid = -1;
220 else
221 remote->nextid = copyid;
222
223 return(0);
224}
225
226/*
227 * Add new remote entry
228 */
229static int
ae183399 230hammer2_ioctl_remote_add(hammer2_inode_t *ip, void *data)
2910a90c 231{
2910a90c 232 hammer2_ioc_remote_t *remote = data;
a5913bdf
MD
233 hammer2_pfsmount_t *pmp = ip->pmp;
234 hammer2_mount_t *hmp;
2910a90c
MD
235 int copyid = remote->copyid;
236 int error = 0;
237
238 if (copyid >= HAMMER2_COPYID_COUNT)
239 return (EINVAL);
240
58e43599 241 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
2910a90c
MD
242 hammer2_voldata_lock(hmp);
243 if (copyid < 0) {
244 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
245 if (hmp->voldata.copyinfo[copyid].copyid == 0)
246 break;
247 }
248 if (copyid == HAMMER2_COPYID_COUNT) {
249 error = ENOSPC;
250 goto failed;
251 }
252 }
50456506 253 hammer2_voldata_modify(hmp);
2910a90c
MD
254 remote->copy1.copyid = copyid;
255 hmp->voldata.copyinfo[copyid] = remote->copy1;
063639ba 256 hammer2_volconf_update(hmp, copyid);
2910a90c 257failed:
50456506 258 hammer2_voldata_unlock(hmp);
2910a90c
MD
259 return (error);
260}
261
262/*
263 * Delete existing remote entry
264 */
265static int
ae183399 266hammer2_ioctl_remote_del(hammer2_inode_t *ip, void *data)
2910a90c 267{
2910a90c 268 hammer2_ioc_remote_t *remote = data;
a5913bdf
MD
269 hammer2_pfsmount_t *pmp = ip->pmp;
270 hammer2_mount_t *hmp;
2910a90c
MD
271 int copyid = remote->copyid;
272 int error = 0;
273
58e43599 274 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
2910a90c
MD
275 if (copyid >= HAMMER2_COPYID_COUNT)
276 return (EINVAL);
277 remote->copy1.path[sizeof(remote->copy1.path) - 1] = 0;
278 hammer2_voldata_lock(hmp);
279 if (copyid < 0) {
280 for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
281 if (hmp->voldata.copyinfo[copyid].copyid == 0)
282 continue;
283 if (strcmp(remote->copy1.path,
284 hmp->voldata.copyinfo[copyid].path) == 0) {
285 break;
286 }
287 }
288 if (copyid == HAMMER2_COPYID_COUNT) {
289 error = ENOENT;
290 goto failed;
291 }
292 }
50456506 293 hammer2_voldata_modify(hmp);
2910a90c 294 hmp->voldata.copyinfo[copyid].copyid = 0;
063639ba 295 hammer2_volconf_update(hmp, copyid);
2910a90c 296failed:
50456506 297 hammer2_voldata_unlock(hmp);
2910a90c
MD
298 return (error);
299}
300
301/*
302 * Replace existing remote entry
303 */
304static int
ae183399 305hammer2_ioctl_remote_rep(hammer2_inode_t *ip, void *data)
2910a90c 306{
2910a90c 307 hammer2_ioc_remote_t *remote = data;
a5913bdf 308 hammer2_mount_t *hmp;
2910a90c
MD
309 int copyid = remote->copyid;
310
58e43599 311 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
a5913bdf 312
2910a90c
MD
313 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
314 return (EINVAL);
315
316 hammer2_voldata_lock(hmp);
50456506 317 hammer2_voldata_modify(hmp);
063639ba 318 /*hammer2_volconf_update(hmp, copyid);*/
50456506 319 hammer2_voldata_unlock(hmp);
2910a90c
MD
320
321 return(0);
322}
323
324/*
325 * Retrieve communications socket
326 */
327static int
ae183399 328hammer2_ioctl_socket_get(hammer2_inode_t *ip, void *data)
2910a90c
MD
329{
330 return (EOPNOTSUPP);
331}
332
333/*
334 * Set communications socket for connection
335 */
336static int
ae183399 337hammer2_ioctl_socket_set(hammer2_inode_t *ip, void *data)
2910a90c 338{
2910a90c 339 hammer2_ioc_remote_t *remote = data;
a5913bdf 340 hammer2_mount_t *hmp;
2910a90c
MD
341 int copyid = remote->copyid;
342
58e43599 343 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
2910a90c
MD
344 if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT)
345 return (EINVAL);
346
347 hammer2_voldata_lock(hmp);
50456506 348 hammer2_voldata_unlock(hmp);
2910a90c
MD
349
350 return(0);
351}
ae183399
MD
352
353/*
a864c5d9
MD
354 * Used to scan and retrieve PFS information. PFS's are directories under
355 * the super-root.
356 *
357 * To scan PFSs pass name_key=0. The function will scan for the next
358 * PFS and set all fields, as well as set name_next to the next key.
359 * When no PFSs remain, name_next is set to (hammer2_key_t)-1.
360 *
361 * To retrieve the PFS associated with the file descriptor, pass
362 * name_key set to (hammer2_key_t)-1.
ae183399
MD
363 */
364static int
365hammer2_ioctl_pfs_get(hammer2_inode_t *ip, void *data)
366{
bca9f8e6 367 const hammer2_inode_data_t *ripdata;
476d2aad
MD
368 hammer2_mount_t *hmp;
369 hammer2_ioc_pfs_t *pfs;
278ab2b2
MD
370 hammer2_cluster_t *cparent;
371 hammer2_cluster_t *rcluster;
372 hammer2_cluster_t *cluster;
1897c66e 373 hammer2_key_t key_next;
476d2aad 374 int error;
278ab2b2 375 int ddflag;
ae183399 376
476d2aad 377 error = 0;
58e43599 378 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
476d2aad 379 pfs = data;
50456506 380 cparent = hammer2_inode_lock_ex(hmp->spmp->iroot);
278ab2b2 381 rcluster = hammer2_inode_lock_ex(ip->pmp->iroot);
ae183399
MD
382
383 /*
384 * Search for the first key or specific key. Remember that keys
385 * can be returned in any order.
386 */
387 if (pfs->name_key == 0) {
278ab2b2
MD
388 cluster = hammer2_cluster_lookup(cparent, &key_next,
389 0, (hammer2_key_t)-1,
390 0, &ddflag);
a864c5d9 391 } else if (pfs->name_key == (hammer2_key_t)-1) {
bca9f8e6 392 ripdata = &hammer2_cluster_rdata(rcluster)->ipdata;
278ab2b2 393 cluster = hammer2_cluster_lookup(cparent, &key_next,
bca9f8e6
MD
394 ripdata->name_key,
395 ripdata->name_key,
278ab2b2 396 0, &ddflag);
bca9f8e6 397 ripdata = NULL; /* safety */
ae183399 398 } else {
278ab2b2
MD
399 cluster = hammer2_cluster_lookup(cparent, &key_next,
400 pfs->name_key, pfs->name_key,
401 0, &ddflag);
ae183399 402 }
278ab2b2 403 hammer2_inode_unlock_ex(ip->pmp->iroot, rcluster);
9b6b3df4 404
278ab2b2
MD
405 while (cluster &&
406 hammer2_cluster_type(cluster) != HAMMER2_BREF_TYPE_INODE) {
407 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
408 key_next, (hammer2_key_t)-1,
409 0);
ae183399 410 }
278ab2b2 411 if (cluster) {
ae183399
MD
412 /*
413 * Load the data being returned by the ioctl.
414 */
bca9f8e6
MD
415 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
416 pfs->name_key = ripdata->name_key;
417 pfs->pfs_type = ripdata->pfs_type;
418 pfs->pfs_clid = ripdata->pfs_clid;
419 pfs->pfs_fsid = ripdata->pfs_fsid;
420 KKASSERT(ripdata->name_len < sizeof(pfs->name));
421 bcopy(ripdata->filename, pfs->name, ripdata->name_len);
422 pfs->name[ripdata->name_len] = 0;
423 ripdata = NULL; /* safety */
ae183399
MD
424
425 /*
426 * Calculate the next field
427 */
428 do {
278ab2b2
MD
429 cluster = hammer2_cluster_next(cparent, cluster,
430 &key_next,
431 0, (hammer2_key_t)-1,
432 0);
433 } while (cluster &&
434 hammer2_cluster_type(cluster) !=
435 HAMMER2_BREF_TYPE_INODE);
436 if (cluster) {
bca9f8e6
MD
437 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
438 pfs->name_next = ripdata->name_key;
278ab2b2 439 hammer2_cluster_unlock(cluster);
ae183399
MD
440 } else {
441 pfs->name_next = (hammer2_key_t)-1;
442 }
443 } else {
444 pfs->name_next = (hammer2_key_t)-1;
445 error = ENOENT;
446 }
50456506 447 hammer2_inode_unlock_ex(hmp->spmp->iroot, cparent);
0dea3156 448
ae183399
MD
449 return (error);
450}
451
458ecb1b
MD
452/*
453 * Find a specific PFS by name
454 */
455static int
456hammer2_ioctl_pfs_lookup(hammer2_inode_t *ip, void *data)
457{
bca9f8e6 458 const hammer2_inode_data_t *ripdata;
476d2aad
MD
459 hammer2_mount_t *hmp;
460 hammer2_ioc_pfs_t *pfs;
278ab2b2
MD
461 hammer2_cluster_t *cparent;
462 hammer2_cluster_t *cluster;
1897c66e 463 hammer2_key_t key_next;
458ecb1b 464 hammer2_key_t lhc;
476d2aad 465 int error;
278ab2b2 466 int ddflag;
458ecb1b
MD
467 size_t len;
468
476d2aad 469 error = 0;
58e43599 470 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
476d2aad 471 pfs = data;
50456506 472 cparent = hammer2_inode_lock_sh(hmp->spmp->iroot);
458ecb1b
MD
473
474 pfs->name[sizeof(pfs->name) - 1] = 0;
475 len = strlen(pfs->name);
476 lhc = hammer2_dirhash(pfs->name, len);
477
278ab2b2
MD
478 cluster = hammer2_cluster_lookup(cparent, &key_next,
479 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
480 HAMMER2_LOOKUP_SHARED, &ddflag);
481 while (cluster) {
482 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE) {
bca9f8e6
MD
483 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
484 if (ripdata->name_len == len &&
485 bcmp(ripdata->filename, pfs->name, len) == 0) {
278ab2b2
MD
486 break;
487 }
bca9f8e6 488 ripdata = NULL; /* safety */
458ecb1b 489 }
278ab2b2 490 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
1897c66e
MD
491 key_next,
492 lhc + HAMMER2_DIRHASH_LOMASK,
278ab2b2 493 HAMMER2_LOOKUP_SHARED);
458ecb1b
MD
494 }
495
496 /*
497 * Load the data being returned by the ioctl.
498 */
278ab2b2 499 if (cluster) {
bca9f8e6
MD
500 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
501 pfs->name_key = ripdata->name_key;
502 pfs->pfs_type = ripdata->pfs_type;
503 pfs->pfs_clid = ripdata->pfs_clid;
504 pfs->pfs_fsid = ripdata->pfs_fsid;
505 ripdata = NULL;
458ecb1b 506
278ab2b2 507 hammer2_cluster_unlock(cluster);
458ecb1b
MD
508 } else {
509 error = ENOENT;
510 }
50456506 511 hammer2_inode_unlock_sh(hmp->spmp->iroot, cparent);
7bed8d7e 512
458ecb1b
MD
513 return (error);
514}
515
ae183399
MD
516/*
517 * Create a new PFS under the super-root
518 */
519static int
520hammer2_ioctl_pfs_create(hammer2_inode_t *ip, void *data)
521{
476d2aad
MD
522 hammer2_inode_data_t *nipdata;
523 hammer2_mount_t *hmp;
524 hammer2_ioc_pfs_t *pfs;
525 hammer2_inode_t *nip;
278ab2b2 526 hammer2_cluster_t *ncluster;
0dea3156 527 hammer2_trans_t trans;
ae183399
MD
528 int error;
529
58e43599 530 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
476d2aad
MD
531 pfs = data;
532 nip = NULL;
533
a864c5d9
MD
534 if (pfs->name[0] == 0)
535 return(EINVAL);
ae183399 536 pfs->name[sizeof(pfs->name) - 1] = 0; /* ensure 0-termination */
10252dc7 537
50456506
MD
538 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_NEWINODE);
539 nip = hammer2_inode_create(&trans, hmp->spmp->iroot, NULL, NULL,
ae183399 540 pfs->name, strlen(pfs->name),
278ab2b2 541 &ncluster, &error);
ae183399 542 if (error == 0) {
850687d2 543 nipdata = hammer2_cluster_modify_ip(&trans, nip, ncluster, 0);
476d2aad
MD
544 nipdata->pfs_type = pfs->pfs_type;
545 nipdata->pfs_clid = pfs->pfs_clid;
546 nipdata->pfs_fsid = pfs->pfs_fsid;
cdfd0a3e
MD
547
548 /*
549 * Do not allow compression on PFS's with the special name
550 * "boot", the boot loader can't decompress (yet).
551 */
552 if (strcmp(pfs->name, "boot") == 0)
e07becf8
MD
553 nipdata->comp_algo = HAMMER2_ENC_ALGO(
554 HAMMER2_COMP_AUTOZERO);
6a5f4fe6 555 hammer2_cluster_modsync(ncluster);
278ab2b2 556 hammer2_inode_unlock_ex(nip, ncluster);
ae183399 557 }
0dea3156 558 hammer2_trans_done(&trans);
278ab2b2 559
ae183399
MD
560 return (error);
561}
562
563/*
564 * Destroy an existing PFS under the super-root
565 */
566static int
567hammer2_ioctl_pfs_delete(hammer2_inode_t *ip, void *data)
568{
a5913bdf 569 hammer2_mount_t *hmp;
ae183399 570 hammer2_ioc_pfs_t *pfs = data;
0dea3156 571 hammer2_trans_t trans;
ae183399
MD
572 int error;
573
58e43599 574 hmp = ip->pmp->iroot->cluster.focus->hmp; /* XXX */
50456506
MD
575 hammer2_trans_init(&trans, ip->pmp, 0);
576 error = hammer2_unlink_file(&trans, hmp->spmp->iroot,
9797e933 577 pfs->name, strlen(pfs->name),
da6f36f4 578 2, NULL, NULL, -1);
a864c5d9
MD
579 hammer2_trans_done(&trans);
580
581 return (error);
582}
583
584static int
585hammer2_ioctl_pfs_snapshot(hammer2_inode_t *ip, void *data)
586{
a864c5d9
MD
587 hammer2_ioc_pfs_t *pfs = data;
588 hammer2_trans_t trans;
278ab2b2 589 hammer2_cluster_t *cparent;
a864c5d9
MD
590 int error;
591
592 if (pfs->name[0] == 0)
593 return(EINVAL);
594 if (pfs->name[sizeof(pfs->name)-1] != 0)
595 return(EINVAL);
596
a7720be7
MD
597 hammer2_vfs_sync(ip->pmp->mp, MNT_WAIT);
598
18e8ab5f
MD
599 hammer2_trans_init(&trans, ip->pmp,
600 HAMMER2_TRANS_ISFLUSH | HAMMER2_TRANS_NEWINODE);
278ab2b2
MD
601 cparent = hammer2_inode_lock_ex(ip);
602 error = hammer2_cluster_snapshot(&trans, cparent, pfs);
603 hammer2_inode_unlock_ex(ip, cparent);
0dea3156
MD
604 hammer2_trans_done(&trans);
605
ae183399
MD
606 return (error);
607}
344d4f82
MD
608
609/*
610 * Retrieve the raw inode structure
611 */
612static int
613hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data)
614{
bca9f8e6 615 const hammer2_inode_data_t *ripdata;
6a5f4fe6 616 hammer2_ioc_inode_t *ino;
278ab2b2 617 hammer2_cluster_t *cparent;
344d4f82 618
6a5f4fe6
MD
619 ino = data;
620
278ab2b2 621 cparent = hammer2_inode_lock_sh(ip);
bca9f8e6
MD
622 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
623 ino->ip_data = *ripdata;
26b047fa 624 ino->kdata = ip;
278ab2b2 625 hammer2_inode_unlock_sh(ip, cparent);
0dea3156 626
344d4f82
MD
627 return (0);
628}
629
f481450f
MD
630/*
631 * Set various parameters in an inode which cannot be set through
632 * normal filesystem VNOPS.
633 */
344d4f82
MD
634static int
635hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data)
636{
6a5f4fe6
MD
637 const hammer2_inode_data_t *ripdata;
638 hammer2_inode_data_t *wipdata;
344d4f82 639 hammer2_ioc_inode_t *ino = data;
278ab2b2 640 hammer2_cluster_t *cparent;
355d67fc 641 hammer2_trans_t trans;
f481450f 642 int error = 0;
6a5f4fe6 643 int dosync = 0;
344d4f82 644
50456506 645 hammer2_trans_init(&trans, ip->pmp, 0);
278ab2b2 646 cparent = hammer2_inode_lock_ex(ip);
bca9f8e6 647 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
f481450f 648
e07becf8
MD
649 if (ino->ip_data.check_algo != ripdata->check_algo) {
650 wipdata = hammer2_cluster_modify_ip(&trans, ip, cparent, 0);
651 wipdata->check_algo = ino->ip_data.check_algo;
652 ripdata = wipdata; /* safety */
653 hammer2_cluster_setmethod_check(&trans, cparent,
654 wipdata->check_algo);
655 dosync = 1;
656 }
6a5f4fe6
MD
657 if (ino->ip_data.comp_algo != ripdata->comp_algo) {
658 wipdata = hammer2_cluster_modify_ip(&trans, ip, cparent, 0);
659 wipdata->comp_algo = ino->ip_data.comp_algo;
660 ripdata = wipdata; /* safety */
661 dosync = 1;
f481450f 662 }
355d67fc
MD
663 ino->kdata = ip;
664
f481450f 665 /* Ignore these flags for now...*/
344d4f82
MD
666 if (ino->flags & HAMMER2IOC_INODE_FLAG_IQUOTA) {
667 }
668 if (ino->flags & HAMMER2IOC_INODE_FLAG_DQUOTA) {
669 }
670 if (ino->flags & HAMMER2IOC_INODE_FLAG_COPIES) {
671 }
6a5f4fe6
MD
672 if (dosync)
673 hammer2_cluster_modsync(cparent);
355d67fc 674 hammer2_trans_done(&trans);
278ab2b2 675 hammer2_inode_unlock_ex(ip, cparent);
0dea3156 676
344d4f82
MD
677 return (error);
678}
03188ed8
MD
679
680static
681int
682hammer2_ioctl_debug_dump(hammer2_inode_t *ip)
683{
684 hammer2_chain_t *chain;
685 int count = 1000;
686 int i;
687
688 for (i = 0; i < ip->cluster.nchains; ++i) {
689 chain = ip->cluster.array[i];
690 if (chain == NULL)
691 continue;
692 hammer2_dump_chain(chain, 0, &count, 'i');
693 }
694 return 0;
695}