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 _lr_create_t
*lr
= &lracl
->lr_create
;
297 char *name
= NULL
; /* location determined later */
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
*)&lracl
->lr_data
[0];
320 zfs_replay_swap_attrs(lrattr
);
321 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
324 aclstart
= &lracl
->lr_data
[xvatlen
];
325 zfs_ace_byteswap(aclstart
, lracl
->lr_acl_bytes
, B_FALSE
);
328 if (lracl
->lr_fuidcnt
) {
329 byteswap_uint64_array(
330 &aclstart
[ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
)],
331 lracl
->lr_fuidcnt
* sizeof (uint64_t));
335 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
338 objid
= LR_FOID_GET_OBJ(lr
->lr_foid
);
339 dnodesize
= LR_FOID_GET_SLOTS(lr
->lr_foid
) << DNODE_SHIFT
;
342 zfs_init_vattr(&xva
.xva_vattr
, ATTR_MODE
| ATTR_UID
| ATTR_GID
,
343 lr
->lr_mode
, lr
->lr_uid
, lr
->lr_gid
, lr
->lr_rdev
, objid
);
346 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
347 * eventually end up in zfs_mknode(), which assigns the object's
348 * creation time, generation number, and dnode size. The generic
349 * zfs_create() has no concept of these attributes, so we smuggle
350 * the values inside the vattr's otherwise unused va_ctime,
351 * va_nblocks, and va_fsid fields.
353 ZFS_TIME_DECODE(&xva
.xva_vattr
.va_ctime
, lr
->lr_crtime
);
354 xva
.xva_vattr
.va_nblocks
= lr
->lr_gen
;
355 xva
.xva_vattr
.va_fsid
= dnodesize
;
357 error
= dnode_try_claim(zfsvfs
->z_os
, objid
, dnodesize
>> DNODE_SHIFT
);
361 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
365 aclstart
= &lracl
->lr_data
[0];
366 fuidstart
= &aclstart
[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
*)&lracl
->lr_data
[0];
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
= &lracl
->lr_data
[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
= &lracl
->lr_data
[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
= &lracl
->lr_data
[0];
402 fuidstart
= &aclstart
[ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
)];
403 zfsvfs
->z_fuid_replay
= zfs_replay_fuids(fuidstart
,
404 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
405 lr
->lr_uid
, lr
->lr_gid
);
407 case TX_MKDIR_ACL_ATTR
:
409 lrattr
= (lr_attr_t
*)(caddr_t
)(lracl
+ 1);
410 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
411 zfs_replay_xvattr(lrattr
, &xva
);
413 vsec
.vsa_mask
= VSA_ACE
| VSA_ACE_ACLFLAGS
;
414 vsec
.vsa_aclentp
= &lracl
->lr_data
[xvatlen
];
415 vsec
.vsa_aclcnt
= lracl
->lr_aclcnt
;
416 vsec
.vsa_aclentsz
= lracl
->lr_acl_bytes
;
417 vsec
.vsa_aclflags
= lracl
->lr_acl_flags
;
418 if (zfsvfs
->z_fuid_replay
== NULL
) {
419 fuidstart
= &lracl
->lr_data
[xvatlen
+
420 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
)];
421 zfsvfs
->z_fuid_replay
=
422 zfs_replay_fuids(fuidstart
,
423 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
424 lr
->lr_uid
, lr
->lr_gid
);
426 #if defined(__linux__)
427 error
= zfs_mkdir(dzp
, name
, &xva
.xva_vattr
,
428 &zp
, kcred
, vflg
, &vsec
, zfs_init_idmap
);
430 error
= zfs_mkdir(dzp
, name
, &xva
.xva_vattr
,
431 &zp
, kcred
, vflg
, &vsec
, NULL
);
435 error
= SET_ERROR(ENOTSUP
);
439 if (error
== 0 && zp
!= NULL
) {
441 VOP_UNLOCK(ZTOV(zp
));
447 if (zfsvfs
->z_fuid_replay
)
448 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
449 zfsvfs
->z_fuid_replay
= NULL
;
455 zfs_replay_create(void *arg1
, void *arg2
, boolean_t byteswap
)
457 zfsvfs_t
*zfsvfs
= arg1
;
458 lr_create_t
*lrc
= arg2
;
459 _lr_create_t
*lr
= &lrc
->lr_create
;
460 char *name
= NULL
; /* location determined later */
461 char *link
; /* symlink content follows name */
474 ASSERT3U(lr
->lr_common
.lrc_reclen
, >, sizeof (*lr
));
476 txtype
= (lr
->lr_common
.lrc_txtype
& ~TX_CI
);
478 byteswap_uint64_array(lrc
, sizeof (*lrc
));
479 if (txtype
== TX_CREATE_ATTR
|| txtype
== TX_MKDIR_ATTR
)
480 zfs_replay_swap_attrs((lr_attr_t
*)&lrc
->lr_data
[0]);
484 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
487 objid
= LR_FOID_GET_OBJ(lr
->lr_foid
);
488 dnodesize
= LR_FOID_GET_SLOTS(lr
->lr_foid
) << DNODE_SHIFT
;
491 zfs_init_vattr(&xva
.xva_vattr
, ATTR_MODE
| ATTR_UID
| ATTR_GID
,
492 lr
->lr_mode
, lr
->lr_uid
, lr
->lr_gid
, lr
->lr_rdev
, objid
);
495 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
496 * eventually end up in zfs_mknode(), which assigns the object's
497 * creation time, generation number, and dnode slot count. The
498 * generic zfs_create() has no concept of these attributes, so
499 * we smuggle the values inside the vattr's otherwise unused
500 * va_ctime, va_nblocks, and va_fsid fields.
502 ZFS_TIME_DECODE(&xva
.xva_vattr
.va_ctime
, lr
->lr_crtime
);
503 xva
.xva_vattr
.va_nblocks
= lr
->lr_gen
;
504 xva
.xva_vattr
.va_fsid
= dnodesize
;
506 error
= dnode_try_claim(zfsvfs
->z_os
, objid
, dnodesize
>> DNODE_SHIFT
);
510 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
514 * Symlinks don't have fuid info, and CIFS never creates
517 * The _ATTR versions will grab the fuid info in their subcases.
519 if (txtype
!= TX_SYMLINK
&&
520 txtype
!= TX_MKDIR_ATTR
&&
521 txtype
!= TX_CREATE_ATTR
) {
522 start
= (void *)&lrc
->lr_data
[0];
523 zfsvfs
->z_fuid_replay
=
524 zfs_replay_fuid_domain(start
, &start
,
525 lr
->lr_uid
, lr
->lr_gid
);
530 lrattr
= (lr_attr_t
*)&lrc
->lr_data
[0];
531 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
532 zfs_replay_xvattr(lrattr
, &xva
);
533 start
= (void *)&lrc
->lr_data
[xvatlen
];
534 zfsvfs
->z_fuid_replay
=
535 zfs_replay_fuid_domain(start
, &start
,
536 lr
->lr_uid
, lr
->lr_gid
);
537 name
= (char *)start
;
542 name
= (char *)start
;
544 #if defined(__linux__)
545 error
= zfs_create(dzp
, name
, &xva
.xva_vattr
,
546 0, 0, &zp
, kcred
, vflg
, NULL
, zfs_init_idmap
);
548 error
= zfs_create(dzp
, name
, &xva
.xva_vattr
,
549 0, 0, &zp
, kcred
, vflg
, NULL
, NULL
);
553 lrattr
= (lr_attr_t
*)&lrc
->lr_data
[0];
554 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
555 zfs_replay_xvattr(lrattr
, &xva
);
556 start
= &lrc
->lr_data
[xvatlen
];
557 zfsvfs
->z_fuid_replay
=
558 zfs_replay_fuid_domain(start
, &start
,
559 lr
->lr_uid
, lr
->lr_gid
);
560 name
= (char *)start
;
565 name
= (char *)&lrc
->lr_data
[0];
567 #if defined(__linux__)
568 error
= zfs_mkdir(dzp
, name
, &xva
.xva_vattr
,
569 &zp
, kcred
, vflg
, NULL
, zfs_init_idmap
);
571 error
= zfs_mkdir(dzp
, name
, &xva
.xva_vattr
,
572 &zp
, kcred
, vflg
, NULL
, NULL
);
577 error
= zfs_make_xattrdir(dzp
, &xva
.xva_vattr
, &zp
, kcred
);
580 name
= &lrc
->lr_data
[0];
581 link
= &lrc
->lr_data
[strlen(name
) + 1];
582 #if defined(__linux__)
583 error
= zfs_symlink(dzp
, name
, &xva
.xva_vattr
,
584 link
, &zp
, kcred
, vflg
, zfs_init_idmap
);
586 error
= zfs_symlink(dzp
, name
, &xva
.xva_vattr
,
587 link
, &zp
, kcred
, vflg
, NULL
);
591 error
= SET_ERROR(ENOTSUP
);
595 if (error
== 0 && zp
!= NULL
) {
597 VOP_UNLOCK(ZTOV(zp
));
603 if (zfsvfs
->z_fuid_replay
)
604 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
605 zfsvfs
->z_fuid_replay
= NULL
;
610 zfs_replay_remove(void *arg1
, void *arg2
, boolean_t byteswap
)
612 zfsvfs_t
*zfsvfs
= arg1
;
613 lr_remove_t
*lr
= arg2
;
614 char *name
= (char *)&lr
->lr_data
[0]; /* name follows lr_remove_t */
619 ASSERT3U(lr
->lr_common
.lrc_reclen
, >, sizeof (*lr
));
622 byteswap_uint64_array(lr
, sizeof (*lr
));
624 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
627 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
630 switch ((int)lr
->lr_common
.lrc_txtype
) {
632 error
= zfs_remove(dzp
, name
, kcred
, vflg
);
635 error
= zfs_rmdir(dzp
, name
, NULL
, kcred
, vflg
);
638 error
= SET_ERROR(ENOTSUP
);
647 zfs_replay_link(void *arg1
, void *arg2
, boolean_t byteswap
)
649 zfsvfs_t
*zfsvfs
= arg1
;
650 lr_link_t
*lr
= arg2
;
651 char *name
= &lr
->lr_data
[0]; /* name follows lr_link_t */
656 ASSERT3U(lr
->lr_common
.lrc_reclen
, >, sizeof (*lr
));
659 byteswap_uint64_array(lr
, sizeof (*lr
));
661 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
664 if ((error
= zfs_zget(zfsvfs
, lr
->lr_link_obj
, &zp
)) != 0) {
669 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
672 error
= zfs_link(dzp
, zp
, name
, kcred
, vflg
);
680 do_zfs_replay_rename(zfsvfs_t
*zfsvfs
, _lr_rename_t
*lr
, char *sname
,
681 char *tname
, uint64_t rflags
, vattr_t
*wo_vap
)
683 znode_t
*sdzp
, *tdzp
;
686 /* Only Linux currently supports RENAME_* flags. */
688 VERIFY0(rflags
& ~(RENAME_EXCHANGE
| RENAME_WHITEOUT
));
690 /* wo_vap must be non-NULL iff. we're doing RENAME_WHITEOUT */
691 VERIFY_EQUIV(rflags
& RENAME_WHITEOUT
, wo_vap
!= NULL
);
696 if ((error
= zfs_zget(zfsvfs
, lr
->lr_sdoid
, &sdzp
)) != 0)
699 if ((error
= zfs_zget(zfsvfs
, lr
->lr_tdoid
, &tdzp
)) != 0) {
704 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
707 #if defined(__linux__)
708 error
= zfs_rename(sdzp
, sname
, tdzp
, tname
, kcred
, vflg
, rflags
,
709 wo_vap
, zfs_init_idmap
);
711 error
= zfs_rename(sdzp
, sname
, tdzp
, tname
, kcred
, vflg
, rflags
,
721 zfs_replay_rename(void *arg1
, void *arg2
, boolean_t byteswap
)
723 zfsvfs_t
*zfsvfs
= arg1
;
724 lr_rename_t
*lrr
= arg2
;
725 _lr_rename_t
*lr
= &lrr
->lr_rename
;
727 ASSERT3U(lr
->lr_common
.lrc_reclen
, >, sizeof (*lr
));
730 byteswap_uint64_array(lrr
, sizeof (*lrr
));
732 /* sname and tname follow lr_rename_t */
733 char *sname
= (char *)&lrr
->lr_data
[0];
734 char *tname
= (char *)&lrr
->lr_data
[strlen(sname
)+1];
735 return (do_zfs_replay_rename(zfsvfs
, lr
, sname
, tname
, 0, NULL
));
739 zfs_replay_rename_exchange(void *arg1
, void *arg2
, boolean_t byteswap
)
742 zfsvfs_t
*zfsvfs
= arg1
;
743 lr_rename_t
*lrr
= arg2
;
744 _lr_rename_t
*lr
= &lrr
->lr_rename
;
746 ASSERT3U(lr
->lr_common
.lrc_reclen
, >, sizeof (*lr
));
749 byteswap_uint64_array(lrr
, sizeof (*lrr
));
751 /* sname and tname follow lr_rename_t */
752 char *sname
= (char *)&lrr
->lr_data
[0];
753 char *tname
= (char *)&lrr
->lr_data
[strlen(sname
)+1];
754 return (do_zfs_replay_rename(zfsvfs
, lr
, sname
, tname
, RENAME_EXCHANGE
,
757 return (SET_ERROR(ENOTSUP
));
762 zfs_replay_rename_whiteout(void *arg1
, void *arg2
, boolean_t byteswap
)
765 zfsvfs_t
*zfsvfs
= arg1
;
766 lr_rename_whiteout_t
*lrrw
= arg2
;
767 _lr_rename_t
*lr
= &lrrw
->lr_rename
;
769 /* For the whiteout file. */
774 ASSERT3U(lr
->lr_common
.lrc_reclen
, >, sizeof (*lr
));
777 byteswap_uint64_array(lrrw
, sizeof (*lrrw
));
779 objid
= LR_FOID_GET_OBJ(lrrw
->lr_wfoid
);
780 dnodesize
= LR_FOID_GET_SLOTS(lrrw
->lr_wfoid
) << DNODE_SHIFT
;
783 zfs_init_vattr(&xva
.xva_vattr
, ATTR_MODE
| ATTR_UID
| ATTR_GID
,
784 lrrw
->lr_wmode
, lrrw
->lr_wuid
, lrrw
->lr_wgid
, lrrw
->lr_wrdev
,
788 * As with TX_CREATE, RENAME_WHITEOUT ends up in zfs_mknode(), which
789 * assigns the object's creation time, generation number, and dnode
790 * slot count. The generic zfs_rename() has no concept of these
791 * attributes, so we smuggle the values inside the vattr's otherwise
792 * unused va_ctime, va_nblocks, and va_fsid fields.
794 ZFS_TIME_DECODE(&xva
.xva_vattr
.va_ctime
, lrrw
->lr_wcrtime
);
795 xva
.xva_vattr
.va_nblocks
= lrrw
->lr_wgen
;
796 xva
.xva_vattr
.va_fsid
= dnodesize
;
798 error
= dnode_try_claim(zfsvfs
->z_os
, objid
, dnodesize
>> DNODE_SHIFT
);
802 /* sname and tname follow lr_rename_whiteout_t */
803 char *sname
= (char *)&lrrw
->lr_data
[0];
804 char *tname
= (char *)&lrrw
->lr_data
[strlen(sname
)+1];
805 return (do_zfs_replay_rename(zfsvfs
, lr
, sname
, tname
,
806 RENAME_WHITEOUT
, &xva
.xva_vattr
));
808 return (SET_ERROR(ENOTSUP
));
813 zfs_replay_write(void *arg1
, void *arg2
, boolean_t byteswap
)
815 zfsvfs_t
*zfsvfs
= arg1
;
816 lr_write_t
*lr
= arg2
;
817 char *data
= &lr
->lr_data
[0]; /* data follows lr_write_t */
820 uint64_t eod
, offset
, length
;
822 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
825 byteswap_uint64_array(lr
, sizeof (*lr
));
827 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0) {
829 * As we can log writes out of order, it's possible the
830 * file has been removed. In this case just drop the write
831 * and return success.
838 offset
= lr
->lr_offset
;
839 length
= lr
->lr_length
;
840 eod
= offset
+ length
; /* end of data for this write */
843 * This may be a write from a dmu_sync() for a whole block,
844 * and may extend beyond the current end of the file.
845 * We can't just replay what was written for this TX_WRITE as
846 * a future TX_WRITE2 may extend the eof and the data for that
847 * write needs to be there. So we write the whole block and
848 * reduce the eof. This needs to be done within the single dmu
849 * transaction created within vn_rdwr -> zfs_write. So a possible
850 * new end of file is passed through in zfsvfs->z_replay_eof
853 zfsvfs
->z_replay_eof
= 0; /* 0 means don't change end of file */
855 /* If it's a dmu_sync() block, write the whole block */
856 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
857 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
858 if (length
< blocksize
) {
859 offset
-= offset
% blocksize
;
862 if (zp
->z_size
< eod
)
863 zfsvfs
->z_replay_eof
= eod
;
865 error
= zfs_write_simple(zp
, data
, length
, offset
, NULL
);
867 zfsvfs
->z_replay_eof
= 0; /* safety */
873 * TX_WRITE2 are only generated when dmu_sync() returns EALREADY
874 * meaning the pool block is already being synced. So now that we always write
875 * out full blocks, all we have to do is expand the eof if
879 zfs_replay_write2(void *arg1
, void *arg2
, boolean_t byteswap
)
881 zfsvfs_t
*zfsvfs
= arg1
;
882 lr_write_t
*lr
= arg2
;
887 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
890 byteswap_uint64_array(lr
, sizeof (*lr
));
892 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
896 end
= lr
->lr_offset
+ lr
->lr_length
;
897 if (end
> zp
->z_size
) {
898 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
901 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
902 error
= dmu_tx_assign(tx
, TXG_WAIT
);
905 if (error
== ERESTART
) {
913 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
914 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
916 /* Ensure the replayed seq is updated */
917 (void) zil_replaying(zfsvfs
->z_log
, tx
);
928 zfs_replay_truncate(void *arg1
, void *arg2
, boolean_t byteswap
)
930 zfsvfs_t
*zfsvfs
= arg1
;
931 lr_truncate_t
*lr
= arg2
;
936 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
939 byteswap_uint64_array(lr
, sizeof (*lr
));
941 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
945 fl
.l_whence
= SEEK_SET
;
946 fl
.l_start
= lr
->lr_offset
;
947 fl
.l_len
= lr
->lr_length
;
949 error
= zfs_space(zp
, F_FREESP
, &fl
, O_RDWR
| O_LARGEFILE
,
950 lr
->lr_offset
, kcred
);
958 zfs_replay_setattr(void *arg1
, void *arg2
, boolean_t byteswap
)
960 zfsvfs_t
*zfsvfs
= arg1
;
961 lr_setattr_t
*lr
= arg2
;
964 vattr_t
*vap
= &xva
.xva_vattr
;
968 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
972 byteswap_uint64_array(lr
, sizeof (*lr
));
974 if ((lr
->lr_mask
& ATTR_XVATTR
) &&
975 zfsvfs
->z_version
>= ZPL_VERSION_INITIAL
)
976 zfs_replay_swap_attrs((lr_attr_t
*)&lr
->lr_data
[0]);
979 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
982 zfs_init_vattr(vap
, lr
->lr_mask
, lr
->lr_mode
,
983 lr
->lr_uid
, lr
->lr_gid
, 0, lr
->lr_foid
);
985 vap
->va_size
= lr
->lr_size
;
986 ZFS_TIME_DECODE(&vap
->va_atime
, lr
->lr_atime
);
987 ZFS_TIME_DECODE(&vap
->va_mtime
, lr
->lr_mtime
);
988 gethrestime(&vap
->va_ctime
);
989 vap
->va_mask
|= ATTR_CTIME
;
992 * Fill in xvattr_t portions if necessary.
995 start
= (void *)&lr
->lr_data
[0];
996 if (vap
->va_mask
& ATTR_XVATTR
) {
997 zfs_replay_xvattr((lr_attr_t
*)start
, &xva
);
998 start
= &lr
->lr_data
[
999 ZIL_XVAT_SIZE(((lr_attr_t
*)start
)->lr_attr_masksize
)];
1001 xva
.xva_vattr
.va_mask
&= ~ATTR_XVATTR
;
1003 zfsvfs
->z_fuid_replay
= zfs_replay_fuid_domain(start
, &start
,
1004 lr
->lr_uid
, lr
->lr_gid
);
1006 #if defined(__linux__)
1007 error
= zfs_setattr(zp
, vap
, 0, kcred
, zfs_init_idmap
);
1009 error
= zfs_setattr(zp
, vap
, 0, kcred
, NULL
);
1012 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
1013 zfsvfs
->z_fuid_replay
= NULL
;
1020 zfs_replay_setsaxattr(void *arg1
, void *arg2
, boolean_t byteswap
)
1022 zfsvfs_t
*zfsvfs
= arg1
;
1023 lr_setsaxattr_t
*lr
= arg2
;
1032 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
1033 ASSERT3U(lr
->lr_common
.lrc_reclen
, >, sizeof (*lr
) + lr
->lr_size
);
1035 ASSERT(spa_feature_is_active(zfsvfs
->z_os
->os_spa
,
1036 SPA_FEATURE_ZILSAXATTR
));
1038 byteswap_uint64_array(lr
, sizeof (*lr
));
1040 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
1043 rw_enter(&zp
->z_xattr_lock
, RW_WRITER
);
1044 mutex_enter(&zp
->z_lock
);
1045 if (zp
->z_xattr_cached
== NULL
)
1046 error
= zfs_sa_get_xattr(zp
);
1047 mutex_exit(&zp
->z_lock
);
1052 ASSERT(zp
->z_xattr_cached
);
1053 nvl
= zp
->z_xattr_cached
;
1055 /* Get xattr name, value and size from log record */
1057 name
= (char *)&lr
->lr_data
[0];
1060 error
= nvlist_remove(nvl
, name
, DATA_TYPE_BYTE_ARRAY
);
1062 value
= &lr
->lr_data
[strlen(name
) + 1];
1063 /* Limited to 32k to keep nvpair memory allocations small */
1064 if (size
> DXATTR_MAX_ENTRY_SIZE
) {
1065 error
= SET_ERROR(EFBIG
);
1069 /* Prevent the DXATTR SA from consuming the entire SA region */
1070 error
= nvlist_size(nvl
, &sa_size
, NV_ENCODE_XDR
);
1074 if (sa_size
> DXATTR_MAX_SA_SIZE
) {
1075 error
= SET_ERROR(EFBIG
);
1079 error
= nvlist_add_byte_array(nvl
, name
, (uchar_t
*)value
,
1084 * Update the SA for additions, modifications, and removals. On
1085 * error drop the inconsistent cached version of the nvlist, it
1086 * will be reconstructed from the ARC when next accessed.
1089 error
= zfs_sa_set_xattr(zp
, name
, value
, size
);
1093 zp
->z_xattr_cached
= NULL
;
1097 rw_exit(&zp
->z_xattr_lock
);
1103 zfs_replay_acl_v0(void *arg1
, void *arg2
, boolean_t byteswap
)
1105 zfsvfs_t
*zfsvfs
= arg1
;
1106 lr_acl_v0_t
*lr
= arg2
;
1107 ace_t
*ace
= (ace_t
*)&lr
->lr_data
[0];
1108 vsecattr_t vsa
= {0};
1112 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
1113 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
) +
1114 sizeof (ace_t
) * lr
->lr_aclcnt
);
1117 byteswap_uint64_array(lr
, sizeof (*lr
));
1118 zfs_oldace_byteswap(ace
, lr
->lr_aclcnt
);
1121 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
1124 vsa
.vsa_mask
= VSA_ACE
| VSA_ACECNT
;
1125 vsa
.vsa_aclcnt
= lr
->lr_aclcnt
;
1126 vsa
.vsa_aclentsz
= sizeof (ace_t
) * vsa
.vsa_aclcnt
;
1127 vsa
.vsa_aclflags
= 0;
1128 vsa
.vsa_aclentp
= ace
;
1130 error
= zfs_setsecattr(zp
, &vsa
, 0, kcred
);
1138 * Replaying ACLs is complicated by FUID support.
1139 * The log record may contain some optional data
1140 * to be used for replaying FUID's. These pieces
1141 * are the actual FUIDs that were created initially.
1142 * The FUID table index may no longer be valid and
1143 * during zfs_create() a new index may be assigned.
1144 * Because of this the log will contain the original
1145 * domain+rid in order to create a new FUID.
1147 * The individual ACEs may contain an ephemeral uid/gid which is no
1148 * longer valid and will need to be replaced with an actual FUID.
1152 zfs_replay_acl(void *arg1
, void *arg2
, boolean_t byteswap
)
1154 zfsvfs_t
*zfsvfs
= arg1
;
1155 lr_acl_t
*lr
= arg2
;
1156 ace_t
*ace
= (ace_t
*)&lr
->lr_data
[0];
1157 vsecattr_t vsa
= {0};
1161 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
1162 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
) + lr
->lr_acl_bytes
);
1165 byteswap_uint64_array(lr
, sizeof (*lr
));
1166 zfs_ace_byteswap(ace
, lr
->lr_acl_bytes
, B_FALSE
);
1167 if (lr
->lr_fuidcnt
) {
1168 byteswap_uint64_array(&lr
->lr_data
[
1169 ZIL_ACE_LENGTH(lr
->lr_acl_bytes
)],
1170 lr
->lr_fuidcnt
* sizeof (uint64_t));
1174 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
1177 vsa
.vsa_mask
= VSA_ACE
| VSA_ACECNT
| VSA_ACE_ACLFLAGS
;
1178 vsa
.vsa_aclcnt
= lr
->lr_aclcnt
;
1179 vsa
.vsa_aclentp
= ace
;
1180 vsa
.vsa_aclentsz
= lr
->lr_acl_bytes
;
1181 vsa
.vsa_aclflags
= lr
->lr_acl_flags
;
1183 if (lr
->lr_fuidcnt
) {
1184 void *fuidstart
= &lr
->lr_data
[
1185 ZIL_ACE_LENGTH(lr
->lr_acl_bytes
)];
1187 zfsvfs
->z_fuid_replay
=
1188 zfs_replay_fuids(fuidstart
, &fuidstart
,
1189 lr
->lr_fuidcnt
, lr
->lr_domcnt
, 0, 0);
1192 error
= zfs_setsecattr(zp
, &vsa
, 0, kcred
);
1194 if (zfsvfs
->z_fuid_replay
)
1195 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
1197 zfsvfs
->z_fuid_replay
= NULL
;
1204 zfs_replay_clone_range(void *arg1
, void *arg2
, boolean_t byteswap
)
1206 zfsvfs_t
*zfsvfs
= arg1
;
1207 lr_clone_range_t
*lr
= arg2
;
1211 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, sizeof (*lr
));
1212 ASSERT3U(lr
->lr_common
.lrc_reclen
, >=, offsetof(lr_clone_range_t
,
1213 lr_bps
[lr
->lr_nbps
]));
1216 byteswap_uint64_array(lr
, sizeof (*lr
));
1218 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0) {
1220 * Clones can be logged out of order, so don't be surprised if
1221 * the file is gone - just return success.
1223 if (error
== ENOENT
)
1228 error
= zfs_clone_range_replay(zp
, lr
->lr_offset
, lr
->lr_length
,
1229 lr
->lr_blksz
, lr
->lr_bps
, lr
->lr_nbps
);
1236 * Callback vectors for replaying records
1238 zil_replay_func_t
*const zfs_replay_vector
[TX_MAX_TYPE
] = {
1239 zfs_replay_error
, /* no such type */
1240 zfs_replay_create
, /* TX_CREATE */
1241 zfs_replay_create
, /* TX_MKDIR */
1242 zfs_replay_create
, /* TX_MKXATTR */
1243 zfs_replay_create
, /* TX_SYMLINK */
1244 zfs_replay_remove
, /* TX_REMOVE */
1245 zfs_replay_remove
, /* TX_RMDIR */
1246 zfs_replay_link
, /* TX_LINK */
1247 zfs_replay_rename
, /* TX_RENAME */
1248 zfs_replay_write
, /* TX_WRITE */
1249 zfs_replay_truncate
, /* TX_TRUNCATE */
1250 zfs_replay_setattr
, /* TX_SETATTR */
1251 zfs_replay_acl_v0
, /* TX_ACL_V0 */
1252 zfs_replay_acl
, /* TX_ACL */
1253 zfs_replay_create_acl
, /* TX_CREATE_ACL */
1254 zfs_replay_create
, /* TX_CREATE_ATTR */
1255 zfs_replay_create_acl
, /* TX_CREATE_ACL_ATTR */
1256 zfs_replay_create_acl
, /* TX_MKDIR_ACL */
1257 zfs_replay_create
, /* TX_MKDIR_ATTR */
1258 zfs_replay_create_acl
, /* TX_MKDIR_ACL_ATTR */
1259 zfs_replay_write2
, /* TX_WRITE2 */
1260 zfs_replay_setsaxattr
, /* TX_SETSAXATTR */
1261 zfs_replay_rename_exchange
, /* TX_RENAME_EXCHANGE */
1262 zfs_replay_rename_whiteout
, /* TX_RENAME_WHITEOUT */
1263 zfs_replay_clone_range
, /* TX_CLONE_RANGE */