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 http://www.opensolaris.org/os/licensing.
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) 2013, 2015 by Delphix. All rights reserved.
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/sysmacros.h>
30 #include <sys/cmn_err.h>
32 #include <sys/thread.h>
34 #include <sys/fcntl.h>
36 #include <sys/fs/zfs.h>
37 #include <sys/zfs_znode.h>
38 #include <sys/zfs_dir.h>
39 #include <sys/zfs_acl.h>
40 #include <sys/zfs_fuid.h>
43 #include <sys/byteorder.h>
47 #include <sys/atomic.h>
51 * Functions to replay ZFS intent log (ZIL) records
52 * The functions are called through a function vector (zfs_replay_vector)
53 * which is indexed by the transaction type.
57 zfs_init_vattr(vattr_t
*vap
, uint64_t mask
, uint64_t mode
,
58 uint64_t uid
, uint64_t gid
, uint64_t rdev
, uint64_t nodeid
)
60 bzero(vap
, sizeof (*vap
));
61 vap
->va_mask
= (uint_t
)mask
;
62 vap
->va_type
= IFTOVT(mode
);
63 vap
->va_mode
= mode
& MODEMASK
;
64 vap
->va_uid
= (uid_t
)(IS_EPHEMERAL(uid
)) ? -1 : uid
;
65 vap
->va_gid
= (gid_t
)(IS_EPHEMERAL(gid
)) ? -1 : gid
;
66 vap
->va_rdev
= zfs_cmpldev(rdev
);
67 vap
->va_nodeid
= nodeid
;
72 zfs_replay_error(void *arg1
, void *arg2
, boolean_t byteswap
)
74 return (SET_ERROR(ENOTSUP
));
78 zfs_replay_xvattr(lr_attr_t
*lrattr
, xvattr_t
*xvap
)
80 xoptattr_t
*xoap
= NULL
;
87 xvap
->xva_vattr
.va_mask
|= AT_XVATTR
;
88 if ((xoap
= xva_getxoptattr(xvap
)) == NULL
) {
89 xvap
->xva_vattr
.va_mask
&= ~AT_XVATTR
; /* shouldn't happen */
93 ASSERT(lrattr
->lr_attr_masksize
== xvap
->xva_mapsize
);
95 bitmap
= &lrattr
->lr_attr_bitmap
;
96 for (i
= 0; i
!= lrattr
->lr_attr_masksize
; i
++, bitmap
++)
97 xvap
->xva_reqattrmap
[i
] = *bitmap
;
99 attrs
= (uint64_t *)(lrattr
+ lrattr
->lr_attr_masksize
- 1);
101 scanstamp
= (caddr_t
)(crtime
+ 2);
103 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
))
104 xoap
->xoa_hidden
= ((*attrs
& XAT0_HIDDEN
) != 0);
105 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
))
106 xoap
->xoa_system
= ((*attrs
& XAT0_SYSTEM
) != 0);
107 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
))
108 xoap
->xoa_archive
= ((*attrs
& XAT0_ARCHIVE
) != 0);
109 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
))
110 xoap
->xoa_readonly
= ((*attrs
& XAT0_READONLY
) != 0);
111 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
))
112 xoap
->xoa_immutable
= ((*attrs
& XAT0_IMMUTABLE
) != 0);
113 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
))
114 xoap
->xoa_nounlink
= ((*attrs
& XAT0_NOUNLINK
) != 0);
115 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
))
116 xoap
->xoa_appendonly
= ((*attrs
& XAT0_APPENDONLY
) != 0);
117 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
))
118 xoap
->xoa_nodump
= ((*attrs
& XAT0_NODUMP
) != 0);
119 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))
120 xoap
->xoa_opaque
= ((*attrs
& XAT0_OPAQUE
) != 0);
121 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
))
122 xoap
->xoa_av_modified
= ((*attrs
& XAT0_AV_MODIFIED
) != 0);
123 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
))
124 xoap
->xoa_av_quarantined
=
125 ((*attrs
& XAT0_AV_QUARANTINED
) != 0);
126 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
))
127 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, crtime
);
128 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
129 bcopy(scanstamp
, xoap
->xoa_av_scanstamp
, AV_SCANSTAMP_SZ
);
130 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
))
131 xoap
->xoa_reparse
= ((*attrs
& XAT0_REPARSE
) != 0);
132 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
))
133 xoap
->xoa_offline
= ((*attrs
& XAT0_OFFLINE
) != 0);
134 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
))
135 xoap
->xoa_sparse
= ((*attrs
& XAT0_SPARSE
) != 0);
139 zfs_replay_domain_cnt(uint64_t uid
, uint64_t gid
)
145 uid_idx
= FUID_INDEX(uid
);
146 gid_idx
= FUID_INDEX(gid
);
149 if (gid_idx
> 0 && gid_idx
!= uid_idx
)
156 zfs_replay_fuid_domain_common(zfs_fuid_info_t
*fuid_infop
, void *start
,
161 for (i
= 0; i
!= domcnt
; i
++) {
162 fuid_infop
->z_domain_table
[i
] = start
;
163 start
= (caddr_t
)start
+ strlen(start
) + 1;
170 * Set the uid/gid in the fuid_info structure.
173 zfs_replay_fuid_ugid(zfs_fuid_info_t
*fuid_infop
, uint64_t uid
, uint64_t gid
)
176 * If owner or group are log specific FUIDs then slurp up
177 * domain information and build zfs_fuid_info_t
179 if (IS_EPHEMERAL(uid
))
180 fuid_infop
->z_fuid_owner
= uid
;
182 if (IS_EPHEMERAL(gid
))
183 fuid_infop
->z_fuid_group
= gid
;
187 * Load fuid domains into fuid_info_t
189 static zfs_fuid_info_t
*
190 zfs_replay_fuid_domain(void *buf
, void **end
, uint64_t uid
, uint64_t gid
)
194 zfs_fuid_info_t
*fuid_infop
;
196 fuid_infop
= zfs_fuid_info_alloc();
198 domcnt
= zfs_replay_domain_cnt(uid
, gid
);
203 fuid_infop
->z_domain_table
=
204 kmem_zalloc(domcnt
* sizeof (char **), KM_SLEEP
);
206 zfs_replay_fuid_ugid(fuid_infop
, uid
, gid
);
208 fuid_infop
->z_domain_cnt
= domcnt
;
209 *end
= zfs_replay_fuid_domain_common(fuid_infop
, buf
, domcnt
);
214 * load zfs_fuid_t's and fuid_domains into fuid_info_t
216 static zfs_fuid_info_t
*
217 zfs_replay_fuids(void *start
, void **end
, int idcnt
, int domcnt
, uint64_t uid
,
220 uint64_t *log_fuid
= (uint64_t *)start
;
221 zfs_fuid_info_t
*fuid_infop
;
224 fuid_infop
= zfs_fuid_info_alloc();
225 fuid_infop
->z_domain_cnt
= domcnt
;
227 fuid_infop
->z_domain_table
=
228 kmem_zalloc(domcnt
* sizeof (char **), KM_SLEEP
);
230 for (i
= 0; i
!= idcnt
; i
++) {
233 zfuid
= kmem_alloc(sizeof (zfs_fuid_t
), KM_SLEEP
);
234 zfuid
->z_logfuid
= *log_fuid
;
237 list_insert_tail(&fuid_infop
->z_fuids
, zfuid
);
241 zfs_replay_fuid_ugid(fuid_infop
, uid
, gid
);
243 *end
= zfs_replay_fuid_domain_common(fuid_infop
, log_fuid
, domcnt
);
248 zfs_replay_swap_attrs(lr_attr_t
*lrattr
)
250 /* swap the lr_attr structure */
251 byteswap_uint32_array(lrattr
, sizeof (*lrattr
));
252 /* swap the bitmap */
253 byteswap_uint32_array(lrattr
+ 1, (lrattr
->lr_attr_masksize
- 1) *
255 /* swap the attributes, create time + 64 bit word for attributes */
256 byteswap_uint64_array((caddr_t
)(lrattr
+ 1) + (sizeof (uint32_t) *
257 (lrattr
->lr_attr_masksize
- 1)), 3 * sizeof (uint64_t));
261 * Replay file create with optional ACL, xvattr information as well
262 * as option FUID information.
265 zfs_replay_create_acl(void *arg1
, void *arg2
, boolean_t byteswap
)
267 zfsvfs_t
*zfsvfs
= arg1
;
268 lr_acl_create_t
*lracl
= arg2
;
269 char *name
= NULL
; /* location determined later */
270 lr_create_t
*lr
= (lr_create_t
*)lracl
;
275 vsecattr_t vsec
= { 0 };
283 txtype
= (lr
->lr_common
.lrc_txtype
& ~TX_CI
);
285 byteswap_uint64_array(lracl
, sizeof (*lracl
));
286 if (txtype
== TX_CREATE_ACL_ATTR
||
287 txtype
== TX_MKDIR_ACL_ATTR
) {
288 lrattr
= (lr_attr_t
*)(caddr_t
)(lracl
+ 1);
289 zfs_replay_swap_attrs(lrattr
);
290 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
293 aclstart
= (caddr_t
)(lracl
+ 1) + xvatlen
;
294 zfs_ace_byteswap(aclstart
, lracl
->lr_acl_bytes
, B_FALSE
);
296 if (lracl
->lr_fuidcnt
) {
297 byteswap_uint64_array((caddr_t
)aclstart
+
298 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
),
299 lracl
->lr_fuidcnt
* sizeof (uint64_t));
303 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
307 zfs_init_vattr(&xva
.xva_vattr
, AT_TYPE
| AT_MODE
| AT_UID
| AT_GID
,
308 lr
->lr_mode
, lr
->lr_uid
, lr
->lr_gid
, lr
->lr_rdev
, lr
->lr_foid
);
311 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
312 * eventually end up in zfs_mknode(), which assigns the object's
313 * creation time and generation number. The generic fop_create()
314 * doesn't have either concept, so we smuggle the values inside
315 * the vattr's otherwise unused va_ctime and va_nblocks fields.
317 ZFS_TIME_DECODE(&xva
.xva_vattr
.va_ctime
, lr
->lr_crtime
);
318 xva
.xva_vattr
.va_nblocks
= lr
->lr_gen
;
320 error
= dmu_object_info(zfsvfs
->z_os
, lr
->lr_foid
, NULL
);
324 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
328 aclstart
= (caddr_t
)(lracl
+ 1);
329 fuidstart
= (caddr_t
)aclstart
+
330 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
331 zfsvfs
->z_fuid_replay
= zfs_replay_fuids(fuidstart
,
332 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
333 lr
->lr_uid
, lr
->lr_gid
);
335 case TX_CREATE_ACL_ATTR
:
337 lrattr
= (lr_attr_t
*)(caddr_t
)(lracl
+ 1);
338 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
339 xva
.xva_vattr
.va_mask
|= AT_XVATTR
;
340 zfs_replay_xvattr(lrattr
, &xva
);
342 vsec
.vsa_mask
= VSA_ACE
| VSA_ACE_ACLFLAGS
;
343 vsec
.vsa_aclentp
= (caddr_t
)(lracl
+ 1) + xvatlen
;
344 vsec
.vsa_aclcnt
= lracl
->lr_aclcnt
;
345 vsec
.vsa_aclentsz
= lracl
->lr_acl_bytes
;
346 vsec
.vsa_aclflags
= lracl
->lr_acl_flags
;
347 if (zfsvfs
->z_fuid_replay
== NULL
) {
348 fuidstart
= (caddr_t
)(lracl
+ 1) + xvatlen
+
349 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
350 zfsvfs
->z_fuid_replay
=
351 zfs_replay_fuids(fuidstart
,
352 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
353 lr
->lr_uid
, lr
->lr_gid
);
356 error
= fop_create(ZTOV(dzp
), name
, &xva
.xva_vattr
,
357 0, 0, &vp
, kcred
, vflg
, NULL
, &vsec
);
360 aclstart
= (caddr_t
)(lracl
+ 1);
361 fuidstart
= (caddr_t
)aclstart
+
362 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
363 zfsvfs
->z_fuid_replay
= zfs_replay_fuids(fuidstart
,
364 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
365 lr
->lr_uid
, lr
->lr_gid
);
367 case TX_MKDIR_ACL_ATTR
:
369 lrattr
= (lr_attr_t
*)(caddr_t
)(lracl
+ 1);
370 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
371 zfs_replay_xvattr(lrattr
, &xva
);
373 vsec
.vsa_mask
= VSA_ACE
| VSA_ACE_ACLFLAGS
;
374 vsec
.vsa_aclentp
= (caddr_t
)(lracl
+ 1) + xvatlen
;
375 vsec
.vsa_aclcnt
= lracl
->lr_aclcnt
;
376 vsec
.vsa_aclentsz
= lracl
->lr_acl_bytes
;
377 vsec
.vsa_aclflags
= lracl
->lr_acl_flags
;
378 if (zfsvfs
->z_fuid_replay
== NULL
) {
379 fuidstart
= (caddr_t
)(lracl
+ 1) + xvatlen
+
380 ZIL_ACE_LENGTH(lracl
->lr_acl_bytes
);
381 zfsvfs
->z_fuid_replay
=
382 zfs_replay_fuids(fuidstart
,
383 (void *)&name
, lracl
->lr_fuidcnt
, lracl
->lr_domcnt
,
384 lr
->lr_uid
, lr
->lr_gid
);
386 error
= fop_mkdir(ZTOV(dzp
), name
, &xva
.xva_vattr
,
387 &vp
, kcred
, NULL
, vflg
, &vsec
);
390 error
= SET_ERROR(ENOTSUP
);
394 if (error
== 0 && vp
!= NULL
)
399 if (zfsvfs
->z_fuid_replay
)
400 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
401 zfsvfs
->z_fuid_replay
= NULL
;
407 zfs_replay_create(void *arg1
, void *arg2
, boolean_t byteswap
)
409 zfsvfs_t
*zfsvfs
= arg1
;
410 lr_create_t
*lr
= arg2
;
411 char *name
= NULL
; /* location determined later */
412 char *link
; /* symlink content follows name */
417 size_t lrsize
= sizeof (lr_create_t
);
424 txtype
= (lr
->lr_common
.lrc_txtype
& ~TX_CI
);
426 byteswap_uint64_array(lr
, sizeof (*lr
));
427 if (txtype
== TX_CREATE_ATTR
|| txtype
== TX_MKDIR_ATTR
)
428 zfs_replay_swap_attrs((lr_attr_t
*)(lr
+ 1));
432 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
436 zfs_init_vattr(&xva
.xva_vattr
, AT_TYPE
| AT_MODE
| AT_UID
| AT_GID
,
437 lr
->lr_mode
, lr
->lr_uid
, lr
->lr_gid
, lr
->lr_rdev
, lr
->lr_foid
);
440 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
441 * eventually end up in zfs_mknode(), which assigns the object's
442 * creation time and generation number. The generic fop_create()
443 * doesn't have either concept, so we smuggle the values inside
444 * the vattr's otherwise unused va_ctime and va_nblocks fields.
446 ZFS_TIME_DECODE(&xva
.xva_vattr
.va_ctime
, lr
->lr_crtime
);
447 xva
.xva_vattr
.va_nblocks
= lr
->lr_gen
;
449 error
= dmu_object_info(zfsvfs
->z_os
, lr
->lr_foid
, NULL
);
453 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
457 * Symlinks don't have fuid info, and CIFS never creates
460 * The _ATTR versions will grab the fuid info in their subcases.
462 if ((int)lr
->lr_common
.lrc_txtype
!= TX_SYMLINK
&&
463 (int)lr
->lr_common
.lrc_txtype
!= TX_MKDIR_ATTR
&&
464 (int)lr
->lr_common
.lrc_txtype
!= TX_CREATE_ATTR
) {
466 zfsvfs
->z_fuid_replay
=
467 zfs_replay_fuid_domain(start
, &start
,
468 lr
->lr_uid
, lr
->lr_gid
);
473 lrattr
= (lr_attr_t
*)(caddr_t
)(lr
+ 1);
474 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
475 zfs_replay_xvattr((lr_attr_t
*)((caddr_t
)lr
+ lrsize
), &xva
);
476 start
= (caddr_t
)(lr
+ 1) + xvatlen
;
477 zfsvfs
->z_fuid_replay
=
478 zfs_replay_fuid_domain(start
, &start
,
479 lr
->lr_uid
, lr
->lr_gid
);
480 name
= (char *)start
;
485 name
= (char *)start
;
487 error
= fop_create(ZTOV(dzp
), name
, &xva
.xva_vattr
,
488 0, 0, &vp
, kcred
, vflg
, NULL
, NULL
);
491 lrattr
= (lr_attr_t
*)(caddr_t
)(lr
+ 1);
492 xvatlen
= ZIL_XVAT_SIZE(lrattr
->lr_attr_masksize
);
493 zfs_replay_xvattr((lr_attr_t
*)((caddr_t
)lr
+ lrsize
), &xva
);
494 start
= (caddr_t
)(lr
+ 1) + xvatlen
;
495 zfsvfs
->z_fuid_replay
=
496 zfs_replay_fuid_domain(start
, &start
,
497 lr
->lr_uid
, lr
->lr_gid
);
498 name
= (char *)start
;
503 name
= (char *)(lr
+ 1);
505 error
= fop_mkdir(ZTOV(dzp
), name
, &xva
.xva_vattr
,
506 &vp
, kcred
, NULL
, vflg
, NULL
);
509 error
= zfs_make_xattrdir(dzp
, &xva
.xva_vattr
, &vp
, kcred
);
512 name
= (char *)(lr
+ 1);
513 link
= name
+ strlen(name
) + 1;
514 error
= fop_symlink(ZTOV(dzp
), name
, &xva
.xva_vattr
,
515 link
, kcred
, NULL
, vflg
);
518 error
= SET_ERROR(ENOTSUP
);
522 if (error
== 0 && vp
!= NULL
)
527 if (zfsvfs
->z_fuid_replay
)
528 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
529 zfsvfs
->z_fuid_replay
= NULL
;
534 zfs_replay_remove(void *arg1
, void *arg2
, boolean_t byteswap
)
536 zfsvfs_t
*zfsvfs
= arg1
;
537 lr_remove_t
*lr
= arg2
;
538 char *name
= (char *)(lr
+ 1); /* name follows lr_remove_t */
544 byteswap_uint64_array(lr
, sizeof (*lr
));
546 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
549 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
552 switch ((int)lr
->lr_common
.lrc_txtype
) {
554 error
= fop_remove(ZTOV(dzp
), name
, kcred
, NULL
, vflg
);
557 error
= fop_rmdir(ZTOV(dzp
), name
, NULL
, kcred
, NULL
, vflg
);
560 error
= SET_ERROR(ENOTSUP
);
569 zfs_replay_link(void *arg1
, void *arg2
, boolean_t byteswap
)
571 zfsvfs_t
*zfsvfs
= arg1
;
572 lr_link_t
*lr
= arg2
;
573 char *name
= (char *)(lr
+ 1); /* name follows lr_link_t */
579 byteswap_uint64_array(lr
, sizeof (*lr
));
581 if ((error
= zfs_zget(zfsvfs
, lr
->lr_doid
, &dzp
)) != 0)
584 if ((error
= zfs_zget(zfsvfs
, lr
->lr_link_obj
, &zp
)) != 0) {
589 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
592 error
= fop_link(ZTOV(dzp
), ZTOV(zp
), name
, kcred
, NULL
, vflg
);
601 zfs_replay_rename(void *arg1
, void *arg2
, boolean_t byteswap
)
603 zfsvfs_t
*zfsvfs
= arg1
;
604 lr_rename_t
*lr
= arg2
;
605 char *sname
= (char *)(lr
+ 1); /* sname and tname follow lr_rename_t */
606 char *tname
= sname
+ strlen(sname
) + 1;
607 znode_t
*sdzp
, *tdzp
;
612 byteswap_uint64_array(lr
, sizeof (*lr
));
614 if ((error
= zfs_zget(zfsvfs
, lr
->lr_sdoid
, &sdzp
)) != 0)
617 if ((error
= zfs_zget(zfsvfs
, lr
->lr_tdoid
, &tdzp
)) != 0) {
622 if (lr
->lr_common
.lrc_txtype
& TX_CI
)
625 error
= fop_rename(ZTOV(sdzp
), sname
, ZTOV(tdzp
), tname
, kcred
,
635 zfs_replay_write(void *arg1
, void *arg2
, boolean_t byteswap
)
637 zfsvfs_t
*zfsvfs
= arg1
;
638 lr_write_t
*lr
= arg2
;
639 char *data
= (char *)(lr
+ 1); /* data follows lr_write_t */
643 uint64_t eod
, offset
, length
;
646 byteswap_uint64_array(lr
, sizeof (*lr
));
648 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0) {
650 * As we can log writes out of order, it's possible the
651 * file has been removed. In this case just drop the write
652 * and return success.
659 offset
= lr
->lr_offset
;
660 length
= lr
->lr_length
;
661 eod
= offset
+ length
; /* end of data for this write */
664 * This may be a write from a dmu_sync() for a whole block,
665 * and may extend beyond the current end of the file.
666 * We can't just replay what was written for this TX_WRITE as
667 * a future TX_WRITE2 may extend the eof and the data for that
668 * write needs to be there. So we write the whole block and
669 * reduce the eof. This needs to be done within the single dmu
670 * transaction created within vn_rdwr -> zfs_write. So a possible
671 * new end of file is passed through in zfsvfs->z_replay_eof
674 zfsvfs
->z_replay_eof
= 0; /* 0 means don't change end of file */
676 /* If it's a dmu_sync() block, write the whole block */
677 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
678 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
679 if (length
< blocksize
) {
680 offset
-= offset
% blocksize
;
683 if (zp
->z_size
< eod
)
684 zfsvfs
->z_replay_eof
= eod
;
687 error
= vn_rdwr(UIO_WRITE
, ZTOV(zp
), data
, length
, offset
,
688 UIO_SYSSPACE
, 0, RLIM64_INFINITY
, kcred
, &resid
);
691 zfsvfs
->z_replay_eof
= 0; /* safety */
697 * TX_WRITE2 are only generated when dmu_sync() returns EALREADY
698 * meaning the pool block is already being synced. So now that we always write
699 * out full blocks, all we have to do is expand the eof if
703 zfs_replay_write2(void *arg1
, void *arg2
, boolean_t byteswap
)
705 zfsvfs_t
*zfsvfs
= arg1
;
706 lr_write_t
*lr
= arg2
;
712 byteswap_uint64_array(lr
, sizeof (*lr
));
714 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
718 end
= lr
->lr_offset
+ lr
->lr_length
;
719 if (end
> zp
->z_size
) {
720 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
723 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
724 error
= dmu_tx_assign(tx
, TXG_WAIT
);
727 if (error
== ERESTART
) {
735 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
736 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
738 /* Ensure the replayed seq is updated */
739 (void) zil_replaying(zfsvfs
->z_log
, tx
);
750 zfs_replay_truncate(void *arg1
, void *arg2
, boolean_t byteswap
)
752 zfsvfs_t
*zfsvfs
= arg1
;
753 lr_truncate_t
*lr
= arg2
;
759 byteswap_uint64_array(lr
, sizeof (*lr
));
761 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
764 bzero(&fl
, sizeof (fl
));
767 fl
.l_start
= lr
->lr_offset
;
768 fl
.l_len
= lr
->lr_length
;
770 error
= fop_space(ZTOV(zp
), F_FREESP
, &fl
, FWRITE
| FOFFMAX
,
771 lr
->lr_offset
, kcred
, NULL
);
779 zfs_replay_setattr(void *arg1
, void *arg2
, boolean_t byteswap
)
781 zfsvfs_t
*zfsvfs
= arg1
;
782 lr_setattr_t
*lr
= arg2
;
785 vattr_t
*vap
= &xva
.xva_vattr
;
791 byteswap_uint64_array(lr
, sizeof (*lr
));
793 if ((lr
->lr_mask
& AT_XVATTR
) &&
794 zfsvfs
->z_version
>= ZPL_VERSION_INITIAL
)
795 zfs_replay_swap_attrs((lr_attr_t
*)(lr
+ 1));
798 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
801 zfs_init_vattr(vap
, lr
->lr_mask
, lr
->lr_mode
,
802 lr
->lr_uid
, lr
->lr_gid
, 0, lr
->lr_foid
);
804 vap
->va_size
= lr
->lr_size
;
805 ZFS_TIME_DECODE(&vap
->va_atime
, lr
->lr_atime
);
806 ZFS_TIME_DECODE(&vap
->va_mtime
, lr
->lr_mtime
);
809 * Fill in xvattr_t portions if necessary.
812 start
= (lr_setattr_t
*)(lr
+ 1);
813 if (vap
->va_mask
& AT_XVATTR
) {
814 zfs_replay_xvattr((lr_attr_t
*)start
, &xva
);
815 start
= (caddr_t
)start
+
816 ZIL_XVAT_SIZE(((lr_attr_t
*)start
)->lr_attr_masksize
);
818 xva
.xva_vattr
.va_mask
&= ~AT_XVATTR
;
820 zfsvfs
->z_fuid_replay
= zfs_replay_fuid_domain(start
, &start
,
821 lr
->lr_uid
, lr
->lr_gid
);
823 error
= fop_setattr(ZTOV(zp
), vap
, 0, kcred
, NULL
);
825 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
826 zfsvfs
->z_fuid_replay
= NULL
;
833 zfs_replay_acl_v0(void *arg1
, void *arg2
, boolean_t byteswap
)
835 zfsvfs_t
*zfsvfs
= arg1
;
836 lr_acl_v0_t
*lr
= arg2
;
837 ace_t
*ace
= (ace_t
*)(lr
+ 1); /* ace array follows lr_acl_t */
843 byteswap_uint64_array(lr
, sizeof (*lr
));
844 zfs_oldace_byteswap(ace
, lr
->lr_aclcnt
);
847 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
850 bzero(&vsa
, sizeof (vsa
));
851 vsa
.vsa_mask
= VSA_ACE
| VSA_ACECNT
;
852 vsa
.vsa_aclcnt
= lr
->lr_aclcnt
;
853 vsa
.vsa_aclentsz
= sizeof (ace_t
) * vsa
.vsa_aclcnt
;
854 vsa
.vsa_aclflags
= 0;
855 vsa
.vsa_aclentp
= ace
;
857 error
= fop_setsecattr(ZTOV(zp
), &vsa
, 0, kcred
, NULL
);
865 * Replaying ACLs is complicated by FUID support.
866 * The log record may contain some optional data
867 * to be used for replaying FUID's. These pieces
868 * are the actual FUIDs that were created initially.
869 * The FUID table index may no longer be valid and
870 * during zfs_create() a new index may be assigned.
871 * Because of this the log will contain the original
872 * doman+rid in order to create a new FUID.
874 * The individual ACEs may contain an ephemeral uid/gid which is no
875 * longer valid and will need to be replaced with an actual FUID.
879 zfs_replay_acl(void *arg1
, void *arg2
, boolean_t byteswap
)
881 zfsvfs_t
*zfsvfs
= arg1
;
883 ace_t
*ace
= (ace_t
*)(lr
+ 1);
889 byteswap_uint64_array(lr
, sizeof (*lr
));
890 zfs_ace_byteswap(ace
, lr
->lr_acl_bytes
, B_FALSE
);
891 if (lr
->lr_fuidcnt
) {
892 byteswap_uint64_array((caddr_t
)ace
+
893 ZIL_ACE_LENGTH(lr
->lr_acl_bytes
),
894 lr
->lr_fuidcnt
* sizeof (uint64_t));
898 if ((error
= zfs_zget(zfsvfs
, lr
->lr_foid
, &zp
)) != 0)
901 bzero(&vsa
, sizeof (vsa
));
902 vsa
.vsa_mask
= VSA_ACE
| VSA_ACECNT
| VSA_ACE_ACLFLAGS
;
903 vsa
.vsa_aclcnt
= lr
->lr_aclcnt
;
904 vsa
.vsa_aclentp
= ace
;
905 vsa
.vsa_aclentsz
= lr
->lr_acl_bytes
;
906 vsa
.vsa_aclflags
= lr
->lr_acl_flags
;
908 if (lr
->lr_fuidcnt
) {
909 void *fuidstart
= (caddr_t
)ace
+
910 ZIL_ACE_LENGTH(lr
->lr_acl_bytes
);
912 zfsvfs
->z_fuid_replay
=
913 zfs_replay_fuids(fuidstart
, &fuidstart
,
914 lr
->lr_fuidcnt
, lr
->lr_domcnt
, 0, 0);
917 error
= fop_setsecattr(ZTOV(zp
), &vsa
, 0, kcred
, NULL
);
919 if (zfsvfs
->z_fuid_replay
)
920 zfs_fuid_info_free(zfsvfs
->z_fuid_replay
);
922 zfsvfs
->z_fuid_replay
= NULL
;
929 * Callback vectors for replaying records
931 zil_replay_func_t
*zfs_replay_vector
[TX_MAX_TYPE
] = {
932 zfs_replay_error
, /* 0 no such transaction type */
933 zfs_replay_create
, /* TX_CREATE */
934 zfs_replay_create
, /* TX_MKDIR */
935 zfs_replay_create
, /* TX_MKXATTR */
936 zfs_replay_create
, /* TX_SYMLINK */
937 zfs_replay_remove
, /* TX_REMOVE */
938 zfs_replay_remove
, /* TX_RMDIR */
939 zfs_replay_link
, /* TX_LINK */
940 zfs_replay_rename
, /* TX_RENAME */
941 zfs_replay_write
, /* TX_WRITE */
942 zfs_replay_truncate
, /* TX_TRUNCATE */
943 zfs_replay_setattr
, /* TX_SETATTR */
944 zfs_replay_acl_v0
, /* TX_ACL_V0 */
945 zfs_replay_acl
, /* TX_ACL */
946 zfs_replay_create_acl
, /* TX_CREATE_ACL */
947 zfs_replay_create
, /* TX_CREATE_ATTR */
948 zfs_replay_create_acl
, /* TX_CREATE_ACL_ATTR */
949 zfs_replay_create_acl
, /* TX_MKDIR_ACL */
950 zfs_replay_create
, /* TX_MKDIR_ATTR */
951 zfs_replay_create_acl
, /* TX_MKDIR_ACL_ATTR */
952 zfs_replay_write2
, /* TX_WRITE2 */