Implemented NFS export_operations.
[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/zpl.h>
29
30
31 static int
32 zpl_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len, int connectable)
33 {
34         fid_t *fid = (fid_t *)fh;
35         struct inode *ip = dentry->d_inode;
36         int len_bytes, rc;
37
38         len_bytes = *max_len * sizeof (__u32);
39
40         if (len_bytes < offsetof(fid_t, fid_data))
41                 return 255;
42
43         fid->fid_len = len_bytes - offsetof(fid_t, fid_data);
44
45         rc = zfs_fid(ip, fid);
46
47         len_bytes = offsetof(fid_t, fid_data) + fid->fid_len;
48         *max_len = roundup(len_bytes, sizeof (__u32)) / sizeof (__u32);
49
50         return (rc == 0 ? FILEID_INO32_GEN : 255);
51 }
52
53 static struct dentry *
54 zpl_dentry_obtain_alias(struct inode *ip)
55 {
56         struct dentry *result;
57
58 #ifdef HAVE_D_OBTAIN_ALIAS
59         result = d_obtain_alias(ip);
60 #else
61         result = d_alloc_anon(ip);
62
63         if (result == NULL) {
64                 iput(ip);
65                 result = ERR_PTR(-ENOMEM);
66         }
67 #endif /* HAVE_D_OBTAIN_ALIAS */
68
69         return result;
70 }
71
72 static struct dentry *
73 zpl_fh_to_dentry(struct super_block *sb, struct fid *fh,
74     int fh_len, int fh_type)
75 {
76         zfs_sb_t *zsb = sb->s_fs_info;
77         struct vfsmount *vfs = zsb->z_vfs;
78         fid_t *fid = (fid_t *)fh;
79         struct inode *ip;
80         int len_bytes, rc;
81
82         len_bytes = fh_len * sizeof (__u32);
83
84         if (fh_type != FILEID_INO32_GEN ||
85             len_bytes < offsetof(fid_t, fid_data) ||
86             len_bytes < offsetof(fid_t, fid_data) + fid->fid_len)
87                 return ERR_PTR(-EINVAL);
88
89         rc = zfs_vget(vfs, &ip, fid);
90
91         if (rc != 0)
92                 return ERR_PTR(-rc);
93
94         ASSERT((ip != NULL) && !IS_ERR(ip));
95
96         return zpl_dentry_obtain_alias(ip);
97 }
98
99 static struct dentry *
100 zpl_get_parent(struct dentry *child)
101 {
102         cred_t *cr = CRED();
103         struct inode *ip;
104         int error;
105
106         crhold(cr);
107         error = -zfs_lookup(child->d_inode, "..", &ip, 0, cr, NULL, NULL);
108         crfree(cr);
109         ASSERT3S(error, <=, 0);
110
111         if (error)
112                 return ERR_PTR(error);
113
114         return zpl_dentry_obtain_alias(ip);
115 }
116
117 const struct export_operations zpl_export_operations = {
118         .encode_fh      = zpl_encode_fh,
119         .fh_to_dentry   = zpl_fh_to_dentry,
120         .get_parent     = zpl_get_parent
121 };