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