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 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
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>
35 #include <sys/zfs_znode.h>
36 #include <sys/zfs_dir.h>
38 #include <sys/zil_impl.h>
39 #include <sys/byteorder.h>
40 #include <sys/policy.h>
46 #include <sys/zfs_fuid.h>
48 #include <sys/dsl_dataset.h>
50 #define ZFS_HANDLE_REPLAY(zilog, tx) \
51 if (zilog->zl_replay) { \
52 dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); \
53 zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] = \
54 zilog->zl_replaying_seq; \
59 * These zfs_log_* functions must be called within a dmu tx, in one
60 * of 2 contexts depending on zilog->z_replay:
64 * We need to record the transaction so that if it is committed to
65 * the Intent Log then it can be replayed. An intent log transaction
66 * structure (itx_t) is allocated and all the information necessary to
67 * possibly replay the transaction is saved in it. The itx is then assigned
68 * a sequence number and inserted in the in-memory list anchored in the zilog.
72 * We need to mark the intent log record as replayed in the log header.
73 * This is done in the same transaction as the replay so that they
78 zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
80 int isxvattr = (vap->va_mask & AT_XVATTR);
83 if (vsecp == NULL && !isxvattr)
85 if (vsecp && isxvattr)
86 return (TX_CREATE_ACL_ATTR);
88 return (TX_CREATE_ACL);
90 return (TX_CREATE_ATTR);
93 if (vsecp == NULL && !isxvattr)
95 if (vsecp && isxvattr)
96 return (TX_MKDIR_ACL_ATTR);
98 return (TX_MKDIR_ACL);
100 return (TX_MKDIR_ATTR);
105 return (TX_MAX_TYPE);
109 * build up the log data necessary for logging xvattr_t
110 * First lr_attr_t is initialized. following the lr_attr_t
111 * is the mapsize and attribute bitmap copied from the xvattr_t.
112 * Following the bitmap and bitmapsize two 64 bit words are reserved
113 * for the create time which may be set. Following the create time
114 * records a single 64 bit integer which has the bits to set on
115 * replay for the xvattr.
118 zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
127 xoap = xva_getxoptattr(xvap);
130 lrattr->lr_attr_masksize = xvap->xva_mapsize;
131 bitmap = &lrattr->lr_attr_bitmap;
132 for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) {
133 *bitmap = xvap->xva_reqattrmap[i];
136 /* Now pack the attributes up in a single uint64_t */
137 attrs = (uint64_t *)bitmap;
139 scanstamp = (caddr_t)(crtime + 2);
141 if (XVA_ISSET_REQ(xvap, XAT_READONLY))
142 *attrs |= (xoap->xoa_readonly == 0) ? 0 :
144 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
145 *attrs |= (xoap->xoa_hidden == 0) ? 0 :
147 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
148 *attrs |= (xoap->xoa_system == 0) ? 0 :
150 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
151 *attrs |= (xoap->xoa_archive == 0) ? 0 :
153 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
154 *attrs |= (xoap->xoa_immutable == 0) ? 0 :
156 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
157 *attrs |= (xoap->xoa_nounlink == 0) ? 0 :
159 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
160 *attrs |= (xoap->xoa_appendonly == 0) ? 0 :
162 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
163 *attrs |= (xoap->xoa_opaque == 0) ? 0 :
165 if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
166 *attrs |= (xoap->xoa_nodump == 0) ? 0 :
168 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
169 *attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
171 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
172 *attrs |= (xoap->xoa_av_modified == 0) ? 0 :
174 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
175 ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime);
176 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
177 bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ);
181 zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
184 uint64_t *fuidloc = start;
186 /* First copy in the ACE FUIDs */
187 for (zfuid = list_head(&fuidp->z_fuids); zfuid;
188 zfuid = list_next(&fuidp->z_fuids, zfuid)) {
189 *fuidloc++ = zfuid->z_logfuid;
196 zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
198 zfs_fuid_domain_t *zdomain;
200 /* now copy in the domain info, if any */
201 if (fuidp->z_domain_str_sz != 0) {
202 for (zdomain = list_head(&fuidp->z_domains); zdomain;
203 zdomain = list_next(&fuidp->z_domains, zdomain)) {
204 bcopy((void *)zdomain->z_domain, start,
205 strlen(zdomain->z_domain) + 1);
206 start = (caddr_t)start +
207 strlen(zdomain->z_domain) + 1;
214 * zfs_log_create() is used to handle TX_CREATE, TX_CREATE_ATTR, TX_MKDIR,
215 * TX_MKDIR_ATTR and TX_MKXATTR
218 * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
219 * domain information appended prior to the name. In this case the
220 * uid/gid in the log record will be a log centric FUID.
222 * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
223 * may contain attributes, ACL and optional fuid information.
225 * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
226 * and ACL and normal users/groups in the ACEs.
228 * There may be an optional xvattr attribute information similar
229 * to zfs_log_setattr.
231 * Also, after the file name "domain" strings may be appended.
234 zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
235 znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp,
236 zfs_fuid_info_t *fuidp, vattr_t *vap)
241 lr_acl_create_t *lracl;
245 xvattr_t *xvap = (xvattr_t *)vap;
248 size_t namesize = strlen(name) + 1;
254 ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
257 * If we have FUIDs present then add in space for
258 * domains and ACE fuid's if any.
261 fuidsz += fuidp->z_domain_str_sz;
262 fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
265 if (vap->va_mask & AT_XVATTR)
266 xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
268 if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
269 (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
270 (int)txtype == TX_MKXATTR) {
271 txsize = sizeof (*lr) + namesize + fuidsz + xvatsize;
272 lrsize = sizeof (*lr);
274 aclsize = (vsecp) ? vsecp->vsa_aclentsz : 0;
276 sizeof (lr_acl_create_t) + namesize + fuidsz +
277 ZIL_ACE_LENGTH(aclsize) + xvatsize;
278 lrsize = sizeof (lr_acl_create_t);
281 itx = zil_itx_create(txtype, txsize);
283 lr = (lr_create_t *)&itx->itx_lr;
284 lr->lr_doid = dzp->z_id;
285 lr->lr_foid = zp->z_id;
286 lr->lr_mode = zp->z_phys->zp_mode;
287 if (!IS_EPHEMERAL(zp->z_phys->zp_uid)) {
288 lr->lr_uid = (uint64_t)zp->z_phys->zp_uid;
290 lr->lr_uid = fuidp->z_fuid_owner;
292 if (!IS_EPHEMERAL(zp->z_phys->zp_gid)) {
293 lr->lr_gid = (uint64_t)zp->z_phys->zp_gid;
295 lr->lr_gid = fuidp->z_fuid_group;
297 lr->lr_gen = zp->z_phys->zp_gen;
298 lr->lr_crtime[0] = zp->z_phys->zp_crtime[0];
299 lr->lr_crtime[1] = zp->z_phys->zp_crtime[1];
300 lr->lr_rdev = zp->z_phys->zp_rdev;
303 * Fill in xvattr info if any
305 if (vap->va_mask & AT_XVATTR) {
306 zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap);
307 end = (caddr_t)lr + lrsize + xvatsize;
309 end = (caddr_t)lr + lrsize;
312 /* Now fill in any ACL info */
315 lracl = (lr_acl_create_t *)&itx->itx_lr;
316 lracl->lr_aclcnt = vsecp->vsa_aclcnt;
317 lracl->lr_acl_bytes = aclsize;
318 lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
319 lracl->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
320 if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
321 lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
323 lracl->lr_acl_flags = 0;
325 bcopy(vsecp->vsa_aclentp, end, aclsize);
326 end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize);
329 /* drop in FUID info */
331 end = zfs_log_fuid_ids(fuidp, end);
332 end = zfs_log_fuid_domains(fuidp, end);
335 * Now place file name in log record
337 bcopy(name, end, namesize);
339 seq = zil_itx_assign(zilog, itx, tx);
340 dzp->z_last_itx = seq;
341 zp->z_last_itx = seq;
345 * zfs_log_remove() handles both TX_REMOVE and TX_RMDIR transactions.
348 zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
349 znode_t *dzp, char *name)
354 size_t namesize = strlen(name) + 1;
359 ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
361 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
362 lr = (lr_remove_t *)&itx->itx_lr;
363 lr->lr_doid = dzp->z_id;
364 bcopy(name, (char *)(lr + 1), namesize);
366 seq = zil_itx_assign(zilog, itx, tx);
367 dzp->z_last_itx = seq;
371 * zfs_log_link() handles TX_LINK transactions.
374 zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
375 znode_t *dzp, znode_t *zp, char *name)
380 size_t namesize = strlen(name) + 1;
385 ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
387 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
388 lr = (lr_link_t *)&itx->itx_lr;
389 lr->lr_doid = dzp->z_id;
390 lr->lr_link_obj = zp->z_id;
391 bcopy(name, (char *)(lr + 1), namesize);
393 seq = zil_itx_assign(zilog, itx, tx);
394 dzp->z_last_itx = seq;
395 zp->z_last_itx = seq;
399 * zfs_log_symlink() handles TX_SYMLINK transactions.
402 zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
403 znode_t *dzp, znode_t *zp, char *name, char *link)
408 size_t namesize = strlen(name) + 1;
409 size_t linksize = strlen(link) + 1;
414 ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
416 itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
417 lr = (lr_create_t *)&itx->itx_lr;
418 lr->lr_doid = dzp->z_id;
419 lr->lr_foid = zp->z_id;
420 lr->lr_mode = zp->z_phys->zp_mode;
421 lr->lr_uid = zp->z_phys->zp_uid;
422 lr->lr_gid = zp->z_phys->zp_gid;
423 lr->lr_gen = zp->z_phys->zp_gen;
424 lr->lr_crtime[0] = zp->z_phys->zp_crtime[0];
425 lr->lr_crtime[1] = zp->z_phys->zp_crtime[1];
426 bcopy(name, (char *)(lr + 1), namesize);
427 bcopy(link, (char *)(lr + 1) + namesize, linksize);
429 seq = zil_itx_assign(zilog, itx, tx);
430 dzp->z_last_itx = seq;
431 zp->z_last_itx = seq;
435 * zfs_log_rename() handles TX_RENAME transactions.
438 zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
439 znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp)
444 size_t snamesize = strlen(sname) + 1;
445 size_t dnamesize = strlen(dname) + 1;
450 ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
452 itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
453 lr = (lr_rename_t *)&itx->itx_lr;
454 lr->lr_sdoid = sdzp->z_id;
455 lr->lr_tdoid = tdzp->z_id;
456 bcopy(sname, (char *)(lr + 1), snamesize);
457 bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize);
459 seq = zil_itx_assign(zilog, itx, tx);
460 sdzp->z_last_itx = seq;
461 tdzp->z_last_itx = seq;
462 szp->z_last_itx = seq;
466 * zfs_log_write() handles TX_WRITE transactions.
468 ssize_t zfs_immediate_write_sz = 32768;
471 zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
472 znode_t *zp, offset_t off, ssize_t resid, int ioflag)
474 itx_wr_state_t write_state;
478 if (zilog == NULL || zp->z_unlinked)
481 ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
483 slogging = spa_has_slogs(zilog->zl_spa);
484 if (resid > zfs_immediate_write_sz && !slogging && resid <= zp->z_blksz)
485 write_state = WR_INDIRECT;
486 else if (ioflag & (FSYNC | FDSYNC))
487 write_state = WR_COPIED;
489 write_state = WR_NEED_COPY;
491 if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
492 (void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
501 * If the write would overflow the largest block then split it.
503 if (write_state != WR_INDIRECT && resid > ZIL_MAX_LOG_DATA)
504 len = SPA_MAXBLOCKSIZE >> 1;
508 itx = zil_itx_create(txtype, sizeof (*lr) +
509 (write_state == WR_COPIED ? len : 0));
510 lr = (lr_write_t *)&itx->itx_lr;
511 if (write_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os,
512 zp->z_id, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
513 kmem_free(itx, offsetof(itx_t, itx_lr) +
514 itx->itx_lr.lrc_reclen);
515 itx = zil_itx_create(txtype, sizeof (*lr));
516 lr = (lr_write_t *)&itx->itx_lr;
517 write_state = WR_NEED_COPY;
520 itx->itx_wr_state = write_state;
521 if (write_state == WR_NEED_COPY)
523 lr->lr_foid = zp->z_id;
527 BP_ZERO(&lr->lr_blkptr);
529 itx->itx_private = zp->z_zfsvfs;
531 if ((zp->z_sync_cnt != 0) || (fsync_cnt != 0) ||
532 (ioflag & (FSYNC | FDSYNC)))
533 itx->itx_sync = B_TRUE;
535 itx->itx_sync = B_FALSE;
537 zp->z_last_itx = zil_itx_assign(zilog, itx, tx);
545 * zfs_log_truncate() handles TX_TRUNCATE transactions.
548 zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
549 znode_t *zp, uint64_t off, uint64_t len)
555 if (zilog == NULL || zp->z_unlinked)
558 ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
560 itx = zil_itx_create(txtype, sizeof (*lr));
561 lr = (lr_truncate_t *)&itx->itx_lr;
562 lr->lr_foid = zp->z_id;
566 itx->itx_sync = (zp->z_sync_cnt != 0);
567 seq = zil_itx_assign(zilog, itx, tx);
568 zp->z_last_itx = seq;
572 * zfs_log_setattr() handles TX_SETATTR transactions.
575 zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
576 znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
581 xvattr_t *xvap = (xvattr_t *)vap;
582 size_t recsize = sizeof (lr_setattr_t);
586 if (zilog == NULL || zp->z_unlinked)
589 ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
592 * If XVATTR set, then log record size needs to allow
593 * for lr_attr_t + xvattr mask, mapsize and create time
594 * plus actual attribute values
596 if (vap->va_mask & AT_XVATTR)
597 recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
600 recsize += fuidp->z_domain_str_sz;
602 itx = zil_itx_create(txtype, recsize);
603 lr = (lr_setattr_t *)&itx->itx_lr;
604 lr->lr_foid = zp->z_id;
605 lr->lr_mask = (uint64_t)mask_applied;
606 lr->lr_mode = (uint64_t)vap->va_mode;
607 if ((mask_applied & AT_UID) && IS_EPHEMERAL(vap->va_uid))
608 lr->lr_uid = fuidp->z_fuid_owner;
610 lr->lr_uid = (uint64_t)vap->va_uid;
612 if ((mask_applied & AT_GID) && IS_EPHEMERAL(vap->va_gid))
613 lr->lr_gid = fuidp->z_fuid_group;
615 lr->lr_gid = (uint64_t)vap->va_gid;
617 lr->lr_size = (uint64_t)vap->va_size;
618 ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
619 ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
620 start = (lr_setattr_t *)(lr + 1);
621 if (vap->va_mask & AT_XVATTR) {
622 zfs_log_xvattr((lr_attr_t *)start, xvap);
623 start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize);
627 * Now stick on domain information if any on end
631 (void) zfs_log_fuid_domains(fuidp, start);
633 itx->itx_sync = (zp->z_sync_cnt != 0);
634 seq = zil_itx_assign(zilog, itx, tx);
635 zp->z_last_itx = seq;
639 * zfs_log_acl() handles TX_ACL transactions.
642 zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
643 vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
652 size_t aclbytes = vsecp->vsa_aclentsz;
654 if (zilog == NULL || zp->z_unlinked)
657 ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
659 txtype = (zp->z_zfsvfs->z_version < ZPL_VERSION_FUID) ?
662 if (txtype == TX_ACL)
663 lrsize = sizeof (*lr);
665 lrsize = sizeof (*lrv0);
668 ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
669 (fuidp ? fuidp->z_domain_str_sz : 0) +
670 sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0);
672 itx = zil_itx_create(txtype, txsize);
674 lr = (lr_acl_t *)&itx->itx_lr;
675 lr->lr_foid = zp->z_id;
676 if (txtype == TX_ACL) {
677 lr->lr_acl_bytes = aclbytes;
678 lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
679 lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
680 if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
681 lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
683 lr->lr_acl_flags = 0;
685 lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
687 if (txtype == TX_ACL_V0) {
688 lrv0 = (lr_acl_v0_t *)lr;
689 bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes);
691 void *start = (ace_t *)(lr + 1);
693 bcopy(vsecp->vsa_aclentp, start, aclbytes);
695 start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes);
698 start = zfs_log_fuid_ids(fuidp, start);
699 (void) zfs_log_fuid_domains(fuidp, start);
703 itx->itx_sync = (zp->z_sync_cnt != 0);
704 seq = zil_itx_assign(zilog, itx, tx);
705 zp->z_last_itx = seq;