OSDN Git Service

85eb8ebb5372279ba5ac32296fcd5eaabaa29ea6
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / fs / sdcardfs / inode.c
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
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)
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
53 /* Do not directly use this function, use REVERT_CRED() instead. */
54 void revert_fsids(const struct cred *old_cred)
55 {
56         const struct cred *cur_cred;
57
58         cur_cred = current->cred;
59         revert_creds(old_cred);
60         put_cred(cur_cred);
61 }
62
63 static int sdcardfs_create(struct inode *dir, struct dentry *dentry,
64                          umode_t mode, bool want_excl)
65 {
66         int err;
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;
74
75         if (!check_caller_access_to_name(dir, &dentry->d_name)) {
76                 err = -EACCES;
77                 goto out_eacces;
78         }
79
80         /* save current_cred and override it */
81         OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir));
82
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);
87
88         /* set last 16bytes of mode field to 0664 */
89         mode = (mode & S_IFMT) | 00664;
90
91         /* temporarily change umask for lower fs write */
92         saved_fs = current->fs;
93         copied_fs = copy_fs_struct(current->fs);
94         if (!copied_fs) {
95                 err = -ENOMEM;
96                 goto out_unlock;
97         }
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);
101         if (err)
102                 goto out;
103
104         err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path,
105                         SDCARDFS_I(dir)->data->userid);
106         if (err)
107                 goto out;
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);
111
112 out:
113         current->fs = saved_fs;
114         free_fs_struct(copied_fs);
115 out_unlock:
116         unlock_dir(lower_parent_dentry);
117         sdcardfs_put_lower_path(dentry, &lower_path);
118         REVERT_CRED(saved_cred);
119 out_eacces:
120         return err;
121 }
122
123 #if 0
124 static int sdcardfs_link(struct dentry *old_dentry, struct inode *dir,
125                        struct dentry *new_dentry)
126 {
127         struct dentry *lower_old_dentry;
128         struct dentry *lower_new_dentry;
129         struct dentry *lower_dir_dentry;
130         u64 file_size_save;
131         int err;
132         struct path lower_old_path, lower_new_path;
133
134         OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb));
135
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);
142
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))
146                 goto out;
147
148         err = sdcardfs_interpose(new_dentry, dir->i_sb, &lower_new_path);
149         if (err)
150                 goto out;
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);
156 out:
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);
160         REVERT_CRED();
161         return err;
162 }
163 #endif
164
165 static int sdcardfs_unlink(struct inode *dir, struct dentry *dentry)
166 {
167         int err;
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;
174
175         if (!check_caller_access_to_name(dir, &dentry->d_name)) {
176                 err = -EACCES;
177                 goto out_eacces;
178         }
179
180         /* save current_cred and override it */
181         OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir));
182
183         sdcardfs_get_lower_path(dentry, &lower_path);
184         lower_dentry = lower_path.dentry;
185         lower_mnt = lower_path.mnt;
186         dget(lower_dentry);
187         lower_dir_dentry = lock_parent(lower_dentry);
188
189         err = vfs_unlink2(lower_mnt, lower_dir_inode, lower_dentry, NULL);
190
191         /*
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.
197          */
198         if (err == -EBUSY && lower_dentry->d_flags & DCACHE_NFSFS_RENAMED)
199                 err = 0;
200         if (err)
201                 goto out;
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) */
208 out:
209         unlock_dir(lower_dir_dentry);
210         dput(lower_dentry);
211         sdcardfs_put_lower_path(dentry, &lower_path);
212         REVERT_CRED(saved_cred);
213 out_eacces:
214         return err;
215 }
216
217 #if 0
218 static int sdcardfs_symlink(struct inode *dir, struct dentry *dentry,
219                           const char *symname)
220 {
221         int err;
222         struct dentry *lower_dentry;
223         struct dentry *lower_parent_dentry = NULL;
224         struct path lower_path;
225
226         OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb));
227
228         sdcardfs_get_lower_path(dentry, &lower_path);
229         lower_dentry = lower_path.dentry;
230         lower_parent_dentry = lock_parent(lower_dentry);
231
232         err = vfs_symlink(d_inode(lower_parent_dentry), lower_dentry, symname);
233         if (err)
234                 goto out;
235         err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path);
236         if (err)
237                 goto out;
238         fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
239         fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
240
241 out:
242         unlock_dir(lower_parent_dentry);
243         sdcardfs_put_lower_path(dentry, &lower_path);
244         REVERT_CRED();
245         return err;
246 }
247 #endif
248
249 static int touch(char *abs_path, mode_t mode)
250 {
251         struct file *filp = filp_open(abs_path, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, mode);
252
253         if (IS_ERR(filp)) {
254                 if (PTR_ERR(filp) == -EEXIST) {
255                         return 0;
256                 } else {
257                         pr_err("sdcardfs: failed to open(%s): %ld\n",
258                                                 abs_path, PTR_ERR(filp));
259                         return PTR_ERR(filp);
260                 }
261         }
262         filp_close(filp, current->files);
263         return 0;
264 }
265
266 static int sdcardfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
267 {
268         int err;
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;
277         int touch_err = 0;
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");
282
283         if (!check_caller_access_to_name(dir, &dentry->d_name)) {
284                 err = -EACCES;
285                 goto out_eacces;
286         }
287
288         /* save current_cred and override it */
289         OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir));
290
291         /* check disk space */
292         if (!check_min_free_space(dentry, 0, 1)) {
293                 pr_err("sdcardfs: No minimum free space.\n");
294                 err = -ENOSPC;
295                 goto out_revert;
296         }
297
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);
303
304         /* set last 16bytes of mode field to 0775 */
305         mode = (mode & S_IFMT) | 00775;
306
307         /* temporarily change umask for lower fs write */
308         saved_fs = current->fs;
309         copied_fs = copy_fs_struct(current->fs);
310         if (!copied_fs) {
311                 err = -ENOMEM;
312                 unlock_dir(lower_parent_dentry);
313                 goto out_unlock;
314         }
315         current->fs = copied_fs;
316         current->fs->umask = 0;
317         err = vfs_mkdir2(lower_mnt, d_inode(lower_parent_dentry), lower_dentry, mode);
318
319         if (err) {
320                 unlock_dir(lower_parent_dentry);
321                 goto out;
322         }
323
324         /* if it is a local obb dentry, setup it with the base obbpath */
325         if (need_graft_path(dentry)) {
326
327                 err = setup_obb_dentry(dentry, &lower_path);
328                 if (err) {
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
333                          */
334                         sdcardfs_put_reset_lower_path(dentry);
335
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
339                          */
340                         path_get(&lower_path);
341                 } else
342                         make_nomedia_in_obb = 1;
343         }
344
345         err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path, pd->userid);
346         if (err) {
347                 unlock_dir(lower_parent_dentry);
348                 goto out;
349         }
350
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;
360
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);
369                 if (touch_err) {
370                         pr_err("sdcardfs: failed to create .nomedia in %s: %d\n",
371                                                         lower_path.dentry->d_name.name, touch_err);
372                         goto out;
373                 }
374         }
375 out:
376         current->fs = saved_fs;
377         free_fs_struct(copied_fs);
378 out_unlock:
379         sdcardfs_put_lower_path(dentry, &lower_path);
380 out_revert:
381         REVERT_CRED(saved_cred);
382 out_eacces:
383         return err;
384 }
385
386 static int sdcardfs_rmdir(struct inode *dir, struct dentry *dentry)
387 {
388         struct dentry *lower_dentry;
389         struct dentry *lower_dir_dentry;
390         struct vfsmount *lower_mnt;
391         int err;
392         struct path lower_path;
393         const struct cred *saved_cred = NULL;
394
395         if (!check_caller_access_to_name(dir, &dentry->d_name)) {
396                 err = -EACCES;
397                 goto out_eacces;
398         }
399
400         /* save current_cred and override it */
401         OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir));
402
403         /* sdcardfs_get_real_lower(): in case of remove an user's obb dentry
404          * the dentry on the original path should be deleted.
405          */
406         sdcardfs_get_real_lower(dentry, &lower_path);
407
408         lower_dentry = lower_path.dentry;
409         lower_mnt = lower_path.mnt;
410         lower_dir_dentry = lock_parent(lower_dentry);
411
412         err = vfs_rmdir2(lower_mnt, d_inode(lower_dir_dentry), lower_dentry);
413         if (err)
414                 goto out;
415
416         d_drop(dentry); /* drop our dentry on success (why not VFS's job?) */
417         if (d_inode(dentry))
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);
422
423 out:
424         unlock_dir(lower_dir_dentry);
425         sdcardfs_put_real_lower(dentry, &lower_path);
426         REVERT_CRED(saved_cred);
427 out_eacces:
428         return err;
429 }
430
431 #if 0
432 static int sdcardfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
433                         dev_t dev)
434 {
435         int err;
436         struct dentry *lower_dentry;
437         struct dentry *lower_parent_dentry = NULL;
438         struct path lower_path;
439
440         OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb));
441
442         sdcardfs_get_lower_path(dentry, &lower_path);
443         lower_dentry = lower_path.dentry;
444         lower_parent_dentry = lock_parent(lower_dentry);
445
446         err = vfs_mknod(d_inode(lower_parent_dentry), lower_dentry, mode, dev);
447         if (err)
448                 goto out;
449
450         err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path);
451         if (err)
452                 goto out;
453         fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
454         fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
455
456 out:
457         unlock_dir(lower_parent_dentry);
458         sdcardfs_put_lower_path(dentry, &lower_path);
459         REVERT_CRED();
460         return err;
461 }
462 #endif
463
464 /*
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.
467  */
468 static int sdcardfs_rename(struct inode *old_dir, struct dentry *old_dentry,
469                          struct inode *new_dir, struct dentry *new_dentry)
470 {
471         int err = 0;
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;
480
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)) {
483                 err = -EACCES;
484                 goto out_eacces;
485         }
486
487         /* save current_cred and override it */
488         OVERRIDE_CRED(SDCARDFS_SB(old_dir->i_sb), saved_cred, SDCARDFS_I(new_dir));
489
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);
497
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) {
501                 err = -EINVAL;
502                 goto out;
503         }
504         /* target should not be ancestor of source */
505         if (trap == lower_new_dentry) {
506                 err = -ENOTEMPTY;
507                 goto out;
508         }
509
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,
513                          NULL, 0);
514         if (err)
515                 goto out;
516
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));
520
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));
524         }
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 :( */
529 out:
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);
536 out_eacces:
537         return err;
538 }
539
540 #if 0
541 static int sdcardfs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
542 {
543         int err;
544         struct dentry *lower_dentry;
545         struct path lower_path;
546         /* XXX readlink does not requires overriding credential */
547
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) {
552                 err = -EINVAL;
553                 goto out;
554         }
555
556         err = d_inode(lower_dentry)->i_op->readlink(lower_dentry,
557                                                     buf, bufsiz);
558         if (err < 0)
559                 goto out;
560         fsstack_copy_attr_atime(d_inode(dentry), d_inode(lower_dentry));
561
562 out:
563         sdcardfs_put_lower_path(dentry, &lower_path);
564         return err;
565 }
566 #endif
567
568 #if 0
569 static const char *sdcardfs_follow_link(struct dentry *dentry, void **cookie)
570 {
571         char *buf;
572         int len = PAGE_SIZE, err;
573         mm_segment_t old_fs;
574
575         /* This is freed by the put_link method assuming a successful call. */
576         buf = kmalloc(len, GFP_KERNEL);
577         if (!buf) {
578                 buf = ERR_PTR(-ENOMEM);
579                 return buf;
580         }
581
582         /* read the symlink, and then we will follow it */
583         old_fs = get_fs();
584         set_fs(KERNEL_DS);
585         err = sdcardfs_readlink(dentry, buf, len);
586         set_fs(old_fs);
587         if (err < 0) {
588                 kfree(buf);
589                 buf = ERR_PTR(err);
590         } else {
591                 buf[err] = '\0';
592         }
593         return *cookie = buf;
594 }
595 #endif
596
597 static int sdcardfs_permission_wrn(struct inode *inode, int mask)
598 {
599         WARN_RATELIMIT(1, "sdcardfs does not support permission. Use permission2.\n");
600         return -EINVAL;
601 }
602
603 void copy_attrs(struct inode *dest, const struct inode *src)
604 {
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;
616 #endif
617 #ifdef CONFIG_SECURITY
618         dest->i_security = src->i_security;
619 #endif
620 }
621
622 static int sdcardfs_permission(struct vfsmount *mnt, struct inode *inode, int mask)
623 {
624         int err;
625         struct inode tmp;
626         struct sdcardfs_inode_data *top = top_data_get(SDCARDFS_I(inode));
627
628         if (!top)
629                 return -EINVAL;
630
631         /*
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.
637          *
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.
641          */
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);
647         data_put(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);
652         /* XXX
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.
660          */
661 #if 0
662         if (!err) {
663                 /*
664                  * Permission check on lower_inode(=EXT4).
665                  * we check it with AID_MEDIA_RW permission
666                  */
667                 struct inode *lower_inode;
668
669                 OVERRIDE_CRED(SDCARDFS_SB(inode->sb));
670
671                 lower_inode = sdcardfs_lower_inode(inode);
672                 err = inode_permission(lower_inode, mask);
673
674                 REVERT_CRED();
675         }
676 #endif
677         return err;
678
679 }
680
681 static int sdcardfs_setattr_wrn(struct dentry *dentry, struct iattr *ia)
682 {
683         WARN_RATELIMIT(1, "sdcardfs does not support setattr. User setattr2.\n");
684         return -EINVAL;
685 }
686
687 static int sdcardfs_setattr(struct vfsmount *mnt, struct dentry *dentry, struct iattr *ia)
688 {
689         int err;
690         struct dentry *lower_dentry;
691         struct vfsmount *lower_mnt;
692         struct inode *inode;
693         struct inode *lower_inode;
694         struct path lower_path;
695         struct iattr lower_ia;
696         struct dentry *parent;
697         struct inode tmp;
698         struct sdcardfs_inode_data *top;
699         const struct cred *saved_cred = NULL;
700
701         inode = d_inode(dentry);
702         top = top_data_get(SDCARDFS_I(inode));
703
704         if (!top)
705                 return -EINVAL;
706
707         /*
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.
713          *
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.
717          *
718          */
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);
725         data_put(top);
726         tmp.i_sb = inode->i_sb;
727
728         /*
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.
732          */
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
737          */
738         ia->ia_valid |= ATTR_FORCE;
739         err = inode_change_ok(&tmp, ia);
740
741         if (!err) {
742                 /* check the Android group ID */
743                 parent = dget_parent(dentry);
744                 if (!check_caller_access_to_name(d_inode(parent), &dentry->d_name))
745                         err = -EACCES;
746                 dput(parent);
747         }
748
749         if (err)
750                 goto out_err;
751
752         /* save current_cred and override it */
753         OVERRIDE_CRED(SDCARDFS_SB(dentry->d_sb), saved_cred, SDCARDFS_I(inode));
754
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);
759
760         if (ia->ia_valid & ATTR_FILE)
761                 lower_ia.ia_file = sdcardfs_lower_file(ia->ia_file);
762
763         lower_ia.ia_valid &= ~(ATTR_UID | ATTR_GID | ATTR_MODE);
764
765         /*
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
771          * the lower level.
772          */
773         if (ia->ia_valid & ATTR_SIZE) {
774                 err = inode_newsize_ok(&tmp, ia->ia_size);
775                 if (err) {
776                         goto out;
777                 }
778                 truncate_setsize(inode, ia->ia_size);
779         }
780
781         /*
782          * mode change is for clearing setuid/setgid bits. Allow lower fs
783          * to interpret this in its own way.
784          */
785         if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
786                 lower_ia.ia_valid &= ~ATTR_MODE;
787
788         /* notify the (possibly copied-up) lower inode */
789         /*
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.
793          */
794         mutex_lock(&d_inode(lower_dentry)->i_mutex);
795         err = notify_change2(lower_mnt, lower_dentry, &lower_ia, /* note: lower_ia */
796                         NULL);
797         mutex_unlock(&d_inode(lower_dentry)->i_mutex);
798         if (err)
799                 goto out;
800
801         /* get attributes from the lower inode and update derived permissions */
802         sdcardfs_copy_and_fix_attrs(inode, lower_inode);
803
804         /*
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.
808          */
809
810 out:
811         sdcardfs_put_lower_path(dentry, &lower_path);
812         REVERT_CRED(saved_cred);
813 out_err:
814         return err;
815 }
816
817 static int sdcardfs_fillattr(struct vfsmount *mnt,
818                                 struct inode *inode, struct kstat *stat)
819 {
820         struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
821         struct sdcardfs_inode_data *top = top_data_get(info);
822
823         if (!top)
824                 return -EINVAL;
825
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;
839         data_put(top);
840         return 0;
841 }
842
843 static int sdcardfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
844                  struct kstat *stat)
845 {
846         struct kstat lower_stat;
847         struct path lower_path;
848         struct dentry *parent;
849         int err;
850
851         parent = dget_parent(dentry);
852         if (!check_caller_access_to_name(d_inode(parent), &dentry->d_name)) {
853                 dput(parent);
854                 return -EACCES;
855         }
856         dput(parent);
857
858         sdcardfs_get_lower_path(dentry, &lower_path);
859         err = vfs_getattr(&lower_path, &lower_stat);
860         if (err)
861                 goto out;
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;
866 out:
867         sdcardfs_put_lower_path(dentry, &lower_path);
868         return err;
869 }
870
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,
880          */
881 };
882
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,
901          */
902 };
903
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,
910 };