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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "@(#)zfs_log.c 1.13 08/04/09 SMI"
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sysmacros.h>
32 #include <sys/cmn_err.h>
34 #include <sys/thread.h>
37 #include <sys/zfs_znode.h>
38 #include <sys/zfs_dir.h>
40 #include <sys/zil_impl.h>
41 #include <sys/byteorder.h>
42 #include <sys/policy.h>
48 #include <sys/zfs_fuid.h>
52 * All the functions in this file are used to construct the log entries
53 * to record transactions. They allocate * an intent log transaction
54 * structure (itx_t) and save within it all the information necessary to
55 * possibly replay the transaction. The itx is then assigned a sequence
56 * number and inserted in the in-memory list anchored in the zilog.
60 zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
62 int isxvattr = (vap->va_mask & AT_XVATTR);
65 if (vsecp == NULL && !isxvattr)
67 if (vsecp && isxvattr)
68 return (TX_CREATE_ACL_ATTR);
70 return (TX_CREATE_ACL);
72 return (TX_CREATE_ATTR);
75 if (vsecp == NULL && !isxvattr)
77 if (vsecp && isxvattr)
78 return (TX_MKDIR_ACL_ATTR);
80 return (TX_MKDIR_ACL);
82 return (TX_MKDIR_ATTR);
91 * build up the log data necessary for logging xvattr_t
92 * First lr_attr_t is initialized. following the lr_attr_t
93 * is the mapsize and attribute bitmap copied from the xvattr_t.
94 * Following the bitmap and bitmapsize two 64 bit words are reserved
95 * for the create time which may be set. Following the create time
96 * records a single 64 bit integer which has the bits to set on
97 * replay for the xvattr.
100 zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
109 xoap = xva_getxoptattr(xvap);
112 lrattr->lr_attr_masksize = xvap->xva_mapsize;
113 bitmap = &lrattr->lr_attr_bitmap;
114 for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) {
115 *bitmap = xvap->xva_reqattrmap[i];
118 /* Now pack the attributes up in a single uint64_t */
119 attrs = (uint64_t *)bitmap;
121 scanstamp = (caddr_t)(crtime + 2);
123 if (XVA_ISSET_REQ(xvap, XAT_READONLY))
124 *attrs |= (xoap->xoa_readonly == 0) ? 0 :
126 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
127 *attrs |= (xoap->xoa_hidden == 0) ? 0 :
129 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
130 *attrs |= (xoap->xoa_system == 0) ? 0 :
132 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
133 *attrs |= (xoap->xoa_archive == 0) ? 0 :
135 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
136 *attrs |= (xoap->xoa_immutable == 0) ? 0 :
138 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
139 *attrs |= (xoap->xoa_nounlink == 0) ? 0 :
141 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
142 *attrs |= (xoap->xoa_appendonly == 0) ? 0 :
144 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
145 *attrs |= (xoap->xoa_opaque == 0) ? 0 :
147 if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
148 *attrs |= (xoap->xoa_nodump == 0) ? 0 :
150 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
151 *attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
153 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
154 *attrs |= (xoap->xoa_av_modified == 0) ? 0 :
156 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
157 ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime);
158 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
159 bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ);
163 zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
166 uint64_t *fuidloc = start;
168 /* First copy in the ACE FUIDs */
169 for (zfuid = list_head(&fuidp->z_fuids); zfuid;
170 zfuid = list_next(&fuidp->z_fuids, zfuid)) {
171 *fuidloc++ = zfuid->z_logfuid;
178 zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
180 zfs_fuid_domain_t *zdomain;
182 /* now copy in the domain info, if any */
183 if (fuidp->z_domain_str_sz != 0) {
184 for (zdomain = list_head(&fuidp->z_domains); zdomain;
185 zdomain = list_next(&fuidp->z_domains, zdomain)) {
186 bcopy((void *)zdomain->z_domain, start,
187 strlen(zdomain->z_domain) + 1);
188 start = (caddr_t)start +
189 strlen(zdomain->z_domain) + 1;
196 * zfs_log_create() is used to handle TX_CREATE, TX_CREATE_ATTR, TX_MKDIR,
197 * TX_MKDIR_ATTR and TX_MKXATTR
200 * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
201 * domain information appended prior to the name. In this case the
202 * uid/gid in the log record will be a log centric FUID.
204 * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
205 * may contain attributes, ACL and optional fuid information.
207 * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
208 * and ACL and normal users/groups in the ACEs.
210 * There may be an optional xvattr attribute information similar
211 * to zfs_log_setattr.
213 * Also, after the file name "domain" strings may be appended.
216 zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
217 znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp,
218 zfs_fuid_info_t *fuidp, vattr_t *vap)
223 lr_acl_create_t *lracl;
227 xvattr_t *xvap = (xvattr_t *)vap;
231 size_t namesize = strlen(name) + 1;
238 * If we have FUIDs present then add in space for
239 * domains and ACE fuid's if any.
242 fuidsz += fuidp->z_domain_str_sz;
243 fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
246 if (vap->va_mask & AT_XVATTR)
247 xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
249 if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
250 (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
251 (int)txtype == TX_MKXATTR) {
252 txsize = sizeof (*lr) + namesize + fuidsz + xvatsize;
253 lrsize = sizeof (*lr);
255 aclsize = (vsecp) ? vsecp->vsa_aclentsz : 0;
257 sizeof (lr_acl_create_t) + namesize + fuidsz +
258 ZIL_ACE_LENGTH(aclsize) + xvatsize;
259 lrsize = sizeof (lr_acl_create_t);
262 itx = zil_itx_create(txtype, txsize);
264 lr = (lr_create_t *)&itx->itx_lr;
265 lr->lr_doid = dzp->z_id;
266 lr->lr_foid = zp->z_id;
267 lr->lr_mode = zp->z_phys->zp_mode;
268 if (!IS_EPHEMERAL(zp->z_phys->zp_uid)) {
269 lr->lr_uid = (uint64_t)zp->z_phys->zp_uid;
271 lr->lr_uid = fuidp->z_fuid_owner;
273 if (!IS_EPHEMERAL(zp->z_phys->zp_gid)) {
274 lr->lr_gid = (uint64_t)zp->z_phys->zp_gid;
276 lr->lr_gid = fuidp->z_fuid_group;
278 lr->lr_gen = zp->z_phys->zp_gen;
279 lr->lr_crtime[0] = zp->z_phys->zp_crtime[0];
280 lr->lr_crtime[1] = zp->z_phys->zp_crtime[1];
281 lr->lr_rdev = zp->z_phys->zp_rdev;
284 * Fill in xvattr info if any
286 if (vap->va_mask & AT_XVATTR) {
287 zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap);
288 end = (caddr_t)lr + lrsize + xvatsize;
290 end = (caddr_t)lr + lrsize;
293 /* Now fill in any ACL info */
296 lracl = (lr_acl_create_t *)&itx->itx_lr;
297 lracl->lr_aclcnt = vsecp->vsa_aclcnt;
298 lracl->lr_acl_bytes = aclsize;
299 lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
300 lracl->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
301 if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
302 lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
304 lracl->lr_acl_flags = 0;
306 bcopy(vsecp->vsa_aclentp, end, aclsize);
307 end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize);
310 /* drop in FUID info */
312 end = zfs_log_fuid_ids(fuidp, end);
313 end = zfs_log_fuid_domains(fuidp, end);
316 * Now place file name in log record
318 bcopy(name, end, namesize);
320 seq = zil_itx_assign(zilog, itx, tx);
321 dzp->z_last_itx = seq;
322 zp->z_last_itx = seq;
326 * zfs_log_remove() handles both TX_REMOVE and TX_RMDIR transactions.
329 zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
330 znode_t *dzp, char *name)
335 size_t namesize = strlen(name) + 1;
340 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
341 lr = (lr_remove_t *)&itx->itx_lr;
342 lr->lr_doid = dzp->z_id;
343 bcopy(name, (char *)(lr + 1), namesize);
345 seq = zil_itx_assign(zilog, itx, tx);
346 dzp->z_last_itx = seq;
350 * zfs_log_link() handles TX_LINK transactions.
353 zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
354 znode_t *dzp, znode_t *zp, char *name)
359 size_t namesize = strlen(name) + 1;
364 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
365 lr = (lr_link_t *)&itx->itx_lr;
366 lr->lr_doid = dzp->z_id;
367 lr->lr_link_obj = zp->z_id;
368 bcopy(name, (char *)(lr + 1), namesize);
370 seq = zil_itx_assign(zilog, itx, tx);
371 dzp->z_last_itx = seq;
372 zp->z_last_itx = seq;
376 * zfs_log_symlink() handles TX_SYMLINK transactions.
379 zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
380 znode_t *dzp, znode_t *zp, char *name, char *link)
385 size_t namesize = strlen(name) + 1;
386 size_t linksize = strlen(link) + 1;
391 itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
392 lr = (lr_create_t *)&itx->itx_lr;
393 lr->lr_doid = dzp->z_id;
394 lr->lr_foid = zp->z_id;
395 lr->lr_mode = zp->z_phys->zp_mode;
396 lr->lr_uid = zp->z_phys->zp_uid;
397 lr->lr_gid = zp->z_phys->zp_gid;
398 lr->lr_gen = zp->z_phys->zp_gen;
399 lr->lr_crtime[0] = zp->z_phys->zp_crtime[0];
400 lr->lr_crtime[1] = zp->z_phys->zp_crtime[1];
401 bcopy(name, (char *)(lr + 1), namesize);
402 bcopy(link, (char *)(lr + 1) + namesize, linksize);
404 seq = zil_itx_assign(zilog, itx, tx);
405 dzp->z_last_itx = seq;
406 zp->z_last_itx = seq;
410 * zfs_log_rename() handles TX_RENAME transactions.
413 zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
414 znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp)
419 size_t snamesize = strlen(sname) + 1;
420 size_t dnamesize = strlen(dname) + 1;
425 itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
426 lr = (lr_rename_t *)&itx->itx_lr;
427 lr->lr_sdoid = sdzp->z_id;
428 lr->lr_tdoid = tdzp->z_id;
429 bcopy(sname, (char *)(lr + 1), snamesize);
430 bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize);
432 seq = zil_itx_assign(zilog, itx, tx);
433 sdzp->z_last_itx = seq;
434 tdzp->z_last_itx = seq;
435 szp->z_last_itx = seq;
439 * zfs_log_write() handles TX_WRITE transactions.
441 ssize_t zfs_immediate_write_sz = 32768;
443 #define ZIL_MAX_LOG_DATA (SPA_MAXBLOCKSIZE - sizeof (zil_trailer_t) - \
447 zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
448 znode_t *zp, offset_t off, ssize_t resid, int ioflag)
450 itx_wr_state_t write_state;
454 if (zilog == NULL || zp->z_unlinked)
458 * Writes are handled in three different ways:
461 * If the write is greater than zfs_immediate_write_sz and there are
462 * no separate logs in this pool then later *if* we need to log the
463 * write then dmu_sync() is used to immediately write the block and
464 * its block pointer is put in the log record.
466 * If we know we'll immediately be committing the
467 * transaction (FSYNC or FDSYNC), the we allocate a larger
468 * log record here for the data and copy the data in.
470 * Otherwise we don't allocate a buffer, and *if* we need to
471 * flush the write later then a buffer is allocated and
472 * we retrieve the data using the dmu.
474 slogging = spa_has_slogs(zilog->zl_spa);
475 if (resid > zfs_immediate_write_sz && !slogging)
476 write_state = WR_INDIRECT;
477 else if (ioflag & (FSYNC | FDSYNC))
478 write_state = WR_COPIED;
480 write_state = WR_NEED_COPY;
482 if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
483 (void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
492 * If there are slogs and the write would overflow the largest
493 * block, then because we don't want to use the main pool
494 * to dmu_sync, we have to split the write.
496 if (slogging && resid > ZIL_MAX_LOG_DATA)
497 len = SPA_MAXBLOCKSIZE >> 1;
501 itx = zil_itx_create(txtype, sizeof (*lr) +
502 (write_state == WR_COPIED ? len : 0));
503 lr = (lr_write_t *)&itx->itx_lr;
504 if (write_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os,
505 zp->z_id, off, len, lr + 1) != 0) {
506 kmem_free(itx, offsetof(itx_t, itx_lr) +
507 itx->itx_lr.lrc_reclen);
508 itx = zil_itx_create(txtype, sizeof (*lr));
509 lr = (lr_write_t *)&itx->itx_lr;
510 write_state = WR_NEED_COPY;
513 itx->itx_wr_state = write_state;
514 if (write_state == WR_NEED_COPY)
516 lr->lr_foid = zp->z_id;
520 BP_ZERO(&lr->lr_blkptr);
522 itx->itx_private = zp->z_zfsvfs;
524 if ((zp->z_sync_cnt != 0) || (fsync_cnt != 0) ||
525 (ioflag & (FSYNC | FDSYNC)))
526 itx->itx_sync = B_TRUE;
528 itx->itx_sync = B_FALSE;
530 zp->z_last_itx = zil_itx_assign(zilog, itx, tx);
538 * zfs_log_truncate() handles TX_TRUNCATE transactions.
541 zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
542 znode_t *zp, uint64_t off, uint64_t len)
548 if (zilog == NULL || zp->z_unlinked)
551 itx = zil_itx_create(txtype, sizeof (*lr));
552 lr = (lr_truncate_t *)&itx->itx_lr;
553 lr->lr_foid = zp->z_id;
557 itx->itx_sync = (zp->z_sync_cnt != 0);
558 seq = zil_itx_assign(zilog, itx, tx);
559 zp->z_last_itx = seq;
563 * zfs_log_setattr() handles TX_SETATTR transactions.
566 zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
567 znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
572 xvattr_t *xvap = (xvattr_t *)vap;
573 size_t recsize = sizeof (lr_setattr_t);
577 if (zilog == NULL || zp->z_unlinked)
581 * If XVATTR set, then log record size needs to allow
582 * for lr_attr_t + xvattr mask, mapsize and create time
583 * plus actual attribute values
585 if (vap->va_mask & AT_XVATTR)
586 recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
589 recsize += fuidp->z_domain_str_sz;
591 itx = zil_itx_create(txtype, recsize);
592 lr = (lr_setattr_t *)&itx->itx_lr;
593 lr->lr_foid = zp->z_id;
594 lr->lr_mask = (uint64_t)mask_applied;
595 lr->lr_mode = (uint64_t)vap->va_mode;
596 if ((mask_applied & AT_UID) && IS_EPHEMERAL(vap->va_uid))
597 lr->lr_uid = fuidp->z_fuid_owner;
599 lr->lr_uid = (uint64_t)vap->va_uid;
601 if ((mask_applied & AT_GID) && IS_EPHEMERAL(vap->va_gid))
602 lr->lr_gid = fuidp->z_fuid_group;
604 lr->lr_gid = (uint64_t)vap->va_gid;
606 lr->lr_size = (uint64_t)vap->va_size;
607 ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
608 ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
609 start = (lr_setattr_t *)(lr + 1);
610 if (vap->va_mask & AT_XVATTR) {
611 zfs_log_xvattr((lr_attr_t *)start, xvap);
612 start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize);
616 * Now stick on domain information if any on end
620 (void) zfs_log_fuid_domains(fuidp, start);
622 itx->itx_sync = (zp->z_sync_cnt != 0);
623 seq = zil_itx_assign(zilog, itx, tx);
624 zp->z_last_itx = seq;
628 * zfs_log_acl() handles TX_ACL transactions.
631 zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
632 vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
641 size_t aclbytes = vsecp->vsa_aclentsz;
643 txtype = (zp->z_zfsvfs->z_version == ZPL_VERSION_INITIAL) ?
646 if (txtype == TX_ACL)
647 lrsize = sizeof (*lr);
649 lrsize = sizeof (*lrv0);
651 if (zilog == NULL || zp->z_unlinked)
655 ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
656 (fuidp ? fuidp->z_domain_str_sz : 0) +
657 sizeof (uint64) * (fuidp ? fuidp->z_fuid_cnt : 0);
659 itx = zil_itx_create(txtype, txsize);
661 lr = (lr_acl_t *)&itx->itx_lr;
662 lr->lr_foid = zp->z_id;
663 if (txtype == TX_ACL) {
664 lr->lr_acl_bytes = aclbytes;
665 lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
666 lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
667 if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
668 lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
670 lr->lr_acl_flags = 0;
672 lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
674 if (txtype == TX_ACL_V0) {
675 lrv0 = (lr_acl_v0_t *)lr;
676 bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes);
678 void *start = (ace_t *)(lr + 1);
680 bcopy(vsecp->vsa_aclentp, start, aclbytes);
682 start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes);
685 start = zfs_log_fuid_ids(fuidp, start);
686 (void) zfs_log_fuid_domains(fuidp, start);
690 itx->itx_sync = (zp->z_sync_cnt != 0);
691 seq = zil_itx_assign(zilog, itx, tx);
692 zp->z_last_itx = seq;