xref: /openbmc/linux/fs/isofs/export.c (revision 905251a0)
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 
16 #include "isofs.h"
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 /* This function is surprisingly simple.  The trick is understanding
46  * that "child" is always a directory. So, to find its parent, you
47  * simply need to find its ".." entry, normalize its block and offset,
48  * and return the underlying inode.  See the comments for
49  * isofs_normalize_block_and_offset(). */
50 static struct dentry *isofs_export_get_parent(struct dentry *child)
51 {
52 	unsigned long parent_block = 0;
53 	unsigned long parent_offset = 0;
54 	struct inode *child_inode = child->d_inode;
55 	struct iso_inode_info *e_child_inode = ISOFS_I(child_inode);
56 	struct inode *parent_inode = NULL;
57 	struct iso_directory_record *de = NULL;
58 	struct buffer_head * bh = NULL;
59 	struct dentry *rv = NULL;
60 
61 	/* "child" must always be a directory. */
62 	if (!S_ISDIR(child_inode->i_mode)) {
63 		printk(KERN_ERR "isofs: isofs_export_get_parent(): "
64 		       "child is not a directory!\n");
65 		rv = ERR_PTR(-EACCES);
66 		goto out;
67 	}
68 
69 	/* It is an invariant that the directory offset is zero.  If
70 	 * it is not zero, it means the directory failed to be
71 	 * normalized for some reason. */
72 	if (e_child_inode->i_iget5_offset != 0) {
73 		printk(KERN_ERR "isofs: isofs_export_get_parent(): "
74 		       "child directory not normalized!\n");
75 		rv = ERR_PTR(-EACCES);
76 		goto out;
77 	}
78 
79 	/* The child inode has been normalized such that its
80 	 * i_iget5_block value points to the "." entry.  Fortunately,
81 	 * the ".." entry is located in the same block. */
82 	parent_block = e_child_inode->i_iget5_block;
83 
84 	/* Get the block in question. */
85 	bh = sb_bread(child_inode->i_sb, parent_block);
86 	if (bh == NULL) {
87 		rv = ERR_PTR(-EACCES);
88 		goto out;
89 	}
90 
91 	/* This is the "." entry. */
92 	de = (struct iso_directory_record*)bh->b_data;
93 
94 	/* The ".." entry is always the second entry. */
95 	parent_offset = (unsigned long)isonum_711(de->length);
96 	de = (struct iso_directory_record*)(bh->b_data + parent_offset);
97 
98 	/* Verify it is in fact the ".." entry. */
99 	if ((isonum_711(de->name_len) != 1) || (de->name[0] != 1)) {
100 		printk(KERN_ERR "isofs: Unable to find the \"..\" "
101 		       "directory for NFS.\n");
102 		rv = ERR_PTR(-EACCES);
103 		goto out;
104 	}
105 
106 	/* Normalize */
107 	isofs_normalize_block_and_offset(de, &parent_block, &parent_offset);
108 
109 	/* Get the inode. */
110 	parent_inode = isofs_iget(child_inode->i_sb,
111 				  parent_block,
112 				  parent_offset);
113 	if (parent_inode == NULL) {
114 		rv = ERR_PTR(-EACCES);
115 		goto out;
116 	}
117 
118 	/* Allocate the dentry. */
119 	rv = d_alloc_anon(parent_inode);
120 	if (rv == NULL) {
121 		rv = ERR_PTR(-ENOMEM);
122 		goto out;
123 	}
124 
125  out:
126 	if (bh) {
127 		brelse(bh);
128 	}
129 	return rv;
130 }
131 
132 static int
133 isofs_export_encode_fh(struct dentry *dentry,
134 		       __u32 *fh32,
135 		       int *max_len,
136 		       int connectable)
137 {
138 	struct inode * inode = dentry->d_inode;
139 	struct iso_inode_info * ei = ISOFS_I(inode);
140 	int len = *max_len;
141 	int type = 1;
142 	__u16 *fh16 = (__u16*)fh32;
143 
144 	/*
145 	 * WARNING: max_len is 5 for NFSv2.  Because of this
146 	 * limitation, we use the lower 16 bits of fh32[1] to hold the
147 	 * offset of the inode and the upper 16 bits of fh32[1] to
148 	 * hold the offset of the parent.
149 	 */
150 
151 	if (len < 3 || (connectable && len < 5))
152 		return 255;
153 
154 	len = 3;
155 	fh32[0] = ei->i_iget5_block;
156  	fh16[2] = (__u16)ei->i_iget5_offset;  /* fh16 [sic] */
157 	fh32[2] = inode->i_generation;
158 	if (connectable && !S_ISDIR(inode->i_mode)) {
159 		struct inode *parent;
160 		struct iso_inode_info *eparent;
161 		spin_lock(&dentry->d_lock);
162 		parent = dentry->d_parent->d_inode;
163 		eparent = ISOFS_I(parent);
164 		fh32[3] = eparent->i_iget5_block;
165 		fh16[3] = (__u16)eparent->i_iget5_offset;  /* fh16 [sic] */
166 		fh32[4] = parent->i_generation;
167 		spin_unlock(&dentry->d_lock);
168 		len = 5;
169 		type = 2;
170 	}
171 	*max_len = len;
172 	return type;
173 }
174 
175 struct isofs_fid {
176 	u32 block;
177 	u16 offset;
178 	u16 parent_offset;
179 	u32 generation;
180 	u32 parent_block;
181 	u32 parent_generation;
182 };
183 
184 static struct dentry *isofs_fh_to_dentry(struct super_block *sb,
185 	struct fid *fid, int fh_len, int fh_type)
186 {
187 	struct isofs_fid *ifid = (struct isofs_fid *)fid;
188 
189 	if (fh_len < 3 || fh_type > 2)
190 		return NULL;
191 
192 	return isofs_export_iget(sb, ifid->block, ifid->offset,
193 			ifid->generation);
194 }
195 
196 static struct dentry *isofs_fh_to_parent(struct super_block *sb,
197 		struct fid *fid, int fh_len, int fh_type)
198 {
199 	struct isofs_fid *ifid = (struct isofs_fid *)fid;
200 
201 	if (fh_type != 2)
202 		return NULL;
203 
204 	return isofs_export_iget(sb,
205 			fh_len > 2 ? ifid->parent_block : 0,
206 			ifid->parent_offset,
207 			fh_len > 4 ? ifid->parent_generation : 0);
208 }
209 
210 struct export_operations isofs_export_ops = {
211 	.encode_fh	= isofs_export_encode_fh,
212 	.fh_to_dentry	= isofs_fh_to_dentry,
213 	.fh_to_parent	= isofs_fh_to_parent,
214 	.get_parent     = isofs_export_get_parent,
215 };
216