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.
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>
51 * These zfs_log_* functions must be called within a dmu tx, in one
52 * of 2 contexts depending on zilog->z_replay:
56 * We need to record the transaction so that if it is committed to
57 * the Intent Log then it can be replayed. An intent log transaction
58 * structure (itx_t) is allocated and all the information necessary to
59 * possibly replay the transaction is saved in it. The itx is then assigned
60 * a sequence number and inserted in the in-memory list anchored in the zilog.
64 * We need to mark the intent log record as replayed in the log header.
65 * This is done in the same transaction as the replay so that they
70 zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
73 int isxvattr = (vap->va_mask & AT_XVATTR);
76 #endif /* HAVE_XVATTR */
79 if (vsecp == NULL && !isxvattr)
81 if (vsecp && isxvattr)
82 return (TX_CREATE_ACL_ATTR);
84 return (TX_CREATE_ACL);
86 return (TX_CREATE_ATTR);
89 if (vsecp == NULL && !isxvattr)
91 if (vsecp && isxvattr)
92 return (TX_MKDIR_ACL_ATTR);
94 return (TX_MKDIR_ACL);
96 return (TX_MKDIR_ATTR);
101 return (TX_MAX_TYPE);
106 * build up the log data necessary for logging xvattr_t
107 * First lr_attr_t is initialized. following the lr_attr_t
108 * is the mapsize and attribute bitmap copied from the xvattr_t.
109 * Following the bitmap and bitmapsize two 64 bit words are reserved
110 * for the create time which may be set. Following the create time
111 * records a single 64 bit integer which has the bits to set on
112 * replay for the xvattr.
115 zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
124 xoap = xva_getxoptattr(xvap);
127 lrattr->lr_attr_masksize = xvap->xva_mapsize;
128 bitmap = &lrattr->lr_attr_bitmap;
129 for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) {
130 *bitmap = xvap->xva_reqattrmap[i];
133 /* Now pack the attributes up in a single uint64_t */
134 attrs = (uint64_t *)bitmap;
136 scanstamp = (caddr_t)(crtime + 2);
138 if (XVA_ISSET_REQ(xvap, XAT_READONLY))
139 *attrs |= (xoap->xoa_readonly == 0) ? 0 :
141 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
142 *attrs |= (xoap->xoa_hidden == 0) ? 0 :
144 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
145 *attrs |= (xoap->xoa_system == 0) ? 0 :
147 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
148 *attrs |= (xoap->xoa_archive == 0) ? 0 :
150 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
151 *attrs |= (xoap->xoa_immutable == 0) ? 0 :
153 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
154 *attrs |= (xoap->xoa_nounlink == 0) ? 0 :
156 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
157 *attrs |= (xoap->xoa_appendonly == 0) ? 0 :
159 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
160 *attrs |= (xoap->xoa_opaque == 0) ? 0 :
162 if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
163 *attrs |= (xoap->xoa_nodump == 0) ? 0 :
165 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
166 *attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
168 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
169 *attrs |= (xoap->xoa_av_modified == 0) ? 0 :
171 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
172 ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime);
173 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
174 bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ);
175 if (XVA_ISSET_REQ(xvap, XAT_REPARSE))
176 *attrs |= (xoap->xoa_reparse == 0) ? 0 :
178 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE))
179 *attrs |= (xoap->xoa_offline == 0) ? 0 :
181 if (XVA_ISSET_REQ(xvap, XAT_SPARSE))
182 *attrs |= (xoap->xoa_sparse == 0) ? 0 :
187 zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
190 uint64_t *fuidloc = start;
192 /* First copy in the ACE FUIDs */
193 for (zfuid = list_head(&fuidp->z_fuids); zfuid;
194 zfuid = list_next(&fuidp->z_fuids, zfuid)) {
195 *fuidloc++ = zfuid->z_logfuid;
202 zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
204 zfs_fuid_domain_t *zdomain;
206 /* now copy in the domain info, if any */
207 if (fuidp->z_domain_str_sz != 0) {
208 for (zdomain = list_head(&fuidp->z_domains); zdomain;
209 zdomain = list_next(&fuidp->z_domains, zdomain)) {
210 bcopy((void *)zdomain->z_domain, start,
211 strlen(zdomain->z_domain) + 1);
212 start = (caddr_t)start +
213 strlen(zdomain->z_domain) + 1;
218 #endif /* HAVE_XVATTR */
221 * zfs_log_create() is used to handle TX_CREATE, TX_CREATE_ATTR, TX_MKDIR,
222 * TX_MKDIR_ATTR and TX_MKXATTR
225 * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
226 * domain information appended prior to the name. In this case the
227 * uid/gid in the log record will be a log centric FUID.
229 * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
230 * may contain attributes, ACL and optional fuid information.
232 * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
233 * and ACL and normal users/groups in the ACEs.
235 * There may be an optional xvattr attribute information similar
236 * to zfs_log_setattr.
238 * Also, after the file name "domain" strings may be appended.
241 zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
242 znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp,
243 zfs_fuid_info_t *fuidp, vattr_t *vap)
248 lr_acl_create_t *lracl;
249 xvattr_t *xvap = (xvattr_t *)vap;
250 #endif /* HAVE_XVATTR */
256 size_t namesize = strlen(name) + 1;
259 if (zil_replaying(zilog, tx))
263 * If we have FUIDs present then add in space for
264 * domains and ACE fuid's if any.
267 fuidsz += fuidp->z_domain_str_sz;
268 fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
272 if (vap->va_mask & AT_XVATTR)
273 xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
274 #endif /* HAVE_XVATTR */
276 if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
277 (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
278 (int)txtype == TX_MKXATTR) {
279 txsize = sizeof (*lr) + namesize + fuidsz + xvatsize;
280 lrsize = sizeof (*lr);
282 aclsize = (vsecp) ? vsecp->vsa_aclentsz : 0;
284 sizeof (lr_acl_create_t) + namesize + fuidsz +
285 ZIL_ACE_LENGTH(aclsize) + xvatsize;
286 lrsize = sizeof (lr_acl_create_t);
289 itx = zil_itx_create(txtype, txsize);
291 lr = (lr_create_t *)&itx->itx_lr;
292 lr->lr_doid = dzp->z_id;
293 lr->lr_foid = zp->z_id;
294 lr->lr_mode = zp->z_mode;
295 if (!IS_EPHEMERAL(zp->z_uid)) {
296 lr->lr_uid = (uint64_t)zp->z_uid;
298 lr->lr_uid = fuidp->z_fuid_owner;
300 if (!IS_EPHEMERAL(zp->z_gid)) {
301 lr->lr_gid = (uint64_t)zp->z_gid;
303 lr->lr_gid = fuidp->z_fuid_group;
305 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(zp)), &lr->lr_gen,
307 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
308 lr->lr_crtime, sizeof (uint64_t) * 2);
310 if (sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(ZTOZSB(zp)), &lr->lr_rdev,
311 sizeof (lr->lr_rdev)) != 0)
315 * Fill in xvattr info if any
318 if (vap->va_mask & AT_XVATTR) {
319 zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap);
320 end = (caddr_t)lr + lrsize + xvatsize;
322 end = (caddr_t)lr + lrsize;
325 /* Now fill in any ACL info */
328 lracl = (lr_acl_create_t *)&itx->itx_lr;
329 lracl->lr_aclcnt = vsecp->vsa_aclcnt;
330 lracl->lr_acl_bytes = aclsize;
331 lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
332 lracl->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
333 if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
334 lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
336 lracl->lr_acl_flags = 0;
338 bcopy(vsecp->vsa_aclentp, end, aclsize);
339 end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize);
342 /* drop in FUID info */
344 end = zfs_log_fuid_ids(fuidp, end);
345 end = zfs_log_fuid_domains(fuidp, end);
348 end = (caddr_t)lr + lrsize;
349 #endif /* HAVE_XVATTR */
351 * Now place file name in log record
353 bcopy(name, end, namesize);
355 zil_itx_assign(zilog, itx, tx);
359 * zfs_log_remove() handles both TX_REMOVE and TX_RMDIR transactions.
362 zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
363 znode_t *dzp, char *name, uint64_t foid)
367 size_t namesize = strlen(name) + 1;
369 if (zil_replaying(zilog, tx))
372 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
373 lr = (lr_remove_t *)&itx->itx_lr;
374 lr->lr_doid = dzp->z_id;
375 bcopy(name, (char *)(lr + 1), namesize);
379 zil_itx_assign(zilog, itx, tx);
383 * zfs_log_link() handles TX_LINK transactions.
386 zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
387 znode_t *dzp, znode_t *zp, char *name)
391 size_t namesize = strlen(name) + 1;
393 if (zil_replaying(zilog, tx))
396 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
397 lr = (lr_link_t *)&itx->itx_lr;
398 lr->lr_doid = dzp->z_id;
399 lr->lr_link_obj = zp->z_id;
400 bcopy(name, (char *)(lr + 1), namesize);
402 zil_itx_assign(zilog, itx, tx);
406 * zfs_log_symlink() handles TX_SYMLINK transactions.
409 zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
410 znode_t *dzp, znode_t *zp, char *name, char *link)
414 size_t namesize = strlen(name) + 1;
415 size_t linksize = strlen(link) + 1;
417 if (zil_replaying(zilog, tx))
420 itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
421 lr = (lr_create_t *)&itx->itx_lr;
422 lr->lr_doid = dzp->z_id;
423 lr->lr_foid = zp->z_id;
424 lr->lr_uid = zp->z_uid;
425 lr->lr_gid = zp->z_gid;
426 lr->lr_mode = zp->z_mode;
427 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(zp)), &lr->lr_gen,
429 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
430 lr->lr_crtime, sizeof (uint64_t) * 2);
431 bcopy(name, (char *)(lr + 1), namesize);
432 bcopy(link, (char *)(lr + 1) + namesize, linksize);
434 zil_itx_assign(zilog, itx, tx);
438 * zfs_log_rename() handles TX_RENAME transactions.
441 zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
442 znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp)
446 size_t snamesize = strlen(sname) + 1;
447 size_t dnamesize = strlen(dname) + 1;
449 if (zil_replaying(zilog, tx))
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);
458 itx->itx_oid = szp->z_id;
460 zil_itx_assign(zilog, itx, tx);
464 * zfs_log_write() handles TX_WRITE transactions.
466 ssize_t zfs_immediate_write_sz = 32768;
469 zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
470 znode_t *zp, offset_t off, ssize_t resid, int ioflag)
472 itx_wr_state_t write_state;
475 ssize_t immediate_write_sz;
477 if (zil_replaying(zilog, tx) || zp->z_unlinked)
480 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
481 ? 0 : zfs_immediate_write_sz;
483 slogging = spa_has_slogs(zilog->zl_spa) &&
484 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
485 if (resid > immediate_write_sz && !slogging && resid <= zp->z_blksz)
486 write_state = WR_INDIRECT;
487 else if (ioflag & (FSYNC | FDSYNC))
488 write_state = WR_COPIED;
490 write_state = WR_NEED_COPY;
492 if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
493 (void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
502 * If the write would overflow the largest block then split it.
504 if (write_state != WR_INDIRECT && resid > ZIL_MAX_LOG_DATA)
505 len = SPA_MAXBLOCKSIZE >> 1;
509 itx = zil_itx_create(txtype, sizeof (*lr) +
510 (write_state == WR_COPIED ? len : 0));
511 lr = (lr_write_t *)&itx->itx_lr;
512 if (write_state == WR_COPIED && dmu_read(ZTOZSB(zp)->z_os,
513 zp->z_id, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
514 zil_itx_destroy(itx);
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 = ZTOZSB(zp);
531 if (!(ioflag & (FSYNC | FDSYNC)) && (zp->z_sync_cnt == 0) &&
533 itx->itx_sync = B_FALSE;
535 zil_itx_assign(zilog, itx, tx);
543 * zfs_log_truncate() handles TX_TRUNCATE transactions.
546 zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
547 znode_t *zp, uint64_t off, uint64_t len)
552 if (zil_replaying(zilog, tx) || zp->z_unlinked)
555 itx = zil_itx_create(txtype, sizeof (*lr));
556 lr = (lr_truncate_t *)&itx->itx_lr;
557 lr->lr_foid = zp->z_id;
561 itx->itx_sync = (zp->z_sync_cnt != 0);
562 zil_itx_assign(zilog, itx, tx);
566 * zfs_log_setattr() handles TX_SETATTR transactions.
569 zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype, znode_t *zp,
570 struct iattr *attr, uint_t mask_applied, zfs_fuid_info_t *fuidp)
575 xvattr_t *xvap = (xvattr_t *)vap;
576 #endif /* HAVEXVATTR */
577 size_t recsize = sizeof (lr_setattr_t);
580 if (zil_replaying(zilog, tx) || zp->z_unlinked)
584 * If XVATTR set, then log record size needs to allow
585 * for lr_attr_t + xvattr mask, mapsize and create time
586 * plus actual attribute values
589 if (attr->ia_valid & AT_XVATTR)
590 recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
593 recsize += fuidp->z_domain_str_sz;
594 #endif /* HAVE_XVATTR */
596 itx = zil_itx_create(txtype, recsize);
597 lr = (lr_setattr_t *)&itx->itx_lr;
598 lr->lr_foid = zp->z_id;
599 lr->lr_mask = (uint64_t)mask_applied;
600 lr->lr_mode = (uint64_t)attr->ia_mode;
601 if ((mask_applied & ATTR_UID) && IS_EPHEMERAL(attr->ia_uid))
602 lr->lr_uid = fuidp->z_fuid_owner;
604 lr->lr_uid = (uint64_t)attr->ia_uid;
606 if ((mask_applied & ATTR_GID) && IS_EPHEMERAL(attr->ia_gid))
607 lr->lr_gid = fuidp->z_fuid_group;
609 lr->lr_gid = (uint64_t)attr->ia_gid;
611 lr->lr_size = (uint64_t)attr->ia_size;
612 ZFS_TIME_ENCODE(&attr->ia_atime, lr->lr_atime);
613 ZFS_TIME_ENCODE(&attr->ia_mtime, lr->lr_mtime);
614 start = (lr_setattr_t *)(lr + 1);
616 if (attr->ia_valid & ATTR_XVATTR) {
617 zfs_log_xvattr((lr_attr_t *)start, xvap);
618 start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize);
622 * Now stick on domain information if any on end
626 (void) zfs_log_fuid_domains(fuidp, start);
627 #endif /* HAVE_XVATTR */
629 itx->itx_sync = (zp->z_sync_cnt != 0);
630 zil_itx_assign(zilog, itx, tx);
634 * zfs_log_acl() handles TX_ACL transactions.
637 zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
638 vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
646 size_t aclbytes = vsecp->vsa_aclentsz;
648 if (zil_replaying(zilog, tx) || zp->z_unlinked)
651 txtype = (ZTOZSB(zp)->z_version < ZPL_VERSION_FUID) ?
654 if (txtype == TX_ACL)
655 lrsize = sizeof (*lr);
657 lrsize = sizeof (*lrv0);
660 ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
661 (fuidp ? fuidp->z_domain_str_sz : 0) +
662 sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0);
664 itx = zil_itx_create(txtype, txsize);
666 lr = (lr_acl_t *)&itx->itx_lr;
667 lr->lr_foid = zp->z_id;
668 if (txtype == TX_ACL) {
669 lr->lr_acl_bytes = aclbytes;
670 lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
671 lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
672 if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
673 lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
675 lr->lr_acl_flags = 0;
677 lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
679 if (txtype == TX_ACL_V0) {
680 lrv0 = (lr_acl_v0_t *)lr;
681 bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes);
683 void *start = (ace_t *)(lr + 1);
685 bcopy(vsecp->vsa_aclentp, start, aclbytes);
687 start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes);
691 start = zfs_log_fuid_ids(fuidp, start);
692 (void) zfs_log_fuid_domains(fuidp, start);
694 #endif /* HAVE_XVATTR */
697 itx->itx_sync = (zp->z_sync_cnt != 0);
698 zil_itx_assign(zilog, itx, tx);