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 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 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; 1725331Samw } 1735331Samw 1745331Samw static void * 1755331Samw zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start) 1765331Samw { 1775331Samw zfs_fuid_t *zfuid; 1785331Samw uint64_t *fuidloc = start; 1795331Samw 1805331Samw /* First copy in the ACE FUIDs */ 1815331Samw for (zfuid = list_head(&fuidp->z_fuids); zfuid; 1825331Samw zfuid = list_next(&fuidp->z_fuids, zfuid)) { 1835331Samw *fuidloc++ = zfuid->z_logfuid; 1845331Samw } 1855331Samw return (fuidloc); 1865331Samw } 1875331Samw 1885331Samw 1895331Samw static void * 1905331Samw zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start) 1915331Samw { 1925331Samw zfs_fuid_domain_t *zdomain; 1935331Samw 1945331Samw /* now copy in the domain info, if any */ 1955331Samw if (fuidp->z_domain_str_sz != 0) { 1965331Samw for (zdomain = list_head(&fuidp->z_domains); zdomain; 1975331Samw zdomain = list_next(&fuidp->z_domains, zdomain)) { 1985331Samw bcopy((void *)zdomain->z_domain, start, 1995331Samw strlen(zdomain->z_domain) + 1); 2005331Samw start = (caddr_t)start + 2015331Samw strlen(zdomain->z_domain) + 1; 2025331Samw } 2035331Samw } 2045331Samw return (start); 2055331Samw } 2065331Samw 2075331Samw /* 2085331Samw * zfs_log_create() is used to handle TX_CREATE, TX_CREATE_ATTR, TX_MKDIR, 2095331Samw * TX_MKDIR_ATTR and TX_MKXATTR 210789Sahrens * transactions. 2115331Samw * 2125331Samw * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID 2135331Samw * domain information appended prior to the name. In this case the 2145331Samw * uid/gid in the log record will be a log centric FUID. 2155331Samw * 2165331Samw * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that 2175331Samw * may contain attributes, ACL and optional fuid information. 2185331Samw * 2195331Samw * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify 2205331Samw * and ACL and normal users/groups in the ACEs. 2215331Samw * 2225331Samw * There may be an optional xvattr attribute information similar 2235331Samw * to zfs_log_setattr. 2245331Samw * 2255331Samw * Also, after the file name "domain" strings may be appended. 226789Sahrens */ 2272638Sperrin void 2285331Samw zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 2295331Samw znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp, 2305331Samw zfs_fuid_info_t *fuidp, vattr_t *vap) 231789Sahrens { 232789Sahrens itx_t *itx; 233789Sahrens lr_create_t *lr; 2345331Samw lr_acl_create_t *lracl; 2355331Samw size_t aclsize; 2365331Samw size_t xvatsize = 0; 2375331Samw size_t txsize; 2385331Samw xvattr_t *xvap = (xvattr_t *)vap; 2395331Samw void *end; 2405331Samw size_t lrsize; 241789Sahrens size_t namesize = strlen(name) + 1; 2425331Samw size_t fuidsz = 0; 243789Sahrens 24410922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx)) 2452638Sperrin return; 246789Sahrens 2475331Samw /* 2485331Samw * If we have FUIDs present then add in space for 2495331Samw * domains and ACE fuid's if any. 2505331Samw */ 2515331Samw if (fuidp) { 2525331Samw fuidsz += fuidp->z_domain_str_sz; 2535331Samw fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t); 2545331Samw } 2555331Samw 2565331Samw if (vap->va_mask & AT_XVATTR) 2575331Samw xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize); 2585331Samw 2595331Samw if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR || 2605331Samw (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR || 2615331Samw (int)txtype == TX_MKXATTR) { 2625331Samw txsize = sizeof (*lr) + namesize + fuidsz + xvatsize; 2635331Samw lrsize = sizeof (*lr); 2645331Samw } else { 2655331Samw aclsize = (vsecp) ? vsecp->vsa_aclentsz : 0; 2665331Samw txsize = 2675331Samw sizeof (lr_acl_create_t) + namesize + fuidsz + 2685435Smarks ZIL_ACE_LENGTH(aclsize) + xvatsize; 2695331Samw lrsize = sizeof (lr_acl_create_t); 2705331Samw } 2715331Samw 2725331Samw itx = zil_itx_create(txtype, txsize); 2735331Samw 274789Sahrens lr = (lr_create_t *)&itx->itx_lr; 275789Sahrens lr->lr_doid = dzp->z_id; 276789Sahrens lr->lr_foid = zp->z_id; 27711935SMark.Shellenbaum@Sun.COM lr->lr_mode = zp->z_mode; 27811935SMark.Shellenbaum@Sun.COM if (!IS_EPHEMERAL(zp->z_uid)) { 27911935SMark.Shellenbaum@Sun.COM lr->lr_uid = (uint64_t)zp->z_uid; 2805331Samw } else { 2815331Samw lr->lr_uid = fuidp->z_fuid_owner; 2825331Samw } 28311935SMark.Shellenbaum@Sun.COM if (!IS_EPHEMERAL(zp->z_gid)) { 28411935SMark.Shellenbaum@Sun.COM lr->lr_gid = (uint64_t)zp->z_gid; 2855331Samw } else { 2865331Samw lr->lr_gid = fuidp->z_fuid_group; 2875331Samw } 28811935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zp->z_zfsvfs), &lr->lr_gen, 28911935SMark.Shellenbaum@Sun.COM sizeof (uint64_t)); 29011935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs), 29111935SMark.Shellenbaum@Sun.COM lr->lr_crtime, sizeof (uint64_t) * 2); 29211935SMark.Shellenbaum@Sun.COM 29311935SMark.Shellenbaum@Sun.COM if (sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zp->z_zfsvfs), &lr->lr_rdev, 29411935SMark.Shellenbaum@Sun.COM sizeof (lr->lr_rdev)) != 0) 29511935SMark.Shellenbaum@Sun.COM lr->lr_rdev = 0; 2965331Samw 2975331Samw /* 2985331Samw * Fill in xvattr info if any 2995331Samw */ 3005331Samw if (vap->va_mask & AT_XVATTR) { 3015331Samw zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap); 3025331Samw end = (caddr_t)lr + lrsize + xvatsize; 3035331Samw } else { 3045331Samw end = (caddr_t)lr + lrsize; 3055331Samw } 3065331Samw 3075331Samw /* Now fill in any ACL info */ 3085331Samw 3095331Samw if (vsecp) { 3105331Samw lracl = (lr_acl_create_t *)&itx->itx_lr; 3115331Samw lracl->lr_aclcnt = vsecp->vsa_aclcnt; 3125331Samw lracl->lr_acl_bytes = aclsize; 3135331Samw lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0; 3145331Samw lracl->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0; 3155331Samw if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS) 3165331Samw lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags; 3175331Samw else 3185331Samw lracl->lr_acl_flags = 0; 3195331Samw 3205331Samw bcopy(vsecp->vsa_aclentp, end, aclsize); 3215435Smarks end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize); 3225331Samw } 3235331Samw 3245331Samw /* drop in FUID info */ 3255331Samw if (fuidp) { 3265331Samw end = zfs_log_fuid_ids(fuidp, end); 3275331Samw end = zfs_log_fuid_domains(fuidp, end); 3285331Samw } 3295331Samw /* 3305331Samw * Now place file name in log record 3315331Samw */ 3325331Samw bcopy(name, end, namesize); 333789Sahrens 33412699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx); 335789Sahrens } 336789Sahrens 337789Sahrens /* 338789Sahrens * zfs_log_remove() handles both TX_REMOVE and TX_RMDIR transactions. 339789Sahrens */ 3402638Sperrin void 3415331Samw zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 34212699SNeil.Perrin@Sun.COM znode_t *dzp, char *name, uint64_t foid) 343789Sahrens { 344789Sahrens itx_t *itx; 345789Sahrens lr_remove_t *lr; 346789Sahrens size_t namesize = strlen(name) + 1; 347789Sahrens 34810922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx)) 3492638Sperrin return; 350789Sahrens 351789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + namesize); 352789Sahrens lr = (lr_remove_t *)&itx->itx_lr; 353789Sahrens lr->lr_doid = dzp->z_id; 354789Sahrens bcopy(name, (char *)(lr + 1), namesize); 355789Sahrens 35612700SNeil.Perrin@Sun.COM itx->itx_oid = foid; 35712699SNeil.Perrin@Sun.COM 35812699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx); 359789Sahrens } 360789Sahrens 361789Sahrens /* 362789Sahrens * zfs_log_link() handles TX_LINK transactions. 363789Sahrens */ 3642638Sperrin void 3655331Samw zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 366789Sahrens znode_t *dzp, znode_t *zp, char *name) 367789Sahrens { 368789Sahrens itx_t *itx; 369789Sahrens lr_link_t *lr; 370789Sahrens size_t namesize = strlen(name) + 1; 371789Sahrens 37210922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx)) 3732638Sperrin return; 374789Sahrens 375789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + namesize); 376789Sahrens lr = (lr_link_t *)&itx->itx_lr; 377789Sahrens lr->lr_doid = dzp->z_id; 378789Sahrens lr->lr_link_obj = zp->z_id; 379789Sahrens bcopy(name, (char *)(lr + 1), namesize); 380789Sahrens 38112699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx); 382789Sahrens } 383789Sahrens 384789Sahrens /* 385789Sahrens * zfs_log_symlink() handles TX_SYMLINK transactions. 386789Sahrens */ 3872638Sperrin void 3885331Samw zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 3895331Samw znode_t *dzp, znode_t *zp, char *name, char *link) 390789Sahrens { 391789Sahrens itx_t *itx; 392789Sahrens lr_create_t *lr; 393789Sahrens size_t namesize = strlen(name) + 1; 394789Sahrens size_t linksize = strlen(link) + 1; 395789Sahrens 39610922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx)) 3972638Sperrin return; 398789Sahrens 399789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize); 400789Sahrens lr = (lr_create_t *)&itx->itx_lr; 401789Sahrens lr->lr_doid = dzp->z_id; 402789Sahrens lr->lr_foid = zp->z_id; 40311935SMark.Shellenbaum@Sun.COM lr->lr_uid = zp->z_uid; 40411935SMark.Shellenbaum@Sun.COM lr->lr_gid = zp->z_gid; 40511935SMark.Shellenbaum@Sun.COM lr->lr_mode = zp->z_mode; 40611935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zp->z_zfsvfs), &lr->lr_gen, 40711935SMark.Shellenbaum@Sun.COM sizeof (uint64_t)); 40811935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs), 40911935SMark.Shellenbaum@Sun.COM lr->lr_crtime, sizeof (uint64_t) * 2); 410789Sahrens bcopy(name, (char *)(lr + 1), namesize); 411789Sahrens bcopy(link, (char *)(lr + 1) + namesize, linksize); 412789Sahrens 41312699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx); 414789Sahrens } 415789Sahrens 416789Sahrens /* 417789Sahrens * zfs_log_rename() handles TX_RENAME transactions. 418789Sahrens */ 4192638Sperrin void 4205331Samw zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 421*12771SNeil.Perrin@Sun.COM znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp) 422789Sahrens { 423789Sahrens itx_t *itx; 424789Sahrens lr_rename_t *lr; 425789Sahrens size_t snamesize = strlen(sname) + 1; 426789Sahrens size_t dnamesize = strlen(dname) + 1; 427789Sahrens 42810922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx)) 4292638Sperrin return; 430789Sahrens 431789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize); 432789Sahrens lr = (lr_rename_t *)&itx->itx_lr; 433789Sahrens lr->lr_sdoid = sdzp->z_id; 434789Sahrens lr->lr_tdoid = tdzp->z_id; 435789Sahrens bcopy(sname, (char *)(lr + 1), snamesize); 436789Sahrens bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize); 437*12771SNeil.Perrin@Sun.COM itx->itx_oid = szp->z_id; 438789Sahrens 43912699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx); 440789Sahrens } 441789Sahrens 442789Sahrens /* 443789Sahrens * zfs_log_write() handles TX_WRITE transactions. 444789Sahrens */ 4452237Smaybee ssize_t zfs_immediate_write_sz = 32768; 446789Sahrens 4472638Sperrin void 448789Sahrens zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype, 4494620Sperrin znode_t *zp, offset_t off, ssize_t resid, int ioflag) 450789Sahrens { 4511669Sperrin itx_wr_state_t write_state; 4524620Sperrin boolean_t slogging; 4534720Sfr157268 uintptr_t fsync_cnt; 45410310SNeil.Perrin@Sun.COM ssize_t immediate_write_sz; 455789Sahrens 45610922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx) || zp->z_unlinked) 4572638Sperrin return; 458789Sahrens 45910310SNeil.Perrin@Sun.COM immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) 46010310SNeil.Perrin@Sun.COM ? 0 : zfs_immediate_write_sz; 46110310SNeil.Perrin@Sun.COM 46210310SNeil.Perrin@Sun.COM slogging = spa_has_slogs(zilog->zl_spa) && 46310310SNeil.Perrin@Sun.COM (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); 46410310SNeil.Perrin@Sun.COM if (resid > immediate_write_sz && !slogging && resid <= zp->z_blksz) 4651669Sperrin write_state = WR_INDIRECT; 4666396Sperrin else if (ioflag & (FSYNC | FDSYNC)) 4671669Sperrin write_state = WR_COPIED; 4683638Sbillm else 4691669Sperrin write_state = WR_NEED_COPY; 4703638Sbillm 4714720Sfr157268 if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) { 4724720Sfr157268 (void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1)); 4734720Sfr157268 } 4744720Sfr157268 4754620Sperrin while (resid) { 4764620Sperrin itx_t *itx; 4774620Sperrin lr_write_t *lr; 4784620Sperrin ssize_t len; 4794620Sperrin 4804620Sperrin /* 4817360SNeil.Perrin@Sun.COM * If the write would overflow the largest block then split it. 4824620Sperrin */ 4837360SNeil.Perrin@Sun.COM if (write_state != WR_INDIRECT && resid > ZIL_MAX_LOG_DATA) 4844620Sperrin len = SPA_MAXBLOCKSIZE >> 1; 4854620Sperrin else 4864620Sperrin len = resid; 4874620Sperrin 4884620Sperrin itx = zil_itx_create(txtype, sizeof (*lr) + 4894620Sperrin (write_state == WR_COPIED ? len : 0)); 4904620Sperrin lr = (lr_write_t *)&itx->itx_lr; 4914620Sperrin if (write_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os, 4929512SNeil.Perrin@Sun.COM zp->z_id, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) { 49310922SJeff.Bonwick@Sun.COM zil_itx_destroy(itx); 4941669Sperrin itx = zil_itx_create(txtype, sizeof (*lr)); 4953638Sbillm lr = (lr_write_t *)&itx->itx_lr; 4961669Sperrin write_state = WR_NEED_COPY; 4971669Sperrin } 4984620Sperrin 4994620Sperrin itx->itx_wr_state = write_state; 5006101Sperrin if (write_state == WR_NEED_COPY) 5016101Sperrin itx->itx_sod += len; 5024620Sperrin lr->lr_foid = zp->z_id; 5034620Sperrin lr->lr_offset = off; 5044620Sperrin lr->lr_length = len; 5054620Sperrin lr->lr_blkoff = 0; 5064620Sperrin BP_ZERO(&lr->lr_blkptr); 5073638Sbillm 5084620Sperrin itx->itx_private = zp->z_zfsvfs; 509789Sahrens 51012699SNeil.Perrin@Sun.COM if (!(ioflag & (FSYNC | FDSYNC)) && (zp->z_sync_cnt == 0) && 51112699SNeil.Perrin@Sun.COM (fsync_cnt == 0)) 5124720Sfr157268 itx->itx_sync = B_FALSE; 5134720Sfr157268 51412699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx); 515789Sahrens 5164620Sperrin off += len; 5174620Sperrin resid -= len; 5184620Sperrin } 519789Sahrens } 520789Sahrens 521789Sahrens /* 522789Sahrens * zfs_log_truncate() handles TX_TRUNCATE transactions. 523789Sahrens */ 5242638Sperrin void 525789Sahrens zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype, 526789Sahrens znode_t *zp, uint64_t off, uint64_t len) 527789Sahrens { 528789Sahrens itx_t *itx; 529789Sahrens lr_truncate_t *lr; 530789Sahrens 53110922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx) || zp->z_unlinked) 5322638Sperrin return; 533789Sahrens 534789Sahrens itx = zil_itx_create(txtype, sizeof (*lr)); 535789Sahrens lr = (lr_truncate_t *)&itx->itx_lr; 536789Sahrens lr->lr_foid = zp->z_id; 537789Sahrens lr->lr_offset = off; 538789Sahrens lr->lr_length = len; 539789Sahrens 5403063Sperrin itx->itx_sync = (zp->z_sync_cnt != 0); 54112699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx); 542789Sahrens } 543789Sahrens 544789Sahrens /* 545789Sahrens * zfs_log_setattr() handles TX_SETATTR transactions. 546789Sahrens */ 5472638Sperrin void 548789Sahrens zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype, 5495331Samw znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp) 550789Sahrens { 5515331Samw itx_t *itx; 5525331Samw lr_setattr_t *lr; 5535331Samw xvattr_t *xvap = (xvattr_t *)vap; 5545331Samw size_t recsize = sizeof (lr_setattr_t); 5555331Samw void *start; 5565331Samw 55710922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx) || zp->z_unlinked) 5582638Sperrin return; 559789Sahrens 5605331Samw /* 5615331Samw * If XVATTR set, then log record size needs to allow 5625331Samw * for lr_attr_t + xvattr mask, mapsize and create time 5635331Samw * plus actual attribute values 5645331Samw */ 5655331Samw if (vap->va_mask & AT_XVATTR) 5665331Samw recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize); 5675331Samw 5685331Samw if (fuidp) 5695331Samw recsize += fuidp->z_domain_str_sz; 5705331Samw 5715331Samw itx = zil_itx_create(txtype, recsize); 572789Sahrens lr = (lr_setattr_t *)&itx->itx_lr; 573789Sahrens lr->lr_foid = zp->z_id; 574789Sahrens lr->lr_mask = (uint64_t)mask_applied; 575789Sahrens lr->lr_mode = (uint64_t)vap->va_mode; 5765331Samw if ((mask_applied & AT_UID) && IS_EPHEMERAL(vap->va_uid)) 5775331Samw lr->lr_uid = fuidp->z_fuid_owner; 5785331Samw else 5795331Samw lr->lr_uid = (uint64_t)vap->va_uid; 5805331Samw 5815331Samw if ((mask_applied & AT_GID) && IS_EPHEMERAL(vap->va_gid)) 5825331Samw lr->lr_gid = fuidp->z_fuid_group; 5835331Samw else 5845331Samw lr->lr_gid = (uint64_t)vap->va_gid; 5855331Samw 586789Sahrens lr->lr_size = (uint64_t)vap->va_size; 587789Sahrens ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime); 588789Sahrens ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime); 5895331Samw start = (lr_setattr_t *)(lr + 1); 5905331Samw if (vap->va_mask & AT_XVATTR) { 5915331Samw zfs_log_xvattr((lr_attr_t *)start, xvap); 5925331Samw start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize); 5935331Samw } 5945331Samw 5955331Samw /* 5965331Samw * Now stick on domain information if any on end 5975331Samw */ 5985331Samw 5995331Samw if (fuidp) 6005331Samw (void) zfs_log_fuid_domains(fuidp, start); 601789Sahrens 6023063Sperrin itx->itx_sync = (zp->z_sync_cnt != 0); 60312699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx); 604789Sahrens } 605789Sahrens 606789Sahrens /* 607789Sahrens * zfs_log_acl() handles TX_ACL transactions. 608789Sahrens */ 6092638Sperrin void 6105331Samw zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp, 6115331Samw vsecattr_t *vsecp, zfs_fuid_info_t *fuidp) 612789Sahrens { 613789Sahrens itx_t *itx; 6145331Samw lr_acl_v0_t *lrv0; 615789Sahrens lr_acl_t *lr; 6165331Samw int txtype; 6175331Samw int lrsize; 6185331Samw size_t txsize; 6195331Samw size_t aclbytes = vsecp->vsa_aclentsz; 6205331Samw 62110922SJeff.Bonwick@Sun.COM if (zil_replaying(zilog, tx) || zp->z_unlinked) 6226514Smarks return; 6236514Smarks 6246514Smarks txtype = (zp->z_zfsvfs->z_version < ZPL_VERSION_FUID) ? 6255331Samw TX_ACL_V0 : TX_ACL; 6265331Samw 6275331Samw if (txtype == TX_ACL) 6285331Samw lrsize = sizeof (*lr); 6295331Samw else 6305331Samw lrsize = sizeof (*lrv0); 631789Sahrens 6325435Smarks txsize = lrsize + 6335435Smarks ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) + 6345435Smarks (fuidp ? fuidp->z_domain_str_sz : 0) + 6356514Smarks sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0); 6365331Samw 6375331Samw itx = zil_itx_create(txtype, txsize); 6385331Samw 639789Sahrens lr = (lr_acl_t *)&itx->itx_lr; 640789Sahrens lr->lr_foid = zp->z_id; 6415331Samw if (txtype == TX_ACL) { 6425331Samw lr->lr_acl_bytes = aclbytes; 6435331Samw lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0; 6445331Samw lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0; 6455331Samw if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS) 6465331Samw lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags; 6475331Samw else 6485331Samw lr->lr_acl_flags = 0; 6495331Samw } 6505331Samw lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt; 6515331Samw 6525331Samw if (txtype == TX_ACL_V0) { 6535331Samw lrv0 = (lr_acl_v0_t *)lr; 6545331Samw bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes); 6555331Samw } else { 6565331Samw void *start = (ace_t *)(lr + 1); 6575331Samw 6585331Samw bcopy(vsecp->vsa_aclentp, start, aclbytes); 6595331Samw 6605435Smarks start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes); 6615331Samw 6625331Samw if (fuidp) { 6635331Samw start = zfs_log_fuid_ids(fuidp, start); 6645331Samw (void) zfs_log_fuid_domains(fuidp, start); 6655331Samw } 6665331Samw } 667789Sahrens 6683063Sperrin itx->itx_sync = (zp->z_sync_cnt != 0); 66912699SNeil.Perrin@Sun.COM zil_itx_assign(zilog, itx, tx); 670789Sahrens } 671