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