Merge branch 'linux-linaro-lsk-v4.4' into linux-linaro-lsk-v4.4-android
[firefly-linux-kernel-4.4.55.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
24 /* Do not directly use this function. Use OVERRIDE_CRED() instead. */
25 const struct cred * override_fsids(struct sdcardfs_sb_info* sbi, struct sdcardfs_inode_info *info)
26 {
27         struct cred * cred;
28         const struct cred * old_cred;
29         uid_t uid;
30
31         cred = prepare_creds();
32         if (!cred)
33                 return NULL;
34
35         if (info->under_obb)
36                 uid = AID_MEDIA_OBB;
37         else
38                 uid = multiuser_get_uid(info->userid, sbi->options.fs_low_uid);
39         cred->fsuid = make_kuid(&init_user_ns, uid);
40         cred->fsgid = make_kgid(&init_user_ns, sbi->options.fs_low_gid);
41
42         old_cred = override_creds(cred);
43
44         return old_cred;
45 }
46
47 /* Do not directly use this function, use REVERT_CRED() instead. */
48 void revert_fsids(const struct cred * old_cred)
49 {
50         const struct cred * cur_cred;
51
52         cur_cred = current->cred;
53         revert_creds(old_cred);
54         put_cred(cur_cred);
55 }
56
57 static int sdcardfs_create(struct inode *dir, struct dentry *dentry,
58                          umode_t mode, bool want_excl)
59 {
60         int err;
61         struct dentry *lower_dentry;
62         struct vfsmount *lower_dentry_mnt;
63         struct dentry *lower_parent_dentry = NULL;
64         struct path lower_path;
65         const struct cred *saved_cred = NULL;
66         struct fs_struct *saved_fs;
67         struct fs_struct *copied_fs;
68
69         if(!check_caller_access_to_name(dir, &dentry->d_name)) {
70                 printk(KERN_INFO "%s: need to check the caller's gid in packages.list\n"
71                                                  "  dentry: %s, task:%s\n",
72                                                  __func__, dentry->d_name.name, current->comm);
73                 err = -EACCES;
74                 goto out_eacces;
75         }
76
77         /* save current_cred and override it */
78         OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir));
79
80         sdcardfs_get_lower_path(dentry, &lower_path);
81         lower_dentry = lower_path.dentry;
82         lower_dentry_mnt = lower_path.mnt;
83         lower_parent_dentry = lock_parent(lower_dentry);
84
85         /* set last 16bytes of mode field to 0664 */
86         mode = (mode & S_IFMT) | 00664;
87
88         /* temporarily change umask for lower fs write */
89         saved_fs = current->fs;
90         copied_fs = copy_fs_struct(current->fs);
91         if (!copied_fs) {
92                 err = -ENOMEM;
93                 goto out_unlock;
94         }
95         current->fs = copied_fs;
96         current->fs->umask = 0;
97         err = vfs_create2(lower_dentry_mnt, d_inode(lower_parent_dentry), lower_dentry, mode, want_excl);
98         if (err)
99                 goto out;
100
101         err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path, SDCARDFS_I(dir)->userid);
102         if (err)
103                 goto out;
104         fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
105         fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
106         fixup_lower_ownership(dentry, dentry->d_name.name);
107
108 out:
109         current->fs = saved_fs;
110         free_fs_struct(copied_fs);
111 out_unlock:
112         unlock_dir(lower_parent_dentry);
113         sdcardfs_put_lower_path(dentry, &lower_path);
114         REVERT_CRED(saved_cred);
115 out_eacces:
116         return err;
117 }
118
119 #if 0
120 static int sdcardfs_link(struct dentry *old_dentry, struct inode *dir,
121                        struct dentry *new_dentry)
122 {
123         struct dentry *lower_old_dentry;
124         struct dentry *lower_new_dentry;
125         struct dentry *lower_dir_dentry;
126         u64 file_size_save;
127         int err;
128         struct path lower_old_path, lower_new_path;
129
130         OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb));
131
132         file_size_save = i_size_read(d_inode(old_dentry));
133         sdcardfs_get_lower_path(old_dentry, &lower_old_path);
134         sdcardfs_get_lower_path(new_dentry, &lower_new_path);
135         lower_old_dentry = lower_old_path.dentry;
136         lower_new_dentry = lower_new_path.dentry;
137         lower_dir_dentry = lock_parent(lower_new_dentry);
138
139         err = vfs_link(lower_old_dentry, d_inode(lower_dir_dentry),
140                        lower_new_dentry, NULL);
141         if (err || !d_inode(lower_new_dentry))
142                 goto out;
143
144         err = sdcardfs_interpose(new_dentry, dir->i_sb, &lower_new_path);
145         if (err)
146                 goto out;
147         fsstack_copy_attr_times(dir, d_inode(lower_new_dentry));
148         fsstack_copy_inode_size(dir, d_inode(lower_new_dentry));
149         set_nlink(d_inode(old_dentry),
150                   sdcardfs_lower_inode(d_inode(old_dentry))->i_nlink);
151         i_size_write(d_inode(new_dentry), file_size_save);
152 out:
153         unlock_dir(lower_dir_dentry);
154         sdcardfs_put_lower_path(old_dentry, &lower_old_path);
155         sdcardfs_put_lower_path(new_dentry, &lower_new_path);
156         REVERT_CRED();
157         return err;
158 }
159 #endif
160
161 static int sdcardfs_unlink(struct inode *dir, struct dentry *dentry)
162 {
163         int err;
164         struct dentry *lower_dentry;
165         struct vfsmount *lower_mnt;
166         struct inode *lower_dir_inode = sdcardfs_lower_inode(dir);
167         struct dentry *lower_dir_dentry;
168         struct path lower_path;
169         const struct cred *saved_cred = NULL;
170
171         if(!check_caller_access_to_name(dir, &dentry->d_name)) {
172                 printk(KERN_INFO "%s: need to check the caller's gid in packages.list\n"
173                                                  "  dentry: %s, task:%s\n",
174                                                  __func__, dentry->d_name.name, current->comm);
175                 err = -EACCES;
176                 goto out_eacces;
177         }
178
179         /* save current_cred and override it */
180         OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir));
181
182         sdcardfs_get_lower_path(dentry, &lower_path);
183         lower_dentry = lower_path.dentry;
184         lower_mnt = lower_path.mnt;
185         dget(lower_dentry);
186         lower_dir_dentry = lock_parent(lower_dentry);
187
188         err = vfs_unlink2(lower_mnt, lower_dir_inode, lower_dentry, NULL);
189
190         /*
191          * Note: unlinking on top of NFS can cause silly-renamed files.
192          * Trying to delete such files results in EBUSY from NFS
193          * below.  Silly-renamed files will get deleted by NFS later on, so
194          * we just need to detect them here and treat such EBUSY errors as
195          * if the upper file was successfully deleted.
196          */
197         if (err == -EBUSY && lower_dentry->d_flags & DCACHE_NFSFS_RENAMED)
198                 err = 0;
199         if (err)
200                 goto out;
201         fsstack_copy_attr_times(dir, lower_dir_inode);
202         fsstack_copy_inode_size(dir, lower_dir_inode);
203         set_nlink(d_inode(dentry),
204                   sdcardfs_lower_inode(d_inode(dentry))->i_nlink);
205         d_inode(dentry)->i_ctime = dir->i_ctime;
206         d_drop(dentry); /* this is needed, else LTP fails (VFS won't do it) */
207 out:
208         unlock_dir(lower_dir_dentry);
209         dput(lower_dentry);
210         sdcardfs_put_lower_path(dentry, &lower_path);
211         REVERT_CRED(saved_cred);
212 out_eacces:
213         return err;
214 }
215
216 #if 0
217 static int sdcardfs_symlink(struct inode *dir, struct dentry *dentry,
218                           const char *symname)
219 {
220         int err;
221         struct dentry *lower_dentry;
222         struct dentry *lower_parent_dentry = NULL;
223         struct path lower_path;
224
225         OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb));
226
227         sdcardfs_get_lower_path(dentry, &lower_path);
228         lower_dentry = lower_path.dentry;
229         lower_parent_dentry = lock_parent(lower_dentry);
230
231         err = vfs_symlink(d_inode(lower_parent_dentry), lower_dentry, symname);
232         if (err)
233                 goto out;
234         err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path);
235         if (err)
236                 goto out;
237         fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
238         fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
239
240 out:
241         unlock_dir(lower_parent_dentry);
242         sdcardfs_put_lower_path(dentry, &lower_path);
243         REVERT_CRED();
244         return err;
245 }
246 #endif
247
248 static int touch(char *abs_path, mode_t mode) {
249         struct file *filp = filp_open(abs_path, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, mode);
250         if (IS_ERR(filp)) {
251                 if (PTR_ERR(filp) == -EEXIST) {
252                         return 0;
253                 }
254                 else {
255                         printk(KERN_ERR "sdcardfs: failed to open(%s): %ld\n",
256                                                 abs_path, PTR_ERR(filp));
257                         return PTR_ERR(filp);
258                 }
259         }
260         filp_close(filp, current->files);
261         return 0;
262 }
263
264 static int sdcardfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
265 {
266         int err;
267         int make_nomedia_in_obb = 0;
268         struct dentry *lower_dentry;
269         struct vfsmount *lower_mnt;
270         struct dentry *lower_parent_dentry = NULL;
271         struct path lower_path;
272         struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
273         const struct cred *saved_cred = NULL;
274         struct sdcardfs_inode_info *pi = SDCARDFS_I(dir);
275         int touch_err = 0;
276         struct fs_struct *saved_fs;
277         struct fs_struct *copied_fs;
278         struct qstr q_obb = QSTR_LITERAL("obb");
279         struct qstr q_data = QSTR_LITERAL("data");
280
281         if(!check_caller_access_to_name(dir, &dentry->d_name)) {
282                 printk(KERN_INFO "%s: need to check the caller's gid in packages.list\n"
283                                                  "  dentry: %s, task:%s\n",
284                                                  __func__, dentry->d_name.name, current->comm);
285                 err = -EACCES;
286                 goto out_eacces;
287         }
288
289         /* save current_cred and override it */
290         OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir));
291
292         /* check disk space */
293         if (!check_min_free_space(dentry, 0, 1)) {
294                 printk(KERN_INFO "sdcardfs: No minimum free space.\n");
295                 err = -ENOSPC;
296                 goto out_revert;
297         }
298
299         /* the lower_dentry is negative here */
300         sdcardfs_get_lower_path(dentry, &lower_path);
301         lower_dentry = lower_path.dentry;
302         lower_mnt = lower_path.mnt;
303         lower_parent_dentry = lock_parent(lower_dentry);
304
305         /* set last 16bytes of mode field to 0775 */
306         mode = (mode & S_IFMT) | 00775;
307
308         /* temporarily change umask for lower fs write */
309         saved_fs = current->fs;
310         copied_fs = copy_fs_struct(current->fs);
311         if (!copied_fs) {
312                 err = -ENOMEM;
313                 unlock_dir(lower_parent_dentry);
314                 goto out_unlock;
315         }
316         current->fs = copied_fs;
317         current->fs->umask = 0;
318         err = vfs_mkdir2(lower_mnt, d_inode(lower_parent_dentry), lower_dentry, mode);
319
320         if (err) {
321                 unlock_dir(lower_parent_dentry);
322                 goto out;
323         }
324
325         /* if it is a local obb dentry, setup it with the base obbpath */
326         if(need_graft_path(dentry)) {
327
328                 err = setup_obb_dentry(dentry, &lower_path);
329                 if(err) {
330                         /* if the sbi->obbpath is not available, the lower_path won't be
331                          * changed by setup_obb_dentry() but the lower path is saved to
332                          * its orig_path. this dentry will be revalidated later.
333                          * but now, the lower_path should be NULL */
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                         path_get(&lower_path);
340                 } else
341                         make_nomedia_in_obb = 1;
342         }
343
344         err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path, pi->userid);
345         if (err) {
346                 unlock_dir(lower_parent_dentry);
347                 goto out;
348         }
349
350         fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
351         fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
352         /* update number of links on parent directory */
353         set_nlink(dir, sdcardfs_lower_inode(dir)->i_nlink);
354         fixup_lower_ownership(dentry, dentry->d_name.name);
355         unlock_dir(lower_parent_dentry);
356         if ((!sbi->options.multiuser) && (qstr_case_eq(&dentry->d_name, &q_obb))
357                 && (pi->perm == PERM_ANDROID) && (pi->userid == 0))
358                 make_nomedia_in_obb = 1;
359
360         /* When creating /Android/data and /Android/obb, mark them as .nomedia */
361         if (make_nomedia_in_obb ||
362                 ((pi->perm == PERM_ANDROID) && (qstr_case_eq(&dentry->d_name, &q_data)))) {
363                 REVERT_CRED(saved_cred);
364                 OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(d_inode(dentry)));
365                 set_fs_pwd(current->fs, &lower_path);
366                 touch_err = touch(".nomedia", 0664);
367                 if (touch_err) {
368                         printk(KERN_ERR "sdcardfs: failed to create .nomedia in %s: %d\n",
369                                                         lower_path.dentry->d_name.name, touch_err);
370                         goto out;
371                 }
372         }
373 out:
374         current->fs = saved_fs;
375         free_fs_struct(copied_fs);
376 out_unlock:
377         sdcardfs_put_lower_path(dentry, &lower_path);
378 out_revert:
379         REVERT_CRED(saved_cred);
380 out_eacces:
381         return err;
382 }
383
384 static int sdcardfs_rmdir(struct inode *dir, struct dentry *dentry)
385 {
386         struct dentry *lower_dentry;
387         struct dentry *lower_dir_dentry;
388         struct vfsmount *lower_mnt;
389         int err;
390         struct path lower_path;
391         const struct cred *saved_cred = NULL;
392
393         if(!check_caller_access_to_name(dir, &dentry->d_name)) {
394                 printk(KERN_INFO "%s: need to check the caller's gid in packages.list\n"
395                                                  "  dentry: %s, task:%s\n",
396                                                  __func__, dentry->d_name.name, current->comm);
397                 err = -EACCES;
398                 goto out_eacces;
399         }
400
401         /* save current_cred and override it */
402         OVERRIDE_CRED(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir));
403
404         /* sdcardfs_get_real_lower(): in case of remove an user's obb dentry
405          * the dentry on the original path should be deleted. */
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                 printk(KERN_INFO "%s: need to check the caller's gid in packages.list\n"
484                                                  "  new_dentry: %s, task:%s\n",
485                                                  __func__, new_dentry->d_name.name, current->comm);
486                 err = -EACCES;
487                 goto out_eacces;
488         }
489
490         /* save current_cred and override it */
491         OVERRIDE_CRED(SDCARDFS_SB(old_dir->i_sb), saved_cred, SDCARDFS_I(new_dir));
492
493         sdcardfs_get_real_lower(old_dentry, &lower_old_path);
494         sdcardfs_get_lower_path(new_dentry, &lower_new_path);
495         lower_old_dentry = lower_old_path.dentry;
496         lower_new_dentry = lower_new_path.dentry;
497         lower_mnt = lower_old_path.mnt;
498         lower_old_dir_dentry = dget_parent(lower_old_dentry);
499         lower_new_dir_dentry = dget_parent(lower_new_dentry);
500
501         trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
502         /* source should not be ancestor of target */
503         if (trap == lower_old_dentry) {
504                 err = -EINVAL;
505                 goto out;
506         }
507         /* target should not be ancestor of source */
508         if (trap == lower_new_dentry) {
509                 err = -ENOTEMPTY;
510                 goto out;
511         }
512
513         err = vfs_rename2(lower_mnt,
514                          d_inode(lower_old_dir_dentry), lower_old_dentry,
515                          d_inode(lower_new_dir_dentry), lower_new_dentry,
516                          NULL, 0);
517         if (err)
518                 goto out;
519
520         /* Copy attrs from lower dir, but i_uid/i_gid */
521         sdcardfs_copy_and_fix_attrs(new_dir, d_inode(lower_new_dir_dentry));
522         fsstack_copy_inode_size(new_dir, d_inode(lower_new_dir_dentry));
523
524         if (new_dir != old_dir) {
525                 sdcardfs_copy_and_fix_attrs(old_dir, d_inode(lower_old_dir_dentry));
526                 fsstack_copy_inode_size(old_dir, d_inode(lower_old_dir_dentry));
527         }
528         get_derived_permission_new(new_dentry->d_parent, old_dentry, &new_dentry->d_name);
529         fixup_tmp_permissions(d_inode(old_dentry));
530         fixup_lower_ownership(old_dentry, new_dentry->d_name.name);
531         drop_recursive(old_dentry); /* Can't fixup ownership recursively :( */
532 out:
533         unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
534         dput(lower_old_dir_dentry);
535         dput(lower_new_dir_dentry);
536         sdcardfs_put_real_lower(old_dentry, &lower_old_path);
537         sdcardfs_put_lower_path(new_dentry, &lower_new_path);
538         REVERT_CRED(saved_cred);
539 out_eacces:
540         return err;
541 }
542
543 #if 0
544 static int sdcardfs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
545 {
546         int err;
547         struct dentry *lower_dentry;
548         struct path lower_path;
549         /* XXX readlink does not requires overriding credential */
550
551         sdcardfs_get_lower_path(dentry, &lower_path);
552         lower_dentry = lower_path.dentry;
553         if (!d_inode(lower_dentry)->i_op ||
554             !d_inode(lower_dentry)->i_op->readlink) {
555                 err = -EINVAL;
556                 goto out;
557         }
558
559         err = d_inode(lower_dentry)->i_op->readlink(lower_dentry,
560                                                     buf, bufsiz);
561         if (err < 0)
562                 goto out;
563         fsstack_copy_attr_atime(d_inode(dentry), d_inode(lower_dentry));
564
565 out:
566         sdcardfs_put_lower_path(dentry, &lower_path);
567         return err;
568 }
569 #endif
570
571 #if 0
572 static const char *sdcardfs_follow_link(struct dentry *dentry, void **cookie)
573 {
574         char *buf;
575         int len = PAGE_SIZE, err;
576         mm_segment_t old_fs;
577
578         /* This is freed by the put_link method assuming a successful call. */
579         buf = kmalloc(len, GFP_KERNEL);
580         if (!buf) {
581                 buf = ERR_PTR(-ENOMEM);
582                 return buf;
583         }
584
585         /* read the symlink, and then we will follow it */
586         old_fs = get_fs();
587         set_fs(KERNEL_DS);
588         err = sdcardfs_readlink(dentry, buf, len);
589         set_fs(old_fs);
590         if (err < 0) {
591                 kfree(buf);
592                 buf = ERR_PTR(err);
593         } else {
594                 buf[err] = '\0';
595         }
596         return *cookie = buf;
597 }
598 #endif
599
600 static int sdcardfs_permission_wrn(struct inode *inode, int mask)
601 {
602         WARN(1, "sdcardfs does not support permission. Use permission2.\n");
603         return -EINVAL;
604 }
605
606 void copy_attrs(struct inode *dest, const struct inode *src)
607 {
608         dest->i_mode = src->i_mode;
609         dest->i_uid = src->i_uid;
610         dest->i_gid = src->i_gid;
611         dest->i_rdev = src->i_rdev;
612         dest->i_atime = src->i_atime;
613         dest->i_mtime = src->i_mtime;
614         dest->i_ctime = src->i_ctime;
615         dest->i_blkbits = src->i_blkbits;
616         dest->i_flags = src->i_flags;
617 #ifdef CONFIG_FS_POSIX_ACL
618         dest->i_acl = src->i_acl;
619 #endif
620 #ifdef CONFIG_SECURITY
621         dest->i_security = src->i_security;
622 #endif
623 }
624
625 static int sdcardfs_permission(struct vfsmount *mnt, struct inode *inode, int mask)
626 {
627         int err;
628         struct inode tmp;
629         struct inode *top = grab_top(SDCARDFS_I(inode));
630
631         if (!top) {
632                 release_top(SDCARDFS_I(inode));
633                 WARN(1, "Top value was null!\n");
634                 return -EINVAL;
635         }
636
637         /*
638          * Permission check on sdcardfs inode.
639          * Calling process should have AID_SDCARD_RW permission
640          * Since generic_permission only needs i_mode, i_uid,
641          * i_gid, and i_sb, we can create a fake inode to pass
642          * this information down in.
643          *
644          * The underlying code may attempt to take locks in some
645          * cases for features we're not using, but if that changes,
646          * locks must be dealt with to avoid undefined behavior.
647          */
648         copy_attrs(&tmp, inode);
649         tmp.i_uid = make_kuid(&init_user_ns, SDCARDFS_I(top)->d_uid);
650         tmp.i_gid = make_kgid(&init_user_ns, get_gid(mnt, SDCARDFS_I(top)));
651         tmp.i_mode = (inode->i_mode & S_IFMT) | get_mode(mnt, SDCARDFS_I(top));
652         release_top(SDCARDFS_I(inode));
653         tmp.i_sb = inode->i_sb;
654         if (IS_POSIXACL(inode))
655                 printk(KERN_WARNING "%s: This may be undefined behavior... \n", __func__);
656         err = generic_permission(&tmp, mask);
657         /* XXX
658          * Original sdcardfs code calls inode_permission(lower_inode,.. )
659          * for checking inode permission. But doing such things here seems
660          * duplicated work, because the functions called after this func,
661          * such as vfs_create, vfs_unlink, vfs_rename, and etc,
662          * does exactly same thing, i.e., they calls inode_permission().
663          * So we just let they do the things.
664          * If there are any security hole, just uncomment following if block.
665          */
666 #if 0
667         if (!err) {
668                 /*
669                  * Permission check on lower_inode(=EXT4).
670                  * we check it with AID_MEDIA_RW permission
671                  */
672                 struct inode *lower_inode;
673                 OVERRIDE_CRED(SDCARDFS_SB(inode->sb));
674
675                 lower_inode = sdcardfs_lower_inode(inode);
676                 err = inode_permission(lower_inode, mask);
677
678                 REVERT_CRED();
679         }
680 #endif
681         return err;
682
683 }
684
685 static int sdcardfs_setattr_wrn(struct dentry *dentry, struct iattr *ia)
686 {
687         WARN(1, "sdcardfs does not support setattr. User setattr2.\n");
688         return -EINVAL;
689 }
690
691 static int sdcardfs_setattr(struct vfsmount *mnt, struct dentry *dentry, struct iattr *ia)
692 {
693         int err;
694         struct dentry *lower_dentry;
695         struct vfsmount *lower_mnt;
696         struct inode *inode;
697         struct inode *lower_inode;
698         struct path lower_path;
699         struct iattr lower_ia;
700         struct dentry *parent;
701         struct inode tmp;
702         struct inode *top;
703         const struct cred *saved_cred = NULL;
704
705         inode = d_inode(dentry);
706         top = grab_top(SDCARDFS_I(inode));
707
708         if (!top) {
709                 release_top(SDCARDFS_I(inode));
710                 return -EINVAL;
711         }
712
713         /*
714          * Permission check on sdcardfs inode.
715          * Calling process should have AID_SDCARD_RW permission
716          * Since generic_permission only needs i_mode, i_uid,
717          * i_gid, and i_sb, we can create a fake inode to pass
718          * this information down in.
719          *
720          * The underlying code may attempt to take locks in some
721          * cases for features we're not using, but if that changes,
722          * locks must be dealt with to avoid undefined behavior.
723          *
724          */
725         copy_attrs(&tmp, inode);
726         tmp.i_uid = make_kuid(&init_user_ns, SDCARDFS_I(top)->d_uid);
727         tmp.i_gid = make_kgid(&init_user_ns, get_gid(mnt, SDCARDFS_I(top)));
728         tmp.i_mode = (inode->i_mode & S_IFMT) | get_mode(mnt, SDCARDFS_I(top));
729         tmp.i_size = i_size_read(inode);
730         release_top(SDCARDFS_I(inode));
731         tmp.i_sb = inode->i_sb;
732
733         /*
734          * Check if user has permission to change inode.  We don't check if
735          * this user can change the lower inode: that should happen when
736          * calling notify_change on the lower inode.
737          */
738         /* prepare our own lower struct iattr (with the lower file) */
739         memcpy(&lower_ia, ia, sizeof(lower_ia));
740         /* Allow touch updating timestamps. A previous permission check ensures
741          * we have write access. Changes to mode, owner, and group are ignored*/
742         ia->ia_valid |= ATTR_FORCE;
743         err = inode_change_ok(&tmp, ia);
744
745         if (!err) {
746                 /* check the Android group ID */
747                 parent = dget_parent(dentry);
748                 if(!check_caller_access_to_name(d_inode(parent), &dentry->d_name)) {
749                         printk(KERN_INFO "%s: need to check the caller's gid in packages.list\n"
750                                                          "  dentry: %s, task:%s\n",
751                                                          __func__, dentry->d_name.name, current->comm);
752                         err = -EACCES;
753                 }
754                 dput(parent);
755         }
756
757         if (err)
758                 goto out_err;
759
760         /* save current_cred and override it */
761         OVERRIDE_CRED(SDCARDFS_SB(dentry->d_sb), saved_cred, SDCARDFS_I(inode));
762
763         sdcardfs_get_lower_path(dentry, &lower_path);
764         lower_dentry = lower_path.dentry;
765         lower_mnt = lower_path.mnt;
766         lower_inode = sdcardfs_lower_inode(inode);
767
768         if (ia->ia_valid & ATTR_FILE)
769                 lower_ia.ia_file = sdcardfs_lower_file(ia->ia_file);
770
771         lower_ia.ia_valid &= ~(ATTR_UID | ATTR_GID | ATTR_MODE);
772
773         /*
774          * If shrinking, first truncate upper level to cancel writing dirty
775          * pages beyond the new eof; and also if its' maxbytes is more
776          * limiting (fail with -EFBIG before making any change to the lower
777          * level).  There is no need to vmtruncate the upper level
778          * afterwards in the other cases: we fsstack_copy_inode_size from
779          * the lower level.
780          */
781         if (current->mm)
782                 down_write(&current->mm->mmap_sem);
783         if (ia->ia_valid & ATTR_SIZE) {
784                 err = inode_newsize_ok(&tmp, ia->ia_size);
785                 if (err) {
786                         if (current->mm)
787                                 up_write(&current->mm->mmap_sem);
788                         goto out;
789                 }
790                 truncate_setsize(inode, ia->ia_size);
791         }
792
793         /*
794          * mode change is for clearing setuid/setgid bits. Allow lower fs
795          * to interpret this in its own way.
796          */
797         if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
798                 lower_ia.ia_valid &= ~ATTR_MODE;
799
800         /* notify the (possibly copied-up) lower inode */
801         /*
802          * Note: we use d_inode(lower_dentry), because lower_inode may be
803          * unlinked (no inode->i_sb and i_ino==0.  This happens if someone
804          * tries to open(), unlink(), then ftruncate() a file.
805          */
806         mutex_lock(&d_inode(lower_dentry)->i_mutex);
807         err = notify_change2(lower_mnt, lower_dentry, &lower_ia, /* note: lower_ia */
808                         NULL);
809         mutex_unlock(&d_inode(lower_dentry)->i_mutex);
810         if (current->mm)
811                 up_write(&current->mm->mmap_sem);
812         if (err)
813                 goto out;
814
815         /* get attributes from the lower inode and update derived permissions */
816         sdcardfs_copy_and_fix_attrs(inode, lower_inode);
817
818         /*
819          * Not running fsstack_copy_inode_size(inode, lower_inode), because
820          * VFS should update our inode size, and notify_change on
821          * lower_inode should update its size.
822          */
823
824 out:
825         sdcardfs_put_lower_path(dentry, &lower_path);
826         REVERT_CRED(saved_cred);
827 out_err:
828         return err;
829 }
830
831 static int sdcardfs_fillattr(struct vfsmount *mnt, struct inode *inode, struct kstat *stat)
832 {
833         struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
834         struct inode *top = grab_top(info);
835         if (!top)
836                 return -EINVAL;
837
838         stat->dev = inode->i_sb->s_dev;
839         stat->ino = inode->i_ino;
840         stat->mode = (inode->i_mode  & S_IFMT) | get_mode(mnt, SDCARDFS_I(top));
841         stat->nlink = inode->i_nlink;
842         stat->uid = make_kuid(&init_user_ns, SDCARDFS_I(top)->d_uid);
843         stat->gid = make_kgid(&init_user_ns, get_gid(mnt, SDCARDFS_I(top)));
844         stat->rdev = inode->i_rdev;
845         stat->size = i_size_read(inode);
846         stat->atime = inode->i_atime;
847         stat->mtime = inode->i_mtime;
848         stat->ctime = inode->i_ctime;
849         stat->blksize = (1 << inode->i_blkbits);
850         stat->blocks = inode->i_blocks;
851         release_top(info);
852         return 0;
853 }
854
855 static int sdcardfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
856                  struct kstat *stat)
857 {
858         struct dentry *lower_dentry;
859         struct inode *inode;
860         struct inode *lower_inode;
861         struct path lower_path;
862         struct dentry *parent;
863         int err;
864
865         parent = dget_parent(dentry);
866         if(!check_caller_access_to_name(d_inode(parent), &dentry->d_name)) {
867                 printk(KERN_INFO "%s: need to check the caller's gid in packages.list\n"
868                                                  "  dentry: %s, task:%s\n",
869                                                  __func__, dentry->d_name.name, current->comm);
870                 dput(parent);
871                 return -EACCES;
872         }
873         dput(parent);
874
875         inode = d_inode(dentry);
876
877         sdcardfs_get_lower_path(dentry, &lower_path);
878         lower_dentry = lower_path.dentry;
879         lower_inode = sdcardfs_lower_inode(inode);
880
881         sdcardfs_copy_and_fix_attrs(inode, lower_inode);
882         fsstack_copy_inode_size(inode, lower_inode);
883
884         err = sdcardfs_fillattr(mnt, inode, stat);
885         sdcardfs_put_lower_path(dentry, &lower_path);
886         return err;
887 }
888
889 const struct inode_operations sdcardfs_symlink_iops = {
890         .permission2    = sdcardfs_permission,
891         .setattr2       = sdcardfs_setattr,
892         /* XXX Following operations are implemented,
893          *     but FUSE(sdcard) or FAT does not support them
894          *     These methods are *NOT* perfectly tested.
895         .readlink       = sdcardfs_readlink,
896         .follow_link    = sdcardfs_follow_link,
897         .put_link       = kfree_put_link,
898          */
899 };
900
901 const struct inode_operations sdcardfs_dir_iops = {
902         .create         = sdcardfs_create,
903         .lookup         = sdcardfs_lookup,
904         .permission     = sdcardfs_permission_wrn,
905         .permission2    = sdcardfs_permission,
906         .unlink         = sdcardfs_unlink,
907         .mkdir          = sdcardfs_mkdir,
908         .rmdir          = sdcardfs_rmdir,
909         .rename         = sdcardfs_rename,
910         .setattr        = sdcardfs_setattr_wrn,
911         .setattr2       = sdcardfs_setattr,
912         .getattr        = sdcardfs_getattr,
913         /* XXX Following operations are implemented,
914          *     but FUSE(sdcard) or FAT does not support them
915          *     These methods are *NOT* perfectly tested.
916         .symlink        = sdcardfs_symlink,
917         .link           = sdcardfs_link,
918         .mknod          = sdcardfs_mknod,
919          */
920 };
921
922 const struct inode_operations sdcardfs_main_iops = {
923         .permission     = sdcardfs_permission_wrn,
924         .permission2    = sdcardfs_permission,
925         .setattr        = sdcardfs_setattr_wrn,
926         .setattr2       = sdcardfs_setattr,
927         .getattr        = sdcardfs_getattr,
928 };