]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zpl_export.c
94625e13c85a3e44dc7ad6993674edfe79174a1a
[mirror_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 * Copyright (c) 2012 Cyril Plisko. All rights reserved.
24 */
25
26
27 #include <sys/zfs_vnops.h>
28 #include <sys/zfs_znode.h>
29 #include <sys/zfs_ctldir.h>
30 #include <sys/zpl.h>
31
32
33 static int
34 #ifdef HAVE_ENCODE_FH_WITH_INODE
35 zpl_encode_fh(struct inode *ip, __u32 *fh, int *max_len, struct inode *parent)
36 {
37 #else
38 zpl_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len, int connectable)
39 {
40 struct inode *ip = dentry->d_inode;
41 #endif /* HAVE_ENCODE_FH_WITH_INODE */
42 fid_t *fid = (fid_t *)fh;
43 int len_bytes, rc;
44
45 len_bytes = *max_len * sizeof (__u32);
46
47 if (len_bytes < offsetof(fid_t, fid_data))
48 return 255;
49
50 fid->fid_len = len_bytes - offsetof(fid_t, fid_data);
51
52 if (zfsctl_is_node(ip))
53 rc = zfsctl_fid(ip, fid);
54 else
55 rc = zfs_fid(ip, fid);
56
57 len_bytes = offsetof(fid_t, fid_data) + fid->fid_len;
58 *max_len = roundup(len_bytes, sizeof (__u32)) / sizeof (__u32);
59
60 return (rc == 0 ? FILEID_INO32_GEN : 255);
61 }
62
63 static struct dentry *
64 zpl_dentry_obtain_alias(struct inode *ip)
65 {
66 struct dentry *result;
67
68 #ifdef HAVE_D_OBTAIN_ALIAS
69 result = d_obtain_alias(ip);
70 #else
71 result = d_alloc_anon(ip);
72
73 if (result == NULL) {
74 iput(ip);
75 result = ERR_PTR(-ENOMEM);
76 }
77 #endif /* HAVE_D_OBTAIN_ALIAS */
78
79 return result;
80 }
81
82 static struct dentry *
83 zpl_fh_to_dentry(struct super_block *sb, struct fid *fh,
84 int fh_len, int fh_type)
85 {
86 fid_t *fid = (fid_t *)fh;
87 struct inode *ip;
88 int len_bytes, rc;
89
90 len_bytes = fh_len * sizeof (__u32);
91
92 if (fh_type != FILEID_INO32_GEN ||
93 len_bytes < offsetof(fid_t, fid_data) ||
94 len_bytes < offsetof(fid_t, fid_data) + fid->fid_len)
95 return ERR_PTR(-EINVAL);
96
97 rc = zfs_vget(sb, &ip, fid);
98
99 if (rc != 0)
100 return ERR_PTR(-rc);
101
102 ASSERT((ip != NULL) && !IS_ERR(ip));
103
104 return zpl_dentry_obtain_alias(ip);
105 }
106
107 static struct dentry *
108 zpl_get_parent(struct dentry *child)
109 {
110 cred_t *cr = CRED();
111 struct inode *ip;
112 int error;
113
114 crhold(cr);
115 error = -zfs_lookup(child->d_inode, "..", &ip, 0, cr, NULL, NULL);
116 crfree(cr);
117 ASSERT3S(error, <=, 0);
118
119 if (error)
120 return ERR_PTR(error);
121
122 return zpl_dentry_obtain_alias(ip);
123 }
124
125 #ifdef HAVE_COMMIT_METADATA
126 static int
127 zpl_commit_metadata(struct inode *inode)
128 {
129 cred_t *cr = CRED();
130 int error;
131
132 crhold(cr);
133 error = -zfs_fsync(inode, 0, cr);
134 crfree(cr);
135 ASSERT3S(error, <=, 0);
136
137 return error;
138 }
139 #endif /* HAVE_COMMIT_METADATA */
140
141 const struct export_operations zpl_export_operations = {
142 .encode_fh = zpl_encode_fh,
143 .fh_to_dentry = zpl_fh_to_dentry,
144 .get_parent = zpl_get_parent,
145 #ifdef HAVE_COMMIT_METADATA
146 .commit_metadata= zpl_commit_metadata,
147 #endif /* HAVE_COMMIT_METADATA */
148 };