1 /*
2 * fs/sdcardfs/inode.c
3 *
4 * Copyright (c) 2013 Samsung Electronics Co. Ltd
5 * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
6 * Sunghwan Yun, Sungjong Seo
7 *
8 * This program has been developed as a stackable file system based on
9 * the WrapFS which written by
10 *
11 * Copyright (c) 1998-2011 Erez Zadok
12 * Copyright (c) 2009 Shrikar Archak
13 * Copyright (c) 2003-2011 Stony Brook University
14 * Copyright (c) 2003-2011 The Research Foundation of SUNY
15 *
16 * This file is dual licensed. It may be redistributed and/or modified
17 * under the terms of the Apache 2.0 License OR version 2 of the GNU
18 * General Public License.
19 */
20
21 #include "sdcardfs.h"
22 #include <linux/fs_struct.h>
23 #include <linux/ratelimit.h>
24
override_fsids(struct sdcardfs_sb_info * sbi,struct sdcardfs_inode_data * data)25 const struct cred *override_fsids(struct sdcardfs_sb_info *sbi,
26 struct sdcardfs_inode_data *data)
27 {
28 struct cred *cred;
29 const struct cred *old_cred;
30 uid_t uid;
31
32 cred = prepare_creds();
33 if (!cred)
34 return NULL;
35
36 if (sbi->options.gid_derivation) {
37 if (data->under_obb)
38 uid = AID_MEDIA_OBB;
39 else
40 uid = multiuser_get_uid(data->userid, sbi->options.fs_low_uid);
41 } else {
42 uid = sbi->options.fs_low_uid;
43 }
44 cred->fsuid = make_kuid(&init_user_ns, uid);
45 cred->fsgid = make_kgid(&init_user_ns, sbi->options.fs_low_gid);
46
47 old_cred = override_creds(cred);
48
49 return old_cred;
50 }
51
revert_fsids(const struct cred * old_cred)52 void revert_fsids(const struct cred *old_cred)
53 {
54 const struct cred *cur_cred;
55
56 cur_cred = current->cred;
57 revert_creds(old_cred);
58 put_cred(cur_cred);
59 }
60
sdcardfs_create(struct inode * dir,struct dentry * dentry,umode_t mode,bool want_excl)61 static int sdcardfs_create(struct inode *dir, struct dentry *dentry,
62 umode_t mode, bool want_excl)
63 {
64 int err;
65 struct dentry *lower_dentry;
66 struct vfsmount *lower_dentry_mnt;
67 struct dentry *lower_parent_dentry = NULL;
68 struct path lower_path;
69 const struct cred *saved_cred = NULL;
70 struct fs_struct *saved_fs;
71 struct fs_struct *copied_fs;
72
73 if (!check_caller_access_to_name(dir, &dentry->d_name)) {
74 err = -EACCES;
75 goto out_eacces;
76 }
77
78 /* save current_cred and override it */
79 saved_cred = override_fsids(SDCARDFS_SB(dir->i_sb),
80 SDCARDFS_I(dir)->data);
81 if (!saved_cred)
82 return -ENOMEM;
83
84 sdcardfs_get_lower_path(dentry, &lower_path);
85 lower_dentry = lower_path.dentry;
86 lower_dentry_mnt = lower_path.mnt;
87 lower_parent_dentry = lock_parent(lower_dentry);
88
89 /* set last 16bytes of mode field to 0664 */
90 mode = (mode & S_IFMT) | 00664;
91
92 /* temporarily change umask for lower fs write */
93 saved_fs = current->fs;
94 copied_fs = copy_fs_struct(current->fs);
95 if (!copied_fs) {
96 err = -ENOMEM;
97 goto out_unlock;
98 }
99 copied_fs->umask = 0;
100 task_lock(current);
101 current->fs = copied_fs;
102 task_unlock(current);
103
104 err = vfs_create2(lower_dentry_mnt, d_inode(lower_parent_dentry), lower_dentry, mode, want_excl);
105 if (err)
106 goto out;
107
108 err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path,
109 SDCARDFS_I(dir)->data->userid);
110 if (err)
111 goto out;
112 fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
113 fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
114 fixup_lower_ownership(dentry, dentry->d_name.name);
115
116 out:
117 task_lock(current);
118 current->fs = saved_fs;
119 task_unlock(current);
120 free_fs_struct(copied_fs);
121 out_unlock:
122 unlock_dir(lower_parent_dentry);
123 sdcardfs_put_lower_path(dentry, &lower_path);
124 revert_fsids(saved_cred);
125 out_eacces:
126 return err;
127 }
128
sdcardfs_unlink(struct inode * dir,struct dentry * dentry)129 static int sdcardfs_unlink(struct inode *dir, struct dentry *dentry)
130 {
131 int err;
132 struct dentry *lower_dentry;
133 struct vfsmount *lower_mnt;
134 struct inode *lower_dir_inode = sdcardfs_lower_inode(dir);
135 struct dentry *lower_dir_dentry;
136 struct path lower_path;
137 const struct cred *saved_cred = NULL;
138
139 if (!check_caller_access_to_name(dir, &dentry->d_name)) {
140 err = -EACCES;
141 goto out_eacces;
142 }
143
144 /* save current_cred and override it */
145 saved_cred = override_fsids(SDCARDFS_SB(dir->i_sb),
146 SDCARDFS_I(dir)->data);
147 if (!saved_cred)
148 return -ENOMEM;
149
150 sdcardfs_get_lower_path(dentry, &lower_path);
151 lower_dentry = lower_path.dentry;
152 lower_mnt = lower_path.mnt;
153 dget(lower_dentry);
154 lower_dir_dentry = lock_parent(lower_dentry);
155
156 err = vfs_unlink2(lower_mnt, lower_dir_inode, lower_dentry, NULL);
157
158 /*
159 * Note: unlinking on top of NFS can cause silly-renamed files.
160 * Trying to delete such files results in EBUSY from NFS
161 * below. Silly-renamed files will get deleted by NFS later on, so
162 * we just need to detect them here and treat such EBUSY errors as
163 * if the upper file was successfully deleted.
164 */
165 if (err == -EBUSY && lower_dentry->d_flags & DCACHE_NFSFS_RENAMED)
166 err = 0;
167 if (err)
168 goto out;
169 fsstack_copy_attr_times(dir, lower_dir_inode);
170 fsstack_copy_inode_size(dir, lower_dir_inode);
171 set_nlink(d_inode(dentry),
172 sdcardfs_lower_inode(d_inode(dentry))->i_nlink);
173 d_inode(dentry)->i_ctime = dir->i_ctime;
174 d_drop(dentry); /* this is needed, else LTP fails (VFS won't do it) */
175 out:
176 unlock_dir(lower_dir_dentry);
177 dput(lower_dentry);
178 sdcardfs_put_lower_path(dentry, &lower_path);
179 revert_fsids(saved_cred);
180 out_eacces:
181 return err;
182 }
183
touch(char * abs_path,mode_t mode)184 static int touch(char *abs_path, mode_t mode)
185 {
186 struct file *filp = filp_open(abs_path, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, mode);
187
188 if (IS_ERR(filp)) {
189 if (PTR_ERR(filp) == -EEXIST) {
190 return 0;
191 } else {
192 pr_err("sdcardfs: failed to open(%s): %ld\n",
193 abs_path, PTR_ERR(filp));
194 return PTR_ERR(filp);
195 }
196 }
197 filp_close(filp, current->files);
198 return 0;
199 }
200
sdcardfs_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)201 static int sdcardfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
202 {
203 int err;
204 int make_nomedia_in_obb = 0;
205 struct dentry *lower_dentry;
206 struct vfsmount *lower_mnt;
207 struct dentry *lower_parent_dentry = NULL;
208 struct dentry *parent_dentry = NULL;
209 struct path lower_path;
210 struct sdcardfs_sb_info *sbi = SDCARDFS_SB(dentry->d_sb);
211 const struct cred *saved_cred = NULL;
212 struct sdcardfs_inode_data *pd = SDCARDFS_I(dir)->data;
213 int touch_err = 0;
214 struct fs_struct *saved_fs;
215 struct fs_struct *copied_fs;
216 struct qstr q_obb = QSTR_LITERAL("obb");
217 struct qstr q_data = QSTR_LITERAL("data");
218
219 if (!check_caller_access_to_name(dir, &dentry->d_name)) {
220 err = -EACCES;
221 goto out_eacces;
222 }
223
224 /* save current_cred and override it */
225 saved_cred = override_fsids(SDCARDFS_SB(dir->i_sb),
226 SDCARDFS_I(dir)->data);
227 if (!saved_cred)
228 return -ENOMEM;
229
230 /* check disk space */
231 parent_dentry = dget_parent(dentry);
232 if (!check_min_free_space(parent_dentry, 0, 1)) {
233 pr_err("sdcardfs: No minimum free space.\n");
234 err = -ENOSPC;
235 dput(parent_dentry);
236 goto out_revert;
237 }
238 dput(parent_dentry);
239
240 /* the lower_dentry is negative here */
241 sdcardfs_get_lower_path(dentry, &lower_path);
242 lower_dentry = lower_path.dentry;
243 lower_mnt = lower_path.mnt;
244 lower_parent_dentry = lock_parent(lower_dentry);
245
246 /* set last 16bytes of mode field to 0775 */
247 mode = (mode & S_IFMT) | 00775;
248
249 /* temporarily change umask for lower fs write */
250 saved_fs = current->fs;
251 copied_fs = copy_fs_struct(current->fs);
252 if (!copied_fs) {
253 err = -ENOMEM;
254 unlock_dir(lower_parent_dentry);
255 goto out_unlock;
256 }
257 copied_fs->umask = 0;
258 task_lock(current);
259 current->fs = copied_fs;
260 task_unlock(current);
261
262 err = vfs_mkdir2(lower_mnt, d_inode(lower_parent_dentry), lower_dentry, mode);
263
264 if (err) {
265 unlock_dir(lower_parent_dentry);
266 goto out;
267 }
268
269 /* if it is a local obb dentry, setup it with the base obbpath */
270 if (need_graft_path(dentry)) {
271
272 err = setup_obb_dentry(dentry, &lower_path);
273 if (err) {
274 /* if the sbi->obbpath is not available, the lower_path won't be
275 * changed by setup_obb_dentry() but the lower path is saved to
276 * its orig_path. this dentry will be revalidated later.
277 * but now, the lower_path should be NULL
278 */
279 sdcardfs_put_reset_lower_path(dentry);
280
281 /* the newly created lower path which saved to its orig_path or
282 * the lower_path is the base obbpath.
283 * therefore, an additional path_get is required
284 */
285 path_get(&lower_path);
286 } else
287 make_nomedia_in_obb = 1;
288 }
289
290 err = sdcardfs_interpose(dentry, dir->i_sb, &lower_path, pd->userid);
291 if (err) {
292 unlock_dir(lower_parent_dentry);
293 goto out;
294 }
295
296 fsstack_copy_attr_times(dir, sdcardfs_lower_inode(dir));
297 fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
298 /* update number of links on parent directory */
299 set_nlink(dir, sdcardfs_lower_inode(dir)->i_nlink);
300 fixup_lower_ownership(dentry, dentry->d_name.name);
301 unlock_dir(lower_parent_dentry);
302 if ((!sbi->options.multiuser) && (qstr_case_eq(&dentry->d_name, &q_obb))
303 && (pd->perm == PERM_ANDROID) && (pd->userid == 0))
304 make_nomedia_in_obb = 1;
305
306 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
307 if (make_nomedia_in_obb ||
308 ((pd->perm == PERM_ANDROID)
309 && (qstr_case_eq(&dentry->d_name, &q_data)))) {
310 revert_fsids(saved_cred);
311 saved_cred = override_fsids(sbi,
312 SDCARDFS_I(d_inode(dentry))->data);
313 if (!saved_cred) {
314 pr_err("sdcardfs: failed to set up .nomedia in %s: %d\n",
315 lower_path.dentry->d_name.name,
316 -ENOMEM);
317 goto out;
318 }
319 set_fs_pwd(current->fs, &lower_path);
320 touch_err = touch(".nomedia", 0664);
321 if (touch_err) {
322 pr_err("sdcardfs: failed to create .nomedia in %s: %d\n",
323 lower_path.dentry->d_name.name,
324 touch_err);
325 goto out;
326 }
327 }
328 out:
329 task_lock(current);
330 current->fs = saved_fs;
331 task_unlock(current);
332
333 free_fs_struct(copied_fs);
334 out_unlock:
335 sdcardfs_put_lower_path(dentry, &lower_path);
336 out_revert:
337 revert_fsids(saved_cred);
338 out_eacces:
339 return err;
340 }
341
sdcardfs_rmdir(struct inode * dir,struct dentry * dentry)342 static int sdcardfs_rmdir(struct inode *dir, struct dentry *dentry)
343 {
344 struct dentry *lower_dentry;
345 struct dentry *lower_dir_dentry;
346 struct vfsmount *lower_mnt;
347 int err;
348 struct path lower_path;
349 const struct cred *saved_cred = NULL;
350
351 if (!check_caller_access_to_name(dir, &dentry->d_name)) {
352 err = -EACCES;
353 goto out_eacces;
354 }
355
356 /* save current_cred and override it */
357 saved_cred = override_fsids(SDCARDFS_SB(dir->i_sb),
358 SDCARDFS_I(dir)->data);
359 if (!saved_cred)
360 return -ENOMEM;
361
362 /* sdcardfs_get_real_lower(): in case of remove an user's obb dentry
363 * the dentry on the original path should be deleted.
364 */
365 sdcardfs_get_real_lower(dentry, &lower_path);
366
367 lower_dentry = lower_path.dentry;
368 lower_mnt = lower_path.mnt;
369 lower_dir_dentry = lock_parent(lower_dentry);
370
371 err = vfs_rmdir2(lower_mnt, d_inode(lower_dir_dentry), lower_dentry);
372 if (err)
373 goto out;
374
375 d_drop(dentry); /* drop our dentry on success (why not VFS's job?) */
376 if (d_inode(dentry))
377 clear_nlink(d_inode(dentry));
378 fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
379 fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
380 set_nlink(dir, d_inode(lower_dir_dentry)->i_nlink);
381
382 out:
383 unlock_dir(lower_dir_dentry);
384 sdcardfs_put_real_lower(dentry, &lower_path);
385 revert_fsids(saved_cred);
386 out_eacces:
387 return err;
388 }
389
390 /*
391 * The locking rules in sdcardfs_rename are complex. We could use a simpler
392 * superblock-level name-space lock for renames and copy-ups.
393 */
sdcardfs_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)394 static int sdcardfs_rename(struct inode *old_dir, struct dentry *old_dentry,
395 struct inode *new_dir, struct dentry *new_dentry)
396 {
397 int err = 0;
398 struct dentry *lower_old_dentry = NULL;
399 struct dentry *lower_new_dentry = NULL;
400 struct dentry *lower_old_dir_dentry = NULL;
401 struct dentry *lower_new_dir_dentry = NULL;
402 struct vfsmount *lower_mnt = NULL;
403 struct dentry *trap = NULL;
404 struct path lower_old_path, lower_new_path;
405 const struct cred *saved_cred = NULL;
406
407 if (!check_caller_access_to_name(old_dir, &old_dentry->d_name) ||
408 !check_caller_access_to_name(new_dir, &new_dentry->d_name)) {
409 err = -EACCES;
410 goto out_eacces;
411 }
412
413 /* save current_cred and override it */
414 saved_cred = override_fsids(SDCARDFS_SB(old_dir->i_sb),
415 SDCARDFS_I(new_dir)->data);
416 if (!saved_cred)
417 return -ENOMEM;
418
419 sdcardfs_get_real_lower(old_dentry, &lower_old_path);
420 sdcardfs_get_lower_path(new_dentry, &lower_new_path);
421 lower_old_dentry = lower_old_path.dentry;
422 lower_new_dentry = lower_new_path.dentry;
423 lower_mnt = lower_old_path.mnt;
424 lower_old_dir_dentry = dget_parent(lower_old_dentry);
425 lower_new_dir_dentry = dget_parent(lower_new_dentry);
426
427 trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
428 /* source should not be ancestor of target */
429 if (trap == lower_old_dentry) {
430 err = -EINVAL;
431 goto out;
432 }
433 /* target should not be ancestor of source */
434 if (trap == lower_new_dentry) {
435 err = -ENOTEMPTY;
436 goto out;
437 }
438
439 err = vfs_rename2(lower_mnt,
440 d_inode(lower_old_dir_dentry), lower_old_dentry,
441 d_inode(lower_new_dir_dentry), lower_new_dentry,
442 NULL, 0);
443 if (err)
444 goto out;
445
446 /* Copy attrs from lower dir, but i_uid/i_gid */
447 sdcardfs_copy_and_fix_attrs(new_dir, d_inode(lower_new_dir_dentry));
448 fsstack_copy_inode_size(new_dir, d_inode(lower_new_dir_dentry));
449
450 if (new_dir != old_dir) {
451 sdcardfs_copy_and_fix_attrs(old_dir, d_inode(lower_old_dir_dentry));
452 fsstack_copy_inode_size(old_dir, d_inode(lower_old_dir_dentry));
453 }
454 get_derived_permission_new(new_dentry->d_parent, old_dentry, &new_dentry->d_name);
455 fixup_tmp_permissions(d_inode(old_dentry));
456 fixup_lower_ownership(old_dentry, new_dentry->d_name.name);
457 d_invalidate(old_dentry); /* Can't fixup ownership recursively :( */
458 out:
459 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
460 dput(lower_old_dir_dentry);
461 dput(lower_new_dir_dentry);
462 sdcardfs_put_real_lower(old_dentry, &lower_old_path);
463 sdcardfs_put_lower_path(new_dentry, &lower_new_path);
464 revert_fsids(saved_cred);
465 out_eacces:
466 return err;
467 }
468
469 #if 0
470 static int sdcardfs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
471 {
472 int err;
473 struct dentry *lower_dentry;
474 struct path lower_path;
475 /* XXX readlink does not requires overriding credential */
476
477 sdcardfs_get_lower_path(dentry, &lower_path);
478 lower_dentry = lower_path.dentry;
479 if (!d_inode(lower_dentry)->i_op ||
480 !d_inode(lower_dentry)->i_op->readlink) {
481 err = -EINVAL;
482 goto out;
483 }
484
485 err = d_inode(lower_dentry)->i_op->readlink(lower_dentry,
486 buf, bufsiz);
487 if (err < 0)
488 goto out;
489 fsstack_copy_attr_atime(d_inode(dentry), d_inode(lower_dentry));
490
491 out:
492 sdcardfs_put_lower_path(dentry, &lower_path);
493 return err;
494 }
495 #endif
496
497 #if 0
498 static const char *sdcardfs_follow_link(struct dentry *dentry, void **cookie)
499 {
500 char *buf;
501 int len = PAGE_SIZE, err;
502 mm_segment_t old_fs;
503
504 /* This is freed by the put_link method assuming a successful call. */
505 buf = kmalloc(len, GFP_KERNEL);
506 if (!buf) {
507 buf = ERR_PTR(-ENOMEM);
508 return buf;
509 }
510
511 /* read the symlink, and then we will follow it */
512 old_fs = get_fs();
513 set_fs(KERNEL_DS);
514 err = sdcardfs_readlink(dentry, buf, len);
515 set_fs(old_fs);
516 if (err < 0) {
517 kfree(buf);
518 buf = ERR_PTR(err);
519 } else {
520 buf[err] = '\0';
521 }
522 return *cookie = buf;
523 }
524 #endif
525
sdcardfs_permission_wrn(struct inode * inode,int mask)526 static int sdcardfs_permission_wrn(struct inode *inode, int mask)
527 {
528 WARN_RATELIMIT(1, "sdcardfs does not support permission. Use permission2.\n");
529 return -EINVAL;
530 }
531
copy_attrs(struct inode * dest,const struct inode * src)532 void copy_attrs(struct inode *dest, const struct inode *src)
533 {
534 dest->i_mode = src->i_mode;
535 dest->i_uid = src->i_uid;
536 dest->i_gid = src->i_gid;
537 dest->i_rdev = src->i_rdev;
538 dest->i_atime = src->i_atime;
539 dest->i_mtime = src->i_mtime;
540 dest->i_ctime = src->i_ctime;
541 dest->i_blkbits = src->i_blkbits;
542 dest->i_flags = src->i_flags;
543 #ifdef CONFIG_FS_POSIX_ACL
544 dest->i_acl = src->i_acl;
545 #endif
546 #ifdef CONFIG_SECURITY
547 dest->i_security = src->i_security;
548 #endif
549 }
550
sdcardfs_permission(struct vfsmount * mnt,struct inode * inode,int mask)551 static int sdcardfs_permission(struct vfsmount *mnt, struct inode *inode, int mask)
552 {
553 int err;
554 struct inode tmp;
555 struct sdcardfs_inode_data *top = top_data_get(SDCARDFS_I(inode));
556
557 if (IS_ERR(mnt))
558 return PTR_ERR(mnt);
559 if (!top)
560 return -EINVAL;
561
562 /*
563 * Permission check on sdcardfs inode.
564 * Calling process should have AID_SDCARD_RW permission
565 * Since generic_permission only needs i_mode, i_uid,
566 * i_gid, and i_sb, we can create a fake inode to pass
567 * this information down in.
568 *
569 * The underlying code may attempt to take locks in some
570 * cases for features we're not using, but if that changes,
571 * locks must be dealt with to avoid undefined behavior.
572 */
573 copy_attrs(&tmp, inode);
574 tmp.i_uid = make_kuid(&init_user_ns, top->d_uid);
575 tmp.i_gid = make_kgid(&init_user_ns, get_gid(mnt, inode->i_sb, top));
576 tmp.i_mode = (inode->i_mode & S_IFMT)
577 | get_mode(mnt, SDCARDFS_I(inode), top);
578 data_put(top);
579 tmp.i_sb = inode->i_sb;
580 if (IS_POSIXACL(inode))
581 pr_warn("%s: This may be undefined behavior...\n", __func__);
582 err = generic_permission(&tmp, mask);
583 return err;
584 }
585
sdcardfs_setattr_wrn(struct dentry * dentry,struct iattr * ia)586 static int sdcardfs_setattr_wrn(struct dentry *dentry, struct iattr *ia)
587 {
588 WARN_RATELIMIT(1, "sdcardfs does not support setattr. User setattr2.\n");
589 return -EINVAL;
590 }
591
sdcardfs_setattr(struct vfsmount * mnt,struct dentry * dentry,struct iattr * ia)592 static int sdcardfs_setattr(struct vfsmount *mnt, struct dentry *dentry, struct iattr *ia)
593 {
594 int err;
595 struct dentry *lower_dentry;
596 struct vfsmount *lower_mnt;
597 struct inode *inode;
598 struct inode *lower_inode;
599 struct path lower_path;
600 struct iattr lower_ia;
601 struct dentry *parent;
602 struct inode tmp;
603 struct sdcardfs_inode_data *top;
604 const struct cred *saved_cred = NULL;
605
606 inode = d_inode(dentry);
607 top = top_data_get(SDCARDFS_I(inode));
608
609 if (!top)
610 return -EINVAL;
611
612 /*
613 * Permission check on sdcardfs inode.
614 * Calling process should have AID_SDCARD_RW permission
615 * Since generic_permission only needs i_mode, i_uid,
616 * i_gid, and i_sb, we can create a fake inode to pass
617 * this information down in.
618 *
619 * The underlying code may attempt to take locks in some
620 * cases for features we're not using, but if that changes,
621 * locks must be dealt with to avoid undefined behavior.
622 *
623 */
624 copy_attrs(&tmp, inode);
625 tmp.i_uid = make_kuid(&init_user_ns, top->d_uid);
626 tmp.i_gid = make_kgid(&init_user_ns, get_gid(mnt, dentry->d_sb, top));
627 tmp.i_mode = (inode->i_mode & S_IFMT)
628 | get_mode(mnt, SDCARDFS_I(inode), top);
629 tmp.i_size = i_size_read(inode);
630 data_put(top);
631 tmp.i_sb = inode->i_sb;
632
633 /*
634 * Check if user has permission to change inode. We don't check if
635 * this user can change the lower inode: that should happen when
636 * calling notify_change on the lower inode.
637 */
638 /* prepare our own lower struct iattr (with the lower file) */
639 memcpy(&lower_ia, ia, sizeof(lower_ia));
640 /* Allow touch updating timestamps. A previous permission check ensures
641 * we have write access. Changes to mode, owner, and group are ignored
642 */
643 ia->ia_valid |= ATTR_FORCE;
644 err = inode_change_ok(&tmp, ia);
645
646 if (!err) {
647 /* check the Android group ID */
648 parent = dget_parent(dentry);
649 if (!check_caller_access_to_name(d_inode(parent), &dentry->d_name))
650 err = -EACCES;
651 dput(parent);
652 }
653
654 if (err)
655 goto out_err;
656
657 /* save current_cred and override it */
658 saved_cred = override_fsids(SDCARDFS_SB(dentry->d_sb),
659 SDCARDFS_I(inode)->data);
660 if (!saved_cred)
661 return -ENOMEM;
662
663 sdcardfs_get_lower_path(dentry, &lower_path);
664 lower_dentry = lower_path.dentry;
665 lower_mnt = lower_path.mnt;
666 lower_inode = sdcardfs_lower_inode(inode);
667
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 (ia->ia_valid & ATTR_SIZE) {
682 err = inode_newsize_ok(&tmp, ia->ia_size);
683 if (err) {
684 goto out;
685 }
686 truncate_setsize(inode, ia->ia_size);
687 }
688
689 /*
690 * mode change is for clearing setuid/setgid bits. Allow lower fs
691 * to interpret this in its own way.
692 */
693 if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
694 lower_ia.ia_valid &= ~ATTR_MODE;
695
696 /* notify the (possibly copied-up) lower inode */
697 /*
698 * Note: we use d_inode(lower_dentry), because lower_inode may be
699 * unlinked (no inode->i_sb and i_ino==0. This happens if someone
700 * tries to open(), unlink(), then ftruncate() a file.
701 */
702 mutex_lock(&d_inode(lower_dentry)->i_mutex);
703 err = notify_change2(lower_mnt, lower_dentry, &lower_ia, /* note: lower_ia */
704 NULL);
705 mutex_unlock(&d_inode(lower_dentry)->i_mutex);
706 if (err)
707 goto out;
708
709 /* get attributes from the lower inode and update derived permissions */
710 sdcardfs_copy_and_fix_attrs(inode, lower_inode);
711
712 /*
713 * Not running fsstack_copy_inode_size(inode, lower_inode), because
714 * VFS should update our inode size, and notify_change on
715 * lower_inode should update its size.
716 */
717
718 out:
719 sdcardfs_put_lower_path(dentry, &lower_path);
720 revert_fsids(saved_cred);
721 out_err:
722 return err;
723 }
724
sdcardfs_fillattr(struct vfsmount * mnt,struct inode * inode,struct kstat * lower_stat,struct kstat * stat)725 static int sdcardfs_fillattr(struct vfsmount *mnt, struct inode *inode,
726 struct kstat *lower_stat, struct kstat *stat)
727 {
728 struct sdcardfs_inode_info *info = SDCARDFS_I(inode);
729 struct sdcardfs_inode_data *top = top_data_get(info);
730 struct super_block *sb = inode->i_sb;
731
732 if (!top)
733 return -EINVAL;
734
735 stat->dev = inode->i_sb->s_dev;
736 stat->ino = inode->i_ino;
737 stat->mode = (inode->i_mode & S_IFMT) | get_mode(mnt, info, top);
738 stat->nlink = inode->i_nlink;
739 stat->uid = make_kuid(&init_user_ns, top->d_uid);
740 stat->gid = make_kgid(&init_user_ns, get_gid(mnt, sb, top));
741 stat->rdev = inode->i_rdev;
742 stat->size = lower_stat->size;
743 stat->atime = lower_stat->atime;
744 stat->mtime = lower_stat->mtime;
745 stat->ctime = lower_stat->ctime;
746 stat->blksize = lower_stat->blksize;
747 stat->blocks = lower_stat->blocks;
748 data_put(top);
749 return 0;
750 }
751
sdcardfs_getattr(struct vfsmount * mnt,struct dentry * dentry,struct kstat * stat)752 static int sdcardfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
753 struct kstat *stat)
754 {
755 struct kstat lower_stat;
756 struct path lower_path;
757 struct dentry *parent;
758 int err;
759
760 parent = dget_parent(dentry);
761 if (!check_caller_access_to_name(d_inode(parent), &dentry->d_name)) {
762 dput(parent);
763 return -EACCES;
764 }
765 dput(parent);
766
767 sdcardfs_get_lower_path(dentry, &lower_path);
768 err = vfs_getattr(&lower_path, &lower_stat);
769 if (err)
770 goto out;
771 sdcardfs_copy_and_fix_attrs(d_inode(dentry),
772 d_inode(lower_path.dentry));
773 err = sdcardfs_fillattr(mnt, d_inode(dentry), &lower_stat, stat);
774 out:
775 sdcardfs_put_lower_path(dentry, &lower_path);
776 return err;
777 }
778
779 const struct inode_operations sdcardfs_symlink_iops = {
780 .permission2 = sdcardfs_permission,
781 .setattr2 = sdcardfs_setattr,
782 /* XXX Following operations are implemented,
783 * but FUSE(sdcard) or FAT does not support them
784 * These methods are *NOT* perfectly tested.
785 .readlink = sdcardfs_readlink,
786 .follow_link = sdcardfs_follow_link,
787 .put_link = kfree_put_link,
788 */
789 };
790
791 const struct inode_operations sdcardfs_dir_iops = {
792 .create = sdcardfs_create,
793 .lookup = sdcardfs_lookup,
794 .permission = sdcardfs_permission_wrn,
795 .permission2 = sdcardfs_permission,
796 .unlink = sdcardfs_unlink,
797 .mkdir = sdcardfs_mkdir,
798 .rmdir = sdcardfs_rmdir,
799 .rename = sdcardfs_rename,
800 .setattr = sdcardfs_setattr_wrn,
801 .setattr2 = sdcardfs_setattr,
802 .getattr = sdcardfs_getattr,
803 };
804
805 const struct inode_operations sdcardfs_main_iops = {
806 .permission = sdcardfs_permission_wrn,
807 .permission2 = sdcardfs_permission,
808 .setattr = sdcardfs_setattr_wrn,
809 .setattr2 = sdcardfs_setattr,
810 .getattr = sdcardfs_getattr,
811 };
812