]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - fs/xfs/xfs_export.c
xfs: reshuffle dir2 definitions around for userspace
[mirror_ubuntu-eoan-kernel.git] / fs / xfs / xfs_export.c
1 /*
2 * Copyright (c) 2004-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
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
7 * published by the Free Software Foundation.
8 *
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.
13 *
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
17 */
18 #include "xfs.h"
19 #include "xfs_types.h"
20 #include "xfs_log.h"
21 #include "xfs_trans.h"
22 #include "xfs_sb.h"
23 #include "xfs_ag.h"
24 #include "xfs_mount.h"
25 #include "xfs_da_btree.h"
26 #include "xfs_dir2_format.h"
27 #include "xfs_dir2.h"
28 #include "xfs_export.h"
29 #include "xfs_vnodeops.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_inode.h"
32 #include "xfs_inode_item.h"
33 #include "xfs_trace.h"
34 #include "xfs_icache.h"
35
36 /*
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.
40 */
41 static int xfs_fileid_length(int fileid_type)
42 {
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;
52 }
53 return FILEID_INVALID;
54 }
55
56 STATIC int
57 xfs_fs_encode_fh(
58 struct inode *inode,
59 __u32 *fh,
60 int *max_len,
61 struct inode *parent)
62 {
63 struct fid *fid = (struct fid *)fh;
64 struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fh;
65 int fileid_type;
66 int len;
67
68 /* Directories don't need their parent encoded, they have ".." */
69 if (!parent)
70 fileid_type = FILEID_INO32_GEN;
71 else
72 fileid_type = FILEID_INO32_GEN_PARENT;
73
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))
84 fileid_type |= XFS_FILEID_TYPE_64FLAG;
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 */
92 len = xfs_fileid_length(fileid_type);
93 if (*max_len < len) {
94 *max_len = len;
95 return FILEID_INVALID;
96 }
97 *max_len = len;
98
99 switch (fileid_type) {
100 case FILEID_INO32_GEN_PARENT:
101 fid->i32.parent_ino = XFS_I(parent)->i_ino;
102 fid->i32.parent_gen = parent->i_generation;
103 /*FALLTHRU*/
104 case FILEID_INO32_GEN:
105 fid->i32.ino = XFS_I(inode)->i_ino;
106 fid->i32.gen = inode->i_generation;
107 break;
108 case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
109 fid64->parent_ino = XFS_I(parent)->i_ino;
110 fid64->parent_gen = parent->i_generation;
111 /*FALLTHRU*/
112 case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG:
113 fid64->ino = XFS_I(inode)->i_ino;
114 fid64->gen = inode->i_generation;
115 break;
116 }
117
118 return fileid_type;
119 }
120
121 STATIC struct inode *
122 xfs_nfs_get_inode(
123 struct super_block *sb,
124 u64 ino,
125 u32 generation)
126 {
127 xfs_mount_t *mp = XFS_M(sb);
128 xfs_inode_t *ip;
129 int error;
130
131 /*
132 * NFS can sometimes send requests for ino 0. Fail them gracefully.
133 */
134 if (ino == 0)
135 return ERR_PTR(-ESTALE);
136
137 /*
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..
141 */
142 error = xfs_iget(mp, NULL, ino, XFS_IGET_UNTRUSTED, 0, &ip);
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 */
151 if (error == EINVAL || error == ENOENT)
152 error = ESTALE;
153 return ERR_PTR(-error);
154 }
155
156 if (ip->i_d.di_gen != generation) {
157 IRELE(ip);
158 return ERR_PTR(-ESTALE);
159 }
160
161 return VFS_I(ip);
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;
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
185 return d_obtain_alias(inode);
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;
194
195 if (fh_len < xfs_fileid_length(fileid_type))
196 return NULL;
197
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
209 return d_obtain_alias(inode);
210 }
211
212 STATIC struct dentry *
213 xfs_fs_get_parent(
214 struct dentry *child)
215 {
216 int error;
217 struct xfs_inode *cip;
218
219 error = xfs_lookup(XFS_I(child->d_inode), &xfs_name_dotdot, &cip, NULL);
220 if (unlikely(error))
221 return ERR_PTR(-error);
222
223 return d_obtain_alias(VFS_I(cip));
224 }
225
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;
232 xfs_lsn_t lsn = 0;
233
234 xfs_ilock(ip, XFS_ILOCK_SHARED);
235 if (xfs_ipincount(ip))
236 lsn = ip->i_itemp->ili_last_lsn;
237 xfs_iunlock(ip, XFS_ILOCK_SHARED);
238
239 if (!lsn)
240 return 0;
241 return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
242 }
243
244 const struct export_operations xfs_export_operations = {
245 .encode_fh = xfs_fs_encode_fh,
246 .fh_to_dentry = xfs_fs_fh_to_dentry,
247 .fh_to_parent = xfs_fs_fh_to_parent,
248 .get_parent = xfs_fs_get_parent,
249 .commit_metadata = xfs_fs_nfs_commit_metadata,
250 };