gpfs: Move set_gpfs_lease to vfs_gpfs.c
[samba.git] / source3 / modules / gpfs.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Provide a connection to GPFS specific features
4  *  Copyright (C) Volker Lendecke 2005
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
23
24 #include <fcntl.h>
25 #include <gpfs_fcntl.h>
26 #include "vfs_gpfs.h"
27
28 static int (*gpfs_set_share_fn)(int fd, unsigned int allow, unsigned int deny);
29 static int (*gpfs_set_lease_fn)(int fd, unsigned int type);
30 static int (*gpfs_getacl_fn)(char *pathname, int flags, void *acl);
31 static int (*gpfs_putacl_fn)(char *pathname, int flags, void *acl);
32 static int (*gpfs_get_realfilename_path_fn)(char *pathname, char *filenamep,
33                                             int *len);
34 static int (*gpfs_set_winattrs_path_fn)(char *pathname, int flags,
35                                         struct gpfs_winattr *attrs);
36 static int (*gpfs_get_winattrs_path_fn)(char *pathname,
37                                         struct gpfs_winattr *attrs);
38 static int (*gpfs_get_winattrs_fn)(int fd, struct gpfs_winattr *attrs);
39 static int (*gpfs_prealloc_fn)(int fd, gpfs_off64_t start, gpfs_off64_t bytes);
40 static int (*gpfs_ftruncate_fn)(int fd, gpfs_off64_t length);
41 static int (*gpfs_lib_init_fn)(int flags);
42 static int (*gpfs_set_times_path_fn)(char *pathname, int flags,
43                                      gpfs_timestruc_t times[4]);
44 static int (*gpfs_quotactl_fn)(char *pathname, int cmd, int id, void *bufp);
45 static int (*gpfs_fcntl_fn)(int fd, void *argp);
46 static int (*gpfs_getfilesetid_fn)(char *pathname, char *name, int *idp);
47
48 int gpfswrap_init(void)
49 {
50         static void *l;
51
52         if (l != NULL) {
53                 return 0;
54         }
55
56         l = dlopen("libgpfs.so", RTLD_LAZY);
57         if (l == NULL) {
58                 return -1;
59         }
60
61         gpfs_set_share_fn             = dlsym(l, "gpfs_set_share");
62         gpfs_set_lease_fn             = dlsym(l, "gpfs_set_lease");
63         gpfs_getacl_fn                = dlsym(l, "gpfs_getacl");
64         gpfs_putacl_fn                = dlsym(l, "gpfs_putacl");
65         gpfs_get_realfilename_path_fn = dlsym(l, "gpfs_get_realfilename_path");
66         gpfs_set_winattrs_path_fn     = dlsym(l, "gpfs_set_winattrs_path");
67         gpfs_get_winattrs_path_fn     = dlsym(l, "gpfs_get_winattrs_path");
68         gpfs_get_winattrs_fn          = dlsym(l, "gpfs_get_winattrs");
69         gpfs_prealloc_fn              = dlsym(l, "gpfs_prealloc");
70         gpfs_ftruncate_fn             = dlsym(l, "gpfs_ftruncate");
71         gpfs_lib_init_fn              = dlsym(l, "gpfs_lib_init");
72         gpfs_set_times_path_fn        = dlsym(l, "gpfs_set_times_path");
73         gpfs_quotactl_fn              = dlsym(l, "gpfs_quotactl");
74         gpfs_fcntl_fn                 = dlsym(l, "gpfs_fcntl");
75         gpfs_getfilesetid_fn          = dlsym(l, "gpfs_getfilesetid");
76
77         return 0;
78 }
79
80 int gpfswrap_set_share(int fd, unsigned int allow, unsigned int deny)
81 {
82         if (gpfs_set_share_fn == NULL) {
83                 errno = ENOSYS;
84                 return -1;
85         }
86
87         return gpfs_set_share_fn(fd, allow, deny);
88 }
89
90 int gpfswrap_set_lease(int fd, unsigned int type)
91 {
92         if (gpfs_set_lease_fn == NULL) {
93                 errno = ENOSYS;
94                 return -1;
95         }
96
97         return gpfs_set_lease_fn(fd, type);
98 }
99
100 int gpfswrap_getacl(char *pathname, int flags, void *acl)
101 {
102         if (gpfs_getacl_fn == NULL) {
103                 errno = ENOSYS;
104                 return -1;
105         }
106
107         return gpfs_getacl_fn(pathname, flags, acl);
108 }
109
110 int gpfswrap_putacl(char *pathname, int flags, void *acl)
111 {
112         if (gpfs_putacl_fn == NULL) {
113                 errno = ENOSYS;
114                 return -1;
115         }
116
117         return gpfs_putacl_fn(pathname, flags, acl);
118 }
119
120 int gpfswrap_get_realfilename_path(char *pathname, char *filenamep, int *len)
121 {
122         if (gpfs_get_realfilename_path_fn == NULL) {
123                 errno = ENOSYS;
124                 return -1;
125         }
126
127         return gpfs_get_realfilename_path_fn(pathname, filenamep, len);
128 }
129
130 int gpfswrap_set_winattrs_path(char *pathname, int flags,
131                                struct gpfs_winattr *attrs)
132 {
133         if (gpfs_set_winattrs_path_fn == NULL) {
134                 errno = ENOSYS;
135                 return -1;
136         }
137
138         return gpfs_set_winattrs_path_fn(pathname, flags, attrs);
139 }
140
141 int gpfswrap_get_winattrs_path(char *pathname, struct gpfs_winattr *attrs)
142 {
143         if (gpfs_get_winattrs_path_fn == NULL) {
144                 errno = ENOSYS;
145                 return -1;
146         }
147
148         return gpfs_get_winattrs_path_fn(pathname, attrs);
149 }
150
151 int gpfswrap_get_winattrs(int fd, struct gpfs_winattr *attrs)
152 {
153         if (gpfs_get_winattrs_fn == NULL) {
154                 errno = ENOSYS;
155                 return -1;
156         }
157
158         return gpfs_get_winattrs_fn(fd, attrs);
159 }
160
161 int gpfswrap_prealloc(int fd, gpfs_off64_t start, gpfs_off64_t bytes)
162 {
163         if (gpfs_prealloc_fn == NULL) {
164                 errno = ENOSYS;
165                 return -1;
166         }
167
168         return gpfs_prealloc_fn(fd, start, bytes);
169 }
170
171 int gpfswrap_ftruncate(int fd, gpfs_off64_t length)
172 {
173         if (gpfs_ftruncate_fn == NULL) {
174                 errno = ENOSYS;
175                 return -1;
176         }
177
178         return gpfs_ftruncate_fn(fd, length);
179 }
180
181 int gpfswrap_lib_init(int flags)
182 {
183         if (gpfs_lib_init_fn == NULL) {
184                 errno = ENOSYS;
185                 return -1;
186         }
187
188         return gpfs_lib_init_fn(flags);
189 }
190
191 int gpfswrap_set_times_path(char *pathname, int flags,
192                             gpfs_timestruc_t times[4])
193 {
194         if (gpfs_set_times_path_fn == NULL) {
195                 errno = ENOSYS;
196                 return -1;
197         }
198
199         return gpfs_set_times_path_fn(pathname, flags, times);
200 }
201
202 int gpfswrap_quotactl(char *pathname, int cmd, int id, void *bufp)
203 {
204         if (gpfs_quotactl_fn == NULL) {
205                 errno = ENOSYS;
206                 return -1;
207         }
208
209         return gpfs_quotactl_fn(pathname, cmd, id, bufp);
210 }
211
212 int gpfswrap_fcntl(int fd, void *argp)
213 {
214         if (gpfs_fcntl_fn == NULL) {
215                 errno = ENOSYS;
216                 return -1;
217         }
218
219         return gpfs_fcntl_fn(fd, argp);
220 }
221
222 int gpfswrap_getfilesetid(char *pathname, char *name, int *idp)
223 {
224         if (gpfs_getfilesetid_fn == NULL) {
225                 errno = ENOSYS;
226                 return -1;
227         }
228
229         return gpfs_getfilesetid_fn(pathname, name, idp);
230 }
231
232 int get_gpfs_quota(const char *pathname, int type, int id,
233                    struct gpfs_quotaInfo *qi)
234 {
235         int ret;
236
237         ZERO_STRUCTP(qi);
238         ret = gpfswrap_quotactl(discard_const_p(char, pathname),
239                                 GPFS_QCMD(Q_GETQUOTA, type), id, qi);
240
241         if (ret) {
242                 if (errno == GPFS_E_NO_QUOTA_INST) {
243                         DEBUG(10, ("Quotas disabled on GPFS filesystem.\n"));
244                 } else if (errno != ENOSYS) {
245                         DEBUG(0, ("Get quota failed, type %d, id, %d, "
246                                   "errno %d.\n", type, id, errno));
247                 }
248
249                 return ret;
250         }
251
252         DEBUG(10, ("quota type %d, id %d, blk u:%lld h:%lld s:%lld gt:%u\n",
253                    type, id, qi->blockUsage, qi->blockHardLimit,
254                    qi->blockSoftLimit, qi->blockGraceTime));
255
256         return ret;
257 }
258
259 int get_gpfs_fset_id(const char *pathname, int *fset_id)
260 {
261         int err, fd, errno_fcntl;
262
263         struct {
264                 gpfsFcntlHeader_t hdr;
265                 gpfsGetFilesetName_t fsn;
266         } arg;
267
268         arg.hdr.totalLength = sizeof(arg);
269         arg.hdr.fcntlVersion = GPFS_FCNTL_CURRENT_VERSION;
270         arg.hdr.fcntlReserved = 0;
271         arg.fsn.structLen = sizeof(arg.fsn);
272         arg.fsn.structType = GPFS_FCNTL_GET_FILESETNAME;
273
274         fd = open(pathname, O_RDONLY);
275         if (fd == -1) {
276                 DEBUG(1, ("Could not open %s: %s\n",
277                           pathname, strerror(errno)));
278                 return fd;
279         }
280
281         err = gpfswrap_fcntl(fd, &arg);
282         errno_fcntl = errno;
283         close(fd);
284
285         if (err) {
286                 errno = errno_fcntl;
287                 if (errno != ENOSYS) {
288                         DEBUG(1, ("GPFS_FCNTL_GET_FILESETNAME for %s failed: "
289                                   "%s\n", pathname, strerror(errno)));
290                 }
291                 return err;
292         }
293
294         err = gpfswrap_getfilesetid(discard_const_p(char, pathname),
295                                     arg.fsn.buffer, fset_id);
296         if (err && errno != ENOSYS) {
297                 DEBUG(1, ("gpfs_getfilesetid for %s failed: %s\n",
298                           pathname, strerror(errno)));
299         }
300         return err;
301 }
302
303 static void timespec_to_gpfs_time(struct timespec ts, gpfs_timestruc_t *gt,
304                                   int idx, int *flags)
305 {
306         if (!null_timespec(ts)) {
307                 *flags |= 1 << idx;
308                 gt[idx].tv_sec = ts.tv_sec;
309                 gt[idx].tv_nsec = ts.tv_nsec;
310                 DEBUG(10, ("Setting GPFS time %d, flags 0x%x\n", idx, *flags));
311         }
312 }
313
314 int smbd_gpfs_set_times_path(char *path, struct smb_file_time *ft)
315 {
316         gpfs_timestruc_t gpfs_times[4];
317         int flags = 0;
318         int rc;
319
320         ZERO_ARRAY(gpfs_times);
321         timespec_to_gpfs_time(ft->atime, gpfs_times, 0, &flags);
322         timespec_to_gpfs_time(ft->mtime, gpfs_times, 1, &flags);
323         /* No good mapping from LastChangeTime to ctime, not storing */
324         timespec_to_gpfs_time(ft->create_time, gpfs_times, 3, &flags);
325
326         if (!flags) {
327                 DEBUG(10, ("nothing to do, return to avoid EINVAL\n"));
328                 return 0;
329         }
330
331         rc = gpfswrap_set_times_path(path, flags, gpfs_times);
332
333         if (rc != 0 && errno != ENOSYS) {
334                 DEBUG(1,("gpfs_set_times() returned with error %s\n",
335                         strerror(errno)));
336         }
337
338         return rc;
339 }