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