Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame^] | 2 | * Copyright (c) 2004-2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame^] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame^] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame^] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
| 19 | #include "xfs_types.h" |
| 20 | #include "xfs_dmapi.h" |
| 21 | #include "xfs_log.h" |
| 22 | #include "xfs_trans.h" |
| 23 | #include "xfs_sb.h" |
| 24 | #include "xfs_dir.h" |
| 25 | #include "xfs_mount.h" |
| 26 | #include "xfs_export.h" |
| 27 | |
| 28 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame^] | 29 | * XFS encodes and decodes the fileid portion of NFS filehandles |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | * itself instead of letting the generic NFS code do it. This |
| 31 | * allows filesystems with 64 bit inode numbers to be exported. |
| 32 | * |
| 33 | * Note that a side effect is that xfs_vget() won't be passed a |
| 34 | * zero inode/generation pair under normal circumstances. As |
| 35 | * however a malicious client could send us such data, the check |
| 36 | * remains in that code. |
| 37 | */ |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | STATIC struct dentry * |
| 40 | linvfs_decode_fh( |
| 41 | struct super_block *sb, |
| 42 | __u32 *fh, |
| 43 | int fh_len, |
| 44 | int fileid_type, |
| 45 | int (*acceptable)( |
| 46 | void *context, |
| 47 | struct dentry *de), |
| 48 | void *context) |
| 49 | { |
| 50 | xfs_fid2_t ifid; |
| 51 | xfs_fid2_t pfid; |
| 52 | void *parent = NULL; |
| 53 | int is64 = 0; |
| 54 | __u32 *p = fh; |
| 55 | |
| 56 | #if XFS_BIG_INUMS |
| 57 | is64 = (fileid_type & XFS_FILEID_TYPE_64FLAG); |
| 58 | fileid_type &= ~XFS_FILEID_TYPE_64FLAG; |
| 59 | #endif |
| 60 | |
| 61 | /* |
| 62 | * Note that we only accept fileids which are long enough |
| 63 | * rather than allow the parent generation number to default |
| 64 | * to zero. XFS considers zero a valid generation number not |
| 65 | * an invalid/wildcard value. There's little point printk'ing |
| 66 | * a warning here as we don't have the client information |
| 67 | * which would make such a warning useful. |
| 68 | */ |
| 69 | if (fileid_type > 2 || |
| 70 | fh_len < xfs_fileid_length((fileid_type == 2), is64)) |
| 71 | return NULL; |
| 72 | |
| 73 | p = xfs_fileid_decode_fid2(p, &ifid, is64); |
| 74 | |
| 75 | if (fileid_type == 2) { |
| 76 | p = xfs_fileid_decode_fid2(p, &pfid, is64); |
| 77 | parent = &pfid; |
| 78 | } |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame^] | 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | fh = (__u32 *)&ifid; |
| 81 | return find_exported_dentry(sb, fh, parent, acceptable, context); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | STATIC int |
| 86 | linvfs_encode_fh( |
| 87 | struct dentry *dentry, |
| 88 | __u32 *fh, |
| 89 | int *max_len, |
| 90 | int connectable) |
| 91 | { |
| 92 | struct inode *inode = dentry->d_inode; |
| 93 | int type = 1; |
| 94 | __u32 *p = fh; |
| 95 | int len; |
| 96 | int is64 = 0; |
| 97 | #if XFS_BIG_INUMS |
| 98 | vfs_t *vfs = LINVFS_GET_VFS(inode->i_sb); |
| 99 | xfs_mount_t *mp = XFS_VFSTOM(vfs); |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame^] | 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | if (!(mp->m_flags & XFS_MOUNT_32BITINOOPT)) { |
| 102 | /* filesystem may contain 64bit inode numbers */ |
| 103 | is64 = XFS_FILEID_TYPE_64FLAG; |
| 104 | } |
| 105 | #endif |
| 106 | |
| 107 | /* Directories don't need their parent encoded, they have ".." */ |
| 108 | if (S_ISDIR(inode->i_mode)) |
| 109 | connectable = 0; |
| 110 | |
| 111 | /* |
| 112 | * Only encode if there is enough space given. In practice |
| 113 | * this means we can't export a filesystem with 64bit inodes |
| 114 | * over NFSv2 with the subtree_check export option; the other |
| 115 | * seven combinations work. The real answer is "don't use v2". |
| 116 | */ |
| 117 | len = xfs_fileid_length(connectable, is64); |
| 118 | if (*max_len < len) |
| 119 | return 255; |
| 120 | *max_len = len; |
| 121 | |
| 122 | p = xfs_fileid_encode_inode(p, inode, is64); |
| 123 | if (connectable) { |
| 124 | spin_lock(&dentry->d_lock); |
| 125 | p = xfs_fileid_encode_inode(p, dentry->d_parent->d_inode, is64); |
| 126 | spin_unlock(&dentry->d_lock); |
| 127 | type = 2; |
| 128 | } |
| 129 | BUG_ON((p - fh) != len); |
| 130 | return type | is64; |
| 131 | } |
| 132 | |
| 133 | STATIC struct dentry * |
| 134 | linvfs_get_dentry( |
| 135 | struct super_block *sb, |
| 136 | void *data) |
| 137 | { |
| 138 | vnode_t *vp; |
| 139 | struct inode *inode; |
| 140 | struct dentry *result; |
| 141 | vfs_t *vfsp = LINVFS_GET_VFS(sb); |
| 142 | int error; |
| 143 | |
| 144 | VFS_VGET(vfsp, &vp, (fid_t *)data, error); |
| 145 | if (error || vp == NULL) |
| 146 | return ERR_PTR(-ESTALE) ; |
| 147 | |
| 148 | inode = LINVFS_GET_IP(vp); |
| 149 | result = d_alloc_anon(inode); |
| 150 | if (!result) { |
| 151 | iput(inode); |
| 152 | return ERR_PTR(-ENOMEM); |
| 153 | } |
| 154 | return result; |
| 155 | } |
| 156 | |
| 157 | STATIC struct dentry * |
| 158 | linvfs_get_parent( |
| 159 | struct dentry *child) |
| 160 | { |
| 161 | int error; |
| 162 | vnode_t *vp, *cvp; |
| 163 | struct dentry *parent; |
| 164 | struct dentry dotdot; |
| 165 | |
| 166 | dotdot.d_name.name = ".."; |
| 167 | dotdot.d_name.len = 2; |
| 168 | dotdot.d_inode = NULL; |
| 169 | |
| 170 | cvp = NULL; |
| 171 | vp = LINVFS_GET_VP(child->d_inode); |
| 172 | VOP_LOOKUP(vp, &dotdot, &cvp, 0, NULL, NULL, error); |
| 173 | if (unlikely(error)) |
| 174 | return ERR_PTR(-error); |
| 175 | |
| 176 | parent = d_alloc_anon(LINVFS_GET_IP(cvp)); |
| 177 | if (unlikely(!parent)) { |
| 178 | VN_RELE(cvp); |
| 179 | return ERR_PTR(-ENOMEM); |
| 180 | } |
| 181 | return parent; |
| 182 | } |
| 183 | |
| 184 | struct export_operations linvfs_export_ops = { |
| 185 | .decode_fh = linvfs_decode_fh, |
| 186 | .encode_fh = linvfs_encode_fh, |
| 187 | .get_parent = linvfs_get_parent, |
| 188 | .get_dentry = linvfs_get_dentry, |
| 189 | }; |