]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/fat/nfs.c
fat-exportfs-rebuild-inode-if-ilookup-fails-fix
[karo-tx-linux.git] / fs / fat / nfs.c
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
17 struct fat_fid {
18         u32 ino;
19         u32 gen;
20         u64 i_pos;
21         u32 parent_ino;
22         u32 parent_gen;
23 } __packed;
24
25 /**
26  * Look up a directory inode given its starting cluster.
27  */
28 static struct inode *fat_dget(struct super_block *sb, int i_logstart)
29 {
30         struct msdos_sb_info *sbi = MSDOS_SB(sb);
31         struct hlist_head *head;
32         struct hlist_node *_p;
33         struct msdos_inode_info *i;
34         struct inode *inode = NULL;
35
36         head = sbi->dir_hashtable + fat_dir_hash(i_logstart);
37         spin_lock(&sbi->dir_hash_lock);
38         hlist_for_each_entry(i, _p, head, i_dir_hash) {
39                 BUG_ON(i->vfs_inode.i_sb != sb);
40                 if (i->i_logstart != i_logstart)
41                         continue;
42                 inode = igrab(&i->vfs_inode);
43                 if (inode)
44                         break;
45         }
46         spin_unlock(&sbi->dir_hash_lock);
47         return inode;
48 }
49
50 static struct inode *fat_nfs_get_inode(struct super_block *sb,
51                                        u64 ino, u32 generation, loff_t i_pos)
52 {
53         struct inode *inode;
54
55         if ((ino < MSDOS_ROOT_INO) || (ino == MSDOS_FSINFO_INO))
56                 return NULL;
57
58         inode = ilookup(sb, ino);
59         if (inode && generation && (inode->i_generation != generation)) {
60                 iput(inode);
61                 inode = NULL;
62         }
63         if (inode == NULL && MSDOS_SB(sb)->options.nfs == FAT_NFS_NOSTALE_RO) {
64                 struct buffer_head *bh = NULL;
65                 struct msdos_dir_entry *de ;
66                 sector_t blocknr;
67                 int offset;
68                 fat_get_blknr_offset(MSDOS_SB(sb), i_pos, &blocknr, &offset);
69                 bh = sb_bread(sb, blocknr);
70                 if (!bh) {
71                         fat_msg(sb, KERN_ERR,
72                                 "unable to read block(%llu) for building NFS inode",
73                                 (llu)blocknr);
74                         return inode;
75                 }
76                 de = (struct msdos_dir_entry *)bh->b_data;
77                 /* If a file is deleted on server and client is not updated
78                  * yet, we must not build the inode upon a lookup call.
79                  */
80                 if (IS_FREE(de[offset].name))
81                         inode = NULL;
82                 else
83                         inode = fat_build_inode(sb, &de[offset], i_pos);
84                 brelse(bh);
85         }
86
87         return inode;
88 }
89
90
91 int
92 fat_encode_fh(struct inode *inode, __u32 *fh, int *lenp, struct inode *parent)
93 {
94         int len = *lenp;
95         struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
96         struct fat_fid *fid = (struct fat_fid *) fh;
97         loff_t i_pos;
98         int type = FILEID_INO32_GEN;
99
100         if (parent && (len < 5)) {
101                 *lenp = 5;
102                 return 255;
103         } else if (len < 3) {
104                 *lenp = 3;
105                 return 255;
106         }
107
108         i_pos = fat_i_pos_read(sbi, inode);
109         *lenp = 3;
110         fid->ino = inode->i_ino;
111         fid->gen = inode->i_generation;
112         fid->i_pos = i_pos;
113         if (parent) {
114                 fid->parent_ino = parent->i_ino;
115                 fid->parent_gen = parent->i_generation;
116                 type = FILEID_INO32_GEN_PARENT;
117                 *lenp = 5;
118         }
119
120         return type;
121 }
122
123 /**
124  * Map a NFS file handle to a corresponding dentry.
125  * The dentry may or may not be connected to the filesystem root.
126  */
127 struct dentry *fat_fh_to_dentry(struct super_block *sb, struct fid *fh,
128                                 int fh_len, int fh_type)
129 {
130         struct inode *inode = NULL;
131         struct fat_fid *fid = (struct fat_fid *)fh;
132         if (fh_len < 3)
133                 return NULL;
134
135         switch (fh_type) {
136         case FILEID_INO32_GEN:
137         case FILEID_INO32_GEN_PARENT:
138                 inode = fat_nfs_get_inode(sb, fid->ino, fid->gen, fid->i_pos);
139
140                 break;
141         }
142
143         return d_obtain_alias(inode);
144 }
145
146 /*
147  * Find the parent for a file specified by NFS handle.
148  * This requires that the handle contain the i_ino of the parent.
149  */
150 struct dentry *fat_fh_to_parent(struct super_block *sb, struct fid *fh,
151                                 int fh_len, int fh_type)
152 {
153         struct inode *inode = NULL;
154         struct fat_fid *fid = (struct fat_fid *)fh;
155         if (fh_len < 5)
156                 return NULL;
157
158         switch (fh_type) {
159         case FILEID_INO32_GEN_PARENT:
160                 inode = fat_nfs_get_inode(sb, fid->parent_ino, fid->parent_gen,
161                                                 fid->i_pos);
162                 break;
163         }
164
165         return d_obtain_alias(inode);
166 }
167
168 /*
169  * Find the parent for a directory that is not currently connected to
170  * the filesystem root.
171  *
172  * On entry, the caller holds child_dir->d_inode->i_mutex.
173  */
174 struct dentry *fat_get_parent(struct dentry *child_dir)
175 {
176         struct super_block *sb = child_dir->d_sb;
177         struct buffer_head *bh = NULL;
178         struct msdos_dir_entry *de;
179         struct inode *parent_inode = NULL;
180
181         if (!fat_get_dotdot_entry(child_dir->d_inode, &bh, &de)) {
182                 int parent_logstart = fat_get_start(MSDOS_SB(sb), de);
183                 parent_inode = fat_dget(sb, parent_logstart);
184         }
185         brelse(bh);
186
187         return d_obtain_alias(parent_inode);
188 }