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