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