da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * CDDL HEADER START
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 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * See the License for the specific language governing permissions
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * and limitations under the License.
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 * CDDL HEADER END
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 * General Structures Layout
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * -------------------------
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * This is a simplified diagram showing the relationship between most of the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * main structures.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | SMB_INFO |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------+ +-------------------+ +-------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | SESSION |<----->| SESSION |......| SESSION |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------+ +-------------------+ +-------------------+
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * | +-------------------+ +-------------------+ +-------------------+
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * | | USER |<--->| USER |...| USER |
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * | +-------------------+ +-------------------+ +-------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------+ +-------------------+ +-------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | TREE |<----->| TREE |......| TREE |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------+ +-------------------+ +-------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | +-------+ +-------+ +-------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | | OFILE |<----->| OFILE |......| OFILE |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | +-------+ +-------+ +-------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------+ +------+ +------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | ODIR |<----->| ODIR |......| ODIR |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------+ +------+ +------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Ofile State Machine
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * ------------------
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------------+ T0
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | SMB_OFILE_STATE_OPEN |<----------- Creation/Allocation
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | SMB_OFILE_STATE_CLOSING |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------------+ T3
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * | SMB_OFILE_STATE_CLOSED |----------> Deletion/Free
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * +-------------------------+
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * SMB_OFILE_STATE_OPEN
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 * SMB_OFILE_STATE_CLOSING
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 * SMB_OFILE_STATE_CLOSED
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 * Transition T0
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 * Transition T1
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * This transition occurs in smb_ofile_close().
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Transition T2
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 * Comments
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 * 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 * Rules of access to a ofile structure:
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 * 2) All actions applied to an ofile require a reference count.
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 * 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 * 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 * 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.
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 Brownstatic int smb_ofile_netinfo_init(smb_ofile_t *, smb_netfileinfo_t *);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownstatic void smb_ofile_netinfo_fini(smb_netfileinfo_t *);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofile_open
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser enum errstates { EMPTY, FIDALLOC, CRHELD, MUTEXINIT };
8622ec4569457733001d4982ef7f5b44427069beGordon Ross of = kmem_cache_alloc(smb_cache_ofile, KM_SLEEP);
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;
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * grab a ref for of->f_user
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser * released in smb_ofile_delete()
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross /* See smb_opipe_open. */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw ASSERT(ftype == SMB_FTYPE_DISK); /* Regular file, not a pipe */
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 if ((of->f_granted_access & FILE_DATA_ALL) == FILE_EXECUTE)
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Ross rc = smb_node_getattr(NULL, node, of->f_cr, NULL, &attr);
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh if (crgetuid(of->f_cr) == attr.sa_vattr.va_uid) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Add this bit for the file's owner even if it's not
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * specified in the request (Windows behavior).
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (smb_node_is_file(node)) {
c8ec8eea9849cac239663c46be8a7f5d2ba7ca00jose borrego if (smb_fsop_open(node, of->f_mode, of->f_cr) != 0) {
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.
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States smb_node_ref(node);
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser /*FALLTHROUGH*/
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser /*FALLTHROUGH*/
3b13a1ef7511135ec0c75b5f94de8075454efd79Thomas Keiser /*FALLTHROUGH*/
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofile_close
5fd03bc0f2e00e7ba02316c2e08f45d52aab15dbGordon Rosssmb_ofile_close(smb_ofile_t *of, int32_t mtime_sec)
8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09afshin salek ardakani - Sun Microsystems - Irvine United States uint32_t flags = 0;
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 * 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 if (of->f_flags & SMB_OFLAGS_SET_DELETE_ON_CLOSE) {
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross smb_fsop_unshrlock(of->f_cr, of->f_node, of->f_uniqid);
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross * If there was an odir, close it.
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross if (smb_node_dec_open_ofiles(of->f_node) == 0) {
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.
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross /* Let's leave this zero when not in use. */
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 * Cancel any notify change requests that
68b2bbf26c7040fea4281dcb58b81e7627e46f34Gordon Ross * may be using this open instance.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofile_close_all
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofiles_close_by_pid
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * If the enumeration request is for ofile data, handle it here.
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * Otherwise, return.
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * This function should be called with a hold on the ofile.
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownsmb_ofile_enum(smb_ofile_t *of, smb_svcenum_t *svcenum)
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown if (svcenum->se_nitems >= svcenum->se_nlimit) {
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown rc = smb_ofile_netinfo_encode(of, pb, svcenum->se_bavail,
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * Take a reference on an open file.
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.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States SMB_OFILE_VALID(of);
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);
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * smb_ofile_request_complete
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 * 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)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofile_lookup_by_fid
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.
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
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * smb_ofile_lookup_by_uniqid
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * Find the open file whose uniqid matches the one specified in the request.
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownsmb_ofile_lookup_by_uniqid(smb_tree_t *tree, uint32_t uniqid)
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.
bbf6f00c25b6a2bed23c35eac6d62998ecdb338cJordan Brown if (smb_strcasecmp(of->f_pipe->p_name, "SRVSVC", 0) == 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofile_set_flags
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Return value:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Current flags value
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofile_seek
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Return value:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * 0 Success
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * EINVAL Unknown mode
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * EOVERFLOW offset too big
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw switch (mode) {
8622ec4569457733001d4982ef7f5b44427069beGordon Ross rc = smb_fsop_getattr(NULL, zone_kcred(), of->f_node, &attr);
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh if (off < 0 && (-off) > attr.sa_vattr.va_size)
037cac007b685e7ea79f6ef7e8e62bfd342a4d56joyce mcintosh newoff = attr.sa_vattr.va_size + (u_offset_t)off;
55bf511df53aad0fdb7eb3fa349f0308cc05234cas * See comments at the beginning of smb_seek.c.
55bf511df53aad0fdb7eb3fa349f0308cc05234cas * If the offset is greater than UINT_MAX, we will return an error.
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca * smb_ofile_flush
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca * If writes on this file are not synchronous, flush it using the NFSv3
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca * commit interface.
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca * XXX - todo: Flush named pipe should drain writes.
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatcasmb_ofile_flush(struct smb_request *sr, struct smb_ofile *of)
6d1c73b5858fefc6161c7d686345f0dc887ea799Dan Vatca if ((of->f_node->flags & NODE_FLAGS_WRITE_THROUGH) == 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofile_is_open
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/* *************************** Static Functions ***************************** */
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * Determine whether or not an ofile is open.
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * This function must be called with the mutex held.
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 /* The file is still open. */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The ofile exists but is closed or
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * in the process being closed.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw next_of = smb_llist_next(&of->f_tree->t_ofile_list, of);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw next_of = smb_llist_next(&of->f_tree->t_ofile_list, of);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * Delete an ofile.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States * Remove the ofile from the tree list before freeing resources
c586600796766c83eb9485c446886fd9ed2359a9Keyur Desai * associated with the ofile.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United Statesvoid
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United Statessmb_ofile_delete(void *arg)
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);
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);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smb_opipe_dealloc(of->f_pipe);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * smb_ofile_access
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * This function will check to see if the access requested is granted.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Returns NT status codes.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwsmb_ofile_access(smb_ofile_t *of, cred_t *cr, uint32_t access)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If the request is for something
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * I don't grant it is an error
cb174861876aea6950a7ab4ce944aff84b1914cdjoyce mcintosh * smb_ofile_share_check
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
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * check file sharing rules for current open request
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * against existing open instances of the same file
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * Returns NT_STATUS_SHARING_VIOLATION if there is any
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * sharing conflict, otherwise returns NT_STATUS_SUCCESS.
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United Statessmb_ofile_open_check(smb_ofile_t *of, uint32_t desired_access,
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States if (of->f_state != SMB_OFILE_STATE_OPEN) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb /* if it's just meta data */
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * Check requested share access against the
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * open granted (desired) access
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb if (SMB_DENY_DELETE(share_access) && (of->f_granted_access & DELETE)) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb (of->f_granted_access & (FILE_READ_DATA | FILE_EXECUTE))) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb (of->f_granted_access & (FILE_WRITE_DATA | FILE_APPEND_DATA))) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb /* check requested desired access against the open share access */
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb if (SMB_DENY_DELETE(of->f_share_access) && (desired_access & DELETE)) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb (desired_access & (FILE_WRITE_DATA | FILE_APPEND_DATA))) {
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * smb_ofile_rename_check
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * An open file can be renamed if
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * 1. isn't opened for data writing or deleting
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * 2. Opened with "Deny Delete" share mode
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * But not opened for data reading or executing
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * (opened for accessing meta data)
3ad684d66b78e06edd37e2c4fd3b3949f095194bjb * smb_ofile_delete_check
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 * 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.
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 return (of->f_cr);
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 * Encode open file information into a buffer; needed in user space to
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown * support RPC requests.
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownsmb_ofile_netinfo_encode(smb_ofile_t *of, uint8_t *buf, size_t buflen,
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown rc = smb_netfileinfo_encode(&fi, buf, buflen, nbytes);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brownsmb_ofile_netinfo_init(smb_ofile_t *of, smb_netfileinfo_t *fi)
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright fi->fi_numlocks = smb_lock_get_lock_count(node, of);
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright rc = smb_node_getshrpath(node, tree, path, MAXPATHLEN);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown (void) strlcpy(path, node->od_name, MAXPATHLEN);
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown (void) snprintf(buf, MAXPATHLEN, "%s:%s", tree->t_sharename,
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown fi->fi_permissions = FILE_READ_DATA | FILE_WRITE_DATA |
1fcced4c370617db71610fecffd5451a5894ca5eJordan Brown return (-1);
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States fi->fi_path = smb_mem_strdup(buf);
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",
9fb67ea305c66b6a297583b9b0db6796b0dfe497afshin salek ardakani - Sun Microsystems - Irvine United States smb_mem_free(fi->fi_path);
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);