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