1 /*
2 * fs/sdcardfs/inode.c
3 *
4 * Copyright (c) 2013 Samsung Electronics Co. Ltd
5 * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
6 * Sunghwan Yun, Sungjong Seo
7 *
8 * This program has been developed as a stackable file system based on
9 * the WrapFS which written by
10 *
11 * Copyright (c) 1998-2011 Erez Zadok
12 * Copyright (c) 2009 Shrikar Archak
13 * Copyright (c) 2003-2011 Stony Brook University
14 * Copyright (c) 2003-2011 The Research Foundation of SUNY
15 *
16 * This file is dual licensed. It may be redistributed and/or modified
17 * under the terms of the Apache 2.0 License OR version 2 of the GNU
18 * General Public License.
19 */
20
21 #include "sdcardfs.h"
22 #include <linux/fs_struct.h>
23 #include <linux/ratelimit.h>
24 #include <linux/sched/task.h>
25
override_fsids(struct sdcardfs_sb_info * sbi,struct sdcardfs_inode_data * data)26 const struct cred *override_fsids(struct sdcardfs_sb_info *sbi,
27 struct sdcardfs_inode_data *data)
28 {
29 struct cred *cred;
30 const struct cred *old_cred;
31 uid_t uid;
32
33 cred = prepare_creds();
34 if (!cred)
35 return NULL;
36
37 if (sbi->options.gid_derivation) {
38 if (data->under_obb)
39 uid = AID_MEDIA_OBB;
40 else
41 uid = multiuser_get_uid(data->userid, sbi->options.fs_low_uid);
42 } else {
43 uid = sbi->options.fs_low_uid;
44 }
45 cred->fsuid = make_kuid(&init_user_ns, uid);
46 cred->fsgid = make_kgid(&init_user_ns, sbi->options.fs_low_gid);
47
48 old_cred = override_creds(cred);
49
50 return old_cred;
51 }
52
revert_fsids(const struct cred * old_cred)53 void revert_fsids(const struct cred *old_cred)
54 {
55 const struct cred *cur_cred;
56
57 cur_cred = current->cred;
58 revert_creds(old_cred);
59 put_cred(cur_cred);
60 }
61
sdcardfs_create(struct inode * dir,struct dentry * dentry,umode_t mode,bool want_excl)62 static int sdcardfs_create(struct inode *dir, struct dentry *dentry,
63 umode_t mode, bool want_excl)
64 {
65 int err;
66 struct dentry *lower_dentry;
67 struct vfsmount *lower_dentry_mnt;
68 struct dentry *lower_parent_dentry = NULL;
69 struct path lower_path;
70 const struct cred *saved_cred = NULL;
71 struct fs_struct *saved_fs;
72 struct fs_struct *copied_fs;
73
74 if (!check_caller_access_to_name(dir, &dentry->d_name)) {
75 err = -EACCES;
76 goto out_eacces;
77 }
78
79 /* save current_cred and override it */
80 saved_cred = override_fsids(SDCARDFS_SB(dir->i_sb),
81 SDCARDFS_I(dir)->data);
82 if (!saved_cred)
83 return -ENOMEM;
84
85 sdcardfs_get_lower_path(dentry, &lower_path);
86 lower_dentry = lower_path.dentry;
87 lower_dentry_mnt = lower_path.mnt;
88 lower_parent_dentry = lock_parent(lower_dentry);
89
90 if (d_is_positive(lower_dentry))
91 return -EEXIST;
92
93 /* set last 16bytes of mode field to 0664 */
94 mode = (mode & S_IFMT) | 00664;
95
96 /* temporarily change umask for lower fs write */
97 saved_fs = current->fs;
98 copied_fs = copy_fs_struct(current->fs);
99 if (!copied_fs) {
100 err = -ENOMEM;
101 goto out_unlock;
102 }
103 copied_fs->umask = 0;
104 task_lock(current);
105 current->fs = copied_fs;
106 task_unlock(current);
107
108 err = vfs_create2(lower_dentry_mnt, d_inode(lower_parent_dentry), lower_dentry, mode, want_excl);
109 if (err)
110 goto out;
111
112 err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path,
113 SDCARDFS_I(dir)->data->userid);
114 if (err)
115 goto out;
116 fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
117 fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
118 fixup_lower_ownership(dentry, dentry->d_name.name);
119
120 out:
121 task_lock(current);
122 current->fs = saved_fs;
123 task_unlock(current);
124 free_fs_struct(copied_fs);
125 out_unlock:
126 unlock_dir(lower_parent_dentry);
127 sdcardfs_put_lower_path(dentry, &lower_path);
128 revert_fsids(saved_cred);
129 out_eacces:
130 return err;
131 }
132
sdcardfs_unlink(struct inode * dir,struct dentry * dentry)133 static int sdcardfs_unlink(struct inode *dir, struct dentry *dentry)
134 {
135 int err;
136 struct dentry *lower_dentry;
137 struct vfsmount *lower_mnt;
138 struct inode *lower_dir_inode = sdcardfs_lower_inode(dir);
139 struct dentry *lower_dir_dentry;
140 struct path lower_path;
141 const struct cred *saved_cred = NULL;
142
143 if (!check_caller_access_to_name(dir, &dentry->d_name)) {
144 err = -EACCES;
145 goto out_eacces;
146 }
147
148 /* save current_cred and override it */
149 saved_cred = override_fsids(SDCARDFS_SB(dir->i_sb),
150 SDCARDFS_I(dir)->data);
151 if (!saved_cred)
152 return -ENOMEM;
153
154 sdcardfs_get_lower_path(dentry, &lower_path);
155 lower_dentry = lower_path.dentry;
156 lower_mnt = lower_path.mnt;
157 dget(lower_dentry);
158 lower_dir_dentry = lock_parent(lower_dentry);
159
160 err = vfs_unlink2(lower_mnt, lower_dir_inode, lower_dentry, NULL);
161
162 /*
163 * Note: unlinking on top of NFS can cause silly-renamed files.
164 * Trying to delete such files results in EBUSY from NFS
165 * below. Silly-renamed files will get deleted by NFS later on, so
166 * we just need to detect them here and treat such EBUSY errors as
167 * if the upper file was successfully deleted.
168 */
169 if (err == -EBUSY && lower_dentry->d_flags & DCACHE_NFSFS_RENAMED)
170 err = 0;
171 if (err)
172 goto out;
173 fsstack_copy_attr_times(dir, lower_dir_inode);
174 fsstack_copy_inode_size(dir, lower_dir_inode);
175 set_nlink(d_inode(dentry),
176 sdcardfs_lower_inode(d_inode(dentry))->i_nlink);
177 d_inode(dentry)->i_ctime = dir->i_ctime;
178 d_drop(dentry); /* this is needed, else LTP fails (VFS won't do it) */
179 out:
180 unlock_dir(lower_dir_dentry);
181 dput(lower_dentry);
182 sdcardfs_put_lower_path(dentry, &lower_path);
183 revert_fsids(saved_cred);
184 out_eacces:
185 return err;
186 }
187
touch(char * abs_path,mode_t mode)188 static int touch(char *abs_path, mode_t mode)
189 {
190 struct file *filp = filp_open(abs_path, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, mode);
191
192 if (IS_ERR(filp)) {
193 if (PTR_ERR(filp) == -EEXIST) {
194 return 0;
195 } else {
196 pr_err("sdcardfs: failed to open(%s): %ld\n",
197 abs_path, PTR_ERR(filp));
198 return PTR_ERR(filp);
199 }
200 }
201 filp_close(filp, current->files);
202 return 0;
203 }
204
sdcardfs_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)205 static int sdcardfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
206 {
207 int err;
208 int make_nomedia_in_obb = 0;
209 struct dentry *lower_dentry;
210 struct vfsmount *lower_mnt;
211 struct dentry *lower_parent_dentry = NULL;
212 struct dentry *parent_dentry = NULL;
213 struct path lower_path;
214 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
215 const struct cred *saved_cred = NULL;
216 struct sdcardfs_inode_data *pd = SDCARDFS_I(dir)->data;
217 int touch_err = 0;
218 struct fs_struct *saved_fs;
219 struct fs_struct *copied_fs;
220 struct qstr q_obb = QSTR_LITERAL("obb");
221 struct qstr q_data = QSTR_LITERAL("data");
222
223 if (!check_caller_access_to_name(dir, &dentry->d_name)) {
224 err = -EACCES;
225 goto out_eacces;
226 }
227
228 /* save current_cred and override it */
229 saved_cred = override_fsids(SDCARDFS_SB(dir->i_sb),
230 SDCARDFS_I(dir)->data);
231 if (!saved_cred)
232 return -ENOMEM;
233
234 /* check disk space */
235 parent_dentry = dget_parent(dentry);
236 if (!check_min_free_space(parent_dentry, 0, 1)) {
237 pr_err("sdcardfs: No minimum free space.\n");
238 err = -ENOSPC;
239 dput(parent_dentry);
240 goto out_revert;
241 }
242 dput(parent_dentry);
243
244 /* the lower_dentry is negative here */
245 sdcardfs_get_lower_path(dentry, &lower_path);
246 lower_dentry = lower_path.dentry;
247 lower_mnt = lower_path.mnt;
248 lower_parent_dentry = lock_parent(lower_dentry);
249
250 /* set last 16bytes of mode field to 0775 */
251 mode = (mode & S_IFMT) | 00775;
252
253 /* temporarily change umask for lower fs write */
254 saved_fs = current->fs;
255 copied_fs = copy_fs_struct(current->fs);
256 if (!copied_fs) {
257 err = -ENOMEM;
258 unlock_dir(lower_parent_dentry);
259 goto out_unlock;
260 }
261 copied_fs->umask = 0;
262 task_lock(current);
263 current->fs = copied_fs;
264 task_unlock(current);
265
266 err = vfs_mkdir2(lower_mnt, d_inode(lower_parent_dentry), lower_dentry, mode);
267
268 if (err) {
269 unlock_dir(lower_parent_dentry);
270 goto out;
271 }
272
273 /* if it is a local obb dentry, setup it with the base obbpath */
274 if (need_graft_path(dentry)) {
275
276 err = setup_obb_dentry(dentry, &lower_path);
277 if (err) {
278 /* if the sbi->obbpath is not available, the lower_path won't be
279 * changed by setup_obb_dentry() but the lower path is saved to
280 * its orig_path. this dentry will be revalidated later.
281 * but now, the lower_path should be NULL
282 */
283 sdcardfs_put_reset_lower_path(dentry);
284
285 /* the newly created lower path which saved to its orig_path or
286 * the lower_path is the base obbpath.
287 * therefore, an additional path_get is required
288 */
289 path_get(&lower_path);
290 } else
291 make_nomedia_in_obb = 1;
292 }
293
294 err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path, pd->userid);
295 if (err) {
296 unlock_dir(lower_parent_dentry);
297 goto out;
298 }
299
300 fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
301 fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
302 /* update number of links on parent directory */
303 set_nlink(dir, sdcardfs_lower_inode(dir)->i_nlink);
304 fixup_lower_ownership(dentry, dentry->d_name.name);
305 unlock_dir(lower_parent_dentry);
306 if ((!sbi->options.multiuser) && (qstr_case_eq(&dentry->d_name, &q_obb))
307 && (pd->perm == PERM_ANDROID) && (pd->userid == 0))
308 make_nomedia_in_obb = 1;
309
310 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
311 if (make_nomedia_in_obb ||
312 ((pd->perm == PERM_ANDROID)
313 && (qstr_case_eq(&dentry->d_name, &q_data)))) {
314 revert_fsids(saved_cred);
315 saved_cred = override_fsids(sbi,
316 SDCARDFS_I(d_inode(dentry))->data);
317 if (!saved_cred) {
318 pr_err("sdcardfs: failed to set up .nomedia in %s: %d\n",
319 lower_path.dentry->d_name.name,
320 -ENOMEM);
321 goto out;
322 }
323 set_fs_pwd(current->fs, &lower_path);
324 touch_err = touch(".nomedia", 0664);
325 if (touch_err) {
326 pr_err("sdcardfs: failed to create .nomedia in %s: %d\n",
327 lower_path.dentry->d_name.name,
328 touch_err);
329 goto out;
330 }
331 }
332 out:
333 task_lock(current);
334 current->fs = saved_fs;
335 task_unlock(current);
336
337 free_fs_struct(copied_fs);
338 out_unlock:
339 sdcardfs_put_lower_path(dentry, &lower_path);
340 out_revert:
341 revert_fsids(saved_cred);
342 out_eacces:
343 return err;
344 }
345
sdcardfs_rmdir(struct inode * dir,struct dentry * dentry)346 static int sdcardfs_rmdir(struct inode *dir, struct dentry *dentry)
347 {
348 struct dentry *lower_dentry;
349 struct dentry *lower_dir_dentry;
350 struct vfsmount *lower_mnt;
351 int err;
352 struct path lower_path;
353 const struct cred *saved_cred = NULL;
354
355 if (!check_caller_access_to_name(dir, &dentry->d_name)) {
356 err = -EACCES;
357 goto out_eacces;
358 }
359
360 /* save current_cred and override it */
361 saved_cred = override_fsids(SDCARDFS_SB(dir->i_sb),
362 SDCARDFS_I(dir)->data);
363 if (!saved_cred)
364 return -ENOMEM;
365
366 /* sdcardfs_get_real_lower(): in case of remove an user's obb dentry
367 * the dentry on the original path should be deleted.
368 */
369 sdcardfs_get_real_lower(dentry, &lower_path);
370
371 lower_dentry = lower_path.dentry;
372 lower_mnt = lower_path.mnt;
373 lower_dir_dentry = lock_parent(lower_dentry);
374
375 err = vfs_rmdir2(lower_mnt, d_inode(lower_dir_dentry), lower_dentry);
376 if (err)
377 goto out;
378
379 d_drop(dentry); /* drop our dentry on success (why not VFS's job?) */
380 if (d_inode(dentry))
381 clear_nlink(d_inode(dentry));
382 fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
383 fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
384 set_nlink(dir, d_inode(lower_dir_dentry)->i_nlink);
385
386 out:
387 unlock_dir(lower_dir_dentry);
388 sdcardfs_put_real_lower(dentry, &lower_path);
389 revert_fsids(saved_cred);
390 out_eacces:
391 return err;
392 }
393
394 /*
395 * The locking rules in sdcardfs_rename are complex. We could use a simpler
396 * superblock-level name-space lock for renames and copy-ups.
397 */
sdcardfs_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)398 static int sdcardfs_rename(struct inode *old_dir, struct dentry *old_dentry,
399 struct inode *new_dir, struct dentry *new_dentry,
400 unsigned int flags)
401 {
402 int err = 0;
403 struct dentry *lower_old_dentry = NULL;
404 struct dentry *lower_new_dentry = NULL;
405 struct dentry *lower_old_dir_dentry = NULL;
406 struct dentry *lower_new_dir_dentry = NULL;
407 struct vfsmount *lower_mnt = NULL;
408 struct dentry *trap = NULL;
409 struct path lower_old_path, lower_new_path;
410 const struct cred *saved_cred = NULL;
411
412 if (flags)
413 return -EINVAL;
414
415 if (!check_caller_access_to_name(old_dir, &old_dentry->d_name) ||
416 !check_caller_access_to_name(new_dir, &new_dentry->d_name)) {
417 err = -EACCES;
418 goto out_eacces;
419 }
420
421 /* save current_cred and override it */
422 saved_cred = override_fsids(SDCARDFS_SB(old_dir->i_sb),
423 SDCARDFS_I(new_dir)->data);
424 if (!saved_cred)
425 return -ENOMEM;
426
427 sdcardfs_get_real_lower(old_dentry, &lower_old_path);
428 sdcardfs_get_lower_path(new_dentry, &lower_new_path);
429 lower_old_dentry = lower_old_path.dentry;
430 lower_new_dentry = lower_new_path.dentry;
431 lower_mnt = lower_old_path.mnt;
432 lower_old_dir_dentry = dget_parent(lower_old_dentry);
433 lower_new_dir_dentry = dget_parent(lower_new_dentry);
434
435 trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
436 /* source should not be ancestor of target */
437 if (trap == lower_old_dentry) {
438 err = -EINVAL;
439 goto out;
440 }
441 /* target should not be ancestor of source */
442 if (trap == lower_new_dentry) {
443 err = -ENOTEMPTY;
444 goto out;
445 }
446
447 err = vfs_rename2(lower_mnt,
448 d_inode(lower_old_dir_dentry), lower_old_dentry,
449 d_inode(lower_new_dir_dentry), lower_new_dentry,
450 NULL, 0);
451 if (err)
452 goto out;
453
454 /* Copy attrs from lower dir, but i_uid/i_gid */
455 sdcardfs_copy_and_fix_attrs(new_dir, d_inode(lower_new_dir_dentry));
456 fsstack_copy_inode_size(new_dir, d_inode(lower_new_dir_dentry));
457
458 if (new_dir != old_dir) {
459 sdcardfs_copy_and_fix_attrs(old_dir, d_inode(lower_old_dir_dentry));
460 fsstack_copy_inode_size(old_dir, d_inode(lower_old_dir_dentry));
461 }
462 get_derived_permission_new(new_dentry->d_parent, old_dentry, &new_dentry->d_name);
463 fixup_tmp_permissions(d_inode(old_dentry));
464 fixup_lower_ownership(old_dentry, new_dentry->d_name.name);
465 d_invalidate(old_dentry); /* Can't fixup ownership recursively :( */
466 out:
467 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
468 dput(lower_old_dir_dentry);
469 dput(lower_new_dir_dentry);
470 sdcardfs_put_real_lower(old_dentry, &lower_old_path);
471 sdcardfs_put_lower_path(new_dentry, &lower_new_path);
472 revert_fsids(saved_cred);
473 out_eacces:
474 return err;
475 }
476
477 #if 0
478 static int sdcardfs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
479 {
480 int err;
481 struct dentry *lower_dentry;
482 struct path lower_path;
483 /* XXX readlink does not requires overriding credential */
484
485 sdcardfs_get_lower_path(dentry, &lower_path);
486 lower_dentry = lower_path.dentry;
487 if (!d_inode(lower_dentry)->i_op ||
488 !d_inode(lower_dentry)->i_op->readlink) {
489 err = -EINVAL;
490 goto out;
491 }
492
493 err = d_inode(lower_dentry)->i_op->readlink(lower_dentry,
494 buf, bufsiz);
495 if (err < 0)
496 goto out;
497 fsstack_copy_attr_atime(d_inode(dentry), d_inode(lower_dentry));
498
499 out:
500 sdcardfs_put_lower_path(dentry, &lower_path);
501 return err;
502 }
503 #endif
504
505 #if 0
506 static const char *sdcardfs_follow_link(struct dentry *dentry, void **cookie)
507 {
508 char *buf;
509 int len = PAGE_SIZE, err;
510 mm_segment_t old_fs;
511
512 /* This is freed by the put_link method assuming a successful call. */
513 buf = kmalloc(len, GFP_KERNEL);
514 if (!buf) {
515 buf = ERR_PTR(-ENOMEM);
516 return buf;
517 }
518
519 /* read the symlink, and then we will follow it */
520 old_fs = get_fs();
521 set_fs(KERNEL_DS);
522 err = sdcardfs_readlink(dentry, buf, len);
523 set_fs(old_fs);
524 if (err < 0) {
525 kfree(buf);
526 buf = ERR_PTR(err);
527 } else {
528 buf[err] = '\0';
529 }
530 return *cookie = buf;
531 }
532 #endif
533
sdcardfs_permission_wrn(struct inode * inode,int mask)534 static int sdcardfs_permission_wrn(struct inode *inode, int mask)
535 {
536 WARN_RATELIMIT(1, "sdcardfs does not support permission. Use permission2.\n");
537 return -EINVAL;
538 }
539
copy_attrs(struct inode * dest,const struct inode * src)540 void copy_attrs(struct inode *dest, const struct inode *src)
541 {
542 dest->i_mode = src->i_mode;
543 dest->i_uid = src->i_uid;
544 dest->i_gid = src->i_gid;
545 dest->i_rdev = src->i_rdev;
546 dest->i_atime = src->i_atime;
547 dest->i_mtime = src->i_mtime;
548 dest->i_ctime = src->i_ctime;
549 dest->i_blkbits = src->i_blkbits;
550 dest->i_flags = src->i_flags;
551 #ifdef CONFIG_FS_POSIX_ACL
552 dest->i_acl = src->i_acl;
553 #endif
554 #ifdef CONFIG_SECURITY
555 dest->i_security = src->i_security;
556 #endif
557 }
558
sdcardfs_permission(struct vfsmount * mnt,struct inode * inode,int mask)559 static int sdcardfs_permission(struct vfsmount *mnt, struct inode *inode, int mask)
560 {
561 int err;
562 struct inode tmp;
563 struct sdcardfs_inode_data *top = top_data_get(SDCARDFS_I(inode));
564
565 if (IS_ERR(mnt))
566 return PTR_ERR(mnt);
567
568 if (!top)
569 return -EINVAL;
570
571 /*
572 * Permission check on sdcardfs inode.
573 * Calling process should have AID_SDCARD_RW permission
574 * Since generic_permission only needs i_mode, i_uid,
575 * i_gid, and i_sb, we can create a fake inode to pass
576 * this information down in.
577 *
578 * The underlying code may attempt to take locks in some
579 * cases for features we're not using, but if that changes,
580 * locks must be dealt with to avoid undefined behavior.
581 */
582 copy_attrs(&tmp, inode);
583 tmp.i_uid = make_kuid(&init_user_ns, top->d_uid);
584 tmp.i_gid = make_kgid(&init_user_ns, get_gid(mnt, inode->i_sb, top));
585 tmp.i_mode = (inode->i_mode & S_IFMT)
586 | get_mode(mnt, SDCARDFS_I(inode), top);
587 data_put(top);
588 tmp.i_sb = inode->i_sb;
589 if (IS_POSIXACL(inode))
590 pr_warn("%s: This may be undefined behavior...\n", __func__);
591 err = generic_permission(&tmp, mask);
592 return err;
593 }
594
sdcardfs_setattr_wrn(struct dentry * dentry,struct iattr * ia)595 static int sdcardfs_setattr_wrn(struct dentry *dentry, struct iattr *ia)
596 {
597 WARN_RATELIMIT(1, "sdcardfs does not support setattr. User setattr2.\n");
598 return -EINVAL;
599 }
600
sdcardfs_setattr(struct vfsmount * mnt,struct dentry * dentry,struct iattr * ia)601 static int sdcardfs_setattr(struct vfsmount *mnt, struct dentry *dentry, struct iattr *ia)
602 {
603 int err;
604 struct dentry *lower_dentry;
605 struct vfsmount *lower_mnt;
606 struct inode *inode;
607 struct inode *lower_inode;
608 struct path lower_path;
609 struct iattr lower_ia;
610 struct dentry *parent;
611 struct inode tmp;
612 struct dentry tmp_d;
613 struct sdcardfs_inode_data *top;
614
615 const struct cred *saved_cred = NULL;
616
617 inode = d_inode(dentry);
618 top = top_data_get(SDCARDFS_I(inode));
619
620 if (!top)
621 return -EINVAL;
622
623 /*
624 * Permission check on sdcardfs inode.
625 * Calling process should have AID_SDCARD_RW permission
626 * Since generic_permission only needs i_mode, i_uid,
627 * i_gid, and i_sb, we can create a fake inode to pass
628 * this information down in.
629 *
630 * The underlying code may attempt to take locks in some
631 * cases for features we're not using, but if that changes,
632 * locks must be dealt with to avoid undefined behavior.
633 *
634 */
635 copy_attrs(&tmp, inode);
636 tmp.i_uid = make_kuid(&init_user_ns, top->d_uid);
637 tmp.i_gid = make_kgid(&init_user_ns, get_gid(mnt, dentry->d_sb, top));
638 tmp.i_mode = (inode->i_mode & S_IFMT)
639 | get_mode(mnt, SDCARDFS_I(inode), top);
640 tmp.i_size = i_size_read(inode);
641 data_put(top);
642 tmp.i_sb = inode->i_sb;
643 tmp_d.d_inode = &tmp;
644
645 /*
646 * Check if user has permission to change dentry. We don't check if
647 * this user can change the lower inode: that should happen when
648 * calling notify_change on the lower inode.
649 */
650 /* prepare our own lower struct iattr (with the lower file) */
651 memcpy(&lower_ia, ia, sizeof(lower_ia));
652 /* Allow touch updating timestamps. A previous permission check ensures
653 * we have write access. Changes to mode, owner, and group are ignored
654 */
655 ia->ia_valid |= ATTR_FORCE;
656 err = setattr_prepare(&tmp_d, ia);
657
658 if (!err) {
659 /* check the Android group ID */
660 parent = dget_parent(dentry);
661 if (!check_caller_access_to_name(d_inode(parent), &dentry->d_name))
662 err = -EACCES;
663 dput(parent);
664 }
665
666 if (err)
667 goto out_err;
668
669 /* save current_cred and override it */
670 saved_cred = override_fsids(SDCARDFS_SB(dentry->d_sb),
671 SDCARDFS_I(inode)->data);
672 if (!saved_cred)
673 return -ENOMEM;
674
675 sdcardfs_get_lower_path(dentry, &lower_path);
676 lower_dentry = lower_path.dentry;
677 lower_mnt = lower_path.mnt;
678 lower_inode = sdcardfs_lower_inode(inode);
679
680 if (ia->ia_valid & ATTR_FILE)
681 lower_ia.ia_file = sdcardfs_lower_file(ia->ia_file);
682
683 lower_ia.ia_valid &= ~(ATTR_UID | ATTR_GID | ATTR_MODE);
684
685 /*
686 * If shrinking, first truncate upper level to cancel writing dirty
687 * pages beyond the new eof; and also if its' maxbytes is more
688 * limiting (fail with -EFBIG before making any change to the lower
689 * level). There is no need to vmtruncate the upper level
690 * afterwards in the other cases: we fsstack_copy_inode_size from
691 * the lower level.
692 */
693 if (ia->ia_valid & ATTR_SIZE) {
694 err = inode_newsize_ok(&tmp, ia->ia_size);
695 if (err) {
696 goto out;
697 }
698 truncate_setsize(inode, ia->ia_size);
699 }
700
701 /*
702 * mode change is for clearing setuid/setgid bits. Allow lower fs
703 * to interpret this in its own way.
704 */
705 if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
706 lower_ia.ia_valid &= ~ATTR_MODE;
707
708 /* notify the (possibly copied-up) lower inode */
709 /*
710 * Note: we use d_inode(lower_dentry), because lower_inode may be
711 * unlinked (no inode->i_sb and i_ino==0. This happens if someone
712 * tries to open(), unlink(), then ftruncate() a file.
713 */
714 inode_lock(d_inode(lower_dentry));
715 err = notify_change2(lower_mnt, lower_dentry, &lower_ia, /* note: lower_ia */
716 NULL);
717 inode_unlock(d_inode(lower_dentry));
718 if (err)
719 goto out;
720
721 /* get attributes from the lower inode and update derived permissions */
722 sdcardfs_copy_and_fix_attrs(inode, lower_inode);
723
724 /*
725 * Not running fsstack_copy_inode_size(inode, lower_inode), because
726 * VFS should update our inode size, and notify_change on
727 * lower_inode should update its size.
728 */
729
730 out:
731 sdcardfs_put_lower_path(dentry, &lower_path);
732 revert_fsids(saved_cred);
733 out_err:
734 return err;
735 }
736
sdcardfs_fillattr(struct vfsmount * mnt,struct inode * inode,struct kstat * lower_stat,struct kstat * stat)737 static int sdcardfs_fillattr(struct vfsmount *mnt, struct inode *inode,
738 struct kstat *lower_stat, struct kstat *stat)
739 {
740 struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
741 struct sdcardfs_inode_data *top = top_data_get(info);
742 struct super_block *sb = inode->i_sb;
743
744 if (!top)
745 return -EINVAL;
746
747 stat->dev = inode->i_sb->s_dev;
748 stat->ino = inode->i_ino;
749 stat->mode = (inode->i_mode & S_IFMT) | get_mode(mnt, info, top);
750 stat->nlink = inode->i_nlink;
751 stat->uid = make_kuid(&init_user_ns, top->d_uid);
752 stat->gid = make_kgid(&init_user_ns, get_gid(mnt, sb, top));
753 stat->rdev = inode->i_rdev;
754 stat->size = lower_stat->size;
755 stat->atime = lower_stat->atime;
756 stat->mtime = lower_stat->mtime;
757 stat->ctime = lower_stat->ctime;
758 stat->blksize = lower_stat->blksize;
759 stat->blocks = lower_stat->blocks;
760 data_put(top);
761 return 0;
762 }
sdcardfs_getattr(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)763 static int sdcardfs_getattr(const struct path *path, struct kstat *stat,
764 u32 request_mask, unsigned int flags)
765 {
766 struct vfsmount *mnt = path->mnt;
767 struct dentry *dentry = path->dentry;
768 struct kstat lower_stat;
769 struct path lower_path;
770 struct dentry *parent;
771 int err;
772
773 parent = dget_parent(dentry);
774 if (!check_caller_access_to_name(d_inode(parent), &dentry->d_name)) {
775 dput(parent);
776 return -EACCES;
777 }
778 dput(parent);
779
780 sdcardfs_get_lower_path(dentry, &lower_path);
781 err = vfs_getattr(&lower_path, &lower_stat, request_mask, flags);
782 if (err)
783 goto out;
784 sdcardfs_copy_and_fix_attrs(d_inode(dentry),
785 d_inode(lower_path.dentry));
786 err = sdcardfs_fillattr(mnt, d_inode(dentry), &lower_stat, stat);
787 out:
788 sdcardfs_put_lower_path(dentry, &lower_path);
789 return err;
790 }
791
792 const struct inode_operations sdcardfs_symlink_iops = {
793 .permission2 = sdcardfs_permission,
794 .setattr2 = sdcardfs_setattr,
795 /* XXX Following operations are implemented,
796 * but FUSE(sdcard) or FAT does not support them
797 * These methods are *NOT* perfectly tested.
798 .readlink = sdcardfs_readlink,
799 .follow_link = sdcardfs_follow_link,
800 .put_link = kfree_put_link,
801 */
802 };
803
804 const struct inode_operations sdcardfs_dir_iops = {
805 .create = sdcardfs_create,
806 .lookup = sdcardfs_lookup,
807 .permission = sdcardfs_permission_wrn,
808 .permission2 = sdcardfs_permission,
809 .unlink = sdcardfs_unlink,
810 .mkdir = sdcardfs_mkdir,
811 .rmdir = sdcardfs_rmdir,
812 .rename = sdcardfs_rename,
813 .setattr = sdcardfs_setattr_wrn,
814 .setattr2 = sdcardfs_setattr,
815 .getattr = sdcardfs_getattr,
816 };
817
818 const struct inode_operations sdcardfs_main_iops = {
819 .permission = sdcardfs_permission_wrn,
820 .permission2 = sdcardfs_permission,
821 .setattr = sdcardfs_setattr_wrn,
822 .setattr2 = sdcardfs_setattr,
823 .getattr = sdcardfs_getattr,
824 };
825