| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * fs/isofs/export.c | 
 | 3 |  * | 
 | 4 |  *  (C) 2004  Paul Serice - The new inode scheme requires switching | 
 | 5 |  *                          from iget() to iget5_locked() which means | 
 | 6 |  *                          the NFS export operations have to be hand | 
 | 7 |  *                          coded because the default routines rely on | 
 | 8 |  *                          iget(). | 
 | 9 |  * | 
 | 10 |  * The following files are helpful: | 
 | 11 |  * | 
 | 12 |  *     Documentation/filesystems/Exporting | 
 | 13 |  *     fs/exportfs/expfs.c. | 
 | 14 |  */ | 
 | 15 |  | 
| Al Viro | 94f2f71 | 2005-04-25 18:32:12 -0700 | [diff] [blame] | 16 | #include "isofs.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 |  | 
 | 18 | static struct dentry * | 
 | 19 | isofs_export_iget(struct super_block *sb, | 
 | 20 | 		  unsigned long block, | 
 | 21 | 		  unsigned long offset, | 
 | 22 | 		  __u32 generation) | 
 | 23 | { | 
 | 24 | 	struct inode *inode; | 
 | 25 | 	struct dentry *result; | 
 | 26 | 	if (block == 0) | 
 | 27 | 		return ERR_PTR(-ESTALE); | 
 | 28 | 	inode = isofs_iget(sb, block, offset); | 
 | 29 | 	if (inode == NULL) | 
 | 30 | 		return ERR_PTR(-ENOMEM); | 
 | 31 | 	if (is_bad_inode(inode) | 
 | 32 | 	    || (generation && inode->i_generation != generation)) | 
 | 33 | 	{ | 
 | 34 | 		iput(inode); | 
 | 35 | 		return ERR_PTR(-ESTALE); | 
 | 36 | 	} | 
 | 37 | 	result = d_alloc_anon(inode); | 
 | 38 | 	if (!result) { | 
 | 39 | 		iput(inode); | 
 | 40 | 		return ERR_PTR(-ENOMEM); | 
 | 41 | 	} | 
 | 42 | 	return result; | 
 | 43 | } | 
 | 44 |  | 
 | 45 | static struct dentry * | 
 | 46 | isofs_export_get_dentry(struct super_block *sb, void *vobjp) | 
 | 47 | { | 
 | 48 | 	__u32 *objp = vobjp; | 
 | 49 | 	unsigned long block = objp[0]; | 
 | 50 | 	unsigned long offset = objp[1]; | 
 | 51 | 	__u32 generation = objp[2]; | 
 | 52 | 	return isofs_export_iget(sb, block, offset, generation); | 
 | 53 | } | 
 | 54 |  | 
 | 55 | /* This function is surprisingly simple.  The trick is understanding | 
 | 56 |  * that "child" is always a directory. So, to find its parent, you | 
 | 57 |  * simply need to find its ".." entry, normalize its block and offset, | 
 | 58 |  * and return the underlying inode.  See the comments for | 
 | 59 |  * isofs_normalize_block_and_offset(). */ | 
 | 60 | static struct dentry *isofs_export_get_parent(struct dentry *child) | 
 | 61 | { | 
 | 62 | 	unsigned long parent_block = 0; | 
 | 63 | 	unsigned long parent_offset = 0; | 
 | 64 | 	struct inode *child_inode = child->d_inode; | 
 | 65 | 	struct iso_inode_info *e_child_inode = ISOFS_I(child_inode); | 
 | 66 | 	struct inode *parent_inode = NULL; | 
 | 67 | 	struct iso_directory_record *de = NULL; | 
 | 68 | 	struct buffer_head * bh = NULL; | 
 | 69 | 	struct dentry *rv = NULL; | 
 | 70 |  | 
 | 71 | 	/* "child" must always be a directory. */ | 
 | 72 | 	if (!S_ISDIR(child_inode->i_mode)) { | 
 | 73 | 		printk(KERN_ERR "isofs: isofs_export_get_parent(): " | 
 | 74 | 		       "child is not a directory!\n"); | 
 | 75 | 		rv = ERR_PTR(-EACCES); | 
 | 76 | 		goto out; | 
 | 77 | 	} | 
 | 78 |  | 
 | 79 | 	/* It is an invariant that the directory offset is zero.  If | 
 | 80 | 	 * it is not zero, it means the directory failed to be | 
 | 81 | 	 * normalized for some reason. */ | 
 | 82 | 	if (e_child_inode->i_iget5_offset != 0) { | 
 | 83 | 		printk(KERN_ERR "isofs: isofs_export_get_parent(): " | 
 | 84 | 		       "child directory not normalized!\n"); | 
 | 85 | 		rv = ERR_PTR(-EACCES); | 
 | 86 | 		goto out; | 
 | 87 | 	} | 
 | 88 |  | 
 | 89 | 	/* The child inode has been normalized such that its | 
 | 90 | 	 * i_iget5_block value points to the "." entry.  Fortunately, | 
 | 91 | 	 * the ".." entry is located in the same block. */ | 
 | 92 | 	parent_block = e_child_inode->i_iget5_block; | 
 | 93 |  | 
 | 94 | 	/* Get the block in question. */ | 
 | 95 | 	bh = sb_bread(child_inode->i_sb, parent_block); | 
 | 96 | 	if (bh == NULL) { | 
 | 97 | 		rv = ERR_PTR(-EACCES); | 
 | 98 | 		goto out; | 
 | 99 | 	} | 
 | 100 |  | 
 | 101 | 	/* This is the "." entry. */ | 
 | 102 | 	de = (struct iso_directory_record*)bh->b_data; | 
 | 103 |  | 
 | 104 | 	/* The ".." entry is always the second entry. */ | 
 | 105 | 	parent_offset = (unsigned long)isonum_711(de->length); | 
 | 106 | 	de = (struct iso_directory_record*)(bh->b_data + parent_offset); | 
 | 107 |  | 
 | 108 | 	/* Verify it is in fact the ".." entry. */ | 
 | 109 | 	if ((isonum_711(de->name_len) != 1) || (de->name[0] != 1)) { | 
 | 110 | 		printk(KERN_ERR "isofs: Unable to find the \"..\" " | 
 | 111 | 		       "directory for NFS.\n"); | 
 | 112 | 		rv = ERR_PTR(-EACCES); | 
 | 113 | 		goto out; | 
 | 114 | 	} | 
 | 115 |  | 
 | 116 | 	/* Normalize */ | 
 | 117 | 	isofs_normalize_block_and_offset(de, &parent_block, &parent_offset); | 
 | 118 |  | 
 | 119 | 	/* Get the inode. */ | 
 | 120 | 	parent_inode = isofs_iget(child_inode->i_sb, | 
 | 121 | 				  parent_block, | 
 | 122 | 				  parent_offset); | 
 | 123 | 	if (parent_inode == NULL) { | 
 | 124 | 		rv = ERR_PTR(-EACCES); | 
 | 125 | 		goto out; | 
 | 126 | 	} | 
 | 127 |  | 
 | 128 | 	/* Allocate the dentry. */ | 
 | 129 | 	rv = d_alloc_anon(parent_inode); | 
 | 130 | 	if (rv == NULL) { | 
 | 131 | 		rv = ERR_PTR(-ENOMEM); | 
 | 132 | 		goto out; | 
 | 133 | 	} | 
 | 134 |  | 
 | 135 |  out: | 
 | 136 | 	if (bh) { | 
 | 137 | 		brelse(bh); | 
 | 138 | 	} | 
 | 139 | 	return rv; | 
 | 140 | } | 
 | 141 |  | 
 | 142 | static int | 
 | 143 | isofs_export_encode_fh(struct dentry *dentry, | 
 | 144 | 		       __u32 *fh32, | 
 | 145 | 		       int *max_len, | 
 | 146 | 		       int connectable) | 
 | 147 | { | 
 | 148 | 	struct inode * inode = dentry->d_inode; | 
 | 149 | 	struct iso_inode_info * ei = ISOFS_I(inode); | 
 | 150 | 	int len = *max_len; | 
 | 151 | 	int type = 1; | 
 | 152 | 	__u16 *fh16 = (__u16*)fh32; | 
 | 153 |  | 
 | 154 | 	/* | 
 | 155 | 	 * WARNING: max_len is 5 for NFSv2.  Because of this | 
 | 156 | 	 * limitation, we use the lower 16 bits of fh32[1] to hold the | 
 | 157 | 	 * offset of the inode and the upper 16 bits of fh32[1] to | 
 | 158 | 	 * hold the offset of the parent. | 
 | 159 | 	 */ | 
 | 160 |  | 
 | 161 | 	if (len < 3 || (connectable && len < 5)) | 
 | 162 | 		return 255; | 
 | 163 |  | 
 | 164 | 	len = 3; | 
 | 165 | 	fh32[0] = ei->i_iget5_block; | 
 | 166 |  	fh16[2] = (__u16)ei->i_iget5_offset;  /* fh16 [sic] */ | 
 | 167 | 	fh32[2] = inode->i_generation; | 
 | 168 | 	if (connectable && !S_ISDIR(inode->i_mode)) { | 
 | 169 | 		struct inode *parent; | 
 | 170 | 		struct iso_inode_info *eparent; | 
 | 171 | 		spin_lock(&dentry->d_lock); | 
 | 172 | 		parent = dentry->d_parent->d_inode; | 
 | 173 | 		eparent = ISOFS_I(parent); | 
 | 174 | 		fh32[3] = eparent->i_iget5_block; | 
 | 175 | 		fh16[3] = (__u16)eparent->i_iget5_offset;  /* fh16 [sic] */ | 
 | 176 | 		fh32[4] = parent->i_generation; | 
 | 177 | 		spin_unlock(&dentry->d_lock); | 
 | 178 | 		len = 5; | 
 | 179 | 		type = 2; | 
 | 180 | 	} | 
 | 181 | 	*max_len = len; | 
 | 182 | 	return type; | 
 | 183 | } | 
 | 184 |  | 
 | 185 |  | 
 | 186 | static struct dentry * | 
 | 187 | isofs_export_decode_fh(struct super_block *sb, | 
 | 188 | 		       __u32 *fh32, | 
 | 189 | 		       int fh_len, | 
 | 190 | 		       int fileid_type, | 
 | 191 | 		       int (*acceptable)(void *context, struct dentry *de), | 
 | 192 | 		       void *context) | 
 | 193 | { | 
 | 194 | 	__u16 *fh16 = (__u16*)fh32; | 
 | 195 | 	__u32 child[3];   /* The child is what triggered all this. */ | 
 | 196 | 	__u32 parent[3];  /* The parent is just along for the ride. */ | 
 | 197 |  | 
 | 198 | 	if (fh_len < 3 || fileid_type > 2) | 
 | 199 | 		return NULL; | 
 | 200 |  | 
 | 201 | 	child[0] = fh32[0]; | 
 | 202 | 	child[1] = fh16[2];  /* fh16 [sic] */ | 
 | 203 | 	child[2] = fh32[2]; | 
 | 204 |  | 
 | 205 | 	parent[0] = 0; | 
 | 206 | 	parent[1] = 0; | 
 | 207 | 	parent[2] = 0; | 
 | 208 | 	if (fileid_type == 2) { | 
 | 209 | 		if (fh_len > 2) parent[0] = fh32[3]; | 
 | 210 | 		parent[1] = fh16[3];  /* fh16 [sic] */ | 
 | 211 | 		if (fh_len > 4) parent[2] = fh32[4]; | 
 | 212 | 	} | 
 | 213 |  | 
 | 214 | 	return sb->s_export_op->find_exported_dentry(sb, child, parent, | 
 | 215 | 						     acceptable, context); | 
 | 216 | } | 
 | 217 |  | 
 | 218 |  | 
 | 219 | struct export_operations isofs_export_ops = { | 
 | 220 | 	.decode_fh	= isofs_export_decode_fh, | 
 | 221 | 	.encode_fh	= isofs_export_encode_fh, | 
 | 222 | 	.get_dentry	= isofs_export_get_dentry, | 
 | 223 | 	.get_parent     = isofs_export_get_parent, | 
 | 224 | }; |