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.
25 #include <sys/types.h>
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/sysmacros.h>
29 #include <sys/cmn_err.h>
33 #include <sys/zfs_znode.h>
34 #include <sys/zfs_dir.h>
36 #include <sys/zil_impl.h>
37 #include <sys/byteorder.h>
38 #include <sys/policy.h>
43 #include <sys/zfs_fuid.h>
44 #include <sys/dsl_dataset.h>
47 * These zfs_log_* functions must be called within a dmu tx, in one
48 * of 2 contexts depending on zilog->z_replay:
52 * We need to record the transaction so that if it is committed to
53 * the Intent Log then it can be replayed. An intent log transaction
54 * structure (itx_t) is allocated and all the information necessary to
55 * possibly replay the transaction is saved in it. The itx is then assigned
56 * a sequence number and inserted in the in-memory list anchored in the zilog.
60 * We need to mark the intent log record as replayed in the log header.
61 * This is done in the same transaction as the replay so that they
66 zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
68 int isxvattr = (vap->va_mask & AT_XVATTR);
71 if (vsecp == NULL && !isxvattr)
73 if (vsecp && isxvattr)
75 return (TX_CREATE_ACL_ATTR);
77 panic("%s:%u: unsupported condition", __func__, __LINE__);
80 return (TX_CREATE_ACL);
82 return (TX_CREATE_ATTR);
85 if (vsecp == NULL && !isxvattr)
87 if (vsecp && isxvattr)
89 return (TX_MKDIR_ACL_ATTR);
91 panic("%s:%u: unsupported condition", __func__, __LINE__);
94 return (TX_MKDIR_ACL);
96 return (TX_MKDIR_ATTR);
101 return (TX_MAX_TYPE);
105 * build up the log data necessary for logging xvattr_t
106 * First lr_attr_t is initialized. following the lr_attr_t
107 * is the mapsize and attribute bitmap copied from the xvattr_t.
108 * Following the bitmap and bitmapsize two 64 bit words are reserved
109 * for the create time which may be set. Following the create time
110 * records a single 64 bit integer which has the bits to set on
111 * replay for the xvattr.
114 zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
123 xoap = xva_getxoptattr(xvap);
126 lrattr->lr_attr_masksize = xvap->xva_mapsize;
127 bitmap = &lrattr->lr_attr_bitmap;
128 for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) {
129 *bitmap = xvap->xva_reqattrmap[i];
132 /* Now pack the attributes up in a single uint64_t */
133 attrs = (uint64_t *)bitmap;
135 scanstamp = (caddr_t)(crtime + 2);
137 if (XVA_ISSET_REQ(xvap, XAT_READONLY))
138 *attrs |= (xoap->xoa_readonly == 0) ? 0 :
140 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
141 *attrs |= (xoap->xoa_hidden == 0) ? 0 :
143 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
144 *attrs |= (xoap->xoa_system == 0) ? 0 :
146 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
147 *attrs |= (xoap->xoa_archive == 0) ? 0 :
149 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
150 *attrs |= (xoap->xoa_immutable == 0) ? 0 :
152 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
153 *attrs |= (xoap->xoa_nounlink == 0) ? 0 :
155 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
156 *attrs |= (xoap->xoa_appendonly == 0) ? 0 :
158 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
159 *attrs |= (xoap->xoa_opaque == 0) ? 0 :
161 if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
162 *attrs |= (xoap->xoa_nodump == 0) ? 0 :
164 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
165 *attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
167 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
168 *attrs |= (xoap->xoa_av_modified == 0) ? 0 :
170 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
171 ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime);
172 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
173 bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ);
174 if (XVA_ISSET_REQ(xvap, XAT_REPARSE))
175 *attrs |= (xoap->xoa_reparse == 0) ? 0 :
177 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE))
178 *attrs |= (xoap->xoa_offline == 0) ? 0 :
180 if (XVA_ISSET_REQ(xvap, XAT_SPARSE))
181 *attrs |= (xoap->xoa_sparse == 0) ? 0 :
186 zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
189 uint64_t *fuidloc = start;
191 /* First copy in the ACE FUIDs */
192 for (zfuid = list_head(&fuidp->z_fuids); zfuid;
193 zfuid = list_next(&fuidp->z_fuids, zfuid)) {
194 *fuidloc++ = zfuid->z_logfuid;
201 zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
203 zfs_fuid_domain_t *zdomain;
205 /* now copy in the domain info, if any */
206 if (fuidp->z_domain_str_sz != 0) {
207 for (zdomain = list_head(&fuidp->z_domains); zdomain;
208 zdomain = list_next(&fuidp->z_domains, zdomain)) {
209 bcopy((void *)zdomain->z_domain, start,
210 strlen(zdomain->z_domain) + 1);
211 start = (caddr_t)start +
212 strlen(zdomain->z_domain) + 1;
219 * Handles TX_CREATE, TX_CREATE_ATTR, TX_MKDIR, TX_MKDIR_ATTR and
220 * TK_MKXATTR transactions.
222 * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
223 * domain information appended prior to the name. In this case the
224 * uid/gid in the log record will be a log centric FUID.
226 * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
227 * may contain attributes, ACL and optional fuid information.
229 * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
230 * and ACL and normal users/groups in the ACEs.
232 * There may be an optional xvattr attribute information similar
233 * to zfs_log_setattr.
235 * Also, after the file name "domain" strings may be appended.
238 zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
239 znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp,
240 zfs_fuid_info_t *fuidp, vattr_t *vap)
244 lr_acl_create_t *lracl;
245 size_t aclsize = (vsecp != NULL) ? vsecp->vsa_aclentsz : 0;
248 xvattr_t *xvap = (xvattr_t *)vap;
251 size_t namesize = strlen(name) + 1;
254 if (zil_replaying(zilog, tx))
258 * If we have FUIDs present then add in space for
259 * domains and ACE fuid's if any.
262 fuidsz += fuidp->z_domain_str_sz;
263 fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
266 if (vap->va_mask & AT_XVATTR)
267 xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
269 if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
270 (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
271 (int)txtype == TX_MKXATTR) {
272 txsize = sizeof (*lr) + namesize + fuidsz + xvatsize;
273 lrsize = sizeof (*lr);
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_mode;
287 if (!IS_EPHEMERAL(zp->z_uid)) {
288 lr->lr_uid = (uint64_t)zp->z_uid;
290 lr->lr_uid = fuidp->z_fuid_owner;
292 if (!IS_EPHEMERAL(zp->z_gid)) {
293 lr->lr_gid = (uint64_t)zp->z_gid;
295 lr->lr_gid = fuidp->z_fuid_group;
297 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zp->z_zfsvfs), &lr->lr_gen,
299 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
300 lr->lr_crtime, sizeof (uint64_t) * 2);
302 if (sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zp->z_zfsvfs), &lr->lr_rdev,
303 sizeof (lr->lr_rdev)) != 0)
307 * Fill in xvattr info if any
309 if (vap->va_mask & AT_XVATTR) {
310 zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap);
311 end = (caddr_t)lr + lrsize + xvatsize;
313 end = (caddr_t)lr + lrsize;
316 /* Now fill in any ACL info */
319 lracl = (lr_acl_create_t *)&itx->itx_lr;
320 lracl->lr_aclcnt = vsecp->vsa_aclcnt;
321 lracl->lr_acl_bytes = aclsize;
322 lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
323 lracl->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
324 if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
325 lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
327 lracl->lr_acl_flags = 0;
329 bcopy(vsecp->vsa_aclentp, end, aclsize);
330 end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize);
333 /* drop in FUID info */
335 end = zfs_log_fuid_ids(fuidp, end);
336 end = zfs_log_fuid_domains(fuidp, end);
339 * Now place file name in log record
341 bcopy(name, end, namesize);
343 zil_itx_assign(zilog, itx, tx);
347 * Handles both TX_REMOVE and TX_RMDIR transactions.
350 zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
351 znode_t *dzp, char *name, uint64_t foid)
355 size_t namesize = strlen(name) + 1;
357 if (zil_replaying(zilog, tx))
360 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
361 lr = (lr_remove_t *)&itx->itx_lr;
362 lr->lr_doid = dzp->z_id;
363 bcopy(name, (char *)(lr + 1), namesize);
367 zil_itx_assign(zilog, itx, tx);
371 * 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)
379 size_t namesize = strlen(name) + 1;
381 if (zil_replaying(zilog, tx))
384 itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
385 lr = (lr_link_t *)&itx->itx_lr;
386 lr->lr_doid = dzp->z_id;
387 lr->lr_link_obj = zp->z_id;
388 bcopy(name, (char *)(lr + 1), namesize);
390 zil_itx_assign(zilog, itx, tx);
394 * Handles TX_SYMLINK transactions.
397 zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
398 znode_t *dzp, znode_t *zp, char *name, char *link)
402 size_t namesize = strlen(name) + 1;
403 size_t linksize = strlen(link) + 1;
405 if (zil_replaying(zilog, tx))
408 itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
409 lr = (lr_create_t *)&itx->itx_lr;
410 lr->lr_doid = dzp->z_id;
411 lr->lr_foid = zp->z_id;
412 lr->lr_uid = zp->z_uid;
413 lr->lr_gid = zp->z_gid;
414 lr->lr_mode = zp->z_mode;
415 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zp->z_zfsvfs), &lr->lr_gen,
417 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
418 lr->lr_crtime, sizeof (uint64_t) * 2);
419 bcopy(name, (char *)(lr + 1), namesize);
420 bcopy(link, (char *)(lr + 1) + namesize, linksize);
422 zil_itx_assign(zilog, itx, tx);
426 * Handles TX_RENAME transactions.
429 zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
430 znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp)
434 size_t snamesize = strlen(sname) + 1;
435 size_t dnamesize = strlen(dname) + 1;
437 if (zil_replaying(zilog, tx))
440 itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
441 lr = (lr_rename_t *)&itx->itx_lr;
442 lr->lr_sdoid = sdzp->z_id;
443 lr->lr_tdoid = tdzp->z_id;
444 bcopy(sname, (char *)(lr + 1), snamesize);
445 bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize);
446 itx->itx_oid = szp->z_id;
448 zil_itx_assign(zilog, itx, tx);
452 * Handles TX_WRITE transactions.
454 ssize_t zfs_immediate_write_sz = 32768;
457 zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
458 znode_t *zp, offset_t off, ssize_t resid, int ioflag)
460 itx_wr_state_t write_state;
463 ssize_t immediate_write_sz;
465 if (zil_replaying(zilog, tx) || zp->z_unlinked)
468 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
469 ? 0 : zfs_immediate_write_sz;
471 slogging = spa_has_slogs(zilog->zl_spa) &&
472 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
473 if (resid > immediate_write_sz && !slogging && resid <= zp->z_blksz)
474 write_state = WR_INDIRECT;
475 else if (ioflag & (FSYNC | FDSYNC))
476 write_state = WR_COPIED;
478 write_state = WR_NEED_COPY;
480 if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
481 (void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
490 * If the write would overflow the largest block then split it.
492 if (write_state != WR_INDIRECT && resid > ZIL_MAX_LOG_DATA)
493 len = SPA_OLD_MAXBLOCKSIZE >> 1;
497 itx = zil_itx_create(txtype, sizeof (*lr) +
498 (write_state == WR_COPIED ? len : 0));
499 lr = (lr_write_t *)&itx->itx_lr;
500 if (write_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os,
501 zp->z_id, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
502 zil_itx_destroy(itx);
503 itx = zil_itx_create(txtype, sizeof (*lr));
504 lr = (lr_write_t *)&itx->itx_lr;
505 write_state = WR_NEED_COPY;
508 itx->itx_wr_state = write_state;
509 if (write_state == WR_NEED_COPY)
511 lr->lr_foid = zp->z_id;
515 BP_ZERO(&lr->lr_blkptr);
517 itx->itx_private = zp->z_zfsvfs;
519 if (!(ioflag & (FSYNC | FDSYNC)) && (zp->z_sync_cnt == 0) &&
521 itx->itx_sync = B_FALSE;
523 zil_itx_assign(zilog, itx, tx);
531 * Handles TX_TRUNCATE transactions.
534 zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
535 znode_t *zp, uint64_t off, uint64_t len)
540 if (zil_replaying(zilog, tx) || zp->z_unlinked)
543 itx = zil_itx_create(txtype, sizeof (*lr));
544 lr = (lr_truncate_t *)&itx->itx_lr;
545 lr->lr_foid = zp->z_id;
549 itx->itx_sync = (zp->z_sync_cnt != 0);
550 zil_itx_assign(zilog, itx, tx);
554 * Handles TX_SETATTR transactions.
557 zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
558 znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
562 xvattr_t *xvap = (xvattr_t *)vap;
563 size_t recsize = sizeof (lr_setattr_t);
566 if (zil_replaying(zilog, tx) || zp->z_unlinked)
570 * If XVATTR set, then log record size needs to allow
571 * for lr_attr_t + xvattr mask, mapsize and create time
572 * plus actual attribute values
574 if (vap->va_mask & AT_XVATTR)
575 recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
578 recsize += fuidp->z_domain_str_sz;
580 itx = zil_itx_create(txtype, recsize);
581 lr = (lr_setattr_t *)&itx->itx_lr;
582 lr->lr_foid = zp->z_id;
583 lr->lr_mask = (uint64_t)mask_applied;
584 lr->lr_mode = (uint64_t)vap->va_mode;
585 if ((mask_applied & AT_UID) && IS_EPHEMERAL(vap->va_uid))
586 lr->lr_uid = fuidp->z_fuid_owner;
588 lr->lr_uid = (uint64_t)vap->va_uid;
590 if ((mask_applied & AT_GID) && IS_EPHEMERAL(vap->va_gid))
591 lr->lr_gid = fuidp->z_fuid_group;
593 lr->lr_gid = (uint64_t)vap->va_gid;
595 lr->lr_size = (uint64_t)vap->va_size;
596 ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
597 ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
598 start = (lr_setattr_t *)(lr + 1);
599 if (vap->va_mask & AT_XVATTR) {
600 zfs_log_xvattr((lr_attr_t *)start, xvap);
601 start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize);
605 * Now stick on domain information if any on end
609 (void) zfs_log_fuid_domains(fuidp, start);
611 itx->itx_sync = (zp->z_sync_cnt != 0);
612 zil_itx_assign(zilog, itx, tx);
616 * Handles TX_ACL transactions.
619 zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
620 vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
628 size_t aclbytes = vsecp->vsa_aclentsz;
630 if (zil_replaying(zilog, tx) || zp->z_unlinked)
633 txtype = (zp->z_zfsvfs->z_version < ZPL_VERSION_FUID) ?
636 if (txtype == TX_ACL)
637 lrsize = sizeof (*lr);
639 lrsize = sizeof (*lrv0);
642 ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
643 (fuidp ? fuidp->z_domain_str_sz : 0) +
644 sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0);
646 itx = zil_itx_create(txtype, txsize);
648 lr = (lr_acl_t *)&itx->itx_lr;
649 lr->lr_foid = zp->z_id;
650 if (txtype == TX_ACL) {
651 lr->lr_acl_bytes = aclbytes;
652 lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
653 lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
654 if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
655 lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
657 lr->lr_acl_flags = 0;
659 lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
661 if (txtype == TX_ACL_V0) {
662 lrv0 = (lr_acl_v0_t *)lr;
663 bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes);
665 void *start = (ace_t *)(lr + 1);
667 bcopy(vsecp->vsa_aclentp, start, aclbytes);
669 start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes);
672 start = zfs_log_fuid_ids(fuidp, start);
673 (void) zfs_log_fuid_domains(fuidp, start);
677 itx->itx_sync = (zp->z_sync_cnt != 0);
678 zil_itx_assign(zilog, itx, tx);