s3 OneFS: Add debugging for createfile_flags
[samba.git] / source3 / modules / onefs_system.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Support for OneFS system interfaces.
4  *
5  * Copyright (C) Tim Prouty, 2008
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include "onefs.h"
22
23 #include <ifs/ifs_syscalls.h>
24 #include <isi_acl/isi_acl_util.h>
25
26 /*
27  * Initialize the sm_lock struct before passing it to ifs_createfile.
28  */
29 static void smlock_init(connection_struct *conn, struct sm_lock *sml,
30     bool isexe, uint32_t access_mask, uint32_t share_access,
31     uint32_t create_options)
32 {
33         sml->sm_type.doc = false;
34         sml->sm_type.isexe = isexe;
35         sml->sm_type.statonly = is_stat_open(access_mask);
36         sml->sm_type.access_mask = access_mask;
37         sml->sm_type.share_access = share_access;
38
39         /*
40          * private_options was previously used for DENY_DOS/DENY_FCB checks in
41          * the kernel, but are now properly handled by fcb_or_dos_open. In
42          * these cases, ifs_createfile will return a sharing violation, which
43          * gives fcb_or_dos_open the chance to open a duplicate file handle.
44          */
45         sml->sm_type.private_options = 0;
46
47         /* 1 second delay is handled in onefs_open.c by deferring the open */
48         sml->sm_timeout = timeval_set(0, 0);
49 }
50
51 static void smlock_dump(int debuglevel, const struct sm_lock *sml)
52 {
53         if (sml == NULL) {
54                 DEBUG(debuglevel, ("sml == NULL\n"));
55                 return;
56         }
57
58         DEBUG(debuglevel,
59               ("smlock: doc=%s, isexec=%s, statonly=%s, access_mask=0x%x, "
60                "share_access=0x%x, private_options=0x%x timeout=%d/%d\n",
61                sml->sm_type.doc ? "True" : "False",
62                sml->sm_type.isexe ? "True" : "False",
63                sml->sm_type.statonly ? "True" : "False",
64                sml->sm_type.access_mask,
65                sml->sm_type.share_access,
66                sml->sm_type.private_options,
67                (int)sml->sm_timeout.tv_sec,
68                (int)sml->sm_timeout.tv_usec));
69 }
70
71 /**
72  * External interface to ifs_createfile
73  */
74 int onefs_sys_create_file(connection_struct *conn,
75                           int base_fd,
76                           const char *path,
77                           uint32_t access_mask,
78                           uint32_t open_access_mask,
79                           uint32_t share_access,
80                           uint32_t create_options,
81                           int flags,
82                           mode_t mode,
83                           int oplock_request,
84                           uint64_t id,
85                           struct security_descriptor *sd,
86                           uint32_t dos_flags,
87                           int *granted_oplock)
88 {
89         struct sm_lock sml, *psml = NULL;
90         enum oplock_type onefs_oplock;
91         enum oplock_type onefs_granted_oplock = OPLOCK_NONE;
92         struct ifs_security_descriptor ifs_sd = {}, *pifs_sd = NULL;
93         int secinfo = 0;
94         int ret_fd = -1;
95         uint32_t onefs_dos_attributes;
96         struct ifs_createfile_flags cf_flags = CF_FLAGS_NONE;
97
98         /* Setup security descriptor and get secinfo. */
99         if (sd != NULL) {
100                 NTSTATUS status;
101
102                 secinfo = (get_sec_info(sd) & IFS_SEC_INFO_KNOWN_MASK);
103
104                 status = onefs_samba_sd_to_sd(secinfo, sd, &ifs_sd, SNUM(conn));
105
106                 if (!NT_STATUS_IS_OK(status)) {
107                         DEBUG(1, ("SD initialization failure: %s",
108                                   nt_errstr(status)));
109                         errno = EINVAL;
110                         goto out;
111                 }
112
113                 pifs_sd = &ifs_sd;
114         }
115
116         /* Stripping off private bits will be done for us. */
117         onefs_oplock = onefs_samba_oplock_to_oplock(oplock_request);
118
119         if (!lp_oplocks(SNUM(conn))) {
120                 SMB_ASSERT(onefs_oplock == OPLOCK_NONE);
121         }
122
123         /* Convert samba dos flags to UF_DOS_* attributes. */
124         onefs_dos_attributes = dos_attributes_to_stat_dos_flags(dos_flags);
125
126         /**
127          * Deal with kernel creating Default ACLs. (Isilon bug 47447.)
128          *
129          * 1) "nt acl support = no", default_acl = no
130          * 2) "inherit permissions = yes", default_acl = no
131          */
132         if (lp_nt_acl_support(SNUM(conn)) && !lp_inherit_perms(SNUM(conn)))
133                 cf_flags = cf_flags_or(cf_flags, CF_FLAGS_DEFAULT_ACL);
134
135         DEBUG(10,("onefs_sys_create_file: base_fd = %d, "
136                   "open_access_mask = 0x%x, flags = 0x%x, mode = 0%o, "
137                   "desired_oplock = %s, id = 0x%x, secinfo = 0x%x, sd = %p, "
138                   "dos_attributes = 0x%x, path = %s, "
139                   "default_acl=%s\n", base_fd,
140                   (unsigned int)open_access_mask,
141                   (unsigned int)flags,
142                   (unsigned int)mode,
143                   onefs_oplock_str(onefs_oplock),
144                   (unsigned int)id,
145                   (unsigned int)secinfo, sd,
146                   (unsigned int)onefs_dos_attributes, path,
147                   cf_flags_and_bool(cf_flags, CF_FLAGS_DEFAULT_ACL) ?
148                       "true" : "false"));
149
150         /* Initialize smlock struct for files/dirs but not internal opens */
151         if (!(oplock_request & INTERNAL_OPEN_ONLY)) {
152                 smlock_init(conn, &sml, is_executable(path), access_mask,
153                     share_access, create_options);
154                 psml = &sml;
155         }
156
157         smlock_dump(10, psml);
158
159         ret_fd = ifs_createfile(base_fd, path,
160             (enum ifs_ace_rights)open_access_mask, flags & ~O_ACCMODE, mode,
161             onefs_oplock, id, psml, secinfo, pifs_sd, onefs_dos_attributes,
162             cf_flags, &onefs_granted_oplock);
163
164         DEBUG(10,("onefs_sys_create_file(%s): ret_fd = %d, "
165                   "onefs_granted_oplock = %s\n",
166                   ret_fd < 0 ? strerror(errno) : "success", ret_fd,
167                   onefs_oplock_str(onefs_granted_oplock)));
168
169         if (granted_oplock) {
170                 *granted_oplock =
171                     onefs_oplock_to_samba_oplock(onefs_granted_oplock);
172         }
173
174  out:
175         aclu_free_sd(pifs_sd, false);
176
177         return ret_fd;
178 }
179
180 /**
181  * Only talloc the spill buffer once (reallocing when necessary).
182  */
183 static char *get_spill_buffer(size_t new_count)
184 {
185         static int cur_count = 0;
186         static char *spill_buffer = NULL;
187
188         /* If a sufficiently sized buffer exists, just return. */
189         if (new_count <= cur_count) {
190                 SMB_ASSERT(spill_buffer);
191                 return spill_buffer;
192         }
193
194         /* Allocate the first time. */
195         if (cur_count == 0) {
196                 SMB_ASSERT(!spill_buffer);
197                 spill_buffer = talloc_array(NULL, char, new_count);
198                 if (spill_buffer) {
199                         cur_count = new_count;
200                 }
201                 return spill_buffer;
202         }
203
204         /* A buffer exists, but it's not big enough, so realloc. */
205         SMB_ASSERT(spill_buffer);
206         spill_buffer = talloc_realloc(NULL, spill_buffer, char, new_count);
207         if (spill_buffer) {
208                 cur_count = new_count;
209         }
210         return spill_buffer;
211 }
212
213 /**
214  * recvfile does zero-copy writes given an fd to write to, and a socket with
215  * some data to write.  If recvfile read more than it was able to write, it
216  * spills the data into a buffer.  After first reading any additional data
217  * from the socket into the buffer, the spill buffer is then written with a
218  * standard pwrite.
219  */
220 ssize_t onefs_sys_recvfile(int fromfd, int tofd, SMB_OFF_T offset,
221                            size_t count)
222 {
223         char *spill_buffer = NULL;
224         bool socket_drained = false;
225         int ret;
226         off_t total_rbytes = 0;
227         off_t total_wbytes = 0;
228         off_t rbytes;
229         off_t wbytes;
230
231         DEBUG(10,("onefs_recvfile: from = %d, to = %d, offset=%llu, count = "
232                   "%lu\n", fromfd, tofd, offset, count));
233
234         if (count == 0) {
235                 return 0;
236         }
237
238         /*
239          * Setup up a buffer for recvfile to spill data that has been read
240          * from the socket but not written.
241          */
242         spill_buffer = get_spill_buffer(count);
243         if (spill_buffer == NULL) {
244                 ret = -1;
245                 goto out;
246         }
247
248         /*
249          * Keep trying recvfile until:
250          *  - There is no data left to read on the socket, or
251          *  - bytes read != bytes written, or
252          *  - An error is returned that isn't EINTR/EAGAIN
253          */
254         do {
255                 /* Keep track of bytes read/written for recvfile */
256                 rbytes = 0;
257                 wbytes = 0;
258
259                 DEBUG(10, ("calling recvfile loop, offset + total_wbytes = "
260                            "%llu, count - total_rbytes = %llu\n",
261                            offset + total_wbytes, count - total_rbytes));
262
263                 ret = recvfile(tofd, fromfd, offset + total_wbytes,
264                                count - total_wbytes, &rbytes, &wbytes, 0,
265                                spill_buffer);
266
267                 DEBUG(10, ("recvfile ret = %d, errno = %d, rbytes = %llu, "
268                            "wbytes = %llu\n", ret, ret >= 0 ? 0 : errno,
269                            rbytes, wbytes));
270
271                 /* Update our progress so far */
272                 total_rbytes += rbytes;
273                 total_wbytes += wbytes;
274
275         } while ((count - total_rbytes) && (rbytes == wbytes) &&
276                  (ret == -1 && (errno == EINTR || errno == EAGAIN)));
277
278         DEBUG(10, ("total_rbytes = %llu, total_wbytes = %llu\n",
279                    total_rbytes, total_wbytes));
280
281         /* Log if recvfile didn't write everything it read. */
282         if (total_rbytes != total_wbytes) {
283                 DEBUG(0, ("partial recvfile: total_rbytes=%llu but "
284                           "total_wbytes=%llu, diff = %llu\n", total_rbytes,
285                           total_wbytes, total_rbytes - total_wbytes));
286                 SMB_ASSERT(total_rbytes > total_wbytes);
287         }
288
289         /*
290          * If there is still data on the socket, read it off.
291          */
292         while (total_rbytes < count) {
293
294                 DEBUG(0, ("shallow recvfile, reading %llu\n",
295                           count - total_rbytes));
296
297                 /*
298                  * Read the remaining data into the spill buffer.  recvfile
299                  * may already have some data in the spill buffer, so start
300                  * filling the buffer at total_rbytes - total_wbytes.
301                  */
302                 ret = sys_read(fromfd,
303                                spill_buffer + (total_rbytes - total_wbytes),
304                                count - total_rbytes);
305
306                 if (ret == -1) {
307                         DEBUG(0, ("shallow recvfile read failed: %s\n",
308                                   strerror(errno)));
309                         /* Socket is dead, so treat as if it were drained. */
310                         socket_drained = true;
311                         goto out;
312                 }
313
314                 /* Data was read so update the rbytes */
315                 total_rbytes += ret;
316         }
317
318         if (total_rbytes != count) {
319                 smb_panic("Unread recvfile data still on the socket!");
320         }
321
322         /*
323          * Now write any spilled data + the extra data read off the socket.
324          */
325         while (total_wbytes < count) {
326
327                 DEBUG(0, ("partial recvfile, writing %llu\n", count - total_wbytes));
328
329                 ret = sys_pwrite(tofd, spill_buffer, count - total_wbytes,
330                                  offset + total_wbytes);
331
332                 if (ret == -1) {
333                         DEBUG(0, ("partial recvfile write failed: %s\n",
334                                   strerror(errno)));
335                         goto out;
336                 }
337
338                 /* Data was written so update the wbytes */
339                 total_wbytes += ret;
340         }
341
342         /* Success! */
343         ret = total_wbytes;
344
345 out:
346         /* Make sure we always try to drain the socket. */
347         if (!socket_drained && count - total_rbytes) {
348                 int saved_errno = errno;
349
350                 if (drain_socket(fromfd, count - total_rbytes) !=
351                     count - total_rbytes) {
352                         /* Socket is dead! */
353                         DEBUG(0, ("drain socket failed: %d\n", errno));
354                 }
355                 errno = saved_errno;
356         }
357
358         return ret;
359 }