]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_vfsops.c
Remove Solaris VFS Hooks
[mirror_zfs.git] / module / zfs / zfs_vfsops.c
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 */
24
25 /* Portions Copyright 2010 Robert Milkowski */
26
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/sysmacros.h>
31 #include <sys/kmem.h>
32 #include <sys/pathname.h>
33 #include <sys/vnode.h>
34 #include <sys/vfs.h>
35 #include <sys/vfs_opreg.h>
36 #include <sys/mntent.h>
37 #include <sys/mount.h>
38 #include <sys/cmn_err.h>
39 #include "fs/fs_subr.h"
40 #include <sys/zfs_znode.h>
41 #include <sys/zfs_dir.h>
42 #include <sys/zil.h>
43 #include <sys/fs/zfs.h>
44 #include <sys/dmu.h>
45 #include <sys/dsl_prop.h>
46 #include <sys/dsl_dataset.h>
47 #include <sys/dsl_deleg.h>
48 #include <sys/spa.h>
49 #include <sys/zap.h>
50 #include <sys/sa.h>
51 #include <sys/varargs.h>
52 #include <sys/policy.h>
53 #include <sys/atomic.h>
54 #include <sys/mkdev.h>
55 #include <sys/modctl.h>
56 #include <sys/refstr.h>
57 #include <sys/zfs_ioctl.h>
58 #include <sys/zfs_ctldir.h>
59 #include <sys/zfs_fuid.h>
60 #include <sys/bootconf.h>
61 #include <sys/sunddi.h>
62 #include <sys/dnlc.h>
63 #include <sys/dmu_objset.h>
64 #include <sys/spa_boot.h>
65 #include <sys/sa.h>
66 #include "zfs_comutil.h"
67
68 #ifdef HAVE_ZPL
69 int zfsfstype;
70 static major_t zfs_major;
71 static minor_t zfs_minor;
72 static kmutex_t zfs_dev_mtx;
73
74 extern int sys_shutdown;
75
76 static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
77 static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
78 static void zfs_freevfs(vfs_t *vfsp);
79
80 /*
81 * We need to keep a count of active fs's.
82 * This is necessary to prevent our module
83 * from being unloaded after a umount -f
84 */
85 static uint32_t zfs_active_fs_count = 0;
86
87 static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
88 static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
89 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
90 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
91
92 /*
93 * MO_DEFAULT is not used since the default value is determined
94 * by the equivalent property.
95 */
96 static mntopt_t mntopts[] = {
97 { MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
98 { MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
99 { MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
100 { MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
101 };
102
103 static mntopts_t zfs_mntopts = {
104 sizeof (mntopts) / sizeof (mntopt_t),
105 mntopts
106 };
107
108 /*ARGSUSED*/
109 int
110 zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
111 {
112 /*
113 * Data integrity is job one. We don't want a compromised kernel
114 * writing to the storage pool, so we never sync during panic.
115 */
116 if (panicstr)
117 return (0);
118
119 /*
120 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
121 * to sync metadata, which they would otherwise cache indefinitely.
122 * Semantically, the only requirement is that the sync be initiated.
123 * The DMU syncs out txgs frequently, so there's nothing to do.
124 */
125 if (flag & SYNC_ATTR)
126 return (0);
127
128 if (vfsp != NULL) {
129 /*
130 * Sync a specific filesystem.
131 */
132 zfsvfs_t *zfsvfs = vfsp->vfs_data;
133 dsl_pool_t *dp;
134
135 ZFS_ENTER(zfsvfs);
136 dp = dmu_objset_pool(zfsvfs->z_os);
137
138 /*
139 * If the system is shutting down, then skip any
140 * filesystems which may exist on a suspended pool.
141 */
142 if (sys_shutdown && spa_suspended(dp->dp_spa)) {
143 ZFS_EXIT(zfsvfs);
144 return (0);
145 }
146
147 if (zfsvfs->z_log != NULL)
148 zil_commit(zfsvfs->z_log, 0);
149
150 ZFS_EXIT(zfsvfs);
151 } else {
152 /*
153 * Sync all ZFS filesystems. This is what happens when you
154 * run sync(1M). Unlike other filesystems, ZFS honors the
155 * request by waiting for all pools to commit all dirty data.
156 */
157 spa_sync_allpools();
158 }
159
160 return (0);
161 }
162 EXPORT_SYMBOL(zfs_sync);
163
164 static int
165 zfs_create_unique_device(dev_t *dev)
166 {
167 major_t new_major;
168
169 do {
170 ASSERT3U(zfs_minor, <=, MAXMIN32);
171 minor_t start = zfs_minor;
172 do {
173 mutex_enter(&zfs_dev_mtx);
174 if (zfs_minor >= MAXMIN32) {
175 /*
176 * If we're still using the real major
177 * keep out of /dev/zfs and /dev/zvol minor
178 * number space. If we're using a getudev()'ed
179 * major number, we can use all of its minors.
180 */
181 if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
182 zfs_minor = ZFS_MIN_MINOR;
183 else
184 zfs_minor = 0;
185 } else {
186 zfs_minor++;
187 }
188 *dev = makedevice(zfs_major, zfs_minor);
189 mutex_exit(&zfs_dev_mtx);
190 } while (vfs_devismounted(*dev) && zfs_minor != start);
191 if (zfs_minor == start) {
192 /*
193 * We are using all ~262,000 minor numbers for the
194 * current major number. Create a new major number.
195 */
196 if ((new_major = getudev()) == (major_t)-1) {
197 cmn_err(CE_WARN,
198 "zfs_mount: Can't get unique major "
199 "device number.");
200 return (-1);
201 }
202 mutex_enter(&zfs_dev_mtx);
203 zfs_major = new_major;
204 zfs_minor = 0;
205
206 mutex_exit(&zfs_dev_mtx);
207 } else {
208 break;
209 }
210 /* CONSTANTCONDITION */
211 } while (1);
212
213 return (0);
214 }
215
216 static void
217 atime_changed_cb(void *arg, uint64_t newval)
218 {
219 zfsvfs_t *zfsvfs = arg;
220
221 if (newval == TRUE) {
222 zfsvfs->z_atime = TRUE;
223 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
224 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
225 } else {
226 zfsvfs->z_atime = FALSE;
227 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
228 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
229 }
230 }
231
232 static void
233 xattr_changed_cb(void *arg, uint64_t newval)
234 {
235 zfsvfs_t *zfsvfs = arg;
236
237 if (newval == TRUE) {
238 /* XXX locking on vfs_flag? */
239 zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
240 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
241 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
242 } else {
243 /* XXX locking on vfs_flag? */
244 zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
245 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
246 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
247 }
248 }
249
250 static void
251 blksz_changed_cb(void *arg, uint64_t newval)
252 {
253 zfsvfs_t *zfsvfs = arg;
254
255 if (newval < SPA_MINBLOCKSIZE ||
256 newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
257 newval = SPA_MAXBLOCKSIZE;
258
259 zfsvfs->z_max_blksz = newval;
260 zfsvfs->z_vfs->vfs_bsize = newval;
261 }
262
263 static void
264 readonly_changed_cb(void *arg, uint64_t newval)
265 {
266 zfsvfs_t *zfsvfs = arg;
267
268 if (newval) {
269 /* XXX locking on vfs_flag? */
270 zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
271 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
272 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
273 } else {
274 /* XXX locking on vfs_flag? */
275 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
276 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
277 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
278 }
279 }
280
281 static void
282 devices_changed_cb(void *arg, uint64_t newval)
283 {
284 zfsvfs_t *zfsvfs = arg;
285
286 if (newval == FALSE) {
287 zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
288 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
289 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
290 } else {
291 zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
292 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
293 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
294 }
295 }
296
297 static void
298 setuid_changed_cb(void *arg, uint64_t newval)
299 {
300 zfsvfs_t *zfsvfs = arg;
301
302 if (newval == FALSE) {
303 zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
304 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
305 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
306 } else {
307 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
308 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
309 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
310 }
311 }
312
313 static void
314 exec_changed_cb(void *arg, uint64_t newval)
315 {
316 zfsvfs_t *zfsvfs = arg;
317
318 if (newval == FALSE) {
319 zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
320 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
321 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
322 } else {
323 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
324 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
325 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
326 }
327 }
328
329 /*
330 * The nbmand mount option can be changed at mount time.
331 * We can't allow it to be toggled on live file systems or incorrect
332 * behavior may be seen from cifs clients
333 *
334 * This property isn't registered via dsl_prop_register(), but this callback
335 * will be called when a file system is first mounted
336 */
337 static void
338 nbmand_changed_cb(void *arg, uint64_t newval)
339 {
340 zfsvfs_t *zfsvfs = arg;
341 if (newval == FALSE) {
342 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
343 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
344 } else {
345 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
346 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
347 }
348 }
349
350 static void
351 snapdir_changed_cb(void *arg, uint64_t newval)
352 {
353 zfsvfs_t *zfsvfs = arg;
354
355 zfsvfs->z_show_ctldir = newval;
356 }
357
358 static void
359 vscan_changed_cb(void *arg, uint64_t newval)
360 {
361 zfsvfs_t *zfsvfs = arg;
362
363 zfsvfs->z_vscan = newval;
364 }
365
366 static void
367 acl_inherit_changed_cb(void *arg, uint64_t newval)
368 {
369 zfsvfs_t *zfsvfs = arg;
370
371 zfsvfs->z_acl_inherit = newval;
372 }
373
374 int
375 zfs_register_callbacks(vfs_t *vfsp)
376 {
377 struct dsl_dataset *ds = NULL;
378 objset_t *os = NULL;
379 zfsvfs_t *zfsvfs = NULL;
380 uint64_t nbmand;
381 int readonly, do_readonly = B_FALSE;
382 int setuid, do_setuid = B_FALSE;
383 int exec, do_exec = B_FALSE;
384 int devices, do_devices = B_FALSE;
385 int xattr, do_xattr = B_FALSE;
386 int atime, do_atime = B_FALSE;
387 int error = 0;
388
389 ASSERT(vfsp);
390 zfsvfs = vfsp->vfs_data;
391 ASSERT(zfsvfs);
392 os = zfsvfs->z_os;
393
394 /*
395 * The act of registering our callbacks will destroy any mount
396 * options we may have. In order to enable temporary overrides
397 * of mount options, we stash away the current values and
398 * restore them after we register the callbacks.
399 */
400 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
401 !spa_writeable(dmu_objset_spa(os))) {
402 readonly = B_TRUE;
403 do_readonly = B_TRUE;
404 } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
405 readonly = B_FALSE;
406 do_readonly = B_TRUE;
407 }
408 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
409 devices = B_FALSE;
410 setuid = B_FALSE;
411 do_devices = B_TRUE;
412 do_setuid = B_TRUE;
413 } else {
414 if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
415 devices = B_FALSE;
416 do_devices = B_TRUE;
417 } else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
418 devices = B_TRUE;
419 do_devices = B_TRUE;
420 }
421
422 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
423 setuid = B_FALSE;
424 do_setuid = B_TRUE;
425 } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
426 setuid = B_TRUE;
427 do_setuid = B_TRUE;
428 }
429 }
430 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
431 exec = B_FALSE;
432 do_exec = B_TRUE;
433 } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
434 exec = B_TRUE;
435 do_exec = B_TRUE;
436 }
437 if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
438 xattr = B_FALSE;
439 do_xattr = B_TRUE;
440 } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
441 xattr = B_TRUE;
442 do_xattr = B_TRUE;
443 }
444 if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
445 atime = B_FALSE;
446 do_atime = B_TRUE;
447 } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
448 atime = B_TRUE;
449 do_atime = B_TRUE;
450 }
451
452 /*
453 * nbmand is a special property. It can only be changed at
454 * mount time.
455 *
456 * This is weird, but it is documented to only be changeable
457 * at mount time.
458 */
459 if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
460 nbmand = B_FALSE;
461 } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
462 nbmand = B_TRUE;
463 } else {
464 char osname[MAXNAMELEN];
465
466 dmu_objset_name(os, osname);
467 if ((error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
468 NULL))) {
469 return (error);
470 }
471 }
472
473 /*
474 * Register property callbacks.
475 *
476 * It would probably be fine to just check for i/o error from
477 * the first prop_register(), but I guess I like to go
478 * overboard...
479 */
480 ds = dmu_objset_ds(os);
481 error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
482 error = error ? error : dsl_prop_register(ds,
483 "xattr", xattr_changed_cb, zfsvfs);
484 error = error ? error : dsl_prop_register(ds,
485 "recordsize", blksz_changed_cb, zfsvfs);
486 error = error ? error : dsl_prop_register(ds,
487 "readonly", readonly_changed_cb, zfsvfs);
488 error = error ? error : dsl_prop_register(ds,
489 "devices", devices_changed_cb, zfsvfs);
490 error = error ? error : dsl_prop_register(ds,
491 "setuid", setuid_changed_cb, zfsvfs);
492 error = error ? error : dsl_prop_register(ds,
493 "exec", exec_changed_cb, zfsvfs);
494 error = error ? error : dsl_prop_register(ds,
495 "snapdir", snapdir_changed_cb, zfsvfs);
496 error = error ? error : dsl_prop_register(ds,
497 "aclinherit", acl_inherit_changed_cb, zfsvfs);
498 error = error ? error : dsl_prop_register(ds,
499 "vscan", vscan_changed_cb, zfsvfs);
500 if (error)
501 goto unregister;
502
503 /*
504 * Invoke our callbacks to restore temporary mount options.
505 */
506 if (do_readonly)
507 readonly_changed_cb(zfsvfs, readonly);
508 if (do_setuid)
509 setuid_changed_cb(zfsvfs, setuid);
510 if (do_exec)
511 exec_changed_cb(zfsvfs, exec);
512 if (do_devices)
513 devices_changed_cb(zfsvfs, devices);
514 if (do_xattr)
515 xattr_changed_cb(zfsvfs, xattr);
516 if (do_atime)
517 atime_changed_cb(zfsvfs, atime);
518
519 nbmand_changed_cb(zfsvfs, nbmand);
520
521 return (0);
522
523 unregister:
524 /*
525 * We may attempt to unregister some callbacks that are not
526 * registered, but this is OK; it will simply return ENOMSG,
527 * which we will ignore.
528 */
529 (void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
530 (void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
531 (void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
532 (void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
533 (void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
534 (void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
535 (void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
536 (void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
537 (void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
538 zfsvfs);
539 (void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
540 return (error);
541
542 }
543 EXPORT_SYMBOL(zfs_register_callbacks);
544 #endif /* HAVE_ZPL */
545
546 static int
547 zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
548 uint64_t *userp, uint64_t *groupp)
549 {
550 znode_phys_t *znp = data;
551 int error = 0;
552
553 /*
554 * Is it a valid type of object to track?
555 */
556 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
557 return (ENOENT);
558
559 /*
560 * If we have a NULL data pointer
561 * then assume the id's aren't changing and
562 * return EEXIST to the dmu to let it know to
563 * use the same ids
564 */
565 if (data == NULL)
566 return (EEXIST);
567
568 if (bonustype == DMU_OT_ZNODE) {
569 *userp = znp->zp_uid;
570 *groupp = znp->zp_gid;
571 } else {
572 int hdrsize;
573
574 ASSERT(bonustype == DMU_OT_SA);
575 hdrsize = sa_hdrsize(data);
576
577 if (hdrsize != 0) {
578 *userp = *((uint64_t *)((uintptr_t)data + hdrsize +
579 SA_UID_OFFSET));
580 *groupp = *((uint64_t *)((uintptr_t)data + hdrsize +
581 SA_GID_OFFSET));
582 } else {
583 /*
584 * This should only happen for newly created
585 * files that haven't had the znode data filled
586 * in yet.
587 */
588 *userp = 0;
589 *groupp = 0;
590 }
591 }
592 return (error);
593 }
594
595 #ifdef HAVE_ZPL
596 static void
597 fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
598 char *domainbuf, int buflen, uid_t *ridp)
599 {
600 uint64_t fuid;
601 const char *domain;
602
603 fuid = strtonum(fuidstr, NULL);
604
605 domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
606 if (domain)
607 (void) strlcpy(domainbuf, domain, buflen);
608 else
609 domainbuf[0] = '\0';
610 *ridp = FUID_RID(fuid);
611 }
612
613 static uint64_t
614 zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
615 {
616 switch (type) {
617 case ZFS_PROP_USERUSED:
618 return (DMU_USERUSED_OBJECT);
619 case ZFS_PROP_GROUPUSED:
620 return (DMU_GROUPUSED_OBJECT);
621 case ZFS_PROP_USERQUOTA:
622 return (zfsvfs->z_userquota_obj);
623 case ZFS_PROP_GROUPQUOTA:
624 return (zfsvfs->z_groupquota_obj);
625 default:
626 return (ENOTSUP);
627 }
628 return (0);
629 }
630
631 int
632 zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
633 uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
634 {
635 int error;
636 zap_cursor_t zc;
637 zap_attribute_t za;
638 zfs_useracct_t *buf = vbuf;
639 uint64_t obj;
640
641 if (!dmu_objset_userspace_present(zfsvfs->z_os))
642 return (ENOTSUP);
643
644 obj = zfs_userquota_prop_to_obj(zfsvfs, type);
645 if (obj == 0) {
646 *bufsizep = 0;
647 return (0);
648 }
649
650 for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
651 (error = zap_cursor_retrieve(&zc, &za)) == 0;
652 zap_cursor_advance(&zc)) {
653 if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
654 *bufsizep)
655 break;
656
657 fuidstr_to_sid(zfsvfs, za.za_name,
658 buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
659
660 buf->zu_space = za.za_first_integer;
661 buf++;
662 }
663 if (error == ENOENT)
664 error = 0;
665
666 ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
667 *bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
668 *cookiep = zap_cursor_serialize(&zc);
669 zap_cursor_fini(&zc);
670 return (error);
671 }
672 EXPORT_SYMBOL(zfs_userspace_many);
673
674 /*
675 * buf must be big enough (eg, 32 bytes)
676 */
677 static int
678 id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
679 char *buf, boolean_t addok)
680 {
681 uint64_t fuid;
682 int domainid = 0;
683
684 if (domain && domain[0]) {
685 domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
686 if (domainid == -1)
687 return (ENOENT);
688 }
689 fuid = FUID_ENCODE(domainid, rid);
690 (void) sprintf(buf, "%llx", (longlong_t)fuid);
691 return (0);
692 }
693
694 int
695 zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
696 const char *domain, uint64_t rid, uint64_t *valp)
697 {
698 char buf[32];
699 int err;
700 uint64_t obj;
701
702 *valp = 0;
703
704 if (!dmu_objset_userspace_present(zfsvfs->z_os))
705 return (ENOTSUP);
706
707 obj = zfs_userquota_prop_to_obj(zfsvfs, type);
708 if (obj == 0)
709 return (0);
710
711 err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_FALSE);
712 if (err)
713 return (err);
714
715 err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
716 if (err == ENOENT)
717 err = 0;
718 return (err);
719 }
720 EXPORT_SYMBOL(zfs_userspace_one);
721
722 int
723 zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
724 const char *domain, uint64_t rid, uint64_t quota)
725 {
726 char buf[32];
727 int err;
728 dmu_tx_t *tx;
729 uint64_t *objp;
730 boolean_t fuid_dirtied;
731
732 if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
733 return (EINVAL);
734
735 if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
736 return (ENOTSUP);
737
738 objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj :
739 &zfsvfs->z_groupquota_obj;
740
741 err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
742 if (err)
743 return (err);
744 fuid_dirtied = zfsvfs->z_fuid_dirty;
745
746 tx = dmu_tx_create(zfsvfs->z_os);
747 dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
748 if (*objp == 0) {
749 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
750 zfs_userquota_prop_prefixes[type]);
751 }
752 if (fuid_dirtied)
753 zfs_fuid_txhold(zfsvfs, tx);
754 err = dmu_tx_assign(tx, TXG_WAIT);
755 if (err) {
756 dmu_tx_abort(tx);
757 return (err);
758 }
759
760 mutex_enter(&zfsvfs->z_lock);
761 if (*objp == 0) {
762 *objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
763 DMU_OT_NONE, 0, tx);
764 VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
765 zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
766 }
767 mutex_exit(&zfsvfs->z_lock);
768
769 if (quota == 0) {
770 err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
771 if (err == ENOENT)
772 err = 0;
773 } else {
774 err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, &quota, tx);
775 }
776 ASSERT(err == 0);
777 if (fuid_dirtied)
778 zfs_fuid_sync(zfsvfs, tx);
779 dmu_tx_commit(tx);
780 return (err);
781 }
782 EXPORT_SYMBOL(zfs_set_userquota);
783
784 boolean_t
785 zfs_fuid_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid)
786 {
787 char buf[32];
788 uint64_t used, quota, usedobj, quotaobj;
789 int err;
790
791 usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
792 quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
793
794 if (quotaobj == 0 || zfsvfs->z_replay)
795 return (B_FALSE);
796
797 (void) sprintf(buf, "%llx", (longlong_t)fuid);
798 err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
799 if (err != 0)
800 return (B_FALSE);
801
802 err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
803 if (err != 0)
804 return (B_FALSE);
805 return (used >= quota);
806 }
807 EXPORT_SYMBOL(zfs_fuid_overquota);
808
809 boolean_t
810 zfs_owner_overquota(zfsvfs_t *zfsvfs, znode_t *zp, boolean_t isgroup)
811 {
812 uint64_t fuid;
813 uint64_t quotaobj;
814
815 quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
816
817 fuid = isgroup ? zp->z_gid : zp->z_uid;
818
819 if (quotaobj == 0 || zfsvfs->z_replay)
820 return (B_FALSE);
821
822 return (zfs_fuid_overquota(zfsvfs, isgroup, fuid));
823 }
824 EXPORT_SYMBOL(zfs_owner_overquota);
825
826 int
827 zfsvfs_create(const char *osname, zfsvfs_t **zfvp)
828 {
829 objset_t *os;
830 zfsvfs_t *zfsvfs;
831 uint64_t zval;
832 int i, error;
833 uint64_t sa_obj;
834
835 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
836
837 /*
838 * We claim to always be readonly so we can open snapshots;
839 * other ZPL code will prevent us from writing to snapshots.
840 */
841 error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os);
842 if (error) {
843 kmem_free(zfsvfs, sizeof (zfsvfs_t));
844 return (error);
845 }
846
847 /*
848 * Initialize the zfs-specific filesystem structure.
849 * Should probably make this a kmem cache, shuffle fields,
850 * and just bzero up to z_hold_mtx[].
851 */
852 zfsvfs->z_vfs = NULL;
853 zfsvfs->z_parent = zfsvfs;
854 zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
855 zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
856 zfsvfs->z_os = os;
857
858 error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
859 if (error) {
860 goto out;
861 } else if (zfsvfs->z_version >
862 zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
863 (void) printk("Can't mount a version %lld file system "
864 "on a version %lld pool\n. Pool must be upgraded to mount "
865 "this file system.", (u_longlong_t)zfsvfs->z_version,
866 (u_longlong_t)spa_version(dmu_objset_spa(os)));
867 error = ENOTSUP;
868 goto out;
869 }
870 if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
871 goto out;
872 zfsvfs->z_norm = (int)zval;
873
874 if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
875 goto out;
876 zfsvfs->z_utf8 = (zval != 0);
877
878 if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
879 goto out;
880 zfsvfs->z_case = (uint_t)zval;
881
882 /*
883 * Fold case on file systems that are always or sometimes case
884 * insensitive.
885 */
886 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
887 zfsvfs->z_case == ZFS_CASE_MIXED)
888 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
889
890 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
891 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
892
893 if (zfsvfs->z_use_sa) {
894 /* should either have both of these objects or none */
895 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
896 &sa_obj);
897 if (error)
898 return (error);
899 } else {
900 /*
901 * Pre SA versions file systems should never touch
902 * either the attribute registration or layout objects.
903 */
904 sa_obj = 0;
905 }
906
907 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
908 &zfsvfs->z_attr_table);
909 if (error)
910 goto out;
911
912 if (zfsvfs->z_version >= ZPL_VERSION_SA)
913 sa_register_update_callback(os, zfs_sa_upgrade);
914
915 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
916 &zfsvfs->z_root);
917 if (error)
918 goto out;
919 ASSERT(zfsvfs->z_root != 0);
920
921 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
922 &zfsvfs->z_unlinkedobj);
923 if (error)
924 goto out;
925
926 error = zap_lookup(os, MASTER_NODE_OBJ,
927 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
928 8, 1, &zfsvfs->z_userquota_obj);
929 if (error && error != ENOENT)
930 goto out;
931
932 error = zap_lookup(os, MASTER_NODE_OBJ,
933 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
934 8, 1, &zfsvfs->z_groupquota_obj);
935 if (error && error != ENOENT)
936 goto out;
937
938 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
939 &zfsvfs->z_fuid_obj);
940 if (error && error != ENOENT)
941 goto out;
942
943 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
944 &zfsvfs->z_shares_dir);
945 if (error && error != ENOENT)
946 goto out;
947
948 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
949 mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
950 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
951 offsetof(znode_t, z_link_node));
952 rrw_init(&zfsvfs->z_teardown_lock);
953 rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
954 rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
955 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
956 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
957
958 *zfvp = zfsvfs;
959 return (0);
960
961 out:
962 dmu_objset_disown(os, zfsvfs);
963 *zfvp = NULL;
964 kmem_free(zfsvfs, sizeof (zfsvfs_t));
965 return (error);
966 }
967
968 static int
969 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
970 {
971 int error;
972
973 error = zfs_register_callbacks(zfsvfs->z_vfs);
974 if (error)
975 return (error);
976
977 /*
978 * Set the objset user_ptr to track its zfsvfs.
979 */
980 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
981 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
982 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
983
984 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
985
986 /*
987 * If we are not mounting (ie: online recv), then we don't
988 * have to worry about replaying the log as we blocked all
989 * operations out since we closed the ZIL.
990 */
991 if (mounting) {
992 boolean_t readonly;
993
994 /*
995 * During replay we remove the read only flag to
996 * allow replays to succeed.
997 */
998 readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
999 if (readonly != 0)
1000 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
1001 else
1002 zfs_unlinked_drain(zfsvfs);
1003
1004 /*
1005 * Parse and replay the intent log.
1006 *
1007 * Because of ziltest, this must be done after
1008 * zfs_unlinked_drain(). (Further note: ziltest
1009 * doesn't use readonly mounts, where
1010 * zfs_unlinked_drain() isn't called.) This is because
1011 * ziltest causes spa_sync() to think it's committed,
1012 * but actually it is not, so the intent log contains
1013 * many txg's worth of changes.
1014 *
1015 * In particular, if object N is in the unlinked set in
1016 * the last txg to actually sync, then it could be
1017 * actually freed in a later txg and then reallocated
1018 * in a yet later txg. This would write a "create
1019 * object N" record to the intent log. Normally, this
1020 * would be fine because the spa_sync() would have
1021 * written out the fact that object N is free, before
1022 * we could write the "create object N" intent log
1023 * record.
1024 *
1025 * But when we are in ziltest mode, we advance the "open
1026 * txg" without actually spa_sync()-ing the changes to
1027 * disk. So we would see that object N is still
1028 * allocated and in the unlinked set, and there is an
1029 * intent log record saying to allocate it.
1030 */
1031 if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
1032 if (zil_replay_disable) {
1033 zil_destroy(zfsvfs->z_log, B_FALSE);
1034 } else {
1035 zfsvfs->z_replay = B_TRUE;
1036 zil_replay(zfsvfs->z_os, zfsvfs,
1037 zfs_replay_vector);
1038 zfsvfs->z_replay = B_FALSE;
1039 }
1040 }
1041 zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
1042 }
1043
1044 return (0);
1045 }
1046
1047 void
1048 zfsvfs_free(zfsvfs_t *zfsvfs)
1049 {
1050 int i;
1051 extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
1052
1053 /*
1054 * This is a barrier to prevent the filesystem from going away in
1055 * zfs_znode_move() until we can safely ensure that the filesystem is
1056 * not unmounted. We consider the filesystem valid before the barrier
1057 * and invalid after the barrier.
1058 */
1059 rw_enter(&zfsvfs_lock, RW_READER);
1060 rw_exit(&zfsvfs_lock);
1061
1062 zfs_fuid_destroy(zfsvfs);
1063
1064 mutex_destroy(&zfsvfs->z_znodes_lock);
1065 mutex_destroy(&zfsvfs->z_lock);
1066 list_destroy(&zfsvfs->z_all_znodes);
1067 rrw_destroy(&zfsvfs->z_teardown_lock);
1068 rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1069 rw_destroy(&zfsvfs->z_fuid_lock);
1070 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1071 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1072 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1073 }
1074
1075 static void
1076 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
1077 {
1078 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1079 if (zfsvfs->z_use_fuids && zfsvfs->z_vfs) {
1080 vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1081 vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1082 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1083 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1084 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1085 vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1086 }
1087 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1088 }
1089
1090 int
1091 zfs_domount(vfs_t *vfsp, char *osname)
1092 {
1093 dev_t mount_dev;
1094 uint64_t recordsize, fsid_guid;
1095 int error = 0;
1096 zfsvfs_t *zfsvfs;
1097
1098 ASSERT(vfsp);
1099 ASSERT(osname);
1100
1101 error = zfsvfs_create(osname, &zfsvfs);
1102 if (error)
1103 return (error);
1104 zfsvfs->z_vfs = vfsp;
1105
1106 /* Initialize the generic filesystem structure. */
1107 vfsp->vfs_bcount = 0;
1108 vfsp->vfs_data = NULL;
1109
1110 if (zfs_create_unique_device(&mount_dev) == -1) {
1111 error = ENODEV;
1112 goto out;
1113 }
1114 ASSERT(vfs_devismounted(mount_dev) == 0);
1115
1116 if ((error = dsl_prop_get_integer(osname, "recordsize",
1117 &recordsize, NULL)))
1118 goto out;
1119
1120 vfsp->vfs_dev = mount_dev;
1121 vfsp->vfs_fstype = zfsfstype;
1122 vfsp->vfs_bsize = recordsize;
1123 vfsp->vfs_flag |= VFS_NOTRUNC;
1124 vfsp->vfs_data = zfsvfs;
1125
1126 /*
1127 * The fsid is 64 bits, composed of an 8-bit fs type, which
1128 * separates our fsid from any other filesystem types, and a
1129 * 56-bit objset unique ID. The objset unique ID is unique to
1130 * all objsets open on this system, provided by unique_create().
1131 * The 8-bit fs type must be put in the low bits of fsid[1]
1132 * because that's where other Solaris filesystems put it.
1133 */
1134 fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
1135 ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
1136 vfsp->vfs_fsid.val[0] = fsid_guid;
1137 vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
1138 zfsfstype & 0xFF;
1139
1140 /*
1141 * Set features for file system.
1142 */
1143 zfs_set_fuid_feature(zfsvfs);
1144 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1145 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1146 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1147 vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1148 } else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1149 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1150 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1151 }
1152 vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1153
1154 if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1155 uint64_t pval;
1156
1157 atime_changed_cb(zfsvfs, B_FALSE);
1158 readonly_changed_cb(zfsvfs, B_TRUE);
1159 if ((error = dsl_prop_get_integer(osname,"xattr",&pval,NULL)))
1160 goto out;
1161 xattr_changed_cb(zfsvfs, pval);
1162 zfsvfs->z_issnap = B_TRUE;
1163 zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1164
1165 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1166 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1167 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1168 } else {
1169 error = zfsvfs_setup(zfsvfs, B_TRUE);
1170 }
1171
1172 if (!zfsvfs->z_issnap)
1173 zfsctl_create(zfsvfs);
1174 out:
1175 if (error) {
1176 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1177 zfsvfs_free(zfsvfs);
1178 } else {
1179 atomic_add_32(&zfs_active_fs_count, 1);
1180 }
1181
1182 return (error);
1183 }
1184 EXPORT_SYMBOL(zfs_domount);
1185
1186 void
1187 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1188 {
1189 objset_t *os = zfsvfs->z_os;
1190 struct dsl_dataset *ds;
1191
1192 /*
1193 * Unregister properties.
1194 */
1195 if (!dmu_objset_is_snapshot(os)) {
1196 ds = dmu_objset_ds(os);
1197 VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
1198 zfsvfs) == 0);
1199
1200 VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
1201 zfsvfs) == 0);
1202
1203 VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
1204 zfsvfs) == 0);
1205
1206 VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
1207 zfsvfs) == 0);
1208
1209 VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
1210 zfsvfs) == 0);
1211
1212 VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
1213 zfsvfs) == 0);
1214
1215 VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
1216 zfsvfs) == 0);
1217
1218 VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
1219 zfsvfs) == 0);
1220
1221 VERIFY(dsl_prop_unregister(ds, "aclinherit",
1222 acl_inherit_changed_cb, zfsvfs) == 0);
1223
1224 VERIFY(dsl_prop_unregister(ds, "vscan",
1225 vscan_changed_cb, zfsvfs) == 0);
1226 }
1227 }
1228 EXPORT_SYMBOL(zfs_unregister_callbacks);
1229
1230 #ifdef HAVE_MLSLABEL
1231 /*
1232 * zfs_check_global_label:
1233 * Check that the hex label string is appropriate for the dataset
1234 * being mounted into the global_zone proper.
1235 *
1236 * Return an error if the hex label string is not default or
1237 * admin_low/admin_high. For admin_low labels, the corresponding
1238 * dataset must be readonly.
1239 */
1240 int
1241 zfs_check_global_label(const char *dsname, const char *hexsl)
1242 {
1243 if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1244 return (0);
1245 if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1246 return (0);
1247 if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1248 /* must be readonly */
1249 uint64_t rdonly;
1250
1251 if (dsl_prop_get_integer(dsname,
1252 zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1253 return (EACCES);
1254 return (rdonly ? 0 : EACCES);
1255 }
1256 return (EACCES);
1257 }
1258 #endif /* HAVE_MLSLABEL */
1259
1260 /*
1261 * zfs_mount_label_policy:
1262 * Determine whether the mount is allowed according to MAC check.
1263 * by comparing (where appropriate) label of the dataset against
1264 * the label of the zone being mounted into. If the dataset has
1265 * no label, create one.
1266 *
1267 * Returns:
1268 * 0 : access allowed
1269 * >0 : error code, such as EACCES
1270 */
1271 static int
1272 zfs_mount_label_policy(vfs_t *vfsp, char *osname)
1273 {
1274 int error, retv;
1275 zone_t *mntzone = NULL;
1276 ts_label_t *mnt_tsl;
1277 bslabel_t *mnt_sl;
1278 bslabel_t ds_sl;
1279 char ds_hexsl[MAXNAMELEN];
1280
1281 retv = EACCES; /* assume the worst */
1282
1283 /*
1284 * Start by getting the dataset label if it exists.
1285 */
1286 error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1287 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
1288 if (error)
1289 return (EACCES);
1290
1291 /*
1292 * If labeling is NOT enabled, then disallow the mount of datasets
1293 * which have a non-default label already. No other label checks
1294 * are needed.
1295 */
1296 if (!is_system_labeled()) {
1297 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1298 return (0);
1299 return (EACCES);
1300 }
1301
1302 /*
1303 * Get the label of the mountpoint. If mounting into the global
1304 * zone (i.e. mountpoint is not within an active zone and the
1305 * zoned property is off), the label must be default or
1306 * admin_low/admin_high only; no other checks are needed.
1307 */
1308 mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
1309 if (mntzone->zone_id == GLOBAL_ZONEID) {
1310 uint64_t zoned;
1311
1312 zone_rele(mntzone);
1313
1314 if (dsl_prop_get_integer(osname,
1315 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
1316 return (EACCES);
1317 if (!zoned)
1318 return (zfs_check_global_label(osname, ds_hexsl));
1319 else
1320 /*
1321 * This is the case of a zone dataset being mounted
1322 * initially, before the zone has been fully created;
1323 * allow this mount into global zone.
1324 */
1325 return (0);
1326 }
1327
1328 mnt_tsl = mntzone->zone_slabel;
1329 ASSERT(mnt_tsl != NULL);
1330 label_hold(mnt_tsl);
1331 mnt_sl = label2bslabel(mnt_tsl);
1332
1333 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
1334 /*
1335 * The dataset doesn't have a real label, so fabricate one.
1336 */
1337 char *str = NULL;
1338
1339 if (l_to_str_internal(mnt_sl, &str) == 0 &&
1340 dsl_prop_set(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1341 ZPROP_SRC_LOCAL, 1, strlen(str) + 1, str) == 0)
1342 retv = 0;
1343 if (str != NULL)
1344 kmem_free(str, strlen(str) + 1);
1345 } else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
1346 /*
1347 * Now compare labels to complete the MAC check. If the
1348 * labels are equal then allow access. If the mountpoint
1349 * label dominates the dataset label, allow readonly access.
1350 * Otherwise, access is denied.
1351 */
1352 if (blequal(mnt_sl, &ds_sl))
1353 retv = 0;
1354 else if (bldominates(mnt_sl, &ds_sl)) {
1355 vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1356 retv = 0;
1357 }
1358 }
1359
1360 label_rele(mnt_tsl);
1361 zone_rele(mntzone);
1362 return (retv);
1363 }
1364
1365 static int
1366 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
1367 {
1368 int error = 0;
1369 static int zfsrootdone = 0;
1370 zfsvfs_t *zfsvfs = NULL;
1371 znode_t *zp = NULL;
1372 vnode_t *vp = NULL;
1373 char *zfs_bootfs;
1374 char *zfs_devid;
1375
1376 ASSERT(vfsp);
1377
1378 /*
1379 * The filesystem that we mount as root is defined in the
1380 * boot property "zfs-bootfs" with a format of
1381 * "poolname/root-dataset-objnum".
1382 */
1383 if (why == ROOT_INIT) {
1384 if (zfsrootdone++)
1385 return (EBUSY);
1386 /*
1387 * the process of doing a spa_load will require the
1388 * clock to be set before we could (for example) do
1389 * something better by looking at the timestamp on
1390 * an uberblock, so just set it to -1.
1391 */
1392 clkset(-1);
1393
1394 if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
1395 cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
1396 "bootfs name");
1397 return (EINVAL);
1398 }
1399 zfs_devid = spa_get_bootprop("diskdevid");
1400 error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
1401 if (zfs_devid)
1402 spa_free_bootprop(zfs_devid);
1403 if (error) {
1404 spa_free_bootprop(zfs_bootfs);
1405 cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
1406 error);
1407 return (error);
1408 }
1409 if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
1410 spa_free_bootprop(zfs_bootfs);
1411 cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
1412 error);
1413 return (error);
1414 }
1415
1416 spa_free_bootprop(zfs_bootfs);
1417
1418 if (error = vfs_lock(vfsp))
1419 return (error);
1420
1421 if (error = zfs_domount(vfsp, rootfs.bo_name)) {
1422 cmn_err(CE_NOTE, "zfs_domount: error %d", error);
1423 goto out;
1424 }
1425
1426 zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
1427 ASSERT(zfsvfs);
1428 if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
1429 cmn_err(CE_NOTE, "zfs_zget: error %d", error);
1430 goto out;
1431 }
1432
1433 vp = ZTOV(zp);
1434 mutex_enter(&vp->v_lock);
1435 vp->v_flag |= VROOT;
1436 mutex_exit(&vp->v_lock);
1437 rootvp = vp;
1438
1439 /*
1440 * Leave rootvp held. The root file system is never unmounted.
1441 */
1442
1443 vfs_add((struct vnode *)0, vfsp,
1444 (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
1445 out:
1446 vfs_unlock(vfsp);
1447 return (error);
1448 } else if (why == ROOT_REMOUNT) {
1449 readonly_changed_cb(vfsp->vfs_data, B_FALSE);
1450 vfsp->vfs_flag |= VFS_REMOUNT;
1451
1452 /* refresh mount options */
1453 zfs_unregister_callbacks(vfsp->vfs_data);
1454 return (zfs_register_callbacks(vfsp));
1455
1456 } else if (why == ROOT_UNMOUNT) {
1457 zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
1458 (void) zfs_sync(vfsp, 0, 0);
1459 return (0);
1460 }
1461
1462 /*
1463 * if "why" is equal to anything else other than ROOT_INIT,
1464 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
1465 */
1466 return (ENOTSUP);
1467 }
1468
1469 /*ARGSUSED*/
1470 static int
1471 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
1472 {
1473 char *osname;
1474 pathname_t spn;
1475 int error = 0;
1476 uio_seg_t fromspace = (uap->flags & MS_SYSSPACE) ?
1477 UIO_SYSSPACE : UIO_USERSPACE;
1478 int canwrite;
1479
1480 if (mvp->v_type != VDIR)
1481 return (ENOTDIR);
1482
1483 mutex_enter(&mvp->v_lock);
1484 if ((uap->flags & MS_REMOUNT) == 0 &&
1485 (uap->flags & MS_OVERLAY) == 0 &&
1486 (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
1487 mutex_exit(&mvp->v_lock);
1488 return (EBUSY);
1489 }
1490 mutex_exit(&mvp->v_lock);
1491
1492 /*
1493 * ZFS does not support passing unparsed data in via MS_DATA.
1494 * Users should use the MS_OPTIONSTR interface; this means
1495 * that all option parsing is already done and the options struct
1496 * can be interrogated.
1497 */
1498 if ((uap->flags & MS_DATA) && uap->datalen > 0)
1499 return (EINVAL);
1500
1501 /*
1502 * Get the objset name (the "special" mount argument).
1503 */
1504 if ((error = pn_get(uap->spec, fromspace, &spn)))
1505 return (error);
1506
1507 osname = spn.pn_path;
1508
1509 /*
1510 * Check for mount privilege?
1511 *
1512 * If we don't have privilege then see if
1513 * we have local permission to allow it
1514 */
1515 error = secpolicy_fs_mount(cr, mvp, vfsp);
1516 if (error) {
1517 if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) == 0) {
1518 vattr_t vattr;
1519
1520 /*
1521 * Make sure user is the owner of the mount point
1522 * or has sufficient privileges.
1523 */
1524
1525 vattr.va_mask = AT_UID;
1526
1527 if (VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
1528 goto out;
1529 }
1530
1531 if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
1532 VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
1533 goto out;
1534 }
1535 secpolicy_fs_mount_clearopts(cr, vfsp);
1536 } else {
1537 goto out;
1538 }
1539 }
1540
1541 /*
1542 * Refuse to mount a filesystem if we are in a local zone and the
1543 * dataset is not visible.
1544 */
1545 if (!INGLOBALZONE(curproc) &&
1546 (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1547 error = EPERM;
1548 goto out;
1549 }
1550
1551 error = zfs_mount_label_policy(vfsp, osname);
1552 if (error)
1553 goto out;
1554
1555 /*
1556 * When doing a remount, we simply refresh our temporary properties
1557 * according to those options set in the current VFS options.
1558 */
1559 if (uap->flags & MS_REMOUNT) {
1560 /* refresh mount options */
1561 zfs_unregister_callbacks(vfsp->vfs_data);
1562 error = zfs_register_callbacks(vfsp);
1563 goto out;
1564 }
1565
1566 error = zfs_domount(vfsp, osname);
1567
1568 /*
1569 * Add an extra VFS_HOLD on our parent vfs so that it can't
1570 * disappear due to a forced unmount.
1571 */
1572 if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
1573 VFS_HOLD(mvp->v_vfsp);
1574
1575 out:
1576 pn_free(&spn);
1577 return (error);
1578 }
1579
1580 int
1581 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1582 {
1583 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1584 dev32_t d32;
1585 uint64_t refdbytes, availbytes, usedobjs, availobjs;
1586
1587 ZFS_ENTER(zfsvfs);
1588
1589 dmu_objset_space(zfsvfs->z_os,
1590 &refdbytes, &availbytes, &usedobjs, &availobjs);
1591
1592 /*
1593 * The underlying storage pool actually uses multiple block sizes.
1594 * We report the fragsize as the smallest block size we support,
1595 * and we report our blocksize as the filesystem's maximum blocksize.
1596 */
1597 statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1598 statp->f_bsize = zfsvfs->z_max_blksz;
1599
1600 /*
1601 * The following report "total" blocks of various kinds in the
1602 * file system, but reported in terms of f_frsize - the
1603 * "fragment" size.
1604 */
1605
1606 statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1607 statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1608 statp->f_bavail = statp->f_bfree; /* no root reservation */
1609
1610 /*
1611 * statvfs() should really be called statufs(), because it assumes
1612 * static metadata. ZFS doesn't preallocate files, so the best
1613 * we can do is report the max that could possibly fit in f_files,
1614 * and that minus the number actually used in f_ffree.
1615 * For f_ffree, report the smaller of the number of object available
1616 * and the number of blocks (each object will take at least a block).
1617 */
1618 statp->f_ffree = MIN(availobjs, statp->f_bfree);
1619 statp->f_favail = statp->f_ffree; /* no "root reservation" */
1620 statp->f_files = statp->f_ffree + usedobjs;
1621
1622 (void) cmpldev(&d32, vfsp->vfs_dev);
1623 statp->f_fsid = d32;
1624
1625 /*
1626 * We're a zfs filesystem.
1627 */
1628 (void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1629
1630 statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1631
1632 statp->f_namemax = ZFS_MAXNAMELEN;
1633
1634 /*
1635 * We have all of 32 characters to stuff a string here.
1636 * Is there anything useful we could/should provide?
1637 */
1638 bzero(statp->f_fstr, sizeof (statp->f_fstr));
1639
1640 ZFS_EXIT(zfsvfs);
1641 return (0);
1642 }
1643 EXPORT_SYMBOL(zfs_statvfs);
1644
1645 int
1646 zfs_root(vfs_t *vfsp, vnode_t **vpp)
1647 {
1648 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1649 znode_t *rootzp;
1650 int error;
1651
1652 ZFS_ENTER(zfsvfs);
1653
1654 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1655 if (error == 0)
1656 *vpp = ZTOV(rootzp);
1657
1658 ZFS_EXIT(zfsvfs);
1659 return (error);
1660 }
1661 EXPORT_SYMBOL(zfs_root);
1662
1663 /*
1664 * Teardown the zfsvfs::z_os.
1665 *
1666 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1667 * and 'z_teardown_inactive_lock' held.
1668 */
1669 static int
1670 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1671 {
1672 znode_t *zp;
1673
1674 rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1675
1676 if (!unmounting) {
1677 /*
1678 * We purge the parent filesystem's vfsp as the parent
1679 * filesystem and all of its snapshots have their vnode's
1680 * v_vfsp set to the parent's filesystem's vfsp. Note,
1681 * 'z_parent' is self referential for non-snapshots.
1682 */
1683 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1684 }
1685
1686 /*
1687 * Close the zil. NB: Can't close the zil while zfs_inactive
1688 * threads are blocked as zil_close can call zfs_inactive.
1689 */
1690 if (zfsvfs->z_log) {
1691 zil_close(zfsvfs->z_log);
1692 zfsvfs->z_log = NULL;
1693 }
1694
1695 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1696
1697 /*
1698 * If we are not unmounting (ie: online recv) and someone already
1699 * unmounted this file system while we were doing the switcheroo,
1700 * or a reopen of z_os failed then just bail out now.
1701 */
1702 if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1703 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1704 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1705 return (EIO);
1706 }
1707
1708 /*
1709 * At this point there are no vops active, and any new vops will
1710 * fail with EIO since we have z_teardown_lock for writer (only
1711 * relavent for forced unmount).
1712 *
1713 * Release all holds on dbufs.
1714 */
1715 mutex_enter(&zfsvfs->z_znodes_lock);
1716 for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1717 zp = list_next(&zfsvfs->z_all_znodes, zp))
1718 if (zp->z_sa_hdl) {
1719 ASSERT(ZTOV(zp)->v_count > 0);
1720 zfs_znode_dmu_fini(zp);
1721 }
1722 mutex_exit(&zfsvfs->z_znodes_lock);
1723
1724 /*
1725 * If we are unmounting, set the unmounted flag and let new vops
1726 * unblock. zfs_inactive will have the unmounted behavior, and all
1727 * other vops will fail with EIO.
1728 */
1729 if (unmounting) {
1730 zfsvfs->z_unmounted = B_TRUE;
1731 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1732 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1733 }
1734
1735 /*
1736 * z_os will be NULL if there was an error in attempting to reopen
1737 * zfsvfs, so just return as the properties had already been
1738 * unregistered and cached data had been evicted before.
1739 */
1740 if (zfsvfs->z_os == NULL)
1741 return (0);
1742
1743 /*
1744 * Unregister properties.
1745 */
1746 zfs_unregister_callbacks(zfsvfs);
1747
1748 /*
1749 * Evict cached data
1750 */
1751 if (dmu_objset_is_dirty_anywhere(zfsvfs->z_os))
1752 if (!(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
1753 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1754 (void) dmu_objset_evict_dbufs(zfsvfs->z_os);
1755
1756 return (0);
1757 }
1758
1759 /*ARGSUSED*/
1760 int
1761 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1762 {
1763 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1764 objset_t *os;
1765 int ret;
1766
1767 ret = secpolicy_fs_unmount(cr, vfsp);
1768 if (ret) {
1769 if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1770 ZFS_DELEG_PERM_MOUNT, cr))
1771 return (ret);
1772 }
1773
1774 /*
1775 * We purge the parent filesystem's vfsp as the parent filesystem
1776 * and all of its snapshots have their vnode's v_vfsp set to the
1777 * parent's filesystem's vfsp. Note, 'z_parent' is self
1778 * referential for non-snapshots.
1779 */
1780 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1781
1782 /*
1783 * Unmount any snapshots mounted under .zfs before unmounting the
1784 * dataset itself.
1785 */
1786 if (zfsvfs->z_ctldir != NULL &&
1787 (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1788 return (ret);
1789 }
1790
1791 if (!(fflag & MS_FORCE)) {
1792 /*
1793 * Check the number of active vnodes in the file system.
1794 * Our count is maintained in the vfs structure, but the
1795 * number is off by 1 to indicate a hold on the vfs
1796 * structure itself.
1797 *
1798 * The '.zfs' directory maintains a reference of its
1799 * own, and any active references underneath are
1800 * reflected in the vnode count.
1801 */
1802 if (zfsvfs->z_ctldir == NULL) {
1803 if (vfsp->vfs_count > 1)
1804 return (EBUSY);
1805 } else {
1806 if (vfsp->vfs_count > 2 ||
1807 zfsvfs->z_ctldir->v_count > 1)
1808 return (EBUSY);
1809 }
1810 }
1811
1812 vfsp->vfs_flag |= VFS_UNMOUNTED;
1813
1814 VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1815 os = zfsvfs->z_os;
1816
1817 /*
1818 * z_os will be NULL if there was an error in
1819 * attempting to reopen zfsvfs.
1820 */
1821 if (os != NULL) {
1822 /*
1823 * Unset the objset user_ptr.
1824 */
1825 mutex_enter(&os->os_user_ptr_lock);
1826 dmu_objset_set_user(os, NULL);
1827 mutex_exit(&os->os_user_ptr_lock);
1828
1829 /*
1830 * Finally release the objset
1831 */
1832 dmu_objset_disown(os, zfsvfs);
1833 }
1834
1835 /*
1836 * We can now safely destroy the '.zfs' directory node.
1837 */
1838 if (zfsvfs->z_ctldir != NULL)
1839 zfsctl_destroy(zfsvfs);
1840
1841 return (0);
1842 }
1843 EXPORT_SYMBOL(zfs_umount);
1844
1845 int
1846 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1847 {
1848 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1849 znode_t *zp;
1850 uint64_t object = 0;
1851 uint64_t fid_gen = 0;
1852 uint64_t gen_mask;
1853 uint64_t zp_gen;
1854 int i, err;
1855
1856 *vpp = NULL;
1857
1858 ZFS_ENTER(zfsvfs);
1859
1860 if (fidp->fid_len == LONG_FID_LEN) {
1861 zfid_long_t *zlfid = (zfid_long_t *)fidp;
1862 uint64_t objsetid = 0;
1863 uint64_t setgen = 0;
1864
1865 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1866 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1867
1868 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1869 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1870
1871 ZFS_EXIT(zfsvfs);
1872
1873 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1874 if (err)
1875 return (EINVAL);
1876 ZFS_ENTER(zfsvfs);
1877 }
1878
1879 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1880 zfid_short_t *zfid = (zfid_short_t *)fidp;
1881
1882 for (i = 0; i < sizeof (zfid->zf_object); i++)
1883 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1884
1885 for (i = 0; i < sizeof (zfid->zf_gen); i++)
1886 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1887 } else {
1888 ZFS_EXIT(zfsvfs);
1889 return (EINVAL);
1890 }
1891
1892 /* A zero fid_gen means we are in the .zfs control directories */
1893 if (fid_gen == 0 &&
1894 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1895 *vpp = zfsvfs->z_ctldir;
1896 ASSERT(*vpp != NULL);
1897 if (object == ZFSCTL_INO_SNAPDIR) {
1898 VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1899 0, NULL, NULL, NULL, NULL, NULL) == 0);
1900 } else {
1901 VN_HOLD(*vpp);
1902 }
1903 ZFS_EXIT(zfsvfs);
1904 return (0);
1905 }
1906
1907 gen_mask = -1ULL >> (64 - 8 * i);
1908
1909 dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1910 if ((err = zfs_zget(zfsvfs, object, &zp))) {
1911 ZFS_EXIT(zfsvfs);
1912 return (err);
1913 }
1914 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
1915 sizeof (uint64_t));
1916 zp_gen = zp_gen & gen_mask;
1917 if (zp_gen == 0)
1918 zp_gen = 1;
1919 if (zp->z_unlinked || zp_gen != fid_gen) {
1920 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1921 VN_RELE(ZTOV(zp));
1922 ZFS_EXIT(zfsvfs);
1923 return (EINVAL);
1924 }
1925
1926 *vpp = ZTOV(zp);
1927 if (*vpp)
1928 zfs_inode_update(VTOZ(*vpp));
1929
1930 ZFS_EXIT(zfsvfs);
1931 return (0);
1932 }
1933 EXPORT_SYMBOL(zfs_vget);
1934
1935 /*
1936 * Block out VOPs and close zfsvfs_t::z_os
1937 *
1938 * Note, if successful, then we return with the 'z_teardown_lock' and
1939 * 'z_teardown_inactive_lock' write held.
1940 */
1941 int
1942 zfs_suspend_fs(zfsvfs_t *zfsvfs)
1943 {
1944 int error;
1945
1946 if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
1947 return (error);
1948 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1949
1950 return (0);
1951 }
1952 EXPORT_SYMBOL(zfs_suspend_fs);
1953
1954 /*
1955 * Reopen zfsvfs_t::z_os and release VOPs.
1956 */
1957 int
1958 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname)
1959 {
1960 int err, err2;
1961
1962 ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
1963 ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
1964
1965 err = dmu_objset_own(osname, DMU_OST_ZFS, B_FALSE, zfsvfs,
1966 &zfsvfs->z_os);
1967 if (err) {
1968 zfsvfs->z_os = NULL;
1969 } else {
1970 znode_t *zp;
1971 uint64_t sa_obj = 0;
1972
1973 err2 = zap_lookup(zfsvfs->z_os, MASTER_NODE_OBJ,
1974 ZFS_SA_ATTRS, 8, 1, &sa_obj);
1975
1976 if ((err || err2) && zfsvfs->z_version >= ZPL_VERSION_SA)
1977 goto bail;
1978
1979
1980 if ((err = sa_setup(zfsvfs->z_os, sa_obj,
1981 zfs_attr_table, ZPL_END, &zfsvfs->z_attr_table)) != 0)
1982 goto bail;
1983
1984 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
1985
1986 /*
1987 * Attempt to re-establish all the active znodes with
1988 * their dbufs. If a zfs_rezget() fails, then we'll let
1989 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1990 * when they try to use their znode.
1991 */
1992 mutex_enter(&zfsvfs->z_znodes_lock);
1993 for (zp = list_head(&zfsvfs->z_all_znodes); zp;
1994 zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1995 (void) zfs_rezget(zp);
1996 }
1997 mutex_exit(&zfsvfs->z_znodes_lock);
1998
1999 }
2000
2001 bail:
2002 /* release the VOPs */
2003 rw_exit(&zfsvfs->z_teardown_inactive_lock);
2004 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
2005
2006 if (err) {
2007 /*
2008 * Since we couldn't reopen zfsvfs::z_os, force
2009 * unmount this file system.
2010 */
2011 if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
2012 (void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
2013 }
2014 return (err);
2015 }
2016 EXPORT_SYMBOL(zfs_resume_fs);
2017
2018 static void
2019 zfs_freevfs(vfs_t *vfsp)
2020 {
2021 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2022
2023 /*
2024 * If this is a snapshot, we have an extra VFS_HOLD on our parent
2025 * from zfs_mount(). Release it here. If we came through
2026 * zfs_mountroot() instead, we didn't grab an extra hold, so
2027 * skip the VFS_RELE for rootvfs.
2028 */
2029 if (zfsvfs->z_issnap && (vfsp != rootvfs))
2030 VFS_RELE(zfsvfs->z_parent->z_vfs);
2031
2032 zfsvfs_free(zfsvfs);
2033
2034 atomic_add_32(&zfs_active_fs_count, -1);
2035 }
2036
2037 /*
2038 * VFS_INIT() initialization. Note that there is no VFS_FINI(),
2039 * so we can't safely do any non-idempotent initialization here.
2040 * Leave that to zfs_init() and zfs_fini(), which are called
2041 * from the module's _init() and _fini() entry points.
2042 */
2043 /*ARGSUSED*/
2044 static int
2045 zfs_vfsinit(int fstype, char *name)
2046 {
2047 int error;
2048
2049 zfsfstype = fstype;
2050 mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
2051
2052 /*
2053 * Unique major number for all zfs mounts.
2054 * If we run out of 32-bit minors, we'll getudev() another major.
2055 */
2056 zfs_major = ddi_name_to_major(ZFS_DRIVER);
2057 zfs_minor = ZFS_MIN_MINOR;
2058
2059 return (0);
2060 }
2061 #endif /* HAVE_ZPL */
2062
2063 void
2064 zfs_init(void)
2065 {
2066 #ifdef HAVE_ZPL
2067 /*
2068 * Initialize .zfs directory structures
2069 */
2070 zfsctl_init();
2071
2072 /*
2073 * Initialize znode cache, vnode ops, etc...
2074 */
2075 zfs_znode_init();
2076 #endif /* HAVE_ZPL */
2077
2078 dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
2079 }
2080
2081 void
2082 zfs_fini(void)
2083 {
2084 #ifdef HAVE_ZPL
2085 zfsctl_fini();
2086 zfs_znode_fini();
2087 #endif /* HAVE_ZPL */
2088 }
2089
2090 #ifdef HAVE_ZPL
2091 int
2092 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
2093 {
2094 int error;
2095 objset_t *os = zfsvfs->z_os;
2096 dmu_tx_t *tx;
2097
2098 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2099 return (EINVAL);
2100
2101 if (newvers < zfsvfs->z_version)
2102 return (EINVAL);
2103
2104 if (zfs_spa_version_map(newvers) >
2105 spa_version(dmu_objset_spa(zfsvfs->z_os)))
2106 return (ENOTSUP);
2107
2108 tx = dmu_tx_create(os);
2109 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
2110 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2111 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
2112 ZFS_SA_ATTRS);
2113 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2114 }
2115 error = dmu_tx_assign(tx, TXG_WAIT);
2116 if (error) {
2117 dmu_tx_abort(tx);
2118 return (error);
2119 }
2120
2121 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
2122 8, 1, &newvers, tx);
2123
2124 if (error) {
2125 dmu_tx_commit(tx);
2126 return (error);
2127 }
2128
2129 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2130 uint64_t sa_obj;
2131
2132 ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
2133 SPA_VERSION_SA);
2134 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
2135 DMU_OT_NONE, 0, tx);
2136
2137 error = zap_add(os, MASTER_NODE_OBJ,
2138 ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2139 ASSERT3U(error, ==, 0);
2140
2141 VERIFY(0 == sa_set_sa_object(os, sa_obj));
2142 sa_register_update_callback(os, zfs_sa_upgrade);
2143 }
2144
2145 spa_history_log_internal(LOG_DS_UPGRADE,
2146 dmu_objset_spa(os), tx, "oldver=%llu newver=%llu dataset = %llu",
2147 zfsvfs->z_version, newvers, dmu_objset_id(os));
2148
2149 dmu_tx_commit(tx);
2150
2151 zfsvfs->z_version = newvers;
2152
2153 if (zfsvfs->z_version >= ZPL_VERSION_FUID)
2154 zfs_set_fuid_feature(zfsvfs);
2155
2156 return (0);
2157 }
2158 EXPORT_SYMBOL(zfs_set_version);
2159 #endif /* HAVE_ZPL */
2160
2161 /*
2162 * Read a property stored within the master node.
2163 */
2164 int
2165 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2166 {
2167 const char *pname;
2168 int error = ENOENT;
2169
2170 /*
2171 * Look up the file system's value for the property. For the
2172 * version property, we look up a slightly different string.
2173 */
2174 if (prop == ZFS_PROP_VERSION)
2175 pname = ZPL_VERSION_STR;
2176 else
2177 pname = zfs_prop_to_name(prop);
2178
2179 if (os != NULL)
2180 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2181
2182 if (error == ENOENT) {
2183 /* No value set, use the default value */
2184 switch (prop) {
2185 case ZFS_PROP_VERSION:
2186 *value = ZPL_VERSION;
2187 break;
2188 case ZFS_PROP_NORMALIZE:
2189 case ZFS_PROP_UTF8ONLY:
2190 *value = 0;
2191 break;
2192 case ZFS_PROP_CASE:
2193 *value = ZFS_CASE_SENSITIVE;
2194 break;
2195 default:
2196 return (error);
2197 }
2198 error = 0;
2199 }
2200 return (error);
2201 }