1 /* 2 * Copyright (C) 2007 Red Hat. All rights reserved. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public 6 * License v2 as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public 14 * License along with this program; if not, write to the 15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16 * Boston, MA 021110-1307, USA. 17 */ 18 19 #include <linux/init.h> 20 #include <linux/fs.h> 21 #include <linux/slab.h> 22 #include <linux/rwsem.h> 23 #include <linux/xattr.h> 24 #include "ctree.h" 25 #include "btrfs_inode.h" 26 #include "transaction.h" 27 #include "xattr.h" 28 #include "disk-io.h" 29 30 static struct xattr_handler *btrfs_xattr_handler_map[] = { 31 [BTRFS_XATTR_INDEX_USER] = &btrfs_xattr_user_handler, 32 [BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS] = &btrfs_xattr_acl_access_handler, 33 [BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &btrfs_xattr_acl_default_handler, 34 [BTRFS_XATTR_INDEX_TRUSTED] = &btrfs_xattr_trusted_handler, 35 [BTRFS_XATTR_INDEX_SECURITY] = &btrfs_xattr_security_handler, 36 [BTRFS_XATTR_INDEX_SYSTEM] = &btrfs_xattr_system_handler, 37 }; 38 39 struct xattr_handler *btrfs_xattr_handlers[] = { 40 &btrfs_xattr_user_handler, 41 &btrfs_xattr_acl_access_handler, 42 &btrfs_xattr_acl_default_handler, 43 &btrfs_xattr_trusted_handler, 44 &btrfs_xattr_security_handler, 45 &btrfs_xattr_system_handler, 46 NULL, 47 }; 48 49 /* 50 * @param name - the xattr name 51 * @return - the xattr_handler for the xattr, NULL if its not found 52 * 53 * use this with listxattr where we don't already know the type of xattr we 54 * have 55 */ 56 static struct xattr_handler *find_btrfs_xattr_handler(struct extent_buffer *l, 57 unsigned long name_ptr, 58 u16 name_len) 59 { 60 struct xattr_handler *handler = NULL; 61 int i = 0; 62 63 for (handler = btrfs_xattr_handlers[i]; handler != NULL; i++, 64 handler = btrfs_xattr_handlers[i]) { 65 u16 prefix_len = strlen(handler->prefix); 66 67 if (name_len < prefix_len) 68 continue; 69 70 if (memcmp_extent_buffer(l, handler->prefix, name_ptr, 71 prefix_len) == 0) 72 break; 73 } 74 75 return handler; 76 } 77 78 /* 79 * @param name_index - the index for the xattr handler 80 * @return the xattr_handler if we found it, NULL otherwise 81 * 82 * use this if we know the type of the xattr already 83 */ 84 static struct xattr_handler *btrfs_xattr_handler(int name_index) 85 { 86 struct xattr_handler *handler = NULL; 87 88 if (name_index >= 0 && 89 name_index < ARRAY_SIZE(btrfs_xattr_handler_map)) 90 handler = btrfs_xattr_handler_map[name_index]; 91 92 return handler; 93 } 94 95 static inline char *get_name(const char *name, int name_index) 96 { 97 char *ret = NULL; 98 struct xattr_handler *handler = btrfs_xattr_handler(name_index); 99 int prefix_len; 100 101 if (!handler) 102 return ret; 103 104 prefix_len = strlen(handler->prefix); 105 106 ret = kmalloc(strlen(name) + prefix_len + 1, GFP_KERNEL); 107 if (!ret) 108 return ret; 109 110 memcpy(ret, handler->prefix, prefix_len); 111 memcpy(ret+prefix_len, name, strlen(name)); 112 ret[prefix_len + strlen(name)] = '\0'; 113 114 return ret; 115 } 116 117 size_t btrfs_xattr_generic_list(struct inode *inode, char *list, 118 size_t list_size, const char *name, 119 size_t name_len) 120 { 121 if (list && (name_len+1) <= list_size) { 122 memcpy(list, name, name_len); 123 list[name_len] = '\0'; 124 } else 125 return -ERANGE; 126 127 return name_len+1; 128 } 129 130 ssize_t btrfs_xattr_get(struct inode *inode, int name_index, 131 const char *attr_name, void *buffer, size_t size) 132 { 133 struct btrfs_dir_item *di; 134 struct btrfs_root *root = BTRFS_I(inode)->root; 135 struct btrfs_path *path; 136 struct extent_buffer *leaf; 137 struct xattr_handler *handler = btrfs_xattr_handler(name_index); 138 int ret = 0; 139 unsigned long data_ptr; 140 char *name; 141 142 if (!handler) 143 return -EOPNOTSUPP; 144 name = get_name(attr_name, name_index); 145 if (!name) 146 return -ENOMEM; 147 148 path = btrfs_alloc_path(); 149 if (!path) { 150 kfree(name); 151 return -ENOMEM; 152 } 153 154 mutex_lock(&root->fs_info->fs_mutex); 155 /* lookup the xattr by name */ 156 di = btrfs_lookup_xattr(NULL, root, path, inode->i_ino, name, 157 strlen(name), 0); 158 if (!di || IS_ERR(di)) { 159 ret = -ENODATA; 160 goto out; 161 } 162 163 leaf = path->nodes[0]; 164 /* if size is 0, that means we want the size of the attr */ 165 if (!size) { 166 ret = btrfs_dir_data_len(leaf, di); 167 goto out; 168 } 169 170 /* now get the data out of our dir_item */ 171 if (btrfs_dir_data_len(leaf, di) > size) { 172 ret = -ERANGE; 173 goto out; 174 } 175 data_ptr = (unsigned long)((char *)(di + 1) + 176 btrfs_dir_name_len(leaf, di)); 177 read_extent_buffer(leaf, buffer, data_ptr, 178 btrfs_dir_data_len(leaf, di)); 179 ret = btrfs_dir_data_len(leaf, di); 180 181 out: 182 mutex_unlock(&root->fs_info->fs_mutex); 183 kfree(name); 184 btrfs_free_path(path); 185 return ret; 186 } 187 188 int btrfs_xattr_set(struct inode *inode, int name_index, 189 const char *attr_name, const void *value, size_t size, 190 int flags) 191 { 192 struct btrfs_dir_item *di; 193 struct btrfs_root *root = BTRFS_I(inode)->root; 194 struct btrfs_trans_handle *trans; 195 struct btrfs_path *path; 196 struct xattr_handler *handler = btrfs_xattr_handler(name_index); 197 char *name; 198 int ret = 0, mod = 0; 199 if (!handler) 200 return -EOPNOTSUPP; 201 name = get_name(attr_name, name_index); 202 if (!name) 203 return -ENOMEM; 204 205 path = btrfs_alloc_path(); 206 if (!path) { 207 kfree(name); 208 return -ENOMEM; 209 } 210 211 mutex_lock(&root->fs_info->fs_mutex); 212 trans = btrfs_start_transaction(root, 1); 213 btrfs_set_trans_block_group(trans, inode); 214 215 /* first lets see if we already have this xattr */ 216 di = btrfs_lookup_xattr(trans, root, path, inode->i_ino, name, 217 strlen(name), -1); 218 if (IS_ERR(di)) { 219 ret = PTR_ERR(di); 220 goto out; 221 } 222 223 /* ok we already have this xattr, lets remove it */ 224 if (di) { 225 /* if we want create only exit */ 226 if (flags & XATTR_CREATE) { 227 ret = -EEXIST; 228 goto out; 229 } 230 231 ret = btrfs_delete_one_dir_name(trans, root, path, di); 232 if (ret) 233 goto out; 234 btrfs_release_path(root, path); 235 236 /* if we don't have a value then we are removing the xattr */ 237 if (!value) { 238 mod = 1; 239 goto out; 240 } 241 } else if (flags & XATTR_REPLACE) { 242 /* we couldn't find the attr to replace, so error out */ 243 ret = -ENODATA; 244 goto out; 245 } 246 247 /* ok we have to create a completely new xattr */ 248 ret = btrfs_insert_xattr_item(trans, root, name, strlen(name), 249 value, size, inode->i_ino); 250 if (ret) 251 goto out; 252 mod = 1; 253 254 out: 255 if (mod) { 256 inode->i_ctime = CURRENT_TIME; 257 ret = btrfs_update_inode(trans, root, inode); 258 } 259 260 btrfs_end_transaction(trans, root); 261 mutex_unlock(&root->fs_info->fs_mutex); 262 kfree(name); 263 btrfs_free_path(path); 264 265 return ret; 266 } 267 268 ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size) 269 { 270 struct btrfs_key key, found_key; 271 struct inode *inode = dentry->d_inode; 272 struct btrfs_root *root = BTRFS_I(inode)->root; 273 struct btrfs_path *path; 274 struct btrfs_item *item; 275 struct extent_buffer *leaf; 276 struct btrfs_dir_item *di; 277 struct xattr_handler *handler; 278 int ret = 0, slot, advance; 279 size_t total_size = 0, size_left = size, written; 280 unsigned long name_ptr; 281 char *name; 282 u32 nritems; 283 284 /* 285 * ok we want all objects associated with this id. 286 * NOTE: we set key.offset = 0; because we want to start with the 287 * first xattr that we find and walk forward 288 */ 289 key.objectid = inode->i_ino; 290 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY); 291 key.offset = 0; 292 293 path = btrfs_alloc_path(); 294 if (!path) 295 return -ENOMEM; 296 path->reada = 2; 297 298 mutex_lock(&root->fs_info->fs_mutex); 299 300 /* search for our xattrs */ 301 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 302 if (ret < 0) 303 goto err; 304 ret = 0; 305 advance = 0; 306 while (1) { 307 leaf = path->nodes[0]; 308 nritems = btrfs_header_nritems(leaf); 309 slot = path->slots[0]; 310 311 /* this is where we start walking through the path */ 312 if (advance || slot >= nritems) { 313 /* 314 * if we've reached the last slot in this leaf we need 315 * to go to the next leaf and reset everything 316 */ 317 if (slot >= nritems-1) { 318 ret = btrfs_next_leaf(root, path); 319 if (ret) 320 break; 321 leaf = path->nodes[0]; 322 nritems = btrfs_header_nritems(leaf); 323 slot = path->slots[0]; 324 } else { 325 /* 326 * just walking through the slots on this leaf 327 */ 328 slot++; 329 path->slots[0]++; 330 } 331 } 332 advance = 1; 333 334 item = btrfs_item_nr(leaf, slot); 335 btrfs_item_key_to_cpu(leaf, &found_key, slot); 336 337 /* check to make sure this item is what we want */ 338 if (found_key.objectid != key.objectid) 339 break; 340 if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY) 341 break; 342 343 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 344 345 total_size += btrfs_dir_name_len(leaf, di)+1; 346 347 /* we are just looking for how big our buffer needs to be */ 348 if (!size) 349 continue; 350 351 /* find our handler for this xattr */ 352 name_ptr = (unsigned long)(di + 1); 353 handler = find_btrfs_xattr_handler(leaf, name_ptr, 354 btrfs_dir_name_len(leaf, di)); 355 if (!handler) { 356 printk(KERN_ERR "btrfs: unsupported xattr found\n"); 357 continue; 358 } 359 360 name = kmalloc(btrfs_dir_name_len(leaf, di), GFP_KERNEL); 361 read_extent_buffer(leaf, name, name_ptr, 362 btrfs_dir_name_len(leaf, di)); 363 364 /* call the list function associated with this xattr */ 365 written = handler->list(inode, buffer, size_left, name, 366 btrfs_dir_name_len(leaf, di)); 367 kfree(name); 368 369 if (written < 0) { 370 ret = -ERANGE; 371 break; 372 } 373 374 size_left -= written; 375 buffer += written; 376 } 377 ret = total_size; 378 379 err: 380 mutex_unlock(&root->fs_info->fs_mutex); 381 btrfs_free_path(path); 382 383 return ret; 384 } 385 386 /* 387 * delete all the xattrs associated with the inode. fs_mutex should be 388 * held when we come into here 389 */ 390 int btrfs_delete_xattrs(struct btrfs_trans_handle *trans, 391 struct btrfs_root *root, struct inode *inode) 392 { 393 struct btrfs_path *path; 394 struct btrfs_key key, found_key; 395 struct btrfs_item *item; 396 struct extent_buffer *leaf; 397 int ret; 398 399 path = btrfs_alloc_path(); 400 if (!path) 401 return -ENOMEM; 402 path->reada = -1; 403 key.objectid = inode->i_ino; 404 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY); 405 key.offset = (u64)-1; 406 407 while(1) { 408 /* look for our next xattr */ 409 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 410 if (ret < 0) 411 goto out; 412 BUG_ON(ret == 0); 413 414 if (path->slots[0] == 0) 415 break; 416 417 path->slots[0]--; 418 leaf = path->nodes[0]; 419 item = btrfs_item_nr(leaf, path->slots[0]); 420 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 421 422 if (found_key.objectid != key.objectid) 423 break; 424 if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY) 425 break; 426 427 ret = btrfs_del_item(trans, root, path); 428 BUG_ON(ret); 429 btrfs_release_path(root, path); 430 } 431 ret = 0; 432 out: 433 btrfs_free_path(path); 434 435 return ret; 436 } 437 438 /* 439 * Handler functions 440 */ 441 #define BTRFS_XATTR_SETGET_FUNCS(name, index) \ 442 static int btrfs_xattr_##name##_get(struct inode *inode, \ 443 const char *name, void *value, \ 444 size_t size) \ 445 { \ 446 if (*name == '\0') \ 447 return -EINVAL; \ 448 return btrfs_xattr_get(inode, index, name, value, size); \ 449 } \ 450 static int btrfs_xattr_##name##_set(struct inode *inode, \ 451 const char *name, const void *value,\ 452 size_t size, int flags) \ 453 { \ 454 if (*name == '\0') \ 455 return -EINVAL; \ 456 return btrfs_xattr_set(inode, index, name, value, size, flags); \ 457 } \ 458 BTRFS_XATTR_SETGET_FUNCS(security, BTRFS_XATTR_INDEX_SECURITY); 459 BTRFS_XATTR_SETGET_FUNCS(system, BTRFS_XATTR_INDEX_SYSTEM); 460 BTRFS_XATTR_SETGET_FUNCS(user, BTRFS_XATTR_INDEX_USER); 461 BTRFS_XATTR_SETGET_FUNCS(trusted, BTRFS_XATTR_INDEX_TRUSTED); 462 463 struct xattr_handler btrfs_xattr_security_handler = { 464 .prefix = XATTR_SECURITY_PREFIX, 465 .list = btrfs_xattr_generic_list, 466 .get = btrfs_xattr_security_get, 467 .set = btrfs_xattr_security_set, 468 }; 469 470 struct xattr_handler btrfs_xattr_system_handler = { 471 .prefix = XATTR_SYSTEM_PREFIX, 472 .list = btrfs_xattr_generic_list, 473 .get = btrfs_xattr_system_get, 474 .set = btrfs_xattr_system_set, 475 }; 476 477 struct xattr_handler btrfs_xattr_user_handler = { 478 .prefix = XATTR_USER_PREFIX, 479 .list = btrfs_xattr_generic_list, 480 .get = btrfs_xattr_user_get, 481 .set = btrfs_xattr_user_set, 482 }; 483 484 struct xattr_handler btrfs_xattr_trusted_handler = { 485 .prefix = XATTR_TRUSTED_PREFIX, 486 .list = btrfs_xattr_generic_list, 487 .get = btrfs_xattr_trusted_get, 488 .set = btrfs_xattr_trusted_set, 489 }; 490