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