Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 1 | /* |
| 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 */ |
| 25 | static struct kmem_cache *sdcardfs_dentry_cachep; |
| 26 | |
| 27 | int 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 | |
| 37 | void sdcardfs_destroy_dentry_cache(void) |
| 38 | { |
Daniel Rosenberg | b6704a8 | 2017-03-21 16:29:13 -0700 | [diff] [blame] | 39 | kmem_cache_destroy(sdcardfs_dentry_cachep); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void free_dentry_private_data(struct dentry *dentry) |
| 43 | { |
| 44 | if (!dentry || !dentry->d_fsdata) |
| 45 | return; |
| 46 | kmem_cache_free(sdcardfs_dentry_cachep, dentry->d_fsdata); |
| 47 | dentry->d_fsdata = NULL; |
| 48 | } |
| 49 | |
| 50 | /* allocate new dentry private data */ |
| 51 | int new_dentry_private_data(struct dentry *dentry) |
| 52 | { |
| 53 | struct sdcardfs_dentry_info *info = SDCARDFS_D(dentry); |
| 54 | |
| 55 | /* use zalloc to init dentry_info.lower_path */ |
| 56 | info = kmem_cache_zalloc(sdcardfs_dentry_cachep, GFP_ATOMIC); |
| 57 | if (!info) |
| 58 | return -ENOMEM; |
| 59 | |
| 60 | spin_lock_init(&info->lock); |
| 61 | dentry->d_fsdata = info; |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 66 | struct inode_data { |
| 67 | struct inode *lower_inode; |
| 68 | userid_t id; |
| 69 | }; |
| 70 | |
| 71 | static int sdcardfs_inode_test(struct inode *inode, void *candidate_data/*void *candidate_lower_inode*/) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 72 | { |
| 73 | struct inode *current_lower_inode = sdcardfs_lower_inode(inode); |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 74 | userid_t current_userid = SDCARDFS_I(inode)->userid; |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 75 | |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 76 | if (current_lower_inode == ((struct inode_data *)candidate_data)->lower_inode && |
| 77 | current_userid == ((struct inode_data *)candidate_data)->id) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 78 | return 1; /* found a match */ |
| 79 | else |
| 80 | return 0; /* no match */ |
| 81 | } |
| 82 | |
| 83 | static 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 Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 89 | struct inode *sdcardfs_iget(struct super_block *sb, struct inode *lower_inode, userid_t id) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 90 | { |
| 91 | struct sdcardfs_inode_info *info; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 92 | struct inode_data data; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 93 | struct inode *inode; /* the new inode to return */ |
Daniel Rosenberg | 480cd3e | 2017-04-18 12:45:48 -0700 | [diff] [blame] | 94 | |
| 95 | if (!igrab(lower_inode)) |
| 96 | return ERR_PTR(-ESTALE); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 97 | |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 98 | data.id = id; |
| 99 | data.lower_inode = lower_inode; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 100 | inode = iget5_locked(sb, /* our superblock */ |
| 101 | /* |
| 102 | * hashval: we use inode number, but we can |
| 103 | * also use "(unsigned long)lower_inode" |
| 104 | * instead. |
| 105 | */ |
| 106 | lower_inode->i_ino, /* hashval */ |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 107 | sdcardfs_inode_test, /* inode comparison function */ |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 108 | sdcardfs_inode_set, /* inode init function */ |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 109 | &data); /* data passed to test+set fxns */ |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 110 | if (!inode) { |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 111 | iput(lower_inode); |
Daniel Rosenberg | 480cd3e | 2017-04-18 12:45:48 -0700 | [diff] [blame] | 112 | return ERR_PTR(-ENOMEM); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 113 | } |
Daniel Rosenberg | 480cd3e | 2017-04-18 12:45:48 -0700 | [diff] [blame] | 114 | /* if found a cached inode, then just return it (after iput) */ |
| 115 | if (!(inode->i_state & I_NEW)) { |
| 116 | iput(lower_inode); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 117 | return inode; |
Daniel Rosenberg | 480cd3e | 2017-04-18 12:45:48 -0700 | [diff] [blame] | 118 | } |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 119 | |
| 120 | /* initialize new inode */ |
| 121 | info = SDCARDFS_I(inode); |
| 122 | |
| 123 | inode->i_ino = lower_inode->i_ino; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 124 | sdcardfs_set_lower_inode(inode, lower_inode); |
| 125 | |
| 126 | inode->i_version++; |
| 127 | |
| 128 | /* use different set of inode ops for symlinks & directories */ |
| 129 | if (S_ISDIR(lower_inode->i_mode)) |
| 130 | inode->i_op = &sdcardfs_dir_iops; |
| 131 | else if (S_ISLNK(lower_inode->i_mode)) |
| 132 | inode->i_op = &sdcardfs_symlink_iops; |
| 133 | else |
| 134 | inode->i_op = &sdcardfs_main_iops; |
| 135 | |
| 136 | /* use different set of file ops for directories */ |
| 137 | if (S_ISDIR(lower_inode->i_mode)) |
| 138 | inode->i_fop = &sdcardfs_dir_fops; |
| 139 | else |
| 140 | inode->i_fop = &sdcardfs_main_fops; |
| 141 | |
| 142 | inode->i_mapping->a_ops = &sdcardfs_aops; |
| 143 | |
| 144 | inode->i_atime.tv_sec = 0; |
| 145 | inode->i_atime.tv_nsec = 0; |
| 146 | inode->i_mtime.tv_sec = 0; |
| 147 | inode->i_mtime.tv_nsec = 0; |
| 148 | inode->i_ctime.tv_sec = 0; |
| 149 | inode->i_ctime.tv_nsec = 0; |
| 150 | |
| 151 | /* properly initialize special inodes */ |
| 152 | if (S_ISBLK(lower_inode->i_mode) || S_ISCHR(lower_inode->i_mode) || |
| 153 | S_ISFIFO(lower_inode->i_mode) || S_ISSOCK(lower_inode->i_mode)) |
| 154 | init_special_inode(inode, lower_inode->i_mode, |
| 155 | lower_inode->i_rdev); |
| 156 | |
| 157 | /* all well, copy inode attributes */ |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 158 | sdcardfs_copy_and_fix_attrs(inode, lower_inode); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 159 | fsstack_copy_inode_size(inode, lower_inode); |
| 160 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 161 | unlock_new_inode(inode); |
| 162 | return inode; |
| 163 | } |
| 164 | |
| 165 | /* |
Daniel Rosenberg | d0497fe | 2017-03-09 22:11:08 -0800 | [diff] [blame] | 166 | * Helper interpose routine, called directly by ->lookup to handle |
| 167 | * spliced dentries. |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 168 | */ |
Daniel Rosenberg | d0497fe | 2017-03-09 22:11:08 -0800 | [diff] [blame] | 169 | static struct dentry *__sdcardfs_interpose(struct dentry *dentry, |
| 170 | struct super_block *sb, |
| 171 | struct path *lower_path, |
| 172 | userid_t id) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 173 | { |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 174 | struct inode *inode; |
| 175 | struct inode *lower_inode; |
| 176 | struct super_block *lower_sb; |
Daniel Rosenberg | d0497fe | 2017-03-09 22:11:08 -0800 | [diff] [blame] | 177 | struct dentry *ret_dentry; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 178 | |
Daniel Rosenberg | 63d2076 | 2016-12-01 14:36:29 -0800 | [diff] [blame] | 179 | lower_inode = d_inode(lower_path->dentry); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 180 | lower_sb = sdcardfs_lower_super(sb); |
| 181 | |
| 182 | /* check that the lower file system didn't cross a mount point */ |
| 183 | if (lower_inode->i_sb != lower_sb) { |
Daniel Rosenberg | d0497fe | 2017-03-09 22:11:08 -0800 | [diff] [blame] | 184 | ret_dentry = ERR_PTR(-EXDEV); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 185 | goto out; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * We allocate our new inode below by calling sdcardfs_iget, |
| 190 | * which will initialize some of the new inode's fields |
| 191 | */ |
| 192 | |
| 193 | /* inherit lower inode number for sdcardfs's inode */ |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 194 | inode = sdcardfs_iget(sb, lower_inode, id); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 195 | if (IS_ERR(inode)) { |
Daniel Rosenberg | d0497fe | 2017-03-09 22:11:08 -0800 | [diff] [blame] | 196 | ret_dentry = ERR_CAST(inode); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 197 | goto out; |
| 198 | } |
| 199 | |
Daniel Rosenberg | d0497fe | 2017-03-09 22:11:08 -0800 | [diff] [blame] | 200 | ret_dentry = d_splice_alias(inode, dentry); |
| 201 | dentry = ret_dentry ?: dentry; |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 202 | update_derived_permission_lock(dentry); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 203 | out: |
Daniel Rosenberg | d0497fe | 2017-03-09 22:11:08 -0800 | [diff] [blame] | 204 | return ret_dentry; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Connect an sdcardfs inode dentry/inode with several lower ones. This is |
| 209 | * the classic stackable file system "vnode interposition" action. |
| 210 | * |
| 211 | * @dentry: sdcardfs's dentry which interposes on lower one |
| 212 | * @sb: sdcardfs's super_block |
| 213 | * @lower_path: the lower path (caller does path_get/put) |
| 214 | */ |
| 215 | int sdcardfs_interpose(struct dentry *dentry, struct super_block *sb, |
| 216 | struct path *lower_path, userid_t id) |
| 217 | { |
| 218 | struct dentry *ret_dentry; |
| 219 | |
| 220 | ret_dentry = __sdcardfs_interpose(dentry, sb, lower_path, id); |
| 221 | return PTR_ERR(ret_dentry); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Daniel Rosenberg | fe0756d | 2017-03-01 17:04:41 -0800 | [diff] [blame] | 224 | struct sdcardfs_name_data { |
| 225 | struct dir_context ctx; |
| 226 | const struct qstr *to_find; |
| 227 | char *name; |
| 228 | bool found; |
| 229 | }; |
| 230 | |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 231 | static int sdcardfs_name_match(struct dir_context *ctx, const char *name, |
| 232 | int namelen, loff_t offset, u64 ino, unsigned int d_type) |
Daniel Rosenberg | fe0756d | 2017-03-01 17:04:41 -0800 | [diff] [blame] | 233 | { |
| 234 | struct sdcardfs_name_data *buf = container_of(ctx, struct sdcardfs_name_data, ctx); |
| 235 | struct qstr candidate = QSTR_INIT(name, namelen); |
| 236 | |
| 237 | if (qstr_case_eq(buf->to_find, &candidate)) { |
| 238 | memcpy(buf->name, name, namelen); |
| 239 | buf->name[namelen] = 0; |
| 240 | buf->found = true; |
| 241 | return 1; |
| 242 | } |
| 243 | return 0; |
| 244 | } |
| 245 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 246 | /* |
| 247 | * Main driver function for sdcardfs's lookup. |
| 248 | * |
| 249 | * Returns: NULL (ok), ERR_PTR if an error occurred. |
| 250 | * Fills in lower_parent_path with <dentry,mnt> on success. |
| 251 | */ |
| 252 | static struct dentry *__sdcardfs_lookup(struct dentry *dentry, |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 253 | unsigned int flags, struct path *lower_parent_path, userid_t id) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 254 | { |
| 255 | int err = 0; |
| 256 | struct vfsmount *lower_dir_mnt; |
| 257 | struct dentry *lower_dir_dentry = NULL; |
| 258 | struct dentry *lower_dentry; |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 259 | const struct qstr *name; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 260 | struct path lower_path; |
Daniel Rosenberg | 8e818ff | 2017-02-14 20:47:17 -0800 | [diff] [blame] | 261 | struct qstr dname; |
Daniel Rosenberg | d0497fe | 2017-03-09 22:11:08 -0800 | [diff] [blame] | 262 | struct dentry *ret_dentry = NULL; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 263 | struct sdcardfs_sb_info *sbi; |
| 264 | |
| 265 | sbi = SDCARDFS_SB(dentry->d_sb); |
| 266 | /* must initialize dentry operations */ |
| 267 | d_set_d_op(dentry, &sdcardfs_ci_dops); |
| 268 | |
| 269 | if (IS_ROOT(dentry)) |
| 270 | goto out; |
| 271 | |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 272 | name = &dentry->d_name; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 273 | |
| 274 | /* now start the actual lookup procedure */ |
| 275 | lower_dir_dentry = lower_parent_path->dentry; |
| 276 | lower_dir_mnt = lower_parent_path->mnt; |
| 277 | |
| 278 | /* Use vfs_path_lookup to check if the dentry exists or not */ |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 279 | err = vfs_path_lookup(lower_dir_dentry, lower_dir_mnt, name->name, 0, |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 280 | &lower_path); |
Daniel Rosenberg | 7c4dcd2 | 2016-04-27 15:31:29 -0700 | [diff] [blame] | 281 | /* check for other cases */ |
| 282 | if (err == -ENOENT) { |
Daniel Rosenberg | fe0756d | 2017-03-01 17:04:41 -0800 | [diff] [blame] | 283 | struct file *file; |
| 284 | const struct cred *cred = current_cred(); |
| 285 | |
| 286 | struct sdcardfs_name_data buffer = { |
| 287 | .ctx.actor = sdcardfs_name_match, |
| 288 | .to_find = name, |
| 289 | .name = __getname(), |
| 290 | .found = false, |
| 291 | }; |
| 292 | |
| 293 | if (!buffer.name) { |
| 294 | err = -ENOMEM; |
| 295 | goto out; |
Daniel Rosenberg | 7c4dcd2 | 2016-04-27 15:31:29 -0700 | [diff] [blame] | 296 | } |
Daniel Rosenberg | fe0756d | 2017-03-01 17:04:41 -0800 | [diff] [blame] | 297 | file = dentry_open(lower_parent_path, O_RDONLY, cred); |
| 298 | if (IS_ERR(file)) { |
| 299 | err = PTR_ERR(file); |
| 300 | goto put_name; |
| 301 | } |
| 302 | err = iterate_dir(file, &buffer.ctx); |
| 303 | fput(file); |
| 304 | if (err) |
| 305 | goto put_name; |
| 306 | |
| 307 | if (buffer.found) |
Daniel Rosenberg | 7c4dcd2 | 2016-04-27 15:31:29 -0700 | [diff] [blame] | 308 | err = vfs_path_lookup(lower_dir_dentry, |
| 309 | lower_dir_mnt, |
Daniel Rosenberg | fe0756d | 2017-03-01 17:04:41 -0800 | [diff] [blame] | 310 | buffer.name, 0, |
Daniel Rosenberg | 7c4dcd2 | 2016-04-27 15:31:29 -0700 | [diff] [blame] | 311 | &lower_path); |
Daniel Rosenberg | fe0756d | 2017-03-01 17:04:41 -0800 | [diff] [blame] | 312 | else |
| 313 | err = -ENOENT; |
| 314 | put_name: |
| 315 | __putname(buffer.name); |
Daniel Rosenberg | 7c4dcd2 | 2016-04-27 15:31:29 -0700 | [diff] [blame] | 316 | } |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 317 | |
| 318 | /* no error: handle positive dentries */ |
| 319 | if (!err) { |
| 320 | /* check if the dentry is an obb dentry |
| 321 | * if true, the lower_inode must be replaced with |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 322 | * the inode of the graft path |
| 323 | */ |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 324 | |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 325 | if (need_graft_path(dentry)) { |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 326 | |
| 327 | /* setup_obb_dentry() |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 328 | * The lower_path will be stored to the dentry's orig_path |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 329 | * and the base obbpath will be copyed to the lower_path variable. |
| 330 | * if an error returned, there's no change in the lower_path |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 331 | * returns: -ERRNO if error (0: no error) |
| 332 | */ |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 333 | err = setup_obb_dentry(dentry, &lower_path); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 334 | |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 335 | if (err) { |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 336 | /* if the sbi->obbpath is not available, we can optionally |
| 337 | * setup the lower_path with its orig_path. |
| 338 | * but, the current implementation just returns an error |
| 339 | * because the sdcard daemon also regards this case as |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 340 | * a lookup fail. |
| 341 | */ |
Daniel Rosenberg | 77ecf21 | 2017-03-16 17:46:13 -0700 | [diff] [blame] | 342 | pr_info("sdcardfs: base obbpath is not available\n"); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 343 | sdcardfs_put_reset_orig_path(dentry); |
| 344 | goto out; |
| 345 | } |
| 346 | } |
| 347 | |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 348 | sdcardfs_set_lower_path(dentry, &lower_path); |
Daniel Rosenberg | d0497fe | 2017-03-09 22:11:08 -0800 | [diff] [blame] | 349 | ret_dentry = |
| 350 | __sdcardfs_interpose(dentry, dentry->d_sb, &lower_path, id); |
| 351 | if (IS_ERR(ret_dentry)) { |
| 352 | err = PTR_ERR(ret_dentry); |
| 353 | /* path_put underlying path on error */ |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 354 | sdcardfs_put_reset_lower_path(dentry); |
Daniel Rosenberg | d0497fe | 2017-03-09 22:11:08 -0800 | [diff] [blame] | 355 | } |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 356 | goto out; |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * We don't consider ENOENT an error, and we want to return a |
| 361 | * negative dentry. |
| 362 | */ |
| 363 | if (err && err != -ENOENT) |
| 364 | goto out; |
| 365 | |
| 366 | /* instatiate a new negative dentry */ |
Daniel Rosenberg | 8e818ff | 2017-02-14 20:47:17 -0800 | [diff] [blame] | 367 | dname.name = name->name; |
| 368 | dname.len = name->len; |
| 369 | dname.hash = full_name_hash(lower_dir_dentry, dname.name, dname.len); |
| 370 | lower_dentry = d_lookup(lower_dir_dentry, &dname); |
Daniel Rosenberg | 9560fe6 | 2017-04-20 12:15:01 -0700 | [diff] [blame] | 371 | if (lower_dentry) |
| 372 | goto setup_lower; |
| 373 | |
| 374 | lower_dentry = d_alloc(lower_dir_dentry, &dname); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 375 | if (!lower_dentry) { |
Daniel Rosenberg | 9560fe6 | 2017-04-20 12:15:01 -0700 | [diff] [blame] | 376 | err = -ENOMEM; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 377 | goto out; |
| 378 | } |
Daniel Rosenberg | 9560fe6 | 2017-04-20 12:15:01 -0700 | [diff] [blame] | 379 | d_add(lower_dentry, NULL); /* instantiate and hash */ |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 380 | |
Daniel Rosenberg | 9560fe6 | 2017-04-20 12:15:01 -0700 | [diff] [blame] | 381 | setup_lower: |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 382 | lower_path.dentry = lower_dentry; |
| 383 | lower_path.mnt = mntget(lower_dir_mnt); |
| 384 | sdcardfs_set_lower_path(dentry, &lower_path); |
| 385 | |
| 386 | /* |
| 387 | * If the intent is to create a file, then don't return an error, so |
| 388 | * the VFS will continue the process of making this negative dentry |
| 389 | * into a positive one. |
| 390 | */ |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 391 | if (flags & (LOOKUP_CREATE|LOOKUP_RENAME_TARGET)) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 392 | err = 0; |
| 393 | |
| 394 | out: |
Daniel Rosenberg | d0497fe | 2017-03-09 22:11:08 -0800 | [diff] [blame] | 395 | if (err) |
| 396 | return ERR_PTR(err); |
| 397 | return ret_dentry; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /* |
| 401 | * On success: |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 402 | * fills dentry object appropriate values and returns NULL. |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 403 | * On fail (== error) |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 404 | * returns error ptr |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 405 | * |
Amit Pundir | b47e110 | 2016-06-01 21:53:20 +0530 | [diff] [blame] | 406 | * @dir : Parent inode. |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 407 | * @dentry : Target dentry to lookup. we should set each of fields. |
| 408 | * (dentry->d_name is initialized already) |
| 409 | * @nd : nameidata of parent inode |
| 410 | */ |
| 411 | struct dentry *sdcardfs_lookup(struct inode *dir, struct dentry *dentry, |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 412 | unsigned int flags) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 413 | { |
| 414 | struct dentry *ret = NULL, *parent; |
| 415 | struct path lower_parent_path; |
| 416 | int err = 0; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 417 | const struct cred *saved_cred = NULL; |
| 418 | |
| 419 | parent = dget_parent(dentry); |
| 420 | |
Daniel Rosenberg | 5004c5f | 2017-01-31 20:07:51 -0800 | [diff] [blame] | 421 | if (!check_caller_access_to_name(d_inode(parent), &dentry->d_name)) { |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 422 | ret = ERR_PTR(-EACCES); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 423 | goto out_err; |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 424 | } |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 425 | |
| 426 | /* save current_cred and override it */ |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 427 | OVERRIDE_CRED_PTR(SDCARDFS_SB(dir->i_sb), saved_cred, SDCARDFS_I(dir)); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 428 | |
| 429 | sdcardfs_get_lower_path(parent, &lower_parent_path); |
| 430 | |
| 431 | /* allocate dentry private data. We free it in ->d_release */ |
| 432 | err = new_dentry_private_data(dentry); |
| 433 | if (err) { |
| 434 | ret = ERR_PTR(err); |
| 435 | goto out; |
| 436 | } |
| 437 | |
Daniel Rosenberg | 497ac90 | 2016-02-03 21:08:21 -0800 | [diff] [blame] | 438 | ret = __sdcardfs_lookup(dentry, flags, &lower_parent_path, SDCARDFS_I(dir)->userid); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 439 | if (IS_ERR(ret)) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 440 | goto out; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 441 | if (ret) |
| 442 | dentry = ret; |
Daniel Rosenberg | 63d2076 | 2016-12-01 14:36:29 -0800 | [diff] [blame] | 443 | if (d_inode(dentry)) { |
| 444 | fsstack_copy_attr_times(d_inode(dentry), |
| 445 | sdcardfs_lower_inode(d_inode(dentry))); |
Daniel Rosenberg | cb1b945 | 2016-08-16 15:19:26 -0700 | [diff] [blame] | 446 | /* get derived permission */ |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 447 | get_derived_permission(parent, dentry); |
Daniel Rosenberg | 9021927 | 2016-10-26 20:27:20 -0700 | [diff] [blame] | 448 | fixup_tmp_permissions(d_inode(dentry)); |
Daniel Rosenberg | ad90525 | 2017-01-25 13:48:45 -0800 | [diff] [blame] | 449 | fixup_lower_ownership(dentry, dentry->d_name.name); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 450 | } |
| 451 | /* update parent directory's atime */ |
Daniel Rosenberg | 63d2076 | 2016-12-01 14:36:29 -0800 | [diff] [blame] | 452 | fsstack_copy_attr_atime(d_inode(parent), |
| 453 | sdcardfs_lower_inode(d_inode(parent))); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 454 | |
| 455 | out: |
| 456 | sdcardfs_put_lower_path(parent, &lower_parent_path); |
| 457 | REVERT_CRED(saved_cred); |
| 458 | out_err: |
| 459 | dput(parent); |
| 460 | return ret; |
| 461 | } |