inode.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 <linux/capability.h>
  13. #include <linux/errno.h>
  14. #include <linux/sched.h>
  15. #include <asm/semaphore.h>
  16. #include "sysfs.h"
  17. extern struct super_block * sysfs_sb;
  18. static const struct address_space_operations sysfs_aops = {
  19. .readpage = simple_readpage,
  20. .prepare_write = simple_prepare_write,
  21. .commit_write = simple_commit_write
  22. };
  23. static struct backing_dev_info sysfs_backing_dev_info = {
  24. .ra_pages = 0, /* No readahead */
  25. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
  26. };
  27. static const struct inode_operations sysfs_inode_operations ={
  28. .setattr = sysfs_setattr,
  29. };
  30. void sysfs_delete_inode(struct inode *inode)
  31. {
  32. /* Free the shadowed directory inode operations */
  33. if (sysfs_is_shadowed_inode(inode)) {
  34. kfree(inode->i_op);
  35. inode->i_op = NULL;
  36. }
  37. return generic_delete_inode(inode);
  38. }
  39. int sysfs_setattr(struct dentry * dentry, struct iattr * iattr)
  40. {
  41. struct inode * inode = dentry->d_inode;
  42. struct sysfs_dirent * sd = dentry->d_fsdata;
  43. struct iattr * sd_iattr;
  44. unsigned int ia_valid = iattr->ia_valid;
  45. int error;
  46. if (!sd)
  47. return -EINVAL;
  48. sd_iattr = sd->s_iattr;
  49. error = inode_change_ok(inode, iattr);
  50. if (error)
  51. return error;
  52. error = inode_setattr(inode, iattr);
  53. if (error)
  54. return error;
  55. if (!sd_iattr) {
  56. /* setting attributes for the first time, allocate now */
  57. sd_iattr = kzalloc(sizeof(struct iattr), GFP_KERNEL);
  58. if (!sd_iattr)
  59. return -ENOMEM;
  60. /* assign default attributes */
  61. sd_iattr->ia_mode = sd->s_mode;
  62. sd_iattr->ia_uid = 0;
  63. sd_iattr->ia_gid = 0;
  64. sd_iattr->ia_atime = sd_iattr->ia_mtime = sd_iattr->ia_ctime = CURRENT_TIME;
  65. sd->s_iattr = sd_iattr;
  66. }
  67. /* attributes were changed atleast once in past */
  68. if (ia_valid & ATTR_UID)
  69. sd_iattr->ia_uid = iattr->ia_uid;
  70. if (ia_valid & ATTR_GID)
  71. sd_iattr->ia_gid = iattr->ia_gid;
  72. if (ia_valid & ATTR_ATIME)
  73. sd_iattr->ia_atime = timespec_trunc(iattr->ia_atime,
  74. inode->i_sb->s_time_gran);
  75. if (ia_valid & ATTR_MTIME)
  76. sd_iattr->ia_mtime = timespec_trunc(iattr->ia_mtime,
  77. inode->i_sb->s_time_gran);
  78. if (ia_valid & ATTR_CTIME)
  79. sd_iattr->ia_ctime = timespec_trunc(iattr->ia_ctime,
  80. inode->i_sb->s_time_gran);
  81. if (ia_valid & ATTR_MODE) {
  82. umode_t mode = iattr->ia_mode;
  83. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  84. mode &= ~S_ISGID;
  85. sd_iattr->ia_mode = sd->s_mode = mode;
  86. }
  87. return error;
  88. }
  89. static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
  90. {
  91. inode->i_mode = mode;
  92. inode->i_uid = 0;
  93. inode->i_gid = 0;
  94. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  95. }
  96. static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
  97. {
  98. inode->i_mode = iattr->ia_mode;
  99. inode->i_uid = iattr->ia_uid;
  100. inode->i_gid = iattr->ia_gid;
  101. inode->i_atime = iattr->ia_atime;
  102. inode->i_mtime = iattr->ia_mtime;
  103. inode->i_ctime = iattr->ia_ctime;
  104. }
  105. /*
  106. * sysfs has a different i_mutex lock order behavior for i_mutex than other
  107. * filesystems; sysfs i_mutex is called in many places with subsystem locks
  108. * held. At the same time, many of the VFS locking rules do not apply to
  109. * sysfs at all (cross directory rename for example). To untangle this mess
  110. * (which gives false positives in lockdep), we're giving sysfs inodes their
  111. * own class for i_mutex.
  112. */
  113. static struct lock_class_key sysfs_inode_imutex_key;
  114. void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
  115. {
  116. inode->i_blocks = 0;
  117. inode->i_mapping->a_ops = &sysfs_aops;
  118. inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
  119. inode->i_op = &sysfs_inode_operations;
  120. inode->i_ino = sd->s_ino;
  121. lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key);
  122. if (sd->s_iattr) {
  123. /* sysfs_dirent has non-default attributes
  124. * get them for the new inode from persistent copy
  125. * in sysfs_dirent
  126. */
  127. set_inode_attr(inode, sd->s_iattr);
  128. } else
  129. set_default_inode_attr(inode, sd->s_mode);
  130. }
  131. /**
  132. * sysfs_new_inode - allocate new inode for sysfs_dirent
  133. * @sd: sysfs_dirent to allocate inode for
  134. *
  135. * Allocate inode for @sd and initialize basics.
  136. *
  137. * LOCKING:
  138. * Kernel thread context (may sleep).
  139. *
  140. * RETURNS:
  141. * Pointer to allocated inode on success, NULL on failure.
  142. */
  143. struct inode * sysfs_new_inode(struct sysfs_dirent *sd)
  144. {
  145. struct inode *inode;
  146. inode = new_inode(sysfs_sb);
  147. if (inode)
  148. sysfs_init_inode(sd, inode);
  149. return inode;
  150. }
  151. /**
  152. * sysfs_instantiate - instantiate dentry
  153. * @dentry: dentry to be instantiated
  154. * @inode: inode associated with @sd
  155. *
  156. * Instantiate @dentry with @inode.
  157. *
  158. * LOCKING:
  159. * None.
  160. */
  161. void sysfs_instantiate(struct dentry *dentry, struct inode *inode)
  162. {
  163. BUG_ON(!dentry || dentry->d_inode);
  164. if (dentry->d_parent && dentry->d_parent->d_inode) {
  165. struct inode *p_inode = dentry->d_parent->d_inode;
  166. p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME;
  167. }
  168. d_instantiate(dentry, inode);
  169. }
  170. /**
  171. * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
  172. * @sd: target sysfs_dirent
  173. *
  174. * Drop dentry for @sd. @sd must have been unlinked from its
  175. * parent on entry to this function such that it can't be looked
  176. * up anymore.
  177. *
  178. * @sd->s_dentry which is protected with sysfs_lock points to the
  179. * currently associated dentry but we're not holding a reference
  180. * to it and racing with dput(). Grab dcache_lock and verify
  181. * dentry before dropping it. If @sd->s_dentry is NULL or dput()
  182. * beats us, no need to bother.
  183. */
  184. void sysfs_drop_dentry(struct sysfs_dirent *sd)
  185. {
  186. struct dentry *dentry = NULL, *parent = NULL;
  187. struct inode *dir;
  188. struct timespec curtime;
  189. /* We're not holding a reference to ->s_dentry dentry but the
  190. * field will stay valid as long as sysfs_lock is held.
  191. */
  192. spin_lock(&sysfs_lock);
  193. spin_lock(&dcache_lock);
  194. if (sd->s_dentry && sd->s_dentry->d_inode) {
  195. /* get dentry if it's there and dput() didn't kill it yet */
  196. dentry = dget_locked(sd->s_dentry);
  197. parent = dentry->d_parent;
  198. } else if (sd->s_parent->s_dentry->d_inode) {
  199. /* We need to update the parent even if dentry for the
  200. * victim itself doesn't exist.
  201. */
  202. parent = dget_locked(sd->s_parent->s_dentry);
  203. }
  204. /* drop */
  205. if (dentry) {
  206. spin_lock(&dentry->d_lock);
  207. __d_drop(dentry);
  208. spin_unlock(&dentry->d_lock);
  209. }
  210. spin_unlock(&dcache_lock);
  211. spin_unlock(&sysfs_lock);
  212. /* nothing to do if the parent isn't in dcache */
  213. if (!parent)
  214. return;
  215. /* adjust nlink and update timestamp */
  216. dir = parent->d_inode;
  217. mutex_lock(&dir->i_mutex);
  218. curtime = CURRENT_TIME;
  219. dir->i_ctime = dir->i_mtime = curtime;
  220. if (dentry) {
  221. dentry->d_inode->i_ctime = curtime;
  222. drop_nlink(dentry->d_inode);
  223. if (sd->s_type & SYSFS_DIR) {
  224. drop_nlink(dentry->d_inode);
  225. drop_nlink(dir);
  226. /* XXX: unpin if directory, this will go away soon */
  227. dput(dentry);
  228. }
  229. }
  230. mutex_unlock(&dir->i_mutex);
  231. /* bye bye */
  232. if (dentry)
  233. dput(dentry);
  234. else
  235. dput(parent);
  236. }
  237. int sysfs_hash_and_remove(struct dentry * dir, const char * name)
  238. {
  239. struct sysfs_dirent * sd;
  240. struct sysfs_dirent * parent_sd;
  241. int found = 0;
  242. if (!dir)
  243. return -ENOENT;
  244. if (dir->d_inode == NULL)
  245. /* no inode means this hasn't been made visible yet */
  246. return -ENOENT;
  247. parent_sd = dir->d_fsdata;
  248. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  249. list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
  250. if (!sd->s_type)
  251. continue;
  252. if (!strcmp(sd->s_name, name)) {
  253. list_del_init(&sd->s_sibling);
  254. found = 1;
  255. break;
  256. }
  257. }
  258. mutex_unlock(&dir->d_inode->i_mutex);
  259. if (!found)
  260. return -ENOENT;
  261. sysfs_drop_dentry(sd);
  262. sysfs_deactivate(sd);
  263. sysfs_put(sd);
  264. return 0;
  265. }