blob: d59c02543a10ad9433924b41f680d78efdf06f15 [file] [log] [blame]
Steven J. Magnani21b6633d52012-10-04 17:14:44 -07001/* 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 Jeonea3983a2013-04-29 16:21:11 -070017struct 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. Magnani7669e8f2012-10-04 17:14:45 -070029/**
30 * Look up a directory inode given its starting cluster.
Steven J. Magnani21b6633d52012-10-04 17:14:44 -070031 */
Steven J. Magnani7669e8f2012-10-04 17:14:45 -070032static struct inode *fat_dget(struct super_block *sb, int i_logstart)
Steven J. Magnani21b6633d52012-10-04 17:14:44 -070033{
Steven J. Magnani7669e8f2012-10-04 17:14:45 -070034 struct msdos_sb_info *sbi = MSDOS_SB(sb);
35 struct hlist_head *head;
Steven J. Magnani7669e8f2012-10-04 17:14:45 -070036 struct msdos_inode_info *i;
37 struct inode *inode = NULL;
Steven J. Magnani21b6633d52012-10-04 17:14:44 -070038
Steven J. Magnani7669e8f2012-10-04 17:14:45 -070039 head = sbi->dir_hashtable + fat_dir_hash(i_logstart);
40 spin_lock(&sbi->dir_hash_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -080041 hlist_for_each_entry(i, head, i_dir_hash) {
Steven J. Magnani7669e8f2012-10-04 17:14:45 -070042 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. Magnani21b6633d52012-10-04 17:14:44 -070048 }
Steven J. Magnani7669e8f2012-10-04 17:14:45 -070049 spin_unlock(&sbi->dir_hash_lock);
50 return inode;
Steven J. Magnani21b6633d52012-10-04 17:14:44 -070051}
52
Namjae Jeonea3983a2013-04-29 16:21:11 -070053static struct inode *__fat_nfs_get_inode(struct super_block *sb,
54 u64 ino, u32 generation, loff_t i_pos)
Steven J. Magnani21b6633d52012-10-04 17:14:44 -070055{
Steven J. Magnani7669e8f2012-10-04 17:14:45 -070056 struct inode *inode;
57
58 if ((ino < MSDOS_ROOT_INO) || (ino == MSDOS_FSINFO_INO))
59 return NULL;
60
61 inode = ilookup(sb, ino);
62 if (inode && generation && (inode->i_generation != generation)) {
63 iput(inode);
64 inode = NULL;
65 }
66
67 return inode;
Steven J. Magnani21b6633d52012-10-04 17:14:44 -070068}
69
Namjae Jeonea3983a2013-04-29 16:21:11 -070070static struct inode *fat_nfs_get_inode(struct super_block *sb,
71 u64 ino, u32 generation)
72{
73
74 return __fat_nfs_get_inode(sb, ino, generation, 0);
75}
76
77static int
78fat_encode_fh_nostale(struct inode *inode, __u32 *fh, int *lenp,
79 struct inode *parent)
80{
81 int len = *lenp;
82 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
83 struct fat_fid *fid = (struct fat_fid *) fh;
84 loff_t i_pos;
85 int type = FILEID_FAT_WITHOUT_PARENT;
86
87 if (parent) {
88 if (len < FAT_FID_SIZE_WITH_PARENT) {
89 *lenp = FAT_FID_SIZE_WITH_PARENT;
90 return FILEID_INVALID;
91 }
92 } else {
93 if (len < FAT_FID_SIZE_WITHOUT_PARENT) {
94 *lenp = FAT_FID_SIZE_WITHOUT_PARENT;
95 return FILEID_INVALID;
96 }
97 }
98
99 i_pos = fat_i_pos_read(sbi, inode);
100 *lenp = FAT_FID_SIZE_WITHOUT_PARENT;
101 fid->i_gen = inode->i_generation;
102 fid->i_pos_low = i_pos & 0xFFFFFFFF;
103 fid->i_pos_hi = (i_pos >> 32) & 0xFFFF;
104 if (parent) {
105 i_pos = fat_i_pos_read(sbi, parent);
106 fid->parent_i_pos_hi = (i_pos >> 32) & 0xFFFF;
107 fid->parent_i_pos_low = i_pos & 0xFFFFFFFF;
108 fid->parent_i_gen = parent->i_generation;
109 type = FILEID_FAT_WITH_PARENT;
110 *lenp = FAT_FID_SIZE_WITH_PARENT;
111 }
112
113 return type;
114}
115
Steven J. Magnani21b6633d52012-10-04 17:14:44 -0700116/**
117 * Map a NFS file handle to a corresponding dentry.
118 * The dentry may or may not be connected to the filesystem root.
119 */
Namjae Jeonea3983a2013-04-29 16:21:11 -0700120static struct dentry *fat_fh_to_dentry(struct super_block *sb, struct fid *fid,
Steven J. Magnani21b6633d52012-10-04 17:14:44 -0700121 int fh_len, int fh_type)
122{
Steven J. Magnani7669e8f2012-10-04 17:14:45 -0700123 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
124 fat_nfs_get_inode);
125}
Steven J. Magnani21b6633d52012-10-04 17:14:44 -0700126
Namjae Jeonea3983a2013-04-29 16:21:11 -0700127static struct dentry *fat_fh_to_dentry_nostale(struct super_block *sb,
128 struct fid *fh, int fh_len,
129 int fh_type)
130{
131 struct inode *inode = NULL;
132 struct fat_fid *fid = (struct fat_fid *)fh;
133 loff_t i_pos;
134
135 switch (fh_type) {
136 case FILEID_FAT_WITHOUT_PARENT:
137 if (fh_len < FAT_FID_SIZE_WITHOUT_PARENT)
138 return NULL;
139 break;
140 case FILEID_FAT_WITH_PARENT:
141 if (fh_len < FAT_FID_SIZE_WITH_PARENT)
142 return NULL;
143 break;
144 default:
145 return NULL;
146 }
147 i_pos = fid->i_pos_hi;
148 i_pos = (i_pos << 32) | (fid->i_pos_low);
149 inode = __fat_nfs_get_inode(sb, 0, fid->i_gen, i_pos);
150
151 return d_obtain_alias(inode);
152}
153
Steven J. Magnani7669e8f2012-10-04 17:14:45 -0700154/*
155 * Find the parent for a file specified by NFS handle.
156 * This requires that the handle contain the i_ino of the parent.
157 */
Namjae Jeonea3983a2013-04-29 16:21:11 -0700158static struct dentry *fat_fh_to_parent(struct super_block *sb, struct fid *fid,
Steven J. Magnani7669e8f2012-10-04 17:14:45 -0700159 int fh_len, int fh_type)
160{
161 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
162 fat_nfs_get_inode);
Steven J. Magnani21b6633d52012-10-04 17:14:44 -0700163}
164
Namjae Jeonea3983a2013-04-29 16:21:11 -0700165static struct dentry *fat_fh_to_parent_nostale(struct super_block *sb,
166 struct fid *fh, int fh_len,
167 int fh_type)
168{
169 struct inode *inode = NULL;
170 struct fat_fid *fid = (struct fat_fid *)fh;
171 loff_t i_pos;
172
173 if (fh_len < FAT_FID_SIZE_WITH_PARENT)
174 return NULL;
175
176 switch (fh_type) {
177 case FILEID_FAT_WITH_PARENT:
178 i_pos = fid->parent_i_pos_hi;
179 i_pos = (i_pos << 32) | (fid->parent_i_pos_low);
180 inode = __fat_nfs_get_inode(sb, 0, fid->parent_i_gen, i_pos);
181 break;
182 }
183
184 return d_obtain_alias(inode);
185}
186
Steven J. Magnani21b6633d52012-10-04 17:14:44 -0700187/*
188 * Find the parent for a directory that is not currently connected to
189 * the filesystem root.
190 *
191 * On entry, the caller holds child_dir->d_inode->i_mutex.
192 */
Namjae Jeonea3983a2013-04-29 16:21:11 -0700193static struct dentry *fat_get_parent(struct dentry *child_dir)
Steven J. Magnani21b6633d52012-10-04 17:14:44 -0700194{
195 struct super_block *sb = child_dir->d_sb;
196 struct buffer_head *bh = NULL;
197 struct msdos_dir_entry *de;
Steven J. Magnani7669e8f2012-10-04 17:14:45 -0700198 struct inode *parent_inode = NULL;
Steven J. Magnani21b6633d52012-10-04 17:14:44 -0700199
Steven J. Magnani7669e8f2012-10-04 17:14:45 -0700200 if (!fat_get_dotdot_entry(child_dir->d_inode, &bh, &de)) {
201 int parent_logstart = fat_get_start(MSDOS_SB(sb), de);
202 parent_inode = fat_dget(sb, parent_logstart);
Steven J. Magnani21b6633d52012-10-04 17:14:44 -0700203 }
Steven J. Magnani21b6633d52012-10-04 17:14:44 -0700204 brelse(bh);
Steven J. Magnani21b6633d52012-10-04 17:14:44 -0700205
Steven J. Magnani7669e8f2012-10-04 17:14:45 -0700206 return d_obtain_alias(parent_inode);
Steven J. Magnani21b6633d52012-10-04 17:14:44 -0700207}
Namjae Jeonea3983a2013-04-29 16:21:11 -0700208
209const struct export_operations fat_export_ops = {
210 .fh_to_dentry = fat_fh_to_dentry,
211 .fh_to_parent = fat_fh_to_parent,
212 .get_parent = fat_get_parent,
213};
214
215const struct export_operations fat_export_ops_nostale = {
216 .encode_fh = fat_encode_fh_nostale,
217 .fh_to_dentry = fat_fh_to_dentry_nostale,
218 .fh_to_parent = fat_fh_to_parent_nostale,
219 .get_parent = fat_get_parent,
220};