Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2004-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 | 17 | */ |
1da177e4 LT |
18 | #include "xfs.h" |
19 | #include "xfs_types.h" | |
1da177e4 LT |
20 | #include "xfs_log.h" |
21 | #include "xfs_trans.h" | |
22 | #include "xfs_sb.h" | |
da353b0d | 23 | #include "xfs_ag.h" |
1da177e4 | 24 | #include "xfs_mount.h" |
2b9ab5ab DC |
25 | #include "xfs_da_btree.h" |
26 | #include "xfs_dir2_format.h" | |
27 | #include "xfs_dir2.h" | |
1da177e4 | 28 | #include "xfs_export.h" |
739bfb2a CH |
29 | #include "xfs_vnodeops.h" |
30 | #include "xfs_bmap_btree.h" | |
31 | #include "xfs_inode.h" | |
978ebd97 | 32 | #include "xfs_inode_item.h" |
ef35e925 | 33 | #include "xfs_trace.h" |
33479e05 | 34 | #include "xfs_icache.h" |
1da177e4 LT |
35 | |
36 | /* | |
c38344fe CH |
37 | * Note that we only accept fileids which are long enough rather than allow |
38 | * the parent generation number to default to zero. XFS considers zero a | |
39 | * valid generation number not an invalid/wildcard value. | |
1da177e4 | 40 | */ |
c38344fe | 41 | static int xfs_fileid_length(int fileid_type) |
1da177e4 | 42 | { |
c38344fe CH |
43 | switch (fileid_type) { |
44 | case FILEID_INO32_GEN: | |
45 | return 2; | |
46 | case FILEID_INO32_GEN_PARENT: | |
47 | return 4; | |
48 | case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG: | |
49 | return 3; | |
50 | case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG: | |
51 | return 6; | |
1da177e4 | 52 | } |
94e07a75 | 53 | return FILEID_INVALID; |
1da177e4 LT |
54 | } |
55 | ||
1da177e4 | 56 | STATIC int |
a50cd269 | 57 | xfs_fs_encode_fh( |
b0b0382b AV |
58 | struct inode *inode, |
59 | __u32 *fh, | |
60 | int *max_len, | |
61 | struct inode *parent) | |
1da177e4 | 62 | { |
c38344fe CH |
63 | struct fid *fid = (struct fid *)fh; |
64 | struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fh; | |
c38344fe | 65 | int fileid_type; |
1da177e4 | 66 | int len; |
1da177e4 LT |
67 | |
68 | /* Directories don't need their parent encoded, they have ".." */ | |
b0b0382b | 69 | if (!parent) |
c38344fe CH |
70 | fileid_type = FILEID_INO32_GEN; |
71 | else | |
72 | fileid_type = FILEID_INO32_GEN_PARENT; | |
73 | ||
576ecb8e SK |
74 | /* |
75 | * If the the filesystem may contain 64bit inode numbers, we need | |
76 | * to use larger file handles that can represent them. | |
77 | * | |
78 | * While we only allocate inodes that do not fit into 32 bits any | |
79 | * large enough filesystem may contain them, thus the slightly | |
80 | * confusing looking conditional below. | |
81 | */ | |
82 | if (!(XFS_M(inode->i_sb)->m_flags & XFS_MOUNT_SMALL_INUMS) || | |
83 | (XFS_M(inode->i_sb)->m_flags & XFS_MOUNT_32BITINODES)) | |
c38344fe | 84 | fileid_type |= XFS_FILEID_TYPE_64FLAG; |
1da177e4 LT |
85 | |
86 | /* | |
87 | * Only encode if there is enough space given. In practice | |
88 | * this means we can't export a filesystem with 64bit inodes | |
89 | * over NFSv2 with the subtree_check export option; the other | |
90 | * seven combinations work. The real answer is "don't use v2". | |
91 | */ | |
c38344fe | 92 | len = xfs_fileid_length(fileid_type); |
5fe0c237 AK |
93 | if (*max_len < len) { |
94 | *max_len = len; | |
94e07a75 | 95 | return FILEID_INVALID; |
5fe0c237 | 96 | } |
1da177e4 LT |
97 | *max_len = len; |
98 | ||
c38344fe CH |
99 | switch (fileid_type) { |
100 | case FILEID_INO32_GEN_PARENT: | |
b0b0382b AV |
101 | fid->i32.parent_ino = XFS_I(parent)->i_ino; |
102 | fid->i32.parent_gen = parent->i_generation; | |
c38344fe CH |
103 | /*FALLTHRU*/ |
104 | case FILEID_INO32_GEN: | |
c29f7d45 | 105 | fid->i32.ino = XFS_I(inode)->i_ino; |
c38344fe CH |
106 | fid->i32.gen = inode->i_generation; |
107 | break; | |
108 | case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG: | |
b0b0382b AV |
109 | fid64->parent_ino = XFS_I(parent)->i_ino; |
110 | fid64->parent_gen = parent->i_generation; | |
c38344fe CH |
111 | /*FALLTHRU*/ |
112 | case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG: | |
c29f7d45 | 113 | fid64->ino = XFS_I(inode)->i_ino; |
c38344fe CH |
114 | fid64->gen = inode->i_generation; |
115 | break; | |
1da177e4 | 116 | } |
c38344fe CH |
117 | |
118 | return fileid_type; | |
1da177e4 LT |
119 | } |
120 | ||
c38344fe CH |
121 | STATIC struct inode * |
122 | xfs_nfs_get_inode( | |
1da177e4 | 123 | struct super_block *sb, |
c38344fe CH |
124 | u64 ino, |
125 | u32 generation) | |
126 | { | |
f71354bc CH |
127 | xfs_mount_t *mp = XFS_M(sb); |
128 | xfs_inode_t *ip; | |
1da177e4 LT |
129 | int error; |
130 | ||
f71354bc CH |
131 | /* |
132 | * NFS can sometimes send requests for ino 0. Fail them gracefully. | |
133 | */ | |
134 | if (ino == 0) | |
135 | return ERR_PTR(-ESTALE); | |
1da177e4 | 136 | |
c9a98553 | 137 | /* |
1920779e DC |
138 | * The XFS_IGET_UNTRUSTED means that an invalid inode number is just |
139 | * fine and not an indication of a corrupted filesystem as clients can | |
140 | * send invalid file handles and we have to handle it gracefully.. | |
c9a98553 | 141 | */ |
ef35e925 | 142 | error = xfs_iget(mp, NULL, ino, XFS_IGET_UNTRUSTED, 0, &ip); |
c9a98553 CH |
143 | if (error) { |
144 | /* | |
145 | * EINVAL means the inode cluster doesn't exist anymore. | |
146 | * This implies the filehandle is stale, so we should | |
147 | * translate it here. | |
148 | * We don't use ESTALE directly down the chain to not | |
149 | * confuse applications using bulkstat that expect EINVAL. | |
150 | */ | |
ad1a2c87 | 151 | if (error == EINVAL || error == ENOENT) |
c9a98553 | 152 | error = ESTALE; |
c38344fe | 153 | return ERR_PTR(-error); |
c9a98553 | 154 | } |
f71354bc | 155 | |
6a7f422d | 156 | if (ip->i_d.di_gen != generation) { |
ef35e925 | 157 | IRELE(ip); |
ad1a2c87 | 158 | return ERR_PTR(-ESTALE); |
f71354bc | 159 | } |
c38344fe | 160 | |
01651646 | 161 | return VFS_I(ip); |
c38344fe CH |
162 | } |
163 | ||
164 | STATIC struct dentry * | |
165 | xfs_fs_fh_to_dentry(struct super_block *sb, struct fid *fid, | |
166 | int fh_len, int fileid_type) | |
167 | { | |
168 | struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fid; | |
169 | struct inode *inode = NULL; | |
c38344fe CH |
170 | |
171 | if (fh_len < xfs_fileid_length(fileid_type)) | |
172 | return NULL; | |
173 | ||
174 | switch (fileid_type) { | |
175 | case FILEID_INO32_GEN_PARENT: | |
176 | case FILEID_INO32_GEN: | |
177 | inode = xfs_nfs_get_inode(sb, fid->i32.ino, fid->i32.gen); | |
178 | break; | |
179 | case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG: | |
180 | case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG: | |
181 | inode = xfs_nfs_get_inode(sb, fid64->ino, fid64->gen); | |
182 | break; | |
183 | } | |
184 | ||
44003728 | 185 | return d_obtain_alias(inode); |
c38344fe CH |
186 | } |
187 | ||
188 | STATIC struct dentry * | |
189 | xfs_fs_fh_to_parent(struct super_block *sb, struct fid *fid, | |
190 | int fh_len, int fileid_type) | |
191 | { | |
192 | struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fid; | |
193 | struct inode *inode = NULL; | |
c38344fe | 194 | |
35c2a7f4 HD |
195 | if (fh_len < xfs_fileid_length(fileid_type)) |
196 | return NULL; | |
197 | ||
c38344fe CH |
198 | switch (fileid_type) { |
199 | case FILEID_INO32_GEN_PARENT: | |
200 | inode = xfs_nfs_get_inode(sb, fid->i32.parent_ino, | |
201 | fid->i32.parent_gen); | |
202 | break; | |
203 | case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG: | |
204 | inode = xfs_nfs_get_inode(sb, fid64->parent_ino, | |
205 | fid64->parent_gen); | |
206 | break; | |
207 | } | |
208 | ||
44003728 | 209 | return d_obtain_alias(inode); |
1da177e4 LT |
210 | } |
211 | ||
212 | STATIC struct dentry * | |
a50cd269 | 213 | xfs_fs_get_parent( |
1da177e4 LT |
214 | struct dentry *child) |
215 | { | |
216 | int error; | |
ef1f5e7a | 217 | struct xfs_inode *cip; |
1da177e4 | 218 | |
384f3ced | 219 | error = xfs_lookup(XFS_I(child->d_inode), &xfs_name_dotdot, &cip, NULL); |
1da177e4 LT |
220 | if (unlikely(error)) |
221 | return ERR_PTR(-error); | |
222 | ||
44003728 | 223 | return d_obtain_alias(VFS_I(cip)); |
1da177e4 LT |
224 | } |
225 | ||
978ebd97 BM |
226 | STATIC int |
227 | xfs_fs_nfs_commit_metadata( | |
228 | struct inode *inode) | |
229 | { | |
230 | struct xfs_inode *ip = XFS_I(inode); | |
231 | struct xfs_mount *mp = ip->i_mount; | |
8292d88c | 232 | xfs_lsn_t lsn = 0; |
978ebd97 BM |
233 | |
234 | xfs_ilock(ip, XFS_ILOCK_SHARED); | |
8292d88c CH |
235 | if (xfs_ipincount(ip)) |
236 | lsn = ip->i_itemp->ili_last_lsn; | |
978ebd97 BM |
237 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
238 | ||
8292d88c CH |
239 | if (!lsn) |
240 | return 0; | |
241 | return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL); | |
978ebd97 BM |
242 | } |
243 | ||
39655164 | 244 | const struct export_operations xfs_export_operations = { |
a50cd269 | 245 | .encode_fh = xfs_fs_encode_fh, |
c38344fe CH |
246 | .fh_to_dentry = xfs_fs_fh_to_dentry, |
247 | .fh_to_parent = xfs_fs_fh_to_parent, | |
a50cd269 | 248 | .get_parent = xfs_fs_get_parent, |
978ebd97 | 249 | .commit_metadata = xfs_fs_nfs_commit_metadata, |
1da177e4 | 250 | }; |