blob: 85a9093769a955ba1cd7355190e2a1462d626d96 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * fs/isofs/export.c
4 *
5 * (C) 2004 Paul Serice - The new inode scheme requires switching
6 * from iget() to iget5_locked() which means
7 * the NFS export operations have to be hand
8 * coded because the default routines rely on
9 * iget().
10 *
11 * The following files are helpful:
12 *
J. Bruce Fieldsdc7a0812009-10-27 14:41:35 -040013 * Documentation/filesystems/nfs/Exporting
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * fs/exportfs/expfs.c.
15 */
16
Al Viro94f2f7152005-04-25 18:32:12 -070017#include "isofs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19static struct dentry *
20isofs_export_iget(struct super_block *sb,
21 unsigned long block,
22 unsigned long offset,
23 __u32 generation)
24{
25 struct inode *inode;
Christoph Hellwig44003722008-08-11 15:49:04 +020026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 if (block == 0)
28 return ERR_PTR(-ESTALE);
29 inode = isofs_iget(sb, block, offset);
David Howellsc4386c82008-02-07 00:15:41 -080030 if (IS_ERR(inode))
31 return ERR_CAST(inode);
32 if (generation && inode->i_generation != generation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 iput(inode);
34 return ERR_PTR(-ESTALE);
35 }
Christoph Hellwig44003722008-08-11 15:49:04 +020036 return d_obtain_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* This function is surprisingly simple. The trick is understanding
40 * that "child" is always a directory. So, to find its parent, you
41 * simply need to find its ".." entry, normalize its block and offset,
42 * and return the underlying inode. See the comments for
43 * isofs_normalize_block_and_offset(). */
44static struct dentry *isofs_export_get_parent(struct dentry *child)
45{
46 unsigned long parent_block = 0;
47 unsigned long parent_offset = 0;
David Howells2b0143b2015-03-17 22:25:59 +000048 struct inode *child_inode = d_inode(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 struct iso_inode_info *e_child_inode = ISOFS_I(child_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 struct iso_directory_record *de = NULL;
51 struct buffer_head * bh = NULL;
52 struct dentry *rv = NULL;
53
54 /* "child" must always be a directory. */
55 if (!S_ISDIR(child_inode->i_mode)) {
56 printk(KERN_ERR "isofs: isofs_export_get_parent(): "
57 "child is not a directory!\n");
58 rv = ERR_PTR(-EACCES);
59 goto out;
60 }
61
62 /* It is an invariant that the directory offset is zero. If
63 * it is not zero, it means the directory failed to be
64 * normalized for some reason. */
65 if (e_child_inode->i_iget5_offset != 0) {
66 printk(KERN_ERR "isofs: isofs_export_get_parent(): "
67 "child directory not normalized!\n");
68 rv = ERR_PTR(-EACCES);
69 goto out;
70 }
71
72 /* The child inode has been normalized such that its
73 * i_iget5_block value points to the "." entry. Fortunately,
74 * the ".." entry is located in the same block. */
75 parent_block = e_child_inode->i_iget5_block;
76
77 /* Get the block in question. */
78 bh = sb_bread(child_inode->i_sb, parent_block);
79 if (bh == NULL) {
80 rv = ERR_PTR(-EACCES);
81 goto out;
82 }
83
84 /* This is the "." entry. */
85 de = (struct iso_directory_record*)bh->b_data;
86
87 /* The ".." entry is always the second entry. */
88 parent_offset = (unsigned long)isonum_711(de->length);
89 de = (struct iso_directory_record*)(bh->b_data + parent_offset);
90
91 /* Verify it is in fact the ".." entry. */
92 if ((isonum_711(de->name_len) != 1) || (de->name[0] != 1)) {
93 printk(KERN_ERR "isofs: Unable to find the \"..\" "
94 "directory for NFS.\n");
95 rv = ERR_PTR(-EACCES);
96 goto out;
97 }
98
99 /* Normalize */
100 isofs_normalize_block_and_offset(de, &parent_block, &parent_offset);
101
Christoph Hellwig44003722008-08-11 15:49:04 +0200102 rv = d_obtain_alias(isofs_iget(child_inode->i_sb, parent_block,
103 parent_offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 out:
Christoph Hellwig44003722008-08-11 15:49:04 +0200105 if (bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return rv;
108}
109
110static int
Al Virob0b03822012-04-02 14:34:06 -0400111isofs_export_encode_fh(struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 __u32 *fh32,
113 int *max_len,
Al Virob0b03822012-04-02 14:34:06 -0400114 struct inode *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct iso_inode_info * ei = ISOFS_I(inode);
117 int len = *max_len;
118 int type = 1;
119 __u16 *fh16 = (__u16*)fh32;
120
121 /*
122 * WARNING: max_len is 5 for NFSv2. Because of this
123 * limitation, we use the lower 16 bits of fh32[1] to hold the
124 * offset of the inode and the upper 16 bits of fh32[1] to
125 * hold the offset of the parent.
126 */
Al Virob0b03822012-04-02 14:34:06 -0400127 if (parent && (len < 5)) {
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +0530128 *max_len = 5;
Namjae Jeon94e07a752013-02-17 15:48:11 +0900129 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +0530130 } else if (len < 3) {
131 *max_len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +0900132 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +0530133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 len = 3;
136 fh32[0] = ei->i_iget5_block;
137 fh16[2] = (__u16)ei->i_iget5_offset; /* fh16 [sic] */
Mathias Krausefe685aa2012-07-12 08:46:54 +0200138 fh16[3] = 0; /* avoid leaking uninitialized data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 fh32[2] = inode->i_generation;
Al Virob0b03822012-04-02 14:34:06 -0400140 if (parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct iso_inode_info *eparent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 eparent = ISOFS_I(parent);
143 fh32[3] = eparent->i_iget5_block;
144 fh16[3] = (__u16)eparent->i_iget5_offset; /* fh16 [sic] */
145 fh32[4] = parent->i_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 len = 5;
147 type = 2;
148 }
149 *max_len = len;
150 return type;
151}
152
Christoph Hellwig905251a2007-10-21 16:42:12 -0700153struct isofs_fid {
154 u32 block;
155 u16 offset;
156 u16 parent_offset;
157 u32 generation;
158 u32 parent_block;
159 u32 parent_generation;
160};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Christoph Hellwig905251a2007-10-21 16:42:12 -0700162static struct dentry *isofs_fh_to_dentry(struct super_block *sb,
163 struct fid *fid, int fh_len, int fh_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Christoph Hellwig905251a2007-10-21 16:42:12 -0700165 struct isofs_fid *ifid = (struct isofs_fid *)fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Christoph Hellwig905251a2007-10-21 16:42:12 -0700167 if (fh_len < 3 || fh_type > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return NULL;
169
Christoph Hellwig905251a2007-10-21 16:42:12 -0700170 return isofs_export_iget(sb, ifid->block, ifid->offset,
171 ifid->generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Christoph Hellwig905251a2007-10-21 16:42:12 -0700174static struct dentry *isofs_fh_to_parent(struct super_block *sb,
175 struct fid *fid, int fh_len, int fh_type)
176{
177 struct isofs_fid *ifid = (struct isofs_fid *)fid;
178
Hugh Dickins35c2a7f2012-10-07 20:32:51 -0700179 if (fh_len < 2 || fh_type != 2)
Christoph Hellwig905251a2007-10-21 16:42:12 -0700180 return NULL;
181
182 return isofs_export_iget(sb,
183 fh_len > 2 ? ifid->parent_block : 0,
184 ifid->parent_offset,
185 fh_len > 4 ? ifid->parent_generation : 0);
186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Christoph Hellwig39655162007-10-21 16:42:17 -0700188const struct export_operations isofs_export_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 .encode_fh = isofs_export_encode_fh,
Christoph Hellwig905251a2007-10-21 16:42:12 -0700190 .fh_to_dentry = isofs_fh_to_dentry,
191 .fh_to_parent = isofs_fh_to_parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .get_parent = isofs_export_get_parent,
193};