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