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