inode.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_get_inode - get inode for sysfs_dirent
  133. * @sd: sysfs_dirent to allocate inode for
  134. *
  135. * Get inode for @sd. If such inode doesn't exist, a new inode
  136. * is allocated and basics are initialized. New inode is
  137. * returned locked.
  138. *
  139. * LOCKING:
  140. * Kernel thread context (may sleep).
  141. *
  142. * RETURNS:
  143. * Pointer to allocated inode on success, NULL on failure.
  144. */
  145. struct inode * sysfs_get_inode(struct sysfs_dirent *sd)
  146. {
  147. struct inode *inode;
  148. inode = iget_locked(sysfs_sb, sd->s_ino);
  149. if (inode && (inode->i_state & I_NEW))
  150. sysfs_init_inode(sd, inode);
  151. return inode;
  152. }
  153. /**
  154. * sysfs_instantiate - instantiate dentry
  155. * @dentry: dentry to be instantiated
  156. * @inode: inode associated with @sd
  157. *
  158. * Unlock @inode if locked and instantiate @dentry with @inode.
  159. *
  160. * LOCKING:
  161. * None.
  162. */
  163. void sysfs_instantiate(struct dentry *dentry, struct inode *inode)
  164. {
  165. BUG_ON(!dentry || dentry->d_inode);
  166. if (inode->i_state & I_NEW) {
  167. unlock_new_inode(inode);
  168. if (dentry->d_parent && dentry->d_parent->d_inode) {
  169. struct inode *p_inode = dentry->d_parent->d_inode;
  170. p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME;
  171. }
  172. }
  173. d_instantiate(dentry, inode);
  174. }
  175. /**
  176. * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
  177. * @sd: target sysfs_dirent
  178. *
  179. * Drop dentry for @sd. @sd must have been unlinked from its
  180. * parent on entry to this function such that it can't be looked
  181. * up anymore.
  182. *
  183. * @sd->s_dentry which is protected with sysfs_lock points to the
  184. * currently associated dentry but we're not holding a reference
  185. * to it and racing with dput(). Grab dcache_lock and verify
  186. * dentry before dropping it. If @sd->s_dentry is NULL or dput()
  187. * beats us, no need to bother.
  188. */
  189. void sysfs_drop_dentry(struct sysfs_dirent *sd)
  190. {
  191. struct dentry *dentry = NULL, *parent = NULL;
  192. struct inode *dir;
  193. struct timespec curtime;
  194. /* We're not holding a reference to ->s_dentry dentry but the
  195. * field will stay valid as long as sysfs_lock is held.
  196. */
  197. spin_lock(&sysfs_lock);
  198. spin_lock(&dcache_lock);
  199. if (sd->s_dentry && sd->s_dentry->d_inode) {
  200. /* get dentry if it's there and dput() didn't kill it yet */
  201. dentry = dget_locked(sd->s_dentry);
  202. parent = dentry->d_parent;
  203. } else if (sd->s_parent->s_dentry->d_inode) {
  204. /* We need to update the parent even if dentry for the
  205. * victim itself doesn't exist.
  206. */
  207. parent = dget_locked(sd->s_parent->s_dentry);
  208. }
  209. /* drop */
  210. if (dentry) {
  211. spin_lock(&dentry->d_lock);
  212. __d_drop(dentry);
  213. spin_unlock(&dentry->d_lock);
  214. }
  215. spin_unlock(&dcache_lock);
  216. spin_unlock(&sysfs_lock);
  217. /* nothing to do if the parent isn't in dcache */
  218. if (!parent)
  219. return;
  220. /* adjust nlink and update timestamp */
  221. dir = parent->d_inode;
  222. mutex_lock(&dir->i_mutex);
  223. curtime = CURRENT_TIME;
  224. dir->i_ctime = dir->i_mtime = curtime;
  225. if (dentry) {
  226. dentry->d_inode->i_ctime = curtime;
  227. drop_nlink(dentry->d_inode);
  228. if (sd->s_type & SYSFS_DIR) {
  229. drop_nlink(dentry->d_inode);
  230. drop_nlink(dir);
  231. /* XXX: unpin if directory, this will go away soon */
  232. dput(dentry);
  233. }
  234. }
  235. mutex_unlock(&dir->i_mutex);
  236. /* bye bye */
  237. if (dentry)
  238. dput(dentry);
  239. else
  240. dput(parent);
  241. }
  242. int sysfs_hash_and_remove(struct dentry * dir, const char * name)
  243. {
  244. struct sysfs_dirent **pos, *sd;
  245. struct sysfs_dirent *parent_sd;
  246. int found = 0;
  247. if (!dir)
  248. return -ENOENT;
  249. if (dir->d_inode == NULL)
  250. /* no inode means this hasn't been made visible yet */
  251. return -ENOENT;
  252. parent_sd = dir->d_fsdata;
  253. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
  254. for (pos = &parent_sd->s_children; *pos; pos = &(*pos)->s_sibling) {
  255. sd = *pos;
  256. if (!sd->s_type)
  257. continue;
  258. if (!strcmp(sd->s_name, name)) {
  259. *pos = sd->s_sibling;
  260. sd->s_sibling = NULL;
  261. found = 1;
  262. break;
  263. }
  264. }
  265. mutex_unlock(&dir->d_inode->i_mutex);
  266. if (!found)
  267. return -ENOENT;
  268. sysfs_drop_dentry(sd);
  269. sysfs_deactivate(sd);
  270. sysfs_put(sd);
  271. return 0;
  272. }