2 Unix SMB/Netbios implementation.
4 file opening and share modes
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 extern int DEBUGLEVEL;
26 extern pstring sesssetup_user;
27 extern uint16 global_oplock_port;
28 extern BOOL global_client_failed_oplock_break;
30 /****************************************************************************
31 fd support routines - attempt to do a dos_open.
32 ****************************************************************************/
34 static int fd_open(struct connection_struct *conn, char *fname,
35 int flags, mode_t mode)
37 int fd = conn->vfs_ops.open(dos_to_unix(fname,False),flags,mode);
39 /* Fix for files ending in '.' */
40 if((fd == -1) && (errno == ENOENT) &&
41 (strchr(fname,'.')==NULL)) {
43 fd = conn->vfs_ops.open(dos_to_unix(fname,False),flags,mode);
46 DEBUG(10,("fd_open: name %s, mode = %d, fd = %d. %s\n", fname, (int)mode, fd,
47 (fd == -1) ? strerror(errno) : "" ));
52 /****************************************************************************
53 Close the file associated with a fsp.
54 ****************************************************************************/
56 int fd_close(struct connection_struct *conn, files_struct *fsp)
58 int ret = conn->vfs_ops.close(fsp->fd);
64 /****************************************************************************
65 Check a filename for the pipe string.
66 ****************************************************************************/
68 static void check_for_pipe(char *fname)
70 /* special case of pipe opens */
72 StrnCpy(s,fname,sizeof(s)-1);
74 if (strstr(s,"pipe/")) {
75 DEBUG(3,("Rejecting named pipe open for %s\n",fname));
76 unix_ERR_class = ERRSRV;
77 unix_ERR_code = ERRaccess;
81 /****************************************************************************
83 ****************************************************************************/
85 static void open_file(files_struct *fsp,connection_struct *conn,
86 char *fname1,int flags,mode_t mode)
88 extern struct current_user current_user;
90 int accmode = (flags & O_ACCMODE);
95 fsp->oplock_type = NO_OPLOCK;
98 pstrcpy(fname,fname1);
100 /* Check permissions */
103 * This code was changed after seeing a client open request
104 * containing the open mode of (DENY_WRITE/read-only) with
105 * the 'create if not exist' bit set. The previous code
106 * would fail to open the file read only on a read-only share
107 * as it was checking the flags parameter directly against O_RDONLY,
108 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
112 if (!CAN_WRITE(conn)) {
113 /* It's a read-only share - fail if we wanted to write. */
114 if(accmode != O_RDONLY) {
115 DEBUG(3,("Permission denied opening %s\n",fname));
116 check_for_pipe(fname);
118 } else if(flags & O_CREAT) {
119 /* We don't want to write - but we must make sure that O_CREAT
120 doesn't create the file if we have write access into the
127 /* actually do the open */
128 fsp->fd = fd_open(conn, fname, flags, mode);
131 DEBUG(3,("Error opening file %s (%s) (flags=%d)\n",
132 fname,strerror(errno),flags));
133 check_for_pipe(fname);
137 conn->vfs_ops.fstat(fsp->fd, &sbuf);
140 * POSIX allows read-only opens of directories. We don't
141 * want to do this (we use a different code path for this)
142 * so catch a directory open and return an EISDIR. JRA.
145 if(S_ISDIR(sbuf.st_mode)) {
151 conn->num_files_open++;
152 fsp->mode = sbuf.st_mode;
153 fsp->inode = sbuf.st_ino;
154 fsp->dev = sbuf.st_dev;
155 GetTimeOfDay(&fsp->open_time);
156 fsp->vuid = current_user.vuid;
160 fsp->can_lock = True;
161 fsp->can_read = ((flags & O_WRONLY)==0);
162 fsp->can_write = ((flags & (O_WRONLY|O_RDWR))!=0);
164 fsp->print_file = False;
165 fsp->modified = False;
166 fsp->oplock_type = NO_OPLOCK;
167 fsp->sent_oplock_break = NO_BREAK_SENT;
168 fsp->is_directory = False;
169 fsp->stat_open = False;
170 fsp->directory_delete_on_close = False;
173 * Note that the file name here is the *untranslated* name
174 * ie. it is still in the DOS codepage sent from the client.
175 * All use of this filename will pass though the sys_xxxx
176 * functions which will do the dos_to_unix translation before
177 * mapping into a UNIX filename. JRA.
179 string_set(&fsp->fsp_name,fname);
180 fsp->wbmpx_ptr = NULL;
181 fsp->wcp = NULL; /* Write cache pointer. */
183 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
184 *sesssetup_user ? sesssetup_user : conn->user,fsp->fsp_name,
185 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
186 conn->num_files_open));
189 /****************************************************************************
191 Helper for open_file_shared.
192 Truncate a file after checking locking; close file if locked.
193 **************************************************************************/
195 static void truncate_unless_locked(files_struct *fsp, connection_struct *conn, int token,
199 SMB_OFF_T mask2 = ((SMB_OFF_T)0x3) << (SMB_OFF_T_BITS-4);
200 SMB_OFF_T mask = (mask2<<2);
202 if (is_locked(fsp,conn,~mask,0,WRITE_LOCK)){
203 /* If share modes are in force for this connection we
204 have the share entry locked. Unlock it before closing. */
205 if (*share_locked && lp_share_modes(SNUM(conn)))
206 unlock_share_entry_fsp(fsp);
207 close_file(fsp,False);
208 /* Share mode no longer locked. */
209 *share_locked = False;
211 unix_ERR_class = ERRDOS;
212 unix_ERR_code = ERRlock;
214 sys_ftruncate(fsp->fd,0);
220 /*******************************************************************
221 return True if the filename is one of the special executable types
222 ********************************************************************/
223 static BOOL is_executable(char *fname)
225 if ((fname = strrchr(fname,'.'))) {
226 if (strequal(fname,".com") ||
227 strequal(fname,".dll") ||
228 strequal(fname,".exe") ||
229 strequal(fname,".sym")) {
236 enum {AFAIL,AREAD,AWRITE,AALL};
238 /*******************************************************************
239 reproduce the share mode access table
240 this is horrendoously complex, and really can't be justified on any
241 rational grounds except that this is _exactly_ what NT does. See
242 the DENY1 and DENY2 tests in smbtorture for a comprehensive set of
244 ********************************************************************/
245 static int access_table(int new_deny,int old_deny,int old_mode,
246 BOOL same_pid, BOOL isexe)
248 if (new_deny == DENY_ALL || old_deny == DENY_ALL) return(AFAIL);
251 if (isexe && old_mode == DOS_OPEN_RDONLY &&
252 old_deny == DENY_DOS && new_deny == DENY_READ) {
255 if (!isexe && old_mode == DOS_OPEN_RDONLY &&
256 old_deny == DENY_DOS && new_deny == DENY_DOS) {
259 if (new_deny == DENY_FCB && old_deny == DENY_DOS) {
260 if (isexe) return AFAIL;
261 if (old_mode == DOS_OPEN_RDONLY) return AFAIL;
264 if (old_mode == DOS_OPEN_RDONLY && old_deny == DENY_DOS) {
265 if (new_deny == DENY_FCB || new_deny == DENY_READ) {
266 if (isexe) return AREAD;
270 if (old_deny == DENY_FCB) {
271 if (new_deny == DENY_DOS || new_deny == DENY_FCB) return AALL;
276 if (old_deny == DENY_DOS || new_deny == DENY_DOS ||
277 old_deny == DENY_FCB || new_deny == DENY_FCB) {
279 if (old_deny == DENY_FCB || new_deny == DENY_FCB) {
282 if (old_deny == DENY_DOS) {
283 if (new_deny == DENY_READ &&
284 (old_mode == DOS_OPEN_RDONLY ||
285 old_mode == DOS_OPEN_RDWR)) {
288 if (new_deny == DENY_WRITE &&
289 (old_mode == DOS_OPEN_WRONLY ||
290 old_mode == DOS_OPEN_RDWR)) {
295 if (old_deny == DENY_NONE) return AALL;
296 if (old_deny == DENY_READ) return AWRITE;
297 if (old_deny == DENY_WRITE) return AREAD;
299 /* it isn't a exe, dll, sym or com file */
300 if (old_deny == new_deny && same_pid)
303 if (old_deny == DENY_READ || new_deny == DENY_READ) return AFAIL;
304 if (old_mode == DOS_OPEN_RDONLY) return(AREAD);
312 if (old_deny==DENY_WRITE && old_mode==DOS_OPEN_RDONLY) return(AREAD);
313 if (old_deny==DENY_READ && old_mode==DOS_OPEN_RDONLY) return(AWRITE);
314 if (old_deny==DENY_NONE && old_mode==DOS_OPEN_RDONLY) return(AALL);
317 if (old_deny==DENY_WRITE && old_mode==DOS_OPEN_WRONLY) return(AREAD);
318 if (old_deny==DENY_READ && old_mode==DOS_OPEN_WRONLY) return(AWRITE);
319 if (old_deny==DENY_NONE && old_mode==DOS_OPEN_WRONLY) return(AALL);
322 if (old_deny==DENY_WRITE) return(AREAD);
323 if (old_deny==DENY_READ) return(AWRITE);
324 if (old_deny==DENY_NONE) return(AALL);
331 /****************************************************************************
332 check if we can open a file with a share mode
333 ****************************************************************************/
335 static int check_share_mode( share_mode_entry *share, int deny_mode,
337 BOOL fcbopen, int *flags)
339 int old_open_mode = GET_OPEN_MODE(share->share_mode);
340 int old_deny_mode = GET_DENY_MODE(share->share_mode);
343 * Don't allow any open once the delete on close flag has been
347 if(GET_DELETE_ON_CLOSE_FLAG(share->share_mode))
349 DEBUG(5,("check_share_mode: Failing open on file %s as delete on close flag is set.\n",
351 unix_ERR_class = ERRDOS;
352 unix_ERR_code = ERRnoaccess;
357 int access_allowed = access_table(deny_mode,old_deny_mode,old_open_mode,
358 (share->pid == getpid()),is_executable(fname));
360 if ((access_allowed == AFAIL) ||
361 (!fcbopen && (access_allowed == AREAD && *flags == O_RDWR)) ||
362 (access_allowed == AREAD && *flags != O_RDONLY) ||
363 (access_allowed == AWRITE && *flags != O_WRONLY))
365 DEBUG(2,("Share violation on file (%d,%d,%d,%d,%s,fcbopen = %d, flags = %d) = %d\n",
366 deny_mode,old_deny_mode,old_open_mode,
367 (int)share->pid,fname, fcbopen, *flags, access_allowed));
369 unix_ERR_class = ERRDOS;
370 unix_ERR_code = ERRbadshare;
375 if (access_allowed == AREAD)
378 if (access_allowed == AWRITE)
386 /****************************************************************************
387 open a file with a share mode
388 ****************************************************************************/
390 void open_file_shared(files_struct *fsp,connection_struct *conn,char *fname,int share_mode,int ofun,
391 mode_t mode,int oplock_request, int *Access,int *action)
395 int deny_mode = GET_DENY_MODE(share_mode);
396 BOOL allow_share_delete = GET_ALLOW_SHARE_DELETE(share_mode);
397 SMB_STRUCT_STAT sbuf;
398 BOOL file_existed = vfs_file_exist(conn, fname, &sbuf);
399 BOOL share_locked = False;
400 BOOL fcbopen = False;
404 int num_share_modes = 0;
405 int oplock_contention_count = 0;
406 BOOL all_current_opens_are_level_II = False;
411 /* printers are handled completely differently. Most of the passed parameters are
413 *Access = DOS_OPEN_WRONLY;
414 *action = FILE_WAS_CREATED;
415 print_open_file(fsp, conn, fname);
419 DEBUG(10,("open_file_shared: fname = %s, share_mode = %x, ofun = %x, mode = %o, oplock request = %d\n",
420 fname, share_mode, ofun, (int)mode, oplock_request ));
422 if (!check_name(fname,conn)) {
426 /* ignore any oplock requests if oplocks are disabled */
427 if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break) {
431 /* this is for OS/2 EAs - try and say we don't support them */
432 if (strstr(fname,".+,;=[]."))
434 unix_ERR_class = ERRDOS;
435 /* OS/2 Workplace shell fix may be main code stream in a later release. */
436 #if 1 /* OS2_WPS_FIX - Recent versions of OS/2 need this. */
437 unix_ERR_code = ERRcannotopen;
438 #else /* OS2_WPS_FIX */
439 unix_ERR_code = ERROR_EAS_NOT_SUPPORTED;
440 #endif /* OS2_WPS_FIX */
442 DEBUG(5,("open_file_shared: OS/2 EA's are not supported.\n"));
446 if ((GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_FAIL) && file_existed)
448 DEBUG(5,("open_file_shared: create new requested for file %s and file already exists.\n",
454 if (GET_FILE_CREATE_DISPOSITION(ofun) == FILE_CREATE_IF_NOT_EXIST)
457 if (GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_TRUNCATE)
460 if (GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_FAIL)
463 /* note that we ignore the append flag as
464 append does not mean the same thing under dos and unix */
466 switch (GET_OPEN_MODE(share_mode))
468 case DOS_OPEN_WRONLY:
484 if (GET_FILE_SYNC_OPENMODE(share_mode)) {
489 if (flags != O_RDONLY && file_existed &&
490 (!CAN_WRITE(conn) || IS_DOS_READONLY(dos_mode(conn,fname,&sbuf))))
494 DEBUG(5,("open_file_shared: read/write access requested for file %s on read only %s\n",
495 fname, !CAN_WRITE(conn) ? "share" : "file" ));
502 if (deny_mode > DENY_NONE && deny_mode!=DENY_FCB)
504 DEBUG(2,("Invalid deny mode %d on file %s\n",deny_mode,fname));
509 if (lp_share_modes(SNUM(conn)))
512 share_mode_entry *old_shares = 0;
518 lock_share_entry(conn, dev, inode);
520 num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
524 * Check if the share modes will give us access.
527 if(share_locked && (num_share_modes != 0))
534 broke_oplock = False;
535 all_current_opens_are_level_II = True;
537 for(i = 0; i < num_share_modes; i++)
539 share_mode_entry *share_entry = &old_shares[i];
542 * By observation of NetBench, oplocks are broken *before* share
543 * modes are checked. This allows a file to be closed by the client
544 * if the share mode would deny access and the client has an oplock.
545 * Check if someone has an oplock on this file. If so we must break
546 * it before continuing.
548 if((oplock_request && EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) ||
549 (!oplock_request && (share_entry->op_type != NO_OPLOCK)))
552 DEBUG(5,("open_file_shared: breaking oplock (%x) on file %s, \
553 dev = %x, inode = %.0f\n", share_entry->op_type, fname, (unsigned int)dev, (double)inode));
555 /* Oplock break.... */
556 unlock_share_entry(conn, dev, inode);
557 if(request_oplock_break(share_entry, dev, inode) == False)
559 free((char *)old_shares);
561 DEBUG(0,("open_file_shared: FAILED when breaking oplock (%x) on file %s, \
562 dev = %x, inode = %.0f\n", old_shares[i].op_type, fname, (unsigned int)dev, (double)inode));
565 unix_ERR_class = ERRDOS;
566 unix_ERR_code = ERRbadshare;
569 lock_share_entry(conn, dev, inode);
571 all_current_opens_are_level_II = False;
573 } else if (!LEVEL_II_OPLOCK_TYPE(share_entry->op_type)) {
574 all_current_opens_are_level_II = False;
577 /* someone else has a share lock on it, check to see
579 if(check_share_mode(share_entry, deny_mode, fname, fcbopen, &flags) == False)
581 free((char *)old_shares);
582 unlock_share_entry(conn, dev, inode);
591 free((char *)old_shares);
592 num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
593 oplock_contention_count++;
595 } while(broke_oplock);
599 free((char *)old_shares);
603 * Refuse to grant an oplock in case the contention limit is
604 * reached when going through the lock list multiple times.
607 if(oplock_contention_count >= lp_oplock_contention_limit(SNUM(conn)))
610 DEBUG(4,("open_file_shared: oplock contention = %d. Not granting oplock.\n",
611 oplock_contention_count ));
614 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o\n",
615 flags,flags2,(int)mode));
617 open_file(fsp,conn,fname,flags|(flags2&~(O_TRUNC)),mode);
618 if (!fsp->open && flags==O_RDWR && errno != ENOENT && fcbopen) {
620 open_file(fsp,conn,fname,flags,mode);
627 if((share_locked == False) && lp_share_modes(SNUM(conn)))
629 /* We created the file - thus we must now lock the share entry before creating it. */
630 lock_share_entry_fsp(fsp);
637 open_mode = DOS_OPEN_RDONLY;
640 open_mode = DOS_OPEN_RDWR;
643 open_mode = DOS_OPEN_WRONLY;
647 fsp->share_mode = SET_DENY_MODE(deny_mode) |
648 SET_OPEN_MODE(open_mode) |
649 SET_ALLOW_SHARE_DELETE(allow_share_delete);
652 (*Access) = open_mode;
656 if (file_existed && !(flags2 & O_TRUNC)) *action = FILE_WAS_OPENED;
657 if (!file_existed) *action = FILE_WAS_CREATED;
658 if (file_existed && (flags2 & O_TRUNC)) *action = FILE_WAS_OVERWRITTEN;
660 /* We must create the share mode entry before truncate as
661 truncate can fail due to locking and have to close the
662 file (which expects the share_mode_entry to be there).
664 if (lp_share_modes(SNUM(conn)))
669 * Setup the oplock info in both the shared memory and
673 if(oplock_request && (num_share_modes == 0) &&
674 !IS_VETO_OPLOCK_PATH(conn,fname) && set_file_oplock(fsp, oplock_request) ) {
675 port = global_oplock_port;
676 } else if (oplock_request && all_current_opens_are_level_II) {
677 port = global_oplock_port;
678 oplock_request = LEVEL_II_OPLOCK;
679 set_file_oplock(fsp, oplock_request);
685 set_share_mode(fsp, port, oplock_request);
688 if ((flags2&O_TRUNC) && file_existed)
689 truncate_unless_locked(fsp,conn,token,&share_locked);
692 if (share_locked && lp_share_modes(SNUM(conn)))
693 unlock_share_entry_fsp(fsp);
696 /****************************************************************************
697 Open a file for permissions read only. Return a pseudo file entry
698 with the 'stat_open' flag set
699 ****************************************************************************/
701 int open_file_stat(files_struct *fsp,connection_struct *conn,
702 char *fname, int smb_ofun, SMB_STRUCT_STAT *pst, int *action)
704 extern struct current_user current_user;
706 if(conn->vfs_ops.stat(dos_to_unix(fname, False), pst) < 0) {
707 DEBUG(0,("open_file_stat: unable to stat name = %s. Error was %s\n",
708 fname, strerror(errno) ));
712 if(S_ISDIR(pst->st_mode)) {
713 DEBUG(0,("open_file_stat: %s is a directory !\n", fname ));
717 *action = FILE_WAS_OPENED;
719 DEBUG(5,("open_file_stat: opening file %s as a stat entry\n", fname));
722 * Setup the files_struct for it.
726 conn->num_files_open++;
728 GetTimeOfDay(&fsp->open_time);
729 fsp->vuid = current_user.vuid;
733 fsp->can_lock = False;
734 fsp->can_read = False;
735 fsp->can_write = False;
737 fsp->print_file = False;
738 fsp->modified = False;
739 fsp->oplock_type = NO_OPLOCK;
740 fsp->sent_oplock_break = NO_BREAK_SENT;
741 fsp->is_directory = False;
742 fsp->stat_open = True;
743 fsp->directory_delete_on_close = False;
746 * Note that the file name here is the *untranslated* name
747 * ie. it is still in the DOS codepage sent from the client.
748 * All use of this filename will pass though the sys_xxxx
749 * functions which will do the dos_to_unix translation before
750 * mapping into a UNIX filename. JRA.
752 string_set(&fsp->fsp_name,fname);
753 fsp->wbmpx_ptr = NULL;
754 fsp->wcp = NULL; /* Write cache pointer. */
759 /****************************************************************************
760 Open a directory from an NT SMB call.
761 ****************************************************************************/
763 int open_directory(files_struct *fsp,connection_struct *conn,
764 char *fname, int smb_ofun, mode_t unixmode, int *action)
766 extern struct current_user current_user;
768 BOOL got_stat = False;
770 if(conn->vfs_ops.stat(dos_to_unix(fname, False), &st) == 0) {
774 if (got_stat && (GET_FILE_OPEN_DISPOSITION(smb_ofun) == FILE_EXISTS_FAIL)) {
775 errno = EEXIST; /* Setup so correct error is returned to client. */
779 if (GET_FILE_CREATE_DISPOSITION(smb_ofun) == FILE_CREATE_IF_NOT_EXIST) {
783 if(!S_ISDIR(st.st_mode)) {
784 DEBUG(0,("open_directory: %s is not a directory !\n", fname ));
788 *action = FILE_WAS_OPENED;
793 * Try and create the directory.
796 if(!CAN_WRITE(conn)) {
797 DEBUG(2,("open_directory: failing create on read-only share\n"));
802 if(conn->vfs_ops.mkdir(dos_to_unix(fname, False),
803 unix_mode(conn,aDIR, fname)) < 0) {
804 DEBUG(0,("open_directory: unable to create %s. Error was %s\n",
805 fname, strerror(errno) ));
808 *action = FILE_WAS_CREATED;
814 * Don't create - just check that it *was* a directory.
818 DEBUG(0,("open_directory: unable to stat name = %s. Error was %s\n",
819 fname, strerror(errno) ));
823 if(!S_ISDIR(st.st_mode)) {
824 DEBUG(0,("open_directory: %s is not a directory !\n", fname ));
828 *action = FILE_WAS_OPENED;
831 DEBUG(5,("open_directory: opening directory %s\n",
835 * Setup the files_struct for it.
839 conn->num_files_open++;
841 GetTimeOfDay(&fsp->open_time);
842 fsp->vuid = current_user.vuid;
846 fsp->can_lock = True;
847 fsp->can_read = False;
848 fsp->can_write = False;
850 fsp->print_file = False;
851 fsp->modified = False;
852 fsp->oplock_type = NO_OPLOCK;
853 fsp->sent_oplock_break = NO_BREAK_SENT;
854 fsp->is_directory = True;
855 fsp->directory_delete_on_close = False;
858 * Note that the file name here is the *untranslated* name
859 * ie. it is still in the DOS codepage sent from the client.
860 * All use of this filename will pass though the sys_xxxx
861 * functions which will do the dos_to_unix translation before
862 * mapping into a UNIX filename. JRA.
864 string_set(&fsp->fsp_name,fname);
865 fsp->wbmpx_ptr = NULL;
870 /*******************************************************************
871 Check if the share mode on a file allows it to be deleted or unlinked.
872 Return True if sharing doesn't prevent the operation.
873 ********************************************************************/
875 BOOL check_file_sharing(connection_struct *conn,char *fname, BOOL rename_op)
879 share_mode_entry *old_shares = 0;
881 SMB_STRUCT_STAT sbuf;
882 pid_t pid = getpid();
886 if(!lp_share_modes(SNUM(conn)))
889 if (conn->vfs_ops.stat(dos_to_unix(fname,False),&sbuf) == -1) return(True);
894 lock_share_entry(conn, dev, inode);
895 num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
898 * Check if the share modes will give us access.
901 if(num_share_modes != 0)
908 broke_oplock = False;
909 for(i = 0; i < num_share_modes; i++)
911 share_mode_entry *share_entry = &old_shares[i];
914 * Break oplocks before checking share modes. See comment in
915 * open_file_shared for details.
916 * Check if someone has an oplock on this file. If so we must
917 * break it before continuing.
919 if(BATCH_OPLOCK_TYPE(share_entry->op_type))
924 /* JRA. Try removing this code to see if the new oplock changes
925 fix the problem. I'm dubious, but Andrew is recommending we
930 * It appears that the NT redirector may have a bug, in that
931 * it tries to do an SMBmv on a file that it has open with a
932 * batch oplock, and then fails to respond to the oplock break
933 * request. This only seems to occur when the client is doing an
934 * SMBmv to the smbd it is using - thus we try and detect this
935 * condition by checking if the file being moved is open and oplocked by
936 * this smbd process, and then not sending the oplock break in this
937 * special case. If the file was open with a deny mode that
938 * prevents the move the SMBmv will fail anyway with a share
939 * violation error. JRA.
941 if(rename_op && (share_entry->pid == pid))
944 DEBUG(0,("check_file_sharing: NT redirector workaround - rename attempted on \
945 batch oplocked file %s, dev = %x, inode = %.0f\n", fname, (unsigned int)dev, (double)inode));
948 * This next line is a test that allows the deny-mode
949 * processing to be skipped. This seems to be needed as
950 * NT insists on the rename succeeding (in Office 9x no less !).
951 * This should be removed as soon as (a) MS fix the redirector
952 * bug or (b) NT SMB support in Samba makes NT not issue the
953 * call (as is my fervent hope). JRA.
961 DEBUG(5,("check_file_sharing: breaking oplock (%x) on file %s, \
962 dev = %x, inode = %.0f\n", share_entry->op_type, fname, (unsigned int)dev, (double)inode));
964 /* Oplock break.... */
965 unlock_share_entry(conn, dev, inode);
966 if(request_oplock_break(share_entry, dev, inode) == False)
968 free((char *)old_shares);
970 DEBUG(0,("check_file_sharing: FAILED when breaking oplock (%x) on file %s, \
971 dev = %x, inode = %.0f\n", old_shares[i].op_type, fname, (unsigned int)dev, (double)inode));
975 lock_share_entry(conn, dev, inode);
982 * If this is a delete request and ALLOW_SHARE_DELETE is set then allow
983 * this to proceed. This takes precedence over share modes.
986 if(!rename_op && GET_ALLOW_SHARE_DELETE(share_entry->share_mode))
990 * Someone else has a share lock on it, check to see
994 if ((GET_DENY_MODE(share_entry->share_mode) != DENY_DOS) ||
995 (share_entry->pid != pid))
1002 free((char *)old_shares);
1003 num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
1005 } while(broke_oplock);
1008 /* XXXX exactly what share mode combinations should be allowed for
1009 deleting/renaming? */
1011 * If we got here then either there were no share modes or
1012 * all share modes were DENY_DOS and the pid == getpid() or
1013 * delete access was requested and all share modes had the
1014 * ALLOW_SHARE_DELETE bit set (takes precedence over other
1022 unlock_share_entry(conn, dev, inode);
1023 if(old_shares != NULL)
1024 free((char *)old_shares);