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