xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_seek.c (revision b89a8333f5e1f75ec0c269b22524bd2eccb972ba)
1da6c28aaSamw /*
2da6c28aaSamw  * CDDL HEADER START
3da6c28aaSamw  *
4da6c28aaSamw  * The contents of this file are subject to the terms of the
5da6c28aaSamw  * Common Development and Distribution License (the "License").
6da6c28aaSamw  * You may not use this file except in compliance with the License.
7da6c28aaSamw  *
8da6c28aaSamw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aaSamw  * or http://www.opensolaris.org/os/licensing.
10da6c28aaSamw  * See the License for the specific language governing permissions
11da6c28aaSamw  * and limitations under the License.
12da6c28aaSamw  *
13da6c28aaSamw  * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aaSamw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aaSamw  * If applicable, add the following below this CDDL HEADER, with the
16da6c28aaSamw  * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aaSamw  * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aaSamw  *
19da6c28aaSamw  * CDDL HEADER END
20da6c28aaSamw  */
21da6c28aaSamw /*
22dc20a302Sas200622  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23da6c28aaSamw  * Use is subject to license terms.
24da6c28aaSamw  */
25da6c28aaSamw 
26da6c28aaSamw /*
27da6c28aaSamw  * The seek message is sent to set the current file pointer for FID.
28da6c28aaSamw  * This request should generally only be used by clients wishing to
29da6c28aaSamw  * find the size of a file, since all read and write requests include
30da6c28aaSamw  * the read or write file position as part of the SMB. This request
31da6c28aaSamw  * is inappropriate for large files, as the offsets specified are only
32da6c28aaSamw  * 32 bits.
33da6c28aaSamw  *
34da6c28aaSamw  * The CIFS/1.0 (1996) spec contains the following incomplete statement:
35da6c28aaSamw  *
36da6c28aaSamw  * "A seek which results in an Offset which can not be expressed
37da6c28aaSamw  *  in 32 bits returns the least significant."
38da6c28aaSamw  *
39da6c28aaSamw  * It would probably be a mistake to make an assumption about what this
40da6c28aaSamw  * statement means. So, for now, we return an error if the resultant
41da6c28aaSamw  * file offset is beyond the 32-bit limit.
42da6c28aaSamw  */
43da6c28aaSamw 
44da6c28aaSamw #include <smbsrv/smb_incl.h>
45da6c28aaSamw 
46da6c28aaSamw 
47da6c28aaSamw /*
48da6c28aaSamw  * smb_com_seek
49da6c28aaSamw  *
50da6c28aaSamw  * Client Request                     Description
51da6c28aaSamw  * ================================== =================================
52da6c28aaSamw  * UCHAR WordCount;                   Count of parameter words = 4
53da6c28aaSamw  * USHORT Fid;                        File handle
54da6c28aaSamw  * USHORT Mode;                       Seek mode: 0, 1 or 2
55da6c28aaSamw  * LONG Offset;                       Relative offset
56da6c28aaSamw  * USHORT ByteCount;                  Count of data bytes = 0
57da6c28aaSamw  *
58da6c28aaSamw  * The starting point of the seek is set by Mode:
59da6c28aaSamw  *
60da6c28aaSamw  *      0  seek from start of file
61da6c28aaSamw  *      1  seek from current current position
62da6c28aaSamw  *      2  seek from end of file
63da6c28aaSamw  *
64da6c28aaSamw  * The "current position" reflects the offset plus data length specified in
65da6c28aaSamw  * the previous read, write or seek request, and the pointer set by this
66da6c28aaSamw  * command will be replaced by the offset specified in the next read, write
67da6c28aaSamw  * or seek command.
68da6c28aaSamw  *
69da6c28aaSamw  * Server Response                    Description
70da6c28aaSamw  * ================================== =================================
71da6c28aaSamw  * UCHAR WordCount;                   Count of parameter words = 2
72da6c28aaSamw  * ULONG Offset;                      Offset from start of file
73da6c28aaSamw  * USHORT ByteCount;                  Count of data bytes = 0
74da6c28aaSamw  *
75da6c28aaSamw  * The response returns the new file pointer in Offset, which is expressed
76da6c28aaSamw  * as the offset from the start of the file, and may be beyond the current
77da6c28aaSamw  * end of file. An attempt to seek before the start of the file sets the
78da6c28aaSamw  * current file pointer to the start of the file.
79da6c28aaSamw  */
807b59d02dSjb150015 smb_sdrc_t
81faa1795aSjb150015 smb_pre_seek(smb_request_t *sr)
82faa1795aSjb150015 {
83faa1795aSjb150015 	DTRACE_SMB_1(op__Seek__start, smb_request_t *, sr);
84faa1795aSjb150015 	return (SDRC_SUCCESS);
85faa1795aSjb150015 }
86faa1795aSjb150015 
87faa1795aSjb150015 void
88faa1795aSjb150015 smb_post_seek(smb_request_t *sr)
89faa1795aSjb150015 {
90faa1795aSjb150015 	DTRACE_SMB_1(op__Seek__done, smb_request_t *, sr);
91faa1795aSjb150015 }
92faa1795aSjb150015 
93faa1795aSjb150015 smb_sdrc_t
94faa1795aSjb150015 smb_com_seek(smb_request_t *sr)
95da6c28aaSamw {
96da6c28aaSamw 	ushort_t	mode;
97da6c28aaSamw 	int32_t		off;
98da6c28aaSamw 	uint32_t	off_ret;
99da6c28aaSamw 	int		rc;
100da6c28aaSamw 
1017b59d02dSjb150015 	if (smbsr_decode_vwv(sr, "wwl", &sr->smb_fid, &mode, &off) != 0)
102faa1795aSjb150015 		return (SDRC_ERROR);
103da6c28aaSamw 
104da6c28aaSamw 	sr->fid_ofile = smb_ofile_lookup_by_fid(sr->tid_tree, sr->smb_fid);
105da6c28aaSamw 	if (sr->fid_ofile == NULL) {
106dc20a302Sas200622 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
107faa1795aSjb150015 		return (SDRC_ERROR);
108da6c28aaSamw 	}
109da6c28aaSamw 
110*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	sr->user_cr = smb_ofile_getcred(sr->fid_ofile);
111*b89a8333Snatalie li - Sun Microsystems - Irvine United States 
1127b59d02dSjb150015 	if (mode == SMB_SEEK_END)
1138c10a865Sas200622 		(void) smb_set_file_size(sr, sr->fid_ofile->f_node);
114da6c28aaSamw 
115dc20a302Sas200622 	if ((rc = smb_ofile_seek(sr->fid_ofile, mode, off, &off_ret)) != 0) {
116dc20a302Sas200622 		if (rc == EINVAL) {
117dc20a302Sas200622 			smbsr_error(sr, 0, ERRDOS, ERRbadfunc);
118faa1795aSjb150015 			return (SDRC_ERROR);
119dc20a302Sas200622 		} else {
120dc20a302Sas200622 			smbsr_error(sr, 0, ERRSRV, ERRerror);
121faa1795aSjb150015 			return (SDRC_ERROR);
122dc20a302Sas200622 		}
123dc20a302Sas200622 	}
124dc20a302Sas200622 
1257b59d02dSjb150015 	if (smbsr_encode_result(sr, 2, 0, "blw", 2, off_ret, 0))
126faa1795aSjb150015 		return (SDRC_ERROR);
1277b59d02dSjb150015 
128faa1795aSjb150015 	return (SDRC_SUCCESS);
129da6c28aaSamw }
130