/* * Deal with a reply when a break-to-level II was sent. */ bool downgrade_oplock(files_struct *fsp) { bool ret; struct share_mode_lock *lck; struct byte_range_lock *brl; DEBUG(10, ("downgrade_oplock called for %s\n", fsp_str_dbg(fsp))); lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id); if (lck == NULL) { DEBUG(0,("downgrade_oplock: failed to lock share entry for " "file %s\n", fsp_str_dbg(fsp))); return False; } ret = downgrade_share_oplock(lck, fsp); if (!ret) { DEBUG(0,("downgrade_oplock: failed to downgrade share oplock " "for file %s, %s, file_id %s\n", fsp_str_dbg(fsp), fsp_fnum_dbg(fsp), file_id_string_tos(&fsp->file_id))); } downgrade_file_oplock(fsp); brl = brl_get_locks(talloc_tos(), fsp); if (brl != NULL) { brl_set_have_read_oplocks(brl, true); TALLOC_FREE(brl); } TALLOC_FREE(lck); return ret; }
/* * Deal with a reply when a break-to-level II was sent. */ bool downgrade_oplock(files_struct *fsp) { bool ret; struct share_mode_lock *lck; DEBUG(10, ("downgrade_oplock called for %s\n", fsp_str_dbg(fsp))); lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id); if (lck == NULL) { DEBUG(0,("downgrade_oplock: failed to lock share entry for " "file %s\n", fsp_str_dbg(fsp))); return False; } ret = downgrade_share_oplock(lck, fsp); if (!ret) { DEBUG(0,("downgrade_oplock: failed to downgrade share oplock " "for file %s, %s, file_id %s\n", fsp_str_dbg(fsp), fsp_fnum_dbg(fsp), file_id_string_tos(&fsp->file_id))); } downgrade_file_oplock(fsp); ret = update_num_read_oplocks(fsp, lck); if (!ret) { DEBUG(0, ("%s: update_num_read_oplocks failed for " "file %s, %s, %s\n", __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp), file_id_string_tos(&fsp->file_id))); } TALLOC_FREE(lck); return ret; }
static NTSTATUS update_write_time_on_close(struct files_struct *fsp) { struct smb_file_time ft; NTSTATUS status; struct share_mode_lock *lck = NULL; ZERO_STRUCT(ft); if (!fsp->update_write_time_on_close) { return NT_STATUS_OK; } if (null_timespec(fsp->close_write_time)) { fsp->close_write_time = timespec_current(); } /* Ensure we have a valid stat struct for the source. */ status = vfs_stat_fsp(fsp); if (!NT_STATUS_IS_OK(status)) { return status; } if (!VALID_STAT(fsp->fsp_name->st)) { /* if it doesn't seem to be a real file */ return NT_STATUS_OK; } /* On close if we're changing the real file time we * must update it in the open file db too. */ (void)set_write_time(fsp->file_id, fsp->close_write_time); lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id); if (lck) { /* Close write times overwrite sticky write times so we must replace any sticky write time here. */ if (!null_timespec(lck->data->changed_write_time)) { (void)set_sticky_write_time(fsp->file_id, fsp->close_write_time); } TALLOC_FREE(lck); } ft.mtime = fsp->close_write_time; /* As this is a close based update, we are not directly changing the file attributes from a client call, but indirectly from a write. */ status = smb_set_file_time(fsp->conn, fsp, fsp->fsp_name, &ft, false); if (!NT_STATUS_IS_OK(status)) { DEBUG(10,("update_write_time_on_close: smb_set_file_time " "on file %s returned %s\n", fsp_str_dbg(fsp), nt_errstr(status))); return status; } return status; }
bool set_write_time(struct file_id fileid, struct timespec write_time) { struct share_mode_lock *lck; DEBUG(5,("set_write_time: %s id=%s\n", timestring(talloc_tos(), convert_timespec_to_time_t(write_time)), file_id_string_tos(&fileid))); lck = get_existing_share_mode_lock(talloc_tos(), fileid); if (lck == NULL) { return False; } if (timespec_compare(&lck->data->old_write_time, &write_time) != 0) { lck->data->modified = True; lck->data->old_write_time = write_time; } TALLOC_FREE(lck); return True; }
bool set_delete_on_close(files_struct *fsp, bool delete_on_close, const struct security_token *nt_tok, const struct security_unix_token *tok) { struct share_mode_lock *lck; DEBUG(10,("set_delete_on_close: %s delete on close flag for " "%s, file %s\n", delete_on_close ? "Adding" : "Removing", fsp_fnum_dbg(fsp), fsp_str_dbg(fsp))); lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id); if (lck == NULL) { return False; } if (delete_on_close) { set_delete_on_close_lck(fsp, lck, true, nt_tok, tok); } else { set_delete_on_close_lck(fsp, lck, false, NULL, NULL); } if (fsp->is_directory) { SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name)); send_stat_cache_delete_message(fsp->conn->sconn->msg_ctx, fsp->fsp_name->base_name); } TALLOC_FREE(lck); fsp->delete_on_close = delete_on_close; return True; }
bool remove_oplock(files_struct *fsp) { bool ret; struct share_mode_lock *lck; DEBUG(10, ("remove_oplock called for %s\n", fsp_str_dbg(fsp))); /* Remove the oplock flag from the sharemode. */ lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id); if (lck == NULL) { DEBUG(0,("remove_oplock: failed to lock share entry for " "file %s\n", fsp_str_dbg(fsp))); return False; } ret = remove_share_oplock(lck, fsp); if (!ret) { DEBUG(0,("remove_oplock: failed to remove share oplock for " "file %s, %s, %s\n", fsp_str_dbg(fsp), fsp_fnum_dbg(fsp), file_id_string_tos(&fsp->file_id))); } release_file_oplock(fsp); ret = update_num_read_oplocks(fsp, lck); if (!ret) { DEBUG(0, ("%s: update_num_read_oplocks failed for " "file %s, %s, %s\n", __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp), file_id_string_tos(&fsp->file_id))); } TALLOC_FREE(lck); return ret; }
static void lease_timeout_handler(struct tevent_context *ctx, struct tevent_timer *te, struct timeval now, void *private_data) { struct fsp_lease *lease = talloc_get_type_abort(private_data, struct fsp_lease); struct files_struct *fsp; struct share_mode_lock *lck; uint16_t old_epoch = lease->lease.lease_epoch; fsp = file_find_one_fsp_from_lease_key(lease->sconn, &lease->lease.lease_key); if (fsp == NULL) { /* race? */ TALLOC_FREE(lease->timeout); return; } lck = get_existing_share_mode_lock( talloc_tos(), fsp->file_id); if (lck == NULL) { /* race? */ TALLOC_FREE(lease->timeout); return; } fsp_lease_update(lck, fsp_client_guid(fsp), lease); if (lease->lease.lease_epoch != old_epoch) { /* * If the epoch changed we need to wait for * the next timeout to happen. */ DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n", fsp_str_dbg(fsp))); TALLOC_FREE(lck); return; } if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) { /* * If the epoch changed we need to wait for * the next timeout to happen. */ DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n", fsp_str_dbg(fsp))); TALLOC_FREE(lck); return; } DEBUG(1, ("lease break timed out for file %s -- replying anyway\n", fsp_str_dbg(fsp))); (void)downgrade_lease(lease->sconn->client->connections, 1, &fsp->file_id, &lease->lease.lease_key, SMB2_LEASE_NONE); TALLOC_FREE(lck); }
static NTSTATUS update_write_time_on_close(struct files_struct *fsp) { struct smb_file_time ft; NTSTATUS status; struct share_mode_lock *lck = NULL; ZERO_STRUCT(ft); if (!fsp->update_write_time_on_close) { return NT_STATUS_OK; } if (null_timespec(fsp->close_write_time)) { fsp->close_write_time = timespec_current(); } /* Ensure we have a valid stat struct for the source. */ status = vfs_stat_fsp(fsp); if (!NT_STATUS_IS_OK(status)) { return status; } if (!VALID_STAT(fsp->fsp_name->st)) { /* if it doesn't seem to be a real file */ return NT_STATUS_OK; } /* * get_existing_share_mode_lock() isn't really the right * call here, as we're being called after * close_remove_share_mode() inside close_normal_file() * so it's quite normal to not have an existing share * mode here. However, get_share_mode_lock() doesn't * work because that will create a new share mode if * one doesn't exist - so stick with this call (just * ignore any error we get if the share mode doesn't * exist. */ lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id); if (lck) { /* On close if we're changing the real file time we * must update it in the open file db too. */ (void)set_write_time(fsp->file_id, fsp->close_write_time); /* Close write times overwrite sticky write times so we must replace any sticky write time here. */ if (!null_timespec(lck->data->changed_write_time)) { (void)set_sticky_write_time(fsp->file_id, fsp->close_write_time); } TALLOC_FREE(lck); } ft.mtime = fsp->close_write_time; /* As this is a close based update, we are not directly changing the file attributes from a client call, but indirectly from a write. */ status = smb_set_file_time(fsp->conn, fsp, fsp->fsp_name, &ft, false); if (!NT_STATUS_IS_OK(status)) { DEBUG(10,("update_write_time_on_close: smb_set_file_time " "on file %s returned %s\n", fsp_str_dbg(fsp), nt_errstr(status))); return status; } return status; }
static NTSTATUS close_remove_share_mode(files_struct *fsp, enum file_close_type close_type) { connection_struct *conn = fsp->conn; struct server_id self = messaging_server_id(conn->sconn->msg_ctx); bool delete_file = false; bool changed_user = false; struct share_mode_lock *lck = NULL; NTSTATUS status = NT_STATUS_OK; NTSTATUS tmp_status; struct file_id id; const struct security_unix_token *del_token = NULL; const struct security_token *del_nt_token = NULL; bool got_tokens = false; bool normal_close; int ret_flock, retries = 1; /* Ensure any pending write time updates are done. */ if (fsp->update_write_time_event) { update_write_time_handler(fsp->conn->sconn->ev_ctx, fsp->update_write_time_event, timeval_current(), (void *)fsp); } /* * Lock the share entries, and determine if we should delete * on close. If so delete whilst the lock is still in effect. * This prevents race conditions with the file being created. JRA. */ lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id); if (lck == NULL) { DEBUG(0, ("close_remove_share_mode: Could not get share mode " "lock for file %s\n", fsp_str_dbg(fsp))); return NT_STATUS_INVALID_PARAMETER; } if (fsp->write_time_forced) { DEBUG(10,("close_remove_share_mode: write time forced " "for file %s\n", fsp_str_dbg(fsp))); set_close_write_time(fsp, lck->data->changed_write_time); } else if (fsp->update_write_time_on_close) { /* Someone had a pending write. */ if (null_timespec(fsp->close_write_time)) { DEBUG(10,("close_remove_share_mode: update to current time " "for file %s\n", fsp_str_dbg(fsp))); /* Update to current time due to "normal" write. */ set_close_write_time(fsp, timespec_current()); } else { DEBUG(10,("close_remove_share_mode: write time pending " "for file %s\n", fsp_str_dbg(fsp))); /* Update to time set on close call. */ set_close_write_time(fsp, fsp->close_write_time); } } if (fsp->initial_delete_on_close && !is_delete_on_close_set(lck, fsp->name_hash)) { bool became_user = False; /* Initial delete on close was set and no one else * wrote a real delete on close. */ if (get_current_vuid(conn) != fsp->vuid) { become_user(conn, fsp->vuid); became_user = True; } fsp->delete_on_close = true; set_delete_on_close_lck(fsp, lck, get_current_nttok(conn), get_current_utok(conn)); if (became_user) { unbecome_user(); } } delete_file = is_delete_on_close_set(lck, fsp->name_hash); if (delete_file) { int i; /* See if others still have the file open via this pathname. If this is the case, then don't delete. If all opens are POSIX delete now. */ for (i=0; i<lck->data->num_share_modes; i++) { struct share_mode_entry *e = &lck->data->share_modes[i]; if (!is_valid_share_mode_entry(e)) { continue; } if (e->name_hash != fsp->name_hash) { continue; } if ((fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) { continue; } if (serverid_equal(&self, &e->pid) && (e->share_file_id == fsp->fh->gen_id)) { continue; } if (share_mode_stale_pid(lck->data, i)) { continue; } delete_file = False; break; } } /* * NT can set delete_on_close of the last open * reference to a file. */ normal_close = (close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE); if (!normal_close || !delete_file) { status = NT_STATUS_OK; goto done; } /* * Ok, we have to delete the file */ DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set " "- deleting file.\n", fsp_str_dbg(fsp))); /* * Don't try to update the write time when we delete the file */ fsp->update_write_time_on_close = false; got_tokens = get_delete_on_close_token(lck, fsp->name_hash, &del_nt_token, &del_token); SMB_ASSERT(got_tokens); if (!unix_token_equal(del_token, get_current_utok(conn))) { /* Become the user who requested the delete. */ DEBUG(5,("close_remove_share_mode: file %s. " "Change user to uid %u\n", fsp_str_dbg(fsp), (unsigned int)del_token->uid)); if (!push_sec_ctx()) { smb_panic("close_remove_share_mode: file %s. failed to push " "sec_ctx.\n"); } set_sec_ctx(del_token->uid, del_token->gid, del_token->ngroups, del_token->groups, del_nt_token); changed_user = true; } /* We can only delete the file if the name we have is still valid and hasn't been renamed. */ tmp_status = vfs_stat_fsp(fsp); if (!NT_STATUS_IS_OK(tmp_status)) { DEBUG(5,("close_remove_share_mode: file %s. Delete on close " "was set and stat failed with error %s\n", fsp_str_dbg(fsp), nt_errstr(tmp_status))); /* * Don't save the errno here, we ignore this error */ goto done; } id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st); if (!file_id_equal(&fsp->file_id, &id)) { DEBUG(5,("close_remove_share_mode: file %s. Delete on close " "was set and dev and/or inode does not match\n", fsp_str_dbg(fsp))); DEBUG(5,("close_remove_share_mode: file %s. stored file_id %s, " "stat file_id %s\n", fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id), file_id_string_tos(&id))); /* * Don't save the errno here, we ignore this error */ goto done; } if ((conn->fs_capabilities & FILE_NAMED_STREAMS) && !is_ntfs_stream_smb_fname(fsp->fsp_name)) { status = delete_all_streams(conn, fsp->fsp_name->base_name); if (!NT_STATUS_IS_OK(status)) { DEBUG(5, ("delete_all_streams failed: %s\n", nt_errstr(status))); goto done; } } retry_delete: /* temporary files with delete on close set will not be deleted on a * cifs share using a netapp backend since they are opened with * read + write access mask. * close the file to allow the delete. */ if (fsp->can_write && !S_ISDIR(fsp->fsp_name->st.st_ex_mode) && fsp->fh->ref_count == 1 && retries) { status = fd_close(fsp); if (!NT_STATUS_IS_OK(status)) { DEBUG(3, ("close_remove_share_mode: Error %s closing %s\n", nt_errstr(status), smb_fname_str_dbg(fsp->fsp_name))); goto skip_retry; } if (SMB_VFS_UNLINK(conn, fsp->fsp_name) != 0) { /* * This call can potentially fail as another smbd may * have had the file open with delete on close set and * deleted it when its last reference to this file * went away. Hence we log this but not at debug level * zero. */ DEBUG(5,("close_remove_share_mode: file %s. Delete on close " "was set and unlink failed with error %s\n", fsp_str_dbg(fsp), strerror(errno))); status = map_nt_error_from_unix(errno); retries = 0; goto retry_delete; } } else { if (SMB_VFS_UNLINK(conn, fsp->fsp_name) != 0) { /* * This call can potentially fail as another smbd may * have had the file open with delete on close set and * deleted it when its last reference to this file * went away. Hence we log this but not at debug level * zero. */ DEBUG(5,("close_remove_share_mode: file %s. Delete on close " "was set and unlink failed with error %s\n", fsp_str_dbg(fsp), strerror(errno))); status = map_nt_error_from_unix(errno); } } /* As we now have POSIX opens which can unlink * with other open files we may have taken * this code path with more than one share mode * entry - ensure we only delete once by resetting * the delete on close flag. JRA. */ skip_retry: fsp->delete_on_close = false; reset_delete_on_close_lck(fsp, lck); done: if (changed_user) { /* unbecome user. */ pop_sec_ctx(); } /* remove filesystem sharemodes */ ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, 0, 0); if (ret_flock == -1) { DEBUG(2, ("close_remove_share_mode: removing kernel flock for " "%s failed: %s\n", fsp_str_dbg(fsp), strerror(errno))); } if (!del_share_mode(lck, fsp)) { DEBUG(0, ("close_remove_share_mode: Could not delete share " "entry for file %s\n", fsp_str_dbg(fsp))); } TALLOC_FREE(lck); if (delete_file) { /* * Do the notification after we released the share * mode lock. Inside notify_fname we take out another * tdb lock. With ctdb also accessing our databases, * this can lead to deadlocks. Putting this notify * after the TALLOC_FREE(lck) above we avoid locking * two records simultaneously. Notifies are async and * informational only, so calling the notify_fname * without holding the share mode lock should not do * any harm. */ notify_fname(conn, NOTIFY_ACTION_REMOVED, FILE_NOTIFY_CHANGE_FILE_NAME, fsp->fsp_name->base_name); } return status; }
static NTSTATUS close_directory(struct smb_request *req, files_struct *fsp, enum file_close_type close_type) { struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx); struct share_mode_lock *lck = NULL; bool delete_dir = False; NTSTATUS status = NT_STATUS_OK; NTSTATUS status1 = NT_STATUS_OK; const struct security_token *del_nt_token = NULL; const struct security_unix_token *del_token = NULL; NTSTATUS notify_status; if (fsp->conn->sconn->using_smb2) { notify_status = STATUS_NOTIFY_CLEANUP; } else { notify_status = NT_STATUS_OK; } /* * NT can set delete_on_close of the last open * reference to a directory also. */ lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id); if (lck == NULL) { DEBUG(0, ("close_directory: Could not get share mode lock for " "%s\n", fsp_str_dbg(fsp))); return NT_STATUS_INVALID_PARAMETER; } if (fsp->initial_delete_on_close) { bool became_user = False; /* Initial delete on close was set - for * directories we don't care if anyone else * wrote a real delete on close. */ if (get_current_vuid(fsp->conn) != fsp->vuid) { become_user(fsp->conn, fsp->vuid); became_user = True; } send_stat_cache_delete_message(fsp->conn->sconn->msg_ctx, fsp->fsp_name->base_name); set_delete_on_close_lck(fsp, lck, get_current_nttok(fsp->conn), get_current_utok(fsp->conn)); fsp->delete_on_close = true; if (became_user) { unbecome_user(); } } delete_dir = get_delete_on_close_token(lck, fsp->name_hash, &del_nt_token, &del_token); if (delete_dir) { int i; /* See if others still have the dir open. If this is the * case, then don't delete. If all opens are POSIX delete now. */ for (i=0; i<lck->data->num_share_modes; i++) { struct share_mode_entry *e = &lck->data->share_modes[i]; if (is_valid_share_mode_entry(e) && e->name_hash == fsp->name_hash) { if ((fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) && (e->flags & SHARE_MODE_FLAG_POSIX_OPEN)) { continue; } if (serverid_equal(&self, &e->pid) && (e->share_file_id == fsp->fh->gen_id)) { continue; } if (share_mode_stale_pid(lck->data, i)) { continue; } delete_dir = False; break; } } } if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) && delete_dir) { /* Become the user who requested the delete. */ if (!push_sec_ctx()) { smb_panic("close_directory: failed to push sec_ctx.\n"); } set_sec_ctx(del_token->uid, del_token->gid, del_token->ngroups, del_token->groups, del_nt_token); if (!del_share_mode(lck, fsp)) { DEBUG(0, ("close_directory: Could not delete share entry for " "%s\n", fsp_str_dbg(fsp))); } TALLOC_FREE(lck); if ((fsp->conn->fs_capabilities & FILE_NAMED_STREAMS) && !is_ntfs_stream_smb_fname(fsp->fsp_name)) { status = delete_all_streams(fsp->conn, fsp->fsp_name->base_name); if (!NT_STATUS_IS_OK(status)) { DEBUG(5, ("delete_all_streams failed: %s\n", nt_errstr(status))); return status; } } status = rmdir_internals(talloc_tos(), fsp); DEBUG(5,("close_directory: %s. Delete on close was set - " "deleting directory returned %s.\n", fsp_str_dbg(fsp), nt_errstr(status))); /* unbecome user. */ pop_sec_ctx(); /* * Ensure we remove any change notify requests that would * now fail as the directory has been deleted. */ if (NT_STATUS_IS_OK(status)) { notify_status = NT_STATUS_DELETE_PENDING; } } else { if (!del_share_mode(lck, fsp)) { DEBUG(0, ("close_directory: Could not delete share entry for " "%s\n", fsp_str_dbg(fsp))); } TALLOC_FREE(lck); } remove_pending_change_notify_requests_by_fid(fsp, notify_status); status1 = fd_close(fsp); if (!NT_STATUS_IS_OK(status1)) { DEBUG(0, ("Could not close dir! fname=%s, fd=%d, err=%d=%s\n", fsp_str_dbg(fsp), fsp->fh->fd, errno, strerror(errno))); } /* * Do the code common to files and directories. */ close_filestruct(fsp); file_free(req, fsp); if (NT_STATUS_IS_OK(status) && !NT_STATUS_IS_OK(status1)) { status = status1; } return status; }
bool remove_oplock(files_struct *fsp) { bool ret; struct share_mode_lock *lck; DEBUG(10, ("remove_oplock called for %s\n", fsp_str_dbg(fsp))); /* Remove the oplock flag from the sharemode. */ lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id); if (lck == NULL) { DEBUG(0,("remove_oplock: failed to lock share entry for " "file %s\n", fsp_str_dbg(fsp))); return False; } if (fsp->oplock_type == LEVEL_II_OPLOCK) { /* * If we're the only LEVEL_II holder, we have to remove the * have_read_oplocks from the brlock entry */ struct share_mode_data *data = lck->data; uint32_t i, num_level2; num_level2 = 0; for (i=0; i<data->num_share_modes; i++) { if (data->share_modes[i].op_type == LEVEL_II_OPLOCK) { num_level2 += 1; } if (num_level2 > 1) { /* * No need to count them all... */ break; } } if (num_level2 == 1) { /* * That's only us. We are dropping that level2 oplock, * so remove the brlock flag. */ struct byte_range_lock *brl; brl = brl_get_locks(talloc_tos(), fsp); if (brl) { brl_set_have_read_oplocks(brl, false); TALLOC_FREE(brl); } } } ret = remove_share_oplock(lck, fsp); if (!ret) { DEBUG(0,("remove_oplock: failed to remove share oplock for " "file %s, %s, %s\n", fsp_str_dbg(fsp), fsp_fnum_dbg(fsp), file_id_string_tos(&fsp->file_id))); } release_file_oplock(fsp); TALLOC_FREE(lck); return ret; }
bool run_cleanup3(int dummy) { struct cli_state *cli; const char *fname = "cleanup3"; uint16_t fnum; NTSTATUS status; struct share_mode_lock *lck; struct file_id id; size_t i; struct corruption_fns fns[] = { { corrupt_dummy, "no corruption" }, { invalidate_sharemode, "invalidate_sharemode" }, { create_duplicate_batch, "create_duplicate_batch" }, }; printf("CLEANUP3: Checking that a share mode is cleaned up on " "conflict\n"); for (i=0; i<ARRAY_SIZE(fns); i++) { printf("testing %s\n", fns[i].descr); if (!create_stale_share_mode_entry(fname, &id)) { printf("create_stale_entry failed\n"); return false; } printf("%d %d %d\n", (int)id.devid, (int)id.inode, (int)id.extid); if (!locking_init()) { printf("locking_init failed\n"); return false; } lck = get_existing_share_mode_lock(talloc_tos(), id); if (lck == NULL) { printf("get_existing_share_mode_lock failed\n"); return false; } if (lck->data->num_share_modes != 1) { printf("get_existing_share_mode_lock did clean up\n"); return false; } fns[i].fn(lck->data); TALLOC_FREE(lck); if (!torture_open_connection(&cli, 0)) { return false; } status = cli_openx(cli, fname, O_RDWR|O_CREAT, DENY_ALL, &fnum); if (!NT_STATUS_IS_OK(status)) { printf("open of %s failed (%s)\n", fname, nt_errstr(status)); return false; } lck = get_existing_share_mode_lock(talloc_tos(), id); if (lck == NULL) { printf("get_existing_share_mode_lock failed\n"); return false; } if (lck->data->num_share_modes != 1) { printf("conflicting open did not clean up\n"); return false; } TALLOC_FREE(lck); torture_close_connection(cli); } return true; }
NTSTATUS vfs_default_durable_reconnect(struct connection_struct *conn, struct smb_request *smb1req, struct smbXsrv_open *op, const DATA_BLOB old_cookie, TALLOC_CTX *mem_ctx, files_struct **result, DATA_BLOB *new_cookie) { struct share_mode_lock *lck; struct share_mode_entry *e; struct files_struct *fsp = NULL; NTSTATUS status; bool ok; int ret; int flags = 0; struct file_id file_id; struct smb_filename *smb_fname = NULL; enum ndr_err_code ndr_err; struct vfs_default_durable_cookie cookie; DATA_BLOB new_cookie_blob = data_blob_null; *result = NULL; *new_cookie = data_blob_null; if (!lp_durable_handles(SNUM(conn))) { return NT_STATUS_NOT_SUPPORTED; } /* * the checks for kernel oplocks * and similar things are done * in the vfs_default_durable_cookie() * call below. */ ZERO_STRUCT(cookie); ndr_err = ndr_pull_struct_blob(&old_cookie, talloc_tos(), &cookie, (ndr_pull_flags_fn_t)ndr_pull_vfs_default_durable_cookie); if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) { status = ndr_map_error2ntstatus(ndr_err); return status; } if (strcmp(cookie.magic, VFS_DEFAULT_DURABLE_COOKIE_MAGIC) != 0) { return NT_STATUS_INVALID_PARAMETER; } if (cookie.version != VFS_DEFAULT_DURABLE_COOKIE_VERSION) { return NT_STATUS_INVALID_PARAMETER; } if (!cookie.allow_reconnect) { return NT_STATUS_OBJECT_NAME_NOT_FOUND; } if (strcmp(cookie.servicepath, conn->connectpath) != 0) { return NT_STATUS_OBJECT_NAME_NOT_FOUND; } /* Create an smb_filename with stream_name == NULL. */ smb_fname = synthetic_smb_fname(talloc_tos(), cookie.base_name, NULL, NULL); if (smb_fname == NULL) { return NT_STATUS_NO_MEMORY; } ret = SMB_VFS_LSTAT(conn, smb_fname); if (ret == -1) { status = map_nt_error_from_unix_common(errno); DEBUG(1, ("Unable to lstat stream: %s => %s\n", smb_fname_str_dbg(smb_fname), nt_errstr(status))); return status; } if (!S_ISREG(smb_fname->st.st_ex_mode)) { return NT_STATUS_OBJECT_NAME_NOT_FOUND; } file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st); if (!file_id_equal(&cookie.id, &file_id)) { return NT_STATUS_OBJECT_NAME_NOT_FOUND; } /* * 1. check entry in locking.tdb */ lck = get_existing_share_mode_lock(mem_ctx, file_id); if (lck == NULL) { DEBUG(5, ("vfs_default_durable_reconnect: share-mode lock " "not obtained from db\n")); return NT_STATUS_OBJECT_NAME_NOT_FOUND; } if (lck->data->num_share_modes == 0) { DEBUG(1, ("vfs_default_durable_reconnect: Error: no share-mode " "entry in existing share mode lock\n")); TALLOC_FREE(lck); return NT_STATUS_INTERNAL_DB_ERROR; } if (lck->data->num_share_modes > 1) { /* * It can't be durable if there is more than one handle * on the file. */ DEBUG(5, ("vfs_default_durable_reconnect: more than one " "share-mode entry - can not be durable\n")); TALLOC_FREE(lck); return NT_STATUS_OBJECT_NAME_NOT_FOUND; } e = &lck->data->share_modes[0]; if (!server_id_is_disconnected(&e->pid)) { DEBUG(5, ("vfs_default_durable_reconnect: denying durable " "reconnect for handle that was not marked " "disconnected (e.g. smbd or cluster node died)\n")); TALLOC_FREE(lck); return NT_STATUS_OBJECT_NAME_NOT_FOUND; } if (e->share_file_id != op->global->open_persistent_id) { DEBUG(5, ("vfs_default_durable_reconnect: denying durable " "share_file_id changed %llu != %llu" "(e.g. another client had opened the file)\n", (unsigned long long)e->share_file_id, (unsigned long long)op->global->open_persistent_id)); TALLOC_FREE(lck); return NT_STATUS_OBJECT_NAME_NOT_FOUND; } if ((e->access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA)) && !CAN_WRITE(conn)) { DEBUG(5, ("vfs_default_durable_reconnect: denying durable " "share[%s] is not writeable anymore\n", lp_servicename(talloc_tos(), SNUM(conn)))); TALLOC_FREE(lck); return NT_STATUS_OBJECT_NAME_NOT_FOUND; } /* * 2. proceed with opening file */ status = fsp_new(conn, conn, &fsp); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("vfs_default_durable_reconnect: failed to create " "new fsp: %s\n", nt_errstr(status))); TALLOC_FREE(lck); return status; } fsp->fh->private_options = e->private_options; fsp->fh->gen_id = smbXsrv_open_hash(op); fsp->file_id = file_id; fsp->file_pid = smb1req->smbpid; fsp->vuid = smb1req->vuid; fsp->open_time = e->time; fsp->access_mask = e->access_mask; fsp->share_access = e->share_access; fsp->can_read = ((fsp->access_mask & (FILE_READ_DATA)) != 0); fsp->can_write = ((fsp->access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA)) != 0); fsp->fnum = op->local_id; /* * TODO: * Do we need to store the modified flag in the DB? */ fsp->modified = false; /* * no durables for directories */ fsp->is_directory = false; /* * For normal files, can_lock == !is_directory */ fsp->can_lock = true; /* * We do not support aio write behind for smb2 */ fsp->aio_write_behind = false; fsp->oplock_type = e->op_type; fsp->initial_allocation_size = cookie.initial_allocation_size; fsp->fh->position_information = cookie.position_information; fsp->update_write_time_triggered = cookie.update_write_time_triggered; fsp->update_write_time_on_close = cookie.update_write_time_on_close; fsp->write_time_forced = cookie.write_time_forced; fsp->close_write_time = cookie.close_write_time; status = fsp_set_smb_fname(fsp, smb_fname); if (!NT_STATUS_IS_OK(status)) { TALLOC_FREE(lck); fsp_free(fsp); DEBUG(0, ("vfs_default_durable_reconnect: " "fsp_set_smb_fname failed: %s\n", nt_errstr(status))); return status; } op->compat = fsp; fsp->op = op; e->pid = messaging_server_id(conn->sconn->msg_ctx); e->op_mid = smb1req->mid; e->share_file_id = fsp->fh->gen_id; ok = brl_reconnect_disconnected(fsp); if (!ok) { status = NT_STATUS_INTERNAL_ERROR; DEBUG(1, ("vfs_default_durable_reconnect: " "failed to reopen brlocks: %s\n", nt_errstr(status))); TALLOC_FREE(lck); op->compat = NULL; fsp_free(fsp); return status; } /* * TODO: properly calculate open flags */ if (fsp->can_write && fsp->can_read) { flags = O_RDWR; } else if (fsp->can_write) { flags = O_WRONLY; } else if (fsp->can_read) { flags = O_RDONLY; } status = fd_open(conn, fsp, flags, 0 /* mode */); if (!NT_STATUS_IS_OK(status)) { TALLOC_FREE(lck); DEBUG(1, ("vfs_default_durable_reconnect: failed to open " "file: %s\n", nt_errstr(status))); op->compat = NULL; fsp_free(fsp); return status; } /* * We now check the stat info stored in the cookie against * the current stat data from the file we just opened. * If any detail differs, we deny the durable reconnect, * because in that case it is very likely that someone * opened the file while the handle was disconnected, * which has to be interpreted as an oplock break. */ ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st); if (ret == -1) { status = map_nt_error_from_unix_common(errno); DEBUG(1, ("Unable to fstat stream: %s => %s\n", smb_fname_str_dbg(smb_fname), nt_errstr(status))); ret = SMB_VFS_CLOSE(fsp); if (ret == -1) { DEBUG(0, ("vfs_default_durable_reconnect: " "SMB_VFS_CLOSE failed (%s) - leaking file " "descriptor\n", strerror(errno))); } TALLOC_FREE(lck); op->compat = NULL; fsp_free(fsp); return status; } if (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) { ret = SMB_VFS_CLOSE(fsp); if (ret == -1) { DEBUG(0, ("vfs_default_durable_reconnect: " "SMB_VFS_CLOSE failed (%s) - leaking file " "descriptor\n", strerror(errno))); } TALLOC_FREE(lck); op->compat = NULL; fsp_free(fsp); return NT_STATUS_OBJECT_NAME_NOT_FOUND; } file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st); if (!file_id_equal(&cookie.id, &file_id)) { ret = SMB_VFS_CLOSE(fsp); if (ret == -1) { DEBUG(0, ("vfs_default_durable_reconnect: " "SMB_VFS_CLOSE failed (%s) - leaking file " "descriptor\n", strerror(errno))); } TALLOC_FREE(lck); op->compat = NULL; fsp_free(fsp); return NT_STATUS_OBJECT_NAME_NOT_FOUND; } ok = vfs_default_durable_reconnect_check_stat(&cookie.stat_info, &fsp->fsp_name->st, fsp_str_dbg(fsp)); if (!ok) { ret = SMB_VFS_CLOSE(fsp); if (ret == -1) { DEBUG(0, ("vfs_default_durable_reconnect: " "SMB_VFS_CLOSE failed (%s) - leaking file " "descriptor\n", strerror(errno))); } TALLOC_FREE(lck); op->compat = NULL; fsp_free(fsp); return NT_STATUS_OBJECT_NAME_NOT_FOUND; } status = set_file_oplock(fsp); if (!NT_STATUS_IS_OK(status)) { DEBUG(1, ("vfs_default_durable_reconnect failed to set oplock " "after opening file: %s\n", nt_errstr(status))); ret = SMB_VFS_CLOSE(fsp); if (ret == -1) { DEBUG(0, ("vfs_default_durable_reconnect: " "SMB_VFS_CLOSE failed (%s) - leaking file " "descriptor\n", strerror(errno))); } TALLOC_FREE(lck); op->compat = NULL; fsp_free(fsp); return status; } status = vfs_default_durable_cookie(fsp, mem_ctx, &new_cookie_blob); if (!NT_STATUS_IS_OK(status)) { TALLOC_FREE(lck); DEBUG(1, ("vfs_default_durable_reconnect: " "vfs_default_durable_cookie - %s\n", nt_errstr(status))); op->compat = NULL; fsp_free(fsp); return status; } smb1req->chain_fsp = fsp; smb1req->smb2req->compat_chain_fsp = fsp; DEBUG(10, ("vfs_default_durable_reconnect: opened file '%s'\n", fsp_str_dbg(fsp))); /* * release the sharemode lock: this writes the changes */ lck->data->modified = true; TALLOC_FREE(lck); *result = fsp; *new_cookie = new_cookie_blob; return NT_STATUS_OK; }
NTSTATUS vfs_default_durable_disconnect(struct files_struct *fsp, const DATA_BLOB old_cookie, TALLOC_CTX *mem_ctx, DATA_BLOB *new_cookie) { struct connection_struct *conn = fsp->conn; NTSTATUS status; enum ndr_err_code ndr_err; struct vfs_default_durable_cookie cookie; DATA_BLOB new_cookie_blob = data_blob_null; struct share_mode_lock *lck; bool ok; *new_cookie = data_blob_null; ZERO_STRUCT(cookie); ndr_err = ndr_pull_struct_blob(&old_cookie, talloc_tos(), &cookie, (ndr_pull_flags_fn_t)ndr_pull_vfs_default_durable_cookie); if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) { status = ndr_map_error2ntstatus(ndr_err); return status; } if (strcmp(cookie.magic, VFS_DEFAULT_DURABLE_COOKIE_MAGIC) != 0) { return NT_STATUS_INVALID_PARAMETER; } if (cookie.version != VFS_DEFAULT_DURABLE_COOKIE_VERSION) { return NT_STATUS_INVALID_PARAMETER; } if (!file_id_equal(&fsp->file_id, &cookie.id)) { return NT_STATUS_INVALID_PARAMETER; } if (!BATCH_OPLOCK_TYPE(fsp->oplock_type)) { return NT_STATUS_NOT_SUPPORTED; } /* * For now let it be simple and do not keep * delete on close files durable open */ if (fsp->initial_delete_on_close) { return NT_STATUS_NOT_SUPPORTED; } if (fsp->delete_on_close) { return NT_STATUS_NOT_SUPPORTED; } if (!VALID_STAT(fsp->fsp_name->st)) { return NT_STATUS_NOT_SUPPORTED; } if (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) { return NT_STATUS_NOT_SUPPORTED; } /* Ensure any pending write time updates are done. */ if (fsp->update_write_time_event) { update_write_time_handler(fsp->conn->sconn->ev_ctx, fsp->update_write_time_event, timeval_current(), (void *)fsp); } /* * The above checks are done in mark_share_mode_disconnected() too * but we want to avoid getting the lock if possible */ lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id); if (lck != NULL) { struct smb_file_time ft; ZERO_STRUCT(ft); if (fsp->write_time_forced) { ft.mtime = lck->data->changed_write_time; } else if (fsp->update_write_time_on_close) { if (null_timespec(fsp->close_write_time)) { ft.mtime = timespec_current(); } else { ft.mtime = fsp->close_write_time; } } if (!null_timespec(ft.mtime)) { round_timespec(conn->ts_res, &ft.mtime); file_ntimes(conn, fsp->fsp_name, &ft); } ok = mark_share_mode_disconnected(lck, fsp); if (!ok) { TALLOC_FREE(lck); } } if (lck != NULL) { ok = brl_mark_disconnected(fsp); if (!ok) { TALLOC_FREE(lck); } } if (lck == NULL) { return NT_STATUS_NOT_SUPPORTED; } TALLOC_FREE(lck); status = vfs_stat_fsp(fsp); if (!NT_STATUS_IS_OK(status)) { return status; } ZERO_STRUCT(cookie); cookie.allow_reconnect = true; cookie.id = fsp->file_id; cookie.servicepath = conn->connectpath; cookie.base_name = fsp->fsp_name->base_name; cookie.initial_allocation_size = fsp->initial_allocation_size; cookie.position_information = fsp->fh->position_information; cookie.update_write_time_triggered = fsp->update_write_time_triggered; cookie.update_write_time_on_close = fsp->update_write_time_on_close; cookie.write_time_forced = fsp->write_time_forced; cookie.close_write_time = fsp->close_write_time; cookie.stat_info.st_ex_dev = fsp->fsp_name->st.st_ex_dev; cookie.stat_info.st_ex_ino = fsp->fsp_name->st.st_ex_ino; cookie.stat_info.st_ex_mode = fsp->fsp_name->st.st_ex_mode; cookie.stat_info.st_ex_nlink = fsp->fsp_name->st.st_ex_nlink; cookie.stat_info.st_ex_uid = fsp->fsp_name->st.st_ex_uid; cookie.stat_info.st_ex_gid = fsp->fsp_name->st.st_ex_gid; cookie.stat_info.st_ex_rdev = fsp->fsp_name->st.st_ex_rdev; cookie.stat_info.st_ex_size = fsp->fsp_name->st.st_ex_size; cookie.stat_info.st_ex_atime = fsp->fsp_name->st.st_ex_atime; cookie.stat_info.st_ex_mtime = fsp->fsp_name->st.st_ex_mtime; cookie.stat_info.st_ex_ctime = fsp->fsp_name->st.st_ex_ctime; cookie.stat_info.st_ex_btime = fsp->fsp_name->st.st_ex_btime; cookie.stat_info.st_ex_calculated_birthtime = fsp->fsp_name->st.st_ex_calculated_birthtime; cookie.stat_info.st_ex_blksize = fsp->fsp_name->st.st_ex_blksize; cookie.stat_info.st_ex_blocks = fsp->fsp_name->st.st_ex_blocks; cookie.stat_info.st_ex_flags = fsp->fsp_name->st.st_ex_flags; cookie.stat_info.st_ex_mask = fsp->fsp_name->st.st_ex_mask; ndr_err = ndr_push_struct_blob(&new_cookie_blob, mem_ctx, &cookie, (ndr_push_flags_fn_t)ndr_push_vfs_default_durable_cookie); if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) { status = ndr_map_error2ntstatus(ndr_err); return status; } status = fd_close(fsp); if (!NT_STATUS_IS_OK(status)) { data_blob_free(&new_cookie_blob); return status; } *new_cookie = new_cookie_blob; return NT_STATUS_OK; }