]> git.proxmox.com Git - mirror_zfs-debian.git/blob - module/zfs/zpl_export.c
Imported Upstream version 0.6.4.2
[mirror_zfs-debian.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 fstrans_cookie_t cookie;
43 fid_t *fid = (fid_t *)fh;
44 int len_bytes, rc;
45
46 len_bytes = *max_len * sizeof (__u32);
47
48 if (len_bytes < offsetof(fid_t, fid_data))
49 return (255);
50
51 fid->fid_len = len_bytes - offsetof(fid_t, fid_data);
52 cookie = spl_fstrans_mark();
53
54 if (zfsctl_is_node(ip))
55 rc = zfsctl_fid(ip, fid);
56 else
57 rc = zfs_fid(ip, fid);
58
59 spl_fstrans_unmark(cookie);
60 len_bytes = offsetof(fid_t, fid_data) + fid->fid_len;
61 *max_len = roundup(len_bytes, sizeof (__u32)) / sizeof (__u32);
62
63 return (rc == 0 ? FILEID_INO32_GEN : 255);
64 }
65
66 static struct dentry *
67 zpl_dentry_obtain_alias(struct inode *ip)
68 {
69 struct dentry *result;
70
71 #ifdef HAVE_D_OBTAIN_ALIAS
72 result = d_obtain_alias(ip);
73 #else
74 result = d_alloc_anon(ip);
75
76 if (result == NULL) {
77 iput(ip);
78 result = ERR_PTR(-ENOMEM);
79 }
80 #endif /* HAVE_D_OBTAIN_ALIAS */
81
82 return (result);
83 }
84
85 static struct dentry *
86 zpl_fh_to_dentry(struct super_block *sb, struct fid *fh,
87 int fh_len, int fh_type)
88 {
89 fid_t *fid = (fid_t *)fh;
90 fstrans_cookie_t cookie;
91 struct inode *ip;
92 int len_bytes, rc;
93
94 len_bytes = fh_len * sizeof (__u32);
95
96 if (fh_type != FILEID_INO32_GEN ||
97 len_bytes < offsetof(fid_t, fid_data) ||
98 len_bytes < offsetof(fid_t, fid_data) + fid->fid_len)
99 return (ERR_PTR(-EINVAL));
100
101 cookie = spl_fstrans_mark();
102 rc = zfs_vget(sb, &ip, fid);
103 spl_fstrans_unmark(cookie);
104
105 if (rc != 0)
106 return (ERR_PTR(-rc));
107
108 ASSERT((ip != NULL) && !IS_ERR(ip));
109
110 return (zpl_dentry_obtain_alias(ip));
111 }
112
113 static struct dentry *
114 zpl_get_parent(struct dentry *child)
115 {
116 cred_t *cr = CRED();
117 fstrans_cookie_t cookie;
118 struct inode *ip;
119 int error;
120
121 crhold(cr);
122 cookie = spl_fstrans_mark();
123 error = -zfs_lookup(child->d_inode, "..", &ip, 0, cr, NULL, NULL);
124 spl_fstrans_unmark(cookie);
125 crfree(cr);
126 ASSERT3S(error, <=, 0);
127
128 if (error)
129 return (ERR_PTR(error));
130
131 return (zpl_dentry_obtain_alias(ip));
132 }
133
134 #ifdef HAVE_COMMIT_METADATA
135 static int
136 zpl_commit_metadata(struct inode *inode)
137 {
138 cred_t *cr = CRED();
139 fstrans_cookie_t cookie;
140 int error;
141
142 crhold(cr);
143 cookie = spl_fstrans_mark();
144 error = -zfs_fsync(inode, 0, cr);
145 spl_fstrans_unmark(cookie);
146 crfree(cr);
147 ASSERT3S(error, <=, 0);
148
149 return (error);
150 }
151 #endif /* HAVE_COMMIT_METADATA */
152
153 const struct export_operations zpl_export_operations = {
154 .encode_fh = zpl_encode_fh,
155 .fh_to_dentry = zpl_fh_to_dentry,
156 .get_parent = zpl_get_parent,
157 #ifdef HAVE_COMMIT_METADATA
158 .commit_metadata = zpl_commit_metadata,
159 #endif /* HAVE_COMMIT_METADATA */
160 };