blob: 0c5f721b4e91d019d8b25a756b40dacaff0f4463 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
J. Bruce Fieldsdc7a0812009-10-27 14:41:35 -040012 * Documentation/filesystems/nfs/Exporting
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * fs/exportfs/expfs.c.
14 */
15
Al Viro94f2f7152005-04-25 18:32:12 -070016#include "isofs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18static struct dentry *
19isofs_export_iget(struct super_block *sb,
20 unsigned long block,
21 unsigned long offset,
22 __u32 generation)
23{
24 struct inode *inode;
Christoph Hellwig44003722008-08-11 15:49:04 +020025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 if (block == 0)
27 return ERR_PTR(-ESTALE);
28 inode = isofs_iget(sb, block, offset);
David Howellsc4386c82008-02-07 00:15:41 -080029 if (IS_ERR(inode))
30 return ERR_CAST(inode);
31 if (generation && inode->i_generation != generation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 iput(inode);
33 return ERR_PTR(-ESTALE);
34 }
Christoph Hellwig44003722008-08-11 15:49:04 +020035 return d_obtain_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/* This function is surprisingly simple. The trick is understanding
39 * that "child" is always a directory. So, to find its parent, you
40 * simply need to find its ".." entry, normalize its block and offset,
41 * and return the underlying inode. See the comments for
42 * isofs_normalize_block_and_offset(). */
43static struct dentry *isofs_export_get_parent(struct dentry *child)
44{
45 unsigned long parent_block = 0;
46 unsigned long parent_offset = 0;
David Howells2b0143b2015-03-17 22:25:59 +000047 struct inode *child_inode = d_inode(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct iso_inode_info *e_child_inode = ISOFS_I(child_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 struct iso_directory_record *de = NULL;
50 struct buffer_head * bh = NULL;
51 struct dentry *rv = NULL;
52
53 /* "child" must always be a directory. */
54 if (!S_ISDIR(child_inode->i_mode)) {
55 printk(KERN_ERR "isofs: isofs_export_get_parent(): "
56 "child is not a directory!\n");
57 rv = ERR_PTR(-EACCES);
58 goto out;
59 }
60
61 /* It is an invariant that the directory offset is zero. If
62 * it is not zero, it means the directory failed to be
63 * normalized for some reason. */
64 if (e_child_inode->i_iget5_offset != 0) {
65 printk(KERN_ERR "isofs: isofs_export_get_parent(): "
66 "child directory not normalized!\n");
67 rv = ERR_PTR(-EACCES);
68 goto out;
69 }
70
71 /* The child inode has been normalized such that its
72 * i_iget5_block value points to the "." entry. Fortunately,
73 * the ".." entry is located in the same block. */
74 parent_block = e_child_inode->i_iget5_block;
75
76 /* Get the block in question. */
77 bh = sb_bread(child_inode->i_sb, parent_block);
78 if (bh == NULL) {
79 rv = ERR_PTR(-EACCES);
80 goto out;
81 }
82
83 /* This is the "." entry. */
84 de = (struct iso_directory_record*)bh->b_data;
85
86 /* The ".." entry is always the second entry. */
87 parent_offset = (unsigned long)isonum_711(de->length);
88 de = (struct iso_directory_record*)(bh->b_data + parent_offset);
89
90 /* Verify it is in fact the ".." entry. */
91 if ((isonum_711(de->name_len) != 1) || (de->name[0] != 1)) {
92 printk(KERN_ERR "isofs: Unable to find the \"..\" "
93 "directory for NFS.\n");
94 rv = ERR_PTR(-EACCES);
95 goto out;
96 }
97
98 /* Normalize */
99 isofs_normalize_block_and_offset(de, &parent_block, &parent_offset);
100
Christoph Hellwig44003722008-08-11 15:49:04 +0200101 rv = d_obtain_alias(isofs_iget(child_inode->i_sb, parent_block,
102 parent_offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 out:
Christoph Hellwig44003722008-08-11 15:49:04 +0200104 if (bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return rv;
107}
108
109static int
Al Virob0b03822012-04-02 14:34:06 -0400110isofs_export_encode_fh(struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 __u32 *fh32,
112 int *max_len,
Al Virob0b03822012-04-02 14:34:06 -0400113 struct inode *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 struct iso_inode_info * ei = ISOFS_I(inode);
116 int len = *max_len;
117 int type = 1;
118 __u16 *fh16 = (__u16*)fh32;
119
120 /*
121 * WARNING: max_len is 5 for NFSv2. Because of this
122 * limitation, we use the lower 16 bits of fh32[1] to hold the
123 * offset of the inode and the upper 16 bits of fh32[1] to
124 * hold the offset of the parent.
125 */
Al Virob0b03822012-04-02 14:34:06 -0400126 if (parent && (len < 5)) {
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +0530127 *max_len = 5;
Namjae Jeon94e07a752013-02-17 15:48:11 +0900128 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +0530129 } else if (len < 3) {
130 *max_len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +0900131 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +0530132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 len = 3;
135 fh32[0] = ei->i_iget5_block;
136 fh16[2] = (__u16)ei->i_iget5_offset; /* fh16 [sic] */
Mathias Krausefe685aa2012-07-12 08:46:54 +0200137 fh16[3] = 0; /* avoid leaking uninitialized data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 fh32[2] = inode->i_generation;
Al Virob0b03822012-04-02 14:34:06 -0400139 if (parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 struct iso_inode_info *eparent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 eparent = ISOFS_I(parent);
142 fh32[3] = eparent->i_iget5_block;
143 fh16[3] = (__u16)eparent->i_iget5_offset; /* fh16 [sic] */
144 fh32[4] = parent->i_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 len = 5;
146 type = 2;
147 }
148 *max_len = len;
149 return type;
150}
151
Christoph Hellwig905251a2007-10-21 16:42:12 -0700152struct isofs_fid {
153 u32 block;
154 u16 offset;
155 u16 parent_offset;
156 u32 generation;
157 u32 parent_block;
158 u32 parent_generation;
159};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Christoph Hellwig905251a2007-10-21 16:42:12 -0700161static struct dentry *isofs_fh_to_dentry(struct super_block *sb,
162 struct fid *fid, int fh_len, int fh_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Christoph Hellwig905251a2007-10-21 16:42:12 -0700164 struct isofs_fid *ifid = (struct isofs_fid *)fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Christoph Hellwig905251a2007-10-21 16:42:12 -0700166 if (fh_len < 3 || fh_type > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return NULL;
168
Christoph Hellwig905251a2007-10-21 16:42:12 -0700169 return isofs_export_iget(sb, ifid->block, ifid->offset,
170 ifid->generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Christoph Hellwig905251a2007-10-21 16:42:12 -0700173static struct dentry *isofs_fh_to_parent(struct super_block *sb,
174 struct fid *fid, int fh_len, int fh_type)
175{
176 struct isofs_fid *ifid = (struct isofs_fid *)fid;
177
Hugh Dickins35c2a7f2012-10-07 20:32:51 -0700178 if (fh_len < 2 || fh_type != 2)
Christoph Hellwig905251a2007-10-21 16:42:12 -0700179 return NULL;
180
181 return isofs_export_iget(sb,
182 fh_len > 2 ? ifid->parent_block : 0,
183 ifid->parent_offset,
184 fh_len > 4 ? ifid->parent_generation : 0);
185}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Christoph Hellwig39655162007-10-21 16:42:17 -0700187const struct export_operations isofs_export_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 .encode_fh = isofs_export_encode_fh,
Christoph Hellwig905251a2007-10-21 16:42:12 -0700189 .fh_to_dentry = isofs_fh_to_dentry,
190 .fh_to_parent = isofs_fh_to_parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 .get_parent = isofs_export_get_parent,
192};