]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | /* | |
428870ff | 22 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
2e528b49 | 23 | * Copyright (c) 2013 by Delphix. All rights reserved. |
34dc7c2f BB |
24 | */ |
25 | ||
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> | |
f1512ee6 | 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 | /* | |
5074bfe8 TC |
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, | |
5074bfe8 | 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 | |
79c76d5b | 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 | ||
79c76d5b | 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) { |
0a50679c | 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: | |
53b1d979 BB |
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: | |
53b1d979 BB |
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; |
7f89ae6b | 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 | ||
7f89ae6b | 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); | |
07d63f0c | 413 | zp->z_sa_hdl = NULL; |
3558fd73 | 414 | goto error; |
34dc7c2f | 415 | } |
7304b6e5 | 416 | |
7f89ae6b BB |
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: | |
3558fd73 | 451 | iput(ip); |
d1d7e268 | 452 | return (NULL); |
34dc7c2f BB |
453 | } |
454 | ||
9d317793 RY |
455 | void |
456 | zfs_set_inode_flags(znode_t *zp, struct inode *ip) | |
457 | { | |
458 | /* | |
459 | * Linux and Solaris have different sets of file attributes, so we | |
460 | * restrict this conversion to the intersection of the two. | |
461 | */ | |
462 | ||
463 | if (zp->z_pflags & ZFS_IMMUTABLE) | |
464 | ip->i_flags |= S_IMMUTABLE; | |
465 | else | |
466 | ip->i_flags &= ~S_IMMUTABLE; | |
467 | ||
468 | if (zp->z_pflags & ZFS_APPENDONLY) | |
469 | ip->i_flags |= S_APPEND; | |
470 | else | |
471 | ip->i_flags &= ~S_APPEND; | |
472 | } | |
473 | ||
960e08fe BB |
474 | /* |
475 | * Update the embedded inode given the znode. We should work toward | |
476 | * eliminating this function as soon as possible by removing values | |
477 | * which are duplicated between the znode and inode. If the generic | |
478 | * inode has the correct field it should be used, and the ZFS code | |
479 | * updated to access the inode. This can be done incrementally. | |
480 | */ | |
481 | void | |
482 | zfs_inode_update(znode_t *zp) | |
483 | { | |
3558fd73 BB |
484 | zfs_sb_t *zsb; |
485 | struct inode *ip; | |
960e08fe | 486 | uint32_t blksize; |
919efe93 | 487 | u_longlong_t i_blocks; |
960e08fe BB |
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 | ||
919efe93 RY |
502 | dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &blksize, &i_blocks); |
503 | ||
3558fd73 BB |
504 | spin_lock(&ip->i_lock); |
505 | ip->i_generation = zp->z_gen; | |
570d6edf RY |
506 | ip->i_uid = SUID_TO_KUID(zp->z_uid); |
507 | ip->i_gid = SGID_TO_KGID(zp->z_gid); | |
28eb9213 | 508 | set_nlink(ip, zp->z_links); |
3558fd73 | 509 | ip->i_mode = zp->z_mode; |
9d317793 | 510 | zfs_set_inode_flags(zp, ip); |
3558fd73 | 511 | ip->i_blkbits = SPA_MINBLOCKSHIFT; |
919efe93 | 512 | ip->i_blocks = i_blocks; |
960e08fe | 513 | |
3558fd73 BB |
514 | ZFS_TIME_DECODE(&ip->i_atime, atime); |
515 | ZFS_TIME_DECODE(&ip->i_mtime, mtime); | |
516 | ZFS_TIME_DECODE(&ip->i_ctime, ctime); | |
960e08fe | 517 | |
3558fd73 BB |
518 | i_size_write(ip, zp->z_size); |
519 | spin_unlock(&ip->i_lock); | |
960e08fe BB |
520 | } |
521 | ||
1e8db771 BB |
522 | /* |
523 | * Safely mark an inode dirty. Inodes which are part of a read-only | |
524 | * file system or snapshot may not be dirtied. | |
525 | */ | |
526 | void | |
527 | zfs_mark_inode_dirty(struct inode *ip) | |
528 | { | |
529 | zfs_sb_t *zsb = ITOZSB(ip); | |
530 | ||
531 | if (zfs_is_readonly(zsb) || dmu_objset_is_snapshot(zsb->z_os)) | |
532 | return; | |
533 | ||
534 | mark_inode_dirty(ip); | |
535 | } | |
536 | ||
428870ff BB |
537 | static uint64_t empty_xattr; |
538 | static uint64_t pad[4]; | |
539 | static zfs_acl_phys_t acl_phys; | |
34dc7c2f BB |
540 | /* |
541 | * Create a new DMU object to hold a zfs znode. | |
542 | * | |
543 | * IN: dzp - parent directory for new znode | |
544 | * vap - file attributes for new znode | |
545 | * tx - dmu transaction id for zap operations | |
546 | * cr - credentials of caller | |
547 | * flag - flags: | |
548 | * IS_ROOT_NODE - new object will be root | |
549 | * IS_XATTR - new object is an attribute | |
34dc7c2f BB |
550 | * bonuslen - length of bonus buffer |
551 | * setaclp - File/Dir initial ACL | |
552 | * fuidp - Tracks fuid allocation. | |
553 | * | |
554 | * OUT: zpp - allocated znode | |
555 | * | |
556 | */ | |
557 | void | |
558 | zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr, | |
428870ff | 559 | uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids) |
34dc7c2f | 560 | { |
428870ff BB |
561 | uint64_t crtime[2], atime[2], mtime[2], ctime[2]; |
562 | uint64_t mode, size, links, parent, pflags; | |
572e2857 | 563 | uint64_t dzp_pflags = 0; |
428870ff | 564 | uint64_t rdev = 0; |
3558fd73 | 565 | zfs_sb_t *zsb = ZTOZSB(dzp); |
428870ff | 566 | dmu_buf_t *db; |
34dc7c2f BB |
567 | timestruc_t now; |
568 | uint64_t gen, obj; | |
428870ff BB |
569 | int bonuslen; |
570 | sa_handle_t *sa_hdl; | |
571 | dmu_object_type_t obj_type; | |
f30484af | 572 | sa_bulk_attr_t *sa_attrs; |
428870ff BB |
573 | int cnt = 0; |
574 | zfs_acl_locator_cb_t locate = { 0 }; | |
34dc7c2f | 575 | |
3558fd73 | 576 | if (zsb->z_replay) { |
34dc7c2f | 577 | obj = vap->va_nodeid; |
34dc7c2f BB |
578 | now = vap->va_ctime; /* see zfs_replay_create() */ |
579 | gen = vap->va_nblocks; /* ditto */ | |
580 | } else { | |
581 | obj = 0; | |
582 | gethrestime(&now); | |
583 | gen = dmu_tx_get_txg(tx); | |
584 | } | |
585 | ||
3558fd73 | 586 | obj_type = zsb->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE; |
428870ff BB |
587 | bonuslen = (obj_type == DMU_OT_SA) ? |
588 | DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE; | |
589 | ||
34dc7c2f BB |
590 | /* |
591 | * Create a new DMU object. | |
592 | */ | |
593 | /* | |
594 | * There's currently no mechanism for pre-reading the blocks that will | |
572e2857 | 595 | * be needed to allocate a new object, so we accept the small chance |
34dc7c2f BB |
596 | * that there will be an i/o error and we will fail one of the |
597 | * assertions below. | |
598 | */ | |
3558fd73 BB |
599 | if (S_ISDIR(vap->va_mode)) { |
600 | if (zsb->z_replay) { | |
b0bc7a84 | 601 | VERIFY0(zap_create_claim_norm(zsb->z_os, obj, |
3558fd73 | 602 | zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS, |
b0bc7a84 | 603 | obj_type, bonuslen, tx)); |
34dc7c2f | 604 | } else { |
3558fd73 BB |
605 | obj = zap_create_norm(zsb->z_os, |
606 | zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS, | |
428870ff | 607 | obj_type, bonuslen, tx); |
34dc7c2f BB |
608 | } |
609 | } else { | |
3558fd73 | 610 | if (zsb->z_replay) { |
b0bc7a84 | 611 | VERIFY0(dmu_object_claim(zsb->z_os, obj, |
34dc7c2f | 612 | DMU_OT_PLAIN_FILE_CONTENTS, 0, |
b0bc7a84 | 613 | obj_type, bonuslen, tx)); |
34dc7c2f | 614 | } else { |
3558fd73 | 615 | obj = dmu_object_alloc(zsb->z_os, |
34dc7c2f | 616 | DMU_OT_PLAIN_FILE_CONTENTS, 0, |
428870ff | 617 | obj_type, bonuslen, tx); |
34dc7c2f BB |
618 | } |
619 | } | |
34dc7c2f | 620 | |
3558fd73 BB |
621 | ZFS_OBJ_HOLD_ENTER(zsb, obj); |
622 | VERIFY(0 == sa_buf_hold(zsb->z_os, obj, NULL, &db)); | |
34dc7c2f BB |
623 | |
624 | /* | |
625 | * If this is the root, fix up the half-initialized parent pointer | |
626 | * to reference the just-allocated physical data area. | |
627 | */ | |
628 | if (flag & IS_ROOT_NODE) { | |
34dc7c2f | 629 | dzp->z_id = obj; |
428870ff BB |
630 | } else { |
631 | dzp_pflags = dzp->z_pflags; | |
34dc7c2f BB |
632 | } |
633 | ||
634 | /* | |
635 | * If parent is an xattr, so am I. | |
636 | */ | |
428870ff | 637 | if (dzp_pflags & ZFS_XATTR) { |
34dc7c2f | 638 | flag |= IS_XATTR; |
34dc7c2f BB |
639 | } |
640 | ||
3558fd73 | 641 | if (zsb->z_use_fuids) |
428870ff BB |
642 | pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED; |
643 | else | |
644 | pflags = 0; | |
34dc7c2f | 645 | |
3558fd73 | 646 | if (S_ISDIR(vap->va_mode)) { |
428870ff BB |
647 | size = 2; /* contents ("." and "..") */ |
648 | links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1; | |
649 | } else { | |
650 | size = links = 0; | |
34dc7c2f BB |
651 | } |
652 | ||
aa6d8c10 | 653 | if (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode)) |
dc1d7665 | 654 | rdev = vap->va_rdev; |
428870ff BB |
655 | |
656 | parent = dzp->z_id; | |
657 | mode = acl_ids->z_mode; | |
34dc7c2f | 658 | if (flag & IS_XATTR) |
428870ff | 659 | pflags |= ZFS_XATTR; |
34dc7c2f | 660 | |
428870ff BB |
661 | /* |
662 | * No execs denied will be deterimed when zfs_mode_compute() is called. | |
663 | */ | |
664 | pflags |= acl_ids->z_aclp->z_hints & | |
665 | (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT| | |
666 | ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED); | |
34dc7c2f | 667 | |
428870ff BB |
668 | ZFS_TIME_ENCODE(&now, crtime); |
669 | ZFS_TIME_ENCODE(&now, ctime); | |
34dc7c2f | 670 | |
3558fd73 | 671 | if (vap->va_mask & ATTR_ATIME) { |
428870ff | 672 | ZFS_TIME_ENCODE(&vap->va_atime, atime); |
34dc7c2f | 673 | } else { |
428870ff | 674 | ZFS_TIME_ENCODE(&now, atime); |
34dc7c2f BB |
675 | } |
676 | ||
3558fd73 | 677 | if (vap->va_mask & ATTR_MTIME) { |
428870ff BB |
678 | ZFS_TIME_ENCODE(&vap->va_mtime, mtime); |
679 | } else { | |
680 | ZFS_TIME_ENCODE(&now, mtime); | |
681 | } | |
682 | ||
683 | /* Now add in all of the "SA" attributes */ | |
3558fd73 | 684 | VERIFY(0 == sa_handle_get_from_db(zsb->z_os, db, NULL, SA_HDL_SHARED, |
428870ff BB |
685 | &sa_hdl)); |
686 | ||
687 | /* | |
688 | * Setup the array of attributes to be replaced/set on the new file | |
689 | * | |
690 | * order for DMU_OT_ZNODE is critical since it needs to be constructed | |
691 | * in the old znode_phys_t format. Don't change this ordering | |
692 | */ | |
79c76d5b | 693 | sa_attrs = kmem_alloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP); |
428870ff BB |
694 | |
695 | if (obj_type == DMU_OT_ZNODE) { | |
3558fd73 | 696 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb), |
428870ff | 697 | NULL, &atime, 16); |
3558fd73 | 698 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb), |
428870ff | 699 | NULL, &mtime, 16); |
3558fd73 | 700 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb), |
428870ff | 701 | NULL, &ctime, 16); |
3558fd73 | 702 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb), |
428870ff | 703 | NULL, &crtime, 16); |
3558fd73 | 704 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb), |
428870ff | 705 | NULL, &gen, 8); |
3558fd73 | 706 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb), |
428870ff | 707 | NULL, &mode, 8); |
3558fd73 | 708 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb), |
428870ff | 709 | NULL, &size, 8); |
3558fd73 | 710 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb), |
428870ff | 711 | NULL, &parent, 8); |
34dc7c2f | 712 | } else { |
3558fd73 | 713 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb), |
428870ff | 714 | NULL, &mode, 8); |
3558fd73 | 715 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb), |
428870ff | 716 | NULL, &size, 8); |
3558fd73 | 717 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb), |
428870ff | 718 | NULL, &gen, 8); |
3558fd73 BB |
719 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb), |
720 | NULL, &acl_ids->z_fuid, 8); | |
721 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb), | |
722 | NULL, &acl_ids->z_fgid, 8); | |
723 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb), | |
428870ff | 724 | NULL, &parent, 8); |
3558fd73 | 725 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb), |
428870ff | 726 | NULL, &pflags, 8); |
3558fd73 | 727 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb), |
428870ff | 728 | NULL, &atime, 16); |
3558fd73 | 729 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb), |
428870ff | 730 | NULL, &mtime, 16); |
3558fd73 | 731 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb), |
428870ff | 732 | NULL, &ctime, 16); |
3558fd73 | 733 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb), |
428870ff BB |
734 | NULL, &crtime, 16); |
735 | } | |
736 | ||
3558fd73 | 737 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zsb), NULL, &links, 8); |
428870ff BB |
738 | |
739 | if (obj_type == DMU_OT_ZNODE) { | |
3558fd73 | 740 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zsb), NULL, |
428870ff | 741 | &empty_xattr, 8); |
34dc7c2f | 742 | } |
428870ff | 743 | if (obj_type == DMU_OT_ZNODE || |
aa6d8c10 | 744 | (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))) { |
3558fd73 | 745 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zsb), |
428870ff | 746 | NULL, &rdev, 8); |
428870ff BB |
747 | } |
748 | if (obj_type == DMU_OT_ZNODE) { | |
3558fd73 | 749 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb), |
428870ff | 750 | NULL, &pflags, 8); |
3558fd73 | 751 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb), NULL, |
428870ff | 752 | &acl_ids->z_fuid, 8); |
3558fd73 | 753 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb), NULL, |
428870ff | 754 | &acl_ids->z_fgid, 8); |
3558fd73 | 755 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zsb), NULL, pad, |
428870ff | 756 | sizeof (uint64_t) * 4); |
3558fd73 | 757 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zsb), NULL, |
428870ff BB |
758 | &acl_phys, sizeof (zfs_acl_phys_t)); |
759 | } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) { | |
3558fd73 | 760 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zsb), NULL, |
428870ff BB |
761 | &acl_ids->z_aclp->z_acl_count, 8); |
762 | locate.cb_aclp = acl_ids->z_aclp; | |
3558fd73 | 763 | SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zsb), |
428870ff BB |
764 | zfs_acl_data_locator, &locate, |
765 | acl_ids->z_aclp->z_acl_bytes); | |
766 | mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags, | |
767 | acl_ids->z_fuid, acl_ids->z_fgid); | |
768 | } | |
769 | ||
770 | VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0); | |
34dc7c2f | 771 | |
34dc7c2f | 772 | if (!(flag & IS_ROOT_NODE)) { |
c85b224f | 773 | *zpp = zfs_znode_alloc(zsb, db, 0, obj_type, obj, sa_hdl, |
7b3e34ba BB |
774 | ZTOI(dzp)); |
775 | VERIFY(*zpp != NULL); | |
776 | VERIFY(dzp != NULL); | |
34dc7c2f BB |
777 | } else { |
778 | /* | |
779 | * If we are creating the root node, the "parent" we | |
780 | * passed in is the znode for the root. | |
781 | */ | |
782 | *zpp = dzp; | |
428870ff BB |
783 | |
784 | (*zpp)->z_sa_hdl = sa_hdl; | |
34dc7c2f | 785 | } |
428870ff BB |
786 | |
787 | (*zpp)->z_pflags = pflags; | |
788 | (*zpp)->z_mode = mode; | |
789 | ||
428870ff BB |
790 | if (obj_type == DMU_OT_ZNODE || |
791 | acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) { | |
b0bc7a84 | 792 | VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx)); |
428870ff | 793 | } |
d1d7e268 | 794 | kmem_free(sa_attrs, sizeof (sa_bulk_attr_t) * ZPL_END); |
3558fd73 | 795 | ZFS_OBJ_HOLD_EXIT(zsb, obj); |
34dc7c2f BB |
796 | } |
797 | ||
5484965a | 798 | /* |
d3cc8b15 WA |
799 | * Update in-core attributes. It is assumed the caller will be doing an |
800 | * sa_bulk_update to push the changes out. | |
5484965a BB |
801 | */ |
802 | void | |
803 | zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx) | |
804 | { | |
805 | xoptattr_t *xoap; | |
806 | ||
807 | xoap = xva_getxoptattr(xvap); | |
808 | ASSERT(xoap); | |
809 | ||
810 | if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { | |
811 | uint64_t times[2]; | |
812 | ZFS_TIME_ENCODE(&xoap->xoa_createtime, times); | |
813 | (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)), | |
814 | ×, sizeof (times), tx); | |
815 | XVA_SET_RTN(xvap, XAT_CREATETIME); | |
816 | } | |
817 | if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { | |
818 | ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly, | |
819 | zp->z_pflags, tx); | |
820 | XVA_SET_RTN(xvap, XAT_READONLY); | |
821 | } | |
822 | if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { | |
823 | ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden, | |
824 | zp->z_pflags, tx); | |
825 | XVA_SET_RTN(xvap, XAT_HIDDEN); | |
826 | } | |
827 | if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { | |
828 | ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system, | |
829 | zp->z_pflags, tx); | |
830 | XVA_SET_RTN(xvap, XAT_SYSTEM); | |
831 | } | |
832 | if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { | |
833 | ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive, | |
834 | zp->z_pflags, tx); | |
835 | XVA_SET_RTN(xvap, XAT_ARCHIVE); | |
836 | } | |
837 | if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { | |
838 | ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable, | |
839 | zp->z_pflags, tx); | |
840 | XVA_SET_RTN(xvap, XAT_IMMUTABLE); | |
841 | } | |
842 | if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { | |
843 | ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink, | |
844 | zp->z_pflags, tx); | |
845 | XVA_SET_RTN(xvap, XAT_NOUNLINK); | |
846 | } | |
847 | if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { | |
848 | ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly, | |
849 | zp->z_pflags, tx); | |
850 | XVA_SET_RTN(xvap, XAT_APPENDONLY); | |
851 | } | |
852 | if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { | |
853 | ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump, | |
854 | zp->z_pflags, tx); | |
855 | XVA_SET_RTN(xvap, XAT_NODUMP); | |
856 | } | |
857 | if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { | |
858 | ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque, | |
859 | zp->z_pflags, tx); | |
860 | XVA_SET_RTN(xvap, XAT_OPAQUE); | |
861 | } | |
862 | if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { | |
863 | ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED, | |
864 | xoap->xoa_av_quarantined, zp->z_pflags, tx); | |
865 | XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); | |
866 | } | |
867 | if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { | |
868 | ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified, | |
869 | zp->z_pflags, tx); | |
870 | XVA_SET_RTN(xvap, XAT_AV_MODIFIED); | |
871 | } | |
872 | if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) { | |
873 | zfs_sa_set_scanstamp(zp, xvap, tx); | |
874 | XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP); | |
875 | } | |
876 | if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { | |
877 | ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse, | |
878 | zp->z_pflags, tx); | |
879 | XVA_SET_RTN(xvap, XAT_REPARSE); | |
880 | } | |
881 | if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) { | |
882 | ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline, | |
883 | zp->z_pflags, tx); | |
884 | XVA_SET_RTN(xvap, XAT_OFFLINE); | |
885 | } | |
886 | if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) { | |
887 | ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse, | |
888 | zp->z_pflags, tx); | |
889 | XVA_SET_RTN(xvap, XAT_SPARSE); | |
890 | } | |
891 | } | |
892 | ||
34dc7c2f | 893 | int |
3558fd73 | 894 | zfs_zget(zfs_sb_t *zsb, uint64_t obj_num, znode_t **zpp) |
34dc7c2f BB |
895 | { |
896 | dmu_object_info_t doi; | |
897 | dmu_buf_t *db; | |
898 | znode_t *zp; | |
899 | int err; | |
428870ff | 900 | sa_handle_t *hdl; |
34dc7c2f BB |
901 | |
902 | *zpp = NULL; | |
903 | ||
6f9548c4 | 904 | again: |
3558fd73 | 905 | ZFS_OBJ_HOLD_ENTER(zsb, obj_num); |
34dc7c2f | 906 | |
3558fd73 | 907 | err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db); |
34dc7c2f | 908 | if (err) { |
3558fd73 | 909 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
34dc7c2f BB |
910 | return (err); |
911 | } | |
912 | ||
913 | dmu_object_info_from_db(db, &doi); | |
428870ff BB |
914 | if (doi.doi_bonus_type != DMU_OT_SA && |
915 | (doi.doi_bonus_type != DMU_OT_ZNODE || | |
916 | (doi.doi_bonus_type == DMU_OT_ZNODE && | |
917 | doi.doi_bonus_size < sizeof (znode_phys_t)))) { | |
918 | sa_buf_rele(db, NULL); | |
3558fd73 | 919 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
2e528b49 | 920 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
921 | } |
922 | ||
428870ff BB |
923 | hdl = dmu_buf_get_user(db); |
924 | if (hdl != NULL) { | |
36df2843 | 925 | zp = sa_get_userdata(hdl); |
34dc7c2f | 926 | |
8ac67298 | 927 | |
34dc7c2f | 928 | /* |
428870ff BB |
929 | * Since "SA" does immediate eviction we |
930 | * should never find a sa handle that doesn't | |
931 | * know about the znode. | |
34dc7c2f | 932 | */ |
428870ff BB |
933 | |
934 | ASSERT3P(zp, !=, NULL); | |
935 | ||
936 | mutex_enter(&zp->z_lock); | |
34dc7c2f BB |
937 | ASSERT3U(zp->z_id, ==, obj_num); |
938 | if (zp->z_unlinked) { | |
2e528b49 | 939 | err = SET_ERROR(ENOENT); |
34dc7c2f | 940 | } else { |
6f9548c4 RY |
941 | /* |
942 | * If igrab() returns NULL the VFS has independently | |
943 | * determined the inode should be evicted and has | |
944 | * called iput_final() to start the eviction process. | |
945 | * The SA handle is still valid but because the VFS | |
946 | * requires that the eviction succeed we must drop | |
947 | * our locks and references to allow the eviction to | |
948 | * complete. The zfs_zget() may then be retried. | |
949 | * | |
950 | * This unlikely case could be optimized by registering | |
951 | * a sops->drop_inode() callback. The callback would | |
952 | * need to detect the active SA hold thereby informing | |
953 | * the VFS that this inode should not be evicted. | |
954 | */ | |
955 | if (igrab(ZTOI(zp)) == NULL) { | |
956 | mutex_exit(&zp->z_lock); | |
957 | sa_buf_rele(db, NULL); | |
958 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); | |
6102d037 | 959 | /* inode might need this to finish evict */ |
960 | cond_resched(); | |
6f9548c4 RY |
961 | goto again; |
962 | } | |
34dc7c2f BB |
963 | *zpp = zp; |
964 | err = 0; | |
965 | } | |
34dc7c2f | 966 | mutex_exit(&zp->z_lock); |
f3ad9cd6 | 967 | sa_buf_rele(db, NULL); |
3558fd73 | 968 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
34dc7c2f BB |
969 | return (err); |
970 | } | |
971 | ||
972 | /* | |
3558fd73 | 973 | * Not found create new znode/vnode but only if file exists. |
428870ff BB |
974 | * |
975 | * There is a small window where zfs_vget() could | |
976 | * find this object while a file create is still in | |
977 | * progress. This is checked for in zfs_znode_alloc() | |
978 | * | |
979 | * if zfs_znode_alloc() fails it will drop the hold on the | |
980 | * bonus buffer. | |
34dc7c2f | 981 | */ |
3558fd73 | 982 | zp = zfs_znode_alloc(zsb, db, doi.doi_data_block_size, |
7b3e34ba | 983 | doi.doi_bonus_type, obj_num, NULL, NULL); |
428870ff | 984 | if (zp == NULL) { |
2e528b49 | 985 | err = SET_ERROR(ENOENT); |
428870ff BB |
986 | } else { |
987 | *zpp = zp; | |
988 | } | |
3558fd73 | 989 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
428870ff | 990 | return (err); |
34dc7c2f BB |
991 | } |
992 | ||
993 | int | |
994 | zfs_rezget(znode_t *zp) | |
995 | { | |
3558fd73 | 996 | zfs_sb_t *zsb = ZTOZSB(zp); |
34dc7c2f BB |
997 | dmu_object_info_t doi; |
998 | dmu_buf_t *db; | |
999 | uint64_t obj_num = zp->z_id; | |
428870ff | 1000 | uint64_t mode; |
428870ff | 1001 | sa_bulk_attr_t bulk[8]; |
34dc7c2f | 1002 | int err; |
428870ff BB |
1003 | int count = 0; |
1004 | uint64_t gen; | |
34dc7c2f | 1005 | |
3558fd73 | 1006 | ZFS_OBJ_HOLD_ENTER(zsb, obj_num); |
34dc7c2f | 1007 | |
428870ff BB |
1008 | mutex_enter(&zp->z_acl_lock); |
1009 | if (zp->z_acl_cached) { | |
1010 | zfs_acl_free(zp->z_acl_cached); | |
1011 | zp->z_acl_cached = NULL; | |
1012 | } | |
428870ff | 1013 | mutex_exit(&zp->z_acl_lock); |
7b3e34ba | 1014 | |
a8ad3bf0 BB |
1015 | /* |
1016 | * Lock inversion with zpl_xattr_get->__zpl_xattr_get->zfs_lookup | |
1017 | * between z_xattr_lock and z_teardown_lock. Detect this case and | |
1018 | * return EBUSY so zfs_resume_fs() will mark the inode stale and it | |
1019 | * will safely be revalidated on next access. | |
1020 | */ | |
1021 | err = rw_tryenter(&zp->z_xattr_lock, RW_WRITER); | |
1022 | if (!err) | |
1023 | return (SET_ERROR(EBUSY)); | |
1024 | ||
7b3e34ba BB |
1025 | if (zp->z_xattr_cached) { |
1026 | nvlist_free(zp->z_xattr_cached); | |
1027 | zp->z_xattr_cached = NULL; | |
1028 | } | |
1029 | ||
1030 | if (zp->z_xattr_parent) { | |
2d501583 | 1031 | zfs_iput_async(ZTOI(zp->z_xattr_parent)); |
7b3e34ba BB |
1032 | zp->z_xattr_parent = NULL; |
1033 | } | |
1034 | rw_exit(&zp->z_xattr_lock); | |
1035 | ||
428870ff | 1036 | ASSERT(zp->z_sa_hdl == NULL); |
3558fd73 | 1037 | err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db); |
34dc7c2f | 1038 | if (err) { |
3558fd73 | 1039 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
34dc7c2f BB |
1040 | return (err); |
1041 | } | |
1042 | ||
1043 | dmu_object_info_from_db(db, &doi); | |
428870ff BB |
1044 | if (doi.doi_bonus_type != DMU_OT_SA && |
1045 | (doi.doi_bonus_type != DMU_OT_ZNODE || | |
1046 | (doi.doi_bonus_type == DMU_OT_ZNODE && | |
1047 | doi.doi_bonus_size < sizeof (znode_phys_t)))) { | |
1048 | sa_buf_rele(db, NULL); | |
3558fd73 | 1049 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
2e528b49 | 1050 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
1051 | } |
1052 | ||
3558fd73 | 1053 | zfs_znode_sa_init(zsb, zp, db, doi.doi_bonus_type, NULL); |
428870ff BB |
1054 | |
1055 | /* reload cached values */ | |
3558fd73 | 1056 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zsb), NULL, |
428870ff | 1057 | &gen, sizeof (gen)); |
3558fd73 | 1058 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, |
428870ff | 1059 | &zp->z_size, sizeof (zp->z_size)); |
3558fd73 | 1060 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb), NULL, |
428870ff | 1061 | &zp->z_links, sizeof (zp->z_links)); |
3558fd73 | 1062 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL, |
428870ff | 1063 | &zp->z_pflags, sizeof (zp->z_pflags)); |
3558fd73 | 1064 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL, |
428870ff | 1065 | &zp->z_atime, sizeof (zp->z_atime)); |
3558fd73 | 1066 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL, |
572e2857 | 1067 | &zp->z_uid, sizeof (zp->z_uid)); |
3558fd73 | 1068 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL, |
572e2857 | 1069 | &zp->z_gid, sizeof (zp->z_gid)); |
3558fd73 | 1070 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL, |
428870ff BB |
1071 | &mode, sizeof (mode)); |
1072 | ||
428870ff BB |
1073 | if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) { |
1074 | zfs_znode_dmu_fini(zp); | |
3558fd73 | 1075 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
2e528b49 | 1076 | return (SET_ERROR(EIO)); |
428870ff BB |
1077 | } |
1078 | ||
572e2857 BB |
1079 | zp->z_mode = mode; |
1080 | ||
428870ff BB |
1081 | if (gen != zp->z_gen) { |
1082 | zfs_znode_dmu_fini(zp); | |
3558fd73 | 1083 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
2e528b49 | 1084 | return (SET_ERROR(EIO)); |
34dc7c2f BB |
1085 | } |
1086 | ||
428870ff | 1087 | zp->z_unlinked = (zp->z_links == 0); |
34dc7c2f | 1088 | zp->z_blksz = doi.doi_data_block_size; |
7b3e34ba | 1089 | zfs_inode_update(zp); |
34dc7c2f | 1090 | |
3558fd73 | 1091 | ZFS_OBJ_HOLD_EXIT(zsb, obj_num); |
34dc7c2f BB |
1092 | |
1093 | return (0); | |
1094 | } | |
1095 | ||
1096 | void | |
1097 | zfs_znode_delete(znode_t *zp, dmu_tx_t *tx) | |
1098 | { | |
3558fd73 BB |
1099 | zfs_sb_t *zsb = ZTOZSB(zp); |
1100 | objset_t *os = zsb->z_os; | |
34dc7c2f | 1101 | uint64_t obj = zp->z_id; |
572e2857 | 1102 | uint64_t acl_obj = zfs_external_acl(zp); |
34dc7c2f | 1103 | |
3558fd73 | 1104 | ZFS_OBJ_HOLD_ENTER(zsb, obj); |
572e2857 BB |
1105 | if (acl_obj) { |
1106 | VERIFY(!zp->z_is_sa); | |
b128c09f | 1107 | VERIFY(0 == dmu_object_free(os, acl_obj, tx)); |
572e2857 | 1108 | } |
b128c09f | 1109 | VERIFY(0 == dmu_object_free(os, obj, tx)); |
34dc7c2f | 1110 | zfs_znode_dmu_fini(zp); |
3558fd73 | 1111 | ZFS_OBJ_HOLD_EXIT(zsb, obj); |
34dc7c2f BB |
1112 | } |
1113 | ||
1114 | void | |
1115 | zfs_zinactive(znode_t *zp) | |
1116 | { | |
3558fd73 | 1117 | zfs_sb_t *zsb = ZTOZSB(zp); |
34dc7c2f BB |
1118 | uint64_t z_id = zp->z_id; |
1119 | ||
428870ff | 1120 | ASSERT(zp->z_sa_hdl); |
34dc7c2f BB |
1121 | |
1122 | /* | |
d6bd8eaa | 1123 | * Don't allow a zfs_zget() while were trying to release this znode. |
34dc7c2f | 1124 | */ |
40d06e3c | 1125 | ZFS_OBJ_HOLD_ENTER(zsb, z_id); |
d6bd8eaa | 1126 | |
34dc7c2f | 1127 | mutex_enter(&zp->z_lock); |
34dc7c2f BB |
1128 | |
1129 | /* | |
1130 | * If this was the last reference to a file with no links, | |
1131 | * remove the file from the file system. | |
1132 | */ | |
1133 | if (zp->z_unlinked) { | |
1134 | mutex_exit(&zp->z_lock); | |
d6bd8eaa | 1135 | |
40d06e3c | 1136 | ZFS_OBJ_HOLD_EXIT(zsb, z_id); |
d6bd8eaa | 1137 | |
34dc7c2f BB |
1138 | zfs_rmnode(zp); |
1139 | return; | |
1140 | } | |
428870ff | 1141 | |
34dc7c2f BB |
1142 | mutex_exit(&zp->z_lock); |
1143 | zfs_znode_dmu_fini(zp); | |
d6bd8eaa | 1144 | |
40d06e3c | 1145 | ZFS_OBJ_HOLD_EXIT(zsb, z_id); |
34dc7c2f BB |
1146 | } |
1147 | ||
6d111134 TC |
1148 | static inline int |
1149 | zfs_compare_timespec(struct timespec *t1, struct timespec *t2) | |
1150 | { | |
1151 | if (t1->tv_sec < t2->tv_sec) | |
1152 | return (-1); | |
1153 | ||
1154 | if (t1->tv_sec > t2->tv_sec) | |
1155 | return (1); | |
1156 | ||
1157 | return (t1->tv_nsec - t2->tv_nsec); | |
1158 | } | |
1159 | ||
1160 | /* | |
1161 | * Determine whether the znode's atime must be updated. The logic mostly | |
1162 | * duplicates the Linux kernel's relatime_need_update() functionality. | |
1163 | * This function is only called if the underlying filesystem actually has | |
1164 | * atime updates enabled. | |
1165 | */ | |
1166 | static inline boolean_t | |
1167 | zfs_atime_need_update(znode_t *zp, timestruc_t *now) | |
1168 | { | |
1169 | if (!ZTOZSB(zp)->z_relatime) | |
1170 | return (B_TRUE); | |
1171 | ||
1172 | /* | |
1173 | * In relatime mode, only update the atime if the previous atime | |
1174 | * is earlier than either the ctime or mtime or if at least a day | |
1175 | * has passed since the last update of atime. | |
1176 | */ | |
1177 | if (zfs_compare_timespec(&ZTOI(zp)->i_mtime, &ZTOI(zp)->i_atime) >= 0) | |
1178 | return (B_TRUE); | |
1179 | ||
1180 | if (zfs_compare_timespec(&ZTOI(zp)->i_ctime, &ZTOI(zp)->i_atime) >= 0) | |
1181 | return (B_TRUE); | |
1182 | ||
1183 | if ((long)now->tv_sec - ZTOI(zp)->i_atime.tv_sec >= 24*60*60) | |
1184 | return (B_TRUE); | |
1185 | ||
1186 | return (B_FALSE); | |
1187 | } | |
1188 | ||
1189 | /* | |
1190 | * Prepare to update znode time stamps. | |
1191 | * | |
1192 | * IN: zp - znode requiring timestamp update | |
1193 | * flag - ATTR_MTIME, ATTR_CTIME, ATTR_ATIME flags | |
1194 | * have_tx - true of caller is creating a new txg | |
1195 | * | |
1196 | * OUT: zp - new atime (via underlying inode's i_atime) | |
1197 | * mtime - new mtime | |
1198 | * ctime - new ctime | |
1199 | * | |
1200 | * NOTE: The arguments are somewhat redundant. The following condition | |
1201 | * is always true: | |
1202 | * | |
1203 | * have_tx == !(flag & ATTR_ATIME) | |
1204 | */ | |
34dc7c2f | 1205 | void |
428870ff BB |
1206 | zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2], |
1207 | uint64_t ctime[2], boolean_t have_tx) | |
34dc7c2f BB |
1208 | { |
1209 | timestruc_t now; | |
1210 | ||
6d111134 | 1211 | ASSERT(have_tx == !(flag & ATTR_ATIME)); |
34dc7c2f BB |
1212 | gethrestime(&now); |
1213 | ||
6d111134 TC |
1214 | /* |
1215 | * NOTE: The following test intentionally does not update z_atime_dirty | |
1216 | * in the case where an ATIME update has been requested but for which | |
1217 | * the update is omitted due to relatime logic. The rationale being | |
1218 | * that if the flag was set somewhere else, we should leave it alone | |
1219 | * here. | |
1220 | */ | |
1221 | if (flag & ATTR_ATIME) { | |
1222 | if (zfs_atime_need_update(zp, &now)) { | |
1223 | ZFS_TIME_ENCODE(&now, zp->z_atime); | |
1224 | ZTOI(zp)->i_atime.tv_sec = zp->z_atime[0]; | |
1225 | ZTOI(zp)->i_atime.tv_nsec = zp->z_atime[1]; | |
1226 | zp->z_atime_dirty = 1; | |
1227 | } | |
1228 | } else { | |
34dc7c2f BB |
1229 | zp->z_atime_dirty = 0; |
1230 | zp->z_seq++; | |
428870ff | 1231 | } |
34dc7c2f | 1232 | |
3558fd73 | 1233 | if (flag & ATTR_MTIME) { |
428870ff | 1234 | ZFS_TIME_ENCODE(&now, mtime); |
3558fd73 | 1235 | if (ZTOZSB(zp)->z_use_fuids) { |
428870ff BB |
1236 | zp->z_pflags |= (ZFS_ARCHIVE | |
1237 | ZFS_AV_MODIFIED); | |
1238 | } | |
34dc7c2f BB |
1239 | } |
1240 | ||
3558fd73 | 1241 | if (flag & ATTR_CTIME) { |
428870ff | 1242 | ZFS_TIME_ENCODE(&now, ctime); |
3558fd73 | 1243 | if (ZTOZSB(zp)->z_use_fuids) |
428870ff | 1244 | zp->z_pflags |= ZFS_ARCHIVE; |
34dc7c2f BB |
1245 | } |
1246 | } | |
1247 | ||
34dc7c2f BB |
1248 | /* |
1249 | * Grow the block size for a file. | |
1250 | * | |
1251 | * IN: zp - znode of file to free data in. | |
1252 | * size - requested block size | |
1253 | * tx - open transaction. | |
1254 | * | |
1255 | * NOTE: this function assumes that the znode is write locked. | |
1256 | */ | |
1257 | void | |
1258 | zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx) | |
1259 | { | |
1260 | int error; | |
1261 | u_longlong_t dummy; | |
1262 | ||
1263 | if (size <= zp->z_blksz) | |
1264 | return; | |
1265 | /* | |
1266 | * If the file size is already greater than the current blocksize, | |
1267 | * we will not grow. If there is more than one block in a file, | |
1268 | * the blocksize cannot change. | |
1269 | */ | |
428870ff | 1270 | if (zp->z_blksz && zp->z_size > zp->z_blksz) |
34dc7c2f BB |
1271 | return; |
1272 | ||
3558fd73 | 1273 | error = dmu_object_set_blocksize(ZTOZSB(zp)->z_os, zp->z_id, |
34dc7c2f | 1274 | size, 0, tx); |
428870ff | 1275 | |
34dc7c2f BB |
1276 | if (error == ENOTSUP) |
1277 | return; | |
c99c9001 | 1278 | ASSERT0(error); |
34dc7c2f BB |
1279 | |
1280 | /* What blocksize did we actually get? */ | |
428870ff | 1281 | dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy); |
34dc7c2f BB |
1282 | } |
1283 | ||
34dc7c2f | 1284 | /* |
b128c09f | 1285 | * Increase the file length |
34dc7c2f BB |
1286 | * |
1287 | * IN: zp - znode of file to free data in. | |
b128c09f | 1288 | * end - new end-of-file |
34dc7c2f | 1289 | * |
d3cc8b15 | 1290 | * RETURN: 0 on success, error code on failure |
34dc7c2f | 1291 | */ |
b128c09f BB |
1292 | static int |
1293 | zfs_extend(znode_t *zp, uint64_t end) | |
34dc7c2f | 1294 | { |
3558fd73 | 1295 | zfs_sb_t *zsb = ZTOZSB(zp); |
b128c09f | 1296 | dmu_tx_t *tx; |
34dc7c2f | 1297 | rl_t *rl; |
b128c09f | 1298 | uint64_t newblksz; |
34dc7c2f BB |
1299 | int error; |
1300 | ||
34dc7c2f | 1301 | /* |
b128c09f | 1302 | * We will change zp_size, lock the whole file. |
34dc7c2f | 1303 | */ |
b128c09f | 1304 | rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER); |
34dc7c2f BB |
1305 | |
1306 | /* | |
1307 | * Nothing to do if file already at desired length. | |
1308 | */ | |
428870ff | 1309 | if (end <= zp->z_size) { |
34dc7c2f BB |
1310 | zfs_range_unlock(rl); |
1311 | return (0); | |
1312 | } | |
3558fd73 | 1313 | tx = dmu_tx_create(zsb->z_os); |
428870ff BB |
1314 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); |
1315 | zfs_sa_upgrade_txholds(tx, zp); | |
b128c09f | 1316 | if (end > zp->z_blksz && |
3558fd73 | 1317 | (!ISP2(zp->z_blksz) || zp->z_blksz < zsb->z_max_blksz)) { |
34dc7c2f BB |
1318 | /* |
1319 | * We are growing the file past the current block size. | |
1320 | */ | |
3558fd73 | 1321 | if (zp->z_blksz > ZTOZSB(zp)->z_max_blksz) { |
f1512ee6 MA |
1322 | /* |
1323 | * File's blocksize is already larger than the | |
1324 | * "recordsize" property. Only let it grow to | |
1325 | * the next power of 2. | |
1326 | */ | |
34dc7c2f | 1327 | ASSERT(!ISP2(zp->z_blksz)); |
f1512ee6 | 1328 | newblksz = MIN(end, 1 << highbit64(zp->z_blksz)); |
34dc7c2f | 1329 | } else { |
3558fd73 | 1330 | newblksz = MIN(end, ZTOZSB(zp)->z_max_blksz); |
34dc7c2f | 1331 | } |
b128c09f BB |
1332 | dmu_tx_hold_write(tx, zp->z_id, 0, newblksz); |
1333 | } else { | |
1334 | newblksz = 0; | |
34dc7c2f BB |
1335 | } |
1336 | ||
384f8a09 | 1337 | error = dmu_tx_assign(tx, TXG_WAIT); |
34dc7c2f | 1338 | if (error) { |
34dc7c2f BB |
1339 | dmu_tx_abort(tx); |
1340 | zfs_range_unlock(rl); | |
1341 | return (error); | |
1342 | } | |
1343 | ||
b128c09f BB |
1344 | if (newblksz) |
1345 | zfs_grow_blocksize(zp, newblksz, tx); | |
34dc7c2f | 1346 | |
428870ff BB |
1347 | zp->z_size = end; |
1348 | ||
3558fd73 | 1349 | VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(ZTOZSB(zp)), |
428870ff | 1350 | &zp->z_size, sizeof (zp->z_size), tx)); |
34dc7c2f | 1351 | |
b128c09f | 1352 | zfs_range_unlock(rl); |
34dc7c2f | 1353 | |
b128c09f | 1354 | dmu_tx_commit(tx); |
34dc7c2f | 1355 | |
b128c09f BB |
1356 | return (0); |
1357 | } | |
1358 | ||
223df016 TC |
1359 | /* |
1360 | * zfs_zero_partial_page - Modeled after update_pages() but | |
1361 | * with different arguments and semantics for use by zfs_freesp(). | |
1362 | * | |
1363 | * Zeroes a piece of a single page cache entry for zp at offset | |
1364 | * start and length len. | |
1365 | * | |
1366 | * Caller must acquire a range lock on the file for the region | |
1367 | * being zeroed in order that the ARC and page cache stay in sync. | |
1368 | */ | |
1369 | static void | |
1370 | zfs_zero_partial_page(znode_t *zp, uint64_t start, uint64_t len) | |
1371 | { | |
1372 | struct address_space *mp = ZTOI(zp)->i_mapping; | |
1373 | struct page *pp; | |
1374 | int64_t off; | |
1375 | void *pb; | |
1376 | ||
1377 | ASSERT((start & PAGE_CACHE_MASK) == | |
1378 | ((start + len - 1) & PAGE_CACHE_MASK)); | |
1379 | ||
1380 | off = start & (PAGE_CACHE_SIZE - 1); | |
1381 | start &= PAGE_CACHE_MASK; | |
1382 | ||
1383 | pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT); | |
1384 | if (pp) { | |
1385 | if (mapping_writably_mapped(mp)) | |
1386 | flush_dcache_page(pp); | |
1387 | ||
1388 | pb = kmap(pp); | |
1389 | bzero(pb + off, len); | |
1390 | kunmap(pp); | |
1391 | ||
1392 | if (mapping_writably_mapped(mp)) | |
1393 | flush_dcache_page(pp); | |
1394 | ||
1395 | mark_page_accessed(pp); | |
1396 | SetPageUptodate(pp); | |
1397 | ClearPageError(pp); | |
1398 | unlock_page(pp); | |
1399 | page_cache_release(pp); | |
1400 | } | |
1401 | } | |
1402 | ||
b128c09f BB |
1403 | /* |
1404 | * Free space in a file. | |
1405 | * | |
1406 | * IN: zp - znode of file to free data in. | |
1407 | * off - start of section to free. | |
1408 | * len - length of section to free. | |
1409 | * | |
d3cc8b15 | 1410 | * RETURN: 0 on success, error code on failure |
b128c09f BB |
1411 | */ |
1412 | static int | |
1413 | zfs_free_range(znode_t *zp, uint64_t off, uint64_t len) | |
1414 | { | |
3558fd73 | 1415 | zfs_sb_t *zsb = ZTOZSB(zp); |
b128c09f BB |
1416 | rl_t *rl; |
1417 | int error; | |
1418 | ||
1419 | /* | |
1420 | * Lock the range being freed. | |
1421 | */ | |
1422 | rl = zfs_range_lock(zp, off, len, RL_WRITER); | |
1423 | ||
1424 | /* | |
1425 | * Nothing to do if file already at desired length. | |
1426 | */ | |
428870ff | 1427 | if (off >= zp->z_size) { |
b128c09f BB |
1428 | zfs_range_unlock(rl); |
1429 | return (0); | |
34dc7c2f BB |
1430 | } |
1431 | ||
428870ff BB |
1432 | if (off + len > zp->z_size) |
1433 | len = zp->z_size - off; | |
b128c09f | 1434 | |
3558fd73 | 1435 | error = dmu_free_long_range(zsb->z_os, zp->z_id, off, len); |
b128c09f | 1436 | |
223df016 TC |
1437 | /* |
1438 | * Zero partial page cache entries. This must be done under a | |
1439 | * range lock in order to keep the ARC and page cache in sync. | |
1440 | */ | |
1441 | if (zp->z_is_mapped) { | |
1442 | loff_t first_page, last_page, page_len; | |
1443 | loff_t first_page_offset, last_page_offset; | |
1444 | ||
1445 | /* first possible full page in hole */ | |
1446 | first_page = (off + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | |
1447 | /* last page of hole */ | |
1448 | last_page = (off + len) >> PAGE_CACHE_SHIFT; | |
1449 | ||
1450 | /* offset of first_page */ | |
1451 | first_page_offset = first_page << PAGE_CACHE_SHIFT; | |
1452 | /* offset of last_page */ | |
1453 | last_page_offset = last_page << PAGE_CACHE_SHIFT; | |
1454 | ||
cb08f063 TC |
1455 | /* truncate whole pages */ |
1456 | if (last_page_offset > first_page_offset) { | |
1457 | truncate_inode_pages_range(ZTOI(zp)->i_mapping, | |
1458 | first_page_offset, last_page_offset - 1); | |
1459 | } | |
1460 | ||
1461 | /* truncate sub-page ranges */ | |
223df016 TC |
1462 | if (first_page > last_page) { |
1463 | /* entire punched area within a single page */ | |
1464 | zfs_zero_partial_page(zp, off, len); | |
1465 | } else { | |
1466 | /* beginning of punched area at the end of a page */ | |
1467 | page_len = first_page_offset - off; | |
1468 | if (page_len > 0) | |
1469 | zfs_zero_partial_page(zp, off, page_len); | |
1470 | ||
1471 | /* end of punched area at the beginning of a page */ | |
1472 | page_len = off + len - last_page_offset; | |
1473 | if (page_len > 0) | |
1474 | zfs_zero_partial_page(zp, last_page_offset, | |
1475 | page_len); | |
1476 | } | |
1477 | } | |
34dc7c2f BB |
1478 | zfs_range_unlock(rl); |
1479 | ||
b128c09f BB |
1480 | return (error); |
1481 | } | |
1482 | ||
1483 | /* | |
1484 | * Truncate a file | |
1485 | * | |
1486 | * IN: zp - znode of file to free data in. | |
1487 | * end - new end-of-file. | |
1488 | * | |
d3cc8b15 | 1489 | * RETURN: 0 on success, error code on failure |
b128c09f BB |
1490 | */ |
1491 | static int | |
1492 | zfs_trunc(znode_t *zp, uint64_t end) | |
1493 | { | |
3558fd73 | 1494 | zfs_sb_t *zsb = ZTOZSB(zp); |
b128c09f BB |
1495 | dmu_tx_t *tx; |
1496 | rl_t *rl; | |
1497 | int error; | |
572e2857 BB |
1498 | sa_bulk_attr_t bulk[2]; |
1499 | int count = 0; | |
b128c09f BB |
1500 | |
1501 | /* | |
1502 | * We will change zp_size, lock the whole file. | |
1503 | */ | |
1504 | rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER); | |
1505 | ||
1506 | /* | |
1507 | * Nothing to do if file already at desired length. | |
1508 | */ | |
428870ff | 1509 | if (end >= zp->z_size) { |
b128c09f BB |
1510 | zfs_range_unlock(rl); |
1511 | return (0); | |
1512 | } | |
1513 | ||
3558fd73 | 1514 | error = dmu_free_long_range(zsb->z_os, zp->z_id, end, -1); |
b128c09f BB |
1515 | if (error) { |
1516 | zfs_range_unlock(rl); | |
1517 | return (error); | |
1518 | } | |
3558fd73 | 1519 | tx = dmu_tx_create(zsb->z_os); |
428870ff BB |
1520 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); |
1521 | zfs_sa_upgrade_txholds(tx, zp); | |
7a8f0e80 | 1522 | error = dmu_tx_assign(tx, TXG_WAIT); |
b128c09f | 1523 | if (error) { |
b128c09f BB |
1524 | dmu_tx_abort(tx); |
1525 | zfs_range_unlock(rl); | |
1526 | return (error); | |
1527 | } | |
b128c09f | 1528 | |
428870ff | 1529 | zp->z_size = end; |
3558fd73 | 1530 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), |
572e2857 | 1531 | NULL, &zp->z_size, sizeof (zp->z_size)); |
428870ff | 1532 | |
572e2857 BB |
1533 | if (end == 0) { |
1534 | zp->z_pflags &= ~ZFS_SPARSE; | |
3558fd73 | 1535 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), |
572e2857 BB |
1536 | NULL, &zp->z_pflags, 8); |
1537 | } | |
1538 | VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0); | |
b128c09f | 1539 | |
34dc7c2f BB |
1540 | dmu_tx_commit(tx); |
1541 | ||
d164b209 | 1542 | zfs_range_unlock(rl); |
34dc7c2f BB |
1543 | |
1544 | return (0); | |
1545 | } | |
1546 | ||
b128c09f BB |
1547 | /* |
1548 | * Free space in a file | |
1549 | * | |
1550 | * IN: zp - znode of file to free data in. | |
1551 | * off - start of range | |
1552 | * len - end of range (0 => EOF) | |
1553 | * flag - current file open mode flags. | |
1554 | * log - TRUE if this action should be logged | |
1555 | * | |
d3cc8b15 | 1556 | * RETURN: 0 on success, error code on failure |
b128c09f BB |
1557 | */ |
1558 | int | |
1559 | zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log) | |
1560 | { | |
b128c09f | 1561 | dmu_tx_t *tx; |
3558fd73 BB |
1562 | zfs_sb_t *zsb = ZTOZSB(zp); |
1563 | zilog_t *zilog = zsb->z_log; | |
428870ff BB |
1564 | uint64_t mode; |
1565 | uint64_t mtime[2], ctime[2]; | |
1566 | sa_bulk_attr_t bulk[3]; | |
1567 | int count = 0; | |
b128c09f BB |
1568 | int error; |
1569 | ||
3558fd73 | 1570 | if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zsb), &mode, |
428870ff BB |
1571 | sizeof (mode))) != 0) |
1572 | return (error); | |
1573 | ||
1574 | if (off > zp->z_size) { | |
b128c09f BB |
1575 | error = zfs_extend(zp, off+len); |
1576 | if (error == 0 && log) | |
1577 | goto log; | |
223df016 | 1578 | goto out; |
b128c09f BB |
1579 | } |
1580 | ||
b128c09f BB |
1581 | if (len == 0) { |
1582 | error = zfs_trunc(zp, off); | |
1583 | } else { | |
1584 | if ((error = zfs_free_range(zp, off, len)) == 0 && | |
428870ff | 1585 | off + len > zp->z_size) |
b128c09f BB |
1586 | error = zfs_extend(zp, off+len); |
1587 | } | |
1588 | if (error || !log) | |
223df016 | 1589 | goto out; |
b128c09f | 1590 | log: |
3558fd73 | 1591 | tx = dmu_tx_create(zsb->z_os); |
428870ff BB |
1592 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); |
1593 | zfs_sa_upgrade_txholds(tx, zp); | |
384f8a09 | 1594 | error = dmu_tx_assign(tx, TXG_WAIT); |
b128c09f | 1595 | if (error) { |
b128c09f | 1596 | dmu_tx_abort(tx); |
223df016 | 1597 | goto out; |
b128c09f BB |
1598 | } |
1599 | ||
3558fd73 BB |
1600 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, mtime, 16); |
1601 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, ctime, 16); | |
1602 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), | |
428870ff BB |
1603 | NULL, &zp->z_pflags, 8); |
1604 | zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE); | |
1605 | error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); | |
1606 | ASSERT(error == 0); | |
1607 | ||
b128c09f BB |
1608 | zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len); |
1609 | ||
1610 | dmu_tx_commit(tx); | |
223df016 | 1611 | |
960e08fe | 1612 | zfs_inode_update(zp); |
223df016 TC |
1613 | error = 0; |
1614 | ||
1615 | out: | |
1616 | /* | |
1617 | * Truncate the page cache - for file truncate operations, use | |
1618 | * the purpose-built API for truncations. For punching operations, | |
cb08f063 | 1619 | * the truncation is handled under a range lock in zfs_free_range. |
223df016 TC |
1620 | */ |
1621 | if (len == 0) | |
1622 | truncate_setsize(ZTOI(zp), off); | |
223df016 | 1623 | return (error); |
b128c09f BB |
1624 | } |
1625 | ||
34dc7c2f BB |
1626 | void |
1627 | zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx) | |
1628 | { | |
22872ff5 BB |
1629 | struct super_block *sb; |
1630 | zfs_sb_t *zsb; | |
428870ff | 1631 | uint64_t moid, obj, sa_obj, version; |
22872ff5 | 1632 | uint64_t sense = ZFS_CASE_SENSITIVE; |
34dc7c2f BB |
1633 | uint64_t norm = 0; |
1634 | nvpair_t *elem; | |
1635 | int error; | |
22872ff5 BB |
1636 | int i; |
1637 | znode_t *rootzp = NULL; | |
1638 | vattr_t vattr; | |
1639 | znode_t *zp; | |
1640 | zfs_acl_ids_t acl_ids; | |
34dc7c2f BB |
1641 | |
1642 | /* | |
1643 | * First attempt to create master node. | |
1644 | */ | |
1645 | /* | |
1646 | * In an empty objset, there are no blocks to read and thus | |
1647 | * there can be no i/o errors (which we assert below). | |
1648 | */ | |
1649 | moid = MASTER_NODE_OBJ; | |
1650 | error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE, | |
1651 | DMU_OT_NONE, 0, tx); | |
1652 | ASSERT(error == 0); | |
1653 | ||
1654 | /* | |
1655 | * Set starting attributes. | |
1656 | */ | |
428870ff | 1657 | version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os))); |
34dc7c2f BB |
1658 | elem = NULL; |
1659 | while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) { | |
1660 | /* For the moment we expect all zpl props to be uint64_ts */ | |
1661 | uint64_t val; | |
1662 | char *name; | |
1663 | ||
1664 | ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64); | |
1665 | VERIFY(nvpair_value_uint64(elem, &val) == 0); | |
1666 | name = nvpair_name(elem); | |
1667 | if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) { | |
9babb374 BB |
1668 | if (val < version) |
1669 | version = val; | |
34dc7c2f BB |
1670 | } else { |
1671 | error = zap_update(os, moid, name, 8, 1, &val, tx); | |
1672 | } | |
1673 | ASSERT(error == 0); | |
1674 | if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0) | |
1675 | norm = val; | |
22872ff5 BB |
1676 | else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0) |
1677 | sense = val; | |
34dc7c2f BB |
1678 | } |
1679 | ASSERT(version != 0); | |
9babb374 | 1680 | error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx); |
34dc7c2f | 1681 | |
428870ff BB |
1682 | /* |
1683 | * Create zap object used for SA attribute registration | |
1684 | */ | |
1685 | ||
1686 | if (version >= ZPL_VERSION_SA) { | |
1687 | sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE, | |
1688 | DMU_OT_NONE, 0, tx); | |
1689 | error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx); | |
1690 | ASSERT(error == 0); | |
1691 | } else { | |
1692 | sa_obj = 0; | |
1693 | } | |
34dc7c2f BB |
1694 | /* |
1695 | * Create a delete queue. | |
1696 | */ | |
9babb374 | 1697 | obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx); |
34dc7c2f | 1698 | |
9babb374 | 1699 | error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx); |
34dc7c2f BB |
1700 | ASSERT(error == 0); |
1701 | ||
9babb374 | 1702 | /* |
22872ff5 BB |
1703 | * Create root znode. Create minimal znode/inode/zsb/sb |
1704 | * to allow zfs_mknode to work. | |
9babb374 | 1705 | */ |
22872ff5 BB |
1706 | vattr.va_mask = ATTR_MODE|ATTR_UID|ATTR_GID; |
1707 | vattr.va_mode = S_IFDIR|0755; | |
1708 | vattr.va_uid = crgetuid(cr); | |
1709 | vattr.va_gid = crgetgid(cr); | |
1710 | ||
79c76d5b | 1711 | rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP); |
22872ff5 BB |
1712 | rootzp->z_moved = 0; |
1713 | rootzp->z_unlinked = 0; | |
1714 | rootzp->z_atime_dirty = 0; | |
1715 | rootzp->z_is_sa = USE_SA(version, os); | |
1716 | ||
79c76d5b | 1717 | zsb = kmem_zalloc(sizeof (zfs_sb_t), KM_SLEEP); |
22872ff5 BB |
1718 | zsb->z_os = os; |
1719 | zsb->z_parent = zsb; | |
1720 | zsb->z_version = version; | |
1721 | zsb->z_use_fuids = USE_FUIDS(version, os); | |
1722 | zsb->z_use_sa = USE_SA(version, os); | |
1723 | zsb->z_norm = norm; | |
1724 | ||
79c76d5b | 1725 | sb = kmem_zalloc(sizeof (struct super_block), KM_SLEEP); |
22872ff5 BB |
1726 | sb->s_fs_info = zsb; |
1727 | ||
1728 | ZTOI(rootzp)->i_sb = sb; | |
1729 | ||
1730 | error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END, | |
1731 | &zsb->z_attr_table); | |
9babb374 | 1732 | |
22872ff5 | 1733 | ASSERT(error == 0); |
9babb374 | 1734 | |
60101509 | 1735 | /* |
22872ff5 BB |
1736 | * Fold case on file systems that are always or sometimes case |
1737 | * insensitive. | |
60101509 | 1738 | */ |
22872ff5 BB |
1739 | if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED) |
1740 | zsb->z_norm |= U8_TEXTPREP_TOUPPER; | |
60101509 | 1741 | |
22872ff5 BB |
1742 | mutex_init(&zsb->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL); |
1743 | list_create(&zsb->z_all_znodes, sizeof (znode_t), | |
1744 | offsetof(znode_t, z_link_node)); | |
60101509 | 1745 | |
d07b7c7f CD |
1746 | zsb->z_hold_mtx = vmem_zalloc(sizeof (kmutex_t) * ZFS_OBJ_MTX_SZ, |
1747 | KM_SLEEP); | |
22872ff5 BB |
1748 | for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) |
1749 | mutex_init(&zsb->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL); | |
60101509 | 1750 | |
22872ff5 BB |
1751 | VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr, |
1752 | cr, NULL, &acl_ids)); | |
1753 | zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids); | |
1754 | ASSERT3P(zp, ==, rootzp); | |
1755 | error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx); | |
1756 | ASSERT(error == 0); | |
1757 | zfs_acl_ids_free(&acl_ids); | |
60101509 | 1758 | |
22872ff5 BB |
1759 | atomic_set(&ZTOI(rootzp)->i_count, 0); |
1760 | sa_handle_destroy(rootzp->z_sa_hdl); | |
22872ff5 BB |
1761 | kmem_cache_free(znode_cache, rootzp); |
1762 | ||
1763 | /* | |
1764 | * Create shares directory | |
1765 | */ | |
22872ff5 | 1766 | error = zfs_create_share_dir(zsb, tx); |
9babb374 | 1767 | ASSERT(error == 0); |
428870ff | 1768 | |
22872ff5 BB |
1769 | for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) |
1770 | mutex_destroy(&zsb->z_hold_mtx[i]); | |
2708f716 | 1771 | |
d07b7c7f | 1772 | vmem_free(zsb->z_hold_mtx, sizeof (kmutex_t) * ZFS_OBJ_MTX_SZ); |
2708f716 BB |
1773 | kmem_free(sb, sizeof (struct super_block)); |
1774 | kmem_free(zsb, sizeof (zfs_sb_t)); | |
34dc7c2f | 1775 | } |
34dc7c2f | 1776 | #endif /* _KERNEL */ |
428870ff | 1777 | |
34dc7c2f | 1778 | static int |
572e2857 BB |
1779 | zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table) |
1780 | { | |
1781 | uint64_t sa_obj = 0; | |
1782 | int error; | |
1783 | ||
1784 | error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj); | |
1785 | if (error != 0 && error != ENOENT) | |
1786 | return (error); | |
1787 | ||
1788 | error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table); | |
1789 | return (error); | |
1790 | } | |
1791 | ||
1792 | static int | |
1793 | zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp, | |
7b8518cb | 1794 | dmu_buf_t **db, void *tag) |
34dc7c2f | 1795 | { |
34dc7c2f | 1796 | dmu_object_info_t doi; |
34dc7c2f | 1797 | int error; |
428870ff | 1798 | |
7b8518cb | 1799 | if ((error = sa_buf_hold(osp, obj, tag, db)) != 0) |
34dc7c2f BB |
1800 | return (error); |
1801 | ||
572e2857 | 1802 | dmu_object_info_from_db(*db, &doi); |
428870ff BB |
1803 | if ((doi.doi_bonus_type != DMU_OT_SA && |
1804 | doi.doi_bonus_type != DMU_OT_ZNODE) || | |
d6320ddb BB |
1805 | (doi.doi_bonus_type == DMU_OT_ZNODE && |
1806 | doi.doi_bonus_size < sizeof (znode_phys_t))) { | |
7b8518cb | 1807 | sa_buf_rele(*db, tag); |
2e528b49 | 1808 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f BB |
1809 | } |
1810 | ||
572e2857 BB |
1811 | error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp); |
1812 | if (error != 0) { | |
7b8518cb | 1813 | sa_buf_rele(*db, tag); |
428870ff BB |
1814 | return (error); |
1815 | } | |
1816 | ||
572e2857 BB |
1817 | return (0); |
1818 | } | |
1819 | ||
1820 | void | |
7b8518cb | 1821 | zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag) |
572e2857 BB |
1822 | { |
1823 | sa_handle_destroy(hdl); | |
7b8518cb | 1824 | sa_buf_rele(db, tag); |
572e2857 BB |
1825 | } |
1826 | ||
1827 | /* | |
1828 | * Given an object number, return its parent object number and whether | |
1829 | * or not the object is an extended attribute directory. | |
1830 | */ | |
1831 | static int | |
1832 | zfs_obj_to_pobj(sa_handle_t *hdl, sa_attr_type_t *sa_table, uint64_t *pobjp, | |
1833 | int *is_xattrdir) | |
1834 | { | |
1835 | uint64_t parent; | |
1836 | uint64_t pflags; | |
1837 | uint64_t mode; | |
1838 | sa_bulk_attr_t bulk[3]; | |
1839 | int count = 0; | |
1840 | int error; | |
1841 | ||
1842 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL, | |
1843 | &parent, sizeof (parent)); | |
428870ff | 1844 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL, |
572e2857 | 1845 | &pflags, sizeof (pflags)); |
428870ff | 1846 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL, |
572e2857 | 1847 | &mode, sizeof (mode)); |
428870ff | 1848 | |
572e2857 | 1849 | if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0) |
428870ff | 1850 | return (error); |
572e2857 | 1851 | |
428870ff BB |
1852 | *pobjp = parent; |
1853 | *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode); | |
34dc7c2f BB |
1854 | |
1855 | return (0); | |
1856 | } | |
1857 | ||
572e2857 BB |
1858 | /* |
1859 | * Given an object number, return some zpl level statistics | |
1860 | */ | |
1861 | static int | |
1862 | zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table, | |
1863 | zfs_stat_t *sb) | |
34dc7c2f | 1864 | { |
572e2857 BB |
1865 | sa_bulk_attr_t bulk[4]; |
1866 | int count = 0; | |
1867 | ||
1868 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL, | |
1869 | &sb->zs_mode, sizeof (sb->zs_mode)); | |
1870 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL, | |
1871 | &sb->zs_gen, sizeof (sb->zs_gen)); | |
1872 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL, | |
1873 | &sb->zs_links, sizeof (sb->zs_links)); | |
1874 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL, | |
1875 | &sb->zs_ctime, sizeof (sb->zs_ctime)); | |
1876 | ||
1877 | return (sa_bulk_lookup(hdl, bulk, count)); | |
1878 | } | |
1879 | ||
1880 | static int | |
1881 | zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl, | |
1882 | sa_attr_type_t *sa_table, char *buf, int len) | |
1883 | { | |
1884 | sa_handle_t *sa_hdl; | |
1885 | sa_handle_t *prevhdl = NULL; | |
1886 | dmu_buf_t *prevdb = NULL; | |
1887 | dmu_buf_t *sa_db = NULL; | |
34dc7c2f BB |
1888 | char *path = buf + len - 1; |
1889 | int error; | |
1890 | ||
1891 | *path = '\0'; | |
572e2857 | 1892 | sa_hdl = hdl; |
428870ff | 1893 | |
34dc7c2f | 1894 | for (;;) { |
17897ce2 | 1895 | uint64_t pobj = 0; |
34dc7c2f BB |
1896 | char component[MAXNAMELEN + 2]; |
1897 | size_t complen; | |
17897ce2 | 1898 | int is_xattrdir = 0; |
34dc7c2f | 1899 | |
572e2857 | 1900 | if (prevdb) |
7b8518cb | 1901 | zfs_release_sa_handle(prevhdl, prevdb, FTAG); |
572e2857 BB |
1902 | |
1903 | if ((error = zfs_obj_to_pobj(sa_hdl, sa_table, &pobj, | |
1904 | &is_xattrdir)) != 0) | |
34dc7c2f BB |
1905 | break; |
1906 | ||
1907 | if (pobj == obj) { | |
1908 | if (path[0] != '/') | |
1909 | *--path = '/'; | |
1910 | break; | |
1911 | } | |
1912 | ||
1913 | component[0] = '/'; | |
1914 | if (is_xattrdir) { | |
1915 | (void) sprintf(component + 1, "<xattrdir>"); | |
1916 | } else { | |
1917 | error = zap_value_search(osp, pobj, obj, | |
1918 | ZFS_DIRENT_OBJ(-1ULL), component + 1); | |
1919 | if (error != 0) | |
1920 | break; | |
1921 | } | |
1922 | ||
1923 | complen = strlen(component); | |
1924 | path -= complen; | |
1925 | ASSERT(path >= buf); | |
1926 | bcopy(component, path, complen); | |
1927 | obj = pobj; | |
572e2857 BB |
1928 | |
1929 | if (sa_hdl != hdl) { | |
1930 | prevhdl = sa_hdl; | |
1931 | prevdb = sa_db; | |
1932 | } | |
7b8518cb | 1933 | error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG); |
572e2857 BB |
1934 | if (error != 0) { |
1935 | sa_hdl = prevhdl; | |
1936 | sa_db = prevdb; | |
1937 | break; | |
1938 | } | |
1939 | } | |
1940 | ||
1941 | if (sa_hdl != NULL && sa_hdl != hdl) { | |
1942 | ASSERT(sa_db != NULL); | |
7b8518cb | 1943 | zfs_release_sa_handle(sa_hdl, sa_db, FTAG); |
34dc7c2f BB |
1944 | } |
1945 | ||
1946 | if (error == 0) | |
1947 | (void) memmove(buf, path, buf + len - path); | |
428870ff | 1948 | |
34dc7c2f BB |
1949 | return (error); |
1950 | } | |
572e2857 BB |
1951 | |
1952 | int | |
1953 | zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len) | |
1954 | { | |
1955 | sa_attr_type_t *sa_table; | |
1956 | sa_handle_t *hdl; | |
1957 | dmu_buf_t *db; | |
1958 | int error; | |
1959 | ||
1960 | error = zfs_sa_setup(osp, &sa_table); | |
1961 | if (error != 0) | |
1962 | return (error); | |
1963 | ||
7b8518cb | 1964 | error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG); |
572e2857 BB |
1965 | if (error != 0) |
1966 | return (error); | |
1967 | ||
1968 | error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len); | |
1969 | ||
7b8518cb | 1970 | zfs_release_sa_handle(hdl, db, FTAG); |
572e2857 BB |
1971 | return (error); |
1972 | } | |
1973 | ||
1974 | int | |
1975 | zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb, | |
1976 | char *buf, int len) | |
1977 | { | |
1978 | char *path = buf + len - 1; | |
1979 | sa_attr_type_t *sa_table; | |
1980 | sa_handle_t *hdl; | |
1981 | dmu_buf_t *db; | |
1982 | int error; | |
1983 | ||
1984 | *path = '\0'; | |
1985 | ||
1986 | error = zfs_sa_setup(osp, &sa_table); | |
1987 | if (error != 0) | |
1988 | return (error); | |
1989 | ||
7b8518cb | 1990 | error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG); |
572e2857 BB |
1991 | if (error != 0) |
1992 | return (error); | |
1993 | ||
1994 | error = zfs_obj_to_stats_impl(hdl, sa_table, sb); | |
1995 | if (error != 0) { | |
7b8518cb | 1996 | zfs_release_sa_handle(hdl, db, FTAG); |
572e2857 BB |
1997 | return (error); |
1998 | } | |
1999 | ||
2000 | error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len); | |
2001 | ||
7b8518cb | 2002 | zfs_release_sa_handle(hdl, db, FTAG); |
572e2857 BB |
2003 | return (error); |
2004 | } | |
c28b2279 BB |
2005 | |
2006 | #if defined(_KERNEL) && defined(HAVE_SPL) | |
2007 | EXPORT_SYMBOL(zfs_create_fs); | |
2008 | EXPORT_SYMBOL(zfs_obj_to_path); | |
2009 | #endif |