]>
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 | 1327 | struct inode *ip = ZTOI(zp); |
b128c09f | 1328 | dmu_tx_t *tx; |
3558fd73 BB |
1329 | zfs_sb_t *zsb = ZTOZSB(zp); |
1330 | zilog_t *zilog = zsb->z_log; | |
428870ff BB |
1331 | uint64_t mode; |
1332 | uint64_t mtime[2], ctime[2]; | |
1333 | sa_bulk_attr_t bulk[3]; | |
1334 | int count = 0; | |
b128c09f BB |
1335 | int error; |
1336 | ||
3558fd73 | 1337 | if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zsb), &mode, |
428870ff BB |
1338 | sizeof (mode))) != 0) |
1339 | return (error); | |
1340 | ||
1341 | if (off > zp->z_size) { | |
b128c09f BB |
1342 | error = zfs_extend(zp, off+len); |
1343 | if (error == 0 && log) | |
1344 | goto log; | |
1345 | else | |
1346 | return (error); | |
1347 | } | |
1348 | ||
1349 | /* | |
1350 | * Check for any locks in the region to be freed. | |
1351 | */ | |
a89c3e0b | 1352 | if (ip->i_flock && mandatory_lock(ip)) { |
428870ff | 1353 | uint64_t length = (len ? len : zp->z_size - off); |
a89c3e0b RP |
1354 | if (!lock_may_write(ip, off, length)) |
1355 | return (EAGAIN); | |
b128c09f BB |
1356 | } |
1357 | ||
1358 | if (len == 0) { | |
1359 | error = zfs_trunc(zp, off); | |
1360 | } else { | |
1361 | if ((error = zfs_free_range(zp, off, len)) == 0 && | |
428870ff | 1362 | off + len > zp->z_size) |
b128c09f BB |
1363 | error = zfs_extend(zp, off+len); |
1364 | } | |
1365 | if (error || !log) | |
1366 | return (error); | |
1367 | log: | |
3558fd73 | 1368 | tx = dmu_tx_create(zsb->z_os); |
428870ff BB |
1369 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); |
1370 | zfs_sa_upgrade_txholds(tx, zp); | |
fb5f0bc8 | 1371 | error = dmu_tx_assign(tx, TXG_NOWAIT); |
b128c09f | 1372 | if (error) { |
fb5f0bc8 | 1373 | if (error == ERESTART) { |
b128c09f BB |
1374 | dmu_tx_wait(tx); |
1375 | dmu_tx_abort(tx); | |
1376 | goto log; | |
1377 | } | |
1378 | dmu_tx_abort(tx); | |
1379 | return (error); | |
1380 | } | |
1381 | ||
3558fd73 BB |
1382 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, mtime, 16); |
1383 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, ctime, 16); | |
1384 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), | |
428870ff BB |
1385 | NULL, &zp->z_pflags, 8); |
1386 | zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE); | |
1387 | error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); | |
1388 | ASSERT(error == 0); | |
1389 | ||
b128c09f BB |
1390 | zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len); |
1391 | ||
1392 | dmu_tx_commit(tx); | |
960e08fe | 1393 | zfs_inode_update(zp); |
b128c09f BB |
1394 | return (0); |
1395 | } | |
1396 | ||
34dc7c2f BB |
1397 | void |
1398 | zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx) | |
1399 | { | |
428870ff | 1400 | uint64_t moid, obj, sa_obj, version; |
34dc7c2f BB |
1401 | uint64_t norm = 0; |
1402 | nvpair_t *elem; | |
1403 | int error; | |
60101509 BB |
1404 | timestruc_t now; |
1405 | dmu_buf_t *db; | |
1406 | znode_phys_t *pzp; | |
34dc7c2f BB |
1407 | |
1408 | /* | |
1409 | * First attempt to create master node. | |
1410 | */ | |
1411 | /* | |
1412 | * In an empty objset, there are no blocks to read and thus | |
1413 | * there can be no i/o errors (which we assert below). | |
1414 | */ | |
1415 | moid = MASTER_NODE_OBJ; | |
1416 | error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE, | |
1417 | DMU_OT_NONE, 0, tx); | |
1418 | ASSERT(error == 0); | |
1419 | ||
1420 | /* | |
1421 | * Set starting attributes. | |
1422 | */ | |
428870ff | 1423 | version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os))); |
34dc7c2f BB |
1424 | elem = NULL; |
1425 | while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) { | |
1426 | /* For the moment we expect all zpl props to be uint64_ts */ | |
1427 | uint64_t val; | |
1428 | char *name; | |
1429 | ||
1430 | ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64); | |
1431 | VERIFY(nvpair_value_uint64(elem, &val) == 0); | |
1432 | name = nvpair_name(elem); | |
1433 | if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) { | |
9babb374 BB |
1434 | if (val < version) |
1435 | version = val; | |
34dc7c2f BB |
1436 | } else { |
1437 | error = zap_update(os, moid, name, 8, 1, &val, tx); | |
1438 | } | |
1439 | ASSERT(error == 0); | |
1440 | if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0) | |
1441 | norm = val; | |
34dc7c2f BB |
1442 | } |
1443 | ASSERT(version != 0); | |
9babb374 | 1444 | error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx); |
34dc7c2f | 1445 | |
428870ff BB |
1446 | /* |
1447 | * Create zap object used for SA attribute registration | |
1448 | */ | |
1449 | ||
1450 | if (version >= ZPL_VERSION_SA) { | |
1451 | sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE, | |
1452 | DMU_OT_NONE, 0, tx); | |
1453 | error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx); | |
1454 | ASSERT(error == 0); | |
1455 | } else { | |
1456 | sa_obj = 0; | |
1457 | } | |
34dc7c2f BB |
1458 | /* |
1459 | * Create a delete queue. | |
1460 | */ | |
9babb374 | 1461 | obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx); |
34dc7c2f | 1462 | |
9babb374 | 1463 | error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx); |
34dc7c2f BB |
1464 | ASSERT(error == 0); |
1465 | ||
9babb374 | 1466 | /* |
eb28321e BB |
1467 | * Create root znode with code free of VFS dependencies. This |
1468 | * is important because without a registered filesystem and super | |
1469 | * block all the required VFS hooks will be missing. The critical | |
1470 | * thing is to just crete the required root znode. | |
9babb374 | 1471 | */ |
60101509 BB |
1472 | obj = zap_create_norm(os, norm, DMU_OT_DIRECTORY_CONTENTS, |
1473 | DMU_OT_ZNODE, sizeof (znode_phys_t), tx); | |
9babb374 | 1474 | |
60101509 BB |
1475 | VERIFY(0 == dmu_bonus_hold(os, obj, FTAG, &db)); |
1476 | dmu_buf_will_dirty(db, tx); | |
9babb374 | 1477 | |
60101509 BB |
1478 | /* |
1479 | * Initialize the znode physical data to zero. | |
1480 | */ | |
1481 | ASSERT(db->db_size >= sizeof (znode_phys_t)); | |
1482 | bzero(db->db_data, db->db_size); | |
1483 | pzp = db->db_data; | |
1484 | ||
1485 | if (USE_FUIDS(version, os)) | |
1486 | pzp->zp_flags = ZFS_ARCHIVE | ZFS_AV_MODIFIED; | |
1487 | ||
1488 | pzp->zp_size = 2; /* "." and ".." */ | |
1489 | pzp->zp_links = 2; | |
1490 | pzp->zp_parent = obj; | |
1491 | pzp->zp_gen = dmu_tx_get_txg(tx); | |
1492 | pzp->zp_mode = S_IFDIR | 0755; | |
1493 | pzp->zp_flags = ZFS_ACL_TRIVIAL; | |
1494 | ||
1495 | gethrestime(&now); | |
1496 | ||
1497 | ZFS_TIME_ENCODE(&now, pzp->zp_crtime); | |
1498 | ZFS_TIME_ENCODE(&now, pzp->zp_ctime); | |
1499 | ZFS_TIME_ENCODE(&now, pzp->zp_atime); | |
1500 | ZFS_TIME_ENCODE(&now, pzp->zp_mtime); | |
1501 | ||
1502 | error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &obj, tx); | |
9babb374 | 1503 | ASSERT(error == 0); |
428870ff | 1504 | |
60101509 | 1505 | dmu_buf_rele(db, FTAG); |
34dc7c2f BB |
1506 | } |
1507 | ||
1508 | #endif /* _KERNEL */ | |
428870ff | 1509 | |
34dc7c2f | 1510 | static int |
572e2857 BB |
1511 | zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table) |
1512 | { | |
1513 | uint64_t sa_obj = 0; | |
1514 | int error; | |
1515 | ||
1516 | error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj); | |
1517 | if (error != 0 && error != ENOENT) | |
1518 | return (error); | |
1519 | ||
1520 | error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table); | |
1521 | return (error); | |
1522 | } | |
1523 | ||
1524 | static int | |
1525 | zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp, | |
1526 | dmu_buf_t **db) | |
34dc7c2f | 1527 | { |
34dc7c2f | 1528 | dmu_object_info_t doi; |
34dc7c2f | 1529 | int error; |
428870ff | 1530 | |
572e2857 | 1531 | if ((error = sa_buf_hold(osp, obj, FTAG, db)) != 0) |
34dc7c2f BB |
1532 | return (error); |
1533 | ||
572e2857 | 1534 | dmu_object_info_from_db(*db, &doi); |
428870ff BB |
1535 | if ((doi.doi_bonus_type != DMU_OT_SA && |
1536 | doi.doi_bonus_type != DMU_OT_ZNODE) || | |
d6320ddb BB |
1537 | (doi.doi_bonus_type == DMU_OT_ZNODE && |
1538 | doi.doi_bonus_size < sizeof (znode_phys_t))) { | |
572e2857 BB |
1539 | sa_buf_rele(*db, FTAG); |
1540 | return (ENOTSUP); | |
34dc7c2f BB |
1541 | } |
1542 | ||
572e2857 BB |
1543 | error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp); |
1544 | if (error != 0) { | |
1545 | sa_buf_rele(*db, FTAG); | |
428870ff BB |
1546 | return (error); |
1547 | } | |
1548 | ||
572e2857 BB |
1549 | return (0); |
1550 | } | |
1551 | ||
1552 | void | |
1553 | zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db) | |
1554 | { | |
1555 | sa_handle_destroy(hdl); | |
1556 | sa_buf_rele(db, FTAG); | |
1557 | } | |
1558 | ||
1559 | /* | |
1560 | * Given an object number, return its parent object number and whether | |
1561 | * or not the object is an extended attribute directory. | |
1562 | */ | |
1563 | static int | |
1564 | zfs_obj_to_pobj(sa_handle_t *hdl, sa_attr_type_t *sa_table, uint64_t *pobjp, | |
1565 | int *is_xattrdir) | |
1566 | { | |
1567 | uint64_t parent; | |
1568 | uint64_t pflags; | |
1569 | uint64_t mode; | |
1570 | sa_bulk_attr_t bulk[3]; | |
1571 | int count = 0; | |
1572 | int error; | |
1573 | ||
1574 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL, | |
1575 | &parent, sizeof (parent)); | |
428870ff | 1576 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL, |
572e2857 | 1577 | &pflags, sizeof (pflags)); |
428870ff | 1578 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL, |
572e2857 | 1579 | &mode, sizeof (mode)); |
428870ff | 1580 | |
572e2857 | 1581 | if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0) |
428870ff | 1582 | return (error); |
572e2857 | 1583 | |
428870ff BB |
1584 | *pobjp = parent; |
1585 | *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode); | |
34dc7c2f BB |
1586 | |
1587 | return (0); | |
1588 | } | |
1589 | ||
572e2857 BB |
1590 | /* |
1591 | * Given an object number, return some zpl level statistics | |
1592 | */ | |
1593 | static int | |
1594 | zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table, | |
1595 | zfs_stat_t *sb) | |
34dc7c2f | 1596 | { |
572e2857 BB |
1597 | sa_bulk_attr_t bulk[4]; |
1598 | int count = 0; | |
1599 | ||
1600 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL, | |
1601 | &sb->zs_mode, sizeof (sb->zs_mode)); | |
1602 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL, | |
1603 | &sb->zs_gen, sizeof (sb->zs_gen)); | |
1604 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL, | |
1605 | &sb->zs_links, sizeof (sb->zs_links)); | |
1606 | SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL, | |
1607 | &sb->zs_ctime, sizeof (sb->zs_ctime)); | |
1608 | ||
1609 | return (sa_bulk_lookup(hdl, bulk, count)); | |
1610 | } | |
1611 | ||
1612 | static int | |
1613 | zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl, | |
1614 | sa_attr_type_t *sa_table, char *buf, int len) | |
1615 | { | |
1616 | sa_handle_t *sa_hdl; | |
1617 | sa_handle_t *prevhdl = NULL; | |
1618 | dmu_buf_t *prevdb = NULL; | |
1619 | dmu_buf_t *sa_db = NULL; | |
34dc7c2f BB |
1620 | char *path = buf + len - 1; |
1621 | int error; | |
1622 | ||
1623 | *path = '\0'; | |
572e2857 | 1624 | sa_hdl = hdl; |
428870ff | 1625 | |
34dc7c2f BB |
1626 | for (;;) { |
1627 | uint64_t pobj; | |
1628 | char component[MAXNAMELEN + 2]; | |
1629 | size_t complen; | |
1630 | int is_xattrdir; | |
1631 | ||
572e2857 BB |
1632 | if (prevdb) |
1633 | zfs_release_sa_handle(prevhdl, prevdb); | |
1634 | ||
1635 | if ((error = zfs_obj_to_pobj(sa_hdl, sa_table, &pobj, | |
1636 | &is_xattrdir)) != 0) | |
34dc7c2f BB |
1637 | break; |
1638 | ||
1639 | if (pobj == obj) { | |
1640 | if (path[0] != '/') | |
1641 | *--path = '/'; | |
1642 | break; | |
1643 | } | |
1644 | ||
1645 | component[0] = '/'; | |
1646 | if (is_xattrdir) { | |
1647 | (void) sprintf(component + 1, "<xattrdir>"); | |
1648 | } else { | |
1649 | error = zap_value_search(osp, pobj, obj, | |
1650 | ZFS_DIRENT_OBJ(-1ULL), component + 1); | |
1651 | if (error != 0) | |
1652 | break; | |
1653 | } | |
1654 | ||
1655 | complen = strlen(component); | |
1656 | path -= complen; | |
1657 | ASSERT(path >= buf); | |
1658 | bcopy(component, path, complen); | |
1659 | obj = pobj; | |
572e2857 BB |
1660 | |
1661 | if (sa_hdl != hdl) { | |
1662 | prevhdl = sa_hdl; | |
1663 | prevdb = sa_db; | |
1664 | } | |
1665 | error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db); | |
1666 | if (error != 0) { | |
1667 | sa_hdl = prevhdl; | |
1668 | sa_db = prevdb; | |
1669 | break; | |
1670 | } | |
1671 | } | |
1672 | ||
1673 | if (sa_hdl != NULL && sa_hdl != hdl) { | |
1674 | ASSERT(sa_db != NULL); | |
1675 | zfs_release_sa_handle(sa_hdl, sa_db); | |
34dc7c2f BB |
1676 | } |
1677 | ||
1678 | if (error == 0) | |
1679 | (void) memmove(buf, path, buf + len - path); | |
428870ff | 1680 | |
34dc7c2f BB |
1681 | return (error); |
1682 | } | |
572e2857 BB |
1683 | |
1684 | int | |
1685 | zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len) | |
1686 | { | |
1687 | sa_attr_type_t *sa_table; | |
1688 | sa_handle_t *hdl; | |
1689 | dmu_buf_t *db; | |
1690 | int error; | |
1691 | ||
1692 | error = zfs_sa_setup(osp, &sa_table); | |
1693 | if (error != 0) | |
1694 | return (error); | |
1695 | ||
1696 | error = zfs_grab_sa_handle(osp, obj, &hdl, &db); | |
1697 | if (error != 0) | |
1698 | return (error); | |
1699 | ||
1700 | error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len); | |
1701 | ||
1702 | zfs_release_sa_handle(hdl, db); | |
1703 | return (error); | |
1704 | } | |
1705 | ||
1706 | int | |
1707 | zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb, | |
1708 | char *buf, int len) | |
1709 | { | |
1710 | char *path = buf + len - 1; | |
1711 | sa_attr_type_t *sa_table; | |
1712 | sa_handle_t *hdl; | |
1713 | dmu_buf_t *db; | |
1714 | int error; | |
1715 | ||
1716 | *path = '\0'; | |
1717 | ||
1718 | error = zfs_sa_setup(osp, &sa_table); | |
1719 | if (error != 0) | |
1720 | return (error); | |
1721 | ||
1722 | error = zfs_grab_sa_handle(osp, obj, &hdl, &db); | |
1723 | if (error != 0) | |
1724 | return (error); | |
1725 | ||
1726 | error = zfs_obj_to_stats_impl(hdl, sa_table, sb); | |
1727 | if (error != 0) { | |
1728 | zfs_release_sa_handle(hdl, db); | |
1729 | return (error); | |
1730 | } | |
1731 | ||
1732 | error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len); | |
1733 | ||
1734 | zfs_release_sa_handle(hdl, db); | |
1735 | return (error); | |
1736 | } | |
c28b2279 BB |
1737 | |
1738 | #if defined(_KERNEL) && defined(HAVE_SPL) | |
1739 | EXPORT_SYMBOL(zfs_create_fs); | |
1740 | EXPORT_SYMBOL(zfs_obj_to_path); | |
1741 | #endif |