1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2017, Joyent, Inc.
* Copyright (c) 2011, 2017 by Delphix. All rights reserved.
* Copyright 2018 Nexenta Systems, Inc. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* University Copyright- Copyright (c) 1982, 1986, 1988
* The Regents of the University of California
* All Rights Reserved
*
* University Acknowledgment- Portions of this document are derived from
* software developed by the University of California, Berkeley, and its
* contributors.
*/
/*
* Portions of code from both of:
* syscall/open.c
* fs/vnode.c
* heavily modified for this use.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/t_lock.h>
#include <sys/errno.h>
#include <sys/cred.h>
#include <sys/user.h>
#include <sys/uio.h>
#include <sys/file.h>
#include <sys/pathname.h>
#include <sys/vfs.h>
#include <sys/vfs_opreg.h>
#include <sys/vnode.h>
#include <sys/rwstlock.h>
#include <sys/fem.h>
#include <sys/stat.h>
#include <sys/mode.h>
#include <sys/conf.h>
#include <sys/sysmacros.h>
#include <sys/cmn_err.h>
#include <sys/systm.h>
#include <sys/kmem.h>
#include <sys/debug.h>
#include <sys/acl.h>
#include <sys/nbmlock.h>
#include <sys/fcntl.h>
#include <fs/fs_subr.h>
#include <sys/taskq.h>
#include <fs/fs_reparse.h>
#include <sys/time.h>
#include <libfksmbfs.h>
/* close and release */
int
vn_close_rele(vnode_t *vp, int flag)
{
int error;
error = VOP_CLOSE(vp, flag, 0, 0, CRED(), NULL);
vn_rele(vp);
return (error);
}
/*
* Open/create a vnode.
* This may be callable by the kernel, the only known use
* of user context being that the current user credentials
* are used for permissions. crwhy is defined iff filemode & FCREAT.
*/
int
vn_open(
char *pnamep,
enum uio_seg seg,
int filemode,
int createmode,
struct vnode **vpp,
enum create crwhy,
mode_t umask)
{
struct vnode *vp;
int mode;
int accessflags;
int error;
int open_done = 0;
struct vattr vattr;
int estale_retry = 0;
mode = 0;
accessflags = 0;
if (filemode & FREAD)
mode |= VREAD;
if (filemode & (FWRITE|FTRUNC))
mode |= VWRITE;
if (filemode & (FSEARCH|FEXEC|FXATTRDIROPEN))
mode |= VEXEC;
if (filemode & FAPPEND)
accessflags |= V_APPEND;
top:
if (filemode & FCREAT) {
enum vcexcl excl;
/*
* Wish to create a file.
*/
vattr.va_type = VREG;
vattr.va_mode = createmode;
vattr.va_mask = AT_TYPE|AT_MODE;
if (filemode & FTRUNC) {
vattr.va_size = 0;
vattr.va_mask |= AT_SIZE;
}
if (filemode & FEXCL)
excl = EXCL;
else
excl = NONEXCL;
if ((error =
vn_create(pnamep, seg, &vattr, excl, mode, &vp, crwhy,
(filemode & ~(FTRUNC|FEXCL)), umask)) != 0)
return (error);
} else {
/*
* Wish to open a file. Just look it up.
* Was lookupnameat()
*/
if ((error = fake_lookup(NULL, pnamep, &vp)) != 0) {
if ((error == ESTALE) &&
fs_need_estale_retry(estale_retry++))
goto top;
return (error);
}
/*
* Want the XATTRDIR under it?
*/
if (filemode & FXATTRDIROPEN) {
vnode_t *xvp = NULL;
error = VOP_LOOKUP(vp, NULL, &xvp, NULL,
LOOKUP_XATTR, rootdir, CRED(), NULL,
NULL, NULL);
VN_RELE(vp);
vp = xvp;
/* continue with vp */
}
/*
* Can't write directories, active texts, or
* read-only filesystems. Can't truncate files
* on which mandatory locking is in effect.
*/
if (filemode & (FWRITE|FTRUNC)) {
if (vp->v_type == VDIR) {
error = EISDIR;
goto out;
}
}
/*
* Check permissions.
*/
if (error = VOP_ACCESS(vp, mode, accessflags, CRED(), NULL))
goto out;
/*
* Require FSEARCH to return a directory.
* Require FEXEC to return a regular file.
*/
if ((filemode & FSEARCH) && vp->v_type != VDIR) {
error = ENOTDIR;
goto out;
}
if ((filemode & FEXEC) && vp->v_type != VREG) {
error = ENOEXEC;
goto out;
}
}
/*
* Do remaining checks for FNOFOLLOW and FNOLINKS.
*/
if ((filemode & FNOFOLLOW) && vp->v_type == VLNK) {
error = ELOOP;
goto out;
}
if (filemode & FNOLINKS) {
vattr.va_mask = AT_NLINK;
if ((error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))) {
goto out;
}
if (vattr.va_nlink != 1) {
error = EMLINK;
goto out;
}
}
/*
* Opening a socket corresponding to the AF_UNIX pathname
* in the filesystem name space is not supported...
*/
if (vp->v_type == VSOCK) {
error = EOPNOTSUPP;
goto out;
}
/*
* Do opening protocol.
*/
error = VOP_OPEN(&vp, filemode, CRED(), NULL);
if (error)
goto out;
open_done = 1;
/*
* Truncate if required.
*/
if ((filemode & FTRUNC) && !(filemode & FCREAT)) {
vattr.va_size = 0;
vattr.va_mask = AT_SIZE;
if ((error = VOP_SETATTR(vp, &vattr, 0, CRED(), NULL)) != 0)
goto out;
}
out:
ASSERT(vp->v_count > 0);
if (error) {
if (open_done) {
(void) VOP_CLOSE(vp, filemode, 1, (offset_t)0, CRED(),
NULL);
open_done = 0;
}
VN_RELE(vp);
} else
*vpp = vp;
return (error);
}
/*
* Create a vnode (makenode).
*/
int
vn_create(
char *pnamep,
enum uio_seg seg,
struct vattr *vap,
enum vcexcl excl,
int mode,
struct vnode **vpp,
enum create why,
int flag,
mode_t umask)
{
struct vnode *dvp = NULL; /* ptr to parent dir vnode */
char *lastcomp = NULL;
int error;
ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
flag &= ~(FNOFOLLOW|FNOLINKS);
*vpp = NULL;
/*
* Lookup directory and last component
*/
error = fake_lookup_dir(pnamep, &dvp, &lastcomp);
if (error != 0) {
/* dir not found */
return (error);
}
/*
* If default ACLs are defined for the directory don't apply the
* umask if umask is passed.
*/
if (umask) {
/*
* Apply the umask if no default ACLs...
*/
vap->va_mode &= ~umask;
}
if (dvp->v_vfsp->vfs_flag & VFS_RDONLY) {
error = EROFS;
goto out;
}
/*
* Call mkdir() if specified, otherwise create().
*/
if (why == CRMKDIR) {
/*
* N.B., if vn_createat() ever requests
* case-insensitive behavior then it will need
* to be passed to VOP_MKDIR(). VOP_CREATE()
* will already get it via "flag"
*/
error = VOP_MKDIR(dvp, lastcomp, vap, vpp, CRED(),
NULL, 0, NULL);
} else {
error = VOP_CREATE(dvp, lastcomp, vap,
excl, mode, vpp, CRED(), flag, NULL, NULL);
}
out:
if (dvp != NULL)
VN_RELE(dvp);
return (error);
}
|