Linux 3.5 compat, eops->encode_fh() takes inodes
[zfs.git] / module / zfs / zpl_export.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2011 Gunnar Beutner
23  */
24
25
26 #include <sys/zfs_vnops.h>
27 #include <sys/zfs_znode.h>
28 #include <sys/zfs_ctldir.h>
29 #include <sys/zpl.h>
30
31
32 static int
33 #ifdef HAVE_ENCODE_FH_WITH_INODE
34 zpl_encode_fh(struct inode *ip, __u32 *fh, int *max_len, struct inode *parent)
35 {
36 #else
37 zpl_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len, int connectable)
38 {
39         struct inode *ip = dentry->d_inode;
40 #endif /* HAVE_ENCODE_FH_WITH_INODE */
41         fid_t *fid = (fid_t *)fh;
42         int len_bytes, rc;
43
44         len_bytes = *max_len * sizeof (__u32);
45
46         if (len_bytes < offsetof(fid_t, fid_data))
47                 return 255;
48
49         fid->fid_len = len_bytes - offsetof(fid_t, fid_data);
50
51         if (zfsctl_is_node(ip))
52                 rc = zfsctl_fid(ip, fid);
53         else
54                 rc = zfs_fid(ip, fid);
55
56         len_bytes = offsetof(fid_t, fid_data) + fid->fid_len;
57         *max_len = roundup(len_bytes, sizeof (__u32)) / sizeof (__u32);
58
59         return (rc == 0 ? FILEID_INO32_GEN : 255);
60 }
61
62 static struct dentry *
63 zpl_dentry_obtain_alias(struct inode *ip)
64 {
65         struct dentry *result;
66
67 #ifdef HAVE_D_OBTAIN_ALIAS
68         result = d_obtain_alias(ip);
69 #else
70         result = d_alloc_anon(ip);
71
72         if (result == NULL) {
73                 iput(ip);
74                 result = ERR_PTR(-ENOMEM);
75         }
76 #endif /* HAVE_D_OBTAIN_ALIAS */
77
78         return result;
79 }
80
81 static struct dentry *
82 zpl_fh_to_dentry(struct super_block *sb, struct fid *fh,
83     int fh_len, int fh_type)
84 {
85         fid_t *fid = (fid_t *)fh;
86         struct inode *ip;
87         int len_bytes, rc;
88
89         len_bytes = fh_len * sizeof (__u32);
90
91         if (fh_type != FILEID_INO32_GEN ||
92             len_bytes < offsetof(fid_t, fid_data) ||
93             len_bytes < offsetof(fid_t, fid_data) + fid->fid_len)
94                 return ERR_PTR(-EINVAL);
95
96         rc = zfs_vget(sb, &ip, fid);
97
98         if (rc != 0)
99                 return ERR_PTR(-rc);
100
101         ASSERT((ip != NULL) && !IS_ERR(ip));
102
103         return zpl_dentry_obtain_alias(ip);
104 }
105
106 static struct dentry *
107 zpl_get_parent(struct dentry *child)
108 {
109         cred_t *cr = CRED();
110         struct inode *ip;
111         int error;
112
113         crhold(cr);
114         error = -zfs_lookup(child->d_inode, "..", &ip, 0, cr, NULL, NULL);
115         crfree(cr);
116         ASSERT3S(error, <=, 0);
117
118         if (error)
119                 return ERR_PTR(error);
120
121         return zpl_dentry_obtain_alias(ip);
122 }
123
124 const struct export_operations zpl_export_operations = {
125         .encode_fh      = zpl_encode_fh,
126         .fh_to_dentry   = zpl_fh_to_dentry,
127         .get_parent     = zpl_get_parent
128 };