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