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