inode.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * inode.c - basic inode and dentry operations.
  3. *
  4. * sysfs is Copyright (c) 2001-3 Patrick Mochel
  5. *
  6. * Please see Documentation/filesystems/sysfs.txt for more information.
  7. */
  8. #undef DEBUG
  9. #include <linux/pagemap.h>
  10. #include <linux/namei.h>
  11. #include <linux/backing-dev.h>
  12. #include "sysfs.h"
  13. extern struct super_block * sysfs_sb;
  14. static struct address_space_operations sysfs_aops = {
  15. .readpage = simple_readpage,
  16. .prepare_write = simple_prepare_write,
  17. .commit_write = simple_commit_write
  18. };
  19. static struct backing_dev_info sysfs_backing_dev_info = {
  20. .ra_pages = 0, /* No readahead */
  21. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
  22. };
  23. struct inode * sysfs_new_inode(mode_t mode)
  24. {
  25. struct inode * inode = new_inode(sysfs_sb);
  26. if (inode) {
  27. inode->i_mode = mode;
  28. inode->i_uid = 0;
  29. inode->i_gid = 0;
  30. inode->i_blksize = PAGE_CACHE_SIZE;
  31. inode->i_blocks = 0;
  32. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  33. inode->i_mapping->a_ops = &sysfs_aops;
  34. inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
  35. }
  36. return inode;
  37. }
  38. int sysfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *))
  39. {
  40. int error = 0;
  41. struct inode * inode = NULL;
  42. if (dentry) {
  43. if (!dentry->d_inode) {
  44. if ((inode = sysfs_new_inode(mode))) {
  45. if (dentry->d_parent && dentry->d_parent->d_inode) {
  46. struct inode *p_inode = dentry->d_parent->d_inode;
  47. p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME;
  48. }
  49. goto Proceed;
  50. }
  51. else
  52. error = -ENOMEM;
  53. } else
  54. error = -EEXIST;
  55. } else
  56. error = -ENOENT;
  57. goto Done;
  58. Proceed:
  59. if (init)
  60. error = init(inode);
  61. if (!error) {
  62. d_instantiate(dentry, inode);
  63. if (S_ISDIR(mode))
  64. dget(dentry); /* pin only directory dentry in core */
  65. } else
  66. iput(inode);
  67. Done:
  68. return error;
  69. }
  70. struct dentry * sysfs_get_dentry(struct dentry * parent, const char * name)
  71. {
  72. struct qstr qstr;
  73. qstr.name = name;
  74. qstr.len = strlen(name);
  75. qstr.hash = full_name_hash(name,qstr.len);
  76. return lookup_hash(&qstr,parent);
  77. }
  78. /*
  79. * Get the name for corresponding element represented by the given sysfs_dirent
  80. */
  81. const unsigned char * sysfs_get_name(struct sysfs_dirent *sd)
  82. {
  83. struct attribute * attr;
  84. struct bin_attribute * bin_attr;
  85. struct sysfs_symlink * sl;
  86. if (!sd || !sd->s_element)
  87. BUG();
  88. switch (sd->s_type) {
  89. case SYSFS_DIR:
  90. /* Always have a dentry so use that */
  91. return sd->s_dentry->d_name.name;
  92. case SYSFS_KOBJ_ATTR:
  93. attr = sd->s_element;
  94. return attr->name;
  95. case SYSFS_KOBJ_BIN_ATTR:
  96. bin_attr = sd->s_element;
  97. return bin_attr->attr.name;
  98. case SYSFS_KOBJ_LINK:
  99. sl = sd->s_element;
  100. return sl->link_name;
  101. }
  102. return NULL;
  103. }
  104. /*
  105. * Unhashes the dentry corresponding to given sysfs_dirent
  106. * Called with parent inode's i_sem held.
  107. */
  108. void sysfs_drop_dentry(struct sysfs_dirent * sd, struct dentry * parent)
  109. {
  110. struct dentry * dentry = sd->s_dentry;
  111. if (dentry) {
  112. spin_lock(&dcache_lock);
  113. spin_lock(&dentry->d_lock);
  114. if (!(d_unhashed(dentry) && dentry->d_inode)) {
  115. dget_locked(dentry);
  116. __d_drop(dentry);
  117. spin_unlock(&dentry->d_lock);
  118. spin_unlock(&dcache_lock);
  119. simple_unlink(parent->d_inode, dentry);
  120. } else {
  121. spin_unlock(&dentry->d_lock);
  122. spin_unlock(&dcache_lock);
  123. }
  124. }
  125. }
  126. void sysfs_hash_and_remove(struct dentry * dir, const char * name)
  127. {
  128. struct sysfs_dirent * sd;
  129. struct sysfs_dirent * parent_sd = dir->d_fsdata;
  130. down(&dir->d_inode->i_sem);
  131. list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
  132. if (!sd->s_element)
  133. continue;
  134. if (!strcmp(sysfs_get_name(sd), name)) {
  135. list_del_init(&sd->s_sibling);
  136. sysfs_drop_dentry(sd, dir);
  137. sysfs_put(sd);
  138. break;
  139. }
  140. }
  141. up(&dir->d_inode->i_sem);
  142. }