1 /* 2 * dir.c - Operations for sysfs directories. 3 */ 4 5 #undef DEBUG 6 7 #include <linux/fs.h> 8 #include <linux/mount.h> 9 #include <linux/module.h> 10 #include <linux/kobject.h> 11 #include <linux/namei.h> 12 #include <linux/idr.h> 13 #include <asm/semaphore.h> 14 #include "sysfs.h" 15 16 DECLARE_RWSEM(sysfs_rename_sem); 17 spinlock_t sysfs_lock = SPIN_LOCK_UNLOCKED; 18 19 static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED; 20 static DEFINE_IDA(sysfs_ino_ida); 21 22 int sysfs_alloc_ino(ino_t *pino) 23 { 24 int ino, rc; 25 26 retry: 27 spin_lock(&sysfs_ino_lock); 28 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino); 29 spin_unlock(&sysfs_ino_lock); 30 31 if (rc == -EAGAIN) { 32 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL)) 33 goto retry; 34 rc = -ENOMEM; 35 } 36 37 *pino = ino; 38 return rc; 39 } 40 41 static void sysfs_free_ino(ino_t ino) 42 { 43 spin_lock(&sysfs_ino_lock); 44 ida_remove(&sysfs_ino_ida, ino); 45 spin_unlock(&sysfs_ino_lock); 46 } 47 48 void release_sysfs_dirent(struct sysfs_dirent * sd) 49 { 50 if (sd->s_type & SYSFS_KOBJ_LINK) { 51 struct sysfs_symlink * sl = sd->s_element; 52 kfree(sl->link_name); 53 kobject_put(sl->target_kobj); 54 kfree(sl); 55 } 56 kfree(sd->s_iattr); 57 sysfs_free_ino(sd->s_ino); 58 kmem_cache_free(sysfs_dir_cachep, sd); 59 } 60 61 static void sysfs_d_iput(struct dentry * dentry, struct inode * inode) 62 { 63 struct sysfs_dirent * sd = dentry->d_fsdata; 64 65 if (sd) { 66 /* sd->s_dentry is protected with sysfs_lock. This 67 * allows sysfs_drop_dentry() to dereference it. 68 */ 69 spin_lock(&sysfs_lock); 70 71 /* The dentry might have been deleted or another 72 * lookup could have happened updating sd->s_dentry to 73 * point the new dentry. Ignore if it isn't pointing 74 * to this dentry. 75 */ 76 if (sd->s_dentry == dentry) 77 sd->s_dentry = NULL; 78 spin_unlock(&sysfs_lock); 79 sysfs_put(sd); 80 } 81 iput(inode); 82 } 83 84 static struct dentry_operations sysfs_dentry_ops = { 85 .d_iput = sysfs_d_iput, 86 }; 87 88 struct sysfs_dirent *sysfs_new_dirent(void *element, umode_t mode, int type) 89 { 90 struct sysfs_dirent * sd; 91 92 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL); 93 if (!sd) 94 return NULL; 95 96 if (sysfs_alloc_ino(&sd->s_ino)) { 97 kmem_cache_free(sysfs_dir_cachep, sd); 98 return NULL; 99 } 100 101 atomic_set(&sd->s_count, 1); 102 atomic_set(&sd->s_event, 1); 103 INIT_LIST_HEAD(&sd->s_children); 104 INIT_LIST_HEAD(&sd->s_sibling); 105 106 sd->s_element = element; 107 sd->s_mode = mode; 108 sd->s_type = type; 109 110 return sd; 111 } 112 113 void sysfs_attach_dirent(struct sysfs_dirent *sd, 114 struct sysfs_dirent *parent_sd, struct dentry *dentry) 115 { 116 if (dentry) { 117 sd->s_dentry = dentry; 118 dentry->d_fsdata = sysfs_get(sd); 119 dentry->d_op = &sysfs_dentry_ops; 120 } 121 122 if (parent_sd) 123 list_add(&sd->s_sibling, &parent_sd->s_children); 124 } 125 126 /* 127 * 128 * Return -EEXIST if there is already a sysfs element with the same name for 129 * the same parent. 130 * 131 * called with parent inode's i_mutex held 132 */ 133 int sysfs_dirent_exist(struct sysfs_dirent *parent_sd, 134 const unsigned char *new) 135 { 136 struct sysfs_dirent * sd; 137 138 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { 139 if (sd->s_element) { 140 const unsigned char *existing = sysfs_get_name(sd); 141 if (strcmp(existing, new)) 142 continue; 143 else 144 return -EEXIST; 145 } 146 } 147 148 return 0; 149 } 150 151 static int init_dir(struct inode * inode) 152 { 153 inode->i_op = &sysfs_dir_inode_operations; 154 inode->i_fop = &sysfs_dir_operations; 155 156 /* directory inodes start off with i_nlink == 2 (for "." entry) */ 157 inc_nlink(inode); 158 return 0; 159 } 160 161 static int init_file(struct inode * inode) 162 { 163 inode->i_size = PAGE_SIZE; 164 inode->i_fop = &sysfs_file_operations; 165 return 0; 166 } 167 168 static int init_symlink(struct inode * inode) 169 { 170 inode->i_op = &sysfs_symlink_inode_operations; 171 return 0; 172 } 173 174 static int create_dir(struct kobject *kobj, struct dentry *parent, 175 const char *name, struct dentry **p_dentry) 176 { 177 int error; 178 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; 179 struct dentry *dentry; 180 struct sysfs_dirent *sd; 181 182 mutex_lock(&parent->d_inode->i_mutex); 183 184 dentry = lookup_one_len(name, parent, strlen(name)); 185 if (IS_ERR(dentry)) { 186 error = PTR_ERR(dentry); 187 goto out_unlock; 188 } 189 190 error = -EEXIST; 191 if (sysfs_dirent_exist(parent->d_fsdata, name)) 192 goto out_dput; 193 194 error = -ENOMEM; 195 sd = sysfs_new_dirent(kobj, mode, SYSFS_DIR); 196 if (!sd) 197 goto out_drop; 198 sysfs_attach_dirent(sd, parent->d_fsdata, dentry); 199 200 error = sysfs_create(dentry, mode, init_dir); 201 if (error) 202 goto out_sput; 203 204 inc_nlink(parent->d_inode); 205 dentry->d_op = &sysfs_dentry_ops; 206 d_rehash(dentry); 207 208 *p_dentry = dentry; 209 error = 0; 210 goto out_dput; 211 212 out_sput: 213 list_del_init(&sd->s_sibling); 214 sysfs_put(sd); 215 out_drop: 216 d_drop(dentry); 217 out_dput: 218 dput(dentry); 219 out_unlock: 220 mutex_unlock(&parent->d_inode->i_mutex); 221 return error; 222 } 223 224 225 int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d) 226 { 227 return create_dir(k,k->dentry,n,d); 228 } 229 230 /** 231 * sysfs_create_dir - create a directory for an object. 232 * @kobj: object we're creating directory for. 233 * @shadow_parent: parent parent object. 234 */ 235 236 int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent) 237 { 238 struct dentry * dentry = NULL; 239 struct dentry * parent; 240 int error = 0; 241 242 BUG_ON(!kobj); 243 244 if (shadow_parent) 245 parent = shadow_parent; 246 else if (kobj->parent) 247 parent = kobj->parent->dentry; 248 else if (sysfs_mount && sysfs_mount->mnt_sb) 249 parent = sysfs_mount->mnt_sb->s_root; 250 else 251 return -EFAULT; 252 253 error = create_dir(kobj,parent,kobject_name(kobj),&dentry); 254 if (!error) 255 kobj->dentry = dentry; 256 return error; 257 } 258 259 /* attaches attribute's sysfs_dirent to the dentry corresponding to the 260 * attribute file 261 */ 262 static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry) 263 { 264 struct attribute * attr = NULL; 265 struct bin_attribute * bin_attr = NULL; 266 int (* init) (struct inode *) = NULL; 267 int error = 0; 268 269 if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) { 270 bin_attr = sd->s_element; 271 attr = &bin_attr->attr; 272 } else { 273 attr = sd->s_element; 274 init = init_file; 275 } 276 277 dentry->d_fsdata = sysfs_get(sd); 278 /* protect sd->s_dentry against sysfs_d_iput */ 279 spin_lock(&sysfs_lock); 280 sd->s_dentry = dentry; 281 spin_unlock(&sysfs_lock); 282 error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init); 283 if (error) { 284 sysfs_put(sd); 285 return error; 286 } 287 288 if (bin_attr) { 289 dentry->d_inode->i_size = bin_attr->size; 290 dentry->d_inode->i_fop = &bin_fops; 291 } 292 dentry->d_op = &sysfs_dentry_ops; 293 d_rehash(dentry); 294 295 return 0; 296 } 297 298 static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry) 299 { 300 int err = 0; 301 302 dentry->d_fsdata = sysfs_get(sd); 303 /* protect sd->s_dentry against sysfs_d_iput */ 304 spin_lock(&sysfs_lock); 305 sd->s_dentry = dentry; 306 spin_unlock(&sysfs_lock); 307 err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink); 308 if (!err) { 309 dentry->d_op = &sysfs_dentry_ops; 310 d_rehash(dentry); 311 } else 312 sysfs_put(sd); 313 314 return err; 315 } 316 317 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry, 318 struct nameidata *nd) 319 { 320 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata; 321 struct sysfs_dirent * sd; 322 int err = 0; 323 324 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { 325 if (sd->s_type & SYSFS_NOT_PINNED) { 326 const unsigned char * name = sysfs_get_name(sd); 327 328 if (strcmp(name, dentry->d_name.name)) 329 continue; 330 331 if (sd->s_type & SYSFS_KOBJ_LINK) 332 err = sysfs_attach_link(sd, dentry); 333 else 334 err = sysfs_attach_attr(sd, dentry); 335 break; 336 } 337 } 338 339 return ERR_PTR(err); 340 } 341 342 const struct inode_operations sysfs_dir_inode_operations = { 343 .lookup = sysfs_lookup, 344 .setattr = sysfs_setattr, 345 }; 346 347 static void remove_dir(struct dentry * d) 348 { 349 struct dentry * parent = dget(d->d_parent); 350 struct sysfs_dirent * sd; 351 352 mutex_lock(&parent->d_inode->i_mutex); 353 d_delete(d); 354 sd = d->d_fsdata; 355 list_del_init(&sd->s_sibling); 356 sysfs_put(sd); 357 if (d->d_inode) 358 simple_rmdir(parent->d_inode,d); 359 360 pr_debug(" o %s removing done (%d)\n",d->d_name.name, 361 atomic_read(&d->d_count)); 362 363 mutex_unlock(&parent->d_inode->i_mutex); 364 dput(parent); 365 } 366 367 void sysfs_remove_subdir(struct dentry * d) 368 { 369 remove_dir(d); 370 } 371 372 373 static void __sysfs_remove_dir(struct dentry *dentry) 374 { 375 struct sysfs_dirent * parent_sd; 376 struct sysfs_dirent * sd, * tmp; 377 378 dget(dentry); 379 if (!dentry) 380 return; 381 382 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name); 383 mutex_lock(&dentry->d_inode->i_mutex); 384 parent_sd = dentry->d_fsdata; 385 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) { 386 if (!sd->s_element || !(sd->s_type & SYSFS_NOT_PINNED)) 387 continue; 388 list_del_init(&sd->s_sibling); 389 sysfs_drop_dentry(sd, dentry); 390 sysfs_put(sd); 391 } 392 mutex_unlock(&dentry->d_inode->i_mutex); 393 394 remove_dir(dentry); 395 /** 396 * Drop reference from dget() on entrance. 397 */ 398 dput(dentry); 399 } 400 401 /** 402 * sysfs_remove_dir - remove an object's directory. 403 * @kobj: object. 404 * 405 * The only thing special about this is that we remove any files in 406 * the directory before we remove the directory, and we've inlined 407 * what used to be sysfs_rmdir() below, instead of calling separately. 408 */ 409 410 void sysfs_remove_dir(struct kobject * kobj) 411 { 412 __sysfs_remove_dir(kobj->dentry); 413 kobj->dentry = NULL; 414 } 415 416 int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent, 417 const char *new_name) 418 { 419 int error; 420 struct dentry * new_dentry; 421 struct sysfs_dirent *sd, *parent_sd; 422 423 if (!new_parent) 424 return -EFAULT; 425 426 down_write(&sysfs_rename_sem); 427 mutex_lock(&new_parent->d_inode->i_mutex); 428 429 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name)); 430 if (IS_ERR(new_dentry)) { 431 error = PTR_ERR(new_dentry); 432 goto out_unlock; 433 } 434 435 /* By allowing two different directories with the same 436 * d_parent we allow this routine to move between different 437 * shadows of the same directory 438 */ 439 error = -EINVAL; 440 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode || 441 new_dentry->d_parent->d_inode != new_parent->d_inode || 442 new_dentry == kobj->dentry) 443 goto out_dput; 444 445 error = -EEXIST; 446 if (new_dentry->d_inode) 447 goto out_dput; 448 449 error = kobject_set_name(kobj, "%s", new_name); 450 if (error) 451 goto out_drop; 452 453 d_add(new_dentry, NULL); 454 d_move(kobj->dentry, new_dentry); 455 456 sd = kobj->dentry->d_fsdata; 457 parent_sd = new_parent->d_fsdata; 458 459 list_del_init(&sd->s_sibling); 460 list_add(&sd->s_sibling, &parent_sd->s_children); 461 462 error = 0; 463 goto out_unlock; 464 465 out_drop: 466 d_drop(new_dentry); 467 out_dput: 468 dput(new_dentry); 469 out_unlock: 470 mutex_unlock(&new_parent->d_inode->i_mutex); 471 up_write(&sysfs_rename_sem); 472 return error; 473 } 474 475 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent) 476 { 477 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry; 478 struct sysfs_dirent *new_parent_sd, *sd; 479 int error; 480 481 old_parent_dentry = kobj->parent ? 482 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root; 483 new_parent_dentry = new_parent ? 484 new_parent->dentry : sysfs_mount->mnt_sb->s_root; 485 486 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode) 487 return 0; /* nothing to move */ 488 again: 489 mutex_lock(&old_parent_dentry->d_inode->i_mutex); 490 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) { 491 mutex_unlock(&old_parent_dentry->d_inode->i_mutex); 492 goto again; 493 } 494 495 new_parent_sd = new_parent_dentry->d_fsdata; 496 sd = kobj->dentry->d_fsdata; 497 498 new_dentry = lookup_one_len(kobj->name, new_parent_dentry, 499 strlen(kobj->name)); 500 if (IS_ERR(new_dentry)) { 501 error = PTR_ERR(new_dentry); 502 goto out; 503 } else 504 error = 0; 505 d_add(new_dentry, NULL); 506 d_move(kobj->dentry, new_dentry); 507 dput(new_dentry); 508 509 /* Remove from old parent's list and insert into new parent's list. */ 510 list_del_init(&sd->s_sibling); 511 list_add(&sd->s_sibling, &new_parent_sd->s_children); 512 513 out: 514 mutex_unlock(&new_parent_dentry->d_inode->i_mutex); 515 mutex_unlock(&old_parent_dentry->d_inode->i_mutex); 516 517 return error; 518 } 519 520 static int sysfs_dir_open(struct inode *inode, struct file *file) 521 { 522 struct dentry * dentry = file->f_path.dentry; 523 struct sysfs_dirent * parent_sd = dentry->d_fsdata; 524 struct sysfs_dirent * sd; 525 526 mutex_lock(&dentry->d_inode->i_mutex); 527 sd = sysfs_new_dirent(NULL, 0, 0); 528 if (sd) 529 sysfs_attach_dirent(sd, parent_sd, NULL); 530 mutex_unlock(&dentry->d_inode->i_mutex); 531 532 file->private_data = sd; 533 return sd ? 0 : -ENOMEM; 534 } 535 536 static int sysfs_dir_close(struct inode *inode, struct file *file) 537 { 538 struct dentry * dentry = file->f_path.dentry; 539 struct sysfs_dirent * cursor = file->private_data; 540 541 mutex_lock(&dentry->d_inode->i_mutex); 542 list_del_init(&cursor->s_sibling); 543 mutex_unlock(&dentry->d_inode->i_mutex); 544 545 release_sysfs_dirent(cursor); 546 547 return 0; 548 } 549 550 /* Relationship between s_mode and the DT_xxx types */ 551 static inline unsigned char dt_type(struct sysfs_dirent *sd) 552 { 553 return (sd->s_mode >> 12) & 15; 554 } 555 556 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir) 557 { 558 struct dentry *dentry = filp->f_path.dentry; 559 struct sysfs_dirent * parent_sd = dentry->d_fsdata; 560 struct sysfs_dirent *cursor = filp->private_data; 561 struct list_head *p, *q = &cursor->s_sibling; 562 ino_t ino; 563 int i = filp->f_pos; 564 565 switch (i) { 566 case 0: 567 ino = parent_sd->s_ino; 568 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 569 break; 570 filp->f_pos++; 571 i++; 572 /* fallthrough */ 573 case 1: 574 ino = parent_ino(dentry); 575 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 576 break; 577 filp->f_pos++; 578 i++; 579 /* fallthrough */ 580 default: 581 if (filp->f_pos == 2) 582 list_move(q, &parent_sd->s_children); 583 584 for (p=q->next; p!= &parent_sd->s_children; p=p->next) { 585 struct sysfs_dirent *next; 586 const char * name; 587 int len; 588 589 next = list_entry(p, struct sysfs_dirent, 590 s_sibling); 591 if (!next->s_element) 592 continue; 593 594 name = sysfs_get_name(next); 595 len = strlen(name); 596 ino = next->s_ino; 597 598 if (filldir(dirent, name, len, filp->f_pos, ino, 599 dt_type(next)) < 0) 600 return 0; 601 602 list_move(q, p); 603 p = q; 604 filp->f_pos++; 605 } 606 } 607 return 0; 608 } 609 610 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin) 611 { 612 struct dentry * dentry = file->f_path.dentry; 613 614 mutex_lock(&dentry->d_inode->i_mutex); 615 switch (origin) { 616 case 1: 617 offset += file->f_pos; 618 case 0: 619 if (offset >= 0) 620 break; 621 default: 622 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex); 623 return -EINVAL; 624 } 625 if (offset != file->f_pos) { 626 file->f_pos = offset; 627 if (file->f_pos >= 2) { 628 struct sysfs_dirent *sd = dentry->d_fsdata; 629 struct sysfs_dirent *cursor = file->private_data; 630 struct list_head *p; 631 loff_t n = file->f_pos - 2; 632 633 list_del(&cursor->s_sibling); 634 p = sd->s_children.next; 635 while (n && p != &sd->s_children) { 636 struct sysfs_dirent *next; 637 next = list_entry(p, struct sysfs_dirent, 638 s_sibling); 639 if (next->s_element) 640 n--; 641 p = p->next; 642 } 643 list_add_tail(&cursor->s_sibling, p); 644 } 645 } 646 mutex_unlock(&dentry->d_inode->i_mutex); 647 return offset; 648 } 649 650 651 /** 652 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed 653 * @kobj: object we're creating shadow of. 654 */ 655 656 int sysfs_make_shadowed_dir(struct kobject *kobj, 657 void * (*follow_link)(struct dentry *, struct nameidata *)) 658 { 659 struct inode *inode; 660 struct inode_operations *i_op; 661 662 inode = kobj->dentry->d_inode; 663 if (inode->i_op != &sysfs_dir_inode_operations) 664 return -EINVAL; 665 666 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL); 667 if (!i_op) 668 return -ENOMEM; 669 670 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op)); 671 i_op->follow_link = follow_link; 672 673 /* Locking of inode->i_op? 674 * Since setting i_op is a single word write and they 675 * are atomic we should be ok here. 676 */ 677 inode->i_op = i_op; 678 return 0; 679 } 680 681 /** 682 * sysfs_create_shadow_dir - create a shadow directory for an object. 683 * @kobj: object we're creating directory for. 684 * 685 * sysfs_make_shadowed_dir must already have been called on this 686 * directory. 687 */ 688 689 struct dentry *sysfs_create_shadow_dir(struct kobject *kobj) 690 { 691 struct sysfs_dirent *sd; 692 struct dentry *parent, *dir, *shadow; 693 struct inode *inode; 694 695 dir = kobj->dentry; 696 inode = dir->d_inode; 697 parent = dir->d_parent; 698 shadow = ERR_PTR(-EINVAL); 699 if (!sysfs_is_shadowed_inode(inode)) 700 goto out; 701 702 shadow = d_alloc(parent, &dir->d_name); 703 if (!shadow) 704 goto nomem; 705 706 sd = sysfs_new_dirent(kobj, inode->i_mode, SYSFS_DIR); 707 if (!sd) 708 goto nomem; 709 sysfs_attach_dirent(sd, NULL, shadow); 710 711 d_instantiate(shadow, igrab(inode)); 712 inc_nlink(inode); 713 inc_nlink(parent->d_inode); 714 shadow->d_op = &sysfs_dentry_ops; 715 716 dget(shadow); /* Extra count - pin the dentry in core */ 717 718 out: 719 return shadow; 720 nomem: 721 dput(shadow); 722 shadow = ERR_PTR(-ENOMEM); 723 goto out; 724 } 725 726 /** 727 * sysfs_remove_shadow_dir - remove an object's directory. 728 * @shadow: dentry of shadow directory 729 * 730 * The only thing special about this is that we remove any files in 731 * the directory before we remove the directory, and we've inlined 732 * what used to be sysfs_rmdir() below, instead of calling separately. 733 */ 734 735 void sysfs_remove_shadow_dir(struct dentry *shadow) 736 { 737 __sysfs_remove_dir(shadow); 738 } 739 740 const struct file_operations sysfs_dir_operations = { 741 .open = sysfs_dir_open, 742 .release = sysfs_dir_close, 743 .llseek = sysfs_dir_lseek, 744 .read = generic_read_dir, 745 .readdir = sysfs_readdir, 746 }; 747