1 /* 2 * Copyright (C) International Business Machines Corp., 2000-2004 3 * Copyright (C) Christoph Hellwig, 2002 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13 * the GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 */ 19 20 #include <linux/fs.h> 21 #include <linux/xattr.h> 22 #include <linux/posix_acl_xattr.h> 23 #include <linux/quotaops.h> 24 #include <linux/security.h> 25 #include "jfs_incore.h" 26 #include "jfs_superblock.h" 27 #include "jfs_dmap.h" 28 #include "jfs_debug.h" 29 #include "jfs_dinode.h" 30 #include "jfs_extent.h" 31 #include "jfs_metapage.h" 32 #include "jfs_xattr.h" 33 #include "jfs_acl.h" 34 35 /* 36 * jfs_xattr.c: extended attribute service 37 * 38 * Overall design -- 39 * 40 * Format: 41 * 42 * Extended attribute lists (jfs_ea_list) consist of an overall size (32 bit 43 * value) and a variable (0 or more) number of extended attribute 44 * entries. Each extended attribute entry (jfs_ea) is a <name,value> double 45 * where <name> is constructed from a null-terminated ascii string 46 * (1 ... 255 bytes in the name) and <value> is arbitrary 8 bit data 47 * (1 ... 65535 bytes). The in-memory format is 48 * 49 * 0 1 2 4 4 + namelen + 1 50 * +-------+--------+--------+----------------+-------------------+ 51 * | Flags | Name | Value | Name String \0 | Data . . . . | 52 * | | Length | Length | | | 53 * +-------+--------+--------+----------------+-------------------+ 54 * 55 * A jfs_ea_list then is structured as 56 * 57 * 0 4 4 + EA_SIZE(ea1) 58 * +------------+-------------------+--------------------+----- 59 * | Overall EA | First FEA Element | Second FEA Element | ..... 60 * | List Size | | | 61 * +------------+-------------------+--------------------+----- 62 * 63 * On-disk: 64 * 65 * FEALISTs are stored on disk using blocks allocated by dbAlloc() and 66 * written directly. An EA list may be in-lined in the inode if there is 67 * sufficient room available. 68 */ 69 70 struct ea_buffer { 71 int flag; /* Indicates what storage xattr points to */ 72 int max_size; /* largest xattr that fits in current buffer */ 73 dxd_t new_ea; /* dxd to replace ea when modifying xattr */ 74 struct metapage *mp; /* metapage containing ea list */ 75 struct jfs_ea_list *xattr; /* buffer containing ea list */ 76 }; 77 78 /* 79 * ea_buffer.flag values 80 */ 81 #define EA_INLINE 0x0001 82 #define EA_EXTENT 0x0002 83 #define EA_NEW 0x0004 84 #define EA_MALLOC 0x0008 85 86 87 /* 88 * These three routines are used to recognize on-disk extended attributes 89 * that are in a recognized namespace. If the attribute is not recognized, 90 * "os2." is prepended to the name 91 */ 92 static inline int is_os2_xattr(struct jfs_ea *ea) 93 { 94 /* 95 * Check for "system." 96 */ 97 if ((ea->namelen >= XATTR_SYSTEM_PREFIX_LEN) && 98 !strncmp(ea->name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 99 return FALSE; 100 /* 101 * Check for "user." 102 */ 103 if ((ea->namelen >= XATTR_USER_PREFIX_LEN) && 104 !strncmp(ea->name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) 105 return FALSE; 106 /* 107 * Check for "security." 108 */ 109 if ((ea->namelen >= XATTR_SECURITY_PREFIX_LEN) && 110 !strncmp(ea->name, XATTR_SECURITY_PREFIX, 111 XATTR_SECURITY_PREFIX_LEN)) 112 return FALSE; 113 /* 114 * Check for "trusted." 115 */ 116 if ((ea->namelen >= XATTR_TRUSTED_PREFIX_LEN) && 117 !strncmp(ea->name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) 118 return FALSE; 119 /* 120 * Add any other valid namespace prefixes here 121 */ 122 123 /* 124 * We assume it's OS/2's flat namespace 125 */ 126 return TRUE; 127 } 128 129 static inline int name_size(struct jfs_ea *ea) 130 { 131 if (is_os2_xattr(ea)) 132 return ea->namelen + XATTR_OS2_PREFIX_LEN; 133 else 134 return ea->namelen; 135 } 136 137 static inline int copy_name(char *buffer, struct jfs_ea *ea) 138 { 139 int len = ea->namelen; 140 141 if (is_os2_xattr(ea)) { 142 memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN); 143 buffer += XATTR_OS2_PREFIX_LEN; 144 len += XATTR_OS2_PREFIX_LEN; 145 } 146 memcpy(buffer, ea->name, ea->namelen); 147 buffer[ea->namelen] = 0; 148 149 return len; 150 } 151 152 /* Forward references */ 153 static void ea_release(struct inode *inode, struct ea_buffer *ea_buf); 154 155 /* 156 * NAME: ea_write_inline 157 * 158 * FUNCTION: Attempt to write an EA inline if area is available 159 * 160 * PRE CONDITIONS: 161 * Already verified that the specified EA is small enough to fit inline 162 * 163 * PARAMETERS: 164 * ip - Inode pointer 165 * ealist - EA list pointer 166 * size - size of ealist in bytes 167 * ea - dxd_t structure to be filled in with necessary EA information 168 * if we successfully copy the EA inline 169 * 170 * NOTES: 171 * Checks if the inode's inline area is available. If so, copies EA inline 172 * and sets <ea> fields appropriately. Otherwise, returns failure, EA will 173 * have to be put into an extent. 174 * 175 * RETURNS: 0 for successful copy to inline area; -1 if area not available 176 */ 177 static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist, 178 int size, dxd_t * ea) 179 { 180 struct jfs_inode_info *ji = JFS_IP(ip); 181 182 /* 183 * Make sure we have an EA -- the NULL EA list is valid, but you 184 * can't copy it! 185 */ 186 if (ealist && size > sizeof (struct jfs_ea_list)) { 187 assert(size <= sizeof (ji->i_inline_ea)); 188 189 /* 190 * See if the space is available or if it is already being 191 * used for an inline EA. 192 */ 193 if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE)) 194 return -EPERM; 195 196 DXDsize(ea, size); 197 DXDlength(ea, 0); 198 DXDaddress(ea, 0); 199 memcpy(ji->i_inline_ea, ealist, size); 200 ea->flag = DXD_INLINE; 201 ji->mode2 &= ~INLINEEA; 202 } else { 203 ea->flag = 0; 204 DXDsize(ea, 0); 205 DXDlength(ea, 0); 206 DXDaddress(ea, 0); 207 208 /* Free up INLINE area */ 209 if (ji->ea.flag & DXD_INLINE) 210 ji->mode2 |= INLINEEA; 211 } 212 213 return 0; 214 } 215 216 /* 217 * NAME: ea_write 218 * 219 * FUNCTION: Write an EA for an inode 220 * 221 * PRE CONDITIONS: EA has been verified 222 * 223 * PARAMETERS: 224 * ip - Inode pointer 225 * ealist - EA list pointer 226 * size - size of ealist in bytes 227 * ea - dxd_t structure to be filled in appropriately with where the 228 * EA was copied 229 * 230 * NOTES: Will write EA inline if able to, otherwise allocates blocks for an 231 * extent and synchronously writes it to those blocks. 232 * 233 * RETURNS: 0 for success; Anything else indicates failure 234 */ 235 static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size, 236 dxd_t * ea) 237 { 238 struct super_block *sb = ip->i_sb; 239 struct jfs_inode_info *ji = JFS_IP(ip); 240 struct jfs_sb_info *sbi = JFS_SBI(sb); 241 int nblocks; 242 s64 blkno; 243 int rc = 0, i; 244 char *cp; 245 s32 nbytes, nb; 246 s32 bytes_to_write; 247 struct metapage *mp; 248 249 /* 250 * Quick check to see if this is an in-linable EA. Short EAs 251 * and empty EAs are all in-linable, provided the space exists. 252 */ 253 if (!ealist || size <= sizeof (ji->i_inline_ea)) { 254 if (!ea_write_inline(ip, ealist, size, ea)) 255 return 0; 256 } 257 258 /* figure out how many blocks we need */ 259 nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits; 260 261 /* Allocate new blocks to quota. */ 262 if (DQUOT_ALLOC_BLOCK(ip, nblocks)) { 263 return -EDQUOT; 264 } 265 266 rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno); 267 if (rc) { 268 /*Rollback quota allocation. */ 269 DQUOT_FREE_BLOCK(ip, nblocks); 270 return rc; 271 } 272 273 /* 274 * Now have nblocks worth of storage to stuff into the FEALIST. 275 * loop over the FEALIST copying data into the buffer one page at 276 * a time. 277 */ 278 cp = (char *) ealist; 279 nbytes = size; 280 for (i = 0; i < nblocks; i += sbi->nbperpage) { 281 /* 282 * Determine how many bytes for this request, and round up to 283 * the nearest aggregate block size 284 */ 285 nb = min(PSIZE, nbytes); 286 bytes_to_write = 287 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits)) 288 << sb->s_blocksize_bits; 289 290 if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) { 291 rc = -EIO; 292 goto failed; 293 } 294 295 memcpy(mp->data, cp, nb); 296 297 /* 298 * We really need a way to propagate errors for 299 * forced writes like this one. --hch 300 * 301 * (__write_metapage => release_metapage => flush_metapage) 302 */ 303 #ifdef _JFS_FIXME 304 if ((rc = flush_metapage(mp))) { 305 /* 306 * the write failed -- this means that the buffer 307 * is still assigned and the blocks are not being 308 * used. this seems like the best error recovery 309 * we can get ... 310 */ 311 goto failed; 312 } 313 #else 314 flush_metapage(mp); 315 #endif 316 317 cp += PSIZE; 318 nbytes -= nb; 319 } 320 321 ea->flag = DXD_EXTENT; 322 DXDsize(ea, le32_to_cpu(ealist->size)); 323 DXDlength(ea, nblocks); 324 DXDaddress(ea, blkno); 325 326 /* Free up INLINE area */ 327 if (ji->ea.flag & DXD_INLINE) 328 ji->mode2 |= INLINEEA; 329 330 return 0; 331 332 failed: 333 /* Rollback quota allocation. */ 334 DQUOT_FREE_BLOCK(ip, nblocks); 335 336 dbFree(ip, blkno, nblocks); 337 return rc; 338 } 339 340 /* 341 * NAME: ea_read_inline 342 * 343 * FUNCTION: Read an inlined EA into user's buffer 344 * 345 * PARAMETERS: 346 * ip - Inode pointer 347 * ealist - Pointer to buffer to fill in with EA 348 * 349 * RETURNS: 0 350 */ 351 static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist) 352 { 353 struct jfs_inode_info *ji = JFS_IP(ip); 354 int ea_size = sizeDXD(&ji->ea); 355 356 if (ea_size == 0) { 357 ealist->size = 0; 358 return 0; 359 } 360 361 /* Sanity Check */ 362 if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea))) 363 return -EIO; 364 if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size) 365 != ea_size) 366 return -EIO; 367 368 memcpy(ealist, ji->i_inline_ea, ea_size); 369 return 0; 370 } 371 372 /* 373 * NAME: ea_read 374 * 375 * FUNCTION: copy EA data into user's buffer 376 * 377 * PARAMETERS: 378 * ip - Inode pointer 379 * ealist - Pointer to buffer to fill in with EA 380 * 381 * NOTES: If EA is inline calls ea_read_inline() to copy EA. 382 * 383 * RETURNS: 0 for success; other indicates failure 384 */ 385 static int ea_read(struct inode *ip, struct jfs_ea_list *ealist) 386 { 387 struct super_block *sb = ip->i_sb; 388 struct jfs_inode_info *ji = JFS_IP(ip); 389 struct jfs_sb_info *sbi = JFS_SBI(sb); 390 int nblocks; 391 s64 blkno; 392 char *cp = (char *) ealist; 393 int i; 394 int nbytes, nb; 395 s32 bytes_to_read; 396 struct metapage *mp; 397 398 /* quick check for in-line EA */ 399 if (ji->ea.flag & DXD_INLINE) 400 return ea_read_inline(ip, ealist); 401 402 nbytes = sizeDXD(&ji->ea); 403 if (!nbytes) { 404 jfs_error(sb, "ea_read: nbytes is 0"); 405 return -EIO; 406 } 407 408 /* 409 * Figure out how many blocks were allocated when this EA list was 410 * originally written to disk. 411 */ 412 nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage; 413 blkno = addressDXD(&ji->ea) << sbi->l2nbperpage; 414 415 /* 416 * I have found the disk blocks which were originally used to store 417 * the FEALIST. now i loop over each contiguous block copying the 418 * data into the buffer. 419 */ 420 for (i = 0; i < nblocks; i += sbi->nbperpage) { 421 /* 422 * Determine how many bytes for this request, and round up to 423 * the nearest aggregate block size 424 */ 425 nb = min(PSIZE, nbytes); 426 bytes_to_read = 427 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits)) 428 << sb->s_blocksize_bits; 429 430 if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1))) 431 return -EIO; 432 433 memcpy(cp, mp->data, nb); 434 release_metapage(mp); 435 436 cp += PSIZE; 437 nbytes -= nb; 438 } 439 440 return 0; 441 } 442 443 /* 444 * NAME: ea_get 445 * 446 * FUNCTION: Returns buffer containing existing extended attributes. 447 * The size of the buffer will be the larger of the existing 448 * attributes size, or min_size. 449 * 450 * The buffer, which may be inlined in the inode or in the 451 * page cache must be release by calling ea_release or ea_put 452 * 453 * PARAMETERS: 454 * inode - Inode pointer 455 * ea_buf - Structure to be populated with ealist and its metadata 456 * min_size- minimum size of buffer to be returned 457 * 458 * RETURNS: 0 for success; Other indicates failure 459 */ 460 static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size) 461 { 462 struct jfs_inode_info *ji = JFS_IP(inode); 463 struct super_block *sb = inode->i_sb; 464 int size; 465 int ea_size = sizeDXD(&ji->ea); 466 int blocks_needed, current_blocks; 467 s64 blkno; 468 int rc; 469 int quota_allocation = 0; 470 471 /* When fsck.jfs clears a bad ea, it doesn't clear the size */ 472 if (ji->ea.flag == 0) 473 ea_size = 0; 474 475 if (ea_size == 0) { 476 if (min_size == 0) { 477 ea_buf->flag = 0; 478 ea_buf->max_size = 0; 479 ea_buf->xattr = NULL; 480 return 0; 481 } 482 if ((min_size <= sizeof (ji->i_inline_ea)) && 483 (ji->mode2 & INLINEEA)) { 484 ea_buf->flag = EA_INLINE | EA_NEW; 485 ea_buf->max_size = sizeof (ji->i_inline_ea); 486 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea; 487 DXDlength(&ea_buf->new_ea, 0); 488 DXDaddress(&ea_buf->new_ea, 0); 489 ea_buf->new_ea.flag = DXD_INLINE; 490 DXDsize(&ea_buf->new_ea, min_size); 491 return 0; 492 } 493 current_blocks = 0; 494 } else if (ji->ea.flag & DXD_INLINE) { 495 if (min_size <= sizeof (ji->i_inline_ea)) { 496 ea_buf->flag = EA_INLINE; 497 ea_buf->max_size = sizeof (ji->i_inline_ea); 498 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea; 499 goto size_check; 500 } 501 current_blocks = 0; 502 } else { 503 if (!(ji->ea.flag & DXD_EXTENT)) { 504 jfs_error(sb, "ea_get: invalid ea.flag)"); 505 return -EIO; 506 } 507 current_blocks = (ea_size + sb->s_blocksize - 1) >> 508 sb->s_blocksize_bits; 509 } 510 size = max(min_size, ea_size); 511 512 if (size > PSIZE) { 513 /* 514 * To keep the rest of the code simple. Allocate a 515 * contiguous buffer to work with 516 */ 517 ea_buf->xattr = kmalloc(size, GFP_KERNEL); 518 if (ea_buf->xattr == NULL) 519 return -ENOMEM; 520 521 ea_buf->flag = EA_MALLOC; 522 ea_buf->max_size = (size + sb->s_blocksize - 1) & 523 ~(sb->s_blocksize - 1); 524 525 if (ea_size == 0) 526 return 0; 527 528 if ((rc = ea_read(inode, ea_buf->xattr))) { 529 kfree(ea_buf->xattr); 530 ea_buf->xattr = NULL; 531 return rc; 532 } 533 goto size_check; 534 } 535 blocks_needed = (min_size + sb->s_blocksize - 1) >> 536 sb->s_blocksize_bits; 537 538 if (blocks_needed > current_blocks) { 539 /* Allocate new blocks to quota. */ 540 if (DQUOT_ALLOC_BLOCK(inode, blocks_needed)) 541 return -EDQUOT; 542 543 quota_allocation = blocks_needed; 544 545 rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed, 546 &blkno); 547 if (rc) 548 goto clean_up; 549 550 DXDlength(&ea_buf->new_ea, blocks_needed); 551 DXDaddress(&ea_buf->new_ea, blkno); 552 ea_buf->new_ea.flag = DXD_EXTENT; 553 DXDsize(&ea_buf->new_ea, min_size); 554 555 ea_buf->flag = EA_EXTENT | EA_NEW; 556 557 ea_buf->mp = get_metapage(inode, blkno, 558 blocks_needed << sb->s_blocksize_bits, 559 1); 560 if (ea_buf->mp == NULL) { 561 dbFree(inode, blkno, (s64) blocks_needed); 562 rc = -EIO; 563 goto clean_up; 564 } 565 ea_buf->xattr = ea_buf->mp->data; 566 ea_buf->max_size = (min_size + sb->s_blocksize - 1) & 567 ~(sb->s_blocksize - 1); 568 if (ea_size == 0) 569 return 0; 570 if ((rc = ea_read(inode, ea_buf->xattr))) { 571 discard_metapage(ea_buf->mp); 572 dbFree(inode, blkno, (s64) blocks_needed); 573 goto clean_up; 574 } 575 goto size_check; 576 } 577 ea_buf->flag = EA_EXTENT; 578 ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea), 579 lengthDXD(&ji->ea) << sb->s_blocksize_bits, 580 1); 581 if (ea_buf->mp == NULL) { 582 rc = -EIO; 583 goto clean_up; 584 } 585 ea_buf->xattr = ea_buf->mp->data; 586 ea_buf->max_size = (ea_size + sb->s_blocksize - 1) & 587 ~(sb->s_blocksize - 1); 588 589 size_check: 590 if (EALIST_SIZE(ea_buf->xattr) != ea_size) { 591 printk(KERN_ERR "ea_get: invalid extended attribute\n"); 592 dump_mem("xattr", ea_buf->xattr, ea_size); 593 ea_release(inode, ea_buf); 594 rc = -EIO; 595 goto clean_up; 596 } 597 598 return ea_size; 599 600 clean_up: 601 /* Rollback quota allocation */ 602 if (quota_allocation) 603 DQUOT_FREE_BLOCK(inode, quota_allocation); 604 605 return (rc); 606 } 607 608 static void ea_release(struct inode *inode, struct ea_buffer *ea_buf) 609 { 610 if (ea_buf->flag & EA_MALLOC) 611 kfree(ea_buf->xattr); 612 else if (ea_buf->flag & EA_EXTENT) { 613 assert(ea_buf->mp); 614 release_metapage(ea_buf->mp); 615 616 if (ea_buf->flag & EA_NEW) 617 dbFree(inode, addressDXD(&ea_buf->new_ea), 618 lengthDXD(&ea_buf->new_ea)); 619 } 620 } 621 622 static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf, 623 int new_size) 624 { 625 struct jfs_inode_info *ji = JFS_IP(inode); 626 unsigned long old_blocks, new_blocks; 627 int rc = 0; 628 629 if (new_size == 0) { 630 ea_release(inode, ea_buf); 631 ea_buf = NULL; 632 } else if (ea_buf->flag & EA_INLINE) { 633 assert(new_size <= sizeof (ji->i_inline_ea)); 634 ji->mode2 &= ~INLINEEA; 635 ea_buf->new_ea.flag = DXD_INLINE; 636 DXDsize(&ea_buf->new_ea, new_size); 637 DXDaddress(&ea_buf->new_ea, 0); 638 DXDlength(&ea_buf->new_ea, 0); 639 } else if (ea_buf->flag & EA_MALLOC) { 640 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea); 641 kfree(ea_buf->xattr); 642 } else if (ea_buf->flag & EA_NEW) { 643 /* We have already allocated a new dxd */ 644 flush_metapage(ea_buf->mp); 645 } else { 646 /* ->xattr must point to original ea's metapage */ 647 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea); 648 discard_metapage(ea_buf->mp); 649 } 650 if (rc) 651 return rc; 652 653 old_blocks = new_blocks = 0; 654 655 if (ji->ea.flag & DXD_EXTENT) { 656 invalidate_dxd_metapages(inode, ji->ea); 657 old_blocks = lengthDXD(&ji->ea); 658 } 659 660 if (ea_buf) { 661 txEA(tid, inode, &ji->ea, &ea_buf->new_ea); 662 if (ea_buf->new_ea.flag & DXD_EXTENT) { 663 new_blocks = lengthDXD(&ea_buf->new_ea); 664 if (ji->ea.flag & DXD_INLINE) 665 ji->mode2 |= INLINEEA; 666 } 667 ji->ea = ea_buf->new_ea; 668 } else { 669 txEA(tid, inode, &ji->ea, NULL); 670 if (ji->ea.flag & DXD_INLINE) 671 ji->mode2 |= INLINEEA; 672 ji->ea.flag = 0; 673 ji->ea.size = 0; 674 } 675 676 /* If old blocks exist, they must be removed from quota allocation. */ 677 if (old_blocks) 678 DQUOT_FREE_BLOCK(inode, old_blocks); 679 680 inode->i_ctime = CURRENT_TIME; 681 682 return 0; 683 } 684 685 /* 686 * can_set_system_xattr 687 * 688 * This code is specific to the system.* namespace. It contains policy 689 * which doesn't belong in the main xattr codepath. 690 */ 691 static int can_set_system_xattr(struct inode *inode, const char *name, 692 const void *value, size_t value_len) 693 { 694 #ifdef CONFIG_JFS_POSIX_ACL 695 struct posix_acl *acl; 696 int rc; 697 698 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER)) 699 return -EPERM; 700 701 /* 702 * POSIX_ACL_XATTR_ACCESS is tied to i_mode 703 */ 704 if (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0) { 705 acl = posix_acl_from_xattr(value, value_len); 706 if (IS_ERR(acl)) { 707 rc = PTR_ERR(acl); 708 printk(KERN_ERR "posix_acl_from_xattr returned %d\n", 709 rc); 710 return rc; 711 } 712 if (acl) { 713 mode_t mode = inode->i_mode; 714 rc = posix_acl_equiv_mode(acl, &mode); 715 posix_acl_release(acl); 716 if (rc < 0) { 717 printk(KERN_ERR 718 "posix_acl_equiv_mode returned %d\n", 719 rc); 720 return rc; 721 } 722 inode->i_mode = mode; 723 mark_inode_dirty(inode); 724 } 725 /* 726 * We're changing the ACL. Get rid of the cached one 727 */ 728 acl =JFS_IP(inode)->i_acl; 729 if (acl != JFS_ACL_NOT_CACHED) 730 posix_acl_release(acl); 731 JFS_IP(inode)->i_acl = JFS_ACL_NOT_CACHED; 732 733 return 0; 734 } else if (strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0) { 735 acl = posix_acl_from_xattr(value, value_len); 736 if (IS_ERR(acl)) { 737 rc = PTR_ERR(acl); 738 printk(KERN_ERR "posix_acl_from_xattr returned %d\n", 739 rc); 740 return rc; 741 } 742 posix_acl_release(acl); 743 744 /* 745 * We're changing the default ACL. Get rid of the cached one 746 */ 747 acl =JFS_IP(inode)->i_default_acl; 748 if (acl && (acl != JFS_ACL_NOT_CACHED)) 749 posix_acl_release(acl); 750 JFS_IP(inode)->i_default_acl = JFS_ACL_NOT_CACHED; 751 752 return 0; 753 } 754 #endif /* CONFIG_JFS_POSIX_ACL */ 755 return -EOPNOTSUPP; 756 } 757 758 static int can_set_xattr(struct inode *inode, const char *name, 759 const void *value, size_t value_len) 760 { 761 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 762 return can_set_system_xattr(inode, name, value, value_len); 763 764 /* 765 * Don't allow setting an attribute in an unknown namespace. 766 */ 767 if (strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) && 768 strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) && 769 strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) && 770 strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN)) 771 return -EOPNOTSUPP; 772 773 if (!S_ISREG(inode->i_mode) && 774 (!S_ISDIR(inode->i_mode) || inode->i_mode &S_ISVTX)) 775 return -EPERM; 776 777 return 0; 778 } 779 780 int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name, 781 const void *value, size_t value_len, int flags) 782 { 783 struct jfs_ea_list *ealist; 784 struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL; 785 struct ea_buffer ea_buf; 786 int old_ea_size = 0; 787 int xattr_size; 788 int new_size; 789 int namelen = strlen(name); 790 char *os2name = NULL; 791 int found = 0; 792 int rc; 793 int length; 794 795 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) { 796 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1, 797 GFP_KERNEL); 798 if (!os2name) 799 return -ENOMEM; 800 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN); 801 name = os2name; 802 namelen -= XATTR_OS2_PREFIX_LEN; 803 } 804 805 down_write(&JFS_IP(inode)->xattr_sem); 806 807 xattr_size = ea_get(inode, &ea_buf, 0); 808 if (xattr_size < 0) { 809 rc = xattr_size; 810 goto out; 811 } 812 813 again: 814 ealist = (struct jfs_ea_list *) ea_buf.xattr; 815 new_size = sizeof (struct jfs_ea_list); 816 817 if (xattr_size) { 818 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); 819 ea = NEXT_EA(ea)) { 820 if ((namelen == ea->namelen) && 821 (memcmp(name, ea->name, namelen) == 0)) { 822 found = 1; 823 if (flags & XATTR_CREATE) { 824 rc = -EEXIST; 825 goto release; 826 } 827 old_ea = ea; 828 old_ea_size = EA_SIZE(ea); 829 next_ea = NEXT_EA(ea); 830 } else 831 new_size += EA_SIZE(ea); 832 } 833 } 834 835 if (!found) { 836 if (flags & XATTR_REPLACE) { 837 rc = -ENODATA; 838 goto release; 839 } 840 if (value == NULL) { 841 rc = 0; 842 goto release; 843 } 844 } 845 if (value) 846 new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len; 847 848 if (new_size > ea_buf.max_size) { 849 /* 850 * We need to allocate more space for merged ea list. 851 * We should only have loop to again: once. 852 */ 853 ea_release(inode, &ea_buf); 854 xattr_size = ea_get(inode, &ea_buf, new_size); 855 if (xattr_size < 0) { 856 rc = xattr_size; 857 goto out; 858 } 859 goto again; 860 } 861 862 /* Remove old ea of the same name */ 863 if (found) { 864 /* number of bytes following target EA */ 865 length = (char *) END_EALIST(ealist) - (char *) next_ea; 866 if (length > 0) 867 memmove(old_ea, next_ea, length); 868 xattr_size -= old_ea_size; 869 } 870 871 /* Add new entry to the end */ 872 if (value) { 873 if (xattr_size == 0) 874 /* Completely new ea list */ 875 xattr_size = sizeof (struct jfs_ea_list); 876 877 ea = (struct jfs_ea *) ((char *) ealist + xattr_size); 878 ea->flag = 0; 879 ea->namelen = namelen; 880 ea->valuelen = (cpu_to_le16(value_len)); 881 memcpy(ea->name, name, namelen); 882 ea->name[namelen] = 0; 883 if (value_len) 884 memcpy(&ea->name[namelen + 1], value, value_len); 885 xattr_size += EA_SIZE(ea); 886 } 887 888 /* DEBUG - If we did this right, these number match */ 889 if (xattr_size != new_size) { 890 printk(KERN_ERR 891 "jfs_xsetattr: xattr_size = %d, new_size = %d\n", 892 xattr_size, new_size); 893 894 rc = -EINVAL; 895 goto release; 896 } 897 898 /* 899 * If we're left with an empty list, there's no ea 900 */ 901 if (new_size == sizeof (struct jfs_ea_list)) 902 new_size = 0; 903 904 ealist->size = cpu_to_le32(new_size); 905 906 rc = ea_put(tid, inode, &ea_buf, new_size); 907 908 goto out; 909 release: 910 ea_release(inode, &ea_buf); 911 out: 912 up_write(&JFS_IP(inode)->xattr_sem); 913 914 kfree(os2name); 915 916 return rc; 917 } 918 919 int jfs_setxattr(struct dentry *dentry, const char *name, const void *value, 920 size_t value_len, int flags) 921 { 922 struct inode *inode = dentry->d_inode; 923 struct jfs_inode_info *ji = JFS_IP(inode); 924 int rc; 925 tid_t tid; 926 927 if ((rc = can_set_xattr(inode, name, value, value_len))) 928 return rc; 929 930 if (value == NULL) { /* empty EA, do not remove */ 931 value = ""; 932 value_len = 0; 933 } 934 935 tid = txBegin(inode->i_sb, 0); 936 down(&ji->commit_sem); 937 rc = __jfs_setxattr(tid, dentry->d_inode, name, value, value_len, 938 flags); 939 if (!rc) 940 rc = txCommit(tid, 1, &inode, 0); 941 txEnd(tid); 942 up(&ji->commit_sem); 943 944 return rc; 945 } 946 947 ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data, 948 size_t buf_size) 949 { 950 struct jfs_ea_list *ealist; 951 struct jfs_ea *ea; 952 struct ea_buffer ea_buf; 953 int xattr_size; 954 ssize_t size; 955 int namelen = strlen(name); 956 char *os2name = NULL; 957 char *value; 958 959 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) { 960 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1, 961 GFP_KERNEL); 962 if (!os2name) 963 return -ENOMEM; 964 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN); 965 name = os2name; 966 namelen -= XATTR_OS2_PREFIX_LEN; 967 } 968 969 down_read(&JFS_IP(inode)->xattr_sem); 970 971 xattr_size = ea_get(inode, &ea_buf, 0); 972 973 if (xattr_size < 0) { 974 size = xattr_size; 975 goto out; 976 } 977 978 if (xattr_size == 0) 979 goto not_found; 980 981 ealist = (struct jfs_ea_list *) ea_buf.xattr; 982 983 /* Find the named attribute */ 984 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) 985 if ((namelen == ea->namelen) && 986 memcmp(name, ea->name, namelen) == 0) { 987 /* Found it */ 988 size = le16_to_cpu(ea->valuelen); 989 if (!data) 990 goto release; 991 else if (size > buf_size) { 992 size = -ERANGE; 993 goto release; 994 } 995 value = ((char *) &ea->name) + ea->namelen + 1; 996 memcpy(data, value, size); 997 goto release; 998 } 999 not_found: 1000 size = -ENODATA; 1001 release: 1002 ea_release(inode, &ea_buf); 1003 out: 1004 up_read(&JFS_IP(inode)->xattr_sem); 1005 1006 kfree(os2name); 1007 1008 return size; 1009 } 1010 1011 ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data, 1012 size_t buf_size) 1013 { 1014 int err; 1015 1016 err = __jfs_getxattr(dentry->d_inode, name, data, buf_size); 1017 1018 return err; 1019 } 1020 1021 /* 1022 * No special permissions are needed to list attributes except for trusted.* 1023 */ 1024 static inline int can_list(struct jfs_ea *ea) 1025 { 1026 return (strncmp(ea->name, XATTR_TRUSTED_PREFIX, 1027 XATTR_TRUSTED_PREFIX_LEN) || 1028 capable(CAP_SYS_ADMIN)); 1029 } 1030 1031 ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size) 1032 { 1033 struct inode *inode = dentry->d_inode; 1034 char *buffer; 1035 ssize_t size = 0; 1036 int xattr_size; 1037 struct jfs_ea_list *ealist; 1038 struct jfs_ea *ea; 1039 struct ea_buffer ea_buf; 1040 1041 down_read(&JFS_IP(inode)->xattr_sem); 1042 1043 xattr_size = ea_get(inode, &ea_buf, 0); 1044 if (xattr_size < 0) { 1045 size = xattr_size; 1046 goto out; 1047 } 1048 1049 if (xattr_size == 0) 1050 goto release; 1051 1052 ealist = (struct jfs_ea_list *) ea_buf.xattr; 1053 1054 /* compute required size of list */ 1055 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) { 1056 if (can_list(ea)) 1057 size += name_size(ea) + 1; 1058 } 1059 1060 if (!data) 1061 goto release; 1062 1063 if (size > buf_size) { 1064 size = -ERANGE; 1065 goto release; 1066 } 1067 1068 /* Copy attribute names to buffer */ 1069 buffer = data; 1070 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) { 1071 if (can_list(ea)) { 1072 int namelen = copy_name(buffer, ea); 1073 buffer += namelen + 1; 1074 } 1075 } 1076 1077 release: 1078 ea_release(inode, &ea_buf); 1079 out: 1080 up_read(&JFS_IP(inode)->xattr_sem); 1081 return size; 1082 } 1083 1084 int jfs_removexattr(struct dentry *dentry, const char *name) 1085 { 1086 struct inode *inode = dentry->d_inode; 1087 struct jfs_inode_info *ji = JFS_IP(inode); 1088 int rc; 1089 tid_t tid; 1090 1091 if ((rc = can_set_xattr(inode, name, NULL, 0))) 1092 return rc; 1093 1094 tid = txBegin(inode->i_sb, 0); 1095 down(&ji->commit_sem); 1096 rc = __jfs_setxattr(tid, dentry->d_inode, name, NULL, 0, XATTR_REPLACE); 1097 if (!rc) 1098 rc = txCommit(tid, 1, &inode, 0); 1099 txEnd(tid); 1100 up(&ji->commit_sem); 1101 1102 return rc; 1103 } 1104 1105 #ifdef CONFIG_JFS_SECURITY 1106 int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir) 1107 { 1108 int rc; 1109 size_t len; 1110 void *value; 1111 char *suffix; 1112 char *name; 1113 1114 rc = security_inode_init_security(inode, dir, &suffix, &value, &len); 1115 if (rc) { 1116 if (rc == -EOPNOTSUPP) 1117 return 0; 1118 return rc; 1119 } 1120 name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 1 + strlen(suffix), 1121 GFP_NOFS); 1122 if (!name) { 1123 rc = -ENOMEM; 1124 goto kmalloc_failed; 1125 } 1126 strcpy(name, XATTR_SECURITY_PREFIX); 1127 strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix); 1128 1129 rc = __jfs_setxattr(tid, inode, name, value, len, 0); 1130 1131 kfree(name); 1132 kmalloc_failed: 1133 kfree(suffix); 1134 kfree(value); 1135 1136 return rc; 1137 } 1138 #endif 1139