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 /* 226101Sperrin * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #include <sys/types.h> 27789Sahrens #include <sys/param.h> 28789Sahrens #include <sys/systm.h> 29789Sahrens #include <sys/sysmacros.h> 30789Sahrens #include <sys/cmn_err.h> 31789Sahrens #include <sys/kmem.h> 32789Sahrens #include <sys/thread.h> 33789Sahrens #include <sys/file.h> 34789Sahrens #include <sys/vfs.h> 35789Sahrens #include <sys/zfs_znode.h> 36789Sahrens #include <sys/zfs_dir.h> 37789Sahrens #include <sys/zil.h> 384620Sperrin #include <sys/zil_impl.h> 39789Sahrens #include <sys/byteorder.h> 40789Sahrens #include <sys/policy.h> 41789Sahrens #include <sys/stat.h> 42789Sahrens #include <sys/mode.h> 43789Sahrens #include <sys/acl.h> 44789Sahrens #include <sys/dmu.h> 45789Sahrens #include <sys/spa.h> 465331Samw #include <sys/zfs_fuid.h> 47789Sahrens #include <sys/ddi.h> 48*8227SNeil.Perrin@Sun.COM #include <sys/dsl_dataset.h> 49*8227SNeil.Perrin@Sun.COM 50*8227SNeil.Perrin@Sun.COM #define ZFS_HANDLE_REPLAY(zilog, tx) \ 51*8227SNeil.Perrin@Sun.COM if (zilog->zl_replay) { \ 52*8227SNeil.Perrin@Sun.COM dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); \ 53*8227SNeil.Perrin@Sun.COM zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] = \ 54*8227SNeil.Perrin@Sun.COM zilog->zl_replaying_seq; \ 55*8227SNeil.Perrin@Sun.COM return; \ 56*8227SNeil.Perrin@Sun.COM } 57789Sahrens 58789Sahrens /* 59*8227SNeil.Perrin@Sun.COM * These zfs_log_* functions must be called within a dmu tx, in one 60*8227SNeil.Perrin@Sun.COM * of 2 contexts depending on zilog->z_replay: 61*8227SNeil.Perrin@Sun.COM * 62*8227SNeil.Perrin@Sun.COM * Non replay mode 63*8227SNeil.Perrin@Sun.COM * --------------- 64*8227SNeil.Perrin@Sun.COM * We need to record the transaction so that if it is committed to 65*8227SNeil.Perrin@Sun.COM * the Intent Log then it can be replayed. An intent log transaction 66*8227SNeil.Perrin@Sun.COM * structure (itx_t) is allocated and all the information necessary to 67*8227SNeil.Perrin@Sun.COM * possibly replay the transaction is saved in it. The itx is then assigned 68*8227SNeil.Perrin@Sun.COM * a sequence number and inserted in the in-memory list anchored in the zilog. 69*8227SNeil.Perrin@Sun.COM * 70*8227SNeil.Perrin@Sun.COM * Replay mode 71*8227SNeil.Perrin@Sun.COM * ----------- 72*8227SNeil.Perrin@Sun.COM * We need to mark the intent log record as replayed in the log header. 73*8227SNeil.Perrin@Sun.COM * This is done in the same transaction as the replay so that they 74*8227SNeil.Perrin@Sun.COM * commit atomically. 75789Sahrens */ 76789Sahrens 775331Samw int 785331Samw zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap) 795331Samw { 805331Samw int isxvattr = (vap->va_mask & AT_XVATTR); 815331Samw switch (type) { 825331Samw case Z_FILE: 835331Samw if (vsecp == NULL && !isxvattr) 845331Samw return (TX_CREATE); 855331Samw if (vsecp && isxvattr) 865331Samw return (TX_CREATE_ACL_ATTR); 875331Samw if (vsecp) 885331Samw return (TX_CREATE_ACL); 895331Samw else 905331Samw return (TX_CREATE_ATTR); 915331Samw /*NOTREACHED*/ 925331Samw case Z_DIR: 935331Samw if (vsecp == NULL && !isxvattr) 945331Samw return (TX_MKDIR); 955331Samw if (vsecp && isxvattr) 965331Samw return (TX_MKDIR_ACL_ATTR); 975331Samw if (vsecp) 985331Samw return (TX_MKDIR_ACL); 995331Samw else 1005331Samw return (TX_MKDIR_ATTR); 1015331Samw case Z_XATTRDIR: 1025331Samw return (TX_MKXATTR); 1035331Samw } 1045331Samw ASSERT(0); 1055331Samw return (TX_MAX_TYPE); 1065331Samw } 1075331Samw 108789Sahrens /* 1095331Samw * build up the log data necessary for logging xvattr_t 1105331Samw * First lr_attr_t is initialized. following the lr_attr_t 1115331Samw * is the mapsize and attribute bitmap copied from the xvattr_t. 1125331Samw * Following the bitmap and bitmapsize two 64 bit words are reserved 1135331Samw * for the create time which may be set. Following the create time 1145331Samw * records a single 64 bit integer which has the bits to set on 1155331Samw * replay for the xvattr. 1165331Samw */ 1175331Samw static void 1185331Samw zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap) 1195331Samw { 1205331Samw uint32_t *bitmap; 1215331Samw uint64_t *attrs; 1225331Samw uint64_t *crtime; 1235331Samw xoptattr_t *xoap; 1245331Samw void *scanstamp; 1255331Samw int i; 1265331Samw 1275331Samw xoap = xva_getxoptattr(xvap); 1285331Samw ASSERT(xoap); 1295331Samw 1305331Samw lrattr->lr_attr_masksize = xvap->xva_mapsize; 1315331Samw bitmap = &lrattr->lr_attr_bitmap; 1325331Samw for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) { 1335331Samw *bitmap = xvap->xva_reqattrmap[i]; 1345331Samw } 1355331Samw 1365331Samw /* Now pack the attributes up in a single uint64_t */ 1375331Samw attrs = (uint64_t *)bitmap; 1385331Samw crtime = attrs + 1; 1395331Samw scanstamp = (caddr_t)(crtime + 2); 1405331Samw *attrs = 0; 1415331Samw if (XVA_ISSET_REQ(xvap, XAT_READONLY)) 1425331Samw *attrs |= (xoap->xoa_readonly == 0) ? 0 : 1435331Samw XAT0_READONLY; 1445331Samw if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) 1455331Samw *attrs |= (xoap->xoa_hidden == 0) ? 0 : 1465331Samw XAT0_HIDDEN; 1475331Samw if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) 1485331Samw *attrs |= (xoap->xoa_system == 0) ? 0 : 1495331Samw XAT0_SYSTEM; 1505331Samw if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) 1515331Samw *attrs |= (xoap->xoa_archive == 0) ? 0 : 1525331Samw XAT0_ARCHIVE; 1535331Samw if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) 1545331Samw *attrs |= (xoap->xoa_immutable == 0) ? 0 : 1555331Samw XAT0_IMMUTABLE; 1565331Samw if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) 1575331Samw *attrs |= (xoap->xoa_nounlink == 0) ? 0 : 1585331Samw XAT0_NOUNLINK; 1595331Samw if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) 1605331Samw *attrs |= (xoap->xoa_appendonly == 0) ? 0 : 1615331Samw XAT0_APPENDONLY; 1625331Samw if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) 1635331Samw *attrs |= (xoap->xoa_opaque == 0) ? 0 : 1645331Samw XAT0_APPENDONLY; 1655331Samw if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) 1665331Samw *attrs |= (xoap->xoa_nodump == 0) ? 0 : 1675331Samw XAT0_NODUMP; 1685331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) 1695331Samw *attrs |= (xoap->xoa_av_quarantined == 0) ? 0 : 1705331Samw XAT0_AV_QUARANTINED; 1715331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) 1725331Samw *attrs |= (xoap->xoa_av_modified == 0) ? 0 : 1735331Samw XAT0_AV_MODIFIED; 1745331Samw if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) 1755331Samw ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime); 1765331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 1775331Samw bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ); 1785331Samw } 1795331Samw 1805331Samw static void * 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 * 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 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 uint64_t seq; 240789Sahrens lr_create_t *lr; 2415331Samw lr_acl_create_t *lracl; 2425331Samw size_t aclsize; 2435331Samw size_t xvatsize = 0; 2445331Samw size_t txsize; 2455331Samw xvattr_t *xvap = (xvattr_t *)vap; 2465331Samw void *end; 2475331Samw size_t lrsize; 248789Sahrens size_t namesize = strlen(name) + 1; 2495331Samw size_t fuidsz = 0; 250789Sahrens 251789Sahrens if (zilog == NULL) 2522638Sperrin return; 253789Sahrens 254*8227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 255*8227SNeil.Perrin@Sun.COM 2565331Samw /* 2575331Samw * If we have FUIDs present then add in space for 2585331Samw * domains and ACE fuid's if any. 2595331Samw */ 2605331Samw if (fuidp) { 2615331Samw fuidsz += fuidp->z_domain_str_sz; 2625331Samw fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t); 2635331Samw } 2645331Samw 2655331Samw if (vap->va_mask & AT_XVATTR) 2665331Samw xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize); 2675331Samw 2685331Samw if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR || 2695331Samw (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR || 2705331Samw (int)txtype == TX_MKXATTR) { 2715331Samw txsize = sizeof (*lr) + namesize + fuidsz + xvatsize; 2725331Samw lrsize = sizeof (*lr); 2735331Samw } else { 2745331Samw aclsize = (vsecp) ? vsecp->vsa_aclentsz : 0; 2755331Samw txsize = 2765331Samw sizeof (lr_acl_create_t) + namesize + fuidsz + 2775435Smarks ZIL_ACE_LENGTH(aclsize) + xvatsize; 2785331Samw lrsize = sizeof (lr_acl_create_t); 2795331Samw } 2805331Samw 2815331Samw itx = zil_itx_create(txtype, txsize); 2825331Samw 283789Sahrens lr = (lr_create_t *)&itx->itx_lr; 284789Sahrens lr->lr_doid = dzp->z_id; 285789Sahrens lr->lr_foid = zp->z_id; 286789Sahrens lr->lr_mode = zp->z_phys->zp_mode; 2875331Samw if (!IS_EPHEMERAL(zp->z_phys->zp_uid)) { 2885331Samw lr->lr_uid = (uint64_t)zp->z_phys->zp_uid; 2895331Samw } else { 2905331Samw lr->lr_uid = fuidp->z_fuid_owner; 2915331Samw } 2925331Samw if (!IS_EPHEMERAL(zp->z_phys->zp_gid)) { 2935331Samw lr->lr_gid = (uint64_t)zp->z_phys->zp_gid; 2945331Samw } else { 2955331Samw lr->lr_gid = fuidp->z_fuid_group; 2965331Samw } 297789Sahrens lr->lr_gen = zp->z_phys->zp_gen; 298789Sahrens lr->lr_crtime[0] = zp->z_phys->zp_crtime[0]; 299789Sahrens lr->lr_crtime[1] = zp->z_phys->zp_crtime[1]; 300789Sahrens lr->lr_rdev = zp->z_phys->zp_rdev; 3015331Samw 3025331Samw /* 3035331Samw * Fill in xvattr info if any 3045331Samw */ 3055331Samw if (vap->va_mask & AT_XVATTR) { 3065331Samw zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap); 3075331Samw end = (caddr_t)lr + lrsize + xvatsize; 3085331Samw } else { 3095331Samw end = (caddr_t)lr + lrsize; 3105331Samw } 3115331Samw 3125331Samw /* Now fill in any ACL info */ 3135331Samw 3145331Samw if (vsecp) { 3155331Samw lracl = (lr_acl_create_t *)&itx->itx_lr; 3165331Samw lracl->lr_aclcnt = vsecp->vsa_aclcnt; 3175331Samw lracl->lr_acl_bytes = aclsize; 3185331Samw lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0; 3195331Samw lracl->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0; 3205331Samw if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS) 3215331Samw lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags; 3225331Samw else 3235331Samw lracl->lr_acl_flags = 0; 3245331Samw 3255331Samw bcopy(vsecp->vsa_aclentp, end, aclsize); 3265435Smarks end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize); 3275331Samw } 3285331Samw 3295331Samw /* drop in FUID info */ 3305331Samw if (fuidp) { 3315331Samw end = zfs_log_fuid_ids(fuidp, end); 3325331Samw end = zfs_log_fuid_domains(fuidp, end); 3335331Samw } 3345331Samw /* 3355331Samw * Now place file name in log record 3365331Samw */ 3375331Samw bcopy(name, end, namesize); 338789Sahrens 339789Sahrens seq = zil_itx_assign(zilog, itx, tx); 340789Sahrens dzp->z_last_itx = seq; 341789Sahrens zp->z_last_itx = seq; 342789Sahrens } 343789Sahrens 344789Sahrens /* 345789Sahrens * zfs_log_remove() handles both TX_REMOVE and TX_RMDIR transactions. 346789Sahrens */ 3472638Sperrin void 3485331Samw zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 349789Sahrens znode_t *dzp, char *name) 350789Sahrens { 351789Sahrens itx_t *itx; 352789Sahrens uint64_t seq; 353789Sahrens lr_remove_t *lr; 354789Sahrens size_t namesize = strlen(name) + 1; 355789Sahrens 356789Sahrens if (zilog == NULL) 3572638Sperrin return; 358789Sahrens 359*8227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 360*8227SNeil.Perrin@Sun.COM 361789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + namesize); 362789Sahrens lr = (lr_remove_t *)&itx->itx_lr; 363789Sahrens lr->lr_doid = dzp->z_id; 364789Sahrens bcopy(name, (char *)(lr + 1), namesize); 365789Sahrens 366789Sahrens seq = zil_itx_assign(zilog, itx, tx); 367789Sahrens dzp->z_last_itx = seq; 368789Sahrens } 369789Sahrens 370789Sahrens /* 371789Sahrens * zfs_log_link() handles TX_LINK transactions. 372789Sahrens */ 3732638Sperrin void 3745331Samw zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 375789Sahrens znode_t *dzp, znode_t *zp, char *name) 376789Sahrens { 377789Sahrens itx_t *itx; 378789Sahrens uint64_t seq; 379789Sahrens lr_link_t *lr; 380789Sahrens size_t namesize = strlen(name) + 1; 381789Sahrens 382789Sahrens if (zilog == NULL) 3832638Sperrin return; 384789Sahrens 385*8227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 386*8227SNeil.Perrin@Sun.COM 387789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + namesize); 388789Sahrens lr = (lr_link_t *)&itx->itx_lr; 389789Sahrens lr->lr_doid = dzp->z_id; 390789Sahrens lr->lr_link_obj = zp->z_id; 391789Sahrens bcopy(name, (char *)(lr + 1), namesize); 392789Sahrens 393789Sahrens seq = zil_itx_assign(zilog, itx, tx); 394789Sahrens dzp->z_last_itx = seq; 395789Sahrens zp->z_last_itx = seq; 396789Sahrens } 397789Sahrens 398789Sahrens /* 399789Sahrens * zfs_log_symlink() handles TX_SYMLINK transactions. 400789Sahrens */ 4012638Sperrin void 4025331Samw zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 4035331Samw znode_t *dzp, znode_t *zp, char *name, char *link) 404789Sahrens { 405789Sahrens itx_t *itx; 406789Sahrens uint64_t seq; 407789Sahrens lr_create_t *lr; 408789Sahrens size_t namesize = strlen(name) + 1; 409789Sahrens size_t linksize = strlen(link) + 1; 410789Sahrens 411789Sahrens if (zilog == NULL) 4122638Sperrin return; 413789Sahrens 414*8227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 415*8227SNeil.Perrin@Sun.COM 416789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize); 417789Sahrens lr = (lr_create_t *)&itx->itx_lr; 418789Sahrens lr->lr_doid = dzp->z_id; 419789Sahrens lr->lr_foid = zp->z_id; 420789Sahrens lr->lr_mode = zp->z_phys->zp_mode; 421789Sahrens lr->lr_uid = zp->z_phys->zp_uid; 422789Sahrens lr->lr_gid = zp->z_phys->zp_gid; 423789Sahrens lr->lr_gen = zp->z_phys->zp_gen; 424789Sahrens lr->lr_crtime[0] = zp->z_phys->zp_crtime[0]; 425789Sahrens lr->lr_crtime[1] = zp->z_phys->zp_crtime[1]; 426789Sahrens bcopy(name, (char *)(lr + 1), namesize); 427789Sahrens bcopy(link, (char *)(lr + 1) + namesize, linksize); 428789Sahrens 429789Sahrens seq = zil_itx_assign(zilog, itx, tx); 430789Sahrens dzp->z_last_itx = seq; 431789Sahrens zp->z_last_itx = seq; 432789Sahrens } 433789Sahrens 434789Sahrens /* 435789Sahrens * zfs_log_rename() handles TX_RENAME transactions. 436789Sahrens */ 4372638Sperrin void 4385331Samw zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, 439789Sahrens znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp) 440789Sahrens { 441789Sahrens itx_t *itx; 442789Sahrens uint64_t seq; 443789Sahrens lr_rename_t *lr; 444789Sahrens size_t snamesize = strlen(sname) + 1; 445789Sahrens size_t dnamesize = strlen(dname) + 1; 446789Sahrens 447789Sahrens if (zilog == NULL) 4482638Sperrin return; 449789Sahrens 450*8227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 451*8227SNeil.Perrin@Sun.COM 452789Sahrens itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize); 453789Sahrens lr = (lr_rename_t *)&itx->itx_lr; 454789Sahrens lr->lr_sdoid = sdzp->z_id; 455789Sahrens lr->lr_tdoid = tdzp->z_id; 456789Sahrens bcopy(sname, (char *)(lr + 1), snamesize); 457789Sahrens bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize); 458789Sahrens 459789Sahrens seq = zil_itx_assign(zilog, itx, tx); 460789Sahrens sdzp->z_last_itx = seq; 461789Sahrens tdzp->z_last_itx = seq; 462789Sahrens szp->z_last_itx = seq; 463789Sahrens } 464789Sahrens 465789Sahrens /* 466789Sahrens * zfs_log_write() handles TX_WRITE transactions. 467789Sahrens */ 4682237Smaybee ssize_t zfs_immediate_write_sz = 32768; 469789Sahrens 4704620Sperrin #define ZIL_MAX_LOG_DATA (SPA_MAXBLOCKSIZE - sizeof (zil_trailer_t) - \ 4714620Sperrin sizeof (lr_write_t)) 4724620Sperrin 4732638Sperrin void 474789Sahrens zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype, 4754620Sperrin znode_t *zp, offset_t off, ssize_t resid, int ioflag) 476789Sahrens { 4771669Sperrin itx_wr_state_t write_state; 4784620Sperrin boolean_t slogging; 4794720Sfr157268 uintptr_t fsync_cnt; 480789Sahrens 4813461Sahrens if (zilog == NULL || zp->z_unlinked) 4822638Sperrin return; 483789Sahrens 484*8227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 485*8227SNeil.Perrin@Sun.COM 4861669Sperrin /* 4871669Sperrin * Writes are handled in three different ways: 4881669Sperrin * 4891669Sperrin * WR_INDIRECT: 4907360SNeil.Perrin@Sun.COM * In this mode, if we need to commit the write later, then the block 4917360SNeil.Perrin@Sun.COM * is immediately written into the file system (using dmu_sync), 4927360SNeil.Perrin@Sun.COM * and a pointer to the block is put into the log record. 4937360SNeil.Perrin@Sun.COM * When the txg commits the block is linked in. 4947360SNeil.Perrin@Sun.COM * This saves additionally writing the data into the log record. 4957360SNeil.Perrin@Sun.COM * There are a few requirements for this to occur: 4967360SNeil.Perrin@Sun.COM * - write is greater than zfs_immediate_write_sz 4977360SNeil.Perrin@Sun.COM * - not using slogs (as slogs are assumed to always be faster 4987360SNeil.Perrin@Sun.COM * than writing into the main pool) 4997360SNeil.Perrin@Sun.COM * - the write occupies only one block 5001669Sperrin * WR_COPIED: 5011669Sperrin * If we know we'll immediately be committing the 5026396Sperrin * transaction (FSYNC or FDSYNC), the we allocate a larger 5031669Sperrin * log record here for the data and copy the data in. 5041669Sperrin * WR_NEED_COPY: 5051669Sperrin * Otherwise we don't allocate a buffer, and *if* we need to 5061669Sperrin * flush the write later then a buffer is allocated and 5071669Sperrin * we retrieve the data using the dmu. 5081669Sperrin */ 5094620Sperrin slogging = spa_has_slogs(zilog->zl_spa); 5107360SNeil.Perrin@Sun.COM if (resid > zfs_immediate_write_sz && !slogging && resid <= zp->z_blksz) 5111669Sperrin write_state = WR_INDIRECT; 5126396Sperrin else if (ioflag & (FSYNC | FDSYNC)) 5131669Sperrin write_state = WR_COPIED; 5143638Sbillm else 5151669Sperrin write_state = WR_NEED_COPY; 5163638Sbillm 5174720Sfr157268 if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) { 5184720Sfr157268 (void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1)); 5194720Sfr157268 } 5204720Sfr157268 5214620Sperrin while (resid) { 5224620Sperrin itx_t *itx; 5234620Sperrin lr_write_t *lr; 5244620Sperrin ssize_t len; 5254620Sperrin 5264620Sperrin /* 5277360SNeil.Perrin@Sun.COM * If the write would overflow the largest block then split it. 5284620Sperrin */ 5297360SNeil.Perrin@Sun.COM if (write_state != WR_INDIRECT && resid > ZIL_MAX_LOG_DATA) 5304620Sperrin len = SPA_MAXBLOCKSIZE >> 1; 5314620Sperrin else 5324620Sperrin len = resid; 5334620Sperrin 5344620Sperrin itx = zil_itx_create(txtype, sizeof (*lr) + 5354620Sperrin (write_state == WR_COPIED ? len : 0)); 5364620Sperrin lr = (lr_write_t *)&itx->itx_lr; 5374620Sperrin if (write_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os, 5384620Sperrin zp->z_id, off, len, lr + 1) != 0) { 5393638Sbillm kmem_free(itx, offsetof(itx_t, itx_lr) + 5403638Sbillm itx->itx_lr.lrc_reclen); 5411669Sperrin itx = zil_itx_create(txtype, sizeof (*lr)); 5423638Sbillm lr = (lr_write_t *)&itx->itx_lr; 5431669Sperrin write_state = WR_NEED_COPY; 5441669Sperrin } 5454620Sperrin 5464620Sperrin itx->itx_wr_state = write_state; 5476101Sperrin if (write_state == WR_NEED_COPY) 5486101Sperrin itx->itx_sod += len; 5494620Sperrin lr->lr_foid = zp->z_id; 5504620Sperrin lr->lr_offset = off; 5514620Sperrin lr->lr_length = len; 5524620Sperrin lr->lr_blkoff = 0; 5534620Sperrin BP_ZERO(&lr->lr_blkptr); 5543638Sbillm 5554620Sperrin itx->itx_private = zp->z_zfsvfs; 556789Sahrens 5576396Sperrin if ((zp->z_sync_cnt != 0) || (fsync_cnt != 0) || 5586396Sperrin (ioflag & (FSYNC | FDSYNC))) 5594720Sfr157268 itx->itx_sync = B_TRUE; 5604720Sfr157268 else 5614720Sfr157268 itx->itx_sync = B_FALSE; 5624720Sfr157268 5634620Sperrin zp->z_last_itx = zil_itx_assign(zilog, itx, tx); 564789Sahrens 5654620Sperrin off += len; 5664620Sperrin resid -= len; 5674620Sperrin } 568789Sahrens } 569789Sahrens 570789Sahrens /* 571789Sahrens * zfs_log_truncate() handles TX_TRUNCATE transactions. 572789Sahrens */ 5732638Sperrin void 574789Sahrens zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype, 575789Sahrens znode_t *zp, uint64_t off, uint64_t len) 576789Sahrens { 577789Sahrens itx_t *itx; 578789Sahrens uint64_t seq; 579789Sahrens lr_truncate_t *lr; 580789Sahrens 5813461Sahrens if (zilog == NULL || zp->z_unlinked) 5822638Sperrin return; 583789Sahrens 584*8227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 585*8227SNeil.Perrin@Sun.COM 586789Sahrens itx = zil_itx_create(txtype, sizeof (*lr)); 587789Sahrens lr = (lr_truncate_t *)&itx->itx_lr; 588789Sahrens lr->lr_foid = zp->z_id; 589789Sahrens lr->lr_offset = off; 590789Sahrens lr->lr_length = len; 591789Sahrens 5923063Sperrin itx->itx_sync = (zp->z_sync_cnt != 0); 593789Sahrens seq = zil_itx_assign(zilog, itx, tx); 594789Sahrens zp->z_last_itx = seq; 595789Sahrens } 596789Sahrens 597789Sahrens /* 598789Sahrens * zfs_log_setattr() handles TX_SETATTR transactions. 599789Sahrens */ 6002638Sperrin void 601789Sahrens zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype, 6025331Samw znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp) 603789Sahrens { 6045331Samw itx_t *itx; 6055331Samw uint64_t seq; 6065331Samw lr_setattr_t *lr; 6075331Samw xvattr_t *xvap = (xvattr_t *)vap; 6085331Samw size_t recsize = sizeof (lr_setattr_t); 6095331Samw void *start; 6105331Samw 611789Sahrens 6123461Sahrens if (zilog == NULL || zp->z_unlinked) 6132638Sperrin return; 614789Sahrens 615*8227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 616*8227SNeil.Perrin@Sun.COM 6175331Samw /* 6185331Samw * If XVATTR set, then log record size needs to allow 6195331Samw * for lr_attr_t + xvattr mask, mapsize and create time 6205331Samw * plus actual attribute values 6215331Samw */ 6225331Samw if (vap->va_mask & AT_XVATTR) 6235331Samw recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize); 6245331Samw 6255331Samw if (fuidp) 6265331Samw recsize += fuidp->z_domain_str_sz; 6275331Samw 6285331Samw itx = zil_itx_create(txtype, recsize); 629789Sahrens lr = (lr_setattr_t *)&itx->itx_lr; 630789Sahrens lr->lr_foid = zp->z_id; 631789Sahrens lr->lr_mask = (uint64_t)mask_applied; 632789Sahrens lr->lr_mode = (uint64_t)vap->va_mode; 6335331Samw if ((mask_applied & AT_UID) && IS_EPHEMERAL(vap->va_uid)) 6345331Samw lr->lr_uid = fuidp->z_fuid_owner; 6355331Samw else 6365331Samw lr->lr_uid = (uint64_t)vap->va_uid; 6375331Samw 6385331Samw if ((mask_applied & AT_GID) && IS_EPHEMERAL(vap->va_gid)) 6395331Samw lr->lr_gid = fuidp->z_fuid_group; 6405331Samw else 6415331Samw lr->lr_gid = (uint64_t)vap->va_gid; 6425331Samw 643789Sahrens lr->lr_size = (uint64_t)vap->va_size; 644789Sahrens ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime); 645789Sahrens ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime); 6465331Samw start = (lr_setattr_t *)(lr + 1); 6475331Samw if (vap->va_mask & AT_XVATTR) { 6485331Samw zfs_log_xvattr((lr_attr_t *)start, xvap); 6495331Samw start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize); 6505331Samw } 6515331Samw 6525331Samw /* 6535331Samw * Now stick on domain information if any on end 6545331Samw */ 6555331Samw 6565331Samw if (fuidp) 6575331Samw (void) zfs_log_fuid_domains(fuidp, start); 658789Sahrens 6593063Sperrin itx->itx_sync = (zp->z_sync_cnt != 0); 660789Sahrens seq = zil_itx_assign(zilog, itx, tx); 661789Sahrens zp->z_last_itx = seq; 662789Sahrens } 663789Sahrens 664789Sahrens /* 665789Sahrens * zfs_log_acl() handles TX_ACL transactions. 666789Sahrens */ 6672638Sperrin void 6685331Samw zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp, 6695331Samw vsecattr_t *vsecp, zfs_fuid_info_t *fuidp) 670789Sahrens { 671789Sahrens itx_t *itx; 672789Sahrens uint64_t seq; 6735331Samw lr_acl_v0_t *lrv0; 674789Sahrens lr_acl_t *lr; 6755331Samw int txtype; 6765331Samw int lrsize; 6775331Samw size_t txsize; 6785331Samw size_t aclbytes = vsecp->vsa_aclentsz; 6795331Samw 6806514Smarks if (zilog == NULL || zp->z_unlinked) 6816514Smarks return; 6826514Smarks 683*8227SNeil.Perrin@Sun.COM ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */ 684*8227SNeil.Perrin@Sun.COM 6856514Smarks txtype = (zp->z_zfsvfs->z_version < ZPL_VERSION_FUID) ? 6865331Samw TX_ACL_V0 : TX_ACL; 6875331Samw 6885331Samw if (txtype == TX_ACL) 6895331Samw lrsize = sizeof (*lr); 6905331Samw else 6915331Samw lrsize = sizeof (*lrv0); 692789Sahrens 6935435Smarks txsize = lrsize + 6945435Smarks ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) + 6955435Smarks (fuidp ? fuidp->z_domain_str_sz : 0) + 6966514Smarks sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0); 6975331Samw 6985331Samw itx = zil_itx_create(txtype, txsize); 6995331Samw 700789Sahrens lr = (lr_acl_t *)&itx->itx_lr; 701789Sahrens lr->lr_foid = zp->z_id; 7025331Samw if (txtype == TX_ACL) { 7035331Samw lr->lr_acl_bytes = aclbytes; 7045331Samw lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0; 7055331Samw lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0; 7065331Samw if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS) 7075331Samw lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags; 7085331Samw else 7095331Samw lr->lr_acl_flags = 0; 7105331Samw } 7115331Samw lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt; 7125331Samw 7135331Samw if (txtype == TX_ACL_V0) { 7145331Samw lrv0 = (lr_acl_v0_t *)lr; 7155331Samw bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes); 7165331Samw } else { 7175331Samw void *start = (ace_t *)(lr + 1); 7185331Samw 7195331Samw bcopy(vsecp->vsa_aclentp, start, aclbytes); 7205331Samw 7215435Smarks start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes); 7225331Samw 7235331Samw if (fuidp) { 7245331Samw start = zfs_log_fuid_ids(fuidp, start); 7255331Samw (void) zfs_log_fuid_domains(fuidp, start); 7265331Samw } 7275331Samw } 728789Sahrens 7293063Sperrin itx->itx_sync = (zp->z_sync_cnt != 0); 730789Sahrens seq = zil_itx_assign(zilog, itx, tx); 731789Sahrens zp->z_last_itx = seq; 732789Sahrens } 733