4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012 Cyril Plisko. All rights reserved.
24 * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2021, 2022 by Pawel Jakub Dawidek
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/sysmacros.h>
31 #include <sys/cmn_err.h>
33 #include <sys/thread.h>
35 #include <sys/fcntl.h>
37 #include <sys/fs/zfs.h>
38 #include <sys/zfs_znode.h>
39 #include <sys/zfs_dir.h>
40 #include <sys/zfs_acl.h>
41 #include <sys/zfs_fuid.h>
42 #include <sys/zfs_vnops.h>
45 #include <sys/byteorder.h>
48 #include <sys/atomic.h>
51 #include <sys/dmu_objset.h>
52 #include <sys/zfeature.h>
55 * NB: FreeBSD expects to be able to do vnode locking in lookup and
56 * hold the locks across all subsequent VOPs until vput is called.
57 * This means that its zfs vnops routines can't do any internal locking.
58 * In order to have the same contract as the Linux vnops there would
59 * needed to be duplicate locked vnops. If the vnops were used more widely
60 * in common code this would likely be preferable. However, currently
61 * this is the only file where this is the case.
65 * Functions to replay ZFS intent log (ZIL) records
66 * The functions are called through a function vector (zfs_replay_vector)
67 * which is indexed by the transaction type.
71 zfs_init_vattr(vattr_t
*vap
, uint64_t mask
, uint64_t mode
,
72 uint64_t uid
, uint64_t gid
, uint64_t rdev
, uint64_t nodeid
)
74 memset(vap
, 0, sizeof (*vap
));
75 vap
->va_mask
= (uint_t
)mask
;
77 #if defined(__FreeBSD__) || defined(__APPLE__)
78 vap
->va_type
= IFTOVT(mode
);
80 vap
->va_uid
= (uid_t
)(IS_EPHEMERAL(uid
)) ? -1 : uid
;
81 vap
->va_gid
= (gid_t
)(IS_EPHEMERAL(gid
)) ? -1 : gid
;
82 vap
->va_rdev
= zfs_cmpldev(rdev
);
83 vap
->va_nodeid
= nodeid
;
87 zfs_replay_error(void *arg1
, void *arg2
, boolean_t byteswap
)
89 (void) arg1
, (void) arg2
, (void) byteswap
;
90 return (SET_ERROR(ENOTSUP
));
94 zfs_replay_xvattr(lr_attr_t
*lrattr
, xvattr_t
*xvap
)
96 xoptattr_t
*xoap
= NULL
;
103 xvap
->xva_vattr
.va_mask
|= ATTR_XVATTR
;
104 if ((xoap
= xva_getxoptattr(xvap
)) == NULL
) {
105 xvap
->xva_vattr
.va_mask
&= ~ATTR_XVATTR
; /* shouldn't happen */
109 ASSERT(lrattr
->lr_attr_masksize
== xvap
->xva_mapsize
);
111 bitmap
= &lrattr
->lr_attr_bitmap
;
112 for (i
= 0; i
!= lrattr
->lr_attr_masksize
; i
++, bitmap
++)
113 xvap
->xva_reqattrmap
[i
] = *bitmap
;
115 attrs
= (uint64_t *)(lrattr
+ lrattr
->lr_attr_masksize
- 1);
117 scanstamp
= (caddr_t
)(crtime
+ 2);
119 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
))
120 xoap
->xoa_hidden
= ((*attrs
& XAT0_HIDDEN
) != 0);
121 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
))
122 xoap
->xoa_system
= ((*attrs
& XAT0_SYSTEM
) != 0);
123 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
))
124 xoap
->xoa_archive
= ((*attrs
& XAT0_ARCHIVE
) != 0);
125 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
))
126 xoap
->xoa_readonly
= ((*attrs
& XAT0_READONLY
) != 0);
127 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
))
128 xoap
->xoa_immutable
= ((*attrs
& XAT0_IMMUTABLE
) != 0);
129 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
))
130 xoap
->xoa_nounlink
= ((*attrs
& XAT0_NOUNLINK
) != 0);
131 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
))
132 xoap
->xoa_appendonly
= ((*attrs
& XAT0_APPENDONLY
) != 0);
133 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
))
134 xoap
->xoa_nodump
= ((*attrs
& XAT0_NODUMP
) != 0);
135 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))
136 xoap
->xoa_opaque
= ((*attrs
& XAT0_OPAQUE
) != 0);
137 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
))
138 xoap
->xoa_av_modified
= ((*attrs
& XAT0_AV_MODIFIED
) != 0);
139 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
))
140 xoap
->xoa_av_quarantined
=
141 ((*attrs
& XAT0_AV_QUARANTINED
) != 0);
142 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
))
143 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, crtime
);
144 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) {
145 ASSERT(!XVA_ISSET_REQ(xvap
, XAT_PROJID
));
147 memcpy(xoap
->xoa_av_scanstamp
, scanstamp
, AV_SCANSTAMP_SZ
);
148 } else if (XVA_ISSET_REQ(xvap
, XAT_PROJID
)) {
150 * XAT_PROJID and XAT_AV_SCANSTAMP will never be valid
151 * at the same time, so we can share the same space.
153 memcpy(&xoap
->xoa_projid
, scanstamp
, sizeof (uint64_t));
155 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
))
156 xoap
->xoa_reparse
= ((*attrs
& XAT0_REPARSE
) != 0);
157 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
))
158 xoap
->xoa_offline
= ((*attrs
& XAT0_OFFLINE
) != 0);
159 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
))
160 xoap
->xoa_sparse
= ((*attrs
& XAT0_SPARSE
) != 0);
161 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
))
162 xoap
->xoa_projinherit
= ((*attrs
& XAT0_PROJINHERIT
) != 0);
166 zfs_replay_domain_cnt(uint64_t uid
, uint64_t gid
)
172 uid_idx
= FUID_INDEX(uid
);
173 gid_idx
= FUID_INDEX(gid
);
176 if (gid_idx
> 0 && gid_idx
!= uid_idx
)
183 zfs_replay_fuid_domain_common(zfs_fuid_info_t
*fuid_infop
, void *start
,
188 for (i
= 0; i
!= domcnt
; i
++) {
189 fuid_infop
->z_domain_table
[i
] = start
;
190 start
= (caddr_t
)start
+ strlen(start
) + 1;
197 * Set the uid/gid in the fuid_info structure.
200 zfs_replay_fuid_ugid(zfs_fuid_info_t
*fuid_infop
, uint64_t uid
, uint64_t gid
)
203 * If owner or group are log specific FUIDs then slurp up
204 * domain information and build zfs_fuid_info_t
206 if (IS_EPHEMERAL(uid
))
207 fuid_infop
->z_fuid_owner
= uid
;
209 if (IS_EPHEMERAL(gid
))
210 fuid_infop
->z_fuid_group
= gid
;
214 * Load fuid domains into fuid_info_t
216 static zfs_fuid_info_t
*
217 zfs_replay_fuid_domain(void *buf
, void **end
, uint64_t uid
, uint64_t gid
)
221 zfs_fuid_info_t
*fuid_infop
;
223 fuid_infop
= zfs_fuid_info_alloc();
225 domcnt
= zfs_replay_domain_cnt(uid
, gid
);
230 fuid_infop
->z_domain_table
=
231 kmem_zalloc(domcnt
* sizeof (char *), KM_SLEEP
);
233 zfs_replay_fuid_ugid(fuid_infop
, uid
, gid
);
235 fuid_infop
->z_domain_cnt
= domcnt
;
236 *end
= zfs_replay_fuid_domain_common(fuid_infop
, buf
, domcnt
);
241 * load zfs_fuid_t's and fuid_domains into fuid_info_t
243 static zfs_fuid_info_t
*
244 zfs_replay_fuids(void *start
, void **end
, int idcnt
, int domcnt
, uint64_t uid
,
247 uint64_t *log_fuid
= (uint64_t *)start
;
248 zfs_fuid_info_t
*fuid_infop
;
251 fuid_infop
= zfs_fuid_info_alloc();
252 fuid_infop
->z_domain_cnt
= domcnt
;
254 fuid_infop
->z_domain_table
=
255 kmem_zalloc(domcnt
* sizeof (char *), KM_SLEEP
);
257 for (i
= 0; i
!= idcnt
; i
++) {
260 zfuid
= kmem_alloc(sizeof (zfs_fuid_t
), KM_SLEEP
);
261 zfuid
->z_logfuid
= *log_fuid
;
264 list_insert_tail(&fuid_infop
->z_fuids
, zfuid
);
268 zfs_replay_fuid_ugid(fuid_infop
, uid
, gid
);
270 *end
= zfs_replay_fuid_domain_common(fuid_infop
, log_fuid
, domcnt
);
275 zfs_replay_swap_attrs(lr_attr_t
*lrattr
)
277 /* swap the lr_attr structure */
278 byteswap_uint32_array(lrattr
, sizeof (*lrattr
));
279 /* swap the bitmap */
280 byteswap_uint32_array(lrattr
+ 1, (lrattr
->lr_attr_masksize
- 1) *
282 /* swap the attributes, create time + 64 bit word for attributes */
283 byteswap_uint64_array((caddr_t
)(lrattr
+ 1) + (sizeof (uint32_t) *
284 (lrattr
->lr_attr_masksize
- 1)), 3 * sizeof (uint64_t));
288 * Replay file create with optional ACL, xvattr information as well
289 * as option FUID information.
292 zfs_replay_create_acl(void *arg1
, void *arg2
, boolean_t byteswap
)
294 zfsvfs_t
*zfsvfs
= arg1
;
295 lr_acl_create_t
*lracl
= arg2
;
296 char *name
= NULL
; /* location determined later */
297 lr_create_t
*lr
= (lr_create_t
*)lracl
;
302 vsecattr_t vsec
= { 0 };
312 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lracl
));
314 txtype
= (lr
->lr_common
.lrc_txtype
& ~TX_CI
);
316 byteswap_uint64_array(lracl
, sizeof (*lracl
));
317 if (txtype
== TX_CREATE_ACL_ATTR
||
318 txtype
== TX_MKDIR_ACL_ATTR
) {
319 lrattr
= (lr_attr_t
*)(caddr_t
)(lracl
+ 1);
320 zfs_replay_swap_attrs(lrattr
);
321 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
324 aclstart
= (caddr_t
)(lracl
+ 1) + xvatlen
;
325 zfs_ace_byteswap(aclstart
, lracl
->lr_acl_bytes
, B_FALSE
);
327 if (lracl
->lr_fuidcnt
) {
328 byteswap_uint64_array((caddr_t
)aclstart
+
329 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
),
330 lracl
->lr_fuidcnt
* sizeof (uint64_t));
334 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
337 objid
= LR_FOID_GET_OBJ(lr
->lr_foid
);
338 dnodesize
= LR_FOID_GET_SLOTS(lr
->lr_foid
) << DNODE_SHIFT
;
341 zfs_init_vattr(&xva
.xva_vattr
, ATTR_MODE
| ATTR_UID
| ATTR_GID
,
342 lr
->lr_mode
, lr
->lr_uid
, lr
->lr_gid
, lr
->lr_rdev
, objid
);
345 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
346 * eventually end up in zfs_mknode(), which assigns the object's
347 * creation time, generation number, and dnode size. The generic
348 * zfs_create() has no concept of these attributes, so we smuggle
349 * the values inside the vattr's otherwise unused va_ctime,
350 * va_nblocks, and va_fsid fields.
352 ZFS_TIME_DECODE(&xva
.xva_vattr
.va_ctime
, lr
->lr_crtime
);
353 xva
.xva_vattr
.va_nblocks
= lr
->lr_gen
;
354 xva
.xva_vattr
.va_fsid
= dnodesize
;
356 error
= dnode_try_claim(zfsvfs
->z_os
, objid
, dnodesize
>> DNODE_SHIFT
);
360 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
364 aclstart
= (caddr_t
)(lracl
+ 1);
365 fuidstart
= (caddr_t
)aclstart
+
366 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
367 zfsvfs
->z_fuid_replay
= zfs_replay_fuids(fuidstart
,
368 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
369 lr
->lr_uid
, lr
->lr_gid
);
371 case TX_CREATE_ACL_ATTR
:
373 lrattr
= (lr_attr_t
*)(caddr_t
)(lracl
+ 1);
374 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
375 xva
.xva_vattr
.va_mask
|= ATTR_XVATTR
;
376 zfs_replay_xvattr(lrattr
, &xva
);
378 vsec
.vsa_mask
= VSA_ACE
| VSA_ACE_ACLFLAGS
;
379 vsec
.vsa_aclentp
= (caddr_t
)(lracl
+ 1) + xvatlen
;
380 vsec
.vsa_aclcnt
= lracl
->lr_aclcnt
;
381 vsec
.vsa_aclentsz
= lracl
->lr_acl_bytes
;
382 vsec
.vsa_aclflags
= lracl
->lr_acl_flags
;
383 if (zfsvfs
->z_fuid_replay
== NULL
) {
384 fuidstart
= (caddr_t
)(lracl
+ 1) + xvatlen
+
385 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
386 zfsvfs
->z_fuid_replay
=
387 zfs_replay_fuids(fuidstart
,
388 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
389 lr
->lr_uid
, lr
->lr_gid
);
392 #if defined(__linux__)
393 error
= zfs_create(dzp
, name
, &xva
.xva_vattr
,
394 0, 0, &zp
, kcred
, vflg
, &vsec
, zfs_init_idmap
);
396 error
= zfs_create(dzp
, name
, &xva
.xva_vattr
,
397 0, 0, &zp
, kcred
, vflg
, &vsec
, NULL
);
401 aclstart
= (caddr_t
)(lracl
+ 1);
402 fuidstart
= (caddr_t
)aclstart
+
403 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
404 zfsvfs
->z_fuid_replay
= zfs_replay_fuids(fuidstart
,
405 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
406 lr
->lr_uid
, lr
->lr_gid
);
408 case TX_MKDIR_ACL_ATTR
:
410 lrattr
= (lr_attr_t
*)(caddr_t
)(lracl
+ 1);
411 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
412 zfs_replay_xvattr(lrattr
, &xva
);
414 vsec
.vsa_mask
= VSA_ACE
| VSA_ACE_ACLFLAGS
;
415 vsec
.vsa_aclentp
= (caddr_t
)(lracl
+ 1) + xvatlen
;
416 vsec
.vsa_aclcnt
= lracl
->lr_aclcnt
;
417 vsec
.vsa_aclentsz
= lracl
->lr_acl_bytes
;
418 vsec
.vsa_aclflags
= lracl
->lr_acl_flags
;
419 if (zfsvfs
->z_fuid_replay
== NULL
) {
420 fuidstart
= (caddr_t
)(lracl
+ 1) + xvatlen
+
421 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
422 zfsvfs
->z_fuid_replay
=
423 zfs_replay_fuids(fuidstart
,
424 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
425 lr
->lr_uid
, lr
->lr_gid
);
427 #if defined(__linux__)
428 error
= zfs_mkdir(dzp
, name
, &xva
.xva_vattr
,
429 &zp
, kcred
, vflg
, &vsec
, zfs_init_idmap
);
431 error
= zfs_mkdir(dzp
, name
, &xva
.xva_vattr
,
432 &zp
, kcred
, vflg
, &vsec
, NULL
);
436 error
= SET_ERROR(ENOTSUP
);
440 if (error
== 0 && zp
!= NULL
) {
442 VOP_UNLOCK1(ZTOV(zp
));
448 if (zfsvfs
->z_fuid_replay
)
449 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
450 zfsvfs
->z_fuid_replay
= NULL
;
456 zfs_replay_create(void *arg1
, void *arg2
, boolean_t byteswap
)
458 zfsvfs_t
*zfsvfs
= arg1
;
459 lr_create_t
*lr
= arg2
;
460 char *name
= NULL
; /* location determined later */
461 char *link
; /* symlink content follows name */
466 size_t lrsize
= sizeof (lr_create_t
);
475 ASSERT3U(lr
->lr_common
.lrc_reclen
, >, sizeof (*lr
));
477 txtype
= (lr
->lr_common
.lrc_txtype
& ~TX_CI
);
479 byteswap_uint64_array(lr
, sizeof (*lr
));
480 if (txtype
== TX_CREATE_ATTR
|| txtype
== TX_MKDIR_ATTR
)
481 zfs_replay_swap_attrs((lr_attr_t
*)(lr
+ 1));
485 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
488 objid
= LR_FOID_GET_OBJ(lr
->lr_foid
);
489 dnodesize
= LR_FOID_GET_SLOTS(lr
->lr_foid
) << DNODE_SHIFT
;
492 zfs_init_vattr(&xva
.xva_vattr
, ATTR_MODE
| ATTR_UID
| ATTR_GID
,
493 lr
->lr_mode
, lr
->lr_uid
, lr
->lr_gid
, lr
->lr_rdev
, objid
);
496 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
497 * eventually end up in zfs_mknode(), which assigns the object's
498 * creation time, generation number, and dnode slot count. The
499 * generic zfs_create() has no concept of these attributes, so
500 * we smuggle the values inside the vattr's otherwise unused
501 * va_ctime, va_nblocks, and va_fsid fields.
503 ZFS_TIME_DECODE(&xva
.xva_vattr
.va_ctime
, lr
->lr_crtime
);
504 xva
.xva_vattr
.va_nblocks
= lr
->lr_gen
;
505 xva
.xva_vattr
.va_fsid
= dnodesize
;
507 error
= dnode_try_claim(zfsvfs
->z_os
, objid
, dnodesize
>> DNODE_SHIFT
);
511 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
515 * Symlinks don't have fuid info, and CIFS never creates
518 * The _ATTR versions will grab the fuid info in their subcases.
520 if (txtype
!= TX_SYMLINK
&&
521 txtype
!= TX_MKDIR_ATTR
&&
522 txtype
!= TX_CREATE_ATTR
) {
524 zfsvfs
->z_fuid_replay
=
525 zfs_replay_fuid_domain(start
, &start
,
526 lr
->lr_uid
, lr
->lr_gid
);
531 lrattr
= (lr_attr_t
*)(caddr_t
)(lr
+ 1);
532 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
533 zfs_replay_xvattr((lr_attr_t
*)((caddr_t
)lr
+ lrsize
), &xva
);
534 start
= (caddr_t
)(lr
+ 1) + xvatlen
;
535 zfsvfs
->z_fuid_replay
=
536 zfs_replay_fuid_domain(start
, &start
,
537 lr
->lr_uid
, lr
->lr_gid
);
538 name
= (char *)start
;
543 name
= (char *)start
;
545 #if defined(__linux__)
546 error
= zfs_create(dzp
, name
, &xva
.xva_vattr
,
547 0, 0, &zp
, kcred
, vflg
, NULL
, zfs_init_idmap
);
549 error
= zfs_create(dzp
, name
, &xva
.xva_vattr
,
550 0, 0, &zp
, kcred
, vflg
, NULL
, NULL
);
554 lrattr
= (lr_attr_t
*)(caddr_t
)(lr
+ 1);
555 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
556 zfs_replay_xvattr((lr_attr_t
*)((caddr_t
)lr
+ lrsize
), &xva
);
557 start
= (caddr_t
)(lr
+ 1) + xvatlen
;
558 zfsvfs
->z_fuid_replay
=
559 zfs_replay_fuid_domain(start
, &start
,
560 lr
->lr_uid
, lr
->lr_gid
);
561 name
= (char *)start
;
566 name
= (char *)(lr
+ 1);
568 #if defined(__linux__)
569 error
= zfs_mkdir(dzp
, name
, &xva
.xva_vattr
,
570 &zp
, kcred
, vflg
, NULL
, zfs_init_idmap
);
572 error
= zfs_mkdir(dzp
, name
, &xva
.xva_vattr
,
573 &zp
, kcred
, vflg
, NULL
, NULL
);
578 error
= zfs_make_xattrdir(dzp
, &xva
.xva_vattr
, &zp
, kcred
);
581 name
= (char *)(lr
+ 1);
582 link
= name
+ strlen(name
) + 1;
583 #if defined(__linux__)
584 error
= zfs_symlink(dzp
, name
, &xva
.xva_vattr
,
585 link
, &zp
, kcred
, vflg
, zfs_init_idmap
);
587 error
= zfs_symlink(dzp
, name
, &xva
.xva_vattr
,
588 link
, &zp
, kcred
, vflg
, NULL
);
592 error
= SET_ERROR(ENOTSUP
);
596 if (error
== 0 && zp
!= NULL
) {
598 VOP_UNLOCK1(ZTOV(zp
));
604 if (zfsvfs
->z_fuid_replay
)
605 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
606 zfsvfs
->z_fuid_replay
= NULL
;
611 zfs_replay_remove(void *arg1
, void *arg2
, boolean_t byteswap
)
613 zfsvfs_t
*zfsvfs
= arg1
;
614 lr_remove_t
*lr
= arg2
;
615 char *name
= (char *)(lr
+ 1); /* name follows lr_remove_t */
620 ASSERT3U(lr
->lr_common
.lrc_reclen
, >, sizeof (*lr
));
623 byteswap_uint64_array(lr
, sizeof (*lr
));
625 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
628 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
631 switch ((int)lr
->lr_common
.lrc_txtype
) {
633 error
= zfs_remove(dzp
, name
, kcred
, vflg
);
636 error
= zfs_rmdir(dzp
, name
, NULL
, kcred
, vflg
);
639 error
= SET_ERROR(ENOTSUP
);
648 zfs_replay_link(void *arg1
, void *arg2
, boolean_t byteswap
)
650 zfsvfs_t
*zfsvfs
= arg1
;
651 lr_link_t
*lr
= arg2
;
652 char *name
= (char *)(lr
+ 1); /* name follows lr_link_t */
657 ASSERT3U(lr
->lr_common
.lrc_reclen
, >, sizeof (*lr
));
660 byteswap_uint64_array(lr
, sizeof (*lr
));
662 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
665 if ((error
= zfs_zget(zfsvfs
, lr
->lr_link_obj
, &zp
)) != 0) {
670 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
673 error
= zfs_link(dzp
, zp
, name
, kcred
, vflg
);
681 do_zfs_replay_rename(zfsvfs_t
*zfsvfs
, lr_rename_t
*lr
, char *sname
,
682 char *tname
, uint64_t rflags
, vattr_t
*wo_vap
)
684 znode_t
*sdzp
, *tdzp
;
687 /* Only Linux currently supports RENAME_* flags. */
689 VERIFY0(rflags
& ~(RENAME_EXCHANGE
| RENAME_WHITEOUT
));
691 /* wo_vap must be non-NULL iff. we're doing RENAME_WHITEOUT */
692 VERIFY_EQUIV(rflags
& RENAME_WHITEOUT
, wo_vap
!= NULL
);
697 if ((error
= zfs_zget(zfsvfs
, lr
->lr_sdoid
, &sdzp
)) != 0)
700 if ((error
= zfs_zget(zfsvfs
, lr
->lr_tdoid
, &tdzp
)) != 0) {
705 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
708 #if defined(__linux__)
709 error
= zfs_rename(sdzp
, sname
, tdzp
, tname
, kcred
, vflg
, rflags
,
710 wo_vap
, zfs_init_idmap
);
712 error
= zfs_rename(sdzp
, sname
, tdzp
, tname
, kcred
, vflg
, rflags
,
722 zfs_replay_rename(void *arg1
, void *arg2
, boolean_t byteswap
)
724 zfsvfs_t
*zfsvfs
= arg1
;
725 lr_rename_t
*lr
= arg2
;
727 ASSERT3U(lr
->lr_common
.lrc_reclen
, >, sizeof (*lr
));
730 byteswap_uint64_array(lr
, sizeof (*lr
));
732 char *sname
= (char *)(lr
+ 1); /* sname and tname follow lr_rename_t */
733 char *tname
= sname
+ strlen(sname
) + 1;
734 return (do_zfs_replay_rename(zfsvfs
, lr
, sname
, tname
, 0, NULL
));
738 zfs_replay_rename_exchange(void *arg1
, void *arg2
, boolean_t byteswap
)
741 zfsvfs_t
*zfsvfs
= arg1
;
742 lr_rename_t
*lr
= arg2
;
744 ASSERT3U(lr
->lr_common
.lrc_reclen
, >, sizeof (*lr
));
747 byteswap_uint64_array(lr
, sizeof (*lr
));
749 char *sname
= (char *)(lr
+ 1); /* sname and tname follow lr_rename_t */
750 char *tname
= sname
+ strlen(sname
) + 1;
751 return (do_zfs_replay_rename(zfsvfs
, lr
, sname
, tname
, RENAME_EXCHANGE
,
754 return (SET_ERROR(ENOTSUP
));
759 zfs_replay_rename_whiteout(void *arg1
, void *arg2
, boolean_t byteswap
)
762 zfsvfs_t
*zfsvfs
= arg1
;
763 lr_rename_whiteout_t
*lr
= arg2
;
765 /* For the whiteout file. */
770 ASSERT3U(lr
->lr_rename
.lr_common
.lrc_reclen
, >, sizeof (*lr
));
773 byteswap_uint64_array(lr
, sizeof (*lr
));
775 objid
= LR_FOID_GET_OBJ(lr
->lr_wfoid
);
776 dnodesize
= LR_FOID_GET_SLOTS(lr
->lr_wfoid
) << DNODE_SHIFT
;
779 zfs_init_vattr(&xva
.xva_vattr
, ATTR_MODE
| ATTR_UID
| ATTR_GID
,
780 lr
->lr_wmode
, lr
->lr_wuid
, lr
->lr_wgid
, lr
->lr_wrdev
, objid
);
783 * As with TX_CREATE, RENAME_WHITEOUT ends up in zfs_mknode(), which
784 * assigns the object's creation time, generation number, and dnode
785 * slot count. The generic zfs_rename() has no concept of these
786 * attributes, so we smuggle the values inside the vattr's otherwise
787 * unused va_ctime, va_nblocks, and va_fsid fields.
789 ZFS_TIME_DECODE(&xva
.xva_vattr
.va_ctime
, lr
->lr_wcrtime
);
790 xva
.xva_vattr
.va_nblocks
= lr
->lr_wgen
;
791 xva
.xva_vattr
.va_fsid
= dnodesize
;
793 error
= dnode_try_claim(zfsvfs
->z_os
, objid
, dnodesize
>> DNODE_SHIFT
);
797 /* sname and tname follow lr_rename_whiteout_t */
798 char *sname
= (char *)(lr
+ 1);
799 char *tname
= sname
+ strlen(sname
) + 1;
800 return (do_zfs_replay_rename(zfsvfs
, &lr
->lr_rename
, sname
, tname
,
801 RENAME_WHITEOUT
, &xva
.xva_vattr
));
803 return (SET_ERROR(ENOTSUP
));
808 zfs_replay_write(void *arg1
, void *arg2
, boolean_t byteswap
)
810 zfsvfs_t
*zfsvfs
= arg1
;
811 lr_write_t
*lr
= arg2
;
812 char *data
= (char *)(lr
+ 1); /* data follows lr_write_t */
815 uint64_t eod
, offset
, length
;
817 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
820 byteswap_uint64_array(lr
, sizeof (*lr
));
822 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0) {
824 * As we can log writes out of order, it's possible the
825 * file has been removed. In this case just drop the write
826 * and return success.
833 offset
= lr
->lr_offset
;
834 length
= lr
->lr_length
;
835 eod
= offset
+ length
; /* end of data for this write */
838 * This may be a write from a dmu_sync() for a whole block,
839 * and may extend beyond the current end of the file.
840 * We can't just replay what was written for this TX_WRITE as
841 * a future TX_WRITE2 may extend the eof and the data for that
842 * write needs to be there. So we write the whole block and
843 * reduce the eof. This needs to be done within the single dmu
844 * transaction created within vn_rdwr -> zfs_write. So a possible
845 * new end of file is passed through in zfsvfs->z_replay_eof
848 zfsvfs
->z_replay_eof
= 0; /* 0 means don't change end of file */
850 /* If it's a dmu_sync() block, write the whole block */
851 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
852 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
853 if (length
< blocksize
) {
854 offset
-= offset
% blocksize
;
857 if (zp
->z_size
< eod
)
858 zfsvfs
->z_replay_eof
= eod
;
860 error
= zfs_write_simple(zp
, data
, length
, offset
, NULL
);
862 zfsvfs
->z_replay_eof
= 0; /* safety */
868 * TX_WRITE2 are only generated when dmu_sync() returns EALREADY
869 * meaning the pool block is already being synced. So now that we always write
870 * out full blocks, all we have to do is expand the eof if
874 zfs_replay_write2(void *arg1
, void *arg2
, boolean_t byteswap
)
876 zfsvfs_t
*zfsvfs
= arg1
;
877 lr_write_t
*lr
= arg2
;
882 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
885 byteswap_uint64_array(lr
, sizeof (*lr
));
887 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
891 end
= lr
->lr_offset
+ lr
->lr_length
;
892 if (end
> zp
->z_size
) {
893 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
896 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
897 error
= dmu_tx_assign(tx
, TXG_WAIT
);
900 if (error
== ERESTART
) {
908 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
909 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
911 /* Ensure the replayed seq is updated */
912 (void) zil_replaying(zfsvfs
->z_log
, tx
);
923 zfs_replay_truncate(void *arg1
, void *arg2
, boolean_t byteswap
)
925 zfsvfs_t
*zfsvfs
= arg1
;
926 lr_truncate_t
*lr
= arg2
;
931 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
934 byteswap_uint64_array(lr
, sizeof (*lr
));
936 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
940 fl
.l_whence
= SEEK_SET
;
941 fl
.l_start
= lr
->lr_offset
;
942 fl
.l_len
= lr
->lr_length
;
944 error
= zfs_space(zp
, F_FREESP
, &fl
, O_RDWR
| O_LARGEFILE
,
945 lr
->lr_offset
, kcred
);
953 zfs_replay_setattr(void *arg1
, void *arg2
, boolean_t byteswap
)
955 zfsvfs_t
*zfsvfs
= arg1
;
956 lr_setattr_t
*lr
= arg2
;
959 vattr_t
*vap
= &xva
.xva_vattr
;
963 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
967 byteswap_uint64_array(lr
, sizeof (*lr
));
969 if ((lr
->lr_mask
& ATTR_XVATTR
) &&
970 zfsvfs
->z_version
>= ZPL_VERSION_INITIAL
)
971 zfs_replay_swap_attrs((lr_attr_t
*)(lr
+ 1));
974 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
977 zfs_init_vattr(vap
, lr
->lr_mask
, lr
->lr_mode
,
978 lr
->lr_uid
, lr
->lr_gid
, 0, lr
->lr_foid
);
980 vap
->va_size
= lr
->lr_size
;
981 ZFS_TIME_DECODE(&vap
->va_atime
, lr
->lr_atime
);
982 ZFS_TIME_DECODE(&vap
->va_mtime
, lr
->lr_mtime
);
983 gethrestime(&vap
->va_ctime
);
984 vap
->va_mask
|= ATTR_CTIME
;
987 * Fill in xvattr_t portions if necessary.
990 start
= (lr_setattr_t
*)(lr
+ 1);
991 if (vap
->va_mask
& ATTR_XVATTR
) {
992 zfs_replay_xvattr((lr_attr_t
*)start
, &xva
);
993 start
= (caddr_t
)start
+
994 ZIL_XVAT_SIZE(((lr_attr_t
*)start
)->lr_attr_masksize
);
996 xva
.xva_vattr
.va_mask
&= ~ATTR_XVATTR
;
998 zfsvfs
->z_fuid_replay
= zfs_replay_fuid_domain(start
, &start
,
999 lr
->lr_uid
, lr
->lr_gid
);
1001 #if defined(__linux__)
1002 error
= zfs_setattr(zp
, vap
, 0, kcred
, zfs_init_idmap
);
1004 error
= zfs_setattr(zp
, vap
, 0, kcred
, NULL
);
1007 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
1008 zfsvfs
->z_fuid_replay
= NULL
;
1015 zfs_replay_setsaxattr(void *arg1
, void *arg2
, boolean_t byteswap
)
1017 zfsvfs_t
*zfsvfs
= arg1
;
1018 lr_setsaxattr_t
*lr
= arg2
;
1027 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
1028 ASSERT3U(lr
->lr_common
.lrc_reclen
, >, sizeof (*lr
) + lr
->lr_size
);
1030 ASSERT(spa_feature_is_active(zfsvfs
->z_os
->os_spa
,
1031 SPA_FEATURE_ZILSAXATTR
));
1033 byteswap_uint64_array(lr
, sizeof (*lr
));
1035 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
1038 rw_enter(&zp
->z_xattr_lock
, RW_WRITER
);
1039 mutex_enter(&zp
->z_lock
);
1040 if (zp
->z_xattr_cached
== NULL
)
1041 error
= zfs_sa_get_xattr(zp
);
1042 mutex_exit(&zp
->z_lock
);
1047 ASSERT(zp
->z_xattr_cached
);
1048 nvl
= zp
->z_xattr_cached
;
1050 /* Get xattr name, value and size from log record */
1052 name
= (char *)(lr
+ 1);
1055 error
= nvlist_remove(nvl
, name
, DATA_TYPE_BYTE_ARRAY
);
1057 value
= name
+ strlen(name
) + 1;
1058 /* Limited to 32k to keep nvpair memory allocations small */
1059 if (size
> DXATTR_MAX_ENTRY_SIZE
) {
1060 error
= SET_ERROR(EFBIG
);
1064 /* Prevent the DXATTR SA from consuming the entire SA region */
1065 error
= nvlist_size(nvl
, &sa_size
, NV_ENCODE_XDR
);
1069 if (sa_size
> DXATTR_MAX_SA_SIZE
) {
1070 error
= SET_ERROR(EFBIG
);
1074 error
= nvlist_add_byte_array(nvl
, name
, (uchar_t
*)value
,
1079 * Update the SA for additions, modifications, and removals. On
1080 * error drop the inconsistent cached version of the nvlist, it
1081 * will be reconstructed from the ARC when next accessed.
1084 error
= zfs_sa_set_xattr(zp
, name
, value
, size
);
1088 zp
->z_xattr_cached
= NULL
;
1092 rw_exit(&zp
->z_xattr_lock
);
1098 zfs_replay_acl_v0(void *arg1
, void *arg2
, boolean_t byteswap
)
1100 zfsvfs_t
*zfsvfs
= arg1
;
1101 lr_acl_v0_t
*lr
= arg2
;
1102 ace_t
*ace
= (ace_t
*)(lr
+ 1); /* ace array follows lr_acl_t */
1103 vsecattr_t vsa
= {0};
1107 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
1108 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
) +
1109 sizeof (ace_t
) * lr
->lr_aclcnt
);
1112 byteswap_uint64_array(lr
, sizeof (*lr
));
1113 zfs_oldace_byteswap(ace
, lr
->lr_aclcnt
);
1116 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
1119 vsa
.vsa_mask
= VSA_ACE
| VSA_ACECNT
;
1120 vsa
.vsa_aclcnt
= lr
->lr_aclcnt
;
1121 vsa
.vsa_aclentsz
= sizeof (ace_t
) * vsa
.vsa_aclcnt
;
1122 vsa
.vsa_aclflags
= 0;
1123 vsa
.vsa_aclentp
= ace
;
1125 error
= zfs_setsecattr(zp
, &vsa
, 0, kcred
);
1133 * Replaying ACLs is complicated by FUID support.
1134 * The log record may contain some optional data
1135 * to be used for replaying FUID's. These pieces
1136 * are the actual FUIDs that were created initially.
1137 * The FUID table index may no longer be valid and
1138 * during zfs_create() a new index may be assigned.
1139 * Because of this the log will contain the original
1140 * domain+rid in order to create a new FUID.
1142 * The individual ACEs may contain an ephemeral uid/gid which is no
1143 * longer valid and will need to be replaced with an actual FUID.
1147 zfs_replay_acl(void *arg1
, void *arg2
, boolean_t byteswap
)
1149 zfsvfs_t
*zfsvfs
= arg1
;
1150 lr_acl_t
*lr
= arg2
;
1151 ace_t
*ace
= (ace_t
*)(lr
+ 1);
1152 vsecattr_t vsa
= {0};
1156 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
1157 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
) + lr
->lr_acl_bytes
);
1160 byteswap_uint64_array(lr
, sizeof (*lr
));
1161 zfs_ace_byteswap(ace
, lr
->lr_acl_bytes
, B_FALSE
);
1162 if (lr
->lr_fuidcnt
) {
1163 byteswap_uint64_array((caddr_t
)ace
+
1164 ZIL_ACE_LENGTH(lr
->lr_acl_bytes
),
1165 lr
->lr_fuidcnt
* sizeof (uint64_t));
1169 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
1172 vsa
.vsa_mask
= VSA_ACE
| VSA_ACECNT
| VSA_ACE_ACLFLAGS
;
1173 vsa
.vsa_aclcnt
= lr
->lr_aclcnt
;
1174 vsa
.vsa_aclentp
= ace
;
1175 vsa
.vsa_aclentsz
= lr
->lr_acl_bytes
;
1176 vsa
.vsa_aclflags
= lr
->lr_acl_flags
;
1178 if (lr
->lr_fuidcnt
) {
1179 void *fuidstart
= (caddr_t
)ace
+
1180 ZIL_ACE_LENGTH(lr
->lr_acl_bytes
);
1182 zfsvfs
->z_fuid_replay
=
1183 zfs_replay_fuids(fuidstart
, &fuidstart
,
1184 lr
->lr_fuidcnt
, lr
->lr_domcnt
, 0, 0);
1187 error
= zfs_setsecattr(zp
, &vsa
, 0, kcred
);
1189 if (zfsvfs
->z_fuid_replay
)
1190 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
1192 zfsvfs
->z_fuid_replay
= NULL
;
1199 zfs_replay_clone_range(void *arg1
, void *arg2
, boolean_t byteswap
)
1201 zfsvfs_t
*zfsvfs
= arg1
;
1202 lr_clone_range_t
*lr
= arg2
;
1206 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
1207 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, offsetof(lr_clone_range_t
,
1208 lr_bps
[lr
->lr_nbps
]));
1211 byteswap_uint64_array(lr
, sizeof (*lr
));
1213 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0) {
1215 * Clones can be logged out of order, so don't be surprised if
1216 * the file is gone - just return success.
1218 if (error
== ENOENT
)
1223 error
= zfs_clone_range_replay(zp
, lr
->lr_offset
, lr
->lr_length
,
1224 lr
->lr_blksz
, lr
->lr_bps
, lr
->lr_nbps
);
1231 * Callback vectors for replaying records
1233 zil_replay_func_t
*const zfs_replay_vector
[TX_MAX_TYPE
] = {
1234 zfs_replay_error
, /* no such type */
1235 zfs_replay_create
, /* TX_CREATE */
1236 zfs_replay_create
, /* TX_MKDIR */
1237 zfs_replay_create
, /* TX_MKXATTR */
1238 zfs_replay_create
, /* TX_SYMLINK */
1239 zfs_replay_remove
, /* TX_REMOVE */
1240 zfs_replay_remove
, /* TX_RMDIR */
1241 zfs_replay_link
, /* TX_LINK */
1242 zfs_replay_rename
, /* TX_RENAME */
1243 zfs_replay_write
, /* TX_WRITE */
1244 zfs_replay_truncate
, /* TX_TRUNCATE */
1245 zfs_replay_setattr
, /* TX_SETATTR */
1246 zfs_replay_acl_v0
, /* TX_ACL_V0 */
1247 zfs_replay_acl
, /* TX_ACL */
1248 zfs_replay_create_acl
, /* TX_CREATE_ACL */
1249 zfs_replay_create
, /* TX_CREATE_ATTR */
1250 zfs_replay_create_acl
, /* TX_CREATE_ACL_ATTR */
1251 zfs_replay_create_acl
, /* TX_MKDIR_ACL */
1252 zfs_replay_create
, /* TX_MKDIR_ATTR */
1253 zfs_replay_create_acl
, /* TX_MKDIR_ACL_ATTR */
1254 zfs_replay_write2
, /* TX_WRITE2 */
1255 zfs_replay_setsaxattr
, /* TX_SETSAXATTR */
1256 zfs_replay_rename_exchange
, /* TX_RENAME_EXCHANGE */
1257 zfs_replay_rename_whiteout
, /* TX_RENAME_WHITEOUT */
1258 zfs_replay_clone_range
, /* TX_CLONE_RANGE */