vfs_io_uring: move error handling out of vfs_io_uring_pread_recv()
[Samba.git] / source3 / modules / lib_vxfs.c
blobdcb5cb304e139128b16c880f25284c522fa69a7c
1 /*
2 Unix SMB/CIFS implementation.
3 Wrap VxFS xattr calls.
5 Copyright (C) Veritas Technologies LLC <www.veritas.com> 2016
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "string.h"
25 #include "vfs_vxfs.h"
28 * Available under GPL at
29 * http://www.veritas.com/community/downloads/vxfsmisc-library
31 #define LIBVXFS "/usr/lib64/vxfsmisc.so"
34 static int (*vxfs_setxattr_fd_func) (int fd, const char *name,
35 const void *value, size_t len, int flags);
36 static int (*vxfs_getxattr_fd_func) (int fd, const char *name, void *value,
37 size_t *len);
38 static int (*vxfs_removexattr_fd_func) (int fd, const char *name);
39 static int (*vxfs_listxattr_fd_func) (int fd, void *value, size_t *len);
40 static int (*vxfs_setwxattr_fd_func) (int fd);
41 static int (*vxfs_clearwxattr_fd_func) (int fd);
42 static int (*vxfs_checkwxattr_fd_func) (int fd);
44 int vxfs_setxattr_fd(int fd, const char *name, const void *value,
45 size_t len, int flags)
47 int ret = -1;
49 if (vxfs_setxattr_fd_func == NULL) {
50 errno = ENOSYS;
51 return ret;
54 DEBUG(10, ("Calling vxfs_setxattr_fd\n"));
55 ret = vxfs_setxattr_fd_func(fd, name, value, len, flags);
56 if (ret) {
57 errno = ret;
58 ret = -1;
61 return ret;
64 int vxfs_setxattr_path(const char *path, const char *name, const void *value,
65 size_t len, int flags, bool is_dir)
67 int ret, fd = -1;
69 if (is_dir) {
70 fd = open(path, O_RDONLY|O_DIRECTORY);
71 } else {
72 fd = open(path, O_WRONLY);
75 if (fd == -1) {
76 DEBUG(10, ("error in vxfs_setxattr_path: %s\n",
77 strerror(errno)));
78 return -1;
81 ret = vxfs_setxattr_fd(fd, name, value, len, flags);
83 close(fd);
85 return ret;
88 int vxfs_getxattr_fd(int fd, const char *name, void *value, size_t len)
90 int ret;
91 size_t size = len;
93 if (vxfs_getxattr_fd_func == NULL) {
94 errno = ENOSYS;
95 return -1;
98 DEBUG(10, ("Calling vxfs_getxattr_fd with %s\n", name));
99 ret = vxfs_getxattr_fd_func(fd, name, value, &size);
100 if (ret) {
101 errno = ret;
102 if (ret == EFBIG) {
103 errno = ERANGE;
105 return -1;
108 return size;
111 int vxfs_getxattr_path(const char *path, const char *name, void *value,
112 size_t len)
114 int ret, fd = -1;
116 fd = open(path, O_RDONLY);
117 if (fd == -1) {
118 DEBUG(10, ("file not opened: vxfs_getxattr_path for %s\n",
119 path));
120 return -1;
123 ret = vxfs_getxattr_fd(fd, name, value, len);
124 close(fd);
126 return ret;
129 int vxfs_removexattr_fd(int fd, const char *name)
131 int ret = 0;
133 if (vxfs_removexattr_fd_func == NULL) {
134 errno = ENOSYS;
135 return -1;
138 DEBUG(10, ("Calling vxfs_removexattr_fd with %s\n", name));
139 ret = vxfs_removexattr_fd_func(fd, name);
140 if (ret) {
141 errno = ret;
142 ret = -1;
145 return ret;
148 int vxfs_removexattr_path(const char *path, const char *name, bool is_dir)
150 int ret, fd = -1;
152 if (is_dir) {
153 fd = open(path, O_RDONLY|O_DIRECTORY);
154 } else {
155 fd = open(path, O_WRONLY);
157 if (fd == -1) {
158 DEBUG(10, ("file not opened: vxfs_removexattr_path for %s\n",
159 path));
160 return -1;
163 ret = vxfs_removexattr_fd(fd, name);
164 close(fd);
166 return ret;
169 int vxfs_listxattr_fd(int fd, char *list, size_t size)
171 int ret;
172 size_t len = size;
174 if (vxfs_listxattr_fd_func == NULL) {
175 errno = ENOSYS;
176 return -1;
179 ret = vxfs_listxattr_fd_func(fd, list, &len);
180 DEBUG(10, ("vxfs_listxattr_fd: returned ret = %d\n", ret));
181 if (ret) {
182 errno = ret;
183 if (ret == EFBIG) {
184 errno = ERANGE;
186 return -1;
189 return len;
192 int vxfs_listxattr_path(const char *path, char *list, size_t size)
194 int ret, fd = -1;
196 fd = open(path, O_RDONLY);
197 if (fd == -1) {
198 DEBUG(10, ("file not opened: vxfs_listxattr_path for %s\n",
199 path));
200 return -1;
203 ret = vxfs_listxattr_fd(fd, list, size);
204 close(fd);
206 return ret;
209 int vxfs_setwxattr_fd(int fd)
211 int ret = 0;
213 if (vxfs_setwxattr_fd_func == NULL) {
214 errno = ENOSYS;
215 return -1;
217 ret = vxfs_setwxattr_fd_func(fd);
218 DBG_DEBUG("ret = %d\n", ret);
219 if (ret != 0) {
220 errno = ret;
221 ret = -1;
224 return ret;
227 int vxfs_setwxattr_path(const char *path, bool is_dir)
229 int ret, fd = -1;
231 if (is_dir) {
232 fd = open(path, O_RDONLY|O_DIRECTORY);
233 } else {
234 fd = open(path, O_WRONLY);
236 if (fd == -1) {
237 DBG_DEBUG("file %s not opened, errno:%s\n",
238 path, strerror(errno));
239 return -1;
242 ret = vxfs_setwxattr_fd(fd);
243 DBG_DEBUG("ret = %d\n", ret);
244 close(fd);
246 return ret;
249 int vxfs_clearwxattr_fd(int fd)
251 int ret;
252 if (vxfs_clearwxattr_fd_func == NULL) {
253 errno = ENOSYS;
254 return -1;
256 ret = vxfs_clearwxattr_fd_func(fd);
257 DBG_DEBUG("ret = %d\n", ret);
258 if (ret != 0) {
259 errno = ret;
260 ret = -1;
263 return ret;
266 int vxfs_clearwxattr_path(const char *path, bool is_dir)
268 int ret, fd = -1;
270 if (is_dir) {
271 fd = open(path, O_RDONLY|O_DIRECTORY);
272 } else {
273 fd = open(path, O_WRONLY);
276 if (fd == -1) {
277 DBG_DEBUG("file %s not opened, errno:%s\n",
278 path, strerror(errno));
279 return -1;
281 ret = vxfs_clearwxattr_fd(fd);
282 DBG_DEBUG("ret = %d\n", ret);
283 close(fd);
285 return ret;
288 int vxfs_checkwxattr_fd(int fd)
290 int ret;
292 if (vxfs_checkwxattr_fd_func == NULL) {
293 errno = ENOSYS;
294 return -1;
296 ret = vxfs_checkwxattr_fd_func(fd);
297 DBG_DEBUG("ret = %d\n", ret);
298 if (ret != 0) {
299 errno = ret;
300 ret = -1;
302 return ret;
305 int vxfs_checkwxattr_path(const char *path)
307 int ret, fd = -1;
309 fd = open(path, O_RDONLY);
311 if (fd == -1) {
312 DBG_DEBUG("file %s not opened, errno:%s\n",
313 path, strerror(errno));
314 return -1;
316 ret = vxfs_checkwxattr_fd(fd);
317 close(fd);
319 return ret;
322 static bool load_lib_vxfs_function(void *lib_handle, void *fn_ptr,
323 const char *fnc_name)
325 void **vlib_handle = (void **)lib_handle;
326 void **fn_pointer = (void **)fn_ptr;
328 *fn_pointer = dlsym(*vlib_handle, fnc_name);
329 if (*fn_pointer == NULL) {
330 DEBUG(10, ("Cannot find symbol for %s\n", fnc_name));
331 return true;
334 return false;
337 void vxfs_init()
339 static void *lib_handle = NULL;
341 if (lib_handle != NULL ) {
342 return;
345 lib_handle = dlopen(LIBVXFS, RTLD_LAZY);
346 if (lib_handle == NULL) {
347 DEBUG(10, ("Cannot get lib handle\n"));
348 return;
351 DEBUG(10, ("Calling vxfs_init\n"));
352 load_lib_vxfs_function(&lib_handle, &vxfs_setxattr_fd_func,
353 "vxfs_nxattr_set");
354 load_lib_vxfs_function(&lib_handle, &vxfs_getxattr_fd_func,
355 "vxfs_nxattr_get");
356 load_lib_vxfs_function(&lib_handle, &vxfs_removexattr_fd_func,
357 "vxfs_nxattr_remove");
358 load_lib_vxfs_function(&lib_handle, &vxfs_listxattr_fd_func,
359 "vxfs_nxattr_list");
360 load_lib_vxfs_function(&lib_handle, &vxfs_setwxattr_fd_func,
361 "vxfs_wattr_set");
362 load_lib_vxfs_function(&lib_handle, &vxfs_clearwxattr_fd_func,
363 "vxfs_wattr_clear");
364 load_lib_vxfs_function(&lib_handle, &vxfs_checkwxattr_fd_func,
365 "vxfs_wattr_check");