da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * CDDL HEADER START
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The contents of this file are subject to the terms of the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Common Development and Distribution License (the "License").
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * You may not use this file except in compliance with the License.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * or http://www.opensolaris.org/os/licensing.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * See the License for the specific language governing permissions
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * and limitations under the License.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * When distributing Covered Code, include this CDDL HEADER in each
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If applicable, add the following below this CDDL HEADER, with the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * fields enclosed by brackets "[]" replaced with your own identifying
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * information: Portions Copyright [yyyy] [name of copyright owner]
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * CDDL HEADER END
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
c586600796766c83eb9485c446886fd9ed2359a9Keyur Desai * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca * Copyright 2016 Syneto S.R.L. All rights reserved.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * General Structures Layout
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * -------------------------
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * This is a simplified diagram showing the relationship between most of the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * main structures.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | SMB_INFO |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * v
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------+ +-------------------+ +-------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | SESSION |<----->| SESSION |......| SESSION |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------+ +-------------------+ +-------------------+
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * | |
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * | |
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * | v
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * | +-------------------+ +-------------------+ +-------------------+
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * | | USER |<--->| USER |...| USER |
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * | +-------------------+ +-------------------+ +-------------------+
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * |
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * |
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * v
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------+ +-------------------+ +-------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | TREE |<----->| TREE |......| TREE |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------+ +-------------------+ +-------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | v
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | +-------+ +-------+ +-------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | | OFILE |<----->| OFILE |......| OFILE |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | +-------+ +-------+ +-------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * v
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------+ +------+ +------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | ODIR |<----->| ODIR |......| ODIR |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------+ +------+ +------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Ofile State Machine
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * ------------------
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------------+ T0
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | SMB_OFILE_STATE_OPEN |<----------- Creation/Allocation
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | T1
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * v
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | SMB_OFILE_STATE_CLOSING |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | T2
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * v
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------------+ T3
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | SMB_OFILE_STATE_CLOSED |----------> Deletion/Free
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * SMB_OFILE_STATE_OPEN
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * While in this state:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * - The ofile is queued in the list of ofiles of its tree.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * - References will be given out if the ofile is looked up.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * SMB_OFILE_STATE_CLOSING
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * While in this state:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * - The ofile is queued in the list of ofiles of its tree.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * - References will not be given out if the ofile is looked up.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * - The file is closed and the locks held are being released.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * - The resources associated with the ofile remain.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * SMB_OFILE_STATE_CLOSED
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * While in this state:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * - The ofile is queued in the list of ofiles of its tree.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * - References will not be given out if the ofile is looked up.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * - The resources associated with the ofile remain.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Transition T0
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * This transition occurs in smb_ofile_open(). A new ofile is created and
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * added to the list of ofiles of a tree.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Transition T1
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * This transition occurs in smb_ofile_close().
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Transition T2
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * This transition occurs in smb_ofile_release(). The resources associated
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * with the ofile are freed as well as the ofile structure. For the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * transition to occur, the ofile must be in the SMB_OFILE_STATE_CLOSED
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * state and the reference count be zero.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Comments
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * --------
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The state machine of the ofile structures is controlled by 3 elements:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * - The list of ofiles of the tree it belongs to.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * - The mutex embedded in the structure itself.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * - The reference count.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * There's a mutex embedded in the ofile structure used to protect its fields
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * and there's a lock embedded in the list of ofiles of a tree. To
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * increment or to decrement the reference count the mutex must be entered.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * To insert the ofile into the list of ofiles of the tree and to remove
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * the ofile from it, the lock must be entered in RW_WRITER mode.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Rules of access to a ofile structure:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * 1) In order to avoid deadlocks, when both (mutex and lock of the ofile
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * list) have to be entered, the lock must be entered first.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * 2) All actions applied to an ofile require a reference count.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * 3) There are 2 ways of getting a reference count. One is when the ofile
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * is opened. The other one when the ofile is looked up. This translates
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * into 2 functions: smb_ofile_open() and smb_ofile_lookup_by_fid().
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * It should be noted that the reference count of an ofile registers the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * number of references to the ofile in other structures (such as an smb
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * request). The reference count is not incremented in these 2 instances:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * 1) The ofile is open. An ofile is anchored by his state. If there's
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * no activity involving an ofile currently open, the reference count
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * of that ofile is zero.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * 2) The ofile is queued in the list of ofiles of its tree. The fact of
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * being queued in that list is NOT registered by incrementing the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * reference count.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/smb_kproto.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <smbsrv/smb_fsops.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownstatic boolean_t smb_ofile_is_open_locked(smb_ofile_t *);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownstatic smb_ofile_t *smb_ofile_close_and_next(smb_ofile_t *);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownstatic int smb_ofile_netinfo_encode(smb_ofile_t *, uint8_t *, size_t,
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown uint32_t *);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownstatic int smb_ofile_netinfo_init(smb_ofile_t *, smb_netfileinfo_t *);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownstatic void smb_ofile_netinfo_fini(smb_netfileinfo_t *);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofile_open
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmb_ofile_t *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmb_ofile_open(
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser smb_request_t *sr,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_node_t *node,
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego struct open_param *op,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw uint16_t ftype,
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as uint32_t uniqid,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_error_t *err)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser smb_tree_t *tree = sr->tid_tree;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_ofile_t *of;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw uint16_t fid;
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh smb_attr_t attr;
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross int rc;
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser enum errstates { EMPTY, FIDALLOC, CRHELD, MUTEXINIT };
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser enum errstates state = EMPTY;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (smb_idpool_alloc(&tree->t_fid_pool, &fid)) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw err->status = NT_STATUS_TOO_MANY_OPENED_FILES;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw err->errcls = ERRDOS;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw err->errcode = ERROR_TOO_MANY_OPEN_FILES;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser state = FIDALLOC;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
8622ec4569457733001d4982ef7f5b44427069beGordon Ross of = kmem_cache_alloc(smb_cache_ofile, KM_SLEEP);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw bzero(of, sizeof (smb_ofile_t));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of->f_magic = SMB_OFILE_MAGIC;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of->f_refcnt = 1;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of->f_fid = fid;
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as of->f_uniqid = uniqid;
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross of->f_opened_by_pid = sr->smb_pid;
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego of->f_granted_access = op->desired_access;
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego of->f_share_access = op->share_access;
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego of->f_create_options = op->create_options;
b89a8333f5e1f75ec0c269b22524bd2eccb972banatalie li - Sun Microsystems - Irvine United States of->f_cr = (op->create_options & FILE_OPEN_FOR_BACKUP_INTENT) ?
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser smb_user_getprivcred(sr->uid_user) : sr->uid_user->u_cred;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw crhold(of->f_cr);
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser state = CRHELD;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of->f_ftype = ftype;
faa1795a28a5c712eed6d0a3f84d98c368a316c6jb of->f_server = tree->t_server;
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser of->f_session = tree->t_session;
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser /*
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * grab a ref for of->f_user
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * released in smb_ofile_delete()
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser */
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser smb_user_hold_internal(sr->uid_user);
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser of->f_user = sr->uid_user;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of->f_tree = tree;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of->f_node = node;
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_init(&of->f_mutex, NULL, MUTEX_DEFAULT, NULL);
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser state = MUTEXINIT;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of->f_state = SMB_OFILE_STATE_OPEN;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (ftype == SMB_FTYPE_MESG_PIPE) {
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross /* See smb_opipe_open. */
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross of->f_pipe = op->pipe;
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright smb_server_inc_pipes(of->f_server);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw } else {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(ftype == SMB_FTYPE_DISK); /* Regular file, not a pipe */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(node);
2c2961f8403049d948b9f3e6c35d6488b6b7e1aajose borrego
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross /*
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross * Note that the common open path often adds bits like
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross * READ_CONTROL, so the logic "is this open exec-only"
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross * needs to look at only the FILE_DATA_ALL bits.
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross */
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross if ((of->f_granted_access & FILE_DATA_ALL) == FILE_EXECUTE)
2c2961f8403049d948b9f3e6c35d6488b6b7e1aajose borrego of->f_flags |= SMB_OFLAGS_EXECONLY;
2c2961f8403049d948b9f3e6c35d6488b6b7e1aajose borrego
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh bzero(&attr, sizeof (smb_attr_t));
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross attr.sa_mask = SMB_AT_UID | SMB_AT_DOSATTR;
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross rc = smb_node_getattr(NULL, node, of->f_cr, NULL, &attr);
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross if (rc != 0) {
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh err->status = NT_STATUS_INTERNAL_ERROR;
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh err->errcls = ERRDOS;
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh err->errcode = ERROR_INTERNAL_ERROR;
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser goto errout;
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh }
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh if (crgetuid(of->f_cr) == attr.sa_vattr.va_uid) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Add this bit for the file's owner even if it's not
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * specified in the request (Windows behavior).
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of->f_granted_access |= FILE_READ_ATTRIBUTES;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (smb_node_is_file(node)) {
8c10a8659ac31335ed870a1711c0182623f72fd6as of->f_mode =
8c10a8659ac31335ed870a1711c0182623f72fd6as smb_fsop_amask_to_omode(of->f_granted_access);
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego if (smb_fsop_open(node, of->f_mode, of->f_cr) != 0) {
8c10a8659ac31335ed870a1711c0182623f72fd6as err->status = NT_STATUS_ACCESS_DENIED;
8c10a8659ac31335ed870a1711c0182623f72fd6as err->errcls = ERRDOS;
8c10a8659ac31335ed870a1711c0182623f72fd6as err->errcode = ERROR_ACCESS_DENIED;
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser goto errout;
8c10a8659ac31335ed870a1711c0182623f72fd6as }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
8c10a8659ac31335ed870a1711c0182623f72fd6as
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego if (tree->t_flags & SMB_TREE_READONLY)
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego of->f_flags |= SMB_OFLAGS_READONLY;
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross /*
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross * Note that if we created_readonly, that
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross * will _not_ yet show in attr.sa_dosattr
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross * so creating a readonly file gives the
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross * caller a writable handle as it should.
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross */
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross if (attr.sa_dosattr & FILE_ATTRIBUTE_READONLY)
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross of->f_flags |= SMB_OFLAGS_READONLY;
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego
2c2961f8403049d948b9f3e6c35d6488b6b7e1aajose borrego smb_node_inc_open_ofiles(node);
fc724630b14603e4c1147df68b7bf45f7de7431fAlan Wright smb_node_add_ofile(node, of);
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States smb_node_ref(node);
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright smb_server_inc_files(of->f_server);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_llist_enter(&tree->t_ofile_list, RW_WRITER);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_llist_insert_tail(&tree->t_ofile_list, of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_llist_exit(&tree->t_ofile_list);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown atomic_inc_32(&tree->t_open_files);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw atomic_inc_32(&of->f_session->s_file_cnt);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (of);
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keisererrout:
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser switch (state) {
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser case MUTEXINIT:
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser mutex_destroy(&of->f_mutex);
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser smb_user_release(of->f_user);
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser /*FALLTHROUGH*/
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser case CRHELD:
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser crfree(of->f_cr);
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser of->f_magic = 0;
8622ec4569457733001d4982ef7f5b44427069beGordon Ross kmem_cache_free(smb_cache_ofile, of);
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser /*FALLTHROUGH*/
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser case FIDALLOC:
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser smb_idpool_free(&tree->t_fid_pool, fid);
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser /*FALLTHROUGH*/
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser case EMPTY:
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser break;
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser }
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser return (NULL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofile_close
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borregovoid
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Rosssmb_ofile_close(smb_ofile_t *of, int32_t mtime_sec)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross smb_attr_t *pa;
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross timestruc_t now;
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States uint32_t flags = 0;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross SMB_OFILE_VALID(of);
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_enter(&of->f_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of->f_refcnt);
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross if (of->f_state != SMB_OFILE_STATE_OPEN) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_exit(&of->f_mutex);
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross return;
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross }
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross of->f_state = SMB_OFILE_STATE_CLOSING;
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross mutex_exit(&of->f_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross switch (of->f_ftype) {
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross case SMB_FTYPE_BYTE_PIPE:
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross case SMB_FTYPE_MESG_PIPE:
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross smb_opipe_close(of);
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross smb_server_dec_pipes(of->f_server);
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross break;
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross case SMB_FTYPE_DISK:
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross case SMB_FTYPE_PRINTER:
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross /*
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * In here we make changes to of->f_pending_attr
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * while not holding of->f_mutex. This is OK
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * because we've changed f_state to CLOSING,
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * so no more threads will take this path.
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross */
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross pa = &of->f_pending_attr;
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross if (mtime_sec != 0) {
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross pa->sa_vattr.va_mtime.tv_sec = mtime_sec;
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross pa->sa_mask |= SMB_AT_MTIME;
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross /*
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * If we have ever modified data via this handle
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * (write or truncate) and if the mtime was not
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * set via this handle, update the mtime again
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * during the close. Windows expects this.
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * [ MS-FSA 2.1.5.4 "Update Timestamps" ]
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross */
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross if (of->f_written &&
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross (pa->sa_mask & SMB_AT_MTIME) == 0) {
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross pa->sa_mask |= SMB_AT_MTIME;
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross gethrestime(&now);
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross pa->sa_vattr.va_mtime = now;
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross }
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross if (of->f_flags & SMB_OFLAGS_SET_DELETE_ON_CLOSE) {
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross if (smb_tree_has_feature(of->f_tree,
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross SMB_TREE_CATIA)) {
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross flags |= SMB_CATIA;
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross }
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross (void) smb_node_set_delete_on_close(of->f_node,
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross of->f_cr, flags);
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross }
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross smb_fsop_unshrlock(of->f_cr, of->f_node, of->f_uniqid);
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross smb_node_destroy_lock_by_ofile(of->f_node, of);
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross if (smb_node_is_file(of->f_node)) {
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross (void) smb_fsop_close(of->f_node, of->f_mode,
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross of->f_cr);
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross smb_oplock_release(of->f_node, of);
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross } else {
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross /*
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross * If there was an odir, close it.
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross */
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross if (of->f_odir != NULL)
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross smb_odir_close(of->f_odir);
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross }
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross if (smb_node_dec_open_ofiles(of->f_node) == 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * Last close. The f_pending_attr has
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * only times (atime,ctime,mtime) so
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * we can borrow it to commit the
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * n_pending_dosattr from the node.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross pa->sa_dosattr =
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross of->f_node->n_pending_dosattr;
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross if (pa->sa_dosattr != 0)
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross pa->sa_mask |= SMB_AT_DOSATTR;
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross /* Let's leave this zero when not in use. */
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross of->f_node->n_allocsz = 0;
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross }
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross if (pa->sa_mask != 0) {
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross /*
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * Commit any pending attributes from
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * the ofile we're closing. Note that
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * we pass NULL as the ofile to setattr
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * so it will write to the file system
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * and not keep anything on the ofile.
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * This clears n_pending_dosattr if
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * there are no opens, otherwise the
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * dosattr will be pending again.
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross */
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross (void) smb_node_setattr(NULL, of->f_node,
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross of->f_cr, NULL, pa);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross /*
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * Cancel any notify change requests that
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * may be using this open instance.
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross */
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross if (of->f_node->n_fcn.fcn_count)
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross smb_notify_file_closed(of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross smb_server_dec_files(of->f_server);
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross atomic_dec_32(&of->f_tree->t_open_files);
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross mutex_enter(&of->f_mutex);
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross ASSERT(of->f_refcnt);
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross ASSERT(of->f_state == SMB_OFILE_STATE_CLOSING);
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross of->f_state = SMB_OFILE_STATE_CLOSED;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_exit(&of->f_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofile_close_all
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwvoid
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmb_ofile_close_all(
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_tree_t *tree)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_ofile_t *of;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(tree);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(tree->t_magic == SMB_TREE_MAGIC);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_llist_enter(&tree->t_ofile_list, RW_READER);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of = smb_llist_head(&tree->t_ofile_list);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw while (of) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of->f_magic == SMB_OFILE_MAGIC);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of->f_tree == tree);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of = smb_ofile_close_and_next(of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_llist_exit(&tree->t_ofile_list);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofiles_close_by_pid
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwvoid
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmb_ofile_close_all_by_pid(
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_tree_t *tree,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw uint16_t pid)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_ofile_t *of;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(tree);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(tree->t_magic == SMB_TREE_MAGIC);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_llist_enter(&tree->t_ofile_list, RW_READER);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of = smb_llist_head(&tree->t_ofile_list);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw while (of) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of->f_magic == SMB_OFILE_MAGIC);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of->f_tree == tree);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (of->f_opened_by_pid == pid) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of = smb_ofile_close_and_next(of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw } else {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of = smb_llist_next(&tree->t_ofile_list, of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_llist_exit(&tree->t_ofile_list);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown/*
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * If the enumeration request is for ofile data, handle it here.
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * Otherwise, return.
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown *
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * This function should be called with a hold on the ofile.
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown */
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownint
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownsmb_ofile_enum(smb_ofile_t *of, smb_svcenum_t *svcenum)
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown{
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown uint8_t *pb;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown uint_t nbytes;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown int rc;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(of);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(of->f_magic == SMB_OFILE_MAGIC);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(of->f_refcnt);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown if (svcenum->se_type != SMB_SVCENUM_TYPE_FILE)
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown return (0);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown if (svcenum->se_nskip > 0) {
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown svcenum->se_nskip--;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown return (0);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown }
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown if (svcenum->se_nitems >= svcenum->se_nlimit) {
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown svcenum->se_nitems = svcenum->se_nlimit;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown return (0);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown }
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown pb = &svcenum->se_buf[svcenum->se_bused];
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown rc = smb_ofile_netinfo_encode(of, pb, svcenum->se_bavail,
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown &nbytes);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown if (rc == 0) {
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown svcenum->se_bavail -= nbytes;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown svcenum->se_bused += nbytes;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown svcenum->se_nitems++;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown }
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown return (rc);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown}
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown/*
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * Take a reference on an open file.
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown */
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownboolean_t
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownsmb_ofile_hold(smb_ofile_t *of)
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown{
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(of);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(of->f_magic == SMB_OFILE_MAGIC);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown mutex_enter(&of->f_mutex);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross if (of->f_state != SMB_OFILE_STATE_OPEN) {
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown mutex_exit(&of->f_mutex);
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross return (B_FALSE);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown }
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross of->f_refcnt++;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown mutex_exit(&of->f_mutex);
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross return (B_TRUE);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown}
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * Release a reference on a file. If the reference count falls to
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * zero and the file has been closed, post the object for deletion.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * Object deletion is deferred to avoid modifying a list while an
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * iteration may be in progress.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwvoid
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintoshsmb_ofile_release(smb_ofile_t *of)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States SMB_OFILE_VALID(of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_enter(&of->f_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of->f_refcnt);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of->f_refcnt--;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw switch (of->f_state) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw case SMB_OFILE_STATE_OPEN:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw case SMB_OFILE_STATE_CLOSING:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw case SMB_OFILE_STATE_CLOSED:
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (of->f_refcnt == 0)
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smb_tree_post_ofile(of->f_tree, of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw default:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_exit(&of->f_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh/*
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * smb_ofile_request_complete
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh *
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * During oplock acquisition, all other oplock requests on the node
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * are blocked until the acquire request completes and the response
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * is on the wire.
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * Call smb_oplock_broadcast to notify the node that the request
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * has completed.
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh *
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * THIS MECHANISM RELIES ON THE FACT THAT THE OFILE IS NOT REMOVED
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * FROM THE SR UNTIL REQUEST COMPLETION (when the sr is destroyed)
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh */
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintoshvoid
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintoshsmb_ofile_request_complete(smb_ofile_t *of)
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh{
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh SMB_OFILE_VALID(of);
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh switch (of->f_ftype) {
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh case SMB_FTYPE_DISK:
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh ASSERT(of->f_node);
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh smb_oplock_broadcast(of->f_node);
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh break;
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh case SMB_FTYPE_MESG_PIPE:
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh break;
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh default:
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh break;
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh }
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh}
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofile_lookup_by_fid
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Find the open file whose fid matches the one specified in the request.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If we can't find the fid or the shares (trees) don't match, we have a
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * bad fid.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmb_ofile_t *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmb_ofile_lookup_by_fid(
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser smb_request_t *sr,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw uint16_t fid)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser smb_tree_t *tree = sr->tid_tree;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_llist_t *of_list;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_ofile_t *of;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(tree->t_magic == SMB_TREE_MAGIC);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of_list = &tree->t_ofile_list;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_llist_enter(of_list, RW_READER);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of = smb_llist_head(of_list);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw while (of) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of->f_magic == SMB_OFILE_MAGIC);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of->f_tree == tree);
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser if (of->f_fid == fid)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of = smb_llist_next(of_list, of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser if (of == NULL)
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser goto out;
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser /*
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * Only allow use of a given FID with the same UID that
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * was used to open it. MS-CIFS 3.3.5.14
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser */
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser if (of->f_user != sr->uid_user) {
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser of = NULL;
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser goto out;
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser }
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser mutex_enter(&of->f_mutex);
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser if (of->f_state != SMB_OFILE_STATE_OPEN) {
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser mutex_exit(&of->f_mutex);
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser of = NULL;
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser goto out;
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser }
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser of->f_refcnt++;
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser mutex_exit(&of->f_mutex);
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiserout:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_llist_exit(of_list);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown/*
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * smb_ofile_lookup_by_uniqid
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown *
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * Find the open file whose uniqid matches the one specified in the request.
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown */
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownsmb_ofile_t *
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownsmb_ofile_lookup_by_uniqid(smb_tree_t *tree, uint32_t uniqid)
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown{
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown smb_llist_t *of_list;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown smb_ofile_t *of;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(tree->t_magic == SMB_TREE_MAGIC);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown of_list = &tree->t_ofile_list;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown smb_llist_enter(of_list, RW_READER);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown of = smb_llist_head(of_list);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown while (of) {
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(of->f_magic == SMB_OFILE_MAGIC);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(of->f_tree == tree);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown if (of->f_uniqid == uniqid) {
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown if (smb_ofile_hold(of)) {
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown smb_llist_exit(of_list);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown return (of);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown }
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown }
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown of = smb_llist_next(of_list, of);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown }
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown smb_llist_exit(of_list);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown return (NULL);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown}
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown/*
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * Disallow NetFileClose on certain ofiles to avoid side-effects.
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * Closing a tree root is not allowed: use NetSessionDel or NetShareDel.
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * Closing SRVSVC connections is not allowed because this NetFileClose
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * request may depend on this ofile.
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown */
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownboolean_t
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownsmb_ofile_disallow_fclose(smb_ofile_t *of)
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown{
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(of);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(of->f_magic == SMB_OFILE_MAGIC);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(of->f_refcnt);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown switch (of->f_ftype) {
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown case SMB_FTYPE_DISK:
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(of->f_tree);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown return (of->f_node == of->f_tree->t_snode);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown case SMB_FTYPE_MESG_PIPE:
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(of->f_pipe);
bbf6f00c25b6a2bed23c35eac6d62998ecdb338cJordan Brown if (smb_strcasecmp(of->f_pipe->p_name, "SRVSVC", 0) == 0)
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown return (B_TRUE);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown break;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown default:
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown break;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown }
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown return (B_FALSE);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown}
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofile_set_flags
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Return value:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Current flags value
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwvoid
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmb_ofile_set_flags(
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_ofile_t *of,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw uint32_t flags)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of->f_magic == SMB_OFILE_MAGIC);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of->f_refcnt);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_enter(&of->f_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of->f_flags |= flags;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_exit(&of->f_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
f96bd5c800e73e351b0b6e4bd7f00b578dad29bbAlan Wright
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofile_seek
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Return value:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * 0 Success
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * EINVAL Unknown mode
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * EOVERFLOW offset too big
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwint
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmb_ofile_seek(
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_ofile_t *of,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ushort_t mode,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int32_t off,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw uint32_t *retoff)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
55bf511df53aad0fdb7eb3fa349f0308cc05234cas u_offset_t newoff = 0;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int rc = 0;
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh smb_attr_t attr;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of->f_magic == SMB_OFILE_MAGIC);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of->f_refcnt);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_enter(&of->f_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw switch (mode) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw case SMB_SEEK_SET:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (off < 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw newoff = 0;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw else
55bf511df53aad0fdb7eb3fa349f0308cc05234cas newoff = (u_offset_t)off;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw case SMB_SEEK_CUR:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (off < 0 && (-off) > of->f_seek_pos)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw newoff = 0;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw else
55bf511df53aad0fdb7eb3fa349f0308cc05234cas newoff = of->f_seek_pos + (u_offset_t)off;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw case SMB_SEEK_END:
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh bzero(&attr, sizeof (smb_attr_t));
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh attr.sa_mask |= SMB_AT_SIZE;
8622ec4569457733001d4982ef7f5b44427069beGordon Ross rc = smb_fsop_getattr(NULL, zone_kcred(), of->f_node, &attr);
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh if (rc != 0) {
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh mutex_exit(&of->f_mutex);
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh return (rc);
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh }
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh if (off < 0 && (-off) > attr.sa_vattr.va_size)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw newoff = 0;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw else
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh newoff = attr.sa_vattr.va_size + (u_offset_t)off;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw default:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_exit(&of->f_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (EINVAL);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
55bf511df53aad0fdb7eb3fa349f0308cc05234cas /*
55bf511df53aad0fdb7eb3fa349f0308cc05234cas * See comments at the beginning of smb_seek.c.
55bf511df53aad0fdb7eb3fa349f0308cc05234cas * If the offset is greater than UINT_MAX, we will return an error.
55bf511df53aad0fdb7eb3fa349f0308cc05234cas */
55bf511df53aad0fdb7eb3fa349f0308cc05234cas
55bf511df53aad0fdb7eb3fa349f0308cc05234cas if (newoff > UINT_MAX) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw rc = EOVERFLOW;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw } else {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of->f_seek_pos = newoff;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *retoff = (uint32_t)newoff;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_exit(&of->f_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (rc);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca/*
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca * smb_ofile_flush
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca *
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca * If writes on this file are not synchronous, flush it using the NFSv3
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca * commit interface.
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca *
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca * XXX - todo: Flush named pipe should drain writes.
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca */
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatcavoid
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatcasmb_ofile_flush(struct smb_request *sr, struct smb_ofile *of)
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca{
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca switch (of->f_ftype) {
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca case SMB_FTYPE_DISK:
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca if ((of->f_node->flags & NODE_FLAGS_WRITE_THROUGH) == 0)
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca (void) smb_fsop_commit(sr, of->f_cr, of->f_node);
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca break;
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca default:
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca break;
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca }
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca}
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofile_is_open
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwboolean_t
2c2961f8403049d948b9f3e6c35d6488b6b7e1aajose borregosmb_ofile_is_open(smb_ofile_t *of)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown boolean_t rc;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
2c2961f8403049d948b9f3e6c35d6488b6b7e1aajose borrego SMB_OFILE_VALID(of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_enter(&of->f_mutex);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown rc = smb_ofile_is_open_locked(of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_exit(&of->f_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (rc);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/* *************************** Static Functions ***************************** */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown/*
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * Determine whether or not an ofile is open.
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * This function must be called with the mutex held.
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown */
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownstatic boolean_t
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownsmb_ofile_is_open_locked(smb_ofile_t *of)
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown{
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown switch (of->f_state) {
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown case SMB_OFILE_STATE_OPEN:
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown return (B_TRUE);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown case SMB_OFILE_STATE_CLOSING:
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown case SMB_OFILE_STATE_CLOSED:
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown return (B_FALSE);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown default:
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(0);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown return (B_FALSE);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown }
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown}
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * This function closes the file passed in (if appropriate) and returns the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * next open file in the list of open files of the tree of the open file passed
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * in. It requires that the list of open files of the tree be entered in
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * RW_READER mode before being called.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwstatic smb_ofile_t *
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintoshsmb_ofile_close_and_next(smb_ofile_t *of)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_ofile_t *next_of;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_tree_t *tree;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of->f_magic == SMB_OFILE_MAGIC);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_enter(&of->f_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw switch (of->f_state) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw case SMB_OFILE_STATE_OPEN:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /* The file is still open. */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of->f_refcnt++;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of->f_refcnt);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw tree = of->f_tree;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_exit(&of->f_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_llist_exit(&of->f_tree->t_ofile_list);
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego smb_ofile_close(of, 0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_ofile_release(of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_llist_enter(&tree->t_ofile_list, RW_READER);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw next_of = smb_llist_head(&tree->t_ofile_list);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw case SMB_OFILE_STATE_CLOSING:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw case SMB_OFILE_STATE_CLOSED:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The ofile exists but is closed or
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * in the process being closed.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_exit(&of->f_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw next_of = smb_llist_next(&of->f_tree->t_ofile_list, of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw default:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_exit(&of->f_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw next_of = smb_llist_next(&of->f_tree->t_ofile_list, of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (next_of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * Delete an ofile.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * Remove the ofile from the tree list before freeing resources
c586600796766c83eb9485c446886fd9ed2359a9Keyur Desai * associated with the ofile.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United Statesvoid
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United Statessmb_ofile_delete(void *arg)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smb_tree_t *tree;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smb_ofile_t *of = (smb_ofile_t *)arg;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States SMB_OFILE_VALID(of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of->f_refcnt == 0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(of->f_state == SMB_OFILE_STATE_CLOSED);
1fdeec650620e8498c06f832ea4bd2292f7e9632joyce mcintosh ASSERT(!SMB_OFILE_OPLOCK_GRANTED(of));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States tree = of->f_tree;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smb_llist_enter(&tree->t_ofile_list, RW_WRITER);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smb_llist_remove(&tree->t_ofile_list, of);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smb_idpool_free(&tree->t_fid_pool, of->f_fid);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States atomic_dec_32(&tree->t_session->s_file_cnt);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smb_llist_exit(&tree->t_ofile_list);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States mutex_enter(&of->f_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States mutex_exit(&of->f_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross switch (of->f_ftype) {
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross case SMB_FTYPE_BYTE_PIPE:
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross case SMB_FTYPE_MESG_PIPE:
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smb_opipe_dealloc(of->f_pipe);
3db3f65c6274eb042354801a308c8e9bc4994553amw of->f_pipe = NULL;
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross break;
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross case SMB_FTYPE_DISK:
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross if (of->f_odir != NULL)
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross smb_odir_release(of->f_odir);
2c2961f8403049d948b9f3e6c35d6488b6b7e1aajose borrego smb_node_rem_ofile(of->f_node, of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw smb_node_release(of->f_node);
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross break;
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross default:
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross ASSERT(!"f_ftype");
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw of->f_magic = (uint32_t)~SMB_OFILE_MAGIC;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mutex_destroy(&of->f_mutex);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw crfree(of->f_cr);
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser smb_user_release(of->f_user);
8622ec4569457733001d4982ef7f5b44427069beGordon Ross kmem_cache_free(smb_cache_ofile, of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofile_access
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * This function will check to see if the access requested is granted.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Returns NT status codes.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwuint32_t
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmb_ofile_access(smb_ofile_t *of, cred_t *cr, uint32_t access)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
8622ec4569457733001d4982ef7f5b44427069beGordon Ross if ((of == NULL) || (cr == zone_kcred()))
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (NT_STATUS_SUCCESS);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If the request is for something
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * I don't grant it is an error
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (~(of->f_granted_access) & access) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (!(of->f_granted_access & ACCESS_SYSTEM_SECURITY) &&
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (access & ACCESS_SYSTEM_SECURITY)) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (NT_STATUS_PRIVILEGE_NOT_HELD);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (NT_STATUS_ACCESS_DENIED);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (NT_STATUS_SUCCESS);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh/*
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * smb_ofile_share_check
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh *
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * Check if ofile was opened with share access NONE (0).
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * Returns: B_TRUE - share access non-zero
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * B_FALSE - share access NONE
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh */
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintoshboolean_t
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintoshsmb_ofile_share_check(smb_ofile_t *of)
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh{
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh return (!SMB_DENY_ALL(of->f_share_access));
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh}
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb/*
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * check file sharing rules for current open request
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * against existing open instances of the same file
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb *
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * Returns NT_STATUS_SHARING_VIOLATION if there is any
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * sharing conflict, otherwise returns NT_STATUS_SUCCESS.
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb */
3ad684d66b78e06edd37e2c4fd3b3949f095194bjbuint32_t
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United Statessmb_ofile_open_check(smb_ofile_t *of, uint32_t desired_access,
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb uint32_t share_access)
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb{
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb ASSERT(of->f_magic == SMB_OFILE_MAGIC);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_enter(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (of->f_state != SMB_OFILE_STATE_OPEN) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_exit(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb return (NT_STATUS_INVALID_HANDLE);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb }
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb /* if it's just meta data */
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb if ((of->f_granted_access & FILE_DATA_ALL) == 0) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_exit(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb return (NT_STATUS_SUCCESS);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb }
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb /*
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * Check requested share access against the
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * open granted (desired) access
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb */
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb if (SMB_DENY_DELETE(share_access) && (of->f_granted_access & DELETE)) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_exit(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb return (NT_STATUS_SHARING_VIOLATION);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb }
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb if (SMB_DENY_READ(share_access) &&
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb (of->f_granted_access & (FILE_READ_DATA | FILE_EXECUTE))) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_exit(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb return (NT_STATUS_SHARING_VIOLATION);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb }
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb if (SMB_DENY_WRITE(share_access) &&
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb (of->f_granted_access & (FILE_WRITE_DATA | FILE_APPEND_DATA))) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_exit(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb return (NT_STATUS_SHARING_VIOLATION);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb }
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb /* check requested desired access against the open share access */
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb if (SMB_DENY_DELETE(of->f_share_access) && (desired_access & DELETE)) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_exit(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb return (NT_STATUS_SHARING_VIOLATION);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb }
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb if (SMB_DENY_READ(of->f_share_access) &&
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb (desired_access & (FILE_READ_DATA | FILE_EXECUTE))) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_exit(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb return (NT_STATUS_SHARING_VIOLATION);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb }
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb if (SMB_DENY_WRITE(of->f_share_access) &&
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb (desired_access & (FILE_WRITE_DATA | FILE_APPEND_DATA))) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_exit(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb return (NT_STATUS_SHARING_VIOLATION);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb }
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_exit(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb return (NT_STATUS_SUCCESS);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb}
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb/*
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * smb_ofile_rename_check
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb *
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * An open file can be renamed if
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb *
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * 1. isn't opened for data writing or deleting
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb *
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * 2. Opened with "Deny Delete" share mode
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * But not opened for data reading or executing
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * (opened for accessing meta data)
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb */
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjbuint32_t
3ad684d66b78e06edd37e2c4fd3b3949f095194bjbsmb_ofile_rename_check(smb_ofile_t *of)
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb{
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb ASSERT(of->f_magic == SMB_OFILE_MAGIC);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_enter(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb if (of->f_state != SMB_OFILE_STATE_OPEN) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_exit(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb return (NT_STATUS_INVALID_HANDLE);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb }
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb if (of->f_granted_access &
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb (FILE_WRITE_DATA | FILE_APPEND_DATA | DELETE)) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_exit(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb return (NT_STATUS_SHARING_VIOLATION);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb }
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb if ((of->f_share_access & FILE_SHARE_DELETE) == 0) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb if (of->f_granted_access &
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb (FILE_READ_DATA | FILE_EXECUTE)) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_exit(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb return (NT_STATUS_SHARING_VIOLATION);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb }
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb }
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_exit(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb return (NT_STATUS_SUCCESS);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb}
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb/*
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * smb_ofile_delete_check
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb *
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * An open file can be deleted only if opened for
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * accessing meta data. Share modes aren't important
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * in this case.
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb *
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * NOTE: there is another mechanism for deleting an
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * open file that NT clients usually use.
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * That's setting "Delete on close" flag for an open
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * file. In this way the file will be deleted after
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * last close. This flag can be set by SmbTrans2SetFileInfo
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * with FILE_DISPOSITION_INFO information level.
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * For setting this flag, the file should be opened by
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * DELETE access in the FID that is passed in the Trans2
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * request.
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb */
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjbuint32_t
3ad684d66b78e06edd37e2c4fd3b3949f095194bjbsmb_ofile_delete_check(smb_ofile_t *of)
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb{
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb ASSERT(of->f_magic == SMB_OFILE_MAGIC);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_enter(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb if (of->f_state != SMB_OFILE_STATE_OPEN) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_exit(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb return (NT_STATUS_INVALID_HANDLE);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb }
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb if (of->f_granted_access &
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb (FILE_READ_DATA | FILE_WRITE_DATA |
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb FILE_APPEND_DATA | FILE_EXECUTE | DELETE)) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_exit(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb return (NT_STATUS_SHARING_VIOLATION);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb }
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb mutex_exit(&of->f_mutex);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb return (NT_STATUS_SUCCESS);
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb}
b89a8333f5e1f75ec0c269b22524bd2eccb972banatalie li - Sun Microsystems - Irvine United States
b89a8333f5e1f75ec0c269b22524bd2eccb972banatalie li - Sun Microsystems - Irvine United Statescred_t *
b89a8333f5e1f75ec0c269b22524bd2eccb972banatalie li - Sun Microsystems - Irvine United Statessmb_ofile_getcred(smb_ofile_t *of)
b89a8333f5e1f75ec0c269b22524bd2eccb972banatalie li - Sun Microsystems - Irvine United States{
b89a8333f5e1f75ec0c269b22524bd2eccb972banatalie li - Sun Microsystems - Irvine United States return (of->f_cr);
b89a8333f5e1f75ec0c269b22524bd2eccb972banatalie li - Sun Microsystems - Irvine United States}
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States/*
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States * smb_ofile_set_delete_on_close
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States *
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States * Set the DeleteOnClose flag on the smb file. When the file is closed,
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States * the flag will be transferred to the smb node, which will commit the
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States * delete operation and inhibit subsequent open requests.
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States *
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States * When DeleteOnClose is set on an smb_node, the common open code will
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States * reject subsequent open requests for the file. Observation of Windows
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States * 2000 indicates that subsequent opens should be allowed (assuming
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States * there would be no sharing violation) until the file is closed using
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States * the fid on which the DeleteOnClose was requested.
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States */
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United Statesvoid
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United Statessmb_ofile_set_delete_on_close(smb_ofile_t *of)
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States{
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States mutex_enter(&of->f_mutex);
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States of->f_flags |= SMB_OFLAGS_SET_DELETE_ON_CLOSE;
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States mutex_exit(&of->f_mutex);
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States}
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown/*
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * Encode open file information into a buffer; needed in user space to
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * support RPC requests.
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown */
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownstatic int
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownsmb_ofile_netinfo_encode(smb_ofile_t *of, uint8_t *buf, size_t buflen,
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown uint32_t *nbytes)
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown{
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown smb_netfileinfo_t fi;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown int rc;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown rc = smb_ofile_netinfo_init(of, &fi);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown if (rc == 0) {
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown rc = smb_netfileinfo_encode(&fi, buf, buflen, nbytes);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown smb_ofile_netinfo_fini(&fi);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown }
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown return (rc);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown}
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownstatic int
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownsmb_ofile_netinfo_init(smb_ofile_t *of, smb_netfileinfo_t *fi)
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown{
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown smb_user_t *user;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown smb_tree_t *tree;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown smb_node_t *node;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown char *path;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown char *buf;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown int rc;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(of);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown user = of->f_user;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown tree = of->f_tree;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(user);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(tree);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown buf = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown switch (of->f_ftype) {
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown case SMB_FTYPE_DISK:
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown node = of->f_node;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(node);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown fi->fi_permissions = of->f_granted_access;
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright fi->fi_numlocks = smb_lock_get_lock_count(node, of);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown if (node != tree->t_snode) {
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright rc = smb_node_getshrpath(node, tree, path, MAXPATHLEN);
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright if (rc != 0)
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown (void) strlcpy(path, node->od_name, MAXPATHLEN);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown }
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown (void) snprintf(buf, MAXPATHLEN, "%s:%s", tree->t_sharename,
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown path);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown kmem_free(path, MAXPATHLEN);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown break;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown case SMB_FTYPE_MESG_PIPE:
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown ASSERT(of->f_pipe);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown fi->fi_permissions = FILE_READ_DATA | FILE_WRITE_DATA |
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown FILE_EXECUTE;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown fi->fi_numlocks = 0;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown (void) snprintf(buf, MAXPATHLEN, "\\PIPE\\%s",
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown of->f_pipe->p_name);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown break;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown default:
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown kmem_free(buf, MAXPATHLEN);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown return (-1);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown }
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown fi->fi_fid = of->f_fid;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown fi->fi_uniqid = of->f_uniqid;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown fi->fi_pathlen = strlen(buf) + 1;
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States fi->fi_path = smb_mem_strdup(buf);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown kmem_free(buf, MAXPATHLEN);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown fi->fi_namelen = user->u_domain_len + user->u_name_len + 2;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown fi->fi_username = kmem_alloc(fi->fi_namelen, KM_SLEEP);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown (void) snprintf(fi->fi_username, fi->fi_namelen, "%s\\%s",
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown user->u_domain, user->u_name);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown return (0);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown}
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownstatic void
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownsmb_ofile_netinfo_fini(smb_netfileinfo_t *fi)
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown{
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown if (fi == NULL)
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown return;
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown if (fi->fi_path)
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smb_mem_free(fi->fi_path);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown if (fi->fi_username)
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown kmem_free(fi->fi_username, fi->fi_namelen);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown bzero(fi, sizeof (smb_netfileinfo_t));
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown}
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States/*
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * A query of user and group quotas may span multiple requests.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * f_quota_resume is used to determine where the query should
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * be resumed, in a subsequent request. f_quota_resume contains
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * the SID of the last quota entry returned to the client.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States */
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United Statesvoid
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United Statessmb_ofile_set_quota_resume(smb_ofile_t *ofile, char *resume)
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States{
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States ASSERT(ofile);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States mutex_enter(&ofile->f_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (resume == NULL)
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States bzero(ofile->f_quota_resume, SMB_SID_STRSZ);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States else
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) strlcpy(ofile->f_quota_resume, resume, SMB_SID_STRSZ);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States mutex_exit(&ofile->f_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States}
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United Statesvoid
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United Statessmb_ofile_get_quota_resume(smb_ofile_t *ofile, char *buf, int bufsize)
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States{
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States ASSERT(ofile);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States mutex_enter(&ofile->f_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States (void) strlcpy(buf, ofile->f_quota_resume, bufsize);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States mutex_exit(&ofile->f_mutex);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States}