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 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>.
24 * All rights reserved.
25 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
26 * Copyright (c) 2014 Integros [integros.com]
27 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
28 */
29
30 /* Portions Copyright 2010 Robert Milkowski */
31
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/sysmacros.h>
37 #include <sys/kmem.h>
38 #include <sys/acl.h>
39 #include <sys/vnode.h>
40 #include <sys/vfs.h>
41 #include <sys/mntent.h>
42 #include <sys/mount.h>
43 #include <sys/cmn_err.h>
44 #include <sys/zfs_znode.h>
45 #include <sys/zfs_dir.h>
46 #include <sys/zil.h>
47 #include <sys/fs/zfs.h>
48 #include <sys/dmu.h>
49 #include <sys/dsl_prop.h>
50 #include <sys/dsl_dataset.h>
51 #include <sys/dsl_deleg.h>
52 #include <sys/spa.h>
53 #include <sys/zap.h>
54 #include <sys/sa.h>
55 #include <sys/sa_impl.h>
56 #include <sys/varargs.h>
57 #include <sys/policy.h>
58 #include <sys/atomic.h>
59 #include <sys/zfs_ioctl.h>
60 #include <sys/zfs_ctldir.h>
61 #include <sys/zfs_fuid.h>
62 #include <sys/sunddi.h>
63 #include <sys/dnlc.h>
64 #include <sys/dmu_objset.h>
65 #include <sys/spa_boot.h>
66 #include <sys/jail.h>
67 #include <ufs/ufs/quota.h>
68 #include <sys/rmlock.h>
69
70 #include "zfs_comutil.h"
71
72 struct mtx zfs_debug_mtx;
73 MTX_SYSINIT(zfs_debug_mtx, &zfs_debug_mtx, "zfs_debug", MTX_DEF);
74
75 SYSCTL_NODE(_vfs, OID_AUTO, zfs, CTLFLAG_RW, 0, "ZFS file system");
76
77 int zfs_super_owner;
78 SYSCTL_INT(_vfs_zfs, OID_AUTO, super_owner, CTLFLAG_RW, &zfs_super_owner, 0,
79 "File system owner can perform privileged operation on his file systems");
80
81 int zfs_debug_level;
82 SYSCTL_INT(_vfs_zfs, OID_AUTO, debug, CTLFLAG_RWTUN, &zfs_debug_level, 0,
83 "Debug level");
84
85 SYSCTL_NODE(_vfs_zfs, OID_AUTO, version, CTLFLAG_RD, 0, "ZFS versions");
86 static int zfs_version_acl = ZFS_ACL_VERSION;
87 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, acl, CTLFLAG_RD, &zfs_version_acl, 0,
88 "ZFS_ACL_VERSION");
89 static int zfs_version_spa = SPA_VERSION;
90 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, spa, CTLFLAG_RD, &zfs_version_spa, 0,
91 "SPA_VERSION");
92 static int zfs_version_zpl = ZPL_VERSION;
93 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, zpl, CTLFLAG_RD, &zfs_version_zpl, 0,
94 "ZPL_VERSION");
95
96 static int zfs_root_setvnode(zfsvfs_t *zfsvfs);
97 static void zfs_root_dropvnode(zfsvfs_t *zfsvfs);
98
99 static int zfs_quotactl(vfs_t *vfsp, int cmds, uid_t id, void *arg);
100 static int zfs_mount(vfs_t *vfsp);
101 static int zfs_umount(vfs_t *vfsp, int fflag);
102 static int zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp);
103 static int zfs_statfs(vfs_t *vfsp, struct statfs *statp);
104 static int zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp);
105 static int zfs_sync(vfs_t *vfsp, int waitfor);
106 static int zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp,
107 struct ucred **credanonp, int *numsecflavors, int **secflavors);
108 static int zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, int flags, vnode_t **vpp);
109 static void zfs_objset_close(zfsvfs_t *zfsvfs);
110 static void zfs_freevfs(vfs_t *vfsp);
111
112 struct vfsops zfs_vfsops = {
113 .vfs_mount = zfs_mount,
114 .vfs_unmount = zfs_umount,
115 .vfs_root = zfs_root,
116 .vfs_statfs = zfs_statfs,
117 .vfs_vget = zfs_vget,
118 .vfs_sync = zfs_sync,
119 .vfs_checkexp = zfs_checkexp,
120 .vfs_fhtovp = zfs_fhtovp,
121 .vfs_quotactl = zfs_quotactl,
122 };
123
124 VFS_SET(zfs_vfsops, zfs, VFCF_JAIL | VFCF_DELEGADMIN);
125
126 /*
127 * We need to keep a count of active fs's.
128 * This is necessary to prevent our module
129 * from being unloaded after a umount -f
130 */
131 static uint32_t zfs_active_fs_count = 0;
132
133 static int
zfs_getquota(zfsvfs_t * zfsvfs,uid_t id,int isgroup,struct dqblk64 * dqp)134 zfs_getquota(zfsvfs_t *zfsvfs, uid_t id, int isgroup, struct dqblk64 *dqp)
135 {
136 int error = 0;
137 char buf[32];
138 int err;
139 uint64_t usedobj, quotaobj;
140 uint64_t quota, used = 0;
141 timespec_t now;
142
143 usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
144 quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
145
146 if (quotaobj == 0 || zfsvfs->z_replay) {
147 error = EINVAL;
148 goto done;
149 }
150 (void)sprintf(buf, "%llx", (longlong_t)id);
151 if ((error = zap_lookup(zfsvfs->z_os, quotaobj,
152 buf, sizeof(quota), 1, "a)) != 0) {
153 dprintf("%s(%d): quotaobj lookup failed\n", __FUNCTION__, __LINE__);
154 goto done;
155 }
156 /*
157 * quota(8) uses bsoftlimit as "quoota", and hardlimit as "limit".
158 * So we set them to be the same.
159 */
160 dqp->dqb_bsoftlimit = dqp->dqb_bhardlimit = btodb(quota);
161 error = zap_lookup(zfsvfs->z_os, usedobj, buf, sizeof(used), 1, &used);
162 if (error && error != ENOENT) {
163 dprintf("%s(%d): usedobj failed; %d\n", __FUNCTION__, __LINE__, error);
164 goto done;
165 }
166 dqp->dqb_curblocks = btodb(used);
167 dqp->dqb_ihardlimit = dqp->dqb_isoftlimit = 0;
168 vfs_timestamp(&now);
169 /*
170 * Setting this to 0 causes FreeBSD quota(8) to print
171 * the number of days since the epoch, which isn't
172 * particularly useful.
173 */
174 dqp->dqb_btime = dqp->dqb_itime = now.tv_sec;
175 done:
176 return (error);
177 }
178
179 static int
zfs_quotactl(vfs_t * vfsp,int cmds,uid_t id,void * arg)180 zfs_quotactl(vfs_t *vfsp, int cmds, uid_t id, void *arg)
181 {
182 zfsvfs_t *zfsvfs = vfsp->vfs_data;
183 struct thread *td;
184 int cmd, type, error = 0;
185 int bitsize;
186 uint64_t fuid;
187 zfs_userquota_prop_t quota_type;
188 struct dqblk64 dqblk = { 0 };
189
190 td = curthread;
191 cmd = cmds >> SUBCMDSHIFT;
192 type = cmds & SUBCMDMASK;
193
194 ZFS_ENTER(zfsvfs);
195 if (id == -1) {
196 switch (type) {
197 case USRQUOTA:
198 id = td->td_ucred->cr_ruid;
199 break;
200 case GRPQUOTA:
201 id = td->td_ucred->cr_rgid;
202 break;
203 default:
204 error = EINVAL;
205 if (cmd == Q_QUOTAON || cmd == Q_QUOTAOFF)
206 vfs_unbusy(vfsp);
207 goto done;
208 }
209 }
210 /*
211 * Map BSD type to:
212 * ZFS_PROP_USERUSED,
213 * ZFS_PROP_USERQUOTA,
214 * ZFS_PROP_GROUPUSED,
215 * ZFS_PROP_GROUPQUOTA
216 */
217 switch (cmd) {
218 case Q_SETQUOTA:
219 case Q_SETQUOTA32:
220 if (type == USRQUOTA)
221 quota_type = ZFS_PROP_USERQUOTA;
222 else if (type == GRPQUOTA)
223 quota_type = ZFS_PROP_GROUPQUOTA;
224 else
225 error = EINVAL;
226 break;
227 case Q_GETQUOTA:
228 case Q_GETQUOTA32:
229 if (type == USRQUOTA)
230 quota_type = ZFS_PROP_USERUSED;
231 else if (type == GRPQUOTA)
232 quota_type = ZFS_PROP_GROUPUSED;
233 else
234 error = EINVAL;
235 break;
236 }
237
238 /*
239 * Depending on the cmd, we may need to get
240 * the ruid and domain (see fuidstr_to_sid?),
241 * the fuid (how?), or other information.
242 * Create fuid using zfs_fuid_create(zfsvfs, id,
243 * ZFS_OWNER or ZFS_GROUP, cr, &fuidp)?
244 * I think I can use just the id?
245 *
246 * Look at zfs_fuid_overquota() to look up a quota.
247 * zap_lookup(something, quotaobj, fuidstring, sizeof(long long), 1, "a)
248 *
249 * See zfs_set_userquota() to set a quota.
250 */
251 if ((u_int)type >= MAXQUOTAS) {
252 error = EINVAL;
253 goto done;
254 }
255
256 switch (cmd) {
257 case Q_GETQUOTASIZE:
258 bitsize = 64;
259 error = copyout(&bitsize, arg, sizeof(int));
260 break;
261 case Q_QUOTAON:
262 // As far as I can tell, you can't turn quotas on or off on zfs
263 error = 0;
264 vfs_unbusy(vfsp);
265 break;
266 case Q_QUOTAOFF:
267 error = ENOTSUP;
268 vfs_unbusy(vfsp);
269 break;
270 case Q_SETQUOTA:
271 error = copyin(arg, &dqblk, sizeof(dqblk));
272 if (error == 0)
273 error = zfs_set_userquota(zfsvfs, quota_type,
274 "", id, dbtob(dqblk.dqb_bhardlimit));
275 break;
276 case Q_GETQUOTA:
277 error = zfs_getquota(zfsvfs, id, type == GRPQUOTA, &dqblk);
278 if (error == 0)
279 error = copyout(&dqblk, arg, sizeof(dqblk));
280 break;
281 default:
282 error = EINVAL;
283 break;
284 }
285 done:
286 ZFS_EXIT(zfsvfs);
287 return (error);
288 }
289
290 /*ARGSUSED*/
291 static int
zfs_sync(vfs_t * vfsp,int waitfor)292 zfs_sync(vfs_t *vfsp, int waitfor)
293 {
294
295 /*
296 * Data integrity is job one. We don't want a compromised kernel
297 * writing to the storage pool, so we never sync during panic.
298 */
299 if (panicstr)
300 return (0);
301
302 /*
303 * Ignore the system syncher. ZFS already commits async data
304 * at zfs_txg_timeout intervals.
305 */
306 if (waitfor == MNT_LAZY)
307 return (0);
308
309 if (vfsp != NULL) {
310 /*
311 * Sync a specific filesystem.
312 */
313 zfsvfs_t *zfsvfs = vfsp->vfs_data;
314 dsl_pool_t *dp;
315 int error;
316
317 error = vfs_stdsync(vfsp, waitfor);
318 if (error != 0)
319 return (error);
320
321 ZFS_ENTER(zfsvfs);
322 dp = dmu_objset_pool(zfsvfs->z_os);
323
324 /*
325 * If the system is shutting down, then skip any
326 * filesystems which may exist on a suspended pool.
327 */
328 if (sys_shutdown && spa_suspended(dp->dp_spa)) {
329 ZFS_EXIT(zfsvfs);
330 return (0);
331 }
332
333 if (zfsvfs->z_log != NULL)
334 zil_commit(zfsvfs->z_log, 0);
335
336 ZFS_EXIT(zfsvfs);
337 } else {
338 /*
339 * Sync all ZFS filesystems. This is what happens when you
340 * run sync(1M). Unlike other filesystems, ZFS honors the
341 * request by waiting for all pools to commit all dirty data.
342 */
343 spa_sync_allpools();
344 }
345
346 return (0);
347 }
348
349 #ifndef __FreeBSD_kernel__
350 static int
zfs_create_unique_device(dev_t * dev)351 zfs_create_unique_device(dev_t *dev)
352 {
353 major_t new_major;
354
355 do {
356 ASSERT3U(zfs_minor, <=, MAXMIN32);
357 minor_t start = zfs_minor;
358 do {
359 mutex_enter(&zfs_dev_mtx);
360 if (zfs_minor >= MAXMIN32) {
361 /*
362 * If we're still using the real major
363 * keep out of /dev/zfs and /dev/zvol minor
364 * number space. If we're using a getudev()'ed
365 * major number, we can use all of its minors.
366 */
367 if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
368 zfs_minor = ZFS_MIN_MINOR;
369 else
370 zfs_minor = 0;
371 } else {
372 zfs_minor++;
373 }
374 *dev = makedevice(zfs_major, zfs_minor);
375 mutex_exit(&zfs_dev_mtx);
376 } while (vfs_devismounted(*dev) && zfs_minor != start);
377 if (zfs_minor == start) {
378 /*
379 * We are using all ~262,000 minor numbers for the
380 * current major number. Create a new major number.
381 */
382 if ((new_major = getudev()) == (major_t)-1) {
383 cmn_err(CE_WARN,
384 "zfs_mount: Can't get unique major "
385 "device number.");
386 return (-1);
387 }
388 mutex_enter(&zfs_dev_mtx);
389 zfs_major = new_major;
390 zfs_minor = 0;
391
392 mutex_exit(&zfs_dev_mtx);
393 } else {
394 break;
395 }
396 /* CONSTANTCONDITION */
397 } while (1);
398
399 return (0);
400 }
401 #endif /* !__FreeBSD_kernel__ */
402
403 static void
atime_changed_cb(void * arg,uint64_t newval)404 atime_changed_cb(void *arg, uint64_t newval)
405 {
406 zfsvfs_t *zfsvfs = arg;
407
408 if (newval == TRUE) {
409 zfsvfs->z_atime = TRUE;
410 zfsvfs->z_vfs->vfs_flag &= ~MNT_NOATIME;
411 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
412 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
413 } else {
414 zfsvfs->z_atime = FALSE;
415 zfsvfs->z_vfs->vfs_flag |= MNT_NOATIME;
416 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
417 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
418 }
419 }
420
421 static void
xattr_changed_cb(void * arg,uint64_t newval)422 xattr_changed_cb(void *arg, uint64_t newval)
423 {
424 zfsvfs_t *zfsvfs = arg;
425
426 if (newval == TRUE) {
427 /* XXX locking on vfs_flag? */
428 #ifdef TODO
429 zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
430 #endif
431 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
432 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
433 } else {
434 /* XXX locking on vfs_flag? */
435 #ifdef TODO
436 zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
437 #endif
438 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
439 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
440 }
441 }
442
443 static void
blksz_changed_cb(void * arg,uint64_t newval)444 blksz_changed_cb(void *arg, uint64_t newval)
445 {
446 zfsvfs_t *zfsvfs = arg;
447 ASSERT3U(newval, <=, spa_maxblocksize(dmu_objset_spa(zfsvfs->z_os)));
448 ASSERT3U(newval, >=, SPA_MINBLOCKSIZE);
449 ASSERT(ISP2(newval));
450
451 zfsvfs->z_max_blksz = newval;
452 zfsvfs->z_vfs->mnt_stat.f_iosize = newval;
453 }
454
455 static void
readonly_changed_cb(void * arg,uint64_t newval)456 readonly_changed_cb(void *arg, uint64_t newval)
457 {
458 zfsvfs_t *zfsvfs = arg;
459
460 if (newval) {
461 /* XXX locking on vfs_flag? */
462 zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
463 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
464 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
465 } else {
466 /* XXX locking on vfs_flag? */
467 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
468 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
469 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
470 }
471 }
472
473 static void
setuid_changed_cb(void * arg,uint64_t newval)474 setuid_changed_cb(void *arg, uint64_t newval)
475 {
476 zfsvfs_t *zfsvfs = arg;
477
478 if (newval == FALSE) {
479 zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
480 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
481 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
482 } else {
483 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
484 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
485 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
486 }
487 }
488
489 static void
exec_changed_cb(void * arg,uint64_t newval)490 exec_changed_cb(void *arg, uint64_t newval)
491 {
492 zfsvfs_t *zfsvfs = arg;
493
494 if (newval == FALSE) {
495 zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
496 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
497 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
498 } else {
499 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
500 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
501 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
502 }
503 }
504
505 /*
506 * The nbmand mount option can be changed at mount time.
507 * We can't allow it to be toggled on live file systems or incorrect
508 * behavior may be seen from cifs clients
509 *
510 * This property isn't registered via dsl_prop_register(), but this callback
511 * will be called when a file system is first mounted
512 */
513 static void
nbmand_changed_cb(void * arg,uint64_t newval)514 nbmand_changed_cb(void *arg, uint64_t newval)
515 {
516 zfsvfs_t *zfsvfs = arg;
517 if (newval == FALSE) {
518 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
519 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
520 } else {
521 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
522 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
523 }
524 }
525
526 static void
snapdir_changed_cb(void * arg,uint64_t newval)527 snapdir_changed_cb(void *arg, uint64_t newval)
528 {
529 zfsvfs_t *zfsvfs = arg;
530
531 zfsvfs->z_show_ctldir = newval;
532 }
533
534 static void
vscan_changed_cb(void * arg,uint64_t newval)535 vscan_changed_cb(void *arg, uint64_t newval)
536 {
537 zfsvfs_t *zfsvfs = arg;
538
539 zfsvfs->z_vscan = newval;
540 }
541
542 static void
acl_mode_changed_cb(void * arg,uint64_t newval)543 acl_mode_changed_cb(void *arg, uint64_t newval)
544 {
545 zfsvfs_t *zfsvfs = arg;
546
547 zfsvfs->z_acl_mode = newval;
548 }
549
550 static void
acl_inherit_changed_cb(void * arg,uint64_t newval)551 acl_inherit_changed_cb(void *arg, uint64_t newval)
552 {
553 zfsvfs_t *zfsvfs = arg;
554
555 zfsvfs->z_acl_inherit = newval;
556 }
557
558 static int
zfs_register_callbacks(vfs_t * vfsp)559 zfs_register_callbacks(vfs_t *vfsp)
560 {
561 struct dsl_dataset *ds = NULL;
562 objset_t *os = NULL;
563 zfsvfs_t *zfsvfs = NULL;
564 uint64_t nbmand;
565 boolean_t readonly = B_FALSE;
566 boolean_t do_readonly = B_FALSE;
567 boolean_t setuid = B_FALSE;
568 boolean_t do_setuid = B_FALSE;
569 boolean_t exec = B_FALSE;
570 boolean_t do_exec = B_FALSE;
571 #ifdef illumos
572 boolean_t devices = B_FALSE;
573 boolean_t do_devices = B_FALSE;
574 #endif
575 boolean_t xattr = B_FALSE;
576 boolean_t do_xattr = B_FALSE;
577 boolean_t atime = B_FALSE;
578 boolean_t do_atime = B_FALSE;
579 int error = 0;
580
581 ASSERT(vfsp);
582 zfsvfs = vfsp->vfs_data;
583 ASSERT(zfsvfs);
584 os = zfsvfs->z_os;
585
586 /*
587 * This function can be called for a snapshot when we update snapshot's
588 * mount point, which isn't really supported.
589 */
590 if (dmu_objset_is_snapshot(os))
591 return (EOPNOTSUPP);
592
593 /*
594 * The act of registering our callbacks will destroy any mount
595 * options we may have. In order to enable temporary overrides
596 * of mount options, we stash away the current values and
597 * restore them after we register the callbacks.
598 */
599 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
600 !spa_writeable(dmu_objset_spa(os))) {
601 readonly = B_TRUE;
602 do_readonly = B_TRUE;
603 } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
604 readonly = B_FALSE;
605 do_readonly = B_TRUE;
606 }
607 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
608 setuid = B_FALSE;
609 do_setuid = B_TRUE;
610 } else {
611 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
612 setuid = B_FALSE;
613 do_setuid = B_TRUE;
614 } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
615 setuid = B_TRUE;
616 do_setuid = B_TRUE;
617 }
618 }
619 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
620 exec = B_FALSE;
621 do_exec = B_TRUE;
622 } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
623 exec = B_TRUE;
624 do_exec = B_TRUE;
625 }
626 if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
627 xattr = B_FALSE;
628 do_xattr = B_TRUE;
629 } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
630 xattr = B_TRUE;
631 do_xattr = B_TRUE;
632 }
633 if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
634 atime = B_FALSE;
635 do_atime = B_TRUE;
636 } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
637 atime = B_TRUE;
638 do_atime = B_TRUE;
639 }
640
641 /*
642 * We need to enter pool configuration here, so that we can use
643 * dsl_prop_get_int_ds() to handle the special nbmand property below.
644 * dsl_prop_get_integer() can not be used, because it has to acquire
645 * spa_namespace_lock and we can not do that because we already hold
646 * z_teardown_lock. The problem is that spa_write_cachefile() is called
647 * with spa_namespace_lock held and the function calls ZFS vnode
648 * operations to write the cache file and thus z_teardown_lock is
649 * acquired after spa_namespace_lock.
650 */
651 ds = dmu_objset_ds(os);
652 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
653
654 /*
655 * nbmand is a special property. It can only be changed at
656 * mount time.
657 *
658 * This is weird, but it is documented to only be changeable
659 * at mount time.
660 */
661 if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
662 nbmand = B_FALSE;
663 } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
664 nbmand = B_TRUE;
665 } else if (error = dsl_prop_get_int_ds(ds, "nbmand", &nbmand) != 0) {
666 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
667 return (error);
668 }
669
670 /*
671 * Register property callbacks.
672 *
673 * It would probably be fine to just check for i/o error from
674 * the first prop_register(), but I guess I like to go
675 * overboard...
676 */
677 error = dsl_prop_register(ds,
678 zfs_prop_to_name(ZFS_PROP_ATIME), atime_changed_cb, zfsvfs);
679 error = error ? error : dsl_prop_register(ds,
680 zfs_prop_to_name(ZFS_PROP_XATTR), xattr_changed_cb, zfsvfs);
681 error = error ? error : dsl_prop_register(ds,
682 zfs_prop_to_name(ZFS_PROP_RECORDSIZE), blksz_changed_cb, zfsvfs);
683 error = error ? error : dsl_prop_register(ds,
684 zfs_prop_to_name(ZFS_PROP_READONLY), readonly_changed_cb, zfsvfs);
685 #ifdef illumos
686 error = error ? error : dsl_prop_register(ds,
687 zfs_prop_to_name(ZFS_PROP_DEVICES), devices_changed_cb, zfsvfs);
688 #endif
689 error = error ? error : dsl_prop_register(ds,
690 zfs_prop_to_name(ZFS_PROP_SETUID), setuid_changed_cb, zfsvfs);
691 error = error ? error : dsl_prop_register(ds,
692 zfs_prop_to_name(ZFS_PROP_EXEC), exec_changed_cb, zfsvfs);
693 error = error ? error : dsl_prop_register(ds,
694 zfs_prop_to_name(ZFS_PROP_SNAPDIR), snapdir_changed_cb, zfsvfs);
695 error = error ? error : dsl_prop_register(ds,
696 zfs_prop_to_name(ZFS_PROP_ACLMODE), acl_mode_changed_cb, zfsvfs);
697 error = error ? error : dsl_prop_register(ds,
698 zfs_prop_to_name(ZFS_PROP_ACLINHERIT), acl_inherit_changed_cb,
699 zfsvfs);
700 error = error ? error : dsl_prop_register(ds,
701 zfs_prop_to_name(ZFS_PROP_VSCAN), vscan_changed_cb, zfsvfs);
702 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
703 if (error)
704 goto unregister;
705
706 /*
707 * Invoke our callbacks to restore temporary mount options.
708 */
709 if (do_readonly)
710 readonly_changed_cb(zfsvfs, readonly);
711 if (do_setuid)
712 setuid_changed_cb(zfsvfs, setuid);
713 if (do_exec)
714 exec_changed_cb(zfsvfs, exec);
715 if (do_xattr)
716 xattr_changed_cb(zfsvfs, xattr);
717 if (do_atime)
718 atime_changed_cb(zfsvfs, atime);
719
720 nbmand_changed_cb(zfsvfs, nbmand);
721
722 return (0);
723
724 unregister:
725 dsl_prop_unregister_all(ds, zfsvfs);
726 return (error);
727 }
728
729 static int
zfs_space_delta_cb(dmu_object_type_t bonustype,void * data,uint64_t * userp,uint64_t * groupp)730 zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
731 uint64_t *userp, uint64_t *groupp)
732 {
733 /*
734 * Is it a valid type of object to track?
735 */
736 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
737 return (SET_ERROR(ENOENT));
738
739 /*
740 * If we have a NULL data pointer
741 * then assume the id's aren't changing and
742 * return EEXIST to the dmu to let it know to
743 * use the same ids
744 */
745 if (data == NULL)
746 return (SET_ERROR(EEXIST));
747
748 if (bonustype == DMU_OT_ZNODE) {
749 znode_phys_t *znp = data;
750 *userp = znp->zp_uid;
751 *groupp = znp->zp_gid;
752 } else {
753 int hdrsize;
754 sa_hdr_phys_t *sap = data;
755 sa_hdr_phys_t sa = *sap;
756 boolean_t swap = B_FALSE;
757
758 ASSERT(bonustype == DMU_OT_SA);
759
760 if (sa.sa_magic == 0) {
761 /*
762 * This should only happen for newly created
763 * files that haven't had the znode data filled
764 * in yet.
765 */
766 *userp = 0;
767 *groupp = 0;
768 return (0);
769 }
770 if (sa.sa_magic == BSWAP_32(SA_MAGIC)) {
771 sa.sa_magic = SA_MAGIC;
772 sa.sa_layout_info = BSWAP_16(sa.sa_layout_info);
773 swap = B_TRUE;
774 } else {
775 VERIFY3U(sa.sa_magic, ==, SA_MAGIC);
776 }
777
778 hdrsize = sa_hdrsize(&sa);
779 VERIFY3U(hdrsize, >=, sizeof (sa_hdr_phys_t));
780 *userp = *((uint64_t *)((uintptr_t)data + hdrsize +
781 SA_UID_OFFSET));
782 *groupp = *((uint64_t *)((uintptr_t)data + hdrsize +
783 SA_GID_OFFSET));
784 if (swap) {
785 *userp = BSWAP_64(*userp);
786 *groupp = BSWAP_64(*groupp);
787 }
788 }
789 return (0);
790 }
791
792 static void
fuidstr_to_sid(zfsvfs_t * zfsvfs,const char * fuidstr,char * domainbuf,int buflen,uid_t * ridp)793 fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
794 char *domainbuf, int buflen, uid_t *ridp)
795 {
796 uint64_t fuid;
797 const char *domain;
798
799 fuid = zfs_strtonum(fuidstr, NULL);
800
801 domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
802 if (domain)
803 (void) strlcpy(domainbuf, domain, buflen);
804 else
805 domainbuf[0] = '\0';
806 *ridp = FUID_RID(fuid);
807 }
808
809 static uint64_t
zfs_userquota_prop_to_obj(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type)810 zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
811 {
812 switch (type) {
813 case ZFS_PROP_USERUSED:
814 return (DMU_USERUSED_OBJECT);
815 case ZFS_PROP_GROUPUSED:
816 return (DMU_GROUPUSED_OBJECT);
817 case ZFS_PROP_USERQUOTA:
818 return (zfsvfs->z_userquota_obj);
819 case ZFS_PROP_GROUPQUOTA:
820 return (zfsvfs->z_groupquota_obj);
821 }
822 return (0);
823 }
824
825 int
zfs_userspace_many(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,uint64_t * cookiep,void * vbuf,uint64_t * bufsizep)826 zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
827 uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
828 {
829 int error;
830 zap_cursor_t zc;
831 zap_attribute_t za;
832 zfs_useracct_t *buf = vbuf;
833 uint64_t obj;
834
835 if (!dmu_objset_userspace_present(zfsvfs->z_os))
836 return (SET_ERROR(ENOTSUP));
837
838 obj = zfs_userquota_prop_to_obj(zfsvfs, type);
839 if (obj == 0) {
840 *bufsizep = 0;
841 return (0);
842 }
843
844 for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
845 (error = zap_cursor_retrieve(&zc, &za)) == 0;
846 zap_cursor_advance(&zc)) {
847 if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
848 *bufsizep)
849 break;
850
851 fuidstr_to_sid(zfsvfs, za.za_name,
852 buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
853
854 buf->zu_space = za.za_first_integer;
855 buf++;
856 }
857 if (error == ENOENT)
858 error = 0;
859
860 ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
861 *bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
862 *cookiep = zap_cursor_serialize(&zc);
863 zap_cursor_fini(&zc);
864 return (error);
865 }
866
867 /*
868 * buf must be big enough (eg, 32 bytes)
869 */
870 static int
id_to_fuidstr(zfsvfs_t * zfsvfs,const char * domain,uid_t rid,char * buf,boolean_t addok)871 id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
872 char *buf, boolean_t addok)
873 {
874 uint64_t fuid;
875 int domainid = 0;
876
877 if (domain && domain[0]) {
878 domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
879 if (domainid == -1)
880 return (SET_ERROR(ENOENT));
881 }
882 fuid = FUID_ENCODE(domainid, rid);
883 (void) sprintf(buf, "%llx", (longlong_t)fuid);
884 return (0);
885 }
886
887 int
zfs_userspace_one(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,const char * domain,uint64_t rid,uint64_t * valp)888 zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
889 const char *domain, uint64_t rid, uint64_t *valp)
890 {
891 char buf[32];
892 int err;
893 uint64_t obj;
894
895 *valp = 0;
896
897 if (!dmu_objset_userspace_present(zfsvfs->z_os))
898 return (SET_ERROR(ENOTSUP));
899
900 obj = zfs_userquota_prop_to_obj(zfsvfs, type);
901 if (obj == 0)
902 return (0);
903
904 err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_FALSE);
905 if (err)
906 return (err);
907
908 err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
909 if (err == ENOENT)
910 err = 0;
911 return (err);
912 }
913
914 int
zfs_set_userquota(zfsvfs_t * zfsvfs,zfs_userquota_prop_t type,const char * domain,uint64_t rid,uint64_t quota)915 zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
916 const char *domain, uint64_t rid, uint64_t quota)
917 {
918 char buf[32];
919 int err;
920 dmu_tx_t *tx;
921 uint64_t *objp;
922 boolean_t fuid_dirtied;
923
924 if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
925 return (SET_ERROR(EINVAL));
926
927 if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
928 return (SET_ERROR(ENOTSUP));
929
930 objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj :
931 &zfsvfs->z_groupquota_obj;
932
933 err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
934 if (err)
935 return (err);
936 fuid_dirtied = zfsvfs->z_fuid_dirty;
937
938 tx = dmu_tx_create(zfsvfs->z_os);
939 dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
940 if (*objp == 0) {
941 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
942 zfs_userquota_prop_prefixes[type]);
943 }
944 if (fuid_dirtied)
945 zfs_fuid_txhold(zfsvfs, tx);
946 err = dmu_tx_assign(tx, TXG_WAIT);
947 if (err) {
948 dmu_tx_abort(tx);
949 return (err);
950 }
951
952 mutex_enter(&zfsvfs->z_lock);
953 if (*objp == 0) {
954 *objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
955 DMU_OT_NONE, 0, tx);
956 VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
957 zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
958 }
959 mutex_exit(&zfsvfs->z_lock);
960
961 if (quota == 0) {
962 err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
963 if (err == ENOENT)
964 err = 0;
965 } else {
966 err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, "a, tx);
967 }
968 ASSERT(err == 0);
969 if (fuid_dirtied)
970 zfs_fuid_sync(zfsvfs, tx);
971 dmu_tx_commit(tx);
972 return (err);
973 }
974
975 boolean_t
zfs_fuid_overquota(zfsvfs_t * zfsvfs,boolean_t isgroup,uint64_t fuid)976 zfs_fuid_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid)
977 {
978 char buf[32];
979 uint64_t used, quota, usedobj, quotaobj;
980 int err;
981
982 usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
983 quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
984
985 if (quotaobj == 0 || zfsvfs->z_replay)
986 return (B_FALSE);
987
988 (void) sprintf(buf, "%llx", (longlong_t)fuid);
989 err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, "a);
990 if (err != 0)
991 return (B_FALSE);
992
993 err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
994 if (err != 0)
995 return (B_FALSE);
996 return (used >= quota);
997 }
998
999 boolean_t
zfs_owner_overquota(zfsvfs_t * zfsvfs,znode_t * zp,boolean_t isgroup)1000 zfs_owner_overquota(zfsvfs_t *zfsvfs, znode_t *zp, boolean_t isgroup)
1001 {
1002 uint64_t fuid;
1003 uint64_t quotaobj;
1004
1005 quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
1006
1007 fuid = isgroup ? zp->z_gid : zp->z_uid;
1008
1009 if (quotaobj == 0 || zfsvfs->z_replay)
1010 return (B_FALSE);
1011
1012 return (zfs_fuid_overquota(zfsvfs, isgroup, fuid));
1013 }
1014
1015 /*
1016 * Associate this zfsvfs with the given objset, which must be owned.
1017 * This will cache a bunch of on-disk state from the objset in the
1018 * zfsvfs.
1019 */
1020 static int
zfsvfs_init(zfsvfs_t * zfsvfs,objset_t * os)1021 zfsvfs_init(zfsvfs_t *zfsvfs, objset_t *os)
1022 {
1023 int error;
1024 uint64_t val;
1025
1026 zfsvfs->z_max_blksz = SPA_OLD_MAXBLOCKSIZE;
1027 zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
1028 zfsvfs->z_os = os;
1029
1030 error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
1031 if (error != 0)
1032 return (error);
1033 if (zfsvfs->z_version >
1034 zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
1035 (void) printf("Can't mount a version %lld file system "
1036 "on a version %lld pool\n. Pool must be upgraded to mount "
1037 "this file system.", (u_longlong_t)zfsvfs->z_version,
1038 (u_longlong_t)spa_version(dmu_objset_spa(os)));
1039 return (SET_ERROR(ENOTSUP));
1040 }
1041 error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &val);
1042 if (error != 0)
1043 return (error);
1044 zfsvfs->z_norm = (int)val;
1045
1046 error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &val);
1047 if (error != 0)
1048 return (error);
1049 zfsvfs->z_utf8 = (val != 0);
1050
1051 error = zfs_get_zplprop(os, ZFS_PROP_CASE, &val);
1052 if (error != 0)
1053 return (error);
1054 zfsvfs->z_case = (uint_t)val;
1055
1056 /*
1057 * Fold case on file systems that are always or sometimes case
1058 * insensitive.
1059 */
1060 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
1061 zfsvfs->z_case == ZFS_CASE_MIXED)
1062 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
1063
1064 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1065 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1066
1067 uint64_t sa_obj = 0;
1068 if (zfsvfs->z_use_sa) {
1069 /* should either have both of these objects or none */
1070 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
1071 &sa_obj);
1072 if (error != 0)
1073 return (error);
1074 }
1075
1076 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1077 &zfsvfs->z_attr_table);
1078 if (error != 0)
1079 return (error);
1080
1081 if (zfsvfs->z_version >= ZPL_VERSION_SA)
1082 sa_register_update_callback(os, zfs_sa_upgrade);
1083
1084 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
1085 &zfsvfs->z_root);
1086 if (error != 0)
1087 return (error);
1088 ASSERT(zfsvfs->z_root != 0);
1089
1090 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
1091 &zfsvfs->z_unlinkedobj);
1092 if (error != 0)
1093 return (error);
1094
1095 error = zap_lookup(os, MASTER_NODE_OBJ,
1096 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
1097 8, 1, &zfsvfs->z_userquota_obj);
1098 if (error == ENOENT)
1099 zfsvfs->z_userquota_obj = 0;
1100 else if (error != 0)
1101 return (error);
1102
1103 error = zap_lookup(os, MASTER_NODE_OBJ,
1104 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
1105 8, 1, &zfsvfs->z_groupquota_obj);
1106 if (error == ENOENT)
1107 zfsvfs->z_groupquota_obj = 0;
1108 else if (error != 0)
1109 return (error);
1110
1111 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
1112 &zfsvfs->z_fuid_obj);
1113 if (error == ENOENT)
1114 zfsvfs->z_fuid_obj = 0;
1115 else if (error != 0)
1116 return (error);
1117
1118 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
1119 &zfsvfs->z_shares_dir);
1120 if (error == ENOENT)
1121 zfsvfs->z_shares_dir = 0;
1122 else if (error != 0)
1123 return (error);
1124
1125 /*
1126 * Only use the name cache if we are looking for a
1127 * name on a file system that does not require normalization
1128 * or case folding. We can also look there if we happen to be
1129 * on a non-normalizing, mixed sensitivity file system IF we
1130 * are looking for the exact name (which is always the case on
1131 * FreeBSD).
1132 */
1133 zfsvfs->z_use_namecache = !zfsvfs->z_norm ||
1134 ((zfsvfs->z_case == ZFS_CASE_MIXED) &&
1135 !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER));
1136
1137 return (0);
1138 }
1139
1140 #if defined(__FreeBSD__)
1141 taskq_t *zfsvfs_taskq;
1142
1143 static void
zfsvfs_task_unlinked_drain(void * context,int pending __unused)1144 zfsvfs_task_unlinked_drain(void *context, int pending __unused)
1145 {
1146
1147 zfs_unlinked_drain((zfsvfs_t *)context);
1148 }
1149 #endif
1150
1151 int
zfsvfs_create(const char * osname,zfsvfs_t ** zfvp)1152 zfsvfs_create(const char *osname, zfsvfs_t **zfvp)
1153 {
1154 objset_t *os;
1155 zfsvfs_t *zfsvfs;
1156 int error;
1157
1158 /*
1159 * XXX: Fix struct statfs so this isn't necessary!
1160 *
1161 * The 'osname' is used as the filesystem's special node, which means
1162 * it must fit in statfs.f_mntfromname, or else it can't be
1163 * enumerated, so libzfs_mnttab_find() returns NULL, which causes
1164 * 'zfs unmount' to think it's not mounted when it is.
1165 */
1166 if (strlen(osname) >= MNAMELEN)
1167 return (SET_ERROR(ENAMETOOLONG));
1168
1169 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
1170
1171 /*
1172 * We claim to always be readonly so we can open snapshots;
1173 * other ZPL code will prevent us from writing to snapshots.
1174 */
1175
1176 error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os);
1177 if (error != 0) {
1178 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1179 return (error);
1180 }
1181
1182 error = zfsvfs_create_impl(zfvp, zfsvfs, os);
1183 if (error != 0) {
1184 dmu_objset_disown(os, zfsvfs);
1185 }
1186 return (error);
1187 }
1188
1189
1190 int
zfsvfs_create_impl(zfsvfs_t ** zfvp,zfsvfs_t * zfsvfs,objset_t * os)1191 zfsvfs_create_impl(zfsvfs_t **zfvp, zfsvfs_t *zfsvfs, objset_t *os)
1192 {
1193 int error;
1194
1195 zfsvfs->z_vfs = NULL;
1196 zfsvfs->z_parent = zfsvfs;
1197
1198 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1199 mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
1200 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
1201 offsetof(znode_t, z_link_node));
1202 #if defined(__FreeBSD__)
1203 TASK_INIT(&zfsvfs->z_unlinked_drain_task, 0,
1204 zfsvfs_task_unlinked_drain, zfsvfs);
1205 #endif
1206 #ifdef DIAGNOSTIC
1207 rrm_init(&zfsvfs->z_teardown_lock, B_TRUE);
1208 #else
1209 rrm_init(&zfsvfs->z_teardown_lock, B_FALSE);
1210 #endif
1211 rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
1212 rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
1213 for (int i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1214 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1215
1216 rm_init(&zfsvfs->z_rootvnodelock, "zfs root vnode lock");
1217
1218 error = zfsvfs_init(zfsvfs, os);
1219 if (error != 0) {
1220 *zfvp = NULL;
1221 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1222 return (error);
1223 }
1224
1225 *zfvp = zfsvfs;
1226 return (0);
1227 }
1228
1229 static int
zfsvfs_setup(zfsvfs_t * zfsvfs,boolean_t mounting)1230 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
1231 {
1232 int error;
1233
1234 error = zfs_register_callbacks(zfsvfs->z_vfs);
1235 if (error)
1236 return (error);
1237
1238 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
1239
1240 /*
1241 * If we are not mounting (ie: online recv), then we don't
1242 * have to worry about replaying the log as we blocked all
1243 * operations out since we closed the ZIL.
1244 */
1245 if (mounting) {
1246 boolean_t readonly;
1247
1248 ASSERT3P(zfsvfs->z_kstat.dk_kstats, ==, NULL);
1249 dataset_kstats_create(&zfsvfs->z_kstat, zfsvfs->z_os);
1250
1251 /*
1252 * During replay we remove the read only flag to
1253 * allow replays to succeed.
1254 */
1255 readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
1256 if (readonly != 0)
1257 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
1258 else
1259 zfs_unlinked_drain(zfsvfs);
1260
1261 /*
1262 * Parse and replay the intent log.
1263 *
1264 * Because of ziltest, this must be done after
1265 * zfs_unlinked_drain(). (Further note: ziltest
1266 * doesn't use readonly mounts, where
1267 * zfs_unlinked_drain() isn't called.) This is because
1268 * ziltest causes spa_sync() to think it's committed,
1269 * but actually it is not, so the intent log contains
1270 * many txg's worth of changes.
1271 *
1272 * In particular, if object N is in the unlinked set in
1273 * the last txg to actually sync, then it could be
1274 * actually freed in a later txg and then reallocated
1275 * in a yet later txg. This would write a "create
1276 * object N" record to the intent log. Normally, this
1277 * would be fine because the spa_sync() would have
1278 * written out the fact that object N is free, before
1279 * we could write the "create object N" intent log
1280 * record.
1281 *
1282 * But when we are in ziltest mode, we advance the "open
1283 * txg" without actually spa_sync()-ing the changes to
1284 * disk. So we would see that object N is still
1285 * allocated and in the unlinked set, and there is an
1286 * intent log record saying to allocate it.
1287 */
1288 if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
1289 if (zil_replay_disable) {
1290 zil_destroy(zfsvfs->z_log, B_FALSE);
1291 } else {
1292 zfsvfs->z_replay = B_TRUE;
1293 zil_replay(zfsvfs->z_os, zfsvfs,
1294 zfs_replay_vector);
1295 zfsvfs->z_replay = B_FALSE;
1296 }
1297 }
1298 zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
1299 }
1300
1301 /*
1302 * Set the objset user_ptr to track its zfsvfs.
1303 */
1304 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1305 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1306 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1307
1308 return (0);
1309 }
1310
1311 extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
1312
1313 void
zfsvfs_free(zfsvfs_t * zfsvfs)1314 zfsvfs_free(zfsvfs_t *zfsvfs)
1315 {
1316 int i;
1317
1318 /*
1319 * This is a barrier to prevent the filesystem from going away in
1320 * zfs_znode_move() until we can safely ensure that the filesystem is
1321 * not unmounted. We consider the filesystem valid before the barrier
1322 * and invalid after the barrier.
1323 */
1324 rw_enter(&zfsvfs_lock, RW_READER);
1325 rw_exit(&zfsvfs_lock);
1326
1327 rm_destroy(&zfsvfs->z_rootvnodelock);
1328
1329 zfs_fuid_destroy(zfsvfs);
1330
1331 mutex_destroy(&zfsvfs->z_znodes_lock);
1332 mutex_destroy(&zfsvfs->z_lock);
1333 list_destroy(&zfsvfs->z_all_znodes);
1334 rrm_destroy(&zfsvfs->z_teardown_lock);
1335 rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1336 rw_destroy(&zfsvfs->z_fuid_lock);
1337 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1338 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1339 dataset_kstats_destroy(&zfsvfs->z_kstat);
1340 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1341 }
1342
1343 static void
zfs_set_fuid_feature(zfsvfs_t * zfsvfs)1344 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
1345 {
1346 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1347 if (zfsvfs->z_vfs) {
1348 if (zfsvfs->z_use_fuids) {
1349 vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1350 vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1351 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1352 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1353 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1354 vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1355 } else {
1356 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1357 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1358 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1359 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1360 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1361 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1362 }
1363 }
1364 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1365 }
1366
1367 static int
zfs_domount(vfs_t * vfsp,char * osname)1368 zfs_domount(vfs_t *vfsp, char *osname)
1369 {
1370 uint64_t recordsize, fsid_guid;
1371 int error = 0;
1372 zfsvfs_t *zfsvfs;
1373 vnode_t *vp;
1374
1375 ASSERT(vfsp);
1376 ASSERT(osname);
1377
1378 error = zfsvfs_create(osname, &zfsvfs);
1379 if (error)
1380 return (error);
1381 zfsvfs->z_vfs = vfsp;
1382
1383 #ifdef illumos
1384 /* Initialize the generic filesystem structure. */
1385 vfsp->vfs_bcount = 0;
1386 vfsp->vfs_data = NULL;
1387
1388 if (zfs_create_unique_device(&mount_dev) == -1) {
1389 error = SET_ERROR(ENODEV);
1390 goto out;
1391 }
1392 ASSERT(vfs_devismounted(mount_dev) == 0);
1393 #endif
1394
1395 if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
1396 NULL))
1397 goto out;
1398 zfsvfs->z_vfs->vfs_bsize = SPA_MINBLOCKSIZE;
1399 zfsvfs->z_vfs->mnt_stat.f_iosize = recordsize;
1400
1401 vfsp->vfs_data = zfsvfs;
1402 vfsp->mnt_flag |= MNT_LOCAL;
1403 vfsp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
1404 vfsp->mnt_kern_flag |= MNTK_SHARED_WRITES;
1405 vfsp->mnt_kern_flag |= MNTK_EXTENDED_SHARED;
1406 vfsp->mnt_kern_flag |= MNTK_NO_IOPF; /* vn_io_fault can be used */
1407 vfsp->mnt_kern_flag |= MNTK_VMSETSIZE_BUG;
1408
1409 /*
1410 * The fsid is 64 bits, composed of an 8-bit fs type, which
1411 * separates our fsid from any other filesystem types, and a
1412 * 56-bit objset unique ID. The objset unique ID is unique to
1413 * all objsets open on this system, provided by unique_create().
1414 * The 8-bit fs type must be put in the low bits of fsid[1]
1415 * because that's where other Solaris filesystems put it.
1416 */
1417 fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
1418 ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
1419 vfsp->vfs_fsid.val[0] = fsid_guid;
1420 vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
1421 vfsp->mnt_vfc->vfc_typenum & 0xFF;
1422
1423 /*
1424 * Set features for file system.
1425 */
1426 zfs_set_fuid_feature(zfsvfs);
1427 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1428 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1429 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1430 vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1431 } else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1432 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1433 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1434 }
1435 vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1436
1437 if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1438 uint64_t pval;
1439
1440 atime_changed_cb(zfsvfs, B_FALSE);
1441 readonly_changed_cb(zfsvfs, B_TRUE);
1442 if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
1443 goto out;
1444 xattr_changed_cb(zfsvfs, pval);
1445 zfsvfs->z_issnap = B_TRUE;
1446 zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1447
1448 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1449 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1450 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1451 } else {
1452 error = zfsvfs_setup(zfsvfs, B_TRUE);
1453 }
1454
1455 vfs_mountedfrom(vfsp, osname);
1456
1457 if (!zfsvfs->z_issnap)
1458 zfsctl_create(zfsvfs);
1459 out:
1460 if (error) {
1461 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1462 zfsvfs_free(zfsvfs);
1463 } else {
1464 atomic_inc_32(&zfs_active_fs_count);
1465 }
1466
1467 return (error);
1468 }
1469
1470 void
zfs_unregister_callbacks(zfsvfs_t * zfsvfs)1471 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1472 {
1473 objset_t *os = zfsvfs->z_os;
1474
1475 if (!dmu_objset_is_snapshot(os))
1476 dsl_prop_unregister_all(dmu_objset_ds(os), zfsvfs);
1477 }
1478
1479 #ifdef SECLABEL
1480 /*
1481 * Convert a decimal digit string to a uint64_t integer.
1482 */
1483 static int
str_to_uint64(char * str,uint64_t * objnum)1484 str_to_uint64(char *str, uint64_t *objnum)
1485 {
1486 uint64_t num = 0;
1487
1488 while (*str) {
1489 if (*str < '0' || *str > '9')
1490 return (SET_ERROR(EINVAL));
1491
1492 num = num*10 + *str++ - '0';
1493 }
1494
1495 *objnum = num;
1496 return (0);
1497 }
1498
1499 /*
1500 * The boot path passed from the boot loader is in the form of
1501 * "rootpool-name/root-filesystem-object-number'. Convert this
1502 * string to a dataset name: "rootpool-name/root-filesystem-name".
1503 */
1504 static int
zfs_parse_bootfs(char * bpath,char * outpath)1505 zfs_parse_bootfs(char *bpath, char *outpath)
1506 {
1507 char *slashp;
1508 uint64_t objnum;
1509 int error;
1510
1511 if (*bpath == 0 || *bpath == '/')
1512 return (SET_ERROR(EINVAL));
1513
1514 (void) strcpy(outpath, bpath);
1515
1516 slashp = strchr(bpath, '/');
1517
1518 /* if no '/', just return the pool name */
1519 if (slashp == NULL) {
1520 return (0);
1521 }
1522
1523 /* if not a number, just return the root dataset name */
1524 if (str_to_uint64(slashp+1, &objnum)) {
1525 return (0);
1526 }
1527
1528 *slashp = '\0';
1529 error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
1530 *slashp = '/';
1531
1532 return (error);
1533 }
1534
1535 /*
1536 * Check that the hex label string is appropriate for the dataset being
1537 * mounted into the global_zone proper.
1538 *
1539 * Return an error if the hex label string is not default or
1540 * admin_low/admin_high. For admin_low labels, the corresponding
1541 * dataset must be readonly.
1542 */
1543 int
zfs_check_global_label(const char * dsname,const char * hexsl)1544 zfs_check_global_label(const char *dsname, const char *hexsl)
1545 {
1546 if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1547 return (0);
1548 if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1549 return (0);
1550 if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1551 /* must be readonly */
1552 uint64_t rdonly;
1553
1554 if (dsl_prop_get_integer(dsname,
1555 zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1556 return (SET_ERROR(EACCES));
1557 return (rdonly ? 0 : EACCES);
1558 }
1559 return (SET_ERROR(EACCES));
1560 }
1561
1562 /*
1563 * Determine whether the mount is allowed according to MAC check.
1564 * by comparing (where appropriate) label of the dataset against
1565 * the label of the zone being mounted into. If the dataset has
1566 * no label, create one.
1567 *
1568 * Returns 0 if access allowed, error otherwise (e.g. EACCES)
1569 */
1570 static int
zfs_mount_label_policy(vfs_t * vfsp,char * osname)1571 zfs_mount_label_policy(vfs_t *vfsp, char *osname)
1572 {
1573 int error, retv;
1574 zone_t *mntzone = NULL;
1575 ts_label_t *mnt_tsl;
1576 bslabel_t *mnt_sl;
1577 bslabel_t ds_sl;
1578 char ds_hexsl[MAXNAMELEN];
1579
1580 retv = EACCES; /* assume the worst */
1581
1582 /*
1583 * Start by getting the dataset label if it exists.
1584 */
1585 error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1586 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
1587 if (error)
1588 return (SET_ERROR(EACCES));
1589
1590 /*
1591 * If labeling is NOT enabled, then disallow the mount of datasets
1592 * which have a non-default label already. No other label checks
1593 * are needed.
1594 */
1595 if (!is_system_labeled()) {
1596 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1597 return (0);
1598 return (SET_ERROR(EACCES));
1599 }
1600
1601 /*
1602 * Get the label of the mountpoint. If mounting into the global
1603 * zone (i.e. mountpoint is not within an active zone and the
1604 * zoned property is off), the label must be default or
1605 * admin_low/admin_high only; no other checks are needed.
1606 */
1607 mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
1608 if (mntzone->zone_id == GLOBAL_ZONEID) {
1609 uint64_t zoned;
1610
1611 zone_rele(mntzone);
1612
1613 if (dsl_prop_get_integer(osname,
1614 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
1615 return (SET_ERROR(EACCES));
1616 if (!zoned)
1617 return (zfs_check_global_label(osname, ds_hexsl));
1618 else
1619 /*
1620 * This is the case of a zone dataset being mounted
1621 * initially, before the zone has been fully created;
1622 * allow this mount into global zone.
1623 */
1624 return (0);
1625 }
1626
1627 mnt_tsl = mntzone->zone_slabel;
1628 ASSERT(mnt_tsl != NULL);
1629 label_hold(mnt_tsl);
1630 mnt_sl = label2bslabel(mnt_tsl);
1631
1632 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
1633 /*
1634 * The dataset doesn't have a real label, so fabricate one.
1635 */
1636 char *str = NULL;
1637
1638 if (l_to_str_internal(mnt_sl, &str) == 0 &&
1639 dsl_prop_set_string(osname,
1640 zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1641 ZPROP_SRC_LOCAL, str) == 0)
1642 retv = 0;
1643 if (str != NULL)
1644 kmem_free(str, strlen(str) + 1);
1645 } else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
1646 /*
1647 * Now compare labels to complete the MAC check. If the
1648 * labels are equal then allow access. If the mountpoint
1649 * label dominates the dataset label, allow readonly access.
1650 * Otherwise, access is denied.
1651 */
1652 if (blequal(mnt_sl, &ds_sl))
1653 retv = 0;
1654 else if (bldominates(mnt_sl, &ds_sl)) {
1655 vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1656 retv = 0;
1657 }
1658 }
1659
1660 label_rele(mnt_tsl);
1661 zone_rele(mntzone);
1662 return (retv);
1663 }
1664 #endif /* SECLABEL */
1665
1666 #ifdef OPENSOLARIS_MOUNTROOT
1667 static int
zfs_mountroot(vfs_t * vfsp,enum whymountroot why)1668 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
1669 {
1670 int error = 0;
1671 static int zfsrootdone = 0;
1672 zfsvfs_t *zfsvfs = NULL;
1673 znode_t *zp = NULL;
1674 vnode_t *vp = NULL;
1675 char *zfs_bootfs;
1676 char *zfs_devid;
1677
1678 ASSERT(vfsp);
1679
1680 /*
1681 * The filesystem that we mount as root is defined in the
1682 * boot property "zfs-bootfs" with a format of
1683 * "poolname/root-dataset-objnum".
1684 */
1685 if (why == ROOT_INIT) {
1686 if (zfsrootdone++)
1687 return (SET_ERROR(EBUSY));
1688 /*
1689 * the process of doing a spa_load will require the
1690 * clock to be set before we could (for example) do
1691 * something better by looking at the timestamp on
1692 * an uberblock, so just set it to -1.
1693 */
1694 clkset(-1);
1695
1696 if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
1697 cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
1698 "bootfs name");
1699 return (SET_ERROR(EINVAL));
1700 }
1701 zfs_devid = spa_get_bootprop("diskdevid");
1702 error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
1703 if (zfs_devid)
1704 spa_free_bootprop(zfs_devid);
1705 if (error) {
1706 spa_free_bootprop(zfs_bootfs);
1707 cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
1708 error);
1709 return (error);
1710 }
1711 if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
1712 spa_free_bootprop(zfs_bootfs);
1713 cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
1714 error);
1715 return (error);
1716 }
1717
1718 spa_free_bootprop(zfs_bootfs);
1719
1720 if (error = vfs_lock(vfsp))
1721 return (error);
1722
1723 if (error = zfs_domount(vfsp, rootfs.bo_name)) {
1724 cmn_err(CE_NOTE, "zfs_domount: error %d", error);
1725 goto out;
1726 }
1727
1728 zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
1729 ASSERT(zfsvfs);
1730 if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
1731 cmn_err(CE_NOTE, "zfs_zget: error %d", error);
1732 goto out;
1733 }
1734
1735 vp = ZTOV(zp);
1736 mutex_enter(&vp->v_lock);
1737 vp->v_flag |= VROOT;
1738 mutex_exit(&vp->v_lock);
1739 rootvp = vp;
1740
1741 /*
1742 * Leave rootvp held. The root file system is never unmounted.
1743 */
1744
1745 vfs_add((struct vnode *)0, vfsp,
1746 (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
1747 out:
1748 vfs_unlock(vfsp);
1749 return (error);
1750 } else if (why == ROOT_REMOUNT) {
1751 readonly_changed_cb(vfsp->vfs_data, B_FALSE);
1752 vfsp->vfs_flag |= VFS_REMOUNT;
1753
1754 /* refresh mount options */
1755 zfs_unregister_callbacks(vfsp->vfs_data);
1756 return (zfs_register_callbacks(vfsp));
1757
1758 } else if (why == ROOT_UNMOUNT) {
1759 zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
1760 (void) zfs_sync(vfsp, 0, 0);
1761 return (0);
1762 }
1763
1764 /*
1765 * if "why" is equal to anything else other than ROOT_INIT,
1766 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
1767 */
1768 return (SET_ERROR(ENOTSUP));
1769 }
1770 #endif /* OPENSOLARIS_MOUNTROOT */
1771
1772 static int
getpoolname(const char * osname,char * poolname)1773 getpoolname(const char *osname, char *poolname)
1774 {
1775 char *p;
1776
1777 p = strchr(osname, '/');
1778 if (p == NULL) {
1779 if (strlen(osname) >= MAXNAMELEN)
1780 return (ENAMETOOLONG);
1781 (void) strcpy(poolname, osname);
1782 } else {
1783 if (p - osname >= MAXNAMELEN)
1784 return (ENAMETOOLONG);
1785 (void) strncpy(poolname, osname, p - osname);
1786 poolname[p - osname] = '\0';
1787 }
1788 return (0);
1789 }
1790
1791 /*ARGSUSED*/
1792 static int
zfs_mount(vfs_t * vfsp)1793 zfs_mount(vfs_t *vfsp)
1794 {
1795 kthread_t *td = curthread;
1796 vnode_t *mvp = vfsp->mnt_vnodecovered;
1797 cred_t *cr = td->td_ucred;
1798 char *osname;
1799 int error = 0;
1800 int canwrite;
1801
1802 #ifdef illumos
1803 if (mvp->v_type != VDIR)
1804 return (SET_ERROR(ENOTDIR));
1805
1806 mutex_enter(&mvp->v_lock);
1807 if ((uap->flags & MS_REMOUNT) == 0 &&
1808 (uap->flags & MS_OVERLAY) == 0 &&
1809 (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
1810 mutex_exit(&mvp->v_lock);
1811 return (SET_ERROR(EBUSY));
1812 }
1813 mutex_exit(&mvp->v_lock);
1814
1815 /*
1816 * ZFS does not support passing unparsed data in via MS_DATA.
1817 * Users should use the MS_OPTIONSTR interface; this means
1818 * that all option parsing is already done and the options struct
1819 * can be interrogated.
1820 */
1821 if ((uap->flags & MS_DATA) && uap->datalen > 0)
1822 return (SET_ERROR(EINVAL));
1823
1824 /*
1825 * Get the objset name (the "special" mount argument).
1826 */
1827 if (error = pn_get(uap->spec, fromspace, &spn))
1828 return (error);
1829
1830 osname = spn.pn_path;
1831 #else /* !illumos */
1832 if (vfs_getopt(vfsp->mnt_optnew, "from", (void **)&osname, NULL))
1833 return (SET_ERROR(EINVAL));
1834
1835 /*
1836 * If full-owner-access is enabled and delegated administration is
1837 * turned on, we must set nosuid.
1838 */
1839 if (zfs_super_owner &&
1840 dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != ECANCELED) {
1841 secpolicy_fs_mount_clearopts(cr, vfsp);
1842 }
1843 #endif /* illumos */
1844
1845 /*
1846 * Check for mount privilege?
1847 *
1848 * If we don't have privilege then see if
1849 * we have local permission to allow it
1850 */
1851 error = secpolicy_fs_mount(cr, mvp, vfsp);
1852 if (error) {
1853 if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != 0)
1854 goto out;
1855
1856 if (!(vfsp->vfs_flag & MS_REMOUNT)) {
1857 vattr_t vattr;
1858
1859 /*
1860 * Make sure user is the owner of the mount point
1861 * or has sufficient privileges.
1862 */
1863
1864 vattr.va_mask = AT_UID;
1865
1866 vn_lock(mvp, LK_SHARED | LK_RETRY);
1867 if (VOP_GETATTR(mvp, &vattr, cr)) {
1868 VOP_UNLOCK(mvp, 0);
1869 goto out;
1870 }
1871
1872 if (secpolicy_vnode_owner(mvp, cr, vattr.va_uid) != 0 &&
1873 VOP_ACCESS(mvp, VWRITE, cr, td) != 0) {
1874 VOP_UNLOCK(mvp, 0);
1875 goto out;
1876 }
1877 VOP_UNLOCK(mvp, 0);
1878 }
1879
1880 secpolicy_fs_mount_clearopts(cr, vfsp);
1881 }
1882
1883 /*
1884 * Refuse to mount a filesystem if we are in a local zone and the
1885 * dataset is not visible.
1886 */
1887 if (!INGLOBALZONE(curthread) &&
1888 (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1889 error = SET_ERROR(EPERM);
1890 goto out;
1891 }
1892
1893 #ifdef SECLABEL
1894 error = zfs_mount_label_policy(vfsp, osname);
1895 if (error)
1896 goto out;
1897 #endif
1898
1899 vfsp->vfs_flag |= MNT_NFS4ACLS;
1900
1901 /*
1902 * When doing a remount, we simply refresh our temporary properties
1903 * according to those options set in the current VFS options.
1904 */
1905 if (vfsp->vfs_flag & MS_REMOUNT) {
1906 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1907
1908 /*
1909 * Refresh mount options with z_teardown_lock blocking I/O while
1910 * the filesystem is in an inconsistent state.
1911 * The lock also serializes this code with filesystem
1912 * manipulations between entry to zfs_suspend_fs() and return
1913 * from zfs_resume_fs().
1914 */
1915 rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1916 zfs_unregister_callbacks(zfsvfs);
1917 error = zfs_register_callbacks(vfsp);
1918 rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1919 goto out;
1920 }
1921
1922 /* Initial root mount: try hard to import the requested root pool. */
1923 if ((vfsp->vfs_flag & MNT_ROOTFS) != 0 &&
1924 (vfsp->vfs_flag & MNT_UPDATE) == 0) {
1925 char pname[MAXNAMELEN];
1926
1927 error = getpoolname(osname, pname);
1928 if (error == 0)
1929 error = spa_import_rootpool(pname);
1930 if (error)
1931 goto out;
1932 }
1933 DROP_GIANT();
1934 error = zfs_domount(vfsp, osname);
1935 PICKUP_GIANT();
1936
1937 if (error == 0)
1938 zfs_root_setvnode((zfsvfs_t *)vfsp->vfs_data);
1939
1940 #ifdef illumos
1941 /*
1942 * Add an extra VFS_HOLD on our parent vfs so that it can't
1943 * disappear due to a forced unmount.
1944 */
1945 if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
1946 VFS_HOLD(mvp->v_vfsp);
1947 #endif
1948
1949 out:
1950 return (error);
1951 }
1952
1953 static int
zfs_statfs(vfs_t * vfsp,struct statfs * statp)1954 zfs_statfs(vfs_t *vfsp, struct statfs *statp)
1955 {
1956 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1957 uint64_t refdbytes, availbytes, usedobjs, availobjs;
1958
1959 statp->f_version = STATFS_VERSION;
1960
1961 ZFS_ENTER(zfsvfs);
1962
1963 dmu_objset_space(zfsvfs->z_os,
1964 &refdbytes, &availbytes, &usedobjs, &availobjs);
1965
1966 /*
1967 * The underlying storage pool actually uses multiple block sizes.
1968 * We report the fragsize as the smallest block size we support,
1969 * and we report our blocksize as the filesystem's maximum blocksize.
1970 */
1971 statp->f_bsize = SPA_MINBLOCKSIZE;
1972 statp->f_iosize = zfsvfs->z_vfs->mnt_stat.f_iosize;
1973
1974 /*
1975 * The following report "total" blocks of various kinds in the
1976 * file system, but reported in terms of f_frsize - the
1977 * "fragment" size.
1978 */
1979
1980 statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1981 statp->f_bfree = availbytes / statp->f_bsize;
1982 statp->f_bavail = statp->f_bfree; /* no root reservation */
1983
1984 /*
1985 * statvfs() should really be called statufs(), because it assumes
1986 * static metadata. ZFS doesn't preallocate files, so the best
1987 * we can do is report the max that could possibly fit in f_files,
1988 * and that minus the number actually used in f_ffree.
1989 * For f_ffree, report the smaller of the number of object available
1990 * and the number of blocks (each object will take at least a block).
1991 */
1992 statp->f_ffree = MIN(availobjs, statp->f_bfree);
1993 statp->f_files = statp->f_ffree + usedobjs;
1994
1995 /*
1996 * We're a zfs filesystem.
1997 */
1998 (void) strlcpy(statp->f_fstypename, "zfs", sizeof(statp->f_fstypename));
1999
2000 strlcpy(statp->f_mntfromname, vfsp->mnt_stat.f_mntfromname,
2001 sizeof(statp->f_mntfromname));
2002 strlcpy(statp->f_mntonname, vfsp->mnt_stat.f_mntonname,
2003 sizeof(statp->f_mntonname));
2004
2005 statp->f_namemax = MAXNAMELEN - 1;
2006
2007 ZFS_EXIT(zfsvfs);
2008 return (0);
2009 }
2010
2011 static int
zfs_root_setvnode(zfsvfs_t * zfsvfs)2012 zfs_root_setvnode(zfsvfs_t *zfsvfs)
2013 {
2014 znode_t *rootzp;
2015 int error;
2016
2017 ZFS_ENTER(zfsvfs);
2018 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
2019 if (error != 0)
2020 panic("could not zfs_zget for root vnode");
2021 ZFS_EXIT(zfsvfs);
2022
2023 rm_wlock(&zfsvfs->z_rootvnodelock);
2024 if (zfsvfs->z_rootvnode != NULL)
2025 panic("zfs mount point already has a root vnode: %p\n",
2026 zfsvfs->z_rootvnode);
2027 zfsvfs->z_rootvnode = ZTOV(rootzp);
2028 rm_wunlock(&zfsvfs->z_rootvnodelock);
2029 return (0);
2030 }
2031
2032 static void
zfs_root_putvnode(zfsvfs_t * zfsvfs)2033 zfs_root_putvnode(zfsvfs_t *zfsvfs)
2034 {
2035 struct vnode *vp;
2036
2037 rm_wlock(&zfsvfs->z_rootvnodelock);
2038 vp = zfsvfs->z_rootvnode;
2039 zfsvfs->z_rootvnode = NULL;
2040 rm_wunlock(&zfsvfs->z_rootvnodelock);
2041 if (vp != NULL)
2042 vrele(vp);
2043 }
2044
2045 static int
zfs_root(vfs_t * vfsp,int flags,vnode_t ** vpp)2046 zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp)
2047 {
2048 struct rm_priotracker tracker;
2049 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2050 znode_t *rootzp;
2051 int error;
2052
2053 rm_rlock(&zfsvfs->z_rootvnodelock, &tracker);
2054 *vpp = zfsvfs->z_rootvnode;
2055 if (*vpp != NULL && (((*vpp)->v_iflag & VI_DOOMED) == 0)) {
2056 vrefact(*vpp);
2057 rm_runlock(&zfsvfs->z_rootvnodelock, &tracker);
2058 goto lock;
2059 }
2060 rm_runlock(&zfsvfs->z_rootvnodelock, &tracker);
2061
2062 /*
2063 * We found the vnode but did not like it.
2064 */
2065 if (*vpp != NULL) {
2066 *vpp = NULL;
2067 zfs_root_putvnode(zfsvfs);
2068 }
2069
2070 ZFS_ENTER(zfsvfs);
2071 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
2072 if (error == 0)
2073 *vpp = ZTOV(rootzp);
2074
2075 ZFS_EXIT(zfsvfs);
2076
2077 if (error == 0) {
2078 lock:
2079 error = vn_lock(*vpp, flags);
2080 if (error != 0) {
2081 VN_RELE(*vpp);
2082 *vpp = NULL;
2083 }
2084 }
2085 return (error);
2086 }
2087
2088 /*
2089 * Teardown the zfsvfs::z_os.
2090 *
2091 * Note, if 'unmounting' is FALSE, we return with the 'z_teardown_lock'
2092 * and 'z_teardown_inactive_lock' held.
2093 */
2094 static int
zfsvfs_teardown(zfsvfs_t * zfsvfs,boolean_t unmounting)2095 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
2096 {
2097 znode_t *zp;
2098
2099 rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
2100
2101 if (!unmounting) {
2102 /*
2103 * We purge the parent filesystem's vfsp as the parent
2104 * filesystem and all of its snapshots have their vnode's
2105 * v_vfsp set to the parent's filesystem's vfsp. Note,
2106 * 'z_parent' is self referential for non-snapshots.
2107 */
2108 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
2109 #ifdef FREEBSD_NAMECACHE
2110 cache_purgevfs(zfsvfs->z_parent->z_vfs, true);
2111 #endif
2112 }
2113
2114 /*
2115 * Close the zil. NB: Can't close the zil while zfs_inactive
2116 * threads are blocked as zil_close can call zfs_inactive.
2117 */
2118 if (zfsvfs->z_log) {
2119 zil_close(zfsvfs->z_log);
2120 zfsvfs->z_log = NULL;
2121 }
2122
2123 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
2124
2125 /*
2126 * If we are not unmounting (ie: online recv) and someone already
2127 * unmounted this file system while we were doing the switcheroo,
2128 * or a reopen of z_os failed then just bail out now.
2129 */
2130 if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
2131 rw_exit(&zfsvfs->z_teardown_inactive_lock);
2132 rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2133 return (SET_ERROR(EIO));
2134 }
2135
2136 /*
2137 * At this point there are no vops active, and any new vops will
2138 * fail with EIO since we have z_teardown_lock for writer (only
2139 * relavent for forced unmount).
2140 *
2141 * Release all holds on dbufs.
2142 */
2143 mutex_enter(&zfsvfs->z_znodes_lock);
2144 for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
2145 zp = list_next(&zfsvfs->z_all_znodes, zp))
2146 if (zp->z_sa_hdl) {
2147 ASSERT(ZTOV(zp)->v_count >= 0);
2148 zfs_znode_dmu_fini(zp);
2149 }
2150 mutex_exit(&zfsvfs->z_znodes_lock);
2151
2152 /*
2153 * If we are unmounting, set the unmounted flag and let new vops
2154 * unblock. zfs_inactive will have the unmounted behavior, and all
2155 * other vops will fail with EIO.
2156 */
2157 if (unmounting) {
2158 zfsvfs->z_unmounted = B_TRUE;
2159 rw_exit(&zfsvfs->z_teardown_inactive_lock);
2160 rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2161 }
2162
2163 /*
2164 * z_os will be NULL if there was an error in attempting to reopen
2165 * zfsvfs, so just return as the properties had already been
2166 * unregistered and cached data had been evicted before.
2167 */
2168 if (zfsvfs->z_os == NULL)
2169 return (0);
2170
2171 /*
2172 * Unregister properties.
2173 */
2174 zfs_unregister_callbacks(zfsvfs);
2175
2176 /*
2177 * Evict cached data
2178 */
2179 if (dsl_dataset_is_dirty(dmu_objset_ds(zfsvfs->z_os)) &&
2180 !(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
2181 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
2182 dmu_objset_evict_dbufs(zfsvfs->z_os);
2183
2184 return (0);
2185 }
2186
2187 /*ARGSUSED*/
2188 static int
zfs_umount(vfs_t * vfsp,int fflag)2189 zfs_umount(vfs_t *vfsp, int fflag)
2190 {
2191 kthread_t *td = curthread;
2192 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2193 objset_t *os;
2194 cred_t *cr = td->td_ucred;
2195 int ret;
2196
2197 zfs_root_putvnode(zfsvfs);
2198
2199 ret = secpolicy_fs_unmount(cr, vfsp);
2200 if (ret) {
2201 if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
2202 ZFS_DELEG_PERM_MOUNT, cr))
2203 return (ret);
2204 }
2205
2206 /*
2207 * We purge the parent filesystem's vfsp as the parent filesystem
2208 * and all of its snapshots have their vnode's v_vfsp set to the
2209 * parent's filesystem's vfsp. Note, 'z_parent' is self
2210 * referential for non-snapshots.
2211 */
2212 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
2213
2214 /*
2215 * Unmount any snapshots mounted under .zfs before unmounting the
2216 * dataset itself.
2217 */
2218 if (zfsvfs->z_ctldir != NULL) {
2219 if ((ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0)
2220 return (ret);
2221 }
2222
2223 if (fflag & MS_FORCE) {
2224 /*
2225 * Mark file system as unmounted before calling
2226 * vflush(FORCECLOSE). This way we ensure no future vnops
2227 * will be called and risk operating on DOOMED vnodes.
2228 */
2229 rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
2230 zfsvfs->z_unmounted = B_TRUE;
2231 rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2232 }
2233
2234 /*
2235 * Flush all the files.
2236 */
2237 ret = vflush(vfsp, 0, (fflag & MS_FORCE) ? FORCECLOSE : 0, td);
2238 if (ret != 0)
2239 return (ret);
2240
2241 #ifdef illumos
2242 if (!(fflag & MS_FORCE)) {
2243 /*
2244 * Check the number of active vnodes in the file system.
2245 * Our count is maintained in the vfs structure, but the
2246 * number is off by 1 to indicate a hold on the vfs
2247 * structure itself.
2248 *
2249 * The '.zfs' directory maintains a reference of its
2250 * own, and any active references underneath are
2251 * reflected in the vnode count.
2252 */
2253 if (zfsvfs->z_ctldir == NULL) {
2254 if (vfsp->vfs_count > 1)
2255 return (SET_ERROR(EBUSY));
2256 } else {
2257 if (vfsp->vfs_count > 2 ||
2258 zfsvfs->z_ctldir->v_count > 1)
2259 return (SET_ERROR(EBUSY));
2260 }
2261 }
2262 #endif
2263
2264 while (taskqueue_cancel(zfsvfs_taskq->tq_queue,
2265 &zfsvfs->z_unlinked_drain_task, NULL) != 0)
2266 taskqueue_drain(zfsvfs_taskq->tq_queue,
2267 &zfsvfs->z_unlinked_drain_task);
2268
2269 VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
2270 os = zfsvfs->z_os;
2271
2272 /*
2273 * z_os will be NULL if there was an error in
2274 * attempting to reopen zfsvfs.
2275 */
2276 if (os != NULL) {
2277 /*
2278 * Unset the objset user_ptr.
2279 */
2280 mutex_enter(&os->os_user_ptr_lock);
2281 dmu_objset_set_user(os, NULL);
2282 mutex_exit(&os->os_user_ptr_lock);
2283
2284 /*
2285 * Finally release the objset
2286 */
2287 dmu_objset_disown(os, zfsvfs);
2288 }
2289
2290 /*
2291 * We can now safely destroy the '.zfs' directory node.
2292 */
2293 if (zfsvfs->z_ctldir != NULL)
2294 zfsctl_destroy(zfsvfs);
2295 zfs_freevfs(vfsp);
2296
2297 return (0);
2298 }
2299
2300 static int
zfs_vget(vfs_t * vfsp,ino_t ino,int flags,vnode_t ** vpp)2301 zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp)
2302 {
2303 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2304 znode_t *zp;
2305 int err;
2306
2307 /*
2308 * zfs_zget() can't operate on virtual entries like .zfs/ or
2309 * .zfs/snapshot/ directories, that's why we return EOPNOTSUPP.
2310 * This will make NFS to switch to LOOKUP instead of using VGET.
2311 */
2312 if (ino == ZFSCTL_INO_ROOT || ino == ZFSCTL_INO_SNAPDIR ||
2313 (zfsvfs->z_shares_dir != 0 && ino == zfsvfs->z_shares_dir))
2314 return (EOPNOTSUPP);
2315
2316 ZFS_ENTER(zfsvfs);
2317 err = zfs_zget(zfsvfs, ino, &zp);
2318 if (err == 0 && zp->z_unlinked) {
2319 vrele(ZTOV(zp));
2320 err = EINVAL;
2321 }
2322 if (err == 0)
2323 *vpp = ZTOV(zp);
2324 ZFS_EXIT(zfsvfs);
2325 if (err == 0) {
2326 err = vn_lock(*vpp, flags);
2327 if (err != 0)
2328 vrele(*vpp);
2329 }
2330 if (err != 0)
2331 *vpp = NULL;
2332 return (err);
2333 }
2334
2335 static int
zfs_checkexp(vfs_t * vfsp,struct sockaddr * nam,int * extflagsp,struct ucred ** credanonp,int * numsecflavors,int ** secflavors)2336 zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp,
2337 struct ucred **credanonp, int *numsecflavors, int **secflavors)
2338 {
2339 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2340
2341 /*
2342 * If this is regular file system vfsp is the same as
2343 * zfsvfs->z_parent->z_vfs, but if it is snapshot,
2344 * zfsvfs->z_parent->z_vfs represents parent file system
2345 * which we have to use here, because only this file system
2346 * has mnt_export configured.
2347 */
2348 return (vfs_stdcheckexp(zfsvfs->z_parent->z_vfs, nam, extflagsp,
2349 credanonp, numsecflavors, secflavors));
2350 }
2351
2352 CTASSERT(SHORT_FID_LEN <= sizeof(struct fid));
2353 CTASSERT(LONG_FID_LEN <= sizeof(struct fid));
2354
2355 static int
zfs_fhtovp(vfs_t * vfsp,fid_t * fidp,int flags,vnode_t ** vpp)2356 zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, int flags, vnode_t **vpp)
2357 {
2358 struct componentname cn;
2359 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2360 znode_t *zp;
2361 vnode_t *dvp;
2362 uint64_t object = 0;
2363 uint64_t fid_gen = 0;
2364 uint64_t gen_mask;
2365 uint64_t zp_gen;
2366 int i, err;
2367
2368 *vpp = NULL;
2369
2370 ZFS_ENTER(zfsvfs);
2371
2372 /*
2373 * On FreeBSD we can get snapshot's mount point or its parent file
2374 * system mount point depending if snapshot is already mounted or not.
2375 */
2376 if (zfsvfs->z_parent == zfsvfs && fidp->fid_len == LONG_FID_LEN) {
2377 zfid_long_t *zlfid = (zfid_long_t *)fidp;
2378 uint64_t objsetid = 0;
2379 uint64_t setgen = 0;
2380
2381 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
2382 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
2383
2384 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
2385 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
2386
2387 ZFS_EXIT(zfsvfs);
2388
2389 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
2390 if (err)
2391 return (SET_ERROR(EINVAL));
2392 ZFS_ENTER(zfsvfs);
2393 }
2394
2395 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
2396 zfid_short_t *zfid = (zfid_short_t *)fidp;
2397
2398 for (i = 0; i < sizeof (zfid->zf_object); i++)
2399 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
2400
2401 for (i = 0; i < sizeof (zfid->zf_gen); i++)
2402 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
2403 } else {
2404 ZFS_EXIT(zfsvfs);
2405 return (SET_ERROR(EINVAL));
2406 }
2407
2408 /*
2409 * A zero fid_gen means we are in .zfs or the .zfs/snapshot
2410 * directory tree. If the object == zfsvfs->z_shares_dir, then
2411 * we are in the .zfs/shares directory tree.
2412 */
2413 if ((fid_gen == 0 &&
2414 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) ||
2415 (zfsvfs->z_shares_dir != 0 && object == zfsvfs->z_shares_dir)) {
2416 ZFS_EXIT(zfsvfs);
2417 VERIFY0(zfsctl_root(zfsvfs, LK_SHARED, &dvp));
2418 if (object == ZFSCTL_INO_SNAPDIR) {
2419 cn.cn_nameptr = "snapshot";
2420 cn.cn_namelen = strlen(cn.cn_nameptr);
2421 cn.cn_nameiop = LOOKUP;
2422 cn.cn_flags = ISLASTCN | LOCKLEAF;
2423 cn.cn_lkflags = flags;
2424 VERIFY0(VOP_LOOKUP(dvp, vpp, &cn));
2425 vput(dvp);
2426 } else if (object == zfsvfs->z_shares_dir) {
2427 /*
2428 * XXX This branch must not be taken,
2429 * if it is, then the lookup below will
2430 * explode.
2431 */
2432 cn.cn_nameptr = "shares";
2433 cn.cn_namelen = strlen(cn.cn_nameptr);
2434 cn.cn_nameiop = LOOKUP;
2435 cn.cn_flags = ISLASTCN;
2436 cn.cn_lkflags = flags;
2437 VERIFY0(VOP_LOOKUP(dvp, vpp, &cn));
2438 vput(dvp);
2439 } else {
2440 *vpp = dvp;
2441 }
2442 return (err);
2443 }
2444
2445 gen_mask = -1ULL >> (64 - 8 * i);
2446
2447 dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
2448 if (err = zfs_zget(zfsvfs, object, &zp)) {
2449 ZFS_EXIT(zfsvfs);
2450 return (err);
2451 }
2452 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
2453 sizeof (uint64_t));
2454 zp_gen = zp_gen & gen_mask;
2455 if (zp_gen == 0)
2456 zp_gen = 1;
2457 if (zp->z_unlinked || zp_gen != fid_gen) {
2458 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
2459 vrele(ZTOV(zp));
2460 ZFS_EXIT(zfsvfs);
2461 return (SET_ERROR(EINVAL));
2462 }
2463
2464 *vpp = ZTOV(zp);
2465 ZFS_EXIT(zfsvfs);
2466 err = vn_lock(*vpp, flags);
2467 if (err == 0)
2468 vnode_create_vobject(*vpp, zp->z_size, curthread);
2469 else
2470 *vpp = NULL;
2471 return (err);
2472 }
2473
2474 /*
2475 * Block out VOPs and close zfsvfs_t::z_os
2476 *
2477 * Note, if successful, then we return with the 'z_teardown_lock' and
2478 * 'z_teardown_inactive_lock' write held. We leave ownership of the underlying
2479 * dataset and objset intact so that they can be atomically handed off during
2480 * a subsequent rollback or recv operation and the resume thereafter.
2481 */
2482 int
zfs_suspend_fs(zfsvfs_t * zfsvfs)2483 zfs_suspend_fs(zfsvfs_t *zfsvfs)
2484 {
2485 int error;
2486
2487 if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
2488 return (error);
2489
2490 return (0);
2491 }
2492
2493 /*
2494 * Rebuild SA and release VOPs. Note that ownership of the underlying dataset
2495 * is an invariant across any of the operations that can be performed while the
2496 * filesystem was suspended. Whether it succeeded or failed, the preconditions
2497 * are the same: the relevant objset and associated dataset are owned by
2498 * zfsvfs, held, and long held on entry.
2499 */
2500 int
zfs_resume_fs(zfsvfs_t * zfsvfs,dsl_dataset_t * ds)2501 zfs_resume_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds)
2502 {
2503 int err;
2504 znode_t *zp;
2505
2506 ASSERT(RRM_WRITE_HELD(&zfsvfs->z_teardown_lock));
2507 ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
2508
2509 /*
2510 * We already own this, so just update the objset_t, as the one we
2511 * had before may have been evicted.
2512 */
2513 objset_t *os;
2514 VERIFY3P(ds->ds_owner, ==, zfsvfs);
2515 VERIFY(dsl_dataset_long_held(ds));
2516 VERIFY0(dmu_objset_from_ds(ds, &os));
2517
2518 err = zfsvfs_init(zfsvfs, os);
2519 if (err != 0)
2520 goto bail;
2521
2522 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
2523
2524 zfs_set_fuid_feature(zfsvfs);
2525
2526 /*
2527 * Attempt to re-establish all the active znodes with
2528 * their dbufs. If a zfs_rezget() fails, then we'll let
2529 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
2530 * when they try to use their znode.
2531 */
2532 mutex_enter(&zfsvfs->z_znodes_lock);
2533 for (zp = list_head(&zfsvfs->z_all_znodes); zp;
2534 zp = list_next(&zfsvfs->z_all_znodes, zp)) {
2535 (void) zfs_rezget(zp);
2536 }
2537 mutex_exit(&zfsvfs->z_znodes_lock);
2538
2539 bail:
2540 /* release the VOPs */
2541 rw_exit(&zfsvfs->z_teardown_inactive_lock);
2542 rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2543
2544 if (err) {
2545 /*
2546 * Since we couldn't setup the sa framework, try to force
2547 * unmount this file system.
2548 */
2549 if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0) {
2550 vfs_ref(zfsvfs->z_vfs);
2551 (void) dounmount(zfsvfs->z_vfs, MS_FORCE, curthread);
2552 }
2553 }
2554 return (err);
2555 }
2556
2557 static void
zfs_freevfs(vfs_t * vfsp)2558 zfs_freevfs(vfs_t *vfsp)
2559 {
2560 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2561
2562 #ifdef illumos
2563 /*
2564 * If this is a snapshot, we have an extra VFS_HOLD on our parent
2565 * from zfs_mount(). Release it here. If we came through
2566 * zfs_mountroot() instead, we didn't grab an extra hold, so
2567 * skip the VFS_RELE for rootvfs.
2568 */
2569 if (zfsvfs->z_issnap && (vfsp != rootvfs))
2570 VFS_RELE(zfsvfs->z_parent->z_vfs);
2571 #endif
2572
2573 zfsvfs_free(zfsvfs);
2574
2575 atomic_dec_32(&zfs_active_fs_count);
2576 }
2577
2578 #ifdef __i386__
2579 static int desiredvnodes_backup;
2580 #endif
2581
2582 static void
zfs_vnodes_adjust(void)2583 zfs_vnodes_adjust(void)
2584 {
2585 #ifdef __i386__
2586 int newdesiredvnodes;
2587
2588 desiredvnodes_backup = desiredvnodes;
2589
2590 /*
2591 * We calculate newdesiredvnodes the same way it is done in
2592 * vntblinit(). If it is equal to desiredvnodes, it means that
2593 * it wasn't tuned by the administrator and we can tune it down.
2594 */
2595 newdesiredvnodes = min(maxproc + vm_cnt.v_page_count / 4, 2 *
2596 vm_kmem_size / (5 * (sizeof(struct vm_object) +
2597 sizeof(struct vnode))));
2598 if (newdesiredvnodes == desiredvnodes)
2599 desiredvnodes = (3 * newdesiredvnodes) / 4;
2600 #endif
2601 }
2602
2603 static void
zfs_vnodes_adjust_back(void)2604 zfs_vnodes_adjust_back(void)
2605 {
2606
2607 #ifdef __i386__
2608 desiredvnodes = desiredvnodes_backup;
2609 #endif
2610 }
2611
2612 void
zfs_init(void)2613 zfs_init(void)
2614 {
2615
2616 printf("ZFS filesystem version: " ZPL_VERSION_STRING "\n");
2617
2618 /*
2619 * Initialize .zfs directory structures
2620 */
2621 zfsctl_init();
2622
2623 /*
2624 * Initialize znode cache, vnode ops, etc...
2625 */
2626 zfs_znode_init();
2627
2628 /*
2629 * Reduce number of vnodes. Originally number of vnodes is calculated
2630 * with UFS inode in mind. We reduce it here, because it's too big for
2631 * ZFS/i386.
2632 */
2633 zfs_vnodes_adjust();
2634
2635 dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
2636 #if defined(__FreeBSD__)
2637 zfsvfs_taskq = taskq_create("zfsvfs", 1, minclsyspri, 0, 0, 0);
2638 #endif
2639 }
2640
2641 void
zfs_fini(void)2642 zfs_fini(void)
2643 {
2644 #if defined(__FreeBSD__)
2645 taskq_destroy(zfsvfs_taskq);
2646 #endif
2647 zfsctl_fini();
2648 zfs_znode_fini();
2649 zfs_vnodes_adjust_back();
2650 }
2651
2652 int
zfs_busy(void)2653 zfs_busy(void)
2654 {
2655 return (zfs_active_fs_count != 0);
2656 }
2657
2658 int
zfs_set_version(zfsvfs_t * zfsvfs,uint64_t newvers)2659 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
2660 {
2661 int error;
2662 objset_t *os = zfsvfs->z_os;
2663 dmu_tx_t *tx;
2664
2665 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2666 return (SET_ERROR(EINVAL));
2667
2668 if (newvers < zfsvfs->z_version)
2669 return (SET_ERROR(EINVAL));
2670
2671 if (zfs_spa_version_map(newvers) >
2672 spa_version(dmu_objset_spa(zfsvfs->z_os)))
2673 return (SET_ERROR(ENOTSUP));
2674
2675 tx = dmu_tx_create(os);
2676 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
2677 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2678 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
2679 ZFS_SA_ATTRS);
2680 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2681 }
2682 error = dmu_tx_assign(tx, TXG_WAIT);
2683 if (error) {
2684 dmu_tx_abort(tx);
2685 return (error);
2686 }
2687
2688 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
2689 8, 1, &newvers, tx);
2690
2691 if (error) {
2692 dmu_tx_commit(tx);
2693 return (error);
2694 }
2695
2696 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2697 uint64_t sa_obj;
2698
2699 ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
2700 SPA_VERSION_SA);
2701 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
2702 DMU_OT_NONE, 0, tx);
2703
2704 error = zap_add(os, MASTER_NODE_OBJ,
2705 ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2706 ASSERT0(error);
2707
2708 VERIFY(0 == sa_set_sa_object(os, sa_obj));
2709 sa_register_update_callback(os, zfs_sa_upgrade);
2710 }
2711
2712 spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
2713 "from %llu to %llu", zfsvfs->z_version, newvers);
2714
2715 dmu_tx_commit(tx);
2716
2717 zfsvfs->z_version = newvers;
2718 os->os_version = newvers;
2719
2720 zfs_set_fuid_feature(zfsvfs);
2721
2722 return (0);
2723 }
2724
2725 /*
2726 * Read a property stored within the master node.
2727 */
2728 int
zfs_get_zplprop(objset_t * os,zfs_prop_t prop,uint64_t * value)2729 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2730 {
2731 uint64_t *cached_copy = NULL;
2732
2733 /*
2734 * Figure out where in the objset_t the cached copy would live, if it
2735 * is available for the requested property.
2736 */
2737 if (os != NULL) {
2738 switch (prop) {
2739 case ZFS_PROP_VERSION:
2740 cached_copy = &os->os_version;
2741 break;
2742 case ZFS_PROP_NORMALIZE:
2743 cached_copy = &os->os_normalization;
2744 break;
2745 case ZFS_PROP_UTF8ONLY:
2746 cached_copy = &os->os_utf8only;
2747 break;
2748 case ZFS_PROP_CASE:
2749 cached_copy = &os->os_casesensitivity;
2750 break;
2751 default:
2752 break;
2753 }
2754 }
2755 if (cached_copy != NULL && *cached_copy != OBJSET_PROP_UNINITIALIZED) {
2756 *value = *cached_copy;
2757 return (0);
2758 }
2759
2760 /*
2761 * If the property wasn't cached, look up the file system's value for
2762 * the property. For the version property, we look up a slightly
2763 * different string.
2764 */
2765 const char *pname;
2766 int error = ENOENT;
2767 if (prop == ZFS_PROP_VERSION) {
2768 pname = ZPL_VERSION_STR;
2769 } else {
2770 pname = zfs_prop_to_name(prop);
2771 }
2772
2773 if (os != NULL) {
2774 ASSERT3U(os->os_phys->os_type, ==, DMU_OST_ZFS);
2775 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2776 }
2777
2778 if (error == ENOENT) {
2779 /* No value set, use the default value */
2780 switch (prop) {
2781 case ZFS_PROP_VERSION:
2782 *value = ZPL_VERSION;
2783 break;
2784 case ZFS_PROP_NORMALIZE:
2785 case ZFS_PROP_UTF8ONLY:
2786 *value = 0;
2787 break;
2788 case ZFS_PROP_CASE:
2789 *value = ZFS_CASE_SENSITIVE;
2790 break;
2791 default:
2792 return (error);
2793 }
2794 error = 0;
2795 }
2796
2797 /*
2798 * If one of the methods for getting the property value above worked,
2799 * copy it into the objset_t's cache.
2800 */
2801 if (error == 0 && cached_copy != NULL) {
2802 *cached_copy = *value;
2803 }
2804
2805 return (error);
2806 }
2807
2808 /*
2809 * Return true if the coresponding vfs's unmounted flag is set.
2810 * Otherwise return false.
2811 * If this function returns true we know VFS unmount has been initiated.
2812 */
2813 boolean_t
zfs_get_vfs_flag_unmounted(objset_t * os)2814 zfs_get_vfs_flag_unmounted(objset_t *os)
2815 {
2816 zfsvfs_t *zfvp;
2817 boolean_t unmounted = B_FALSE;
2818
2819 ASSERT(dmu_objset_type(os) == DMU_OST_ZFS);
2820
2821 mutex_enter(&os->os_user_ptr_lock);
2822 zfvp = dmu_objset_get_user(os);
2823 if (zfvp != NULL && zfvp->z_vfs != NULL &&
2824 (zfvp->z_vfs->mnt_kern_flag & MNTK_UNMOUNT))
2825 unmounted = B_TRUE;
2826 mutex_exit(&os->os_user_ptr_lock);
2827
2828 return (unmounted);
2829 }
2830
2831 #ifdef _KERNEL
2832 void
zfsvfs_update_fromname(const char * oldname,const char * newname)2833 zfsvfs_update_fromname(const char *oldname, const char *newname)
2834 {
2835 char tmpbuf[MAXPATHLEN];
2836 struct mount *mp;
2837 char *fromname;
2838 size_t oldlen;
2839
2840 oldlen = strlen(oldname);
2841
2842 mtx_lock(&mountlist_mtx);
2843 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2844 fromname = mp->mnt_stat.f_mntfromname;
2845 if (strcmp(fromname, oldname) == 0) {
2846 (void)strlcpy(fromname, newname,
2847 sizeof(mp->mnt_stat.f_mntfromname));
2848 continue;
2849 }
2850 if (strncmp(fromname, oldname, oldlen) == 0 &&
2851 (fromname[oldlen] == '/' || fromname[oldlen] == '@')) {
2852 (void)snprintf(tmpbuf, sizeof(tmpbuf), "%s%s",
2853 newname, fromname + oldlen);
2854 (void)strlcpy(fromname, tmpbuf,
2855 sizeof(mp->mnt_stat.f_mntfromname));
2856 continue;
2857 }
2858 }
2859 mtx_unlock(&mountlist_mtx);
2860 }
2861 #endif
2862