blob: 2d94870c09ba705d0c5de7280a0128b081735eb2 [file] [log] [blame]
Daniel Campello9fbe8662015-07-20 16:23:50 -07001/*
2 * fs/sdcardfs/lookup.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/delay.h"
23
24/* The dentry cache is just so we have properly sized dentries */
25static struct kmem_cache *sdcardfs_dentry_cachep;
26
27int sdcardfs_init_dentry_cache(void)
28{
29 sdcardfs_dentry_cachep =
30 kmem_cache_create("sdcardfs_dentry",
31 sizeof(struct sdcardfs_dentry_info),
32 0, SLAB_RECLAIM_ACCOUNT, NULL);
33
34 return sdcardfs_dentry_cachep ? 0 : -ENOMEM;
35}
36
37void sdcardfs_destroy_dentry_cache(void)
38{
39 if (sdcardfs_dentry_cachep)
40 kmem_cache_destroy(sdcardfs_dentry_cachep);
41}
42
43void free_dentry_private_data(struct dentry *dentry)
44{
45 if (!dentry || !dentry->d_fsdata)
46 return;
47 kmem_cache_free(sdcardfs_dentry_cachep, dentry->d_fsdata);
48 dentry->d_fsdata = NULL;
49}
50
51/* allocate new dentry private data */
52int new_dentry_private_data(struct dentry *dentry)
53{
54 struct sdcardfs_dentry_info *info = SDCARDFS_D(dentry);
55
56 /* use zalloc to init dentry_info.lower_path */
57 info = kmem_cache_zalloc(sdcardfs_dentry_cachep, GFP_ATOMIC);
58 if (!info)
59 return -ENOMEM;
60
61 spin_lock_init(&info->lock);
62 dentry->d_fsdata = info;
63
64 return 0;
65}
66
Daniel Rosenberg44f555a2016-02-03 21:08:21 -080067struct inode_data {
68 struct inode *lower_inode;
69 userid_t id;
70};
71
72static int sdcardfs_inode_test(struct inode *inode, void *candidate_data/*void *candidate_lower_inode*/)
Daniel Campello9fbe8662015-07-20 16:23:50 -070073{
74 struct inode *current_lower_inode = sdcardfs_lower_inode(inode);
Daniel Rosenberg44f555a2016-02-03 21:08:21 -080075 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)
Daniel Campello9fbe8662015-07-20 16:23:50 -070078 return 1; /* found a match */
79 else
80 return 0; /* no match */
81}
82
83static int sdcardfs_inode_set(struct inode *inode, void *lower_inode)
84{
85 /* we do actual inode initialization in sdcardfs_iget */
86 return 0;
87}
88
Daniel Rosenberg44f555a2016-02-03 21:08:21 -080089struct inode *sdcardfs_iget(struct super_block *sb, struct inode *lower_inode, userid_t id)
Daniel Campello9fbe8662015-07-20 16:23:50 -070090{
91 struct sdcardfs_inode_info *info;
Daniel Rosenberg44f555a2016-02-03 21:08:21 -080092 struct inode_data data;
Daniel Campello9fbe8662015-07-20 16:23:50 -070093 struct inode *inode; /* the new inode to return */
94 int err;
95
Daniel Rosenberg44f555a2016-02-03 21:08:21 -080096 data.id = id;
97 data.lower_inode = lower_inode;
Daniel Campello9fbe8662015-07-20 16:23:50 -070098 inode = iget5_locked(sb, /* our superblock */
99 /*
100 * hashval: we use inode number, but we can
101 * also use "(unsigned long)lower_inode"
102 * instead.
103 */
104 lower_inode->i_ino, /* hashval */
105 sdcardfs_inode_test, /* inode comparison function */
106 sdcardfs_inode_set, /* inode init function */
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800107 &data); /* data passed to test+set fxns */
Daniel Campello9fbe8662015-07-20 16:23:50 -0700108 if (!inode) {
109 err = -EACCES;
110 iput(lower_inode);
111 return ERR_PTR(err);
112 }
113 /* if found a cached inode, then just return it */
114 if (!(inode->i_state & I_NEW))
115 return inode;
116
117 /* initialize new inode */
118 info = SDCARDFS_I(inode);
119
120 inode->i_ino = lower_inode->i_ino;
121 if (!igrab(lower_inode)) {
122 err = -ESTALE;
123 return ERR_PTR(err);
124 }
125 sdcardfs_set_lower_inode(inode, lower_inode);
126
127 inode->i_version++;
128
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;
134 else
135 inode->i_op = &sdcardfs_main_iops;
136
137 /* use different set of file ops for directories */
138 if (S_ISDIR(lower_inode->i_mode))
139 inode->i_fop = &sdcardfs_dir_fops;
140 else
141 inode->i_fop = &sdcardfs_main_fops;
142
143 inode->i_mapping->a_ops = &sdcardfs_aops;
144
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;
151
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);
157
158 /* all well, copy inode attributes */
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800159 sdcardfs_copy_and_fix_attrs(inode, lower_inode);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700160 fsstack_copy_inode_size(inode, lower_inode);
161
Daniel Campello9fbe8662015-07-20 16:23:50 -0700162 unlock_new_inode(inode);
163 return inode;
164}
165
166/*
167 * Connect a sdcardfs inode dentry/inode with several lower ones. This is
168 * the classic stackable file system "vnode interposition" action.
169 *
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)
173 */
174int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb,
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800175 struct path *lower_path, userid_t id)
Daniel Campello9fbe8662015-07-20 16:23:50 -0700176{
177 int err = 0;
178 struct inode *inode;
179 struct inode *lower_inode;
180 struct super_block *lower_sb;
181
182 lower_inode = lower_path->dentry->d_inode;
183 lower_sb = sdcardfs_lower_super(sb);
184
185 /* check that the lower file system didn't cross a mount point */
186 if (lower_inode->i_sb != lower_sb) {
187 err = -EXDEV;
188 goto out;
189 }
190
191 /*
192 * We allocate our new inode below by calling sdcardfs_iget,
193 * which will initialize some of the new inode's fields
194 */
195
196 /* inherit lower inode number for sdcardfs's inode */
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800197 inode = sdcardfs_iget(sb, lower_inode, id);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700198 if (IS_ERR(inode)) {
199 err = PTR_ERR(inode);
200 goto out;
201 }
202
203 d_add(dentry, inode);
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800204 update_derived_permission_lock(dentry);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700205out:
206 return err;
207}
208
209/*
210 * Main driver function for sdcardfs's lookup.
211 *
212 * Returns: NULL (ok), ERR_PTR if an error occurred.
213 * Fills in lower_parent_path with <dentry,mnt> on success.
214 */
215static struct dentry *__sdcardfs_lookup(struct dentry *dentry,
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800216 unsigned int flags, struct path *lower_parent_path, userid_t id)
Daniel Campello9fbe8662015-07-20 16:23:50 -0700217{
218 int err = 0;
219 struct vfsmount *lower_dir_mnt;
220 struct dentry *lower_dir_dentry = NULL;
221 struct dentry *lower_dentry;
222 const char *name;
Daniel Campello9fbe8662015-07-20 16:23:50 -0700223 struct path lower_path;
224 struct qstr this;
225 struct sdcardfs_sb_info *sbi;
226
227 sbi = SDCARDFS_SB(dentry->d_sb);
228 /* must initialize dentry operations */
229 d_set_d_op(dentry, &sdcardfs_ci_dops);
230
231 if (IS_ROOT(dentry))
232 goto out;
233
234 name = dentry->d_name.name;
235
236 /* now start the actual lookup procedure */
237 lower_dir_dentry = lower_parent_path->dentry;
238 lower_dir_mnt = lower_parent_path->mnt;
239
240 /* Use vfs_path_lookup to check if the dentry exists or not */
Daniel Rosenberg0e7e0262016-03-23 16:39:30 -0700241 err = vfs_path_lookup(lower_dir_dentry, lower_dir_mnt, name, 0,
Daniel Campelloe84c5252015-07-20 16:27:37 -0700242 &lower_path);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700243
244 /* no error: handle positive dentries */
245 if (!err) {
246 /* check if the dentry is an obb dentry
247 * if true, the lower_inode must be replaced with
248 * the inode of the graft path */
249
250 if(need_graft_path(dentry)) {
251
252 /* setup_obb_dentry()
253 * The lower_path will be stored to the dentry's orig_path
254 * and the base obbpath will be copyed to the lower_path variable.
255 * if an error returned, there's no change in the lower_path
256 * returns: -ERRNO if error (0: no error) */
Daniel Campelloe84c5252015-07-20 16:27:37 -0700257 err = setup_obb_dentry(dentry, &lower_path);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700258
259 if(err) {
260 /* if the sbi->obbpath is not available, we can optionally
261 * setup the lower_path with its orig_path.
262 * but, the current implementation just returns an error
263 * because the sdcard daemon also regards this case as
264 * a lookup fail. */
265 printk(KERN_INFO "sdcardfs: base obbpath is not available\n");
266 sdcardfs_put_reset_orig_path(dentry);
267 goto out;
268 }
269 }
270
Daniel Campelloe84c5252015-07-20 16:27:37 -0700271 sdcardfs_set_lower_path(dentry, &lower_path);
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800272 err = sdcardfs_interpose(dentry, dentry->d_sb, &lower_path, id);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700273 if (err) /* path_put underlying path on error */
274 sdcardfs_put_reset_lower_path(dentry);
275 goto out;
276 }
277
278 /*
279 * We don't consider ENOENT an error, and we want to return a
280 * negative dentry.
281 */
282 if (err && err != -ENOENT)
283 goto out;
284
285 /* instatiate a new negative dentry */
286 this.name = name;
287 this.len = strlen(name);
Amit Pundir31201382016-08-04 21:04:31 +0530288 this.hash = full_name_hash(dentry, this.name, this.len);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700289 lower_dentry = d_lookup(lower_dir_dentry, &this);
290 if (lower_dentry)
291 goto setup_lower;
292
293 lower_dentry = d_alloc(lower_dir_dentry, &this);
294 if (!lower_dentry) {
295 err = -ENOMEM;
296 goto out;
297 }
298 d_add(lower_dentry, NULL); /* instantiate and hash */
299
300setup_lower:
301 lower_path.dentry = lower_dentry;
302 lower_path.mnt = mntget(lower_dir_mnt);
303 sdcardfs_set_lower_path(dentry, &lower_path);
304
305 /*
306 * If the intent is to create a file, then don't return an error, so
307 * the VFS will continue the process of making this negative dentry
308 * into a positive one.
309 */
Daniel Campelloe84c5252015-07-20 16:27:37 -0700310 if (flags & (LOOKUP_CREATE|LOOKUP_RENAME_TARGET))
Daniel Campello9fbe8662015-07-20 16:23:50 -0700311 err = 0;
312
313out:
314 return ERR_PTR(err);
315}
316
317/*
318 * On success:
319 * fills dentry object appropriate values and returns NULL.
320 * On fail (== error)
321 * returns error ptr
322 *
Amit Pundir0fdd9d42016-06-01 21:53:20 +0530323 * @dir : Parent inode.
Daniel Campello9fbe8662015-07-20 16:23:50 -0700324 * @dentry : Target dentry to lookup. we should set each of fields.
325 * (dentry->d_name is initialized already)
326 * @nd : nameidata of parent inode
327 */
328struct dentry *sdcardfs_lookup(struct inode *dir, struct dentry *dentry,
Daniel Campelloe84c5252015-07-20 16:27:37 -0700329 unsigned int flags)
Daniel Campello9fbe8662015-07-20 16:23:50 -0700330{
331 struct dentry *ret = NULL, *parent;
332 struct path lower_parent_path;
333 int err = 0;
Daniel Campello9fbe8662015-07-20 16:23:50 -0700334 const struct cred *saved_cred = NULL;
335
336 parent = dget_parent(dentry);
337
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800338 if(!check_caller_access_to_name(parent->d_inode, dentry->d_name.name)) {
Daniel Campello9fbe8662015-07-20 16:23:50 -0700339 ret = ERR_PTR(-EACCES);
340 printk(KERN_INFO "%s: need to check the caller's gid in packages.list\n"
341 " dentry: %s, task:%s\n",
342 __func__, dentry->d_name.name, current->comm);
343 goto out_err;
344 }
345
346 /* save current_cred and override it */
347 OVERRIDE_CRED_PTR(SDCARDFS_SB(dir->i_sb), saved_cred);
348
349 sdcardfs_get_lower_path(parent, &lower_parent_path);
350
351 /* allocate dentry private data. We free it in ->d_release */
352 err = new_dentry_private_data(dentry);
353 if (err) {
354 ret = ERR_PTR(err);
355 goto out;
356 }
357
Daniel Rosenberg44f555a2016-02-03 21:08:21 -0800358 ret = __sdcardfs_lookup(dentry, flags, &lower_parent_path, SDCARDFS_I(dir)->userid);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700359 if (IS_ERR(ret))
360 {
361 goto out;
362 }
363 if (ret)
364 dentry = ret;
365 if (dentry->d_inode) {
366 fsstack_copy_attr_times(dentry->d_inode,
367 sdcardfs_lower_inode(dentry->d_inode));
368 /* get drived permission */
Amit Pundir0fdd9d42016-06-01 21:53:20 +0530369 inode_lock(dentry->d_inode);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700370 get_derived_permission(parent, dentry);
371 fix_derived_permission(dentry->d_inode);
Amit Pundir0fdd9d42016-06-01 21:53:20 +0530372 inode_unlock(dentry->d_inode);
Daniel Campello9fbe8662015-07-20 16:23:50 -0700373 }
374 /* update parent directory's atime */
375 fsstack_copy_attr_atime(parent->d_inode,
376 sdcardfs_lower_inode(parent->d_inode));
377
378out:
379 sdcardfs_put_lower_path(parent, &lower_parent_path);
380 REVERT_CRED(saved_cred);
381out_err:
382 dput(parent);
383 return ret;
384}