1 /* 1 /* 2 FUSE: Filesystem in Userspace 2 FUSE: Filesystem in Userspace 3 Copyright (C) 2001-2008 Miklos Szeredi <mik 3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu> 4 4 5 This program can be distributed under the te 5 This program can be distributed under the terms of the GNU GPL. 6 See the file COPYING. 6 See the file COPYING. 7 */ 7 */ 8 8 9 #include "fuse_i.h" 9 #include "fuse_i.h" 10 10 11 #include <linux/pagemap.h> 11 #include <linux/pagemap.h> 12 #include <linux/file.h> 12 #include <linux/file.h> 13 #include <linux/fs_context.h> 13 #include <linux/fs_context.h> 14 #include <linux/moduleparam.h> << 15 #include <linux/sched.h> 14 #include <linux/sched.h> 16 #include <linux/namei.h> 15 #include <linux/namei.h> 17 #include <linux/slab.h> 16 #include <linux/slab.h> 18 #include <linux/xattr.h> 17 #include <linux/xattr.h> 19 #include <linux/iversion.h> 18 #include <linux/iversion.h> 20 #include <linux/posix_acl.h> 19 #include <linux/posix_acl.h> 21 #include <linux/security.h> 20 #include <linux/security.h> 22 #include <linux/types.h> 21 #include <linux/types.h> 23 #include <linux/kernel.h> 22 #include <linux/kernel.h> 24 23 25 static bool __read_mostly allow_sys_admin_acce << 26 module_param(allow_sys_admin_access, bool, 064 << 27 MODULE_PARM_DESC(allow_sys_admin_access, << 28 "Allow users with CAP_SYS_ADM << 29 << 30 static void fuse_advise_use_readdirplus(struct 24 static void fuse_advise_use_readdirplus(struct inode *dir) 31 { 25 { 32 struct fuse_inode *fi = get_fuse_inode 26 struct fuse_inode *fi = get_fuse_inode(dir); 33 27 34 set_bit(FUSE_I_ADVISE_RDPLUS, &fi->sta 28 set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state); 35 } 29 } 36 30 37 #if BITS_PER_LONG >= 64 31 #if BITS_PER_LONG >= 64 38 static inline void __fuse_dentry_settime(struc 32 static inline void __fuse_dentry_settime(struct dentry *entry, u64 time) 39 { 33 { 40 entry->d_fsdata = (void *) time; 34 entry->d_fsdata = (void *) time; 41 } 35 } 42 36 43 static inline u64 fuse_dentry_time(const struc 37 static inline u64 fuse_dentry_time(const struct dentry *entry) 44 { 38 { 45 return (u64)entry->d_fsdata; 39 return (u64)entry->d_fsdata; 46 } 40 } 47 41 48 #else 42 #else 49 union fuse_dentry { 43 union fuse_dentry { 50 u64 time; 44 u64 time; 51 struct rcu_head rcu; 45 struct rcu_head rcu; 52 }; 46 }; 53 47 54 static inline void __fuse_dentry_settime(struc 48 static inline void __fuse_dentry_settime(struct dentry *dentry, u64 time) 55 { 49 { 56 ((union fuse_dentry *) dentry->d_fsdat 50 ((union fuse_dentry *) dentry->d_fsdata)->time = time; 57 } 51 } 58 52 59 static inline u64 fuse_dentry_time(const struc 53 static inline u64 fuse_dentry_time(const struct dentry *entry) 60 { 54 { 61 return ((union fuse_dentry *) entry->d 55 return ((union fuse_dentry *) entry->d_fsdata)->time; 62 } 56 } 63 #endif 57 #endif 64 58 65 static void fuse_dentry_settime(struct dentry 59 static void fuse_dentry_settime(struct dentry *dentry, u64 time) 66 { 60 { 67 struct fuse_conn *fc = get_fuse_conn_s 61 struct fuse_conn *fc = get_fuse_conn_super(dentry->d_sb); 68 bool delete = !time && fc->delete_stal 62 bool delete = !time && fc->delete_stale; 69 /* 63 /* 70 * Mess with DCACHE_OP_DELETE because 64 * Mess with DCACHE_OP_DELETE because dput() will be faster without it. 71 * Don't care about races, either way 65 * Don't care about races, either way it's just an optimization 72 */ 66 */ 73 if ((!delete && (dentry->d_flags & DCA 67 if ((!delete && (dentry->d_flags & DCACHE_OP_DELETE)) || 74 (delete && !(dentry->d_flags & DCA 68 (delete && !(dentry->d_flags & DCACHE_OP_DELETE))) { 75 spin_lock(&dentry->d_lock); 69 spin_lock(&dentry->d_lock); 76 if (!delete) 70 if (!delete) 77 dentry->d_flags &= ~DC 71 dentry->d_flags &= ~DCACHE_OP_DELETE; 78 else 72 else 79 dentry->d_flags |= DCA 73 dentry->d_flags |= DCACHE_OP_DELETE; 80 spin_unlock(&dentry->d_lock); 74 spin_unlock(&dentry->d_lock); 81 } 75 } 82 76 83 __fuse_dentry_settime(dentry, time); 77 __fuse_dentry_settime(dentry, time); 84 } 78 } 85 79 86 /* 80 /* 87 * FUSE caches dentries and attributes with se 81 * FUSE caches dentries and attributes with separate timeout. The 88 * time in jiffies until the dentry/attributes 82 * time in jiffies until the dentry/attributes are valid is stored in 89 * dentry->d_fsdata and fuse_inode->i_time res 83 * dentry->d_fsdata and fuse_inode->i_time respectively. 90 */ 84 */ 91 85 92 /* 86 /* 93 * Calculate the time in jiffies until a dentr 87 * Calculate the time in jiffies until a dentry/attributes are valid 94 */ 88 */ 95 u64 fuse_time_to_jiffies(u64 sec, u32 nsec) !! 89 static u64 time_to_jiffies(u64 sec, u32 nsec) 96 { 90 { 97 if (sec || nsec) { 91 if (sec || nsec) { 98 struct timespec64 ts = { 92 struct timespec64 ts = { 99 sec, 93 sec, 100 min_t(u32, nsec, NSEC_ 94 min_t(u32, nsec, NSEC_PER_SEC - 1) 101 }; 95 }; 102 96 103 return get_jiffies_64() + time 97 return get_jiffies_64() + timespec64_to_jiffies(&ts); 104 } else 98 } else 105 return 0; 99 return 0; 106 } 100 } 107 101 108 /* 102 /* 109 * Set dentry and possibly attribute timeouts 103 * Set dentry and possibly attribute timeouts from the lookup/mk* 110 * replies 104 * replies 111 */ 105 */ 112 void fuse_change_entry_timeout(struct dentry * 106 void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o) 113 { 107 { 114 fuse_dentry_settime(entry, 108 fuse_dentry_settime(entry, 115 fuse_time_to_jiffies(o->entry_ !! 109 time_to_jiffies(o->entry_valid, o->entry_valid_nsec)); >> 110 } >> 111 >> 112 static u64 attr_timeout(struct fuse_attr_out *o) >> 113 { >> 114 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec); >> 115 } >> 116 >> 117 u64 entry_attr_timeout(struct fuse_entry_out *o) >> 118 { >> 119 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec); 116 } 120 } 117 121 118 void fuse_invalidate_attr_mask(struct inode *i 122 void fuse_invalidate_attr_mask(struct inode *inode, u32 mask) 119 { 123 { 120 set_mask_bits(&get_fuse_inode(inode)-> 124 set_mask_bits(&get_fuse_inode(inode)->inval_mask, 0, mask); 121 } 125 } 122 126 123 /* 127 /* 124 * Mark the attributes as stale, so that at th 128 * Mark the attributes as stale, so that at the next call to 125 * ->getattr() they will be fetched from users 129 * ->getattr() they will be fetched from userspace 126 */ 130 */ 127 void fuse_invalidate_attr(struct inode *inode) 131 void fuse_invalidate_attr(struct inode *inode) 128 { 132 { 129 fuse_invalidate_attr_mask(inode, STATX 133 fuse_invalidate_attr_mask(inode, STATX_BASIC_STATS); 130 } 134 } 131 135 132 static void fuse_dir_changed(struct inode *dir 136 static void fuse_dir_changed(struct inode *dir) 133 { 137 { 134 fuse_invalidate_attr(dir); 138 fuse_invalidate_attr(dir); 135 inode_maybe_inc_iversion(dir, false); 139 inode_maybe_inc_iversion(dir, false); 136 } 140 } 137 141 138 /* !! 142 /** 139 * Mark the attributes as stale due to an atim 143 * Mark the attributes as stale due to an atime change. Avoid the invalidate if 140 * atime is not used. 144 * atime is not used. 141 */ 145 */ 142 void fuse_invalidate_atime(struct inode *inode 146 void fuse_invalidate_atime(struct inode *inode) 143 { 147 { 144 if (!IS_RDONLY(inode)) 148 if (!IS_RDONLY(inode)) 145 fuse_invalidate_attr_mask(inod 149 fuse_invalidate_attr_mask(inode, STATX_ATIME); 146 } 150 } 147 151 148 /* 152 /* 149 * Just mark the entry as stale, so that a nex 153 * Just mark the entry as stale, so that a next attempt to look it up 150 * will result in a new lookup call to userspa 154 * will result in a new lookup call to userspace 151 * 155 * 152 * This is called when a dentry is about to be 156 * This is called when a dentry is about to become negative and the 153 * timeout is unknown (unlink, rmdir, rename a 157 * timeout is unknown (unlink, rmdir, rename and in some cases 154 * lookup) 158 * lookup) 155 */ 159 */ 156 void fuse_invalidate_entry_cache(struct dentry 160 void fuse_invalidate_entry_cache(struct dentry *entry) 157 { 161 { 158 fuse_dentry_settime(entry, 0); 162 fuse_dentry_settime(entry, 0); 159 } 163 } 160 164 161 /* 165 /* 162 * Same as fuse_invalidate_entry_cache(), but 166 * Same as fuse_invalidate_entry_cache(), but also try to remove the 163 * dentry from the hash 167 * dentry from the hash 164 */ 168 */ 165 static void fuse_invalidate_entry(struct dentr 169 static void fuse_invalidate_entry(struct dentry *entry) 166 { 170 { 167 d_invalidate(entry); 171 d_invalidate(entry); 168 fuse_invalidate_entry_cache(entry); 172 fuse_invalidate_entry_cache(entry); 169 } 173 } 170 174 171 static void fuse_lookup_init(struct fuse_conn 175 static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args, 172 u64 nodeid, const 176 u64 nodeid, const struct qstr *name, 173 struct fuse_entry 177 struct fuse_entry_out *outarg) 174 { 178 { 175 memset(outarg, 0, sizeof(struct fuse_e 179 memset(outarg, 0, sizeof(struct fuse_entry_out)); 176 args->opcode = FUSE_LOOKUP; 180 args->opcode = FUSE_LOOKUP; 177 args->nodeid = nodeid; 181 args->nodeid = nodeid; 178 args->in_numargs = 1; 182 args->in_numargs = 1; 179 args->in_args[0].size = name->len + 1; 183 args->in_args[0].size = name->len + 1; 180 args->in_args[0].value = name->name; 184 args->in_args[0].value = name->name; 181 args->out_numargs = 1; 185 args->out_numargs = 1; 182 args->out_args[0].size = sizeof(struct 186 args->out_args[0].size = sizeof(struct fuse_entry_out); 183 args->out_args[0].value = outarg; 187 args->out_args[0].value = outarg; 184 } 188 } 185 189 186 /* 190 /* 187 * Check whether the dentry is still valid 191 * Check whether the dentry is still valid 188 * 192 * 189 * If the entry validity timeout has expired a 193 * If the entry validity timeout has expired and the dentry is 190 * positive, try to redo the lookup. If the l 194 * positive, try to redo the lookup. If the lookup results in a 191 * different inode, then let the VFS invalidat 195 * different inode, then let the VFS invalidate the dentry and redo 192 * the lookup once more. If the lookup result 196 * the lookup once more. If the lookup results in the same inode, 193 * then refresh the attributes, timeouts and m 197 * then refresh the attributes, timeouts and mark the dentry valid. 194 */ 198 */ 195 static int fuse_dentry_revalidate(struct dentr 199 static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags) 196 { 200 { 197 struct inode *inode; 201 struct inode *inode; 198 struct dentry *parent; 202 struct dentry *parent; 199 struct fuse_mount *fm; 203 struct fuse_mount *fm; 200 struct fuse_inode *fi; 204 struct fuse_inode *fi; 201 int ret; 205 int ret; 202 206 203 inode = d_inode_rcu(entry); 207 inode = d_inode_rcu(entry); 204 if (inode && fuse_is_bad(inode)) 208 if (inode && fuse_is_bad(inode)) 205 goto invalid; 209 goto invalid; 206 else if (time_before64(fuse_dentry_tim 210 else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) || 207 (flags & (LOOKUP_EXCL | LOOKU !! 211 (flags & (LOOKUP_EXCL | LOOKUP_REVAL))) { 208 struct fuse_entry_out outarg; 212 struct fuse_entry_out outarg; 209 FUSE_ARGS(args); 213 FUSE_ARGS(args); 210 struct fuse_forget_link *forge 214 struct fuse_forget_link *forget; 211 u64 attr_version; 215 u64 attr_version; 212 216 213 /* For negative dentries, alwa 217 /* For negative dentries, always do a fresh lookup */ 214 if (!inode) 218 if (!inode) 215 goto invalid; 219 goto invalid; 216 220 217 ret = -ECHILD; 221 ret = -ECHILD; 218 if (flags & LOOKUP_RCU) 222 if (flags & LOOKUP_RCU) 219 goto out; 223 goto out; 220 224 221 fm = get_fuse_mount(inode); 225 fm = get_fuse_mount(inode); 222 226 223 forget = fuse_alloc_forget(); 227 forget = fuse_alloc_forget(); 224 ret = -ENOMEM; 228 ret = -ENOMEM; 225 if (!forget) 229 if (!forget) 226 goto out; 230 goto out; 227 231 228 attr_version = fuse_get_attr_v 232 attr_version = fuse_get_attr_version(fm->fc); 229 233 230 parent = dget_parent(entry); 234 parent = dget_parent(entry); 231 fuse_lookup_init(fm->fc, &args 235 fuse_lookup_init(fm->fc, &args, get_node_id(d_inode(parent)), 232 &entry->d_nam 236 &entry->d_name, &outarg); 233 ret = fuse_simple_request(fm, 237 ret = fuse_simple_request(fm, &args); 234 dput(parent); 238 dput(parent); 235 /* Zero nodeid is same as -ENO 239 /* Zero nodeid is same as -ENOENT */ 236 if (!ret && !outarg.nodeid) 240 if (!ret && !outarg.nodeid) 237 ret = -ENOENT; 241 ret = -ENOENT; 238 if (!ret) { 242 if (!ret) { 239 fi = get_fuse_inode(in 243 fi = get_fuse_inode(inode); 240 if (outarg.nodeid != g 244 if (outarg.nodeid != get_node_id(inode) || 241 (bool) IS_AUTOMOUN 245 (bool) IS_AUTOMOUNT(inode) != (bool) (outarg.attr.flags & FUSE_ATTR_SUBMOUNT)) { 242 fuse_queue_for 246 fuse_queue_forget(fm->fc, forget, 243 247 outarg.nodeid, 1); 244 goto invalid; 248 goto invalid; 245 } 249 } 246 spin_lock(&fi->lock); 250 spin_lock(&fi->lock); 247 fi->nlookup++; 251 fi->nlookup++; 248 spin_unlock(&fi->lock) 252 spin_unlock(&fi->lock); 249 } 253 } 250 kfree(forget); 254 kfree(forget); 251 if (ret == -ENOMEM || ret == - !! 255 if (ret == -ENOMEM) 252 goto out; 256 goto out; 253 if (ret || fuse_invalid_attr(& 257 if (ret || fuse_invalid_attr(&outarg.attr) || 254 fuse_stale_inode(inode, ou 258 fuse_stale_inode(inode, outarg.generation, &outarg.attr)) 255 goto invalid; 259 goto invalid; 256 260 257 forget_all_cached_acls(inode); 261 forget_all_cached_acls(inode); 258 fuse_change_attributes(inode, !! 262 fuse_change_attributes(inode, &outarg.attr, 259 ATTR_TI !! 263 entry_attr_timeout(&outarg), 260 attr_ve 264 attr_version); 261 fuse_change_entry_timeout(entr 265 fuse_change_entry_timeout(entry, &outarg); 262 } else if (inode) { 266 } else if (inode) { 263 fi = get_fuse_inode(inode); 267 fi = get_fuse_inode(inode); 264 if (flags & LOOKUP_RCU) { 268 if (flags & LOOKUP_RCU) { 265 if (test_bit(FUSE_I_IN 269 if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state)) 266 return -ECHILD 270 return -ECHILD; 267 } else if (test_and_clear_bit( 271 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) { 268 parent = dget_parent(e 272 parent = dget_parent(entry); 269 fuse_advise_use_readdi 273 fuse_advise_use_readdirplus(d_inode(parent)); 270 dput(parent); 274 dput(parent); 271 } 275 } 272 } 276 } 273 ret = 1; 277 ret = 1; 274 out: 278 out: 275 return ret; 279 return ret; 276 280 277 invalid: 281 invalid: 278 ret = 0; 282 ret = 0; 279 goto out; 283 goto out; 280 } 284 } 281 285 282 #if BITS_PER_LONG < 64 286 #if BITS_PER_LONG < 64 283 static int fuse_dentry_init(struct dentry *den 287 static int fuse_dentry_init(struct dentry *dentry) 284 { 288 { 285 dentry->d_fsdata = kzalloc(sizeof(unio 289 dentry->d_fsdata = kzalloc(sizeof(union fuse_dentry), 286 GFP_KERNEL_ 290 GFP_KERNEL_ACCOUNT | __GFP_RECLAIMABLE); 287 291 288 return dentry->d_fsdata ? 0 : -ENOMEM; 292 return dentry->d_fsdata ? 0 : -ENOMEM; 289 } 293 } 290 static void fuse_dentry_release(struct dentry 294 static void fuse_dentry_release(struct dentry *dentry) 291 { 295 { 292 union fuse_dentry *fd = dentry->d_fsda 296 union fuse_dentry *fd = dentry->d_fsdata; 293 297 294 kfree_rcu(fd, rcu); 298 kfree_rcu(fd, rcu); 295 } 299 } 296 #endif 300 #endif 297 301 298 static int fuse_dentry_delete(const struct den 302 static int fuse_dentry_delete(const struct dentry *dentry) 299 { 303 { 300 return time_before64(fuse_dentry_time( 304 return time_before64(fuse_dentry_time(dentry), get_jiffies_64()); 301 } 305 } 302 306 303 /* 307 /* 304 * Create a fuse_mount object with a new super 308 * Create a fuse_mount object with a new superblock (with path->dentry 305 * as the root), and return that mount so it c 309 * as the root), and return that mount so it can be auto-mounted on 306 * @path. 310 * @path. 307 */ 311 */ 308 static struct vfsmount *fuse_dentry_automount( 312 static struct vfsmount *fuse_dentry_automount(struct path *path) 309 { 313 { 310 struct fs_context *fsc; 314 struct fs_context *fsc; 311 struct vfsmount *mnt; 315 struct vfsmount *mnt; 312 struct fuse_inode *mp_fi = get_fuse_in 316 struct fuse_inode *mp_fi = get_fuse_inode(d_inode(path->dentry)); 313 317 314 fsc = fs_context_for_submount(path->mn 318 fsc = fs_context_for_submount(path->mnt->mnt_sb->s_type, path->dentry); 315 if (IS_ERR(fsc)) 319 if (IS_ERR(fsc)) 316 return ERR_CAST(fsc); 320 return ERR_CAST(fsc); 317 321 318 /* Pass the FUSE inode of the mount fo 322 /* Pass the FUSE inode of the mount for fuse_get_tree_submount() */ 319 fsc->fs_private = mp_fi; 323 fsc->fs_private = mp_fi; 320 324 321 /* Create the submount */ 325 /* Create the submount */ 322 mnt = fc_mount(fsc); 326 mnt = fc_mount(fsc); 323 if (!IS_ERR(mnt)) 327 if (!IS_ERR(mnt)) 324 mntget(mnt); 328 mntget(mnt); 325 329 326 put_fs_context(fsc); 330 put_fs_context(fsc); 327 return mnt; 331 return mnt; 328 } 332 } 329 333 330 const struct dentry_operations fuse_dentry_ope 334 const struct dentry_operations fuse_dentry_operations = { 331 .d_revalidate = fuse_dentry_revalida 335 .d_revalidate = fuse_dentry_revalidate, 332 .d_delete = fuse_dentry_delete, 336 .d_delete = fuse_dentry_delete, 333 #if BITS_PER_LONG < 64 337 #if BITS_PER_LONG < 64 334 .d_init = fuse_dentry_init, 338 .d_init = fuse_dentry_init, 335 .d_release = fuse_dentry_release, 339 .d_release = fuse_dentry_release, 336 #endif 340 #endif 337 .d_automount = fuse_dentry_automoun 341 .d_automount = fuse_dentry_automount, 338 }; 342 }; 339 343 340 const struct dentry_operations fuse_root_dentr 344 const struct dentry_operations fuse_root_dentry_operations = { 341 #if BITS_PER_LONG < 64 345 #if BITS_PER_LONG < 64 342 .d_init = fuse_dentry_init, 346 .d_init = fuse_dentry_init, 343 .d_release = fuse_dentry_release, 347 .d_release = fuse_dentry_release, 344 #endif 348 #endif 345 }; 349 }; 346 350 347 int fuse_valid_type(int m) 351 int fuse_valid_type(int m) 348 { 352 { 349 return S_ISREG(m) || S_ISDIR(m) || S_I 353 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) || 350 S_ISBLK(m) || S_ISFIFO(m) || S 354 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m); 351 } 355 } 352 356 353 static bool fuse_valid_size(u64 size) << 354 { << 355 return size <= LLONG_MAX; << 356 } << 357 << 358 bool fuse_invalid_attr(struct fuse_attr *attr) 357 bool fuse_invalid_attr(struct fuse_attr *attr) 359 { 358 { 360 return !fuse_valid_type(attr->mode) || !! 359 return !fuse_valid_type(attr->mode) || >> 360 attr->size > LLONG_MAX; 361 } 361 } 362 362 363 int fuse_lookup_name(struct super_block *sb, u 363 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name, 364 struct fuse_entry_out *ou 364 struct fuse_entry_out *outarg, struct inode **inode) 365 { 365 { 366 struct fuse_mount *fm = get_fuse_mount 366 struct fuse_mount *fm = get_fuse_mount_super(sb); 367 FUSE_ARGS(args); 367 FUSE_ARGS(args); 368 struct fuse_forget_link *forget; 368 struct fuse_forget_link *forget; 369 u64 attr_version; 369 u64 attr_version; 370 int err; 370 int err; 371 371 372 *inode = NULL; 372 *inode = NULL; 373 err = -ENAMETOOLONG; 373 err = -ENAMETOOLONG; 374 if (name->len > FUSE_NAME_MAX) 374 if (name->len > FUSE_NAME_MAX) 375 goto out; 375 goto out; 376 376 377 377 378 forget = fuse_alloc_forget(); 378 forget = fuse_alloc_forget(); 379 err = -ENOMEM; 379 err = -ENOMEM; 380 if (!forget) 380 if (!forget) 381 goto out; 381 goto out; 382 382 383 attr_version = fuse_get_attr_version(f 383 attr_version = fuse_get_attr_version(fm->fc); 384 384 385 fuse_lookup_init(fm->fc, &args, nodeid 385 fuse_lookup_init(fm->fc, &args, nodeid, name, outarg); 386 err = fuse_simple_request(fm, &args); 386 err = fuse_simple_request(fm, &args); 387 /* Zero nodeid is same as -ENOENT, but 387 /* Zero nodeid is same as -ENOENT, but with valid timeout */ 388 if (err || !outarg->nodeid) 388 if (err || !outarg->nodeid) 389 goto out_put_forget; 389 goto out_put_forget; 390 390 391 err = -EIO; 391 err = -EIO; >> 392 if (!outarg->nodeid) >> 393 goto out_put_forget; 392 if (fuse_invalid_attr(&outarg->attr)) 394 if (fuse_invalid_attr(&outarg->attr)) 393 goto out_put_forget; 395 goto out_put_forget; 394 if (outarg->nodeid == FUSE_ROOT_ID && << 395 pr_warn_once("root generation << 396 outarg->generation = 0; << 397 } << 398 396 399 *inode = fuse_iget(sb, outarg->nodeid, 397 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation, 400 &outarg->attr, ATTR !! 398 &outarg->attr, entry_attr_timeout(outarg), 401 attr_version); 399 attr_version); 402 err = -ENOMEM; 400 err = -ENOMEM; 403 if (!*inode) { 401 if (!*inode) { 404 fuse_queue_forget(fm->fc, forg 402 fuse_queue_forget(fm->fc, forget, outarg->nodeid, 1); 405 goto out; 403 goto out; 406 } 404 } 407 err = 0; 405 err = 0; 408 406 409 out_put_forget: 407 out_put_forget: 410 kfree(forget); 408 kfree(forget); 411 out: 409 out: 412 return err; 410 return err; 413 } 411 } 414 412 415 static struct dentry *fuse_lookup(struct inode 413 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry, 416 unsigned int 414 unsigned int flags) 417 { 415 { 418 int err; 416 int err; 419 struct fuse_entry_out outarg; 417 struct fuse_entry_out outarg; 420 struct inode *inode; 418 struct inode *inode; 421 struct dentry *newent; 419 struct dentry *newent; 422 bool outarg_valid = true; 420 bool outarg_valid = true; 423 bool locked; 421 bool locked; 424 422 425 if (fuse_is_bad(dir)) 423 if (fuse_is_bad(dir)) 426 return ERR_PTR(-EIO); 424 return ERR_PTR(-EIO); 427 425 428 locked = fuse_lock_inode(dir); 426 locked = fuse_lock_inode(dir); 429 err = fuse_lookup_name(dir->i_sb, get_ 427 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name, 430 &outarg, &inode 428 &outarg, &inode); 431 fuse_unlock_inode(dir, locked); 429 fuse_unlock_inode(dir, locked); 432 if (err == -ENOENT) { 430 if (err == -ENOENT) { 433 outarg_valid = false; 431 outarg_valid = false; 434 err = 0; 432 err = 0; 435 } 433 } 436 if (err) 434 if (err) 437 goto out_err; 435 goto out_err; 438 436 439 err = -EIO; 437 err = -EIO; 440 if (inode && get_node_id(inode) == FUS 438 if (inode && get_node_id(inode) == FUSE_ROOT_ID) 441 goto out_iput; 439 goto out_iput; 442 440 443 newent = d_splice_alias(inode, entry); 441 newent = d_splice_alias(inode, entry); 444 err = PTR_ERR(newent); 442 err = PTR_ERR(newent); 445 if (IS_ERR(newent)) 443 if (IS_ERR(newent)) 446 goto out_err; 444 goto out_err; 447 445 448 entry = newent ? newent : entry; 446 entry = newent ? newent : entry; 449 if (outarg_valid) 447 if (outarg_valid) 450 fuse_change_entry_timeout(entr 448 fuse_change_entry_timeout(entry, &outarg); 451 else 449 else 452 fuse_invalidate_entry_cache(en 450 fuse_invalidate_entry_cache(entry); 453 451 454 if (inode) 452 if (inode) 455 fuse_advise_use_readdirplus(di 453 fuse_advise_use_readdirplus(dir); 456 return newent; 454 return newent; 457 455 458 out_iput: 456 out_iput: 459 iput(inode); 457 iput(inode); 460 out_err: 458 out_err: 461 return ERR_PTR(err); 459 return ERR_PTR(err); 462 } 460 } 463 461 464 static int get_security_context(struct dentry 462 static int get_security_context(struct dentry *entry, umode_t mode, 465 struct fuse_in !! 463 void **security_ctx, u32 *security_ctxlen) 466 { 464 { 467 struct fuse_secctx *fctx; 465 struct fuse_secctx *fctx; 468 struct fuse_secctx_header *header; 466 struct fuse_secctx_header *header; 469 void *ctx = NULL, *ptr; 467 void *ctx = NULL, *ptr; 470 u32 ctxlen, total_len = sizeof(*header 468 u32 ctxlen, total_len = sizeof(*header); 471 int err, nr_ctx = 0; 469 int err, nr_ctx = 0; 472 const char *name; 470 const char *name; 473 size_t namelen; 471 size_t namelen; 474 472 475 err = security_dentry_init_security(en 473 err = security_dentry_init_security(entry, mode, &entry->d_name, 476 &n 474 &name, &ctx, &ctxlen); 477 if (err) { 475 if (err) { 478 if (err != -EOPNOTSUPP) 476 if (err != -EOPNOTSUPP) 479 goto out_err; 477 goto out_err; 480 /* No LSM is supporting this s 478 /* No LSM is supporting this security hook. Ignore error */ 481 ctxlen = 0; 479 ctxlen = 0; 482 ctx = NULL; 480 ctx = NULL; 483 } 481 } 484 482 485 if (ctxlen) { 483 if (ctxlen) { 486 nr_ctx = 1; 484 nr_ctx = 1; 487 namelen = strlen(name) + 1; 485 namelen = strlen(name) + 1; 488 err = -EIO; 486 err = -EIO; 489 if (WARN_ON(namelen > XATTR_NA 487 if (WARN_ON(namelen > XATTR_NAME_MAX + 1 || ctxlen > S32_MAX)) 490 goto out_err; 488 goto out_err; 491 total_len += FUSE_REC_ALIGN(si 489 total_len += FUSE_REC_ALIGN(sizeof(*fctx) + namelen + ctxlen); 492 } 490 } 493 491 494 err = -ENOMEM; 492 err = -ENOMEM; 495 header = ptr = kzalloc(total_len, GFP_ 493 header = ptr = kzalloc(total_len, GFP_KERNEL); 496 if (!ptr) 494 if (!ptr) 497 goto out_err; 495 goto out_err; 498 496 499 header->nr_secctx = nr_ctx; 497 header->nr_secctx = nr_ctx; 500 header->size = total_len; 498 header->size = total_len; 501 ptr += sizeof(*header); 499 ptr += sizeof(*header); 502 if (nr_ctx) { 500 if (nr_ctx) { 503 fctx = ptr; 501 fctx = ptr; 504 fctx->size = ctxlen; 502 fctx->size = ctxlen; 505 ptr += sizeof(*fctx); 503 ptr += sizeof(*fctx); 506 504 507 strcpy(ptr, name); 505 strcpy(ptr, name); 508 ptr += namelen; 506 ptr += namelen; 509 507 510 memcpy(ptr, ctx, ctxlen); 508 memcpy(ptr, ctx, ctxlen); 511 } 509 } 512 ext->size = total_len; !! 510 *security_ctxlen = total_len; 513 ext->value = header; !! 511 *security_ctx = header; 514 err = 0; 512 err = 0; 515 out_err: 513 out_err: 516 kfree(ctx); 514 kfree(ctx); 517 return err; 515 return err; 518 } 516 } 519 517 520 static void *extend_arg(struct fuse_in_arg *bu << 521 { << 522 void *p; << 523 u32 newlen = buf->size + bytes; << 524 << 525 p = krealloc(buf->value, newlen, GFP_K << 526 if (!p) { << 527 kfree(buf->value); << 528 buf->size = 0; << 529 buf->value = NULL; << 530 return NULL; << 531 } << 532 << 533 memset(p + buf->size, 0, bytes); << 534 buf->value = p; << 535 buf->size = newlen; << 536 << 537 return p + newlen - bytes; << 538 } << 539 << 540 static u32 fuse_ext_size(size_t size) << 541 { << 542 return FUSE_REC_ALIGN(sizeof(struct fu << 543 } << 544 << 545 /* << 546 * This adds just a single supplementary group << 547 */ << 548 static int get_create_supp_group(struct inode << 549 { << 550 struct fuse_conn *fc = get_fuse_conn(d << 551 struct fuse_ext_header *xh; << 552 struct fuse_supp_groups *sg; << 553 kgid_t kgid = dir->i_gid; << 554 gid_t parent_gid = from_kgid(fc->user_ << 555 u32 sg_len = fuse_ext_size(sizeof(*sg) << 556 << 557 if (parent_gid == (gid_t) -1 || gid_eq << 558 !in_group_p(kgid)) << 559 return 0; << 560 << 561 xh = extend_arg(ext, sg_len); << 562 if (!xh) << 563 return -ENOMEM; << 564 << 565 xh->size = sg_len; << 566 xh->type = FUSE_EXT_GROUPS; << 567 << 568 sg = (struct fuse_supp_groups *) &xh[1 << 569 sg->nr_groups = 1; << 570 sg->groups[0] = parent_gid; << 571 << 572 return 0; << 573 } << 574 << 575 static int get_create_ext(struct fuse_args *ar << 576 struct inode *dir, s << 577 umode_t mode) << 578 { << 579 struct fuse_conn *fc = get_fuse_conn_s << 580 struct fuse_in_arg ext = { .size = 0, << 581 int err = 0; << 582 << 583 if (fc->init_security) << 584 err = get_security_context(den << 585 if (!err && fc->create_supp_group) << 586 err = get_create_supp_group(di << 587 << 588 if (!err && ext.size) { << 589 WARN_ON(args->in_numargs >= AR << 590 args->is_ext = true; << 591 args->ext_idx = args->in_numar << 592 args->in_args[args->ext_idx] = << 593 } else { << 594 kfree(ext.value); << 595 } << 596 << 597 return err; << 598 } << 599 << 600 static void free_ext_value(struct fuse_args *a << 601 { << 602 if (args->is_ext) << 603 kfree(args->in_args[args->ext_ << 604 } << 605 << 606 /* 518 /* 607 * Atomic create+open operation 519 * Atomic create+open operation 608 * 520 * 609 * If the filesystem doesn't support this, the 521 * If the filesystem doesn't support this, then fall back to separate 610 * 'mknod' + 'open' requests. 522 * 'mknod' + 'open' requests. 611 */ 523 */ 612 static int fuse_create_open(struct inode *dir, 524 static int fuse_create_open(struct inode *dir, struct dentry *entry, 613 struct file *file, 525 struct file *file, unsigned int flags, 614 umode_t mode, u32 !! 526 umode_t mode) 615 { 527 { 616 int err; 528 int err; 617 struct inode *inode; 529 struct inode *inode; 618 struct fuse_mount *fm = get_fuse_mount 530 struct fuse_mount *fm = get_fuse_mount(dir); 619 FUSE_ARGS(args); 531 FUSE_ARGS(args); 620 struct fuse_forget_link *forget; 532 struct fuse_forget_link *forget; 621 struct fuse_create_in inarg; 533 struct fuse_create_in inarg; 622 struct fuse_open_out *outopenp; !! 534 struct fuse_open_out outopen; 623 struct fuse_entry_out outentry; 535 struct fuse_entry_out outentry; 624 struct fuse_inode *fi; 536 struct fuse_inode *fi; 625 struct fuse_file *ff; 537 struct fuse_file *ff; >> 538 void *security_ctx = NULL; >> 539 u32 security_ctxlen; 626 bool trunc = flags & O_TRUNC; 540 bool trunc = flags & O_TRUNC; 627 541 628 /* Userspace expects S_IFREG in create 542 /* Userspace expects S_IFREG in create mode */ 629 BUG_ON((mode & S_IFMT) != S_IFREG); 543 BUG_ON((mode & S_IFMT) != S_IFREG); 630 544 631 forget = fuse_alloc_forget(); 545 forget = fuse_alloc_forget(); 632 err = -ENOMEM; 546 err = -ENOMEM; 633 if (!forget) 547 if (!forget) 634 goto out_err; 548 goto out_err; 635 549 636 err = -ENOMEM; 550 err = -ENOMEM; 637 ff = fuse_file_alloc(fm, true); !! 551 ff = fuse_file_alloc(fm); 638 if (!ff) 552 if (!ff) 639 goto out_put_forget_req; 553 goto out_put_forget_req; 640 554 641 if (!fm->fc->dont_mask) 555 if (!fm->fc->dont_mask) 642 mode &= ~current_umask(); 556 mode &= ~current_umask(); 643 557 644 flags &= ~O_NOCTTY; 558 flags &= ~O_NOCTTY; 645 memset(&inarg, 0, sizeof(inarg)); 559 memset(&inarg, 0, sizeof(inarg)); 646 memset(&outentry, 0, sizeof(outentry)) 560 memset(&outentry, 0, sizeof(outentry)); 647 inarg.flags = flags; 561 inarg.flags = flags; 648 inarg.mode = mode; 562 inarg.mode = mode; 649 inarg.umask = current_umask(); 563 inarg.umask = current_umask(); 650 564 651 if (fm->fc->handle_killpriv_v2 && trun 565 if (fm->fc->handle_killpriv_v2 && trunc && 652 !(flags & O_EXCL) && !capable(CAP_ 566 !(flags & O_EXCL) && !capable(CAP_FSETID)) { 653 inarg.open_flags |= FUSE_OPEN_ 567 inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID; 654 } 568 } 655 569 656 args.opcode = opcode; !! 570 args.opcode = FUSE_CREATE; 657 args.nodeid = get_node_id(dir); 571 args.nodeid = get_node_id(dir); 658 args.in_numargs = 2; 572 args.in_numargs = 2; 659 args.in_args[0].size = sizeof(inarg); 573 args.in_args[0].size = sizeof(inarg); 660 args.in_args[0].value = &inarg; 574 args.in_args[0].value = &inarg; 661 args.in_args[1].size = entry->d_name.l 575 args.in_args[1].size = entry->d_name.len + 1; 662 args.in_args[1].value = entry->d_name. 576 args.in_args[1].value = entry->d_name.name; 663 args.out_numargs = 2; 577 args.out_numargs = 2; 664 args.out_args[0].size = sizeof(outentr 578 args.out_args[0].size = sizeof(outentry); 665 args.out_args[0].value = &outentry; 579 args.out_args[0].value = &outentry; 666 /* Store outarg for fuse_finish_open() !! 580 args.out_args[1].size = sizeof(outopen); 667 outopenp = &ff->args->open_outarg; !! 581 args.out_args[1].value = &outopen; 668 args.out_args[1].size = sizeof(*outope << 669 args.out_args[1].value = outopenp; << 670 582 671 err = get_create_ext(&args, dir, entry !! 583 if (fm->fc->init_security) { 672 if (err) !! 584 err = get_security_context(entry, mode, &security_ctx, 673 goto out_free_ff; !! 585 &security_ctxlen); >> 586 if (err) >> 587 goto out_put_forget_req; >> 588 >> 589 args.in_numargs = 3; >> 590 args.in_args[2].size = security_ctxlen; >> 591 args.in_args[2].value = security_ctx; >> 592 } 674 593 675 err = fuse_simple_request(fm, &args); 594 err = fuse_simple_request(fm, &args); 676 free_ext_value(&args); !! 595 kfree(security_ctx); 677 if (err) 596 if (err) 678 goto out_free_ff; 597 goto out_free_ff; 679 598 680 err = -EIO; 599 err = -EIO; 681 if (!S_ISREG(outentry.attr.mode) || in 600 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid) || 682 fuse_invalid_attr(&outentry.attr)) 601 fuse_invalid_attr(&outentry.attr)) 683 goto out_free_ff; 602 goto out_free_ff; 684 603 685 ff->fh = outopenp->fh; !! 604 ff->fh = outopen.fh; 686 ff->nodeid = outentry.nodeid; 605 ff->nodeid = outentry.nodeid; 687 ff->open_flags = outopenp->open_flags; !! 606 ff->open_flags = outopen.open_flags; 688 inode = fuse_iget(dir->i_sb, outentry. 607 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation, 689 &outentry.attr, ATTR !! 608 &outentry.attr, entry_attr_timeout(&outentry), 0); 690 if (!inode) { 609 if (!inode) { 691 flags &= ~(O_CREAT | O_EXCL | 610 flags &= ~(O_CREAT | O_EXCL | O_TRUNC); 692 fuse_sync_release(NULL, ff, fl 611 fuse_sync_release(NULL, ff, flags); 693 fuse_queue_forget(fm->fc, forg 612 fuse_queue_forget(fm->fc, forget, outentry.nodeid, 1); 694 err = -ENOMEM; 613 err = -ENOMEM; 695 goto out_err; 614 goto out_err; 696 } 615 } 697 kfree(forget); 616 kfree(forget); 698 d_instantiate(entry, inode); 617 d_instantiate(entry, inode); 699 fuse_change_entry_timeout(entry, &oute 618 fuse_change_entry_timeout(entry, &outentry); 700 fuse_dir_changed(dir); 619 fuse_dir_changed(dir); 701 err = generic_file_open(inode, file); !! 620 err = finish_open(file, entry, generic_file_open); 702 if (!err) { << 703 file->private_data = ff; << 704 err = finish_open(file, entry, << 705 } << 706 if (err) { 621 if (err) { 707 fi = get_fuse_inode(inode); 622 fi = get_fuse_inode(inode); 708 fuse_sync_release(fi, ff, flag 623 fuse_sync_release(fi, ff, flags); 709 } else { 624 } else { >> 625 file->private_data = ff; >> 626 fuse_finish_open(inode, file); 710 if (fm->fc->atomic_o_trunc && 627 if (fm->fc->atomic_o_trunc && trunc) 711 truncate_pagecache(ino 628 truncate_pagecache(inode, 0); 712 else if (!(ff->open_flags & FO 629 else if (!(ff->open_flags & FOPEN_KEEP_CACHE)) 713 invalidate_inode_pages 630 invalidate_inode_pages2(inode->i_mapping); 714 } 631 } 715 return err; 632 return err; 716 633 717 out_free_ff: 634 out_free_ff: 718 fuse_file_free(ff); 635 fuse_file_free(ff); 719 out_put_forget_req: 636 out_put_forget_req: 720 kfree(forget); 637 kfree(forget); 721 out_err: 638 out_err: 722 return err; 639 return err; 723 } 640 } 724 641 725 static int fuse_mknod(struct mnt_idmap *, stru !! 642 static int fuse_mknod(struct user_namespace *, struct inode *, struct dentry *, 726 umode_t, dev_t); 643 umode_t, dev_t); 727 static int fuse_atomic_open(struct inode *dir, 644 static int fuse_atomic_open(struct inode *dir, struct dentry *entry, 728 struct file *file, 645 struct file *file, unsigned flags, 729 umode_t mode) 646 umode_t mode) 730 { 647 { 731 int err; 648 int err; 732 struct fuse_conn *fc = get_fuse_conn(d 649 struct fuse_conn *fc = get_fuse_conn(dir); 733 struct dentry *res = NULL; 650 struct dentry *res = NULL; 734 651 735 if (fuse_is_bad(dir)) 652 if (fuse_is_bad(dir)) 736 return -EIO; 653 return -EIO; 737 654 738 if (d_in_lookup(entry)) { 655 if (d_in_lookup(entry)) { 739 res = fuse_lookup(dir, entry, 656 res = fuse_lookup(dir, entry, 0); 740 if (IS_ERR(res)) 657 if (IS_ERR(res)) 741 return PTR_ERR(res); 658 return PTR_ERR(res); 742 659 743 if (res) 660 if (res) 744 entry = res; 661 entry = res; 745 } 662 } 746 663 747 if (!(flags & O_CREAT) || d_really_is_ 664 if (!(flags & O_CREAT) || d_really_is_positive(entry)) 748 goto no_open; 665 goto no_open; 749 666 750 /* Only creates */ 667 /* Only creates */ 751 file->f_mode |= FMODE_CREATED; 668 file->f_mode |= FMODE_CREATED; 752 669 753 if (fc->no_create) 670 if (fc->no_create) 754 goto mknod; 671 goto mknod; 755 672 756 err = fuse_create_open(dir, entry, fil !! 673 err = fuse_create_open(dir, entry, file, flags, mode); 757 if (err == -ENOSYS) { 674 if (err == -ENOSYS) { 758 fc->no_create = 1; 675 fc->no_create = 1; 759 goto mknod; 676 goto mknod; 760 } else if (err == -EEXIST) !! 677 } 761 fuse_invalidate_entry(entry); << 762 out_dput: 678 out_dput: 763 dput(res); 679 dput(res); 764 return err; 680 return err; 765 681 766 mknod: 682 mknod: 767 err = fuse_mknod(&nop_mnt_idmap, dir, !! 683 err = fuse_mknod(&init_user_ns, dir, entry, mode, 0); 768 if (err) 684 if (err) 769 goto out_dput; 685 goto out_dput; 770 no_open: 686 no_open: 771 return finish_no_open(file, res); 687 return finish_no_open(file, res); 772 } 688 } 773 689 774 /* 690 /* 775 * Code shared between mknod, mkdir, symlink a 691 * Code shared between mknod, mkdir, symlink and link 776 */ 692 */ 777 static int create_new_entry(struct fuse_mount 693 static int create_new_entry(struct fuse_mount *fm, struct fuse_args *args, 778 struct inode *dir, 694 struct inode *dir, struct dentry *entry, 779 umode_t mode) 695 umode_t mode) 780 { 696 { 781 struct fuse_entry_out outarg; 697 struct fuse_entry_out outarg; 782 struct inode *inode; 698 struct inode *inode; 783 struct dentry *d; 699 struct dentry *d; 784 int err; 700 int err; 785 struct fuse_forget_link *forget; 701 struct fuse_forget_link *forget; >> 702 void *security_ctx = NULL; >> 703 u32 security_ctxlen; 786 704 787 if (fuse_is_bad(dir)) 705 if (fuse_is_bad(dir)) 788 return -EIO; 706 return -EIO; 789 707 790 forget = fuse_alloc_forget(); 708 forget = fuse_alloc_forget(); 791 if (!forget) 709 if (!forget) 792 return -ENOMEM; 710 return -ENOMEM; 793 711 794 memset(&outarg, 0, sizeof(outarg)); 712 memset(&outarg, 0, sizeof(outarg)); 795 args->nodeid = get_node_id(dir); 713 args->nodeid = get_node_id(dir); 796 args->out_numargs = 1; 714 args->out_numargs = 1; 797 args->out_args[0].size = sizeof(outarg 715 args->out_args[0].size = sizeof(outarg); 798 args->out_args[0].value = &outarg; 716 args->out_args[0].value = &outarg; 799 717 800 if (args->opcode != FUSE_LINK) { !! 718 if (fm->fc->init_security && args->opcode != FUSE_LINK) { 801 err = get_create_ext(args, dir !! 719 err = get_security_context(entry, mode, &security_ctx, >> 720 &security_ctxlen); 802 if (err) 721 if (err) 803 goto out_put_forget_re 722 goto out_put_forget_req; >> 723 >> 724 BUG_ON(args->in_numargs != 2); >> 725 >> 726 args->in_numargs = 3; >> 727 args->in_args[2].size = security_ctxlen; >> 728 args->in_args[2].value = security_ctx; 804 } 729 } 805 730 806 err = fuse_simple_request(fm, args); 731 err = fuse_simple_request(fm, args); 807 free_ext_value(args); !! 732 kfree(security_ctx); 808 if (err) 733 if (err) 809 goto out_put_forget_req; 734 goto out_put_forget_req; 810 735 811 err = -EIO; 736 err = -EIO; 812 if (invalid_nodeid(outarg.nodeid) || f 737 if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr)) 813 goto out_put_forget_req; 738 goto out_put_forget_req; 814 739 815 if ((outarg.attr.mode ^ mode) & S_IFMT 740 if ((outarg.attr.mode ^ mode) & S_IFMT) 816 goto out_put_forget_req; 741 goto out_put_forget_req; 817 742 818 inode = fuse_iget(dir->i_sb, outarg.no 743 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, 819 &outarg.attr, ATTR_T !! 744 &outarg.attr, entry_attr_timeout(&outarg), 0); 820 if (!inode) { 745 if (!inode) { 821 fuse_queue_forget(fm->fc, forg 746 fuse_queue_forget(fm->fc, forget, outarg.nodeid, 1); 822 return -ENOMEM; 747 return -ENOMEM; 823 } 748 } 824 kfree(forget); 749 kfree(forget); 825 750 826 d_drop(entry); 751 d_drop(entry); 827 d = d_splice_alias(inode, entry); 752 d = d_splice_alias(inode, entry); 828 if (IS_ERR(d)) 753 if (IS_ERR(d)) 829 return PTR_ERR(d); 754 return PTR_ERR(d); 830 755 831 if (d) { 756 if (d) { 832 fuse_change_entry_timeout(d, & 757 fuse_change_entry_timeout(d, &outarg); 833 dput(d); 758 dput(d); 834 } else { 759 } else { 835 fuse_change_entry_timeout(entr 760 fuse_change_entry_timeout(entry, &outarg); 836 } 761 } 837 fuse_dir_changed(dir); 762 fuse_dir_changed(dir); 838 return 0; 763 return 0; 839 764 840 out_put_forget_req: 765 out_put_forget_req: 841 if (err == -EEXIST) << 842 fuse_invalidate_entry(entry); << 843 kfree(forget); 766 kfree(forget); 844 return err; 767 return err; 845 } 768 } 846 769 847 static int fuse_mknod(struct mnt_idmap *idmap, !! 770 static int fuse_mknod(struct user_namespace *mnt_userns, struct inode *dir, 848 struct dentry *entry, um 771 struct dentry *entry, umode_t mode, dev_t rdev) 849 { 772 { 850 struct fuse_mknod_in inarg; 773 struct fuse_mknod_in inarg; 851 struct fuse_mount *fm = get_fuse_mount 774 struct fuse_mount *fm = get_fuse_mount(dir); 852 FUSE_ARGS(args); 775 FUSE_ARGS(args); 853 776 854 if (!fm->fc->dont_mask) 777 if (!fm->fc->dont_mask) 855 mode &= ~current_umask(); 778 mode &= ~current_umask(); 856 779 857 memset(&inarg, 0, sizeof(inarg)); 780 memset(&inarg, 0, sizeof(inarg)); 858 inarg.mode = mode; 781 inarg.mode = mode; 859 inarg.rdev = new_encode_dev(rdev); 782 inarg.rdev = new_encode_dev(rdev); 860 inarg.umask = current_umask(); 783 inarg.umask = current_umask(); 861 args.opcode = FUSE_MKNOD; 784 args.opcode = FUSE_MKNOD; 862 args.in_numargs = 2; 785 args.in_numargs = 2; 863 args.in_args[0].size = sizeof(inarg); 786 args.in_args[0].size = sizeof(inarg); 864 args.in_args[0].value = &inarg; 787 args.in_args[0].value = &inarg; 865 args.in_args[1].size = entry->d_name.l 788 args.in_args[1].size = entry->d_name.len + 1; 866 args.in_args[1].value = entry->d_name. 789 args.in_args[1].value = entry->d_name.name; 867 return create_new_entry(fm, &args, dir 790 return create_new_entry(fm, &args, dir, entry, mode); 868 } 791 } 869 792 870 static int fuse_create(struct mnt_idmap *idmap !! 793 static int fuse_create(struct user_namespace *mnt_userns, struct inode *dir, 871 struct dentry *entry, u 794 struct dentry *entry, umode_t mode, bool excl) 872 { 795 { 873 return fuse_mknod(&nop_mnt_idmap, dir, !! 796 return fuse_mknod(&init_user_ns, dir, entry, mode, 0); 874 } << 875 << 876 static int fuse_tmpfile(struct mnt_idmap *idma << 877 struct file *file, umo << 878 { << 879 struct fuse_conn *fc = get_fuse_conn(d << 880 int err; << 881 << 882 if (fc->no_tmpfile) << 883 return -EOPNOTSUPP; << 884 << 885 err = fuse_create_open(dir, file->f_pa << 886 if (err == -ENOSYS) { << 887 fc->no_tmpfile = 1; << 888 err = -EOPNOTSUPP; << 889 } << 890 return err; << 891 } 797 } 892 798 893 static int fuse_mkdir(struct mnt_idmap *idmap, !! 799 static int fuse_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 894 struct dentry *entry, um 800 struct dentry *entry, umode_t mode) 895 { 801 { 896 struct fuse_mkdir_in inarg; 802 struct fuse_mkdir_in inarg; 897 struct fuse_mount *fm = get_fuse_mount 803 struct fuse_mount *fm = get_fuse_mount(dir); 898 FUSE_ARGS(args); 804 FUSE_ARGS(args); 899 805 900 if (!fm->fc->dont_mask) 806 if (!fm->fc->dont_mask) 901 mode &= ~current_umask(); 807 mode &= ~current_umask(); 902 808 903 memset(&inarg, 0, sizeof(inarg)); 809 memset(&inarg, 0, sizeof(inarg)); 904 inarg.mode = mode; 810 inarg.mode = mode; 905 inarg.umask = current_umask(); 811 inarg.umask = current_umask(); 906 args.opcode = FUSE_MKDIR; 812 args.opcode = FUSE_MKDIR; 907 args.in_numargs = 2; 813 args.in_numargs = 2; 908 args.in_args[0].size = sizeof(inarg); 814 args.in_args[0].size = sizeof(inarg); 909 args.in_args[0].value = &inarg; 815 args.in_args[0].value = &inarg; 910 args.in_args[1].size = entry->d_name.l 816 args.in_args[1].size = entry->d_name.len + 1; 911 args.in_args[1].value = entry->d_name. 817 args.in_args[1].value = entry->d_name.name; 912 return create_new_entry(fm, &args, dir 818 return create_new_entry(fm, &args, dir, entry, S_IFDIR); 913 } 819 } 914 820 915 static int fuse_symlink(struct mnt_idmap *idma !! 821 static int fuse_symlink(struct user_namespace *mnt_userns, struct inode *dir, 916 struct dentry *entry, 822 struct dentry *entry, const char *link) 917 { 823 { 918 struct fuse_mount *fm = get_fuse_mount 824 struct fuse_mount *fm = get_fuse_mount(dir); 919 unsigned len = strlen(link) + 1; 825 unsigned len = strlen(link) + 1; 920 FUSE_ARGS(args); 826 FUSE_ARGS(args); 921 827 922 args.opcode = FUSE_SYMLINK; 828 args.opcode = FUSE_SYMLINK; 923 args.in_numargs = 2; 829 args.in_numargs = 2; 924 args.in_args[0].size = entry->d_name.l 830 args.in_args[0].size = entry->d_name.len + 1; 925 args.in_args[0].value = entry->d_name. 831 args.in_args[0].value = entry->d_name.name; 926 args.in_args[1].size = len; 832 args.in_args[1].size = len; 927 args.in_args[1].value = link; 833 args.in_args[1].value = link; 928 return create_new_entry(fm, &args, dir 834 return create_new_entry(fm, &args, dir, entry, S_IFLNK); 929 } 835 } 930 836 931 void fuse_flush_time_update(struct inode *inod 837 void fuse_flush_time_update(struct inode *inode) 932 { 838 { 933 int err = sync_inode_metadata(inode, 1 839 int err = sync_inode_metadata(inode, 1); 934 840 935 mapping_set_error(inode->i_mapping, er 841 mapping_set_error(inode->i_mapping, err); 936 } 842 } 937 843 938 static void fuse_update_ctime_in_cache(struct 844 static void fuse_update_ctime_in_cache(struct inode *inode) 939 { 845 { 940 if (!IS_NOCMTIME(inode)) { 846 if (!IS_NOCMTIME(inode)) { 941 inode_set_ctime_current(inode) !! 847 inode->i_ctime = current_time(inode); 942 mark_inode_dirty_sync(inode); 848 mark_inode_dirty_sync(inode); 943 fuse_flush_time_update(inode); 849 fuse_flush_time_update(inode); 944 } 850 } 945 } 851 } 946 852 947 void fuse_update_ctime(struct inode *inode) 853 void fuse_update_ctime(struct inode *inode) 948 { 854 { 949 fuse_invalidate_attr_mask(inode, STATX 855 fuse_invalidate_attr_mask(inode, STATX_CTIME); 950 fuse_update_ctime_in_cache(inode); 856 fuse_update_ctime_in_cache(inode); 951 } 857 } 952 858 953 static void fuse_entry_unlinked(struct dentry 859 static void fuse_entry_unlinked(struct dentry *entry) 954 { 860 { 955 struct inode *inode = d_inode(entry); 861 struct inode *inode = d_inode(entry); 956 struct fuse_conn *fc = get_fuse_conn(i 862 struct fuse_conn *fc = get_fuse_conn(inode); 957 struct fuse_inode *fi = get_fuse_inode 863 struct fuse_inode *fi = get_fuse_inode(inode); 958 864 959 spin_lock(&fi->lock); 865 spin_lock(&fi->lock); 960 fi->attr_version = atomic64_inc_return 866 fi->attr_version = atomic64_inc_return(&fc->attr_version); 961 /* 867 /* 962 * If i_nlink == 0 then unlink doesn't 868 * If i_nlink == 0 then unlink doesn't make sense, yet this can 963 * happen if userspace filesystem is c 869 * happen if userspace filesystem is careless. It would be 964 * difficult to enforce correct nlink 870 * difficult to enforce correct nlink usage so just ignore this 965 * condition here 871 * condition here 966 */ 872 */ 967 if (S_ISDIR(inode->i_mode)) 873 if (S_ISDIR(inode->i_mode)) 968 clear_nlink(inode); 874 clear_nlink(inode); 969 else if (inode->i_nlink > 0) 875 else if (inode->i_nlink > 0) 970 drop_nlink(inode); 876 drop_nlink(inode); 971 spin_unlock(&fi->lock); 877 spin_unlock(&fi->lock); 972 fuse_invalidate_entry_cache(entry); 878 fuse_invalidate_entry_cache(entry); 973 fuse_update_ctime(inode); 879 fuse_update_ctime(inode); 974 } 880 } 975 881 976 static int fuse_unlink(struct inode *dir, stru 882 static int fuse_unlink(struct inode *dir, struct dentry *entry) 977 { 883 { 978 int err; 884 int err; 979 struct fuse_mount *fm = get_fuse_mount 885 struct fuse_mount *fm = get_fuse_mount(dir); 980 FUSE_ARGS(args); 886 FUSE_ARGS(args); 981 887 982 if (fuse_is_bad(dir)) 888 if (fuse_is_bad(dir)) 983 return -EIO; 889 return -EIO; 984 890 985 args.opcode = FUSE_UNLINK; 891 args.opcode = FUSE_UNLINK; 986 args.nodeid = get_node_id(dir); 892 args.nodeid = get_node_id(dir); 987 args.in_numargs = 1; 893 args.in_numargs = 1; 988 args.in_args[0].size = entry->d_name.l 894 args.in_args[0].size = entry->d_name.len + 1; 989 args.in_args[0].value = entry->d_name. 895 args.in_args[0].value = entry->d_name.name; 990 err = fuse_simple_request(fm, &args); 896 err = fuse_simple_request(fm, &args); 991 if (!err) { 897 if (!err) { 992 fuse_dir_changed(dir); 898 fuse_dir_changed(dir); 993 fuse_entry_unlinked(entry); 899 fuse_entry_unlinked(entry); 994 } else if (err == -EINTR || err == -EN !! 900 } else if (err == -EINTR) 995 fuse_invalidate_entry(entry); 901 fuse_invalidate_entry(entry); 996 return err; 902 return err; 997 } 903 } 998 904 999 static int fuse_rmdir(struct inode *dir, struc 905 static int fuse_rmdir(struct inode *dir, struct dentry *entry) 1000 { 906 { 1001 int err; 907 int err; 1002 struct fuse_mount *fm = get_fuse_moun 908 struct fuse_mount *fm = get_fuse_mount(dir); 1003 FUSE_ARGS(args); 909 FUSE_ARGS(args); 1004 910 1005 if (fuse_is_bad(dir)) 911 if (fuse_is_bad(dir)) 1006 return -EIO; 912 return -EIO; 1007 913 1008 args.opcode = FUSE_RMDIR; 914 args.opcode = FUSE_RMDIR; 1009 args.nodeid = get_node_id(dir); 915 args.nodeid = get_node_id(dir); 1010 args.in_numargs = 1; 916 args.in_numargs = 1; 1011 args.in_args[0].size = entry->d_name. 917 args.in_args[0].size = entry->d_name.len + 1; 1012 args.in_args[0].value = entry->d_name 918 args.in_args[0].value = entry->d_name.name; 1013 err = fuse_simple_request(fm, &args); 919 err = fuse_simple_request(fm, &args); 1014 if (!err) { 920 if (!err) { 1015 fuse_dir_changed(dir); 921 fuse_dir_changed(dir); 1016 fuse_entry_unlinked(entry); 922 fuse_entry_unlinked(entry); 1017 } else if (err == -EINTR || err == -E !! 923 } else if (err == -EINTR) 1018 fuse_invalidate_entry(entry); 924 fuse_invalidate_entry(entry); 1019 return err; 925 return err; 1020 } 926 } 1021 927 1022 static int fuse_rename_common(struct inode *o 928 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent, 1023 struct inode *n 929 struct inode *newdir, struct dentry *newent, 1024 unsigned int fl 930 unsigned int flags, int opcode, size_t argsize) 1025 { 931 { 1026 int err; 932 int err; 1027 struct fuse_rename2_in inarg; 933 struct fuse_rename2_in inarg; 1028 struct fuse_mount *fm = get_fuse_moun 934 struct fuse_mount *fm = get_fuse_mount(olddir); 1029 FUSE_ARGS(args); 935 FUSE_ARGS(args); 1030 936 1031 memset(&inarg, 0, argsize); 937 memset(&inarg, 0, argsize); 1032 inarg.newdir = get_node_id(newdir); 938 inarg.newdir = get_node_id(newdir); 1033 inarg.flags = flags; 939 inarg.flags = flags; 1034 args.opcode = opcode; 940 args.opcode = opcode; 1035 args.nodeid = get_node_id(olddir); 941 args.nodeid = get_node_id(olddir); 1036 args.in_numargs = 3; 942 args.in_numargs = 3; 1037 args.in_args[0].size = argsize; 943 args.in_args[0].size = argsize; 1038 args.in_args[0].value = &inarg; 944 args.in_args[0].value = &inarg; 1039 args.in_args[1].size = oldent->d_name 945 args.in_args[1].size = oldent->d_name.len + 1; 1040 args.in_args[1].value = oldent->d_nam 946 args.in_args[1].value = oldent->d_name.name; 1041 args.in_args[2].size = newent->d_name 947 args.in_args[2].size = newent->d_name.len + 1; 1042 args.in_args[2].value = newent->d_nam 948 args.in_args[2].value = newent->d_name.name; 1043 err = fuse_simple_request(fm, &args); 949 err = fuse_simple_request(fm, &args); 1044 if (!err) { 950 if (!err) { 1045 /* ctime changes */ 951 /* ctime changes */ 1046 fuse_update_ctime(d_inode(old 952 fuse_update_ctime(d_inode(oldent)); 1047 953 1048 if (flags & RENAME_EXCHANGE) 954 if (flags & RENAME_EXCHANGE) 1049 fuse_update_ctime(d_i 955 fuse_update_ctime(d_inode(newent)); 1050 956 1051 fuse_dir_changed(olddir); 957 fuse_dir_changed(olddir); 1052 if (olddir != newdir) 958 if (olddir != newdir) 1053 fuse_dir_changed(newd 959 fuse_dir_changed(newdir); 1054 960 1055 /* newent will end up negativ 961 /* newent will end up negative */ 1056 if (!(flags & RENAME_EXCHANGE 962 if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) 1057 fuse_entry_unlinked(n 963 fuse_entry_unlinked(newent); 1058 } else if (err == -EINTR || err == -E !! 964 } else if (err == -EINTR) { 1059 /* If request was interrupted 965 /* If request was interrupted, DEITY only knows if the 1060 rename actually took place 966 rename actually took place. If the invalidation 1061 fails (e.g. some process h 967 fails (e.g. some process has CWD under the renamed 1062 directory), then there can 968 directory), then there can be inconsistency between 1063 the dcache and the real fi 969 the dcache and the real filesystem. Tough luck. */ 1064 fuse_invalidate_entry(oldent) 970 fuse_invalidate_entry(oldent); 1065 if (d_really_is_positive(newe 971 if (d_really_is_positive(newent)) 1066 fuse_invalidate_entry 972 fuse_invalidate_entry(newent); 1067 } 973 } 1068 974 1069 return err; 975 return err; 1070 } 976 } 1071 977 1072 static int fuse_rename2(struct mnt_idmap *idm !! 978 static int fuse_rename2(struct user_namespace *mnt_userns, struct inode *olddir, 1073 struct dentry *oldent 979 struct dentry *oldent, struct inode *newdir, 1074 struct dentry *newent 980 struct dentry *newent, unsigned int flags) 1075 { 981 { 1076 struct fuse_conn *fc = get_fuse_conn( 982 struct fuse_conn *fc = get_fuse_conn(olddir); 1077 int err; 983 int err; 1078 984 1079 if (fuse_is_bad(olddir)) 985 if (fuse_is_bad(olddir)) 1080 return -EIO; 986 return -EIO; 1081 987 1082 if (flags & ~(RENAME_NOREPLACE | RENA 988 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 1083 return -EINVAL; 989 return -EINVAL; 1084 990 1085 if (flags) { 991 if (flags) { 1086 if (fc->no_rename2 || fc->min 992 if (fc->no_rename2 || fc->minor < 23) 1087 return -EINVAL; 993 return -EINVAL; 1088 994 1089 err = fuse_rename_common(oldd 995 err = fuse_rename_common(olddir, oldent, newdir, newent, flags, 1090 FUSE 996 FUSE_RENAME2, 1091 size 997 sizeof(struct fuse_rename2_in)); 1092 if (err == -ENOSYS) { 998 if (err == -ENOSYS) { 1093 fc->no_rename2 = 1; 999 fc->no_rename2 = 1; 1094 err = -EINVAL; 1000 err = -EINVAL; 1095 } 1001 } 1096 } else { 1002 } else { 1097 err = fuse_rename_common(oldd 1003 err = fuse_rename_common(olddir, oldent, newdir, newent, 0, 1098 FUSE 1004 FUSE_RENAME, 1099 size 1005 sizeof(struct fuse_rename_in)); 1100 } 1006 } 1101 1007 1102 return err; 1008 return err; 1103 } 1009 } 1104 1010 1105 static int fuse_link(struct dentry *entry, st 1011 static int fuse_link(struct dentry *entry, struct inode *newdir, 1106 struct dentry *newent) 1012 struct dentry *newent) 1107 { 1013 { 1108 int err; 1014 int err; 1109 struct fuse_link_in inarg; 1015 struct fuse_link_in inarg; 1110 struct inode *inode = d_inode(entry); 1016 struct inode *inode = d_inode(entry); 1111 struct fuse_mount *fm = get_fuse_moun 1017 struct fuse_mount *fm = get_fuse_mount(inode); 1112 FUSE_ARGS(args); 1018 FUSE_ARGS(args); 1113 1019 1114 memset(&inarg, 0, sizeof(inarg)); 1020 memset(&inarg, 0, sizeof(inarg)); 1115 inarg.oldnodeid = get_node_id(inode); 1021 inarg.oldnodeid = get_node_id(inode); 1116 args.opcode = FUSE_LINK; 1022 args.opcode = FUSE_LINK; 1117 args.in_numargs = 2; 1023 args.in_numargs = 2; 1118 args.in_args[0].size = sizeof(inarg); 1024 args.in_args[0].size = sizeof(inarg); 1119 args.in_args[0].value = &inarg; 1025 args.in_args[0].value = &inarg; 1120 args.in_args[1].size = newent->d_name 1026 args.in_args[1].size = newent->d_name.len + 1; 1121 args.in_args[1].value = newent->d_nam 1027 args.in_args[1].value = newent->d_name.name; 1122 err = create_new_entry(fm, &args, new 1028 err = create_new_entry(fm, &args, newdir, newent, inode->i_mode); 1123 if (!err) 1029 if (!err) 1124 fuse_update_ctime_in_cache(in 1030 fuse_update_ctime_in_cache(inode); 1125 else if (err == -EINTR) 1031 else if (err == -EINTR) 1126 fuse_invalidate_attr(inode); 1032 fuse_invalidate_attr(inode); 1127 1033 1128 return err; 1034 return err; 1129 } 1035 } 1130 1036 1131 static void fuse_fillattr(struct inode *inode 1037 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr, 1132 struct kstat *stat) 1038 struct kstat *stat) 1133 { 1039 { 1134 unsigned int blkbits; 1040 unsigned int blkbits; 1135 struct fuse_conn *fc = get_fuse_conn( 1041 struct fuse_conn *fc = get_fuse_conn(inode); 1136 1042 1137 stat->dev = inode->i_sb->s_dev; 1043 stat->dev = inode->i_sb->s_dev; 1138 stat->ino = attr->ino; 1044 stat->ino = attr->ino; 1139 stat->mode = (inode->i_mode & S_IFMT) 1045 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777); 1140 stat->nlink = attr->nlink; 1046 stat->nlink = attr->nlink; 1141 stat->uid = make_kuid(fc->user_ns, at 1047 stat->uid = make_kuid(fc->user_ns, attr->uid); 1142 stat->gid = make_kgid(fc->user_ns, at 1048 stat->gid = make_kgid(fc->user_ns, attr->gid); 1143 stat->rdev = inode->i_rdev; 1049 stat->rdev = inode->i_rdev; 1144 stat->atime.tv_sec = attr->atime; 1050 stat->atime.tv_sec = attr->atime; 1145 stat->atime.tv_nsec = attr->atimensec 1051 stat->atime.tv_nsec = attr->atimensec; 1146 stat->mtime.tv_sec = attr->mtime; 1052 stat->mtime.tv_sec = attr->mtime; 1147 stat->mtime.tv_nsec = attr->mtimensec 1053 stat->mtime.tv_nsec = attr->mtimensec; 1148 stat->ctime.tv_sec = attr->ctime; 1054 stat->ctime.tv_sec = attr->ctime; 1149 stat->ctime.tv_nsec = attr->ctimensec 1055 stat->ctime.tv_nsec = attr->ctimensec; 1150 stat->size = attr->size; 1056 stat->size = attr->size; 1151 stat->blocks = attr->blocks; 1057 stat->blocks = attr->blocks; 1152 1058 1153 if (attr->blksize != 0) 1059 if (attr->blksize != 0) 1154 blkbits = ilog2(attr->blksize 1060 blkbits = ilog2(attr->blksize); 1155 else 1061 else 1156 blkbits = inode->i_sb->s_bloc 1062 blkbits = inode->i_sb->s_blocksize_bits; 1157 1063 1158 stat->blksize = 1 << blkbits; 1064 stat->blksize = 1 << blkbits; 1159 } 1065 } 1160 1066 1161 static void fuse_statx_to_attr(struct fuse_st << 1162 { << 1163 memset(attr, 0, sizeof(*attr)); << 1164 attr->ino = sx->ino; << 1165 attr->size = sx->size; << 1166 attr->blocks = sx->blocks; << 1167 attr->atime = sx->atime.tv_sec; << 1168 attr->mtime = sx->mtime.tv_sec; << 1169 attr->ctime = sx->ctime.tv_sec; << 1170 attr->atimensec = sx->atime.tv_nsec; << 1171 attr->mtimensec = sx->mtime.tv_nsec; << 1172 attr->ctimensec = sx->ctime.tv_nsec; << 1173 attr->mode = sx->mode; << 1174 attr->nlink = sx->nlink; << 1175 attr->uid = sx->uid; << 1176 attr->gid = sx->gid; << 1177 attr->rdev = new_encode_dev(MKDEV(sx- << 1178 attr->blksize = sx->blksize; << 1179 } << 1180 << 1181 static int fuse_do_statx(struct inode *inode, << 1182 struct kstat *stat) << 1183 { << 1184 int err; << 1185 struct fuse_attr attr; << 1186 struct fuse_statx *sx; << 1187 struct fuse_statx_in inarg; << 1188 struct fuse_statx_out outarg; << 1189 struct fuse_mount *fm = get_fuse_moun << 1190 u64 attr_version = fuse_get_attr_vers << 1191 FUSE_ARGS(args); << 1192 << 1193 memset(&inarg, 0, sizeof(inarg)); << 1194 memset(&outarg, 0, sizeof(outarg)); << 1195 /* Directories have separate file-han << 1196 if (file && S_ISREG(inode->i_mode)) { << 1197 struct fuse_file *ff = file-> << 1198 << 1199 inarg.getattr_flags |= FUSE_G << 1200 inarg.fh = ff->fh; << 1201 } << 1202 /* For now leave sync hints as the de << 1203 inarg.sx_flags = 0; << 1204 inarg.sx_mask = STATX_BASIC_STATS | S << 1205 args.opcode = FUSE_STATX; << 1206 args.nodeid = get_node_id(inode); << 1207 args.in_numargs = 1; << 1208 args.in_args[0].size = sizeof(inarg); << 1209 args.in_args[0].value = &inarg; << 1210 args.out_numargs = 1; << 1211 args.out_args[0].size = sizeof(outarg << 1212 args.out_args[0].value = &outarg; << 1213 err = fuse_simple_request(fm, &args); << 1214 if (err) << 1215 return err; << 1216 << 1217 sx = &outarg.stat; << 1218 if (((sx->mask & STATX_SIZE) && !fuse << 1219 ((sx->mask & STATX_TYPE) && (!fus << 1220 inod << 1221 fuse_make_bad(inode); << 1222 return -EIO; << 1223 } << 1224 << 1225 fuse_statx_to_attr(&outarg.stat, &att << 1226 if ((sx->mask & STATX_BASIC_STATS) == << 1227 fuse_change_attributes(inode, << 1228 ATTR_T << 1229 } << 1230 << 1231 if (stat) { << 1232 stat->result_mask = sx->mask << 1233 stat->btime.tv_sec = sx->btim << 1234 stat->btime.tv_nsec = min_t(u << 1235 fuse_fillattr(inode, &attr, s << 1236 stat->result_mask |= STATX_TY << 1237 } << 1238 << 1239 return 0; << 1240 } << 1241 << 1242 static int fuse_do_getattr(struct inode *inod 1067 static int fuse_do_getattr(struct inode *inode, struct kstat *stat, 1243 struct file *file) 1068 struct file *file) 1244 { 1069 { 1245 int err; 1070 int err; 1246 struct fuse_getattr_in inarg; 1071 struct fuse_getattr_in inarg; 1247 struct fuse_attr_out outarg; 1072 struct fuse_attr_out outarg; 1248 struct fuse_mount *fm = get_fuse_moun 1073 struct fuse_mount *fm = get_fuse_mount(inode); 1249 FUSE_ARGS(args); 1074 FUSE_ARGS(args); 1250 u64 attr_version; 1075 u64 attr_version; 1251 1076 1252 attr_version = fuse_get_attr_version( 1077 attr_version = fuse_get_attr_version(fm->fc); 1253 1078 1254 memset(&inarg, 0, sizeof(inarg)); 1079 memset(&inarg, 0, sizeof(inarg)); 1255 memset(&outarg, 0, sizeof(outarg)); 1080 memset(&outarg, 0, sizeof(outarg)); 1256 /* Directories have separate file-han 1081 /* Directories have separate file-handle space */ 1257 if (file && S_ISREG(inode->i_mode)) { 1082 if (file && S_ISREG(inode->i_mode)) { 1258 struct fuse_file *ff = file-> 1083 struct fuse_file *ff = file->private_data; 1259 1084 1260 inarg.getattr_flags |= FUSE_G 1085 inarg.getattr_flags |= FUSE_GETATTR_FH; 1261 inarg.fh = ff->fh; 1086 inarg.fh = ff->fh; 1262 } 1087 } 1263 args.opcode = FUSE_GETATTR; 1088 args.opcode = FUSE_GETATTR; 1264 args.nodeid = get_node_id(inode); 1089 args.nodeid = get_node_id(inode); 1265 args.in_numargs = 1; 1090 args.in_numargs = 1; 1266 args.in_args[0].size = sizeof(inarg); 1091 args.in_args[0].size = sizeof(inarg); 1267 args.in_args[0].value = &inarg; 1092 args.in_args[0].value = &inarg; 1268 args.out_numargs = 1; 1093 args.out_numargs = 1; 1269 args.out_args[0].size = sizeof(outarg 1094 args.out_args[0].size = sizeof(outarg); 1270 args.out_args[0].value = &outarg; 1095 args.out_args[0].value = &outarg; 1271 err = fuse_simple_request(fm, &args); 1096 err = fuse_simple_request(fm, &args); 1272 if (!err) { 1097 if (!err) { 1273 if (fuse_invalid_attr(&outarg 1098 if (fuse_invalid_attr(&outarg.attr) || 1274 inode_wrong_type(inode, o 1099 inode_wrong_type(inode, outarg.attr.mode)) { 1275 fuse_make_bad(inode); 1100 fuse_make_bad(inode); 1276 err = -EIO; 1101 err = -EIO; 1277 } else { 1102 } else { 1278 fuse_change_attribute !! 1103 fuse_change_attributes(inode, &outarg.attr, 1279 !! 1104 attr_timeout(&outarg), 1280 1105 attr_version); 1281 if (stat) 1106 if (stat) 1282 fuse_fillattr 1107 fuse_fillattr(inode, &outarg.attr, stat); 1283 } 1108 } 1284 } 1109 } 1285 return err; 1110 return err; 1286 } 1111 } 1287 1112 1288 static int fuse_update_get_attr(struct inode 1113 static int fuse_update_get_attr(struct inode *inode, struct file *file, 1289 struct kstat 1114 struct kstat *stat, u32 request_mask, 1290 unsigned int 1115 unsigned int flags) 1291 { 1116 { 1292 struct fuse_inode *fi = get_fuse_inod 1117 struct fuse_inode *fi = get_fuse_inode(inode); 1293 struct fuse_conn *fc = get_fuse_conn( << 1294 int err = 0; 1118 int err = 0; 1295 bool sync; 1119 bool sync; 1296 u32 inval_mask = READ_ONCE(fi->inval_ 1120 u32 inval_mask = READ_ONCE(fi->inval_mask); 1297 u32 cache_mask = fuse_get_cache_mask( 1121 u32 cache_mask = fuse_get_cache_mask(inode); 1298 1122 1299 !! 1123 if (flags & AT_STATX_FORCE_SYNC) 1300 /* FUSE only supports basic stats and << 1301 request_mask &= STATX_BASIC_STATS | S << 1302 retry: << 1303 if (fc->no_statx) << 1304 request_mask &= STATX_BASIC_S << 1305 << 1306 if (!request_mask) << 1307 sync = false; << 1308 else if (flags & AT_STATX_FORCE_SYNC) << 1309 sync = true; 1124 sync = true; 1310 else if (flags & AT_STATX_DONT_SYNC) 1125 else if (flags & AT_STATX_DONT_SYNC) 1311 sync = false; 1126 sync = false; 1312 else if (request_mask & inval_mask & 1127 else if (request_mask & inval_mask & ~cache_mask) 1313 sync = true; 1128 sync = true; 1314 else 1129 else 1315 sync = time_before64(fi->i_ti 1130 sync = time_before64(fi->i_time, get_jiffies_64()); 1316 1131 1317 if (sync) { 1132 if (sync) { 1318 forget_all_cached_acls(inode) 1133 forget_all_cached_acls(inode); 1319 /* Try statx if BTIME is requ !! 1134 err = fuse_do_getattr(inode, stat, file); 1320 if (!fc->no_statx && (request << 1321 err = fuse_do_statx(i << 1322 if (err == -ENOSYS) { << 1323 fc->no_statx << 1324 err = 0; << 1325 goto retry; << 1326 } << 1327 } else { << 1328 err = fuse_do_getattr << 1329 } << 1330 } else if (stat) { 1135 } else if (stat) { 1331 generic_fillattr(&nop_mnt_idm !! 1136 generic_fillattr(&init_user_ns, inode, stat); 1332 stat->mode = fi->orig_i_mode; 1137 stat->mode = fi->orig_i_mode; 1333 stat->ino = fi->orig_ino; 1138 stat->ino = fi->orig_ino; 1334 if (test_bit(FUSE_I_BTIME, &f << 1335 stat->btime = fi->i_b << 1336 stat->result_mask |= << 1337 } << 1338 } 1139 } 1339 1140 1340 return err; 1141 return err; 1341 } 1142 } 1342 1143 1343 int fuse_update_attributes(struct inode *inod 1144 int fuse_update_attributes(struct inode *inode, struct file *file, u32 mask) 1344 { 1145 { 1345 return fuse_update_get_attr(inode, fi 1146 return fuse_update_get_attr(inode, file, NULL, mask, 0); 1346 } 1147 } 1347 1148 1348 int fuse_reverse_inval_entry(struct fuse_conn 1149 int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid, 1349 u64 child_nodeid !! 1150 u64 child_nodeid, struct qstr *name) 1350 { 1151 { 1351 int err = -ENOTDIR; 1152 int err = -ENOTDIR; 1352 struct inode *parent; 1153 struct inode *parent; 1353 struct dentry *dir; 1154 struct dentry *dir; 1354 struct dentry *entry; 1155 struct dentry *entry; 1355 1156 1356 parent = fuse_ilookup(fc, parent_node 1157 parent = fuse_ilookup(fc, parent_nodeid, NULL); 1357 if (!parent) 1158 if (!parent) 1358 return -ENOENT; 1159 return -ENOENT; 1359 1160 1360 inode_lock_nested(parent, I_MUTEX_PAR 1161 inode_lock_nested(parent, I_MUTEX_PARENT); 1361 if (!S_ISDIR(parent->i_mode)) 1162 if (!S_ISDIR(parent->i_mode)) 1362 goto unlock; 1163 goto unlock; 1363 1164 1364 err = -ENOENT; 1165 err = -ENOENT; 1365 dir = d_find_alias(parent); 1166 dir = d_find_alias(parent); 1366 if (!dir) 1167 if (!dir) 1367 goto unlock; 1168 goto unlock; 1368 1169 1369 name->hash = full_name_hash(dir, name 1170 name->hash = full_name_hash(dir, name->name, name->len); 1370 entry = d_lookup(dir, name); 1171 entry = d_lookup(dir, name); 1371 dput(dir); 1172 dput(dir); 1372 if (!entry) 1173 if (!entry) 1373 goto unlock; 1174 goto unlock; 1374 1175 1375 fuse_dir_changed(parent); 1176 fuse_dir_changed(parent); 1376 if (!(flags & FUSE_EXPIRE_ONLY)) !! 1177 fuse_invalidate_entry(entry); 1377 d_invalidate(entry); << 1378 fuse_invalidate_entry_cache(entry); << 1379 1178 1380 if (child_nodeid != 0 && d_really_is_ 1179 if (child_nodeid != 0 && d_really_is_positive(entry)) { 1381 inode_lock(d_inode(entry)); 1180 inode_lock(d_inode(entry)); 1382 if (get_node_id(d_inode(entry 1181 if (get_node_id(d_inode(entry)) != child_nodeid) { 1383 err = -ENOENT; 1182 err = -ENOENT; 1384 goto badentry; 1183 goto badentry; 1385 } 1184 } 1386 if (d_mountpoint(entry)) { 1185 if (d_mountpoint(entry)) { 1387 err = -EBUSY; 1186 err = -EBUSY; 1388 goto badentry; 1187 goto badentry; 1389 } 1188 } 1390 if (d_is_dir(entry)) { 1189 if (d_is_dir(entry)) { 1391 shrink_dcache_parent( 1190 shrink_dcache_parent(entry); 1392 if (!simple_empty(ent 1191 if (!simple_empty(entry)) { 1393 err = -ENOTEM 1192 err = -ENOTEMPTY; 1394 goto badentry 1193 goto badentry; 1395 } 1194 } 1396 d_inode(entry)->i_fla 1195 d_inode(entry)->i_flags |= S_DEAD; 1397 } 1196 } 1398 dont_mount(entry); 1197 dont_mount(entry); 1399 clear_nlink(d_inode(entry)); 1198 clear_nlink(d_inode(entry)); 1400 err = 0; 1199 err = 0; 1401 badentry: 1200 badentry: 1402 inode_unlock(d_inode(entry)); 1201 inode_unlock(d_inode(entry)); 1403 if (!err) 1202 if (!err) 1404 d_delete(entry); 1203 d_delete(entry); 1405 } else { 1204 } else { 1406 err = 0; 1205 err = 0; 1407 } 1206 } 1408 dput(entry); 1207 dput(entry); 1409 1208 1410 unlock: 1209 unlock: 1411 inode_unlock(parent); 1210 inode_unlock(parent); 1412 iput(parent); 1211 iput(parent); 1413 return err; 1212 return err; 1414 } 1213 } 1415 1214 1416 static inline bool fuse_permissible_uidgid(st << 1417 { << 1418 const struct cred *cred = current_cre << 1419 << 1420 return (uid_eq(cred->euid, fc->user_i << 1421 uid_eq(cred->suid, fc->user_i << 1422 uid_eq(cred->uid, fc->user_i << 1423 gid_eq(cred->egid, fc->group_ << 1424 gid_eq(cred->sgid, fc->group_ << 1425 gid_eq(cred->gid, fc->group_ << 1426 } << 1427 << 1428 /* 1215 /* 1429 * Calling into a user-controlled filesystem 1216 * Calling into a user-controlled filesystem gives the filesystem 1430 * daemon ptrace-like capabilities over the c 1217 * daemon ptrace-like capabilities over the current process. This 1431 * means, that the filesystem daemon is able 1218 * means, that the filesystem daemon is able to record the exact 1432 * filesystem operations performed, and can a 1219 * filesystem operations performed, and can also control the behavior 1433 * of the requester process in otherwise impo 1220 * of the requester process in otherwise impossible ways. For example 1434 * it can delay the operation for arbitrary l 1221 * it can delay the operation for arbitrary length of time allowing 1435 * DoS against the requester. 1222 * DoS against the requester. 1436 * 1223 * 1437 * For this reason only those processes can c 1224 * For this reason only those processes can call into the filesystem, 1438 * for which the owner of the mount has ptrac 1225 * for which the owner of the mount has ptrace privilege. This 1439 * excludes processes started by other users, 1226 * excludes processes started by other users, suid or sgid processes. 1440 */ 1227 */ 1441 bool fuse_allow_current_process(struct fuse_c !! 1228 int fuse_allow_current_process(struct fuse_conn *fc) 1442 { 1229 { 1443 bool allow; !! 1230 const struct cred *cred; 1444 1231 1445 if (fc->allow_other) 1232 if (fc->allow_other) 1446 allow = current_in_userns(fc- !! 1233 return current_in_userns(fc->user_ns); 1447 else << 1448 allow = fuse_permissible_uidg << 1449 1234 1450 if (!allow && allow_sys_admin_access !! 1235 cred = current_cred(); 1451 allow = true; !! 1236 if (uid_eq(cred->euid, fc->user_id) && >> 1237 uid_eq(cred->suid, fc->user_id) && >> 1238 uid_eq(cred->uid, fc->user_id) && >> 1239 gid_eq(cred->egid, fc->group_id) && >> 1240 gid_eq(cred->sgid, fc->group_id) && >> 1241 gid_eq(cred->gid, fc->group_id)) >> 1242 return 1; 1452 1243 1453 return allow; !! 1244 return 0; 1454 } 1245 } 1455 1246 1456 static int fuse_access(struct inode *inode, i 1247 static int fuse_access(struct inode *inode, int mask) 1457 { 1248 { 1458 struct fuse_mount *fm = get_fuse_moun 1249 struct fuse_mount *fm = get_fuse_mount(inode); 1459 FUSE_ARGS(args); 1250 FUSE_ARGS(args); 1460 struct fuse_access_in inarg; 1251 struct fuse_access_in inarg; 1461 int err; 1252 int err; 1462 1253 1463 BUG_ON(mask & MAY_NOT_BLOCK); 1254 BUG_ON(mask & MAY_NOT_BLOCK); 1464 1255 1465 if (fm->fc->no_access) 1256 if (fm->fc->no_access) 1466 return 0; 1257 return 0; 1467 1258 1468 memset(&inarg, 0, sizeof(inarg)); 1259 memset(&inarg, 0, sizeof(inarg)); 1469 inarg.mask = mask & (MAY_READ | MAY_W 1260 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC); 1470 args.opcode = FUSE_ACCESS; 1261 args.opcode = FUSE_ACCESS; 1471 args.nodeid = get_node_id(inode); 1262 args.nodeid = get_node_id(inode); 1472 args.in_numargs = 1; 1263 args.in_numargs = 1; 1473 args.in_args[0].size = sizeof(inarg); 1264 args.in_args[0].size = sizeof(inarg); 1474 args.in_args[0].value = &inarg; 1265 args.in_args[0].value = &inarg; 1475 err = fuse_simple_request(fm, &args); 1266 err = fuse_simple_request(fm, &args); 1476 if (err == -ENOSYS) { 1267 if (err == -ENOSYS) { 1477 fm->fc->no_access = 1; 1268 fm->fc->no_access = 1; 1478 err = 0; 1269 err = 0; 1479 } 1270 } 1480 return err; 1271 return err; 1481 } 1272 } 1482 1273 1483 static int fuse_perm_getattr(struct inode *in 1274 static int fuse_perm_getattr(struct inode *inode, int mask) 1484 { 1275 { 1485 if (mask & MAY_NOT_BLOCK) 1276 if (mask & MAY_NOT_BLOCK) 1486 return -ECHILD; 1277 return -ECHILD; 1487 1278 1488 forget_all_cached_acls(inode); 1279 forget_all_cached_acls(inode); 1489 return fuse_do_getattr(inode, NULL, N 1280 return fuse_do_getattr(inode, NULL, NULL); 1490 } 1281 } 1491 1282 1492 /* 1283 /* 1493 * Check permission. The two basic access mo 1284 * Check permission. The two basic access models of FUSE are: 1494 * 1285 * 1495 * 1) Local access checking ('default_permiss 1286 * 1) Local access checking ('default_permissions' mount option) based 1496 * on file mode. This is the plain old disk 1287 * on file mode. This is the plain old disk filesystem permission 1497 * model. !! 1288 * modell. 1498 * 1289 * 1499 * 2) "Remote" access checking, where server 1290 * 2) "Remote" access checking, where server is responsible for 1500 * checking permission in each inode operatio 1291 * checking permission in each inode operation. An exception to this 1501 * is if ->permission() was invoked from sys_ 1292 * is if ->permission() was invoked from sys_access() in which case an 1502 * access request is sent. Execute permissio 1293 * access request is sent. Execute permission is still checked 1503 * locally based on file mode. 1294 * locally based on file mode. 1504 */ 1295 */ 1505 static int fuse_permission(struct mnt_idmap * !! 1296 static int fuse_permission(struct user_namespace *mnt_userns, 1506 struct inode *inod 1297 struct inode *inode, int mask) 1507 { 1298 { 1508 struct fuse_conn *fc = get_fuse_conn( 1299 struct fuse_conn *fc = get_fuse_conn(inode); 1509 bool refreshed = false; 1300 bool refreshed = false; 1510 int err = 0; 1301 int err = 0; 1511 1302 1512 if (fuse_is_bad(inode)) 1303 if (fuse_is_bad(inode)) 1513 return -EIO; 1304 return -EIO; 1514 1305 1515 if (!fuse_allow_current_process(fc)) 1306 if (!fuse_allow_current_process(fc)) 1516 return -EACCES; 1307 return -EACCES; 1517 1308 1518 /* 1309 /* 1519 * If attributes are needed, refresh 1310 * If attributes are needed, refresh them before proceeding 1520 */ 1311 */ 1521 if (fc->default_permissions || 1312 if (fc->default_permissions || 1522 ((mask & MAY_EXEC) && S_ISREG(ino 1313 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) { 1523 struct fuse_inode *fi = get_f 1314 struct fuse_inode *fi = get_fuse_inode(inode); 1524 u32 perm_mask = STATX_MODE | 1315 u32 perm_mask = STATX_MODE | STATX_UID | STATX_GID; 1525 1316 1526 if (perm_mask & READ_ONCE(fi- 1317 if (perm_mask & READ_ONCE(fi->inval_mask) || 1527 time_before64(fi->i_time, 1318 time_before64(fi->i_time, get_jiffies_64())) { 1528 refreshed = true; 1319 refreshed = true; 1529 1320 1530 err = fuse_perm_getat 1321 err = fuse_perm_getattr(inode, mask); 1531 if (err) 1322 if (err) 1532 return err; 1323 return err; 1533 } 1324 } 1534 } 1325 } 1535 1326 1536 if (fc->default_permissions) { 1327 if (fc->default_permissions) { 1537 err = generic_permission(&nop !! 1328 err = generic_permission(&init_user_ns, inode, mask); 1538 1329 1539 /* If permission is denied, t 1330 /* If permission is denied, try to refresh file 1540 attributes. This is also 1331 attributes. This is also needed, because the root 1541 node will at first have no 1332 node will at first have no permissions */ 1542 if (err == -EACCES && !refres 1333 if (err == -EACCES && !refreshed) { 1543 err = fuse_perm_getat 1334 err = fuse_perm_getattr(inode, mask); 1544 if (!err) 1335 if (!err) 1545 err = generic !! 1336 err = generic_permission(&init_user_ns, 1546 1337 inode, mask); 1547 } 1338 } 1548 1339 1549 /* Note: the opposite of the 1340 /* Note: the opposite of the above test does not 1550 exist. So if permissions 1341 exist. So if permissions are revoked this won't be 1551 noticed immediately, only 1342 noticed immediately, only after the attribute 1552 timeout has expired */ 1343 timeout has expired */ 1553 } else if (mask & (MAY_ACCESS | MAY_C 1344 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) { 1554 err = fuse_access(inode, mask 1345 err = fuse_access(inode, mask); 1555 } else if ((mask & MAY_EXEC) && S_ISR 1346 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) { 1556 if (!(inode->i_mode & S_IXUGO 1347 if (!(inode->i_mode & S_IXUGO)) { 1557 if (refreshed) 1348 if (refreshed) 1558 return -EACCE 1349 return -EACCES; 1559 1350 1560 err = fuse_perm_getat 1351 err = fuse_perm_getattr(inode, mask); 1561 if (!err && !(inode-> 1352 if (!err && !(inode->i_mode & S_IXUGO)) 1562 return -EACCE 1353 return -EACCES; 1563 } 1354 } 1564 } 1355 } 1565 return err; 1356 return err; 1566 } 1357 } 1567 1358 1568 static int fuse_readlink_page(struct inode *i 1359 static int fuse_readlink_page(struct inode *inode, struct page *page) 1569 { 1360 { 1570 struct fuse_mount *fm = get_fuse_moun 1361 struct fuse_mount *fm = get_fuse_mount(inode); 1571 struct fuse_page_desc desc = { .lengt 1362 struct fuse_page_desc desc = { .length = PAGE_SIZE - 1 }; 1572 struct fuse_args_pages ap = { 1363 struct fuse_args_pages ap = { 1573 .num_pages = 1, 1364 .num_pages = 1, 1574 .pages = &page, 1365 .pages = &page, 1575 .descs = &desc, 1366 .descs = &desc, 1576 }; 1367 }; 1577 char *link; 1368 char *link; 1578 ssize_t res; 1369 ssize_t res; 1579 1370 1580 ap.args.opcode = FUSE_READLINK; 1371 ap.args.opcode = FUSE_READLINK; 1581 ap.args.nodeid = get_node_id(inode); 1372 ap.args.nodeid = get_node_id(inode); 1582 ap.args.out_pages = true; 1373 ap.args.out_pages = true; 1583 ap.args.out_argvar = true; 1374 ap.args.out_argvar = true; 1584 ap.args.page_zeroing = true; 1375 ap.args.page_zeroing = true; 1585 ap.args.out_numargs = 1; 1376 ap.args.out_numargs = 1; 1586 ap.args.out_args[0].size = desc.lengt 1377 ap.args.out_args[0].size = desc.length; 1587 res = fuse_simple_request(fm, &ap.arg 1378 res = fuse_simple_request(fm, &ap.args); 1588 1379 1589 fuse_invalidate_atime(inode); 1380 fuse_invalidate_atime(inode); 1590 1381 1591 if (res < 0) 1382 if (res < 0) 1592 return res; 1383 return res; 1593 1384 1594 if (WARN_ON(res >= PAGE_SIZE)) 1385 if (WARN_ON(res >= PAGE_SIZE)) 1595 return -EIO; 1386 return -EIO; 1596 1387 1597 link = page_address(page); 1388 link = page_address(page); 1598 link[res] = '\0'; 1389 link[res] = '\0'; 1599 1390 1600 return 0; 1391 return 0; 1601 } 1392 } 1602 1393 1603 static const char *fuse_get_link(struct dentr 1394 static const char *fuse_get_link(struct dentry *dentry, struct inode *inode, 1604 struct delay 1395 struct delayed_call *callback) 1605 { 1396 { 1606 struct fuse_conn *fc = get_fuse_conn( 1397 struct fuse_conn *fc = get_fuse_conn(inode); 1607 struct page *page; 1398 struct page *page; 1608 int err; 1399 int err; 1609 1400 1610 err = -EIO; 1401 err = -EIO; 1611 if (fuse_is_bad(inode)) 1402 if (fuse_is_bad(inode)) 1612 goto out_err; 1403 goto out_err; 1613 1404 1614 if (fc->cache_symlinks) 1405 if (fc->cache_symlinks) 1615 return page_get_link(dentry, 1406 return page_get_link(dentry, inode, callback); 1616 1407 1617 err = -ECHILD; 1408 err = -ECHILD; 1618 if (!dentry) 1409 if (!dentry) 1619 goto out_err; 1410 goto out_err; 1620 1411 1621 page = alloc_page(GFP_KERNEL); 1412 page = alloc_page(GFP_KERNEL); 1622 err = -ENOMEM; 1413 err = -ENOMEM; 1623 if (!page) 1414 if (!page) 1624 goto out_err; 1415 goto out_err; 1625 1416 1626 err = fuse_readlink_page(inode, page) 1417 err = fuse_readlink_page(inode, page); 1627 if (err) { 1418 if (err) { 1628 __free_page(page); 1419 __free_page(page); 1629 goto out_err; 1420 goto out_err; 1630 } 1421 } 1631 1422 1632 set_delayed_call(callback, page_put_l 1423 set_delayed_call(callback, page_put_link, page); 1633 1424 1634 return page_address(page); 1425 return page_address(page); 1635 1426 1636 out_err: 1427 out_err: 1637 return ERR_PTR(err); 1428 return ERR_PTR(err); 1638 } 1429 } 1639 1430 1640 static int fuse_dir_open(struct inode *inode, 1431 static int fuse_dir_open(struct inode *inode, struct file *file) 1641 { 1432 { 1642 struct fuse_mount *fm = get_fuse_moun !! 1433 return fuse_open_common(inode, file, true); 1643 int err; << 1644 << 1645 if (fuse_is_bad(inode)) << 1646 return -EIO; << 1647 << 1648 err = generic_file_open(inode, file); << 1649 if (err) << 1650 return err; << 1651 << 1652 err = fuse_do_open(fm, get_node_id(in << 1653 if (!err) { << 1654 struct fuse_file *ff = file-> << 1655 << 1656 /* << 1657 * Keep handling FOPEN_STREAM << 1658 * directories for backward c << 1659 * to be useful. << 1660 */ << 1661 if (ff->open_flags & (FOPEN_S << 1662 nonseekable_open(inod << 1663 } << 1664 << 1665 return err; << 1666 } 1434 } 1667 1435 1668 static int fuse_dir_release(struct inode *ino 1436 static int fuse_dir_release(struct inode *inode, struct file *file) 1669 { 1437 { 1670 fuse_release_common(file, true); 1438 fuse_release_common(file, true); 1671 1439 1672 return 0; 1440 return 0; 1673 } 1441 } 1674 1442 1675 static int fuse_dir_fsync(struct file *file, 1443 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end, 1676 int datasync) 1444 int datasync) 1677 { 1445 { 1678 struct inode *inode = file->f_mapping 1446 struct inode *inode = file->f_mapping->host; 1679 struct fuse_conn *fc = get_fuse_conn( 1447 struct fuse_conn *fc = get_fuse_conn(inode); 1680 int err; 1448 int err; 1681 1449 1682 if (fuse_is_bad(inode)) 1450 if (fuse_is_bad(inode)) 1683 return -EIO; 1451 return -EIO; 1684 1452 1685 if (fc->no_fsyncdir) 1453 if (fc->no_fsyncdir) 1686 return 0; 1454 return 0; 1687 1455 1688 inode_lock(inode); 1456 inode_lock(inode); 1689 err = fuse_fsync_common(file, start, 1457 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNCDIR); 1690 if (err == -ENOSYS) { 1458 if (err == -ENOSYS) { 1691 fc->no_fsyncdir = 1; 1459 fc->no_fsyncdir = 1; 1692 err = 0; 1460 err = 0; 1693 } 1461 } 1694 inode_unlock(inode); 1462 inode_unlock(inode); 1695 1463 1696 return err; 1464 return err; 1697 } 1465 } 1698 1466 1699 static long fuse_dir_ioctl(struct file *file, 1467 static long fuse_dir_ioctl(struct file *file, unsigned int cmd, 1700 unsigned long arg 1468 unsigned long arg) 1701 { 1469 { 1702 struct fuse_conn *fc = get_fuse_conn( 1470 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host); 1703 1471 1704 /* FUSE_IOCTL_DIR only supported for 1472 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */ 1705 if (fc->minor < 18) 1473 if (fc->minor < 18) 1706 return -ENOTTY; 1474 return -ENOTTY; 1707 1475 1708 return fuse_ioctl_common(file, cmd, a 1476 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR); 1709 } 1477 } 1710 1478 1711 static long fuse_dir_compat_ioctl(struct file 1479 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd, 1712 unsigned l 1480 unsigned long arg) 1713 { 1481 { 1714 struct fuse_conn *fc = get_fuse_conn( 1482 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host); 1715 1483 1716 if (fc->minor < 18) 1484 if (fc->minor < 18) 1717 return -ENOTTY; 1485 return -ENOTTY; 1718 1486 1719 return fuse_ioctl_common(file, cmd, a 1487 return fuse_ioctl_common(file, cmd, arg, 1720 FUSE_IOCTL_C 1488 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR); 1721 } 1489 } 1722 1490 1723 static bool update_mtime(unsigned ivalid, boo 1491 static bool update_mtime(unsigned ivalid, bool trust_local_mtime) 1724 { 1492 { 1725 /* Always update if mtime is explicit 1493 /* Always update if mtime is explicitly set */ 1726 if (ivalid & ATTR_MTIME_SET) 1494 if (ivalid & ATTR_MTIME_SET) 1727 return true; 1495 return true; 1728 1496 1729 /* Or if kernel i_mtime is the offici 1497 /* Or if kernel i_mtime is the official one */ 1730 if (trust_local_mtime) 1498 if (trust_local_mtime) 1731 return true; 1499 return true; 1732 1500 1733 /* If it's an open(O_TRUNC) or an ftr 1501 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */ 1734 if ((ivalid & ATTR_SIZE) && (ivalid & 1502 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE))) 1735 return false; 1503 return false; 1736 1504 1737 /* In all other cases update */ 1505 /* In all other cases update */ 1738 return true; 1506 return true; 1739 } 1507 } 1740 1508 1741 static void iattr_to_fattr(struct fuse_conn * 1509 static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr, 1742 struct fuse_setatt 1510 struct fuse_setattr_in *arg, bool trust_local_cmtime) 1743 { 1511 { 1744 unsigned ivalid = iattr->ia_valid; 1512 unsigned ivalid = iattr->ia_valid; 1745 1513 1746 if (ivalid & ATTR_MODE) 1514 if (ivalid & ATTR_MODE) 1747 arg->valid |= FATTR_MODE, a 1515 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode; 1748 if (ivalid & ATTR_UID) 1516 if (ivalid & ATTR_UID) 1749 arg->valid |= FATTR_UID, a 1517 arg->valid |= FATTR_UID, arg->uid = from_kuid(fc->user_ns, iattr->ia_uid); 1750 if (ivalid & ATTR_GID) 1518 if (ivalid & ATTR_GID) 1751 arg->valid |= FATTR_GID, a 1519 arg->valid |= FATTR_GID, arg->gid = from_kgid(fc->user_ns, iattr->ia_gid); 1752 if (ivalid & ATTR_SIZE) 1520 if (ivalid & ATTR_SIZE) 1753 arg->valid |= FATTR_SIZE, a 1521 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size; 1754 if (ivalid & ATTR_ATIME) { 1522 if (ivalid & ATTR_ATIME) { 1755 arg->valid |= FATTR_ATIME; 1523 arg->valid |= FATTR_ATIME; 1756 arg->atime = iattr->ia_atime. 1524 arg->atime = iattr->ia_atime.tv_sec; 1757 arg->atimensec = iattr->ia_at 1525 arg->atimensec = iattr->ia_atime.tv_nsec; 1758 if (!(ivalid & ATTR_ATIME_SET 1526 if (!(ivalid & ATTR_ATIME_SET)) 1759 arg->valid |= FATTR_A 1527 arg->valid |= FATTR_ATIME_NOW; 1760 } 1528 } 1761 if ((ivalid & ATTR_MTIME) && update_m 1529 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) { 1762 arg->valid |= FATTR_MTIME; 1530 arg->valid |= FATTR_MTIME; 1763 arg->mtime = iattr->ia_mtime. 1531 arg->mtime = iattr->ia_mtime.tv_sec; 1764 arg->mtimensec = iattr->ia_mt 1532 arg->mtimensec = iattr->ia_mtime.tv_nsec; 1765 if (!(ivalid & ATTR_MTIME_SET 1533 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime) 1766 arg->valid |= FATTR_M 1534 arg->valid |= FATTR_MTIME_NOW; 1767 } 1535 } 1768 if ((ivalid & ATTR_CTIME) && trust_lo 1536 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) { 1769 arg->valid |= FATTR_CTIME; 1537 arg->valid |= FATTR_CTIME; 1770 arg->ctime = iattr->ia_ctime. 1538 arg->ctime = iattr->ia_ctime.tv_sec; 1771 arg->ctimensec = iattr->ia_ct 1539 arg->ctimensec = iattr->ia_ctime.tv_nsec; 1772 } 1540 } 1773 } 1541 } 1774 1542 1775 /* 1543 /* 1776 * Prevent concurrent writepages on inode 1544 * Prevent concurrent writepages on inode 1777 * 1545 * 1778 * This is done by adding a negative bias to 1546 * This is done by adding a negative bias to the inode write counter 1779 * and waiting for all pending writes to fini 1547 * and waiting for all pending writes to finish. 1780 */ 1548 */ 1781 void fuse_set_nowrite(struct inode *inode) 1549 void fuse_set_nowrite(struct inode *inode) 1782 { 1550 { 1783 struct fuse_inode *fi = get_fuse_inod 1551 struct fuse_inode *fi = get_fuse_inode(inode); 1784 1552 1785 BUG_ON(!inode_is_locked(inode)); 1553 BUG_ON(!inode_is_locked(inode)); 1786 1554 1787 spin_lock(&fi->lock); 1555 spin_lock(&fi->lock); 1788 BUG_ON(fi->writectr < 0); 1556 BUG_ON(fi->writectr < 0); 1789 fi->writectr += FUSE_NOWRITE; 1557 fi->writectr += FUSE_NOWRITE; 1790 spin_unlock(&fi->lock); 1558 spin_unlock(&fi->lock); 1791 wait_event(fi->page_waitq, fi->writec 1559 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE); 1792 } 1560 } 1793 1561 1794 /* 1562 /* 1795 * Allow writepages on inode 1563 * Allow writepages on inode 1796 * 1564 * 1797 * Remove the bias from the writecounter and 1565 * Remove the bias from the writecounter and send any queued 1798 * writepages. 1566 * writepages. 1799 */ 1567 */ 1800 static void __fuse_release_nowrite(struct ino 1568 static void __fuse_release_nowrite(struct inode *inode) 1801 { 1569 { 1802 struct fuse_inode *fi = get_fuse_inod 1570 struct fuse_inode *fi = get_fuse_inode(inode); 1803 1571 1804 BUG_ON(fi->writectr != FUSE_NOWRITE); 1572 BUG_ON(fi->writectr != FUSE_NOWRITE); 1805 fi->writectr = 0; 1573 fi->writectr = 0; 1806 fuse_flush_writepages(inode); 1574 fuse_flush_writepages(inode); 1807 } 1575 } 1808 1576 1809 void fuse_release_nowrite(struct inode *inode 1577 void fuse_release_nowrite(struct inode *inode) 1810 { 1578 { 1811 struct fuse_inode *fi = get_fuse_inod 1579 struct fuse_inode *fi = get_fuse_inode(inode); 1812 1580 1813 spin_lock(&fi->lock); 1581 spin_lock(&fi->lock); 1814 __fuse_release_nowrite(inode); 1582 __fuse_release_nowrite(inode); 1815 spin_unlock(&fi->lock); 1583 spin_unlock(&fi->lock); 1816 } 1584 } 1817 1585 1818 static void fuse_setattr_fill(struct fuse_con 1586 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args, 1819 struct inode *i 1587 struct inode *inode, 1820 struct fuse_set 1588 struct fuse_setattr_in *inarg_p, 1821 struct fuse_att 1589 struct fuse_attr_out *outarg_p) 1822 { 1590 { 1823 args->opcode = FUSE_SETATTR; 1591 args->opcode = FUSE_SETATTR; 1824 args->nodeid = get_node_id(inode); 1592 args->nodeid = get_node_id(inode); 1825 args->in_numargs = 1; 1593 args->in_numargs = 1; 1826 args->in_args[0].size = sizeof(*inarg 1594 args->in_args[0].size = sizeof(*inarg_p); 1827 args->in_args[0].value = inarg_p; 1595 args->in_args[0].value = inarg_p; 1828 args->out_numargs = 1; 1596 args->out_numargs = 1; 1829 args->out_args[0].size = sizeof(*outa 1597 args->out_args[0].size = sizeof(*outarg_p); 1830 args->out_args[0].value = outarg_p; 1598 args->out_args[0].value = outarg_p; 1831 } 1599 } 1832 1600 1833 /* 1601 /* 1834 * Flush inode->i_mtime to the server 1602 * Flush inode->i_mtime to the server 1835 */ 1603 */ 1836 int fuse_flush_times(struct inode *inode, str 1604 int fuse_flush_times(struct inode *inode, struct fuse_file *ff) 1837 { 1605 { 1838 struct fuse_mount *fm = get_fuse_moun 1606 struct fuse_mount *fm = get_fuse_mount(inode); 1839 FUSE_ARGS(args); 1607 FUSE_ARGS(args); 1840 struct fuse_setattr_in inarg; 1608 struct fuse_setattr_in inarg; 1841 struct fuse_attr_out outarg; 1609 struct fuse_attr_out outarg; 1842 1610 1843 memset(&inarg, 0, sizeof(inarg)); 1611 memset(&inarg, 0, sizeof(inarg)); 1844 memset(&outarg, 0, sizeof(outarg)); 1612 memset(&outarg, 0, sizeof(outarg)); 1845 1613 1846 inarg.valid = FATTR_MTIME; 1614 inarg.valid = FATTR_MTIME; 1847 inarg.mtime = inode_get_mtime_sec(ino !! 1615 inarg.mtime = inode->i_mtime.tv_sec; 1848 inarg.mtimensec = inode_get_mtime_nse !! 1616 inarg.mtimensec = inode->i_mtime.tv_nsec; 1849 if (fm->fc->minor >= 23) { 1617 if (fm->fc->minor >= 23) { 1850 inarg.valid |= FATTR_CTIME; 1618 inarg.valid |= FATTR_CTIME; 1851 inarg.ctime = inode_get_ctime !! 1619 inarg.ctime = inode->i_ctime.tv_sec; 1852 inarg.ctimensec = inode_get_c !! 1620 inarg.ctimensec = inode->i_ctime.tv_nsec; 1853 } 1621 } 1854 if (ff) { 1622 if (ff) { 1855 inarg.valid |= FATTR_FH; 1623 inarg.valid |= FATTR_FH; 1856 inarg.fh = ff->fh; 1624 inarg.fh = ff->fh; 1857 } 1625 } 1858 fuse_setattr_fill(fm->fc, &args, inod 1626 fuse_setattr_fill(fm->fc, &args, inode, &inarg, &outarg); 1859 1627 1860 return fuse_simple_request(fm, &args) 1628 return fuse_simple_request(fm, &args); 1861 } 1629 } 1862 1630 1863 /* 1631 /* 1864 * Set attributes, and at the same time refre 1632 * Set attributes, and at the same time refresh them. 1865 * 1633 * 1866 * Truncation is slightly complicated, becaus 1634 * Truncation is slightly complicated, because the 'truncate' request 1867 * may fail, in which case we don't want to t 1635 * may fail, in which case we don't want to touch the mapping. 1868 * vmtruncate() doesn't allow for this case, 1636 * vmtruncate() doesn't allow for this case, so do the rlimit checking 1869 * and the actual truncation by hand. 1637 * and the actual truncation by hand. 1870 */ 1638 */ 1871 int fuse_do_setattr(struct dentry *dentry, st 1639 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr, 1872 struct file *file) 1640 struct file *file) 1873 { 1641 { 1874 struct inode *inode = d_inode(dentry) 1642 struct inode *inode = d_inode(dentry); 1875 struct fuse_mount *fm = get_fuse_moun 1643 struct fuse_mount *fm = get_fuse_mount(inode); 1876 struct fuse_conn *fc = fm->fc; 1644 struct fuse_conn *fc = fm->fc; 1877 struct fuse_inode *fi = get_fuse_inod 1645 struct fuse_inode *fi = get_fuse_inode(inode); 1878 struct address_space *mapping = inode 1646 struct address_space *mapping = inode->i_mapping; 1879 FUSE_ARGS(args); 1647 FUSE_ARGS(args); 1880 struct fuse_setattr_in inarg; 1648 struct fuse_setattr_in inarg; 1881 struct fuse_attr_out outarg; 1649 struct fuse_attr_out outarg; 1882 bool is_truncate = false; 1650 bool is_truncate = false; 1883 bool is_wb = fc->writeback_cache && S 1651 bool is_wb = fc->writeback_cache && S_ISREG(inode->i_mode); 1884 loff_t oldsize; 1652 loff_t oldsize; 1885 int err; 1653 int err; 1886 bool trust_local_cmtime = is_wb; 1654 bool trust_local_cmtime = is_wb; 1887 bool fault_blocked = false; 1655 bool fault_blocked = false; 1888 1656 1889 if (!fc->default_permissions) 1657 if (!fc->default_permissions) 1890 attr->ia_valid |= ATTR_FORCE; 1658 attr->ia_valid |= ATTR_FORCE; 1891 1659 1892 err = setattr_prepare(&nop_mnt_idmap, !! 1660 err = setattr_prepare(&init_user_ns, dentry, attr); 1893 if (err) 1661 if (err) 1894 return err; 1662 return err; 1895 1663 1896 if (attr->ia_valid & ATTR_SIZE) { 1664 if (attr->ia_valid & ATTR_SIZE) { 1897 if (WARN_ON(!S_ISREG(inode->i 1665 if (WARN_ON(!S_ISREG(inode->i_mode))) 1898 return -EIO; 1666 return -EIO; 1899 is_truncate = true; 1667 is_truncate = true; 1900 } 1668 } 1901 1669 1902 if (FUSE_IS_DAX(inode) && is_truncate 1670 if (FUSE_IS_DAX(inode) && is_truncate) { 1903 filemap_invalidate_lock(mappi 1671 filemap_invalidate_lock(mapping); 1904 fault_blocked = true; 1672 fault_blocked = true; 1905 err = fuse_dax_break_layouts( 1673 err = fuse_dax_break_layouts(inode, 0, 0); 1906 if (err) { 1674 if (err) { 1907 filemap_invalidate_un 1675 filemap_invalidate_unlock(mapping); 1908 return err; 1676 return err; 1909 } 1677 } 1910 } 1678 } 1911 1679 1912 if (attr->ia_valid & ATTR_OPEN) { 1680 if (attr->ia_valid & ATTR_OPEN) { 1913 /* This is coming from open(. 1681 /* This is coming from open(..., ... | O_TRUNC); */ 1914 WARN_ON(!(attr->ia_valid & AT 1682 WARN_ON(!(attr->ia_valid & ATTR_SIZE)); 1915 WARN_ON(attr->ia_size != 0); 1683 WARN_ON(attr->ia_size != 0); 1916 if (fc->atomic_o_trunc) { 1684 if (fc->atomic_o_trunc) { 1917 /* 1685 /* 1918 * No need to send re 1686 * No need to send request to userspace, since actual 1919 * truncation has alr 1687 * truncation has already been done by OPEN. But still 1920 * need to truncate p 1688 * need to truncate page cache. 1921 */ 1689 */ 1922 i_size_write(inode, 0 1690 i_size_write(inode, 0); 1923 truncate_pagecache(in 1691 truncate_pagecache(inode, 0); 1924 goto out; 1692 goto out; 1925 } 1693 } 1926 file = NULL; 1694 file = NULL; 1927 } 1695 } 1928 1696 1929 /* Flush dirty data/metadata before n 1697 /* Flush dirty data/metadata before non-truncate SETATTR */ 1930 if (is_wb && 1698 if (is_wb && 1931 attr->ia_valid & 1699 attr->ia_valid & 1932 (ATTR_MODE | ATTR_UID 1700 (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET | 1933 ATTR_TIMES_SET)) { 1701 ATTR_TIMES_SET)) { 1934 err = write_inode_now(inode, 1702 err = write_inode_now(inode, true); 1935 if (err) 1703 if (err) 1936 return err; 1704 return err; 1937 1705 1938 fuse_set_nowrite(inode); 1706 fuse_set_nowrite(inode); 1939 fuse_release_nowrite(inode); 1707 fuse_release_nowrite(inode); 1940 } 1708 } 1941 1709 1942 if (is_truncate) { 1710 if (is_truncate) { 1943 fuse_set_nowrite(inode); 1711 fuse_set_nowrite(inode); 1944 set_bit(FUSE_I_SIZE_UNSTABLE, 1712 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 1945 if (trust_local_cmtime && att 1713 if (trust_local_cmtime && attr->ia_size != inode->i_size) 1946 attr->ia_valid |= ATT 1714 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME; 1947 } 1715 } 1948 1716 1949 memset(&inarg, 0, sizeof(inarg)); 1717 memset(&inarg, 0, sizeof(inarg)); 1950 memset(&outarg, 0, sizeof(outarg)); 1718 memset(&outarg, 0, sizeof(outarg)); 1951 iattr_to_fattr(fc, attr, &inarg, trus 1719 iattr_to_fattr(fc, attr, &inarg, trust_local_cmtime); 1952 if (file) { 1720 if (file) { 1953 struct fuse_file *ff = file-> 1721 struct fuse_file *ff = file->private_data; 1954 inarg.valid |= FATTR_FH; 1722 inarg.valid |= FATTR_FH; 1955 inarg.fh = ff->fh; 1723 inarg.fh = ff->fh; 1956 } 1724 } 1957 1725 1958 /* Kill suid/sgid for non-directory c 1726 /* Kill suid/sgid for non-directory chown unconditionally */ 1959 if (fc->handle_killpriv_v2 && !S_ISDI 1727 if (fc->handle_killpriv_v2 && !S_ISDIR(inode->i_mode) && 1960 attr->ia_valid & (ATTR_UID | ATTR 1728 attr->ia_valid & (ATTR_UID | ATTR_GID)) 1961 inarg.valid |= FATTR_KILL_SUI 1729 inarg.valid |= FATTR_KILL_SUIDGID; 1962 1730 1963 if (attr->ia_valid & ATTR_SIZE) { 1731 if (attr->ia_valid & ATTR_SIZE) { 1964 /* For mandatory locking in t 1732 /* For mandatory locking in truncate */ 1965 inarg.valid |= FATTR_LOCKOWNE 1733 inarg.valid |= FATTR_LOCKOWNER; 1966 inarg.lock_owner = fuse_lock_ 1734 inarg.lock_owner = fuse_lock_owner_id(fc, current->files); 1967 1735 1968 /* Kill suid/sgid for truncat 1736 /* Kill suid/sgid for truncate only if no CAP_FSETID */ 1969 if (fc->handle_killpriv_v2 && 1737 if (fc->handle_killpriv_v2 && !capable(CAP_FSETID)) 1970 inarg.valid |= FATTR_ 1738 inarg.valid |= FATTR_KILL_SUIDGID; 1971 } 1739 } 1972 fuse_setattr_fill(fc, &args, inode, & 1740 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg); 1973 err = fuse_simple_request(fm, &args); 1741 err = fuse_simple_request(fm, &args); 1974 if (err) { 1742 if (err) { 1975 if (err == -EINTR) 1743 if (err == -EINTR) 1976 fuse_invalidate_attr( 1744 fuse_invalidate_attr(inode); 1977 goto error; 1745 goto error; 1978 } 1746 } 1979 1747 1980 if (fuse_invalid_attr(&outarg.attr) | 1748 if (fuse_invalid_attr(&outarg.attr) || 1981 inode_wrong_type(inode, outarg.at 1749 inode_wrong_type(inode, outarg.attr.mode)) { 1982 fuse_make_bad(inode); 1750 fuse_make_bad(inode); 1983 err = -EIO; 1751 err = -EIO; 1984 goto error; 1752 goto error; 1985 } 1753 } 1986 1754 1987 spin_lock(&fi->lock); 1755 spin_lock(&fi->lock); 1988 /* the kernel maintains i_mtime local 1756 /* the kernel maintains i_mtime locally */ 1989 if (trust_local_cmtime) { 1757 if (trust_local_cmtime) { 1990 if (attr->ia_valid & ATTR_MTI 1758 if (attr->ia_valid & ATTR_MTIME) 1991 inode_set_mtime_to_ts !! 1759 inode->i_mtime = attr->ia_mtime; 1992 if (attr->ia_valid & ATTR_CTI 1760 if (attr->ia_valid & ATTR_CTIME) 1993 inode_set_ctime_to_ts !! 1761 inode->i_ctime = attr->ia_ctime; 1994 /* FIXME: clear I_DIRTY_SYNC? 1762 /* FIXME: clear I_DIRTY_SYNC? */ 1995 } 1763 } 1996 1764 1997 fuse_change_attributes_common(inode, !! 1765 fuse_change_attributes_common(inode, &outarg.attr, 1998 ATTR_TI !! 1766 attr_timeout(&outarg), 1999 fuse_ge 1767 fuse_get_cache_mask(inode)); 2000 oldsize = inode->i_size; 1768 oldsize = inode->i_size; 2001 /* see the comment in fuse_change_att 1769 /* see the comment in fuse_change_attributes() */ 2002 if (!is_wb || is_truncate) 1770 if (!is_wb || is_truncate) 2003 i_size_write(inode, outarg.at 1771 i_size_write(inode, outarg.attr.size); 2004 1772 2005 if (is_truncate) { 1773 if (is_truncate) { 2006 /* NOTE: this may release/rea 1774 /* NOTE: this may release/reacquire fi->lock */ 2007 __fuse_release_nowrite(inode) 1775 __fuse_release_nowrite(inode); 2008 } 1776 } 2009 spin_unlock(&fi->lock); 1777 spin_unlock(&fi->lock); 2010 1778 2011 /* 1779 /* 2012 * Only call invalidate_inode_pages2( 1780 * Only call invalidate_inode_pages2() after removing 2013 * FUSE_NOWRITE, otherwise fuse_laund 1781 * FUSE_NOWRITE, otherwise fuse_launder_folio() would deadlock. 2014 */ 1782 */ 2015 if ((is_truncate || !is_wb) && 1783 if ((is_truncate || !is_wb) && 2016 S_ISREG(inode->i_mode) && oldsize 1784 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) { 2017 truncate_pagecache(inode, out 1785 truncate_pagecache(inode, outarg.attr.size); 2018 invalidate_inode_pages2(mappi 1786 invalidate_inode_pages2(mapping); 2019 } 1787 } 2020 1788 2021 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi-> 1789 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 2022 out: 1790 out: 2023 if (fault_blocked) 1791 if (fault_blocked) 2024 filemap_invalidate_unlock(map 1792 filemap_invalidate_unlock(mapping); 2025 1793 2026 return 0; 1794 return 0; 2027 1795 2028 error: 1796 error: 2029 if (is_truncate) 1797 if (is_truncate) 2030 fuse_release_nowrite(inode); 1798 fuse_release_nowrite(inode); 2031 1799 2032 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi-> 1800 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 2033 1801 2034 if (fault_blocked) 1802 if (fault_blocked) 2035 filemap_invalidate_unlock(map 1803 filemap_invalidate_unlock(mapping); 2036 return err; 1804 return err; 2037 } 1805 } 2038 1806 2039 static int fuse_setattr(struct mnt_idmap *idm !! 1807 static int fuse_setattr(struct user_namespace *mnt_userns, struct dentry *entry, 2040 struct iattr *attr) 1808 struct iattr *attr) 2041 { 1809 { 2042 struct inode *inode = d_inode(entry); 1810 struct inode *inode = d_inode(entry); 2043 struct fuse_conn *fc = get_fuse_conn( 1811 struct fuse_conn *fc = get_fuse_conn(inode); 2044 struct file *file = (attr->ia_valid & 1812 struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL; 2045 int ret; 1813 int ret; 2046 1814 2047 if (fuse_is_bad(inode)) 1815 if (fuse_is_bad(inode)) 2048 return -EIO; 1816 return -EIO; 2049 1817 2050 if (!fuse_allow_current_process(get_f 1818 if (!fuse_allow_current_process(get_fuse_conn(inode))) 2051 return -EACCES; 1819 return -EACCES; 2052 1820 2053 if (attr->ia_valid & (ATTR_KILL_SUID 1821 if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) { 2054 attr->ia_valid &= ~(ATTR_KILL 1822 attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | 2055 ATTR_MODE 1823 ATTR_MODE); 2056 1824 2057 /* 1825 /* 2058 * The only sane way to relia 1826 * The only sane way to reliably kill suid/sgid is to do it in 2059 * the userspace filesystem 1827 * the userspace filesystem 2060 * 1828 * 2061 * This should be done on wri 1829 * This should be done on write(), truncate() and chown(). 2062 */ 1830 */ 2063 if (!fc->handle_killpriv && ! 1831 if (!fc->handle_killpriv && !fc->handle_killpriv_v2) { 2064 /* 1832 /* 2065 * ia_mode calculatio 1833 * ia_mode calculation may have used stale i_mode. 2066 * Refresh and recalc 1834 * Refresh and recalculate. 2067 */ 1835 */ 2068 ret = fuse_do_getattr 1836 ret = fuse_do_getattr(inode, NULL, file); 2069 if (ret) 1837 if (ret) 2070 return ret; 1838 return ret; 2071 1839 2072 attr->ia_mode = inode 1840 attr->ia_mode = inode->i_mode; 2073 if (inode->i_mode & S 1841 if (inode->i_mode & S_ISUID) { 2074 attr->ia_vali 1842 attr->ia_valid |= ATTR_MODE; 2075 attr->ia_mode 1843 attr->ia_mode &= ~S_ISUID; 2076 } 1844 } 2077 if ((inode->i_mode & 1845 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { 2078 attr->ia_vali 1846 attr->ia_valid |= ATTR_MODE; 2079 attr->ia_mode 1847 attr->ia_mode &= ~S_ISGID; 2080 } 1848 } 2081 } 1849 } 2082 } 1850 } 2083 if (!attr->ia_valid) 1851 if (!attr->ia_valid) 2084 return 0; 1852 return 0; 2085 1853 2086 ret = fuse_do_setattr(entry, attr, fi 1854 ret = fuse_do_setattr(entry, attr, file); 2087 if (!ret) { 1855 if (!ret) { 2088 /* 1856 /* 2089 * If filesystem supports acl 1857 * If filesystem supports acls it may have updated acl xattrs in 2090 * the filesystem, so forget 1858 * the filesystem, so forget cached acls for the inode. 2091 */ 1859 */ 2092 if (fc->posix_acl) 1860 if (fc->posix_acl) 2093 forget_all_cached_acl 1861 forget_all_cached_acls(inode); 2094 1862 2095 /* Directory mode changed, ma 1863 /* Directory mode changed, may need to revalidate access */ 2096 if (d_is_dir(entry) && (attr- 1864 if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE)) 2097 fuse_invalidate_entry 1865 fuse_invalidate_entry_cache(entry); 2098 } 1866 } 2099 return ret; 1867 return ret; 2100 } 1868 } 2101 1869 2102 static int fuse_getattr(struct mnt_idmap *idm !! 1870 static int fuse_getattr(struct user_namespace *mnt_userns, 2103 const struct path *pa 1871 const struct path *path, struct kstat *stat, 2104 u32 request_mask, uns 1872 u32 request_mask, unsigned int flags) 2105 { 1873 { 2106 struct inode *inode = d_inode(path->d 1874 struct inode *inode = d_inode(path->dentry); 2107 struct fuse_conn *fc = get_fuse_conn( 1875 struct fuse_conn *fc = get_fuse_conn(inode); 2108 1876 2109 if (fuse_is_bad(inode)) 1877 if (fuse_is_bad(inode)) 2110 return -EIO; 1878 return -EIO; 2111 1879 2112 if (!fuse_allow_current_process(fc)) 1880 if (!fuse_allow_current_process(fc)) { 2113 if (!request_mask) { 1881 if (!request_mask) { 2114 /* 1882 /* 2115 * If user explicitly 1883 * If user explicitly requested *nothing* then don't 2116 * error out, but ret 1884 * error out, but return st_dev only. 2117 */ 1885 */ 2118 stat->result_mask = 0 1886 stat->result_mask = 0; 2119 stat->dev = inode->i_ 1887 stat->dev = inode->i_sb->s_dev; 2120 return 0; 1888 return 0; 2121 } 1889 } 2122 return -EACCES; 1890 return -EACCES; 2123 } 1891 } 2124 1892 2125 return fuse_update_get_attr(inode, NU 1893 return fuse_update_get_attr(inode, NULL, stat, request_mask, flags); 2126 } 1894 } 2127 1895 2128 static const struct inode_operations fuse_dir 1896 static const struct inode_operations fuse_dir_inode_operations = { 2129 .lookup = fuse_lookup, 1897 .lookup = fuse_lookup, 2130 .mkdir = fuse_mkdir, 1898 .mkdir = fuse_mkdir, 2131 .symlink = fuse_symlink, 1899 .symlink = fuse_symlink, 2132 .unlink = fuse_unlink, 1900 .unlink = fuse_unlink, 2133 .rmdir = fuse_rmdir, 1901 .rmdir = fuse_rmdir, 2134 .rename = fuse_rename2, 1902 .rename = fuse_rename2, 2135 .link = fuse_link, 1903 .link = fuse_link, 2136 .setattr = fuse_setattr, 1904 .setattr = fuse_setattr, 2137 .create = fuse_create, 1905 .create = fuse_create, 2138 .atomic_open = fuse_atomic_open, 1906 .atomic_open = fuse_atomic_open, 2139 .tmpfile = fuse_tmpfile, << 2140 .mknod = fuse_mknod, 1907 .mknod = fuse_mknod, 2141 .permission = fuse_permission, 1908 .permission = fuse_permission, 2142 .getattr = fuse_getattr, 1909 .getattr = fuse_getattr, 2143 .listxattr = fuse_listxattr, 1910 .listxattr = fuse_listxattr, 2144 .get_inode_acl = fuse_get_inode_acl, << 2145 .get_acl = fuse_get_acl, 1911 .get_acl = fuse_get_acl, 2146 .set_acl = fuse_set_acl, 1912 .set_acl = fuse_set_acl, 2147 .fileattr_get = fuse_fileattr_get, 1913 .fileattr_get = fuse_fileattr_get, 2148 .fileattr_set = fuse_fileattr_set, 1914 .fileattr_set = fuse_fileattr_set, 2149 }; 1915 }; 2150 1916 2151 static const struct file_operations fuse_dir_ 1917 static const struct file_operations fuse_dir_operations = { 2152 .llseek = generic_file_llseek 1918 .llseek = generic_file_llseek, 2153 .read = generic_read_dir, 1919 .read = generic_read_dir, 2154 .iterate_shared = fuse_readdir, 1920 .iterate_shared = fuse_readdir, 2155 .open = fuse_dir_open, 1921 .open = fuse_dir_open, 2156 .release = fuse_dir_release, 1922 .release = fuse_dir_release, 2157 .fsync = fuse_dir_fsync, 1923 .fsync = fuse_dir_fsync, 2158 .unlocked_ioctl = fuse_dir_ioctl, 1924 .unlocked_ioctl = fuse_dir_ioctl, 2159 .compat_ioctl = fuse_dir_compat_ioc 1925 .compat_ioctl = fuse_dir_compat_ioctl, 2160 }; 1926 }; 2161 1927 2162 static const struct inode_operations fuse_com 1928 static const struct inode_operations fuse_common_inode_operations = { 2163 .setattr = fuse_setattr, 1929 .setattr = fuse_setattr, 2164 .permission = fuse_permission, 1930 .permission = fuse_permission, 2165 .getattr = fuse_getattr, 1931 .getattr = fuse_getattr, 2166 .listxattr = fuse_listxattr, 1932 .listxattr = fuse_listxattr, 2167 .get_inode_acl = fuse_get_inode_acl, << 2168 .get_acl = fuse_get_acl, 1933 .get_acl = fuse_get_acl, 2169 .set_acl = fuse_set_acl, 1934 .set_acl = fuse_set_acl, 2170 .fileattr_get = fuse_fileattr_get, 1935 .fileattr_get = fuse_fileattr_get, 2171 .fileattr_set = fuse_fileattr_set, 1936 .fileattr_set = fuse_fileattr_set, 2172 }; 1937 }; 2173 1938 2174 static const struct inode_operations fuse_sym 1939 static const struct inode_operations fuse_symlink_inode_operations = { 2175 .setattr = fuse_setattr, 1940 .setattr = fuse_setattr, 2176 .get_link = fuse_get_link, 1941 .get_link = fuse_get_link, 2177 .getattr = fuse_getattr, 1942 .getattr = fuse_getattr, 2178 .listxattr = fuse_listxattr, 1943 .listxattr = fuse_listxattr, 2179 }; 1944 }; 2180 1945 2181 void fuse_init_common(struct inode *inode) 1946 void fuse_init_common(struct inode *inode) 2182 { 1947 { 2183 inode->i_op = &fuse_common_inode_oper 1948 inode->i_op = &fuse_common_inode_operations; 2184 } 1949 } 2185 1950 2186 void fuse_init_dir(struct inode *inode) 1951 void fuse_init_dir(struct inode *inode) 2187 { 1952 { 2188 struct fuse_inode *fi = get_fuse_inod 1953 struct fuse_inode *fi = get_fuse_inode(inode); 2189 1954 2190 inode->i_op = &fuse_dir_inode_operati 1955 inode->i_op = &fuse_dir_inode_operations; 2191 inode->i_fop = &fuse_dir_operations; 1956 inode->i_fop = &fuse_dir_operations; 2192 1957 2193 spin_lock_init(&fi->rdc.lock); 1958 spin_lock_init(&fi->rdc.lock); 2194 fi->rdc.cached = false; 1959 fi->rdc.cached = false; 2195 fi->rdc.size = 0; 1960 fi->rdc.size = 0; 2196 fi->rdc.pos = 0; 1961 fi->rdc.pos = 0; 2197 fi->rdc.version = 0; 1962 fi->rdc.version = 0; 2198 } 1963 } 2199 1964 2200 static int fuse_symlink_read_folio(struct fil !! 1965 static int fuse_symlink_readpage(struct file *null, struct page *page) 2201 { 1966 { 2202 int err = fuse_readlink_page(folio->m !! 1967 int err = fuse_readlink_page(page->mapping->host, page); 2203 1968 2204 if (!err) 1969 if (!err) 2205 folio_mark_uptodate(folio); !! 1970 SetPageUptodate(page); 2206 1971 2207 folio_unlock(folio); !! 1972 unlock_page(page); 2208 1973 2209 return err; 1974 return err; 2210 } 1975 } 2211 1976 2212 static const struct address_space_operations 1977 static const struct address_space_operations fuse_symlink_aops = { 2213 .read_folio = fuse_symlink_read_f !! 1978 .readpage = fuse_symlink_readpage, 2214 }; 1979 }; 2215 1980 2216 void fuse_init_symlink(struct inode *inode) 1981 void fuse_init_symlink(struct inode *inode) 2217 { 1982 { 2218 inode->i_op = &fuse_symlink_inode_ope 1983 inode->i_op = &fuse_symlink_inode_operations; 2219 inode->i_data.a_ops = &fuse_symlink_a 1984 inode->i_data.a_ops = &fuse_symlink_aops; 2220 inode_nohighmem(inode); 1985 inode_nohighmem(inode); 2221 } 1986 } 2222 1987
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.