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