Remove zfs_ctldir.[ch]
[zfs.git] / module / zfs / zfs_vfsops.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24
25 /* Portions Copyright 2010 Robert Milkowski */
26
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/sysmacros.h>
31 #include <sys/kmem.h>
32 #include <sys/pathname.h>
33 #include <sys/vnode.h>
34 #include <sys/vfs.h>
35 #include <sys/vfs_opreg.h>
36 #include <sys/mntent.h>
37 #include <sys/mount.h>
38 #include <sys/cmn_err.h>
39 #include "fs/fs_subr.h"
40 #include <sys/zfs_znode.h>
41 #include <sys/zfs_dir.h>
42 #include <sys/zil.h>
43 #include <sys/fs/zfs.h>
44 #include <sys/dmu.h>
45 #include <sys/dsl_prop.h>
46 #include <sys/dsl_dataset.h>
47 #include <sys/dsl_deleg.h>
48 #include <sys/spa.h>
49 #include <sys/zap.h>
50 #include <sys/sa.h>
51 #include <sys/varargs.h>
52 #include <sys/policy.h>
53 #include <sys/atomic.h>
54 #include <sys/mkdev.h>
55 #include <sys/modctl.h>
56 #include <sys/refstr.h>
57 #include <sys/zfs_ioctl.h>
58 #include <sys/zfs_fuid.h>
59 #include <sys/bootconf.h>
60 #include <sys/sunddi.h>
61 #include <sys/dnlc.h>
62 #include <sys/dmu_objset.h>
63 #include <sys/spa_boot.h>
64 #include <sys/sa.h>
65 #include "zfs_comutil.h"
66
67 #ifdef HAVE_ZPL
68
69 /*ARGSUSED*/
70 int
71 zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
72 {
73         /*
74          * Data integrity is job one.  We don't want a compromised kernel
75          * writing to the storage pool, so we never sync during panic.
76          */
77         if (unlikely(oops_in_progress))
78                 return (0);
79
80         if (vfsp != NULL) {
81                 /*
82                  * Sync a specific filesystem.
83                  */
84                 zfsvfs_t *zfsvfs = vfsp->vfs_data;
85                 dsl_pool_t *dp;
86
87                 ZFS_ENTER(zfsvfs);
88                 dp = dmu_objset_pool(zfsvfs->z_os);
89
90 #ifdef HAVE_SHUTDOWN
91                 /*
92                  * If the system is shutting down, then skip any
93                  * filesystems which may exist on a suspended pool.
94                  *
95                  * XXX: This can be implemented using the Linux reboot
96                  *      notifiers: {un}register_reboot_notifier().
97                  */
98                 if (sys_shutdown && spa_suspended(dp->dp_spa)) {
99                         ZFS_EXIT(zfsvfs);
100                         return (0);
101                 }
102 #endif /* HAVE_SHUTDOWN */
103
104                 if (zfsvfs->z_log != NULL)
105                         zil_commit(zfsvfs->z_log, 0);
106
107                 ZFS_EXIT(zfsvfs);
108         } else {
109                 /*
110                  * Sync all ZFS filesystems.  This is what happens when you
111                  * run sync(1M).  Unlike other filesystems, ZFS honors the
112                  * request by waiting for all pools to commit all dirty data.
113                  */
114                 spa_sync_allpools();
115         }
116
117         return (0);
118 }
119 EXPORT_SYMBOL(zfs_sync);
120
121 static void
122 atime_changed_cb(void *arg, uint64_t newval)
123 {
124         zfsvfs_t *zfsvfs = arg;
125
126         if (newval == TRUE) {
127                 zfsvfs->z_atime = TRUE;
128                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
129                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
130         } else {
131                 zfsvfs->z_atime = FALSE;
132                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
133                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
134         }
135 }
136
137 static void
138 xattr_changed_cb(void *arg, uint64_t newval)
139 {
140         zfsvfs_t *zfsvfs = arg;
141
142         if (newval == TRUE) {
143                 /* XXX locking on vfs_flag? */
144                 zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
145                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
146                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
147         } else {
148                 /* XXX locking on vfs_flag? */
149                 zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
150                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
151                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
152         }
153 }
154
155 static void
156 blksz_changed_cb(void *arg, uint64_t newval)
157 {
158         zfsvfs_t *zfsvfs = arg;
159
160         if (newval < SPA_MINBLOCKSIZE ||
161             newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
162                 newval = SPA_MAXBLOCKSIZE;
163
164         zfsvfs->z_max_blksz = newval;
165         zfsvfs->z_vfs->vfs_bsize = newval;
166 }
167
168 static void
169 readonly_changed_cb(void *arg, uint64_t newval)
170 {
171         zfsvfs_t *zfsvfs = arg;
172
173         if (newval) {
174                 /* XXX locking on vfs_flag? */
175                 zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
176                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
177                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
178         } else {
179                 /* XXX locking on vfs_flag? */
180                 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
181                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
182                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
183         }
184 }
185
186 static void
187 devices_changed_cb(void *arg, uint64_t newval)
188 {
189         zfsvfs_t *zfsvfs = arg;
190
191         if (newval == FALSE) {
192                 zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
193                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
194                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
195         } else {
196                 zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
197                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
198                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
199         }
200 }
201
202 static void
203 setuid_changed_cb(void *arg, uint64_t newval)
204 {
205         zfsvfs_t *zfsvfs = arg;
206
207         if (newval == FALSE) {
208                 zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
209                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
210                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
211         } else {
212                 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
213                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
214                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
215         }
216 }
217
218 static void
219 exec_changed_cb(void *arg, uint64_t newval)
220 {
221         zfsvfs_t *zfsvfs = arg;
222
223         if (newval == FALSE) {
224                 zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
225                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
226                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
227         } else {
228                 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
229                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
230                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
231         }
232 }
233
234 /*
235  * The nbmand mount option can be changed at mount time.
236  * We can't allow it to be toggled on live file systems or incorrect
237  * behavior may be seen from cifs clients
238  *
239  * This property isn't registered via dsl_prop_register(), but this callback
240  * will be called when a file system is first mounted
241  */
242 static void
243 nbmand_changed_cb(void *arg, uint64_t newval)
244 {
245         zfsvfs_t *zfsvfs = arg;
246         if (newval == FALSE) {
247                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
248                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
249         } else {
250                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
251                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
252         }
253 }
254
255 static void
256 snapdir_changed_cb(void *arg, uint64_t newval)
257 {
258         zfsvfs_t *zfsvfs = arg;
259
260         zfsvfs->z_show_ctldir = newval;
261 }
262
263 static void
264 vscan_changed_cb(void *arg, uint64_t newval)
265 {
266         zfsvfs_t *zfsvfs = arg;
267
268         zfsvfs->z_vscan = newval;
269 }
270
271 static void
272 acl_inherit_changed_cb(void *arg, uint64_t newval)
273 {
274         zfsvfs_t *zfsvfs = arg;
275
276         zfsvfs->z_acl_inherit = newval;
277 }
278
279 int
280 zfs_register_callbacks(vfs_t *vfsp)
281 {
282         struct dsl_dataset *ds = NULL;
283         objset_t *os = NULL;
284         zfsvfs_t *zfsvfs = NULL;
285         uint64_t nbmand;
286         int readonly, do_readonly = B_FALSE;
287         int setuid, do_setuid = B_FALSE;
288         int exec, do_exec = B_FALSE;
289         int devices, do_devices = B_FALSE;
290         int xattr, do_xattr = B_FALSE;
291         int atime, do_atime = B_FALSE;
292         int error = 0;
293
294         ASSERT(vfsp);
295         zfsvfs = vfsp->vfs_data;
296         ASSERT(zfsvfs);
297         os = zfsvfs->z_os;
298
299         /*
300          * The act of registering our callbacks will destroy any mount
301          * options we may have.  In order to enable temporary overrides
302          * of mount options, we stash away the current values and
303          * restore them after we register the callbacks.
304          */
305         if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
306             !spa_writeable(dmu_objset_spa(os))) {
307                 readonly = B_TRUE;
308                 do_readonly = B_TRUE;
309         } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
310                 readonly = B_FALSE;
311                 do_readonly = B_TRUE;
312         }
313         if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
314                 devices = B_FALSE;
315                 setuid = B_FALSE;
316                 do_devices = B_TRUE;
317                 do_setuid = B_TRUE;
318         } else {
319                 if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
320                         devices = B_FALSE;
321                         do_devices = B_TRUE;
322                 } else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
323                         devices = B_TRUE;
324                         do_devices = B_TRUE;
325                 }
326
327                 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
328                         setuid = B_FALSE;
329                         do_setuid = B_TRUE;
330                 } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
331                         setuid = B_TRUE;
332                         do_setuid = B_TRUE;
333                 }
334         }
335         if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
336                 exec = B_FALSE;
337                 do_exec = B_TRUE;
338         } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
339                 exec = B_TRUE;
340                 do_exec = B_TRUE;
341         }
342         if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
343                 xattr = B_FALSE;
344                 do_xattr = B_TRUE;
345         } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
346                 xattr = B_TRUE;
347                 do_xattr = B_TRUE;
348         }
349         if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
350                 atime = B_FALSE;
351                 do_atime = B_TRUE;
352         } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
353                 atime = B_TRUE;
354                 do_atime = B_TRUE;
355         }
356
357         /*
358          * nbmand is a special property.  It can only be changed at
359          * mount time.
360          *
361          * This is weird, but it is documented to only be changeable
362          * at mount time.
363          */
364         if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
365                 nbmand = B_FALSE;
366         } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
367                 nbmand = B_TRUE;
368         } else {
369                 char osname[MAXNAMELEN];
370
371                 dmu_objset_name(os, osname);
372                 if ((error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
373                     NULL))) {
374                         return (error);
375                 }
376         }
377
378         /*
379          * Register property callbacks.
380          *
381          * It would probably be fine to just check for i/o error from
382          * the first prop_register(), but I guess I like to go
383          * overboard...
384          */
385         ds = dmu_objset_ds(os);
386         error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
387         error = error ? error : dsl_prop_register(ds,
388             "xattr", xattr_changed_cb, zfsvfs);
389         error = error ? error : dsl_prop_register(ds,
390             "recordsize", blksz_changed_cb, zfsvfs);
391         error = error ? error : dsl_prop_register(ds,
392             "readonly", readonly_changed_cb, zfsvfs);
393         error = error ? error : dsl_prop_register(ds,
394             "devices", devices_changed_cb, zfsvfs);
395         error = error ? error : dsl_prop_register(ds,
396             "setuid", setuid_changed_cb, zfsvfs);
397         error = error ? error : dsl_prop_register(ds,
398             "exec", exec_changed_cb, zfsvfs);
399         error = error ? error : dsl_prop_register(ds,
400             "snapdir", snapdir_changed_cb, zfsvfs);
401         error = error ? error : dsl_prop_register(ds,
402             "aclinherit", acl_inherit_changed_cb, zfsvfs);
403         error = error ? error : dsl_prop_register(ds,
404             "vscan", vscan_changed_cb, zfsvfs);
405         if (error)
406                 goto unregister;
407
408         /*
409          * Invoke our callbacks to restore temporary mount options.
410          */
411         if (do_readonly)
412                 readonly_changed_cb(zfsvfs, readonly);
413         if (do_setuid)
414                 setuid_changed_cb(zfsvfs, setuid);
415         if (do_exec)
416                 exec_changed_cb(zfsvfs, exec);
417         if (do_devices)
418                 devices_changed_cb(zfsvfs, devices);
419         if (do_xattr)
420                 xattr_changed_cb(zfsvfs, xattr);
421         if (do_atime)
422                 atime_changed_cb(zfsvfs, atime);
423
424         nbmand_changed_cb(zfsvfs, nbmand);
425
426         return (0);
427
428 unregister:
429         /*
430          * We may attempt to unregister some callbacks that are not
431          * registered, but this is OK; it will simply return ENOMSG,
432          * which we will ignore.
433          */
434         (void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
435         (void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
436         (void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
437         (void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
438         (void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
439         (void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
440         (void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
441         (void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
442         (void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
443             zfsvfs);
444         (void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
445         return (error);
446
447 }
448 EXPORT_SYMBOL(zfs_register_callbacks);
449 #endif /* HAVE_ZPL */
450
451 static int
452 zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
453     uint64_t *userp, uint64_t *groupp)
454 {
455         znode_phys_t *znp = data;
456         int error = 0;
457
458         /*
459          * Is it a valid type of object to track?
460          */
461         if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
462                 return (ENOENT);
463
464         /*
465          * If we have a NULL data pointer
466          * then assume the id's aren't changing and
467          * return EEXIST to the dmu to let it know to
468          * use the same ids
469          */
470         if (data == NULL)
471                 return (EEXIST);
472
473         if (bonustype == DMU_OT_ZNODE) {
474                 *userp = znp->zp_uid;
475                 *groupp = znp->zp_gid;
476         } else {
477                 int hdrsize;
478
479                 ASSERT(bonustype == DMU_OT_SA);
480                 hdrsize = sa_hdrsize(data);
481
482                 if (hdrsize != 0) {
483                         *userp = *((uint64_t *)((uintptr_t)data + hdrsize +
484                             SA_UID_OFFSET));
485                         *groupp = *((uint64_t *)((uintptr_t)data + hdrsize +
486                             SA_GID_OFFSET));
487                 } else {
488                         /*
489                          * This should only happen for newly created
490                          * files that haven't had the znode data filled
491                          * in yet.
492                          */
493                         *userp = 0;
494                         *groupp = 0;
495                 }
496         }
497         return (error);
498 }
499
500 #ifdef HAVE_ZPL
501 static void
502 fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
503     char *domainbuf, int buflen, uid_t *ridp)
504 {
505         uint64_t fuid;
506         const char *domain;
507
508         fuid = strtonum(fuidstr, NULL);
509
510         domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
511         if (domain)
512                 (void) strlcpy(domainbuf, domain, buflen);
513         else
514                 domainbuf[0] = '\0';
515         *ridp = FUID_RID(fuid);
516 }
517
518 static uint64_t
519 zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
520 {
521         switch (type) {
522         case ZFS_PROP_USERUSED:
523                 return (DMU_USERUSED_OBJECT);
524         case ZFS_PROP_GROUPUSED:
525                 return (DMU_GROUPUSED_OBJECT);
526         case ZFS_PROP_USERQUOTA:
527                 return (zfsvfs->z_userquota_obj);
528         case ZFS_PROP_GROUPQUOTA:
529                 return (zfsvfs->z_groupquota_obj);
530         default:
531                 return (ENOTSUP);
532         }
533         return (0);
534 }
535
536 int
537 zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
538     uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
539 {
540         int error;
541         zap_cursor_t zc;
542         zap_attribute_t za;
543         zfs_useracct_t *buf = vbuf;
544         uint64_t obj;
545
546         if (!dmu_objset_userspace_present(zfsvfs->z_os))
547                 return (ENOTSUP);
548
549         obj = zfs_userquota_prop_to_obj(zfsvfs, type);
550         if (obj == 0) {
551                 *bufsizep = 0;
552                 return (0);
553         }
554
555         for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
556             (error = zap_cursor_retrieve(&zc, &za)) == 0;
557             zap_cursor_advance(&zc)) {
558                 if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
559                     *bufsizep)
560                         break;
561
562                 fuidstr_to_sid(zfsvfs, za.za_name,
563                     buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
564
565                 buf->zu_space = za.za_first_integer;
566                 buf++;
567         }
568         if (error == ENOENT)
569                 error = 0;
570
571         ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
572         *bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
573         *cookiep = zap_cursor_serialize(&zc);
574         zap_cursor_fini(&zc);
575         return (error);
576 }
577 EXPORT_SYMBOL(zfs_userspace_many);
578
579 /*
580  * buf must be big enough (eg, 32 bytes)
581  */
582 static int
583 id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
584     char *buf, boolean_t addok)
585 {
586         uint64_t fuid;
587         int domainid = 0;
588
589         if (domain && domain[0]) {
590                 domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
591                 if (domainid == -1)
592                         return (ENOENT);
593         }
594         fuid = FUID_ENCODE(domainid, rid);
595         (void) sprintf(buf, "%llx", (longlong_t)fuid);
596         return (0);
597 }
598
599 int
600 zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
601     const char *domain, uint64_t rid, uint64_t *valp)
602 {
603         char buf[32];
604         int err;
605         uint64_t obj;
606
607         *valp = 0;
608
609         if (!dmu_objset_userspace_present(zfsvfs->z_os))
610                 return (ENOTSUP);
611
612         obj = zfs_userquota_prop_to_obj(zfsvfs, type);
613         if (obj == 0)
614                 return (0);
615
616         err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_FALSE);
617         if (err)
618                 return (err);
619
620         err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
621         if (err == ENOENT)
622                 err = 0;
623         return (err);
624 }
625 EXPORT_SYMBOL(zfs_userspace_one);
626
627 int
628 zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
629     const char *domain, uint64_t rid, uint64_t quota)
630 {
631         char buf[32];
632         int err;
633         dmu_tx_t *tx;
634         uint64_t *objp;
635         boolean_t fuid_dirtied;
636
637         if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
638                 return (EINVAL);
639
640         if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
641                 return (ENOTSUP);
642
643         objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj :
644             &zfsvfs->z_groupquota_obj;
645
646         err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
647         if (err)
648                 return (err);
649         fuid_dirtied = zfsvfs->z_fuid_dirty;
650
651         tx = dmu_tx_create(zfsvfs->z_os);
652         dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
653         if (*objp == 0) {
654                 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
655                     zfs_userquota_prop_prefixes[type]);
656         }
657         if (fuid_dirtied)
658                 zfs_fuid_txhold(zfsvfs, tx);
659         err = dmu_tx_assign(tx, TXG_WAIT);
660         if (err) {
661                 dmu_tx_abort(tx);
662                 return (err);
663         }
664
665         mutex_enter(&zfsvfs->z_lock);
666         if (*objp == 0) {
667                 *objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
668                     DMU_OT_NONE, 0, tx);
669                 VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
670                     zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
671         }
672         mutex_exit(&zfsvfs->z_lock);
673
674         if (quota == 0) {
675                 err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
676                 if (err == ENOENT)
677                         err = 0;
678         } else {
679                 err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, &quota, tx);
680         }
681         ASSERT(err == 0);
682         if (fuid_dirtied)
683                 zfs_fuid_sync(zfsvfs, tx);
684         dmu_tx_commit(tx);
685         return (err);
686 }
687 EXPORT_SYMBOL(zfs_set_userquota);
688
689 boolean_t
690 zfs_fuid_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid)
691 {
692         char buf[32];
693         uint64_t used, quota, usedobj, quotaobj;
694         int err;
695
696         usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
697         quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
698
699         if (quotaobj == 0 || zfsvfs->z_replay)
700                 return (B_FALSE);
701
702         (void) sprintf(buf, "%llx", (longlong_t)fuid);
703         err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
704         if (err != 0)
705                 return (B_FALSE);
706
707         err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
708         if (err != 0)
709                 return (B_FALSE);
710         return (used >= quota);
711 }
712 EXPORT_SYMBOL(zfs_fuid_overquota);
713
714 boolean_t
715 zfs_owner_overquota(zfsvfs_t *zfsvfs, znode_t *zp, boolean_t isgroup)
716 {
717         uint64_t fuid;
718         uint64_t quotaobj;
719
720         quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
721
722         fuid = isgroup ? zp->z_gid : zp->z_uid;
723
724         if (quotaobj == 0 || zfsvfs->z_replay)
725                 return (B_FALSE);
726
727         return (zfs_fuid_overquota(zfsvfs, isgroup, fuid));
728 }
729 EXPORT_SYMBOL(zfs_owner_overquota);
730
731 int
732 zfsvfs_create(const char *osname, zfsvfs_t **zfvp)
733 {
734         objset_t *os;
735         zfsvfs_t *zfsvfs;
736         uint64_t zval;
737         int i, error;
738         uint64_t sa_obj;
739
740         zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
741
742         /*
743          * We claim to always be readonly so we can open snapshots;
744          * other ZPL code will prevent us from writing to snapshots.
745          */
746         error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os);
747         if (error) {
748                 kmem_free(zfsvfs, sizeof (zfsvfs_t));
749                 return (error);
750         }
751
752         /*
753          * Initialize the zfs-specific filesystem structure.
754          * Should probably make this a kmem cache, shuffle fields,
755          * and just bzero up to z_hold_mtx[].
756          */
757         zfsvfs->z_vfs = NULL;
758         zfsvfs->z_parent = zfsvfs;
759         zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
760         zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
761         zfsvfs->z_os = os;
762
763         error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
764         if (error) {
765                 goto out;
766         } else if (zfsvfs->z_version >
767             zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
768                 (void) printk("Can't mount a version %lld file system "
769                     "on a version %lld pool\n. Pool must be upgraded to mount "
770                     "this file system.", (u_longlong_t)zfsvfs->z_version,
771                     (u_longlong_t)spa_version(dmu_objset_spa(os)));
772                 error = ENOTSUP;
773                 goto out;
774         }
775         if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
776                 goto out;
777         zfsvfs->z_norm = (int)zval;
778
779         if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
780                 goto out;
781         zfsvfs->z_utf8 = (zval != 0);
782
783         if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
784                 goto out;
785         zfsvfs->z_case = (uint_t)zval;
786
787         /*
788          * Fold case on file systems that are always or sometimes case
789          * insensitive.
790          */
791         if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
792             zfsvfs->z_case == ZFS_CASE_MIXED)
793                 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
794
795         zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
796         zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
797
798         if (zfsvfs->z_use_sa) {
799                 /* should either have both of these objects or none */
800                 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
801                     &sa_obj);
802                 if (error)
803                         return (error);
804         } else {
805                 /*
806                  * Pre SA versions file systems should never touch
807                  * either the attribute registration or layout objects.
808                  */
809                 sa_obj = 0;
810         }
811
812         error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
813             &zfsvfs->z_attr_table);
814         if (error)
815                 goto out;
816
817         if (zfsvfs->z_version >= ZPL_VERSION_SA)
818                 sa_register_update_callback(os, zfs_sa_upgrade);
819
820         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
821             &zfsvfs->z_root);
822         if (error)
823                 goto out;
824         ASSERT(zfsvfs->z_root != 0);
825
826         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
827             &zfsvfs->z_unlinkedobj);
828         if (error)
829                 goto out;
830
831         error = zap_lookup(os, MASTER_NODE_OBJ,
832             zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
833             8, 1, &zfsvfs->z_userquota_obj);
834         if (error && error != ENOENT)
835                 goto out;
836
837         error = zap_lookup(os, MASTER_NODE_OBJ,
838             zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
839             8, 1, &zfsvfs->z_groupquota_obj);
840         if (error && error != ENOENT)
841                 goto out;
842
843         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
844             &zfsvfs->z_fuid_obj);
845         if (error && error != ENOENT)
846                 goto out;
847
848         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
849             &zfsvfs->z_shares_dir);
850         if (error && error != ENOENT)
851                 goto out;
852
853         mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
854         mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
855         list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
856             offsetof(znode_t, z_link_node));
857         rrw_init(&zfsvfs->z_teardown_lock);
858         rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
859         rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
860         for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
861                 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
862
863         *zfvp = zfsvfs;
864         return (0);
865
866 out:
867         dmu_objset_disown(os, zfsvfs);
868         *zfvp = NULL;
869         kmem_free(zfsvfs, sizeof (zfsvfs_t));
870         return (error);
871 }
872
873 static int
874 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
875 {
876         int error;
877
878         error = zfs_register_callbacks(zfsvfs->z_vfs);
879         if (error)
880                 return (error);
881
882         /*
883          * Set the objset user_ptr to track its zfsvfs.
884          */
885         mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
886         dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
887         mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
888
889         zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
890
891         /*
892          * If we are not mounting (ie: online recv), then we don't
893          * have to worry about replaying the log as we blocked all
894          * operations out since we closed the ZIL.
895          */
896         if (mounting) {
897                 boolean_t readonly;
898
899                 /*
900                  * During replay we remove the read only flag to
901                  * allow replays to succeed.
902                  */
903                 readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
904                 if (readonly != 0)
905                         zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
906                 else
907                         zfs_unlinked_drain(zfsvfs);
908
909                 /*
910                  * Parse and replay the intent log.
911                  *
912                  * Because of ziltest, this must be done after
913                  * zfs_unlinked_drain().  (Further note: ziltest
914                  * doesn't use readonly mounts, where
915                  * zfs_unlinked_drain() isn't called.)  This is because
916                  * ziltest causes spa_sync() to think it's committed,
917                  * but actually it is not, so the intent log contains
918                  * many txg's worth of changes.
919                  *
920                  * In particular, if object N is in the unlinked set in
921                  * the last txg to actually sync, then it could be
922                  * actually freed in a later txg and then reallocated
923                  * in a yet later txg.  This would write a "create
924                  * object N" record to the intent log.  Normally, this
925                  * would be fine because the spa_sync() would have
926                  * written out the fact that object N is free, before
927                  * we could write the "create object N" intent log
928                  * record.
929                  *
930                  * But when we are in ziltest mode, we advance the "open
931                  * txg" without actually spa_sync()-ing the changes to
932                  * disk.  So we would see that object N is still
933                  * allocated and in the unlinked set, and there is an
934                  * intent log record saying to allocate it.
935                  */
936                 if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
937                         if (zil_replay_disable) {
938                                 zil_destroy(zfsvfs->z_log, B_FALSE);
939                         } else {
940                                 zfsvfs->z_replay = B_TRUE;
941                                 zil_replay(zfsvfs->z_os, zfsvfs,
942                                     zfs_replay_vector);
943                                 zfsvfs->z_replay = B_FALSE;
944                         }
945                 }
946                 zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
947         }
948
949         return (0);
950 }
951
952 void
953 zfsvfs_free(zfsvfs_t *zfsvfs)
954 {
955         int i;
956         extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
957
958         /*
959          * This is a barrier to prevent the filesystem from going away in
960          * zfs_znode_move() until we can safely ensure that the filesystem is
961          * not unmounted. We consider the filesystem valid before the barrier
962          * and invalid after the barrier.
963          */
964         rw_enter(&zfsvfs_lock, RW_READER);
965         rw_exit(&zfsvfs_lock);
966
967         zfs_fuid_destroy(zfsvfs);
968
969         mutex_destroy(&zfsvfs->z_znodes_lock);
970         mutex_destroy(&zfsvfs->z_lock);
971         list_destroy(&zfsvfs->z_all_znodes);
972         rrw_destroy(&zfsvfs->z_teardown_lock);
973         rw_destroy(&zfsvfs->z_teardown_inactive_lock);
974         rw_destroy(&zfsvfs->z_fuid_lock);
975         for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
976                 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
977         kmem_free(zfsvfs, sizeof (zfsvfs_t));
978 }
979
980 #ifdef HAVE_FUID_FEATURES
981 static void
982 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
983 {
984         zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
985         if (zfsvfs->z_use_fuids && zfsvfs->z_vfs) {
986                 vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
987                 vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
988                 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
989                 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
990                 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
991                 vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
992         }
993         zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
994 }
995 #endif /* HAVE_FUID_FEATURES */
996
997 int
998 zfs_domount(vfs_t *vfsp, char *osname)
999 {
1000         uint64_t recordsize, fsid_guid;
1001         int error = 0;
1002         zfsvfs_t *zfsvfs;
1003
1004         ASSERT(vfsp);
1005         ASSERT(osname);
1006
1007         error = zfsvfs_create(osname, &zfsvfs);
1008         if (error)
1009                 return (error);
1010         zfsvfs->z_vfs = vfsp;
1011
1012         /* Initialize the generic filesystem structure. */
1013         vfsp->vfs_bcount = 0;
1014         vfsp->vfs_data = NULL;
1015
1016         if ((error = dsl_prop_get_integer(osname, "recordsize",
1017             &recordsize, NULL)))
1018                 goto out;
1019
1020         vfsp->vfs_bsize = recordsize;
1021         vfsp->vfs_flag |= VFS_NOTRUNC;
1022         vfsp->vfs_data = zfsvfs;
1023
1024         /*
1025          * The fsid is 64 bits, composed of an 8-bit fs type, which
1026          * separates our fsid from any other filesystem types, and a
1027          * 56-bit objset unique ID.  The objset unique ID is unique to
1028          * all objsets open on this system, provided by unique_create().
1029          * The 8-bit fs type must be put in the low bits of fsid[1]
1030          * because that's where other Solaris filesystems put it.
1031          */
1032         fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
1033         ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
1034         vfsp->vfs_fsid.val[0] = fsid_guid;
1035         vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8);
1036
1037 #ifdef HAVE_FUID_FEATURES
1038         /*
1039          * Set features for file system.
1040          */
1041         zfs_set_fuid_feature(zfsvfs);
1042         if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1043                 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1044                 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1045                 vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1046         } else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1047                 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1048                 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1049         }
1050         vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1051 #endif /* HAVE_FUID_FEATURES */
1052
1053         if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1054                 uint64_t pval;
1055
1056                 atime_changed_cb(zfsvfs, B_FALSE);
1057                 readonly_changed_cb(zfsvfs, B_TRUE);
1058                 if ((error = dsl_prop_get_integer(osname,"xattr",&pval,NULL)))
1059                         goto out;
1060                 xattr_changed_cb(zfsvfs, pval);
1061                 zfsvfs->z_issnap = B_TRUE;
1062                 zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1063
1064                 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1065                 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1066                 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1067         } else {
1068                 error = zfsvfs_setup(zfsvfs, B_TRUE);
1069         }
1070
1071         if (!zfsvfs->z_issnap)
1072                 zfsctl_create(zfsvfs);
1073 out:
1074         if (error) {
1075                 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1076                 zfsvfs_free(zfsvfs);
1077         }
1078
1079         return (error);
1080 }
1081 EXPORT_SYMBOL(zfs_domount);
1082
1083 void
1084 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1085 {
1086         objset_t *os = zfsvfs->z_os;
1087         struct dsl_dataset *ds;
1088
1089         /*
1090          * Unregister properties.
1091          */
1092         if (!dmu_objset_is_snapshot(os)) {
1093                 ds = dmu_objset_ds(os);
1094                 VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
1095                     zfsvfs) == 0);
1096
1097                 VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
1098                     zfsvfs) == 0);
1099
1100                 VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
1101                     zfsvfs) == 0);
1102
1103                 VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
1104                     zfsvfs) == 0);
1105
1106                 VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
1107                     zfsvfs) == 0);
1108
1109                 VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
1110                     zfsvfs) == 0);
1111
1112                 VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
1113                     zfsvfs) == 0);
1114
1115                 VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
1116                     zfsvfs) == 0);
1117
1118                 VERIFY(dsl_prop_unregister(ds, "aclinherit",
1119                     acl_inherit_changed_cb, zfsvfs) == 0);
1120
1121                 VERIFY(dsl_prop_unregister(ds, "vscan",
1122                     vscan_changed_cb, zfsvfs) == 0);
1123         }
1124 }
1125 EXPORT_SYMBOL(zfs_unregister_callbacks);
1126
1127 #ifdef HAVE_MLSLABEL
1128 /*
1129  * zfs_check_global_label:
1130  *      Check that the hex label string is appropriate for the dataset
1131  *      being mounted into the global_zone proper.
1132  *
1133  *      Return an error if the hex label string is not default or
1134  *      admin_low/admin_high.  For admin_low labels, the corresponding
1135  *      dataset must be readonly.
1136  */
1137 int
1138 zfs_check_global_label(const char *dsname, const char *hexsl)
1139 {
1140         if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1141                 return (0);
1142         if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1143                 return (0);
1144         if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1145                 /* must be readonly */
1146                 uint64_t rdonly;
1147
1148                 if (dsl_prop_get_integer(dsname,
1149                     zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1150                         return (EACCES);
1151                 return (rdonly ? 0 : EACCES);
1152         }
1153         return (EACCES);
1154 }
1155 #endif /* HAVE_MLSLABEL */
1156
1157 int
1158 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1159 {
1160         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1161         dev32_t d32;
1162         uint64_t refdbytes, availbytes, usedobjs, availobjs;
1163
1164         ZFS_ENTER(zfsvfs);
1165
1166         dmu_objset_space(zfsvfs->z_os,
1167             &refdbytes, &availbytes, &usedobjs, &availobjs);
1168
1169         /*
1170          * The underlying storage pool actually uses multiple block sizes.
1171          * We report the fragsize as the smallest block size we support,
1172          * and we report our blocksize as the filesystem's maximum blocksize.
1173          */
1174         statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1175         statp->f_bsize = zfsvfs->z_max_blksz;
1176
1177         /*
1178          * The following report "total" blocks of various kinds in the
1179          * file system, but reported in terms of f_frsize - the
1180          * "fragment" size.
1181          */
1182
1183         statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1184         statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1185         statp->f_bavail = statp->f_bfree; /* no root reservation */
1186
1187         /*
1188          * statvfs() should really be called statufs(), because it assumes
1189          * static metadata.  ZFS doesn't preallocate files, so the best
1190          * we can do is report the max that could possibly fit in f_files,
1191          * and that minus the number actually used in f_ffree.
1192          * For f_ffree, report the smaller of the number of object available
1193          * and the number of blocks (each object will take at least a block).
1194          */
1195         statp->f_ffree = MIN(availobjs, statp->f_bfree);
1196         statp->f_favail = statp->f_ffree;       /* no "root reservation" */
1197         statp->f_files = statp->f_ffree + usedobjs;
1198
1199         (void) cmpldev(&d32, vfsp->vfs_dev);
1200         statp->f_fsid = d32;
1201
1202         /*
1203          * We're a zfs filesystem.
1204          */
1205         (void) strcpy(statp->f_basetype, MNTTYPE_ZFS);
1206
1207         statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1208
1209         statp->f_namemax = ZFS_MAXNAMELEN;
1210
1211         /*
1212          * We have all of 32 characters to stuff a string here.
1213          * Is there anything useful we could/should provide?
1214          */
1215         bzero(statp->f_fstr, sizeof (statp->f_fstr));
1216
1217         ZFS_EXIT(zfsvfs);
1218         return (0);
1219 }
1220 EXPORT_SYMBOL(zfs_statvfs);
1221
1222 int
1223 zfs_root(vfs_t *vfsp, vnode_t **vpp)
1224 {
1225         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1226         znode_t *rootzp;
1227         int error;
1228
1229         ZFS_ENTER(zfsvfs);
1230
1231         error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1232         if (error == 0)
1233                 *vpp = ZTOV(rootzp);
1234
1235         ZFS_EXIT(zfsvfs);
1236         return (error);
1237 }
1238 EXPORT_SYMBOL(zfs_root);
1239
1240 /*
1241  * Teardown the zfsvfs::z_os.
1242  *
1243  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1244  * and 'z_teardown_inactive_lock' held.
1245  */
1246 static int
1247 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1248 {
1249         znode_t *zp;
1250
1251         rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1252
1253         if (!unmounting) {
1254                 /*
1255                  * We purge the parent filesystem's vfsp as the parent
1256                  * filesystem and all of its snapshots have their vnode's
1257                  * v_vfsp set to the parent's filesystem's vfsp.  Note,
1258                  * 'z_parent' is self referential for non-snapshots.
1259                  */
1260                 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1261         }
1262
1263         /*
1264          * Close the zil. NB: Can't close the zil while zfs_inactive
1265          * threads are blocked as zil_close can call zfs_inactive.
1266          */
1267         if (zfsvfs->z_log) {
1268                 zil_close(zfsvfs->z_log);
1269                 zfsvfs->z_log = NULL;
1270         }
1271
1272         rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1273
1274         /*
1275          * If we are not unmounting (ie: online recv) and someone already
1276          * unmounted this file system while we were doing the switcheroo,
1277          * or a reopen of z_os failed then just bail out now.
1278          */
1279         if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1280                 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1281                 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1282                 return (EIO);
1283         }
1284
1285         /*
1286          * At this point there are no vops active, and any new vops will
1287          * fail with EIO since we have z_teardown_lock for writer (only
1288          * relavent for forced unmount).
1289          *
1290          * Release all holds on dbufs.
1291          */
1292         mutex_enter(&zfsvfs->z_znodes_lock);
1293         for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1294             zp = list_next(&zfsvfs->z_all_znodes, zp))
1295                 if (zp->z_sa_hdl) {
1296                         ASSERT(ZTOV(zp)->v_count > 0);
1297                         zfs_znode_dmu_fini(zp);
1298                 }
1299         mutex_exit(&zfsvfs->z_znodes_lock);
1300
1301         /*
1302          * If we are unmounting, set the unmounted flag and let new vops
1303          * unblock.  zfs_inactive will have the unmounted behavior, and all
1304          * other vops will fail with EIO.
1305          */
1306         if (unmounting) {
1307                 zfsvfs->z_unmounted = B_TRUE;
1308                 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1309                 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1310         }
1311
1312         /*
1313          * z_os will be NULL if there was an error in attempting to reopen
1314          * zfsvfs, so just return as the properties had already been
1315          * unregistered and cached data had been evicted before.
1316          */
1317         if (zfsvfs->z_os == NULL)
1318                 return (0);
1319
1320         /*
1321          * Unregister properties.
1322          */
1323         zfs_unregister_callbacks(zfsvfs);
1324
1325         /*
1326          * Evict cached data
1327          */
1328         if (dmu_objset_is_dirty_anywhere(zfsvfs->z_os))
1329                 if (!(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
1330                         txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1331         (void) dmu_objset_evict_dbufs(zfsvfs->z_os);
1332
1333         return (0);
1334 }
1335
1336 /*ARGSUSED*/
1337 int
1338 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1339 {
1340         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1341         objset_t *os;
1342         int ret;
1343
1344         ret = secpolicy_fs_unmount(cr, vfsp);
1345         if (ret) {
1346                 if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1347                     ZFS_DELEG_PERM_MOUNT, cr))
1348                         return (ret);
1349         }
1350
1351         /*
1352          * We purge the parent filesystem's vfsp as the parent filesystem
1353          * and all of its snapshots have their vnode's v_vfsp set to the
1354          * parent's filesystem's vfsp.  Note, 'z_parent' is self
1355          * referential for non-snapshots.
1356          */
1357         (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1358
1359         /*
1360          * Unmount any snapshots mounted under .zfs before unmounting the
1361          * dataset itself.
1362          */
1363         if (zfsvfs->z_ctldir != NULL &&
1364             (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1365                 return (ret);
1366         }
1367
1368         if (!(fflag & MS_FORCE)) {
1369                 /*
1370                  * Check the number of active vnodes in the file system.
1371                  * Our count is maintained in the vfs structure, but the
1372                  * number is off by 1 to indicate a hold on the vfs
1373                  * structure itself.
1374                  *
1375                  * The '.zfs' directory maintains a reference of its
1376                  * own, and any active references underneath are
1377                  * reflected in the vnode count.
1378                  */
1379                 if (zfsvfs->z_ctldir == NULL) {
1380                         if (vfsp->vfs_count > 1)
1381                                 return (EBUSY);
1382                 } else {
1383                         if (vfsp->vfs_count > 2 ||
1384                             zfsvfs->z_ctldir->v_count > 1)
1385                                 return (EBUSY);
1386                 }
1387         }
1388
1389         vfsp->vfs_flag |= VFS_UNMOUNTED;
1390
1391         VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1392         os = zfsvfs->z_os;
1393
1394         /*
1395          * z_os will be NULL if there was an error in
1396          * attempting to reopen zfsvfs.
1397          */
1398         if (os != NULL) {
1399                 /*
1400                  * Unset the objset user_ptr.
1401                  */
1402                 mutex_enter(&os->os_user_ptr_lock);
1403                 dmu_objset_set_user(os, NULL);
1404                 mutex_exit(&os->os_user_ptr_lock);
1405
1406                 /*
1407                  * Finally release the objset
1408                  */
1409                 dmu_objset_disown(os, zfsvfs);
1410         }
1411
1412         /*
1413          * We can now safely destroy the '.zfs' directory node.
1414          */
1415         if (zfsvfs->z_ctldir != NULL)
1416                 zfsctl_destroy(zfsvfs);
1417
1418         return (0);
1419 }
1420 EXPORT_SYMBOL(zfs_umount);
1421
1422 int
1423 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1424 {
1425         zfsvfs_t        *zfsvfs = vfsp->vfs_data;
1426         znode_t         *zp;
1427         uint64_t        object = 0;
1428         uint64_t        fid_gen = 0;
1429         uint64_t        gen_mask;
1430         uint64_t        zp_gen;
1431         int             i, err;
1432
1433         *vpp = NULL;
1434
1435         ZFS_ENTER(zfsvfs);
1436
1437         if (fidp->fid_len == LONG_FID_LEN) {
1438                 zfid_long_t     *zlfid = (zfid_long_t *)fidp;
1439                 uint64_t        objsetid = 0;
1440                 uint64_t        setgen = 0;
1441
1442                 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1443                         objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1444
1445                 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1446                         setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1447
1448                 ZFS_EXIT(zfsvfs);
1449
1450                 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1451                 if (err)
1452                         return (EINVAL);
1453                 ZFS_ENTER(zfsvfs);
1454         }
1455
1456         if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1457                 zfid_short_t    *zfid = (zfid_short_t *)fidp;
1458
1459                 for (i = 0; i < sizeof (zfid->zf_object); i++)
1460                         object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1461
1462                 for (i = 0; i < sizeof (zfid->zf_gen); i++)
1463                         fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1464         } else {
1465                 ZFS_EXIT(zfsvfs);
1466                 return (EINVAL);
1467         }
1468
1469         /* A zero fid_gen means we are in the .zfs control directories */
1470         if (fid_gen == 0 &&
1471             (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1472                 *vpp = zfsvfs->z_ctldir;
1473                 ASSERT(*vpp != NULL);
1474                 if (object == ZFSCTL_INO_SNAPDIR) {
1475                         VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1476                             0, NULL, NULL, NULL, NULL, NULL) == 0);
1477                 } else {
1478                         VN_HOLD(*vpp);
1479                 }
1480                 ZFS_EXIT(zfsvfs);
1481                 return (0);
1482         }
1483
1484         gen_mask = -1ULL >> (64 - 8 * i);
1485
1486         dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1487         if ((err = zfs_zget(zfsvfs, object, &zp))) {
1488                 ZFS_EXIT(zfsvfs);
1489                 return (err);
1490         }
1491         (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
1492             sizeof (uint64_t));
1493         zp_gen = zp_gen & gen_mask;
1494         if (zp_gen == 0)
1495                 zp_gen = 1;
1496         if (zp->z_unlinked || zp_gen != fid_gen) {
1497                 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1498                 VN_RELE(ZTOV(zp));
1499                 ZFS_EXIT(zfsvfs);
1500                 return (EINVAL);
1501         }
1502
1503         *vpp = ZTOV(zp);
1504         if (*vpp)
1505                 zfs_inode_update(VTOZ(*vpp));
1506
1507         ZFS_EXIT(zfsvfs);
1508         return (0);
1509 }
1510 EXPORT_SYMBOL(zfs_vget);
1511
1512 /*
1513  * Block out VOPs and close zfsvfs_t::z_os
1514  *
1515  * Note, if successful, then we return with the 'z_teardown_lock' and
1516  * 'z_teardown_inactive_lock' write held.
1517  */
1518 int
1519 zfs_suspend_fs(zfsvfs_t *zfsvfs)
1520 {
1521         int error;
1522
1523         if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
1524                 return (error);
1525         dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1526
1527         return (0);
1528 }
1529 EXPORT_SYMBOL(zfs_suspend_fs);
1530
1531 /*
1532  * Reopen zfsvfs_t::z_os and release VOPs.
1533  */
1534 int
1535 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname)
1536 {
1537         int err, err2;
1538
1539         ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
1540         ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
1541
1542         err = dmu_objset_own(osname, DMU_OST_ZFS, B_FALSE, zfsvfs,
1543             &zfsvfs->z_os);
1544         if (err) {
1545                 zfsvfs->z_os = NULL;
1546         } else {
1547                 znode_t *zp;
1548                 uint64_t sa_obj = 0;
1549
1550                 err2 = zap_lookup(zfsvfs->z_os, MASTER_NODE_OBJ,
1551                     ZFS_SA_ATTRS, 8, 1, &sa_obj);
1552
1553                 if ((err || err2) && zfsvfs->z_version >= ZPL_VERSION_SA)
1554                         goto bail;
1555
1556
1557                 if ((err = sa_setup(zfsvfs->z_os, sa_obj,
1558                     zfs_attr_table,  ZPL_END, &zfsvfs->z_attr_table)) != 0)
1559                         goto bail;
1560
1561                 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
1562
1563                 /*
1564                  * Attempt to re-establish all the active znodes with
1565                  * their dbufs.  If a zfs_rezget() fails, then we'll let
1566                  * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1567                  * when they try to use their znode.
1568                  */
1569                 mutex_enter(&zfsvfs->z_znodes_lock);
1570                 for (zp = list_head(&zfsvfs->z_all_znodes); zp;
1571                     zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1572                         (void) zfs_rezget(zp);
1573                 }
1574                 mutex_exit(&zfsvfs->z_znodes_lock);
1575
1576         }
1577
1578 bail:
1579         /* release the VOPs */
1580         rw_exit(&zfsvfs->z_teardown_inactive_lock);
1581         rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1582
1583         if (err) {
1584                 /*
1585                  * Since we couldn't reopen zfsvfs::z_os, force
1586                  * unmount this file system.
1587                  */
1588                 if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
1589                         (void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
1590         }
1591         return (err);
1592 }
1593 EXPORT_SYMBOL(zfs_resume_fs);
1594
1595 static void
1596 zfs_freevfs(vfs_t *vfsp)
1597 {
1598         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1599
1600         zfsvfs_free(zfsvfs);
1601 }
1602 #endif /* HAVE_ZPL */
1603
1604 void
1605 zfs_init(void)
1606 {
1607         zfsctl_init();
1608         zfs_znode_init();
1609
1610         dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
1611 }
1612
1613 void
1614 zfs_fini(void)
1615 {
1616         zfsctl_fini();
1617         zfs_znode_fini();
1618 }
1619
1620 #ifdef HAVE_ZPL
1621 int
1622 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
1623 {
1624         int error;
1625         objset_t *os = zfsvfs->z_os;
1626         dmu_tx_t *tx;
1627
1628         if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
1629                 return (EINVAL);
1630
1631         if (newvers < zfsvfs->z_version)
1632                 return (EINVAL);
1633
1634         if (zfs_spa_version_map(newvers) >
1635             spa_version(dmu_objset_spa(zfsvfs->z_os)))
1636                 return (ENOTSUP);
1637
1638         tx = dmu_tx_create(os);
1639         dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
1640         if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
1641                 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
1642                     ZFS_SA_ATTRS);
1643                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1644         }
1645         error = dmu_tx_assign(tx, TXG_WAIT);
1646         if (error) {
1647                 dmu_tx_abort(tx);
1648                 return (error);
1649         }
1650
1651         error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1652             8, 1, &newvers, tx);
1653
1654         if (error) {
1655                 dmu_tx_commit(tx);
1656                 return (error);
1657         }
1658
1659         if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
1660                 uint64_t sa_obj;
1661
1662                 ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
1663                     SPA_VERSION_SA);
1664                 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1665                     DMU_OT_NONE, 0, tx);
1666
1667                 error = zap_add(os, MASTER_NODE_OBJ,
1668                     ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1669                 ASSERT3U(error, ==, 0);
1670
1671                 VERIFY(0 == sa_set_sa_object(os, sa_obj));
1672                 sa_register_update_callback(os, zfs_sa_upgrade);
1673         }
1674
1675         spa_history_log_internal(LOG_DS_UPGRADE,
1676             dmu_objset_spa(os), tx, "oldver=%llu newver=%llu dataset = %llu",
1677             zfsvfs->z_version, newvers, dmu_objset_id(os));
1678
1679         dmu_tx_commit(tx);
1680
1681         zfsvfs->z_version = newvers;
1682
1683 #ifdef HAVE_FUID_FEATURES
1684         if (zfsvfs->z_version >= ZPL_VERSION_FUID)
1685                 zfs_set_fuid_feature(zfsvfs);
1686 #endif /* HAVE_FUID_FEATURES */
1687
1688         return (0);
1689 }
1690 EXPORT_SYMBOL(zfs_set_version);
1691 #endif /* HAVE_ZPL */
1692
1693 /*
1694  * Read a property stored within the master node.
1695  */
1696 int
1697 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
1698 {
1699         const char *pname;
1700         int error = ENOENT;
1701
1702         /*
1703          * Look up the file system's value for the property.  For the
1704          * version property, we look up a slightly different string.
1705          */
1706         if (prop == ZFS_PROP_VERSION)
1707                 pname = ZPL_VERSION_STR;
1708         else
1709                 pname = zfs_prop_to_name(prop);
1710
1711         if (os != NULL)
1712                 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
1713
1714         if (error == ENOENT) {
1715                 /* No value set, use the default value */
1716                 switch (prop) {
1717                 case ZFS_PROP_VERSION:
1718                         *value = ZPL_VERSION;
1719                         break;
1720                 case ZFS_PROP_NORMALIZE:
1721                 case ZFS_PROP_UTF8ONLY:
1722                         *value = 0;
1723                         break;
1724                 case ZFS_PROP_CASE:
1725                         *value = ZFS_CASE_SENSITIVE;
1726                         break;
1727                 default:
1728                         return (error);
1729                 }
1730                 error = 0;
1731         }
1732         return (error);
1733 }