2 Unix SMB/CIFS implementation.
3 Durable Handle default VFS implementation
5 Copyright (C) Stefan Metzmacher 2012
6 Copyright (C) Michael Adam 2012
7 Copyright (C) Volker Lendecke 2012
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "system/filesys.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
27 #include "libcli/security/security.h"
29 #include "librpc/gen_ndr/ndr_open_files.h"
31 #include "fake_file.h"
33 NTSTATUS vfs_default_durable_cookie(struct files_struct *fsp,
35 DATA_BLOB *cookie_blob)
37 struct connection_struct *conn = fsp->conn;
38 enum ndr_err_code ndr_err;
39 struct vfs_default_durable_cookie cookie;
41 if (!lp_durable_handles(SNUM(conn))) {
42 return NT_STATUS_NOT_SUPPORTED;
45 if (lp_kernel_share_modes(SNUM(conn))) {
47 * We do not support durable handles
48 * if kernel share modes (flocks) are used
50 return NT_STATUS_NOT_SUPPORTED;
53 if (lp_kernel_oplocks(SNUM(conn))) {
55 * We do not support durable handles
56 * if kernel oplocks are used
58 return NT_STATUS_NOT_SUPPORTED;
61 if ((fsp->current_lock_count > 0) &&
62 lp_posix_locking(fsp->conn->params))
65 * We do not support durable handles
66 * if the handle has posix locks.
68 return NT_STATUS_NOT_SUPPORTED;
71 if (fsp->is_directory) {
72 return NT_STATUS_NOT_SUPPORTED;
75 if (fsp->fh->fd == -1) {
76 return NT_STATUS_NOT_SUPPORTED;
79 if (is_ntfs_stream_smb_fname(fsp->fsp_name)) {
81 * We do not support durable handles
84 return NT_STATUS_NOT_SUPPORTED;
87 if (is_fake_file(fsp->fsp_name)) {
89 * We do not support durable handles
92 return NT_STATUS_NOT_SUPPORTED;
96 cookie.allow_reconnect = false;
97 cookie.id = fsp->file_id;
98 cookie.servicepath = conn->connectpath;
99 cookie.base_name = fsp->fsp_name->base_name;
100 cookie.initial_allocation_size = fsp->initial_allocation_size;
101 cookie.position_information = fsp->fh->position_information;
103 ndr_err = ndr_push_struct_blob(cookie_blob, mem_ctx, &cookie,
104 (ndr_push_flags_fn_t)ndr_push_vfs_default_durable_cookie);
105 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
106 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
113 NTSTATUS vfs_default_durable_disconnect(struct files_struct *fsp,
114 const DATA_BLOB old_cookie,
116 DATA_BLOB *new_cookie)
118 struct connection_struct *conn = fsp->conn;
120 enum ndr_err_code ndr_err;
121 struct vfs_default_durable_cookie cookie;
122 DATA_BLOB new_cookie_blob = data_blob_null;
123 struct share_mode_lock *lck;
126 *new_cookie = data_blob_null;
130 ndr_err = ndr_pull_struct_blob(&old_cookie, talloc_tos(), &cookie,
131 (ndr_pull_flags_fn_t)ndr_pull_vfs_default_durable_cookie);
132 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
133 status = ndr_map_error2ntstatus(ndr_err);
137 if (strcmp(cookie.magic, VFS_DEFAULT_DURABLE_COOKIE_MAGIC) != 0) {
138 return NT_STATUS_INVALID_PARAMETER;
141 if (cookie.version != VFS_DEFAULT_DURABLE_COOKIE_VERSION) {
142 return NT_STATUS_INVALID_PARAMETER;
145 if (!file_id_equal(&fsp->file_id, &cookie.id)) {
146 return NT_STATUS_INVALID_PARAMETER;
149 if (!BATCH_OPLOCK_TYPE(fsp->oplock_type)) {
150 return NT_STATUS_NOT_SUPPORTED;
153 if (fsp->num_pending_break_messages > 0) {
154 return NT_STATUS_NOT_SUPPORTED;
158 * For now let it be simple and do not keep
159 * delete on close files durable open
161 if (fsp->initial_delete_on_close) {
162 return NT_STATUS_NOT_SUPPORTED;
164 if (fsp->delete_on_close) {
165 return NT_STATUS_NOT_SUPPORTED;
168 if (!VALID_STAT(fsp->fsp_name->st)) {
169 return NT_STATUS_NOT_SUPPORTED;
172 if (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) {
173 return NT_STATUS_NOT_SUPPORTED;
176 /* Ensure any pending write time updates are done. */
177 if (fsp->update_write_time_event) {
178 update_write_time_handler(fsp->conn->sconn->ev_ctx,
179 fsp->update_write_time_event,
185 * The above checks are done in mark_share_mode_disconnected() too
186 * but we want to avoid getting the lock if possible
188 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
190 ok = mark_share_mode_disconnected(lck, fsp);
196 ok = brl_mark_disconnected(fsp);
202 return NT_STATUS_NOT_SUPPORTED;
207 cookie.allow_reconnect = true;
208 cookie.id = fsp->file_id;
209 cookie.servicepath = conn->connectpath;
210 cookie.base_name = fsp->fsp_name->base_name;
211 cookie.initial_allocation_size = fsp->initial_allocation_size;
212 cookie.position_information = fsp->fh->position_information;
214 ndr_err = ndr_push_struct_blob(&new_cookie_blob, mem_ctx, &cookie,
215 (ndr_push_flags_fn_t)ndr_push_vfs_default_durable_cookie);
216 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
217 status = ndr_map_error2ntstatus(ndr_err);
221 status = fd_close(fsp);
222 if (!NT_STATUS_IS_OK(status)) {
223 data_blob_free(&new_cookie_blob);
227 *new_cookie = new_cookie_blob;
231 NTSTATUS vfs_default_durable_reconnect(struct connection_struct *conn,
232 struct smb_request *smb1req,
233 struct smbXsrv_open *op,
234 const DATA_BLOB old_cookie,
236 files_struct **result,
237 DATA_BLOB *new_cookie)
239 struct share_mode_lock *lck;
240 struct share_mode_entry *e;
241 struct files_struct *fsp = NULL;
246 struct file_id file_id;
247 struct smb_filename *smb_fname = NULL;
248 enum ndr_err_code ndr_err;
249 struct vfs_default_durable_cookie cookie;
250 DATA_BLOB new_cookie_blob = data_blob_null;
253 *new_cookie = data_blob_null;
255 if (!lp_durable_handles(SNUM(conn))) {
256 return NT_STATUS_NOT_SUPPORTED;
260 * the checks for kernel oplocks
261 * and similar things are done
262 * in the vfs_default_durable_cookie()
268 ndr_err = ndr_pull_struct_blob(&old_cookie, talloc_tos(), &cookie,
269 (ndr_pull_flags_fn_t)ndr_pull_vfs_default_durable_cookie);
270 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
271 status = ndr_map_error2ntstatus(ndr_err);
275 if (strcmp(cookie.magic, VFS_DEFAULT_DURABLE_COOKIE_MAGIC) != 0) {
276 return NT_STATUS_INVALID_PARAMETER;
279 if (cookie.version != VFS_DEFAULT_DURABLE_COOKIE_VERSION) {
280 return NT_STATUS_INVALID_PARAMETER;
283 if (!cookie.allow_reconnect) {
284 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
287 if (strcmp(cookie.servicepath, conn->connectpath) != 0) {
288 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
291 /* Create an smb_filename with stream_name == NULL. */
292 status = create_synthetic_smb_fname(talloc_tos(),
296 if (!NT_STATUS_IS_OK(status)) {
300 ret = SMB_VFS_LSTAT(conn, smb_fname);
302 status = map_nt_error_from_unix_common(errno);
303 DEBUG(1, ("Unable to lstat stream: %s => %s\n",
304 smb_fname_str_dbg(smb_fname),
309 if (!S_ISREG(smb_fname->st.st_ex_mode)) {
310 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
313 file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
314 if (!file_id_equal(&cookie.id, &file_id)) {
315 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
319 * 1. check entry in locking.tdb
322 lck = get_existing_share_mode_lock(mem_ctx, file_id);
324 DEBUG(5, ("vfs_default_durable_reconnect: share-mode lock "
325 "not obtained from db\n"));
326 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
329 if (lck->data->num_share_modes == 0) {
330 DEBUG(1, ("vfs_default_durable_reconnect: Error: no share-mode "
331 "entry in existing share mode lock\n"));
333 return NT_STATUS_INTERNAL_DB_ERROR;
336 if (lck->data->num_share_modes > 1) {
338 * It can't be durable if there is more than one handle
341 DEBUG(5, ("vfs_default_durable_reconnect: more than one "
342 "share-mode entry - can not be durable\n"));
344 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
347 e = &lck->data->share_modes[0];
349 if (!server_id_is_disconnected(&e->pid)) {
350 DEBUG(5, ("vfs_default_durable_reconnect: denying durable "
351 "reconnect for handle that was not marked "
352 "disconnected (e.g. smbd or cluster node died)\n"));
354 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
357 if (e->share_file_id != op->global->open_persistent_id) {
358 DEBUG(5, ("vfs_default_durable_reconnect: denying durable "
359 "share_file_id changed %llu != %llu"
360 "(e.g. another client had opened the file)\n",
361 (unsigned long long)e->share_file_id,
362 (unsigned long long)op->global->open_persistent_id));
364 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
367 if ((e->access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA)) &&
370 DEBUG(5, ("vfs_default_durable_reconnect: denying durable "
371 "share[%s] is not writeable anymore\n",
372 lp_servicename(talloc_tos(), SNUM(conn))));
374 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
379 * add scavenger timer functionality
381 * For now we always allow the reconnect
384 expire_time = op->global->disconnect_time;
385 expire_time += NTTIME_MAGIC(op->global->durable_timeout_msec);
387 //TODO reopen and close before telling the client...
392 * 2. proceed with opening file
395 status = fsp_new(conn, conn, &fsp);
396 if (!NT_STATUS_IS_OK(status)) {
397 DEBUG(0, ("vfs_default_durable_reconnect: failed to create "
398 "new fsp: %s\n", nt_errstr(status)));
403 fsp->fh->private_options = e->private_options;
404 fsp->fh->gen_id = smbXsrv_open_hash(op);
405 fsp->file_id = file_id;
406 fsp->file_pid = smb1req->smbpid;
407 fsp->vuid = smb1req->vuid;
408 fsp->open_time = e->time;
409 fsp->access_mask = e->access_mask;
410 fsp->share_access = e->share_access;
411 fsp->can_read = ((fsp->access_mask & (FILE_READ_DATA)) != 0);
412 fsp->can_write = ((fsp->access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA)) != 0);
416 * Do we need to store the modified flag in the DB?
417 * How to handle update_write_time and friends
418 * during a disconnected client on a durable handle?
420 fsp->modified = false;
422 * no durables for directories
424 fsp->is_directory = false;
426 * For normal files, can_lock == !is_directory
428 fsp->can_lock = true;
430 * We do not support aio write behind for smb2
432 fsp->aio_write_behind = false;
433 fsp->oplock_type = e->op_type;
435 fsp->initial_allocation_size = cookie.initial_allocation_size;
436 fsp->fh->position_information = cookie.position_information;
438 status = fsp_set_smb_fname(fsp, smb_fname);
439 if (!NT_STATUS_IS_OK(status)) {
442 DEBUG(0, ("vfs_default_durable_reconnect: "
443 "fsp_set_smb_fname failed: %s\n",
451 e->pid = messaging_server_id(conn->sconn->msg_ctx);
452 e->op_mid = smb1req->mid;
453 e->share_file_id = fsp->fh->gen_id;
455 ok = brl_reconnect_disconnected(fsp);
457 status = NT_STATUS_INTERNAL_ERROR;
458 DEBUG(1, ("vfs_default_durable_reconnect: "
459 "failed to reopen brlocks: %s\n",
468 * TODO: properly calculate open flags
470 if (fsp->can_write && fsp->can_read) {
472 } else if (fsp->can_write) {
474 } else if (fsp->can_read) {
478 status = fd_open(conn, fsp, flags, 0 /* mode */);
479 if (!NT_STATUS_IS_OK(status)) {
481 DEBUG(1, ("vfs_default_durable_reconnect: failed to open "
482 "file: %s\n", nt_errstr(status)));
488 ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
490 status = map_nt_error_from_unix_common(errno);
491 DEBUG(1, ("Unable to fstat stream: %s => %s\n",
492 smb_fname_str_dbg(smb_fname),
494 ret = SMB_VFS_CLOSE(fsp);
496 DEBUG(0, ("vfs_default_durable_reconnect: "
497 "SMB_VFS_CLOSE failed (%s) - leaking file "
498 "descriptor\n", strerror(errno)));
507 if (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) {
508 ret = SMB_VFS_CLOSE(fsp);
510 DEBUG(0, ("vfs_default_durable_reconnect: "
511 "SMB_VFS_CLOSE failed (%s) - leaking file "
512 "descriptor\n", strerror(errno)));
517 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
520 file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
521 if (!file_id_equal(&cookie.id, &file_id)) {
522 ret = SMB_VFS_CLOSE(fsp);
524 DEBUG(0, ("vfs_default_durable_reconnect: "
525 "SMB_VFS_CLOSE failed (%s) - leaking file "
526 "descriptor\n", strerror(errno)));
531 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
534 status = set_file_oplock(fsp, e->op_type);
535 if (!NT_STATUS_IS_OK(status)) {
536 DEBUG(1, ("vfs_default_durable_reconnect failed to set oplock "
537 "after opening file: %s\n", nt_errstr(status)));
538 ret = SMB_VFS_CLOSE(fsp);
540 DEBUG(0, ("vfs_default_durable_reconnect: "
541 "SMB_VFS_CLOSE failed (%s) - leaking file "
542 "descriptor\n", strerror(errno)));
550 status = vfs_default_durable_cookie(fsp, mem_ctx, &new_cookie_blob);
551 if (!NT_STATUS_IS_OK(status)) {
553 DEBUG(1, ("vfs_default_durable_reconnect: "
554 "vfs_default_durable_cookie - %s\n",
561 smb1req->chain_fsp = fsp;
562 smb1req->smb2req->compat_chain_fsp = fsp;
564 DEBUG(10, ("vfs_default_durable_reconnect: opened file '%s'\n",
568 * release the sharemode lock: this writes the changes
570 lck->data->modified = true;
574 *new_cookie = new_cookie_blob;