inode.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * fs/kernfs/inode.c - kernfs inode implementation
  3. *
  4. * Copyright (c) 2001-3 Patrick Mochel
  5. * Copyright (c) 2007 SUSE Linux Products GmbH
  6. * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
  7. *
  8. * This file is released under the GPLv2.
  9. */
  10. #include <linux/pagemap.h>
  11. #include <linux/backing-dev.h>
  12. #include <linux/capability.h>
  13. #include <linux/errno.h>
  14. #include <linux/slab.h>
  15. #include <linux/xattr.h>
  16. #include <linux/security.h>
  17. #include "kernfs-internal.h"
  18. static const struct address_space_operations kernfs_aops = {
  19. .readpage = simple_readpage,
  20. .write_begin = simple_write_begin,
  21. .write_end = simple_write_end,
  22. };
  23. static const struct inode_operations kernfs_iops = {
  24. .permission = kernfs_iop_permission,
  25. .setattr = kernfs_iop_setattr,
  26. .getattr = kernfs_iop_getattr,
  27. .listxattr = kernfs_iop_listxattr,
  28. };
  29. static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
  30. {
  31. static DEFINE_MUTEX(iattr_mutex);
  32. struct kernfs_iattrs *ret;
  33. struct iattr *iattrs;
  34. mutex_lock(&iattr_mutex);
  35. if (kn->iattr)
  36. goto out_unlock;
  37. kn->iattr = kzalloc(sizeof(struct kernfs_iattrs), GFP_KERNEL);
  38. if (!kn->iattr)
  39. goto out_unlock;
  40. iattrs = &kn->iattr->ia_iattr;
  41. /* assign default attributes */
  42. iattrs->ia_mode = kn->mode;
  43. iattrs->ia_uid = GLOBAL_ROOT_UID;
  44. iattrs->ia_gid = GLOBAL_ROOT_GID;
  45. ktime_get_real_ts(&iattrs->ia_atime);
  46. iattrs->ia_mtime = iattrs->ia_atime;
  47. iattrs->ia_ctime = iattrs->ia_atime;
  48. simple_xattrs_init(&kn->iattr->xattrs);
  49. out_unlock:
  50. ret = kn->iattr;
  51. mutex_unlock(&iattr_mutex);
  52. return ret;
  53. }
  54. static int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
  55. {
  56. struct kernfs_iattrs *attrs;
  57. struct iattr *iattrs;
  58. unsigned int ia_valid = iattr->ia_valid;
  59. attrs = kernfs_iattrs(kn);
  60. if (!attrs)
  61. return -ENOMEM;
  62. iattrs = &attrs->ia_iattr;
  63. if (ia_valid & ATTR_UID)
  64. iattrs->ia_uid = iattr->ia_uid;
  65. if (ia_valid & ATTR_GID)
  66. iattrs->ia_gid = iattr->ia_gid;
  67. if (ia_valid & ATTR_ATIME)
  68. iattrs->ia_atime = iattr->ia_atime;
  69. if (ia_valid & ATTR_MTIME)
  70. iattrs->ia_mtime = iattr->ia_mtime;
  71. if (ia_valid & ATTR_CTIME)
  72. iattrs->ia_ctime = iattr->ia_ctime;
  73. if (ia_valid & ATTR_MODE) {
  74. umode_t mode = iattr->ia_mode;
  75. iattrs->ia_mode = kn->mode = mode;
  76. }
  77. return 0;
  78. }
  79. /**
  80. * kernfs_setattr - set iattr on a node
  81. * @kn: target node
  82. * @iattr: iattr to set
  83. *
  84. * Returns 0 on success, -errno on failure.
  85. */
  86. int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
  87. {
  88. int ret;
  89. mutex_lock(&kernfs_mutex);
  90. ret = __kernfs_setattr(kn, iattr);
  91. mutex_unlock(&kernfs_mutex);
  92. return ret;
  93. }
  94. int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr)
  95. {
  96. struct inode *inode = d_inode(dentry);
  97. struct kernfs_node *kn = dentry->d_fsdata;
  98. int error;
  99. if (!kn)
  100. return -EINVAL;
  101. mutex_lock(&kernfs_mutex);
  102. error = setattr_prepare(dentry, iattr);
  103. if (error)
  104. goto out;
  105. error = __kernfs_setattr(kn, iattr);
  106. if (error)
  107. goto out;
  108. /* this ignores size changes */
  109. setattr_copy(inode, iattr);
  110. out:
  111. mutex_unlock(&kernfs_mutex);
  112. return error;
  113. }
  114. static int kernfs_node_setsecdata(struct kernfs_iattrs *attrs, void **secdata,
  115. u32 *secdata_len)
  116. {
  117. void *old_secdata;
  118. size_t old_secdata_len;
  119. old_secdata = attrs->ia_secdata;
  120. old_secdata_len = attrs->ia_secdata_len;
  121. attrs->ia_secdata = *secdata;
  122. attrs->ia_secdata_len = *secdata_len;
  123. *secdata = old_secdata;
  124. *secdata_len = old_secdata_len;
  125. return 0;
  126. }
  127. ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
  128. {
  129. struct kernfs_node *kn = dentry->d_fsdata;
  130. struct kernfs_iattrs *attrs;
  131. attrs = kernfs_iattrs(kn);
  132. if (!attrs)
  133. return -ENOMEM;
  134. return simple_xattr_list(d_inode(dentry), &attrs->xattrs, buf, size);
  135. }
  136. static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
  137. {
  138. inode->i_mode = mode;
  139. inode->i_atime = inode->i_mtime =
  140. inode->i_ctime = current_time(inode);
  141. }
  142. static inline void set_inode_attr(struct inode *inode, struct iattr *iattr)
  143. {
  144. struct super_block *sb = inode->i_sb;
  145. inode->i_uid = iattr->ia_uid;
  146. inode->i_gid = iattr->ia_gid;
  147. inode->i_atime = timespec_trunc(iattr->ia_atime, sb->s_time_gran);
  148. inode->i_mtime = timespec_trunc(iattr->ia_mtime, sb->s_time_gran);
  149. inode->i_ctime = timespec_trunc(iattr->ia_ctime, sb->s_time_gran);
  150. }
  151. static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
  152. {
  153. struct kernfs_iattrs *attrs = kn->iattr;
  154. inode->i_mode = kn->mode;
  155. if (attrs) {
  156. /*
  157. * kernfs_node has non-default attributes get them from
  158. * persistent copy in kernfs_node.
  159. */
  160. set_inode_attr(inode, &attrs->ia_iattr);
  161. security_inode_notifysecctx(inode, attrs->ia_secdata,
  162. attrs->ia_secdata_len);
  163. }
  164. if (kernfs_type(kn) == KERNFS_DIR)
  165. set_nlink(inode, kn->dir.subdirs + 2);
  166. }
  167. int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry,
  168. struct kstat *stat)
  169. {
  170. struct kernfs_node *kn = dentry->d_fsdata;
  171. struct inode *inode = d_inode(dentry);
  172. mutex_lock(&kernfs_mutex);
  173. kernfs_refresh_inode(kn, inode);
  174. mutex_unlock(&kernfs_mutex);
  175. generic_fillattr(inode, stat);
  176. return 0;
  177. }
  178. static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
  179. {
  180. kernfs_get(kn);
  181. inode->i_private = kn;
  182. inode->i_mapping->a_ops = &kernfs_aops;
  183. inode->i_op = &kernfs_iops;
  184. set_default_inode_attr(inode, kn->mode);
  185. kernfs_refresh_inode(kn, inode);
  186. /* initialize inode according to type */
  187. switch (kernfs_type(kn)) {
  188. case KERNFS_DIR:
  189. inode->i_op = &kernfs_dir_iops;
  190. inode->i_fop = &kernfs_dir_fops;
  191. if (kn->flags & KERNFS_EMPTY_DIR)
  192. make_empty_dir_inode(inode);
  193. break;
  194. case KERNFS_FILE:
  195. inode->i_size = kn->attr.size;
  196. inode->i_fop = &kernfs_file_fops;
  197. break;
  198. case KERNFS_LINK:
  199. inode->i_op = &kernfs_symlink_iops;
  200. break;
  201. default:
  202. BUG();
  203. }
  204. unlock_new_inode(inode);
  205. }
  206. /**
  207. * kernfs_get_inode - get inode for kernfs_node
  208. * @sb: super block
  209. * @kn: kernfs_node to allocate inode for
  210. *
  211. * Get inode for @kn. If such inode doesn't exist, a new inode is
  212. * allocated and basics are initialized. New inode is returned
  213. * locked.
  214. *
  215. * LOCKING:
  216. * Kernel thread context (may sleep).
  217. *
  218. * RETURNS:
  219. * Pointer to allocated inode on success, NULL on failure.
  220. */
  221. struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
  222. {
  223. struct inode *inode;
  224. inode = iget_locked(sb, kn->ino);
  225. if (inode && (inode->i_state & I_NEW))
  226. kernfs_init_inode(kn, inode);
  227. return inode;
  228. }
  229. /*
  230. * The kernfs_node serves as both an inode and a directory entry for
  231. * kernfs. To prevent the kernfs inode numbers from being freed
  232. * prematurely we take a reference to kernfs_node from the kernfs inode. A
  233. * super_operations.evict_inode() implementation is needed to drop that
  234. * reference upon inode destruction.
  235. */
  236. void kernfs_evict_inode(struct inode *inode)
  237. {
  238. struct kernfs_node *kn = inode->i_private;
  239. truncate_inode_pages_final(&inode->i_data);
  240. clear_inode(inode);
  241. kernfs_put(kn);
  242. }
  243. int kernfs_iop_permission(struct inode *inode, int mask)
  244. {
  245. struct kernfs_node *kn;
  246. if (mask & MAY_NOT_BLOCK)
  247. return -ECHILD;
  248. kn = inode->i_private;
  249. mutex_lock(&kernfs_mutex);
  250. kernfs_refresh_inode(kn, inode);
  251. mutex_unlock(&kernfs_mutex);
  252. return generic_permission(inode, mask);
  253. }
  254. static int kernfs_xattr_get(const struct xattr_handler *handler,
  255. struct dentry *unused, struct inode *inode,
  256. const char *suffix, void *value, size_t size)
  257. {
  258. const char *name = xattr_full_name(handler, suffix);
  259. struct kernfs_node *kn = inode->i_private;
  260. struct kernfs_iattrs *attrs;
  261. attrs = kernfs_iattrs(kn);
  262. if (!attrs)
  263. return -ENOMEM;
  264. return simple_xattr_get(&attrs->xattrs, name, value, size);
  265. }
  266. static int kernfs_xattr_set(const struct xattr_handler *handler,
  267. struct dentry *unused, struct inode *inode,
  268. const char *suffix, const void *value,
  269. size_t size, int flags)
  270. {
  271. const char *name = xattr_full_name(handler, suffix);
  272. struct kernfs_node *kn = inode->i_private;
  273. struct kernfs_iattrs *attrs;
  274. attrs = kernfs_iattrs(kn);
  275. if (!attrs)
  276. return -ENOMEM;
  277. return simple_xattr_set(&attrs->xattrs, name, value, size, flags);
  278. }
  279. const struct xattr_handler kernfs_trusted_xattr_handler = {
  280. .prefix = XATTR_TRUSTED_PREFIX,
  281. .get = kernfs_xattr_get,
  282. .set = kernfs_xattr_set,
  283. };
  284. static int kernfs_security_xattr_set(const struct xattr_handler *handler,
  285. struct dentry *unused, struct inode *inode,
  286. const char *suffix, const void *value,
  287. size_t size, int flags)
  288. {
  289. struct kernfs_node *kn = inode->i_private;
  290. struct kernfs_iattrs *attrs;
  291. void *secdata;
  292. u32 secdata_len = 0;
  293. int error;
  294. attrs = kernfs_iattrs(kn);
  295. if (!attrs)
  296. return -ENOMEM;
  297. error = security_inode_setsecurity(inode, suffix, value, size, flags);
  298. if (error)
  299. return error;
  300. error = security_inode_getsecctx(inode, &secdata, &secdata_len);
  301. if (error)
  302. return error;
  303. mutex_lock(&kernfs_mutex);
  304. error = kernfs_node_setsecdata(attrs, &secdata, &secdata_len);
  305. mutex_unlock(&kernfs_mutex);
  306. if (secdata)
  307. security_release_secctx(secdata, secdata_len);
  308. return error;
  309. }
  310. const struct xattr_handler kernfs_security_xattr_handler = {
  311. .prefix = XATTR_SECURITY_PREFIX,
  312. .get = kernfs_xattr_get,
  313. .set = kernfs_security_xattr_set,
  314. };
  315. const struct xattr_handler *kernfs_xattr_handlers[] = {
  316. &kernfs_trusted_xattr_handler,
  317. &kernfs_security_xattr_handler,
  318. NULL
  319. };