FreeBSD ZFS
The Zettabyte File System

zfs_sa.c

Go to the documentation of this file.
00001 /*
00002  * CDDL HEADER START
00003  *
00004  * The contents of this file are subject to the terms of the
00005  * Common Development and Distribution License (the "License").
00006  * You may not use this file except in compliance with the License.
00007  *
00008  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
00009  * or http://www.opensolaris.org/os/licensing.
00010  * See the License for the specific language governing permissions
00011  * and limitations under the License.
00012  *
00013  * When distributing Covered Code, include this CDDL HEADER in each
00014  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
00015  * If applicable, add the following below this CDDL HEADER, with the
00016  * fields enclosed by brackets "[]" replaced with your own identifying
00017  * information: Portions Copyright [yyyy] [name of copyright owner]
00018  *
00019  * CDDL HEADER END
00020  */
00021 /*
00022  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
00023  */
00024 
00025 #include <sys/types.h>
00026 #include <sys/param.h>
00027 #include <sys/vnode.h>
00028 #include <sys/sa.h>
00029 #include <sys/zfs_acl.h>
00030 #include <sys/zfs_sa.h>
00031 
00045 sa_attr_reg_t zfs_attr_table[ZPL_END+1] = {
00046         {"ZPL_ATIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 0},
00047         {"ZPL_MTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 1},
00048         {"ZPL_CTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 2},
00049         {"ZPL_CRTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 3},
00050         {"ZPL_GEN", sizeof (uint64_t), SA_UINT64_ARRAY, 4},
00051         {"ZPL_MODE", sizeof (uint64_t), SA_UINT64_ARRAY, 5},
00052         {"ZPL_SIZE", sizeof (uint64_t), SA_UINT64_ARRAY, 6},
00053         {"ZPL_PARENT", sizeof (uint64_t), SA_UINT64_ARRAY, 7},
00054         {"ZPL_LINKS", sizeof (uint64_t), SA_UINT64_ARRAY, 8},
00055         {"ZPL_XATTR", sizeof (uint64_t), SA_UINT64_ARRAY, 9},
00056         {"ZPL_RDEV", sizeof (uint64_t), SA_UINT64_ARRAY, 10},
00057         {"ZPL_FLAGS", sizeof (uint64_t), SA_UINT64_ARRAY, 11},
00058         {"ZPL_UID", sizeof (uint64_t), SA_UINT64_ARRAY, 12},
00059         {"ZPL_GID", sizeof (uint64_t), SA_UINT64_ARRAY, 13},
00060         {"ZPL_PAD", sizeof (uint64_t) * 4, SA_UINT64_ARRAY, 14},
00061         {"ZPL_ZNODE_ACL", 88, SA_UINT8_ARRAY, 15},
00062         {"ZPL_DACL_COUNT", sizeof (uint64_t), SA_UINT64_ARRAY, 0},
00063         {"ZPL_SYMLINK", 0, SA_UINT8_ARRAY, 0},
00064         {"ZPL_SCANSTAMP", 32, SA_UINT8_ARRAY, 0},
00065         {"ZPL_DACL_ACES", 0, SA_ACL, 0},
00066         {NULL, 0, 0, 0}
00067 };
00068 
00069 #ifdef _KERNEL
00070 
00071 int
00072 zfs_sa_readlink(znode_t *zp, uio_t *uio)
00073 {
00074         dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
00075         size_t bufsz;
00076         int error;
00077 
00078         bufsz = zp->z_size;
00079         if (bufsz + ZFS_OLD_ZNODE_PHYS_SIZE <= db->db_size) {
00080                 error = uiomove((caddr_t)db->db_data +
00081                     ZFS_OLD_ZNODE_PHYS_SIZE,
00082                     MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
00083         } else {
00084                 dmu_buf_t *dbp;
00085                 if ((error = dmu_buf_hold(zp->z_zfsvfs->z_os, zp->z_id,
00086                     0, FTAG, &dbp, DMU_READ_NO_PREFETCH)) == 0) {
00087                         error = uiomove(dbp->db_data,
00088                             MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
00089                         dmu_buf_rele(dbp, FTAG);
00090                 }
00091         }
00092         return (error);
00093 }
00094 
00095 void
00096 zfs_sa_symlink(znode_t *zp, char *link, int len, dmu_tx_t *tx)
00097 {
00098         dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
00099 
00100         if (ZFS_OLD_ZNODE_PHYS_SIZE + len <= dmu_bonus_max()) {
00101                 VERIFY(dmu_set_bonus(db,
00102                     len + ZFS_OLD_ZNODE_PHYS_SIZE, tx) == 0);
00103                 if (len) {
00104                         bcopy(link, (caddr_t)db->db_data +
00105                             ZFS_OLD_ZNODE_PHYS_SIZE, len);
00106                 }
00107         } else {
00108                 dmu_buf_t *dbp;
00109 
00110                 zfs_grow_blocksize(zp, len, tx);
00111                 VERIFY(0 == dmu_buf_hold(zp->z_zfsvfs->z_os,
00112                     zp->z_id, 0, FTAG, &dbp, DMU_READ_NO_PREFETCH));
00113 
00114                 dmu_buf_will_dirty(dbp, tx);
00115 
00116                 ASSERT3U(len, <=, dbp->db_size);
00117                 bcopy(link, dbp->db_data, len);
00118                 dmu_buf_rele(dbp, FTAG);
00119         }
00120 }
00121 
00122 void
00123 zfs_sa_get_scanstamp(znode_t *zp, xvattr_t *xvap)
00124 {
00125         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
00126         xoptattr_t *xoap;
00127 
00128         ASSERT(MUTEX_HELD(&zp->z_lock));
00129         VERIFY((xoap = xva_getxoptattr(xvap)) != NULL);
00130         if (zp->z_is_sa) {
00131                 if (sa_lookup(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zfsvfs),
00132                     &xoap->xoa_av_scanstamp,
00133                     sizeof (xoap->xoa_av_scanstamp)) != 0)
00134                         return;
00135         } else {
00136                 dmu_object_info_t doi;
00137                 dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
00138                 int len;
00139 
00140                 if (!(zp->z_pflags & ZFS_BONUS_SCANSTAMP))
00141                         return;
00142 
00143                 sa_object_info(zp->z_sa_hdl, &doi);
00144                 len = sizeof (xoap->xoa_av_scanstamp) +
00145                     ZFS_OLD_ZNODE_PHYS_SIZE;
00146 
00147                 if (len <= doi.doi_bonus_size) {
00148                         (void) memcpy(xoap->xoa_av_scanstamp,
00149                             (caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
00150                             sizeof (xoap->xoa_av_scanstamp));
00151                 }
00152         }
00153         XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
00154 }
00155 
00156 void
00157 zfs_sa_set_scanstamp(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
00158 {
00159         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
00160         xoptattr_t *xoap;
00161 
00162         ASSERT(MUTEX_HELD(&zp->z_lock));
00163         VERIFY((xoap = xva_getxoptattr(xvap)) != NULL);
00164         if (zp->z_is_sa)
00165                 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zfsvfs),
00166                     &xoap->xoa_av_scanstamp,
00167                     sizeof (xoap->xoa_av_scanstamp), tx));
00168         else {
00169                 dmu_object_info_t doi;
00170                 dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
00171                 int len;
00172 
00173                 sa_object_info(zp->z_sa_hdl, &doi);
00174                 len = sizeof (xoap->xoa_av_scanstamp) +
00175                     ZFS_OLD_ZNODE_PHYS_SIZE;
00176                 if (len > doi.doi_bonus_size)
00177                         VERIFY(dmu_set_bonus(db, len, tx) == 0);
00178                 (void) memcpy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
00179                     xoap->xoa_av_scanstamp, sizeof (xoap->xoa_av_scanstamp));
00180 
00181                 zp->z_pflags |= ZFS_BONUS_SCANSTAMP;
00182                 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
00183                     &zp->z_pflags, sizeof (uint64_t), tx));
00184         }
00185 }
00186 
00195 void
00196 zfs_sa_upgrade(sa_handle_t *hdl, dmu_tx_t *tx)
00197 {
00198         dmu_buf_t *db = sa_get_db(hdl);
00199         znode_t *zp = sa_get_userdata(hdl);
00200         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
00201         sa_bulk_attr_t bulk[20];
00202         int count = 0;
00203         sa_bulk_attr_t sa_attrs[20] = { 0 };
00204         zfs_acl_locator_cb_t locate = { 0 };
00205         uint64_t uid, gid, mode, rdev, xattr, parent;
00206         uint64_t crtime[2], mtime[2], ctime[2];
00207         zfs_acl_phys_t znode_acl;
00208         char scanstamp[AV_SCANSTAMP_SZ];
00209         boolean_t drop_lock = B_FALSE;
00210 
00211         /*
00212          * No upgrade if ACL isn't cached
00213          * since we won't know which locks are held
00214          * and ready the ACL would require special "locked"
00215          * interfaces that would be messy
00216          */
00217         if (zp->z_acl_cached == NULL || ZTOV(zp)->v_type == VLNK)
00218                 return;
00219 
00220         /*
00221          * If the z_lock is held and we aren't the owner
00222          * the just return since we don't want to deadlock
00223          * trying to update the status of z_is_sa.  This
00224          * file can then be upgraded at a later time.
00225          *
00226          * Otherwise, we know we are doing the
00227          * sa_update() that caused us to enter this function.
00228          */
00229         if (mutex_owner(&zp->z_lock) != curthread) {
00230                 if (mutex_tryenter(&zp->z_lock) == 0)
00231                         return;
00232                 else
00233                         drop_lock = B_TRUE;
00234         }
00235 
00236         /* First do a bulk query of the attributes that aren't cached */
00237         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
00238         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
00239         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
00240         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
00241         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
00242         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_XATTR(zfsvfs), NULL, &xattr, 8);
00243         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL, &rdev, 8);
00244         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8);
00245         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8);
00246         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
00247             &znode_acl, 88);
00248 
00249         if (sa_bulk_lookup_locked(hdl, bulk, count) != 0)
00250                 goto done;
00251 
00252 
00253         /*
00254          * While the order here doesn't matter its best to try and organize
00255          * it is such a way to pick up an already existing layout number
00256          */
00257         count = 0;
00258         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
00259         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SIZE(zfsvfs), NULL,
00260             &zp->z_size, 8);
00261         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GEN(zfsvfs),
00262             NULL, &zp->z_gen, 8);
00263         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8);
00264         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8);
00265         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_PARENT(zfsvfs),
00266             NULL, &parent, 8);
00267         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_FLAGS(zfsvfs), NULL,
00268             &zp->z_pflags, 8);
00269         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_ATIME(zfsvfs), NULL,
00270             zp->z_atime, 16);
00271         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MTIME(zfsvfs), NULL,
00272             &mtime, 16);
00273         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CTIME(zfsvfs), NULL,
00274             &ctime, 16);
00275         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CRTIME(zfsvfs), NULL,
00276             &crtime, 16);
00277         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_LINKS(zfsvfs), NULL,
00278             &zp->z_links, 8);
00279         if (zp->z_vnode->v_type == VBLK || zp->z_vnode->v_type == VCHR)
00280                 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_RDEV(zfsvfs), NULL,
00281                     &rdev, 8);
00282         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
00283             &zp->z_acl_cached->z_acl_count, 8);
00284 
00285         if (zp->z_acl_cached->z_version < ZFS_ACL_VERSION_FUID)
00286                 zfs_acl_xform(zp, zp->z_acl_cached, CRED());
00287 
00288         locate.cb_aclp = zp->z_acl_cached;
00289         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_ACES(zfsvfs),
00290             zfs_acl_data_locator, &locate, zp->z_acl_cached->z_acl_bytes);
00291 
00292         if (xattr)
00293                 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_XATTR(zfsvfs),
00294                     NULL, &xattr, 8);
00295 
00296         /* if scanstamp then add scanstamp */
00297 
00298         if (zp->z_pflags & ZFS_BONUS_SCANSTAMP) {
00299                 bcopy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
00300                     scanstamp, AV_SCANSTAMP_SZ);
00301                 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SCANSTAMP(zfsvfs),
00302                     NULL, scanstamp, AV_SCANSTAMP_SZ);
00303                 zp->z_pflags &= ~ZFS_BONUS_SCANSTAMP;
00304         }
00305 
00306         VERIFY(dmu_set_bonustype(db, DMU_OT_SA, tx) == 0);
00307         VERIFY(sa_replace_all_by_template_locked(hdl, sa_attrs,
00308             count, tx) == 0);
00309         if (znode_acl.z_acl_extern_obj)
00310                 VERIFY(0 == dmu_object_free(zfsvfs->z_os,
00311                     znode_acl.z_acl_extern_obj, tx));
00312 
00313         zp->z_is_sa = B_TRUE;
00314 done:
00315         if (drop_lock)
00316                 mutex_exit(&zp->z_lock);
00317 }
00318 
00319 void
00320 zfs_sa_upgrade_txholds(dmu_tx_t *tx, znode_t *zp)
00321 {
00322         if (!zp->z_zfsvfs->z_use_sa || zp->z_is_sa)
00323                 return;
00324 
00325 
00326         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
00327 
00328         if (zfs_external_acl(zp)) {
00329                 dmu_tx_hold_free(tx, zfs_external_acl(zp), 0,
00330                     DMU_OBJECT_END);
00331         }
00332 }
00333 
00334 #endif
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines