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