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