1 /* 2 * linux/fs/reiserfs/xattr.c 3 * 4 * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com> 5 * 6 */ 7 8 /* 9 * In order to implement EA/ACLs in a clean, backwards compatible manner, 10 * they are implemented as files in a "private" directory. 11 * Each EA is in it's own file, with the directory layout like so (/ is assumed 12 * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory, 13 * directories named using the capital-hex form of the objectid and 14 * generation number are used. Inside each directory are individual files 15 * named with the name of the extended attribute. 16 * 17 * So, for objectid 12648430, we could have: 18 * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access 19 * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default 20 * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type 21 * .. or similar. 22 * 23 * The file contents are the text of the EA. The size is known based on the 24 * stat data describing the file. 25 * 26 * In the case of system.posix_acl_access and system.posix_acl_default, since 27 * these are special cases for filesystem ACLs, they are interpreted by the 28 * kernel, in addition, they are negatively and positively cached and attached 29 * to the inode so that unnecessary lookups are avoided. 30 * 31 * Locking works like so: 32 * Directory components (xattr root, xattr dir) are protectd by their i_mutex. 33 * The xattrs themselves are protected by the xattr_sem. 34 */ 35 36 #include <linux/reiserfs_fs.h> 37 #include <linux/capability.h> 38 #include <linux/dcache.h> 39 #include <linux/namei.h> 40 #include <linux/errno.h> 41 #include <linux/fs.h> 42 #include <linux/file.h> 43 #include <linux/pagemap.h> 44 #include <linux/xattr.h> 45 #include <linux/reiserfs_xattr.h> 46 #include <linux/reiserfs_acl.h> 47 #include <asm/uaccess.h> 48 #include <net/checksum.h> 49 #include <linux/stat.h> 50 #include <linux/quotaops.h> 51 #include <linux/security.h> 52 53 #define PRIVROOT_NAME ".reiserfs_priv" 54 #define XAROOT_NAME "xattrs" 55 56 57 /* Helpers for inode ops. We do this so that we don't have all the VFS 58 * overhead and also for proper i_mutex annotation. 59 * dir->i_mutex must be held for all of them. */ 60 #ifdef CONFIG_REISERFS_FS_XATTR 61 static int xattr_create(struct inode *dir, struct dentry *dentry, int mode) 62 { 63 BUG_ON(!mutex_is_locked(&dir->i_mutex)); 64 vfs_dq_init(dir); 65 return dir->i_op->create(dir, dentry, mode, NULL); 66 } 67 #endif 68 69 static int xattr_mkdir(struct inode *dir, struct dentry *dentry, int mode) 70 { 71 BUG_ON(!mutex_is_locked(&dir->i_mutex)); 72 vfs_dq_init(dir); 73 return dir->i_op->mkdir(dir, dentry, mode); 74 } 75 76 /* We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr 77 * mutation ops aren't called during rename or splace, which are the 78 * only other users of I_MUTEX_CHILD. It violates the ordering, but that's 79 * better than allocating another subclass just for this code. */ 80 static int xattr_unlink(struct inode *dir, struct dentry *dentry) 81 { 82 int error; 83 BUG_ON(!mutex_is_locked(&dir->i_mutex)); 84 vfs_dq_init(dir); 85 86 reiserfs_mutex_lock_nested_safe(&dentry->d_inode->i_mutex, 87 I_MUTEX_CHILD, dir->i_sb); 88 error = dir->i_op->unlink(dir, dentry); 89 mutex_unlock(&dentry->d_inode->i_mutex); 90 91 if (!error) 92 d_delete(dentry); 93 return error; 94 } 95 96 static int xattr_rmdir(struct inode *dir, struct dentry *dentry) 97 { 98 int error; 99 BUG_ON(!mutex_is_locked(&dir->i_mutex)); 100 vfs_dq_init(dir); 101 102 reiserfs_mutex_lock_nested_safe(&dentry->d_inode->i_mutex, 103 I_MUTEX_CHILD, dir->i_sb); 104 dentry_unhash(dentry); 105 error = dir->i_op->rmdir(dir, dentry); 106 if (!error) 107 dentry->d_inode->i_flags |= S_DEAD; 108 mutex_unlock(&dentry->d_inode->i_mutex); 109 if (!error) 110 d_delete(dentry); 111 dput(dentry); 112 113 return error; 114 } 115 116 #define xattr_may_create(flags) (!flags || flags & XATTR_CREATE) 117 118 static struct dentry *open_xa_root(struct super_block *sb, int flags) 119 { 120 struct dentry *privroot = REISERFS_SB(sb)->priv_root; 121 struct dentry *xaroot; 122 if (!privroot->d_inode) 123 return ERR_PTR(-ENODATA); 124 125 mutex_lock_nested(&privroot->d_inode->i_mutex, I_MUTEX_XATTR); 126 127 xaroot = dget(REISERFS_SB(sb)->xattr_root); 128 if (!xaroot) 129 xaroot = ERR_PTR(-ENODATA); 130 else if (!xaroot->d_inode) { 131 int err = -ENODATA; 132 if (xattr_may_create(flags)) 133 err = xattr_mkdir(privroot->d_inode, xaroot, 0700); 134 if (err) { 135 dput(xaroot); 136 xaroot = ERR_PTR(err); 137 } 138 } 139 140 mutex_unlock(&privroot->d_inode->i_mutex); 141 return xaroot; 142 } 143 144 static struct dentry *open_xa_dir(const struct inode *inode, int flags) 145 { 146 struct dentry *xaroot, *xadir; 147 char namebuf[17]; 148 149 xaroot = open_xa_root(inode->i_sb, flags); 150 if (IS_ERR(xaroot)) 151 return xaroot; 152 153 snprintf(namebuf, sizeof(namebuf), "%X.%X", 154 le32_to_cpu(INODE_PKEY(inode)->k_objectid), 155 inode->i_generation); 156 157 mutex_lock_nested(&xaroot->d_inode->i_mutex, I_MUTEX_XATTR); 158 159 xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf)); 160 if (!IS_ERR(xadir) && !xadir->d_inode) { 161 int err = -ENODATA; 162 if (xattr_may_create(flags)) 163 err = xattr_mkdir(xaroot->d_inode, xadir, 0700); 164 if (err) { 165 dput(xadir); 166 xadir = ERR_PTR(err); 167 } 168 } 169 170 mutex_unlock(&xaroot->d_inode->i_mutex); 171 dput(xaroot); 172 return xadir; 173 } 174 175 /* The following are side effects of other operations that aren't explicitly 176 * modifying extended attributes. This includes operations such as permissions 177 * or ownership changes, object deletions, etc. */ 178 struct reiserfs_dentry_buf { 179 struct dentry *xadir; 180 int count; 181 struct dentry *dentries[8]; 182 }; 183 184 static int 185 fill_with_dentries(void *buf, const char *name, int namelen, loff_t offset, 186 u64 ino, unsigned int d_type) 187 { 188 struct reiserfs_dentry_buf *dbuf = buf; 189 struct dentry *dentry; 190 WARN_ON_ONCE(!mutex_is_locked(&dbuf->xadir->d_inode->i_mutex)); 191 192 if (dbuf->count == ARRAY_SIZE(dbuf->dentries)) 193 return -ENOSPC; 194 195 if (name[0] == '.' && (name[1] == '\0' || 196 (name[1] == '.' && name[2] == '\0'))) 197 return 0; 198 199 dentry = lookup_one_len(name, dbuf->xadir, namelen); 200 if (IS_ERR(dentry)) { 201 return PTR_ERR(dentry); 202 } else if (!dentry->d_inode) { 203 /* A directory entry exists, but no file? */ 204 reiserfs_error(dentry->d_sb, "xattr-20003", 205 "Corrupted directory: xattr %s listed but " 206 "not found for file %s.\n", 207 dentry->d_name.name, dbuf->xadir->d_name.name); 208 dput(dentry); 209 return -EIO; 210 } 211 212 dbuf->dentries[dbuf->count++] = dentry; 213 return 0; 214 } 215 216 static void 217 cleanup_dentry_buf(struct reiserfs_dentry_buf *buf) 218 { 219 int i; 220 for (i = 0; i < buf->count; i++) 221 if (buf->dentries[i]) 222 dput(buf->dentries[i]); 223 } 224 225 static int reiserfs_for_each_xattr(struct inode *inode, 226 int (*action)(struct dentry *, void *), 227 void *data) 228 { 229 struct dentry *dir; 230 int i, err = 0; 231 loff_t pos = 0; 232 struct reiserfs_dentry_buf buf = { 233 .count = 0, 234 }; 235 236 /* Skip out, an xattr has no xattrs associated with it */ 237 if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1) 238 return 0; 239 240 reiserfs_write_unlock(inode->i_sb); 241 dir = open_xa_dir(inode, XATTR_REPLACE); 242 if (IS_ERR(dir)) { 243 err = PTR_ERR(dir); 244 reiserfs_write_lock(inode->i_sb); 245 goto out; 246 } else if (!dir->d_inode) { 247 err = 0; 248 reiserfs_write_lock(inode->i_sb); 249 goto out_dir; 250 } 251 252 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR); 253 254 reiserfs_write_lock(inode->i_sb); 255 256 buf.xadir = dir; 257 err = reiserfs_readdir_dentry(dir, &buf, fill_with_dentries, &pos); 258 while ((err == 0 || err == -ENOSPC) && buf.count) { 259 err = 0; 260 261 for (i = 0; i < buf.count && buf.dentries[i]; i++) { 262 int lerr = 0; 263 struct dentry *dentry = buf.dentries[i]; 264 265 if (err == 0 && !S_ISDIR(dentry->d_inode->i_mode)) 266 lerr = action(dentry, data); 267 268 dput(dentry); 269 buf.dentries[i] = NULL; 270 err = lerr ?: err; 271 } 272 buf.count = 0; 273 if (!err) 274 err = reiserfs_readdir_dentry(dir, &buf, 275 fill_with_dentries, &pos); 276 } 277 mutex_unlock(&dir->d_inode->i_mutex); 278 279 /* Clean up after a failed readdir */ 280 cleanup_dentry_buf(&buf); 281 282 if (!err) { 283 /* We start a transaction here to avoid a ABBA situation 284 * between the xattr root's i_mutex and the journal lock. 285 * This doesn't incur much additional overhead since the 286 * new transaction will just nest inside the 287 * outer transaction. */ 288 int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 + 289 4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb); 290 struct reiserfs_transaction_handle th; 291 err = journal_begin(&th, inode->i_sb, blocks); 292 if (!err) { 293 int jerror; 294 reiserfs_mutex_lock_nested_safe( 295 &dir->d_parent->d_inode->i_mutex, 296 I_MUTEX_XATTR, inode->i_sb); 297 err = action(dir, data); 298 jerror = journal_end(&th, inode->i_sb, blocks); 299 mutex_unlock(&dir->d_parent->d_inode->i_mutex); 300 err = jerror ?: err; 301 } 302 } 303 out_dir: 304 dput(dir); 305 out: 306 /* -ENODATA isn't an error */ 307 if (err == -ENODATA) 308 err = 0; 309 return err; 310 } 311 312 static int delete_one_xattr(struct dentry *dentry, void *data) 313 { 314 struct inode *dir = dentry->d_parent->d_inode; 315 316 /* This is the xattr dir, handle specially. */ 317 if (S_ISDIR(dentry->d_inode->i_mode)) 318 return xattr_rmdir(dir, dentry); 319 320 return xattr_unlink(dir, dentry); 321 } 322 323 static int chown_one_xattr(struct dentry *dentry, void *data) 324 { 325 struct iattr *attrs = data; 326 return reiserfs_setattr(dentry, attrs); 327 } 328 329 /* No i_mutex, but the inode is unconnected. */ 330 int reiserfs_delete_xattrs(struct inode *inode) 331 { 332 int err = reiserfs_for_each_xattr(inode, delete_one_xattr, NULL); 333 if (err) 334 reiserfs_warning(inode->i_sb, "jdm-20004", 335 "Couldn't delete all xattrs (%d)\n", err); 336 return err; 337 } 338 339 /* inode->i_mutex: down */ 340 int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs) 341 { 342 int err = reiserfs_for_each_xattr(inode, chown_one_xattr, attrs); 343 if (err) 344 reiserfs_warning(inode->i_sb, "jdm-20007", 345 "Couldn't chown all xattrs (%d)\n", err); 346 return err; 347 } 348 349 #ifdef CONFIG_REISERFS_FS_XATTR 350 /* Returns a dentry corresponding to a specific extended attribute file 351 * for the inode. If flags allow, the file is created. Otherwise, a 352 * valid or negative dentry, or an error is returned. */ 353 static struct dentry *xattr_lookup(struct inode *inode, const char *name, 354 int flags) 355 { 356 struct dentry *xadir, *xafile; 357 int err = 0; 358 359 xadir = open_xa_dir(inode, flags); 360 if (IS_ERR(xadir)) 361 return ERR_CAST(xadir); 362 363 mutex_lock_nested(&xadir->d_inode->i_mutex, I_MUTEX_XATTR); 364 xafile = lookup_one_len(name, xadir, strlen(name)); 365 if (IS_ERR(xafile)) { 366 err = PTR_ERR(xafile); 367 goto out; 368 } 369 370 if (xafile->d_inode && (flags & XATTR_CREATE)) 371 err = -EEXIST; 372 373 if (!xafile->d_inode) { 374 err = -ENODATA; 375 if (xattr_may_create(flags)) 376 err = xattr_create(xadir->d_inode, xafile, 377 0700|S_IFREG); 378 } 379 380 if (err) 381 dput(xafile); 382 out: 383 mutex_unlock(&xadir->d_inode->i_mutex); 384 dput(xadir); 385 if (err) 386 return ERR_PTR(err); 387 return xafile; 388 } 389 390 /* Internal operations on file data */ 391 static inline void reiserfs_put_page(struct page *page) 392 { 393 kunmap(page); 394 page_cache_release(page); 395 } 396 397 static struct page *reiserfs_get_page(struct inode *dir, size_t n) 398 { 399 struct address_space *mapping = dir->i_mapping; 400 struct page *page; 401 /* We can deadlock if we try to free dentries, 402 and an unlink/rmdir has just occured - GFP_NOFS avoids this */ 403 mapping_set_gfp_mask(mapping, GFP_NOFS); 404 page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, NULL); 405 if (!IS_ERR(page)) { 406 kmap(page); 407 if (PageError(page)) 408 goto fail; 409 } 410 return page; 411 412 fail: 413 reiserfs_put_page(page); 414 return ERR_PTR(-EIO); 415 } 416 417 static inline __u32 xattr_hash(const char *msg, int len) 418 { 419 return csum_partial(msg, len, 0); 420 } 421 422 int reiserfs_commit_write(struct file *f, struct page *page, 423 unsigned from, unsigned to); 424 int reiserfs_prepare_write(struct file *f, struct page *page, 425 unsigned from, unsigned to); 426 427 static void update_ctime(struct inode *inode) 428 { 429 struct timespec now = current_fs_time(inode->i_sb); 430 if (hlist_unhashed(&inode->i_hash) || !inode->i_nlink || 431 timespec_equal(&inode->i_ctime, &now)) 432 return; 433 434 inode->i_ctime = CURRENT_TIME_SEC; 435 mark_inode_dirty(inode); 436 } 437 438 static int lookup_and_delete_xattr(struct inode *inode, const char *name) 439 { 440 int err = 0; 441 struct dentry *dentry, *xadir; 442 443 xadir = open_xa_dir(inode, XATTR_REPLACE); 444 if (IS_ERR(xadir)) 445 return PTR_ERR(xadir); 446 447 mutex_lock_nested(&xadir->d_inode->i_mutex, I_MUTEX_XATTR); 448 dentry = lookup_one_len(name, xadir, strlen(name)); 449 if (IS_ERR(dentry)) { 450 err = PTR_ERR(dentry); 451 goto out_dput; 452 } 453 454 if (dentry->d_inode) { 455 reiserfs_write_lock(inode->i_sb); 456 err = xattr_unlink(xadir->d_inode, dentry); 457 reiserfs_write_unlock(inode->i_sb); 458 update_ctime(inode); 459 } 460 461 dput(dentry); 462 out_dput: 463 mutex_unlock(&xadir->d_inode->i_mutex); 464 dput(xadir); 465 return err; 466 } 467 468 469 /* Generic extended attribute operations that can be used by xa plugins */ 470 471 /* 472 * inode->i_mutex: down 473 */ 474 int 475 reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th, 476 struct inode *inode, const char *name, 477 const void *buffer, size_t buffer_size, int flags) 478 { 479 int err = 0; 480 struct dentry *dentry; 481 struct page *page; 482 char *data; 483 size_t file_pos = 0; 484 size_t buffer_pos = 0; 485 size_t new_size; 486 __u32 xahash = 0; 487 488 if (get_inode_sd_version(inode) == STAT_DATA_V1) 489 return -EOPNOTSUPP; 490 491 reiserfs_write_unlock(inode->i_sb); 492 493 if (!buffer) { 494 err = lookup_and_delete_xattr(inode, name); 495 reiserfs_write_lock(inode->i_sb); 496 return err; 497 } 498 499 dentry = xattr_lookup(inode, name, flags); 500 if (IS_ERR(dentry)) { 501 reiserfs_write_lock(inode->i_sb); 502 return PTR_ERR(dentry); 503 } 504 505 down_write(&REISERFS_I(inode)->i_xattr_sem); 506 507 reiserfs_write_lock(inode->i_sb); 508 509 xahash = xattr_hash(buffer, buffer_size); 510 while (buffer_pos < buffer_size || buffer_pos == 0) { 511 size_t chunk; 512 size_t skip = 0; 513 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1)); 514 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE) 515 chunk = PAGE_CACHE_SIZE; 516 else 517 chunk = buffer_size - buffer_pos; 518 519 page = reiserfs_get_page(dentry->d_inode, file_pos); 520 if (IS_ERR(page)) { 521 err = PTR_ERR(page); 522 goto out_unlock; 523 } 524 525 lock_page(page); 526 data = page_address(page); 527 528 if (file_pos == 0) { 529 struct reiserfs_xattr_header *rxh; 530 skip = file_pos = sizeof(struct reiserfs_xattr_header); 531 if (chunk + skip > PAGE_CACHE_SIZE) 532 chunk = PAGE_CACHE_SIZE - skip; 533 rxh = (struct reiserfs_xattr_header *)data; 534 rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC); 535 rxh->h_hash = cpu_to_le32(xahash); 536 } 537 538 err = reiserfs_prepare_write(NULL, page, page_offset, 539 page_offset + chunk + skip); 540 if (!err) { 541 if (buffer) 542 memcpy(data + skip, buffer + buffer_pos, chunk); 543 err = reiserfs_commit_write(NULL, page, page_offset, 544 page_offset + chunk + 545 skip); 546 } 547 unlock_page(page); 548 reiserfs_put_page(page); 549 buffer_pos += chunk; 550 file_pos += chunk; 551 skip = 0; 552 if (err || buffer_size == 0 || !buffer) 553 break; 554 } 555 556 new_size = buffer_size + sizeof(struct reiserfs_xattr_header); 557 if (!err && new_size < i_size_read(dentry->d_inode)) { 558 struct iattr newattrs = { 559 .ia_ctime = current_fs_time(inode->i_sb), 560 .ia_size = buffer_size, 561 .ia_valid = ATTR_SIZE | ATTR_CTIME, 562 }; 563 564 reiserfs_write_unlock(inode->i_sb); 565 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR); 566 down_write(&dentry->d_inode->i_alloc_sem); 567 reiserfs_write_lock(inode->i_sb); 568 569 err = reiserfs_setattr(dentry, &newattrs); 570 up_write(&dentry->d_inode->i_alloc_sem); 571 mutex_unlock(&dentry->d_inode->i_mutex); 572 } else 573 update_ctime(inode); 574 out_unlock: 575 up_write(&REISERFS_I(inode)->i_xattr_sem); 576 dput(dentry); 577 return err; 578 } 579 580 /* We need to start a transaction to maintain lock ordering */ 581 int reiserfs_xattr_set(struct inode *inode, const char *name, 582 const void *buffer, size_t buffer_size, int flags) 583 { 584 585 struct reiserfs_transaction_handle th; 586 int error, error2; 587 size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size); 588 589 if (!(flags & XATTR_REPLACE)) 590 jbegin_count += reiserfs_xattr_jcreate_nblocks(inode); 591 592 reiserfs_write_lock(inode->i_sb); 593 error = journal_begin(&th, inode->i_sb, jbegin_count); 594 if (error) { 595 reiserfs_write_unlock(inode->i_sb); 596 return error; 597 } 598 599 error = reiserfs_xattr_set_handle(&th, inode, name, 600 buffer, buffer_size, flags); 601 602 error2 = journal_end(&th, inode->i_sb, jbegin_count); 603 if (error == 0) 604 error = error2; 605 reiserfs_write_unlock(inode->i_sb); 606 607 return error; 608 } 609 610 /* 611 * inode->i_mutex: down 612 */ 613 int 614 reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer, 615 size_t buffer_size) 616 { 617 ssize_t err = 0; 618 struct dentry *dentry; 619 size_t isize; 620 size_t file_pos = 0; 621 size_t buffer_pos = 0; 622 struct page *page; 623 __u32 hash = 0; 624 625 if (name == NULL) 626 return -EINVAL; 627 628 /* We can't have xattrs attached to v1 items since they don't have 629 * generation numbers */ 630 if (get_inode_sd_version(inode) == STAT_DATA_V1) 631 return -EOPNOTSUPP; 632 633 dentry = xattr_lookup(inode, name, XATTR_REPLACE); 634 if (IS_ERR(dentry)) { 635 err = PTR_ERR(dentry); 636 goto out; 637 } 638 639 down_read(&REISERFS_I(inode)->i_xattr_sem); 640 641 isize = i_size_read(dentry->d_inode); 642 643 /* Just return the size needed */ 644 if (buffer == NULL) { 645 err = isize - sizeof(struct reiserfs_xattr_header); 646 goto out_unlock; 647 } 648 649 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) { 650 err = -ERANGE; 651 goto out_unlock; 652 } 653 654 while (file_pos < isize) { 655 size_t chunk; 656 char *data; 657 size_t skip = 0; 658 if (isize - file_pos > PAGE_CACHE_SIZE) 659 chunk = PAGE_CACHE_SIZE; 660 else 661 chunk = isize - file_pos; 662 663 page = reiserfs_get_page(dentry->d_inode, file_pos); 664 if (IS_ERR(page)) { 665 err = PTR_ERR(page); 666 goto out_unlock; 667 } 668 669 lock_page(page); 670 data = page_address(page); 671 if (file_pos == 0) { 672 struct reiserfs_xattr_header *rxh = 673 (struct reiserfs_xattr_header *)data; 674 skip = file_pos = sizeof(struct reiserfs_xattr_header); 675 chunk -= skip; 676 /* Magic doesn't match up.. */ 677 if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) { 678 unlock_page(page); 679 reiserfs_put_page(page); 680 reiserfs_warning(inode->i_sb, "jdm-20001", 681 "Invalid magic for xattr (%s) " 682 "associated with %k", name, 683 INODE_PKEY(inode)); 684 err = -EIO; 685 goto out_unlock; 686 } 687 hash = le32_to_cpu(rxh->h_hash); 688 } 689 memcpy(buffer + buffer_pos, data + skip, chunk); 690 unlock_page(page); 691 reiserfs_put_page(page); 692 file_pos += chunk; 693 buffer_pos += chunk; 694 skip = 0; 695 } 696 err = isize - sizeof(struct reiserfs_xattr_header); 697 698 if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) != 699 hash) { 700 reiserfs_warning(inode->i_sb, "jdm-20002", 701 "Invalid hash for xattr (%s) associated " 702 "with %k", name, INODE_PKEY(inode)); 703 err = -EIO; 704 } 705 706 out_unlock: 707 up_read(&REISERFS_I(inode)->i_xattr_sem); 708 dput(dentry); 709 710 out: 711 return err; 712 } 713 714 /* 715 * In order to implement different sets of xattr operations for each xattr 716 * prefix with the generic xattr API, a filesystem should create a 717 * null-terminated array of struct xattr_handler (one for each prefix) and 718 * hang a pointer to it off of the s_xattr field of the superblock. 719 * 720 * The generic_fooxattr() functions will use this list to dispatch xattr 721 * operations to the correct xattr_handler. 722 */ 723 #define for_each_xattr_handler(handlers, handler) \ 724 for ((handler) = *(handlers)++; \ 725 (handler) != NULL; \ 726 (handler) = *(handlers)++) 727 728 /* This is the implementation for the xattr plugin infrastructure */ 729 static inline struct xattr_handler * 730 find_xattr_handler_prefix(struct xattr_handler **handlers, 731 const char *name) 732 { 733 struct xattr_handler *xah; 734 735 if (!handlers) 736 return NULL; 737 738 for_each_xattr_handler(handlers, xah) { 739 if (strncmp(xah->prefix, name, strlen(xah->prefix)) == 0) 740 break; 741 } 742 743 return xah; 744 } 745 746 747 /* 748 * Inode operation getxattr() 749 */ 750 ssize_t 751 reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer, 752 size_t size) 753 { 754 struct xattr_handler *handler; 755 756 handler = find_xattr_handler_prefix(dentry->d_sb->s_xattr, name); 757 758 if (!handler || get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1) 759 return -EOPNOTSUPP; 760 761 return handler->get(dentry, name, buffer, size, handler->flags); 762 } 763 764 /* 765 * Inode operation setxattr() 766 * 767 * dentry->d_inode->i_mutex down 768 */ 769 int 770 reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value, 771 size_t size, int flags) 772 { 773 struct xattr_handler *handler; 774 775 handler = find_xattr_handler_prefix(dentry->d_sb->s_xattr, name); 776 777 if (!handler || get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1) 778 return -EOPNOTSUPP; 779 780 return handler->set(dentry, name, value, size, flags, handler->flags); 781 } 782 783 /* 784 * Inode operation removexattr() 785 * 786 * dentry->d_inode->i_mutex down 787 */ 788 int reiserfs_removexattr(struct dentry *dentry, const char *name) 789 { 790 struct xattr_handler *handler; 791 handler = find_xattr_handler_prefix(dentry->d_sb->s_xattr, name); 792 793 if (!handler || get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1) 794 return -EOPNOTSUPP; 795 796 return handler->set(dentry, name, NULL, 0, XATTR_REPLACE, handler->flags); 797 } 798 799 struct listxattr_buf { 800 size_t size; 801 size_t pos; 802 char *buf; 803 struct dentry *dentry; 804 }; 805 806 static int listxattr_filler(void *buf, const char *name, int namelen, 807 loff_t offset, u64 ino, unsigned int d_type) 808 { 809 struct listxattr_buf *b = (struct listxattr_buf *)buf; 810 size_t size; 811 if (name[0] != '.' || 812 (namelen != 1 && (name[1] != '.' || namelen != 2))) { 813 struct xattr_handler *handler; 814 handler = find_xattr_handler_prefix(b->dentry->d_sb->s_xattr, 815 name); 816 if (!handler) /* Unsupported xattr name */ 817 return 0; 818 if (b->buf) { 819 size = handler->list(b->dentry, b->buf + b->pos, 820 b->size, name, namelen, 821 handler->flags); 822 if (size > b->size) 823 return -ERANGE; 824 } else { 825 size = handler->list(b->dentry, NULL, 0, name, 826 namelen, handler->flags); 827 } 828 829 b->pos += size; 830 } 831 return 0; 832 } 833 834 /* 835 * Inode operation listxattr() 836 * 837 * We totally ignore the generic listxattr here because it would be stupid 838 * not to. Since the xattrs are organized in a directory, we can just 839 * readdir to find them. 840 */ 841 ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size) 842 { 843 struct dentry *dir; 844 int err = 0; 845 loff_t pos = 0; 846 struct listxattr_buf buf = { 847 .dentry = dentry, 848 .buf = buffer, 849 .size = buffer ? size : 0, 850 }; 851 852 if (!dentry->d_inode) 853 return -EINVAL; 854 855 if (!dentry->d_sb->s_xattr || 856 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1) 857 return -EOPNOTSUPP; 858 859 dir = open_xa_dir(dentry->d_inode, XATTR_REPLACE); 860 if (IS_ERR(dir)) { 861 err = PTR_ERR(dir); 862 if (err == -ENODATA) 863 err = 0; /* Not an error if there aren't any xattrs */ 864 goto out; 865 } 866 867 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR); 868 err = reiserfs_readdir_dentry(dir, &buf, listxattr_filler, &pos); 869 mutex_unlock(&dir->d_inode->i_mutex); 870 871 if (!err) 872 err = buf.pos; 873 874 dput(dir); 875 out: 876 return err; 877 } 878 879 static int reiserfs_check_acl(struct inode *inode, int mask) 880 { 881 struct posix_acl *acl; 882 int error = -EAGAIN; /* do regular unix permission checks by default */ 883 884 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS); 885 886 if (acl) { 887 if (!IS_ERR(acl)) { 888 error = posix_acl_permission(inode, acl, mask); 889 posix_acl_release(acl); 890 } else if (PTR_ERR(acl) != -ENODATA) 891 error = PTR_ERR(acl); 892 } 893 894 return error; 895 } 896 897 static int create_privroot(struct dentry *dentry) 898 { 899 int err; 900 struct inode *inode = dentry->d_parent->d_inode; 901 WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex)); 902 903 err = xattr_mkdir(inode, dentry, 0700); 904 if (err || !dentry->d_inode) { 905 reiserfs_warning(dentry->d_sb, "jdm-20006", 906 "xattrs/ACLs enabled and couldn't " 907 "find/create .reiserfs_priv. " 908 "Failing mount."); 909 return -EOPNOTSUPP; 910 } 911 912 dentry->d_inode->i_flags |= S_PRIVATE; 913 reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr " 914 "storage.\n", PRIVROOT_NAME); 915 916 return 0; 917 } 918 919 #else 920 int __init reiserfs_xattr_register_handlers(void) { return 0; } 921 void reiserfs_xattr_unregister_handlers(void) {} 922 static int create_privroot(struct dentry *dentry) { return 0; } 923 #endif 924 925 /* Actual operations that are exported to VFS-land */ 926 struct xattr_handler *reiserfs_xattr_handlers[] = { 927 #ifdef CONFIG_REISERFS_FS_XATTR 928 &reiserfs_xattr_user_handler, 929 &reiserfs_xattr_trusted_handler, 930 #endif 931 #ifdef CONFIG_REISERFS_FS_SECURITY 932 &reiserfs_xattr_security_handler, 933 #endif 934 #ifdef CONFIG_REISERFS_FS_POSIX_ACL 935 &reiserfs_posix_acl_access_handler, 936 &reiserfs_posix_acl_default_handler, 937 #endif 938 NULL 939 }; 940 941 static int xattr_mount_check(struct super_block *s) 942 { 943 /* We need generation numbers to ensure that the oid mapping is correct 944 * v3.5 filesystems don't have them. */ 945 if (old_format_only(s)) { 946 if (reiserfs_xattrs_optional(s)) { 947 /* Old format filesystem, but optional xattrs have 948 * been enabled. Error out. */ 949 reiserfs_warning(s, "jdm-2005", 950 "xattrs/ACLs not supported " 951 "on pre-v3.6 format filesystems. " 952 "Failing mount."); 953 return -EOPNOTSUPP; 954 } 955 } 956 957 return 0; 958 } 959 960 int reiserfs_permission(struct inode *inode, int mask) 961 { 962 /* 963 * We don't do permission checks on the internal objects. 964 * Permissions are determined by the "owning" object. 965 */ 966 if (IS_PRIVATE(inode)) 967 return 0; 968 969 #ifdef CONFIG_REISERFS_FS_XATTR 970 /* 971 * Stat data v1 doesn't support ACLs. 972 */ 973 if (get_inode_sd_version(inode) != STAT_DATA_V1) 974 return generic_permission(inode, mask, reiserfs_check_acl); 975 #endif 976 return generic_permission(inode, mask, NULL); 977 } 978 979 /* This will catch lookups from the fs root to .reiserfs_priv */ 980 static int 981 xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name) 982 { 983 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root; 984 if (container_of(q1, struct dentry, d_name) == priv_root) 985 return -ENOENT; 986 if (q1->len == name->len && 987 !memcmp(q1->name, name->name, name->len)) 988 return 0; 989 return 1; 990 } 991 992 static const struct dentry_operations xattr_lookup_poison_ops = { 993 .d_compare = xattr_lookup_poison, 994 }; 995 996 int reiserfs_lookup_privroot(struct super_block *s) 997 { 998 struct dentry *dentry; 999 int err = 0; 1000 1001 /* If we don't have the privroot located yet - go find it */ 1002 reiserfs_mutex_lock_safe(&s->s_root->d_inode->i_mutex, s); 1003 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root, 1004 strlen(PRIVROOT_NAME)); 1005 if (!IS_ERR(dentry)) { 1006 REISERFS_SB(s)->priv_root = dentry; 1007 if (!reiserfs_expose_privroot(s)) 1008 s->s_root->d_op = &xattr_lookup_poison_ops; 1009 if (dentry->d_inode) 1010 dentry->d_inode->i_flags |= S_PRIVATE; 1011 } else 1012 err = PTR_ERR(dentry); 1013 mutex_unlock(&s->s_root->d_inode->i_mutex); 1014 1015 return err; 1016 } 1017 1018 /* We need to take a copy of the mount flags since things like 1019 * MS_RDONLY don't get set until *after* we're called. 1020 * mount_flags != mount_options */ 1021 int reiserfs_xattr_init(struct super_block *s, int mount_flags) 1022 { 1023 int err = 0; 1024 struct dentry *privroot = REISERFS_SB(s)->priv_root; 1025 1026 err = xattr_mount_check(s); 1027 if (err) 1028 goto error; 1029 1030 if (!privroot->d_inode && !(mount_flags & MS_RDONLY)) { 1031 reiserfs_mutex_lock_safe(&s->s_root->d_inode->i_mutex, s); 1032 err = create_privroot(REISERFS_SB(s)->priv_root); 1033 mutex_unlock(&s->s_root->d_inode->i_mutex); 1034 } 1035 1036 if (privroot->d_inode) { 1037 s->s_xattr = reiserfs_xattr_handlers; 1038 reiserfs_mutex_lock_safe(&privroot->d_inode->i_mutex, s); 1039 if (!REISERFS_SB(s)->xattr_root) { 1040 struct dentry *dentry; 1041 dentry = lookup_one_len(XAROOT_NAME, privroot, 1042 strlen(XAROOT_NAME)); 1043 if (!IS_ERR(dentry)) 1044 REISERFS_SB(s)->xattr_root = dentry; 1045 else 1046 err = PTR_ERR(dentry); 1047 } 1048 mutex_unlock(&privroot->d_inode->i_mutex); 1049 } 1050 1051 error: 1052 if (err) { 1053 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt)); 1054 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt)); 1055 } 1056 1057 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */ 1058 if (reiserfs_posixacl(s)) 1059 s->s_flags |= MS_POSIXACL; 1060 else 1061 s->s_flags &= ~MS_POSIXACL; 1062 1063 return err; 1064 } 1065