4 * Copyright (c) 2013 Samsung Electronics Co. Ltd
5 * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
6 * Sunghwan Yun, Sungjong Seo
8 * This program has been developed as a stackable file system based on
9 * the WrapFS which written by
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
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.
22 #include <linux/fs_struct.h>
23 #include <linux/ratelimit.h>
25 /* Do not directly use this function. Use OVERRIDE_CRED() instead. */
26 const struct cred *override_fsids(struct sdcardfs_sb_info *sbi,
27 struct sdcardfs_inode_data *data)
30 const struct cred *old_cred;
33 cred = prepare_creds();
37 if (sbi->options.gid_derivation) {
41 uid = multiuser_get_uid(data->userid, sbi->options.fs_low_uid);
43 uid = sbi->options.fs_low_uid;
45 cred->fsuid = make_kuid(&init_user_ns, uid);
46 cred->fsgid = make_kgid(&init_user_ns, sbi->options.fs_low_gid);
48 old_cred = override_creds(cred);
53 /* Do not directly use this function, use REVERT_CRED() instead. */
54 void revert_fsids(const struct cred *old_cred)
56 const struct cred *cur_cred;
58 cur_cred = current->cred;
59 revert_creds(old_cred);
63 static int sdcardfs_create(struct inode *dir, struct dentry *dentry,
64 umode_t mode, bool want_excl)
67 struct dentry *lower_dentry;
68 struct vfsmount *lower_dentry_mnt;
69 struct dentry *lower_parent_dentry = NULL;
70 struct path lower_path;
71 const struct cred *saved_cred = NULL;
72 struct fs_struct *saved_fs;
73 struct fs_struct *copied_fs;
75 if (!check_caller_access_to_name(dir, &dentry->d_name)) {
80 /* save current_cred and override it */
81 OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir));
83 sdcardfs_get_lower_path(dentry, &lower_path);
84 lower_dentry = lower_path.dentry;
85 lower_dentry_mnt = lower_path.mnt;
86 lower_parent_dentry = lock_parent(lower_dentry);
88 /* set last 16bytes of mode field to 0664 */
89 mode = (mode & S_IFMT) | 00664;
91 /* temporarily change umask for lower fs write */
92 saved_fs = current->fs;
93 copied_fs = copy_fs_struct(current->fs);
98 current->fs = copied_fs;
99 current->fs->umask = 0;
100 err = vfs_create2(lower_dentry_mnt, d_inode(lower_parent_dentry), lower_dentry, mode, want_excl);
104 err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path,
105 SDCARDFS_I(dir)->data->userid);
108 fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
109 fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
110 fixup_lower_ownership(dentry, dentry->d_name.name);
113 current->fs = saved_fs;
114 free_fs_struct(copied_fs);
116 unlock_dir(lower_parent_dentry);
117 sdcardfs_put_lower_path(dentry, &lower_path);
118 REVERT_CRED(saved_cred);
124 static int sdcardfs_link(struct dentry *old_dentry, struct inode *dir,
125 struct dentry *new_dentry)
127 struct dentry *lower_old_dentry;
128 struct dentry *lower_new_dentry;
129 struct dentry *lower_dir_dentry;
132 struct path lower_old_path, lower_new_path;
134 OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb));
136 file_size_save = i_size_read(d_inode(old_dentry));
137 sdcardfs_get_lower_path(old_dentry, &lower_old_path);
138 sdcardfs_get_lower_path(new_dentry, &lower_new_path);
139 lower_old_dentry = lower_old_path.dentry;
140 lower_new_dentry = lower_new_path.dentry;
141 lower_dir_dentry = lock_parent(lower_new_dentry);
143 err = vfs_link(lower_old_dentry, d_inode(lower_dir_dentry),
144 lower_new_dentry, NULL);
145 if (err || !d_inode(lower_new_dentry))
148 err = sdcardfs_interpose(new_dentry, dir->i_sb, &lower_new_path);
151 fsstack_copy_attr_times(dir, d_inode(lower_new_dentry));
152 fsstack_copy_inode_size(dir, d_inode(lower_new_dentry));
153 set_nlink(d_inode(old_dentry),
154 sdcardfs_lower_inode(d_inode(old_dentry))->i_nlink);
155 i_size_write(d_inode(new_dentry), file_size_save);
157 unlock_dir(lower_dir_dentry);
158 sdcardfs_put_lower_path(old_dentry, &lower_old_path);
159 sdcardfs_put_lower_path(new_dentry, &lower_new_path);
165 static int sdcardfs_unlink(struct inode *dir, struct dentry *dentry)
168 struct dentry *lower_dentry;
169 struct vfsmount *lower_mnt;
170 struct inode *lower_dir_inode = sdcardfs_lower_inode(dir);
171 struct dentry *lower_dir_dentry;
172 struct path lower_path;
173 const struct cred *saved_cred = NULL;
175 if (!check_caller_access_to_name(dir, &dentry->d_name)) {
180 /* save current_cred and override it */
181 OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir));
183 sdcardfs_get_lower_path(dentry, &lower_path);
184 lower_dentry = lower_path.dentry;
185 lower_mnt = lower_path.mnt;
187 lower_dir_dentry = lock_parent(lower_dentry);
189 err = vfs_unlink2(lower_mnt, lower_dir_inode, lower_dentry, NULL);
192 * Note: unlinking on top of NFS can cause silly-renamed files.
193 * Trying to delete such files results in EBUSY from NFS
194 * below. Silly-renamed files will get deleted by NFS later on, so
195 * we just need to detect them here and treat such EBUSY errors as
196 * if the upper file was successfully deleted.
198 if (err == -EBUSY && lower_dentry->d_flags & DCACHE_NFSFS_RENAMED)
202 fsstack_copy_attr_times(dir, lower_dir_inode);
203 fsstack_copy_inode_size(dir, lower_dir_inode);
204 set_nlink(d_inode(dentry),
205 sdcardfs_lower_inode(d_inode(dentry))->i_nlink);
206 d_inode(dentry)->i_ctime = dir->i_ctime;
207 d_drop(dentry); /* this is needed, else LTP fails (VFS won't do it) */
209 unlock_dir(lower_dir_dentry);
211 sdcardfs_put_lower_path(dentry, &lower_path);
212 REVERT_CRED(saved_cred);
218 static int sdcardfs_symlink(struct inode *dir, struct dentry *dentry,
222 struct dentry *lower_dentry;
223 struct dentry *lower_parent_dentry = NULL;
224 struct path lower_path;
226 OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb));
228 sdcardfs_get_lower_path(dentry, &lower_path);
229 lower_dentry = lower_path.dentry;
230 lower_parent_dentry = lock_parent(lower_dentry);
232 err = vfs_symlink(d_inode(lower_parent_dentry), lower_dentry, symname);
235 err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path);
238 fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
239 fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
242 unlock_dir(lower_parent_dentry);
243 sdcardfs_put_lower_path(dentry, &lower_path);
249 static int touch(char *abs_path, mode_t mode)
251 struct file *filp = filp_open(abs_path, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, mode);
254 if (PTR_ERR(filp) == -EEXIST) {
257 pr_err("sdcardfs: failed to open(%s): %ld\n",
258 abs_path, PTR_ERR(filp));
259 return PTR_ERR(filp);
262 filp_close(filp, current->files);
266 static int sdcardfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
269 int make_nomedia_in_obb = 0;
270 struct dentry *lower_dentry;
271 struct vfsmount *lower_mnt;
272 struct dentry *lower_parent_dentry = NULL;
273 struct path lower_path;
274 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
275 const struct cred *saved_cred = NULL;
276 struct sdcardfs_inode_data *pd = SDCARDFS_I(dir)->data;
278 struct fs_struct *saved_fs;
279 struct fs_struct *copied_fs;
280 struct qstr q_obb = QSTR_LITERAL("obb");
281 struct qstr q_data = QSTR_LITERAL("data");
283 if (!check_caller_access_to_name(dir, &dentry->d_name)) {
288 /* save current_cred and override it */
289 OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir));
291 /* check disk space */
292 if (!check_min_free_space(dentry, 0, 1)) {
293 pr_err("sdcardfs: No minimum free space.\n");
298 /* the lower_dentry is negative here */
299 sdcardfs_get_lower_path(dentry, &lower_path);
300 lower_dentry = lower_path.dentry;
301 lower_mnt = lower_path.mnt;
302 lower_parent_dentry = lock_parent(lower_dentry);
304 /* set last 16bytes of mode field to 0775 */
305 mode = (mode & S_IFMT) | 00775;
307 /* temporarily change umask for lower fs write */
308 saved_fs = current->fs;
309 copied_fs = copy_fs_struct(current->fs);
312 unlock_dir(lower_parent_dentry);
315 current->fs = copied_fs;
316 current->fs->umask = 0;
317 err = vfs_mkdir2(lower_mnt, d_inode(lower_parent_dentry), lower_dentry, mode);
320 unlock_dir(lower_parent_dentry);
324 /* if it is a local obb dentry, setup it with the base obbpath */
325 if (need_graft_path(dentry)) {
327 err = setup_obb_dentry(dentry, &lower_path);
329 /* if the sbi->obbpath is not available, the lower_path won't be
330 * changed by setup_obb_dentry() but the lower path is saved to
331 * its orig_path. this dentry will be revalidated later.
332 * but now, the lower_path should be NULL
334 sdcardfs_put_reset_lower_path(dentry);
336 /* the newly created lower path which saved to its orig_path or
337 * the lower_path is the base obbpath.
338 * therefore, an additional path_get is required
340 path_get(&lower_path);
342 make_nomedia_in_obb = 1;
345 err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path, pd->userid);
347 unlock_dir(lower_parent_dentry);
351 fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
352 fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
353 /* update number of links on parent directory */
354 set_nlink(dir, sdcardfs_lower_inode(dir)->i_nlink);
355 fixup_lower_ownership(dentry, dentry->d_name.name);
356 unlock_dir(lower_parent_dentry);
357 if ((!sbi->options.multiuser) && (qstr_case_eq(&dentry->d_name, &q_obb))
358 && (pd->perm == PERM_ANDROID) && (pd->userid == 0))
359 make_nomedia_in_obb = 1;
361 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
362 if (make_nomedia_in_obb ||
363 ((pd->perm == PERM_ANDROID)
364 && (qstr_case_eq(&dentry->d_name, &q_data)))) {
365 REVERT_CRED(saved_cred);
366 OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(d_inode(dentry)));
367 set_fs_pwd(current->fs, &lower_path);
368 touch_err = touch(".nomedia", 0664);
370 pr_err("sdcardfs: failed to create .nomedia in %s: %d\n",
371 lower_path.dentry->d_name.name, touch_err);
376 current->fs = saved_fs;
377 free_fs_struct(copied_fs);
379 sdcardfs_put_lower_path(dentry, &lower_path);
381 REVERT_CRED(saved_cred);
386 static int sdcardfs_rmdir(struct inode *dir, struct dentry *dentry)
388 struct dentry *lower_dentry;
389 struct dentry *lower_dir_dentry;
390 struct vfsmount *lower_mnt;
392 struct path lower_path;
393 const struct cred *saved_cred = NULL;
395 if (!check_caller_access_to_name(dir, &dentry->d_name)) {
400 /* save current_cred and override it */
401 OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir));
403 /* sdcardfs_get_real_lower(): in case of remove an user's obb dentry
404 * the dentry on the original path should be deleted.
406 sdcardfs_get_real_lower(dentry, &lower_path);
408 lower_dentry = lower_path.dentry;
409 lower_mnt = lower_path.mnt;
410 lower_dir_dentry = lock_parent(lower_dentry);
412 err = vfs_rmdir2(lower_mnt, d_inode(lower_dir_dentry), lower_dentry);
416 d_drop(dentry); /* drop our dentry on success (why not VFS's job?) */
418 clear_nlink(d_inode(dentry));
419 fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
420 fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
421 set_nlink(dir, d_inode(lower_dir_dentry)->i_nlink);
424 unlock_dir(lower_dir_dentry);
425 sdcardfs_put_real_lower(dentry, &lower_path);
426 REVERT_CRED(saved_cred);
432 static int sdcardfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
436 struct dentry *lower_dentry;
437 struct dentry *lower_parent_dentry = NULL;
438 struct path lower_path;
440 OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb));
442 sdcardfs_get_lower_path(dentry, &lower_path);
443 lower_dentry = lower_path.dentry;
444 lower_parent_dentry = lock_parent(lower_dentry);
446 err = vfs_mknod(d_inode(lower_parent_dentry), lower_dentry, mode, dev);
450 err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path);
453 fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
454 fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
457 unlock_dir(lower_parent_dentry);
458 sdcardfs_put_lower_path(dentry, &lower_path);
465 * The locking rules in sdcardfs_rename are complex. We could use a simpler
466 * superblock-level name-space lock for renames and copy-ups.
468 static int sdcardfs_rename(struct inode *old_dir, struct dentry *old_dentry,
469 struct inode *new_dir, struct dentry *new_dentry)
472 struct dentry *lower_old_dentry = NULL;
473 struct dentry *lower_new_dentry = NULL;
474 struct dentry *lower_old_dir_dentry = NULL;
475 struct dentry *lower_new_dir_dentry = NULL;
476 struct vfsmount *lower_mnt = NULL;
477 struct dentry *trap = NULL;
478 struct path lower_old_path, lower_new_path;
479 const struct cred *saved_cred = NULL;
481 if (!check_caller_access_to_name(old_dir, &old_dentry->d_name) ||
482 !check_caller_access_to_name(new_dir, &new_dentry->d_name)) {
487 /* save current_cred and override it */
488 OVERRIDE_CRED(SDCARDFS_SB(old_dir->i_sb), saved_cred, SDCARDFS_I(new_dir));
490 sdcardfs_get_real_lower(old_dentry, &lower_old_path);
491 sdcardfs_get_lower_path(new_dentry, &lower_new_path);
492 lower_old_dentry = lower_old_path.dentry;
493 lower_new_dentry = lower_new_path.dentry;
494 lower_mnt = lower_old_path.mnt;
495 lower_old_dir_dentry = dget_parent(lower_old_dentry);
496 lower_new_dir_dentry = dget_parent(lower_new_dentry);
498 trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
499 /* source should not be ancestor of target */
500 if (trap == lower_old_dentry) {
504 /* target should not be ancestor of source */
505 if (trap == lower_new_dentry) {
510 err = vfs_rename2(lower_mnt,
511 d_inode(lower_old_dir_dentry), lower_old_dentry,
512 d_inode(lower_new_dir_dentry), lower_new_dentry,
517 /* Copy attrs from lower dir, but i_uid/i_gid */
518 sdcardfs_copy_and_fix_attrs(new_dir, d_inode(lower_new_dir_dentry));
519 fsstack_copy_inode_size(new_dir, d_inode(lower_new_dir_dentry));
521 if (new_dir != old_dir) {
522 sdcardfs_copy_and_fix_attrs(old_dir, d_inode(lower_old_dir_dentry));
523 fsstack_copy_inode_size(old_dir, d_inode(lower_old_dir_dentry));
525 get_derived_permission_new(new_dentry->d_parent, old_dentry, &new_dentry->d_name);
526 fixup_tmp_permissions(d_inode(old_dentry));
527 fixup_lower_ownership(old_dentry, new_dentry->d_name.name);
528 d_invalidate(old_dentry); /* Can't fixup ownership recursively :( */
530 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
531 dput(lower_old_dir_dentry);
532 dput(lower_new_dir_dentry);
533 sdcardfs_put_real_lower(old_dentry, &lower_old_path);
534 sdcardfs_put_lower_path(new_dentry, &lower_new_path);
535 REVERT_CRED(saved_cred);
541 static int sdcardfs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
544 struct dentry *lower_dentry;
545 struct path lower_path;
546 /* XXX readlink does not requires overriding credential */
548 sdcardfs_get_lower_path(dentry, &lower_path);
549 lower_dentry = lower_path.dentry;
550 if (!d_inode(lower_dentry)->i_op ||
551 !d_inode(lower_dentry)->i_op->readlink) {
556 err = d_inode(lower_dentry)->i_op->readlink(lower_dentry,
560 fsstack_copy_attr_atime(d_inode(dentry), d_inode(lower_dentry));
563 sdcardfs_put_lower_path(dentry, &lower_path);
569 static const char *sdcardfs_follow_link(struct dentry *dentry, void **cookie)
572 int len = PAGE_SIZE, err;
575 /* This is freed by the put_link method assuming a successful call. */
576 buf = kmalloc(len, GFP_KERNEL);
578 buf = ERR_PTR(-ENOMEM);
582 /* read the symlink, and then we will follow it */
585 err = sdcardfs_readlink(dentry, buf, len);
593 return *cookie = buf;
597 static int sdcardfs_permission_wrn(struct inode *inode, int mask)
599 WARN_RATELIMIT(1, "sdcardfs does not support permission. Use permission2.\n");
603 void copy_attrs(struct inode *dest, const struct inode *src)
605 dest->i_mode = src->i_mode;
606 dest->i_uid = src->i_uid;
607 dest->i_gid = src->i_gid;
608 dest->i_rdev = src->i_rdev;
609 dest->i_atime = src->i_atime;
610 dest->i_mtime = src->i_mtime;
611 dest->i_ctime = src->i_ctime;
612 dest->i_blkbits = src->i_blkbits;
613 dest->i_flags = src->i_flags;
614 #ifdef CONFIG_FS_POSIX_ACL
615 dest->i_acl = src->i_acl;
617 #ifdef CONFIG_SECURITY
618 dest->i_security = src->i_security;
622 static int sdcardfs_permission(struct vfsmount *mnt, struct inode *inode, int mask)
626 struct sdcardfs_inode_data *top = top_data_get(SDCARDFS_I(inode));
632 * Permission check on sdcardfs inode.
633 * Calling process should have AID_SDCARD_RW permission
634 * Since generic_permission only needs i_mode, i_uid,
635 * i_gid, and i_sb, we can create a fake inode to pass
636 * this information down in.
638 * The underlying code may attempt to take locks in some
639 * cases for features we're not using, but if that changes,
640 * locks must be dealt with to avoid undefined behavior.
642 copy_attrs(&tmp, inode);
643 tmp.i_uid = make_kuid(&init_user_ns, top->d_uid);
644 tmp.i_gid = make_kgid(&init_user_ns, get_gid(mnt, top));
645 tmp.i_mode = (inode->i_mode & S_IFMT)
646 | get_mode(mnt, SDCARDFS_I(inode), top);
648 tmp.i_sb = inode->i_sb;
649 if (IS_POSIXACL(inode))
650 pr_warn("%s: This may be undefined behavior...\n", __func__);
651 err = generic_permission(&tmp, mask);
653 * Original sdcardfs code calls inode_permission(lower_inode,.. )
654 * for checking inode permission. But doing such things here seems
655 * duplicated work, because the functions called after this func,
656 * such as vfs_create, vfs_unlink, vfs_rename, and etc,
657 * does exactly same thing, i.e., they calls inode_permission().
658 * So we just let they do the things.
659 * If there are any security hole, just uncomment following if block.
664 * Permission check on lower_inode(=EXT4).
665 * we check it with AID_MEDIA_RW permission
667 struct inode *lower_inode;
669 OVERRIDE_CRED(SDCARDFS_SB(inode->sb));
671 lower_inode = sdcardfs_lower_inode(inode);
672 err = inode_permission(lower_inode, mask);
681 static int sdcardfs_setattr_wrn(struct dentry *dentry, struct iattr *ia)
683 WARN_RATELIMIT(1, "sdcardfs does not support setattr. User setattr2.\n");
687 static int sdcardfs_setattr(struct vfsmount *mnt, struct dentry *dentry, struct iattr *ia)
690 struct dentry *lower_dentry;
691 struct vfsmount *lower_mnt;
693 struct inode *lower_inode;
694 struct path lower_path;
695 struct iattr lower_ia;
696 struct dentry *parent;
698 struct sdcardfs_inode_data *top;
699 const struct cred *saved_cred = NULL;
701 inode = d_inode(dentry);
702 top = top_data_get(SDCARDFS_I(inode));
708 * Permission check on sdcardfs inode.
709 * Calling process should have AID_SDCARD_RW permission
710 * Since generic_permission only needs i_mode, i_uid,
711 * i_gid, and i_sb, we can create a fake inode to pass
712 * this information down in.
714 * The underlying code may attempt to take locks in some
715 * cases for features we're not using, but if that changes,
716 * locks must be dealt with to avoid undefined behavior.
719 copy_attrs(&tmp, inode);
720 tmp.i_uid = make_kuid(&init_user_ns, top->d_uid);
721 tmp.i_gid = make_kgid(&init_user_ns, get_gid(mnt, top));
722 tmp.i_mode = (inode->i_mode & S_IFMT)
723 | get_mode(mnt, SDCARDFS_I(inode), top);
724 tmp.i_size = i_size_read(inode);
726 tmp.i_sb = inode->i_sb;
729 * Check if user has permission to change inode. We don't check if
730 * this user can change the lower inode: that should happen when
731 * calling notify_change on the lower inode.
733 /* prepare our own lower struct iattr (with the lower file) */
734 memcpy(&lower_ia, ia, sizeof(lower_ia));
735 /* Allow touch updating timestamps. A previous permission check ensures
736 * we have write access. Changes to mode, owner, and group are ignored
738 ia->ia_valid |= ATTR_FORCE;
739 err = inode_change_ok(&tmp, ia);
742 /* check the Android group ID */
743 parent = dget_parent(dentry);
744 if (!check_caller_access_to_name(d_inode(parent), &dentry->d_name))
752 /* save current_cred and override it */
753 OVERRIDE_CRED(SDCARDFS_SB(dentry->d_sb), saved_cred, SDCARDFS_I(inode));
755 sdcardfs_get_lower_path(dentry, &lower_path);
756 lower_dentry = lower_path.dentry;
757 lower_mnt = lower_path.mnt;
758 lower_inode = sdcardfs_lower_inode(inode);
760 if (ia->ia_valid & ATTR_FILE)
761 lower_ia.ia_file = sdcardfs_lower_file(ia->ia_file);
763 lower_ia.ia_valid &= ~(ATTR_UID | ATTR_GID | ATTR_MODE);
766 * If shrinking, first truncate upper level to cancel writing dirty
767 * pages beyond the new eof; and also if its' maxbytes is more
768 * limiting (fail with -EFBIG before making any change to the lower
769 * level). There is no need to vmtruncate the upper level
770 * afterwards in the other cases: we fsstack_copy_inode_size from
773 if (ia->ia_valid & ATTR_SIZE) {
774 err = inode_newsize_ok(&tmp, ia->ia_size);
778 truncate_setsize(inode, ia->ia_size);
782 * mode change is for clearing setuid/setgid bits. Allow lower fs
783 * to interpret this in its own way.
785 if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
786 lower_ia.ia_valid &= ~ATTR_MODE;
788 /* notify the (possibly copied-up) lower inode */
790 * Note: we use d_inode(lower_dentry), because lower_inode may be
791 * unlinked (no inode->i_sb and i_ino==0. This happens if someone
792 * tries to open(), unlink(), then ftruncate() a file.
794 mutex_lock(&d_inode(lower_dentry)->i_mutex);
795 err = notify_change2(lower_mnt, lower_dentry, &lower_ia, /* note: lower_ia */
797 mutex_unlock(&d_inode(lower_dentry)->i_mutex);
801 /* get attributes from the lower inode and update derived permissions */
802 sdcardfs_copy_and_fix_attrs(inode, lower_inode);
805 * Not running fsstack_copy_inode_size(inode, lower_inode), because
806 * VFS should update our inode size, and notify_change on
807 * lower_inode should update its size.
811 sdcardfs_put_lower_path(dentry, &lower_path);
812 REVERT_CRED(saved_cred);
817 static int sdcardfs_fillattr(struct vfsmount *mnt,
818 struct inode *inode, struct kstat *stat)
820 struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
821 struct sdcardfs_inode_data *top = top_data_get(info);
826 stat->dev = inode->i_sb->s_dev;
827 stat->ino = inode->i_ino;
828 stat->mode = (inode->i_mode & S_IFMT) | get_mode(mnt, info, top);
829 stat->nlink = inode->i_nlink;
830 stat->uid = make_kuid(&init_user_ns, top->d_uid);
831 stat->gid = make_kgid(&init_user_ns, get_gid(mnt, top));
832 stat->rdev = inode->i_rdev;
833 stat->size = i_size_read(inode);
834 stat->atime = inode->i_atime;
835 stat->mtime = inode->i_mtime;
836 stat->ctime = inode->i_ctime;
837 stat->blksize = (1 << inode->i_blkbits);
838 stat->blocks = inode->i_blocks;
843 static int sdcardfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
846 struct kstat lower_stat;
847 struct path lower_path;
848 struct dentry *parent;
851 parent = dget_parent(dentry);
852 if (!check_caller_access_to_name(d_inode(parent), &dentry->d_name)) {
858 sdcardfs_get_lower_path(dentry, &lower_path);
859 err = vfs_getattr(&lower_path, &lower_stat);
862 sdcardfs_copy_and_fix_attrs(d_inode(dentry),
863 d_inode(lower_path.dentry));
864 err = sdcardfs_fillattr(mnt, d_inode(dentry), stat);
865 stat->blocks = lower_stat.blocks;
867 sdcardfs_put_lower_path(dentry, &lower_path);
871 const struct inode_operations sdcardfs_symlink_iops = {
872 .permission2 = sdcardfs_permission,
873 .setattr2 = sdcardfs_setattr,
874 /* XXX Following operations are implemented,
875 * but FUSE(sdcard) or FAT does not support them
876 * These methods are *NOT* perfectly tested.
877 .readlink = sdcardfs_readlink,
878 .follow_link = sdcardfs_follow_link,
879 .put_link = kfree_put_link,
883 const struct inode_operations sdcardfs_dir_iops = {
884 .create = sdcardfs_create,
885 .lookup = sdcardfs_lookup,
886 .permission = sdcardfs_permission_wrn,
887 .permission2 = sdcardfs_permission,
888 .unlink = sdcardfs_unlink,
889 .mkdir = sdcardfs_mkdir,
890 .rmdir = sdcardfs_rmdir,
891 .rename = sdcardfs_rename,
892 .setattr = sdcardfs_setattr_wrn,
893 .setattr2 = sdcardfs_setattr,
894 .getattr = sdcardfs_getattr,
895 /* XXX Following operations are implemented,
896 * but FUSE(sdcard) or FAT does not support them
897 * These methods are *NOT* perfectly tested.
898 .symlink = sdcardfs_symlink,
899 .link = sdcardfs_link,
900 .mknod = sdcardfs_mknod,
904 const struct inode_operations sdcardfs_main_iops = {
905 .permission = sdcardfs_permission_wrn,
906 .permission2 = sdcardfs_permission,
907 .setattr = sdcardfs_setattr_wrn,
908 .setattr2 = sdcardfs_setattr,
909 .getattr = sdcardfs_getattr,