s3: Add OneFS alternate data streams implementation
[samba.git] / source3 / modules / onefs_open.c
1 /*
2  * Unix SMB/CIFS implementation.
3  *
4  * This file began with some code from source3/smbd/open.c and has been
5  * modified it work with ifs_createfile.
6  *
7  * ifs_createfile is a CIFS-specific syscall for opening/files and
8  * directories.  It adds support for:
9  *    - Full in-kernel access checks using a windows access_mask
10  *    - Cluster-coherent share mode locks
11  *    - Cluster-coherent oplocks
12  *    - Streams
13  *    - Setting security descriptors at create time
14  *    - Setting dos_attributes at create time
15  *
16  * Copyright (C) Andrew Tridgell 1992-1998
17  * Copyright (C) Jeremy Allison 2001-2004
18  * Copyright (C) Volker Lendecke 2005
19  * Copyright (C) Tim Prouty, 2008
20  *
21  * This program is free software; you can redistribute it and/or modify
22  * it under the terms of the GNU General Public License as published by
23  * the Free Software Foundation; either version 3 of the License, or
24  * (at your option) any later version.
25  *
26  * This program is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  * GNU General Public License for more details.
30  *
31  * You should have received a copy of the GNU General Public License
32  * along with this program; if not, see <http://www.gnu.org/licenses/>.
33  */
34
35 #include "onefs.h"
36
37 extern const struct generic_mapping file_generic_mapping;
38 extern bool global_client_failed_oplock_break;
39
40 struct deferred_open_record {
41         bool delayed_for_oplocks;
42         bool failed; /* added for onefs_oplocks */
43         struct file_id id;
44 };
45
46 static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
47                               struct smb_request *req,
48                               const char *fname,
49                               uint32_t access_mask,
50                               uint32_t share_access,
51                               uint32_t create_disposition,
52                               uint32_t create_options,
53                               uint32_t file_attributes,
54                               uint32_t oplock_request,
55                               uint64_t allocation_size,
56                               struct security_descriptor *sd,
57                               struct ea_list *ea_list,
58
59                               files_struct **result,
60                               int *pinfo,
61                               SMB_STRUCT_STAT *psbuf);
62
63 /****************************************************************************
64  Open a file.
65 ****************************************************************************/
66
67 static NTSTATUS onefs_open_file(files_struct *fsp,
68                                 connection_struct *conn,
69                                 struct smb_request *req,
70                                 const char *parent_dir,
71                                 const char *name,
72                                 const char *path,
73                                 SMB_STRUCT_STAT *psbuf,
74                                 int flags,
75                                 mode_t unx_mode,
76                                 uint32 access_mask,
77                                 uint32 open_access_mask,
78                                 int oplock_request,
79                                 uint64 id,
80                                 uint32 share_access,
81                                 uint32 create_options,
82                                 uint32_t new_dos_attributes,
83                                 struct security_descriptor *sd,
84                                 int *granted_oplock)
85 {
86         NTSTATUS status = NT_STATUS_OK;
87         int accmode = (flags & O_ACCMODE);
88         int local_flags = flags;
89         bool file_existed = VALID_STAT(*psbuf);
90         const char *wild;
91         char *base = NULL;
92         char *stream = NULL;
93         int base_fd = -1;
94
95         fsp->fh->fd = -1;
96         errno = EPERM;
97
98         /* Check permissions */
99
100         /*
101          * This code was changed after seeing a client open request
102          * containing the open mode of (DENY_WRITE/read-only) with
103          * the 'create if not exist' bit set. The previous code
104          * would fail to open the file read only on a read-only share
105          * as it was checking the flags parameter  directly against O_RDONLY,
106          * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
107          * JRA.
108          */
109
110         if (!CAN_WRITE(conn)) {
111                 /* It's a read-only share - fail if we wanted to write. */
112                 if(accmode != O_RDONLY) {
113                         DEBUG(3,("Permission denied opening %s\n", path));
114                         return NT_STATUS_ACCESS_DENIED;
115                 } else if(flags & O_CREAT) {
116                         /* We don't want to write - but we must make sure that
117                            O_CREAT doesn't create the file if we have write
118                            access into the directory.
119                         */
120                         flags &= ~O_CREAT;
121                         local_flags &= ~O_CREAT;
122                 }
123         }
124
125         /*
126          * This little piece of insanity is inspired by the
127          * fact that an NT client can open a file for O_RDONLY,
128          * but set the create disposition to FILE_EXISTS_TRUNCATE.
129          * If the client *can* write to the file, then it expects to
130          * truncate the file, even though it is opening for readonly.
131          * Quicken uses this stupid trick in backup file creation...
132          * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
133          * for helping track this one down. It didn't bite us in 2.0.x
134          * as we always opened files read-write in that release. JRA.
135          */
136
137         if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
138                 DEBUG(10,("onefs_open_file: truncate requested on read-only "
139                           "open for file %s\n", path));
140                 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
141         }
142
143 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
144         /*
145          * We would block on opening a FIFO with no one else on the
146          * other end. Do what we used to do and add O_NONBLOCK to the
147          * open flags. JRA.
148          */
149
150         if (file_existed && S_ISFIFO(psbuf->st_mode)) {
151                 local_flags |= O_NONBLOCK;
152         }
153 #endif
154
155         /* Don't create files with Microsoft wildcard characters. */
156         if (fsp->base_fsp) {
157                 /*
158                  * wildcard characters are allowed in stream names
159                  * only test the basefilename
160                  */
161                 wild = fsp->base_fsp->fsp_name;
162         } else {
163                 wild = path;
164         }
165         if ((local_flags & O_CREAT) && !file_existed &&
166             ms_has_wild(wild))  {
167                 /*
168                  * XXX: may need to remvoe this return...
169                  *
170                  * We dont think this check needs to exist. All it does is
171                  * block creating files with Microsoft wildcards, which is
172                  * fine if the creation originated from NFS or locally and
173                  * then was copied via Samba.
174                  */
175                 DEBUG(1, ("onefs_open_file: creating file with wildcard: %s\n",
176                           path));
177                 return NT_STATUS_OBJECT_NAME_INVALID;
178         }
179
180         /* Actually do the open */
181
182 #ifdef O_NOFOLLOW
183         /*
184          * Never follow symlinks on a POSIX client. The
185          * client should be doing this.
186          */
187
188         if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
189                 flags |= O_NOFOLLOW;
190         }
191 #endif
192         /* Don't request an oplock if oplocks are turned off for the
193          * share. */
194         if (!lp_oplocks(SNUM(conn)))
195                 oplock_request = 0;
196
197         /* Stream handling */
198         if (is_ntfs_stream_name(path)) {
199                 status = onefs_split_ntfs_stream_name(talloc_tos(), path,
200                                                       &base, &stream);
201         }
202         /* It's a stream, so pass in the base_fd */
203         if (stream != NULL) {
204                 SMB_ASSERT(fsp->base_fsp);
205
206                 DEBUG(10,("Opening a stream: base=%s(%d), stream=%s",
207                           base, fsp->base_fsp->fh->fd, stream));
208
209                 base_fd = fsp->base_fsp->fh->fd;
210         }
211
212         fsp->fh->fd = onefs_sys_create_file(conn,
213                                             base_fd,
214                                             stream != NULL ? stream :
215                                             (base != NULL ? base : path),
216                                             access_mask,
217                                             open_access_mask,
218                                             share_access,
219                                             create_options,
220                                             flags,
221                                             unx_mode,
222                                             oplock_request,
223                                             id,
224                                             sd,
225                                             new_dos_attributes,
226                                             granted_oplock);
227
228         if (fsp->fh->fd == -1) {
229                 if (errno == EMFILE) {
230                         static time_t last_warned = 0L;
231
232                         if (time((time_t *) NULL) > last_warned) {
233                                 DEBUG(0, ("Too many open files, unable "
234                                           "to open more!  smbd's max "
235                                           "open files = %d, also check "
236                                           "sysctl kern.maxfiles and "
237                                           "sysctl kern.maxfilesperproc\n",
238                                           lp_max_open_files()));
239                                 last_warned = time((time_t *) NULL);
240                         }
241                 }
242
243                 status = map_nt_error_from_unix(errno);
244                 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
245                           "(flags=%d)\n",
246                           path,nt_errstr(status),local_flags,flags));
247                 return status;
248         }
249
250         if ((local_flags & O_CREAT) && !file_existed) {
251
252                 /* Inherit the ACL if required */
253                 if (lp_inherit_perms(SNUM(conn))) {
254                         inherit_access_posix_acl(conn, parent_dir, path,
255                             unx_mode);
256                 }
257
258                 /* Change the owner if required. */
259                 if (lp_inherit_owner(SNUM(conn))) {
260                         change_file_owner_to_parent(conn, parent_dir,
261                             fsp);
262                 }
263
264                 notify_fname(conn, NOTIFY_ACTION_ADDED,
265                     FILE_NOTIFY_CHANGE_FILE_NAME, path);
266         }
267
268         if (!file_existed) {
269                 int ret;
270
271                 if (fsp->fh->fd == -1) {
272                         ret = SMB_VFS_STAT(conn, path, psbuf);
273                 } else {
274                         ret = SMB_VFS_FSTAT(fsp, psbuf);
275                         /* If we have an fd, this stat should succeed. */
276                         if (ret == -1) {
277                                 DEBUG(0,("Error doing fstat on open file %s "
278                                          "(%s)\n", path,strerror(errno) ));
279                         }
280                 }
281
282                 /* For a non-io open, this stat failing means file not found. JRA */
283                 if (ret == -1) {
284                         status = map_nt_error_from_unix(errno);
285                         fd_close(fsp);
286                         return status;
287                 }
288         }
289
290         /*
291          * POSIX allows read-only opens of directories. We don't
292          * want to do this (we use a different code path for this)
293          * so catch a directory open and return an EISDIR. JRA.
294          */
295
296         if(S_ISDIR(psbuf->st_mode)) {
297                 fd_close(fsp);
298                 errno = EISDIR;
299                 return NT_STATUS_FILE_IS_A_DIRECTORY;
300         }
301
302         fsp->mode = psbuf->st_mode;
303         fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
304         fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
305         fsp->file_pid = req ? req->smbpid : 0;
306         fsp->can_lock = True;
307         fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
308         if (!CAN_WRITE(conn)) {
309                 fsp->can_write = False;
310         } else {
311                 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
312                         True : False;
313         }
314         fsp->print_file = False;
315         fsp->modified = False;
316         fsp->sent_oplock_break = NO_BREAK_SENT;
317         fsp->is_directory = False;
318         if (conn->aio_write_behind_list &&
319             is_in_path(path, conn->aio_write_behind_list, conn->case_sensitive)) {
320                 fsp->aio_write_behind = True;
321         }
322
323         string_set(&fsp->fsp_name, path);
324         fsp->wcp = NULL; /* Write cache pointer. */
325
326         DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
327                  conn->server_info->unix_name,
328                  fsp->fsp_name,
329                  BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
330                  conn->num_files_open));
331
332         errno = 0;
333         return NT_STATUS_OK;
334 }
335
336 /****************************************************************************
337  Handle the 1 second delay in returning a SHARING_VIOLATION error.
338 ****************************************************************************/
339
340 static void defer_open(struct share_mode_lock *lck,
341                        struct timeval request_time,
342                        struct timeval timeout,
343                        struct smb_request *req,
344                        struct deferred_open_record *state)
345 {
346         int i;
347
348         /* Paranoia check */
349
350         for (i=0; i<lck->num_share_modes; i++) {
351                 struct share_mode_entry *e = &lck->share_modes[i];
352
353                 if (!is_deferred_open_entry(e)) {
354                         continue;
355                 }
356
357                 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
358                         DEBUG(0, ("Trying to defer an already deferred "
359                                   "request: mid=%d, exiting\n", req->mid));
360                         exit_server("attempt to defer a deferred request");
361                 }
362         }
363
364         /* End paranoia check */
365
366         DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
367                   "open entry for mid %u\n",
368                   (unsigned int)request_time.tv_sec,
369                   (unsigned int)request_time.tv_usec,
370                   (unsigned int)req->mid));
371
372         if (!push_deferred_smb_message(req, request_time, timeout,
373                                        (char *)state, sizeof(*state))) {
374                 exit_server("push_deferred_smb_message failed");
375         }
376         add_deferred_open(lck, req->mid, request_time, state->id);
377
378         /*
379          * Push the MID of this packet on the signing queue.
380          * We only do this once, the first time we push the packet
381          * onto the deferred open queue, as this has a side effect
382          * of incrementing the response sequence number.
383          */
384
385         srv_defer_sign_response(req->mid);
386 }
387
388 static void schedule_defer_open(struct share_mode_lock *lck,
389                                 struct timeval request_time,
390                                 struct smb_request *req)
391 {
392         struct deferred_open_record state;
393
394         /* This is a relative time, added to the absolute
395            request_time value to get the absolute timeout time.
396            Note that if this is the second or greater time we enter
397            this codepath for this particular request mid then
398            request_time is left as the absolute time of the *first*
399            time this request mid was processed. This is what allows
400            the request to eventually time out. */
401
402         struct timeval timeout;
403
404         /* Normally the smbd we asked should respond within
405          * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
406          * the client did, give twice the timeout as a safety
407          * measure here in case the other smbd is stuck
408          * somewhere else. */
409
410         timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
411
412         /* Nothing actually uses state.delayed_for_oplocks
413            but it's handy to differentiate in debug messages
414            between a 30 second delay due to oplock break, and
415            a 1 second delay for share mode conflicts. */
416
417         state.delayed_for_oplocks = True;
418         state.failed = False;
419         state.id = lck->id;
420
421         if (!request_timed_out(request_time, timeout)) {
422                 defer_open(lck, request_time, timeout, req, &state);
423         }
424 }
425
426 /****************************************************************************
427  Open a file with a share mode.  Passed in an already created files_struct.
428 ****************************************************************************/
429 NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
430                                   struct smb_request *req,
431                                   const char *fname,
432                                   uint32 access_mask,
433                                   uint32 share_access,
434                                   uint32 create_disposition,
435                                   uint32 create_options,
436                                   uint32 new_dos_attributes,
437                                   int oplock_request,
438                                   struct security_descriptor *sd,
439                                   files_struct *fsp,
440                                   int *pinfo,
441                                   SMB_STRUCT_STAT *psbuf)
442 {
443         int flags=0;
444         int flags2=0;
445         bool file_existed = VALID_STAT(*psbuf);
446         bool def_acl = False;
447         bool posix_open = False;
448         bool new_file_created = False;
449         bool clear_ads = False;
450         struct file_id id;
451         mode_t new_unx_mode = (mode_t)0;
452         mode_t unx_mode = (mode_t)0;
453         int info;
454         uint32 existing_dos_attributes = 0;
455         struct pending_message_list *pml = NULL;
456         struct timeval request_time = timeval_zero();
457         struct share_mode_lock *lck = NULL;
458         uint32 open_access_mask = access_mask;
459         NTSTATUS status;
460         int ret_flock;
461         char *parent_dir;
462         const char *newname;
463         int granted_oplock;
464         uint64 oplock_waiter;
465         uint32 createfile_attributes = 0;
466
467         ZERO_STRUCT(id);
468
469         if (conn->printer) {
470                 /*
471                  * Printers are handled completely differently.
472                  * Most of the passed parameters are ignored.
473                  */
474
475                 if (pinfo) {
476                         *pinfo = FILE_WAS_CREATED;
477                 }
478
479                 DEBUG(10, ("onefs_open_file_ntcreate: printer open fname=%s\n",
480                           fname));
481
482                 return print_fsp_open(req, conn, fname, req->vuid, fsp, psbuf);
483         }
484
485         if (!parent_dirname(talloc_tos(), fname, &parent_dir, &newname)) {
486                 return NT_STATUS_NO_MEMORY;
487         }
488
489         if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
490                 posix_open = True;
491                 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
492                 new_dos_attributes = 0;
493         } else {
494                 /* We add aARCH to this as this mode is only used if the file is
495                  * created new. */
496                 unx_mode = unix_mode(conn, new_dos_attributes | aARCH, fname,
497                                      parent_dir);
498         }
499
500         DEBUG(10,("onefs_open_file_ntcreate: fname=%s, dos_attrs=0x%x "
501                   "access_mask=0x%x share_access=0x%x "
502                   "create_disposition = 0x%x create_options=0x%x "
503                   "unix mode=0%o oplock_request=0x%x\n",
504                   fname, new_dos_attributes, access_mask, share_access,
505                   create_disposition, create_options, unx_mode,
506                   oplock_request));
507
508         if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
509                 DEBUG(0, ("No smb request but not an internal only open!\n"));
510                 return NT_STATUS_INTERNAL_ERROR;
511         }
512
513         /*
514          * Only non-internal opens can be deferred at all
515          */
516
517         if ((req != NULL)
518             && ((pml = get_open_deferred_message(req->mid)) != NULL)) {
519                 struct deferred_open_record *state =
520                         (struct deferred_open_record *)pml->private_data.data;
521
522                 /* Remember the absolute time of the original
523                    request with this mid. We'll use it later to
524                    see if this has timed out. */
525
526                 request_time = pml->request_time;
527
528                 /* Remove the deferred open entry under lock. */
529                 lck = get_share_mode_lock(talloc_tos(), state->id, NULL, NULL,
530                                           NULL);
531                 if (lck == NULL) {
532                         DEBUG(0, ("could not get share mode lock\n"));
533                 } else {
534                         del_deferred_open_entry(lck, req->mid);
535                         TALLOC_FREE(lck);
536                 }
537
538                 /* Ensure we don't reprocess this message. */
539                 remove_deferred_open_smb_message(req->mid);
540
541                 /*
542                  * When receiving a semlock_async_failure message, the
543                  * deferred open will be marked as "failed". Returning
544                  * INTERNAL_ERROR.
545                  */
546                 if (state->failed) {
547                         DEBUG(0, ("onefs_open_file_ntcreate: "
548                                   "semlock_async_failure detected!\n"));
549                         return NT_STATUS_INTERNAL_ERROR;
550                 }
551         }
552
553         status = check_name(conn, fname);
554         if (!NT_STATUS_IS_OK(status)) {
555                 return status;
556         }
557
558         if (!posix_open) {
559                 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
560                 if (file_existed) {
561                         existing_dos_attributes = dos_mode(conn, fname, psbuf);
562                 }
563         }
564
565         /* Setup dos_attributes to be set by ifs_createfile */
566         if (lp_store_dos_attributes(SNUM(conn))) {
567                 createfile_attributes = (new_dos_attributes | aARCH) &
568                     ~(FILE_ATTRIBUTE_NONINDEXED | FILE_ATTRIBUTE_COMPRESSED);
569         }
570
571         /* Ignore oplock requests if oplocks are disabled. */
572         if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break ||
573             IS_VETO_OPLOCK_PATH(conn, fname)) {
574                 /* Mask off everything except the private Samba bits. */
575                 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
576         }
577
578         /* this is for OS/2 long file names - say we don't support them */
579         if (!lp_posix_pathnames() && strstr(fname,".+,;=[].")) {
580                 /* OS/2 Workplace shell fix may be main code stream in a later
581                  * release. */
582                 DEBUG(5,("onefs_open_file_ntcreate: OS/2 long filenames are "
583                           "not supported.\n"));
584                 if (use_nt_status()) {
585                         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
586                 }
587                 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
588         }
589
590         switch( create_disposition ) {
591                 /*
592                  * Currently we're using FILE_SUPERSEDE as the same as
593                  * FILE_OVERWRITE_IF but they really are
594                  * different. FILE_SUPERSEDE deletes an existing file
595                  * (requiring delete access) then recreates it.
596                  */
597                 case FILE_SUPERSEDE:
598                         /**
599                          * @todo: Clear all file attributes?
600                          * http://www.osronline.com/article.cfm?article=302
601                          * create if not exist, trunc if exist
602                          *
603                          * If file exists replace/overwrite. If file doesn't
604                          * exist create.
605                          */
606                         flags2 |= (O_CREAT | O_TRUNC);
607                         clear_ads = true;
608                         break;
609
610                 case FILE_OVERWRITE_IF:
611                         /* If file exists replace/overwrite. If file doesn't
612                          * exist create. */
613                         flags2 |= (O_CREAT | O_TRUNC);
614                         clear_ads = true;
615                         break;
616
617                 case FILE_OPEN:
618                         /* If file exists open. If file doesn't exist error. */
619                         if (!file_existed) {
620                                 DEBUG(5,("onefs_open_file_ntcreate: FILE_OPEN "
621                                          "requested for file %s and file "
622                                          "doesn't exist.\n", fname ));
623                                 errno = ENOENT;
624                                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
625                         }
626                         break;
627
628                 case FILE_OVERWRITE:
629                         /* If file exists overwrite. If file doesn't exist
630                          * error. */
631                         if (!file_existed) {
632                                 DEBUG(5, ("onefs_open_file_ntcreate: "
633                                           "FILE_OVERWRITE requested for file "
634                                           "%s and file doesn't exist.\n",
635                                           fname));
636                                 errno = ENOENT;
637                                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
638                         }
639                         flags2 |= O_TRUNC;
640                         clear_ads = true;
641                         break;
642
643                 case FILE_CREATE:
644                         /* If file exists error. If file doesn't exist
645                          * create. */
646                         if (file_existed) {
647                                 DEBUG(5, ("onefs_open_file_ntcreate: "
648                                           "FILE_CREATE requested for file %s "
649                                           "and file already exists.\n",
650                                           fname));
651                                 if (S_ISDIR(psbuf->st_mode)) {
652                                         errno = EISDIR;
653                                 } else {
654                                         errno = EEXIST;
655                                 }
656                                 return map_nt_error_from_unix(errno);
657                         }
658                         flags2 |= (O_CREAT|O_EXCL);
659                         break;
660
661                 case FILE_OPEN_IF:
662                         /* If file exists open. If file doesn't exist
663                          * create. */
664                         flags2 |= O_CREAT;
665                         break;
666
667                 default:
668                         return NT_STATUS_INVALID_PARAMETER;
669         }
670
671         /* Match attributes on file exists and overwrite. */
672         if (!posix_open && file_existed &&
673             ((create_disposition == FILE_OVERWRITE) ||
674                 (create_disposition == FILE_OVERWRITE_IF))) {
675                 if (!open_match_attributes(conn, fname,
676                                            existing_dos_attributes,
677                                            new_dos_attributes, psbuf->st_mode,
678                                            unx_mode, &new_unx_mode)) {
679                         DEBUG(5, ("onefs_open_file_ntcreate: attributes "
680                                   "missmatch for file %s (%x %x) (0%o, 0%o)\n",
681                                   fname, existing_dos_attributes,
682                                   new_dos_attributes,
683                                   (unsigned int)psbuf->st_mode,
684                                   (unsigned int)unx_mode ));
685                         errno = EACCES;
686                         return NT_STATUS_ACCESS_DENIED;
687                 }
688         }
689
690         /*
691          * OneFS understands MAXIMUM_ALLOWED_ACCESS, so only hack the
692          * access_mask, but leave the MAA for the actual open in
693          * open_access_mask.
694          */
695         open_access_mask = access_mask;
696         if (open_access_mask & MAXIMUM_ALLOWED_ACCESS) {
697                 access_mask |= FILE_GENERIC_ALL;
698         }
699
700         /* Convert GENERIC bits to specific bits. */
701         se_map_generic(&access_mask, &file_generic_mapping);
702         se_map_generic(&open_access_mask, &file_generic_mapping);
703
704         if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
705                 /* This will cause oplock breaks. */
706                 open_access_mask |= FILE_WRITE_DATA;
707         }
708
709         DEBUG(10, ("onefs_open_file_ntcreate: fname=%s, after mapping "
710                    "open_access_mask=%#x, access_mask=0x%x\n",
711                    fname, open_access_mask, access_mask));
712
713         /*
714          * Note that we ignore the append flag as append does not
715          * mean the same thing under DOS and Unix.
716          */
717
718         if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
719             (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
720
721                 /*
722                  * DENY_DOS opens are always underlying read-write on the
723                  * file handle, no matter what the requested access mask
724                  * says. Stock samba just sets the flags, but since
725                  * ifs_createfile uses the access_mask, it must be updated as
726                  * well.  This allows BASE-DENY* to pass.
727                  */
728                 if (create_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) {
729
730                         DEBUG(10,("onefs_open_file_ntcreate: deny_dos: "
731                                   "Adding O_RDWR to flags "
732                                   "(0x%x) and some READ bits to "
733                                   "open_access_mask (0x%x)\n",
734                                   flags, open_access_mask));
735
736                         flags = O_RDWR;
737                         open_access_mask |= (FILE_READ_ATTRIBUTES |
738                             FILE_READ_DATA | FILE_READ_EA | FILE_EXECUTE);
739
740                 } else if (access_mask & (FILE_READ_ATTRIBUTES |
741                                FILE_READ_DATA |
742                                FILE_READ_EA |
743                                FILE_EXECUTE)) {
744                         flags = O_RDWR;
745                 } else {
746                         flags = O_WRONLY;
747                 }
748         } else {
749                 flags = O_RDONLY;
750         }
751
752         /* Currently we only look at FILE_WRITE_THROUGH for create options. */
753 #if defined(O_SYNC)
754         if ((create_options & FILE_WRITE_THROUGH) &&
755             lp_strict_sync(SNUM(conn))) {
756                 flags2 |= O_SYNC;
757         }
758 #endif /* O_SYNC */
759
760         if (posix_open && (access_mask & FILE_APPEND_DATA)) {
761                 flags2 |= O_APPEND;
762         }
763
764         if (!posix_open && !CAN_WRITE(conn)) {
765                 /*
766                  * We should really return a permission denied error if either
767                  * O_CREAT or O_TRUNC are set, but for compatibility with
768                  * older versions of Samba we just AND them out.
769                  */
770                 flags2 &= ~(O_CREAT|O_TRUNC);
771
772                 /* Deny DELETE_ACCESS explicitly if the share is read only. */
773                 if (access_mask & DELETE_ACCESS) {
774                         return map_nt_error_from_unix(EACCES);
775                 }
776         }
777
778         /* Ensure we can't write on a read-only share or file. */
779         if (flags != O_RDONLY && file_existed &&
780             (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
781                 DEBUG(5, ("onefs_open_file_ntcreate: write access requested "
782                           "for file %s on read only %s\n",
783                           fname, !CAN_WRITE(conn) ? "share" : "file" ));
784                 errno = EACCES;
785                 return NT_STATUS_ACCESS_DENIED;
786         }
787
788         DEBUG(10, ("fsp = %p\n", fsp));
789
790         fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
791         fsp->share_access = share_access;
792         fsp->fh->private_options = create_options;
793         fsp->access_mask = open_access_mask; /* We change this to the
794                                               * requested access_mask after
795                                               * the open is done. */
796         fsp->posix_open = posix_open;
797
798         /* Ensure no SAMBA_PRIVATE bits can be set. */
799         fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
800
801         if (timeval_is_zero(&request_time)) {
802                 request_time = fsp->open_time;
803         }
804
805         if (file_existed) {
806                 struct timespec old_write_time = get_mtimespec(psbuf);
807                 id = vfs_file_id_from_sbuf(conn, psbuf);
808
809                 lck = get_share_mode_lock(talloc_tos(), id,
810                                           conn->connectpath,
811                                           fname, &old_write_time);
812
813                 if (lck == NULL) {
814                         DEBUG(0, ("Could not get share mode lock\n"));
815                         return NT_STATUS_SHARING_VIOLATION;
816                 }
817
818                 if (lck->delete_on_close) {
819                         /* DELETE_PENDING is not deferred for a second */
820                         TALLOC_FREE(lck);
821                         return NT_STATUS_DELETE_PENDING;
822                 }
823         }
824
825         SMB_ASSERT(!file_existed || (lck != NULL));
826
827         /*
828          * Ensure we pay attention to default ACLs on directories.  May be
829          * neccessary depending on ACL policies.
830          */
831         if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
832             (def_acl = directory_has_default_acl(conn, parent_dir))) {
833                 unx_mode = 0777;
834         }
835
836         DEBUG(4,("calling onefs_open_file with flags=0x%X flags2=0x%X "
837                  "mode=0%o, access_mask = 0x%x, open_access_mask = 0x%x\n",
838                  (unsigned int)flags, (unsigned int)flags2,
839                  (unsigned int)unx_mode, (unsigned int)access_mask,
840                  (unsigned int)open_access_mask));
841
842         oplock_waiter = 1; //ifs_oplock_wait_record(mid);
843
844         if (oplock_waiter == 0) {
845                 return NT_STATUS_NO_MEMORY;
846         }
847
848         /* Do the open. */
849         status = onefs_open_file(fsp,
850                                  conn,
851                                  req,
852                                  parent_dir,
853                                  newname,
854                                  fname,
855                                  psbuf,
856                                  flags|flags2,
857                                  unx_mode,
858                                  access_mask,
859                                  open_access_mask,
860                                  fsp->oplock_type,
861                                  oplock_waiter,
862                                  share_access,
863                                  create_options,
864                                  createfile_attributes,
865                                  sd,
866                                  &granted_oplock);
867
868         if (!NT_STATUS_IS_OK(status)) {
869
870                 /* OneFS Oplock Handling */
871                 if (errno == EINPROGRESS) {
872
873                         if (lck == NULL) {
874
875                                 struct deferred_open_record state;
876                                 struct timespec old_write_time;
877
878                                 old_write_time = get_mtimespec(psbuf);
879
880                                 DEBUG(3, ("Someone created file %s with an "
881                                           "oplock after we looked: Retrying\n",
882                                           fname));
883                                 /*
884                                  * We hit the race that when we did the stat
885                                  * on the file it did not exist, and someone
886                                  * has created it in between the stat and the
887                                  * open_file() call. Just retry immediately.
888                                  */
889                                 id = vfs_file_id_from_sbuf(conn, psbuf);
890                                 if (!(lck = get_share_mode_lock(talloc_tos(),
891                                           id, conn->connectpath, fname,
892                                           &old_write_time))) {
893                                         /*
894                                          * Emergency exit
895                                          */
896                                         DEBUG(0, ("onefs_open_file_ntcreate: "
897                                                   "Could not get share mode "
898                                                   "lock for %s\n", fname));
899                                         status = NT_STATUS_SHARING_VIOLATION;
900                                         goto cleanup_destroy;
901                                 }
902
903                                 state.delayed_for_oplocks = False;
904                                 state.id = id;
905
906                                 if (req != NULL) {
907                                         defer_open(lck, request_time,
908                                             timeval_zero(), req, &state);
909                                 }
910                                 goto cleanup_destroy;
911                         }
912                         /* Waiting for an oplock */
913                         SMB_ASSERT(req);
914                         schedule_defer_open(lck, request_time, req);
915                         goto cleanup;
916                 }
917
918                 /* Check for a sharing violation */
919                 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
920                         uint32 can_access_mask;
921                         bool can_access = True;
922
923                         /* Check if this can be done with the deny_dos and fcb
924                          * calls. */
925
926                         /* Try to find dup fsp if possible. */
927                         if (create_options &
928                             (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
929                              NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
930
931                                 if (req == NULL) {
932                                         DEBUG(0, ("DOS open without an SMB "
933                                                   "request!\n"));
934                                         status = NT_STATUS_INTERNAL_ERROR;
935                                         goto cleanup_destroy;
936                                 }
937
938                                 /* Use the client requested access mask here,
939                                  * not the one we open with. */
940                                 status = fcb_or_dos_open(req,
941                                                         conn,
942                                                         fsp,
943                                                         fname,
944                                                         id,
945                                                         req->smbpid,
946                                                         req->vuid,
947                                                         access_mask,
948                                                         share_access,
949                                                         create_options);
950
951                                 if (NT_STATUS_IS_OK(status)) {
952                                         TALLOC_FREE(lck);
953                                         if (pinfo) {
954                                                 *pinfo = FILE_WAS_OPENED;
955                                         }
956                                         status =  NT_STATUS_OK;
957                                         goto cleanup;
958                                 }
959                         }
960
961                         /*
962                          * This next line is a subtlety we need for
963                          * MS-Access. If a file open will fail due to share
964                          * permissions and also for security (access) reasons,
965                          * we need to return the access failed error, not the
966                          * share error. We can't open the file due to kernel
967                          * oplock deadlock (it's possible we failed above on
968                          * the open_mode_check()) so use a userspace check.
969                          */
970
971                         if (flags & O_RDWR) {
972                                 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
973                         } else if (flags & O_WRONLY) {
974                                 can_access_mask = FILE_WRITE_DATA;
975                         } else {
976                                 can_access_mask = FILE_READ_DATA;
977                         }
978
979                         if (((can_access_mask & FILE_WRITE_DATA) && !CAN_WRITE(conn)) ||
980                             !can_access_file_data(conn,fname,psbuf,can_access_mask)) {
981                                 can_access = False;
982                         }
983
984                         /*
985                          * If we're returning a share violation, ensure we
986                          * cope with the braindead 1 second delay.
987                          */
988                         if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
989                             lp_defer_sharing_violations()) {
990                                 struct timeval timeout;
991                                 struct deferred_open_record state;
992                                 int timeout_usecs;
993
994                                 /* this is a hack to speed up torture tests
995                                    in 'make test' */
996                                 timeout_usecs = lp_parm_int(SNUM(conn),
997                                     "smbd","sharedelay",
998                                     SHARING_VIOLATION_USEC_WAIT);
999
1000                                 /* This is a relative time, added to the
1001                                    absolute request_time value to get the
1002                                    absolute timeout time.  Note that if this
1003                                    is the second or greater time we enter this
1004                                    codepath for this particular request mid
1005                                    then request_time is left as the absolute
1006                                    time of the *first* time this request mid
1007                                    was processed. This is what allows the
1008                                    request to eventually time out. */
1009
1010                                 timeout = timeval_set(0, timeout_usecs);
1011
1012                                 /* Nothing actually uses
1013                                    state.delayed_for_oplocks but it's handy to
1014                                    differentiate in debug messages between a
1015                                    30 second delay due to oplock break, and a
1016                                    1 second delay for share mode conflicts. */
1017
1018                                 state.delayed_for_oplocks = False;
1019                                 state.id = id;
1020                                 state.failed = false;
1021
1022                                 if ((req != NULL)
1023                                     && !request_timed_out(request_time,
1024                                                           timeout)) {
1025                                         defer_open(lck, request_time, timeout,
1026                                                    req, &state);
1027                                 }
1028                         }
1029
1030                         if (can_access) {
1031                                 /*
1032                                  * We have detected a sharing violation here
1033                                  * so return the correct error code
1034                                  */
1035                                 status = NT_STATUS_SHARING_VIOLATION;
1036                         } else {
1037                                 status = NT_STATUS_ACCESS_DENIED;
1038                         }
1039
1040                         goto cleanup_destroy;
1041                 }
1042
1043                 /*
1044                  * Normal error, for example EACCES
1045                  */
1046          cleanup_destroy:
1047                 //destroy_ifs_callback_record(oplock_waiter);
1048          cleanup:
1049                 TALLOC_FREE(lck);
1050                 return status;
1051         }
1052
1053         fsp->oplock_type = granted_oplock;
1054
1055         /* XXX uncomment for oplocks */
1056         //ifs_set_oplock_callback(oplock_waiter, fsp);
1057         //fsp->oplock_callback_id = oplock_waiter;
1058
1059         if (!file_existed) {
1060                 struct timespec old_write_time = get_mtimespec(psbuf);
1061                 /*
1062                  * Deal with the race condition where two smbd's detect the
1063                  * file doesn't exist and do the create at the same time. One
1064                  * of them will win and set a share mode, the other (ie. this
1065                  * one) should check if the requested share mode for this
1066                  * create is allowed.
1067                  */
1068
1069                 /*
1070                  * Now the file exists and fsp is successfully opened,
1071                  * fsp->dev and fsp->inode are valid and should replace the
1072                  * dev=0,inode=0 from a non existent file. Spotted by
1073                  * Nadav Danieli <nadavd@exanet.com>. JRA.
1074                  */
1075
1076                 id = fsp->file_id;
1077
1078                 lck = get_share_mode_lock(talloc_tos(), id,
1079                                           conn->connectpath,
1080                                           fname, &old_write_time);
1081
1082                 if (lck == NULL) {
1083                         DEBUG(0, ("onefs_open_file_ntcreate: Could not get "
1084                                   "share mode lock for %s\n", fname));
1085                         fd_close(fsp);
1086                         return NT_STATUS_SHARING_VIOLATION;
1087                 }
1088
1089                 if (lck->delete_on_close) {
1090                         status = NT_STATUS_DELETE_PENDING;
1091                 }
1092
1093                 if (!NT_STATUS_IS_OK(status)) {
1094                         struct deferred_open_record state;
1095
1096                         fd_close(fsp);
1097
1098                         state.delayed_for_oplocks = False;
1099                         state.id = id;
1100
1101                         /* Do it all over again immediately. In the second
1102                          * round we will find that the file existed and handle
1103                          * the DELETE_PENDING and FCB cases correctly. No need
1104                          * to duplicate the code here. Essentially this is a
1105                          * "goto top of this function", but don't tell
1106                          * anybody... */
1107
1108                         if (req != NULL) {
1109                                 defer_open(lck, request_time, timeval_zero(),
1110                                            req, &state);
1111                         }
1112                         TALLOC_FREE(lck);
1113                         return status;
1114                 }
1115
1116                 /*
1117                  * We exit this block with the share entry *locked*.....
1118                  */
1119
1120         }
1121
1122         SMB_ASSERT(lck != NULL);
1123
1124         /* Delete streams if create_disposition requires it */
1125         if (file_existed && clear_ads) {
1126                 status = delete_all_streams(conn, fname);
1127                 if (!NT_STATUS_IS_OK(status)) {
1128                         TALLOC_FREE(lck);
1129                         fd_close(fsp);
1130                         return status;
1131                 }
1132         }
1133
1134         /* note that we ignore failure for the following. It is
1135            basically a hack for NFS, and NFS will never set one of
1136            these only read them. Nobody but Samba can ever set a deny
1137            mode and we have already checked our more authoritative
1138            locking database for permission to set this deny mode. If
1139            the kernel refuses the operations then the kernel is wrong.
1140            note that GPFS supports it as well - jmcd */
1141
1142         if (fsp->fh->fd != -1) {
1143                 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access);
1144                 if(ret_flock == -1 ){
1145
1146                         TALLOC_FREE(lck);
1147                         fd_close(fsp);
1148                         return NT_STATUS_SHARING_VIOLATION;
1149                 }
1150         }
1151
1152         /*
1153          * At this point onwards, we can guarentee that the share entry
1154          * is locked, whether we created the file or not, and that the
1155          * deny mode is compatible with all current opens.
1156          */
1157
1158         /* Record the options we were opened with. */
1159         fsp->share_access = share_access;
1160         fsp->fh->private_options = create_options;
1161         /*
1162          * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
1163          */
1164         fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
1165
1166         if (file_existed) {
1167                 /* stat opens on existing files don't get oplocks. */
1168                 if (is_stat_open(open_access_mask)) {
1169                         fsp->oplock_type = NO_OPLOCK;
1170                 }
1171
1172                 if (!(flags2 & O_TRUNC)) {
1173                         info = FILE_WAS_OPENED;
1174                 } else {
1175                         info = FILE_WAS_OVERWRITTEN;
1176                 }
1177         } else {
1178                 info = FILE_WAS_CREATED;
1179         }
1180
1181         if (pinfo) {
1182                 *pinfo = info;
1183         }
1184
1185         /*
1186          * Setup the oplock info in both the shared memory and
1187          * file structs.
1188          */
1189
1190         if ((fsp->oplock_type != NO_OPLOCK) &&
1191             (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK)) {
1192                 if (!set_file_oplock(fsp, fsp->oplock_type)) {
1193                         /* Could not get the kernel oplock */
1194                         fsp->oplock_type = NO_OPLOCK;
1195                 }
1196         }
1197
1198         if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED ||
1199             info == FILE_WAS_SUPERSEDED) {
1200                 new_file_created = True;
1201         }
1202
1203         set_share_mode(lck, fsp, conn->server_info->utok.uid, 0,
1204                        fsp->oplock_type);
1205
1206         /* Handle strange delete on close create semantics. */
1207         if (create_options & FILE_DELETE_ON_CLOSE) {
1208                 status = can_set_delete_on_close(fsp, True, new_dos_attributes);
1209
1210                 if (!NT_STATUS_IS_OK(status)) {
1211                         /* Remember to delete the mode we just added. */
1212                         del_share_mode(lck, fsp);
1213                         TALLOC_FREE(lck);
1214                         fd_close(fsp);
1215                         return status;
1216                 }
1217                 /* Note that here we set the *inital* delete on close flag,
1218                    not the regular one. The magic gets handled in close. */
1219                 fsp->initial_delete_on_close = True;
1220         }
1221
1222         /*
1223          * Take care of inherited ACLs on created files - if default ACL not
1224          * selected.
1225          * May be necessary depending on acl policies.
1226          */
1227         if (!posix_open && !file_existed && !def_acl && !(VALID_STAT(*psbuf)
1228                   && (psbuf->st_flags & SF_HASNTFSACL))) {
1229
1230                 int saved_errno = errno; /* We might get ENOSYS in the next
1231                                           * call.. */
1232
1233                 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
1234                     errno == ENOSYS) {
1235                         errno = saved_errno; /* Ignore ENOSYS */
1236                 }
1237
1238         } else if (new_unx_mode) {
1239
1240                 int ret = -1;
1241
1242                 /* Attributes need changing. File already existed. */
1243
1244                 {
1245                         int saved_errno = errno; /* We might get ENOSYS in the
1246                                                   * next call.. */
1247                         ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
1248
1249                         if (ret == -1 && errno == ENOSYS) {
1250                                 errno = saved_errno; /* Ignore ENOSYS */
1251                         } else {
1252                                 DEBUG(5, ("onefs_open_file_ntcreate: reset "
1253                                           "attributes of file %s to 0%o\n",
1254                                           fname, (unsigned int)new_unx_mode));
1255                                 ret = 0; /* Don't do the fchmod below. */
1256                         }
1257                 }
1258
1259                 if ((ret == -1) &&
1260                     (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
1261                         DEBUG(5, ("onefs_open_file_ntcreate: failed to reset "
1262                                   "attributes of file %s to 0%o\n",
1263                                   fname, (unsigned int)new_unx_mode));
1264         }
1265
1266         /* If this is a successful open, we must remove any deferred open
1267          * records. */
1268         if (req != NULL) {
1269                 del_deferred_open_entry(lck, req->mid);
1270         }
1271         TALLOC_FREE(lck);
1272
1273         return NT_STATUS_OK;
1274 }
1275
1276
1277 /****************************************************************************
1278  Open a directory from an NT SMB call.
1279 ****************************************************************************/
1280 static NTSTATUS onefs_open_directory(connection_struct *conn,
1281                                      struct smb_request *req,
1282                                      const char *fname,
1283                                      uint32 access_mask,
1284                                      uint32 share_access,
1285                                      uint32 create_disposition,
1286                                      uint32 create_options,
1287                                      uint32 file_attributes,
1288                                      struct security_descriptor *sd,
1289                                      files_struct **result,
1290                                      int *pinfo,
1291                                      SMB_STRUCT_STAT *psbuf)
1292 {
1293         files_struct *fsp = NULL;
1294         struct share_mode_lock *lck = NULL;
1295         NTSTATUS status;
1296         struct timespec mtimespec;
1297         int info = 0;
1298         char *parent_dir;
1299         const char *dirname;
1300         bool posix_open = false;
1301         uint32 create_flags = 0;
1302         uint32 mode = lp_dir_mask(SNUM(conn));
1303
1304         DEBUG(5, ("onefs_open_directory: opening directory %s, "
1305                   "access_mask = 0x%x, "
1306                   "share_access = 0x%x create_options = 0x%x, "
1307                   "create_disposition = 0x%x, file_attributes = 0x%x\n",
1308                   fname, (unsigned int)access_mask, (unsigned int)share_access,
1309                   (unsigned int)create_options, (unsigned int)create_disposition,
1310                   (unsigned int)file_attributes));
1311
1312         if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
1313             (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
1314             is_ntfs_stream_name(fname)) {
1315                 DEBUG(2, ("onefs_open_directory: %s is a stream name!\n", fname));
1316                 return NT_STATUS_NOT_A_DIRECTORY;
1317         }
1318
1319         switch (create_disposition) {
1320                 case FILE_OPEN:
1321                         /* If directory exists open. If directory doesn't
1322                          * exist error. */
1323                         create_flags = 0;
1324                         info = FILE_WAS_OPENED;
1325                         break;
1326                 case FILE_CREATE:
1327                         /* If directory exists error. If directory doesn't
1328                          * exist create. */
1329                         create_flags = O_CREAT | O_EXCL;
1330                         info = FILE_WAS_CREATED;
1331                         break;
1332                 case FILE_OPEN_IF:
1333                         /* If directory exists open. If directory doesn't
1334                          * exist create. */
1335
1336                         /* Note: in order to return whether the directory was
1337                          * opened or created, we first try to open and then try
1338                          * to create. */
1339                         create_flags = 0;
1340                         info = FILE_WAS_OPENED;
1341                         break;
1342                 case FILE_SUPERSEDE:
1343                 case FILE_OVERWRITE:
1344                 case FILE_OVERWRITE_IF:
1345                 default:
1346                         DEBUG(5, ("onefs_open_directory: invalid "
1347                                   "create_disposition 0x%x for directory %s\n",
1348                                   (unsigned int)create_disposition, fname));
1349                         return NT_STATUS_INVALID_PARAMETER;
1350         }
1351
1352         /*
1353          * Check for write access to the share. Done in mkdir_internal() in
1354          * mainline samba.
1355          */
1356         if (!CAN_WRITE(conn) && (create_flags & O_CREAT)) {
1357                 return NT_STATUS_ACCESS_DENIED;
1358         }
1359
1360         /* Get parent dirname */
1361         if (!parent_dirname(talloc_tos(), fname, &parent_dir, &dirname)) {
1362                 return NT_STATUS_NO_MEMORY;
1363         }
1364
1365         if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1366                 posix_open = true;
1367                 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1368                 file_attributes = 0;
1369         } else {
1370                 mode = unix_mode(conn, aDIR, fname, parent_dir);
1371         }
1372
1373         /*
1374          * The NONINDEXED and COMPRESSED bits seem to always be cleared on
1375          * directories, no matter if you specify that they should be set.
1376          */
1377         file_attributes &=
1378             ~(FILE_ATTRIBUTE_NONINDEXED | FILE_ATTRIBUTE_COMPRESSED);
1379
1380         status = file_new(req, conn, &fsp);
1381         if(!NT_STATUS_IS_OK(status)) {
1382                 return status;
1383         }
1384
1385         /*
1386          * Actual open with retry magic to handle FILE_OPEN_IF which is
1387          * unique because the kernel won't tell us if the file was opened or
1388          * created.
1389          */
1390  retry_open:
1391         fsp->fh->fd = onefs_sys_create_file(conn,
1392                                             -1,
1393                                             fname,
1394                                             access_mask,
1395                                             access_mask,
1396                                             share_access,
1397                                             create_options,
1398                                             create_flags | O_DIRECTORY,
1399                                             mode,
1400                                             0,
1401                                             0,
1402                                             sd,
1403                                             file_attributes,
1404                                             NULL);
1405
1406         if (fsp->fh->fd == -1) {
1407                 DEBUG(3, ("Error opening %s. Errno=%d (%s).\n", fname, errno,
1408                           strerror(errno)));
1409                 SMB_ASSERT(errno != EINPROGRESS);
1410
1411                 if (create_disposition == FILE_OPEN_IF) {
1412                         if (errno == ENOENT) {
1413                                 /* Try again, creating it this time. */
1414                                 create_flags = O_CREAT | O_EXCL;
1415                                 info = FILE_WAS_CREATED;
1416                                 goto retry_open;
1417                         } else if (errno == EEXIST) {
1418                                 /* Uggh. Try again again. */
1419                                 create_flags = 0;
1420                                 info = FILE_WAS_OPENED;
1421                                 goto retry_open;
1422                         }
1423                 }
1424
1425                 /* Error cases below: */
1426                 file_free(req, fsp);
1427
1428                 if ((errno == ENOENT) && (create_disposition == FILE_OPEN)) {
1429                         DEBUG(5,("onefs_open_directory: FILE_OPEN requested "
1430                                   "for directory %s and it doesn't "
1431                                   "exist.\n", fname ));
1432                         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1433                 } else if ((errno == EEXIST) &&
1434                     (create_disposition == FILE_CREATE)) {
1435                         DEBUG(5,("onefs_open_directory: FILE_CREATE "
1436                                   "requested for directory %s and it "
1437                                   "already exists.\n", fname ));
1438                         return NT_STATUS_OBJECT_NAME_COLLISION;
1439                 } else if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
1440                         /* Catch sharing violations. */
1441                         return NT_STATUS_SHARING_VIOLATION;
1442                 }
1443
1444                 return map_nt_error_from_unix(errno);
1445         }
1446
1447         if (info == FILE_WAS_CREATED) {
1448
1449                 /* Pulled from mkdir_internal() */
1450                 if (SMB_VFS_LSTAT(conn, fname, psbuf) == -1) {
1451                         DEBUG(2, ("Could not stat directory '%s' just "
1452                                   "created: %s\n",fname, strerror(errno)));
1453                         return map_nt_error_from_unix(errno);
1454                 }
1455
1456                 if (!S_ISDIR(psbuf->st_mode)) {
1457                         DEBUG(0, ("Directory just '%s' created is not a "
1458                                   "directory\n", fname));
1459                         return NT_STATUS_ACCESS_DENIED;
1460                 }
1461
1462                 if (!posix_open) {
1463                         /*
1464                          * Check if high bits should have been set, then (if
1465                          * bits are missing): add them.  Consider bits
1466                          * automagically set by UNIX, i.e. SGID bit from
1467                          * parent dir.
1468                          */
1469                         if (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO) &&
1470                             (mode & ~psbuf->st_mode)) {
1471                                 SMB_VFS_CHMOD(conn, fname, (psbuf->st_mode |
1472                                                   (mode & ~psbuf->st_mode)));
1473                         }
1474                 }
1475
1476                 /* Change the owner if required. */
1477                 if (lp_inherit_owner(SNUM(conn))) {
1478                         change_dir_owner_to_parent(conn, parent_dir, fname,
1479                                                    psbuf);
1480                 }
1481
1482                 notify_fname(conn, NOTIFY_ACTION_ADDED,
1483                              FILE_NOTIFY_CHANGE_DIR_NAME, fname);
1484         }
1485
1486         /* Stat the fd for Samba bookkeeping. */
1487         if(SMB_VFS_FSTAT(fsp, psbuf) != 0) {
1488                 fd_close(fsp);
1489                 file_free(req, fsp);
1490                 return map_nt_error_from_unix(errno);
1491         }
1492
1493         /* Setup the files_struct for it. */
1494         fsp->mode = psbuf->st_mode;
1495         fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
1496         fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
1497         fsp->file_pid = req ? req->smbpid : 0;
1498         fsp->can_lock = False;
1499         fsp->can_read = False;
1500         fsp->can_write = False;
1501
1502         fsp->share_access = share_access;
1503         fsp->fh->private_options = create_options;
1504         /*
1505          * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
1506          */
1507         fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
1508         fsp->print_file = False;
1509         fsp->modified = False;
1510         fsp->oplock_type = NO_OPLOCK;
1511         fsp->sent_oplock_break = NO_BREAK_SENT;
1512         fsp->is_directory = True;
1513         fsp->posix_open = posix_open;
1514
1515         string_set(&fsp->fsp_name,fname);
1516
1517         mtimespec = get_mtimespec(psbuf);
1518
1519         /*
1520          * Still set the samba share mode lock for correct delete-on-close
1521          * semantics and to make smbstatus more useful.
1522          */
1523         lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
1524                                   conn->connectpath,
1525                                   fname, &mtimespec);
1526
1527         if (lck == NULL) {
1528                 DEBUG(0, ("onefs_open_directory: Could not get share mode "
1529                           "lock for %s\n", fname));
1530                 fd_close(fsp);
1531                 file_free(req, fsp);
1532                 return NT_STATUS_SHARING_VIOLATION;
1533         }
1534
1535         if (lck->delete_on_close) {
1536                 TALLOC_FREE(lck);
1537                 fd_close(fsp);
1538                 file_free(req, fsp);
1539                 return NT_STATUS_DELETE_PENDING;
1540         }
1541
1542         set_share_mode(lck, fsp, conn->server_info->utok.uid, 0, NO_OPLOCK);
1543
1544         /*
1545          * For directories the delete on close bit at open time seems
1546          * always to be honored on close... See test 19 in Samba4 BASE-DELETE.
1547          */
1548         if (create_options & FILE_DELETE_ON_CLOSE) {
1549                 status = can_set_delete_on_close(fsp, True, 0);
1550                 if (!NT_STATUS_IS_OK(status) &&
1551                     !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
1552                         TALLOC_FREE(lck);
1553                         fd_close(fsp);
1554                         file_free(req, fsp);
1555                         return status;
1556                 }
1557
1558                 if (NT_STATUS_IS_OK(status)) {
1559                         /* Note that here we set the *inital* delete on close flag,
1560                            not the regular one. The magic gets handled in close. */
1561                         fsp->initial_delete_on_close = True;
1562                 }
1563         }
1564
1565         TALLOC_FREE(lck);
1566
1567         if (pinfo) {
1568                 *pinfo = info;
1569         }
1570
1571         *result = fsp;
1572         return NT_STATUS_OK;
1573 }
1574
1575 /*
1576  * If a main file is opened for delete, all streams need to be checked for
1577  * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
1578  * If that works, delete them all by setting the delete on close and close.
1579  */
1580
1581 static NTSTATUS open_streams_for_delete(connection_struct *conn,
1582                                         const char *fname)
1583 {
1584         struct stream_struct *stream_info;
1585         files_struct **streams;
1586         int i;
1587         unsigned int num_streams;
1588         TALLOC_CTX *frame = talloc_stackframe();
1589         NTSTATUS status;
1590
1591         status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
1592                                     &num_streams, &stream_info);
1593
1594         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
1595             || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1596                 DEBUG(10, ("no streams around\n"));
1597                 TALLOC_FREE(frame);
1598                 return NT_STATUS_OK;
1599         }
1600
1601         if (!NT_STATUS_IS_OK(status)) {
1602                 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
1603                            nt_errstr(status)));
1604                 goto fail;
1605         }
1606
1607         DEBUG(10, ("open_streams_for_delete found %d streams\n",
1608                    num_streams));
1609
1610         if (num_streams == 0) {
1611                 TALLOC_FREE(frame);
1612                 return NT_STATUS_OK;
1613         }
1614
1615         streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
1616         if (streams == NULL) {
1617                 DEBUG(0, ("talloc failed\n"));
1618                 status = NT_STATUS_NO_MEMORY;
1619                 goto fail;
1620         }
1621
1622         /* Open the base file */
1623
1624         for (i=0; i<num_streams; i++) {
1625                 char *streamname;
1626
1627                 if (strequal(stream_info[i].name, "::$DATA")) {
1628                         streams[i] = NULL;
1629                         continue;
1630                 }
1631
1632                 streamname = talloc_asprintf(talloc_tos(), "%s%s", fname,
1633                                              stream_info[i].name);
1634
1635                 if (streamname == NULL) {
1636                         DEBUG(0, ("talloc_aprintf failed\n"));
1637                         status = NT_STATUS_NO_MEMORY;
1638                         goto fail;
1639                 }
1640
1641                 status = onefs_create_file_unixpath
1642                         (conn,                  /* conn */
1643                          NULL,                  /* req */
1644                          streamname,            /* fname */
1645                          DELETE_ACCESS,         /* access_mask */
1646                          FILE_SHARE_READ | FILE_SHARE_WRITE
1647                          | FILE_SHARE_DELETE,   /* share_access */
1648                          FILE_OPEN,             /* create_disposition*/
1649                          NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* create_options */
1650                          FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1651                          0,                     /* oplock_request */
1652                          0,                     /* allocation_size */
1653                          NULL,                  /* sd */
1654                          NULL,                  /* ea_list */
1655                          &streams[i],           /* result */
1656                          NULL,                  /* pinfo */
1657                          NULL);                 /* psbuf */
1658
1659                 TALLOC_FREE(streamname);
1660
1661                 if (!NT_STATUS_IS_OK(status)) {
1662                         DEBUG(10, ("Could not open stream %s: %s\n",
1663                                    streamname, nt_errstr(status)));
1664                         break;
1665                 }
1666         }
1667
1668         /*
1669          * don't touch the variable "status" beyond this point :-)
1670          */
1671
1672         for (i -= 1 ; i >= 0; i--) {
1673                 if (streams[i] == NULL) {
1674                         continue;
1675                 }
1676
1677                 DEBUG(10, ("Closing stream # %d, %s\n", i,
1678                            streams[i]->fsp_name));
1679                 close_file(NULL, streams[i], NORMAL_CLOSE);
1680         }
1681
1682  fail:
1683         TALLOC_FREE(frame);
1684         return status;
1685 }
1686
1687 /*
1688  * Wrapper around onefs_open_file_ntcreate and onefs_open_directory.
1689  */
1690 static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
1691                                            struct smb_request *req,
1692                                            const char *fname,
1693                                            uint32_t access_mask,
1694                                            uint32_t share_access,
1695                                            uint32_t create_disposition,
1696                                            uint32_t create_options,
1697                                            uint32_t file_attributes,
1698                                            uint32_t oplock_request,
1699                                            uint64_t allocation_size,
1700                                            struct security_descriptor *sd,
1701                                            struct ea_list *ea_list,
1702                                            files_struct **result,
1703                                            int *pinfo,
1704                                            SMB_STRUCT_STAT *psbuf)
1705 {
1706         SMB_STRUCT_STAT sbuf;
1707         int info = FILE_WAS_OPENED;
1708         files_struct *base_fsp = NULL;
1709         files_struct *fsp = NULL;
1710         NTSTATUS status;
1711
1712         DEBUG(10,("onefs_create_file_unixpath: access_mask = 0x%x "
1713                   "file_attributes = 0x%x, share_access = 0x%x, "
1714                   "create_disposition = 0x%x create_options = 0x%x "
1715                   "oplock_request = 0x%x ea_list = 0x%p, sd = 0x%p, "
1716                   "fname = %s\n",
1717                   (unsigned int)access_mask,
1718                   (unsigned int)file_attributes,
1719                   (unsigned int)share_access,
1720                   (unsigned int)create_disposition,
1721                   (unsigned int)create_options,
1722                   (unsigned int)oplock_request,
1723                   ea_list, sd, fname));
1724
1725         if (create_options & FILE_OPEN_BY_FILE_ID) {
1726                 status = NT_STATUS_NOT_SUPPORTED;
1727                 goto fail;
1728         }
1729
1730         if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
1731                 status = NT_STATUS_INVALID_PARAMETER;
1732                 goto fail;
1733         }
1734
1735         if (req == NULL) {
1736                 oplock_request |= INTERNAL_OPEN_ONLY;
1737         }
1738
1739         if (psbuf != NULL) {
1740                 sbuf = *psbuf;
1741         }
1742         else {
1743                 if (SMB_VFS_STAT(conn, fname, &sbuf) == -1) {
1744                         SET_STAT_INVALID(sbuf);
1745                 }
1746         }
1747
1748         if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
1749             && (access_mask & DELETE_ACCESS)
1750             && !is_ntfs_stream_name(fname)) {
1751                 /*
1752                  * We can't open a file with DELETE access if any of the
1753                  * streams is open without FILE_SHARE_DELETE
1754                  */
1755                 status = open_streams_for_delete(conn, fname);
1756
1757                 if (!NT_STATUS_IS_OK(status)) {
1758                         goto fail;
1759                 }
1760         }
1761
1762         if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
1763             && is_ntfs_stream_name(fname)) {
1764                 char *base;
1765                 uint32 base_create_disposition;
1766
1767                 if (create_options & FILE_DIRECTORY_FILE) {
1768                         status = NT_STATUS_NOT_A_DIRECTORY;
1769                         goto fail;
1770                 }
1771
1772                 status = onefs_split_ntfs_stream_name(talloc_tos(), fname,
1773                                                       &base, NULL);
1774                 if (!NT_STATUS_IS_OK(status)) {
1775                         DEBUG(10, ("onefs_create_file_unixpath: "
1776                                   "split_ntfs_stream_name failed: %s\n",
1777                                   nt_errstr(status)));
1778                         goto fail;
1779                 }
1780
1781                 SMB_ASSERT(!is_ntfs_stream_name(base)); /* paranoia.. */
1782
1783                 switch (create_disposition) {
1784                 case FILE_OPEN:
1785                         base_create_disposition = FILE_OPEN;
1786                         break;
1787                 default:
1788                         base_create_disposition = FILE_OPEN_IF;
1789                         break;
1790                 }
1791
1792                 status = onefs_create_file_unixpath(
1793                         conn,                           /* conn */
1794                         NULL,                           /* req */
1795                         base,                           /* fname */
1796                         0,                              /* access_mask */
1797                         (FILE_SHARE_READ |
1798                             FILE_SHARE_WRITE |
1799                             FILE_SHARE_DELETE),         /* share_access */
1800                         base_create_disposition,        /* create_disposition*/
1801                         0,                              /* create_options */
1802                         file_attributes,                /* file_attributes */
1803                         NO_OPLOCK,                      /* oplock_request */
1804                         0,                              /* allocation_size */
1805                         NULL,                           /* sd */
1806                         NULL,                           /* ea_list */
1807                         &base_fsp,                      /* result */
1808                         NULL,                           /* pinfo */
1809                         NULL);                          /* psbuf */
1810
1811                 if (!NT_STATUS_IS_OK(status)) {
1812                         DEBUG(10, ("onefs_create_file_unixpath for base %s "
1813                                   "failed: %s\n", base, nt_errstr(status)));
1814                         goto fail;
1815                 }
1816         }
1817
1818         /* Covert generic bits in the security descriptor. */
1819         if (sd != NULL) {
1820                 security_acl_map_generic(sd->dacl, &file_generic_mapping);
1821                 security_acl_map_generic(sd->sacl, &file_generic_mapping);
1822         }
1823
1824         /*
1825          * If it's a request for a directory open, deal with it separately.
1826          */
1827
1828         if (create_options & FILE_DIRECTORY_FILE) {
1829
1830                 if (create_options & FILE_NON_DIRECTORY_FILE) {
1831                         status = NT_STATUS_INVALID_PARAMETER;
1832                         goto fail;
1833                 }
1834
1835                 /* Can't open a temp directory. IFS kit test. */
1836                 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
1837                      (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
1838                         status = NT_STATUS_INVALID_PARAMETER;
1839                         goto fail;
1840                 }
1841
1842                 /*
1843                  * We will get a create directory here if the Win32
1844                  * app specified a security descriptor in the
1845                  * CreateDirectory() call.
1846                  */
1847
1848                 status = onefs_open_directory(
1849                         conn,                           /* conn */
1850                         req,                            /* req */
1851                         fname,                          /* fname */
1852                         access_mask,                    /* access_mask */
1853                         share_access,                   /* share_access */
1854                         create_disposition,             /* create_disposition*/
1855                         create_options,                 /* create_options */
1856                         file_attributes,                /* file_attributes */
1857                         sd,                             /* sd */
1858                         &fsp,                           /* result */
1859                         &info,                          /* pinfo */
1860                         &sbuf);                         /* psbuf */
1861         } else {
1862
1863                 /*
1864                  * Ordinary file case.
1865                  */
1866
1867                 status = file_new(req, conn, &fsp);
1868                 if(!NT_STATUS_IS_OK(status)) {
1869                         goto fail;
1870                 }
1871
1872                 /*
1873                  * We're opening the stream element of a base_fsp
1874                  * we already opened. Set up the base_fsp pointer.
1875                  */
1876                 if (base_fsp) {
1877                         fsp->base_fsp = base_fsp;
1878                 }
1879
1880                 status = onefs_open_file_ntcreate(
1881                         conn,                           /* conn */
1882                         req,                            /* req */
1883                         fname,                          /* fname */
1884                         access_mask,                    /* access_mask */
1885                         share_access,                   /* share_access */
1886                         create_disposition,             /* create_disposition*/
1887                         create_options,                 /* create_options */
1888                         file_attributes,                /* file_attributes */
1889                         oplock_request,                 /* oplock_request */
1890                         sd,                             /* sd */
1891                         fsp,                            /* result */
1892                         &info,                          /* pinfo */
1893                         &sbuf);                         /* psbuf */
1894
1895                 if(!NT_STATUS_IS_OK(status)) {
1896                         file_free(req, fsp);
1897                         fsp = NULL;
1898                 }
1899
1900                 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
1901
1902                         /* A stream open never opens a directory */
1903
1904                         if (base_fsp) {
1905                                 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1906                                 goto fail;
1907                         }
1908
1909                         /*
1910                          * Fail the open if it was explicitly a non-directory
1911                          * file.
1912                          */
1913
1914                         if (create_options & FILE_NON_DIRECTORY_FILE) {
1915                                 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1916                                 goto fail;
1917                         }
1918
1919                         create_options |= FILE_DIRECTORY_FILE;
1920
1921                         status = onefs_open_directory(
1922                                 conn,                   /* conn */
1923                                 req,                    /* req */
1924                                 fname,                  /* fname */
1925                                 access_mask,            /* access_mask */
1926                                 share_access,           /* share_access */
1927                                 create_disposition,     /* create_disposition*/
1928                                 create_options,         /* create_options */
1929                                 file_attributes,        /* file_attributes */
1930                                 sd,                     /* sd */
1931                                 &fsp,                   /* result */
1932                                 &info,                  /* pinfo */
1933                                 &sbuf);                 /* psbuf */
1934                 }
1935         }
1936
1937         if (!NT_STATUS_IS_OK(status)) {
1938                 goto fail;
1939         }
1940
1941         fsp->base_fsp = base_fsp;
1942
1943         SMB_ASSERT(fsp);
1944
1945         if ((ea_list != NULL) && (info == FILE_WAS_CREATED)) {
1946                 status = set_ea(conn, fsp, fname, ea_list);
1947                 if (!NT_STATUS_IS_OK(status)) {
1948                         goto fail;
1949                 }
1950         }
1951
1952         if (!fsp->is_directory && S_ISDIR(sbuf.st_mode)) {
1953                 status = NT_STATUS_ACCESS_DENIED;
1954                 goto fail;
1955         }
1956
1957         /* Save the requested allocation size. */
1958         if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1959                 if (allocation_size
1960                     && (allocation_size > sbuf.st_size)) {
1961                         fsp->initial_allocation_size = smb_roundup(
1962                                 fsp->conn, allocation_size);
1963                         if (fsp->is_directory) {
1964                                 /* Can't set allocation size on a directory. */
1965                                 status = NT_STATUS_ACCESS_DENIED;
1966                                 goto fail;
1967                         }
1968                         if (vfs_allocate_file_space(
1969                                     fsp, fsp->initial_allocation_size) == -1) {
1970                                 status = NT_STATUS_DISK_FULL;
1971                                 goto fail;
1972                         }
1973                 } else {
1974                         fsp->initial_allocation_size = smb_roundup(
1975                                 fsp->conn, (uint64_t)sbuf.st_size);
1976                 }
1977         }
1978
1979         DEBUG(10, ("onefs_create_file_unixpath: info=%d\n", info));
1980
1981         *result = fsp;
1982         if (pinfo != NULL) {
1983                 *pinfo = info;
1984         }
1985         if (psbuf != NULL) {
1986                 if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
1987                         *psbuf = sbuf;
1988                 }
1989                 else {
1990                         SMB_VFS_FSTAT(fsp, psbuf);
1991                 }
1992         }
1993         return NT_STATUS_OK;
1994
1995  fail:
1996         DEBUG(10, ("onefs_create_file_unixpath: %s\n", nt_errstr(status)));
1997
1998         if (fsp != NULL) {
1999                 if (base_fsp && fsp->base_fsp == base_fsp) {
2000                         /*
2001                          * The close_file below will close
2002                          * fsp->base_fsp.
2003                          */
2004                         base_fsp = NULL;
2005                 }
2006                 close_file(req, fsp, ERROR_CLOSE);
2007                 fsp = NULL;
2008         }
2009         if (base_fsp != NULL) {
2010                 close_file(req, base_fsp, ERROR_CLOSE);
2011                 base_fsp = NULL;
2012         }
2013         return status;
2014 }
2015
2016 /**
2017  * SMB_VFS_CREATE_FILE interface to onefs.
2018  */
2019 NTSTATUS onefs_create_file(vfs_handle_struct *handle,
2020                            struct smb_request *req,
2021                            uint16_t root_dir_fid,
2022                            const char *fname,
2023                            uint32_t create_file_flags,
2024                            uint32_t access_mask,
2025                            uint32_t share_access,
2026                            uint32_t create_disposition,
2027                            uint32_t create_options,
2028                            uint32_t file_attributes,
2029                            uint32_t oplock_request,
2030                            uint64_t allocation_size,
2031                            struct security_descriptor *sd,
2032                            struct ea_list *ea_list,
2033                            files_struct **result,
2034                            int *pinfo,
2035                            SMB_STRUCT_STAT *psbuf)
2036 {
2037         connection_struct *conn = handle->conn;
2038         struct case_semantics_state *case_state = NULL;
2039         SMB_STRUCT_STAT sbuf;
2040         int info = FILE_WAS_OPENED;
2041         files_struct *fsp = NULL;
2042         NTSTATUS status;
2043
2044         DEBUG(10,("onefs_create_file: access_mask = 0x%x "
2045                   "file_attributes = 0x%x, share_access = 0x%x, "
2046                   "create_disposition = 0x%x create_options = 0x%x "
2047                   "oplock_request = 0x%x "
2048                   "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
2049                   "create_file_flags = 0x%x, fname = %s\n",
2050                   (unsigned int)access_mask,
2051                   (unsigned int)file_attributes,
2052                   (unsigned int)share_access,
2053                   (unsigned int)create_disposition,
2054                   (unsigned int)create_options,
2055                   (unsigned int)oplock_request,
2056                   (unsigned int)root_dir_fid,
2057                   ea_list, sd, create_file_flags, fname));
2058
2059         /* Get the file name if root_dir_fid was specified. */
2060         if (root_dir_fid != 0) {
2061                 char *new_fname;
2062
2063                 status = get_relative_fid_filename(conn, req, root_dir_fid,
2064                                                    fname, &new_fname);
2065                 if (!NT_STATUS_IS_OK(status)) {
2066                         goto fail;
2067                 }
2068
2069                 fname = new_fname;
2070         }
2071
2072         /* Resolve the file name if this was a DFS pathname. */
2073         if ((req != NULL) && (req->flags2 & FLAGS2_DFS_PATHNAMES)) {
2074                 char *resolved_fname;
2075
2076                 status = resolve_dfspath(talloc_tos(), conn, true, fname,
2077                                          &resolved_fname);
2078
2079                 if (!NT_STATUS_IS_OK(status)) {
2080                         /*
2081                          * For PATH_NOT_COVERED we had
2082                          * reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2083                          *                 ERRSRV, ERRbadpath);
2084                          * Need to fix in callers
2085                          */
2086                         goto fail;
2087                 }
2088                 fname = resolved_fname;
2089         }
2090
2091         /* Check if POSIX semantics are wanted. */
2092         if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2093                 case_state = set_posix_case_semantics(talloc_tos(), conn);
2094         }
2095
2096         /* Convert dos path to unix path if it hasn't already been done. */
2097         if (create_file_flags & CFF_DOS_PATH) {
2098                 char *converted_fname;
2099
2100                 SET_STAT_INVALID(sbuf);
2101
2102                 status = unix_convert(talloc_tos(), conn, fname, False,
2103                                       &converted_fname, NULL, &sbuf);
2104                 if (!NT_STATUS_IS_OK(status)) {
2105                         goto fail;
2106                 }
2107                 fname = converted_fname;
2108         } else {
2109                 if (psbuf != NULL) {
2110                         sbuf = *psbuf;
2111                 } else {
2112                         if (SMB_VFS_STAT(conn, fname, &sbuf) == -1) {
2113                                 SET_STAT_INVALID(sbuf);
2114                         }
2115                 }
2116
2117         }
2118
2119         TALLOC_FREE(case_state);
2120
2121         /* All file access must go through check_name() */
2122         status = check_name(conn, fname);
2123         if (!NT_STATUS_IS_OK(status)) {
2124                 goto fail;
2125         }
2126
2127         status = onefs_create_file_unixpath(
2128                 conn,                                   /* conn */
2129                 req,                                    /* req */
2130                 fname,                                  /* fname */
2131                 access_mask,                            /* access_mask */
2132                 share_access,                           /* share_access */
2133                 create_disposition,                     /* create_disposition*/
2134                 create_options,                         /* create_options */
2135                 file_attributes,                        /* file_attributes */
2136                 oplock_request,                         /* oplock_request */
2137                 allocation_size,                        /* allocation_size */
2138                 sd,                                     /* sd */
2139                 ea_list,                                /* ea_list */
2140                 &fsp,                                   /* result */
2141                 &info,                                  /* pinfo */
2142                 &sbuf);                                 /* psbuf */
2143
2144         if (!NT_STATUS_IS_OK(status)) {
2145                 goto fail;
2146         }
2147
2148         DEBUG(10, ("onefs_create_file: info=%d\n", info));
2149
2150         *result = fsp;
2151         if (pinfo != NULL) {
2152                 *pinfo = info;
2153         }
2154         if (psbuf != NULL) {
2155                 *psbuf = sbuf;
2156         }
2157         return NT_STATUS_OK;
2158
2159  fail:
2160         DEBUG(10, ("onefs_create_file: %s\n", nt_errstr(status)));
2161
2162         if (fsp != NULL) {
2163                 close_file(req, fsp, ERROR_CLOSE);
2164                 fsp = NULL;
2165         }
2166         return status;
2167 }