]> git.proxmox.com Git - mirror_zfs-debian.git/blame - module/zfs/zpl_export.c
Imported Upstream version 0.6.2+git20140204
[mirror_zfs-debian.git] / module / zfs / zpl_export.c
CommitLineData
055656d4
GB
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
393b44c7 23 * Copyright (c) 2012 Cyril Plisko. All rights reserved.
055656d4
GB
24 */
25
26
27#include <sys/zfs_vnops.h>
28#include <sys/zfs_znode.h>
ebe7e575 29#include <sys/zfs_ctldir.h>
055656d4
GB
30#include <sys/zpl.h>
31
32
33static int
756c3e5a
RY
34#ifdef HAVE_ENCODE_FH_WITH_INODE
35zpl_encode_fh(struct inode *ip, __u32 *fh, int *max_len, struct inode *parent)
36{
37#else
055656d4
GB
38zpl_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len, int connectable)
39{
055656d4 40 struct inode *ip = dentry->d_inode;
756c3e5a
RY
41#endif /* HAVE_ENCODE_FH_WITH_INODE */
42 fid_t *fid = (fid_t *)fh;
055656d4
GB
43 int len_bytes, rc;
44
45 len_bytes = *max_len * sizeof (__u32);
46
47 if (len_bytes < offsetof(fid_t, fid_data))
a08ee875 48 return (255);
055656d4
GB
49
50 fid->fid_len = len_bytes - offsetof(fid_t, fid_data);
51
ebe7e575
BB
52 if (zfsctl_is_node(ip))
53 rc = zfsctl_fid(ip, fid);
54 else
55 rc = zfs_fid(ip, fid);
055656d4
GB
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
63static struct dentry *
64zpl_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
a08ee875 79 return (result);
055656d4
GB
80}
81
82static struct dentry *
83zpl_fh_to_dentry(struct super_block *sb, struct fid *fh,
84 int fh_len, int fh_type)
85{
055656d4
GB
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)
a08ee875 95 return (ERR_PTR(-EINVAL));
055656d4 96
2cf7f52b 97 rc = zfs_vget(sb, &ip, fid);
055656d4
GB
98
99 if (rc != 0)
a08ee875 100 return (ERR_PTR(-rc));
055656d4
GB
101
102 ASSERT((ip != NULL) && !IS_ERR(ip));
103
a08ee875 104 return (zpl_dentry_obtain_alias(ip));
055656d4
GB
105}
106
107static struct dentry *
108zpl_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)
a08ee875 120 return (ERR_PTR(error));
055656d4 121
a08ee875 122 return (zpl_dentry_obtain_alias(ip));
055656d4
GB
123}
124
393b44c7
CP
125#ifdef HAVE_COMMIT_METADATA
126static int
127zpl_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
a08ee875 137 return (error);
393b44c7
CP
138}
139#endif /* HAVE_COMMIT_METADATA */
140
055656d4 141const struct export_operations zpl_export_operations = {
a08ee875
LG
142 .encode_fh = zpl_encode_fh,
143 .fh_to_dentry = zpl_fh_to_dentry,
144 .get_parent = zpl_get_parent,
393b44c7 145#ifdef HAVE_COMMIT_METADATA
a08ee875 146 .commit_metadata = zpl_commit_metadata,
393b44c7 147#endif /* HAVE_COMMIT_METADATA */
055656d4 148};