Thomas Gleixner | 9c92ab6 | 2019-05-29 07:17:56 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 2 | /* fs/fat/nfs.c |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | #include <linux/exportfs.h> |
| 6 | #include "fat.h" |
| 7 | |
Namjae Jeon | ea3983a | 2013-04-29 16:21:11 -0700 | [diff] [blame] | 8 | struct fat_fid { |
| 9 | u32 i_gen; |
| 10 | u32 i_pos_low; |
| 11 | u16 i_pos_hi; |
| 12 | u16 parent_i_pos_hi; |
| 13 | u32 parent_i_pos_low; |
| 14 | u32 parent_i_gen; |
| 15 | }; |
| 16 | |
| 17 | #define FAT_FID_SIZE_WITHOUT_PARENT 3 |
| 18 | #define FAT_FID_SIZE_WITH_PARENT (sizeof(struct fat_fid)/sizeof(u32)) |
| 19 | |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 20 | /** |
| 21 | * Look up a directory inode given its starting cluster. |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 22 | */ |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 23 | static struct inode *fat_dget(struct super_block *sb, int i_logstart) |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 24 | { |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 25 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
| 26 | struct hlist_head *head; |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 27 | struct msdos_inode_info *i; |
| 28 | struct inode *inode = NULL; |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 29 | |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 30 | head = sbi->dir_hashtable + fat_dir_hash(i_logstart); |
| 31 | spin_lock(&sbi->dir_hash_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 32 | hlist_for_each_entry(i, head, i_dir_hash) { |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 33 | BUG_ON(i->vfs_inode.i_sb != sb); |
| 34 | if (i->i_logstart != i_logstart) |
| 35 | continue; |
| 36 | inode = igrab(&i->vfs_inode); |
| 37 | if (inode) |
| 38 | break; |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 39 | } |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 40 | spin_unlock(&sbi->dir_hash_lock); |
| 41 | return inode; |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Namjae Jeon | 8fceb4e | 2013-04-29 16:21:12 -0700 | [diff] [blame] | 44 | static struct inode *fat_ilookup(struct super_block *sb, u64 ino, loff_t i_pos) |
| 45 | { |
| 46 | if (MSDOS_SB(sb)->options.nfs == FAT_NFS_NOSTALE_RO) |
| 47 | return fat_iget(sb, i_pos); |
| 48 | |
| 49 | else { |
| 50 | if ((ino < MSDOS_ROOT_INO) || (ino == MSDOS_FSINFO_INO)) |
| 51 | return NULL; |
| 52 | return ilookup(sb, ino); |
| 53 | } |
| 54 | } |
| 55 | |
Namjae Jeon | ea3983a | 2013-04-29 16:21:11 -0700 | [diff] [blame] | 56 | static struct inode *__fat_nfs_get_inode(struct super_block *sb, |
| 57 | u64 ino, u32 generation, loff_t i_pos) |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 58 | { |
Namjae Jeon | 8fceb4e | 2013-04-29 16:21:12 -0700 | [diff] [blame] | 59 | struct inode *inode = fat_ilookup(sb, ino, i_pos); |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 60 | |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 61 | if (inode && generation && (inode->i_generation != generation)) { |
| 62 | iput(inode); |
| 63 | inode = NULL; |
| 64 | } |
Namjae Jeon | 8fceb4e | 2013-04-29 16:21:12 -0700 | [diff] [blame] | 65 | if (inode == NULL && MSDOS_SB(sb)->options.nfs == FAT_NFS_NOSTALE_RO) { |
| 66 | struct buffer_head *bh = NULL; |
| 67 | struct msdos_dir_entry *de ; |
| 68 | sector_t blocknr; |
| 69 | int offset; |
| 70 | fat_get_blknr_offset(MSDOS_SB(sb), i_pos, &blocknr, &offset); |
| 71 | bh = sb_bread(sb, blocknr); |
| 72 | if (!bh) { |
| 73 | fat_msg(sb, KERN_ERR, |
| 74 | "unable to read block(%llu) for building NFS inode", |
| 75 | (llu)blocknr); |
| 76 | return inode; |
| 77 | } |
| 78 | de = (struct msdos_dir_entry *)bh->b_data; |
| 79 | /* If a file is deleted on server and client is not updated |
| 80 | * yet, we must not build the inode upon a lookup call. |
| 81 | */ |
| 82 | if (IS_FREE(de[offset].name)) |
| 83 | inode = NULL; |
| 84 | else |
| 85 | inode = fat_build_inode(sb, &de[offset], i_pos); |
| 86 | brelse(bh); |
| 87 | } |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 88 | |
| 89 | return inode; |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Namjae Jeon | ea3983a | 2013-04-29 16:21:11 -0700 | [diff] [blame] | 92 | static struct inode *fat_nfs_get_inode(struct super_block *sb, |
| 93 | u64 ino, u32 generation) |
| 94 | { |
| 95 | |
| 96 | return __fat_nfs_get_inode(sb, ino, generation, 0); |
| 97 | } |
| 98 | |
| 99 | static int |
| 100 | fat_encode_fh_nostale(struct inode *inode, __u32 *fh, int *lenp, |
| 101 | struct inode *parent) |
| 102 | { |
| 103 | int len = *lenp; |
| 104 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); |
| 105 | struct fat_fid *fid = (struct fat_fid *) fh; |
| 106 | loff_t i_pos; |
| 107 | int type = FILEID_FAT_WITHOUT_PARENT; |
| 108 | |
| 109 | if (parent) { |
| 110 | if (len < FAT_FID_SIZE_WITH_PARENT) { |
| 111 | *lenp = FAT_FID_SIZE_WITH_PARENT; |
| 112 | return FILEID_INVALID; |
| 113 | } |
| 114 | } else { |
| 115 | if (len < FAT_FID_SIZE_WITHOUT_PARENT) { |
| 116 | *lenp = FAT_FID_SIZE_WITHOUT_PARENT; |
| 117 | return FILEID_INVALID; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | i_pos = fat_i_pos_read(sbi, inode); |
| 122 | *lenp = FAT_FID_SIZE_WITHOUT_PARENT; |
| 123 | fid->i_gen = inode->i_generation; |
| 124 | fid->i_pos_low = i_pos & 0xFFFFFFFF; |
| 125 | fid->i_pos_hi = (i_pos >> 32) & 0xFFFF; |
| 126 | if (parent) { |
| 127 | i_pos = fat_i_pos_read(sbi, parent); |
| 128 | fid->parent_i_pos_hi = (i_pos >> 32) & 0xFFFF; |
| 129 | fid->parent_i_pos_low = i_pos & 0xFFFFFFFF; |
| 130 | fid->parent_i_gen = parent->i_generation; |
| 131 | type = FILEID_FAT_WITH_PARENT; |
| 132 | *lenp = FAT_FID_SIZE_WITH_PARENT; |
| 133 | } |
| 134 | |
| 135 | return type; |
| 136 | } |
| 137 | |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 138 | /** |
| 139 | * Map a NFS file handle to a corresponding dentry. |
| 140 | * The dentry may or may not be connected to the filesystem root. |
| 141 | */ |
Namjae Jeon | ea3983a | 2013-04-29 16:21:11 -0700 | [diff] [blame] | 142 | static struct dentry *fat_fh_to_dentry(struct super_block *sb, struct fid *fid, |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 143 | int fh_len, int fh_type) |
| 144 | { |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 145 | return generic_fh_to_dentry(sb, fid, fh_len, fh_type, |
| 146 | fat_nfs_get_inode); |
| 147 | } |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 148 | |
Namjae Jeon | ea3983a | 2013-04-29 16:21:11 -0700 | [diff] [blame] | 149 | static struct dentry *fat_fh_to_dentry_nostale(struct super_block *sb, |
| 150 | struct fid *fh, int fh_len, |
| 151 | int fh_type) |
| 152 | { |
| 153 | struct inode *inode = NULL; |
| 154 | struct fat_fid *fid = (struct fat_fid *)fh; |
| 155 | loff_t i_pos; |
| 156 | |
| 157 | switch (fh_type) { |
| 158 | case FILEID_FAT_WITHOUT_PARENT: |
| 159 | if (fh_len < FAT_FID_SIZE_WITHOUT_PARENT) |
| 160 | return NULL; |
| 161 | break; |
| 162 | case FILEID_FAT_WITH_PARENT: |
| 163 | if (fh_len < FAT_FID_SIZE_WITH_PARENT) |
| 164 | return NULL; |
| 165 | break; |
| 166 | default: |
| 167 | return NULL; |
| 168 | } |
| 169 | i_pos = fid->i_pos_hi; |
| 170 | i_pos = (i_pos << 32) | (fid->i_pos_low); |
| 171 | inode = __fat_nfs_get_inode(sb, 0, fid->i_gen, i_pos); |
| 172 | |
| 173 | return d_obtain_alias(inode); |
| 174 | } |
| 175 | |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 176 | /* |
| 177 | * Find the parent for a file specified by NFS handle. |
| 178 | * This requires that the handle contain the i_ino of the parent. |
| 179 | */ |
Namjae Jeon | ea3983a | 2013-04-29 16:21:11 -0700 | [diff] [blame] | 180 | static struct dentry *fat_fh_to_parent(struct super_block *sb, struct fid *fid, |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 181 | int fh_len, int fh_type) |
| 182 | { |
| 183 | return generic_fh_to_parent(sb, fid, fh_len, fh_type, |
| 184 | fat_nfs_get_inode); |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Namjae Jeon | ea3983a | 2013-04-29 16:21:11 -0700 | [diff] [blame] | 187 | static struct dentry *fat_fh_to_parent_nostale(struct super_block *sb, |
| 188 | struct fid *fh, int fh_len, |
| 189 | int fh_type) |
| 190 | { |
| 191 | struct inode *inode = NULL; |
| 192 | struct fat_fid *fid = (struct fat_fid *)fh; |
| 193 | loff_t i_pos; |
| 194 | |
| 195 | if (fh_len < FAT_FID_SIZE_WITH_PARENT) |
| 196 | return NULL; |
| 197 | |
| 198 | switch (fh_type) { |
| 199 | case FILEID_FAT_WITH_PARENT: |
| 200 | i_pos = fid->parent_i_pos_hi; |
| 201 | i_pos = (i_pos << 32) | (fid->parent_i_pos_low); |
| 202 | inode = __fat_nfs_get_inode(sb, 0, fid->parent_i_gen, i_pos); |
| 203 | break; |
| 204 | } |
| 205 | |
| 206 | return d_obtain_alias(inode); |
| 207 | } |
| 208 | |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 209 | /* |
Namjae Jeon | f1e6fb0 | 2013-04-29 16:21:14 -0700 | [diff] [blame] | 210 | * Rebuild the parent for a directory that is not connected |
| 211 | * to the filesystem root |
| 212 | */ |
| 213 | static |
| 214 | struct inode *fat_rebuild_parent(struct super_block *sb, int parent_logstart) |
| 215 | { |
| 216 | int search_clus, clus_to_match; |
| 217 | struct msdos_dir_entry *de; |
| 218 | struct inode *parent = NULL; |
| 219 | struct inode *dummy_grand_parent = NULL; |
| 220 | struct fat_slot_info sinfo; |
| 221 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
| 222 | sector_t blknr = fat_clus_to_blknr(sbi, parent_logstart); |
| 223 | struct buffer_head *parent_bh = sb_bread(sb, blknr); |
| 224 | if (!parent_bh) { |
| 225 | fat_msg(sb, KERN_ERR, |
| 226 | "unable to read cluster of parent directory"); |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | de = (struct msdos_dir_entry *) parent_bh->b_data; |
| 231 | clus_to_match = fat_get_start(sbi, &de[0]); |
| 232 | search_clus = fat_get_start(sbi, &de[1]); |
| 233 | |
| 234 | dummy_grand_parent = fat_dget(sb, search_clus); |
| 235 | if (!dummy_grand_parent) { |
| 236 | dummy_grand_parent = new_inode(sb); |
| 237 | if (!dummy_grand_parent) { |
| 238 | brelse(parent_bh); |
| 239 | return parent; |
| 240 | } |
| 241 | |
| 242 | dummy_grand_parent->i_ino = iunique(sb, MSDOS_ROOT_INO); |
| 243 | fat_fill_inode(dummy_grand_parent, &de[1]); |
| 244 | MSDOS_I(dummy_grand_parent)->i_pos = -1; |
| 245 | } |
| 246 | |
| 247 | if (!fat_scan_logstart(dummy_grand_parent, clus_to_match, &sinfo)) |
| 248 | parent = fat_build_inode(sb, sinfo.de, sinfo.i_pos); |
| 249 | |
| 250 | brelse(parent_bh); |
| 251 | iput(dummy_grand_parent); |
| 252 | |
| 253 | return parent; |
| 254 | } |
| 255 | |
| 256 | /* |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 257 | * Find the parent for a directory that is not currently connected to |
| 258 | * the filesystem root. |
| 259 | * |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 260 | * On entry, the caller holds d_inode(child_dir)->i_mutex. |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 261 | */ |
Namjae Jeon | ea3983a | 2013-04-29 16:21:11 -0700 | [diff] [blame] | 262 | static struct dentry *fat_get_parent(struct dentry *child_dir) |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 263 | { |
| 264 | struct super_block *sb = child_dir->d_sb; |
| 265 | struct buffer_head *bh = NULL; |
| 266 | struct msdos_dir_entry *de; |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 267 | struct inode *parent_inode = NULL; |
Namjae Jeon | f1e6fb0 | 2013-04-29 16:21:14 -0700 | [diff] [blame] | 268 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 269 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 270 | if (!fat_get_dotdot_entry(d_inode(child_dir), &bh, &de)) { |
Namjae Jeon | f1e6fb0 | 2013-04-29 16:21:14 -0700 | [diff] [blame] | 271 | int parent_logstart = fat_get_start(sbi, de); |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 272 | parent_inode = fat_dget(sb, parent_logstart); |
Namjae Jeon | f1e6fb0 | 2013-04-29 16:21:14 -0700 | [diff] [blame] | 273 | if (!parent_inode && sbi->options.nfs == FAT_NFS_NOSTALE_RO) |
| 274 | parent_inode = fat_rebuild_parent(sb, parent_logstart); |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 275 | } |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 276 | brelse(bh); |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 277 | |
Steven J. Magnani | 7669e8f | 2012-10-04 17:14:45 -0700 | [diff] [blame] | 278 | return d_obtain_alias(parent_inode); |
Steven J. Magnani | 21b6633d5 | 2012-10-04 17:14:44 -0700 | [diff] [blame] | 279 | } |
Namjae Jeon | ea3983a | 2013-04-29 16:21:11 -0700 | [diff] [blame] | 280 | |
| 281 | const struct export_operations fat_export_ops = { |
| 282 | .fh_to_dentry = fat_fh_to_dentry, |
| 283 | .fh_to_parent = fat_fh_to_parent, |
| 284 | .get_parent = fat_get_parent, |
| 285 | }; |
| 286 | |
| 287 | const struct export_operations fat_export_ops_nostale = { |
| 288 | .encode_fh = fat_encode_fh_nostale, |
| 289 | .fh_to_dentry = fat_fh_to_dentry_nostale, |
| 290 | .fh_to_parent = fat_fh_to_parent_nostale, |
| 291 | .get_parent = fat_get_parent, |
| 292 | }; |