1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2020 Joyent, Inc.
25 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
27 * Copyright 2021 OmniOS Community Edition (OmniOSce) Association.
28 */
29
30 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
31 /* All Rights Reserved */
32
33 /*
34 * University Copyright- Copyright (c) 1982, 1986, 1988
35 * The Regents of the University of California
36 * All Rights Reserved
37 *
38 * University Acknowledgment- Portions of this document are derived from
39 * software developed by the University of California, Berkeley, and its
40 * contributors.
41 */
42
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/t_lock.h>
46 #include <sys/errno.h>
47 #include <sys/cred.h>
48 #include <sys/user.h>
49 #include <sys/uio.h>
50 #include <sys/file.h>
51 #include <sys/pathname.h>
52 #include <sys/vfs.h>
53 #include <sys/vfs_opreg.h>
54 #include <sys/vnode.h>
55 #include <sys/filio.h>
56 #include <sys/rwstlock.h>
57 #include <sys/fem.h>
58 #include <sys/stat.h>
59 #include <sys/mode.h>
60 #include <sys/conf.h>
61 #include <sys/sysmacros.h>
62 #include <sys/cmn_err.h>
63 #include <sys/systm.h>
64 #include <sys/kmem.h>
65 #include <sys/debug.h>
66 #include <c2/audit.h>
67 #include <sys/acl.h>
68 #include <sys/nbmlock.h>
69 #include <sys/fcntl.h>
70 #include <fs/fs_subr.h>
71 #include <sys/taskq.h>
72 #include <fs/fs_reparse.h>
73 #include <sys/time.h>
74 #include <sys/sdt.h>
75
76 /* Determine if this vnode is a file that is read-only */
77 #define ISROFILE(vp) \
78 ((vp)->v_type != VCHR && (vp)->v_type != VBLK && \
79 (vp)->v_type != VFIFO && vn_is_readonly(vp))
80
81 /* Tunable via /etc/system; used only by admin/install */
82 int nfs_global_client_only;
83
84 /*
85 * Array of vopstats_t for per-FS-type vopstats. This array has the same
86 * number of entries as and parallel to the vfssw table. (Arguably, it could
87 * be part of the vfssw table.) Once it's initialized, it's accessed using
88 * the same fstype index that is used to index into the vfssw table.
89 */
90 vopstats_t **vopstats_fstype;
91
92 /* vopstats initialization template used for fast initialization via bcopy() */
93 static vopstats_t *vs_templatep;
94
95 /* Kmem cache handle for vsk_anchor_t allocations */
96 kmem_cache_t *vsk_anchor_cache;
97
98 /* file events cleanup routine */
99 extern void free_fopdata(vnode_t *);
100
101 /*
102 * Root of AVL tree for the kstats associated with vopstats. Lock protects
103 * updates to vsktat_tree.
104 */
105 avl_tree_t vskstat_tree;
106 kmutex_t vskstat_tree_lock;
107
108 /* Global variable which enables/disables the vopstats collection */
109 int vopstats_enabled = 1;
110
111 /* Global used for empty/invalid v_path */
112 char *vn_vpath_empty = "";
113
114 /*
115 * forward declarations for internal vnode specific data (vsd)
116 */
117 static void *vsd_realloc(void *, size_t, size_t);
118
119 /*
120 * forward declarations for reparse point functions
121 */
122 static int fs_reparse_mark(char *target, vattr_t *vap, xvattr_t *xvattr);
123
124 /*
125 * VSD -- VNODE SPECIFIC DATA
126 * The v_data pointer is typically used by a file system to store a
127 * pointer to the file system's private node (e.g. ufs inode, nfs rnode).
128 * However, there are times when additional project private data needs
129 * to be stored separately from the data (node) pointed to by v_data.
130 * This additional data could be stored by the file system itself or
131 * by a completely different kernel entity. VSD provides a way for
132 * callers to obtain a key and store a pointer to private data associated
133 * with a vnode.
134 *
135 * Callers are responsible for protecting the vsd by holding v_vsd_lock
136 * for calls to vsd_set() and vsd_get().
137 */
138
139 /*
140 * vsd_lock protects:
141 * vsd_nkeys - creation and deletion of vsd keys
142 * vsd_list - insertion and deletion of vsd_node in the vsd_list
143 * vsd_destructor - adding and removing destructors to the list
144 */
145 static kmutex_t vsd_lock;
146 static uint_t vsd_nkeys; /* size of destructor array */
147 /* list of vsd_node's */
148 static list_t *vsd_list = NULL;
149 /* per-key destructor funcs */
150 static void (**vsd_destructor)(void *);
151
152 /*
153 * The following is the common set of actions needed to update the
154 * vopstats structure from a vnode op. Both VOPSTATS_UPDATE() and
155 * VOPSTATS_UPDATE_IO() do almost the same thing, except for the
156 * recording of the bytes transferred. Since the code is similar
157 * but small, it is nearly a duplicate. Consequently any changes
158 * to one may need to be reflected in the other.
159 * Rundown of the variables:
160 * vp - Pointer to the vnode
161 * counter - Partial name structure member to update in vopstats for counts
162 * bytecounter - Partial name structure member to update in vopstats for bytes
163 * bytesval - Value to update in vopstats for bytes
164 * fstype - Index into vsanchor_fstype[], same as index into vfssw[]
165 * vsp - Pointer to vopstats structure (either in vfs or vsanchor_fstype[i])
166 */
167
168 #define VOPSTATS_UPDATE(vp, counter) { \
169 vfs_t *vfsp = (vp)->v_vfsp; \
170 if (vfsp && vfsp->vfs_implp && \
171 (vfsp->vfs_flag & VFS_STATS) && (vp)->v_type != VBAD) { \
172 vopstats_t *vsp = &vfsp->vfs_vopstats; \
173 uint64_t *stataddr = &(vsp->n##counter.value.ui64); \
174 extern void __dtrace_probe___fsinfo_##counter(vnode_t *, \
175 size_t, uint64_t *); \
176 __dtrace_probe___fsinfo_##counter(vp, 0, stataddr); \
177 (*stataddr)++; \
178 if ((vsp = vfsp->vfs_fstypevsp) != NULL) { \
179 vsp->n##counter.value.ui64++; \
180 } \
181 } \
182 }
183
184 #define VOPSTATS_UPDATE_IO(vp, counter, bytecounter, bytesval) { \
185 vfs_t *vfsp = (vp)->v_vfsp; \
186 if (vfsp && vfsp->vfs_implp && \
187 (vfsp->vfs_flag & VFS_STATS) && (vp)->v_type != VBAD) { \
188 vopstats_t *vsp = &vfsp->vfs_vopstats; \
189 uint64_t *stataddr = &(vsp->n##counter.value.ui64); \
190 extern void __dtrace_probe___fsinfo_##counter(vnode_t *, \
191 size_t, uint64_t *); \
192 __dtrace_probe___fsinfo_##counter(vp, bytesval, stataddr); \
193 (*stataddr)++; \
194 vsp->bytecounter.value.ui64 += bytesval; \
195 if ((vsp = vfsp->vfs_fstypevsp) != NULL) { \
196 vsp->n##counter.value.ui64++; \
197 vsp->bytecounter.value.ui64 += bytesval; \
198 } \
199 } \
200 }
201
202 /*
203 * If the filesystem does not support XIDs map credential
204 * If the vfsp is NULL, perhaps we should also map?
205 */
206 #define VOPXID_MAP_CR(vp, cr) { \
207 vfs_t *vfsp = (vp)->v_vfsp; \
208 if (vfsp != NULL && (vfsp->vfs_flag & VFS_XID) == 0) \
209 cr = crgetmapped(cr); \
210 }
211
212 #define VOP_LATENCY_10MS 10000000
213 #define VOP_LATENCY_100MS 100000000
214 #define VOP_LATENCY_1S 1000000000
215 #define VOP_LATENCY_10S 10000000000
216
217 /*
218 * Convert stat(2) formats to vnode types and vice versa. (Knows about
219 * numerical order of S_IFMT and vnode types.)
220 */
221 enum vtype iftovt_tab[] = {
222 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
223 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VNON
224 };
225
226 ushort_t vttoif_tab[] = {
227 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFIFO,
228 S_IFDOOR, 0, S_IFSOCK, S_IFPORT, 0
229 };
230
231 /*
232 * The system vnode cache.
233 */
234
235 kmem_cache_t *vn_cache;
236
237
238 /*
239 * Vnode operations vector.
240 */
241
242 static const fs_operation_trans_def_t vn_ops_table[] = {
243 VOPNAME_OPEN, offsetof(struct vnodeops, vop_open),
244 fs_nosys, fs_nosys,
245
246 VOPNAME_CLOSE, offsetof(struct vnodeops, vop_close),
247 fs_nosys, fs_nosys,
248
249 VOPNAME_READ, offsetof(struct vnodeops, vop_read),
250 fs_nosys, fs_nosys,
251
252 VOPNAME_WRITE, offsetof(struct vnodeops, vop_write),
253 fs_nosys, fs_nosys,
254
255 VOPNAME_IOCTL, offsetof(struct vnodeops, vop_ioctl),
256 fs_nosys, fs_nosys,
257
258 VOPNAME_SETFL, offsetof(struct vnodeops, vop_setfl),
259 fs_setfl, fs_nosys,
260
261 VOPNAME_GETATTR, offsetof(struct vnodeops, vop_getattr),
262 fs_nosys, fs_nosys,
263
264 VOPNAME_SETATTR, offsetof(struct vnodeops, vop_setattr),
265 fs_nosys, fs_nosys,
266
267 VOPNAME_ACCESS, offsetof(struct vnodeops, vop_access),
268 fs_nosys, fs_nosys,
269
270 VOPNAME_LOOKUP, offsetof(struct vnodeops, vop_lookup),
271 fs_nosys, fs_nosys,
272
273 VOPNAME_CREATE, offsetof(struct vnodeops, vop_create),
274 fs_nosys, fs_nosys,
275
276 VOPNAME_REMOVE, offsetof(struct vnodeops, vop_remove),
277 fs_nosys, fs_nosys,
278
279 VOPNAME_LINK, offsetof(struct vnodeops, vop_link),
280 fs_nosys, fs_nosys,
281
282 VOPNAME_RENAME, offsetof(struct vnodeops, vop_rename),
283 fs_nosys, fs_nosys,
284
285 VOPNAME_MKDIR, offsetof(struct vnodeops, vop_mkdir),
286 fs_nosys, fs_nosys,
287
288 VOPNAME_RMDIR, offsetof(struct vnodeops, vop_rmdir),
289 fs_nosys, fs_nosys,
290
291 VOPNAME_READDIR, offsetof(struct vnodeops, vop_readdir),
292 fs_nosys, fs_nosys,
293
294 VOPNAME_SYMLINK, offsetof(struct vnodeops, vop_symlink),
295 fs_nosys, fs_nosys,
296
297 VOPNAME_READLINK, offsetof(struct vnodeops, vop_readlink),
298 fs_nosys, fs_nosys,
299
300 VOPNAME_FSYNC, offsetof(struct vnodeops, vop_fsync),
301 fs_nosys, fs_nosys,
302
303 VOPNAME_INACTIVE, offsetof(struct vnodeops, vop_inactive),
304 fs_nosys, fs_nosys,
305
306 VOPNAME_FID, offsetof(struct vnodeops, vop_fid),
307 fs_nosys, fs_nosys,
308
309 VOPNAME_RWLOCK, offsetof(struct vnodeops, vop_rwlock),
310 fs_rwlock, fs_rwlock,
311
312 VOPNAME_RWUNLOCK, offsetof(struct vnodeops, vop_rwunlock),
313 (fs_generic_func_p)(uintptr_t)fs_rwunlock,
314 (fs_generic_func_p)(uintptr_t)fs_rwunlock, /* no errors allowed */
315
316 VOPNAME_SEEK, offsetof(struct vnodeops, vop_seek),
317 fs_nosys, fs_nosys,
318
319 VOPNAME_CMP, offsetof(struct vnodeops, vop_cmp),
320 fs_cmp, fs_cmp, /* no errors allowed */
321
322 VOPNAME_FRLOCK, offsetof(struct vnodeops, vop_frlock),
323 fs_frlock, fs_nosys,
324
325 VOPNAME_SPACE, offsetof(struct vnodeops, vop_space),
326 fs_nosys, fs_nosys,
327
328 VOPNAME_REALVP, offsetof(struct vnodeops, vop_realvp),
329 fs_nosys, fs_nosys,
330
331 VOPNAME_GETPAGE, offsetof(struct vnodeops, vop_getpage),
332 fs_nosys, fs_nosys,
333
334 VOPNAME_PUTPAGE, offsetof(struct vnodeops, vop_putpage),
335 fs_nosys, fs_nosys,
336
337 VOPNAME_MAP, offsetof(struct vnodeops, vop_map),
338 (fs_generic_func_p) fs_nosys_map,
339 (fs_generic_func_p) fs_nosys_map,
340
341 VOPNAME_ADDMAP, offsetof(struct vnodeops, vop_addmap),
342 (fs_generic_func_p) fs_nosys_addmap,
343 (fs_generic_func_p) fs_nosys_addmap,
344
345 VOPNAME_DELMAP, offsetof(struct vnodeops, vop_delmap),
346 fs_nosys, fs_nosys,
347
348 VOPNAME_POLL, offsetof(struct vnodeops, vop_poll),
349 (fs_generic_func_p) fs_poll, (fs_generic_func_p) fs_nosys_poll,
350
351 VOPNAME_DUMP, offsetof(struct vnodeops, vop_dump),
352 fs_nosys, fs_nosys,
353
354 VOPNAME_PATHCONF, offsetof(struct vnodeops, vop_pathconf),
355 fs_pathconf, fs_nosys,
356
357 VOPNAME_PAGEIO, offsetof(struct vnodeops, vop_pageio),
358 fs_nosys, fs_nosys,
359
360 VOPNAME_DUMPCTL, offsetof(struct vnodeops, vop_dumpctl),
361 fs_nosys, fs_nosys,
362
363 VOPNAME_DISPOSE, offsetof(struct vnodeops, vop_dispose),
364 (fs_generic_func_p)(uintptr_t)fs_dispose,
365 (fs_generic_func_p)(uintptr_t)fs_nodispose,
366
367 VOPNAME_SETSECATTR, offsetof(struct vnodeops, vop_setsecattr),
368 fs_nosys, fs_nosys,
369
370 VOPNAME_GETSECATTR, offsetof(struct vnodeops, vop_getsecattr),
371 fs_fab_acl, fs_nosys,
372
373 VOPNAME_SHRLOCK, offsetof(struct vnodeops, vop_shrlock),
374 fs_shrlock, fs_nosys,
375
376 VOPNAME_VNEVENT, offsetof(struct vnodeops, vop_vnevent),
377 (fs_generic_func_p) fs_vnevent_nosupport,
378 (fs_generic_func_p) fs_vnevent_nosupport,
379
380 VOPNAME_REQZCBUF, offsetof(struct vnodeops, vop_reqzcbuf),
381 fs_nosys, fs_nosys,
382
383 VOPNAME_RETZCBUF, offsetof(struct vnodeops, vop_retzcbuf),
384 fs_nosys, fs_nosys,
385
386 NULL, 0, NULL, NULL
387 };
388
389 /* Extensible attribute (xva) routines. */
390
391 /*
392 * Zero out the structure, set the size of the requested/returned bitmaps,
393 * set AT_XVATTR in the embedded vattr_t's va_mask, and set up the pointer
394 * to the returned attributes array.
395 */
396 void
397 xva_init(xvattr_t *xvap)
398 {
399 bzero(xvap, sizeof (xvattr_t));
400 xvap->xva_mapsize = XVA_MAPSIZE;
401 xvap->xva_magic = XVA_MAGIC;
402 xvap->xva_vattr.va_mask = AT_XVATTR;
403 xvap->xva_rtnattrmapp = &(xvap->xva_rtnattrmap)[0];
404 }
405
406 /*
407 * If AT_XVATTR is set, returns a pointer to the embedded xoptattr_t
408 * structure. Otherwise, returns NULL.
409 */
410 xoptattr_t *
411 xva_getxoptattr(xvattr_t *xvap)
412 {
413 xoptattr_t *xoap = NULL;
414 if (xvap->xva_vattr.va_mask & AT_XVATTR)
415 xoap = &xvap->xva_xoptattrs;
416 return (xoap);
417 }
418
419 /*
420 * Used by the AVL routines to compare two vsk_anchor_t structures in the tree.
421 * We use the f_fsid reported by VFS_STATVFS() since we use that for the
422 * kstat name.
423 */
424 static int
425 vska_compar(const void *n1, const void *n2)
426 {
427 int ret;
428 ulong_t p1 = ((vsk_anchor_t *)n1)->vsk_fsid;
429 ulong_t p2 = ((vsk_anchor_t *)n2)->vsk_fsid;
430
431 if (p1 < p2) {
432 ret = -1;
433 } else if (p1 > p2) {
434 ret = 1;
435 } else {
436 ret = 0;
437 }
438
439 return (ret);
440 }
441
442 /*
443 * Used to create a single template which will be bcopy()ed to a newly
444 * allocated vsanchor_combo_t structure in new_vsanchor(), below.
445 */
446 static vopstats_t *
447 create_vopstats_template()
448 {
449 vopstats_t *vsp;
450
451 vsp = kmem_alloc(sizeof (vopstats_t), KM_SLEEP);
452 bzero(vsp, sizeof (*vsp)); /* Start fresh */
453
454 /* VOP_OPEN */
455 kstat_named_init(&vsp->nopen, "nopen", KSTAT_DATA_UINT64);
456 /* VOP_CLOSE */
457 kstat_named_init(&vsp->nclose, "nclose", KSTAT_DATA_UINT64);
458 /* VOP_READ I/O */
459 kstat_named_init(&vsp->nread, "nread", KSTAT_DATA_UINT64);
460 kstat_named_init(&vsp->read_bytes, "read_bytes", KSTAT_DATA_UINT64);
461 /* VOP_WRITE I/O */
462 kstat_named_init(&vsp->nwrite, "nwrite", KSTAT_DATA_UINT64);
463 kstat_named_init(&vsp->write_bytes, "write_bytes", KSTAT_DATA_UINT64);
464 /* VOP_IOCTL */
465 kstat_named_init(&vsp->nioctl, "nioctl", KSTAT_DATA_UINT64);
466 /* VOP_SETFL */
467 kstat_named_init(&vsp->nsetfl, "nsetfl", KSTAT_DATA_UINT64);
468 /* VOP_GETATTR */
469 kstat_named_init(&vsp->ngetattr, "ngetattr", KSTAT_DATA_UINT64);
470 /* VOP_SETATTR */
471 kstat_named_init(&vsp->nsetattr, "nsetattr", KSTAT_DATA_UINT64);
472 /* VOP_ACCESS */
473 kstat_named_init(&vsp->naccess, "naccess", KSTAT_DATA_UINT64);
474 /* VOP_LOOKUP */
475 kstat_named_init(&vsp->nlookup, "nlookup", KSTAT_DATA_UINT64);
476 /* VOP_CREATE */
477 kstat_named_init(&vsp->ncreate, "ncreate", KSTAT_DATA_UINT64);
478 /* VOP_REMOVE */
479 kstat_named_init(&vsp->nremove, "nremove", KSTAT_DATA_UINT64);
480 /* VOP_LINK */
481 kstat_named_init(&vsp->nlink, "nlink", KSTAT_DATA_UINT64);
482 /* VOP_RENAME */
483 kstat_named_init(&vsp->nrename, "nrename", KSTAT_DATA_UINT64);
484 /* VOP_MKDIR */
485 kstat_named_init(&vsp->nmkdir, "nmkdir", KSTAT_DATA_UINT64);
486 /* VOP_RMDIR */
487 kstat_named_init(&vsp->nrmdir, "nrmdir", KSTAT_DATA_UINT64);
488 /* VOP_READDIR I/O */
489 kstat_named_init(&vsp->nreaddir, "nreaddir", KSTAT_DATA_UINT64);
490 kstat_named_init(&vsp->readdir_bytes, "readdir_bytes",
491 KSTAT_DATA_UINT64);
492 /* VOP_SYMLINK */
493 kstat_named_init(&vsp->nsymlink, "nsymlink", KSTAT_DATA_UINT64);
494 /* VOP_READLINK */
495 kstat_named_init(&vsp->nreadlink, "nreadlink", KSTAT_DATA_UINT64);
496 /* VOP_FSYNC */
497 kstat_named_init(&vsp->nfsync, "nfsync", KSTAT_DATA_UINT64);
498 /* VOP_INACTIVE */
499 kstat_named_init(&vsp->ninactive, "ninactive", KSTAT_DATA_UINT64);
500 /* VOP_FID */
501 kstat_named_init(&vsp->nfid, "nfid", KSTAT_DATA_UINT64);
502 /* VOP_RWLOCK */
503 kstat_named_init(&vsp->nrwlock, "nrwlock", KSTAT_DATA_UINT64);
504 /* VOP_RWUNLOCK */
505 kstat_named_init(&vsp->nrwunlock, "nrwunlock", KSTAT_DATA_UINT64);
506 /* VOP_SEEK */
507 kstat_named_init(&vsp->nseek, "nseek", KSTAT_DATA_UINT64);
508 /* VOP_CMP */
509 kstat_named_init(&vsp->ncmp, "ncmp", KSTAT_DATA_UINT64);
510 /* VOP_FRLOCK */
511 kstat_named_init(&vsp->nfrlock, "nfrlock", KSTAT_DATA_UINT64);
512 /* VOP_SPACE */
513 kstat_named_init(&vsp->nspace, "nspace", KSTAT_DATA_UINT64);
514 /* VOP_REALVP */
515 kstat_named_init(&vsp->nrealvp, "nrealvp", KSTAT_DATA_UINT64);
516 /* VOP_GETPAGE */
517 kstat_named_init(&vsp->ngetpage, "ngetpage", KSTAT_DATA_UINT64);
518 /* VOP_PUTPAGE */
519 kstat_named_init(&vsp->nputpage, "nputpage", KSTAT_DATA_UINT64);
520 /* VOP_MAP */
521 kstat_named_init(&vsp->nmap, "nmap", KSTAT_DATA_UINT64);
522 /* VOP_ADDMAP */
523 kstat_named_init(&vsp->naddmap, "naddmap", KSTAT_DATA_UINT64);
524 /* VOP_DELMAP */
525 kstat_named_init(&vsp->ndelmap, "ndelmap", KSTAT_DATA_UINT64);
526 /* VOP_POLL */
527 kstat_named_init(&vsp->npoll, "npoll", KSTAT_DATA_UINT64);
528 /* VOP_DUMP */
529 kstat_named_init(&vsp->ndump, "ndump", KSTAT_DATA_UINT64);
530 /* VOP_PATHCONF */
531 kstat_named_init(&vsp->npathconf, "npathconf", KSTAT_DATA_UINT64);
532 /* VOP_PAGEIO */
533 kstat_named_init(&vsp->npageio, "npageio", KSTAT_DATA_UINT64);
534 /* VOP_DUMPCTL */
535 kstat_named_init(&vsp->ndumpctl, "ndumpctl", KSTAT_DATA_UINT64);
536 /* VOP_DISPOSE */
537 kstat_named_init(&vsp->ndispose, "ndispose", KSTAT_DATA_UINT64);
538 /* VOP_SETSECATTR */
539 kstat_named_init(&vsp->nsetsecattr, "nsetsecattr", KSTAT_DATA_UINT64);
540 /* VOP_GETSECATTR */
541 kstat_named_init(&vsp->ngetsecattr, "ngetsecattr", KSTAT_DATA_UINT64);
542 /* VOP_SHRLOCK */
543 kstat_named_init(&vsp->nshrlock, "nshrlock", KSTAT_DATA_UINT64);
544 /* VOP_VNEVENT */
545 kstat_named_init(&vsp->nvnevent, "nvnevent", KSTAT_DATA_UINT64);
546 /* VOP_REQZCBUF */
547 kstat_named_init(&vsp->nreqzcbuf, "nreqzcbuf", KSTAT_DATA_UINT64);
548 /* VOP_RETZCBUF */
549 kstat_named_init(&vsp->nretzcbuf, "nretzcbuf", KSTAT_DATA_UINT64);
550
551 return (vsp);
552 }
553
554 /*
555 * Creates a kstat structure associated with a vopstats structure.
556 */
557 kstat_t *
558 new_vskstat(char *ksname, vopstats_t *vsp)
559 {
560 kstat_t *ksp;
561
562 if (!vopstats_enabled) {
563 return (NULL);
564 }
565
566 ksp = kstat_create("unix", 0, ksname, "misc", KSTAT_TYPE_NAMED,
567 sizeof (vopstats_t)/sizeof (kstat_named_t),
568 KSTAT_FLAG_VIRTUAL|KSTAT_FLAG_WRITABLE);
569 if (ksp) {
570 ksp->ks_data = vsp;
571 kstat_install(ksp);
572 }
573
574 return (ksp);
575 }
576
577 /*
578 * Called from vfsinit() to initialize the support mechanisms for vopstats
579 */
580 void
581 vopstats_startup()
582 {
583 if (!vopstats_enabled)
584 return;
585
586 /*
587 * Creates the AVL tree which holds per-vfs vopstat anchors. This
588 * is necessary since we need to check if a kstat exists before we
589 * attempt to create it. Also, initialize its lock.
590 */
591 avl_create(&vskstat_tree, vska_compar, sizeof (vsk_anchor_t),
592 offsetof(vsk_anchor_t, vsk_node));
593 mutex_init(&vskstat_tree_lock, NULL, MUTEX_DEFAULT, NULL);
594
595 vsk_anchor_cache = kmem_cache_create("vsk_anchor_cache",
596 sizeof (vsk_anchor_t), sizeof (uintptr_t), NULL, NULL, NULL,
597 NULL, NULL, 0);
598
599 /*
600 * Set up the array of pointers for the vopstats-by-FS-type.
601 * The entries will be allocated/initialized as each file system
602 * goes through modload/mod_installfs.
603 */
604 vopstats_fstype = (vopstats_t **)kmem_zalloc(
605 (sizeof (vopstats_t *) * nfstype), KM_SLEEP);
606
607 /* Set up the global vopstats initialization template */
608 vs_templatep = create_vopstats_template();
609 }
610
611 /*
612 * We need to have the all of the counters zeroed.
613 * The initialization of the vopstats_t includes on the order of
614 * 50 calls to kstat_named_init(). Rather that do that on every call,
615 * we do it once in a template (vs_templatep) then bcopy it over.
616 */
617 void
618 initialize_vopstats(vopstats_t *vsp)
619 {
620 if (vsp == NULL)
621 return;
622
623 bcopy(vs_templatep, vsp, sizeof (vopstats_t));
624 }
625
626 /*
627 * If possible, determine which vopstats by fstype to use and
628 * return a pointer to the caller.
629 */
630 vopstats_t *
631 get_fstype_vopstats(vfs_t *vfsp, struct vfssw *vswp)
632 {
633 int fstype = 0; /* Index into vfssw[] */
634 vopstats_t *vsp = NULL;
635
636 if (vfsp == NULL || (vfsp->vfs_flag & VFS_STATS) == 0 ||
637 !vopstats_enabled)
638 return (NULL);
639 /*
640 * Set up the fstype. We go to so much trouble because all versions
641 * of NFS use the same fstype in their vfs even though they have
642 * distinct entries in the vfssw[] table.
643 * NOTE: A special vfs (e.g., EIO_vfs) may not have an entry.
644 */
645 if (vswp) {
646 fstype = vswp - vfssw; /* Gets us the index */
647 } else {
648 fstype = vfsp->vfs_fstype;
649 }
650
651 /*
652 * Point to the per-fstype vopstats. The only valid values are
653 * non-zero positive values less than the number of vfssw[] table
654 * entries.
655 */
656 if (fstype > 0 && fstype < nfstype) {
657 vsp = vopstats_fstype[fstype];
658 }
659
660 return (vsp);
661 }
662
663 /*
664 * Generate a kstat name, create the kstat structure, and allocate a
665 * vsk_anchor_t to hold it together. Return the pointer to the vsk_anchor_t
666 * to the caller. This must only be called from a mount.
667 */
668 vsk_anchor_t *
669 get_vskstat_anchor(vfs_t *vfsp)
670 {
671 char kstatstr[KSTAT_STRLEN]; /* kstat name for vopstats */
672 statvfs64_t statvfsbuf; /* Needed to find f_fsid */
673 vsk_anchor_t *vskp = NULL; /* vfs <--> kstat anchor */
674 kstat_t *ksp; /* Ptr to new kstat */
675 avl_index_t where; /* Location in the AVL tree */
676
677 if (vfsp == NULL || vfsp->vfs_implp == NULL ||
678 (vfsp->vfs_flag & VFS_STATS) == 0 || !vopstats_enabled)
679 return (NULL);
680
681 /* Need to get the fsid to build a kstat name */
682 if (VFS_STATVFS(vfsp, &statvfsbuf) == 0) {
683 /* Create a name for our kstats based on fsid */
684 (void) snprintf(kstatstr, KSTAT_STRLEN, "%s%lx",
685 VOPSTATS_STR, statvfsbuf.f_fsid);
686
687 /* Allocate and initialize the vsk_anchor_t */
688 vskp = kmem_cache_alloc(vsk_anchor_cache, KM_SLEEP);
689 bzero(vskp, sizeof (*vskp));
690 vskp->vsk_fsid = statvfsbuf.f_fsid;
691
692 mutex_enter(&vskstat_tree_lock);
693 if (avl_find(&vskstat_tree, vskp, &where) == NULL) {
694 avl_insert(&vskstat_tree, vskp, where);
695 mutex_exit(&vskstat_tree_lock);
696
697 /*
698 * Now that we've got the anchor in the AVL
699 * tree, we can create the kstat.
700 */
701 ksp = new_vskstat(kstatstr, &vfsp->vfs_vopstats);
702 if (ksp) {
703 vskp->vsk_ksp = ksp;
704 }
705 } else {
706 /* Oops, found one! Release memory and lock. */
707 mutex_exit(&vskstat_tree_lock);
708 kmem_cache_free(vsk_anchor_cache, vskp);
709 vskp = NULL;
710 }
711 }
712 return (vskp);
713 }
714
715 /*
716 * We're in the process of tearing down the vfs and need to cleanup
717 * the data structures associated with the vopstats. Must only be called
718 * from dounmount().
719 */
720 void
721 teardown_vopstats(vfs_t *vfsp)
722 {
723 vsk_anchor_t *vskap;
724 avl_index_t where;
725
726 if (vfsp == NULL || vfsp->vfs_implp == NULL ||
727 (vfsp->vfs_flag & VFS_STATS) == 0 || !vopstats_enabled)
728 return;
729
730 /* This is a safe check since VFS_STATS must be set (see above) */
731 if ((vskap = vfsp->vfs_vskap) == NULL)
732 return;
733
734 /* Whack the pointer right away */
735 vfsp->vfs_vskap = NULL;
736
737 /* Lock the tree, remove the node, and delete the kstat */
738 mutex_enter(&vskstat_tree_lock);
739 if (avl_find(&vskstat_tree, vskap, &where)) {
740 avl_remove(&vskstat_tree, vskap);
741 }
742
743 if (vskap->vsk_ksp) {
744 kstat_delete(vskap->vsk_ksp);
745 }
746 mutex_exit(&vskstat_tree_lock);
747
748 kmem_cache_free(vsk_anchor_cache, vskap);
749 }
750
751 /*
752 * Read or write a vnode. Called from kernel code.
753 */
754 int
755 vn_rdwr(
756 enum uio_rw rw,
757 struct vnode *vp,
758 caddr_t base,
759 ssize_t len,
760 offset_t offset,
761 enum uio_seg seg,
762 int ioflag,
763 rlim64_t ulimit, /* meaningful only if rw is UIO_WRITE */
764 cred_t *cr,
765 ssize_t *residp)
766 {
767 struct uio uio;
768 struct iovec iov;
769 int error;
770 int in_crit = 0;
771
772 if (rw == UIO_WRITE && ISROFILE(vp))
773 return (EROFS);
774
775 if (len < 0)
776 return (EIO);
777
778 VOPXID_MAP_CR(vp, cr);
779
780 iov.iov_base = base;
781 iov.iov_len = len;
782 uio.uio_iov = &iov;
783 uio.uio_iovcnt = 1;
784 uio.uio_loffset = offset;
785 uio.uio_segflg = (short)seg;
786 uio.uio_resid = len;
787 uio.uio_llimit = ulimit;
788
789 /*
790 * We have to enter the critical region before calling VOP_RWLOCK
791 * to avoid a deadlock with ufs.
792 */
793 if (nbl_need_check(vp)) {
794 int svmand;
795
796 nbl_start_crit(vp, RW_READER);
797 in_crit = 1;
798 error = nbl_svmand(vp, cr, &svmand);
799 if (error != 0)
800 goto done;
801 if (nbl_conflict(vp, rw == UIO_WRITE ? NBL_WRITE : NBL_READ,
802 uio.uio_offset, uio.uio_resid, svmand, NULL)) {
803 error = EACCES;
804 goto done;
805 }
806 }
807
808 (void) VOP_RWLOCK(vp,
809 rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE, NULL);
810 if (rw == UIO_WRITE) {
811 uio.uio_fmode = FWRITE;
812 uio.uio_extflg = UIO_COPY_DEFAULT;
813 error = VOP_WRITE(vp, &uio, ioflag, cr, NULL);
814 } else {
815 uio.uio_fmode = FREAD;
816 uio.uio_extflg = UIO_COPY_CACHED;
817 error = VOP_READ(vp, &uio, ioflag, cr, NULL);
818 }
819 VOP_RWUNLOCK(vp,
820 rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE, NULL);
821 if (residp)
822 *residp = uio.uio_resid;
823 else if (uio.uio_resid)
824 error = EIO;
825
826 done:
827 if (in_crit)
828 nbl_end_crit(vp);
829 return (error);
830 }
831
832 /*
833 * Release a vnode. Call VOP_INACTIVE on last reference or
834 * decrement reference count.
835 *
836 * To avoid race conditions, the v_count is left at 1 for
837 * the call to VOP_INACTIVE. This prevents another thread
838 * from reclaiming and releasing the vnode *before* the
839 * VOP_INACTIVE routine has a chance to destroy the vnode.
840 * We can't have more than 1 thread calling VOP_INACTIVE
841 * on a vnode.
842 */
843 void
844 vn_rele(vnode_t *vp)
845 {
846 mutex_enter(&vp->v_lock);
847 if (vp->v_count == 1) {
848 mutex_exit(&vp->v_lock);
849 VOP_INACTIVE(vp, CRED(), NULL);
850 return;
851 }
852 else{
853 VERIFY(vp->v_count > 0);
854 }
855 VN_RELE_LOCKED(vp);
856 mutex_exit(&vp->v_lock);
857 }
858
859 void
860 vn_phantom_rele(vnode_t *vp)
861 {
862 mutex_enter(&vp->v_lock);
863
864 vp->v_phantom_count--;
865 DTRACE_PROBE1(vn__phantom_rele, vnode_t *, vp);
866 if (vp->v_count == 1) {
867 ASSERT0(vp->v_phantom_count);
868 mutex_exit(&vp->v_lock);
869 VOP_INACTIVE(vp, CRED(), NULL);
870 return;
871 }else{
872 VERIFY(vp->v_count > 0);
873 VERIFY3U(vp->v_count, >=, vp->v_phantom_count);
874 }
875 VN_RELE_LOCKED(vp);
876 mutex_exit(&vp->v_lock);
877 }
878
879 /*
880 * Return the number of non-phantom holds. Things such as portfs will use
881 * phantom holds to prevent it from blocking filesystems from mounting over
882 * watched directories.
883 */
884 uint_t
885 vn_count(vnode_t *vp)
886 {
887 ASSERT(MUTEX_HELD(&vp->v_lock));
888 return (vp->v_count - vp->v_phantom_count);
889 }
890
891 /*
892 * Release a vnode referenced by the DNLC. Multiple DNLC references are treated
893 * as a single reference, so v_count is not decremented until the last DNLC hold
894 * is released. This makes it possible to distinguish vnodes that are referenced
895 * only by the DNLC.
896 */
897 void
898 vn_rele_dnlc(vnode_t *vp)
899 {
900 mutex_enter(&vp->v_lock);
901
902 if (--vp->v_count_dnlc == 0) {
903 if (vp->v_count == 1) {
904 mutex_exit(&vp->v_lock);
905 VOP_INACTIVE(vp, CRED(), NULL);
906 return;
907 }
908 VN_RELE_LOCKED(vp);
909 }else{
910 VERIFY((vp->v_count > 0) && (vp->v_count_dnlc > 0));
911 }
912 mutex_exit(&vp->v_lock);
913 }
914
915 /*
916 * Like vn_rele() except that it clears v_stream under v_lock.
917 * This is used by sockfs when it dismantles the association between
918 * the sockfs node and the vnode in the underlying file system.
919 * v_lock has to be held to prevent a thread coming through the lookupname
920 * path from accessing a stream head that is going away.
921 */
922 void
923 vn_rele_stream(vnode_t *vp)
924 {
925 mutex_enter(&vp->v_lock);
926
927 vp->v_stream = NULL;
928 if (vp->v_count == 1) {
929 mutex_exit(&vp->v_lock);
930 VOP_INACTIVE(vp, CRED(), NULL);
931 return;
932 }
933 else{
934 VERIFY(vp->v_count > 0);
935 }
936 VN_RELE_LOCKED(vp);
937 mutex_exit(&vp->v_lock);
938 }
939
940 static void
941 vn_rele_inactive(vnode_t *vp)
942 {
943 VOP_INACTIVE(vp, CRED(), NULL);
944 }
945
946 /*
947 * Like vn_rele() except if we are going to call VOP_INACTIVE() then do it
948 * asynchronously using a taskq. This can avoid deadlocks caused by re-entering
949 * the file system as a result of releasing the vnode. Note, file systems
950 * already have to handle the race where the vnode is incremented before the
951 * inactive routine is called and does its locking.
952 *
953 * Warning: Excessive use of this routine can lead to performance problems.
954 * This is because taskqs throttle back allocation if too many are created.
955 */
956 void
957 vn_rele_async(vnode_t *vp, taskq_t *taskq)
958 {
959 mutex_enter(&vp->v_lock);
960 if (vp->v_count == 1) {
961 mutex_exit(&vp->v_lock);
962 VERIFY(taskq_dispatch(taskq, (task_func_t *)vn_rele_inactive,
963 vp, TQ_SLEEP) != TASKQID_INVALID);
964 return;
965 }
966 else{
967 VERIFY(vp->v_count > 0);
968 }
969 VN_RELE_LOCKED(vp);
970 mutex_exit(&vp->v_lock);
971 }
972
973 int
974 vn_open(
975 char *pnamep,
976 enum uio_seg seg,
977 int filemode,
978 int createmode,
979 struct vnode **vpp,
980 enum create crwhy,
981 mode_t umask)
982 {
983 return (vn_openat(pnamep, seg, filemode, createmode, vpp, crwhy,
984 umask, NULL, -1));
985 }
986
987
988 /*
989 * Open/create a vnode.
990 * This may be callable by the kernel, the only known use
991 * of user context being that the current user credentials
992 * are used for permissions. crwhy is defined iff filemode & FCREAT.
993 */
994 int
995 vn_openat(
996 char *pnamep,
997 enum uio_seg seg,
998 int filemode,
999 int createmode,
1000 struct vnode **vpp,
1001 enum create crwhy,
1002 mode_t umask,
1003 struct vnode *startvp,
1004 int fd)
1005 {
1006 struct vnode *vp;
1007 int mode;
1008 int accessflags;
1009 int error;
1010 int in_crit = 0;
1011 int open_done = 0;
1012 int shrlock_done = 0;
1013 struct vattr vattr;
1014 enum symfollow follow;
1015 int estale_retry = 0;
1016 struct shrlock shr;
1017 struct shr_locowner shr_own;
1018 boolean_t create;
1019
1020 mode = 0;
1021 accessflags = 0;
1022 if (filemode & FREAD)
1023 mode |= VREAD;
1024 if (filemode & (FWRITE|FTRUNC))
1025 mode |= VWRITE;
1026 if (filemode & (FSEARCH|FEXEC|FXATTRDIROPEN))
1027 mode |= VEXEC;
1028
1029 /* symlink interpretation */
1030 if (filemode & FNOFOLLOW)
1031 follow = NO_FOLLOW;
1032 else
1033 follow = FOLLOW;
1034
1035 if (filemode & FAPPEND)
1036 accessflags |= V_APPEND;
1037
1038 /*
1039 * We need to handle the case of FCREAT | FDIRECTORY and the case of
1040 * FEXCL. If all three are specified, then we always fail because we
1041 * cannot create a directory through this interface and FEXCL says we
1042 * need to fail the request if we can't create it. If, however, only
1043 * FCREAT | FDIRECTORY are specified, then we can treat this as the case
1044 * of opening a file that already exists. If it exists, we can do
1045 * something and if not, we fail. Effectively FCREAT | FDIRECTORY is
1046 * treated as FDIRECTORY.
1047 */
1048 if ((filemode & (FCREAT | FDIRECTORY | FEXCL)) ==
1049 (FCREAT | FDIRECTORY | FEXCL)) {
1050 return (EINVAL);
1051 }
1052
1053 if ((filemode & (FCREAT | FDIRECTORY)) == (FCREAT | FDIRECTORY)) {
1054 create = B_FALSE;
1055 } else if ((filemode & FCREAT) != 0) {
1056 create = B_TRUE;
1057 } else {
1058 create = B_FALSE;
1059 }
1060
1061 top:
1062 if (create) {
1063 enum vcexcl excl;
1064
1065 /*
1066 * Wish to create a file.
1067 */
1068 vattr.va_type = VREG;
1069 vattr.va_mode = createmode;
1070 vattr.va_mask = AT_TYPE|AT_MODE;
1071 if (filemode & FTRUNC) {
1072 vattr.va_size = 0;
1073 vattr.va_mask |= AT_SIZE;
1074 }
1075 if (filemode & FEXCL)
1076 excl = EXCL;
1077 else
1078 excl = NONEXCL;
1079
1080 if (error =
1081 vn_createat(pnamep, seg, &vattr, excl, mode, &vp, crwhy,
1082 (filemode & ~(FTRUNC|FEXCL)), umask, startvp))
1083 return (error);
1084 } else {
1085 /*
1086 * Wish to open a file. Just look it up.
1087 */
1088 if (error = lookupnameat(pnamep, seg, follow,
1089 NULLVPP, &vp, startvp)) {
1090 if ((error == ESTALE) &&
1091 fs_need_estale_retry(estale_retry++))
1092 goto top;
1093 return (error);
1094 }
1095
1096 /*
1097 * Get the attributes to check whether file is large.
1098 * We do this only if the FOFFMAX flag is not set and
1099 * only for regular files.
1100 */
1101
1102 if (!(filemode & FOFFMAX) && (vp->v_type == VREG)) {
1103 vattr.va_mask = AT_SIZE;
1104 if ((error = VOP_GETATTR(vp, &vattr, 0,
1105 CRED(), NULL))) {
1106 goto out;
1107 }
1108 if (vattr.va_size > (u_offset_t)MAXOFF32_T) {
1109 /*
1110 * Large File API - regular open fails
1111 * if FOFFMAX flag is set in file mode
1112 */
1113 error = EOVERFLOW;
1114 goto out;
1115 }
1116 }
1117 /*
1118 * Can't write directories, active texts, or
1119 * read-only filesystems. Can't truncate files
1120 * on which mandatory locking is in effect.
1121 */
1122 if (filemode & (FWRITE|FTRUNC)) {
1123 /*
1124 * Allow writable directory if VDIROPEN flag is set.
1125 */
1126 if (vp->v_type == VDIR && !(vp->v_flag & VDIROPEN)) {
1127 error = EISDIR;
1128 goto out;
1129 }
1130 if (ISROFILE(vp)) {
1131 error = EROFS;
1132 goto out;
1133 }
1134 /*
1135 * Can't truncate files on which
1136 * sysv mandatory locking is in effect.
1137 */
1138 if (filemode & FTRUNC) {
1139 vnode_t *rvp;
1140
1141 if (VOP_REALVP(vp, &rvp, NULL) != 0)
1142 rvp = vp;
1143 if (rvp->v_filocks != NULL) {
1144 vattr.va_mask = AT_MODE;
1145 if ((error = VOP_GETATTR(vp,
1146 &vattr, 0, CRED(), NULL)) == 0 &&
1147 MANDLOCK(vp, vattr.va_mode))
1148 error = EAGAIN;
1149 }
1150 }
1151 if (error)
1152 goto out;
1153 }
1154 /*
1155 * Check permissions.
1156 */
1157 if (error = VOP_ACCESS(vp, mode, accessflags, CRED(), NULL))
1158 goto out;
1159
1160 /*
1161 * Require FSEARCH and FDIRECTORY to return a directory. Require
1162 * FEXEC to return a regular file.
1163 */
1164 if ((filemode & (FSEARCH|FDIRECTORY)) != 0 &&
1165 vp->v_type != VDIR) {
1166 error = ENOTDIR;
1167 goto out;
1168 }
1169 if ((filemode & FEXEC) && vp->v_type != VREG) {
1170 error = ENOEXEC; /* XXX: error code? */
1171 goto out;
1172 }
1173 }
1174
1175 /*
1176 * Do remaining checks for FNOFOLLOW and FNOLINKS.
1177 */
1178 if ((filemode & FNOFOLLOW) && vp->v_type == VLNK) {
1179 /*
1180 * The __FLXPATH flag is a private interface for use by the lx
1181 * brand in order to emulate open(O_NOFOLLOW|O_PATH) which,
1182 * when a symbolic link is encountered, returns a file
1183 * descriptor which references it.
1184 * See uts/common/brand/lx/syscall/lx_open.c
1185 *
1186 * When this flag is set, VOP_OPEN() is not called (for a
1187 * symlink, most filesystems will return ENOSYS anyway)
1188 * and the link's vnode is returned to be linked to the
1189 * file descriptor.
1190 */
1191 if ((filemode & __FLXPATH) == 0)
1192 error = ELOOP;
1193 goto out;
1194 }
1195 if (filemode & FNOLINKS) {
1196 vattr.va_mask = AT_NLINK;
1197 if ((error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))) {
1198 goto out;
1199 }
1200 if (vattr.va_nlink != 1) {
1201 error = EMLINK;
1202 goto out;
1203 }
1204 }
1205
1206 /*
1207 * Opening a socket corresponding to the AF_UNIX pathname
1208 * in the filesystem name space is not supported.
1209 * However, VSOCK nodes in namefs are supported in order
1210 * to make fattach work for sockets.
1211 *
1212 * XXX This uses VOP_REALVP to distinguish between
1213 * an unopened namefs node (where VOP_REALVP returns a
1214 * different VSOCK vnode) and a VSOCK created by vn_create
1215 * in some file system (where VOP_REALVP would never return
1216 * a different vnode).
1217 */
1218 if (vp->v_type == VSOCK) {
1219 struct vnode *nvp;
1220
1221 error = VOP_REALVP(vp, &nvp, NULL);
1222 if (error != 0 || nvp == NULL || nvp == vp ||
1223 nvp->v_type != VSOCK) {
1224 error = EOPNOTSUPP;
1225 goto out;
1226 }
1227 }
1228
1229 if ((vp->v_type == VREG) && nbl_need_check(vp)) {
1230 /* get share reservation */
1231 shr.s_access = 0;
1232 if (filemode & FWRITE)
1233 shr.s_access |= F_WRACC;
1234 if (filemode & FREAD)
1235 shr.s_access |= F_RDACC;
1236 shr.s_deny = 0;
1237 shr.s_sysid = 0;
1238 shr.s_pid = ttoproc(curthread)->p_pid;
1239 shr_own.sl_pid = shr.s_pid;
1240 shr_own.sl_id = fd;
1241 shr.s_own_len = sizeof (shr_own);
1242 shr.s_owner = (caddr_t)&shr_own;
1243 error = VOP_SHRLOCK(vp, F_SHARE_NBMAND, &shr, filemode, CRED(),
1244 NULL);
1245 if (error)
1246 goto out;
1247 shrlock_done = 1;
1248
1249 /* nbmand conflict check if truncating file */
1250 if ((filemode & FTRUNC) && !(filemode & FCREAT)) {
1251 nbl_start_crit(vp, RW_READER);
1252 in_crit = 1;
1253
1254 vattr.va_mask = AT_SIZE;
1255 if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
1256 goto out;
1257 if (nbl_conflict(vp, NBL_WRITE, 0, vattr.va_size, 0,
1258 NULL)) {
1259 error = EACCES;
1260 goto out;
1261 }
1262 }
1263 }
1264
1265 /*
1266 * Do opening protocol.
1267 */
1268 error = VOP_OPEN(&vp, filemode, CRED(), NULL);
1269 if (error)
1270 goto out;
1271 open_done = 1;
1272
1273 /*
1274 * Truncate if required.
1275 */
1276 if ((filemode & FTRUNC) && !(filemode & FCREAT)) {
1277 vattr.va_size = 0;
1278 vattr.va_mask = AT_SIZE;
1279 if ((error = VOP_SETATTR(vp, &vattr, 0, CRED(), NULL)) != 0)
1280 goto out;
1281 }
1282
1283 /*
1284 * Turn on directio, if requested.
1285 */
1286 if (filemode & FDIRECT) {
1287 if ((error = VOP_IOCTL(vp, _FIODIRECTIO, DIRECTIO_ON, 0,
1288 CRED(), NULL, NULL)) != 0) {
1289 /*
1290 * On Linux, O_DIRECT returns EINVAL when the file
1291 * system does not support directio, so we'll do the
1292 * same.
1293 */
1294 error = EINVAL;
1295 goto out;
1296 }
1297 }
1298 out:
1299 ASSERT(vp->v_count > 0);
1300
1301 if (in_crit) {
1302 nbl_end_crit(vp);
1303 in_crit = 0;
1304 }
1305 if (error) {
1306 if (open_done) {
1307 (void) VOP_CLOSE(vp, filemode, 1, (offset_t)0, CRED(),
1308 NULL);
1309 open_done = 0;
1310 shrlock_done = 0;
1311 }
1312 if (shrlock_done) {
1313 (void) VOP_SHRLOCK(vp, F_UNSHARE, &shr, 0, CRED(),
1314 NULL);
1315 shrlock_done = 0;
1316 }
1317
1318 /*
1319 * The following clause was added to handle a problem
1320 * with NFS consistency. It is possible that a lookup
1321 * of the file to be opened succeeded, but the file
1322 * itself doesn't actually exist on the server. This
1323 * is chiefly due to the DNLC containing an entry for
1324 * the file which has been removed on the server. In
1325 * this case, we just start over. If there was some
1326 * other cause for the ESTALE error, then the lookup
1327 * of the file will fail and the error will be returned
1328 * above instead of looping around from here.
1329 */
1330 VN_RELE(vp);
1331 if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1332 goto top;
1333 } else
1334 *vpp = vp;
1335 return (error);
1336 }
1337
1338 /*
1339 * The following two accessor functions are for the NFSv4 server. Since there
1340 * is no VOP_OPEN_UP/DOWNGRADE we need a way for the NFS server to keep the
1341 * vnode open counts correct when a client "upgrades" an open or does an
1342 * open_downgrade. In NFS, an upgrade or downgrade can not only change the
1343 * open mode (add or subtract read or write), but also change the share/deny
1344 * modes. However, share reservations are not integrated with OPEN, yet, so
1345 * we need to handle each separately. These functions are cleaner than having
1346 * the NFS server manipulate the counts directly, however, nobody else should
1347 * use these functions.
1348 */
1349 void
1350 vn_open_upgrade(
1351 vnode_t *vp,
1352 int filemode)
1353 {
1354 ASSERT(vp->v_type == VREG);
1355
1356 if (filemode & FREAD)
1357 atomic_inc_32(&vp->v_rdcnt);
1358 if (filemode & FWRITE)
1359 atomic_inc_32(&vp->v_wrcnt);
1360
1361 }
1362
1363 void
1364 vn_open_downgrade(
1365 vnode_t *vp,
1366 int filemode)
1367 {
1368 ASSERT(vp->v_type == VREG);
1369
1370 if (filemode & FREAD) {
1371 ASSERT(vp->v_rdcnt > 0);
1372 atomic_dec_32(&vp->v_rdcnt);
1373 }
1374 if (filemode & FWRITE) {
1375 ASSERT(vp->v_wrcnt > 0);
1376 atomic_dec_32(&vp->v_wrcnt);
1377 }
1378
1379 }
1380
1381 int
1382 vn_create(
1383 char *pnamep,
1384 enum uio_seg seg,
1385 struct vattr *vap,
1386 enum vcexcl excl,
1387 int mode,
1388 struct vnode **vpp,
1389 enum create why,
1390 int flag,
1391 mode_t umask)
1392 {
1393 return (vn_createat(pnamep, seg, vap, excl, mode, vpp, why, flag,
1394 umask, NULL));
1395 }
1396
1397 /*
1398 * Create a vnode (makenode).
1399 */
1400 int
1401 vn_createat(
1402 char *pnamep,
1403 enum uio_seg seg,
1404 struct vattr *vap,
1405 enum vcexcl excl,
1406 int mode,
1407 struct vnode **vpp,
1408 enum create why,
1409 int flag,
1410 mode_t umask,
1411 struct vnode *startvp)
1412 {
1413 struct vnode *dvp; /* ptr to parent dir vnode */
1414 struct vnode *vp = NULL;
1415 struct pathname pn;
1416 int error;
1417 int in_crit = 0;
1418 struct vattr vattr;
1419 enum symfollow follow;
1420 int estale_retry = 0;
1421 uint32_t auditing = AU_AUDITING();
1422
1423 ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
1424
1425 /* symlink interpretation */
1426 if ((flag & FNOFOLLOW) || excl == EXCL)
1427 follow = NO_FOLLOW;
1428 else
1429 follow = FOLLOW;
1430 flag &= ~(FNOFOLLOW|FNOLINKS);
1431
1432 top:
1433 /*
1434 * Lookup directory.
1435 * If new object is a file, call lower level to create it.
1436 * Note that it is up to the lower level to enforce exclusive
1437 * creation, if the file is already there.
1438 * This allows the lower level to do whatever
1439 * locking or protocol that is needed to prevent races.
1440 * If the new object is directory call lower level to make
1441 * the new directory, with "." and "..".
1442 */
1443 if (error = pn_get(pnamep, seg, &pn))
1444 return (error);
1445 if (auditing)
1446 audit_vncreate_start();
1447 dvp = NULL;
1448 *vpp = NULL;
1449 /*
1450 * lookup will find the parent directory for the vnode.
1451 * When it is done the pn holds the name of the entry
1452 * in the directory.
1453 * If this is a non-exclusive create we also find the node itself.
1454 */
1455 error = lookuppnat(&pn, NULL, follow, &dvp,
1456 (excl == EXCL) ? NULLVPP : vpp, startvp);
1457 if (error) {
1458 pn_free(&pn);
1459 if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1460 goto top;
1461 if (why == CRMKDIR && error == EINVAL)
1462 error = EEXIST; /* SVID */
1463 return (error);
1464 }
1465
1466 if (why != CRMKNOD)
1467 vap->va_mode &= ~VSVTX;
1468
1469 /*
1470 * If default ACLs are defined for the directory don't apply the
1471 * umask if umask is passed.
1472 */
1473
1474 if (umask) {
1475
1476 vsecattr_t vsec;
1477
1478 vsec.vsa_aclcnt = 0;
1479 vsec.vsa_aclentp = NULL;
1480 vsec.vsa_dfaclcnt = 0;
1481 vsec.vsa_dfaclentp = NULL;
1482 vsec.vsa_mask = VSA_DFACLCNT;
1483 error = VOP_GETSECATTR(dvp, &vsec, 0, CRED(), NULL);
1484 /*
1485 * If error is ENOSYS then treat it as no error
1486 * Don't want to force all file systems to support
1487 * aclent_t style of ACL's.
1488 */
1489 if (error == ENOSYS)
1490 error = 0;
1491 if (error) {
1492 if (*vpp != NULL)
1493 VN_RELE(*vpp);
1494 goto out;
1495 } else {
1496 /*
1497 * Apply the umask if no default ACLs.
1498 */
1499 if (vsec.vsa_dfaclcnt == 0)
1500 vap->va_mode &= ~umask;
1501
1502 /*
1503 * VOP_GETSECATTR() may have allocated memory for
1504 * ACLs we didn't request, so double-check and
1505 * free it if necessary.
1506 */
1507 if (vsec.vsa_aclcnt && vsec.vsa_aclentp != NULL)
1508 kmem_free((caddr_t)vsec.vsa_aclentp,
1509 vsec.vsa_aclcnt * sizeof (aclent_t));
1510 if (vsec.vsa_dfaclcnt && vsec.vsa_dfaclentp != NULL)
1511 kmem_free((caddr_t)vsec.vsa_dfaclentp,
1512 vsec.vsa_dfaclcnt * sizeof (aclent_t));
1513 }
1514 }
1515
1516 /*
1517 * In general we want to generate EROFS if the file system is
1518 * readonly. However, POSIX (IEEE Std. 1003.1) section 5.3.1
1519 * documents the open system call, and it says that O_CREAT has no
1520 * effect if the file already exists. Bug 1119649 states
1521 * that open(path, O_CREAT, ...) fails when attempting to open an
1522 * existing file on a read only file system. Thus, the first part
1523 * of the following if statement has 3 checks:
1524 * if the file exists &&
1525 * it is being open with write access &&
1526 * the file system is read only
1527 * then generate EROFS
1528 */
1529 if ((*vpp != NULL && (mode & VWRITE) && ISROFILE(*vpp)) ||
1530 (*vpp == NULL && dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
1531 if (*vpp)
1532 VN_RELE(*vpp);
1533 error = EROFS;
1534 } else if (excl == NONEXCL && *vpp != NULL) {
1535 vnode_t *rvp;
1536
1537 /*
1538 * File already exists. If a mandatory lock has been
1539 * applied, return error.
1540 */
1541 vp = *vpp;
1542 if (VOP_REALVP(vp, &rvp, NULL) != 0)
1543 rvp = vp;
1544 if ((vap->va_mask & AT_SIZE) && nbl_need_check(vp)) {
1545 nbl_start_crit(vp, RW_READER);
1546 in_crit = 1;
1547 }
1548 if (rvp->v_filocks != NULL || rvp->v_shrlocks != NULL) {
1549 vattr.va_mask = AT_MODE|AT_SIZE;
1550 if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL)) {
1551 goto out;
1552 }
1553 if (MANDLOCK(vp, vattr.va_mode)) {
1554 error = EAGAIN;
1555 goto out;
1556 }
1557 /*
1558 * File cannot be truncated if non-blocking mandatory
1559 * locks are currently on the file.
1560 */
1561 if ((vap->va_mask & AT_SIZE) && in_crit) {
1562 u_offset_t offset;
1563 ssize_t length;
1564
1565 offset = vap->va_size > vattr.va_size ?
1566 vattr.va_size : vap->va_size;
1567 length = vap->va_size > vattr.va_size ?
1568 vap->va_size - vattr.va_size :
1569 vattr.va_size - vap->va_size;
1570 if (nbl_conflict(vp, NBL_WRITE, offset,
1571 length, 0, NULL)) {
1572 error = EACCES;
1573 goto out;
1574 }
1575 }
1576 }
1577
1578 /*
1579 * If the file is the root of a VFS, we've crossed a
1580 * mount point and the "containing" directory that we
1581 * acquired above (dvp) is irrelevant because it's in
1582 * a different file system. We apply VOP_CREATE to the
1583 * target itself instead of to the containing directory
1584 * and supply a null path name to indicate (conventionally)
1585 * the node itself as the "component" of interest.
1586 *
1587 * The call to VOP_CREATE() is necessary to ensure
1588 * that the appropriate permission checks are made,
1589 * i.e. EISDIR, EACCES, etc. We already know that vpp
1590 * exists since we are in the else condition where this
1591 * was checked.
1592 */
1593 if (vp->v_flag & VROOT) {
1594 ASSERT(why != CRMKDIR);
1595 error = VOP_CREATE(vp, "", vap, excl, mode, vpp,
1596 CRED(), flag, NULL, NULL);
1597 /*
1598 * If the create succeeded, it will have created a
1599 * new reference on a new vnode (*vpp) in the child
1600 * file system, so we want to drop our reference on
1601 * the old (vp) upon exit.
1602 */
1603 goto out;
1604 }
1605
1606 /*
1607 * Large File API - non-large open (FOFFMAX flag not set)
1608 * of regular file fails if the file size exceeds MAXOFF32_T.
1609 */
1610 if (why != CRMKDIR &&
1611 !(flag & FOFFMAX) &&
1612 (vp->v_type == VREG)) {
1613 vattr.va_mask = AT_SIZE;
1614 if ((error = VOP_GETATTR(vp, &vattr, 0,
1615 CRED(), NULL))) {
1616 goto out;
1617 }
1618 if ((vattr.va_size > (u_offset_t)MAXOFF32_T)) {
1619 error = EOVERFLOW;
1620 goto out;
1621 }
1622 }
1623 }
1624
1625 if (error == 0) {
1626 /*
1627 * Call mkdir() if specified, otherwise create().
1628 */
1629 int must_be_dir = pn_fixslash(&pn); /* trailing '/'? */
1630
1631 if (why == CRMKDIR)
1632 /*
1633 * N.B., if vn_createat() ever requests
1634 * case-insensitive behavior then it will need
1635 * to be passed to VOP_MKDIR(). VOP_CREATE()
1636 * will already get it via "flag"
1637 */
1638 error = VOP_MKDIR(dvp, pn.pn_path, vap, vpp, CRED(),
1639 NULL, 0, NULL);
1640 else if (!must_be_dir)
1641 error = VOP_CREATE(dvp, pn.pn_path, vap,
1642 excl, mode, vpp, CRED(), flag, NULL, NULL);
1643 else
1644 error = ENOTDIR;
1645 }
1646
1647 out:
1648
1649 if (auditing)
1650 audit_vncreate_finish(*vpp, error);
1651 if (in_crit) {
1652 nbl_end_crit(vp);
1653 in_crit = 0;
1654 }
1655 if (vp != NULL) {
1656 VN_RELE(vp);
1657 vp = NULL;
1658 }
1659 pn_free(&pn);
1660 VN_RELE(dvp);
1661 /*
1662 * The following clause was added to handle a problem
1663 * with NFS consistency. It is possible that a lookup
1664 * of the file to be created succeeded, but the file
1665 * itself doesn't actually exist on the server. This
1666 * is chiefly due to the DNLC containing an entry for
1667 * the file which has been removed on the server. In
1668 * this case, we just start over. If there was some
1669 * other cause for the ESTALE error, then the lookup
1670 * of the file will fail and the error will be returned
1671 * above instead of looping around from here.
1672 */
1673 if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1674 goto top;
1675 return (error);
1676 }
1677
1678 int
1679 vn_link(char *from, char *to, enum uio_seg seg)
1680 {
1681 return (vn_linkat(NULL, from, NO_FOLLOW, NULL, to, seg));
1682 }
1683
1684 int
1685 vn_linkat(vnode_t *fstartvp, char *from, enum symfollow follow,
1686 vnode_t *tstartvp, char *to, enum uio_seg seg)
1687 {
1688 struct vnode *fvp; /* from vnode ptr */
1689 struct vnode *tdvp; /* to directory vnode ptr */
1690 struct pathname pn;
1691 int error;
1692 struct vattr vattr;
1693 dev_t fsid;
1694 int estale_retry = 0;
1695 uint32_t auditing = AU_AUDITING();
1696
1697 top:
1698 fvp = tdvp = NULL;
1699 if (error = pn_get(to, seg, &pn))
1700 return (error);
1701 if (auditing && fstartvp != NULL)
1702 audit_setfsat_path(1);
1703 if (error = lookupnameat(from, seg, follow, NULLVPP, &fvp, fstartvp))
1704 goto out;
1705 if (auditing && tstartvp != NULL)
1706 audit_setfsat_path(3);
1707 if (error = lookuppnat(&pn, NULL, NO_FOLLOW, &tdvp, NULLVPP, tstartvp))
1708 goto out;
1709 /*
1710 * Make sure both source vnode and target directory vnode are
1711 * in the same vfs and that it is writeable.
1712 */
1713 vattr.va_mask = AT_FSID;
1714 if (error = VOP_GETATTR(fvp, &vattr, 0, CRED(), NULL))
1715 goto out;
1716 fsid = vattr.va_fsid;
1717 vattr.va_mask = AT_FSID;
1718 if (error = VOP_GETATTR(tdvp, &vattr, 0, CRED(), NULL))
1719 goto out;
1720 if (fsid != vattr.va_fsid) {
1721 error = EXDEV;
1722 goto out;
1723 }
1724 if (tdvp->v_vfsp->vfs_flag & VFS_RDONLY) {
1725 error = EROFS;
1726 goto out;
1727 }
1728 /*
1729 * Do the link.
1730 */
1731 (void) pn_fixslash(&pn);
1732 error = VOP_LINK(tdvp, fvp, pn.pn_path, CRED(), NULL, 0);
1733 out:
1734 pn_free(&pn);
1735 if (fvp)
1736 VN_RELE(fvp);
1737 if (tdvp)
1738 VN_RELE(tdvp);
1739 if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1740 goto top;
1741 return (error);
1742 }
1743
1744 int
1745 vn_rename(char *from, char *to, enum uio_seg seg)
1746 {
1747 return (vn_renameat(NULL, from, NULL, to, seg));
1748 }
1749
1750 int
1751 vn_renameat(vnode_t *fdvp, char *fname, vnode_t *tdvp,
1752 char *tname, enum uio_seg seg)
1753 {
1754 int error;
1755 struct vattr vattr;
1756 struct pathname fpn; /* from pathname */
1757 struct pathname tpn; /* to pathname */
1758 dev_t fsid;
1759 int in_crit_src, in_crit_targ;
1760 vnode_t *fromvp, *fvp;
1761 vnode_t *tovp, *targvp;
1762 int estale_retry = 0;
1763 uint32_t auditing = AU_AUDITING();
1764
1765 top:
1766 fvp = fromvp = tovp = targvp = NULL;
1767 in_crit_src = in_crit_targ = 0;
1768 /*
1769 * Get to and from pathnames.
1770 */
1771 if (error = pn_get(fname, seg, &fpn))
1772 return (error);
1773 if (error = pn_get(tname, seg, &tpn)) {
1774 pn_free(&fpn);
1775 return (error);
1776 }
1777
1778 /*
1779 * First we need to resolve the correct directories
1780 * The passed in directories may only be a starting point,
1781 * but we need the real directories the file(s) live in.
1782 * For example the fname may be something like usr/lib/sparc
1783 * and we were passed in the / directory, but we need to
1784 * use the lib directory for the rename.
1785 */
1786
1787 if (auditing && fdvp != NULL)
1788 audit_setfsat_path(1);
1789 /*
1790 * Lookup to and from directories.
1791 */
1792 if (error = lookuppnat(&fpn, NULL, NO_FOLLOW, &fromvp, &fvp, fdvp)) {
1793 goto out;
1794 }
1795
1796 /*
1797 * Make sure there is an entry.
1798 */
1799 if (fvp == NULL) {
1800 error = ENOENT;
1801 goto out;
1802 }
1803
1804 if (auditing && tdvp != NULL)
1805 audit_setfsat_path(3);
1806 if (error = lookuppnat(&tpn, NULL, NO_FOLLOW, &tovp, &targvp, tdvp)) {
1807 goto out;
1808 }
1809
1810 /*
1811 * Make sure both the from vnode directory and the to directory
1812 * are in the same vfs and the to directory is writable.
1813 * We check fsid's, not vfs pointers, so loopback fs works.
1814 */
1815 if (fromvp != tovp) {
1816 vattr.va_mask = AT_FSID;
1817 if (error = VOP_GETATTR(fromvp, &vattr, 0, CRED(), NULL))
1818 goto out;
1819 fsid = vattr.va_fsid;
1820 vattr.va_mask = AT_FSID;
1821 if (error = VOP_GETATTR(tovp, &vattr, 0, CRED(), NULL))
1822 goto out;
1823 if (fsid != vattr.va_fsid) {
1824 error = EXDEV;
1825 goto out;
1826 }
1827 }
1828
1829 if (tovp->v_vfsp->vfs_flag & VFS_RDONLY) {
1830 error = EROFS;
1831 goto out;
1832 }
1833
1834 /*
1835 * Make sure "from" vp is not a mount point.
1836 * Note, lookup did traverse() already, so
1837 * we'll be looking at the mounted FS root.
1838 * (but allow files like mnttab)
1839 */
1840 if ((fvp->v_flag & VROOT) != 0 && fvp->v_type == VDIR) {
1841 error = EBUSY;
1842 goto out;
1843 }
1844
1845 if (targvp && (fvp != targvp)) {
1846 nbl_start_crit(targvp, RW_READER);
1847 in_crit_targ = 1;
1848 if (nbl_conflict(targvp, NBL_REMOVE, 0, 0, 0, NULL)) {
1849 error = EACCES;
1850 goto out;
1851 }
1852 }
1853
1854 if (nbl_need_check(fvp)) {
1855 nbl_start_crit(fvp, RW_READER);
1856 in_crit_src = 1;
1857 if (nbl_conflict(fvp, NBL_RENAME, 0, 0, 0, NULL)) {
1858 error = EACCES;
1859 goto out;
1860 }
1861 }
1862
1863 /*
1864 * Do the rename.
1865 */
1866 (void) pn_fixslash(&tpn);
1867 error = VOP_RENAME(fromvp, fpn.pn_path, tovp, tpn.pn_path, CRED(),
1868 NULL, 0);
1869
1870 out:
1871 pn_free(&fpn);
1872 pn_free(&tpn);
1873 if (in_crit_src)
1874 nbl_end_crit(fvp);
1875 if (in_crit_targ)
1876 nbl_end_crit(targvp);
1877 if (fromvp)
1878 VN_RELE(fromvp);
1879 if (tovp)
1880 VN_RELE(tovp);
1881 if (targvp)
1882 VN_RELE(targvp);
1883 if (fvp)
1884 VN_RELE(fvp);
1885 if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1886 goto top;
1887 return (error);
1888 }
1889
1890 /*
1891 * Remove a file or directory.
1892 */
1893 int
1894 vn_remove(char *fnamep, enum uio_seg seg, enum rm dirflag)
1895 {
1896 return (vn_removeat(NULL, fnamep, seg, dirflag));
1897 }
1898
1899 int
1900 vn_removeat(vnode_t *startvp, char *fnamep, enum uio_seg seg, enum rm dirflag)
1901 {
1902 struct vnode *vp; /* entry vnode */
1903 struct vnode *dvp; /* ptr to parent dir vnode */
1904 struct vnode *coveredvp;
1905 struct pathname pn; /* name of entry */
1906 enum vtype vtype;
1907 int error;
1908 struct vfs *vfsp;
1909 struct vfs *dvfsp; /* ptr to parent dir vfs */
1910 int in_crit = 0;
1911 int estale_retry = 0;
1912
1913 top:
1914 if (error = pn_get(fnamep, seg, &pn))
1915 return (error);
1916 dvp = vp = NULL;
1917 if (error = lookuppnat(&pn, NULL, NO_FOLLOW, &dvp, &vp, startvp)) {
1918 pn_free(&pn);
1919 if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1920 goto top;
1921 return (error);
1922 }
1923
1924 /*
1925 * Make sure there is an entry.
1926 */
1927 if (vp == NULL) {
1928 error = ENOENT;
1929 goto out;
1930 }
1931
1932 vfsp = vp->v_vfsp;
1933 dvfsp = dvp->v_vfsp;
1934
1935 /*
1936 * If the named file is the root of a mounted filesystem, fail,
1937 * unless it's marked unlinkable. In that case, unmount the
1938 * filesystem and proceed to unlink the covered vnode. (If the
1939 * covered vnode is a directory, use rmdir instead of unlink,
1940 * to avoid file system corruption.)
1941 */
1942 if (vp->v_flag & VROOT) {
1943 if ((vfsp->vfs_flag & VFS_UNLINKABLE) == 0) {
1944 error = EBUSY;
1945 goto out;
1946 }
1947
1948 /*
1949 * Namefs specific code starts here.
1950 */
1951
1952 if (dirflag == RMDIRECTORY) {
1953 /*
1954 * User called rmdir(2) on a file that has
1955 * been namefs mounted on top of. Since
1956 * namefs doesn't allow directories to
1957 * be mounted on other files we know
1958 * vp is not of type VDIR so fail to operation.
1959 */
1960 error = ENOTDIR;
1961 goto out;
1962 }
1963
1964 /*
1965 * If VROOT is still set after grabbing vp->v_lock,
1966 * noone has finished nm_unmount so far and coveredvp
1967 * is valid.
1968 * If we manage to grab vn_vfswlock(coveredvp) before releasing
1969 * vp->v_lock, any race window is eliminated.
1970 */
1971
1972 mutex_enter(&vp->v_lock);
1973 if ((vp->v_flag & VROOT) == 0) {
1974 /* Someone beat us to the unmount */
1975 mutex_exit(&vp->v_lock);
1976 error = EBUSY;
1977 goto out;
1978 }
1979 vfsp = vp->v_vfsp;
1980 coveredvp = vfsp->vfs_vnodecovered;
1981 ASSERT(coveredvp);
1982 /*
1983 * Note: Implementation of vn_vfswlock shows that ordering of
1984 * v_lock / vn_vfswlock is not an issue here.
1985 */
1986 error = vn_vfswlock(coveredvp);
1987 mutex_exit(&vp->v_lock);
1988
1989 if (error)
1990 goto out;
1991
1992 VN_HOLD(coveredvp);
1993 VN_RELE(vp);
1994 error = dounmount(vfsp, 0, CRED());
1995
1996 /*
1997 * Unmounted the namefs file system; now get
1998 * the object it was mounted over.
1999 */
2000 vp = coveredvp;
2001 /*
2002 * If namefs was mounted over a directory, then
2003 * we want to use rmdir() instead of unlink().
2004 */
2005 if (vp->v_type == VDIR)
2006 dirflag = RMDIRECTORY;
2007
2008 if (error)
2009 goto out;
2010 }
2011
2012 /*
2013 * Make sure filesystem is writeable.
2014 * We check the parent directory's vfs in case this is an lofs vnode.
2015 */
2016 if (dvfsp && dvfsp->vfs_flag & VFS_RDONLY) {
2017 error = EROFS;
2018 goto out;
2019 }
2020
2021 vtype = vp->v_type;
2022
2023 /*
2024 * If there is the possibility of an nbmand share reservation, make
2025 * sure it's okay to remove the file. Keep a reference to the
2026 * vnode, so that we can exit the nbl critical region after
2027 * calling VOP_REMOVE.
2028 * If there is no possibility of an nbmand share reservation,
2029 * release the vnode reference now. Filesystems like NFS may
2030 * behave differently if there is an extra reference, so get rid of
2031 * this one. Fortunately, we can't have nbmand mounts on NFS
2032 * filesystems.
2033 */
2034 if (nbl_need_check(vp)) {
2035 nbl_start_crit(vp, RW_READER);
2036 in_crit = 1;
2037 if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0, NULL)) {
2038 error = EACCES;
2039 goto out;
2040 }
2041 } else {
2042 VN_RELE(vp);
2043 vp = NULL;
2044 }
2045
2046 if (dirflag == RMDIRECTORY) {
2047 /*
2048 * Caller is using rmdir(2), which can only be applied to
2049 * directories.
2050 */
2051 if (vtype != VDIR) {
2052 error = ENOTDIR;
2053 } else {
2054 vnode_t *cwd;
2055 proc_t *pp = curproc;
2056
2057 mutex_enter(&pp->p_lock);
2058 cwd = PTOU(pp)->u_cdir;
2059 VN_HOLD(cwd);
2060 mutex_exit(&pp->p_lock);
2061 error = VOP_RMDIR(dvp, pn.pn_path, cwd, CRED(),
2062 NULL, 0);
2063 VN_RELE(cwd);
2064 }
2065 } else {
2066 /*
2067 * Unlink(2) can be applied to anything.
2068 */
2069 error = VOP_REMOVE(dvp, pn.pn_path, CRED(), NULL, 0);
2070 }
2071
2072 out:
2073 pn_free(&pn);
2074 if (in_crit) {
2075 nbl_end_crit(vp);
2076 in_crit = 0;
2077 }
2078 if (vp != NULL)
2079 VN_RELE(vp);
2080 if (dvp != NULL)
2081 VN_RELE(dvp);
2082 if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
2083 goto top;
2084 return (error);
2085 }
2086
2087 /*
2088 * Utility function to compare equality of vnodes.
2089 * Compare the underlying real vnodes, if there are underlying vnodes.
2090 * This is a more thorough comparison than the VN_CMP() macro provides.
2091 */
2092 int
2093 vn_compare(vnode_t *vp1, vnode_t *vp2)
2094 {
2095 vnode_t *realvp;
2096
2097 if (vp1 != NULL && VOP_REALVP(vp1, &realvp, NULL) == 0)
2098 vp1 = realvp;
2099 if (vp2 != NULL && VOP_REALVP(vp2, &realvp, NULL) == 0)
2100 vp2 = realvp;
2101 return (VN_CMP(vp1, vp2));
2102 }
2103
2104 /*
2105 * The number of locks to hash into. This value must be a power
2106 * of 2 minus 1 and should probably also be prime.
2107 */
2108 #define NUM_BUCKETS 1023
2109
2110 struct vn_vfslocks_bucket {
2111 kmutex_t vb_lock;
2112 vn_vfslocks_entry_t *vb_list;
2113 char pad[64 - sizeof (kmutex_t) - sizeof (void *)];
2114 };
2115
2116 /*
2117 * Total number of buckets will be NUM_BUCKETS + 1 .
2118 */
2119
2120 #pragma align 64(vn_vfslocks_buckets)
2121 static struct vn_vfslocks_bucket vn_vfslocks_buckets[NUM_BUCKETS + 1];
2122
2123 #define VN_VFSLOCKS_SHIFT 9
2124
2125 #define VN_VFSLOCKS_HASH(vfsvpptr) \
2126 ((((intptr_t)(vfsvpptr)) >> VN_VFSLOCKS_SHIFT) & NUM_BUCKETS)
2127
2128 /*
2129 * vn_vfslocks_getlock() uses an HASH scheme to generate
2130 * rwstlock using vfs/vnode pointer passed to it.
2131 *
2132 * vn_vfslocks_rele() releases a reference in the
2133 * HASH table which allows the entry allocated by
2134 * vn_vfslocks_getlock() to be freed at a later
2135 * stage when the refcount drops to zero.
2136 */
2137
2138 vn_vfslocks_entry_t *
2139 vn_vfslocks_getlock(void *vfsvpptr)
2140 {
2141 struct vn_vfslocks_bucket *bp;
2142 vn_vfslocks_entry_t *vep;
2143 vn_vfslocks_entry_t *tvep;
2144
2145 ASSERT(vfsvpptr != NULL);
2146 bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vfsvpptr)];
2147
2148 mutex_enter(&bp->vb_lock);
2149 for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
2150 if (vep->ve_vpvfs == vfsvpptr) {
2151 vep->ve_refcnt++;
2152 mutex_exit(&bp->vb_lock);
2153 return (vep);
2154 }
2155 }
2156 mutex_exit(&bp->vb_lock);
2157 vep = kmem_alloc(sizeof (*vep), KM_SLEEP);
2158 rwst_init(&vep->ve_lock, NULL, RW_DEFAULT, NULL);
2159 vep->ve_vpvfs = (char *)vfsvpptr;
2160 vep->ve_refcnt = 1;
2161 mutex_enter(&bp->vb_lock);
2162 for (tvep = bp->vb_list; tvep != NULL; tvep = tvep->ve_next) {
2163 if (tvep->ve_vpvfs == vfsvpptr) {
2164 tvep->ve_refcnt++;
2165 mutex_exit(&bp->vb_lock);
2166
2167 /*
2168 * There is already an entry in the hash
2169 * destroy what we just allocated.
2170 */
2171 rwst_destroy(&vep->ve_lock);
2172 kmem_free(vep, sizeof (*vep));
2173 return (tvep);
2174 }
2175 }
2176 vep->ve_next = bp->vb_list;
2177 bp->vb_list = vep;
2178 mutex_exit(&bp->vb_lock);
2179 return (vep);
2180 }
2181
2182 void
2183 vn_vfslocks_rele(vn_vfslocks_entry_t *vepent)
2184 {
2185 struct vn_vfslocks_bucket *bp;
2186 vn_vfslocks_entry_t *vep;
2187 vn_vfslocks_entry_t *pvep;
2188
2189 ASSERT(vepent != NULL);
2190 ASSERT(vepent->ve_vpvfs != NULL);
2191
2192 bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vepent->ve_vpvfs)];
2193
2194 mutex_enter(&bp->vb_lock);
2195 vepent->ve_refcnt--;
2196
2197 if ((int32_t)vepent->ve_refcnt < 0)
2198 cmn_err(CE_PANIC, "vn_vfslocks_rele: refcount negative");
2199
2200 pvep = NULL;
2201 if (vepent->ve_refcnt == 0) {
2202 for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
2203 if (vep->ve_vpvfs == vepent->ve_vpvfs) {
2204 if (pvep == NULL)
2205 bp->vb_list = vep->ve_next;
2206 else {
2207 pvep->ve_next = vep->ve_next;
2208 }
2209 mutex_exit(&bp->vb_lock);
2210 rwst_destroy(&vep->ve_lock);
2211 kmem_free(vep, sizeof (*vep));
2212 return;
2213 }
2214 pvep = vep;
2215 }
2216 cmn_err(CE_PANIC, "vn_vfslocks_rele: vp/vfs not found");
2217 }
2218 mutex_exit(&bp->vb_lock);
2219 }
2220
2221 /*
2222 * vn_vfswlock_wait is used to implement a lock which is logically a writers
2223 * lock protecting the v_vfsmountedhere field.
2224 * vn_vfswlock_wait has been modified to be similar to vn_vfswlock,
2225 * except that it blocks to acquire the lock VVFSLOCK.
2226 *
2227 * traverse() and routines re-implementing part of traverse (e.g. autofs)
2228 * need to hold this lock. mount(), vn_rename(), vn_remove() and so on
2229 * need the non-blocking version of the writers lock i.e. vn_vfswlock
2230 */
2231 int
2232 vn_vfswlock_wait(vnode_t *vp)
2233 {
2234 int retval;
2235 vn_vfslocks_entry_t *vpvfsentry;
2236 ASSERT(vp != NULL);
2237
2238 vpvfsentry = vn_vfslocks_getlock(vp);
2239 retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_WRITER);
2240
2241 if (retval == EINTR) {
2242 vn_vfslocks_rele(vpvfsentry);
2243 return (EINTR);
2244 }
2245 return (retval);
2246 }
2247
2248 int
2249 vn_vfsrlock_wait(vnode_t *vp)
2250 {
2251 int retval;
2252 vn_vfslocks_entry_t *vpvfsentry;
2253 ASSERT(vp != NULL);
2254
2255 vpvfsentry = vn_vfslocks_getlock(vp);
2256 retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_READER);
2257
2258 if (retval == EINTR) {
2259 vn_vfslocks_rele(vpvfsentry);
2260 return (EINTR);
2261 }
2262
2263 return (retval);
2264 }
2265
2266
2267 /*
2268 * vn_vfswlock is used to implement a lock which is logically a writers lock
2269 * protecting the v_vfsmountedhere field.
2270 */
2271 int
2272 vn_vfswlock(vnode_t *vp)
2273 {
2274 vn_vfslocks_entry_t *vpvfsentry;
2275
2276 /*
2277 * If vp is NULL then somebody is trying to lock the covered vnode
2278 * of /. (vfs_vnodecovered is NULL for /). This situation will
2279 * only happen when unmounting /. Since that operation will fail
2280 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
2281 */
2282 if (vp == NULL)
2283 return (EBUSY);
2284
2285 vpvfsentry = vn_vfslocks_getlock(vp);
2286
2287 if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
2288 return (0);
2289
2290 vn_vfslocks_rele(vpvfsentry);
2291 return (EBUSY);
2292 }
2293
2294 int
2295 vn_vfsrlock(vnode_t *vp)
2296 {
2297 vn_vfslocks_entry_t *vpvfsentry;
2298
2299 /*
2300 * If vp is NULL then somebody is trying to lock the covered vnode
2301 * of /. (vfs_vnodecovered is NULL for /). This situation will
2302 * only happen when unmounting /. Since that operation will fail
2303 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
2304 */
2305 if (vp == NULL)
2306 return (EBUSY);
2307
2308 vpvfsentry = vn_vfslocks_getlock(vp);
2309
2310 if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
2311 return (0);
2312
2313 vn_vfslocks_rele(vpvfsentry);
2314 return (EBUSY);
2315 }
2316
2317 void
2318 vn_vfsunlock(vnode_t *vp)
2319 {
2320 vn_vfslocks_entry_t *vpvfsentry;
2321
2322 /*
2323 * ve_refcnt needs to be decremented twice.
2324 * 1. To release refernce after a call to vn_vfslocks_getlock()
2325 * 2. To release the reference from the locking routines like
2326 * vn_vfsrlock/vn_vfswlock etc,.
2327 */
2328 vpvfsentry = vn_vfslocks_getlock(vp);
2329 vn_vfslocks_rele(vpvfsentry);
2330
2331 rwst_exit(&vpvfsentry->ve_lock);
2332 vn_vfslocks_rele(vpvfsentry);
2333 }
2334
2335 int
2336 vn_vfswlock_held(vnode_t *vp)
2337 {
2338 int held;
2339 vn_vfslocks_entry_t *vpvfsentry;
2340
2341 ASSERT(vp != NULL);
2342
2343 vpvfsentry = vn_vfslocks_getlock(vp);
2344 held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
2345
2346 vn_vfslocks_rele(vpvfsentry);
2347 return (held);
2348 }
2349
2350
2351 int
2352 vn_make_ops(
2353 const char *name, /* Name of file system */
2354 const fs_operation_def_t *templ, /* Operation specification */
2355 vnodeops_t **actual) /* Return the vnodeops */
2356 {
2357 int unused_ops;
2358 int error;
2359
2360 *actual = (vnodeops_t *)kmem_alloc(sizeof (vnodeops_t), KM_SLEEP);
2361
2362 (*actual)->vnop_name = name;
2363
2364 error = fs_build_vector(*actual, &unused_ops, vn_ops_table, templ);
2365 if (error) {
2366 kmem_free(*actual, sizeof (vnodeops_t));
2367 }
2368
2369 #if DEBUG
2370 if (unused_ops != 0)
2371 cmn_err(CE_WARN, "vn_make_ops: %s: %d operations supplied "
2372 "but not used", name, unused_ops);
2373 #endif
2374
2375 return (error);
2376 }
2377
2378 /*
2379 * Free the vnodeops created as a result of vn_make_ops()
2380 */
2381 void
2382 vn_freevnodeops(vnodeops_t *vnops)
2383 {
2384 kmem_free(vnops, sizeof (vnodeops_t));
2385 }
2386
2387 /*
2388 * Vnode cache.
2389 */
2390
2391 /* ARGSUSED */
2392 static int
2393 vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
2394 {
2395 struct vnode *vp;
2396
2397 vp = buf;
2398
2399 mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
2400 mutex_init(&vp->v_vsd_lock, NULL, MUTEX_DEFAULT, NULL);
2401 cv_init(&vp->v_cv, NULL, CV_DEFAULT, NULL);
2402 rw_init(&vp->v_nbllock, NULL, RW_DEFAULT, NULL);
2403 vp->v_femhead = NULL; /* Must be done before vn_reinit() */
2404 vp->v_path = vn_vpath_empty;
2405 vp->v_path_stamp = 0;
2406 vp->v_mpssdata = NULL;
2407 vp->v_vsd = NULL;
2408 vp->v_fopdata = NULL;
2409
2410 return (0);
2411 }
2412
2413 /* ARGSUSED */
2414 static void
2415 vn_cache_destructor(void *buf, void *cdrarg)
2416 {
2417 struct vnode *vp;
2418
2419 vp = buf;
2420
2421 rw_destroy(&vp->v_nbllock);
2422 cv_destroy(&vp->v_cv);
2423 mutex_destroy(&vp->v_vsd_lock);
2424 mutex_destroy(&vp->v_lock);
2425 }
2426
2427 void
2428 vn_create_cache(void)
2429 {
2430 /* LINTED */
2431 ASSERT((1 << VNODE_ALIGN_LOG2) ==
2432 P2ROUNDUP(sizeof (struct vnode), VNODE_ALIGN));
2433 vn_cache = kmem_cache_create("vn_cache", sizeof (struct vnode),
2434 VNODE_ALIGN, vn_cache_constructor, vn_cache_destructor, NULL, NULL,
2435 NULL, 0);
2436 }
2437
2438 void
2439 vn_destroy_cache(void)
2440 {
2441 kmem_cache_destroy(vn_cache);
2442 }
2443
2444 /*
2445 * Used by file systems when fs-specific nodes (e.g., ufs inodes) are
2446 * cached by the file system and vnodes remain associated.
2447 */
2448 void
2449 vn_recycle(vnode_t *vp)
2450 {
2451 ASSERT(vp->v_pages == NULL);
2452 VERIFY(vp->v_path != NULL);
2453
2454 /*
2455 * XXX - This really belongs in vn_reinit(), but we have some issues
2456 * with the counts. Best to have it here for clean initialization.
2457 */
2458 vp->v_rdcnt = 0;
2459 vp->v_wrcnt = 0;
2460 vp->v_mmap_read = 0;
2461 vp->v_mmap_write = 0;
2462
2463 /*
2464 * If FEM was in use, make sure everything gets cleaned up
2465 * NOTE: vp->v_femhead is initialized to NULL in the vnode
2466 * constructor.
2467 */
2468 if (vp->v_femhead) {
2469 /* XXX - There should be a free_femhead() that does all this */
2470 ASSERT(vp->v_femhead->femh_list == NULL);
2471 mutex_destroy(&vp->v_femhead->femh_lock);
2472 kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
2473 vp->v_femhead = NULL;
2474 }
2475 if (vp->v_path != vn_vpath_empty) {
2476 kmem_free(vp->v_path, strlen(vp->v_path) + 1);
2477 vp->v_path = vn_vpath_empty;
2478 }
2479 vp->v_path_stamp = 0;
2480
2481 if (vp->v_fopdata != NULL) {
2482 free_fopdata(vp);
2483 }
2484 vp->v_mpssdata = NULL;
2485 vsd_free(vp);
2486 }
2487
2488 /*
2489 * Used to reset the vnode fields including those that are directly accessible
2490 * as well as those which require an accessor function.
2491 *
2492 * Does not initialize:
2493 * synchronization objects: v_lock, v_vsd_lock, v_nbllock, v_cv
2494 * v_data (since FS-nodes and vnodes point to each other and should
2495 * be updated simultaneously)
2496 * v_op (in case someone needs to make a VOP call on this object)
2497 */
2498 void
2499 vn_reinit(vnode_t *vp)
2500 {
2501 vp->v_count = 1;
2502 vp->v_count_dnlc = 0;
2503 vp->v_phantom_count = 0;
2504 vp->v_vfsp = NULL;
2505 vp->v_stream = NULL;
2506 vp->v_vfsmountedhere = NULL;
2507 vp->v_flag = 0;
2508 vp->v_type = VNON;
2509 vp->v_rdev = NODEV;
2510
2511 vp->v_filocks = NULL;
2512 vp->v_shrlocks = NULL;
2513 vp->v_pages = NULL;
2514
2515 vp->v_locality = NULL;
2516 vp->v_xattrdir = NULL;
2517
2518 /*
2519 * In a few specific instances, vn_reinit() is used to initialize
2520 * locally defined vnode_t instances. Lacking the construction offered
2521 * by vn_alloc(), these vnodes require v_path initialization.
2522 */
2523 if (vp->v_path == NULL) {
2524 vp->v_path = vn_vpath_empty;
2525 }
2526
2527 /* Handles v_femhead, v_path, and the r/w/map counts */
2528 vn_recycle(vp);
2529 }
2530
2531 vnode_t *
2532 vn_alloc(int kmflag)
2533 {
2534 vnode_t *vp;
2535
2536 vp = kmem_cache_alloc(vn_cache, kmflag);
2537
2538 if (vp != NULL) {
2539 vp->v_femhead = NULL; /* Must be done before vn_reinit() */
2540 vp->v_fopdata = NULL;
2541 vn_reinit(vp);
2542 }
2543
2544 return (vp);
2545 }
2546
2547 void
2548 vn_free(vnode_t *vp)
2549 {
2550 ASSERT(vp->v_shrlocks == NULL);
2551 ASSERT(vp->v_filocks == NULL);
2552
2553 /*
2554 * Some file systems call vn_free() with v_count of zero,
2555 * some with v_count of 1. In any case, the value should
2556 * never be anything else.
2557 */
2558 ASSERT((vp->v_count == 0) || (vp->v_count == 1));
2559 ASSERT(vp->v_count_dnlc == 0);
2560 ASSERT0(vp->v_phantom_count);
2561 VERIFY(vp->v_path != NULL);
2562 if (vp->v_path != vn_vpath_empty) {
2563 kmem_free(vp->v_path, strlen(vp->v_path) + 1);
2564 vp->v_path = vn_vpath_empty;
2565 }
2566
2567 /* If FEM was in use, make sure everything gets cleaned up */
2568 if (vp->v_femhead) {
2569 /* XXX - There should be a free_femhead() that does all this */
2570 ASSERT(vp->v_femhead->femh_list == NULL);
2571 mutex_destroy(&vp->v_femhead->femh_lock);
2572 kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
2573 vp->v_femhead = NULL;
2574 }
2575
2576 if (vp->v_fopdata != NULL) {
2577 free_fopdata(vp);
2578 }
2579 vp->v_mpssdata = NULL;
2580 vsd_free(vp);
2581 kmem_cache_free(vn_cache, vp);
2582 }
2583
2584 /*
2585 * vnode status changes, should define better states than 1, 0.
2586 */
2587 void
2588 vn_reclaim(vnode_t *vp)
2589 {
2590 vfs_t *vfsp = vp->v_vfsp;
2591
2592 if (vfsp == NULL ||
2593 vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2594 return;
2595 }
2596 (void) VFS_VNSTATE(vfsp, vp, VNTRANS_RECLAIMED);
2597 }
2598
2599 void
2600 vn_idle(vnode_t *vp)
2601 {
2602 vfs_t *vfsp = vp->v_vfsp;
2603
2604 if (vfsp == NULL ||
2605 vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2606 return;
2607 }
2608 (void) VFS_VNSTATE(vfsp, vp, VNTRANS_IDLED);
2609 }
2610 void
2611 vn_exists(vnode_t *vp)
2612 {
2613 vfs_t *vfsp = vp->v_vfsp;
2614
2615 if (vfsp == NULL ||
2616 vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2617 return;
2618 }
2619 (void) VFS_VNSTATE(vfsp, vp, VNTRANS_EXISTS);
2620 }
2621
2622 void
2623 vn_invalid(vnode_t *vp)
2624 {
2625 vfs_t *vfsp = vp->v_vfsp;
2626
2627 if (vfsp == NULL ||
2628 vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
2629 return;
2630 }
2631 (void) VFS_VNSTATE(vfsp, vp, VNTRANS_DESTROYED);
2632 }
2633
2634 /* Vnode event notification */
2635
2636 int
2637 vnevent_support(vnode_t *vp, caller_context_t *ct)
2638 {
2639 if (vp == NULL)
2640 return (EINVAL);
2641
2642 return (VOP_VNEVENT(vp, VE_SUPPORT, NULL, NULL, ct));
2643 }
2644
2645 void
2646 vnevent_rename_src(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
2647 {
2648 if (vp == NULL || vp->v_femhead == NULL) {
2649 return;
2650 }
2651 (void) VOP_VNEVENT(dvp, VE_RENAME_SRC_DIR, vp, name, ct);
2652 (void) VOP_VNEVENT(vp, VE_RENAME_SRC, dvp, name, ct);
2653 }
2654
2655 void
2656 vnevent_rename_dest(vnode_t *vp, vnode_t *dvp, char *name,
2657 caller_context_t *ct)
2658 {
2659 if (vp == NULL || vp->v_femhead == NULL) {
2660 return;
2661 }
2662 (void) VOP_VNEVENT(vp, VE_RENAME_DEST, dvp, name, ct);
2663 }
2664
2665 void
2666 vnevent_rename_dest_dir(vnode_t *vp, vnode_t *nvp, char *name,
2667 caller_context_t *ct)
2668 {
2669 if (vp == NULL || vp->v_femhead == NULL) {
2670 return;
2671 }
2672 (void) VOP_VNEVENT(vp, VE_RENAME_DEST_DIR, nvp, name, ct);
2673 }
2674
2675 void
2676 vnevent_remove(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
2677 {
2678 if (vp == NULL || vp->v_femhead == NULL) {
2679 return;
2680 }
2681 (void) VOP_VNEVENT(vp, VE_REMOVE, dvp, name, ct);
2682 }
2683
2684 void
2685 vnevent_rmdir(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
2686 {
2687 if (vp == NULL || vp->v_femhead == NULL) {
2688 return;
2689 }
2690 (void) VOP_VNEVENT(vp, VE_RMDIR, dvp, name, ct);
2691 }
2692
2693 void
2694 vnevent_pre_rename_src(vnode_t *vp, vnode_t *dvp, char *name,
2695 caller_context_t *ct)
2696 {
2697 if (vp == NULL || vp->v_femhead == NULL) {
2698 return;
2699 }
2700 (void) VOP_VNEVENT(vp, VE_PRE_RENAME_SRC, dvp, name, ct);
2701 }
2702
2703 void
2704 vnevent_pre_rename_dest(vnode_t *vp, vnode_t *dvp, char *name,
2705 caller_context_t *ct)
2706 {
2707 if (vp == NULL || vp->v_femhead == NULL) {
2708 return;
2709 }
2710 (void) VOP_VNEVENT(vp, VE_PRE_RENAME_DEST, dvp, name, ct);
2711 }
2712
2713 void
2714 vnevent_pre_rename_dest_dir(vnode_t *vp, vnode_t *nvp, char *name,
2715 caller_context_t *ct)
2716 {
2717 if (vp == NULL || vp->v_femhead == NULL) {
2718 return;
2719 }
2720 (void) VOP_VNEVENT(vp, VE_PRE_RENAME_DEST_DIR, nvp, name, ct);
2721 }
2722
2723 void
2724 vnevent_create(vnode_t *vp, caller_context_t *ct)
2725 {
2726 if (vp == NULL || vp->v_femhead == NULL) {
2727 return;
2728 }
2729 (void) VOP_VNEVENT(vp, VE_CREATE, NULL, NULL, ct);
2730 }
2731
2732 void
2733 vnevent_link(vnode_t *vp, caller_context_t *ct)
2734 {
2735 if (vp == NULL || vp->v_femhead == NULL) {
2736 return;
2737 }
2738 (void) VOP_VNEVENT(vp, VE_LINK, NULL, NULL, ct);
2739 }
2740
2741 void
2742 vnevent_mountedover(vnode_t *vp, caller_context_t *ct)
2743 {
2744 if (vp == NULL || vp->v_femhead == NULL) {
2745 return;
2746 }
2747 (void) VOP_VNEVENT(vp, VE_MOUNTEDOVER, NULL, NULL, ct);
2748 }
2749
2750 void
2751 vnevent_truncate(vnode_t *vp, caller_context_t *ct)
2752 {
2753 if (vp == NULL || vp->v_femhead == NULL) {
2754 return;
2755 }
2756 (void) VOP_VNEVENT(vp, VE_TRUNCATE, NULL, NULL, ct);
2757 }
2758
2759 void
2760 vnevent_resize(vnode_t *vp, caller_context_t *ct)
2761 {
2762 if (vp == NULL || vp->v_femhead == NULL) {
2763 return;
2764 }
2765 (void) VOP_VNEVENT(vp, VE_RESIZE, NULL, NULL, ct);
2766 }
2767
2768 /*
2769 * Vnode accessors.
2770 */
2771
2772 int
2773 vn_is_readonly(vnode_t *vp)
2774 {
2775 return (vp->v_vfsp->vfs_flag & VFS_RDONLY);
2776 }
2777
2778 int
2779 vn_has_flocks(vnode_t *vp)
2780 {
2781 return (vp->v_filocks != NULL);
2782 }
2783
2784 int
2785 vn_has_mandatory_locks(vnode_t *vp, int mode)
2786 {
2787 return ((vp->v_filocks != NULL) && (MANDLOCK(vp, mode)));
2788 }
2789
2790 int
2791 vn_has_cached_data(vnode_t *vp)
2792 {
2793 return (vp->v_pages != NULL);
2794 }
2795
2796 /*
2797 * Return 0 if the vnode in question shouldn't be permitted into a zone via
2798 * zone_enter(2).
2799 */
2800 int
2801 vn_can_change_zones(vnode_t *vp)
2802 {
2803 struct vfssw *vswp;
2804 int allow = 1;
2805 vnode_t *rvp;
2806
2807 if (nfs_global_client_only != 0)
2808 return (1);
2809
2810 /*
2811 * We always want to look at the underlying vnode if there is one.
2812 */
2813 if (VOP_REALVP(vp, &rvp, NULL) != 0)
2814 rvp = vp;
2815 /*
2816 * Some pseudo filesystems (including doorfs) don't actually register
2817 * their vfsops_t, so the following may return NULL; we happily let
2818 * such vnodes switch zones.
2819 */
2820 vswp = vfs_getvfsswbyvfsops(vfs_getops(rvp->v_vfsp));
2821 if (vswp != NULL) {
2822 if (vswp->vsw_flag & VSW_NOTZONESAFE)
2823 allow = 0;
2824 vfs_unrefvfssw(vswp);
2825 }
2826 return (allow);
2827 }
2828
2829 /*
2830 * Return nonzero if the vnode is a mount point, zero if not.
2831 */
2832 int
2833 vn_ismntpt(vnode_t *vp)
2834 {
2835 return (vp->v_vfsmountedhere != NULL);
2836 }
2837
2838 /* Retrieve the vfs (if any) mounted on this vnode */
2839 vfs_t *
2840 vn_mountedvfs(vnode_t *vp)
2841 {
2842 return (vp->v_vfsmountedhere);
2843 }
2844
2845 /*
2846 * Return nonzero if the vnode is referenced by the dnlc, zero if not.
2847 */
2848 int
2849 vn_in_dnlc(vnode_t *vp)
2850 {
2851 return (vp->v_count_dnlc > 0);
2852 }
2853
2854 /*
2855 * vn_has_other_opens() checks whether a particular file is opened by more than
2856 * just the caller and whether the open is for read and/or write.
2857 * This routine is for calling after the caller has already called VOP_OPEN()
2858 * and the caller wishes to know if they are the only one with it open for
2859 * the mode(s) specified.
2860 *
2861 * Vnode counts are only kept on regular files (v_type=VREG).
2862 */
2863 int
2864 vn_has_other_opens(
2865 vnode_t *vp,
2866 v_mode_t mode)
2867 {
2868
2869 ASSERT(vp != NULL);
2870
2871 switch (mode) {
2872 case V_WRITE:
2873 if (vp->v_wrcnt > 1)
2874 return (V_TRUE);
2875 break;
2876 case V_RDORWR:
2877 if ((vp->v_rdcnt > 1) || (vp->v_wrcnt > 1))
2878 return (V_TRUE);
2879 break;
2880 case V_RDANDWR:
2881 if ((vp->v_rdcnt > 1) && (vp->v_wrcnt > 1))
2882 return (V_TRUE);
2883 break;
2884 case V_READ:
2885 if (vp->v_rdcnt > 1)
2886 return (V_TRUE);
2887 break;
2888 }
2889
2890 return (V_FALSE);
2891 }
2892
2893 /*
2894 * vn_is_opened() checks whether a particular file is opened and
2895 * whether the open is for read and/or write.
2896 *
2897 * Vnode counts are only kept on regular files (v_type=VREG).
2898 */
2899 int
2900 vn_is_opened(
2901 vnode_t *vp,
2902 v_mode_t mode)
2903 {
2904
2905 ASSERT(vp != NULL);
2906
2907 switch (mode) {
2908 case V_WRITE:
2909 if (vp->v_wrcnt)
2910 return (V_TRUE);
2911 break;
2912 case V_RDANDWR:
2913 if (vp->v_rdcnt && vp->v_wrcnt)
2914 return (V_TRUE);
2915 break;
2916 case V_RDORWR:
2917 if (vp->v_rdcnt || vp->v_wrcnt)
2918 return (V_TRUE);
2919 break;
2920 case V_READ:
2921 if (vp->v_rdcnt)
2922 return (V_TRUE);
2923 break;
2924 }
2925
2926 return (V_FALSE);
2927 }
2928
2929 /*
2930 * vn_is_mapped() checks whether a particular file is mapped and whether
2931 * the file is mapped read and/or write.
2932 */
2933 int
2934 vn_is_mapped(
2935 vnode_t *vp,
2936 v_mode_t mode)
2937 {
2938
2939 ASSERT(vp != NULL);
2940
2941 #if !defined(_LP64)
2942 switch (mode) {
2943 /*
2944 * The atomic_add_64_nv functions force atomicity in the
2945 * case of 32 bit architectures. Otherwise the 64 bit values
2946 * require two fetches. The value of the fields may be
2947 * (potentially) changed between the first fetch and the
2948 * second
2949 */
2950 case V_WRITE:
2951 if (atomic_add_64_nv((&(vp->v_mmap_write)), 0))
2952 return (V_TRUE);
2953 break;
2954 case V_RDANDWR:
2955 if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) &&
2956 (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
2957 return (V_TRUE);
2958 break;
2959 case V_RDORWR:
2960 if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) ||
2961 (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
2962 return (V_TRUE);
2963 break;
2964 case V_READ:
2965 if (atomic_add_64_nv((&(vp->v_mmap_read)), 0))
2966 return (V_TRUE);
2967 break;
2968 }
2969 #else
2970 switch (mode) {
2971 case V_WRITE:
2972 if (vp->v_mmap_write)
2973 return (V_TRUE);
2974 break;
2975 case V_RDANDWR:
2976 if (vp->v_mmap_read && vp->v_mmap_write)
2977 return (V_TRUE);
2978 break;
2979 case V_RDORWR:
2980 if (vp->v_mmap_read || vp->v_mmap_write)
2981 return (V_TRUE);
2982 break;
2983 case V_READ:
2984 if (vp->v_mmap_read)
2985 return (V_TRUE);
2986 break;
2987 }
2988 #endif
2989
2990 return (V_FALSE);
2991 }
2992
2993 /*
2994 * Set the operations vector for a vnode.
2995 *
2996 * FEM ensures that the v_femhead pointer is filled in before the
2997 * v_op pointer is changed. This means that if the v_femhead pointer
2998 * is NULL, and the v_op field hasn't changed since before which checked
2999 * the v_femhead pointer; then our update is ok - we are not racing with
3000 * FEM.
3001 */
3002 void
3003 vn_setops(vnode_t *vp, vnodeops_t *vnodeops)
3004 {
3005 vnodeops_t *op;
3006
3007 ASSERT(vp != NULL);
3008 ASSERT(vnodeops != NULL);
3009
3010 op = vp->v_op;
3011 membar_consumer();
3012 /*
3013 * If vp->v_femhead == NULL, then we'll call atomic_cas_ptr() to do
3014 * the compare-and-swap on vp->v_op. If either fails, then FEM is
3015 * in effect on the vnode and we need to have FEM deal with it.
3016 */
3017 if (vp->v_femhead != NULL || atomic_cas_ptr(&vp->v_op, op, vnodeops) !=
3018 op) {
3019 fem_setvnops(vp, vnodeops);
3020 }
3021 }
3022
3023 /*
3024 * Retrieve the operations vector for a vnode
3025 * As with vn_setops(above); make sure we aren't racing with FEM.
3026 * FEM sets the v_op to a special, internal, vnodeops that wouldn't
3027 * make sense to the callers of this routine.
3028 */
3029 vnodeops_t *
3030 vn_getops(vnode_t *vp)
3031 {
3032 vnodeops_t *op;
3033
3034 ASSERT(vp != NULL);
3035
3036 op = vp->v_op;
3037 membar_consumer();
3038 if (vp->v_femhead == NULL && op == vp->v_op) {
3039 return (op);
3040 } else {
3041 return (fem_getvnops(vp));
3042 }
3043 }
3044
3045 /*
3046 * Returns non-zero (1) if the vnodeops matches that of the vnode.
3047 * Returns zero (0) if not.
3048 */
3049 int
3050 vn_matchops(vnode_t *vp, vnodeops_t *vnodeops)
3051 {
3052 return (vn_getops(vp) == vnodeops);
3053 }
3054
3055 /*
3056 * Returns non-zero (1) if the specified operation matches the
3057 * corresponding operation for that the vnode.
3058 * Returns zero (0) if not.
3059 */
3060
3061 #define MATCHNAME(n1, n2) (((n1)[0] == (n2)[0]) && (strcmp((n1), (n2)) == 0))
3062
3063 int
3064 vn_matchopval(vnode_t *vp, char *vopname, fs_generic_func_p funcp)
3065 {
3066 const fs_operation_trans_def_t *otdp;
3067 fs_generic_func_p *loc = NULL;
3068 vnodeops_t *vop = vn_getops(vp);
3069
3070 ASSERT(vopname != NULL);
3071
3072 for (otdp = vn_ops_table; otdp->name != NULL; otdp++) {
3073 if (MATCHNAME(otdp->name, vopname)) {
3074 loc = (fs_generic_func_p *)
3075 ((char *)(vop) + otdp->offset);
3076 break;
3077 }
3078 }
3079
3080 return ((loc != NULL) && (*loc == funcp));
3081 }
3082
3083 /*
3084 * fs_new_caller_id() needs to return a unique ID on a given local system.
3085 * The IDs do not need to survive across reboots. These are primarily
3086 * used so that (FEM) monitors can detect particular callers (such as
3087 * the NFS server) to a given vnode/vfs operation.
3088 */
3089 u_longlong_t
3090 fs_new_caller_id()
3091 {
3092 static uint64_t next_caller_id = 0LL; /* First call returns 1 */
3093
3094 return ((u_longlong_t)atomic_inc_64_nv(&next_caller_id));
3095 }
3096
3097 /*
3098 * The value stored in v_path is relative to rootdir, located in the global
3099 * zone. Zones or chroot environments which reside deeper inside the VFS
3100 * hierarchy will have a relative view of MAXPATHLEN since they are unaware of
3101 * what lies below their perceived root. In order to keep v_path usable for
3102 * these child environments, its allocations are allowed to exceed MAXPATHLEN.
3103 *
3104 * An upper bound of max_vnode_path is placed upon v_path allocations to
3105 * prevent the system from going too wild at the behest of pathological
3106 * behavior from the operator.
3107 */
3108 size_t max_vnode_path = 4 * MAXPATHLEN;
3109
3110
3111 void
3112 vn_clearpath(vnode_t *vp, hrtime_t compare_stamp)
3113 {
3114 char *buf;
3115
3116 mutex_enter(&vp->v_lock);
3117 /*
3118 * If the snapshot of v_path_stamp passed in via compare_stamp does not
3119 * match the present value on the vnode, it indicates that subsequent
3120 * changes have occurred. The v_path value is not cleared in this case
3121 * since the new value may be valid.
3122 */
3123 if (compare_stamp != 0 && vp->v_path_stamp != compare_stamp) {
3124 mutex_exit(&vp->v_lock);
3125 return;
3126 }
3127 buf = vp->v_path;
3128 vp->v_path = vn_vpath_empty;
3129 vp->v_path_stamp = 0;
3130 mutex_exit(&vp->v_lock);
3131 if (buf != vn_vpath_empty) {
3132 kmem_free(buf, strlen(buf) + 1);
3133 }
3134 }
3135
3136 static void
3137 vn_setpath_common(vnode_t *pvp, vnode_t *vp, const char *name, size_t len,
3138 boolean_t is_rename)
3139 {
3140 char *buf, *oldbuf;
3141 hrtime_t pstamp;
3142 size_t baselen, buflen = 0;
3143
3144 /* Handle the vn_setpath_str case. */
3145 if (pvp == NULL) {
3146 if (len + 1 > max_vnode_path) {
3147 DTRACE_PROBE4(vn__setpath__too__long, vnode_t *, pvp,
3148 vnode_t *, vp, char *, name, size_t, len + 1);
3149 return;
3150 }
3151 buf = kmem_alloc(len + 1, KM_SLEEP);
3152 bcopy(name, buf, len);
3153 buf[len] = '\0';
3154
3155 mutex_enter(&vp->v_lock);
3156 oldbuf = vp->v_path;
3157 vp->v_path = buf;
3158 vp->v_path_stamp = gethrtime();
3159 mutex_exit(&vp->v_lock);
3160 if (oldbuf != vn_vpath_empty) {
3161 kmem_free(oldbuf, strlen(oldbuf) + 1);
3162 }
3163 return;
3164 }
3165
3166 /* Take snapshot of parent dir */
3167 mutex_enter(&pvp->v_lock);
3168
3169 if ((pvp->v_flag & VTRAVERSE) != 0) {
3170 /*
3171 * When the parent vnode has VTRAVERSE set in its flags, normal
3172 * assumptions about v_path calculation no longer apply. The
3173 * primary situation where this occurs is via the VFS tricks
3174 * which procfs plays in order to allow /proc/PID/(root|cwd) to
3175 * yield meaningful results.
3176 *
3177 * When this flag is set, v_path on the child must not be
3178 * updated since the calculated value is likely to be
3179 * incorrect, given the current context.
3180 */
3181 mutex_exit(&pvp->v_lock);
3182 return;
3183 }
3184
3185 retrybuf:
3186 if (pvp->v_path == vn_vpath_empty) {
3187 /*
3188 * Without v_path from the parent directory, generating a child
3189 * path from the name is impossible.
3190 */
3191 if (len > 0) {
3192 pstamp = pvp->v_path_stamp;
3193 mutex_exit(&pvp->v_lock);
3194 vn_clearpath(vp, pstamp);
3195 return;
3196 }
3197
3198 /*
3199 * The only feasible case here is where a NUL lookup is being
3200 * performed on rootdir prior to its v_path being populated.
3201 */
3202 ASSERT(pvp->v_path_stamp == 0);
3203 baselen = 0;
3204 pstamp = 0;
3205 } else {
3206 pstamp = pvp->v_path_stamp;
3207 baselen = strlen(pvp->v_path);
3208 /* ignore a trailing slash if present */
3209 if (pvp->v_path[baselen - 1] == '/') {
3210 /* This should only the be case for rootdir */
3211 ASSERT(baselen == 1 && pvp == rootdir);
3212 baselen--;
3213 }
3214 }
3215 mutex_exit(&pvp->v_lock);
3216
3217 if (buflen != 0) {
3218 /* Free the existing (mis-sized) buffer in case of retry */
3219 kmem_free(buf, buflen);
3220 }
3221 /* base, '/', name and trailing NUL */
3222 buflen = baselen + len + 2;
3223 if (buflen > max_vnode_path) {
3224 DTRACE_PROBE4(vn__setpath_too__long, vnode_t *, pvp,
3225 vnode_t *, vp, char *, name, size_t, buflen);
3226 return;
3227 }
3228 buf = kmem_alloc(buflen, KM_SLEEP);
3229
3230 mutex_enter(&pvp->v_lock);
3231 if (pvp->v_path_stamp != pstamp) {
3232 size_t vlen;
3233
3234 /*
3235 * Since v_path_stamp changed on the parent, it is likely that
3236 * v_path has been altered as well. If the length does not
3237 * exactly match what was previously measured, the buffer
3238 * allocation must be repeated for proper sizing.
3239 */
3240 if (pvp->v_path == vn_vpath_empty) {
3241 /* Give up if parent lack v_path */
3242 mutex_exit(&pvp->v_lock);
3243 kmem_free(buf, buflen);
3244 return;
3245 }
3246 vlen = strlen(pvp->v_path);
3247 if (pvp->v_path[vlen - 1] == '/') {
3248 vlen--;
3249 }
3250 if (vlen != baselen) {
3251 goto retrybuf;
3252 }
3253 }
3254 bcopy(pvp->v_path, buf, baselen);
3255 mutex_exit(&pvp->v_lock);
3256
3257 buf[baselen] = '/';
3258 baselen++;
3259 bcopy(name, &buf[baselen], len + 1);
3260
3261 mutex_enter(&vp->v_lock);
3262 if (vp->v_path_stamp == 0) {
3263 /* never-visited vnode can inherit stamp from parent */
3264 ASSERT(vp->v_path == vn_vpath_empty);
3265 vp->v_path_stamp = pstamp;
3266 vp->v_path = buf;
3267 mutex_exit(&vp->v_lock);
3268 } else if (vp->v_path_stamp < pstamp || is_rename) {
3269 /*
3270 * Install the updated path and stamp, ensuring that the v_path
3271 * pointer is valid at all times for dtrace.
3272 */
3273 oldbuf = vp->v_path;
3274 vp->v_path = buf;
3275 vp->v_path_stamp = gethrtime();
3276 mutex_exit(&vp->v_lock);
3277 kmem_free(oldbuf, strlen(oldbuf) + 1);
3278 } else {
3279 /*
3280 * If the timestamp matches or is greater, it means another
3281 * thread performed the update first while locks were dropped
3282 * here to make the allocation. We defer to the newer value.
3283 */
3284 mutex_exit(&vp->v_lock);
3285 kmem_free(buf, buflen);
3286 }
3287 ASSERT(MUTEX_NOT_HELD(&vp->v_lock));
3288 }
3289
3290 void
3291 vn_updatepath(vnode_t *pvp, vnode_t *vp, const char *name)
3292 {
3293 size_t len;
3294
3295 /*
3296 * If the parent is older or empty, there's nothing further to do.
3297 */
3298 if (pvp->v_path == vn_vpath_empty ||
3299 pvp->v_path_stamp <= vp->v_path_stamp) {
3300 return;
3301 }
3302
3303 /*
3304 * Given the lack of appropriate context, meaningful updates to v_path
3305 * cannot be made for during lookups for the '.' or '..' entries.
3306 */
3307 len = strlen(name);
3308 if (len == 0 || (len == 1 && name[0] == '.') ||
3309 (len == 2 && name[0] == '.' && name[1] == '.')) {
3310 return;
3311 }
3312
3313 vn_setpath_common(pvp, vp, name, len, B_FALSE);
3314 }
3315
3316 /*
3317 * Given a starting vnode and a path, updates the path in the target vnode in
3318 * a safe manner. If the vnode already has path information embedded, then the
3319 * cached path is left untouched.
3320 */
3321 /* ARGSUSED */
3322 void
3323 vn_setpath(vnode_t *rootvp, vnode_t *pvp, vnode_t *vp, const char *name,
3324 size_t len)
3325 {
3326 vn_setpath_common(pvp, vp, name, len, B_FALSE);
3327 }
3328
3329 /*
3330 * Sets the path to the vnode to be the given string, regardless of current
3331 * context. The string must be a complete path from rootdir. This is only used
3332 * by fsop_root() for setting the path based on the mountpoint.
3333 */
3334 void
3335 vn_setpath_str(vnode_t *vp, const char *str, size_t len)
3336 {
3337 vn_setpath_common(NULL, vp, str, len, B_FALSE);
3338 }
3339
3340 /*
3341 * Called from within filesystem's vop_rename() to handle renames once the
3342 * target vnode is available.
3343 */
3344 void
3345 vn_renamepath(vnode_t *pvp, vnode_t *vp, const char *name, size_t len)
3346 {
3347 vn_setpath_common(pvp, vp, name, len, B_TRUE);
3348 }
3349
3350 /*
3351 * Similar to vn_setpath_str(), this function sets the path of the destination
3352 * vnode to the be the same as the source vnode.
3353 */
3354 void
3355 vn_copypath(struct vnode *src, struct vnode *dst)
3356 {
3357 char *buf;
3358 hrtime_t stamp;
3359 size_t buflen;
3360
3361 mutex_enter(&src->v_lock);
3362 if (src->v_path == vn_vpath_empty) {
3363 mutex_exit(&src->v_lock);
3364 return;
3365 }
3366 buflen = strlen(src->v_path) + 1;
3367 mutex_exit(&src->v_lock);
3368
3369 buf = kmem_alloc(buflen, KM_SLEEP);
3370
3371 mutex_enter(&src->v_lock);
3372 if (src->v_path == vn_vpath_empty ||
3373 strlen(src->v_path) + 1 != buflen) {
3374 mutex_exit(&src->v_lock);
3375 kmem_free(buf, buflen);
3376 return;
3377 }
3378 bcopy(src->v_path, buf, buflen);
3379 stamp = src->v_path_stamp;
3380 mutex_exit(&src->v_lock);
3381
3382 mutex_enter(&dst->v_lock);
3383 if (dst->v_path != vn_vpath_empty) {
3384 mutex_exit(&dst->v_lock);
3385 kmem_free(buf, buflen);
3386 return;
3387 }
3388 dst->v_path = buf;
3389 dst->v_path_stamp = stamp;
3390 mutex_exit(&dst->v_lock);
3391 }
3392
3393
3394 /*
3395 * XXX Private interface for segvn routines that handle vnode
3396 * large page segments.
3397 *
3398 * return 1 if vp's file system VOP_PAGEIO() implementation
3399 * can be safely used instead of VOP_GETPAGE() for handling
3400 * pagefaults against regular non swap files. VOP_PAGEIO()
3401 * interface is considered safe here if its implementation
3402 * is very close to VOP_GETPAGE() implementation.
3403 * e.g. It zero's out the part of the page beyond EOF. Doesn't
3404 * panic if there're file holes but instead returns an error.
3405 * Doesn't assume file won't be changed by user writes, etc.
3406 *
3407 * return 0 otherwise.
3408 *
3409 * For now allow segvn to only use VOP_PAGEIO() with ufs and nfs.
3410 */
3411 int
3412 vn_vmpss_usepageio(vnode_t *vp)
3413 {
3414 vfs_t *vfsp = vp->v_vfsp;
3415 char *fsname = vfssw[vfsp->vfs_fstype].vsw_name;
3416 char *pageio_ok_fss[] = {"ufs", "nfs", NULL};
3417 char **fsok = pageio_ok_fss;
3418
3419 if (fsname == NULL) {
3420 return (0);
3421 }
3422
3423 for (; *fsok; fsok++) {
3424 if (strcmp(*fsok, fsname) == 0) {
3425 return (1);
3426 }
3427 }
3428 return (0);
3429 }
3430
3431 /* VOP_XXX() macros call the corresponding fop_xxx() function */
3432
3433 int
3434 fop_open(
3435 vnode_t **vpp,
3436 int mode,
3437 cred_t *cr,
3438 caller_context_t *ct)
3439 {
3440 int ret;
3441 vnode_t *vp = *vpp;
3442
3443 VN_HOLD(vp);
3444 /*
3445 * Adding to the vnode counts before calling open
3446 * avoids the need for a mutex. It circumvents a race
3447 * condition where a query made on the vnode counts results in a
3448 * false negative. The inquirer goes away believing the file is
3449 * not open when there is an open on the file already under way.
3450 *
3451 * The counts are meant to prevent NFS from granting a delegation
3452 * when it would be dangerous to do so.
3453 *
3454 * The vnode counts are only kept on regular files
3455 */
3456 if ((*vpp)->v_type == VREG) {
3457 if (mode & FREAD)
3458 atomic_inc_32(&(*vpp)->v_rdcnt);
3459 if (mode & FWRITE)
3460 atomic_inc_32(&(*vpp)->v_wrcnt);
3461 }
3462
3463 VOPXID_MAP_CR(vp, cr);
3464
3465 ret = (*(*(vpp))->v_op->vop_open)(vpp, mode, cr, ct);
3466
3467 if (ret) {
3468 /*
3469 * Use the saved vp just in case the vnode ptr got trashed
3470 * by the error.
3471 */
3472 VOPSTATS_UPDATE(vp, open);
3473 if ((vp->v_type == VREG) && (mode & FREAD))
3474 atomic_dec_32(&vp->v_rdcnt);
3475 if ((vp->v_type == VREG) && (mode & FWRITE))
3476 atomic_dec_32(&vp->v_wrcnt);
3477 } else {
3478 /*
3479 * Some filesystems will return a different vnode,
3480 * but the same path was still used to open it.
3481 * So if we do change the vnode and need to
3482 * copy over the path, do so here, rather than special
3483 * casing each filesystem. Adjust the vnode counts to
3484 * reflect the vnode switch.
3485 */
3486 VOPSTATS_UPDATE(*vpp, open);
3487 if (*vpp != vp) {
3488 vn_copypath(vp, *vpp);
3489 if (((*vpp)->v_type == VREG) && (mode & FREAD))
3490 atomic_inc_32(&(*vpp)->v_rdcnt);
3491 if ((vp->v_type == VREG) && (mode & FREAD))
3492 atomic_dec_32(&vp->v_rdcnt);
3493 if (((*vpp)->v_type == VREG) && (mode & FWRITE))
3494 atomic_inc_32(&(*vpp)->v_wrcnt);
3495 if ((vp->v_type == VREG) && (mode & FWRITE))
3496 atomic_dec_32(&vp->v_wrcnt);
3497 }
3498 }
3499 VN_RELE(vp);
3500 return (ret);
3501 }
3502
3503 int
3504 fop_close(
3505 vnode_t *vp,
3506 int flag,
3507 int count,
3508 offset_t offset,
3509 cred_t *cr,
3510 caller_context_t *ct)
3511 {
3512 int err;
3513
3514 VOPXID_MAP_CR(vp, cr);
3515
3516 err = (*(vp)->v_op->vop_close)(vp, flag, count, offset, cr, ct);
3517 VOPSTATS_UPDATE(vp, close);
3518 /*
3519 * Check passed in count to handle possible dups. Vnode counts are only
3520 * kept on regular files
3521 */
3522 if ((vp->v_type == VREG) && (count == 1)) {
3523 if (flag & FREAD) {
3524 ASSERT(vp->v_rdcnt > 0);
3525 atomic_dec_32(&vp->v_rdcnt);
3526 }
3527 if (flag & FWRITE) {
3528 ASSERT(vp->v_wrcnt > 0);
3529 atomic_dec_32(&vp->v_wrcnt);
3530 }
3531 }
3532 return (err);
3533 }
3534
3535 int
3536 fop_read(
3537 vnode_t *vp,
3538 uio_t *uiop,
3539 int ioflag,
3540 cred_t *cr,
3541 caller_context_t *ct)
3542 {
3543 ssize_t resid_start = uiop->uio_resid;
3544 zone_t *zonep = curzone;
3545 zone_vfs_kstat_t *zvp = zonep->zone_vfs_stats;
3546
3547 hrtime_t start = 0, lat;
3548 ssize_t len;
3549 int err;
3550
3551 if ((vp->v_type == VREG || vp->v_type == VDIR || vp->v_type == VBLK) &&
3552 vp->v_vfsp != NULL && (vp->v_vfsp->vfs_flag & VFS_STATS)) {
3553 start = gethrtime();
3554
3555 mutex_enter(&zonep->zone_vfs_lock);
3556 kstat_runq_enter(&zonep->zone_vfs_rwstats);
3557 mutex_exit(&zonep->zone_vfs_lock);
3558 }
3559
3560 VOPXID_MAP_CR(vp, cr);
3561
3562 err = (*(vp)->v_op->vop_read)(vp, uiop, ioflag, cr, ct);
3563 len = resid_start - uiop->uio_resid;
3564
3565 VOPSTATS_UPDATE_IO(vp, read, read_bytes, len);
3566
3567 if (start != 0) {
3568 mutex_enter(&zonep->zone_vfs_lock);
3569 zonep->zone_vfs_rwstats.reads++;
3570 zonep->zone_vfs_rwstats.nread += len;
3571 kstat_runq_exit(&zonep->zone_vfs_rwstats);
3572 mutex_exit(&zonep->zone_vfs_lock);
3573
3574 lat = gethrtime() - start;
3575
3576 if (lat >= VOP_LATENCY_10MS) {
3577 if (lat < VOP_LATENCY_100MS)
3578 atomic_inc_64(&zvp->zv_10ms_ops.value.ui64);
3579 else if (lat < VOP_LATENCY_1S) {
3580 atomic_inc_64(&zvp->zv_10ms_ops.value.ui64);
3581 atomic_inc_64(&zvp->zv_100ms_ops.value.ui64);
3582 } else if (lat < VOP_LATENCY_10S) {
3583 atomic_inc_64(&zvp->zv_10ms_ops.value.ui64);
3584 atomic_inc_64(&zvp->zv_100ms_ops.value.ui64);
3585 atomic_inc_64(&zvp->zv_1s_ops.value.ui64);
3586 } else {
3587 atomic_inc_64(&zvp->zv_10ms_ops.value.ui64);
3588 atomic_inc_64(&zvp->zv_100ms_ops.value.ui64);
3589 atomic_inc_64(&zvp->zv_1s_ops.value.ui64);
3590 atomic_inc_64(&zvp->zv_10s_ops.value.ui64);
3591 }
3592 }
3593 }
3594
3595 return (err);
3596 }
3597
3598 int
3599 fop_write(
3600 vnode_t *vp,
3601 uio_t *uiop,
3602 int ioflag,
3603 cred_t *cr,
3604 caller_context_t *ct)
3605 {
3606 ssize_t resid_start = uiop->uio_resid;
3607 zone_t *zonep = curzone;
3608 zone_vfs_kstat_t *zvp = zonep->zone_vfs_stats;
3609
3610 hrtime_t start = 0, lat;
3611 ssize_t len;
3612 int err;
3613
3614 /*
3615 * For the purposes of VFS kstat consumers, the "waitq" calculation is
3616 * repurposed as the active queue for VFS write operations. There's no
3617 * actual wait queue for VFS operations.
3618 */
3619 if ((vp->v_type == VREG || vp->v_type == VDIR || vp->v_type == VBLK) &&
3620 vp->v_vfsp != NULL && (vp->v_vfsp->vfs_flag & VFS_STATS)) {
3621 start = gethrtime();
3622
3623 mutex_enter(&zonep->zone_vfs_lock);
3624 kstat_waitq_enter(&zonep->zone_vfs_rwstats);
3625 mutex_exit(&zonep->zone_vfs_lock);
3626 }
3627
3628 VOPXID_MAP_CR(vp, cr);
3629
3630 err = (*(vp)->v_op->vop_write)(vp, uiop, ioflag, cr, ct);
3631 len = resid_start - uiop->uio_resid;
3632
3633 VOPSTATS_UPDATE_IO(vp, write, write_bytes, len);
3634
3635 if (start != 0) {
3636 mutex_enter(&zonep->zone_vfs_lock);
3637 zonep->zone_vfs_rwstats.writes++;
3638 zonep->zone_vfs_rwstats.nwritten += len;
3639 kstat_waitq_exit(&zonep->zone_vfs_rwstats);
3640 mutex_exit(&zonep->zone_vfs_lock);
3641
3642 lat = gethrtime() - start;
3643
3644 if (lat >= VOP_LATENCY_10MS) {
3645 if (lat < VOP_LATENCY_100MS)
3646 atomic_inc_64(&zvp->zv_10ms_ops.value.ui64);
3647 else if (lat < VOP_LATENCY_1S) {
3648 atomic_inc_64(&zvp->zv_10ms_ops.value.ui64);
3649 atomic_inc_64(&zvp->zv_100ms_ops.value.ui64);
3650 } else if (lat < VOP_LATENCY_10S) {
3651 atomic_inc_64(&zvp->zv_10ms_ops.value.ui64);
3652 atomic_inc_64(&zvp->zv_100ms_ops.value.ui64);
3653 atomic_inc_64(&zvp->zv_1s_ops.value.ui64);
3654 } else {
3655 atomic_inc_64(&zvp->zv_10ms_ops.value.ui64);
3656 atomic_inc_64(&zvp->zv_100ms_ops.value.ui64);
3657 atomic_inc_64(&zvp->zv_1s_ops.value.ui64);
3658 atomic_inc_64(&zvp->zv_10s_ops.value.ui64);
3659 }
3660 }
3661 }
3662
3663 return (err);
3664 }
3665
3666 int
3667 fop_ioctl(
3668 vnode_t *vp,
3669 int cmd,
3670 intptr_t arg,
3671 int flag,
3672 cred_t *cr,
3673 int *rvalp,
3674 caller_context_t *ct)
3675 {
3676 int err;
3677
3678 VOPXID_MAP_CR(vp, cr);
3679
3680 err = (*(vp)->v_op->vop_ioctl)(vp, cmd, arg, flag, cr, rvalp, ct);
3681 VOPSTATS_UPDATE(vp, ioctl);
3682 return (err);
3683 }
3684
3685 int
3686 fop_setfl(
3687 vnode_t *vp,
3688 int oflags,
3689 int nflags,
3690 cred_t *cr,
3691 caller_context_t *ct)
3692 {
3693 int err;
3694
3695 VOPXID_MAP_CR(vp, cr);
3696
3697 err = (*(vp)->v_op->vop_setfl)(vp, oflags, nflags, cr, ct);
3698 VOPSTATS_UPDATE(vp, setfl);
3699 return (err);
3700 }
3701
3702 int
3703 fop_getattr(
3704 vnode_t *vp,
3705 vattr_t *vap,
3706 int flags,
3707 cred_t *cr,
3708 caller_context_t *ct)
3709 {
3710 int err;
3711
3712 VOPXID_MAP_CR(vp, cr);
3713
3714 /*
3715 * If this file system doesn't understand the xvattr extensions
3716 * then turn off the xvattr bit.
3717 */
3718 if (vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) == 0) {
3719 vap->va_mask &= ~AT_XVATTR;
3720 }
3721
3722 /*
3723 * We're only allowed to skip the ACL check iff we used a 32 bit
3724 * ACE mask with VOP_ACCESS() to determine permissions.
3725 */
3726 if ((flags & ATTR_NOACLCHECK) &&
3727 vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3728 return (EINVAL);
3729 }
3730 err = (*(vp)->v_op->vop_getattr)(vp, vap, flags, cr, ct);
3731 VOPSTATS_UPDATE(vp, getattr);
3732 return (err);
3733 }
3734
3735 int
3736 fop_setattr(
3737 vnode_t *vp,
3738 vattr_t *vap,
3739 int flags,
3740 cred_t *cr,
3741 caller_context_t *ct)
3742 {
3743 int err;
3744
3745 VOPXID_MAP_CR(vp, cr);
3746
3747 /*
3748 * If this file system doesn't understand the xvattr extensions
3749 * then turn off the xvattr bit.
3750 */
3751 if (vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) == 0) {
3752 vap->va_mask &= ~AT_XVATTR;
3753 }
3754
3755 /*
3756 * We're only allowed to skip the ACL check iff we used a 32 bit
3757 * ACE mask with VOP_ACCESS() to determine permissions.
3758 */
3759 if ((flags & ATTR_NOACLCHECK) &&
3760 vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3761 return (EINVAL);
3762 }
3763 err = (*(vp)->v_op->vop_setattr)(vp, vap, flags, cr, ct);
3764 VOPSTATS_UPDATE(vp, setattr);
3765 return (err);
3766 }
3767
3768 int
3769 fop_access(
3770 vnode_t *vp,
3771 int mode,
3772 int flags,
3773 cred_t *cr,
3774 caller_context_t *ct)
3775 {
3776 int err;
3777
3778 if ((flags & V_ACE_MASK) &&
3779 vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
3780 return (EINVAL);
3781 }
3782
3783 VOPXID_MAP_CR(vp, cr);
3784
3785 err = (*(vp)->v_op->vop_access)(vp, mode, flags, cr, ct);
3786 VOPSTATS_UPDATE(vp, access);
3787 return (err);
3788 }
3789
3790 int
3791 fop_lookup(
3792 vnode_t *dvp,
3793 char *nm,
3794 vnode_t **vpp,
3795 pathname_t *pnp,
3796 int flags,
3797 vnode_t *rdir,
3798 cred_t *cr,
3799 caller_context_t *ct,
3800 int *deflags, /* Returned per-dirent flags */
3801 pathname_t *ppnp) /* Returned case-preserved name in directory */
3802 {
3803 int ret;
3804
3805 /*
3806 * If this file system doesn't support case-insensitive access
3807 * and said access is requested, fail quickly. It is required
3808 * that if the vfs supports case-insensitive lookup, it also
3809 * supports extended dirent flags.
3810 */
3811 if (flags & FIGNORECASE &&
3812 (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3813 vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3814 return (EINVAL);
3815
3816 VOPXID_MAP_CR(dvp, cr);
3817
3818 if ((flags & LOOKUP_XATTR) && (flags & LOOKUP_HAVE_SYSATTR_DIR) == 0) {
3819 ret = xattr_dir_lookup(dvp, vpp, flags, cr);
3820 } else {
3821 ret = (*(dvp)->v_op->vop_lookup)
3822 (dvp, nm, vpp, pnp, flags, rdir, cr, ct, deflags, ppnp);
3823 }
3824 if (ret == 0 && *vpp) {
3825 VOPSTATS_UPDATE(*vpp, lookup);
3826 vn_updatepath(dvp, *vpp, nm);
3827 }
3828
3829 return (ret);
3830 }
3831
3832 int
3833 fop_create(
3834 vnode_t *dvp,
3835 char *name,
3836 vattr_t *vap,
3837 vcexcl_t excl,
3838 int mode,
3839 vnode_t **vpp,
3840 cred_t *cr,
3841 int flags,
3842 caller_context_t *ct,
3843 vsecattr_t *vsecp) /* ACL to set during create */
3844 {
3845 int ret;
3846
3847 if (vsecp != NULL &&
3848 vfs_has_feature(dvp->v_vfsp, VFSFT_ACLONCREATE) == 0) {
3849 return (EINVAL);
3850 }
3851 /*
3852 * If this file system doesn't support case-insensitive access
3853 * and said access is requested, fail quickly.
3854 */
3855 if (flags & FIGNORECASE &&
3856 (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3857 vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3858 return (EINVAL);
3859
3860 VOPXID_MAP_CR(dvp, cr);
3861
3862 ret = (*(dvp)->v_op->vop_create)
3863 (dvp, name, vap, excl, mode, vpp, cr, flags, ct, vsecp);
3864 if (ret == 0 && *vpp) {
3865 VOPSTATS_UPDATE(*vpp, create);
3866 vn_updatepath(dvp, *vpp, name);
3867 }
3868
3869 return (ret);
3870 }
3871
3872 int
3873 fop_remove(
3874 vnode_t *dvp,
3875 char *nm,
3876 cred_t *cr,
3877 caller_context_t *ct,
3878 int flags)
3879 {
3880 int err;
3881
3882 /*
3883 * If this file system doesn't support case-insensitive access
3884 * and said access is requested, fail quickly.
3885 */
3886 if (flags & FIGNORECASE &&
3887 (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3888 vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3889 return (EINVAL);
3890
3891 VOPXID_MAP_CR(dvp, cr);
3892
3893 err = (*(dvp)->v_op->vop_remove)(dvp, nm, cr, ct, flags);
3894 VOPSTATS_UPDATE(dvp, remove);
3895 return (err);
3896 }
3897
3898 int
3899 fop_link(
3900 vnode_t *tdvp,
3901 vnode_t *svp,
3902 char *tnm,
3903 cred_t *cr,
3904 caller_context_t *ct,
3905 int flags)
3906 {
3907 int err;
3908
3909 /*
3910 * If the target file system doesn't support case-insensitive access
3911 * and said access is requested, fail quickly.
3912 */
3913 if (flags & FIGNORECASE &&
3914 (vfs_has_feature(tdvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3915 vfs_has_feature(tdvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3916 return (EINVAL);
3917
3918 VOPXID_MAP_CR(tdvp, cr);
3919
3920 err = (*(tdvp)->v_op->vop_link)(tdvp, svp, tnm, cr, ct, flags);
3921 VOPSTATS_UPDATE(tdvp, link);
3922 return (err);
3923 }
3924
3925 int
3926 fop_rename(
3927 vnode_t *sdvp,
3928 char *snm,
3929 vnode_t *tdvp,
3930 char *tnm,
3931 cred_t *cr,
3932 caller_context_t *ct,
3933 int flags)
3934 {
3935 int err;
3936
3937 /*
3938 * If the file system involved does not support
3939 * case-insensitive access and said access is requested, fail
3940 * quickly.
3941 */
3942 if (flags & FIGNORECASE &&
3943 ((vfs_has_feature(sdvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3944 vfs_has_feature(sdvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0)))
3945 return (EINVAL);
3946
3947 VOPXID_MAP_CR(tdvp, cr);
3948
3949 err = (*(sdvp)->v_op->vop_rename)(sdvp, snm, tdvp, tnm, cr, ct, flags);
3950 VOPSTATS_UPDATE(sdvp, rename);
3951 return (err);
3952 }
3953
3954 int
3955 fop_mkdir(
3956 vnode_t *dvp,
3957 char *dirname,
3958 vattr_t *vap,
3959 vnode_t **vpp,
3960 cred_t *cr,
3961 caller_context_t *ct,
3962 int flags,
3963 vsecattr_t *vsecp) /* ACL to set during create */
3964 {
3965 int ret;
3966
3967 if (vsecp != NULL &&
3968 vfs_has_feature(dvp->v_vfsp, VFSFT_ACLONCREATE) == 0) {
3969 return (EINVAL);
3970 }
3971 /*
3972 * If this file system doesn't support case-insensitive access
3973 * and said access is requested, fail quickly.
3974 */
3975 if (flags & FIGNORECASE &&
3976 (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
3977 vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
3978 return (EINVAL);
3979
3980 VOPXID_MAP_CR(dvp, cr);
3981
3982 ret = (*(dvp)->v_op->vop_mkdir)
3983 (dvp, dirname, vap, vpp, cr, ct, flags, vsecp);
3984 if (ret == 0 && *vpp) {
3985 VOPSTATS_UPDATE(*vpp, mkdir);
3986 vn_updatepath(dvp, *vpp, dirname);
3987 }
3988
3989 return (ret);
3990 }
3991
3992 int
3993 fop_rmdir(
3994 vnode_t *dvp,
3995 char *nm,
3996 vnode_t *cdir,
3997 cred_t *cr,
3998 caller_context_t *ct,
3999 int flags)
4000 {
4001 int err;
4002
4003 /*
4004 * If this file system doesn't support case-insensitive access
4005 * and said access is requested, fail quickly.
4006 */
4007 if (flags & FIGNORECASE &&
4008 (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
4009 vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
4010 return (EINVAL);
4011
4012 VOPXID_MAP_CR(dvp, cr);
4013
4014 err = (*(dvp)->v_op->vop_rmdir)(dvp, nm, cdir, cr, ct, flags);
4015 VOPSTATS_UPDATE(dvp, rmdir);
4016 return (err);
4017 }
4018
4019 int
4020 fop_readdir(
4021 vnode_t *vp,
4022 uio_t *uiop,
4023 cred_t *cr,
4024 int *eofp,
4025 caller_context_t *ct,
4026 int flags)
4027 {
4028 int err;
4029 ssize_t resid_start = uiop->uio_resid;
4030
4031 /*
4032 * If this file system doesn't support retrieving directory
4033 * entry flags and said access is requested, fail quickly.
4034 */
4035 if (flags & V_RDDIR_ENTFLAGS &&
4036 vfs_has_feature(vp->v_vfsp, VFSFT_DIRENTFLAGS) == 0)
4037 return (EINVAL);
4038
4039 VOPXID_MAP_CR(vp, cr);
4040
4041 err = (*(vp)->v_op->vop_readdir)(vp, uiop, cr, eofp, ct, flags);
4042 VOPSTATS_UPDATE_IO(vp, readdir,
4043 readdir_bytes, (resid_start - uiop->uio_resid));
4044 return (err);
4045 }
4046
4047 int
4048 fop_symlink(
4049 vnode_t *dvp,
4050 char *linkname,
4051 vattr_t *vap,
4052 char *target,
4053 cred_t *cr,
4054 caller_context_t *ct,
4055 int flags)
4056 {
4057 int err;
4058 xvattr_t xvattr;
4059
4060 /*
4061 * If this file system doesn't support case-insensitive access
4062 * and said access is requested, fail quickly.
4063 */
4064 if (flags & FIGNORECASE &&
4065 (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
4066 vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
4067 return (EINVAL);
4068
4069 VOPXID_MAP_CR(dvp, cr);
4070
4071 /* check for reparse point */
4072 if ((vfs_has_feature(dvp->v_vfsp, VFSFT_REPARSE)) &&
4073 (strncmp(target, FS_REPARSE_TAG_STR,
4074 strlen(FS_REPARSE_TAG_STR)) == 0)) {
4075 if (!fs_reparse_mark(target, vap, &xvattr))
4076 vap = (vattr_t *)&xvattr;
4077 }
4078
4079 err = (*(dvp)->v_op->vop_symlink)
4080 (dvp, linkname, vap, target, cr, ct, flags);
4081 VOPSTATS_UPDATE(dvp, symlink);
4082 return (err);
4083 }
4084
4085 int
4086 fop_readlink(
4087 vnode_t *vp,
4088 uio_t *uiop,
4089 cred_t *cr,
4090 caller_context_t *ct)
4091 {
4092 int err;
4093
4094 VOPXID_MAP_CR(vp, cr);
4095
4096 err = (*(vp)->v_op->vop_readlink)(vp, uiop, cr, ct);
4097 VOPSTATS_UPDATE(vp, readlink);
4098 return (err);
4099 }
4100
4101 int
4102 fop_fsync(
4103 vnode_t *vp,
4104 int syncflag,
4105 cred_t *cr,
4106 caller_context_t *ct)
4107 {
4108 int err;
4109
4110 VOPXID_MAP_CR(vp, cr);
4111
4112 err = (*(vp)->v_op->vop_fsync)(vp, syncflag, cr, ct);
4113 VOPSTATS_UPDATE(vp, fsync);
4114 return (err);
4115 }
4116
4117 void
4118 fop_inactive(
4119 vnode_t *vp,
4120 cred_t *cr,
4121 caller_context_t *ct)
4122 {
4123 /* Need to update stats before vop call since we may lose the vnode */
4124 VOPSTATS_UPDATE(vp, inactive);
4125
4126 VOPXID_MAP_CR(vp, cr);
4127
4128 (*(vp)->v_op->vop_inactive)(vp, cr, ct);
4129 }
4130
4131 int
4132 fop_fid(
4133 vnode_t *vp,
4134 fid_t *fidp,
4135 caller_context_t *ct)
4136 {
4137 int err;
4138
4139 err = (*(vp)->v_op->vop_fid)(vp, fidp, ct);
4140 VOPSTATS_UPDATE(vp, fid);
4141 return (err);
4142 }
4143
4144 int
4145 fop_rwlock(
4146 vnode_t *vp,
4147 int write_lock,
4148 caller_context_t *ct)
4149 {
4150 int ret;
4151
4152 ret = ((*(vp)->v_op->vop_rwlock)(vp, write_lock, ct));
4153 VOPSTATS_UPDATE(vp, rwlock);
4154 return (ret);
4155 }
4156
4157 void
4158 fop_rwunlock(
4159 vnode_t *vp,
4160 int write_lock,
4161 caller_context_t *ct)
4162 {
4163 (*(vp)->v_op->vop_rwunlock)(vp, write_lock, ct);
4164 VOPSTATS_UPDATE(vp, rwunlock);
4165 }
4166
4167 int
4168 fop_seek(
4169 vnode_t *vp,
4170 offset_t ooff,
4171 offset_t *noffp,
4172 caller_context_t *ct)
4173 {
4174 int err;
4175
4176 err = (*(vp)->v_op->vop_seek)(vp, ooff, noffp, ct);
4177 VOPSTATS_UPDATE(vp, seek);
4178 return (err);
4179 }
4180
4181 int
4182 fop_cmp(
4183 vnode_t *vp1,
4184 vnode_t *vp2,
4185 caller_context_t *ct)
4186 {
4187 int err;
4188
4189 err = (*(vp1)->v_op->vop_cmp)(vp1, vp2, ct);
4190 VOPSTATS_UPDATE(vp1, cmp);
4191 return (err);
4192 }
4193
4194 int
4195 fop_frlock(
4196 vnode_t *vp,
4197 int cmd,
4198 flock64_t *bfp,
4199 int flag,
4200 offset_t offset,
4201 struct flk_callback *flk_cbp,
4202 cred_t *cr,
4203 caller_context_t *ct)
4204 {
4205 int err;
4206
4207 VOPXID_MAP_CR(vp, cr);
4208
4209 err = (*(vp)->v_op->vop_frlock)
4210 (vp, cmd, bfp, flag, offset, flk_cbp, cr, ct);
4211 VOPSTATS_UPDATE(vp, frlock);
4212 return (err);
4213 }
4214
4215 int
4216 fop_space(
4217 vnode_t *vp,
4218 int cmd,
4219 flock64_t *bfp,
4220 int flag,
4221 offset_t offset,
4222 cred_t *cr,
4223 caller_context_t *ct)
4224 {
4225 int err;
4226
4227 VOPXID_MAP_CR(vp, cr);
4228
4229 err = (*(vp)->v_op->vop_space)(vp, cmd, bfp, flag, offset, cr, ct);
4230 VOPSTATS_UPDATE(vp, space);
4231 return (err);
4232 }
4233
4234 int
4235 fop_realvp(
4236 vnode_t *vp,
4237 vnode_t **vpp,
4238 caller_context_t *ct)
4239 {
4240 int err;
4241
4242 err = (*(vp)->v_op->vop_realvp)(vp, vpp, ct);
4243 VOPSTATS_UPDATE(vp, realvp);
4244 return (err);
4245 }
4246
4247 int
4248 fop_getpage(
4249 vnode_t *vp,
4250 offset_t off,
4251 size_t len,
4252 uint_t *protp,
4253 page_t **plarr,
4254 size_t plsz,
4255 struct seg *seg,
4256 caddr_t addr,
4257 enum seg_rw rw,
4258 cred_t *cr,
4259 caller_context_t *ct)
4260 {
4261 int err;
4262
4263 VOPXID_MAP_CR(vp, cr);
4264
4265 err = (*(vp)->v_op->vop_getpage)
4266 (vp, off, len, protp, plarr, plsz, seg, addr, rw, cr, ct);
4267 VOPSTATS_UPDATE(vp, getpage);
4268 return (err);
4269 }
4270
4271 int
4272 fop_putpage(
4273 vnode_t *vp,
4274 offset_t off,
4275 size_t len,
4276 int flags,
4277 cred_t *cr,
4278 caller_context_t *ct)
4279 {
4280 int err;
4281
4282 VOPXID_MAP_CR(vp, cr);
4283
4284 err = (*(vp)->v_op->vop_putpage)(vp, off, len, flags, cr, ct);
4285 VOPSTATS_UPDATE(vp, putpage);
4286 return (err);
4287 }
4288
4289 int
4290 fop_map(
4291 vnode_t *vp,
4292 offset_t off,
4293 struct as *as,
4294 caddr_t *addrp,
4295 size_t len,
4296 uchar_t prot,
4297 uchar_t maxprot,
4298 uint_t flags,
4299 cred_t *cr,
4300 caller_context_t *ct)
4301 {
4302 int err;
4303
4304 VOPXID_MAP_CR(vp, cr);
4305
4306 err = (*(vp)->v_op->vop_map)
4307 (vp, off, as, addrp, len, prot, maxprot, flags, cr, ct);
4308 VOPSTATS_UPDATE(vp, map);
4309 return (err);
4310 }
4311
4312 int
4313 fop_addmap(
4314 vnode_t *vp,
4315 offset_t off,
4316 struct as *as,
4317 caddr_t addr,
4318 size_t len,
4319 uchar_t prot,
4320 uchar_t maxprot,
4321 uint_t flags,
4322 cred_t *cr,
4323 caller_context_t *ct)
4324 {
4325 int error;
4326 u_longlong_t delta;
4327
4328 VOPXID_MAP_CR(vp, cr);
4329
4330 error = (*(vp)->v_op->vop_addmap)
4331 (vp, off, as, addr, len, prot, maxprot, flags, cr, ct);
4332
4333 if ((!error) && (vp->v_type == VREG)) {
4334 delta = (u_longlong_t)btopr(len);
4335 /*
4336 * If file is declared MAP_PRIVATE, it can't be written back
4337 * even if open for write. Handle as read.
4338 */
4339 if (flags & MAP_PRIVATE) {
4340 atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4341 (int64_t)delta);
4342 } else {
4343 /*
4344 * atomic_add_64 forces the fetch of a 64 bit value to
4345 * be atomic on 32 bit machines
4346 */
4347 if (maxprot & PROT_WRITE)
4348 atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
4349 (int64_t)delta);
4350 if (maxprot & PROT_READ)
4351 atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4352 (int64_t)delta);
4353 if (maxprot & PROT_EXEC)
4354 atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4355 (int64_t)delta);
4356 }
4357 }
4358 VOPSTATS_UPDATE(vp, addmap);
4359 return (error);
4360 }
4361
4362 int
4363 fop_delmap(
4364 vnode_t *vp,
4365 offset_t off,
4366 struct as *as,
4367 caddr_t addr,
4368 size_t len,
4369 uint_t prot,
4370 uint_t maxprot,
4371 uint_t flags,
4372 cred_t *cr,
4373 caller_context_t *ct)
4374 {
4375 int error;
4376 u_longlong_t delta;
4377
4378 VOPXID_MAP_CR(vp, cr);
4379
4380 error = (*(vp)->v_op->vop_delmap)
4381 (vp, off, as, addr, len, prot, maxprot, flags, cr, ct);
4382
4383 /*
4384 * NFS calls into delmap twice, the first time
4385 * it simply establishes a callback mechanism and returns EAGAIN
4386 * while the real work is being done upon the second invocation.
4387 * We have to detect this here and only decrement the counts upon
4388 * the second delmap request.
4389 */
4390 if ((error != EAGAIN) && (vp->v_type == VREG)) {
4391
4392 delta = (u_longlong_t)btopr(len);
4393
4394 if (flags & MAP_PRIVATE) {
4395 atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4396 (int64_t)(-delta));
4397 } else {
4398 /*
4399 * atomic_add_64 forces the fetch of a 64 bit value
4400 * to be atomic on 32 bit machines
4401 */
4402 if (maxprot & PROT_WRITE)
4403 atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
4404 (int64_t)(-delta));
4405 if (maxprot & PROT_READ)
4406 atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4407 (int64_t)(-delta));
4408 if (maxprot & PROT_EXEC)
4409 atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
4410 (int64_t)(-delta));
4411 }
4412 }
4413 VOPSTATS_UPDATE(vp, delmap);
4414 return (error);
4415 }
4416
4417
4418 int
4419 fop_poll(
4420 vnode_t *vp,
4421 short events,
4422 int anyyet,
4423 short *reventsp,
4424 struct pollhead **phpp,
4425 caller_context_t *ct)
4426 {
4427 int err;
4428
4429 err = (*(vp)->v_op->vop_poll)(vp, events, anyyet, reventsp, phpp, ct);
4430 VOPSTATS_UPDATE(vp, poll);
4431 return (err);
4432 }
4433
4434 int
4435 fop_dump(
4436 vnode_t *vp,
4437 caddr_t addr,
4438 offset_t lbdn,
4439 offset_t dblks,
4440 caller_context_t *ct)
4441 {
4442 int err;
4443
4444 /* ensure lbdn and dblks can be passed safely to bdev_dump */
4445 if ((lbdn != (daddr_t)lbdn) || (dblks != (int)dblks))
4446 return (EIO);
4447
4448 err = (*(vp)->v_op->vop_dump)(vp, addr, lbdn, dblks, ct);
4449 VOPSTATS_UPDATE(vp, dump);
4450 return (err);
4451 }
4452
4453 int
4454 fop_pathconf(
4455 vnode_t *vp,
4456 int cmd,
4457 ulong_t *valp,
4458 cred_t *cr,
4459 caller_context_t *ct)
4460 {
4461 int err;
4462
4463 VOPXID_MAP_CR(vp, cr);
4464
4465 err = (*(vp)->v_op->vop_pathconf)(vp, cmd, valp, cr, ct);
4466 VOPSTATS_UPDATE(vp, pathconf);
4467 return (err);
4468 }
4469
4470 int
4471 fop_pageio(
4472 vnode_t *vp,
4473 struct page *pp,
4474 u_offset_t io_off,
4475 size_t io_len,
4476 int flags,
4477 cred_t *cr,
4478 caller_context_t *ct)
4479 {
4480 int err;
4481
4482 VOPXID_MAP_CR(vp, cr);
4483
4484 err = (*(vp)->v_op->vop_pageio)(vp, pp, io_off, io_len, flags, cr, ct);
4485 VOPSTATS_UPDATE(vp, pageio);
4486 return (err);
4487 }
4488
4489 int
4490 fop_dumpctl(
4491 vnode_t *vp,
4492 int action,
4493 offset_t *blkp,
4494 caller_context_t *ct)
4495 {
4496 int err;
4497 err = (*(vp)->v_op->vop_dumpctl)(vp, action, blkp, ct);
4498 VOPSTATS_UPDATE(vp, dumpctl);
4499 return (err);
4500 }
4501
4502 void
4503 fop_dispose(
4504 vnode_t *vp,
4505 page_t *pp,
4506 int flag,
4507 int dn,
4508 cred_t *cr,
4509 caller_context_t *ct)
4510 {
4511 /* Must do stats first since it's possible to lose the vnode */
4512 VOPSTATS_UPDATE(vp, dispose);
4513
4514 VOPXID_MAP_CR(vp, cr);
4515
4516 (*(vp)->v_op->vop_dispose)(vp, pp, flag, dn, cr, ct);
4517 }
4518
4519 int
4520 fop_setsecattr(
4521 vnode_t *vp,
4522 vsecattr_t *vsap,
4523 int flag,
4524 cred_t *cr,
4525 caller_context_t *ct)
4526 {
4527 int err;
4528
4529 VOPXID_MAP_CR(vp, cr);
4530
4531 /*
4532 * We're only allowed to skip the ACL check iff we used a 32 bit
4533 * ACE mask with VOP_ACCESS() to determine permissions.
4534 */
4535 if ((flag & ATTR_NOACLCHECK) &&
4536 vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
4537 return (EINVAL);
4538 }
4539 err = (*(vp)->v_op->vop_setsecattr) (vp, vsap, flag, cr, ct);
4540 VOPSTATS_UPDATE(vp, setsecattr);
4541 return (err);
4542 }
4543
4544 int
4545 fop_getsecattr(
4546 vnode_t *vp,
4547 vsecattr_t *vsap,
4548 int flag,
4549 cred_t *cr,
4550 caller_context_t *ct)
4551 {
4552 int err;
4553
4554 /*
4555 * We're only allowed to skip the ACL check iff we used a 32 bit
4556 * ACE mask with VOP_ACCESS() to determine permissions.
4557 */
4558 if ((flag & ATTR_NOACLCHECK) &&
4559 vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
4560 return (EINVAL);
4561 }
4562
4563 VOPXID_MAP_CR(vp, cr);
4564
4565 err = (*(vp)->v_op->vop_getsecattr) (vp, vsap, flag, cr, ct);
4566 VOPSTATS_UPDATE(vp, getsecattr);
4567 return (err);
4568 }
4569
4570 int
4571 fop_shrlock(
4572 vnode_t *vp,
4573 int cmd,
4574 struct shrlock *shr,
4575 int flag,
4576 cred_t *cr,
4577 caller_context_t *ct)
4578 {
4579 int err;
4580
4581 VOPXID_MAP_CR(vp, cr);
4582
4583 err = (*(vp)->v_op->vop_shrlock)(vp, cmd, shr, flag, cr, ct);
4584 VOPSTATS_UPDATE(vp, shrlock);
4585 return (err);
4586 }
4587
4588 int
4589 fop_vnevent(vnode_t *vp, vnevent_t vnevent, vnode_t *dvp, char *fnm,
4590 caller_context_t *ct)
4591 {
4592 int err;
4593
4594 err = (*(vp)->v_op->vop_vnevent)(vp, vnevent, dvp, fnm, ct);
4595 VOPSTATS_UPDATE(vp, vnevent);
4596 return (err);
4597 }
4598
4599 int
4600 fop_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *uiop, cred_t *cr,
4601 caller_context_t *ct)
4602 {
4603 int err;
4604
4605 if (vfs_has_feature(vp->v_vfsp, VFSFT_ZEROCOPY_SUPPORTED) == 0)
4606 return (ENOTSUP);
4607 err = (*(vp)->v_op->vop_reqzcbuf)(vp, ioflag, uiop, cr, ct);
4608 VOPSTATS_UPDATE(vp, reqzcbuf);
4609 return (err);
4610 }
4611
4612 int
4613 fop_retzcbuf(vnode_t *vp, xuio_t *uiop, cred_t *cr, caller_context_t *ct)
4614 {
4615 int err;
4616
4617 if (vfs_has_feature(vp->v_vfsp, VFSFT_ZEROCOPY_SUPPORTED) == 0)
4618 return (ENOTSUP);
4619 err = (*(vp)->v_op->vop_retzcbuf)(vp, uiop, cr, ct);
4620 VOPSTATS_UPDATE(vp, retzcbuf);
4621 return (err);
4622 }
4623
4624 /*
4625 * Default destructor
4626 * Needed because NULL destructor means that the key is unused
4627 */
4628 /* ARGSUSED */
4629 void
4630 vsd_defaultdestructor(void *value)
4631 {}
4632
4633 /*
4634 * Create a key (index into per vnode array)
4635 * Locks out vsd_create, vsd_destroy, and vsd_free
4636 * May allocate memory with lock held
4637 */
4638 void
4639 vsd_create(uint_t *keyp, void (*destructor)(void *))
4640 {
4641 int i;
4642 uint_t nkeys;
4643
4644 /*
4645 * if key is allocated, do nothing
4646 */
4647 mutex_enter(&vsd_lock);
4648 if (*keyp) {
4649 mutex_exit(&vsd_lock);
4650 return;
4651 }
4652 /*
4653 * find an unused key
4654 */
4655 if (destructor == NULL)
4656 destructor = vsd_defaultdestructor;
4657
4658 for (i = 0; i < vsd_nkeys; ++i)
4659 if (vsd_destructor[i] == NULL)
4660 break;
4661
4662 /*
4663 * if no unused keys, increase the size of the destructor array
4664 */
4665 if (i == vsd_nkeys) {
4666 if ((nkeys = (vsd_nkeys << 1)) == 0)
4667 nkeys = 1;
4668 vsd_destructor =
4669 (void (**)(void *))vsd_realloc((void *)vsd_destructor,
4670 (size_t)(vsd_nkeys * sizeof (void (*)(void *))),
4671 (size_t)(nkeys * sizeof (void (*)(void *))));
4672 vsd_nkeys = nkeys;
4673 }
4674
4675 /*
4676 * allocate the next available unused key
4677 */
4678 vsd_destructor[i] = destructor;
4679 *keyp = i + 1;
4680
4681 /* create vsd_list, if it doesn't exist */
4682 if (vsd_list == NULL) {
4683 vsd_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
4684 list_create(vsd_list, sizeof (struct vsd_node),
4685 offsetof(struct vsd_node, vs_nodes));
4686 }
4687
4688 mutex_exit(&vsd_lock);
4689 }
4690
4691 /*
4692 * Destroy a key
4693 *
4694 * Assumes that the caller is preventing vsd_set and vsd_get
4695 * Locks out vsd_create, vsd_destroy, and vsd_free
4696 * May free memory with lock held
4697 */
4698 void
4699 vsd_destroy(uint_t *keyp)
4700 {
4701 uint_t key;
4702 struct vsd_node *vsd;
4703
4704 /*
4705 * protect the key namespace and our destructor lists
4706 */
4707 mutex_enter(&vsd_lock);
4708 key = *keyp;
4709 *keyp = 0;
4710
4711 ASSERT(key <= vsd_nkeys);
4712
4713 /*
4714 * if the key is valid
4715 */
4716 if (key != 0) {
4717 uint_t k = key - 1;
4718 /*
4719 * for every vnode with VSD, call key's destructor
4720 */
4721 for (vsd = list_head(vsd_list); vsd != NULL;
4722 vsd = list_next(vsd_list, vsd)) {
4723 /*
4724 * no VSD for key in this vnode
4725 */
4726 if (key > vsd->vs_nkeys)
4727 continue;
4728 /*
4729 * call destructor for key
4730 */
4731 if (vsd->vs_value[k] && vsd_destructor[k])
4732 (*vsd_destructor[k])(vsd->vs_value[k]);
4733 /*
4734 * reset value for key
4735 */
4736 vsd->vs_value[k] = NULL;
4737 }
4738 /*
4739 * actually free the key (NULL destructor == unused)
4740 */
4741 vsd_destructor[k] = NULL;
4742 }
4743
4744 mutex_exit(&vsd_lock);
4745 }
4746
4747 /*
4748 * Quickly return the per vnode value that was stored with the specified key
4749 * Assumes the caller is protecting key from vsd_create and vsd_destroy
4750 * Assumes the caller is holding v_vsd_lock to protect the vsd.
4751 */
4752 void *
4753 vsd_get(vnode_t *vp, uint_t key)
4754 {
4755 struct vsd_node *vsd;
4756
4757 ASSERT(vp != NULL);
4758 ASSERT(mutex_owned(&vp->v_vsd_lock));
4759
4760 vsd = vp->v_vsd;
4761
4762 if (key && vsd != NULL && key <= vsd->vs_nkeys)
4763 return (vsd->vs_value[key - 1]);
4764 return (NULL);
4765 }
4766
4767 /*
4768 * Set a per vnode value indexed with the specified key
4769 * Assumes the caller is holding v_vsd_lock to protect the vsd.
4770 */
4771 int
4772 vsd_set(vnode_t *vp, uint_t key, void *value)
4773 {
4774 struct vsd_node *vsd;
4775
4776 ASSERT(vp != NULL);
4777 ASSERT(mutex_owned(&vp->v_vsd_lock));
4778
4779 if (key == 0)
4780 return (EINVAL);
4781
4782 vsd = vp->v_vsd;
4783 if (vsd == NULL)
4784 vsd = vp->v_vsd = kmem_zalloc(sizeof (*vsd), KM_SLEEP);
4785
4786 /*
4787 * If the vsd was just allocated, vs_nkeys will be 0, so the following
4788 * code won't happen and we will continue down and allocate space for
4789 * the vs_value array.
4790 * If the caller is replacing one value with another, then it is up
4791 * to the caller to free/rele/destroy the previous value (if needed).
4792 */
4793 if (key <= vsd->vs_nkeys) {
4794 vsd->vs_value[key - 1] = value;
4795 return (0);
4796 }
4797
4798 ASSERT(key <= vsd_nkeys);
4799
4800 if (vsd->vs_nkeys == 0) {
4801 mutex_enter(&vsd_lock); /* lock out vsd_destroy() */
4802 /*
4803 * Link onto list of all VSD nodes.
4804 */
4805 list_insert_head(vsd_list, vsd);
4806 mutex_exit(&vsd_lock);
4807 }
4808
4809 /*
4810 * Allocate vnode local storage and set the value for key
4811 */
4812 vsd->vs_value = vsd_realloc(vsd->vs_value,
4813 vsd->vs_nkeys * sizeof (void *),
4814 key * sizeof (void *));
4815 vsd->vs_nkeys = key;
4816 vsd->vs_value[key - 1] = value;
4817
4818 return (0);
4819 }
4820
4821 /*
4822 * Called from vn_free() to run the destructor function for each vsd
4823 * Locks out vsd_create and vsd_destroy
4824 * Assumes that the destructor *DOES NOT* use vsd
4825 */
4826 void
4827 vsd_free(vnode_t *vp)
4828 {
4829 int i;
4830 struct vsd_node *vsd = vp->v_vsd;
4831
4832 if (vsd == NULL)
4833 return;
4834
4835 if (vsd->vs_nkeys == 0) {
4836 kmem_free(vsd, sizeof (*vsd));
4837 vp->v_vsd = NULL;
4838 return;
4839 }
4840
4841 /*
4842 * lock out vsd_create and vsd_destroy, call
4843 * the destructor, and mark the value as destroyed.
4844 */
4845 mutex_enter(&vsd_lock);
4846
4847 for (i = 0; i < vsd->vs_nkeys; i++) {
4848 if (vsd->vs_value[i] && vsd_destructor[i])
4849 (*vsd_destructor[i])(vsd->vs_value[i]);
4850 vsd->vs_value[i] = NULL;
4851 }
4852
4853 /*
4854 * remove from linked list of VSD nodes
4855 */
4856 list_remove(vsd_list, vsd);
4857
4858 mutex_exit(&vsd_lock);
4859
4860 /*
4861 * free up the VSD
4862 */
4863 kmem_free(vsd->vs_value, vsd->vs_nkeys * sizeof (void *));
4864 kmem_free(vsd, sizeof (struct vsd_node));
4865 vp->v_vsd = NULL;
4866 }
4867
4868 /*
4869 * realloc
4870 */
4871 static void *
4872 vsd_realloc(void *old, size_t osize, size_t nsize)
4873 {
4874 void *new;
4875
4876 new = kmem_zalloc(nsize, KM_SLEEP);
4877 if (old) {
4878 bcopy(old, new, osize);
4879 kmem_free(old, osize);
4880 }
4881 return (new);
4882 }
4883
4884 /*
4885 * Setup the extensible system attribute for creating a reparse point.
4886 * The symlink data 'target' is validated for proper format of a reparse
4887 * string and a check also made to make sure the symlink data does not
4888 * point to an existing file.
4889 *
4890 * return 0 if ok else -1.
4891 */
4892 static int
4893 fs_reparse_mark(char *target, vattr_t *vap, xvattr_t *xvattr)
4894 {
4895 xoptattr_t *xoap;
4896
4897 if ((!target) || (!vap) || (!xvattr))
4898 return (-1);
4899
4900 /* validate reparse string */
4901 if (reparse_validate((const char *)target))
4902 return (-1);
4903
4904 xva_init(xvattr);
4905 xvattr->xva_vattr = *vap;
4906 xvattr->xva_vattr.va_mask |= AT_XVATTR;
4907 xoap = xva_getxoptattr(xvattr);
4908 ASSERT(xoap);
4909 XVA_SET_REQ(xvattr, XAT_REPARSE);
4910 xoap->xoa_reparse = 1;
4911
4912 return (0);
4913 }
4914
4915 /*
4916 * Function to check whether a symlink is a reparse point.
4917 * Return B_TRUE if it is a reparse point, else return B_FALSE
4918 */
4919 boolean_t
4920 vn_is_reparse(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4921 {
4922 xvattr_t xvattr;
4923 xoptattr_t *xoap;
4924
4925 if ((vp->v_type != VLNK) ||
4926 !(vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR)))
4927 return (B_FALSE);
4928
4929 xva_init(&xvattr);
4930 xoap = xva_getxoptattr(&xvattr);
4931 ASSERT(xoap);
4932 XVA_SET_REQ(&xvattr, XAT_REPARSE);
4933
4934 if (VOP_GETATTR(vp, &xvattr.xva_vattr, 0, cr, ct))
4935 return (B_FALSE);
4936
4937 if ((!(xvattr.xva_vattr.va_mask & AT_XVATTR)) ||
4938 (!(XVA_ISSET_RTN(&xvattr, XAT_REPARSE))))
4939 return (B_FALSE);
4940
4941 return (xoap->xoa_reparse ? B_TRUE : B_FALSE);
4942 }