]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zpl_export.c
Fletcher4: Incremental updates and ctx calculation
[mirror_zfs.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 41#endif /* HAVE_ENCODE_FH_WITH_INODE */
7fad6290 42 fstrans_cookie_t cookie;
756c3e5a 43 fid_t *fid = (fid_t *)fh;
055656d4
GB
44 int len_bytes, rc;
45
46 len_bytes = *max_len * sizeof (__u32);
47
48 if (len_bytes < offsetof(fid_t, fid_data))
d1d7e268 49 return (255);
055656d4
GB
50
51 fid->fid_len = len_bytes - offsetof(fid_t, fid_data);
7fad6290 52 cookie = spl_fstrans_mark();
055656d4 53
ebe7e575
BB
54 if (zfsctl_is_node(ip))
55 rc = zfsctl_fid(ip, fid);
56 else
57 rc = zfs_fid(ip, fid);
055656d4 58
7fad6290 59 spl_fstrans_unmark(cookie);
055656d4
GB
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
66static struct dentry *
67zpl_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
d1d7e268 82 return (result);
055656d4
GB
83}
84
85static struct dentry *
86zpl_fh_to_dentry(struct super_block *sb, struct fid *fh,
87 int fh_len, int fh_type)
88{
055656d4 89 fid_t *fid = (fid_t *)fh;
7fad6290 90 fstrans_cookie_t cookie;
055656d4
GB
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)
d1d7e268 99 return (ERR_PTR(-EINVAL));
055656d4 100
7fad6290 101 cookie = spl_fstrans_mark();
2cf7f52b 102 rc = zfs_vget(sb, &ip, fid);
7fad6290 103 spl_fstrans_unmark(cookie);
055656d4 104
79065ed5
JS
105 if (rc) {
106 /*
107 * If we see ENOENT it might mean that an NFSv4 * client
108 * is using a cached inode value in a file handle and
109 * that the sought after file has had its inode changed
110 * by a third party. So change the error to ESTALE
111 * which will trigger a full lookup by the client and
112 * will find the new filename/inode pair if it still
113 * exists.
114 */
115 if (rc == ENOENT)
116 rc = ESTALE;
117
d1d7e268 118 return (ERR_PTR(-rc));
79065ed5 119 }
055656d4
GB
120
121 ASSERT((ip != NULL) && !IS_ERR(ip));
122
d1d7e268 123 return (zpl_dentry_obtain_alias(ip));
055656d4
GB
124}
125
126static struct dentry *
127zpl_get_parent(struct dentry *child)
128{
129 cred_t *cr = CRED();
7fad6290 130 fstrans_cookie_t cookie;
055656d4
GB
131 struct inode *ip;
132 int error;
133
134 crhold(cr);
7fad6290 135 cookie = spl_fstrans_mark();
055656d4 136 error = -zfs_lookup(child->d_inode, "..", &ip, 0, cr, NULL, NULL);
7fad6290 137 spl_fstrans_unmark(cookie);
055656d4
GB
138 crfree(cr);
139 ASSERT3S(error, <=, 0);
140
141 if (error)
d1d7e268 142 return (ERR_PTR(error));
055656d4 143
d1d7e268 144 return (zpl_dentry_obtain_alias(ip));
055656d4
GB
145}
146
393b44c7
CP
147#ifdef HAVE_COMMIT_METADATA
148static int
149zpl_commit_metadata(struct inode *inode)
150{
151 cred_t *cr = CRED();
7fad6290 152 fstrans_cookie_t cookie;
393b44c7
CP
153 int error;
154
b23975cb
AV
155 if (zfsctl_is_node(inode))
156 return (0);
157
393b44c7 158 crhold(cr);
7fad6290 159 cookie = spl_fstrans_mark();
393b44c7 160 error = -zfs_fsync(inode, 0, cr);
7fad6290 161 spl_fstrans_unmark(cookie);
393b44c7
CP
162 crfree(cr);
163 ASSERT3S(error, <=, 0);
164
d1d7e268 165 return (error);
393b44c7
CP
166}
167#endif /* HAVE_COMMIT_METADATA */
168
055656d4 169const struct export_operations zpl_export_operations = {
d1d7e268
MK
170 .encode_fh = zpl_encode_fh,
171 .fh_to_dentry = zpl_fh_to_dentry,
172 .get_parent = zpl_get_parent,
393b44c7 173#ifdef HAVE_COMMIT_METADATA
d1d7e268 174 .commit_metadata = zpl_commit_metadata,
393b44c7 175#endif /* HAVE_COMMIT_METADATA */
055656d4 176};