1789Sahrens /*
2789Sahrens * CDDL HEADER START
3789Sahrens *
4789Sahrens * The contents of this file are subject to the terms of the
51669Sperrin * Common Development and Distribution License (the "License").
61669Sperrin * You may not use this file except in compliance with the License.
7789Sahrens *
8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens * or http://www.opensolaris.org/os/licensing.
10789Sahrens * See the License for the specific language governing permissions
11789Sahrens * and limitations under the License.
12789Sahrens *
13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens *
19789Sahrens * CDDL HEADER END
20789Sahrens */
21789Sahrens /*
2212699SNeil.Perrin@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens */
24789Sahrens
25789Sahrens #include <sys/types.h>
26789Sahrens #include <sys/param.h>
27789Sahrens #include <sys/systm.h>
28789Sahrens #include <sys/sysmacros.h>
29789Sahrens #include <sys/cmn_err.h>
30789Sahrens #include <sys/kmem.h>
31789Sahrens #include <sys/thread.h>
32789Sahrens #include <sys/file.h>
33789Sahrens #include <sys/vfs.h>
34789Sahrens #include <sys/zfs_znode.h>
35789Sahrens #include <sys/zfs_dir.h>
36789Sahrens #include <sys/zil.h>
374620Sperrin #include <sys/zil_impl.h>
38789Sahrens #include <sys/byteorder.h>
39789Sahrens #include <sys/policy.h>
40789Sahrens #include <sys/stat.h>
41789Sahrens #include <sys/mode.h>
42789Sahrens #include <sys/acl.h>
43789Sahrens #include <sys/dmu.h>
44789Sahrens #include <sys/spa.h>
455331Samw #include <sys/zfs_fuid.h>
46789Sahrens #include <sys/ddi.h>
478227SNeil.Perrin@Sun.COM #include <sys/dsl_dataset.h>
488227SNeil.Perrin@Sun.COM
49789Sahrens /*
508227SNeil.Perrin@Sun.COM * These zfs_log_* functions must be called within a dmu tx, in one
518227SNeil.Perrin@Sun.COM * of 2 contexts depending on zilog->z_replay:
528227SNeil.Perrin@Sun.COM *
538227SNeil.Perrin@Sun.COM * Non replay mode
548227SNeil.Perrin@Sun.COM * ---------------
558227SNeil.Perrin@Sun.COM * We need to record the transaction so that if it is committed to
568227SNeil.Perrin@Sun.COM * the Intent Log then it can be replayed. An intent log transaction
578227SNeil.Perrin@Sun.COM * structure (itx_t) is allocated and all the information necessary to
588227SNeil.Perrin@Sun.COM * possibly replay the transaction is saved in it. The itx is then assigned
598227SNeil.Perrin@Sun.COM * a sequence number and inserted in the in-memory list anchored in the zilog.
608227SNeil.Perrin@Sun.COM *
618227SNeil.Perrin@Sun.COM * Replay mode
628227SNeil.Perrin@Sun.COM * -----------
638227SNeil.Perrin@Sun.COM * We need to mark the intent log record as replayed in the log header.
648227SNeil.Perrin@Sun.COM * This is done in the same transaction as the replay so that they
658227SNeil.Perrin@Sun.COM * commit atomically.
66789Sahrens */
67789Sahrens
685331Samw int
zfs_log_create_txtype(zil_create_t type,vsecattr_t * vsecp,vattr_t * vap)695331Samw zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
705331Samw {
715331Samw int isxvattr = (vap->va_mask & AT_XVATTR);
725331Samw switch (type) {
735331Samw case Z_FILE:
745331Samw if (vsecp == NULL && !isxvattr)
755331Samw return (TX_CREATE);
765331Samw if (vsecp && isxvattr)
775331Samw return (TX_CREATE_ACL_ATTR);
785331Samw if (vsecp)
795331Samw return (TX_CREATE_ACL);
805331Samw else
815331Samw return (TX_CREATE_ATTR);
825331Samw /*NOTREACHED*/
835331Samw case Z_DIR:
845331Samw if (vsecp == NULL && !isxvattr)
855331Samw return (TX_MKDIR);
865331Samw if (vsecp && isxvattr)
875331Samw return (TX_MKDIR_ACL_ATTR);
885331Samw if (vsecp)
895331Samw return (TX_MKDIR_ACL);
905331Samw else
915331Samw return (TX_MKDIR_ATTR);
925331Samw case Z_XATTRDIR:
935331Samw return (TX_MKXATTR);
945331Samw }
955331Samw ASSERT(0);
965331Samw return (TX_MAX_TYPE);
975331Samw }
985331Samw
99789Sahrens /*
1005331Samw * build up the log data necessary for logging xvattr_t
1015331Samw * First lr_attr_t is initialized. following the lr_attr_t
1025331Samw * is the mapsize and attribute bitmap copied from the xvattr_t.
1035331Samw * Following the bitmap and bitmapsize two 64 bit words are reserved
1045331Samw * for the create time which may be set. Following the create time
1055331Samw * records a single 64 bit integer which has the bits to set on
1065331Samw * replay for the xvattr.
1075331Samw */
1085331Samw static void
zfs_log_xvattr(lr_attr_t * lrattr,xvattr_t * xvap)1095331Samw zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
1105331Samw {
1115331Samw uint32_t *bitmap;
1125331Samw uint64_t *attrs;
1135331Samw uint64_t *crtime;
1145331Samw xoptattr_t *xoap;
1155331Samw void *scanstamp;
1165331Samw int i;
1175331Samw
1185331Samw xoap = xva_getxoptattr(xvap);
1195331Samw ASSERT(xoap);
1205331Samw
1215331Samw lrattr->lr_attr_masksize = xvap->xva_mapsize;
1225331Samw bitmap = &lrattr->lr_attr_bitmap;
1235331Samw for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) {
1245331Samw *bitmap = xvap->xva_reqattrmap[i];
1255331Samw }
1265331Samw
1275331Samw /* Now pack the attributes up in a single uint64_t */
1285331Samw attrs = (uint64_t *)bitmap;
1295331Samw crtime = attrs + 1;
1305331Samw scanstamp = (caddr_t)(crtime + 2);
1315331Samw *attrs = 0;
1325331Samw if (XVA_ISSET_REQ(xvap, XAT_READONLY))
1335331Samw *attrs |= (xoap->xoa_readonly == 0) ? 0 :
1345331Samw XAT0_READONLY;
1355331Samw if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
1365331Samw *attrs |= (xoap->xoa_hidden == 0) ? 0 :
1375331Samw XAT0_HIDDEN;
1385331Samw if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
1395331Samw *attrs |= (xoap->xoa_system == 0) ? 0 :
1405331Samw XAT0_SYSTEM;
1415331Samw if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
1425331Samw *attrs |= (xoap->xoa_archive == 0) ? 0 :
1435331Samw XAT0_ARCHIVE;
1445331Samw if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
1455331Samw *attrs |= (xoap->xoa_immutable == 0) ? 0 :
1465331Samw XAT0_IMMUTABLE;
1475331Samw if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
1485331Samw *attrs |= (xoap->xoa_nounlink == 0) ? 0 :
1495331Samw XAT0_NOUNLINK;
1505331Samw if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
1515331Samw *attrs |= (xoap->xoa_appendonly == 0) ? 0 :
1525331Samw XAT0_APPENDONLY;
1535331Samw if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
1545331Samw *attrs |= (xoap->xoa_opaque == 0) ? 0 :
1555331Samw XAT0_APPENDONLY;
1565331Samw if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
1575331Samw *attrs |= (xoap->xoa_nodump == 0) ? 0 :
1585331Samw XAT0_NODUMP;
1595331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
1605331Samw *attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
1615331Samw XAT0_AV_QUARANTINED;
1625331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
1635331Samw *attrs |= (xoap->xoa_av_modified == 0) ? 0 :
1645331Samw XAT0_AV_MODIFIED;
1655331Samw if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
1665331Samw ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime);
1675331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
1685331Samw bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ);
16910793Sdai.ngo@sun.com if (XVA_ISSET_REQ(xvap, XAT_REPARSE))
17010793Sdai.ngo@sun.com *attrs |= (xoap->xoa_reparse == 0) ? 0 :
17110793Sdai.ngo@sun.com XAT0_REPARSE;
172*13082SJoyce.McIntosh@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_OFFLINE))
173*13082SJoyce.McIntosh@Sun.COM *attrs |= (xoap->xoa_offline == 0) ? 0 :
174*13082SJoyce.McIntosh@Sun.COM XAT0_OFFLINE;
175*13082SJoyce.McIntosh@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_SPARSE))
176*13082SJoyce.McIntosh@Sun.COM *attrs |= (xoap->xoa_sparse == 0) ? 0 :
177*13082SJoyce.McIntosh@Sun.COM XAT0_SPARSE;
1785331Samw }
1795331Samw
1805331Samw static void *
zfs_log_fuid_ids(zfs_fuid_info_t * fuidp,void * start)1815331Samw zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
1825331Samw {
1835331Samw zfs_fuid_t *zfuid;
1845331Samw uint64_t *fuidloc = start;
1855331Samw
1865331Samw /* First copy in the ACE FUIDs */
1875331Samw for (zfuid = list_head(&fuidp->z_fuids); zfuid;
1885331Samw zfuid = list_next(&fuidp->z_fuids, zfuid)) {
1895331Samw *fuidloc++ = zfuid->z_logfuid;
1905331Samw }
1915331Samw return (fuidloc);
1925331Samw }
1935331Samw
1945331Samw
1955331Samw static void *
zfs_log_fuid_domains(zfs_fuid_info_t * fuidp,void * start)1965331Samw zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
1975331Samw {
1985331Samw zfs_fuid_domain_t *zdomain;
1995331Samw
2005331Samw /* now copy in the domain info, if any */
2015331Samw if (fuidp->z_domain_str_sz != 0) {
2025331Samw for (zdomain = list_head(&fuidp->z_domains); zdomain;
2035331Samw zdomain = list_next(&fuidp->z_domains, zdomain)) {
2045331Samw bcopy((void *)zdomain->z_domain, start,
2055331Samw strlen(zdomain->z_domain) + 1);
2065331Samw start = (caddr_t)start +
2075331Samw strlen(zdomain->z_domain) + 1;
2085331Samw }
2095331Samw }
2105331Samw return (start);
2115331Samw }
2125331Samw
2135331Samw /*
2145331Samw * zfs_log_create() is used to handle TX_CREATE, TX_CREATE_ATTR, TX_MKDIR,
2155331Samw * TX_MKDIR_ATTR and TX_MKXATTR
216789Sahrens * transactions.
2175331Samw *
2185331Samw * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
2195331Samw * domain information appended prior to the name. In this case the
2205331Samw * uid/gid in the log record will be a log centric FUID.
2215331Samw *
2225331Samw * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
2235331Samw * may contain attributes, ACL and optional fuid information.
2245331Samw *
2255331Samw * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
2265331Samw * and ACL and normal users/groups in the ACEs.
2275331Samw *
2285331Samw * There may be an optional xvattr attribute information similar
2295331Samw * to zfs_log_setattr.
2305331Samw *
2315331Samw * Also, after the file name "domain" strings may be appended.
232789Sahrens */
2332638Sperrin void
zfs_log_create(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * dzp,znode_t * zp,char * name,vsecattr_t * vsecp,zfs_fuid_info_t * fuidp,vattr_t * vap)2345331Samw zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
2355331Samw znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp,
2365331Samw zfs_fuid_info_t *fuidp, vattr_t *vap)
237789Sahrens {
238789Sahrens itx_t *itx;
239789Sahrens lr_create_t *lr;
2405331Samw lr_acl_create_t *lracl;
2415331Samw size_t aclsize;
2425331Samw size_t xvatsize = 0;
2435331Samw size_t txsize;
2445331Samw xvattr_t *xvap = (xvattr_t *)vap;
2455331Samw void *end;
2465331Samw size_t lrsize;
247789Sahrens size_t namesize = strlen(name) + 1;
2485331Samw size_t fuidsz = 0;
249789Sahrens
25010922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx))
2512638Sperrin return;
252789Sahrens
2535331Samw /*
2545331Samw * If we have FUIDs present then add in space for
2555331Samw * domains and ACE fuid's if any.
2565331Samw */
2575331Samw if (fuidp) {
2585331Samw fuidsz += fuidp->z_domain_str_sz;
2595331Samw fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
2605331Samw }
2615331Samw
2625331Samw if (vap->va_mask & AT_XVATTR)
2635331Samw xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
2645331Samw
2655331Samw if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
2665331Samw (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
2675331Samw (int)txtype == TX_MKXATTR) {
2685331Samw txsize = sizeof (*lr) + namesize + fuidsz + xvatsize;
2695331Samw lrsize = sizeof (*lr);
2705331Samw } else {
2715331Samw aclsize = (vsecp) ? vsecp->vsa_aclentsz : 0;
2725331Samw txsize =
2735331Samw sizeof (lr_acl_create_t) + namesize + fuidsz +
2745435Smarks ZIL_ACE_LENGTH(aclsize) + xvatsize;
2755331Samw lrsize = sizeof (lr_acl_create_t);
2765331Samw }
2775331Samw
2785331Samw itx = zil_itx_create(txtype, txsize);
2795331Samw
280789Sahrens lr = (lr_create_t *)&itx->itx_lr;
281789Sahrens lr->lr_doid = dzp->z_id;
282789Sahrens lr->lr_foid = zp->z_id;
28311935SMark.Shellenbaum@Sun.COM lr->lr_mode = zp->z_mode;
28411935SMark.Shellenbaum@Sun.COM if (!IS_EPHEMERAL(zp->z_uid)) {
28511935SMark.Shellenbaum@Sun.COM lr->lr_uid = (uint64_t)zp->z_uid;
2865331Samw } else {
2875331Samw lr->lr_uid = fuidp->z_fuid_owner;
2885331Samw }
28911935SMark.Shellenbaum@Sun.COM if (!IS_EPHEMERAL(zp->z_gid)) {
29011935SMark.Shellenbaum@Sun.COM lr->lr_gid = (uint64_t)zp->z_gid;
2915331Samw } else {
2925331Samw lr->lr_gid = fuidp->z_fuid_group;
2935331Samw }
29411935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zp->z_zfsvfs), &lr->lr_gen,
29511935SMark.Shellenbaum@Sun.COM sizeof (uint64_t));
29611935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
29711935SMark.Shellenbaum@Sun.COM lr->lr_crtime, sizeof (uint64_t) * 2);
29811935SMark.Shellenbaum@Sun.COM
29911935SMark.Shellenbaum@Sun.COM if (sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zp->z_zfsvfs), &lr->lr_rdev,
30011935SMark.Shellenbaum@Sun.COM sizeof (lr->lr_rdev)) != 0)
30111935SMark.Shellenbaum@Sun.COM lr->lr_rdev = 0;
3025331Samw
3035331Samw /*
3045331Samw * Fill in xvattr info if any
3055331Samw */
3065331Samw if (vap->va_mask & AT_XVATTR) {
3075331Samw zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap);
3085331Samw end = (caddr_t)lr + lrsize + xvatsize;
3095331Samw } else {
3105331Samw end = (caddr_t)lr + lrsize;
3115331Samw }
3125331Samw
3135331Samw /* Now fill in any ACL info */
3145331Samw
3155331Samw if (vsecp) {
3165331Samw lracl = (lr_acl_create_t *)&itx->itx_lr;
3175331Samw lracl->lr_aclcnt = vsecp->vsa_aclcnt;
3185331Samw lracl->lr_acl_bytes = aclsize;
3195331Samw lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
3205331Samw lracl->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
3215331Samw if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
3225331Samw lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
3235331Samw else
3245331Samw lracl->lr_acl_flags = 0;
3255331Samw
3265331Samw bcopy(vsecp->vsa_aclentp, end, aclsize);
3275435Smarks end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize);
3285331Samw }
3295331Samw
3305331Samw /* drop in FUID info */
3315331Samw if (fuidp) {
3325331Samw end = zfs_log_fuid_ids(fuidp, end);
3335331Samw end = zfs_log_fuid_domains(fuidp, end);
3345331Samw }
3355331Samw /*
3365331Samw * Now place file name in log record
3375331Samw */
3385331Samw bcopy(name, end, namesize);
339789Sahrens
34012699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx);
341789Sahrens }
342789Sahrens
343789Sahrens /*
344789Sahrens * zfs_log_remove() handles both TX_REMOVE and TX_RMDIR transactions.
345789Sahrens */
3462638Sperrin void
zfs_log_remove(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * dzp,char * name,uint64_t foid)3475331Samw zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
34812699SNeil.Perrin@Sun.COM znode_t *dzp, char *name, uint64_t foid)
349789Sahrens {
350789Sahrens itx_t *itx;
351789Sahrens lr_remove_t *lr;
352789Sahrens size_t namesize = strlen(name) + 1;
353789Sahrens
35410922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx))
3552638Sperrin return;
356789Sahrens
357789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
358789Sahrens lr = (lr_remove_t *)&itx->itx_lr;
359789Sahrens lr->lr_doid = dzp->z_id;
360789Sahrens bcopy(name, (char *)(lr + 1), namesize);
361789Sahrens
36212700SNeil.Perrin@Sun.COM itx->itx_oid = foid;
36312699SNeil.Perrin@Sun.COM
36412699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx);
365789Sahrens }
366789Sahrens
367789Sahrens /*
368789Sahrens * zfs_log_link() handles TX_LINK transactions.
369789Sahrens */
3702638Sperrin void
zfs_log_link(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * dzp,znode_t * zp,char * name)3715331Samw zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
372789Sahrens znode_t *dzp, znode_t *zp, char *name)
373789Sahrens {
374789Sahrens itx_t *itx;
375789Sahrens lr_link_t *lr;
376789Sahrens size_t namesize = strlen(name) + 1;
377789Sahrens
37810922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx))
3792638Sperrin return;
380789Sahrens
381789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
382789Sahrens lr = (lr_link_t *)&itx->itx_lr;
383789Sahrens lr->lr_doid = dzp->z_id;
384789Sahrens lr->lr_link_obj = zp->z_id;
385789Sahrens bcopy(name, (char *)(lr + 1), namesize);
386789Sahrens
38712699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx);
388789Sahrens }
389789Sahrens
390789Sahrens /*
391789Sahrens * zfs_log_symlink() handles TX_SYMLINK transactions.
392789Sahrens */
3932638Sperrin void
zfs_log_symlink(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * dzp,znode_t * zp,char * name,char * link)3945331Samw zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
3955331Samw znode_t *dzp, znode_t *zp, char *name, char *link)
396789Sahrens {
397789Sahrens itx_t *itx;
398789Sahrens lr_create_t *lr;
399789Sahrens size_t namesize = strlen(name) + 1;
400789Sahrens size_t linksize = strlen(link) + 1;
401789Sahrens
40210922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx))
4032638Sperrin return;
404789Sahrens
405789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
406789Sahrens lr = (lr_create_t *)&itx->itx_lr;
407789Sahrens lr->lr_doid = dzp->z_id;
408789Sahrens lr->lr_foid = zp->z_id;
40911935SMark.Shellenbaum@Sun.COM lr->lr_uid = zp->z_uid;
41011935SMark.Shellenbaum@Sun.COM lr->lr_gid = zp->z_gid;
41111935SMark.Shellenbaum@Sun.COM lr->lr_mode = zp->z_mode;
41211935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zp->z_zfsvfs), &lr->lr_gen,
41311935SMark.Shellenbaum@Sun.COM sizeof (uint64_t));
41411935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
41511935SMark.Shellenbaum@Sun.COM lr->lr_crtime, sizeof (uint64_t) * 2);
416789Sahrens bcopy(name, (char *)(lr + 1), namesize);
417789Sahrens bcopy(link, (char *)(lr + 1) + namesize, linksize);
418789Sahrens
41912699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx);
420789Sahrens }
421789Sahrens
422789Sahrens /*
423789Sahrens * zfs_log_rename() handles TX_RENAME transactions.
424789Sahrens */
4252638Sperrin void
zfs_log_rename(zilog_t * zilog,dmu_tx_t * tx,uint64_t txtype,znode_t * sdzp,char * sname,znode_t * tdzp,char * dname,znode_t * szp)4265331Samw zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
42712771SNeil.Perrin@Sun.COM znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp)
428789Sahrens {
429789Sahrens itx_t *itx;
430789Sahrens lr_rename_t *lr;
431789Sahrens size_t snamesize = strlen(sname) + 1;
432789Sahrens size_t dnamesize = strlen(dname) + 1;
433789Sahrens
43410922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx))
4352638Sperrin return;
436789Sahrens
437789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
438789Sahrens lr = (lr_rename_t *)&itx->itx_lr;
439789Sahrens lr->lr_sdoid = sdzp->z_id;
440789Sahrens lr->lr_tdoid = tdzp->z_id;
441789Sahrens bcopy(sname, (char *)(lr + 1), snamesize);
442789Sahrens bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize);
44312771SNeil.Perrin@Sun.COM itx->itx_oid = szp->z_id;
444789Sahrens
44512699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx);
446789Sahrens }
447789Sahrens
448789Sahrens /*
449789Sahrens * zfs_log_write() handles TX_WRITE transactions.
450789Sahrens */
4512237Smaybee ssize_t zfs_immediate_write_sz = 32768;
452789Sahrens
4532638Sperrin void
zfs_log_write(zilog_t * zilog,dmu_tx_t * tx,int txtype,znode_t * zp,offset_t off,ssize_t resid,int ioflag)454789Sahrens zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
4554620Sperrin znode_t *zp, offset_t off, ssize_t resid, int ioflag)
456789Sahrens {
4571669Sperrin itx_wr_state_t write_state;
4584620Sperrin boolean_t slogging;
4594720Sfr157268 uintptr_t fsync_cnt;
46010310SNeil.Perrin@Sun.COM ssize_t immediate_write_sz;
461789Sahrens
46210922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx) || zp->z_unlinked)
4632638Sperrin return;
464789Sahrens
46510310SNeil.Perrin@Sun.COM immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
46610310SNeil.Perrin@Sun.COM ? 0 : zfs_immediate_write_sz;
46710310SNeil.Perrin@Sun.COM
46810310SNeil.Perrin@Sun.COM slogging = spa_has_slogs(zilog->zl_spa) &&
46910310SNeil.Perrin@Sun.COM (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
47010310SNeil.Perrin@Sun.COM if (resid > immediate_write_sz && !slogging && resid <= zp->z_blksz)
4711669Sperrin write_state = WR_INDIRECT;
4726396Sperrin else if (ioflag & (FSYNC | FDSYNC))
4731669Sperrin write_state = WR_COPIED;
4743638Sbillm else
4751669Sperrin write_state = WR_NEED_COPY;
4763638Sbillm
4774720Sfr157268 if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
4784720Sfr157268 (void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
4794720Sfr157268 }
4804720Sfr157268
4814620Sperrin while (resid) {
4824620Sperrin itx_t *itx;
4834620Sperrin lr_write_t *lr;
4844620Sperrin ssize_t len;
4854620Sperrin
4864620Sperrin /*
4877360SNeil.Perrin@Sun.COM * If the write would overflow the largest block then split it.
4884620Sperrin */
4897360SNeil.Perrin@Sun.COM if (write_state != WR_INDIRECT && resid > ZIL_MAX_LOG_DATA)
4904620Sperrin len = SPA_MAXBLOCKSIZE >> 1;
4914620Sperrin else
4924620Sperrin len = resid;
4934620Sperrin
4944620Sperrin itx = zil_itx_create(txtype, sizeof (*lr) +
4954620Sperrin (write_state == WR_COPIED ? len : 0));
4964620Sperrin lr = (lr_write_t *)&itx->itx_lr;
4974620Sperrin if (write_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os,
4989512SNeil.Perrin@Sun.COM zp->z_id, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
49910922SJeff.Bonwick@Sun.COM zil_itx_destroy(itx);
5001669Sperrin itx = zil_itx_create(txtype, sizeof (*lr));
5013638Sbillm lr = (lr_write_t *)&itx->itx_lr;
5021669Sperrin write_state = WR_NEED_COPY;
5031669Sperrin }
5044620Sperrin
5054620Sperrin itx->itx_wr_state = write_state;
5066101Sperrin if (write_state == WR_NEED_COPY)
5076101Sperrin itx->itx_sod += len;
5084620Sperrin lr->lr_foid = zp->z_id;
5094620Sperrin lr->lr_offset = off;
5104620Sperrin lr->lr_length = len;
5114620Sperrin lr->lr_blkoff = 0;
5124620Sperrin BP_ZERO(&lr->lr_blkptr);
5133638Sbillm
5144620Sperrin itx->itx_private = zp->z_zfsvfs;
515789Sahrens
51612699SNeil.Perrin@Sun.COM if (!(ioflag & (FSYNC | FDSYNC)) && (zp->z_sync_cnt == 0) &&
51712699SNeil.Perrin@Sun.COM (fsync_cnt == 0))
5184720Sfr157268 itx->itx_sync = B_FALSE;
5194720Sfr157268
52012699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx);
521789Sahrens
5224620Sperrin off += len;
5234620Sperrin resid -= len;
5244620Sperrin }
525789Sahrens }
526789Sahrens
527789Sahrens /*
528789Sahrens * zfs_log_truncate() handles TX_TRUNCATE transactions.
529789Sahrens */
5302638Sperrin void
zfs_log_truncate(zilog_t * zilog,dmu_tx_t * tx,int txtype,znode_t * zp,uint64_t off,uint64_t len)531789Sahrens zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
532789Sahrens znode_t *zp, uint64_t off, uint64_t len)
533789Sahrens {
534789Sahrens itx_t *itx;
535789Sahrens lr_truncate_t *lr;
536789Sahrens
53710922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx) || zp->z_unlinked)
5382638Sperrin return;
539789Sahrens
540789Sahrens itx = zil_itx_create(txtype, sizeof (*lr));
541789Sahrens lr = (lr_truncate_t *)&itx->itx_lr;
542789Sahrens lr->lr_foid = zp->z_id;
543789Sahrens lr->lr_offset = off;
544789Sahrens lr->lr_length = len;
545789Sahrens
5463063Sperrin itx->itx_sync = (zp->z_sync_cnt != 0);
54712699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx);
548789Sahrens }
549789Sahrens
550789Sahrens /*
551789Sahrens * zfs_log_setattr() handles TX_SETATTR transactions.
552789Sahrens */
5532638Sperrin void
zfs_log_setattr(zilog_t * zilog,dmu_tx_t * tx,int txtype,znode_t * zp,vattr_t * vap,uint_t mask_applied,zfs_fuid_info_t * fuidp)554789Sahrens zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
5555331Samw znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
556789Sahrens {
5575331Samw itx_t *itx;
5585331Samw lr_setattr_t *lr;
5595331Samw xvattr_t *xvap = (xvattr_t *)vap;
5605331Samw size_t recsize = sizeof (lr_setattr_t);
5615331Samw void *start;
5625331Samw
56310922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx) || zp->z_unlinked)
5642638Sperrin return;
565789Sahrens
5665331Samw /*
5675331Samw * If XVATTR set, then log record size needs to allow
5685331Samw * for lr_attr_t + xvattr mask, mapsize and create time
5695331Samw * plus actual attribute values
5705331Samw */
5715331Samw if (vap->va_mask & AT_XVATTR)
5725331Samw recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
5735331Samw
5745331Samw if (fuidp)
5755331Samw recsize += fuidp->z_domain_str_sz;
5765331Samw
5775331Samw itx = zil_itx_create(txtype, recsize);
578789Sahrens lr = (lr_setattr_t *)&itx->itx_lr;
579789Sahrens lr->lr_foid = zp->z_id;
580789Sahrens lr->lr_mask = (uint64_t)mask_applied;
581789Sahrens lr->lr_mode = (uint64_t)vap->va_mode;
5825331Samw if ((mask_applied & AT_UID) && IS_EPHEMERAL(vap->va_uid))
5835331Samw lr->lr_uid = fuidp->z_fuid_owner;
5845331Samw else
5855331Samw lr->lr_uid = (uint64_t)vap->va_uid;
5865331Samw
5875331Samw if ((mask_applied & AT_GID) && IS_EPHEMERAL(vap->va_gid))
5885331Samw lr->lr_gid = fuidp->z_fuid_group;
5895331Samw else
5905331Samw lr->lr_gid = (uint64_t)vap->va_gid;
5915331Samw
592789Sahrens lr->lr_size = (uint64_t)vap->va_size;
593789Sahrens ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
594789Sahrens ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
5955331Samw start = (lr_setattr_t *)(lr + 1);
5965331Samw if (vap->va_mask & AT_XVATTR) {
5975331Samw zfs_log_xvattr((lr_attr_t *)start, xvap);
5985331Samw start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize);
5995331Samw }
6005331Samw
6015331Samw /*
6025331Samw * Now stick on domain information if any on end
6035331Samw */
6045331Samw
6055331Samw if (fuidp)
6065331Samw (void) zfs_log_fuid_domains(fuidp, start);
607789Sahrens
6083063Sperrin itx->itx_sync = (zp->z_sync_cnt != 0);
60912699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx);
610789Sahrens }
611789Sahrens
612789Sahrens /*
613789Sahrens * zfs_log_acl() handles TX_ACL transactions.
614789Sahrens */
6152638Sperrin void
zfs_log_acl(zilog_t * zilog,dmu_tx_t * tx,znode_t * zp,vsecattr_t * vsecp,zfs_fuid_info_t * fuidp)6165331Samw zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
6175331Samw vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
618789Sahrens {
619789Sahrens itx_t *itx;
6205331Samw lr_acl_v0_t *lrv0;
621789Sahrens lr_acl_t *lr;
6225331Samw int txtype;
6235331Samw int lrsize;
6245331Samw size_t txsize;
6255331Samw size_t aclbytes = vsecp->vsa_aclentsz;
6265331Samw
62710922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx) || zp->z_unlinked)
6286514Smarks return;
6296514Smarks
6306514Smarks txtype = (zp->z_zfsvfs->z_version < ZPL_VERSION_FUID) ?
6315331Samw TX_ACL_V0 : TX_ACL;
6325331Samw
6335331Samw if (txtype == TX_ACL)
6345331Samw lrsize = sizeof (*lr);
6355331Samw else
6365331Samw lrsize = sizeof (*lrv0);
637789Sahrens
6385435Smarks txsize = lrsize +
6395435Smarks ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
6405435Smarks (fuidp ? fuidp->z_domain_str_sz : 0) +
6416514Smarks sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0);
6425331Samw
6435331Samw itx = zil_itx_create(txtype, txsize);
6445331Samw
645789Sahrens lr = (lr_acl_t *)&itx->itx_lr;
646789Sahrens lr->lr_foid = zp->z_id;
6475331Samw if (txtype == TX_ACL) {
6485331Samw lr->lr_acl_bytes = aclbytes;
6495331Samw lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
6505331Samw lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
6515331Samw if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
6525331Samw lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
6535331Samw else
6545331Samw lr->lr_acl_flags = 0;
6555331Samw }
6565331Samw lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
6575331Samw
6585331Samw if (txtype == TX_ACL_V0) {
6595331Samw lrv0 = (lr_acl_v0_t *)lr;
6605331Samw bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes);
6615331Samw } else {
6625331Samw void *start = (ace_t *)(lr + 1);
6635331Samw
6645331Samw bcopy(vsecp->vsa_aclentp, start, aclbytes);
6655331Samw
6665435Smarks start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes);
6675331Samw
6685331Samw if (fuidp) {
6695331Samw start = zfs_log_fuid_ids(fuidp, start);
6705331Samw (void) zfs_log_fuid_domains(fuidp, start);
6715331Samw }
6725331Samw }
673789Sahrens
6743063Sperrin itx->itx_sync = (zp->z_sync_cnt != 0);
67512699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx);
676789Sahrens }
677