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