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