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