4 * Copyright (c) 2013 Samsung Electronics Co. Ltd
5 * Authors: Daeho Jeong, Woojoong Lee, Seunghwan Hyun,
6 * Sunghwan Yun, Sungjong Seo
8 * This program has been developed as a stackable file system based on
9 * the WrapFS which written by
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
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.
22 #include "linux/delay.h"
24 /* The dentry cache is just so we have properly sized dentries */
25 static struct kmem_cache *sdcardfs_dentry_cachep;
27 int sdcardfs_init_dentry_cache(void)
29 sdcardfs_dentry_cachep =
30 kmem_cache_create("sdcardfs_dentry",
31 sizeof(struct sdcardfs_dentry_info),
32 0, SLAB_RECLAIM_ACCOUNT, NULL);
34 return sdcardfs_dentry_cachep ? 0 : -ENOMEM;
37 void sdcardfs_destroy_dentry_cache(void)
39 if (sdcardfs_dentry_cachep)
40 kmem_cache_destroy(sdcardfs_dentry_cachep);
43 void free_dentry_private_data(struct dentry *dentry)
45 if (!dentry || !dentry->d_fsdata)
47 kmem_cache_free(sdcardfs_dentry_cachep, dentry->d_fsdata);
48 dentry->d_fsdata = NULL;
51 /* allocate new dentry private data */
52 int new_dentry_private_data(struct dentry *dentry)
54 struct sdcardfs_dentry_info *info = SDCARDFS_D(dentry);
56 /* use zalloc to init dentry_info.lower_path */
57 info = kmem_cache_zalloc(sdcardfs_dentry_cachep, GFP_ATOMIC);
61 spin_lock_init(&info->lock);
62 dentry->d_fsdata = info;
68 struct inode *lower_inode;
72 static int sdcardfs_inode_test(struct inode *inode, void *candidate_data/*void *candidate_lower_inode*/)
74 struct inode *current_lower_inode = sdcardfs_lower_inode(inode);
75 userid_t current_userid = SDCARDFS_I(inode)->userid;
76 if (current_lower_inode == ((struct inode_data *)candidate_data)->lower_inode &&
77 current_userid == ((struct inode_data *)candidate_data)->id)
78 return 1; /* found a match */
80 return 0; /* no match */
83 static int sdcardfs_inode_set(struct inode *inode, void *lower_inode)
85 /* we do actual inode initialization in sdcardfs_iget */
89 struct inode *sdcardfs_iget(struct super_block *sb, struct inode *lower_inode, userid_t id)
91 struct sdcardfs_inode_info *info;
92 struct inode_data data;
93 struct inode *inode; /* the new inode to return */
97 data.lower_inode = lower_inode;
98 inode = iget5_locked(sb, /* our superblock */
100 * hashval: we use inode number, but we can
101 * also use "(unsigned long)lower_inode"
104 lower_inode->i_ino, /* hashval */
105 sdcardfs_inode_test, /* inode comparison function */
106 sdcardfs_inode_set, /* inode init function */
107 &data); /* data passed to test+set fxns */
113 /* if found a cached inode, then just return it */
114 if (!(inode->i_state & I_NEW))
117 /* initialize new inode */
118 info = SDCARDFS_I(inode);
120 inode->i_ino = lower_inode->i_ino;
121 if (!igrab(lower_inode)) {
125 sdcardfs_set_lower_inode(inode, lower_inode);
129 /* use different set of inode ops for symlinks & directories */
130 if (S_ISDIR(lower_inode->i_mode))
131 inode->i_op = &sdcardfs_dir_iops;
132 else if (S_ISLNK(lower_inode->i_mode))
133 inode->i_op = &sdcardfs_symlink_iops;
135 inode->i_op = &sdcardfs_main_iops;
137 /* use different set of file ops for directories */
138 if (S_ISDIR(lower_inode->i_mode))
139 inode->i_fop = &sdcardfs_dir_fops;
141 inode->i_fop = &sdcardfs_main_fops;
143 inode->i_mapping->a_ops = &sdcardfs_aops;
145 inode->i_atime.tv_sec = 0;
146 inode->i_atime.tv_nsec = 0;
147 inode->i_mtime.tv_sec = 0;
148 inode->i_mtime.tv_nsec = 0;
149 inode->i_ctime.tv_sec = 0;
150 inode->i_ctime.tv_nsec = 0;
152 /* properly initialize special inodes */
153 if (S_ISBLK(lower_inode->i_mode) || S_ISCHR(lower_inode->i_mode) ||
154 S_ISFIFO(lower_inode->i_mode) || S_ISSOCK(lower_inode->i_mode))
155 init_special_inode(inode, lower_inode->i_mode,
156 lower_inode->i_rdev);
158 /* all well, copy inode attributes */
159 sdcardfs_copy_and_fix_attrs(inode, lower_inode);
160 fsstack_copy_inode_size(inode, lower_inode);
162 unlock_new_inode(inode);
167 * Connect a sdcardfs inode dentry/inode with several lower ones. This is
168 * the classic stackable file system "vnode interposition" action.
170 * @dentry: sdcardfs's dentry which interposes on lower one
171 * @sb: sdcardfs's super_block
172 * @lower_path: the lower path (caller does path_get/put)
174 int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb,
175 struct path *lower_path, userid_t id)
179 struct inode *lower_inode;
180 struct super_block *lower_sb;
182 lower_inode = d_inode(lower_path->dentry);
183 lower_sb = sdcardfs_lower_super(sb);
185 /* check that the lower file system didn't cross a mount point */
186 if (lower_inode->i_sb != lower_sb) {
192 * We allocate our new inode below by calling sdcardfs_iget,
193 * which will initialize some of the new inode's fields
196 /* inherit lower inode number for sdcardfs's inode */
197 inode = sdcardfs_iget(sb, lower_inode, id);
199 err = PTR_ERR(inode);
203 d_add(dentry, inode);
204 update_derived_permission_lock(dentry);
210 * Main driver function for sdcardfs's lookup.
212 * Returns: NULL (ok), ERR_PTR if an error occurred.
213 * Fills in lower_parent_path with <dentry,mnt> on success.
215 static struct dentry *__sdcardfs_lookup(struct dentry *dentry,
216 unsigned int flags, struct path *lower_parent_path, userid_t id)
219 struct vfsmount *lower_dir_mnt;
220 struct dentry *lower_dir_dentry = NULL;
221 struct dentry *lower_dentry;
222 const struct qstr *name;
223 struct path lower_path;
224 struct sdcardfs_sb_info *sbi;
226 sbi = SDCARDFS_SB(dentry->d_sb);
227 /* must initialize dentry operations */
228 d_set_d_op(dentry, &sdcardfs_ci_dops);
233 name = &dentry->d_name;
235 /* now start the actual lookup procedure */
236 lower_dir_dentry = lower_parent_path->dentry;
237 lower_dir_mnt = lower_parent_path->mnt;
239 /* Use vfs_path_lookup to check if the dentry exists or not */
240 err = vfs_path_lookup(lower_dir_dentry, lower_dir_mnt, name->name, 0,
242 /* check for other cases */
243 if (err == -ENOENT) {
244 struct dentry *child;
245 struct dentry *match = NULL;
246 mutex_lock(&d_inode(lower_dir_dentry)->i_mutex);
247 spin_lock(&lower_dir_dentry->d_lock);
248 list_for_each_entry(child, &lower_dir_dentry->d_subdirs, d_child) {
249 if (child && d_inode(child)) {
250 if (qstr_case_eq(&child->d_name, name)) {
256 spin_unlock(&lower_dir_dentry->d_lock);
257 mutex_unlock(&d_inode(lower_dir_dentry)->i_mutex);
259 err = vfs_path_lookup(lower_dir_dentry,
261 match->d_name.name, 0,
267 /* no error: handle positive dentries */
269 /* check if the dentry is an obb dentry
270 * if true, the lower_inode must be replaced with
271 * the inode of the graft path */
273 if(need_graft_path(dentry)) {
275 /* setup_obb_dentry()
276 * The lower_path will be stored to the dentry's orig_path
277 * and the base obbpath will be copyed to the lower_path variable.
278 * if an error returned, there's no change in the lower_path
279 * returns: -ERRNO if error (0: no error) */
280 err = setup_obb_dentry(dentry, &lower_path);
283 /* if the sbi->obbpath is not available, we can optionally
284 * setup the lower_path with its orig_path.
285 * but, the current implementation just returns an error
286 * because the sdcard daemon also regards this case as
288 printk(KERN_INFO "sdcardfs: base obbpath is not available\n");
289 sdcardfs_put_reset_orig_path(dentry);
294 sdcardfs_set_lower_path(dentry, &lower_path);
295 err = sdcardfs_interpose(dentry, dentry->d_sb, &lower_path, id);
296 if (err) /* path_put underlying path on error */
297 sdcardfs_put_reset_lower_path(dentry);
302 * We don't consider ENOENT an error, and we want to return a
305 if (err && err != -ENOENT)
308 /* instatiate a new negative dentry */
309 lower_dentry = d_lookup(lower_dir_dentry, name);
313 lower_dentry = d_alloc(lower_dir_dentry, name);
318 d_add(lower_dentry, NULL); /* instantiate and hash */
321 lower_path.dentry = lower_dentry;
322 lower_path.mnt = mntget(lower_dir_mnt);
323 sdcardfs_set_lower_path(dentry, &lower_path);
326 * If the intent is to create a file, then don't return an error, so
327 * the VFS will continue the process of making this negative dentry
328 * into a positive one.
330 if (flags & (LOOKUP_CREATE|LOOKUP_RENAME_TARGET))
339 * fills dentry object appropriate values and returns NULL.
343 * @dir : Parent inode. It is locked (dir->i_mutex)
344 * @dentry : Target dentry to lookup. we should set each of fields.
345 * (dentry->d_name is initialized already)
346 * @nd : nameidata of parent inode
348 struct dentry *sdcardfs_lookup(struct inode *dir, struct dentry *dentry,
351 struct dentry *ret = NULL, *parent;
352 struct path lower_parent_path;
354 const struct cred *saved_cred = NULL;
356 parent = dget_parent(dentry);
358 if(!check_caller_access_to_name(d_inode(parent), &dentry->d_name)) {
359 ret = ERR_PTR(-EACCES);
360 printk(KERN_INFO "%s: need to check the caller's gid in packages.list\n"
361 " dentry: %s, task:%s\n",
362 __func__, dentry->d_name.name, current->comm);
366 /* save current_cred and override it */
367 OVERRIDE_CRED_PTR(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir));
369 sdcardfs_get_lower_path(parent, &lower_parent_path);
371 /* allocate dentry private data. We free it in ->d_release */
372 err = new_dentry_private_data(dentry);
378 ret = __sdcardfs_lookup(dentry, flags, &lower_parent_path, SDCARDFS_I(dir)->userid);
385 if (d_inode(dentry)) {
386 fsstack_copy_attr_times(d_inode(dentry),
387 sdcardfs_lower_inode(d_inode(dentry)));
388 /* get derived permission */
389 get_derived_permission(parent, dentry);
390 fixup_tmp_permissions(d_inode(dentry));
391 fixup_lower_ownership(dentry, dentry->d_name.name);
393 /* update parent directory's atime */
394 fsstack_copy_attr_atime(d_inode(parent),
395 sdcardfs_lower_inode(d_inode(parent)));
398 sdcardfs_put_lower_path(parent, &lower_parent_path);
399 REVERT_CRED(saved_cred);