1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/types.h> 27 #include <sys/param.h> 28 #include <sys/vnode.h> 29 #include <sys/sa.h> 30 #include <sys/zfs_acl.h> 31 #include <sys/zfs_sa.h> 32 33 /* 34 * ZPL attribute registration table. 35 * Order of attributes doesn't matter 36 * a unique value will be assigned for each 37 * attribute that is file system specific 38 * 39 * This is just the set of ZPL attributes that this 40 * version of ZFS deals with natively. The file system 41 * could have other attributes stored in files, but they will be 42 * ignored. The SA framework will preserve them, just that 43 * this version of ZFS won't change or delete them. 44 */ 45 46 sa_attr_reg_t zfs_attr_table[ZPL_END+1] = { 47 {"ZPL_ATIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 0}, 48 {"ZPL_MTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 1}, 49 {"ZPL_CTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 2}, 50 {"ZPL_CRTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 3}, 51 {"ZPL_GEN", sizeof (uint64_t), SA_UINT64_ARRAY, 4}, 52 {"ZPL_MODE", sizeof (uint64_t), SA_UINT64_ARRAY, 5}, 53 {"ZPL_SIZE", sizeof (uint64_t), SA_UINT64_ARRAY, 6}, 54 {"ZPL_PARENT", sizeof (uint64_t), SA_UINT64_ARRAY, 7}, 55 {"ZPL_LINKS", sizeof (uint64_t), SA_UINT64_ARRAY, 8}, 56 {"ZPL_XATTR", sizeof (uint64_t), SA_UINT64_ARRAY, 9}, 57 {"ZPL_RDEV", sizeof (uint64_t), SA_UINT64_ARRAY, 10}, 58 {"ZPL_FLAGS", sizeof (uint64_t), SA_UINT64_ARRAY, 11}, 59 {"ZPL_UID", sizeof (uint64_t), SA_UINT64_ARRAY, 12}, 60 {"ZPL_GID", sizeof (uint64_t), SA_UINT64_ARRAY, 13}, 61 {"ZPL_PAD", sizeof (uint64_t) * 4, SA_UINT64_ARRAY, 14}, 62 {"ZPL_ZNODE_ACL", 88, SA_UINT8_ARRAY, 15}, 63 {"ZPL_DACL_COUNT", sizeof (uint64_t), SA_UINT64_ARRAY, 0}, 64 {"ZPL_SYMLINK", 0, SA_UINT8_ARRAY, 0}, 65 {"ZPL_SCANSTAMP", 32, SA_UINT8_ARRAY, 0}, 66 {"ZPL_DACL_ACES", 0, SA_ACL, 0}, 67 {NULL, 0, 0, 0} 68 }; 69 70 #ifdef _KERNEL 71 72 int 73 zfs_sa_readlink(znode_t *zp, uio_t *uio) 74 { 75 dmu_buf_t *db = sa_get_db(zp->z_sa_hdl); 76 size_t bufsz; 77 int error; 78 79 bufsz = zp->z_size; 80 if (bufsz + ZFS_OLD_ZNODE_PHYS_SIZE < db->db_size) { 81 error = uiomove((caddr_t)db->db_data + 82 ZFS_OLD_ZNODE_PHYS_SIZE, 83 MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 84 } else { 85 dmu_buf_t *dbp; 86 if ((error = dmu_buf_hold(zp->z_zfsvfs->z_os, zp->z_id, 87 0, FTAG, &dbp)) == 0) { 88 error = uiomove(dbp->db_data, 89 MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 90 dmu_buf_rele(dbp, FTAG); 91 } 92 } 93 return (error); 94 } 95 96 void 97 zfs_sa_symlink(znode_t *zp, char *link, int len, dmu_tx_t *tx) 98 { 99 dmu_buf_t *db = sa_get_db(zp->z_sa_hdl); 100 101 if (ZFS_OLD_ZNODE_PHYS_SIZE + len <= dmu_bonus_max()) { 102 VERIFY(dmu_set_bonus(db, 103 len + ZFS_OLD_ZNODE_PHYS_SIZE, tx) == 0); 104 if (len) { 105 bcopy(link, (caddr_t)db->db_data + 106 ZFS_OLD_ZNODE_PHYS_SIZE, len); 107 } 108 } else { 109 dmu_buf_t *dbp; 110 111 zfs_grow_blocksize(zp, len, tx); 112 VERIFY(0 == dmu_buf_hold(zp->z_zfsvfs->z_os, 113 zp->z_id, 0, FTAG, &dbp)); 114 115 dmu_buf_will_dirty(dbp, tx); 116 117 ASSERT3U(len, <=, dbp->db_size); 118 bcopy(link, dbp->db_data, len); 119 dmu_buf_rele(dbp, FTAG); 120 } 121 } 122 123 void 124 zfs_sa_get_scanstamp(znode_t *zp, xvattr_t *xvap) 125 { 126 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 127 xoptattr_t *xoap; 128 129 VERIFY((xoap = xva_getxoptattr(xvap)) != NULL); 130 if (zp->z_is_sa) { 131 if (sa_lookup(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zfsvfs), 132 &xoap->xoa_av_scanstamp, 133 sizeof (xoap->xoa_av_scanstamp)) != 0) 134 return; 135 } else { 136 dmu_object_info_t doi; 137 dmu_buf_t *db = sa_get_db(zp->z_sa_hdl); 138 int len; 139 140 if (!(zp->z_pflags & ZFS_BONUS_SCANSTAMP)) 141 return; 142 143 sa_object_info(zp->z_sa_hdl, &doi); 144 len = sizeof (xoap->xoa_av_scanstamp) + 145 ZFS_OLD_ZNODE_PHYS_SIZE; 146 147 if (len <= doi.doi_bonus_size) { 148 (void) memcpy(xoap->xoa_av_scanstamp, 149 (caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE, 150 sizeof (xoap->xoa_av_scanstamp)); 151 } 152 } 153 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP); 154 } 155 156 void 157 zfs_sa_set_scanstamp(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx) 158 { 159 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 160 xoptattr_t *xoap; 161 162 VERIFY((xoap = xva_getxoptattr(xvap)) != NULL); 163 if (zp->z_is_sa) 164 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zfsvfs), 165 &xoap->xoa_av_scanstamp, 166 sizeof (xoap->xoa_av_scanstamp), tx)); 167 else { 168 dmu_object_info_t doi; 169 dmu_buf_t *db = sa_get_db(zp->z_sa_hdl); 170 int len; 171 172 sa_object_info(zp->z_sa_hdl, &doi); 173 len = sizeof (xoap->xoa_av_scanstamp) + 174 ZFS_OLD_ZNODE_PHYS_SIZE; 175 if (len > doi.doi_bonus_size) 176 VERIFY(dmu_set_bonus(db, len, tx) == 0); 177 (void) memcpy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE, 178 xoap->xoa_av_scanstamp, sizeof (xoap->xoa_av_scanstamp)); 179 180 zp->z_pflags |= ZFS_BONUS_SCANSTAMP; 181 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs), 182 &zp->z_pflags, sizeof (uint64_t), tx)); 183 } 184 } 185 186 /* 187 * I'm not convinced we should do any of this upgrade. 188 * since the SA code can read both old/new znode formats 189 * with probably little to know performance difference. 190 * 191 * All new files will be created with the new format. 192 */ 193 194 void 195 zfs_sa_upgrade(sa_handle_t *hdl, dmu_tx_t *tx) 196 { 197 dmu_buf_t *db = sa_get_db(hdl); 198 znode_t *zp = sa_get_userdata(hdl); 199 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 200 sa_bulk_attr_t bulk[20]; 201 int count = 0; 202 sa_bulk_attr_t sa_attrs[20] = { 0 }; 203 zfs_acl_locator_cb_t locate = { 0 }; 204 uint64_t uid, gid, mode, rdev, xattr, parent; 205 uint64_t crtime[2], mtime[2], ctime[2]; 206 zfs_acl_phys_t znode_acl; 207 char *slink = NULL; 208 char scanstamp[AV_SCANSTAMP_SZ]; 209 210 /* 211 * No upgrade if ACL isn't cached 212 * since we won't know which locks are held 213 * and ready the ACL would require special "locked" 214 * interfaces that would be messy 215 */ 216 if (zp->z_acl_cached == NULL) 217 return; 218 219 /* First do a bulk query of the attributes that aren't cached */ 220 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 221 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 222 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16); 223 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8); 224 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8); 225 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_XATTR(zfsvfs), NULL, &xattr, 8); 226 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL, &rdev, 8); 227 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8); 228 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8); 229 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ZNODE_ACL(zfsvfs), NULL, 230 &znode_acl, 88); 231 232 if (sa_bulk_lookup_locked(hdl, bulk, count) != 0) 233 return; 234 235 236 /* 237 * While the order here doesn't matter its best to try and organize 238 * it is such a way to pick up an already existing layout number 239 */ 240 count = 0; 241 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8); 242 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SIZE(zfsvfs), NULL, 243 &zp->z_size, 8); 244 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GEN(zfsvfs), 245 NULL, &zp->z_gen, 8); 246 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8); 247 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8); 248 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_PARENT(zfsvfs), 249 NULL, &parent, 8); 250 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_FLAGS(zfsvfs), NULL, 251 &zp->z_pflags, 8); 252 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_ATIME(zfsvfs), NULL, 253 zp->z_atime, 16); 254 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MTIME(zfsvfs), NULL, 255 &mtime, 16); 256 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CTIME(zfsvfs), NULL, 257 &ctime, 16); 258 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CRTIME(zfsvfs), NULL, 259 &crtime, 16); 260 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_LINKS(zfsvfs), NULL, 261 &zp->z_links, 8); 262 if (zp->z_vnode->v_type == VBLK || zp->z_vnode->v_type == VCHR) 263 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_RDEV(zfsvfs), NULL, 264 &rdev, 8); 265 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_COUNT(zfsvfs), NULL, 266 &zp->z_acl_cached->z_acl_count, 8); 267 268 if (zp->z_acl_cached->z_version < ZFS_ACL_VERSION_FUID) 269 zfs_acl_xform(zp, zp->z_acl_cached, CRED()); 270 271 locate.cb_aclp = zp->z_acl_cached; 272 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_ACES(zfsvfs), 273 zfs_acl_data_locator, &locate, zp->z_acl_cached->z_acl_bytes); 274 if (xattr) 275 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_RDEV(zfsvfs), 276 NULL, &rdev, 8); 277 278 /* 279 * is it a symlink ? 280 * 281 * this will probably never be exercised since we won't 282 * have the cached ACL. 283 */ 284 if (ZTOV(zp)->v_type == VLNK) { 285 slink = kmem_zalloc(zp->z_size + 1, KM_SLEEP); 286 if (zp->z_size + ZFS_OLD_ZNODE_PHYS_SIZE) 287 bcopy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE, 288 slink, zp->z_size); 289 else { 290 dmu_buf_t *dbp; 291 if (dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, 292 FTAG, &dbp)) 293 return; 294 bcopy(dbp->db_data, slink, zp->z_size); 295 dmu_buf_rele(dbp, FTAG); 296 } 297 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SYMLINK(zfsvfs), 298 NULL, slink, zp->z_size); 299 } 300 301 /* if scanstamp then add scanstamp */ 302 303 if (zp->z_pflags & ZFS_BONUS_SCANSTAMP) { 304 bcopy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE, 305 scanstamp, AV_SCANSTAMP_SZ); 306 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SCANSTAMP(zfsvfs), 307 NULL, scanstamp, AV_SCANSTAMP_SZ); 308 zp->z_pflags &= ~ZFS_BONUS_SCANSTAMP; 309 } 310 311 VERIFY(dmu_set_bonustype(db, DMU_OT_SA, tx) == 0); 312 VERIFY(sa_replace_all_by_template_locked(hdl, sa_attrs, 313 count, tx) == 0); 314 if (znode_acl.z_acl_extern_obj) 315 VERIFY(0 == dmu_object_free(zfsvfs->z_os, 316 znode_acl.z_acl_extern_obj, tx)); 317 318 zp->z_is_sa = B_TRUE; 319 if (slink) 320 kmem_free(slink, zp->z_size + 1); 321 322 } 323 324 void 325 zfs_sa_upgrade_txholds(dmu_tx_t *tx, znode_t *zp) 326 { 327 if (!zp->z_zfsvfs->z_use_sa || zp->z_is_sa) 328 return; 329 330 ASSERT(!zp->z_is_sa); 331 332 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 333 334 if (ZFS_EXTERNAL_ACL(zp)) { 335 dmu_tx_hold_free(tx, ZFS_EXTERNAL_ACL(zp), 0, 336 DMU_OBJECT_END); 337 } 338 } 339 340 #endif 341