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 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Portions Copyright 2011 Martin Matuska
25  * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
26  * Portions Copyright 2012 Pawel Jakub Dawidek <pawel@dawidek.net>
27  * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
28  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
29  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
30  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
31  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
32  * Copyright (c) 2013 Steven Hartland. All rights reserved.
33  * Copyright (c) 2014 Integros [integros.com]
34  * Copyright 2016 Toomas Soome <tsoome@me.com>
35  * Copyright (c) 2016 Actifio, Inc. All rights reserved.
36  * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
37  * Copyright 2017 RackTop Systems.
38  * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
39  * Copyright (c) 2019 Datto Inc.
40  */
41 
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/errno.h>
45 #include <sys/uio.h>
46 #include <sys/file.h>
47 #include <sys/kmem.h>
48 #include <sys/stat.h>
49 #include <sys/zfs_ioctl.h>
50 #include <sys/zfs_vfsops.h>
51 #include <sys/zap.h>
52 #include <sys/spa.h>
53 #include <sys/nvpair.h>
54 #include <sys/fs/zfs.h>
55 #include <sys/zfs_ctldir.h>
56 #include <sys/zfs_dir.h>
57 #include <sys/zfs_onexit.h>
58 #include <sys/zvol.h>
59 #include <sys/fm/util.h>
60 #include <sys/dsl_crypt.h>
61 #include <sys/crypto/icp.h>
62 #include <sys/zstd/zstd.h>
63 
64 #include <sys/zfs_ioctl_impl.h>
65 
66 #include <sys/zfs_sysfs.h>
67 #include <linux/miscdevice.h>
68 #include <linux/slab.h>
69 
70 boolean_t
71 zfs_vfs_held(zfsvfs_t *zfsvfs)
72 {
73 	return (zfsvfs->z_sb != NULL);
74 }
75 
76 int
77 zfs_vfs_ref(zfsvfs_t **zfvp)
78 {
79 	if (*zfvp == NULL || (*zfvp)->z_sb == NULL ||
80 	    !atomic_inc_not_zero(&((*zfvp)->z_sb->s_active))) {
81 		return (SET_ERROR(ESRCH));
82 	}
83 	return (0);
84 }
85 
86 void
87 zfs_vfs_rele(zfsvfs_t *zfsvfs)
88 {
89 	deactivate_super(zfsvfs->z_sb);
90 }
91 
92 void
93 zfsdev_private_set_state(void *priv, zfsdev_state_t *zs)
94 {
95 	struct file *filp = priv;
96 
97 	filp->private_data = zs;
98 }
99 
100 zfsdev_state_t *
101 zfsdev_private_get_state(void *priv)
102 {
103 	struct file *filp = priv;
104 
105 	return (filp->private_data);
106 }
107 
108 static int
109 zfsdev_open(struct inode *ino, struct file *filp)
110 {
111 	int error;
112 
113 	mutex_enter(&zfsdev_state_lock);
114 	error = zfsdev_state_init(filp);
115 	mutex_exit(&zfsdev_state_lock);
116 
117 	return (-error);
118 }
119 
120 static int
121 zfsdev_release(struct inode *ino, struct file *filp)
122 {
123 	zfsdev_state_destroy(filp);
124 
125 	return (0);
126 }
127 
128 static long
129 zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
130 {
131 	uint_t vecnum;
132 	zfs_cmd_t *zc;
133 	int error, rc;
134 
135 	vecnum = cmd - ZFS_IOC_FIRST;
136 
137 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
138 
139 	if (ddi_copyin((void *)(uintptr_t)arg, zc, sizeof (zfs_cmd_t), 0)) {
140 		error = -SET_ERROR(EFAULT);
141 		goto out;
142 	}
143 	error = -zfsdev_ioctl_common(vecnum, zc, 0);
144 	rc = ddi_copyout(zc, (void *)(uintptr_t)arg, sizeof (zfs_cmd_t), 0);
145 	if (error == 0 && rc != 0)
146 		error = -SET_ERROR(EFAULT);
147 out:
148 	kmem_free(zc, sizeof (zfs_cmd_t));
149 	return (error);
150 
151 }
152 
153 uint64_t
154 zfs_max_nvlist_src_size_os(void)
155 {
156 	if (zfs_max_nvlist_src_size != 0)
157 		return (zfs_max_nvlist_src_size);
158 
159 	return (MIN(ptob(zfs_totalram_pages) / 4, 128 * 1024 * 1024));
160 }
161 
162 /* Update the VFS's cache of mountpoint properties */
163 void
164 zfs_ioctl_update_mount_cache(const char *dsname)
165 {
166 }
167 
168 void
169 zfs_ioctl_init_os(void)
170 {
171 }
172 
173 #ifdef CONFIG_COMPAT
174 static long
175 zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
176 {
177 	return (zfsdev_ioctl(filp, cmd, arg));
178 }
179 #else
180 #define	zfsdev_compat_ioctl	NULL
181 #endif
182 
183 static const struct file_operations zfsdev_fops = {
184 	.open		= zfsdev_open,
185 	.release	= zfsdev_release,
186 	.unlocked_ioctl	= zfsdev_ioctl,
187 	.compat_ioctl	= zfsdev_compat_ioctl,
188 	.owner		= THIS_MODULE,
189 };
190 
191 static struct miscdevice zfs_misc = {
192 	.minor		= ZFS_DEVICE_MINOR,
193 	.name		= ZFS_DRIVER,
194 	.fops		= &zfsdev_fops,
195 };
196 
197 MODULE_ALIAS_MISCDEV(ZFS_DEVICE_MINOR);
198 MODULE_ALIAS("devname:zfs");
199 
200 int
201 zfsdev_attach(void)
202 {
203 	int error;
204 
205 	error = misc_register(&zfs_misc);
206 	if (error == -EBUSY) {
207 		/*
208 		 * Fallback to dynamic minor allocation in the event of a
209 		 * collision with a reserved minor in linux/miscdevice.h.
210 		 * In this case the kernel modules must be manually loaded.
211 		 */
212 		printk(KERN_INFO "ZFS: misc_register() with static minor %d "
213 		    "failed %d, retrying with MISC_DYNAMIC_MINOR\n",
214 		    ZFS_DEVICE_MINOR, error);
215 
216 		zfs_misc.minor = MISC_DYNAMIC_MINOR;
217 		error = misc_register(&zfs_misc);
218 	}
219 
220 	if (error)
221 		printk(KERN_INFO "ZFS: misc_register() failed %d\n", error);
222 
223 	return (error);
224 }
225 
226 void
227 zfsdev_detach(void)
228 {
229 	misc_deregister(&zfs_misc);
230 }
231 
232 #ifdef ZFS_DEBUG
233 #define	ZFS_DEBUG_STR	" (DEBUG mode)"
234 #else
235 #define	ZFS_DEBUG_STR	""
236 #endif
237 
238 static int
239 openzfs_init_os(void)
240 {
241 	int error;
242 
243 	if ((error = zfs_kmod_init()) != 0) {
244 		printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
245 		    ", rc = %d\n", ZFS_META_VERSION, ZFS_META_RELEASE,
246 		    ZFS_DEBUG_STR, error);
247 
248 		return (-error);
249 	}
250 
251 	zfs_sysfs_init();
252 
253 	printk(KERN_NOTICE "ZFS: Loaded module v%s-%s%s, "
254 	    "ZFS pool version %s, ZFS filesystem version %s\n",
255 	    ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR,
256 	    SPA_VERSION_STRING, ZPL_VERSION_STRING);
257 #ifndef CONFIG_FS_POSIX_ACL
258 	printk(KERN_NOTICE "ZFS: Posix ACLs disabled by kernel\n");
259 #endif /* CONFIG_FS_POSIX_ACL */
260 
261 	return (0);
262 }
263 
264 static void
265 openzfs_fini_os(void)
266 {
267 	zfs_sysfs_fini();
268 	zfs_kmod_fini();
269 
270 	printk(KERN_NOTICE "ZFS: Unloaded module v%s-%s%s\n",
271 	    ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR);
272 }
273 
274 
275 extern int __init zcommon_init(void);
276 extern void zcommon_fini(void);
277 
278 static int __init
279 openzfs_init(void)
280 {
281 	int err;
282 	if ((err = zcommon_init()) != 0)
283 		goto zcommon_failed;
284 	if ((err = icp_init()) != 0)
285 		goto icp_failed;
286 	if ((err = zstd_init()) != 0)
287 		goto zstd_failed;
288 	if ((err = openzfs_init_os()) != 0)
289 		goto openzfs_os_failed;
290 	return (0);
291 
292 openzfs_os_failed:
293 	zstd_fini();
294 zstd_failed:
295 	icp_fini();
296 icp_failed:
297 	zcommon_fini();
298 zcommon_failed:
299 	return (err);
300 }
301 
302 static void __exit
303 openzfs_fini(void)
304 {
305 	openzfs_fini_os();
306 	zstd_fini();
307 	icp_fini();
308 	zcommon_fini();
309 }
310 
311 #if defined(_KERNEL)
312 module_init(openzfs_init);
313 module_exit(openzfs_fini);
314 #endif
315 
316 MODULE_ALIAS("zavl");
317 MODULE_ALIAS("icp");
318 MODULE_ALIAS("zlua");
319 MODULE_ALIAS("znvpair");
320 MODULE_ALIAS("zunicode");
321 MODULE_ALIAS("zcommon");
322 MODULE_ALIAS("zzstd");
323 MODULE_DESCRIPTION("ZFS");
324 MODULE_AUTHOR(ZFS_META_AUTHOR);
325 MODULE_LICENSE("Lua: MIT");
326 MODULE_LICENSE("zstd: Dual BSD/GPL");
327 MODULE_LICENSE("Dual BSD/GPL");
328 MODULE_LICENSE(ZFS_META_LICENSE);
329 MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);
330