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 txtype
= (lr
->lr_common
.lrc_txtype
& ~TX_CI
);
314 byteswap_uint64_array(lracl
, sizeof (*lracl
));
315 if (txtype
== TX_CREATE_ACL_ATTR
||
316 txtype
== TX_MKDIR_ACL_ATTR
) {
317 lrattr
= (lr_attr_t
*)(caddr_t
)(lracl
+ 1);
318 zfs_replay_swap_attrs(lrattr
);
319 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
322 aclstart
= (caddr_t
)(lracl
+ 1) + xvatlen
;
323 zfs_ace_byteswap(aclstart
, lracl
->lr_acl_bytes
, B_FALSE
);
325 if (lracl
->lr_fuidcnt
) {
326 byteswap_uint64_array((caddr_t
)aclstart
+
327 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
),
328 lracl
->lr_fuidcnt
* sizeof (uint64_t));
332 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
335 objid
= LR_FOID_GET_OBJ(lr
->lr_foid
);
336 dnodesize
= LR_FOID_GET_SLOTS(lr
->lr_foid
) << DNODE_SHIFT
;
339 zfs_init_vattr(&xva
.xva_vattr
, ATTR_MODE
| ATTR_UID
| ATTR_GID
,
340 lr
->lr_mode
, lr
->lr_uid
, lr
->lr_gid
, lr
->lr_rdev
, objid
);
343 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
344 * eventually end up in zfs_mknode(), which assigns the object's
345 * creation time, generation number, and dnode size. The generic
346 * zfs_create() has no concept of these attributes, so we smuggle
347 * the values inside the vattr's otherwise unused va_ctime,
348 * va_nblocks, and va_fsid fields.
350 ZFS_TIME_DECODE(&xva
.xva_vattr
.va_ctime
, lr
->lr_crtime
);
351 xva
.xva_vattr
.va_nblocks
= lr
->lr_gen
;
352 xva
.xva_vattr
.va_fsid
= dnodesize
;
354 error
= dnode_try_claim(zfsvfs
->z_os
, objid
, dnodesize
>> DNODE_SHIFT
);
358 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
362 aclstart
= (caddr_t
)(lracl
+ 1);
363 fuidstart
= (caddr_t
)aclstart
+
364 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
365 zfsvfs
->z_fuid_replay
= zfs_replay_fuids(fuidstart
,
366 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
367 lr
->lr_uid
, lr
->lr_gid
);
369 case TX_CREATE_ACL_ATTR
:
371 lrattr
= (lr_attr_t
*)(caddr_t
)(lracl
+ 1);
372 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
373 xva
.xva_vattr
.va_mask
|= ATTR_XVATTR
;
374 zfs_replay_xvattr(lrattr
, &xva
);
376 vsec
.vsa_mask
= VSA_ACE
| VSA_ACE_ACLFLAGS
;
377 vsec
.vsa_aclentp
= (caddr_t
)(lracl
+ 1) + xvatlen
;
378 vsec
.vsa_aclcnt
= lracl
->lr_aclcnt
;
379 vsec
.vsa_aclentsz
= lracl
->lr_acl_bytes
;
380 vsec
.vsa_aclflags
= lracl
->lr_acl_flags
;
381 if (zfsvfs
->z_fuid_replay
== NULL
) {
382 fuidstart
= (caddr_t
)(lracl
+ 1) + xvatlen
+
383 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
384 zfsvfs
->z_fuid_replay
=
385 zfs_replay_fuids(fuidstart
,
386 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
387 lr
->lr_uid
, lr
->lr_gid
);
390 #if defined(__linux__)
391 error
= zfs_create(dzp
, name
, &xva
.xva_vattr
,
392 0, 0, &zp
, kcred
, vflg
, &vsec
, zfs_init_idmap
);
394 error
= zfs_create(dzp
, name
, &xva
.xva_vattr
,
395 0, 0, &zp
, kcred
, vflg
, &vsec
, NULL
);
399 aclstart
= (caddr_t
)(lracl
+ 1);
400 fuidstart
= (caddr_t
)aclstart
+
401 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
402 zfsvfs
->z_fuid_replay
= zfs_replay_fuids(fuidstart
,
403 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
404 lr
->lr_uid
, lr
->lr_gid
);
406 case TX_MKDIR_ACL_ATTR
:
408 lrattr
= (lr_attr_t
*)(caddr_t
)(lracl
+ 1);
409 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
410 zfs_replay_xvattr(lrattr
, &xva
);
412 vsec
.vsa_mask
= VSA_ACE
| VSA_ACE_ACLFLAGS
;
413 vsec
.vsa_aclentp
= (caddr_t
)(lracl
+ 1) + xvatlen
;
414 vsec
.vsa_aclcnt
= lracl
->lr_aclcnt
;
415 vsec
.vsa_aclentsz
= lracl
->lr_acl_bytes
;
416 vsec
.vsa_aclflags
= lracl
->lr_acl_flags
;
417 if (zfsvfs
->z_fuid_replay
== NULL
) {
418 fuidstart
= (caddr_t
)(lracl
+ 1) + xvatlen
+
419 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
420 zfsvfs
->z_fuid_replay
=
421 zfs_replay_fuids(fuidstart
,
422 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
423 lr
->lr_uid
, lr
->lr_gid
);
425 #if defined(__linux__)
426 error
= zfs_mkdir(dzp
, name
, &xva
.xva_vattr
,
427 &zp
, kcred
, vflg
, &vsec
, zfs_init_idmap
);
429 error
= zfs_mkdir(dzp
, name
, &xva
.xva_vattr
,
430 &zp
, kcred
, vflg
, &vsec
, NULL
);
434 error
= SET_ERROR(ENOTSUP
);
438 if (error
== 0 && zp
!= NULL
) {
440 VOP_UNLOCK1(ZTOV(zp
));
446 if (zfsvfs
->z_fuid_replay
)
447 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
448 zfsvfs
->z_fuid_replay
= NULL
;
454 zfs_replay_create(void *arg1
, void *arg2
, boolean_t byteswap
)
456 zfsvfs_t
*zfsvfs
= arg1
;
457 lr_create_t
*lr
= arg2
;
458 char *name
= NULL
; /* location determined later */
459 char *link
; /* symlink content follows name */
464 size_t lrsize
= sizeof (lr_create_t
);
473 txtype
= (lr
->lr_common
.lrc_txtype
& ~TX_CI
);
475 byteswap_uint64_array(lr
, sizeof (*lr
));
476 if (txtype
== TX_CREATE_ATTR
|| txtype
== TX_MKDIR_ATTR
)
477 zfs_replay_swap_attrs((lr_attr_t
*)(lr
+ 1));
481 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
484 objid
= LR_FOID_GET_OBJ(lr
->lr_foid
);
485 dnodesize
= LR_FOID_GET_SLOTS(lr
->lr_foid
) << DNODE_SHIFT
;
488 zfs_init_vattr(&xva
.xva_vattr
, ATTR_MODE
| ATTR_UID
| ATTR_GID
,
489 lr
->lr_mode
, lr
->lr_uid
, lr
->lr_gid
, lr
->lr_rdev
, objid
);
492 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
493 * eventually end up in zfs_mknode(), which assigns the object's
494 * creation time, generation number, and dnode slot count. The
495 * generic zfs_create() has no concept of these attributes, so
496 * we smuggle the values inside the vattr's otherwise unused
497 * va_ctime, va_nblocks, and va_fsid fields.
499 ZFS_TIME_DECODE(&xva
.xva_vattr
.va_ctime
, lr
->lr_crtime
);
500 xva
.xva_vattr
.va_nblocks
= lr
->lr_gen
;
501 xva
.xva_vattr
.va_fsid
= dnodesize
;
503 error
= dnode_try_claim(zfsvfs
->z_os
, objid
, dnodesize
>> DNODE_SHIFT
);
507 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
511 * Symlinks don't have fuid info, and CIFS never creates
514 * The _ATTR versions will grab the fuid info in their subcases.
516 if (txtype
!= TX_SYMLINK
&&
517 txtype
!= TX_MKDIR_ATTR
&&
518 txtype
!= TX_CREATE_ATTR
) {
520 zfsvfs
->z_fuid_replay
=
521 zfs_replay_fuid_domain(start
, &start
,
522 lr
->lr_uid
, lr
->lr_gid
);
527 lrattr
= (lr_attr_t
*)(caddr_t
)(lr
+ 1);
528 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
529 zfs_replay_xvattr((lr_attr_t
*)((caddr_t
)lr
+ lrsize
), &xva
);
530 start
= (caddr_t
)(lr
+ 1) + xvatlen
;
531 zfsvfs
->z_fuid_replay
=
532 zfs_replay_fuid_domain(start
, &start
,
533 lr
->lr_uid
, lr
->lr_gid
);
534 name
= (char *)start
;
539 name
= (char *)start
;
541 #if defined(__linux__)
542 error
= zfs_create(dzp
, name
, &xva
.xva_vattr
,
543 0, 0, &zp
, kcred
, vflg
, NULL
, zfs_init_idmap
);
545 error
= zfs_create(dzp
, name
, &xva
.xva_vattr
,
546 0, 0, &zp
, kcred
, vflg
, NULL
, NULL
);
550 lrattr
= (lr_attr_t
*)(caddr_t
)(lr
+ 1);
551 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
552 zfs_replay_xvattr((lr_attr_t
*)((caddr_t
)lr
+ lrsize
), &xva
);
553 start
= (caddr_t
)(lr
+ 1) + xvatlen
;
554 zfsvfs
->z_fuid_replay
=
555 zfs_replay_fuid_domain(start
, &start
,
556 lr
->lr_uid
, lr
->lr_gid
);
557 name
= (char *)start
;
562 name
= (char *)(lr
+ 1);
564 #if defined(__linux__)
565 error
= zfs_mkdir(dzp
, name
, &xva
.xva_vattr
,
566 &zp
, kcred
, vflg
, NULL
, zfs_init_idmap
);
568 error
= zfs_mkdir(dzp
, name
, &xva
.xva_vattr
,
569 &zp
, kcred
, vflg
, NULL
, NULL
);
574 error
= zfs_make_xattrdir(dzp
, &xva
.xva_vattr
, &zp
, kcred
);
577 name
= (char *)(lr
+ 1);
578 link
= name
+ strlen(name
) + 1;
579 #if defined(__linux__)
580 error
= zfs_symlink(dzp
, name
, &xva
.xva_vattr
,
581 link
, &zp
, kcred
, vflg
, zfs_init_idmap
);
583 error
= zfs_symlink(dzp
, name
, &xva
.xva_vattr
,
584 link
, &zp
, kcred
, vflg
, NULL
);
588 error
= SET_ERROR(ENOTSUP
);
592 if (error
== 0 && zp
!= NULL
) {
594 VOP_UNLOCK1(ZTOV(zp
));
600 if (zfsvfs
->z_fuid_replay
)
601 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
602 zfsvfs
->z_fuid_replay
= NULL
;
607 zfs_replay_remove(void *arg1
, void *arg2
, boolean_t byteswap
)
609 zfsvfs_t
*zfsvfs
= arg1
;
610 lr_remove_t
*lr
= arg2
;
611 char *name
= (char *)(lr
+ 1); /* name follows lr_remove_t */
617 byteswap_uint64_array(lr
, sizeof (*lr
));
619 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
622 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
625 switch ((int)lr
->lr_common
.lrc_txtype
) {
627 error
= zfs_remove(dzp
, name
, kcred
, vflg
);
630 error
= zfs_rmdir(dzp
, name
, NULL
, kcred
, vflg
);
633 error
= SET_ERROR(ENOTSUP
);
642 zfs_replay_link(void *arg1
, void *arg2
, boolean_t byteswap
)
644 zfsvfs_t
*zfsvfs
= arg1
;
645 lr_link_t
*lr
= arg2
;
646 char *name
= (char *)(lr
+ 1); /* name follows lr_link_t */
652 byteswap_uint64_array(lr
, sizeof (*lr
));
654 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
657 if ((error
= zfs_zget(zfsvfs
, lr
->lr_link_obj
, &zp
)) != 0) {
662 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
665 error
= zfs_link(dzp
, zp
, name
, kcred
, vflg
);
673 do_zfs_replay_rename(zfsvfs_t
*zfsvfs
, lr_rename_t
*lr
, char *sname
,
674 char *tname
, uint64_t rflags
, vattr_t
*wo_vap
)
676 znode_t
*sdzp
, *tdzp
;
679 /* Only Linux currently supports RENAME_* flags. */
681 VERIFY0(rflags
& ~(RENAME_EXCHANGE
| RENAME_WHITEOUT
));
683 /* wo_vap must be non-NULL iff. we're doing RENAME_WHITEOUT */
684 VERIFY_EQUIV(rflags
& RENAME_WHITEOUT
, wo_vap
!= NULL
);
689 if ((error
= zfs_zget(zfsvfs
, lr
->lr_sdoid
, &sdzp
)) != 0)
692 if ((error
= zfs_zget(zfsvfs
, lr
->lr_tdoid
, &tdzp
)) != 0) {
697 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
700 #if defined(__linux__)
701 error
= zfs_rename(sdzp
, sname
, tdzp
, tname
, kcred
, vflg
, rflags
,
702 wo_vap
, zfs_init_idmap
);
704 error
= zfs_rename(sdzp
, sname
, tdzp
, tname
, kcred
, vflg
, rflags
,
714 zfs_replay_rename(void *arg1
, void *arg2
, boolean_t byteswap
)
716 zfsvfs_t
*zfsvfs
= arg1
;
717 lr_rename_t
*lr
= arg2
;
718 char *sname
= (char *)(lr
+ 1); /* sname and tname follow lr_rename_t */
719 char *tname
= sname
+ strlen(sname
) + 1;
722 byteswap_uint64_array(lr
, sizeof (*lr
));
724 return (do_zfs_replay_rename(zfsvfs
, lr
, sname
, tname
, 0, NULL
));
728 zfs_replay_rename_exchange(void *arg1
, void *arg2
, boolean_t byteswap
)
731 zfsvfs_t
*zfsvfs
= arg1
;
732 lr_rename_t
*lr
= arg2
;
733 char *sname
= (char *)(lr
+ 1); /* sname and tname follow lr_rename_t */
734 char *tname
= sname
+ strlen(sname
) + 1;
737 byteswap_uint64_array(lr
, sizeof (*lr
));
739 return (do_zfs_replay_rename(zfsvfs
, lr
, sname
, tname
, RENAME_EXCHANGE
,
742 return (SET_ERROR(ENOTSUP
));
747 zfs_replay_rename_whiteout(void *arg1
, void *arg2
, boolean_t byteswap
)
750 zfsvfs_t
*zfsvfs
= arg1
;
751 lr_rename_whiteout_t
*lr
= arg2
;
753 /* sname and tname follow lr_rename_whiteout_t */
754 char *sname
= (char *)(lr
+ 1);
755 char *tname
= sname
+ strlen(sname
) + 1;
756 /* For the whiteout file. */
762 byteswap_uint64_array(lr
, sizeof (*lr
));
764 objid
= LR_FOID_GET_OBJ(lr
->lr_wfoid
);
765 dnodesize
= LR_FOID_GET_SLOTS(lr
->lr_wfoid
) << DNODE_SHIFT
;
768 zfs_init_vattr(&xva
.xva_vattr
, ATTR_MODE
| ATTR_UID
| ATTR_GID
,
769 lr
->lr_wmode
, lr
->lr_wuid
, lr
->lr_wgid
, lr
->lr_wrdev
, objid
);
772 * As with TX_CREATE, RENAME_WHITEOUT ends up in zfs_mknode(), which
773 * assigns the object's creation time, generation number, and dnode
774 * slot count. The generic zfs_rename() has no concept of these
775 * attributes, so we smuggle the values inside the vattr's otherwise
776 * unused va_ctime, va_nblocks, and va_fsid fields.
778 ZFS_TIME_DECODE(&xva
.xva_vattr
.va_ctime
, lr
->lr_wcrtime
);
779 xva
.xva_vattr
.va_nblocks
= lr
->lr_wgen
;
780 xva
.xva_vattr
.va_fsid
= dnodesize
;
782 error
= dnode_try_claim(zfsvfs
->z_os
, objid
, dnodesize
>> DNODE_SHIFT
);
786 return (do_zfs_replay_rename(zfsvfs
, &lr
->lr_rename
, sname
, tname
,
787 RENAME_WHITEOUT
, &xva
.xva_vattr
));
789 return (SET_ERROR(ENOTSUP
));
794 zfs_replay_write(void *arg1
, void *arg2
, boolean_t byteswap
)
796 zfsvfs_t
*zfsvfs
= arg1
;
797 lr_write_t
*lr
= arg2
;
798 char *data
= (char *)(lr
+ 1); /* data follows lr_write_t */
801 uint64_t eod
, offset
, length
;
804 byteswap_uint64_array(lr
, sizeof (*lr
));
806 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0) {
808 * As we can log writes out of order, it's possible the
809 * file has been removed. In this case just drop the write
810 * and return success.
817 offset
= lr
->lr_offset
;
818 length
= lr
->lr_length
;
819 eod
= offset
+ length
; /* end of data for this write */
822 * This may be a write from a dmu_sync() for a whole block,
823 * and may extend beyond the current end of the file.
824 * We can't just replay what was written for this TX_WRITE as
825 * a future TX_WRITE2 may extend the eof and the data for that
826 * write needs to be there. So we write the whole block and
827 * reduce the eof. This needs to be done within the single dmu
828 * transaction created within vn_rdwr -> zfs_write. So a possible
829 * new end of file is passed through in zfsvfs->z_replay_eof
832 zfsvfs
->z_replay_eof
= 0; /* 0 means don't change end of file */
834 /* If it's a dmu_sync() block, write the whole block */
835 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
836 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
837 if (length
< blocksize
) {
838 offset
-= offset
% blocksize
;
841 if (zp
->z_size
< eod
)
842 zfsvfs
->z_replay_eof
= eod
;
844 error
= zfs_write_simple(zp
, data
, length
, offset
, NULL
);
846 zfsvfs
->z_replay_eof
= 0; /* safety */
852 * TX_WRITE2 are only generated when dmu_sync() returns EALREADY
853 * meaning the pool block is already being synced. So now that we always write
854 * out full blocks, all we have to do is expand the eof if
858 zfs_replay_write2(void *arg1
, void *arg2
, boolean_t byteswap
)
860 zfsvfs_t
*zfsvfs
= arg1
;
861 lr_write_t
*lr
= arg2
;
867 byteswap_uint64_array(lr
, sizeof (*lr
));
869 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
873 end
= lr
->lr_offset
+ lr
->lr_length
;
874 if (end
> zp
->z_size
) {
875 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
878 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
879 error
= dmu_tx_assign(tx
, TXG_WAIT
);
882 if (error
== ERESTART
) {
890 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
891 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
893 /* Ensure the replayed seq is updated */
894 (void) zil_replaying(zfsvfs
->z_log
, tx
);
905 zfs_replay_truncate(void *arg1
, void *arg2
, boolean_t byteswap
)
907 zfsvfs_t
*zfsvfs
= arg1
;
908 lr_truncate_t
*lr
= arg2
;
914 byteswap_uint64_array(lr
, sizeof (*lr
));
916 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
920 fl
.l_whence
= SEEK_SET
;
921 fl
.l_start
= lr
->lr_offset
;
922 fl
.l_len
= lr
->lr_length
;
924 error
= zfs_space(zp
, F_FREESP
, &fl
, O_RDWR
| O_LARGEFILE
,
925 lr
->lr_offset
, kcred
);
933 zfs_replay_setattr(void *arg1
, void *arg2
, boolean_t byteswap
)
935 zfsvfs_t
*zfsvfs
= arg1
;
936 lr_setattr_t
*lr
= arg2
;
939 vattr_t
*vap
= &xva
.xva_vattr
;
945 byteswap_uint64_array(lr
, sizeof (*lr
));
947 if ((lr
->lr_mask
& ATTR_XVATTR
) &&
948 zfsvfs
->z_version
>= ZPL_VERSION_INITIAL
)
949 zfs_replay_swap_attrs((lr_attr_t
*)(lr
+ 1));
952 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
955 zfs_init_vattr(vap
, lr
->lr_mask
, lr
->lr_mode
,
956 lr
->lr_uid
, lr
->lr_gid
, 0, lr
->lr_foid
);
958 vap
->va_size
= lr
->lr_size
;
959 ZFS_TIME_DECODE(&vap
->va_atime
, lr
->lr_atime
);
960 ZFS_TIME_DECODE(&vap
->va_mtime
, lr
->lr_mtime
);
961 gethrestime(&vap
->va_ctime
);
962 vap
->va_mask
|= ATTR_CTIME
;
965 * Fill in xvattr_t portions if necessary.
968 start
= (lr_setattr_t
*)(lr
+ 1);
969 if (vap
->va_mask
& ATTR_XVATTR
) {
970 zfs_replay_xvattr((lr_attr_t
*)start
, &xva
);
971 start
= (caddr_t
)start
+
972 ZIL_XVAT_SIZE(((lr_attr_t
*)start
)->lr_attr_masksize
);
974 xva
.xva_vattr
.va_mask
&= ~ATTR_XVATTR
;
976 zfsvfs
->z_fuid_replay
= zfs_replay_fuid_domain(start
, &start
,
977 lr
->lr_uid
, lr
->lr_gid
);
979 #if defined(__linux__)
980 error
= zfs_setattr(zp
, vap
, 0, kcred
, zfs_init_idmap
);
982 error
= zfs_setattr(zp
, vap
, 0, kcred
, NULL
);
985 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
986 zfsvfs
->z_fuid_replay
= NULL
;
993 zfs_replay_setsaxattr(void *arg1
, void *arg2
, boolean_t byteswap
)
995 zfsvfs_t
*zfsvfs
= arg1
;
996 lr_setsaxattr_t
*lr
= arg2
;
1005 ASSERT(spa_feature_is_active(zfsvfs
->z_os
->os_spa
,
1006 SPA_FEATURE_ZILSAXATTR
));
1008 byteswap_uint64_array(lr
, sizeof (*lr
));
1010 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
1013 rw_enter(&zp
->z_xattr_lock
, RW_WRITER
);
1014 mutex_enter(&zp
->z_lock
);
1015 if (zp
->z_xattr_cached
== NULL
)
1016 error
= zfs_sa_get_xattr(zp
);
1017 mutex_exit(&zp
->z_lock
);
1022 ASSERT(zp
->z_xattr_cached
);
1023 nvl
= zp
->z_xattr_cached
;
1025 /* Get xattr name, value and size from log record */
1027 name
= (char *)(lr
+ 1);
1030 error
= nvlist_remove(nvl
, name
, DATA_TYPE_BYTE_ARRAY
);
1032 value
= name
+ strlen(name
) + 1;
1033 /* Limited to 32k to keep nvpair memory allocations small */
1034 if (size
> DXATTR_MAX_ENTRY_SIZE
) {
1035 error
= SET_ERROR(EFBIG
);
1039 /* Prevent the DXATTR SA from consuming the entire SA region */
1040 error
= nvlist_size(nvl
, &sa_size
, NV_ENCODE_XDR
);
1044 if (sa_size
> DXATTR_MAX_SA_SIZE
) {
1045 error
= SET_ERROR(EFBIG
);
1049 error
= nvlist_add_byte_array(nvl
, name
, (uchar_t
*)value
,
1054 * Update the SA for additions, modifications, and removals. On
1055 * error drop the inconsistent cached version of the nvlist, it
1056 * will be reconstructed from the ARC when next accessed.
1059 error
= zfs_sa_set_xattr(zp
, name
, value
, size
);
1063 zp
->z_xattr_cached
= NULL
;
1067 rw_exit(&zp
->z_xattr_lock
);
1073 zfs_replay_acl_v0(void *arg1
, void *arg2
, boolean_t byteswap
)
1075 zfsvfs_t
*zfsvfs
= arg1
;
1076 lr_acl_v0_t
*lr
= arg2
;
1077 ace_t
*ace
= (ace_t
*)(lr
+ 1); /* ace array follows lr_acl_t */
1078 vsecattr_t vsa
= {0};
1083 byteswap_uint64_array(lr
, sizeof (*lr
));
1084 zfs_oldace_byteswap(ace
, lr
->lr_aclcnt
);
1087 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
1090 vsa
.vsa_mask
= VSA_ACE
| VSA_ACECNT
;
1091 vsa
.vsa_aclcnt
= lr
->lr_aclcnt
;
1092 vsa
.vsa_aclentsz
= sizeof (ace_t
) * vsa
.vsa_aclcnt
;
1093 vsa
.vsa_aclflags
= 0;
1094 vsa
.vsa_aclentp
= ace
;
1096 error
= zfs_setsecattr(zp
, &vsa
, 0, kcred
);
1104 * Replaying ACLs is complicated by FUID support.
1105 * The log record may contain some optional data
1106 * to be used for replaying FUID's. These pieces
1107 * are the actual FUIDs that were created initially.
1108 * The FUID table index may no longer be valid and
1109 * during zfs_create() a new index may be assigned.
1110 * Because of this the log will contain the original
1111 * domain+rid in order to create a new FUID.
1113 * The individual ACEs may contain an ephemeral uid/gid which is no
1114 * longer valid and will need to be replaced with an actual FUID.
1118 zfs_replay_acl(void *arg1
, void *arg2
, boolean_t byteswap
)
1120 zfsvfs_t
*zfsvfs
= arg1
;
1121 lr_acl_t
*lr
= arg2
;
1122 ace_t
*ace
= (ace_t
*)(lr
+ 1);
1123 vsecattr_t vsa
= {0};
1128 byteswap_uint64_array(lr
, sizeof (*lr
));
1129 zfs_ace_byteswap(ace
, lr
->lr_acl_bytes
, B_FALSE
);
1130 if (lr
->lr_fuidcnt
) {
1131 byteswap_uint64_array((caddr_t
)ace
+
1132 ZIL_ACE_LENGTH(lr
->lr_acl_bytes
),
1133 lr
->lr_fuidcnt
* sizeof (uint64_t));
1137 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
1140 vsa
.vsa_mask
= VSA_ACE
| VSA_ACECNT
| VSA_ACE_ACLFLAGS
;
1141 vsa
.vsa_aclcnt
= lr
->lr_aclcnt
;
1142 vsa
.vsa_aclentp
= ace
;
1143 vsa
.vsa_aclentsz
= lr
->lr_acl_bytes
;
1144 vsa
.vsa_aclflags
= lr
->lr_acl_flags
;
1146 if (lr
->lr_fuidcnt
) {
1147 void *fuidstart
= (caddr_t
)ace
+
1148 ZIL_ACE_LENGTH(lr
->lr_acl_bytes
);
1150 zfsvfs
->z_fuid_replay
=
1151 zfs_replay_fuids(fuidstart
, &fuidstart
,
1152 lr
->lr_fuidcnt
, lr
->lr_domcnt
, 0, 0);
1155 error
= zfs_setsecattr(zp
, &vsa
, 0, kcred
);
1157 if (zfsvfs
->z_fuid_replay
)
1158 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
1160 zfsvfs
->z_fuid_replay
= NULL
;
1167 zfs_replay_clone_range(void *arg1
, void *arg2
, boolean_t byteswap
)
1169 zfsvfs_t
*zfsvfs
= arg1
;
1170 lr_clone_range_t
*lr
= arg2
;
1175 byteswap_uint64_array(lr
, sizeof (*lr
));
1177 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0) {
1179 * Clones can be logged out of order, so don't be surprised if
1180 * the file is gone - just return success.
1182 if (error
== ENOENT
)
1187 error
= zfs_clone_range_replay(zp
, lr
->lr_offset
, lr
->lr_length
,
1188 lr
->lr_blksz
, lr
->lr_bps
, lr
->lr_nbps
);
1195 * Callback vectors for replaying records
1197 zil_replay_func_t
*const zfs_replay_vector
[TX_MAX_TYPE
] = {
1198 zfs_replay_error
, /* no such type */
1199 zfs_replay_create
, /* TX_CREATE */
1200 zfs_replay_create
, /* TX_MKDIR */
1201 zfs_replay_create
, /* TX_MKXATTR */
1202 zfs_replay_create
, /* TX_SYMLINK */
1203 zfs_replay_remove
, /* TX_REMOVE */
1204 zfs_replay_remove
, /* TX_RMDIR */
1205 zfs_replay_link
, /* TX_LINK */
1206 zfs_replay_rename
, /* TX_RENAME */
1207 zfs_replay_write
, /* TX_WRITE */
1208 zfs_replay_truncate
, /* TX_TRUNCATE */
1209 zfs_replay_setattr
, /* TX_SETATTR */
1210 zfs_replay_acl_v0
, /* TX_ACL_V0 */
1211 zfs_replay_acl
, /* TX_ACL */
1212 zfs_replay_create_acl
, /* TX_CREATE_ACL */
1213 zfs_replay_create
, /* TX_CREATE_ATTR */
1214 zfs_replay_create_acl
, /* TX_CREATE_ACL_ATTR */
1215 zfs_replay_create_acl
, /* TX_MKDIR_ACL */
1216 zfs_replay_create
, /* TX_MKDIR_ATTR */
1217 zfs_replay_create_acl
, /* TX_MKDIR_ACL_ATTR */
1218 zfs_replay_write2
, /* TX_WRITE2 */
1219 zfs_replay_setsaxattr
, /* TX_SETSAXATTR */
1220 zfs_replay_rename_exchange
, /* TX_RENAME_EXCHANGE */
1221 zfs_replay_rename_whiteout
, /* TX_RENAME_WHITEOUT */
1222 zfs_replay_clone_range
, /* TX_CLONE_RANGE */