inode.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * fs/sysfs/inode.c - basic sysfs inode and dentry operations
  3. *
  4. * Copyright (c) 2001-3 Patrick Mochel
  5. * Copyright (c) 2007 SUSE Linux Products GmbH
  6. * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
  7. *
  8. * This file is released under the GPLv2.
  9. *
  10. * Please see Documentation/filesystems/sysfs.txt for more information.
  11. */
  12. #undef DEBUG
  13. #include <linux/pagemap.h>
  14. #include <linux/namei.h>
  15. #include <linux/backing-dev.h>
  16. #include <linux/capability.h>
  17. #include <linux/errno.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/sysfs.h>
  21. #include <linux/xattr.h>
  22. #include <linux/security.h>
  23. #include "sysfs.h"
  24. extern struct super_block * sysfs_sb;
  25. static const struct address_space_operations sysfs_aops = {
  26. .readpage = simple_readpage,
  27. .write_begin = simple_write_begin,
  28. .write_end = simple_write_end,
  29. };
  30. static struct backing_dev_info sysfs_backing_dev_info = {
  31. .name = "sysfs",
  32. .ra_pages = 0, /* No readahead */
  33. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
  34. };
  35. static const struct inode_operations sysfs_inode_operations ={
  36. .permission = sysfs_permission,
  37. .setattr = sysfs_setattr,
  38. .getattr = sysfs_getattr,
  39. .setxattr = sysfs_setxattr,
  40. };
  41. int __init sysfs_inode_init(void)
  42. {
  43. return bdi_init(&sysfs_backing_dev_info);
  44. }
  45. static struct sysfs_inode_attrs *sysfs_init_inode_attrs(struct sysfs_dirent *sd)
  46. {
  47. struct sysfs_inode_attrs *attrs;
  48. struct iattr *iattrs;
  49. attrs = kzalloc(sizeof(struct sysfs_inode_attrs), GFP_KERNEL);
  50. if (!attrs)
  51. return NULL;
  52. iattrs = &attrs->ia_iattr;
  53. /* assign default attributes */
  54. iattrs->ia_mode = sd->s_mode;
  55. iattrs->ia_uid = 0;
  56. iattrs->ia_gid = 0;
  57. iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME;
  58. return attrs;
  59. }
  60. int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr * iattr)
  61. {
  62. struct sysfs_inode_attrs *sd_attrs;
  63. struct iattr *iattrs;
  64. unsigned int ia_valid = iattr->ia_valid;
  65. sd_attrs = sd->s_iattr;
  66. if (!sd_attrs) {
  67. /* setting attributes for the first time, allocate now */
  68. sd_attrs = sysfs_init_inode_attrs(sd);
  69. if (!sd_attrs)
  70. return -ENOMEM;
  71. sd->s_iattr = sd_attrs;
  72. }
  73. /* attributes were changed at least once in past */
  74. iattrs = &sd_attrs->ia_iattr;
  75. if (ia_valid & ATTR_UID)
  76. iattrs->ia_uid = iattr->ia_uid;
  77. if (ia_valid & ATTR_GID)
  78. iattrs->ia_gid = iattr->ia_gid;
  79. if (ia_valid & ATTR_ATIME)
  80. iattrs->ia_atime = iattr->ia_atime;
  81. if (ia_valid & ATTR_MTIME)
  82. iattrs->ia_mtime = iattr->ia_mtime;
  83. if (ia_valid & ATTR_CTIME)
  84. iattrs->ia_ctime = iattr->ia_ctime;
  85. if (ia_valid & ATTR_MODE) {
  86. umode_t mode = iattr->ia_mode;
  87. iattrs->ia_mode = sd->s_mode = mode;
  88. }
  89. return 0;
  90. }
  91. int sysfs_setattr(struct dentry *dentry, struct iattr *iattr)
  92. {
  93. struct inode *inode = dentry->d_inode;
  94. struct sysfs_dirent *sd = dentry->d_fsdata;
  95. int error;
  96. if (!sd)
  97. return -EINVAL;
  98. mutex_lock(&sysfs_mutex);
  99. error = inode_change_ok(inode, iattr);
  100. if (error)
  101. goto out;
  102. error = sysfs_sd_setattr(sd, iattr);
  103. if (error)
  104. goto out;
  105. /* this ignores size changes */
  106. setattr_copy(inode, iattr);
  107. out:
  108. mutex_unlock(&sysfs_mutex);
  109. return error;
  110. }
  111. static int sysfs_sd_setsecdata(struct sysfs_dirent *sd, void **secdata, u32 *secdata_len)
  112. {
  113. struct sysfs_inode_attrs *iattrs;
  114. void *old_secdata;
  115. size_t old_secdata_len;
  116. if (!sd->s_iattr) {
  117. sd->s_iattr = sysfs_init_inode_attrs(sd);
  118. if (!sd->s_iattr)
  119. return -ENOMEM;
  120. }
  121. iattrs = sd->s_iattr;
  122. old_secdata = iattrs->ia_secdata;
  123. old_secdata_len = iattrs->ia_secdata_len;
  124. iattrs->ia_secdata = *secdata;
  125. iattrs->ia_secdata_len = *secdata_len;
  126. *secdata = old_secdata;
  127. *secdata_len = old_secdata_len;
  128. return 0;
  129. }
  130. int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  131. size_t size, int flags)
  132. {
  133. struct sysfs_dirent *sd = dentry->d_fsdata;
  134. void *secdata;
  135. int error;
  136. u32 secdata_len = 0;
  137. if (!sd)
  138. return -EINVAL;
  139. if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) {
  140. const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
  141. error = security_inode_setsecurity(dentry->d_inode, suffix,
  142. value, size, flags);
  143. if (error)
  144. goto out;
  145. error = security_inode_getsecctx(dentry->d_inode,
  146. &secdata, &secdata_len);
  147. if (error)
  148. goto out;
  149. mutex_lock(&sysfs_mutex);
  150. error = sysfs_sd_setsecdata(sd, &secdata, &secdata_len);
  151. mutex_unlock(&sysfs_mutex);
  152. if (secdata)
  153. security_release_secctx(secdata, secdata_len);
  154. } else
  155. return -EINVAL;
  156. out:
  157. return error;
  158. }
  159. static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
  160. {
  161. inode->i_mode = mode;
  162. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  163. }
  164. static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
  165. {
  166. inode->i_uid = iattr->ia_uid;
  167. inode->i_gid = iattr->ia_gid;
  168. inode->i_atime = iattr->ia_atime;
  169. inode->i_mtime = iattr->ia_mtime;
  170. inode->i_ctime = iattr->ia_ctime;
  171. }
  172. static int sysfs_count_nlink(struct sysfs_dirent *sd)
  173. {
  174. struct sysfs_dirent *child;
  175. int nr = 0;
  176. for (child = sd->s_dir.children; child; child = child->s_sibling)
  177. if (sysfs_type(child) == SYSFS_DIR)
  178. nr++;
  179. return nr + 2;
  180. }
  181. static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode)
  182. {
  183. struct sysfs_inode_attrs *iattrs = sd->s_iattr;
  184. inode->i_mode = sd->s_mode;
  185. if (iattrs) {
  186. /* sysfs_dirent has non-default attributes
  187. * get them from persistent copy in sysfs_dirent
  188. */
  189. set_inode_attr(inode, &iattrs->ia_iattr);
  190. security_inode_notifysecctx(inode,
  191. iattrs->ia_secdata,
  192. iattrs->ia_secdata_len);
  193. }
  194. if (sysfs_type(sd) == SYSFS_DIR)
  195. inode->i_nlink = sysfs_count_nlink(sd);
  196. }
  197. int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  198. {
  199. struct sysfs_dirent *sd = dentry->d_fsdata;
  200. struct inode *inode = dentry->d_inode;
  201. mutex_lock(&sysfs_mutex);
  202. sysfs_refresh_inode(sd, inode);
  203. mutex_unlock(&sysfs_mutex);
  204. generic_fillattr(inode, stat);
  205. return 0;
  206. }
  207. static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
  208. {
  209. struct bin_attribute *bin_attr;
  210. inode->i_private = sysfs_get(sd);
  211. inode->i_mapping->a_ops = &sysfs_aops;
  212. inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
  213. inode->i_op = &sysfs_inode_operations;
  214. set_default_inode_attr(inode, sd->s_mode);
  215. sysfs_refresh_inode(sd, inode);
  216. /* initialize inode according to type */
  217. switch (sysfs_type(sd)) {
  218. case SYSFS_DIR:
  219. inode->i_op = &sysfs_dir_inode_operations;
  220. inode->i_fop = &sysfs_dir_operations;
  221. break;
  222. case SYSFS_KOBJ_ATTR:
  223. inode->i_size = PAGE_SIZE;
  224. inode->i_fop = &sysfs_file_operations;
  225. break;
  226. case SYSFS_KOBJ_BIN_ATTR:
  227. bin_attr = sd->s_bin_attr.bin_attr;
  228. inode->i_size = bin_attr->size;
  229. inode->i_fop = &bin_fops;
  230. break;
  231. case SYSFS_KOBJ_LINK:
  232. inode->i_op = &sysfs_symlink_inode_operations;
  233. break;
  234. default:
  235. BUG();
  236. }
  237. unlock_new_inode(inode);
  238. }
  239. /**
  240. * sysfs_get_inode - get inode for sysfs_dirent
  241. * @sb: super block
  242. * @sd: sysfs_dirent to allocate inode for
  243. *
  244. * Get inode for @sd. If such inode doesn't exist, a new inode
  245. * is allocated and basics are initialized. New inode is
  246. * returned locked.
  247. *
  248. * LOCKING:
  249. * Kernel thread context (may sleep).
  250. *
  251. * RETURNS:
  252. * Pointer to allocated inode on success, NULL on failure.
  253. */
  254. struct inode * sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd)
  255. {
  256. struct inode *inode;
  257. inode = iget_locked(sb, sd->s_ino);
  258. if (inode && (inode->i_state & I_NEW))
  259. sysfs_init_inode(sd, inode);
  260. return inode;
  261. }
  262. /*
  263. * The sysfs_dirent serves as both an inode and a directory entry for sysfs.
  264. * To prevent the sysfs inode numbers from being freed prematurely we take a
  265. * reference to sysfs_dirent from the sysfs inode. A
  266. * super_operations.evict_inode() implementation is needed to drop that
  267. * reference upon inode destruction.
  268. */
  269. void sysfs_evict_inode(struct inode *inode)
  270. {
  271. struct sysfs_dirent *sd = inode->i_private;
  272. truncate_inode_pages(&inode->i_data, 0);
  273. end_writeback(inode);
  274. sysfs_put(sd);
  275. }
  276. int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const void *ns, const char *name)
  277. {
  278. struct sysfs_addrm_cxt acxt;
  279. struct sysfs_dirent *sd;
  280. if (!dir_sd)
  281. return -ENOENT;
  282. sysfs_addrm_start(&acxt, dir_sd);
  283. sd = sysfs_find_dirent(dir_sd, ns, name);
  284. if (sd && (sd->s_ns != ns))
  285. sd = NULL;
  286. if (sd)
  287. sysfs_remove_one(&acxt, sd);
  288. sysfs_addrm_finish(&acxt);
  289. if (sd)
  290. return 0;
  291. else
  292. return -ENOENT;
  293. }
  294. int sysfs_permission(struct inode *inode, int mask, unsigned int flags)
  295. {
  296. struct sysfs_dirent *sd;
  297. if (flags & IPERM_FLAG_RCU)
  298. return -ECHILD;
  299. sd = inode->i_private;
  300. mutex_lock(&sysfs_mutex);
  301. sysfs_refresh_inode(sd, inode);
  302. mutex_unlock(&sysfs_mutex);
  303. return generic_permission(inode, mask, flags, NULL);
  304. }