1/*
2 * Copyright (c) 2000-2001, Boris Popov
3 * All rights reserved.
4 *
5 * Portions Copyright (C) 2001 - 2012 Apple Inc. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *    This product includes software developed by Boris Popov.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 */
35#ifndef _FS_SMBFS_SMBFS_SUBR_H_
36#define _FS_SMBFS_SMBFS_SUBR_H_
37
38#include <libkern/OSTypes.h>
39
40#ifdef MALLOC_DECLARE
41MALLOC_DECLARE(M_SMBFSDATA);
42#endif
43
44#ifdef SMB_VNODE_DEBUG
45#define SMBVDEBUG(format, args...) printf("%s: "format, __FUNCTION__ ,## args)
46#else
47#define SMBVDEBUG(format, args...)
48#endif
49
50/*
51 * Possible lock commands
52 */
53#define SMB_LOCK_EXCL		0
54#define	SMB_LOCK_SHARED		1
55#define	SMB_LOCK_RELEASE	2
56
57/*
58 * Currently the valid mask is only used for the reparse tag and vtype, but in
59 * the future I would like to use it for all items in the fa.
60 * NOTE: the vtype valid mask doesn't mean we don't have it, just that we are
61 * not sure if its correct.
62 */
63#define FA_VTYPE_VALID          0x00000001
64#define FA_REPARSE_TAG_VALID    0x00000002
65#define FA_FSTATUS_VALID        0x00000004
66#define FA_UNIX_MODES_VALID     0x00000008
67#define FA_RSRC_FORK_VALID      0x00000010
68#define FA_FINDERINFO_VALID     0x00000020
69#define FA_MAX_ACCESS_VALID     0x00000040
70
71struct smbfattr {
72	uint64_t		fa_valid_mask;
73	uint32_t		fa_attr;
74	u_quad_t		fa_size;	/* data stream size */
75	u_quad_t		fa_data_alloc;	/* data stream allocation size */
76	struct timespec	fa_atime;	/* Access Time */
77	struct timespec	fa_chtime;	/* Change Time */
78	struct timespec	fa_mtime;	/* Modify Time */
79	struct timespec	fa_crtime;	/* Create Time */
80	uint64_t		fa_ino;
81	struct timespec fa_reqtime;
82	enum vtype		fa_vtype;	/* vnode type, once we add the UNIX extensions this will contain any of the vtype */
83	uint64_t		fa_uid;
84	uint64_t		fa_gid;
85	uint64_t		fa_permissions;
86	uint64_t		fa_nlinks;
87	uint32_t		fa_flags_mask;
88	uint32_t		fa_unix;		/* Currently TRUE OR FALSE if we got the UNIX Info 2 data */
89	uint32_t		fa_reparse_tag;
90	uint16_t		fa_fstatus;
91	uint32_t		fa_created_disp;
92    uint64_t        fa_rsrc_size;       /* readdirattr support */
93    uint64_t        fa_rsrc_alloc;      /* readdirattr support */
94    uint8_t         fa_finder_info[32]; /* readdirattr support */
95	uint32_t		fa_max_access;      /* readdirattr and SMB 2/3 Creates */
96};
97
98/*
99 * Context to perform findfirst/findnext/findclose operations
100 */
101#define	SMBFS_RDD_FINDFIRST     0x01        /* Doing FindFirst versus FindNext */
102#define	SMBFS_RDD_EOF           0x02        /* end of search reached */
103#define	SMBFS_RDD_FINDSINGLE	0x04        /* not a wildcard search */
104#define	SMBFS_RDD_NOCLOSE       0x10        /* close not needed, it was closed when eof reached */
105#define	SMBFS_RDD_GOTRNAME      0x1000      /* Got a resume filename */
106
107/*
108 * Search context supplied by server
109 */
110#define	SMB_SKEYLEN		21			/* search context */
111#define SMB_DENTRYLEN		(SMB_SKEYLEN + 22)	/* entire entry */
112
113struct smbfs_fctx {
114	int				f_flags;	/* SMBFS_RDD_ */
115	struct smbfattr	f_attr;		/* current attributes */
116	char *			f_LocalName;
117	size_t			f_LocalNameLen;
118	char *			f_NetworkNameBuffer;
119	size_t			f_MaxNetworkNameBufferSize;
120	uint32_t		f_NetworkNameLen;
121	uint16_t		f_searchCount;	/* maximum number of entries to be returned */
122	int				f_attrmask;	/* SMB_FA_ */
123	int				f_sfm_conversion;	/* SFM Conversion Flag */
124	size_t			f_lookupNameLen;
125	const char *	f_lookupName;
126	struct smbnode*	f_dnp;
127	struct smb_share *f_share;
128	union {
129		struct smb_t2rq * uf_t2;
130	} f_urq;
131	int			f_ecnt;		/* entries left in current response */
132	uint32_t	f_eofs;		/* entry offset in data block */
133	u_char 		f_skey[SMB_SKEYLEN]; /* server side search context */
134	uint16_t	f_Sid;
135	uint16_t	f_infolevel;
136	uint32_t	f_rnamelen;
137	char *		f_rname;	/* resume name, always a network name */
138	uint32_t	f_rnameofs;
139	int			f_rkey;		/* resume key */
140    /* SMB 2/3 fields */
141    struct smb_rq *f_create_rqp;
142    struct smb_rq *f_query_rqp;
143    int         f_need_close;
144    SMBFID      f_create_fid;
145	uint32_t	f_resume_file_index;
146	uint32_t	f_output_buf_len;   /* bytes left in current response */
147};
148
149#define f_t2	f_urq.uf_t2
150
151struct smb_mount_args;
152
153int smbfs_smb_create_unix_symlink(struct smb_share *share, struct smbnode *dnp,
154                                  const char *name, size_t nmlen, char *target,
155                                  size_t targetlen, struct smbfattr *fap,
156                                  vfs_context_t context);
157int smbfs_smb_create_windows_symlink(struct smb_share *share,
158                                     struct smbnode *dnp,
159                                     const char *name, size_t nmlen,
160                                     char *target, size_t targetlen,
161                                     struct smbfattr *fap,
162                                     vfs_context_t context);
163int smb1fs_smb_create_reparse_symlink(struct smb_share *share, struct smbnode *dnp,
164                                      const char *name, size_t nmlen, char *target,
165                                      size_t targetlen, struct smbfattr *fap,
166                                      vfs_context_t context);
167void smbfs_update_symlink_cache(struct smbnode *np, char *target, size_t targetlen);
168int smbfs_smb_get_reparse_tag(struct smb_share *share, SMBFID fid,
169                              uint32_t *reparseTag, char **outTarget,
170                              size_t *outTargetlen, vfs_context_t context);
171int smb1fs_smb_markfordelete(struct smb_share *share, SMBFID fid,
172                             vfs_context_t context);
173int smb1fs_smb_reparse_read_symlink(struct smb_share *share, struct smbnode *np,
174                                    struct uio *uiop, vfs_context_t context);
175int smbfs_smb_windows_read_symlink(struct smb_share *share, struct smbnode *np,
176                                   struct uio *uiop, vfs_context_t context);
177int smbfs_smb_unix_read_symlink(struct smb_share *share, struct smbnode *np,
178                                struct uio *uiop, vfs_context_t context);
179int smb1fs_smb_qstreaminfo(struct smb_share *share, struct smbnode *np,
180                           const char *namep, size_t name_len,
181                           const char *strmname,
182                           uio_t uio, size_t *sizep,
183                           uint64_t *strmsize, uint64_t *strmsize_alloc,
184                           uint32_t *stream_flagsp,
185                           vfs_context_t context);
186void smbfs_unix_whoami(struct smb_share *share, struct smbmount *smp,
187                       vfs_context_t context);
188void smbfs_unix_qfsattr(struct smb_share *share, vfs_context_t context);
189void smb1fs_qfsattr(struct smb_share *share, vfs_context_t context);
190int smb1fs_statfs(struct smb_share *share, struct vfsstatfs *sbp,
191                  vfs_context_t context);
192int smbfs_smb_t2rename(struct smb_share *share, struct smbnode *np,
193                       const char *tname, size_t tnmlen, int overwrite,
194                       SMBFID *infid, vfs_context_t context);
195int smbfs_delete_openfile(struct smb_share *share, struct smbnode *dnp,
196						  struct smbnode *np, vfs_context_t context);
197int smb1fs_smb_flush(struct smb_share *share, SMBFID fid,
198                     vfs_context_t context);
199int smb1fs_seteof(struct smb_share *share, SMBFID fid, uint64_t newsize,
200                  vfs_context_t context);
201int smb1fs_set_allocation(struct smb_share *share, SMBFID fid,
202                          uint64_t newsize, vfs_context_t context);
203int smbfs_seteof(struct smb_share *share, struct smbnode *np, SMBFID fid,
204                     uint64_t newsize, vfs_context_t context);
205int smbfs_smb_fsync(struct smb_share *share, struct smbnode *np,
206                    vfs_context_t context);
207int smb1fs_smb_query_info(struct smb_share *share, struct smbnode *np,
208                          const char *name, size_t len, uint32_t *attr,
209                          vfs_context_t context);
210uint64_t smbfs_getino(struct smbnode *dnp, const char *name, size_t nmlen);
211int smb1fs_smb_lock(struct smb_share *share, int op, SMBFID fid, uint32_t pid,
212                    off_t start, uint64_t len, uint32_t timo,
213                    vfs_context_t context);
214int smb1fs_smb_setpattr(struct smb_share *share, struct smbnode *np, const char *name,
215                        size_t len, uint16_t attr, vfs_context_t context);
216int smbfs_set_hidden_bit(struct smb_share *share, struct smbnode *dnp, enum vtype vnode_type,
217                         const char *name, size_t len,
218                         Boolean hideit, vfs_context_t context);
219int smbfs_set_unix_info2(struct smb_share *share, struct smbnode *np,
220						 struct timespec *crtime, struct timespec *mtime,
221						 struct timespec *atime, uint64_t fsize, uint64_t perms,
222						 uint32_t FileFlags, uint32_t FileFlagsMask, vfs_context_t context);
223int smb1fs_smb_setpattrNT(struct smb_share *share, struct smbnode *np,
224                          uint32_t attr, struct timespec *crtime,
225                          struct timespec *mtime, struct timespec *atime,
226                          vfs_context_t context);
227int smb1fs_smb_setfattrNT(struct smb_share *share, uint32_t attr,
228                          SMBFID fid, struct timespec *crtime,
229                          struct timespec *mtime, struct timespec *atime,
230                          vfs_context_t context);
231int smbfs_tmpopen(struct smb_share *share, struct smbnode *np, uint32_t rights,
232                  SMBFID *fidp, vfs_context_t context);
233int smbfs_tmpclose(struct smb_share *share, struct smbnode *np, SMBFID fid,
234                   vfs_context_t context);
235int smb1fs_smb_open_maxaccess(struct smb_share *share, struct smbnode *dnp,
236                              const char *namep, size_t name_len,
237                              SMBFID *fidp, uint32_t *max_accessp,
238                              vfs_context_t context);
239int smb1fs_smb_openread(struct smb_share *share, struct smbnode *np, SMBFID *fid,
240                        uint32_t rights, uio_t uio, size_t *sizep,  const char *name,
241                        struct timespec *mtime, vfs_context_t context);
242int smb1fs_smb_open_read(struct smb_share *share, struct smbnode *dnp,
243                         const char *namep, size_t name_len,
244                         const char *strm_namep, size_t strm_name_len,
245                         SMBFID *fidp, uio_t uio, size_t *sizep,
246                         uint32_t *max_accessp,
247                         vfs_context_t context);
248int smbfs_smb_open_file(struct smb_share *share, struct smbnode *np,
249                        uint32_t rights, uint32_t shareMode, SMBFID *fidp,
250                        const char *name, size_t nmlen, int xattr,
251                        struct smbfattr *fap, vfs_context_t context);
252int smbfs_smb_open_xattr(struct smb_share *share, struct smbnode *np, uint32_t rights,
253                         uint32_t shareMode, SMBFID *fidp, const char *name,
254                         size_t *sizep, vfs_context_t context);
255int smbfs_smb_reopen_file(struct smb_share *share, struct smbnode *np,
256                          vfs_context_t context);
257int smb1fs_smb_close(struct smb_share *share, SMBFID fid,
258                     vfs_context_t context);
259int smbfs_smb_create(struct smb_share *share, struct smbnode *dnp,
260                     const char *name, size_t nmlen, uint32_t rights,
261                     SMBFID *fidp, uint32_t disp, int xattr,
262                     struct smbfattr *fap, vfs_context_t context);
263int smb1fs_smb_delete(struct smb_share *share, struct smbnode *np,
264                      const char *name, size_t nmlen, int xattr,
265                      vfs_context_t context);
266int smb1fs_smb_rename(struct smb_share *share, struct smbnode *src,
267                      struct smbnode *tdnp, const char *tname, size_t tnmlen,
268                      vfs_context_t context);
269int smbfs_smb_mkdir(struct smb_share *share, struct smbnode *dnp,
270                    const char *name, size_t len, struct smbfattr *fap,
271                    vfs_context_t context);
272int smb1fs_smb_rmdir(struct smb_share *share, struct smbnode *np,
273                    vfs_context_t context);
274int smbfs_smb_findopen(struct smb_share *share, struct smbnode *dnp,
275                       const char *lookupName, size_t lookupNameLen,
276                       struct smbfs_fctx **ctxpp, int wildCardLookup,
277                       vfs_context_t context);
278int smbfs_findnext(struct smbfs_fctx *, vfs_context_t);
279void smbfs_closedirlookup(struct smbnode *, vfs_context_t);
280int smbfs_lookup(struct smb_share *share, struct smbnode *dnp, const char **namep,
281				 size_t *nmlenp, struct smbfattr *fap, vfs_context_t context);
282int smbfs_smb_getsec(struct smb_share *share, struct smbnode *np,
283                     uint32_t desired_access, SMBFID fid, uint32_t selector,
284                     struct ntsecdesc **res, size_t *rt_len, vfs_context_t context);
285int smb1fs_setsec(struct smb_share *share, SMBFID fid, uint32_t selector,
286                  uint16_t ControlFlags, struct ntsid *owner,
287                  struct ntsid *group, struct ntacl *sacl, struct ntacl *dacl,
288                  vfs_context_t context);
289void smb_time_NT2local(uint64_t nsec, struct timespec *tsp);
290void smb_time_local2NT(struct timespec *tsp, uint64_t *nsec, int fat_fstype);
291char *smbfs_ntwrkname_tolocal(const char *ntwrk_name, size_t *nmlen, int usingUnicode);
292void smbfs_create_start_path(struct smbmount *smp, struct smb_mount_args *args,
293							 int usingUnicode);
294int smbfs_fullpath(struct mbchain *mbp, struct smbnode *dnp, const char *name,
295				   size_t *lenp, int name_flags, int usingUnicode, uint8_t sep);
296int smbfs_fullpath_stream(struct mbchain *mbp, struct smbnode *dnp,
297                          const char *namep, const char *strm_namep,
298                          size_t *lenp, size_t strm_name_len, int name_flags,
299                          int usingUnicode, uint8_t sep);
300struct smb_share *smb_get_share_with_reference(struct smbmount *smp);
301int smb1fs_smb_ntcreatex(struct smb_share *share, struct smbnode *dnp_or_np,
302                         uint32_t rights, uint32_t shareMode,
303                         enum vtype vt, SMBFID *fidp, const char *name,
304                         size_t in_nmlen, uint32_t disp, int xattr,
305                         struct smbfattr *fap, int do_create,
306                         vfs_context_t context);
307
308#endif /* !_FS_SMBFS_SMBFS_SUBR_H_ */
309