]>
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. |
a08ee875 | 23 | * Copyright (c) 2013 by Delphix. All rights reserved. |
34dc7c2f BB |
24 | */ |
25 | ||
26 | /* Portions Copyright 2007 Jeremy Teo */ | |
27 | ||
34dc7c2f BB |
28 | #ifdef _KERNEL |
29 | #include <sys/types.h> | |
30 | #include <sys/param.h> | |
31 | #include <sys/time.h> | |
32 | #include <sys/systm.h> | |
33 | #include <sys/sysmacros.h> | |
34 | #include <sys/resource.h> | |
35 | #include <sys/mntent.h> | |
36 | #include <sys/mkdev.h> | |
37 | #include <sys/u8_textprep.h> | |
38 | #include <sys/dsl_dataset.h> | |
39 | #include <sys/vfs.h> | |
40 | #include <sys/vfs_opreg.h> | |
41 | #include <sys/vnode.h> | |
42 | #include <sys/file.h> | |
43 | #include <sys/kmem.h> | |
44 | #include <sys/errno.h> | |
45 | #include <sys/unistd.h> | |
46 | #include <sys/mode.h> | |
47 | #include <sys/atomic.h> | |
48 | #include <vm/pvn.h> | |
49 | #include "fs/fs_subr.h" | |
50 | #include <sys/zfs_dir.h> | |
51 | #include <sys/zfs_acl.h> | |
52 | #include <sys/zfs_ioctl.h> | |
53 | #include <sys/zfs_rlock.h> | |
54 | #include <sys/zfs_fuid.h> | |
3558fd73 | 55 | #include <sys/zfs_vnops.h> |
ebe7e575 | 56 | #include <sys/zfs_ctldir.h> |
428870ff | 57 | #include <sys/dnode.h> |
34dc7c2f BB |
58 | #include <sys/fs/zfs.h> |
59 | #include <sys/kidmap.h> | |
3558fd73 | 60 | #include <sys/zpl.h> |
34dc7c2f BB |
61 | #endif /* _KERNEL */ |
62 | ||
63 | #include <sys/dmu.h> | |
e10b0808 | 64 | #include <sys/dmu_objset.h> |
34dc7c2f BB |
65 | #include <sys/refcount.h> |
66 | #include <sys/stat.h> | |
67 | #include <sys/zap.h> | |
68 | #include <sys/zfs_znode.h> | |
428870ff BB |
69 | #include <sys/sa.h> |
70 | #include <sys/zfs_sa.h> | |
572e2857 | 71 | #include <sys/zfs_stat.h> |
34dc7c2f BB |
72 | |
73 | #include "zfs_prop.h" | |
428870ff | 74 | #include "zfs_comutil.h" |
34dc7c2f | 75 | |
b128c09f BB |
76 | /* |
77 | * Define ZNODE_STATS to turn on statistic gathering. By default, it is only | |
78 | * turned on when DEBUG is also defined. | |
79 | */ | |
80 | #ifdef DEBUG | |
81 | #define ZNODE_STATS | |
82 | #endif /* DEBUG */ | |
83 | ||
84 | #ifdef ZNODE_STATS | |
85 | #define ZNODE_STAT_ADD(stat) ((stat)++) | |
86 | #else | |
87 | #define ZNODE_STAT_ADD(stat) /* nothing */ | |
88 | #endif /* ZNODE_STATS */ | |
89 | ||
34dc7c2f BB |
90 | /* |
91 | * Functions needed for userland (ie: libzpool) are not put under | |
92 | * #ifdef_KERNEL; the rest of the functions have dependencies | |
93 | * (such as VFS logic) that will not compile easily in userland. | |
94 | */ | |
95 | #ifdef _KERNEL | |
9babb374 | 96 | |
b128c09f | 97 | static kmem_cache_t *znode_cache = NULL; |
34dc7c2f | 98 | |
34dc7c2f BB |
99 | /*ARGSUSED*/ |
100 | static int | |
b128c09f | 101 | zfs_znode_cache_constructor(void *buf, void *arg, int kmflags) |
34dc7c2f BB |
102 | { |
103 | znode_t *zp = buf; | |
104 | ||
3558fd73 | 105 | inode_init_once(ZTOI(zp)); |
b128c09f BB |
106 | list_link_init(&zp->z_link_node); |
107 | ||
34dc7c2f | 108 | mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL); |
34dc7c2f BB |
109 | rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL); |
110 | rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL); | |
111 | mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL); | |
82a37189 | 112 | rw_init(&zp->z_xattr_lock, NULL, RW_DEFAULT, NULL); |
34dc7c2f BB |
113 | |
114 | mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL); | |
115 | avl_create(&zp->z_range_avl, zfs_range_compare, | |
116 | sizeof (rl_t), offsetof(rl_t, r_node)); | |
117 | ||
b128c09f | 118 | zp->z_dirlocks = NULL; |
45d1cae3 | 119 | zp->z_acl_cached = NULL; |
82a37189 | 120 | zp->z_xattr_cached = NULL; |
e89260a1 | 121 | zp->z_xattr_parent = NULL; |
572e2857 | 122 | zp->z_moved = 0; |
34dc7c2f BB |
123 | return (0); |
124 | } | |
125 | ||
126 | /*ARGSUSED*/ | |
127 | static void | |
b128c09f | 128 | zfs_znode_cache_destructor(void *buf, void *arg) |
34dc7c2f BB |
129 | { |
130 | znode_t *zp = buf; | |
131 | ||
b128c09f | 132 | ASSERT(!list_link_active(&zp->z_link_node)); |
34dc7c2f | 133 | mutex_destroy(&zp->z_lock); |
34dc7c2f BB |
134 | rw_destroy(&zp->z_parent_lock); |
135 | rw_destroy(&zp->z_name_lock); | |
136 | mutex_destroy(&zp->z_acl_lock); | |
82a37189 | 137 | rw_destroy(&zp->z_xattr_lock); |
34dc7c2f BB |
138 | avl_destroy(&zp->z_range_avl); |
139 | mutex_destroy(&zp->z_range_lock); | |
140 | ||
b128c09f | 141 | ASSERT(zp->z_dirlocks == NULL); |
45d1cae3 | 142 | ASSERT(zp->z_acl_cached == NULL); |
82a37189 | 143 | ASSERT(zp->z_xattr_cached == NULL); |
e89260a1 | 144 | ASSERT(zp->z_xattr_parent == NULL); |
b128c09f BB |
145 | } |
146 | ||
34dc7c2f BB |
147 | void |
148 | zfs_znode_init(void) | |
149 | { | |
150 | /* | |
ea04106b AX |
151 | * Initialize zcache. The KMC_SLAB hint is used in order that it be |
152 | * backed by kmalloc() when on the Linux slab in order that any | |
153 | * wait_on_bit() operations on the related inode operate properly. | |
34dc7c2f BB |
154 | */ |
155 | ASSERT(znode_cache == NULL); | |
156 | znode_cache = kmem_cache_create("zfs_znode_cache", | |
157 | sizeof (znode_t), 0, zfs_znode_cache_constructor, | |
ea04106b | 158 | zfs_znode_cache_destructor, NULL, NULL, NULL, KMC_SLAB); |
34dc7c2f BB |
159 | } |
160 | ||
161 | void | |
162 | zfs_znode_fini(void) | |
163 | { | |
34dc7c2f BB |
164 | /* |
165 | * Cleanup zcache | |
166 | */ | |
167 | if (znode_cache) | |
168 | kmem_cache_destroy(znode_cache); | |
169 | znode_cache = NULL; | |
170 | } | |
171 | ||
34dc7c2f | 172 | int |
3558fd73 | 173 | zfs_create_share_dir(zfs_sb_t *zsb, dmu_tx_t *tx) |
34dc7c2f | 174 | { |
3c9609b3 | 175 | #ifdef HAVE_SMB_SHARE |
9babb374 BB |
176 | zfs_acl_ids_t acl_ids; |
177 | vattr_t vattr; | |
178 | znode_t *sharezp; | |
179 | vnode_t *vp; | |
180 | znode_t *zp; | |
181 | int error; | |
34dc7c2f | 182 | |
9babb374 | 183 | vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; |
3558fd73 | 184 | vattr.va_mode = S_IFDIR | 0555; |
9babb374 BB |
185 | vattr.va_uid = crgetuid(kcred); |
186 | vattr.va_gid = crgetgid(kcred); | |
34dc7c2f | 187 | |
ea04106b | 188 | sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP); |
572e2857 | 189 | sharezp->z_moved = 0; |
9babb374 BB |
190 | sharezp->z_unlinked = 0; |
191 | sharezp->z_atime_dirty = 0; | |
192 | sharezp->z_zfsvfs = zfsvfs; | |
428870ff | 193 | sharezp->z_is_sa = zfsvfs->z_use_sa; |
34dc7c2f | 194 | |
9babb374 BB |
195 | vp = ZTOV(sharezp); |
196 | vn_reinit(vp); | |
197 | vp->v_type = VDIR; | |
34dc7c2f | 198 | |
9babb374 BB |
199 | VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr, |
200 | kcred, NULL, &acl_ids)); | |
428870ff | 201 | zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids); |
9babb374 BB |
202 | ASSERT3P(zp, ==, sharezp); |
203 | ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */ | |
204 | POINTER_INVALIDATE(&sharezp->z_zfsvfs); | |
205 | error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ, | |
206 | ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx); | |
207 | zfsvfs->z_shares_dir = sharezp->z_id; | |
208 | ||
209 | zfs_acl_ids_free(&acl_ids); | |
3558fd73 | 210 | // ZTOV(sharezp)->v_count = 0; |
428870ff | 211 | sa_handle_destroy(sharezp->z_sa_hdl); |
9babb374 | 212 | kmem_cache_free(znode_cache, sharezp); |
34dc7c2f | 213 | |
9babb374 | 214 | return (error); |
9ee7fac5 BB |
215 | #else |
216 | return (0); | |
3c9609b3 | 217 | #endif /* HAVE_SMB_SHARE */ |
34dc7c2f BB |
218 | } |
219 | ||
34dc7c2f | 220 | static void |
3558fd73 | 221 | zfs_znode_sa_init(zfs_sb_t *zsb, znode_t *zp, |
428870ff | 222 | dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl) |
34dc7c2f | 223 | { |
3558fd73 | 224 | ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zsb, zp->z_id))); |
34dc7c2f BB |
225 | |
226 | mutex_enter(&zp->z_lock); | |
227 | ||
428870ff BB |
228 | ASSERT(zp->z_sa_hdl == NULL); |
229 | ASSERT(zp->z_acl_cached == NULL); | |
230 | if (sa_hdl == NULL) { | |
3558fd73 | 231 | VERIFY(0 == sa_handle_get_from_db(zsb->z_os, db, zp, |
428870ff BB |
232 | SA_HDL_SHARED, &zp->z_sa_hdl)); |
233 | } else { | |
234 | zp->z_sa_hdl = sa_hdl; | |
235 | sa_set_userp(sa_hdl, zp); | |
236 | } | |
34dc7c2f | 237 | |
428870ff | 238 | zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE; |
34dc7c2f | 239 | |
34dc7c2f | 240 | mutex_exit(&zp->z_lock); |
34dc7c2f BB |
241 | } |
242 | ||
243 | void | |
244 | zfs_znode_dmu_fini(znode_t *zp) | |
245 | { | |
3558fd73 | 246 | ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(ZTOZSB(zp), zp->z_id)) || |
b128c09f | 247 | zp->z_unlinked || |
3558fd73 | 248 | RW_WRITE_HELD(&ZTOZSB(zp)->z_teardown_inactive_lock)); |
428870ff BB |
249 | |
250 | sa_handle_destroy(zp->z_sa_hdl); | |
251 | zp->z_sa_hdl = NULL; | |
34dc7c2f BB |
252 | } |
253 | ||
254 | /* | |
3558fd73 BB |
255 | * Called by new_inode() to allocate a new inode. |
256 | */ | |
257 | int | |
258 | zfs_inode_alloc(struct super_block *sb, struct inode **ip) | |
259 | { | |
260 | znode_t *zp; | |
261 | ||
ea04106b | 262 | zp = kmem_cache_alloc(znode_cache, KM_SLEEP); |
3558fd73 BB |
263 | *ip = ZTOI(zp); |
264 | ||
265 | return (0); | |
266 | } | |
267 | ||
268 | /* | |
269 | * Called in multiple places when an inode should be destroyed. | |
270 | */ | |
271 | void | |
272 | zfs_inode_destroy(struct inode *ip) | |
273 | { | |
274 | znode_t *zp = ITOZ(ip); | |
275 | zfs_sb_t *zsb = ZTOZSB(zp); | |
276 | ||
277 | mutex_enter(&zsb->z_znodes_lock); | |
7b3e34ba BB |
278 | if (list_link_active(&zp->z_link_node)) { |
279 | list_remove(&zsb->z_all_znodes, zp); | |
280 | zsb->z_nr_znodes--; | |
281 | } | |
3558fd73 BB |
282 | mutex_exit(&zsb->z_znodes_lock); |
283 | ||
284 | if (zp->z_acl_cached) { | |
285 | zfs_acl_free(zp->z_acl_cached); | |
286 | zp->z_acl_cached = NULL; | |
287 | } | |
288 | ||
82a37189 BB |
289 | if (zp->z_xattr_cached) { |
290 | nvlist_free(zp->z_xattr_cached); | |
291 | zp->z_xattr_cached = NULL; | |
292 | } | |
293 | ||
e89260a1 | 294 | if (zp->z_xattr_parent) { |
ea04106b | 295 | zfs_iput_async(ZTOI(zp->z_xattr_parent)); |
e89260a1 BB |
296 | zp->z_xattr_parent = NULL; |
297 | } | |
298 | ||
3558fd73 BB |
299 | kmem_cache_free(znode_cache, zp); |
300 | } | |
301 | ||
302 | static void | |
303 | zfs_inode_set_ops(zfs_sb_t *zsb, struct inode *ip) | |
304 | { | |
aa6d8c10 | 305 | uint64_t rdev = 0; |
3558fd73 BB |
306 | |
307 | switch (ip->i_mode & S_IFMT) { | |
308 | case S_IFREG: | |
309 | ip->i_op = &zpl_inode_operations; | |
310 | ip->i_fop = &zpl_file_operations; | |
311 | ip->i_mapping->a_ops = &zpl_address_space_operations; | |
312 | break; | |
313 | ||
314 | case S_IFDIR: | |
315 | ip->i_op = &zpl_dir_inode_operations; | |
316 | ip->i_fop = &zpl_dir_file_operations; | |
317 | ITOZ(ip)->z_zn_prefetch = B_TRUE; | |
318 | break; | |
319 | ||
320 | case S_IFLNK: | |
321 | ip->i_op = &zpl_symlink_inode_operations; | |
322 | break; | |
323 | ||
aa6d8c10 NB |
324 | /* |
325 | * rdev is only stored in a SA only for device files. | |
326 | */ | |
3558fd73 BB |
327 | case S_IFCHR: |
328 | case S_IFBLK: | |
e10b0808 AX |
329 | sa_lookup(ITOZ(ip)->z_sa_hdl, SA_ZPL_RDEV(zsb), &rdev, |
330 | sizeof (rdev)); | |
aa6d8c10 NB |
331 | /*FALLTHROUGH*/ |
332 | case S_IFIFO: | |
333 | case S_IFSOCK: | |
3558fd73 BB |
334 | init_special_inode(ip, ip->i_mode, rdev); |
335 | ip->i_op = &zpl_special_inode_operations; | |
336 | break; | |
337 | ||
338 | default: | |
e10b0808 AX |
339 | zfs_panic_recover("inode %llu has invalid mode: 0x%x\n", |
340 | (u_longlong_t)ip->i_ino, ip->i_mode); | |
341 | ||
342 | /* Assume the inode is a file and attempt to continue */ | |
343 | ip->i_mode = S_IFREG | 0644; | |
344 | ip->i_op = &zpl_inode_operations; | |
345 | ip->i_fop = &zpl_file_operations; | |
346 | ip->i_mapping->a_ops = &zpl_address_space_operations; | |
347 | break; | |
3558fd73 BB |
348 | } |
349 | } | |
350 | ||
351 | /* | |
352 | * Construct a znode+inode and initialize. | |
34dc7c2f BB |
353 | * |
354 | * This does not do a call to dmu_set_user() that is | |
355 | * up to the caller to do, in case you don't want to | |
356 | * return the znode | |
357 | */ | |
358 | static znode_t * | |
3558fd73 | 359 | zfs_znode_alloc(zfs_sb_t *zsb, dmu_buf_t *db, int blksz, |
c85b224f | 360 | dmu_object_type_t obj_type, uint64_t obj, sa_handle_t *hdl, |
7b3e34ba | 361 | struct inode *dip) |
34dc7c2f BB |
362 | { |
363 | znode_t *zp; | |
3558fd73 | 364 | struct inode *ip; |
a08ee875 | 365 | uint64_t mode; |
428870ff | 366 | uint64_t parent; |
428870ff BB |
367 | sa_bulk_attr_t bulk[9]; |
368 | int count = 0; | |
34dc7c2f | 369 | |
3558fd73 | 370 | ASSERT(zsb != NULL); |
34dc7c2f | 371 | |
3558fd73 BB |
372 | ip = new_inode(zsb->z_sb); |
373 | if (ip == NULL) | |
374 | return (NULL); | |
7304b6e5 | 375 | |
3558fd73 | 376 | zp = ITOZ(ip); |
34dc7c2f | 377 | ASSERT(zp->z_dirlocks == NULL); |
ebe7e575 BB |
378 | ASSERT3P(zp->z_acl_cached, ==, NULL); |
379 | ASSERT3P(zp->z_xattr_cached, ==, NULL); | |
e89260a1 | 380 | ASSERT3P(zp->z_xattr_parent, ==, NULL); |
572e2857 | 381 | zp->z_moved = 0; |
428870ff | 382 | zp->z_sa_hdl = NULL; |
34dc7c2f BB |
383 | zp->z_unlinked = 0; |
384 | zp->z_atime_dirty = 0; | |
385 | zp->z_mapcnt = 0; | |
34dc7c2f BB |
386 | zp->z_id = db->db_object; |
387 | zp->z_blksz = blksz; | |
388 | zp->z_seq = 0x7A4653; | |
389 | zp->z_sync_cnt = 0; | |
ebe7e575 BB |
390 | zp->z_is_zvol = B_FALSE; |
391 | zp->z_is_mapped = B_FALSE; | |
392 | zp->z_is_ctldir = B_FALSE; | |
7b3e34ba | 393 | zp->z_is_stale = B_FALSE; |
34dc7c2f | 394 | |
3558fd73 BB |
395 | zfs_znode_sa_init(zsb, zp, db, obj_type, hdl); |
396 | ||
a08ee875 | 397 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL, &mode, 8); |
3558fd73 BB |
398 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zsb), NULL, &zp->z_gen, 8); |
399 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, &zp->z_size, 8); | |
400 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb), NULL, &zp->z_links, 8); | |
401 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL, | |
428870ff | 402 | &zp->z_pflags, 8); |
3558fd73 | 403 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zsb), NULL, |
7304b6e5 | 404 | &parent, 8); |
3558fd73 | 405 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL, |
428870ff | 406 | &zp->z_atime, 16); |
3558fd73 BB |
407 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL, &zp->z_uid, 8); |
408 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL, &zp->z_gid, 8); | |
428870ff BB |
409 | |
410 | if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) { | |
411 | if (hdl == NULL) | |
412 | sa_handle_destroy(zp->z_sa_hdl); | |
34dc7c2f | 413 | |
3558fd73 | 414 | goto error; |
34dc7c2f | 415 | } |
7304b6e5 | 416 | |
a08ee875 LG |
417 | zp->z_mode = mode; |
418 | ||
e89260a1 BB |
419 | /* |
420 | * xattr znodes hold a reference on their unique parent | |
421 | */ | |
422 | if (dip && zp->z_pflags & ZFS_XATTR) { | |
423 | igrab(dip); | |
424 | zp->z_xattr_parent = ITOZ(dip); | |
425 | } | |
426 | ||
3558fd73 | 427 | ip->i_ino = obj; |
5f35b190 | 428 | zfs_inode_update(zp); |
3558fd73 BB |
429 | zfs_inode_set_ops(zsb, ip); |
430 | ||
7b3e34ba BB |
431 | /* |
432 | * The only way insert_inode_locked() can fail is if the ip->i_ino | |
433 | * number is already hashed for this super block. This can never | |
434 | * happen because the inode numbers map 1:1 with the object numbers. | |
435 | * | |
436 | * The one exception is rolling back a mounted file system, but in | |
437 | * this case all the active inode are unhashed during the rollback. | |
438 | */ | |
439 | VERIFY3S(insert_inode_locked(ip), ==, 0); | |
c85b224f | 440 | |
3558fd73 BB |
441 | mutex_enter(&zsb->z_znodes_lock); |
442 | list_insert_tail(&zsb->z_all_znodes, zp); | |
ab26409d | 443 | zsb->z_nr_znodes++; |
b128c09f | 444 | membar_producer(); |
3558fd73 | 445 | mutex_exit(&zsb->z_znodes_lock); |
b128c09f | 446 | |
3558fd73 | 447 | unlock_new_inode(ip); |
34dc7c2f | 448 | return (zp); |
3558fd73 BB |
449 | |
450 | error: | |
451 | unlock_new_inode(ip); | |
452 | iput(ip); | |
a08ee875 | 453 | return (NULL); |
34dc7c2f BB |
454 | } |
455 | ||
ea04106b AX |
456 | void |
457 | zfs_set_inode_flags(znode_t *zp, struct inode *ip) | |
458 | { | |
459 | /* | |
460 | * Linux and Solaris have different sets of file attributes, so we | |
461 | * restrict this conversion to the intersection of the two. | |
462 | */ | |
463 | ||
464 | if (zp->z_pflags & ZFS_IMMUTABLE) | |
465 | ip->i_flags |= S_IMMUTABLE; | |
466 | else | |
467 | ip->i_flags &= ~S_IMMUTABLE; | |
468 | ||
469 | if (zp->z_pflags & ZFS_APPENDONLY) | |
470 | ip->i_flags |= S_APPEND; | |
471 | else | |
472 | ip->i_flags &= ~S_APPEND; | |
473 | } | |
474 | ||
960e08fe BB |
475 | /* |
476 | * Update the embedded inode given the znode. We should work toward | |
477 | * eliminating this function as soon as possible by removing values | |
478 | * which are duplicated between the znode and inode. If the generic | |
479 | * inode has the correct field it should be used, and the ZFS code | |
480 | * updated to access the inode. This can be done incrementally. | |
481 | */ | |
482 | void | |
483 | zfs_inode_update(znode_t *zp) | |
484 | { | |
3558fd73 BB |
485 | zfs_sb_t *zsb; |
486 | struct inode *ip; | |
960e08fe BB |
487 | uint32_t blksize; |
488 | uint64_t atime[2], mtime[2], ctime[2]; | |
489 | ||
490 | ASSERT(zp != NULL); | |
3558fd73 BB |
491 | zsb = ZTOZSB(zp); |
492 | ip = ZTOI(zp); | |
493 | ||
ebe7e575 BB |
494 | /* Skip .zfs control nodes which do not exist on disk. */ |
495 | if (zfsctl_is_node(ip)) | |
496 | return; | |
497 | ||
3558fd73 BB |
498 | sa_lookup(zp->z_sa_hdl, SA_ZPL_ATIME(zsb), &atime, 16); |
499 | sa_lookup(zp->z_sa_hdl, SA_ZPL_MTIME(zsb), &mtime, 16); | |
500 | sa_lookup(zp->z_sa_hdl, SA_ZPL_CTIME(zsb), &ctime, 16); | |
501 | ||
502 | spin_lock(&ip->i_lock); | |
503 | ip->i_generation = zp->z_gen; | |
c06d4368 AX |
504 | ip->i_uid = SUID_TO_KUID(zp->z_uid); |
505 | ip->i_gid = SGID_TO_KGID(zp->z_gid); | |
28eb9213 | 506 | set_nlink(ip, zp->z_links); |
3558fd73 | 507 | ip->i_mode = zp->z_mode; |
ea04106b | 508 | zfs_set_inode_flags(zp, ip); |
3558fd73 | 509 | ip->i_blkbits = SPA_MINBLOCKSHIFT; |
960e08fe | 510 | dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &blksize, |
3558fd73 | 511 | (u_longlong_t *)&ip->i_blocks); |
960e08fe | 512 | |
3558fd73 BB |
513 | ZFS_TIME_DECODE(&ip->i_atime, atime); |
514 | ZFS_TIME_DECODE(&ip->i_mtime, mtime); | |
515 | ZFS_TIME_DECODE(&ip->i_ctime, ctime); | |
960e08fe | 516 | |
3558fd73 BB |
517 | i_size_write(ip, zp->z_size); |
518 | spin_unlock(&ip->i_lock); | |
960e08fe BB |
519 | } |
520 | ||
ea04106b AX |
521 | /* |
522 | * Safely mark an inode dirty. Inodes which are part of a read-only | |
523 | * file system or snapshot may not be dirtied. | |
524 | */ | |
525 | void | |
526 | zfs_mark_inode_dirty(struct inode *ip) | |
527 | { | |
528 | zfs_sb_t *zsb = ITOZSB(ip); | |
529 | ||
530 | if (zfs_is_readonly(zsb) || dmu_objset_is_snapshot(zsb->z_os)) | |
531 | return; | |
532 | ||
533 | mark_inode_dirty(ip); | |
534 | } | |
535 | ||
428870ff BB |
536 | static uint64_t empty_xattr; |
537 | static uint64_t pad[4]; | |
538 | static zfs_acl_phys_t acl_phys; | |
34dc7c2f BB |
539 | /* |
540 | * Create a new DMU object to hold a zfs znode. | |
541 | * | |
542 | * IN: dzp - parent directory for new znode | |
543 | * vap - file attributes for new znode | |
544 | * tx - dmu transaction id for zap operations | |
545 | * cr - credentials of caller | |
546 | * flag - flags: | |
547 | * IS_ROOT_NODE - new object will be root | |
548 | * IS_XATTR - new object is an attribute | |
34dc7c2f BB |
549 | * bonuslen - length of bonus buffer |
550 | * setaclp - File/Dir initial ACL | |
551 | * fuidp - Tracks fuid allocation. | |
552 | * | |
553 | * OUT: zpp - allocated znode | |
554 | * | |
555 | */ | |
556 | void | |
557 | zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr, | |
428870ff | 558 | uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids) |
34dc7c2f | 559 | { |
428870ff BB |
560 | uint64_t crtime[2], atime[2], mtime[2], ctime[2]; |
561 | uint64_t mode, size, links, parent, pflags; | |
572e2857 | 562 | uint64_t dzp_pflags = 0; |
428870ff | 563 | uint64_t rdev = 0; |
3558fd73 | 564 | zfs_sb_t *zsb = ZTOZSB(dzp); |
428870ff | 565 | dmu_buf_t *db; |
34dc7c2f BB |
566 | timestruc_t now; |
567 | uint64_t gen, obj; | |
428870ff BB |
568 | int bonuslen; |
569 | sa_handle_t *sa_hdl; | |
570 | dmu_object_type_t obj_type; | |
f30484af | 571 | sa_bulk_attr_t *sa_attrs; |
428870ff BB |
572 | int cnt = 0; |
573 | zfs_acl_locator_cb_t locate = { 0 }; | |
34dc7c2f | 574 | |
3558fd73 | 575 | if (zsb->z_replay) { |
34dc7c2f | 576 | obj = vap->va_nodeid; |
34dc7c2f BB |
577 | now = vap->va_ctime; /* see zfs_replay_create() */ |
578 | gen = vap->va_nblocks; /* ditto */ | |
579 | } else { | |
580 | obj = 0; | |
581 | gethrestime(&now); | |
582 | gen = dmu_tx_get_txg(tx); | |
583 | } | |
584 | ||
3558fd73 | 585 | obj_type = zsb->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE; |
428870ff BB |
586 | bonuslen = (obj_type == DMU_OT_SA) ? |
587 | DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE; | |
588 | ||
34dc7c2f BB |
589 | /* |
590 | * Create a new DMU object. | |
591 | */ | |
592 | /* | |
593 | * There's currently no mechanism for pre-reading the blocks that will | |
572e2857 | 594 | * be needed to allocate a new object, so we accept the small chance |
34dc7c2f BB |
595 | * that there will be an i/o error and we will fail one of the |
596 | * assertions below. | |
597 | */ | |
3558fd73 BB |
598 | if (S_ISDIR(vap->va_mode)) { |
599 | if (zsb->z_replay) { | |
ea04106b | 600 | VERIFY0(zap_create_claim_norm(zsb->z_os, obj, |
3558fd73 | 601 | zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS, |
ea04106b | 602 | obj_type, bonuslen, tx)); |
34dc7c2f | 603 | } else { |
3558fd73 BB |
604 | obj = zap_create_norm(zsb->z_os, |
605 | zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS, | |
428870ff | 606 | obj_type, bonuslen, tx); |
34dc7c2f BB |
607 | } |
608 | } else { | |
3558fd73 | 609 | if (zsb->z_replay) { |
ea04106b | 610 | VERIFY0(dmu_object_claim(zsb->z_os, obj, |
34dc7c2f | 611 | DMU_OT_PLAIN_FILE_CONTENTS, 0, |
ea04106b | 612 | obj_type, bonuslen, tx)); |
34dc7c2f | 613 | } else { |
3558fd73 | 614 | obj = dmu_object_alloc(zsb->z_os, |
34dc7c2f | 615 | DMU_OT_PLAIN_FILE_CONTENTS, 0, |
428870ff | 616 | obj_type, bonuslen, tx); |
34dc7c2f BB |
617 | } |
618 | } | |
34dc7c2f | 619 | |
3558fd73 BB |
620 | ZFS_OBJ_HOLD_ENTER(zsb, obj); |
621 | VERIFY(0 == sa_buf_hold(zsb->z_os, obj, NULL, &db)); | |
34dc7c2f BB |
622 | |
623 | /* | |
624 | * If this is the root, fix up the half-initialized parent pointer | |
625 | * to reference the just-allocated physical data area. | |
626 | */ | |
627 | if (flag & IS_ROOT_NODE) { | |
34dc7c2f | 628 | dzp->z_id = obj; |
428870ff BB |
629 | } else { |
630 | dzp_pflags = dzp->z_pflags; | |
34dc7c2f BB |
631 | } |
632 | ||
633 | /* | |
634 | * If parent is an xattr, so am I. | |
635 | */ | |
428870ff | 636 | if (dzp_pflags & ZFS_XATTR) { |
34dc7c2f | 637 | flag |= IS_XATTR; |
34dc7c2f BB |
638 | } |
639 | ||
3558fd73 | 640 | if (zsb->z_use_fuids) |
428870ff BB |
641 | pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED; |
642 | else | |
643 | pflags = 0; | |
34dc7c2f | 644 | |
3558fd73 | 645 | if (S_ISDIR(vap->va_mode)) { |
428870ff BB |
646 | size = 2; /* contents ("." and "..") */ |
647 | links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1; | |
648 | } else { | |
649 | size = links = 0; | |
34dc7c2f BB |
650 | } |
651 | ||
aa6d8c10 | 652 | if (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode)) |
dc1d7665 | 653 | rdev = vap->va_rdev; |
428870ff BB |
654 | |
655 | parent = dzp->z_id; | |
656 | mode = acl_ids->z_mode; | |
34dc7c2f | 657 | if (flag & IS_XATTR) |
428870ff | 658 | pflags |= ZFS_XATTR; |
34dc7c2f | 659 | |
428870ff BB |
660 | /* |
661 | * No execs denied will be deterimed when zfs_mode_compute() is called. | |
662 | */ | |
663 | pflags |= acl_ids->z_aclp->z_hints & | |
664 | (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT| | |
665 | ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED); | |
34dc7c2f | 666 | |
428870ff BB |
667 | ZFS_TIME_ENCODE(&now, crtime); |
668 | ZFS_TIME_ENCODE(&now, ctime); | |
34dc7c2f | 669 | |
3558fd73 | 670 | if (vap->va_mask & ATTR_ATIME) { |
428870ff | 671 | ZFS_TIME_ENCODE(&vap->va_atime, atime); |
34dc7c2f | 672 | } else { |
428870ff | 673 | ZFS_TIME_ENCODE(&now, atime); |
34dc7c2f BB |
674 | } |
675 | ||
3558fd73 | 676 | if (vap->va_mask & ATTR_MTIME) { |
428870ff BB |
677 | ZFS_TIME_ENCODE(&vap->va_mtime, mtime); |
678 | } else { | |
679 | ZFS_TIME_ENCODE(&now, mtime); | |
680 | } | |
681 | ||
682 | /* Now add in all of the "SA" attributes */ | |
3558fd73 | 683 | VERIFY(0 == sa_handle_get_from_db(zsb->z_os, db, NULL, SA_HDL_SHARED, |
428870ff BB |
684 | &sa_hdl)); |
685 | ||
686 | /* | |
687 | * Setup the array of attributes to be replaced/set on the new file | |
688 | * | |
689 | * order for DMU_OT_ZNODE is critical since it needs to be constructed | |
690 | * in the old znode_phys_t format. Don't change this ordering | |
691 | */ | |
ea04106b | 692 | sa_attrs = kmem_alloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP); |
428870ff BB |
693 | |
694 | if (obj_type == DMU_OT_ZNODE) { | |
3558fd73 | 695 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb), |
428870ff | 696 | NULL, &atime, 16); |
3558fd73 | 697 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb), |
428870ff | 698 | NULL, &mtime, 16); |
3558fd73 | 699 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb), |
428870ff | 700 | NULL, &ctime, 16); |
3558fd73 | 701 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb), |
428870ff | 702 | NULL, &crtime, 16); |
3558fd73 | 703 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb), |
428870ff | 704 | NULL, &gen, 8); |
3558fd73 | 705 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb), |
428870ff | 706 | NULL, &mode, 8); |
3558fd73 | 707 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb), |
428870ff | 708 | NULL, &size, 8); |
3558fd73 | 709 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb), |
428870ff | 710 | NULL, &parent, 8); |
34dc7c2f | 711 | } else { |
3558fd73 | 712 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb), |
428870ff | 713 | NULL, &mode, 8); |
3558fd73 | 714 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb), |
428870ff | 715 | NULL, &size, 8); |
3558fd73 | 716 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb), |
428870ff | 717 | NULL, &gen, 8); |
3558fd73 BB |
718 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb), |
719 | NULL, &acl_ids->z_fuid, 8); | |
720 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb), | |
721 | NULL, &acl_ids->z_fgid, 8); | |
722 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb), | |
428870ff | 723 | NULL, &parent, 8); |
3558fd73 | 724 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb), |
428870ff | 725 | NULL, &pflags, 8); |
3558fd73 | 726 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb), |
428870ff | 727 | NULL, &atime, 16); |
3558fd73 | 728 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb), |
428870ff | 729 | NULL, &mtime, 16); |
3558fd73 | 730 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb), |
428870ff | 731 | NULL, &ctime, 16); |
3558fd73 | 732 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb), |
428870ff BB |
733 | NULL, &crtime, 16); |
734 | } | |
735 | ||
3558fd73 | 736 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zsb), NULL, &links, 8); |
428870ff BB |
737 | |
738 | if (obj_type == DMU_OT_ZNODE) { | |
3558fd73 | 739 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zsb), NULL, |
428870ff | 740 | &empty_xattr, 8); |
34dc7c2f | 741 | } |
428870ff | 742 | if (obj_type == DMU_OT_ZNODE || |
aa6d8c10 | 743 | (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))) { |
3558fd73 | 744 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zsb), |
428870ff | 745 | NULL, &rdev, 8); |
428870ff BB |
746 | } |
747 | if (obj_type == DMU_OT_ZNODE) { | |
3558fd73 | 748 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb), |
428870ff | 749 | NULL, &pflags, 8); |
3558fd73 | 750 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb), NULL, |
428870ff | 751 | &acl_ids->z_fuid, 8); |
3558fd73 | 752 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb), NULL, |
428870ff | 753 | &acl_ids->z_fgid, 8); |
3558fd73 | 754 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zsb), NULL, pad, |
428870ff | 755 | sizeof (uint64_t) * 4); |
3558fd73 | 756 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zsb), NULL, |
428870ff BB |
757 | &acl_phys, sizeof (zfs_acl_phys_t)); |
758 | } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) { | |
3558fd73 | 759 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zsb), NULL, |
428870ff BB |
760 | &acl_ids->z_aclp->z_acl_count, 8); |
761 | locate.cb_aclp = acl_ids->z_aclp; | |
3558fd73 | 762 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zsb), |
428870ff BB |
763 | zfs_acl_data_locator, &locate, |
764 | acl_ids->z_aclp->z_acl_bytes); | |
765 | mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags, | |
766 | acl_ids->z_fuid, acl_ids->z_fgid); | |
767 | } | |
768 | ||
769 | VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0); | |
34dc7c2f | 770 | |
34dc7c2f | 771 | if (!(flag & IS_ROOT_NODE)) { |
c85b224f | 772 | *zpp = zfs_znode_alloc(zsb, db, 0, obj_type, obj, sa_hdl, |
7b3e34ba BB |
773 | ZTOI(dzp)); |
774 | VERIFY(*zpp != NULL); | |
775 | VERIFY(dzp != NULL); | |
34dc7c2f BB |
776 | } else { |
777 | /* | |
778 | * If we are creating the root node, the "parent" we | |
779 | * passed in is the znode for the root. | |
780 | */ | |
781 | *zpp = dzp; | |
428870ff BB |
782 | |
783 | (*zpp)->z_sa_hdl = sa_hdl; | |
34dc7c2f | 784 | } |
428870ff BB |
785 | |
786 | (*zpp)->z_pflags = pflags; | |
787 | (*zpp)->z_mode = mode; | |
788 | ||
428870ff BB |
789 | if (obj_type == DMU_OT_ZNODE || |
790 | acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) { | |
ea04106b | 791 | VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx)); |
428870ff | 792 | } |
a08ee875 | 793 | kmem_free(sa_attrs, sizeof (sa_bulk_attr_t) * ZPL_END); |
3558fd73 | 794 | ZFS_OBJ_HOLD_EXIT(zsb, obj); |
34dc7c2f BB |
795 | } |
796 | ||
5484965a | 797 | /* |
a08ee875 LG |
798 | * Update in-core attributes. It is assumed the caller will be doing an |
799 | * sa_bulk_update to push the changes out. | |
5484965a BB |
800 | */ |
801 | void | |
802 | zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx) | |
803 | { | |
804 | xoptattr_t *xoap; | |
805 | ||
806 | xoap = xva_getxoptattr(xvap); | |
807 | ASSERT(xoap); | |
808 | ||
809 | if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { | |
810 | uint64_t times[2]; | |
811 | ZFS_TIME_ENCODE(&xoap->xoa_createtime, times); | |
812 | (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)), | |
813 | ×, sizeof (times), tx); | |
814 | XVA_SET_RTN(xvap, XAT_CREATETIME); | |
815 | } | |
816 | if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { | |
817 | ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly, | |
818 | zp->z_pflags, tx); | |
819 | XVA_SET_RTN(xvap, XAT_READONLY); | |
820 | } | |
821 | if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { | |
822 | ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden, | |
823 | zp->z_pflags, tx); | |
824 | XVA_SET_RTN(xvap, XAT_HIDDEN); | |
825 | } | |
826 | if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { | |
827 | ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system, | |
828 | zp->z_pflags, tx); | |
829 | XVA_SET_RTN(xvap, XAT_SYSTEM); | |
830 | } | |
831 | if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { | |
832 | ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive, | |
833 | zp->z_pflags, tx); | |
834 | XVA_SET_RTN(xvap, XAT_ARCHIVE); | |
835 | } | |
836 | if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { | |
837 | ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable, | |
838 | zp->z_pflags, tx); | |
839 | XVA_SET_RTN(xvap, XAT_IMMUTABLE); | |
840 | } | |
841 | if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { | |
842 | ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink, | |
843 | zp->z_pflags, tx); | |
844 | XVA_SET_RTN(xvap, XAT_NOUNLINK); | |
845 | } | |
846 | if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { | |
847 | ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly, | |
848 | zp->z_pflags, tx); | |
849 | XVA_SET_RTN(xvap, XAT_APPENDONLY); | |
850 | } | |
851 | if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { | |
852 | ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump, | |
853 | zp->z_pflags, tx); | |
854 | XVA_SET_RTN(xvap, XAT_NODUMP); | |
855 | } | |
856 | if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { | |
857 | ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque, | |
858 | zp->z_pflags, tx); | |
859 | XVA_SET_RTN(xvap, XAT_OPAQUE); | |
860 | } | |
861 | if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { | |
862 | ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED, | |
863 | xoap->xoa_av_quarantined, zp->z_pflags, tx); | |
864 | XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); | |
865 | } | |
866 | if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { | |
867 | ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified, | |
868 | zp->z_pflags, tx); | |
869 | XVA_SET_RTN(xvap, XAT_AV_MODIFIED); | |
870 | } | |
871 | if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) { | |
872 | zfs_sa_set_scanstamp(zp, xvap, tx); | |
873 | XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP); | |
874 | } | |
875 | if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { | |
876 | ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse, | |
877 | zp->z_pflags, tx); | |
878 | XVA_SET_RTN(xvap, XAT_REPARSE); | |
879 | } | |
880 | if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) { | |
881 | ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline, | |
882 | zp->z_pflags, tx); | |
883 | XVA_SET_RTN(xvap, XAT_OFFLINE); | |
884 | } | |
885 | if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) { | |
886 | ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse, | |
887 | zp->z_pflags, tx); | |
888 | XVA_SET_RTN(xvap, XAT_SPARSE); | |
889 | } | |
890 | } | |
891 | ||
34dc7c2f | 892 | int |
3558fd73 | 893 | zfs_zget(zfs_sb_t *zsb, uint64_t obj_num, znode_t **zpp) |
34dc7c2f BB |
894 | { |
895 | dmu_object_info_t doi; | |
896 | dmu_buf_t *db; | |
897 | znode_t *zp; | |
898 | int err; | |
428870ff | 899 | sa_handle_t *hdl; |
34dc7c2f BB |
900 | |
901 | *zpp = NULL; | |
902 | ||
36df2843 | 903 | again: |
3558fd73 | 904 | ZFS_OBJ_HOLD_ENTER(zsb, obj_num); |
34dc7c2f | 905 | |
3558fd73 | 906 | err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db); |
34dc7c2f | 907 | if (err) { |
3558fd73 | 908 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
34dc7c2f BB |
909 | return (err); |
910 | } | |
911 | ||
912 | dmu_object_info_from_db(db, &doi); | |
428870ff BB |
913 | if (doi.doi_bonus_type != DMU_OT_SA && |
914 | (doi.doi_bonus_type != DMU_OT_ZNODE || | |
915 | (doi.doi_bonus_type == DMU_OT_ZNODE && | |
916 | doi.doi_bonus_size < sizeof (znode_phys_t)))) { | |
917 | sa_buf_rele(db, NULL); | |
3558fd73 | 918 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
a08ee875 | 919 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
920 | } |
921 | ||
428870ff BB |
922 | hdl = dmu_buf_get_user(db); |
923 | if (hdl != NULL) { | |
36df2843 | 924 | zp = sa_get_userdata(hdl); |
34dc7c2f | 925 | |
ea04106b | 926 | |
34dc7c2f | 927 | /* |
428870ff BB |
928 | * Since "SA" does immediate eviction we |
929 | * should never find a sa handle that doesn't | |
930 | * know about the znode. | |
34dc7c2f | 931 | */ |
428870ff BB |
932 | |
933 | ASSERT3P(zp, !=, NULL); | |
934 | ||
935 | mutex_enter(&zp->z_lock); | |
34dc7c2f BB |
936 | ASSERT3U(zp->z_id, ==, obj_num); |
937 | if (zp->z_unlinked) { | |
a08ee875 | 938 | err = SET_ERROR(ENOENT); |
34dc7c2f | 939 | } else { |
ea04106b AX |
940 | /* |
941 | * If igrab() returns NULL the VFS has independently | |
942 | * determined the inode should be evicted and has | |
943 | * called iput_final() to start the eviction process. | |
944 | * The SA handle is still valid but because the VFS | |
945 | * requires that the eviction succeed we must drop | |
946 | * our locks and references to allow the eviction to | |
947 | * complete. The zfs_zget() may then be retried. | |
948 | * | |
949 | * This unlikely case could be optimized by registering | |
950 | * a sops->drop_inode() callback. The callback would | |
951 | * need to detect the active SA hold thereby informing | |
952 | * the VFS that this inode should not be evicted. | |
953 | */ | |
954 | if (igrab(ZTOI(zp)) == NULL) { | |
955 | mutex_exit(&zp->z_lock); | |
956 | sa_buf_rele(db, NULL); | |
957 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); | |
958 | /* inode might need this to finish evict */ | |
959 | cond_resched(); | |
960 | goto again; | |
961 | } | |
34dc7c2f BB |
962 | *zpp = zp; |
963 | err = 0; | |
964 | } | |
34dc7c2f | 965 | mutex_exit(&zp->z_lock); |
ea04106b | 966 | sa_buf_rele(db, NULL); |
3558fd73 | 967 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
34dc7c2f BB |
968 | return (err); |
969 | } | |
970 | ||
971 | /* | |
3558fd73 | 972 | * Not found create new znode/vnode but only if file exists. |
428870ff BB |
973 | * |
974 | * There is a small window where zfs_vget() could | |
975 | * find this object while a file create is still in | |
976 | * progress. This is checked for in zfs_znode_alloc() | |
977 | * | |
978 | * if zfs_znode_alloc() fails it will drop the hold on the | |
979 | * bonus buffer. | |
34dc7c2f | 980 | */ |
3558fd73 | 981 | zp = zfs_znode_alloc(zsb, db, doi.doi_data_block_size, |
7b3e34ba | 982 | doi.doi_bonus_type, obj_num, NULL, NULL); |
428870ff | 983 | if (zp == NULL) { |
a08ee875 | 984 | err = SET_ERROR(ENOENT); |
428870ff BB |
985 | } else { |
986 | *zpp = zp; | |
987 | } | |
3558fd73 | 988 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
428870ff | 989 | return (err); |
34dc7c2f BB |
990 | } |
991 | ||
992 | int | |
993 | zfs_rezget(znode_t *zp) | |
994 | { | |
3558fd73 | 995 | zfs_sb_t *zsb = ZTOZSB(zp); |
34dc7c2f BB |
996 | dmu_object_info_t doi; |
997 | dmu_buf_t *db; | |
998 | uint64_t obj_num = zp->z_id; | |
428870ff | 999 | uint64_t mode; |
428870ff | 1000 | sa_bulk_attr_t bulk[8]; |
34dc7c2f | 1001 | int err; |
428870ff BB |
1002 | int count = 0; |
1003 | uint64_t gen; | |
34dc7c2f | 1004 | |
3558fd73 | 1005 | ZFS_OBJ_HOLD_ENTER(zsb, obj_num); |
34dc7c2f | 1006 | |
428870ff BB |
1007 | mutex_enter(&zp->z_acl_lock); |
1008 | if (zp->z_acl_cached) { | |
1009 | zfs_acl_free(zp->z_acl_cached); | |
1010 | zp->z_acl_cached = NULL; | |
1011 | } | |
428870ff | 1012 | mutex_exit(&zp->z_acl_lock); |
7b3e34ba BB |
1013 | |
1014 | rw_enter(&zp->z_xattr_lock, RW_WRITER); | |
1015 | if (zp->z_xattr_cached) { | |
1016 | nvlist_free(zp->z_xattr_cached); | |
1017 | zp->z_xattr_cached = NULL; | |
1018 | } | |
1019 | ||
1020 | if (zp->z_xattr_parent) { | |
ea04106b | 1021 | zfs_iput_async(ZTOI(zp->z_xattr_parent)); |
7b3e34ba BB |
1022 | zp->z_xattr_parent = NULL; |
1023 | } | |
1024 | rw_exit(&zp->z_xattr_lock); | |
1025 | ||
428870ff | 1026 | ASSERT(zp->z_sa_hdl == NULL); |
3558fd73 | 1027 | err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db); |
34dc7c2f | 1028 | if (err) { |
3558fd73 | 1029 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
34dc7c2f BB |
1030 | return (err); |
1031 | } | |
1032 | ||
1033 | dmu_object_info_from_db(db, &doi); | |
428870ff BB |
1034 | if (doi.doi_bonus_type != DMU_OT_SA && |
1035 | (doi.doi_bonus_type != DMU_OT_ZNODE || | |
1036 | (doi.doi_bonus_type == DMU_OT_ZNODE && | |
1037 | doi.doi_bonus_size < sizeof (znode_phys_t)))) { | |
1038 | sa_buf_rele(db, NULL); | |
3558fd73 | 1039 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
a08ee875 | 1040 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
1041 | } |
1042 | ||
3558fd73 | 1043 | zfs_znode_sa_init(zsb, zp, db, doi.doi_bonus_type, NULL); |
428870ff BB |
1044 | |
1045 | /* reload cached values */ | |
3558fd73 | 1046 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zsb), NULL, |
428870ff | 1047 | &gen, sizeof (gen)); |
3558fd73 | 1048 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, |
428870ff | 1049 | &zp->z_size, sizeof (zp->z_size)); |
3558fd73 | 1050 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb), NULL, |
428870ff | 1051 | &zp->z_links, sizeof (zp->z_links)); |
3558fd73 | 1052 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL, |
428870ff | 1053 | &zp->z_pflags, sizeof (zp->z_pflags)); |
3558fd73 | 1054 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL, |
428870ff | 1055 | &zp->z_atime, sizeof (zp->z_atime)); |
3558fd73 | 1056 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL, |
572e2857 | 1057 | &zp->z_uid, sizeof (zp->z_uid)); |
3558fd73 | 1058 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL, |
572e2857 | 1059 | &zp->z_gid, sizeof (zp->z_gid)); |
3558fd73 | 1060 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL, |
428870ff BB |
1061 | &mode, sizeof (mode)); |
1062 | ||
428870ff BB |
1063 | if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) { |
1064 | zfs_znode_dmu_fini(zp); | |
3558fd73 | 1065 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
a08ee875 | 1066 | return (SET_ERROR(EIO)); |
428870ff BB |
1067 | } |
1068 | ||
572e2857 BB |
1069 | zp->z_mode = mode; |
1070 | ||
428870ff BB |
1071 | if (gen != zp->z_gen) { |
1072 | zfs_znode_dmu_fini(zp); | |
3558fd73 | 1073 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
a08ee875 | 1074 | return (SET_ERROR(EIO)); |
34dc7c2f BB |
1075 | } |
1076 | ||
428870ff | 1077 | zp->z_unlinked = (zp->z_links == 0); |
34dc7c2f | 1078 | zp->z_blksz = doi.doi_data_block_size; |
7b3e34ba | 1079 | zfs_inode_update(zp); |
34dc7c2f | 1080 | |
3558fd73 | 1081 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
34dc7c2f BB |
1082 | |
1083 | return (0); | |
1084 | } | |
1085 | ||
1086 | void | |
1087 | zfs_znode_delete(znode_t *zp, dmu_tx_t *tx) | |
1088 | { | |
3558fd73 BB |
1089 | zfs_sb_t *zsb = ZTOZSB(zp); |
1090 | objset_t *os = zsb->z_os; | |
34dc7c2f | 1091 | uint64_t obj = zp->z_id; |
572e2857 | 1092 | uint64_t acl_obj = zfs_external_acl(zp); |
34dc7c2f | 1093 | |
3558fd73 | 1094 | ZFS_OBJ_HOLD_ENTER(zsb, obj); |
572e2857 BB |
1095 | if (acl_obj) { |
1096 | VERIFY(!zp->z_is_sa); | |
b128c09f | 1097 | VERIFY(0 == dmu_object_free(os, acl_obj, tx)); |
572e2857 | 1098 | } |
b128c09f | 1099 | VERIFY(0 == dmu_object_free(os, obj, tx)); |
34dc7c2f | 1100 | zfs_znode_dmu_fini(zp); |
3558fd73 | 1101 | ZFS_OBJ_HOLD_EXIT(zsb, obj); |
34dc7c2f BB |
1102 | } |
1103 | ||
1104 | void | |
1105 | zfs_zinactive(znode_t *zp) | |
1106 | { | |
3558fd73 | 1107 | zfs_sb_t *zsb = ZTOZSB(zp); |
34dc7c2f BB |
1108 | uint64_t z_id = zp->z_id; |
1109 | ||
428870ff | 1110 | ASSERT(zp->z_sa_hdl); |
34dc7c2f BB |
1111 | |
1112 | /* | |
d6bd8eaa | 1113 | * Don't allow a zfs_zget() while were trying to release this znode. |
34dc7c2f | 1114 | */ |
ea04106b | 1115 | ZFS_OBJ_HOLD_ENTER(zsb, z_id); |
d6bd8eaa | 1116 | |
34dc7c2f | 1117 | mutex_enter(&zp->z_lock); |
34dc7c2f BB |
1118 | |
1119 | /* | |
1120 | * If this was the last reference to a file with no links, | |
1121 | * remove the file from the file system. | |
1122 | */ | |
1123 | if (zp->z_unlinked) { | |
1124 | mutex_exit(&zp->z_lock); | |
d6bd8eaa | 1125 | |
ea04106b | 1126 | ZFS_OBJ_HOLD_EXIT(zsb, z_id); |
d6bd8eaa | 1127 | |
34dc7c2f BB |
1128 | zfs_rmnode(zp); |
1129 | return; | |
1130 | } | |
428870ff | 1131 | |
34dc7c2f BB |
1132 | mutex_exit(&zp->z_lock); |
1133 | zfs_znode_dmu_fini(zp); | |
d6bd8eaa | 1134 | |
ea04106b | 1135 | ZFS_OBJ_HOLD_EXIT(zsb, z_id); |
34dc7c2f BB |
1136 | } |
1137 | ||
a08ee875 LG |
1138 | static inline int |
1139 | zfs_compare_timespec(struct timespec *t1, struct timespec *t2) | |
1140 | { | |
1141 | if (t1->tv_sec < t2->tv_sec) | |
1142 | return (-1); | |
1143 | ||
1144 | if (t1->tv_sec > t2->tv_sec) | |
1145 | return (1); | |
1146 | ||
1147 | return (t1->tv_nsec - t2->tv_nsec); | |
1148 | } | |
1149 | ||
1150 | /* | |
1151 | * Determine whether the znode's atime must be updated. The logic mostly | |
1152 | * duplicates the Linux kernel's relatime_need_update() functionality. | |
1153 | * This function is only called if the underlying filesystem actually has | |
1154 | * atime updates enabled. | |
1155 | */ | |
1156 | static inline boolean_t | |
1157 | zfs_atime_need_update(znode_t *zp, timestruc_t *now) | |
1158 | { | |
1159 | if (!ZTOZSB(zp)->z_relatime) | |
1160 | return (B_TRUE); | |
1161 | ||
1162 | /* | |
1163 | * In relatime mode, only update the atime if the previous atime | |
1164 | * is earlier than either the ctime or mtime or if at least a day | |
1165 | * has passed since the last update of atime. | |
1166 | */ | |
1167 | if (zfs_compare_timespec(&ZTOI(zp)->i_mtime, &ZTOI(zp)->i_atime) >= 0) | |
1168 | return (B_TRUE); | |
1169 | ||
1170 | if (zfs_compare_timespec(&ZTOI(zp)->i_ctime, &ZTOI(zp)->i_atime) >= 0) | |
1171 | return (B_TRUE); | |
1172 | ||
1173 | if ((long)now->tv_sec - ZTOI(zp)->i_atime.tv_sec >= 24*60*60) | |
1174 | return (B_TRUE); | |
1175 | ||
1176 | return (B_FALSE); | |
1177 | } | |
1178 | ||
1179 | /* | |
1180 | * Prepare to update znode time stamps. | |
1181 | * | |
1182 | * IN: zp - znode requiring timestamp update | |
1183 | * flag - ATTR_MTIME, ATTR_CTIME, ATTR_ATIME flags | |
1184 | * have_tx - true of caller is creating a new txg | |
1185 | * | |
1186 | * OUT: zp - new atime (via underlying inode's i_atime) | |
1187 | * mtime - new mtime | |
1188 | * ctime - new ctime | |
1189 | * | |
1190 | * NOTE: The arguments are somewhat redundant. The following condition | |
1191 | * is always true: | |
1192 | * | |
1193 | * have_tx == !(flag & ATTR_ATIME) | |
1194 | */ | |
34dc7c2f | 1195 | void |
428870ff BB |
1196 | zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2], |
1197 | uint64_t ctime[2], boolean_t have_tx) | |
34dc7c2f BB |
1198 | { |
1199 | timestruc_t now; | |
1200 | ||
a08ee875 | 1201 | ASSERT(have_tx == !(flag & ATTR_ATIME)); |
34dc7c2f BB |
1202 | gethrestime(&now); |
1203 | ||
a08ee875 LG |
1204 | /* |
1205 | * NOTE: The following test intentionally does not update z_atime_dirty | |
1206 | * in the case where an ATIME update has been requested but for which | |
1207 | * the update is omitted due to relatime logic. The rationale being | |
1208 | * that if the flag was set somewhere else, we should leave it alone | |
1209 | * here. | |
1210 | */ | |
1211 | if (flag & ATTR_ATIME) { | |
1212 | if (zfs_atime_need_update(zp, &now)) { | |
1213 | ZFS_TIME_ENCODE(&now, zp->z_atime); | |
1214 | ZTOI(zp)->i_atime.tv_sec = zp->z_atime[0]; | |
1215 | ZTOI(zp)->i_atime.tv_nsec = zp->z_atime[1]; | |
1216 | zp->z_atime_dirty = 1; | |
1217 | } | |
1218 | } else { | |
34dc7c2f BB |
1219 | zp->z_atime_dirty = 0; |
1220 | zp->z_seq++; | |
428870ff | 1221 | } |
34dc7c2f | 1222 | |
3558fd73 | 1223 | if (flag & ATTR_MTIME) { |
428870ff | 1224 | ZFS_TIME_ENCODE(&now, mtime); |
3558fd73 | 1225 | if (ZTOZSB(zp)->z_use_fuids) { |
428870ff BB |
1226 | zp->z_pflags |= (ZFS_ARCHIVE | |
1227 | ZFS_AV_MODIFIED); | |
1228 | } | |
34dc7c2f BB |
1229 | } |
1230 | ||
3558fd73 | 1231 | if (flag & ATTR_CTIME) { |
428870ff | 1232 | ZFS_TIME_ENCODE(&now, ctime); |
3558fd73 | 1233 | if (ZTOZSB(zp)->z_use_fuids) |
428870ff | 1234 | zp->z_pflags |= ZFS_ARCHIVE; |
34dc7c2f BB |
1235 | } |
1236 | } | |
1237 | ||
34dc7c2f BB |
1238 | /* |
1239 | * Grow the block size for a file. | |
1240 | * | |
1241 | * IN: zp - znode of file to free data in. | |
1242 | * size - requested block size | |
1243 | * tx - open transaction. | |
1244 | * | |
1245 | * NOTE: this function assumes that the znode is write locked. | |
1246 | */ | |
1247 | void | |
1248 | zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx) | |
1249 | { | |
1250 | int error; | |
1251 | u_longlong_t dummy; | |
1252 | ||
1253 | if (size <= zp->z_blksz) | |
1254 | return; | |
1255 | /* | |
1256 | * If the file size is already greater than the current blocksize, | |
1257 | * we will not grow. If there is more than one block in a file, | |
1258 | * the blocksize cannot change. | |
1259 | */ | |
428870ff | 1260 | if (zp->z_blksz && zp->z_size > zp->z_blksz) |
34dc7c2f BB |
1261 | return; |
1262 | ||
3558fd73 | 1263 | error = dmu_object_set_blocksize(ZTOZSB(zp)->z_os, zp->z_id, |
34dc7c2f | 1264 | size, 0, tx); |
428870ff | 1265 | |
34dc7c2f BB |
1266 | if (error == ENOTSUP) |
1267 | return; | |
c06d4368 | 1268 | ASSERT0(error); |
34dc7c2f BB |
1269 | |
1270 | /* What blocksize did we actually get? */ | |
428870ff | 1271 | dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy); |
34dc7c2f BB |
1272 | } |
1273 | ||
34dc7c2f | 1274 | /* |
b128c09f | 1275 | * Increase the file length |
34dc7c2f BB |
1276 | * |
1277 | * IN: zp - znode of file to free data in. | |
b128c09f | 1278 | * end - new end-of-file |
34dc7c2f | 1279 | * |
a08ee875 | 1280 | * RETURN: 0 on success, error code on failure |
34dc7c2f | 1281 | */ |
b128c09f BB |
1282 | static int |
1283 | zfs_extend(znode_t *zp, uint64_t end) | |
34dc7c2f | 1284 | { |
3558fd73 | 1285 | zfs_sb_t *zsb = ZTOZSB(zp); |
b128c09f | 1286 | dmu_tx_t *tx; |
34dc7c2f | 1287 | rl_t *rl; |
b128c09f | 1288 | uint64_t newblksz; |
34dc7c2f BB |
1289 | int error; |
1290 | ||
34dc7c2f | 1291 | /* |
b128c09f | 1292 | * We will change zp_size, lock the whole file. |
34dc7c2f | 1293 | */ |
b128c09f | 1294 | rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER); |
34dc7c2f BB |
1295 | |
1296 | /* | |
1297 | * Nothing to do if file already at desired length. | |
1298 | */ | |
428870ff | 1299 | if (end <= zp->z_size) { |
34dc7c2f BB |
1300 | zfs_range_unlock(rl); |
1301 | return (0); | |
1302 | } | |
3558fd73 | 1303 | tx = dmu_tx_create(zsb->z_os); |
428870ff BB |
1304 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); |
1305 | zfs_sa_upgrade_txholds(tx, zp); | |
b128c09f | 1306 | if (end > zp->z_blksz && |
3558fd73 | 1307 | (!ISP2(zp->z_blksz) || zp->z_blksz < zsb->z_max_blksz)) { |
34dc7c2f BB |
1308 | /* |
1309 | * We are growing the file past the current block size. | |
1310 | */ | |
3558fd73 | 1311 | if (zp->z_blksz > ZTOZSB(zp)->z_max_blksz) { |
e10b0808 AX |
1312 | /* |
1313 | * File's blocksize is already larger than the | |
1314 | * "recordsize" property. Only let it grow to | |
1315 | * the next power of 2. | |
1316 | */ | |
34dc7c2f | 1317 | ASSERT(!ISP2(zp->z_blksz)); |
e10b0808 | 1318 | newblksz = MIN(end, 1 << highbit64(zp->z_blksz)); |
34dc7c2f | 1319 | } else { |
3558fd73 | 1320 | newblksz = MIN(end, ZTOZSB(zp)->z_max_blksz); |
34dc7c2f | 1321 | } |
b128c09f BB |
1322 | dmu_tx_hold_write(tx, zp->z_id, 0, newblksz); |
1323 | } else { | |
1324 | newblksz = 0; | |
34dc7c2f BB |
1325 | } |
1326 | ||
a08ee875 | 1327 | error = dmu_tx_assign(tx, TXG_WAIT); |
34dc7c2f | 1328 | if (error) { |
34dc7c2f BB |
1329 | dmu_tx_abort(tx); |
1330 | zfs_range_unlock(rl); | |
1331 | return (error); | |
1332 | } | |
1333 | ||
b128c09f BB |
1334 | if (newblksz) |
1335 | zfs_grow_blocksize(zp, newblksz, tx); | |
34dc7c2f | 1336 | |
428870ff BB |
1337 | zp->z_size = end; |
1338 | ||
3558fd73 | 1339 | VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(ZTOZSB(zp)), |
428870ff | 1340 | &zp->z_size, sizeof (zp->z_size), tx)); |
34dc7c2f | 1341 | |
b128c09f | 1342 | zfs_range_unlock(rl); |
34dc7c2f | 1343 | |
b128c09f | 1344 | dmu_tx_commit(tx); |
34dc7c2f | 1345 | |
b128c09f BB |
1346 | return (0); |
1347 | } | |
1348 | ||
ea04106b AX |
1349 | /* |
1350 | * zfs_zero_partial_page - Modeled after update_pages() but | |
1351 | * with different arguments and semantics for use by zfs_freesp(). | |
1352 | * | |
1353 | * Zeroes a piece of a single page cache entry for zp at offset | |
1354 | * start and length len. | |
1355 | * | |
1356 | * Caller must acquire a range lock on the file for the region | |
1357 | * being zeroed in order that the ARC and page cache stay in sync. | |
1358 | */ | |
1359 | static void | |
1360 | zfs_zero_partial_page(znode_t *zp, uint64_t start, uint64_t len) | |
1361 | { | |
1362 | struct address_space *mp = ZTOI(zp)->i_mapping; | |
1363 | struct page *pp; | |
1364 | int64_t off; | |
1365 | void *pb; | |
1366 | ||
1367 | ASSERT((start & PAGE_CACHE_MASK) == | |
1368 | ((start + len - 1) & PAGE_CACHE_MASK)); | |
1369 | ||
1370 | off = start & (PAGE_CACHE_SIZE - 1); | |
1371 | start &= PAGE_CACHE_MASK; | |
1372 | ||
1373 | pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT); | |
1374 | if (pp) { | |
1375 | if (mapping_writably_mapped(mp)) | |
1376 | flush_dcache_page(pp); | |
1377 | ||
1378 | pb = kmap(pp); | |
1379 | bzero(pb + off, len); | |
1380 | kunmap(pp); | |
1381 | ||
1382 | if (mapping_writably_mapped(mp)) | |
1383 | flush_dcache_page(pp); | |
1384 | ||
1385 | mark_page_accessed(pp); | |
1386 | SetPageUptodate(pp); | |
1387 | ClearPageError(pp); | |
1388 | unlock_page(pp); | |
1389 | page_cache_release(pp); | |
1390 | } | |
1391 | } | |
1392 | ||
b128c09f BB |
1393 | /* |
1394 | * Free space in a file. | |
1395 | * | |
1396 | * IN: zp - znode of file to free data in. | |
1397 | * off - start of section to free. | |
1398 | * len - length of section to free. | |
1399 | * | |
a08ee875 | 1400 | * RETURN: 0 on success, error code on failure |
b128c09f BB |
1401 | */ |
1402 | static int | |
1403 | zfs_free_range(znode_t *zp, uint64_t off, uint64_t len) | |
1404 | { | |
3558fd73 | 1405 | zfs_sb_t *zsb = ZTOZSB(zp); |
b128c09f BB |
1406 | rl_t *rl; |
1407 | int error; | |
1408 | ||
1409 | /* | |
1410 | * Lock the range being freed. | |
1411 | */ | |
1412 | rl = zfs_range_lock(zp, off, len, RL_WRITER); | |
1413 | ||
1414 | /* | |
1415 | * Nothing to do if file already at desired length. | |
1416 | */ | |
428870ff | 1417 | if (off >= zp->z_size) { |
b128c09f BB |
1418 | zfs_range_unlock(rl); |
1419 | return (0); | |
34dc7c2f BB |
1420 | } |
1421 | ||
428870ff BB |
1422 | if (off + len > zp->z_size) |
1423 | len = zp->z_size - off; | |
b128c09f | 1424 | |
3558fd73 | 1425 | error = dmu_free_long_range(zsb->z_os, zp->z_id, off, len); |
b128c09f | 1426 | |
ea04106b AX |
1427 | /* |
1428 | * Zero partial page cache entries. This must be done under a | |
1429 | * range lock in order to keep the ARC and page cache in sync. | |
1430 | */ | |
1431 | if (zp->z_is_mapped) { | |
1432 | loff_t first_page, last_page, page_len; | |
1433 | loff_t first_page_offset, last_page_offset; | |
1434 | ||
1435 | /* first possible full page in hole */ | |
1436 | first_page = (off + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | |
1437 | /* last page of hole */ | |
1438 | last_page = (off + len) >> PAGE_CACHE_SHIFT; | |
1439 | ||
1440 | /* offset of first_page */ | |
1441 | first_page_offset = first_page << PAGE_CACHE_SHIFT; | |
1442 | /* offset of last_page */ | |
1443 | last_page_offset = last_page << PAGE_CACHE_SHIFT; | |
1444 | ||
1445 | /* truncate whole pages */ | |
1446 | if (last_page_offset > first_page_offset) { | |
1447 | truncate_inode_pages_range(ZTOI(zp)->i_mapping, | |
1448 | first_page_offset, last_page_offset - 1); | |
1449 | } | |
1450 | ||
1451 | /* truncate sub-page ranges */ | |
1452 | if (first_page > last_page) { | |
1453 | /* entire punched area within a single page */ | |
1454 | zfs_zero_partial_page(zp, off, len); | |
1455 | } else { | |
1456 | /* beginning of punched area at the end of a page */ | |
1457 | page_len = first_page_offset - off; | |
1458 | if (page_len > 0) | |
1459 | zfs_zero_partial_page(zp, off, page_len); | |
1460 | ||
1461 | /* end of punched area at the beginning of a page */ | |
1462 | page_len = off + len - last_page_offset; | |
1463 | if (page_len > 0) | |
1464 | zfs_zero_partial_page(zp, last_page_offset, | |
1465 | page_len); | |
1466 | } | |
1467 | } | |
34dc7c2f BB |
1468 | zfs_range_unlock(rl); |
1469 | ||
b128c09f BB |
1470 | return (error); |
1471 | } | |
1472 | ||
1473 | /* | |
1474 | * Truncate a file | |
1475 | * | |
1476 | * IN: zp - znode of file to free data in. | |
1477 | * end - new end-of-file. | |
1478 | * | |
a08ee875 | 1479 | * RETURN: 0 on success, error code on failure |
b128c09f BB |
1480 | */ |
1481 | static int | |
1482 | zfs_trunc(znode_t *zp, uint64_t end) | |
1483 | { | |
3558fd73 | 1484 | zfs_sb_t *zsb = ZTOZSB(zp); |
b128c09f BB |
1485 | dmu_tx_t *tx; |
1486 | rl_t *rl; | |
1487 | int error; | |
572e2857 BB |
1488 | sa_bulk_attr_t bulk[2]; |
1489 | int count = 0; | |
b128c09f BB |
1490 | |
1491 | /* | |
1492 | * We will change zp_size, lock the whole file. | |
1493 | */ | |
1494 | rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER); | |
1495 | ||
1496 | /* | |
1497 | * Nothing to do if file already at desired length. | |
1498 | */ | |
428870ff | 1499 | if (end >= zp->z_size) { |
b128c09f BB |
1500 | zfs_range_unlock(rl); |
1501 | return (0); | |
1502 | } | |
1503 | ||
3558fd73 | 1504 | error = dmu_free_long_range(zsb->z_os, zp->z_id, end, -1); |
b128c09f BB |
1505 | if (error) { |
1506 | zfs_range_unlock(rl); | |
1507 | return (error); | |
1508 | } | |
3558fd73 | 1509 | tx = dmu_tx_create(zsb->z_os); |
428870ff BB |
1510 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); |
1511 | zfs_sa_upgrade_txholds(tx, zp); | |
ea04106b | 1512 | error = dmu_tx_assign(tx, TXG_WAIT); |
b128c09f | 1513 | if (error) { |
b128c09f BB |
1514 | dmu_tx_abort(tx); |
1515 | zfs_range_unlock(rl); | |
1516 | return (error); | |
1517 | } | |
b128c09f | 1518 | |
428870ff | 1519 | zp->z_size = end; |
3558fd73 | 1520 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), |
572e2857 | 1521 | NULL, &zp->z_size, sizeof (zp->z_size)); |
428870ff | 1522 | |
572e2857 BB |
1523 | if (end == 0) { |
1524 | zp->z_pflags &= ~ZFS_SPARSE; | |
3558fd73 | 1525 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), |
572e2857 BB |
1526 | NULL, &zp->z_pflags, 8); |
1527 | } | |
1528 | VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0); | |
b128c09f | 1529 | |
34dc7c2f BB |
1530 | dmu_tx_commit(tx); |
1531 | ||
d164b209 | 1532 | zfs_range_unlock(rl); |
34dc7c2f BB |
1533 | |
1534 | return (0); | |
1535 | } | |
1536 | ||
b128c09f BB |
1537 | /* |
1538 | * Free space in a file | |
1539 | * | |
1540 | * IN: zp - znode of file to free data in. | |
1541 | * off - start of range | |
1542 | * len - end of range (0 => EOF) | |
1543 | * flag - current file open mode flags. | |
1544 | * log - TRUE if this action should be logged | |
1545 | * | |
a08ee875 | 1546 | * RETURN: 0 on success, error code on failure |
b128c09f BB |
1547 | */ |
1548 | int | |
1549 | zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log) | |
1550 | { | |
b128c09f | 1551 | dmu_tx_t *tx; |
3558fd73 BB |
1552 | zfs_sb_t *zsb = ZTOZSB(zp); |
1553 | zilog_t *zilog = zsb->z_log; | |
428870ff BB |
1554 | uint64_t mode; |
1555 | uint64_t mtime[2], ctime[2]; | |
1556 | sa_bulk_attr_t bulk[3]; | |
1557 | int count = 0; | |
b128c09f BB |
1558 | int error; |
1559 | ||
3558fd73 | 1560 | if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zsb), &mode, |
428870ff BB |
1561 | sizeof (mode))) != 0) |
1562 | return (error); | |
1563 | ||
1564 | if (off > zp->z_size) { | |
b128c09f BB |
1565 | error = zfs_extend(zp, off+len); |
1566 | if (error == 0 && log) | |
1567 | goto log; | |
ea04106b | 1568 | goto out; |
b128c09f BB |
1569 | } |
1570 | ||
1571 | if (len == 0) { | |
1572 | error = zfs_trunc(zp, off); | |
1573 | } else { | |
1574 | if ((error = zfs_free_range(zp, off, len)) == 0 && | |
428870ff | 1575 | off + len > zp->z_size) |
b128c09f BB |
1576 | error = zfs_extend(zp, off+len); |
1577 | } | |
1578 | if (error || !log) | |
ea04106b | 1579 | goto out; |
b128c09f | 1580 | log: |
3558fd73 | 1581 | tx = dmu_tx_create(zsb->z_os); |
428870ff BB |
1582 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); |
1583 | zfs_sa_upgrade_txholds(tx, zp); | |
a08ee875 | 1584 | error = dmu_tx_assign(tx, TXG_WAIT); |
b128c09f | 1585 | if (error) { |
b128c09f | 1586 | dmu_tx_abort(tx); |
ea04106b | 1587 | goto out; |
b128c09f BB |
1588 | } |
1589 | ||
3558fd73 BB |
1590 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, mtime, 16); |
1591 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, ctime, 16); | |
1592 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), | |
428870ff BB |
1593 | NULL, &zp->z_pflags, 8); |
1594 | zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE); | |
1595 | error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); | |
1596 | ASSERT(error == 0); | |
1597 | ||
b128c09f BB |
1598 | zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len); |
1599 | ||
1600 | dmu_tx_commit(tx); | |
ea04106b | 1601 | |
960e08fe | 1602 | zfs_inode_update(zp); |
ea04106b AX |
1603 | error = 0; |
1604 | ||
1605 | out: | |
1606 | /* | |
1607 | * Truncate the page cache - for file truncate operations, use | |
1608 | * the purpose-built API for truncations. For punching operations, | |
1609 | * the truncation is handled under a range lock in zfs_free_range. | |
1610 | */ | |
1611 | if (len == 0) | |
1612 | truncate_setsize(ZTOI(zp), off); | |
1613 | return (error); | |
b128c09f BB |
1614 | } |
1615 | ||
34dc7c2f BB |
1616 | void |
1617 | zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx) | |
1618 | { | |
22872ff5 BB |
1619 | struct super_block *sb; |
1620 | zfs_sb_t *zsb; | |
428870ff | 1621 | uint64_t moid, obj, sa_obj, version; |
22872ff5 | 1622 | uint64_t sense = ZFS_CASE_SENSITIVE; |
34dc7c2f BB |
1623 | uint64_t norm = 0; |
1624 | nvpair_t *elem; | |
1625 | int error; | |
22872ff5 BB |
1626 | int i; |
1627 | znode_t *rootzp = NULL; | |
1628 | vattr_t vattr; | |
1629 | znode_t *zp; | |
1630 | zfs_acl_ids_t acl_ids; | |
34dc7c2f BB |
1631 | |
1632 | /* | |
1633 | * First attempt to create master node. | |
1634 | */ | |
1635 | /* | |
1636 | * In an empty objset, there are no blocks to read and thus | |
1637 | * there can be no i/o errors (which we assert below). | |
1638 | */ | |
1639 | moid = MASTER_NODE_OBJ; | |
1640 | error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE, | |
1641 | DMU_OT_NONE, 0, tx); | |
1642 | ASSERT(error == 0); | |
1643 | ||
1644 | /* | |
1645 | * Set starting attributes. | |
1646 | */ | |
428870ff | 1647 | version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os))); |
34dc7c2f BB |
1648 | elem = NULL; |
1649 | while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) { | |
1650 | /* For the moment we expect all zpl props to be uint64_ts */ | |
1651 | uint64_t val; | |
1652 | char *name; | |
1653 | ||
1654 | ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64); | |
1655 | VERIFY(nvpair_value_uint64(elem, &val) == 0); | |
1656 | name = nvpair_name(elem); | |
1657 | if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) { | |
9babb374 BB |
1658 | if (val < version) |
1659 | version = val; | |
34dc7c2f BB |
1660 | } else { |
1661 | error = zap_update(os, moid, name, 8, 1, &val, tx); | |
1662 | } | |
1663 | ASSERT(error == 0); | |
1664 | if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0) | |
1665 | norm = val; | |
22872ff5 BB |
1666 | else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0) |
1667 | sense = val; | |
34dc7c2f BB |
1668 | } |
1669 | ASSERT(version != 0); | |
9babb374 | 1670 | error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx); |
34dc7c2f | 1671 | |
428870ff BB |
1672 | /* |
1673 | * Create zap object used for SA attribute registration | |
1674 | */ | |
1675 | ||
1676 | if (version >= ZPL_VERSION_SA) { | |
1677 | sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE, | |
1678 | DMU_OT_NONE, 0, tx); | |
1679 | error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx); | |
1680 | ASSERT(error == 0); | |
1681 | } else { | |
1682 | sa_obj = 0; | |
1683 | } | |
34dc7c2f BB |
1684 | /* |
1685 | * Create a delete queue. | |
1686 | */ | |
9babb374 | 1687 | obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx); |
34dc7c2f | 1688 | |
9babb374 | 1689 | error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx); |
34dc7c2f BB |
1690 | ASSERT(error == 0); |
1691 | ||
9babb374 | 1692 | /* |
22872ff5 BB |
1693 | * Create root znode. Create minimal znode/inode/zsb/sb |
1694 | * to allow zfs_mknode to work. | |
9babb374 | 1695 | */ |
22872ff5 BB |
1696 | vattr.va_mask = ATTR_MODE|ATTR_UID|ATTR_GID; |
1697 | vattr.va_mode = S_IFDIR|0755; | |
1698 | vattr.va_uid = crgetuid(cr); | |
1699 | vattr.va_gid = crgetgid(cr); | |
1700 | ||
ea04106b | 1701 | rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP); |
22872ff5 BB |
1702 | rootzp->z_moved = 0; |
1703 | rootzp->z_unlinked = 0; | |
1704 | rootzp->z_atime_dirty = 0; | |
1705 | rootzp->z_is_sa = USE_SA(version, os); | |
1706 | ||
ea04106b | 1707 | zsb = kmem_zalloc(sizeof (zfs_sb_t), KM_SLEEP); |
22872ff5 BB |
1708 | zsb->z_os = os; |
1709 | zsb->z_parent = zsb; | |
1710 | zsb->z_version = version; | |
1711 | zsb->z_use_fuids = USE_FUIDS(version, os); | |
1712 | zsb->z_use_sa = USE_SA(version, os); | |
1713 | zsb->z_norm = norm; | |
1714 | ||
ea04106b | 1715 | sb = kmem_zalloc(sizeof (struct super_block), KM_SLEEP); |
22872ff5 BB |
1716 | sb->s_fs_info = zsb; |
1717 | ||
1718 | ZTOI(rootzp)->i_sb = sb; | |
1719 | ||
1720 | error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END, | |
1721 | &zsb->z_attr_table); | |
9babb374 | 1722 | |
22872ff5 | 1723 | ASSERT(error == 0); |
9babb374 | 1724 | |
60101509 | 1725 | /* |
22872ff5 BB |
1726 | * Fold case on file systems that are always or sometimes case |
1727 | * insensitive. | |
60101509 | 1728 | */ |
22872ff5 BB |
1729 | if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED) |
1730 | zsb->z_norm |= U8_TEXTPREP_TOUPPER; | |
60101509 | 1731 | |
22872ff5 BB |
1732 | mutex_init(&zsb->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL); |
1733 | list_create(&zsb->z_all_znodes, sizeof (znode_t), | |
1734 | offsetof(znode_t, z_link_node)); | |
60101509 | 1735 | |
ea04106b AX |
1736 | zsb->z_hold_mtx = vmem_zalloc(sizeof (kmutex_t) * ZFS_OBJ_MTX_SZ, |
1737 | KM_SLEEP); | |
22872ff5 BB |
1738 | for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) |
1739 | mutex_init(&zsb->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL); | |
60101509 | 1740 | |
22872ff5 BB |
1741 | VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr, |
1742 | cr, NULL, &acl_ids)); | |
1743 | zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids); | |
1744 | ASSERT3P(zp, ==, rootzp); | |
1745 | error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx); | |
1746 | ASSERT(error == 0); | |
1747 | zfs_acl_ids_free(&acl_ids); | |
60101509 | 1748 | |
22872ff5 BB |
1749 | atomic_set(&ZTOI(rootzp)->i_count, 0); |
1750 | sa_handle_destroy(rootzp->z_sa_hdl); | |
22872ff5 BB |
1751 | kmem_cache_free(znode_cache, rootzp); |
1752 | ||
1753 | /* | |
1754 | * Create shares directory | |
1755 | */ | |
22872ff5 | 1756 | error = zfs_create_share_dir(zsb, tx); |
9babb374 | 1757 | ASSERT(error == 0); |
428870ff | 1758 | |
22872ff5 BB |
1759 | for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) |
1760 | mutex_destroy(&zsb->z_hold_mtx[i]); | |
2708f716 | 1761 | |
ea04106b | 1762 | vmem_free(zsb->z_hold_mtx, sizeof (kmutex_t) * ZFS_OBJ_MTX_SZ); |
2708f716 BB |
1763 | kmem_free(sb, sizeof (struct super_block)); |
1764 | kmem_free(zsb, sizeof (zfs_sb_t)); | |
34dc7c2f | 1765 | } |
34dc7c2f | 1766 | #endif /* _KERNEL */ |
428870ff | 1767 | |
34dc7c2f | 1768 | static int |
572e2857 BB |
1769 | zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table) |
1770 | { | |
1771 | uint64_t sa_obj = 0; | |
1772 | int error; | |
1773 | ||
1774 | error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj); | |
1775 | if (error != 0 && error != ENOENT) | |
1776 | return (error); | |
1777 | ||
1778 | error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table); | |
1779 | return (error); | |
1780 | } | |
1781 | ||
1782 | static int | |
1783 | zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp, | |
7b8518cb | 1784 | dmu_buf_t **db, void *tag) |
34dc7c2f | 1785 | { |
34dc7c2f | 1786 | dmu_object_info_t doi; |
34dc7c2f | 1787 | int error; |
428870ff | 1788 | |
7b8518cb | 1789 | if ((error = sa_buf_hold(osp, obj, tag, db)) != 0) |
34dc7c2f BB |
1790 | return (error); |
1791 | ||
572e2857 | 1792 | dmu_object_info_from_db(*db, &doi); |
428870ff BB |
1793 | if ((doi.doi_bonus_type != DMU_OT_SA && |
1794 | doi.doi_bonus_type != DMU_OT_ZNODE) || | |
d6320ddb BB |
1795 | (doi.doi_bonus_type == DMU_OT_ZNODE && |
1796 | doi.doi_bonus_size < sizeof (znode_phys_t))) { | |
7b8518cb | 1797 | sa_buf_rele(*db, tag); |
a08ee875 | 1798 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f BB |
1799 | } |
1800 | ||
572e2857 BB |
1801 | error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp); |
1802 | if (error != 0) { | |
7b8518cb | 1803 | sa_buf_rele(*db, tag); |
428870ff BB |
1804 | return (error); |
1805 | } | |
1806 | ||
572e2857 BB |
1807 | return (0); |
1808 | } | |
1809 | ||
1810 | void | |
7b8518cb | 1811 | zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag) |
572e2857 BB |
1812 | { |
1813 | sa_handle_destroy(hdl); | |
7b8518cb | 1814 | sa_buf_rele(db, tag); |
572e2857 BB |
1815 | } |
1816 | ||
1817 | /* | |
1818 | * Given an object number, return its parent object number and whether | |
1819 | * or not the object is an extended attribute directory. | |
1820 | */ | |
1821 | static int | |
1822 | zfs_obj_to_pobj(sa_handle_t *hdl, sa_attr_type_t *sa_table, uint64_t *pobjp, | |
1823 | int *is_xattrdir) | |
1824 | { | |
1825 | uint64_t parent; | |
1826 | uint64_t pflags; | |
1827 | uint64_t mode; | |
1828 | sa_bulk_attr_t bulk[3]; | |
1829 | int count = 0; | |
1830 | int error; | |
1831 | ||
1832 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL, | |
1833 | &parent, sizeof (parent)); | |
428870ff | 1834 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL, |
572e2857 | 1835 | &pflags, sizeof (pflags)); |
428870ff | 1836 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL, |
572e2857 | 1837 | &mode, sizeof (mode)); |
428870ff | 1838 | |
572e2857 | 1839 | if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0) |
428870ff | 1840 | return (error); |
572e2857 | 1841 | |
428870ff BB |
1842 | *pobjp = parent; |
1843 | *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode); | |
34dc7c2f BB |
1844 | |
1845 | return (0); | |
1846 | } | |
1847 | ||
572e2857 BB |
1848 | /* |
1849 | * Given an object number, return some zpl level statistics | |
1850 | */ | |
1851 | static int | |
1852 | zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table, | |
1853 | zfs_stat_t *sb) | |
34dc7c2f | 1854 | { |
572e2857 BB |
1855 | sa_bulk_attr_t bulk[4]; |
1856 | int count = 0; | |
1857 | ||
1858 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL, | |
1859 | &sb->zs_mode, sizeof (sb->zs_mode)); | |
1860 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL, | |
1861 | &sb->zs_gen, sizeof (sb->zs_gen)); | |
1862 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL, | |
1863 | &sb->zs_links, sizeof (sb->zs_links)); | |
1864 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL, | |
1865 | &sb->zs_ctime, sizeof (sb->zs_ctime)); | |
1866 | ||
1867 | return (sa_bulk_lookup(hdl, bulk, count)); | |
1868 | } | |
1869 | ||
1870 | static int | |
1871 | zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl, | |
1872 | sa_attr_type_t *sa_table, char *buf, int len) | |
1873 | { | |
1874 | sa_handle_t *sa_hdl; | |
1875 | sa_handle_t *prevhdl = NULL; | |
1876 | dmu_buf_t *prevdb = NULL; | |
1877 | dmu_buf_t *sa_db = NULL; | |
34dc7c2f BB |
1878 | char *path = buf + len - 1; |
1879 | int error; | |
1880 | ||
1881 | *path = '\0'; | |
572e2857 | 1882 | sa_hdl = hdl; |
428870ff | 1883 | |
34dc7c2f | 1884 | for (;;) { |
a08ee875 | 1885 | uint64_t pobj = 0; |
34dc7c2f BB |
1886 | char component[MAXNAMELEN + 2]; |
1887 | size_t complen; | |
a08ee875 | 1888 | int is_xattrdir = 0; |
34dc7c2f | 1889 | |
572e2857 | 1890 | if (prevdb) |
7b8518cb | 1891 | zfs_release_sa_handle(prevhdl, prevdb, FTAG); |
572e2857 BB |
1892 | |
1893 | if ((error = zfs_obj_to_pobj(sa_hdl, sa_table, &pobj, | |
1894 | &is_xattrdir)) != 0) | |
34dc7c2f BB |
1895 | break; |
1896 | ||
1897 | if (pobj == obj) { | |
1898 | if (path[0] != '/') | |
1899 | *--path = '/'; | |
1900 | break; | |
1901 | } | |
1902 | ||
1903 | component[0] = '/'; | |
1904 | if (is_xattrdir) { | |
1905 | (void) sprintf(component + 1, "<xattrdir>"); | |
1906 | } else { | |
1907 | error = zap_value_search(osp, pobj, obj, | |
1908 | ZFS_DIRENT_OBJ(-1ULL), component + 1); | |
1909 | if (error != 0) | |
1910 | break; | |
1911 | } | |
1912 | ||
1913 | complen = strlen(component); | |
1914 | path -= complen; | |
1915 | ASSERT(path >= buf); | |
1916 | bcopy(component, path, complen); | |
1917 | obj = pobj; | |
572e2857 BB |
1918 | |
1919 | if (sa_hdl != hdl) { | |
1920 | prevhdl = sa_hdl; | |
1921 | prevdb = sa_db; | |
1922 | } | |
7b8518cb | 1923 | error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG); |
572e2857 BB |
1924 | if (error != 0) { |
1925 | sa_hdl = prevhdl; | |
1926 | sa_db = prevdb; | |
1927 | break; | |
1928 | } | |
1929 | } | |
1930 | ||
1931 | if (sa_hdl != NULL && sa_hdl != hdl) { | |
1932 | ASSERT(sa_db != NULL); | |
7b8518cb | 1933 | zfs_release_sa_handle(sa_hdl, sa_db, FTAG); |
34dc7c2f BB |
1934 | } |
1935 | ||
1936 | if (error == 0) | |
1937 | (void) memmove(buf, path, buf + len - path); | |
428870ff | 1938 | |
34dc7c2f BB |
1939 | return (error); |
1940 | } | |
572e2857 BB |
1941 | |
1942 | int | |
1943 | zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len) | |
1944 | { | |
1945 | sa_attr_type_t *sa_table; | |
1946 | sa_handle_t *hdl; | |
1947 | dmu_buf_t *db; | |
1948 | int error; | |
1949 | ||
1950 | error = zfs_sa_setup(osp, &sa_table); | |
1951 | if (error != 0) | |
1952 | return (error); | |
1953 | ||
7b8518cb | 1954 | error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG); |
572e2857 BB |
1955 | if (error != 0) |
1956 | return (error); | |
1957 | ||
1958 | error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len); | |
1959 | ||
7b8518cb | 1960 | zfs_release_sa_handle(hdl, db, FTAG); |
572e2857 BB |
1961 | return (error); |
1962 | } | |
1963 | ||
1964 | int | |
1965 | zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb, | |
1966 | char *buf, int len) | |
1967 | { | |
1968 | char *path = buf + len - 1; | |
1969 | sa_attr_type_t *sa_table; | |
1970 | sa_handle_t *hdl; | |
1971 | dmu_buf_t *db; | |
1972 | int error; | |
1973 | ||
1974 | *path = '\0'; | |
1975 | ||
1976 | error = zfs_sa_setup(osp, &sa_table); | |
1977 | if (error != 0) | |
1978 | return (error); | |
1979 | ||
7b8518cb | 1980 | error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG); |
572e2857 BB |
1981 | if (error != 0) |
1982 | return (error); | |
1983 | ||
1984 | error = zfs_obj_to_stats_impl(hdl, sa_table, sb); | |
1985 | if (error != 0) { | |
7b8518cb | 1986 | zfs_release_sa_handle(hdl, db, FTAG); |
572e2857 BB |
1987 | return (error); |
1988 | } | |
1989 | ||
1990 | error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len); | |
1991 | ||
7b8518cb | 1992 | zfs_release_sa_handle(hdl, db, FTAG); |
572e2857 BB |
1993 | return (error); |
1994 | } | |
c28b2279 BB |
1995 | |
1996 | #if defined(_KERNEL) && defined(HAVE_SPL) | |
1997 | EXPORT_SYMBOL(zfs_create_fs); | |
1998 | EXPORT_SYMBOL(zfs_obj_to_path); | |
1999 | #endif |