Print this page
6385 Fix unlocking order in zfs_zget
Reviewed by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed by: Dan Kimmel <dan.kimmel@delphix.com>
Reviewed by: Andriy Gapon <avg@freebsd.org>
Approved by: Robert Mustacchi <rm@joyent.com>
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/fs/zfs/zfs_znode.c
+++ new/usr/src/uts/common/fs/zfs/zfs_znode.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24 24 */
25 25
26 26 /* Portions Copyright 2007 Jeremy Teo */
27 27
28 28 #ifdef _KERNEL
29 29 #include <sys/types.h>
30 30 #include <sys/param.h>
31 31 #include <sys/time.h>
32 32 #include <sys/systm.h>
33 33 #include <sys/sysmacros.h>
34 34 #include <sys/resource.h>
35 35 #include <sys/mntent.h>
36 36 #include <sys/mkdev.h>
37 37 #include <sys/u8_textprep.h>
38 38 #include <sys/dsl_dataset.h>
39 39 #include <sys/vfs.h>
40 40 #include <sys/vfs_opreg.h>
41 41 #include <sys/vnode.h>
42 42 #include <sys/file.h>
43 43 #include <sys/kmem.h>
44 44 #include <sys/errno.h>
45 45 #include <sys/unistd.h>
46 46 #include <sys/mode.h>
47 47 #include <sys/atomic.h>
48 48 #include <vm/pvn.h>
49 49 #include "fs/fs_subr.h"
50 50 #include <sys/zfs_dir.h>
51 51 #include <sys/zfs_acl.h>
52 52 #include <sys/zfs_ioctl.h>
53 53 #include <sys/zfs_rlock.h>
54 54 #include <sys/zfs_fuid.h>
55 55 #include <sys/dnode.h>
56 56 #include <sys/fs/zfs.h>
57 57 #include <sys/kidmap.h>
58 58 #endif /* _KERNEL */
59 59
60 60 #include <sys/dmu.h>
61 61 #include <sys/dmu_objset.h>
62 62 #include <sys/refcount.h>
63 63 #include <sys/stat.h>
64 64 #include <sys/zap.h>
65 65 #include <sys/zfs_znode.h>
66 66 #include <sys/sa.h>
67 67 #include <sys/zfs_sa.h>
68 68 #include <sys/zfs_stat.h>
69 69
70 70 #include "zfs_prop.h"
71 71 #include "zfs_comutil.h"
72 72
73 73 /*
74 74 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
75 75 * turned on when DEBUG is also defined.
76 76 */
77 77 #ifdef DEBUG
78 78 #define ZNODE_STATS
79 79 #endif /* DEBUG */
80 80
81 81 #ifdef ZNODE_STATS
82 82 #define ZNODE_STAT_ADD(stat) ((stat)++)
83 83 #else
84 84 #define ZNODE_STAT_ADD(stat) /* nothing */
85 85 #endif /* ZNODE_STATS */
86 86
87 87 /*
88 88 * Functions needed for userland (ie: libzpool) are not put under
89 89 * #ifdef_KERNEL; the rest of the functions have dependencies
90 90 * (such as VFS logic) that will not compile easily in userland.
91 91 */
92 92 #ifdef _KERNEL
93 93 /*
94 94 * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
95 95 * be freed before it can be safely accessed.
96 96 */
97 97 krwlock_t zfsvfs_lock;
98 98
99 99 static kmem_cache_t *znode_cache = NULL;
100 100
101 101 /*ARGSUSED*/
102 102 static void
103 103 znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
104 104 {
105 105 /*
106 106 * We should never drop all dbuf refs without first clearing
107 107 * the eviction callback.
108 108 */
109 109 panic("evicting znode %p\n", user_ptr);
110 110 }
111 111
112 112 /*ARGSUSED*/
113 113 static int
114 114 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
115 115 {
116 116 znode_t *zp = buf;
117 117
118 118 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
119 119
120 120 zp->z_vnode = vn_alloc(kmflags);
121 121 if (zp->z_vnode == NULL) {
122 122 return (-1);
123 123 }
124 124 ZTOV(zp)->v_data = zp;
125 125
126 126 list_link_init(&zp->z_link_node);
127 127
128 128 mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
129 129 rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
130 130 rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
131 131 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
132 132
133 133 mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
134 134 avl_create(&zp->z_range_avl, zfs_range_compare,
135 135 sizeof (rl_t), offsetof(rl_t, r_node));
136 136
137 137 zp->z_dirlocks = NULL;
138 138 zp->z_acl_cached = NULL;
139 139 zp->z_moved = 0;
140 140 return (0);
141 141 }
142 142
143 143 /*ARGSUSED*/
144 144 static void
145 145 zfs_znode_cache_destructor(void *buf, void *arg)
146 146 {
147 147 znode_t *zp = buf;
148 148
149 149 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
150 150 ASSERT(ZTOV(zp)->v_data == zp);
151 151 vn_free(ZTOV(zp));
152 152 ASSERT(!list_link_active(&zp->z_link_node));
153 153 mutex_destroy(&zp->z_lock);
154 154 rw_destroy(&zp->z_parent_lock);
155 155 rw_destroy(&zp->z_name_lock);
156 156 mutex_destroy(&zp->z_acl_lock);
157 157 avl_destroy(&zp->z_range_avl);
158 158 mutex_destroy(&zp->z_range_lock);
159 159
160 160 ASSERT(zp->z_dirlocks == NULL);
161 161 ASSERT(zp->z_acl_cached == NULL);
162 162 }
163 163
164 164 #ifdef ZNODE_STATS
165 165 static struct {
166 166 uint64_t zms_zfsvfs_invalid;
167 167 uint64_t zms_zfsvfs_recheck1;
168 168 uint64_t zms_zfsvfs_unmounted;
169 169 uint64_t zms_zfsvfs_recheck2;
170 170 uint64_t zms_obj_held;
171 171 uint64_t zms_vnode_locked;
172 172 uint64_t zms_not_only_dnlc;
173 173 } znode_move_stats;
174 174 #endif /* ZNODE_STATS */
175 175
176 176 static void
177 177 zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
178 178 {
179 179 vnode_t *vp;
180 180
181 181 /* Copy fields. */
182 182 nzp->z_zfsvfs = ozp->z_zfsvfs;
183 183
184 184 /* Swap vnodes. */
185 185 vp = nzp->z_vnode;
186 186 nzp->z_vnode = ozp->z_vnode;
187 187 ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
188 188 ZTOV(ozp)->v_data = ozp;
189 189 ZTOV(nzp)->v_data = nzp;
190 190
191 191 nzp->z_id = ozp->z_id;
192 192 ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
193 193 ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
194 194 nzp->z_unlinked = ozp->z_unlinked;
195 195 nzp->z_atime_dirty = ozp->z_atime_dirty;
196 196 nzp->z_zn_prefetch = ozp->z_zn_prefetch;
197 197 nzp->z_blksz = ozp->z_blksz;
198 198 nzp->z_seq = ozp->z_seq;
199 199 nzp->z_mapcnt = ozp->z_mapcnt;
200 200 nzp->z_gen = ozp->z_gen;
201 201 nzp->z_sync_cnt = ozp->z_sync_cnt;
202 202 nzp->z_is_sa = ozp->z_is_sa;
203 203 nzp->z_sa_hdl = ozp->z_sa_hdl;
204 204 bcopy(ozp->z_atime, nzp->z_atime, sizeof (uint64_t) * 2);
205 205 nzp->z_links = ozp->z_links;
206 206 nzp->z_size = ozp->z_size;
207 207 nzp->z_pflags = ozp->z_pflags;
208 208 nzp->z_uid = ozp->z_uid;
209 209 nzp->z_gid = ozp->z_gid;
210 210 nzp->z_mode = ozp->z_mode;
211 211
212 212 /*
213 213 * Since this is just an idle znode and kmem is already dealing with
214 214 * memory pressure, release any cached ACL.
215 215 */
216 216 if (ozp->z_acl_cached) {
217 217 zfs_acl_free(ozp->z_acl_cached);
218 218 ozp->z_acl_cached = NULL;
219 219 }
220 220
221 221 sa_set_userp(nzp->z_sa_hdl, nzp);
222 222
223 223 /*
224 224 * Invalidate the original znode by clearing fields that provide a
225 225 * pointer back to the znode. Set the low bit of the vfs pointer to
226 226 * ensure that zfs_znode_move() recognizes the znode as invalid in any
227 227 * subsequent callback.
228 228 */
229 229 ozp->z_sa_hdl = NULL;
230 230 POINTER_INVALIDATE(&ozp->z_zfsvfs);
231 231
232 232 /*
233 233 * Mark the znode.
234 234 */
235 235 nzp->z_moved = 1;
236 236 ozp->z_moved = (uint8_t)-1;
237 237 }
238 238
239 239 /*ARGSUSED*/
240 240 static kmem_cbrc_t
241 241 zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
242 242 {
243 243 znode_t *ozp = buf, *nzp = newbuf;
244 244 zfsvfs_t *zfsvfs;
245 245 vnode_t *vp;
246 246
247 247 /*
248 248 * The znode is on the file system's list of known znodes if the vfs
249 249 * pointer is valid. We set the low bit of the vfs pointer when freeing
250 250 * the znode to invalidate it, and the memory patterns written by kmem
251 251 * (baddcafe and deadbeef) set at least one of the two low bits. A newly
252 252 * created znode sets the vfs pointer last of all to indicate that the
253 253 * znode is known and in a valid state to be moved by this function.
254 254 */
255 255 zfsvfs = ozp->z_zfsvfs;
256 256 if (!POINTER_IS_VALID(zfsvfs)) {
257 257 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
258 258 return (KMEM_CBRC_DONT_KNOW);
259 259 }
260 260
261 261 /*
262 262 * Close a small window in which it's possible that the filesystem could
263 263 * be unmounted and freed, and zfsvfs, though valid in the previous
264 264 * statement, could point to unrelated memory by the time we try to
265 265 * prevent the filesystem from being unmounted.
266 266 */
267 267 rw_enter(&zfsvfs_lock, RW_WRITER);
268 268 if (zfsvfs != ozp->z_zfsvfs) {
269 269 rw_exit(&zfsvfs_lock);
270 270 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1);
271 271 return (KMEM_CBRC_DONT_KNOW);
272 272 }
273 273
274 274 /*
275 275 * If the znode is still valid, then so is the file system. We know that
276 276 * no valid file system can be freed while we hold zfsvfs_lock, so we
277 277 * can safely ensure that the filesystem is not and will not be
278 278 * unmounted. The next statement is equivalent to ZFS_ENTER().
279 279 */
280 280 rrm_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG);
281 281 if (zfsvfs->z_unmounted) {
282 282 ZFS_EXIT(zfsvfs);
283 283 rw_exit(&zfsvfs_lock);
284 284 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
285 285 return (KMEM_CBRC_DONT_KNOW);
286 286 }
287 287 rw_exit(&zfsvfs_lock);
288 288
289 289 mutex_enter(&zfsvfs->z_znodes_lock);
290 290 /*
291 291 * Recheck the vfs pointer in case the znode was removed just before
292 292 * acquiring the lock.
293 293 */
294 294 if (zfsvfs != ozp->z_zfsvfs) {
295 295 mutex_exit(&zfsvfs->z_znodes_lock);
296 296 ZFS_EXIT(zfsvfs);
297 297 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2);
298 298 return (KMEM_CBRC_DONT_KNOW);
299 299 }
300 300
301 301 /*
302 302 * At this point we know that as long as we hold z_znodes_lock, the
303 303 * znode cannot be freed and fields within the znode can be safely
304 304 * accessed. Now, prevent a race with zfs_zget().
305 305 */
306 306 if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
307 307 mutex_exit(&zfsvfs->z_znodes_lock);
308 308 ZFS_EXIT(zfsvfs);
309 309 ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
310 310 return (KMEM_CBRC_LATER);
311 311 }
312 312
313 313 vp = ZTOV(ozp);
314 314 if (mutex_tryenter(&vp->v_lock) == 0) {
315 315 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
316 316 mutex_exit(&zfsvfs->z_znodes_lock);
317 317 ZFS_EXIT(zfsvfs);
318 318 ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
319 319 return (KMEM_CBRC_LATER);
320 320 }
321 321
322 322 /* Only move znodes that are referenced _only_ by the DNLC. */
323 323 if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
324 324 mutex_exit(&vp->v_lock);
325 325 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
326 326 mutex_exit(&zfsvfs->z_znodes_lock);
327 327 ZFS_EXIT(zfsvfs);
328 328 ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
329 329 return (KMEM_CBRC_LATER);
330 330 }
331 331
332 332 /*
333 333 * The znode is known and in a valid state to move. We're holding the
334 334 * locks needed to execute the critical section.
335 335 */
336 336 zfs_znode_move_impl(ozp, nzp);
337 337 mutex_exit(&vp->v_lock);
338 338 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
339 339
340 340 list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
341 341 mutex_exit(&zfsvfs->z_znodes_lock);
342 342 ZFS_EXIT(zfsvfs);
343 343
344 344 return (KMEM_CBRC_YES);
345 345 }
346 346
347 347 void
348 348 zfs_znode_init(void)
349 349 {
350 350 /*
351 351 * Initialize zcache
352 352 */
353 353 rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL);
354 354 ASSERT(znode_cache == NULL);
355 355 znode_cache = kmem_cache_create("zfs_znode_cache",
356 356 sizeof (znode_t), 0, zfs_znode_cache_constructor,
357 357 zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
358 358 kmem_cache_set_move(znode_cache, zfs_znode_move);
359 359 }
360 360
361 361 void
362 362 zfs_znode_fini(void)
363 363 {
364 364 /*
365 365 * Cleanup vfs & vnode ops
366 366 */
367 367 zfs_remove_op_tables();
368 368
369 369 /*
370 370 * Cleanup zcache
371 371 */
372 372 if (znode_cache)
373 373 kmem_cache_destroy(znode_cache);
374 374 znode_cache = NULL;
375 375 rw_destroy(&zfsvfs_lock);
376 376 }
377 377
378 378 struct vnodeops *zfs_dvnodeops;
379 379 struct vnodeops *zfs_fvnodeops;
380 380 struct vnodeops *zfs_symvnodeops;
381 381 struct vnodeops *zfs_xdvnodeops;
382 382 struct vnodeops *zfs_evnodeops;
383 383 struct vnodeops *zfs_sharevnodeops;
384 384
385 385 void
386 386 zfs_remove_op_tables()
387 387 {
388 388 /*
389 389 * Remove vfs ops
390 390 */
391 391 ASSERT(zfsfstype);
392 392 (void) vfs_freevfsops_by_type(zfsfstype);
393 393 zfsfstype = 0;
394 394
395 395 /*
396 396 * Remove vnode ops
397 397 */
398 398 if (zfs_dvnodeops)
399 399 vn_freevnodeops(zfs_dvnodeops);
400 400 if (zfs_fvnodeops)
401 401 vn_freevnodeops(zfs_fvnodeops);
402 402 if (zfs_symvnodeops)
403 403 vn_freevnodeops(zfs_symvnodeops);
404 404 if (zfs_xdvnodeops)
405 405 vn_freevnodeops(zfs_xdvnodeops);
406 406 if (zfs_evnodeops)
407 407 vn_freevnodeops(zfs_evnodeops);
408 408 if (zfs_sharevnodeops)
409 409 vn_freevnodeops(zfs_sharevnodeops);
410 410
411 411 zfs_dvnodeops = NULL;
412 412 zfs_fvnodeops = NULL;
413 413 zfs_symvnodeops = NULL;
414 414 zfs_xdvnodeops = NULL;
415 415 zfs_evnodeops = NULL;
416 416 zfs_sharevnodeops = NULL;
417 417 }
418 418
419 419 extern const fs_operation_def_t zfs_dvnodeops_template[];
420 420 extern const fs_operation_def_t zfs_fvnodeops_template[];
421 421 extern const fs_operation_def_t zfs_xdvnodeops_template[];
422 422 extern const fs_operation_def_t zfs_symvnodeops_template[];
423 423 extern const fs_operation_def_t zfs_evnodeops_template[];
424 424 extern const fs_operation_def_t zfs_sharevnodeops_template[];
425 425
426 426 int
427 427 zfs_create_op_tables()
428 428 {
429 429 int error;
430 430
431 431 /*
432 432 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
433 433 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
434 434 * In this case we just return as the ops vectors are already set up.
435 435 */
436 436 if (zfs_dvnodeops)
437 437 return (0);
438 438
439 439 error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
440 440 &zfs_dvnodeops);
441 441 if (error)
442 442 return (error);
443 443
444 444 error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
445 445 &zfs_fvnodeops);
446 446 if (error)
447 447 return (error);
448 448
449 449 error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
450 450 &zfs_symvnodeops);
451 451 if (error)
452 452 return (error);
453 453
454 454 error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
455 455 &zfs_xdvnodeops);
456 456 if (error)
457 457 return (error);
458 458
459 459 error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
460 460 &zfs_evnodeops);
461 461 if (error)
462 462 return (error);
463 463
464 464 error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template,
465 465 &zfs_sharevnodeops);
466 466
467 467 return (error);
468 468 }
469 469
470 470 int
471 471 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
472 472 {
473 473 zfs_acl_ids_t acl_ids;
474 474 vattr_t vattr;
475 475 znode_t *sharezp;
476 476 vnode_t *vp;
477 477 znode_t *zp;
478 478 int error;
479 479
480 480 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
481 481 vattr.va_type = VDIR;
482 482 vattr.va_mode = S_IFDIR|0555;
483 483 vattr.va_uid = crgetuid(kcred);
484 484 vattr.va_gid = crgetgid(kcred);
485 485
486 486 sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
487 487 ASSERT(!POINTER_IS_VALID(sharezp->z_zfsvfs));
488 488 sharezp->z_moved = 0;
489 489 sharezp->z_unlinked = 0;
490 490 sharezp->z_atime_dirty = 0;
491 491 sharezp->z_zfsvfs = zfsvfs;
492 492 sharezp->z_is_sa = zfsvfs->z_use_sa;
493 493
494 494 vp = ZTOV(sharezp);
495 495 vn_reinit(vp);
496 496 vp->v_type = VDIR;
497 497
498 498 VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
499 499 kcred, NULL, &acl_ids));
500 500 zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
501 501 ASSERT3P(zp, ==, sharezp);
502 502 ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */
503 503 POINTER_INVALIDATE(&sharezp->z_zfsvfs);
504 504 error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
505 505 ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
506 506 zfsvfs->z_shares_dir = sharezp->z_id;
507 507
508 508 zfs_acl_ids_free(&acl_ids);
509 509 ZTOV(sharezp)->v_count = 0;
510 510 sa_handle_destroy(sharezp->z_sa_hdl);
511 511 kmem_cache_free(znode_cache, sharezp);
512 512
513 513 return (error);
514 514 }
515 515
516 516 /*
517 517 * define a couple of values we need available
518 518 * for both 64 and 32 bit environments.
519 519 */
520 520 #ifndef NBITSMINOR64
521 521 #define NBITSMINOR64 32
522 522 #endif
523 523 #ifndef MAXMAJ64
524 524 #define MAXMAJ64 0xffffffffUL
525 525 #endif
526 526 #ifndef MAXMIN64
527 527 #define MAXMIN64 0xffffffffUL
528 528 #endif
529 529
530 530 /*
531 531 * Create special expldev for ZFS private use.
532 532 * Can't use standard expldev since it doesn't do
533 533 * what we want. The standard expldev() takes a
534 534 * dev32_t in LP64 and expands it to a long dev_t.
535 535 * We need an interface that takes a dev32_t in ILP32
536 536 * and expands it to a long dev_t.
537 537 */
538 538 static uint64_t
539 539 zfs_expldev(dev_t dev)
540 540 {
541 541 #ifndef _LP64
542 542 major_t major = (major_t)dev >> NBITSMINOR32 & MAXMAJ32;
543 543 return (((uint64_t)major << NBITSMINOR64) |
544 544 ((minor_t)dev & MAXMIN32));
545 545 #else
546 546 return (dev);
547 547 #endif
548 548 }
549 549
550 550 /*
551 551 * Special cmpldev for ZFS private use.
552 552 * Can't use standard cmpldev since it takes
553 553 * a long dev_t and compresses it to dev32_t in
554 554 * LP64. We need to do a compaction of a long dev_t
555 555 * to a dev32_t in ILP32.
556 556 */
557 557 dev_t
558 558 zfs_cmpldev(uint64_t dev)
559 559 {
560 560 #ifndef _LP64
561 561 minor_t minor = (minor_t)dev & MAXMIN64;
562 562 major_t major = (major_t)(dev >> NBITSMINOR64) & MAXMAJ64;
563 563
564 564 if (major > MAXMAJ32 || minor > MAXMIN32)
565 565 return (NODEV32);
566 566
567 567 return (((dev32_t)major << NBITSMINOR32) | minor);
568 568 #else
569 569 return (dev);
570 570 #endif
571 571 }
572 572
573 573 static void
574 574 zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
575 575 dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
576 576 {
577 577 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
578 578 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
579 579
580 580 mutex_enter(&zp->z_lock);
581 581
582 582 ASSERT(zp->z_sa_hdl == NULL);
583 583 ASSERT(zp->z_acl_cached == NULL);
584 584 if (sa_hdl == NULL) {
585 585 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp,
586 586 SA_HDL_SHARED, &zp->z_sa_hdl));
587 587 } else {
588 588 zp->z_sa_hdl = sa_hdl;
589 589 sa_set_userp(sa_hdl, zp);
590 590 }
591 591
592 592 zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
593 593
594 594 /*
595 595 * Slap on VROOT if we are the root znode
596 596 */
597 597 if (zp->z_id == zfsvfs->z_root)
598 598 ZTOV(zp)->v_flag |= VROOT;
599 599
600 600 mutex_exit(&zp->z_lock);
601 601 vn_exists(ZTOV(zp));
602 602 }
603 603
604 604 void
605 605 zfs_znode_dmu_fini(znode_t *zp)
606 606 {
607 607 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
608 608 zp->z_unlinked ||
609 609 RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
610 610
611 611 sa_handle_destroy(zp->z_sa_hdl);
612 612 zp->z_sa_hdl = NULL;
613 613 }
614 614
615 615 /*
616 616 * Construct a new znode/vnode and intialize.
617 617 *
618 618 * This does not do a call to dmu_set_user() that is
619 619 * up to the caller to do, in case you don't want to
620 620 * return the znode
621 621 */
622 622 static znode_t *
623 623 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
624 624 dmu_object_type_t obj_type, sa_handle_t *hdl)
625 625 {
626 626 znode_t *zp;
627 627 vnode_t *vp;
628 628 uint64_t mode;
629 629 uint64_t parent;
630 630 sa_bulk_attr_t bulk[9];
631 631 int count = 0;
632 632
633 633 zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
634 634
635 635 ASSERT(zp->z_dirlocks == NULL);
636 636 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
637 637 zp->z_moved = 0;
638 638
639 639 /*
640 640 * Defer setting z_zfsvfs until the znode is ready to be a candidate for
641 641 * the zfs_znode_move() callback.
642 642 */
643 643 zp->z_sa_hdl = NULL;
644 644 zp->z_unlinked = 0;
645 645 zp->z_atime_dirty = 0;
646 646 zp->z_mapcnt = 0;
647 647 zp->z_id = db->db_object;
648 648 zp->z_blksz = blksz;
649 649 zp->z_seq = 0x7A4653;
650 650 zp->z_sync_cnt = 0;
651 651
652 652 vp = ZTOV(zp);
653 653 vn_reinit(vp);
654 654
655 655 zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
656 656
657 657 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
658 658 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8);
659 659 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
660 660 &zp->z_size, 8);
661 661 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
662 662 &zp->z_links, 8);
663 663 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
664 664 &zp->z_pflags, 8);
665 665 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
666 666 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
667 667 &zp->z_atime, 16);
668 668 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
669 669 &zp->z_uid, 8);
670 670 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
671 671 &zp->z_gid, 8);
672 672
673 673 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
674 674 if (hdl == NULL)
675 675 sa_handle_destroy(zp->z_sa_hdl);
676 676 kmem_cache_free(znode_cache, zp);
677 677 return (NULL);
678 678 }
679 679
680 680 zp->z_mode = mode;
681 681 vp->v_vfsp = zfsvfs->z_parent->z_vfs;
682 682
683 683 vp->v_type = IFTOVT((mode_t)mode);
684 684
685 685 switch (vp->v_type) {
686 686 case VDIR:
687 687 if (zp->z_pflags & ZFS_XATTR) {
688 688 vn_setops(vp, zfs_xdvnodeops);
689 689 vp->v_flag |= V_XATTRDIR;
690 690 } else {
691 691 vn_setops(vp, zfs_dvnodeops);
692 692 }
693 693 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
694 694 break;
695 695 case VBLK:
696 696 case VCHR:
697 697 {
698 698 uint64_t rdev;
699 699 VERIFY(sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zfsvfs),
700 700 &rdev, sizeof (rdev)) == 0);
701 701
702 702 vp->v_rdev = zfs_cmpldev(rdev);
703 703 }
704 704 /*FALLTHROUGH*/
705 705 case VFIFO:
706 706 case VSOCK:
707 707 case VDOOR:
708 708 vn_setops(vp, zfs_fvnodeops);
709 709 break;
710 710 case VREG:
711 711 vp->v_flag |= VMODSORT;
712 712 if (parent == zfsvfs->z_shares_dir) {
713 713 ASSERT(zp->z_uid == 0 && zp->z_gid == 0);
714 714 vn_setops(vp, zfs_sharevnodeops);
715 715 } else {
716 716 vn_setops(vp, zfs_fvnodeops);
717 717 }
718 718 break;
719 719 case VLNK:
720 720 vn_setops(vp, zfs_symvnodeops);
721 721 break;
722 722 default:
723 723 vn_setops(vp, zfs_evnodeops);
724 724 break;
725 725 }
726 726
727 727 mutex_enter(&zfsvfs->z_znodes_lock);
728 728 list_insert_tail(&zfsvfs->z_all_znodes, zp);
729 729 membar_producer();
730 730 /*
731 731 * Everything else must be valid before assigning z_zfsvfs makes the
732 732 * znode eligible for zfs_znode_move().
733 733 */
734 734 zp->z_zfsvfs = zfsvfs;
735 735 mutex_exit(&zfsvfs->z_znodes_lock);
736 736
737 737 VFS_HOLD(zfsvfs->z_vfs);
738 738 return (zp);
739 739 }
740 740
741 741 static uint64_t empty_xattr;
742 742 static uint64_t pad[4];
743 743 static zfs_acl_phys_t acl_phys;
744 744 /*
745 745 * Create a new DMU object to hold a zfs znode.
746 746 *
747 747 * IN: dzp - parent directory for new znode
748 748 * vap - file attributes for new znode
749 749 * tx - dmu transaction id for zap operations
750 750 * cr - credentials of caller
751 751 * flag - flags:
752 752 * IS_ROOT_NODE - new object will be root
753 753 * IS_XATTR - new object is an attribute
754 754 * bonuslen - length of bonus buffer
755 755 * setaclp - File/Dir initial ACL
756 756 * fuidp - Tracks fuid allocation.
757 757 *
758 758 * OUT: zpp - allocated znode
759 759 *
760 760 */
761 761 void
762 762 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
763 763 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
764 764 {
765 765 uint64_t crtime[2], atime[2], mtime[2], ctime[2];
766 766 uint64_t mode, size, links, parent, pflags;
767 767 uint64_t dzp_pflags = 0;
768 768 uint64_t rdev = 0;
769 769 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
770 770 dmu_buf_t *db;
771 771 timestruc_t now;
772 772 uint64_t gen, obj;
773 773 int bonuslen;
774 774 sa_handle_t *sa_hdl;
775 775 dmu_object_type_t obj_type;
776 776 sa_bulk_attr_t sa_attrs[ZPL_END];
777 777 int cnt = 0;
778 778 zfs_acl_locator_cb_t locate = { 0 };
779 779
780 780 ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
781 781
782 782 if (zfsvfs->z_replay) {
783 783 obj = vap->va_nodeid;
784 784 now = vap->va_ctime; /* see zfs_replay_create() */
785 785 gen = vap->va_nblocks; /* ditto */
786 786 } else {
787 787 obj = 0;
788 788 gethrestime(&now);
789 789 gen = dmu_tx_get_txg(tx);
790 790 }
791 791
792 792 obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
793 793 bonuslen = (obj_type == DMU_OT_SA) ?
794 794 DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
795 795
796 796 /*
797 797 * Create a new DMU object.
798 798 */
799 799 /*
800 800 * There's currently no mechanism for pre-reading the blocks that will
801 801 * be needed to allocate a new object, so we accept the small chance
802 802 * that there will be an i/o error and we will fail one of the
803 803 * assertions below.
804 804 */
805 805 if (vap->va_type == VDIR) {
806 806 if (zfsvfs->z_replay) {
807 807 VERIFY0(zap_create_claim_norm(zfsvfs->z_os, obj,
808 808 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
809 809 obj_type, bonuslen, tx));
810 810 } else {
811 811 obj = zap_create_norm(zfsvfs->z_os,
812 812 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
813 813 obj_type, bonuslen, tx);
814 814 }
815 815 } else {
816 816 if (zfsvfs->z_replay) {
817 817 VERIFY0(dmu_object_claim(zfsvfs->z_os, obj,
818 818 DMU_OT_PLAIN_FILE_CONTENTS, 0,
819 819 obj_type, bonuslen, tx));
820 820 } else {
821 821 obj = dmu_object_alloc(zfsvfs->z_os,
822 822 DMU_OT_PLAIN_FILE_CONTENTS, 0,
823 823 obj_type, bonuslen, tx);
824 824 }
825 825 }
826 826
827 827 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
828 828 VERIFY(0 == sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
829 829
830 830 /*
831 831 * If this is the root, fix up the half-initialized parent pointer
832 832 * to reference the just-allocated physical data area.
833 833 */
834 834 if (flag & IS_ROOT_NODE) {
835 835 dzp->z_id = obj;
836 836 } else {
837 837 dzp_pflags = dzp->z_pflags;
838 838 }
839 839
840 840 /*
841 841 * If parent is an xattr, so am I.
842 842 */
843 843 if (dzp_pflags & ZFS_XATTR) {
844 844 flag |= IS_XATTR;
845 845 }
846 846
847 847 if (zfsvfs->z_use_fuids)
848 848 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
849 849 else
850 850 pflags = 0;
851 851
852 852 if (vap->va_type == VDIR) {
853 853 size = 2; /* contents ("." and "..") */
854 854 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
855 855 } else {
856 856 size = links = 0;
857 857 }
858 858
859 859 if (vap->va_type == VBLK || vap->va_type == VCHR) {
860 860 rdev = zfs_expldev(vap->va_rdev);
861 861 }
862 862
863 863 parent = dzp->z_id;
864 864 mode = acl_ids->z_mode;
865 865 if (flag & IS_XATTR)
866 866 pflags |= ZFS_XATTR;
867 867
868 868 /*
869 869 * No execs denied will be deterimed when zfs_mode_compute() is called.
870 870 */
871 871 pflags |= acl_ids->z_aclp->z_hints &
872 872 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
873 873 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
874 874
875 875 ZFS_TIME_ENCODE(&now, crtime);
876 876 ZFS_TIME_ENCODE(&now, ctime);
877 877
878 878 if (vap->va_mask & AT_ATIME) {
879 879 ZFS_TIME_ENCODE(&vap->va_atime, atime);
880 880 } else {
881 881 ZFS_TIME_ENCODE(&now, atime);
882 882 }
883 883
884 884 if (vap->va_mask & AT_MTIME) {
885 885 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
886 886 } else {
887 887 ZFS_TIME_ENCODE(&now, mtime);
888 888 }
889 889
890 890 /* Now add in all of the "SA" attributes */
891 891 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
892 892 &sa_hdl));
893 893
894 894 /*
895 895 * Setup the array of attributes to be replaced/set on the new file
896 896 *
897 897 * order for DMU_OT_ZNODE is critical since it needs to be constructed
898 898 * in the old znode_phys_t format. Don't change this ordering
899 899 */
900 900
901 901 if (obj_type == DMU_OT_ZNODE) {
902 902 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
903 903 NULL, &atime, 16);
904 904 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
905 905 NULL, &mtime, 16);
906 906 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
907 907 NULL, &ctime, 16);
908 908 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
909 909 NULL, &crtime, 16);
910 910 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
911 911 NULL, &gen, 8);
912 912 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
913 913 NULL, &mode, 8);
914 914 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
915 915 NULL, &size, 8);
916 916 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
917 917 NULL, &parent, 8);
918 918 } else {
919 919 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
920 920 NULL, &mode, 8);
921 921 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
922 922 NULL, &size, 8);
923 923 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
924 924 NULL, &gen, 8);
925 925 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
926 926 &acl_ids->z_fuid, 8);
927 927 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
928 928 &acl_ids->z_fgid, 8);
929 929 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
930 930 NULL, &parent, 8);
931 931 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
932 932 NULL, &pflags, 8);
933 933 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
934 934 NULL, &atime, 16);
935 935 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
936 936 NULL, &mtime, 16);
937 937 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
938 938 NULL, &ctime, 16);
939 939 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
940 940 NULL, &crtime, 16);
941 941 }
942 942
943 943 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
944 944
945 945 if (obj_type == DMU_OT_ZNODE) {
946 946 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
947 947 &empty_xattr, 8);
948 948 }
949 949 if (obj_type == DMU_OT_ZNODE ||
950 950 (vap->va_type == VBLK || vap->va_type == VCHR)) {
951 951 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
952 952 NULL, &rdev, 8);
953 953
954 954 }
955 955 if (obj_type == DMU_OT_ZNODE) {
956 956 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
957 957 NULL, &pflags, 8);
958 958 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
959 959 &acl_ids->z_fuid, 8);
960 960 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
961 961 &acl_ids->z_fgid, 8);
962 962 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
963 963 sizeof (uint64_t) * 4);
964 964 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
965 965 &acl_phys, sizeof (zfs_acl_phys_t));
966 966 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
967 967 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
968 968 &acl_ids->z_aclp->z_acl_count, 8);
969 969 locate.cb_aclp = acl_ids->z_aclp;
970 970 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
971 971 zfs_acl_data_locator, &locate,
972 972 acl_ids->z_aclp->z_acl_bytes);
973 973 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
974 974 acl_ids->z_fuid, acl_ids->z_fgid);
975 975 }
976 976
977 977 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
978 978
979 979 if (!(flag & IS_ROOT_NODE)) {
980 980 *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
981 981 ASSERT(*zpp != NULL);
982 982 } else {
983 983 /*
984 984 * If we are creating the root node, the "parent" we
985 985 * passed in is the znode for the root.
986 986 */
987 987 *zpp = dzp;
988 988
989 989 (*zpp)->z_sa_hdl = sa_hdl;
990 990 }
991 991
992 992 (*zpp)->z_pflags = pflags;
993 993 (*zpp)->z_mode = mode;
994 994
995 995 if (vap->va_mask & AT_XVATTR)
996 996 zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
997 997
998 998 if (obj_type == DMU_OT_ZNODE ||
999 999 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
1000 1000 VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
1001 1001 }
1002 1002 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1003 1003 }
1004 1004
1005 1005 /*
1006 1006 * Update in-core attributes. It is assumed the caller will be doing an
1007 1007 * sa_bulk_update to push the changes out.
1008 1008 */
1009 1009 void
1010 1010 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
1011 1011 {
1012 1012 xoptattr_t *xoap;
1013 1013
1014 1014 xoap = xva_getxoptattr(xvap);
1015 1015 ASSERT(xoap);
1016 1016
1017 1017 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
1018 1018 uint64_t times[2];
1019 1019 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
1020 1020 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
1021 1021 ×, sizeof (times), tx);
1022 1022 XVA_SET_RTN(xvap, XAT_CREATETIME);
1023 1023 }
1024 1024 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
1025 1025 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
1026 1026 zp->z_pflags, tx);
1027 1027 XVA_SET_RTN(xvap, XAT_READONLY);
1028 1028 }
1029 1029 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
1030 1030 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
1031 1031 zp->z_pflags, tx);
1032 1032 XVA_SET_RTN(xvap, XAT_HIDDEN);
1033 1033 }
1034 1034 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
1035 1035 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
1036 1036 zp->z_pflags, tx);
1037 1037 XVA_SET_RTN(xvap, XAT_SYSTEM);
1038 1038 }
1039 1039 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
1040 1040 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
1041 1041 zp->z_pflags, tx);
1042 1042 XVA_SET_RTN(xvap, XAT_ARCHIVE);
1043 1043 }
1044 1044 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
1045 1045 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
1046 1046 zp->z_pflags, tx);
1047 1047 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
1048 1048 }
1049 1049 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
1050 1050 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
1051 1051 zp->z_pflags, tx);
1052 1052 XVA_SET_RTN(xvap, XAT_NOUNLINK);
1053 1053 }
1054 1054 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
1055 1055 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
1056 1056 zp->z_pflags, tx);
1057 1057 XVA_SET_RTN(xvap, XAT_APPENDONLY);
1058 1058 }
1059 1059 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
1060 1060 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
1061 1061 zp->z_pflags, tx);
1062 1062 XVA_SET_RTN(xvap, XAT_NODUMP);
1063 1063 }
1064 1064 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
1065 1065 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
1066 1066 zp->z_pflags, tx);
1067 1067 XVA_SET_RTN(xvap, XAT_OPAQUE);
1068 1068 }
1069 1069 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1070 1070 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
1071 1071 xoap->xoa_av_quarantined, zp->z_pflags, tx);
1072 1072 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
1073 1073 }
1074 1074 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
1075 1075 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
1076 1076 zp->z_pflags, tx);
1077 1077 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
1078 1078 }
1079 1079 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1080 1080 zfs_sa_set_scanstamp(zp, xvap, tx);
1081 1081 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
1082 1082 }
1083 1083 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
1084 1084 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
1085 1085 zp->z_pflags, tx);
1086 1086 XVA_SET_RTN(xvap, XAT_REPARSE);
1087 1087 }
1088 1088 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
1089 1089 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
1090 1090 zp->z_pflags, tx);
1091 1091 XVA_SET_RTN(xvap, XAT_OFFLINE);
1092 1092 }
1093 1093 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1094 1094 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
1095 1095 zp->z_pflags, tx);
1096 1096 XVA_SET_RTN(xvap, XAT_SPARSE);
1097 1097 }
1098 1098 }
1099 1099
1100 1100 int
1101 1101 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
1102 1102 {
1103 1103 dmu_object_info_t doi;
1104 1104 dmu_buf_t *db;
1105 1105 znode_t *zp;
1106 1106 int err;
1107 1107 sa_handle_t *hdl;
1108 1108
1109 1109 *zpp = NULL;
1110 1110
1111 1111 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1112 1112
1113 1113 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1114 1114 if (err) {
1115 1115 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1116 1116 return (err);
1117 1117 }
1118 1118
1119 1119 dmu_object_info_from_db(db, &doi);
1120 1120 if (doi.doi_bonus_type != DMU_OT_SA &&
1121 1121 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1122 1122 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1123 1123 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1124 1124 sa_buf_rele(db, NULL);
1125 1125 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1126 1126 return (SET_ERROR(EINVAL));
1127 1127 }
1128 1128
1129 1129 hdl = dmu_buf_get_user(db);
1130 1130 if (hdl != NULL) {
1131 1131 zp = sa_get_userdata(hdl);
1132 1132
1133 1133
1134 1134 /*
1135 1135 * Since "SA" does immediate eviction we
1136 1136 * should never find a sa handle that doesn't
1137 1137 * know about the znode.
1138 1138 */
1139 1139
1140 1140 ASSERT3P(zp, !=, NULL);
|
↓ open down ↓ |
1140 lines elided |
↑ open up ↑ |
1141 1141
1142 1142 mutex_enter(&zp->z_lock);
1143 1143 ASSERT3U(zp->z_id, ==, obj_num);
1144 1144 if (zp->z_unlinked) {
1145 1145 err = SET_ERROR(ENOENT);
1146 1146 } else {
1147 1147 VN_HOLD(ZTOV(zp));
1148 1148 *zpp = zp;
1149 1149 err = 0;
1150 1150 }
1151 - sa_buf_rele(db, NULL);
1152 1151 mutex_exit(&zp->z_lock);
1152 + sa_buf_rele(db, NULL);
1153 1153 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1154 1154 return (err);
1155 1155 }
1156 1156
1157 1157 /*
1158 1158 * Not found create new znode/vnode
1159 1159 * but only if file exists.
1160 1160 *
1161 1161 * There is a small window where zfs_vget() could
1162 1162 * find this object while a file create is still in
1163 1163 * progress. This is checked for in zfs_znode_alloc()
1164 1164 *
1165 1165 * if zfs_znode_alloc() fails it will drop the hold on the
1166 1166 * bonus buffer.
1167 1167 */
1168 1168 zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
1169 1169 doi.doi_bonus_type, NULL);
1170 1170 if (zp == NULL) {
1171 1171 err = SET_ERROR(ENOENT);
1172 1172 } else {
1173 1173 *zpp = zp;
1174 1174 }
1175 1175 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1176 1176 return (err);
1177 1177 }
1178 1178
1179 1179 int
1180 1180 zfs_rezget(znode_t *zp)
1181 1181 {
1182 1182 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1183 1183 dmu_object_info_t doi;
1184 1184 dmu_buf_t *db;
1185 1185 uint64_t obj_num = zp->z_id;
1186 1186 uint64_t mode;
1187 1187 sa_bulk_attr_t bulk[8];
1188 1188 int err;
1189 1189 int count = 0;
1190 1190 uint64_t gen;
1191 1191
1192 1192 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1193 1193
1194 1194 mutex_enter(&zp->z_acl_lock);
1195 1195 if (zp->z_acl_cached) {
1196 1196 zfs_acl_free(zp->z_acl_cached);
1197 1197 zp->z_acl_cached = NULL;
1198 1198 }
1199 1199
1200 1200 mutex_exit(&zp->z_acl_lock);
1201 1201 ASSERT(zp->z_sa_hdl == NULL);
1202 1202 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1203 1203 if (err) {
1204 1204 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1205 1205 return (err);
1206 1206 }
1207 1207
1208 1208 dmu_object_info_from_db(db, &doi);
1209 1209 if (doi.doi_bonus_type != DMU_OT_SA &&
1210 1210 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1211 1211 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1212 1212 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1213 1213 sa_buf_rele(db, NULL);
1214 1214 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1215 1215 return (SET_ERROR(EINVAL));
1216 1216 }
1217 1217
1218 1218 zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
1219 1219
1220 1220 /* reload cached values */
1221 1221 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1222 1222 &gen, sizeof (gen));
1223 1223 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
1224 1224 &zp->z_size, sizeof (zp->z_size));
1225 1225 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
1226 1226 &zp->z_links, sizeof (zp->z_links));
1227 1227 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1228 1228 &zp->z_pflags, sizeof (zp->z_pflags));
1229 1229 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1230 1230 &zp->z_atime, sizeof (zp->z_atime));
1231 1231 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1232 1232 &zp->z_uid, sizeof (zp->z_uid));
1233 1233 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1234 1234 &zp->z_gid, sizeof (zp->z_gid));
1235 1235 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1236 1236 &mode, sizeof (mode));
1237 1237
1238 1238 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1239 1239 zfs_znode_dmu_fini(zp);
1240 1240 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1241 1241 return (SET_ERROR(EIO));
1242 1242 }
1243 1243
1244 1244 zp->z_mode = mode;
1245 1245
1246 1246 if (gen != zp->z_gen) {
1247 1247 zfs_znode_dmu_fini(zp);
1248 1248 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1249 1249 return (SET_ERROR(EIO));
1250 1250 }
1251 1251
1252 1252 zp->z_unlinked = (zp->z_links == 0);
1253 1253 zp->z_blksz = doi.doi_data_block_size;
1254 1254
1255 1255 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1256 1256
1257 1257 return (0);
1258 1258 }
1259 1259
1260 1260 void
1261 1261 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1262 1262 {
1263 1263 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1264 1264 objset_t *os = zfsvfs->z_os;
1265 1265 uint64_t obj = zp->z_id;
1266 1266 uint64_t acl_obj = zfs_external_acl(zp);
1267 1267
1268 1268 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1269 1269 if (acl_obj) {
1270 1270 VERIFY(!zp->z_is_sa);
1271 1271 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1272 1272 }
1273 1273 VERIFY(0 == dmu_object_free(os, obj, tx));
1274 1274 zfs_znode_dmu_fini(zp);
1275 1275 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1276 1276 zfs_znode_free(zp);
1277 1277 }
1278 1278
1279 1279 void
1280 1280 zfs_zinactive(znode_t *zp)
1281 1281 {
1282 1282 vnode_t *vp = ZTOV(zp);
1283 1283 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1284 1284 uint64_t z_id = zp->z_id;
1285 1285
1286 1286 ASSERT(zp->z_sa_hdl);
1287 1287
1288 1288 /*
1289 1289 * Don't allow a zfs_zget() while were trying to release this znode
1290 1290 */
1291 1291 ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1292 1292
1293 1293 mutex_enter(&zp->z_lock);
1294 1294 mutex_enter(&vp->v_lock);
1295 1295 vp->v_count--;
1296 1296 if (vp->v_count > 0 || vn_has_cached_data(vp)) {
1297 1297 /*
1298 1298 * If the hold count is greater than zero, somebody has
1299 1299 * obtained a new reference on this znode while we were
1300 1300 * processing it here, so we are done. If we still have
1301 1301 * mapped pages then we are also done, since we don't
1302 1302 * want to inactivate the znode until the pages get pushed.
1303 1303 *
1304 1304 * XXX - if vn_has_cached_data(vp) is true, but count == 0,
1305 1305 * this seems like it would leave the znode hanging with
1306 1306 * no chance to go inactive...
1307 1307 */
1308 1308 mutex_exit(&vp->v_lock);
1309 1309 mutex_exit(&zp->z_lock);
1310 1310 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1311 1311 return;
1312 1312 }
1313 1313 mutex_exit(&vp->v_lock);
1314 1314
1315 1315 /*
1316 1316 * If this was the last reference to a file with no links,
1317 1317 * remove the file from the file system.
1318 1318 */
1319 1319 if (zp->z_unlinked) {
1320 1320 mutex_exit(&zp->z_lock);
1321 1321 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1322 1322 zfs_rmnode(zp);
1323 1323 return;
1324 1324 }
1325 1325
1326 1326 mutex_exit(&zp->z_lock);
1327 1327 zfs_znode_dmu_fini(zp);
1328 1328 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1329 1329 zfs_znode_free(zp);
1330 1330 }
1331 1331
1332 1332 void
1333 1333 zfs_znode_free(znode_t *zp)
1334 1334 {
1335 1335 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1336 1336
1337 1337 vn_invalid(ZTOV(zp));
1338 1338
1339 1339 ASSERT(ZTOV(zp)->v_count == 0);
1340 1340
1341 1341 mutex_enter(&zfsvfs->z_znodes_lock);
1342 1342 POINTER_INVALIDATE(&zp->z_zfsvfs);
1343 1343 list_remove(&zfsvfs->z_all_znodes, zp);
1344 1344 mutex_exit(&zfsvfs->z_znodes_lock);
1345 1345
1346 1346 if (zp->z_acl_cached) {
1347 1347 zfs_acl_free(zp->z_acl_cached);
1348 1348 zp->z_acl_cached = NULL;
1349 1349 }
1350 1350
1351 1351 kmem_cache_free(znode_cache, zp);
1352 1352
1353 1353 VFS_RELE(zfsvfs->z_vfs);
1354 1354 }
1355 1355
1356 1356 void
1357 1357 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1358 1358 uint64_t ctime[2], boolean_t have_tx)
1359 1359 {
1360 1360 timestruc_t now;
1361 1361
1362 1362 gethrestime(&now);
1363 1363
1364 1364 if (have_tx) { /* will sa_bulk_update happen really soon? */
1365 1365 zp->z_atime_dirty = 0;
1366 1366 zp->z_seq++;
1367 1367 } else {
1368 1368 zp->z_atime_dirty = 1;
1369 1369 }
1370 1370
1371 1371 if (flag & AT_ATIME) {
1372 1372 ZFS_TIME_ENCODE(&now, zp->z_atime);
1373 1373 }
1374 1374
1375 1375 if (flag & AT_MTIME) {
1376 1376 ZFS_TIME_ENCODE(&now, mtime);
1377 1377 if (zp->z_zfsvfs->z_use_fuids) {
1378 1378 zp->z_pflags |= (ZFS_ARCHIVE |
1379 1379 ZFS_AV_MODIFIED);
1380 1380 }
1381 1381 }
1382 1382
1383 1383 if (flag & AT_CTIME) {
1384 1384 ZFS_TIME_ENCODE(&now, ctime);
1385 1385 if (zp->z_zfsvfs->z_use_fuids)
1386 1386 zp->z_pflags |= ZFS_ARCHIVE;
1387 1387 }
1388 1388 }
1389 1389
1390 1390 /*
1391 1391 * Grow the block size for a file.
1392 1392 *
1393 1393 * IN: zp - znode of file to free data in.
1394 1394 * size - requested block size
1395 1395 * tx - open transaction.
1396 1396 *
1397 1397 * NOTE: this function assumes that the znode is write locked.
1398 1398 */
1399 1399 void
1400 1400 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1401 1401 {
1402 1402 int error;
1403 1403 u_longlong_t dummy;
1404 1404
1405 1405 if (size <= zp->z_blksz)
1406 1406 return;
1407 1407 /*
1408 1408 * If the file size is already greater than the current blocksize,
1409 1409 * we will not grow. If there is more than one block in a file,
1410 1410 * the blocksize cannot change.
1411 1411 */
1412 1412 if (zp->z_blksz && zp->z_size > zp->z_blksz)
1413 1413 return;
1414 1414
1415 1415 error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1416 1416 size, 0, tx);
1417 1417
1418 1418 if (error == ENOTSUP)
1419 1419 return;
1420 1420 ASSERT0(error);
1421 1421
1422 1422 /* What blocksize did we actually get? */
1423 1423 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1424 1424 }
1425 1425
1426 1426 /*
1427 1427 * This is a dummy interface used when pvn_vplist_dirty() should *not*
1428 1428 * be calling back into the fs for a putpage(). E.g.: when truncating
1429 1429 * a file, the pages being "thrown away* don't need to be written out.
1430 1430 */
1431 1431 /* ARGSUSED */
1432 1432 static int
1433 1433 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1434 1434 int flags, cred_t *cr)
1435 1435 {
1436 1436 ASSERT(0);
1437 1437 return (0);
1438 1438 }
1439 1439
1440 1440 /*
1441 1441 * Increase the file length
1442 1442 *
1443 1443 * IN: zp - znode of file to free data in.
1444 1444 * end - new end-of-file
1445 1445 *
1446 1446 * RETURN: 0 on success, error code on failure
1447 1447 */
1448 1448 static int
1449 1449 zfs_extend(znode_t *zp, uint64_t end)
1450 1450 {
1451 1451 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1452 1452 dmu_tx_t *tx;
1453 1453 rl_t *rl;
1454 1454 uint64_t newblksz;
1455 1455 int error;
1456 1456
1457 1457 /*
1458 1458 * We will change zp_size, lock the whole file.
1459 1459 */
1460 1460 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1461 1461
1462 1462 /*
1463 1463 * Nothing to do if file already at desired length.
1464 1464 */
1465 1465 if (end <= zp->z_size) {
1466 1466 zfs_range_unlock(rl);
1467 1467 return (0);
1468 1468 }
1469 1469 tx = dmu_tx_create(zfsvfs->z_os);
1470 1470 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1471 1471 zfs_sa_upgrade_txholds(tx, zp);
1472 1472 if (end > zp->z_blksz &&
1473 1473 (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1474 1474 /*
1475 1475 * We are growing the file past the current block size.
1476 1476 */
1477 1477 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1478 1478 /*
1479 1479 * File's blocksize is already larger than the
1480 1480 * "recordsize" property. Only let it grow to
1481 1481 * the next power of 2.
1482 1482 */
1483 1483 ASSERT(!ISP2(zp->z_blksz));
1484 1484 newblksz = MIN(end, 1 << highbit64(zp->z_blksz));
1485 1485 } else {
1486 1486 newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1487 1487 }
1488 1488 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1489 1489 } else {
1490 1490 newblksz = 0;
1491 1491 }
1492 1492
1493 1493 error = dmu_tx_assign(tx, TXG_WAIT);
1494 1494 if (error) {
1495 1495 dmu_tx_abort(tx);
1496 1496 zfs_range_unlock(rl);
1497 1497 return (error);
1498 1498 }
1499 1499
1500 1500 if (newblksz)
1501 1501 zfs_grow_blocksize(zp, newblksz, tx);
1502 1502
1503 1503 zp->z_size = end;
1504 1504
1505 1505 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
1506 1506 &zp->z_size, sizeof (zp->z_size), tx));
1507 1507
1508 1508 zfs_range_unlock(rl);
1509 1509
1510 1510 dmu_tx_commit(tx);
1511 1511
1512 1512 return (0);
1513 1513 }
1514 1514
1515 1515 /*
1516 1516 * Free space in a file.
1517 1517 *
1518 1518 * IN: zp - znode of file to free data in.
1519 1519 * off - start of section to free.
1520 1520 * len - length of section to free.
1521 1521 *
1522 1522 * RETURN: 0 on success, error code on failure
1523 1523 */
1524 1524 static int
1525 1525 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1526 1526 {
1527 1527 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1528 1528 rl_t *rl;
1529 1529 int error;
1530 1530
1531 1531 /*
1532 1532 * Lock the range being freed.
1533 1533 */
1534 1534 rl = zfs_range_lock(zp, off, len, RL_WRITER);
1535 1535
1536 1536 /*
1537 1537 * Nothing to do if file already at desired length.
1538 1538 */
1539 1539 if (off >= zp->z_size) {
1540 1540 zfs_range_unlock(rl);
1541 1541 return (0);
1542 1542 }
1543 1543
1544 1544 if (off + len > zp->z_size)
1545 1545 len = zp->z_size - off;
1546 1546
1547 1547 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1548 1548
1549 1549 zfs_range_unlock(rl);
1550 1550
1551 1551 return (error);
1552 1552 }
1553 1553
1554 1554 /*
1555 1555 * Truncate a file
1556 1556 *
1557 1557 * IN: zp - znode of file to free data in.
1558 1558 * end - new end-of-file.
1559 1559 *
1560 1560 * RETURN: 0 on success, error code on failure
1561 1561 */
1562 1562 static int
1563 1563 zfs_trunc(znode_t *zp, uint64_t end)
1564 1564 {
1565 1565 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1566 1566 vnode_t *vp = ZTOV(zp);
1567 1567 dmu_tx_t *tx;
1568 1568 rl_t *rl;
1569 1569 int error;
1570 1570 sa_bulk_attr_t bulk[2];
1571 1571 int count = 0;
1572 1572
1573 1573 /*
1574 1574 * We will change zp_size, lock the whole file.
1575 1575 */
1576 1576 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1577 1577
1578 1578 /*
1579 1579 * Nothing to do if file already at desired length.
1580 1580 */
1581 1581 if (end >= zp->z_size) {
1582 1582 zfs_range_unlock(rl);
1583 1583 return (0);
1584 1584 }
1585 1585
1586 1586 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end, -1);
1587 1587 if (error) {
1588 1588 zfs_range_unlock(rl);
1589 1589 return (error);
1590 1590 }
1591 1591 tx = dmu_tx_create(zfsvfs->z_os);
1592 1592 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1593 1593 zfs_sa_upgrade_txholds(tx, zp);
1594 1594 dmu_tx_mark_netfree(tx);
1595 1595 error = dmu_tx_assign(tx, TXG_WAIT);
1596 1596 if (error) {
1597 1597 dmu_tx_abort(tx);
1598 1598 zfs_range_unlock(rl);
1599 1599 return (error);
1600 1600 }
1601 1601
1602 1602 zp->z_size = end;
1603 1603 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1604 1604 NULL, &zp->z_size, sizeof (zp->z_size));
1605 1605
1606 1606 if (end == 0) {
1607 1607 zp->z_pflags &= ~ZFS_SPARSE;
1608 1608 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1609 1609 NULL, &zp->z_pflags, 8);
1610 1610 }
1611 1611 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1612 1612
1613 1613 dmu_tx_commit(tx);
1614 1614
1615 1615 /*
1616 1616 * Clear any mapped pages in the truncated region. This has to
1617 1617 * happen outside of the transaction to avoid the possibility of
1618 1618 * a deadlock with someone trying to push a page that we are
1619 1619 * about to invalidate.
1620 1620 */
1621 1621 if (vn_has_cached_data(vp)) {
1622 1622 page_t *pp;
1623 1623 uint64_t start = end & PAGEMASK;
1624 1624 int poff = end & PAGEOFFSET;
1625 1625
1626 1626 if (poff != 0 && (pp = page_lookup(vp, start, SE_SHARED))) {
1627 1627 /*
1628 1628 * We need to zero a partial page.
1629 1629 */
1630 1630 pagezero(pp, poff, PAGESIZE - poff);
1631 1631 start += PAGESIZE;
1632 1632 page_unlock(pp);
1633 1633 }
1634 1634 error = pvn_vplist_dirty(vp, start, zfs_no_putpage,
1635 1635 B_INVAL | B_TRUNC, NULL);
1636 1636 ASSERT(error == 0);
1637 1637 }
1638 1638
1639 1639 zfs_range_unlock(rl);
1640 1640
1641 1641 return (0);
1642 1642 }
1643 1643
1644 1644 /*
1645 1645 * Free space in a file
1646 1646 *
1647 1647 * IN: zp - znode of file to free data in.
1648 1648 * off - start of range
1649 1649 * len - end of range (0 => EOF)
1650 1650 * flag - current file open mode flags.
1651 1651 * log - TRUE if this action should be logged
1652 1652 *
1653 1653 * RETURN: 0 on success, error code on failure
1654 1654 */
1655 1655 int
1656 1656 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1657 1657 {
1658 1658 vnode_t *vp = ZTOV(zp);
1659 1659 dmu_tx_t *tx;
1660 1660 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1661 1661 zilog_t *zilog = zfsvfs->z_log;
1662 1662 uint64_t mode;
1663 1663 uint64_t mtime[2], ctime[2];
1664 1664 sa_bulk_attr_t bulk[3];
1665 1665 int count = 0;
1666 1666 int error;
1667 1667
1668 1668 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
1669 1669 sizeof (mode))) != 0)
1670 1670 return (error);
1671 1671
1672 1672 if (off > zp->z_size) {
1673 1673 error = zfs_extend(zp, off+len);
1674 1674 if (error == 0 && log)
1675 1675 goto log;
1676 1676 else
1677 1677 return (error);
1678 1678 }
1679 1679
1680 1680 /*
1681 1681 * Check for any locks in the region to be freed.
1682 1682 */
1683 1683
1684 1684 if (MANDLOCK(vp, (mode_t)mode)) {
1685 1685 uint64_t length = (len ? len : zp->z_size - off);
1686 1686 if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1687 1687 return (error);
1688 1688 }
1689 1689
1690 1690 if (len == 0) {
1691 1691 error = zfs_trunc(zp, off);
1692 1692 } else {
1693 1693 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1694 1694 off + len > zp->z_size)
1695 1695 error = zfs_extend(zp, off+len);
1696 1696 }
1697 1697 if (error || !log)
1698 1698 return (error);
1699 1699 log:
1700 1700 tx = dmu_tx_create(zfsvfs->z_os);
1701 1701 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1702 1702 zfs_sa_upgrade_txholds(tx, zp);
1703 1703 error = dmu_tx_assign(tx, TXG_WAIT);
1704 1704 if (error) {
1705 1705 dmu_tx_abort(tx);
1706 1706 return (error);
1707 1707 }
1708 1708
1709 1709 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1710 1710 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1711 1711 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1712 1712 NULL, &zp->z_pflags, 8);
1713 1713 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1714 1714 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1715 1715 ASSERT(error == 0);
1716 1716
1717 1717 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1718 1718
1719 1719 dmu_tx_commit(tx);
1720 1720 return (0);
1721 1721 }
1722 1722
1723 1723 void
1724 1724 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1725 1725 {
1726 1726 zfsvfs_t zfsvfs;
1727 1727 uint64_t moid, obj, sa_obj, version;
1728 1728 uint64_t sense = ZFS_CASE_SENSITIVE;
1729 1729 uint64_t norm = 0;
1730 1730 nvpair_t *elem;
1731 1731 int error;
1732 1732 int i;
1733 1733 znode_t *rootzp = NULL;
1734 1734 vnode_t *vp;
1735 1735 vattr_t vattr;
1736 1736 znode_t *zp;
1737 1737 zfs_acl_ids_t acl_ids;
1738 1738
1739 1739 /*
1740 1740 * First attempt to create master node.
1741 1741 */
1742 1742 /*
1743 1743 * In an empty objset, there are no blocks to read and thus
1744 1744 * there can be no i/o errors (which we assert below).
1745 1745 */
1746 1746 moid = MASTER_NODE_OBJ;
1747 1747 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1748 1748 DMU_OT_NONE, 0, tx);
1749 1749 ASSERT(error == 0);
1750 1750
1751 1751 /*
1752 1752 * Set starting attributes.
1753 1753 */
1754 1754 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1755 1755 elem = NULL;
1756 1756 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1757 1757 /* For the moment we expect all zpl props to be uint64_ts */
1758 1758 uint64_t val;
1759 1759 char *name;
1760 1760
1761 1761 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1762 1762 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1763 1763 name = nvpair_name(elem);
1764 1764 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1765 1765 if (val < version)
1766 1766 version = val;
1767 1767 } else {
1768 1768 error = zap_update(os, moid, name, 8, 1, &val, tx);
1769 1769 }
1770 1770 ASSERT(error == 0);
1771 1771 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1772 1772 norm = val;
1773 1773 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1774 1774 sense = val;
1775 1775 }
1776 1776 ASSERT(version != 0);
1777 1777 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1778 1778
1779 1779 /*
1780 1780 * Create zap object used for SA attribute registration
1781 1781 */
1782 1782
1783 1783 if (version >= ZPL_VERSION_SA) {
1784 1784 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1785 1785 DMU_OT_NONE, 0, tx);
1786 1786 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1787 1787 ASSERT(error == 0);
1788 1788 } else {
1789 1789 sa_obj = 0;
1790 1790 }
1791 1791 /*
1792 1792 * Create a delete queue.
1793 1793 */
1794 1794 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1795 1795
1796 1796 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1797 1797 ASSERT(error == 0);
1798 1798
1799 1799 /*
1800 1800 * Create root znode. Create minimal znode/vnode/zfsvfs
1801 1801 * to allow zfs_mknode to work.
1802 1802 */
1803 1803 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1804 1804 vattr.va_type = VDIR;
1805 1805 vattr.va_mode = S_IFDIR|0755;
1806 1806 vattr.va_uid = crgetuid(cr);
1807 1807 vattr.va_gid = crgetgid(cr);
1808 1808
1809 1809 rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1810 1810 ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1811 1811 rootzp->z_moved = 0;
1812 1812 rootzp->z_unlinked = 0;
1813 1813 rootzp->z_atime_dirty = 0;
1814 1814 rootzp->z_is_sa = USE_SA(version, os);
1815 1815
1816 1816 vp = ZTOV(rootzp);
1817 1817 vn_reinit(vp);
1818 1818 vp->v_type = VDIR;
1819 1819
1820 1820 bzero(&zfsvfs, sizeof (zfsvfs_t));
1821 1821
1822 1822 zfsvfs.z_os = os;
1823 1823 zfsvfs.z_parent = &zfsvfs;
1824 1824 zfsvfs.z_version = version;
1825 1825 zfsvfs.z_use_fuids = USE_FUIDS(version, os);
1826 1826 zfsvfs.z_use_sa = USE_SA(version, os);
1827 1827 zfsvfs.z_norm = norm;
1828 1828
1829 1829 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1830 1830 &zfsvfs.z_attr_table);
1831 1831
1832 1832 ASSERT(error == 0);
1833 1833
1834 1834 /*
1835 1835 * Fold case on file systems that are always or sometimes case
1836 1836 * insensitive.
1837 1837 */
1838 1838 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1839 1839 zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
1840 1840
1841 1841 mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1842 1842 list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
1843 1843 offsetof(znode_t, z_link_node));
1844 1844
1845 1845 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1846 1846 mutex_init(&zfsvfs.z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1847 1847
1848 1848 rootzp->z_zfsvfs = &zfsvfs;
1849 1849 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1850 1850 cr, NULL, &acl_ids));
1851 1851 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1852 1852 ASSERT3P(zp, ==, rootzp);
1853 1853 ASSERT(!vn_in_dnlc(ZTOV(rootzp))); /* not valid to move */
1854 1854 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1855 1855 ASSERT(error == 0);
1856 1856 zfs_acl_ids_free(&acl_ids);
1857 1857 POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1858 1858
1859 1859 ZTOV(rootzp)->v_count = 0;
1860 1860 sa_handle_destroy(rootzp->z_sa_hdl);
1861 1861 kmem_cache_free(znode_cache, rootzp);
1862 1862
1863 1863 /*
1864 1864 * Create shares directory
1865 1865 */
1866 1866
1867 1867 error = zfs_create_share_dir(&zfsvfs, tx);
1868 1868
1869 1869 ASSERT(error == 0);
1870 1870
1871 1871 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1872 1872 mutex_destroy(&zfsvfs.z_hold_mtx[i]);
1873 1873 }
1874 1874
1875 1875 #endif /* _KERNEL */
1876 1876
1877 1877 static int
1878 1878 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1879 1879 {
1880 1880 uint64_t sa_obj = 0;
1881 1881 int error;
1882 1882
1883 1883 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1884 1884 if (error != 0 && error != ENOENT)
1885 1885 return (error);
1886 1886
1887 1887 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1888 1888 return (error);
1889 1889 }
1890 1890
1891 1891 static int
1892 1892 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1893 1893 dmu_buf_t **db, void *tag)
1894 1894 {
1895 1895 dmu_object_info_t doi;
1896 1896 int error;
1897 1897
1898 1898 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1899 1899 return (error);
1900 1900
1901 1901 dmu_object_info_from_db(*db, &doi);
1902 1902 if ((doi.doi_bonus_type != DMU_OT_SA &&
1903 1903 doi.doi_bonus_type != DMU_OT_ZNODE) ||
1904 1904 doi.doi_bonus_type == DMU_OT_ZNODE &&
1905 1905 doi.doi_bonus_size < sizeof (znode_phys_t)) {
1906 1906 sa_buf_rele(*db, tag);
1907 1907 return (SET_ERROR(ENOTSUP));
1908 1908 }
1909 1909
1910 1910 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1911 1911 if (error != 0) {
1912 1912 sa_buf_rele(*db, tag);
1913 1913 return (error);
1914 1914 }
1915 1915
1916 1916 return (0);
1917 1917 }
1918 1918
1919 1919 void
1920 1920 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
1921 1921 {
1922 1922 sa_handle_destroy(hdl);
1923 1923 sa_buf_rele(db, tag);
1924 1924 }
1925 1925
1926 1926 /*
1927 1927 * Given an object number, return its parent object number and whether
1928 1928 * or not the object is an extended attribute directory.
1929 1929 */
1930 1930 static int
1931 1931 zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table,
1932 1932 uint64_t *pobjp, int *is_xattrdir)
1933 1933 {
1934 1934 uint64_t parent;
1935 1935 uint64_t pflags;
1936 1936 uint64_t mode;
1937 1937 uint64_t parent_mode;
1938 1938 sa_bulk_attr_t bulk[3];
1939 1939 sa_handle_t *sa_hdl;
1940 1940 dmu_buf_t *sa_db;
1941 1941 int count = 0;
1942 1942 int error;
1943 1943
1944 1944 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
1945 1945 &parent, sizeof (parent));
1946 1946 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
1947 1947 &pflags, sizeof (pflags));
1948 1948 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1949 1949 &mode, sizeof (mode));
1950 1950
1951 1951 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
1952 1952 return (error);
1953 1953
1954 1954 /*
1955 1955 * When a link is removed its parent pointer is not changed and will
1956 1956 * be invalid. There are two cases where a link is removed but the
1957 1957 * file stays around, when it goes to the delete queue and when there
1958 1958 * are additional links.
1959 1959 */
1960 1960 error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG);
1961 1961 if (error != 0)
1962 1962 return (error);
1963 1963
1964 1964 error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode));
1965 1965 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
1966 1966 if (error != 0)
1967 1967 return (error);
1968 1968
1969 1969 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
1970 1970
1971 1971 /*
1972 1972 * Extended attributes can be applied to files, directories, etc.
1973 1973 * Otherwise the parent must be a directory.
1974 1974 */
1975 1975 if (!*is_xattrdir && !S_ISDIR(parent_mode))
1976 1976 return (SET_ERROR(EINVAL));
1977 1977
1978 1978 *pobjp = parent;
1979 1979
1980 1980 return (0);
1981 1981 }
1982 1982
1983 1983 /*
1984 1984 * Given an object number, return some zpl level statistics
1985 1985 */
1986 1986 static int
1987 1987 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
1988 1988 zfs_stat_t *sb)
1989 1989 {
1990 1990 sa_bulk_attr_t bulk[4];
1991 1991 int count = 0;
1992 1992
1993 1993 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1994 1994 &sb->zs_mode, sizeof (sb->zs_mode));
1995 1995 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
1996 1996 &sb->zs_gen, sizeof (sb->zs_gen));
1997 1997 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
1998 1998 &sb->zs_links, sizeof (sb->zs_links));
1999 1999 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
2000 2000 &sb->zs_ctime, sizeof (sb->zs_ctime));
2001 2001
2002 2002 return (sa_bulk_lookup(hdl, bulk, count));
2003 2003 }
2004 2004
2005 2005 static int
2006 2006 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
2007 2007 sa_attr_type_t *sa_table, char *buf, int len)
2008 2008 {
2009 2009 sa_handle_t *sa_hdl;
2010 2010 sa_handle_t *prevhdl = NULL;
2011 2011 dmu_buf_t *prevdb = NULL;
2012 2012 dmu_buf_t *sa_db = NULL;
2013 2013 char *path = buf + len - 1;
2014 2014 int error;
2015 2015
2016 2016 *path = '\0';
2017 2017 sa_hdl = hdl;
2018 2018
2019 2019 for (;;) {
2020 2020 uint64_t pobj;
2021 2021 char component[MAXNAMELEN + 2];
2022 2022 size_t complen;
2023 2023 int is_xattrdir;
2024 2024
2025 2025 if (prevdb)
2026 2026 zfs_release_sa_handle(prevhdl, prevdb, FTAG);
2027 2027
2028 2028 if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj,
2029 2029 &is_xattrdir)) != 0)
2030 2030 break;
2031 2031
2032 2032 if (pobj == obj) {
2033 2033 if (path[0] != '/')
2034 2034 *--path = '/';
2035 2035 break;
2036 2036 }
2037 2037
2038 2038 component[0] = '/';
2039 2039 if (is_xattrdir) {
2040 2040 (void) sprintf(component + 1, "<xattrdir>");
2041 2041 } else {
2042 2042 error = zap_value_search(osp, pobj, obj,
2043 2043 ZFS_DIRENT_OBJ(-1ULL), component + 1);
2044 2044 if (error != 0)
2045 2045 break;
2046 2046 }
2047 2047
2048 2048 complen = strlen(component);
2049 2049 path -= complen;
2050 2050 ASSERT(path >= buf);
2051 2051 bcopy(component, path, complen);
2052 2052 obj = pobj;
2053 2053
2054 2054 if (sa_hdl != hdl) {
2055 2055 prevhdl = sa_hdl;
2056 2056 prevdb = sa_db;
2057 2057 }
2058 2058 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
2059 2059 if (error != 0) {
2060 2060 sa_hdl = prevhdl;
2061 2061 sa_db = prevdb;
2062 2062 break;
2063 2063 }
2064 2064 }
2065 2065
2066 2066 if (sa_hdl != NULL && sa_hdl != hdl) {
2067 2067 ASSERT(sa_db != NULL);
2068 2068 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2069 2069 }
2070 2070
2071 2071 if (error == 0)
2072 2072 (void) memmove(buf, path, buf + len - path);
2073 2073
2074 2074 return (error);
2075 2075 }
2076 2076
2077 2077 int
2078 2078 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
2079 2079 {
2080 2080 sa_attr_type_t *sa_table;
2081 2081 sa_handle_t *hdl;
2082 2082 dmu_buf_t *db;
2083 2083 int error;
2084 2084
2085 2085 error = zfs_sa_setup(osp, &sa_table);
2086 2086 if (error != 0)
2087 2087 return (error);
2088 2088
2089 2089 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2090 2090 if (error != 0)
2091 2091 return (error);
2092 2092
2093 2093 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2094 2094
2095 2095 zfs_release_sa_handle(hdl, db, FTAG);
2096 2096 return (error);
2097 2097 }
2098 2098
2099 2099 int
2100 2100 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
2101 2101 char *buf, int len)
2102 2102 {
2103 2103 char *path = buf + len - 1;
2104 2104 sa_attr_type_t *sa_table;
2105 2105 sa_handle_t *hdl;
2106 2106 dmu_buf_t *db;
2107 2107 int error;
2108 2108
2109 2109 *path = '\0';
2110 2110
2111 2111 error = zfs_sa_setup(osp, &sa_table);
2112 2112 if (error != 0)
2113 2113 return (error);
2114 2114
2115 2115 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2116 2116 if (error != 0)
2117 2117 return (error);
2118 2118
2119 2119 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2120 2120 if (error != 0) {
2121 2121 zfs_release_sa_handle(hdl, db, FTAG);
2122 2122 return (error);
2123 2123 }
2124 2124
2125 2125 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2126 2126
2127 2127 zfs_release_sa_handle(hdl, db, FTAG);
2128 2128 return (error);
2129 2129 }
|
↓ open down ↓ |
967 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX