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