Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/sdcardfs/dentry.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/ctype.h" |
| 23 | |
| 24 | /* |
| 25 | * returns: -ERRNO if error (returned to user) |
| 26 | * 0: tell VFS to invalidate dentry |
| 27 | * 1: dentry is valid |
| 28 | */ |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 29 | static int sdcardfs_d_revalidate(struct dentry *dentry, unsigned int flags) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 30 | { |
| 31 | int err = 1; |
| 32 | struct path parent_lower_path, lower_path; |
| 33 | struct dentry *parent_dentry = NULL; |
| 34 | struct dentry *parent_lower_dentry = NULL; |
| 35 | struct dentry *lower_cur_parent_dentry = NULL; |
| 36 | struct dentry *lower_dentry = NULL; |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 37 | struct inode *inode; |
| 38 | struct sdcardfs_inode_data *data; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 39 | |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 40 | if (flags & LOOKUP_RCU) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 41 | return -ECHILD; |
| 42 | |
| 43 | spin_lock(&dentry->d_lock); |
| 44 | if (IS_ROOT(dentry)) { |
| 45 | spin_unlock(&dentry->d_lock); |
| 46 | return 1; |
| 47 | } |
| 48 | spin_unlock(&dentry->d_lock); |
| 49 | |
| 50 | /* check uninitialized obb_dentry and |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 51 | * whether the base obbpath has been changed or not |
| 52 | */ |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 53 | if (is_obbpath_invalid(dentry)) { |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | parent_dentry = dget_parent(dentry); |
| 58 | sdcardfs_get_lower_path(parent_dentry, &parent_lower_path); |
| 59 | sdcardfs_get_real_lower(dentry, &lower_path); |
| 60 | parent_lower_dentry = parent_lower_path.dentry; |
| 61 | lower_dentry = lower_path.dentry; |
| 62 | lower_cur_parent_dentry = dget_parent(lower_dentry); |
| 63 | |
Daniel Rosenberg | 6bf837d | 2017-04-24 19:49:02 -0700 | [diff] [blame] | 64 | if ((lower_dentry->d_flags & DCACHE_OP_REVALIDATE)) { |
| 65 | err = lower_dentry->d_op->d_revalidate(lower_dentry, flags); |
| 66 | if (err == 0) { |
Daniel Rosenberg | 6bf837d | 2017-04-24 19:49:02 -0700 | [diff] [blame] | 67 | goto out; |
| 68 | } |
| 69 | } |
| 70 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 71 | spin_lock(&lower_dentry->d_lock); |
| 72 | if (d_unhashed(lower_dentry)) { |
| 73 | spin_unlock(&lower_dentry->d_lock); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 74 | err = 0; |
| 75 | goto out; |
| 76 | } |
| 77 | spin_unlock(&lower_dentry->d_lock); |
| 78 | |
| 79 | if (parent_lower_dentry != lower_cur_parent_dentry) { |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 80 | err = 0; |
| 81 | goto out; |
| 82 | } |
| 83 | |
| 84 | if (dentry < lower_dentry) { |
| 85 | spin_lock(&dentry->d_lock); |
Daniel Rosenberg | dae31f8 | 2017-03-08 17:11:51 -0800 | [diff] [blame] | 86 | spin_lock_nested(&lower_dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 87 | } else { |
| 88 | spin_lock(&lower_dentry->d_lock); |
Daniel Rosenberg | dae31f8 | 2017-03-08 17:11:51 -0800 | [diff] [blame] | 89 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Daniel Rosenberg | 721274a | 2017-03-08 17:20:02 -0800 | [diff] [blame] | 92 | if (!qstr_case_eq(&dentry->d_name, &lower_dentry->d_name)) { |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 93 | err = 0; |
| 94 | } |
| 95 | |
| 96 | if (dentry < lower_dentry) { |
| 97 | spin_unlock(&lower_dentry->d_lock); |
| 98 | spin_unlock(&dentry->d_lock); |
| 99 | } else { |
| 100 | spin_unlock(&dentry->d_lock); |
| 101 | spin_unlock(&lower_dentry->d_lock); |
| 102 | } |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 103 | if (!err) |
| 104 | goto out; |
| 105 | |
| 106 | /* If our top's inode is gone, we may be out of date */ |
Daniel Rosenberg | db95048 | 2017-05-22 13:23:56 -0700 | [diff] [blame] | 107 | inode = igrab(d_inode(dentry)); |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 108 | if (inode) { |
| 109 | data = top_data_get(SDCARDFS_I(inode)); |
Daniel Rosenberg | db95048 | 2017-05-22 13:23:56 -0700 | [diff] [blame] | 110 | if (!data || data->abandoned) { |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 111 | err = 0; |
| 112 | } |
Daniel Rosenberg | db95048 | 2017-05-22 13:23:56 -0700 | [diff] [blame] | 113 | if (data) |
| 114 | data_put(data); |
| 115 | iput(inode); |
Daniel Rosenberg | a56a105 | 2017-05-15 14:03:15 -0700 | [diff] [blame] | 116 | } |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 117 | |
| 118 | out: |
| 119 | dput(parent_dentry); |
| 120 | dput(lower_cur_parent_dentry); |
| 121 | sdcardfs_put_lower_path(parent_dentry, &parent_lower_path); |
| 122 | sdcardfs_put_real_lower(dentry, &lower_path); |
| 123 | return err; |
| 124 | } |
| 125 | |
Daniel Rosenberg | 80af433 | 2018-07-06 16:24:27 -0700 | [diff] [blame] | 126 | /* 1 = delete, 0 = cache */ |
| 127 | static int sdcardfs_d_delete(const struct dentry *d) |
| 128 | { |
| 129 | return SDCARDFS_SB(d->d_sb)->options.nocache ? 1 : 0; |
| 130 | } |
| 131 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 132 | static void sdcardfs_d_release(struct dentry *dentry) |
| 133 | { |
Daniel Rosenberg | a083817 | 2018-04-11 16:19:10 -0700 | [diff] [blame] | 134 | if (!dentry || !dentry->d_fsdata) |
| 135 | return; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 136 | /* release and reset the lower paths */ |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 137 | if (has_graft_path(dentry)) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 138 | sdcardfs_put_reset_orig_path(dentry); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 139 | sdcardfs_put_reset_lower_path(dentry); |
| 140 | free_dentry_private_data(dentry); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static int sdcardfs_hash_ci(const struct dentry *dentry, |
Daniel Campello | d1d080c | 2015-07-20 16:27:37 -0700 | [diff] [blame] | 144 | struct qstr *qstr) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 145 | { |
| 146 | /* |
| 147 | * This function is copy of vfat_hashi. |
| 148 | * FIXME Should we support national language? |
| 149 | * Refer to vfat_hashi() |
| 150 | * struct nls_table *t = MSDOS_SB(dentry->d_sb)->nls_io; |
| 151 | */ |
| 152 | const unsigned char *name; |
| 153 | unsigned int len; |
| 154 | unsigned long hash; |
| 155 | |
| 156 | name = qstr->name; |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 157 | len = qstr->len; |
| 158 | |
Amit Pundir | 48bc6d3 | 2016-08-04 21:04:31 +0530 | [diff] [blame] | 159 | hash = init_name_hash(dentry); |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 160 | while (len--) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 161 | hash = partial_name_hash(tolower(*name++), hash); |
| 162 | qstr->hash = end_name_hash(hash); |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Case insensitive compare of two vfat names. |
| 169 | */ |
Amit Pundir | c9bae39 | 2016-08-08 12:27:33 +0530 | [diff] [blame] | 170 | static int sdcardfs_cmp_ci(const struct dentry *dentry, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 171 | unsigned int len, const char *str, const struct qstr *name) |
| 172 | { |
Daniel Rosenberg | d64126c | 2017-03-16 19:33:35 -0700 | [diff] [blame] | 173 | /* FIXME Should we support national language? */ |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 174 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 175 | if (name->len == len) { |
Daniel Rosenberg | 721274a | 2017-03-08 17:20:02 -0800 | [diff] [blame] | 176 | if (str_n_case_eq(name->name, str, len)) |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | return 1; |
| 180 | } |
| 181 | |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 182 | static void sdcardfs_canonical_path(const struct path *path, |
| 183 | struct path *actual_path) |
| 184 | { |
Daniel Rosenberg | 973117c | 2016-04-22 00:00:14 -0700 | [diff] [blame] | 185 | sdcardfs_get_real_lower(path->dentry, actual_path); |
| 186 | } |
| 187 | |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 188 | const struct dentry_operations sdcardfs_ci_dops = { |
| 189 | .d_revalidate = sdcardfs_d_revalidate, |
Daniel Rosenberg | 80af433 | 2018-07-06 16:24:27 -0700 | [diff] [blame] | 190 | .d_delete = sdcardfs_d_delete, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 191 | .d_release = sdcardfs_d_release, |
Daniel Rosenberg | 5e024f6 | 2017-03-16 17:42:58 -0700 | [diff] [blame] | 192 | .d_hash = sdcardfs_hash_ci, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 193 | .d_compare = sdcardfs_cmp_ci, |
Daniel Rosenberg | 973117c | 2016-04-22 00:00:14 -0700 | [diff] [blame] | 194 | .d_canonical_path = sdcardfs_canonical_path, |
Daniel Campello | 35c9e24 | 2015-07-20 16:23:50 -0700 | [diff] [blame] | 195 | }; |
| 196 | |