Print this page
6265 speed up mount/umount
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/vfs.c
+++ new/usr/src/uts/common/fs/vfs.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
24 24 */
25 25
26 26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 27 /* All Rights Reserved */
28 28
29 29 /*
30 30 * University Copyright- Copyright (c) 1982, 1986, 1988
31 31 * The Regents of the University of California
32 32 * All Rights Reserved
33 33 *
34 34 * University Acknowledgment- Portions of this document are derived from
35 35 * software developed by the University of California, Berkeley, and its
36 36 * contributors.
37 37 */
38 38
39 39 #include <sys/types.h>
40 40 #include <sys/t_lock.h>
41 41 #include <sys/param.h>
42 42 #include <sys/errno.h>
43 43 #include <sys/user.h>
44 44 #include <sys/fstyp.h>
45 45 #include <sys/kmem.h>
46 46 #include <sys/systm.h>
47 47 #include <sys/proc.h>
48 48 #include <sys/mount.h>
49 49 #include <sys/vfs.h>
50 50 #include <sys/vfs_opreg.h>
51 51 #include <sys/fem.h>
52 52 #include <sys/mntent.h>
53 53 #include <sys/stat.h>
54 54 #include <sys/statvfs.h>
55 55 #include <sys/statfs.h>
56 56 #include <sys/cred.h>
57 57 #include <sys/vnode.h>
58 58 #include <sys/rwstlock.h>
59 59 #include <sys/dnlc.h>
60 60 #include <sys/file.h>
61 61 #include <sys/time.h>
62 62 #include <sys/atomic.h>
63 63 #include <sys/cmn_err.h>
64 64 #include <sys/buf.h>
65 65 #include <sys/swap.h>
66 66 #include <sys/debug.h>
67 67 #include <sys/vnode.h>
68 68 #include <sys/modctl.h>
69 69 #include <sys/ddi.h>
70 70 #include <sys/pathname.h>
71 71 #include <sys/bootconf.h>
72 72 #include <sys/dumphdr.h>
73 73 #include <sys/dc_ki.h>
74 74 #include <sys/poll.h>
75 75 #include <sys/sunddi.h>
76 76 #include <sys/sysmacros.h>
77 77 #include <sys/zone.h>
↓ open down ↓ |
77 lines elided |
↑ open up ↑ |
78 78 #include <sys/policy.h>
79 79 #include <sys/ctfs.h>
80 80 #include <sys/objfs.h>
81 81 #include <sys/console.h>
82 82 #include <sys/reboot.h>
83 83 #include <sys/attr.h>
84 84 #include <sys/zio.h>
85 85 #include <sys/spa.h>
86 86 #include <sys/lofi.h>
87 87 #include <sys/bootprops.h>
88 +#include <sys/avl.h>
88 89
89 90 #include <vm/page.h>
90 91
91 92 #include <fs/fs_subr.h>
92 93 /* Private interfaces to create vopstats-related data structures */
93 94 extern void initialize_vopstats(vopstats_t *);
94 95 extern vopstats_t *get_fstype_vopstats(struct vfs *, struct vfssw *);
95 96 extern vsk_anchor_t *get_vskstat_anchor(struct vfs *);
96 97
97 98 static void vfs_clearmntopt_nolock(mntopts_t *, const char *, int);
98 99 static void vfs_setmntopt_nolock(mntopts_t *, const char *,
99 100 const char *, int, int);
100 101 static int vfs_optionisset_nolock(const mntopts_t *, const char *, char **);
101 102 static void vfs_freemnttab(struct vfs *);
102 103 static void vfs_freeopt(mntopt_t *);
103 104 static void vfs_swapopttbl_nolock(mntopts_t *, mntopts_t *);
104 105 static void vfs_swapopttbl(mntopts_t *, mntopts_t *);
105 106 static void vfs_copyopttbl_extend(const mntopts_t *, mntopts_t *, int);
106 107 static void vfs_createopttbl_extend(mntopts_t *, const char *,
107 108 const mntopts_t *);
108 109 static char **vfs_copycancelopt_extend(char **const, int);
109 110 static void vfs_freecancelopt(char **);
110 111 static void getrootfs(char **, char **);
111 112 static int getmacpath(dev_info_t *, void *);
112 113 static void vfs_mnttabvp_setup(void);
113 114
114 115 struct ipmnt {
115 116 struct ipmnt *mip_next;
116 117 dev_t mip_dev;
117 118 struct vfs *mip_vfsp;
118 119 };
119 120
120 121 static kmutex_t vfs_miplist_mutex;
121 122 static struct ipmnt *vfs_miplist = NULL;
122 123 static struct ipmnt *vfs_miplist_end = NULL;
123 124
124 125 static kmem_cache_t *vfs_cache; /* Pointer to VFS kmem cache */
125 126
126 127 /*
127 128 * VFS global data.
128 129 */
129 130 vnode_t *rootdir; /* pointer to root inode vnode. */
↓ open down ↓ |
32 lines elided |
↑ open up ↑ |
130 131 vnode_t *devicesdir; /* pointer to inode of devices root */
131 132 vnode_t *devdir; /* pointer to inode of dev root */
132 133
133 134 char *server_rootpath; /* root path for diskless clients */
134 135 char *server_hostname; /* hostname of diskless server */
135 136
136 137 static struct vfs root;
137 138 static struct vfs devices;
138 139 static struct vfs dev;
139 140 struct vfs *rootvfs = &root; /* pointer to root vfs; head of VFS list. */
141 +avl_tree_t vfs_by_dev; /* avl tree to index mounted VFSs by dev */
142 +avl_tree_t vfs_by_mntpnt; /* avl tree to index mounted VFSs by mntpnt */
143 +uint64_t vfs_curr_mntix; /* counter to provide a unique mntix for
144 + * entries in the above avl trees.
145 + * protected by vfslist lock */
140 146 rvfs_t *rvfs_list; /* array of vfs ptrs for vfs hash list */
141 147 int vfshsz = 512; /* # of heads/locks in vfs hash arrays */
142 148 /* must be power of 2! */
143 149 timespec_t vfs_mnttab_ctime; /* mnttab created time */
144 150 timespec_t vfs_mnttab_mtime; /* mnttab last modified time */
145 151 char *vfs_dummyfstype = "\0";
146 152 struct pollhead vfs_pollhd; /* for mnttab pollers */
147 153 struct vnode *vfs_mntdummyvp; /* to fake mnttab read/write for file events */
148 154 int mntfstype; /* will be set once mnt fs is mounted */
149 155
150 156 /*
151 157 * Table for generic options recognized in the VFS layer and acted
152 158 * on at this level before parsing file system specific options.
153 159 * The nosuid option is stronger than any of the devices and setuid
154 160 * options, so those are canceled when nosuid is seen.
155 161 *
156 162 * All options which are added here need to be added to the
157 163 * list of standard options in usr/src/cmd/fs.d/fslib.c as well.
158 164 */
159 165 /*
160 166 * VFS Mount options table
161 167 */
162 168 static char *ro_cancel[] = { MNTOPT_RW, NULL };
163 169 static char *rw_cancel[] = { MNTOPT_RO, NULL };
164 170 static char *suid_cancel[] = { MNTOPT_NOSUID, NULL };
165 171 static char *nosuid_cancel[] = { MNTOPT_SUID, MNTOPT_DEVICES, MNTOPT_NODEVICES,
166 172 MNTOPT_NOSETUID, MNTOPT_SETUID, NULL };
167 173 static char *devices_cancel[] = { MNTOPT_NODEVICES, NULL };
168 174 static char *nodevices_cancel[] = { MNTOPT_DEVICES, NULL };
169 175 static char *setuid_cancel[] = { MNTOPT_NOSETUID, NULL };
170 176 static char *nosetuid_cancel[] = { MNTOPT_SETUID, NULL };
171 177 static char *nbmand_cancel[] = { MNTOPT_NONBMAND, NULL };
172 178 static char *nonbmand_cancel[] = { MNTOPT_NBMAND, NULL };
173 179 static char *exec_cancel[] = { MNTOPT_NOEXEC, NULL };
174 180 static char *noexec_cancel[] = { MNTOPT_EXEC, NULL };
175 181
176 182 static const mntopt_t mntopts[] = {
177 183 /*
178 184 * option name cancel options default arg flags
179 185 */
180 186 { MNTOPT_REMOUNT, NULL, NULL,
181 187 MO_NODISPLAY, (void *)0 },
182 188 { MNTOPT_RO, ro_cancel, NULL, 0,
183 189 (void *)0 },
184 190 { MNTOPT_RW, rw_cancel, NULL, 0,
185 191 (void *)0 },
186 192 { MNTOPT_SUID, suid_cancel, NULL, 0,
187 193 (void *)0 },
188 194 { MNTOPT_NOSUID, nosuid_cancel, NULL, 0,
189 195 (void *)0 },
190 196 { MNTOPT_DEVICES, devices_cancel, NULL, 0,
191 197 (void *)0 },
192 198 { MNTOPT_NODEVICES, nodevices_cancel, NULL, 0,
193 199 (void *)0 },
194 200 { MNTOPT_SETUID, setuid_cancel, NULL, 0,
195 201 (void *)0 },
196 202 { MNTOPT_NOSETUID, nosetuid_cancel, NULL, 0,
197 203 (void *)0 },
198 204 { MNTOPT_NBMAND, nbmand_cancel, NULL, 0,
199 205 (void *)0 },
200 206 { MNTOPT_NONBMAND, nonbmand_cancel, NULL, 0,
201 207 (void *)0 },
202 208 { MNTOPT_EXEC, exec_cancel, NULL, 0,
203 209 (void *)0 },
204 210 { MNTOPT_NOEXEC, noexec_cancel, NULL, 0,
205 211 (void *)0 },
206 212 };
207 213
208 214 const mntopts_t vfs_mntopts = {
209 215 sizeof (mntopts) / sizeof (mntopt_t),
210 216 (mntopt_t *)&mntopts[0]
211 217 };
212 218
213 219 /*
214 220 * File system operation dispatch functions.
215 221 */
216 222
217 223 int
218 224 fsop_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
219 225 {
220 226 return (*(vfsp)->vfs_op->vfs_mount)(vfsp, mvp, uap, cr);
221 227 }
222 228
223 229 int
224 230 fsop_unmount(vfs_t *vfsp, int flag, cred_t *cr)
225 231 {
226 232 return (*(vfsp)->vfs_op->vfs_unmount)(vfsp, flag, cr);
227 233 }
228 234
229 235 int
230 236 fsop_root(vfs_t *vfsp, vnode_t **vpp)
231 237 {
232 238 refstr_t *mntpt;
233 239 int ret = (*(vfsp)->vfs_op->vfs_root)(vfsp, vpp);
234 240 /*
235 241 * Make sure this root has a path. With lofs, it is possible to have
236 242 * a NULL mountpoint.
237 243 */
238 244 if (ret == 0 && vfsp->vfs_mntpt != NULL && (*vpp)->v_path == NULL) {
239 245 mntpt = vfs_getmntpoint(vfsp);
240 246 vn_setpath_str(*vpp, refstr_value(mntpt),
241 247 strlen(refstr_value(mntpt)));
242 248 refstr_rele(mntpt);
243 249 }
244 250
245 251 return (ret);
246 252 }
247 253
248 254 int
249 255 fsop_statfs(vfs_t *vfsp, statvfs64_t *sp)
250 256 {
251 257 return (*(vfsp)->vfs_op->vfs_statvfs)(vfsp, sp);
252 258 }
253 259
254 260 int
255 261 fsop_sync(vfs_t *vfsp, short flag, cred_t *cr)
256 262 {
257 263 return (*(vfsp)->vfs_op->vfs_sync)(vfsp, flag, cr);
258 264 }
259 265
260 266 int
261 267 fsop_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
262 268 {
263 269 /*
264 270 * In order to handle system attribute fids in a manner
265 271 * transparent to the underlying fs, we embed the fid for
266 272 * the sysattr parent object in the sysattr fid and tack on
267 273 * some extra bytes that only the sysattr layer knows about.
268 274 *
269 275 * This guarantees that sysattr fids are larger than other fids
270 276 * for this vfs. If the vfs supports the sysattr view interface
271 277 * (as indicated by VFSFT_SYSATTR_VIEWS), we cannot have a size
272 278 * collision with XATTR_FIDSZ.
273 279 */
274 280 if (vfs_has_feature(vfsp, VFSFT_SYSATTR_VIEWS) &&
275 281 fidp->fid_len == XATTR_FIDSZ)
276 282 return (xattr_dir_vget(vfsp, vpp, fidp));
277 283
278 284 return (*(vfsp)->vfs_op->vfs_vget)(vfsp, vpp, fidp);
279 285 }
280 286
281 287 int
282 288 fsop_mountroot(vfs_t *vfsp, enum whymountroot reason)
283 289 {
284 290 return (*(vfsp)->vfs_op->vfs_mountroot)(vfsp, reason);
285 291 }
286 292
287 293 void
288 294 fsop_freefs(vfs_t *vfsp)
289 295 {
290 296 (*(vfsp)->vfs_op->vfs_freevfs)(vfsp);
291 297 }
292 298
293 299 int
294 300 fsop_vnstate(vfs_t *vfsp, vnode_t *vp, vntrans_t nstate)
295 301 {
296 302 return ((*(vfsp)->vfs_op->vfs_vnstate)(vfsp, vp, nstate));
297 303 }
298 304
299 305 int
300 306 fsop_sync_by_kind(int fstype, short flag, cred_t *cr)
301 307 {
302 308 ASSERT((fstype >= 0) && (fstype < nfstype));
303 309
304 310 if (ALLOCATED_VFSSW(&vfssw[fstype]) && VFS_INSTALLED(&vfssw[fstype]))
305 311 return (*vfssw[fstype].vsw_vfsops.vfs_sync) (NULL, flag, cr);
306 312 else
307 313 return (ENOTSUP);
308 314 }
309 315
310 316 /*
311 317 * File system initialization. vfs_setfsops() must be called from a file
312 318 * system's init routine.
313 319 */
314 320
315 321 static int
316 322 fs_copyfsops(const fs_operation_def_t *template, vfsops_t *actual,
317 323 int *unused_ops)
318 324 {
319 325 static const fs_operation_trans_def_t vfs_ops_table[] = {
320 326 VFSNAME_MOUNT, offsetof(vfsops_t, vfs_mount),
321 327 fs_nosys, fs_nosys,
322 328
323 329 VFSNAME_UNMOUNT, offsetof(vfsops_t, vfs_unmount),
324 330 fs_nosys, fs_nosys,
325 331
326 332 VFSNAME_ROOT, offsetof(vfsops_t, vfs_root),
327 333 fs_nosys, fs_nosys,
328 334
329 335 VFSNAME_STATVFS, offsetof(vfsops_t, vfs_statvfs),
330 336 fs_nosys, fs_nosys,
331 337
332 338 VFSNAME_SYNC, offsetof(vfsops_t, vfs_sync),
333 339 (fs_generic_func_p) fs_sync,
334 340 (fs_generic_func_p) fs_sync, /* No errors allowed */
335 341
336 342 VFSNAME_VGET, offsetof(vfsops_t, vfs_vget),
337 343 fs_nosys, fs_nosys,
338 344
339 345 VFSNAME_MOUNTROOT, offsetof(vfsops_t, vfs_mountroot),
340 346 fs_nosys, fs_nosys,
341 347
342 348 VFSNAME_FREEVFS, offsetof(vfsops_t, vfs_freevfs),
343 349 (fs_generic_func_p)fs_freevfs,
344 350 (fs_generic_func_p)fs_freevfs, /* Shouldn't fail */
345 351
346 352 VFSNAME_VNSTATE, offsetof(vfsops_t, vfs_vnstate),
347 353 (fs_generic_func_p)fs_nosys,
348 354 (fs_generic_func_p)fs_nosys,
349 355
350 356 NULL, 0, NULL, NULL
351 357 };
352 358
353 359 return (fs_build_vector(actual, unused_ops, vfs_ops_table, template));
354 360 }
355 361
356 362 void
357 363 zfs_boot_init() {
358 364
359 365 if (strcmp(rootfs.bo_fstype, MNTTYPE_ZFS) == 0)
360 366 spa_boot_init();
361 367 }
362 368
363 369 int
364 370 vfs_setfsops(int fstype, const fs_operation_def_t *template, vfsops_t **actual)
365 371 {
366 372 int error;
367 373 int unused_ops;
368 374
369 375 /*
370 376 * Verify that fstype refers to a valid fs. Note that
371 377 * 0 is valid since it's used to set "stray" ops.
372 378 */
373 379 if ((fstype < 0) || (fstype >= nfstype))
374 380 return (EINVAL);
375 381
376 382 if (!ALLOCATED_VFSSW(&vfssw[fstype]))
377 383 return (EINVAL);
378 384
379 385 /* Set up the operations vector. */
380 386
381 387 error = fs_copyfsops(template, &vfssw[fstype].vsw_vfsops, &unused_ops);
382 388
383 389 if (error != 0)
384 390 return (error);
385 391
386 392 vfssw[fstype].vsw_flag |= VSW_INSTALLED;
387 393
388 394 if (actual != NULL)
389 395 *actual = &vfssw[fstype].vsw_vfsops;
390 396
391 397 #if DEBUG
392 398 if (unused_ops != 0)
393 399 cmn_err(CE_WARN, "vfs_setfsops: %s: %d operations supplied "
394 400 "but not used", vfssw[fstype].vsw_name, unused_ops);
395 401 #endif
396 402
397 403 return (0);
398 404 }
399 405
400 406 int
401 407 vfs_makefsops(const fs_operation_def_t *template, vfsops_t **actual)
402 408 {
403 409 int error;
404 410 int unused_ops;
405 411
406 412 *actual = (vfsops_t *)kmem_alloc(sizeof (vfsops_t), KM_SLEEP);
407 413
408 414 error = fs_copyfsops(template, *actual, &unused_ops);
409 415 if (error != 0) {
410 416 kmem_free(*actual, sizeof (vfsops_t));
411 417 *actual = NULL;
412 418 return (error);
413 419 }
414 420
415 421 return (0);
416 422 }
417 423
418 424 /*
419 425 * Free a vfsops structure created as a result of vfs_makefsops().
420 426 * NOTE: For a vfsops structure initialized by vfs_setfsops(), use
421 427 * vfs_freevfsops_by_type().
422 428 */
423 429 void
424 430 vfs_freevfsops(vfsops_t *vfsops)
425 431 {
426 432 kmem_free(vfsops, sizeof (vfsops_t));
427 433 }
428 434
429 435 /*
430 436 * Since the vfsops structure is part of the vfssw table and wasn't
431 437 * really allocated, we're not really freeing anything. We keep
432 438 * the name for consistency with vfs_freevfsops(). We do, however,
433 439 * need to take care of a little bookkeeping.
434 440 * NOTE: For a vfsops structure created by vfs_setfsops(), use
435 441 * vfs_freevfsops_by_type().
436 442 */
437 443 int
438 444 vfs_freevfsops_by_type(int fstype)
439 445 {
440 446
441 447 /* Verify that fstype refers to a loaded fs (and not fsid 0). */
442 448 if ((fstype <= 0) || (fstype >= nfstype))
443 449 return (EINVAL);
444 450
445 451 WLOCK_VFSSW();
446 452 if ((vfssw[fstype].vsw_flag & VSW_INSTALLED) == 0) {
447 453 WUNLOCK_VFSSW();
448 454 return (EINVAL);
449 455 }
450 456
451 457 vfssw[fstype].vsw_flag &= ~VSW_INSTALLED;
452 458 WUNLOCK_VFSSW();
453 459
454 460 return (0);
455 461 }
456 462
457 463 /* Support routines used to reference vfs_op */
458 464
459 465 /* Set the operations vector for a vfs */
460 466 void
461 467 vfs_setops(vfs_t *vfsp, vfsops_t *vfsops)
462 468 {
463 469 vfsops_t *op;
464 470
465 471 ASSERT(vfsp != NULL);
466 472 ASSERT(vfsops != NULL);
467 473
468 474 op = vfsp->vfs_op;
469 475 membar_consumer();
470 476 if (vfsp->vfs_femhead == NULL &&
471 477 atomic_cas_ptr(&vfsp->vfs_op, op, vfsops) == op) {
472 478 return;
473 479 }
474 480 fsem_setvfsops(vfsp, vfsops);
475 481 }
476 482
477 483 /* Retrieve the operations vector for a vfs */
478 484 vfsops_t *
479 485 vfs_getops(vfs_t *vfsp)
480 486 {
481 487 vfsops_t *op;
482 488
483 489 ASSERT(vfsp != NULL);
484 490
485 491 op = vfsp->vfs_op;
486 492 membar_consumer();
487 493 if (vfsp->vfs_femhead == NULL && op == vfsp->vfs_op) {
488 494 return (op);
489 495 } else {
490 496 return (fsem_getvfsops(vfsp));
491 497 }
492 498 }
493 499
494 500 /*
495 501 * Returns non-zero (1) if the vfsops matches that of the vfs.
496 502 * Returns zero (0) if not.
497 503 */
498 504 int
499 505 vfs_matchops(vfs_t *vfsp, vfsops_t *vfsops)
500 506 {
501 507 return (vfs_getops(vfsp) == vfsops);
502 508 }
503 509
504 510 /*
505 511 * Returns non-zero (1) if the file system has installed a non-default,
506 512 * non-error vfs_sync routine. Returns zero (0) otherwise.
507 513 */
508 514 int
509 515 vfs_can_sync(vfs_t *vfsp)
510 516 {
511 517 /* vfs_sync() routine is not the default/error function */
512 518 return (vfs_getops(vfsp)->vfs_sync != fs_sync);
513 519 }
514 520
515 521 /*
516 522 * Initialize a vfs structure.
517 523 */
518 524 void
519 525 vfs_init(vfs_t *vfsp, vfsops_t *op, void *data)
520 526 {
521 527 /* Other initialization has been moved to vfs_alloc() */
522 528 vfsp->vfs_count = 0;
523 529 vfsp->vfs_next = vfsp;
524 530 vfsp->vfs_prev = vfsp;
525 531 vfsp->vfs_zone_next = vfsp;
526 532 vfsp->vfs_zone_prev = vfsp;
527 533 vfsp->vfs_lofi_minor = 0;
528 534 sema_init(&vfsp->vfs_reflock, 1, NULL, SEMA_DEFAULT, NULL);
529 535 vfsimpl_setup(vfsp);
530 536 vfsp->vfs_data = (data);
531 537 vfs_setops((vfsp), (op));
532 538 }
533 539
534 540 /*
535 541 * Allocate and initialize the vfs implementation private data
536 542 * structure, vfs_impl_t.
537 543 */
538 544 void
539 545 vfsimpl_setup(vfs_t *vfsp)
540 546 {
541 547 int i;
542 548
543 549 if (vfsp->vfs_implp != NULL) {
544 550 return;
545 551 }
546 552
547 553 vfsp->vfs_implp = kmem_alloc(sizeof (vfs_impl_t), KM_SLEEP);
548 554 /* Note that these are #define'd in vfs.h */
549 555 vfsp->vfs_vskap = NULL;
550 556 vfsp->vfs_fstypevsp = NULL;
551 557
552 558 /* Set size of counted array, then zero the array */
553 559 vfsp->vfs_featureset[0] = VFS_FEATURE_MAXSZ - 1;
554 560 for (i = 1; i < VFS_FEATURE_MAXSZ; i++) {
555 561 vfsp->vfs_featureset[i] = 0;
556 562 }
557 563 }
558 564
559 565 /*
560 566 * Release the vfs_impl_t structure, if it exists. Some unbundled
561 567 * filesystems may not use the newer version of vfs and thus
562 568 * would not contain this implementation private data structure.
563 569 */
564 570 void
565 571 vfsimpl_teardown(vfs_t *vfsp)
566 572 {
567 573 vfs_impl_t *vip = vfsp->vfs_implp;
568 574
569 575 if (vip == NULL)
570 576 return;
571 577
572 578 kmem_free(vfsp->vfs_implp, sizeof (vfs_impl_t));
573 579 vfsp->vfs_implp = NULL;
574 580 }
575 581
576 582 /*
577 583 * VFS system calls: mount, umount, syssync, statfs, fstatfs, statvfs,
578 584 * fstatvfs, and sysfs moved to common/syscall.
579 585 */
580 586
581 587 /*
582 588 * Update every mounted file system. We call the vfs_sync operation of
583 589 * each file system type, passing it a NULL vfsp to indicate that all
584 590 * mounted file systems of that type should be updated.
585 591 */
586 592 void
587 593 vfs_sync(int flag)
588 594 {
589 595 struct vfssw *vswp;
590 596 RLOCK_VFSSW();
591 597 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
592 598 if (ALLOCATED_VFSSW(vswp) && VFS_INSTALLED(vswp)) {
593 599 vfs_refvfssw(vswp);
594 600 RUNLOCK_VFSSW();
595 601 (void) (*vswp->vsw_vfsops.vfs_sync)(NULL, flag,
596 602 CRED());
597 603 vfs_unrefvfssw(vswp);
598 604 RLOCK_VFSSW();
599 605 }
600 606 }
↓ open down ↓ |
451 lines elided |
↑ open up ↑ |
601 607 RUNLOCK_VFSSW();
602 608 }
603 609
604 610 void
605 611 sync(void)
606 612 {
607 613 vfs_sync(0);
608 614 }
609 615
610 616 /*
617 + * compare function for vfs_by_dev avl tree. compare dev first, then mntix
618 + */
619 +static int
620 +vfs_cmp_dev(const void *aa, const void *bb)
621 +{
622 + const vfs_t *a = aa;
623 + const vfs_t *b = bb;
624 +
625 + if (a->vfs_dev < b->vfs_dev)
626 + return (-1);
627 + if (a->vfs_dev > b->vfs_dev)
628 + return (1);
629 + if (a->vfs_mntix < b->vfs_mntix)
630 + return (-1);
631 + if (a->vfs_mntix > b->vfs_mntix)
632 + return (1);
633 + return (0);
634 +}
635 +
636 +/*
637 + * compare function for vfs_by_mntpnt avl tree. compare mntpnt first, then mntix
638 + */
639 +static int
640 +vfs_cmp_mntpnt(const void *aa, const void *bb)
641 +{
642 + const vfs_t *a = aa;
643 + const vfs_t *b = bb;
644 + int ret;
645 +
646 + ret = strcmp(refstr_value(a->vfs_mntpt), refstr_value(b->vfs_mntpt));
647 + if (ret < 0)
648 + return (-1);
649 + if (ret > 0)
650 + return (1);
651 + if (a->vfs_mntix < b->vfs_mntix)
652 + return (-1);
653 + if (a->vfs_mntix > b->vfs_mntix)
654 + return (1);
655 + return (0);
656 +}
657 +
658 +/*
611 659 * External routines.
612 660 */
613 661
614 662 krwlock_t vfssw_lock; /* lock accesses to vfssw */
615 663
616 664 /*
617 665 * Lock for accessing the vfs linked list. Initialized in vfs_mountroot(),
618 666 * but otherwise should be accessed only via vfs_list_lock() and
619 667 * vfs_list_unlock(). Also used to protect the timestamp for mods to the list.
620 668 */
621 669 static krwlock_t vfslist;
622 670
623 671 /*
624 672 * Mount devfs on /devices. This is done right after root is mounted
625 673 * to provide device access support for the system
626 674 */
627 675 static void
628 676 vfs_mountdevices(void)
629 677 {
630 678 struct vfssw *vsw;
631 679 struct vnode *mvp;
632 680 struct mounta mounta = { /* fake mounta for devfs_mount() */
633 681 NULL,
634 682 NULL,
635 683 MS_SYSSPACE,
636 684 NULL,
637 685 NULL,
638 686 0,
639 687 NULL,
640 688 0
641 689 };
642 690
643 691 /*
644 692 * _init devfs module to fill in the vfssw
645 693 */
646 694 if (modload("fs", "devfs") == -1)
647 695 panic("Cannot _init devfs module");
648 696
649 697 /*
650 698 * Hold vfs
651 699 */
652 700 RLOCK_VFSSW();
653 701 vsw = vfs_getvfsswbyname("devfs");
654 702 VFS_INIT(&devices, &vsw->vsw_vfsops, NULL);
655 703 VFS_HOLD(&devices);
656 704
657 705 /*
658 706 * Locate mount point
659 707 */
660 708 if (lookupname("/devices", UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp))
661 709 panic("Cannot find /devices");
662 710
663 711 /*
664 712 * Perform the mount of /devices
665 713 */
666 714 if (VFS_MOUNT(&devices, mvp, &mounta, CRED()))
667 715 panic("Cannot mount /devices");
668 716
669 717 RUNLOCK_VFSSW();
670 718
671 719 /*
672 720 * Set appropriate members and add to vfs list for mnttab display
673 721 */
674 722 vfs_setresource(&devices, "/devices", 0);
675 723 vfs_setmntpoint(&devices, "/devices", 0);
676 724
677 725 /*
678 726 * Hold the root of /devices so it won't go away
679 727 */
680 728 if (VFS_ROOT(&devices, &devicesdir))
681 729 panic("vfs_mountdevices: not devices root");
682 730
683 731 if (vfs_lock(&devices) != 0) {
684 732 VN_RELE(devicesdir);
685 733 cmn_err(CE_NOTE, "Cannot acquire vfs_lock of /devices");
686 734 return;
687 735 }
688 736
689 737 if (vn_vfswlock(mvp) != 0) {
690 738 vfs_unlock(&devices);
691 739 VN_RELE(devicesdir);
692 740 cmn_err(CE_NOTE, "Cannot acquire vfswlock of /devices");
693 741 return;
694 742 }
695 743
696 744 vfs_add(mvp, &devices, 0);
697 745 vn_vfsunlock(mvp);
698 746 vfs_unlock(&devices);
699 747 VN_RELE(devicesdir);
700 748 }
701 749
702 750 /*
703 751 * mount the first instance of /dev to root and remain mounted
704 752 */
705 753 static void
706 754 vfs_mountdev1(void)
707 755 {
708 756 struct vfssw *vsw;
709 757 struct vnode *mvp;
710 758 struct mounta mounta = { /* fake mounta for sdev_mount() */
711 759 NULL,
712 760 NULL,
713 761 MS_SYSSPACE | MS_OVERLAY,
714 762 NULL,
715 763 NULL,
716 764 0,
717 765 NULL,
718 766 0
719 767 };
720 768
721 769 /*
722 770 * _init dev module to fill in the vfssw
723 771 */
724 772 if (modload("fs", "dev") == -1)
725 773 cmn_err(CE_PANIC, "Cannot _init dev module\n");
726 774
727 775 /*
728 776 * Hold vfs
729 777 */
730 778 RLOCK_VFSSW();
731 779 vsw = vfs_getvfsswbyname("dev");
732 780 VFS_INIT(&dev, &vsw->vsw_vfsops, NULL);
733 781 VFS_HOLD(&dev);
734 782
735 783 /*
736 784 * Locate mount point
737 785 */
738 786 if (lookupname("/dev", UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp))
739 787 cmn_err(CE_PANIC, "Cannot find /dev\n");
740 788
741 789 /*
742 790 * Perform the mount of /dev
743 791 */
744 792 if (VFS_MOUNT(&dev, mvp, &mounta, CRED()))
745 793 cmn_err(CE_PANIC, "Cannot mount /dev 1\n");
746 794
747 795 RUNLOCK_VFSSW();
748 796
749 797 /*
750 798 * Set appropriate members and add to vfs list for mnttab display
751 799 */
752 800 vfs_setresource(&dev, "/dev", 0);
753 801 vfs_setmntpoint(&dev, "/dev", 0);
754 802
755 803 /*
756 804 * Hold the root of /dev so it won't go away
757 805 */
758 806 if (VFS_ROOT(&dev, &devdir))
759 807 cmn_err(CE_PANIC, "vfs_mountdev1: not dev root");
760 808
761 809 if (vfs_lock(&dev) != 0) {
762 810 VN_RELE(devdir);
763 811 cmn_err(CE_NOTE, "Cannot acquire vfs_lock of /dev");
764 812 return;
765 813 }
766 814
767 815 if (vn_vfswlock(mvp) != 0) {
768 816 vfs_unlock(&dev);
769 817 VN_RELE(devdir);
770 818 cmn_err(CE_NOTE, "Cannot acquire vfswlock of /dev");
771 819 return;
772 820 }
773 821
774 822 vfs_add(mvp, &dev, 0);
775 823 vn_vfsunlock(mvp);
776 824 vfs_unlock(&dev);
777 825 VN_RELE(devdir);
778 826 }
779 827
780 828 /*
781 829 * Mount required filesystem. This is done right after root is mounted.
782 830 */
783 831 static void
784 832 vfs_mountfs(char *module, char *spec, char *path)
785 833 {
786 834 struct vnode *mvp;
787 835 struct mounta mounta;
788 836 vfs_t *vfsp;
789 837
790 838 mounta.flags = MS_SYSSPACE | MS_DATA;
791 839 mounta.fstype = module;
792 840 mounta.spec = spec;
793 841 mounta.dir = path;
794 842 if (lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &mvp)) {
795 843 cmn_err(CE_WARN, "Cannot find %s", path);
796 844 return;
797 845 }
798 846 if (domount(NULL, &mounta, mvp, CRED(), &vfsp))
799 847 cmn_err(CE_WARN, "Cannot mount %s", path);
800 848 else
801 849 VFS_RELE(vfsp);
802 850 VN_RELE(mvp);
803 851 }
804 852
805 853 /*
806 854 * vfs_mountroot is called by main() to mount the root filesystem.
807 855 */
808 856 void
809 857 vfs_mountroot(void)
810 858 {
↓ open down ↓ |
190 lines elided |
↑ open up ↑ |
811 859 struct vnode *rvp = NULL;
812 860 char *path;
813 861 size_t plen;
814 862 struct vfssw *vswp;
815 863 proc_t *p;
816 864
817 865 rw_init(&vfssw_lock, NULL, RW_DEFAULT, NULL);
818 866 rw_init(&vfslist, NULL, RW_DEFAULT, NULL);
819 867
820 868 /*
869 + * Alloc the avl trees for quick indexing via dev and mountpoint
870 + */
871 + avl_create(&vfs_by_dev, vfs_cmp_dev, sizeof(vfs_t),
872 + offsetof(vfs_t, vfs_avldev));
873 + avl_create(&vfs_by_mntpnt, vfs_cmp_mntpnt, sizeof(vfs_t),
874 + offsetof(vfs_t, vfs_avlmntpnt));
875 +
876 + /*
821 877 * Alloc the vfs hash bucket array and locks
822 878 */
823 879 rvfs_list = kmem_zalloc(vfshsz * sizeof (rvfs_t), KM_SLEEP);
824 880
825 881 /*
826 882 * Call machine-dependent routine "rootconf" to choose a root
827 883 * file system type.
828 884 */
829 885 if (rootconf())
830 886 panic("vfs_mountroot: cannot mount root");
831 887 /*
832 888 * Get vnode for '/'. Set up rootdir, u.u_rdir and u.u_cdir
833 889 * to point to it. These are used by lookuppn() so that it
834 890 * knows where to start from ('/' or '.').
835 891 */
836 892 vfs_setmntpoint(rootvfs, "/", 0);
837 893 if (VFS_ROOT(rootvfs, &rootdir))
838 894 panic("vfs_mountroot: no root vnode");
839 895
840 896 /*
841 897 * At this point, the process tree consists of p0 and possibly some
842 898 * direct children of p0. (i.e. there are no grandchildren)
843 899 *
844 900 * Walk through them all, setting their current directory.
845 901 */
846 902 mutex_enter(&pidlock);
847 903 for (p = practive; p != NULL; p = p->p_next) {
848 904 ASSERT(p == &p0 || p->p_parent == &p0);
849 905
850 906 PTOU(p)->u_cdir = rootdir;
851 907 VN_HOLD(PTOU(p)->u_cdir);
852 908 PTOU(p)->u_rdir = NULL;
853 909 }
854 910 mutex_exit(&pidlock);
855 911
856 912 /*
857 913 * Setup the global zone's rootvp, now that it exists.
858 914 */
859 915 global_zone->zone_rootvp = rootdir;
860 916 VN_HOLD(global_zone->zone_rootvp);
861 917
862 918 /*
863 919 * Notify the module code that it can begin using the
864 920 * root filesystem instead of the boot program's services.
865 921 */
866 922 modrootloaded = 1;
867 923
868 924 /*
869 925 * Special handling for a ZFS root file system.
870 926 */
871 927 zfs_boot_init();
872 928
873 929 /*
874 930 * Set up mnttab information for root
875 931 */
876 932 vfs_setresource(rootvfs, rootfs.bo_name, 0);
877 933
878 934 /*
879 935 * Notify cluster software that the root filesystem is available.
880 936 */
881 937 clboot_mountroot();
882 938
883 939 /* Now that we're all done with the root FS, set up its vopstats */
884 940 if ((vswp = vfs_getvfsswbyvfsops(vfs_getops(rootvfs))) != NULL) {
885 941 /* Set flag for statistics collection */
886 942 if (vswp->vsw_flag & VSW_STATS) {
887 943 initialize_vopstats(&rootvfs->vfs_vopstats);
888 944 rootvfs->vfs_flag |= VFS_STATS;
889 945 rootvfs->vfs_fstypevsp =
890 946 get_fstype_vopstats(rootvfs, vswp);
891 947 rootvfs->vfs_vskap = get_vskstat_anchor(rootvfs);
892 948 }
893 949 vfs_unrefvfssw(vswp);
894 950 }
895 951
896 952 /*
897 953 * Mount /devices, /dev instance 1, /system/contract, /etc/mnttab,
898 954 * /etc/svc/volatile, /etc/dfs/sharetab, /system/object, and /proc.
899 955 */
900 956 vfs_mountdevices();
901 957 vfs_mountdev1();
902 958
903 959 vfs_mountfs("ctfs", "ctfs", CTFS_ROOT);
904 960 vfs_mountfs("proc", "/proc", "/proc");
905 961 vfs_mountfs("mntfs", "/etc/mnttab", "/etc/mnttab");
906 962 vfs_mountfs("tmpfs", "/etc/svc/volatile", "/etc/svc/volatile");
907 963 vfs_mountfs("objfs", "objfs", OBJFS_ROOT);
908 964
909 965 if (getzoneid() == GLOBAL_ZONEID) {
910 966 vfs_mountfs("sharefs", "sharefs", "/etc/dfs/sharetab");
911 967 }
912 968
913 969 #ifdef __sparc
914 970 /*
915 971 * This bit of magic can go away when we convert sparc to
916 972 * the new boot architecture based on ramdisk.
917 973 *
918 974 * Booting off a mirrored root volume:
919 975 * At this point, we have booted and mounted root on a
920 976 * single component of the mirror. Complete the boot
921 977 * by configuring SVM and converting the root to the
922 978 * dev_t of the mirrored root device. This dev_t conversion
923 979 * only works because the underlying device doesn't change.
924 980 */
925 981 if (root_is_svm) {
926 982 if (svm_rootconf()) {
927 983 panic("vfs_mountroot: cannot remount root");
928 984 }
929 985
930 986 /*
931 987 * mnttab should reflect the new root device
932 988 */
933 989 vfs_lock_wait(rootvfs);
934 990 vfs_setresource(rootvfs, rootfs.bo_name, 0);
935 991 vfs_unlock(rootvfs);
936 992 }
937 993 #endif /* __sparc */
938 994
939 995 if (strcmp(rootfs.bo_fstype, "zfs") != 0) {
940 996 /*
941 997 * Look up the root device via devfs so that a dv_node is
942 998 * created for it. The vnode is never VN_RELE()ed.
943 999 * We allocate more than MAXPATHLEN so that the
944 1000 * buffer passed to i_ddi_prompath_to_devfspath() is
945 1001 * exactly MAXPATHLEN (the function expects a buffer
946 1002 * of that length).
947 1003 */
948 1004 plen = strlen("/devices");
949 1005 path = kmem_alloc(plen + MAXPATHLEN, KM_SLEEP);
950 1006 (void) strcpy(path, "/devices");
951 1007
952 1008 if (i_ddi_prompath_to_devfspath(rootfs.bo_name, path + plen)
953 1009 != DDI_SUCCESS ||
954 1010 lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &rvp)) {
955 1011
956 1012 /* NUL terminate in case "path" has garbage */
957 1013 path[plen + MAXPATHLEN - 1] = '\0';
958 1014 #ifdef DEBUG
959 1015 cmn_err(CE_WARN, "!Cannot lookup root device: %s",
960 1016 path);
961 1017 #endif
962 1018 }
963 1019 kmem_free(path, plen + MAXPATHLEN);
964 1020 }
965 1021
966 1022 vfs_mnttabvp_setup();
967 1023 }
968 1024
969 1025 /*
970 1026 * Check to see if our "block device" is actually a file. If so,
971 1027 * automatically add a lofi device, and keep track of this fact.
972 1028 */
973 1029 static int
974 1030 lofi_add(const char *fsname, struct vfs *vfsp,
975 1031 mntopts_t *mntopts, struct mounta *uap)
976 1032 {
977 1033 int fromspace = (uap->flags & MS_SYSSPACE) ?
978 1034 UIO_SYSSPACE : UIO_USERSPACE;
979 1035 struct lofi_ioctl *li = NULL;
980 1036 struct vnode *vp = NULL;
981 1037 struct pathname pn = { NULL };
982 1038 ldi_ident_t ldi_id;
983 1039 ldi_handle_t ldi_hdl;
984 1040 vfssw_t *vfssw;
985 1041 int minor;
986 1042 int err = 0;
987 1043
988 1044 if ((vfssw = vfs_getvfssw(fsname)) == NULL)
989 1045 return (0);
990 1046
991 1047 if (!(vfssw->vsw_flag & VSW_CANLOFI)) {
992 1048 vfs_unrefvfssw(vfssw);
993 1049 return (0);
994 1050 }
995 1051
996 1052 vfs_unrefvfssw(vfssw);
997 1053 vfssw = NULL;
998 1054
999 1055 if (pn_get(uap->spec, fromspace, &pn) != 0)
1000 1056 return (0);
1001 1057
1002 1058 if (lookupname(uap->spec, fromspace, FOLLOW, NULL, &vp) != 0)
1003 1059 goto out;
1004 1060
1005 1061 if (vp->v_type != VREG)
1006 1062 goto out;
1007 1063
1008 1064 /* OK, this is a lofi mount. */
1009 1065
1010 1066 if ((uap->flags & (MS_REMOUNT|MS_GLOBAL)) ||
1011 1067 vfs_optionisset_nolock(mntopts, MNTOPT_SUID, NULL) ||
1012 1068 vfs_optionisset_nolock(mntopts, MNTOPT_SETUID, NULL) ||
1013 1069 vfs_optionisset_nolock(mntopts, MNTOPT_DEVICES, NULL)) {
1014 1070 err = EINVAL;
1015 1071 goto out;
1016 1072 }
1017 1073
1018 1074 ldi_id = ldi_ident_from_anon();
1019 1075 li = kmem_zalloc(sizeof (*li), KM_SLEEP);
1020 1076 (void) strlcpy(li->li_filename, pn.pn_path, MAXPATHLEN);
1021 1077
1022 1078 err = ldi_open_by_name("/dev/lofictl", FREAD | FWRITE, kcred,
1023 1079 &ldi_hdl, ldi_id);
1024 1080
1025 1081 if (err)
1026 1082 goto out2;
1027 1083
1028 1084 err = ldi_ioctl(ldi_hdl, LOFI_MAP_FILE, (intptr_t)li,
1029 1085 FREAD | FWRITE | FKIOCTL, kcred, &minor);
1030 1086
1031 1087 (void) ldi_close(ldi_hdl, FREAD | FWRITE, kcred);
1032 1088
1033 1089 if (!err)
1034 1090 vfsp->vfs_lofi_minor = minor;
1035 1091
1036 1092 out2:
1037 1093 ldi_ident_release(ldi_id);
1038 1094 out:
1039 1095 if (li != NULL)
1040 1096 kmem_free(li, sizeof (*li));
1041 1097 if (vp != NULL)
1042 1098 VN_RELE(vp);
1043 1099 pn_free(&pn);
1044 1100 return (err);
1045 1101 }
1046 1102
1047 1103 static void
1048 1104 lofi_remove(struct vfs *vfsp)
1049 1105 {
1050 1106 struct lofi_ioctl *li = NULL;
1051 1107 ldi_ident_t ldi_id;
1052 1108 ldi_handle_t ldi_hdl;
1053 1109 int err;
1054 1110
1055 1111 if (vfsp->vfs_lofi_minor == 0)
1056 1112 return;
1057 1113
1058 1114 ldi_id = ldi_ident_from_anon();
1059 1115
1060 1116 li = kmem_zalloc(sizeof (*li), KM_SLEEP);
1061 1117 li->li_minor = vfsp->vfs_lofi_minor;
1062 1118 li->li_cleanup = B_TRUE;
1063 1119
1064 1120 err = ldi_open_by_name("/dev/lofictl", FREAD | FWRITE, kcred,
1065 1121 &ldi_hdl, ldi_id);
1066 1122
1067 1123 if (err)
1068 1124 goto out;
1069 1125
1070 1126 err = ldi_ioctl(ldi_hdl, LOFI_UNMAP_FILE_MINOR, (intptr_t)li,
1071 1127 FREAD | FWRITE | FKIOCTL, kcred, NULL);
1072 1128
1073 1129 (void) ldi_close(ldi_hdl, FREAD | FWRITE, kcred);
1074 1130
1075 1131 if (!err)
1076 1132 vfsp->vfs_lofi_minor = 0;
1077 1133
1078 1134 out:
1079 1135 ldi_ident_release(ldi_id);
1080 1136 if (li != NULL)
1081 1137 kmem_free(li, sizeof (*li));
1082 1138 }
1083 1139
1084 1140 /*
1085 1141 * Common mount code. Called from the system call entry point, from autofs,
1086 1142 * nfsv4 trigger mounts, and from pxfs.
1087 1143 *
1088 1144 * Takes the effective file system type, mount arguments, the mount point
1089 1145 * vnode, flags specifying whether the mount is a remount and whether it
1090 1146 * should be entered into the vfs list, and credentials. Fills in its vfspp
1091 1147 * parameter with the mounted file system instance's vfs.
1092 1148 *
1093 1149 * Note that the effective file system type is specified as a string. It may
1094 1150 * be null, in which case it's determined from the mount arguments, and may
1095 1151 * differ from the type specified in the mount arguments; this is a hook to
1096 1152 * allow interposition when instantiating file system instances.
1097 1153 *
1098 1154 * The caller is responsible for releasing its own hold on the mount point
1099 1155 * vp (this routine does its own hold when necessary).
1100 1156 * Also note that for remounts, the mount point vp should be the vnode for
1101 1157 * the root of the file system rather than the vnode that the file system
1102 1158 * is mounted on top of.
1103 1159 */
1104 1160 int
1105 1161 domount(char *fsname, struct mounta *uap, vnode_t *vp, struct cred *credp,
1106 1162 struct vfs **vfspp)
1107 1163 {
1108 1164 struct vfssw *vswp;
1109 1165 vfsops_t *vfsops;
1110 1166 struct vfs *vfsp;
1111 1167 struct vnode *bvp;
1112 1168 dev_t bdev = 0;
1113 1169 mntopts_t mnt_mntopts;
1114 1170 int error = 0;
1115 1171 int copyout_error = 0;
1116 1172 int ovflags;
1117 1173 char *opts = uap->optptr;
1118 1174 char *inargs = opts;
1119 1175 int optlen = uap->optlen;
1120 1176 int remount;
1121 1177 int rdonly;
1122 1178 int nbmand = 0;
1123 1179 int delmip = 0;
1124 1180 int addmip = 0;
1125 1181 int splice = ((uap->flags & MS_NOSPLICE) == 0);
1126 1182 int fromspace = (uap->flags & MS_SYSSPACE) ?
1127 1183 UIO_SYSSPACE : UIO_USERSPACE;
1128 1184 char *resource = NULL, *mountpt = NULL;
1129 1185 refstr_t *oldresource, *oldmntpt;
1130 1186 struct pathname pn, rpn;
1131 1187 vsk_anchor_t *vskap;
1132 1188 char fstname[FSTYPSZ];
1133 1189 zone_t *zone;
1134 1190
1135 1191 /*
1136 1192 * The v_flag value for the mount point vp is permanently set
1137 1193 * to VVFSLOCK so that no one bypasses the vn_vfs*locks routine
1138 1194 * for mount point locking.
1139 1195 */
1140 1196 mutex_enter(&vp->v_lock);
1141 1197 vp->v_flag |= VVFSLOCK;
1142 1198 mutex_exit(&vp->v_lock);
1143 1199
1144 1200 mnt_mntopts.mo_count = 0;
1145 1201 /*
1146 1202 * Find the ops vector to use to invoke the file system-specific mount
1147 1203 * method. If the fsname argument is non-NULL, use it directly.
1148 1204 * Otherwise, dig the file system type information out of the mount
1149 1205 * arguments.
1150 1206 *
1151 1207 * A side effect is to hold the vfssw entry.
1152 1208 *
1153 1209 * Mount arguments can be specified in several ways, which are
1154 1210 * distinguished by flag bit settings. The preferred way is to set
1155 1211 * MS_OPTIONSTR, indicating an 8 argument mount with the file system
1156 1212 * type supplied as a character string and the last two arguments
1157 1213 * being a pointer to a character buffer and the size of the buffer.
1158 1214 * On entry, the buffer holds a null terminated list of options; on
1159 1215 * return, the string is the list of options the file system
1160 1216 * recognized. If MS_DATA is set arguments five and six point to a
1161 1217 * block of binary data which the file system interprets.
1162 1218 * A further wrinkle is that some callers don't set MS_FSS and MS_DATA
1163 1219 * consistently with these conventions. To handle them, we check to
1164 1220 * see whether the pointer to the file system name has a numeric value
1165 1221 * less than 256. If so, we treat it as an index.
1166 1222 */
1167 1223 if (fsname != NULL) {
1168 1224 if ((vswp = vfs_getvfssw(fsname)) == NULL) {
1169 1225 return (EINVAL);
1170 1226 }
1171 1227 } else if (uap->flags & (MS_OPTIONSTR | MS_DATA | MS_FSS)) {
1172 1228 size_t n;
1173 1229 uint_t fstype;
1174 1230
1175 1231 fsname = fstname;
1176 1232
1177 1233 if ((fstype = (uintptr_t)uap->fstype) < 256) {
1178 1234 RLOCK_VFSSW();
1179 1235 if (fstype == 0 || fstype >= nfstype ||
1180 1236 !ALLOCATED_VFSSW(&vfssw[fstype])) {
1181 1237 RUNLOCK_VFSSW();
1182 1238 return (EINVAL);
1183 1239 }
1184 1240 (void) strcpy(fsname, vfssw[fstype].vsw_name);
1185 1241 RUNLOCK_VFSSW();
1186 1242 if ((vswp = vfs_getvfssw(fsname)) == NULL)
1187 1243 return (EINVAL);
1188 1244 } else {
1189 1245 /*
1190 1246 * Handle either kernel or user address space.
1191 1247 */
1192 1248 if (uap->flags & MS_SYSSPACE) {
1193 1249 error = copystr(uap->fstype, fsname,
1194 1250 FSTYPSZ, &n);
1195 1251 } else {
1196 1252 error = copyinstr(uap->fstype, fsname,
1197 1253 FSTYPSZ, &n);
1198 1254 }
1199 1255 if (error) {
1200 1256 if (error == ENAMETOOLONG)
1201 1257 return (EINVAL);
1202 1258 return (error);
1203 1259 }
1204 1260 if ((vswp = vfs_getvfssw(fsname)) == NULL)
1205 1261 return (EINVAL);
1206 1262 }
1207 1263 } else {
1208 1264 if ((vswp = vfs_getvfsswbyvfsops(vfs_getops(rootvfs))) == NULL)
1209 1265 return (EINVAL);
1210 1266 fsname = vswp->vsw_name;
1211 1267 }
1212 1268 if (!VFS_INSTALLED(vswp))
1213 1269 return (EINVAL);
1214 1270
1215 1271 if ((error = secpolicy_fs_allowed_mount(fsname)) != 0) {
1216 1272 vfs_unrefvfssw(vswp);
1217 1273 return (error);
1218 1274 }
1219 1275
1220 1276 vfsops = &vswp->vsw_vfsops;
1221 1277
1222 1278 vfs_copyopttbl(&vswp->vsw_optproto, &mnt_mntopts);
1223 1279 /*
1224 1280 * Fetch mount options and parse them for generic vfs options
1225 1281 */
1226 1282 if (uap->flags & MS_OPTIONSTR) {
1227 1283 /*
1228 1284 * Limit the buffer size
1229 1285 */
1230 1286 if (optlen < 0 || optlen > MAX_MNTOPT_STR) {
1231 1287 error = EINVAL;
1232 1288 goto errout;
1233 1289 }
1234 1290 if ((uap->flags & MS_SYSSPACE) == 0) {
1235 1291 inargs = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
1236 1292 inargs[0] = '\0';
1237 1293 if (optlen) {
1238 1294 error = copyinstr(opts, inargs, (size_t)optlen,
1239 1295 NULL);
1240 1296 if (error) {
1241 1297 goto errout;
1242 1298 }
1243 1299 }
1244 1300 }
1245 1301 vfs_parsemntopts(&mnt_mntopts, inargs, 0);
1246 1302 }
1247 1303 /*
1248 1304 * Flag bits override the options string.
1249 1305 */
1250 1306 if (uap->flags & MS_REMOUNT)
1251 1307 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_REMOUNT, NULL, 0, 0);
1252 1308 if (uap->flags & MS_RDONLY)
1253 1309 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_RO, NULL, 0, 0);
1254 1310 if (uap->flags & MS_NOSUID)
1255 1311 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL, 0, 0);
1256 1312
1257 1313 /*
1258 1314 * Check if this is a remount; must be set in the option string and
1259 1315 * the file system must support a remount option.
1260 1316 */
1261 1317 if (remount = vfs_optionisset_nolock(&mnt_mntopts,
1262 1318 MNTOPT_REMOUNT, NULL)) {
1263 1319 if (!(vswp->vsw_flag & VSW_CANREMOUNT)) {
1264 1320 error = ENOTSUP;
1265 1321 goto errout;
1266 1322 }
1267 1323 uap->flags |= MS_REMOUNT;
1268 1324 }
1269 1325
1270 1326 /*
1271 1327 * uap->flags and vfs_optionisset() should agree.
1272 1328 */
1273 1329 if (rdonly = vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_RO, NULL)) {
1274 1330 uap->flags |= MS_RDONLY;
1275 1331 }
1276 1332 if (vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL)) {
1277 1333 uap->flags |= MS_NOSUID;
1278 1334 }
1279 1335 nbmand = vfs_optionisset_nolock(&mnt_mntopts, MNTOPT_NBMAND, NULL);
1280 1336 ASSERT(splice || !remount);
1281 1337 /*
1282 1338 * If we are splicing the fs into the namespace,
1283 1339 * perform mount point checks.
1284 1340 *
1285 1341 * We want to resolve the path for the mount point to eliminate
1286 1342 * '.' and ".." and symlinks in mount points; we can't do the
1287 1343 * same for the resource string, since it would turn
1288 1344 * "/dev/dsk/c0t0d0s0" into "/devices/pci@...". We need to do
1289 1345 * this before grabbing vn_vfswlock(), because otherwise we
1290 1346 * would deadlock with lookuppn().
1291 1347 */
1292 1348 if (splice) {
1293 1349 ASSERT(vp->v_count > 0);
1294 1350
1295 1351 /*
1296 1352 * Pick up mount point and device from appropriate space.
1297 1353 */
1298 1354 if (pn_get(uap->spec, fromspace, &pn) == 0) {
1299 1355 resource = kmem_alloc(pn.pn_pathlen + 1,
1300 1356 KM_SLEEP);
1301 1357 (void) strcpy(resource, pn.pn_path);
1302 1358 pn_free(&pn);
1303 1359 }
1304 1360 /*
1305 1361 * Do a lookupname prior to taking the
1306 1362 * writelock. Mark this as completed if
1307 1363 * successful for later cleanup and addition to
1308 1364 * the mount in progress table.
1309 1365 */
1310 1366 if ((uap->flags & MS_GLOBAL) == 0 &&
1311 1367 lookupname(uap->spec, fromspace,
1312 1368 FOLLOW, NULL, &bvp) == 0) {
1313 1369 addmip = 1;
1314 1370 }
1315 1371
1316 1372 if ((error = pn_get(uap->dir, fromspace, &pn)) == 0) {
1317 1373 pathname_t *pnp;
1318 1374
1319 1375 if (*pn.pn_path != '/') {
1320 1376 error = EINVAL;
1321 1377 pn_free(&pn);
1322 1378 goto errout;
1323 1379 }
1324 1380 pn_alloc(&rpn);
1325 1381 /*
1326 1382 * Kludge to prevent autofs from deadlocking with
1327 1383 * itself when it calls domount().
1328 1384 *
1329 1385 * If autofs is calling, it is because it is doing
1330 1386 * (autofs) mounts in the process of an NFS mount. A
1331 1387 * lookuppn() here would cause us to block waiting for
1332 1388 * said NFS mount to complete, which can't since this
1333 1389 * is the thread that was supposed to doing it.
1334 1390 */
1335 1391 if (fromspace == UIO_USERSPACE) {
1336 1392 if ((error = lookuppn(&pn, &rpn, FOLLOW, NULL,
1337 1393 NULL)) == 0) {
1338 1394 pnp = &rpn;
1339 1395 } else {
1340 1396 /*
1341 1397 * The file disappeared or otherwise
1342 1398 * became inaccessible since we opened
1343 1399 * it; might as well fail the mount
1344 1400 * since the mount point is no longer
1345 1401 * accessible.
1346 1402 */
1347 1403 pn_free(&rpn);
1348 1404 pn_free(&pn);
1349 1405 goto errout;
1350 1406 }
1351 1407 } else {
1352 1408 pnp = &pn;
1353 1409 }
1354 1410 mountpt = kmem_alloc(pnp->pn_pathlen + 1, KM_SLEEP);
1355 1411 (void) strcpy(mountpt, pnp->pn_path);
1356 1412
1357 1413 /*
1358 1414 * If the addition of the zone's rootpath
1359 1415 * would push us over a total path length
1360 1416 * of MAXPATHLEN, we fail the mount with
1361 1417 * ENAMETOOLONG, which is what we would have
1362 1418 * gotten if we were trying to perform the same
1363 1419 * mount in the global zone.
1364 1420 *
1365 1421 * strlen() doesn't count the trailing
1366 1422 * '\0', but zone_rootpathlen counts both a
1367 1423 * trailing '/' and the terminating '\0'.
1368 1424 */
1369 1425 if ((curproc->p_zone->zone_rootpathlen - 1 +
1370 1426 strlen(mountpt)) > MAXPATHLEN ||
1371 1427 (resource != NULL &&
1372 1428 (curproc->p_zone->zone_rootpathlen - 1 +
1373 1429 strlen(resource)) > MAXPATHLEN)) {
1374 1430 error = ENAMETOOLONG;
1375 1431 }
1376 1432
1377 1433 pn_free(&rpn);
1378 1434 pn_free(&pn);
1379 1435 }
1380 1436
1381 1437 if (error)
1382 1438 goto errout;
1383 1439
1384 1440 /*
1385 1441 * Prevent path name resolution from proceeding past
1386 1442 * the mount point.
1387 1443 */
1388 1444 if (vn_vfswlock(vp) != 0) {
1389 1445 error = EBUSY;
1390 1446 goto errout;
1391 1447 }
1392 1448
1393 1449 /*
1394 1450 * Verify that it's legitimate to establish a mount on
1395 1451 * the prospective mount point.
1396 1452 */
1397 1453 if (vn_mountedvfs(vp) != NULL) {
1398 1454 /*
1399 1455 * The mount point lock was obtained after some
1400 1456 * other thread raced through and established a mount.
1401 1457 */
1402 1458 vn_vfsunlock(vp);
1403 1459 error = EBUSY;
1404 1460 goto errout;
1405 1461 }
1406 1462 if (vp->v_flag & VNOMOUNT) {
1407 1463 vn_vfsunlock(vp);
1408 1464 error = EINVAL;
1409 1465 goto errout;
1410 1466 }
1411 1467 }
1412 1468 if ((uap->flags & (MS_DATA | MS_OPTIONSTR)) == 0) {
1413 1469 uap->dataptr = NULL;
1414 1470 uap->datalen = 0;
1415 1471 }
1416 1472
1417 1473 /*
1418 1474 * If this is a remount, we don't want to create a new VFS.
1419 1475 * Instead, we pass the existing one with a remount flag.
1420 1476 */
1421 1477 if (remount) {
1422 1478 /*
1423 1479 * Confirm that the mount point is the root vnode of the
1424 1480 * file system that is being remounted.
1425 1481 * This can happen if the user specifies a different
1426 1482 * mount point directory pathname in the (re)mount command.
1427 1483 *
1428 1484 * Code below can only be reached if splice is true, so it's
1429 1485 * safe to do vn_vfsunlock() here.
1430 1486 */
1431 1487 if ((vp->v_flag & VROOT) == 0) {
1432 1488 vn_vfsunlock(vp);
1433 1489 error = ENOENT;
1434 1490 goto errout;
1435 1491 }
1436 1492 /*
1437 1493 * Disallow making file systems read-only unless file system
1438 1494 * explicitly allows it in its vfssw. Ignore other flags.
1439 1495 */
1440 1496 if (rdonly && vn_is_readonly(vp) == 0 &&
1441 1497 (vswp->vsw_flag & VSW_CANRWRO) == 0) {
1442 1498 vn_vfsunlock(vp);
1443 1499 error = EINVAL;
1444 1500 goto errout;
1445 1501 }
1446 1502 /*
1447 1503 * Disallow changing the NBMAND disposition of the file
1448 1504 * system on remounts.
1449 1505 */
1450 1506 if ((nbmand && ((vp->v_vfsp->vfs_flag & VFS_NBMAND) == 0)) ||
1451 1507 (!nbmand && (vp->v_vfsp->vfs_flag & VFS_NBMAND))) {
1452 1508 vn_vfsunlock(vp);
1453 1509 error = EINVAL;
1454 1510 goto errout;
1455 1511 }
1456 1512 vfsp = vp->v_vfsp;
1457 1513 ovflags = vfsp->vfs_flag;
1458 1514 vfsp->vfs_flag |= VFS_REMOUNT;
1459 1515 vfsp->vfs_flag &= ~VFS_RDONLY;
1460 1516 } else {
1461 1517 vfsp = vfs_alloc(KM_SLEEP);
1462 1518 VFS_INIT(vfsp, vfsops, NULL);
1463 1519 }
1464 1520
1465 1521 VFS_HOLD(vfsp);
1466 1522
1467 1523 if ((error = lofi_add(fsname, vfsp, &mnt_mntopts, uap)) != 0) {
1468 1524 if (!remount) {
1469 1525 if (splice)
1470 1526 vn_vfsunlock(vp);
1471 1527 vfs_free(vfsp);
1472 1528 } else {
1473 1529 vn_vfsunlock(vp);
1474 1530 VFS_RELE(vfsp);
1475 1531 }
1476 1532 goto errout;
1477 1533 }
1478 1534
1479 1535 /*
1480 1536 * PRIV_SYS_MOUNT doesn't mean you can become root.
1481 1537 */
1482 1538 if (vfsp->vfs_lofi_minor != 0) {
1483 1539 uap->flags |= MS_NOSUID;
1484 1540 vfs_setmntopt_nolock(&mnt_mntopts, MNTOPT_NOSUID, NULL, 0, 0);
1485 1541 }
1486 1542
1487 1543 /*
1488 1544 * The vfs_reflock is not used anymore the code below explicitly
1489 1545 * holds it preventing others accesing it directly.
1490 1546 */
1491 1547 if ((sema_tryp(&vfsp->vfs_reflock) == 0) &&
1492 1548 !(vfsp->vfs_flag & VFS_REMOUNT))
1493 1549 cmn_err(CE_WARN,
1494 1550 "mount type %s couldn't get vfs_reflock", vswp->vsw_name);
1495 1551
1496 1552 /*
1497 1553 * Lock the vfs. If this is a remount we want to avoid spurious umount
1498 1554 * failures that happen as a side-effect of fsflush() and other mount
1499 1555 * and unmount operations that might be going on simultaneously and
1500 1556 * may have locked the vfs currently. To not return EBUSY immediately
1501 1557 * here we use vfs_lock_wait() instead vfs_lock() for the remount case.
1502 1558 */
1503 1559 if (!remount) {
1504 1560 if (error = vfs_lock(vfsp)) {
1505 1561 vfsp->vfs_flag = ovflags;
1506 1562
1507 1563 lofi_remove(vfsp);
1508 1564
1509 1565 if (splice)
1510 1566 vn_vfsunlock(vp);
1511 1567 vfs_free(vfsp);
1512 1568 goto errout;
1513 1569 }
1514 1570 } else {
1515 1571 vfs_lock_wait(vfsp);
1516 1572 }
1517 1573
1518 1574 /*
1519 1575 * Add device to mount in progress table, global mounts require special
1520 1576 * handling. It is possible that we have already done the lookupname
1521 1577 * on a spliced, non-global fs. If so, we don't want to do it again
1522 1578 * since we cannot do a lookupname after taking the
1523 1579 * wlock above. This case is for a non-spliced, non-global filesystem.
1524 1580 */
1525 1581 if (!addmip) {
1526 1582 if ((uap->flags & MS_GLOBAL) == 0 &&
1527 1583 lookupname(uap->spec, fromspace, FOLLOW, NULL, &bvp) == 0) {
1528 1584 addmip = 1;
1529 1585 }
1530 1586 }
1531 1587
1532 1588 if (addmip) {
1533 1589 vnode_t *lvp = NULL;
1534 1590
1535 1591 error = vfs_get_lofi(vfsp, &lvp);
1536 1592 if (error > 0) {
1537 1593 lofi_remove(vfsp);
1538 1594
1539 1595 if (splice)
1540 1596 vn_vfsunlock(vp);
1541 1597 vfs_unlock(vfsp);
1542 1598
1543 1599 if (remount) {
1544 1600 VFS_RELE(vfsp);
1545 1601 } else {
1546 1602 vfs_free(vfsp);
1547 1603 }
1548 1604
1549 1605 goto errout;
1550 1606 } else if (error == -1) {
1551 1607 bdev = bvp->v_rdev;
1552 1608 VN_RELE(bvp);
1553 1609 } else {
1554 1610 bdev = lvp->v_rdev;
1555 1611 VN_RELE(lvp);
1556 1612 VN_RELE(bvp);
1557 1613 }
1558 1614
1559 1615 vfs_addmip(bdev, vfsp);
1560 1616 addmip = 0;
1561 1617 delmip = 1;
1562 1618 }
1563 1619 /*
1564 1620 * Invalidate cached entry for the mount point.
1565 1621 */
1566 1622 if (splice)
1567 1623 dnlc_purge_vp(vp);
1568 1624
1569 1625 /*
1570 1626 * If have an option string but the filesystem doesn't supply a
1571 1627 * prototype options table, create a table with the global
1572 1628 * options and sufficient room to accept all the options in the
1573 1629 * string. Then parse the passed in option string
1574 1630 * accepting all the options in the string. This gives us an
1575 1631 * option table with all the proper cancel properties for the
1576 1632 * global options.
1577 1633 *
1578 1634 * Filesystems that supply a prototype options table are handled
1579 1635 * earlier in this function.
1580 1636 */
1581 1637 if (uap->flags & MS_OPTIONSTR) {
1582 1638 if (!(vswp->vsw_flag & VSW_HASPROTO)) {
1583 1639 mntopts_t tmp_mntopts;
1584 1640
1585 1641 tmp_mntopts.mo_count = 0;
1586 1642 vfs_createopttbl_extend(&tmp_mntopts, inargs,
1587 1643 &mnt_mntopts);
1588 1644 vfs_parsemntopts(&tmp_mntopts, inargs, 1);
1589 1645 vfs_swapopttbl_nolock(&mnt_mntopts, &tmp_mntopts);
1590 1646 vfs_freeopttbl(&tmp_mntopts);
1591 1647 }
1592 1648 }
1593 1649
1594 1650 /*
1595 1651 * Serialize with zone state transitions.
1596 1652 * See vfs_list_add; zone mounted into is:
1597 1653 * zone_find_by_path(refstr_value(vfsp->vfs_mntpt))
1598 1654 * not the zone doing the mount (curproc->p_zone), but if we're already
1599 1655 * inside a NGZ, then we know what zone we are.
1600 1656 */
1601 1657 if (INGLOBALZONE(curproc)) {
1602 1658 zone = zone_find_by_path(mountpt);
1603 1659 ASSERT(zone != NULL);
1604 1660 } else {
1605 1661 zone = curproc->p_zone;
1606 1662 /*
1607 1663 * zone_find_by_path does a hold, so do one here too so that
1608 1664 * we can do a zone_rele after mount_completed.
1609 1665 */
1610 1666 zone_hold(zone);
1611 1667 }
1612 1668 mount_in_progress(zone);
1613 1669 /*
1614 1670 * Instantiate (or reinstantiate) the file system. If appropriate,
1615 1671 * splice it into the file system name space.
1616 1672 *
1617 1673 * We want VFS_MOUNT() to be able to override the vfs_resource
1618 1674 * string if necessary (ie, mntfs), and also for a remount to
1619 1675 * change the same (necessary when remounting '/' during boot).
1620 1676 * So we set up vfs_mntpt and vfs_resource to what we think they
1621 1677 * should be, then hand off control to VFS_MOUNT() which can
1622 1678 * override this.
1623 1679 *
1624 1680 * For safety's sake, when changing vfs_resource or vfs_mntpt of
1625 1681 * a vfs which is on the vfs list (i.e. during a remount), we must
1626 1682 * never set those fields to NULL. Several bits of code make
1627 1683 * assumptions that the fields are always valid.
1628 1684 */
1629 1685 vfs_swapopttbl(&mnt_mntopts, &vfsp->vfs_mntopts);
1630 1686 if (remount) {
1631 1687 if ((oldresource = vfsp->vfs_resource) != NULL)
1632 1688 refstr_hold(oldresource);
1633 1689 if ((oldmntpt = vfsp->vfs_mntpt) != NULL)
1634 1690 refstr_hold(oldmntpt);
1635 1691 }
1636 1692 vfs_setresource(vfsp, resource, 0);
1637 1693 vfs_setmntpoint(vfsp, mountpt, 0);
1638 1694
1639 1695 /*
1640 1696 * going to mount on this vnode, so notify.
1641 1697 */
1642 1698 vnevent_mountedover(vp, NULL);
1643 1699 error = VFS_MOUNT(vfsp, vp, uap, credp);
1644 1700
1645 1701 if (uap->flags & MS_RDONLY)
1646 1702 vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1647 1703 if (uap->flags & MS_NOSUID)
1648 1704 vfs_setmntopt(vfsp, MNTOPT_NOSUID, NULL, 0);
1649 1705 if (uap->flags & MS_GLOBAL)
1650 1706 vfs_setmntopt(vfsp, MNTOPT_GLOBAL, NULL, 0);
1651 1707
1652 1708 if (error) {
1653 1709 lofi_remove(vfsp);
1654 1710
1655 1711 if (remount) {
1656 1712 /* put back pre-remount options */
1657 1713 vfs_swapopttbl(&mnt_mntopts, &vfsp->vfs_mntopts);
1658 1714 vfs_setmntpoint(vfsp, refstr_value(oldmntpt),
1659 1715 VFSSP_VERBATIM);
1660 1716 if (oldmntpt)
1661 1717 refstr_rele(oldmntpt);
1662 1718 vfs_setresource(vfsp, refstr_value(oldresource),
1663 1719 VFSSP_VERBATIM);
1664 1720 if (oldresource)
1665 1721 refstr_rele(oldresource);
1666 1722 vfsp->vfs_flag = ovflags;
1667 1723 vfs_unlock(vfsp);
1668 1724 VFS_RELE(vfsp);
1669 1725 } else {
1670 1726 vfs_unlock(vfsp);
1671 1727 vfs_freemnttab(vfsp);
1672 1728 vfs_free(vfsp);
1673 1729 }
1674 1730 } else {
1675 1731 /*
1676 1732 * Set the mount time to now
1677 1733 */
1678 1734 vfsp->vfs_mtime = ddi_get_time();
1679 1735 if (remount) {
1680 1736 vfsp->vfs_flag &= ~VFS_REMOUNT;
1681 1737 if (oldresource)
1682 1738 refstr_rele(oldresource);
1683 1739 if (oldmntpt)
1684 1740 refstr_rele(oldmntpt);
1685 1741 } else if (splice) {
1686 1742 /*
1687 1743 * Link vfsp into the name space at the mount
1688 1744 * point. Vfs_add() is responsible for
1689 1745 * holding the mount point which will be
1690 1746 * released when vfs_remove() is called.
1691 1747 */
1692 1748 vfs_add(vp, vfsp, uap->flags);
1693 1749 } else {
1694 1750 /*
1695 1751 * Hold the reference to file system which is
1696 1752 * not linked into the name space.
1697 1753 */
1698 1754 vfsp->vfs_zone = NULL;
1699 1755 VFS_HOLD(vfsp);
1700 1756 vfsp->vfs_vnodecovered = NULL;
1701 1757 }
1702 1758 /*
1703 1759 * Set flags for global options encountered
1704 1760 */
1705 1761 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL))
1706 1762 vfsp->vfs_flag |= VFS_RDONLY;
1707 1763 else
1708 1764 vfsp->vfs_flag &= ~VFS_RDONLY;
1709 1765 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
1710 1766 vfsp->vfs_flag |= (VFS_NOSETUID|VFS_NODEVICES);
1711 1767 } else {
1712 1768 if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
1713 1769 vfsp->vfs_flag |= VFS_NODEVICES;
1714 1770 else
1715 1771 vfsp->vfs_flag &= ~VFS_NODEVICES;
1716 1772 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
1717 1773 vfsp->vfs_flag |= VFS_NOSETUID;
1718 1774 else
1719 1775 vfsp->vfs_flag &= ~VFS_NOSETUID;
1720 1776 }
1721 1777 if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL))
1722 1778 vfsp->vfs_flag |= VFS_NBMAND;
1723 1779 else
1724 1780 vfsp->vfs_flag &= ~VFS_NBMAND;
1725 1781
1726 1782 if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL))
1727 1783 vfsp->vfs_flag |= VFS_XATTR;
1728 1784 else
1729 1785 vfsp->vfs_flag &= ~VFS_XATTR;
1730 1786
1731 1787 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL))
1732 1788 vfsp->vfs_flag |= VFS_NOEXEC;
1733 1789 else
1734 1790 vfsp->vfs_flag &= ~VFS_NOEXEC;
1735 1791
1736 1792 /*
1737 1793 * Now construct the output option string of options
1738 1794 * we recognized.
1739 1795 */
1740 1796 if (uap->flags & MS_OPTIONSTR) {
1741 1797 vfs_list_read_lock();
1742 1798 copyout_error = vfs_buildoptionstr(
1743 1799 &vfsp->vfs_mntopts, inargs, optlen);
1744 1800 vfs_list_unlock();
1745 1801 if (copyout_error == 0 &&
1746 1802 (uap->flags & MS_SYSSPACE) == 0) {
1747 1803 copyout_error = copyoutstr(inargs, opts,
1748 1804 optlen, NULL);
1749 1805 }
1750 1806 }
1751 1807
1752 1808 /*
1753 1809 * If this isn't a remount, set up the vopstats before
1754 1810 * anyone can touch this. We only allow spliced file
1755 1811 * systems (file systems which are in the namespace) to
1756 1812 * have the VFS_STATS flag set.
1757 1813 * NOTE: PxFS mounts the underlying file system with
1758 1814 * MS_NOSPLICE set and copies those vfs_flags to its private
1759 1815 * vfs structure. As a result, PxFS should never have
1760 1816 * the VFS_STATS flag or else we might access the vfs
1761 1817 * statistics-related fields prior to them being
1762 1818 * properly initialized.
1763 1819 */
1764 1820 if (!remount && (vswp->vsw_flag & VSW_STATS) && splice) {
1765 1821 initialize_vopstats(&vfsp->vfs_vopstats);
1766 1822 /*
1767 1823 * We need to set vfs_vskap to NULL because there's
1768 1824 * a chance it won't be set below. This is checked
1769 1825 * in teardown_vopstats() so we can't have garbage.
1770 1826 */
1771 1827 vfsp->vfs_vskap = NULL;
1772 1828 vfsp->vfs_flag |= VFS_STATS;
1773 1829 vfsp->vfs_fstypevsp = get_fstype_vopstats(vfsp, vswp);
1774 1830 }
1775 1831
1776 1832 if (vswp->vsw_flag & VSW_XID)
1777 1833 vfsp->vfs_flag |= VFS_XID;
1778 1834
1779 1835 vfs_unlock(vfsp);
1780 1836 }
1781 1837 mount_completed(zone);
1782 1838 zone_rele(zone);
1783 1839 if (splice)
1784 1840 vn_vfsunlock(vp);
1785 1841
1786 1842 if ((error == 0) && (copyout_error == 0)) {
1787 1843 if (!remount) {
1788 1844 /*
1789 1845 * Don't call get_vskstat_anchor() while holding
1790 1846 * locks since it allocates memory and calls
1791 1847 * VFS_STATVFS(). For NFS, the latter can generate
1792 1848 * an over-the-wire call.
1793 1849 */
1794 1850 vskap = get_vskstat_anchor(vfsp);
1795 1851 /* Only take the lock if we have something to do */
1796 1852 if (vskap != NULL) {
1797 1853 vfs_lock_wait(vfsp);
1798 1854 if (vfsp->vfs_flag & VFS_STATS) {
1799 1855 vfsp->vfs_vskap = vskap;
1800 1856 }
1801 1857 vfs_unlock(vfsp);
1802 1858 }
1803 1859 }
1804 1860 /* Return vfsp to caller. */
1805 1861 *vfspp = vfsp;
1806 1862 }
1807 1863 errout:
1808 1864 vfs_freeopttbl(&mnt_mntopts);
1809 1865 if (resource != NULL)
1810 1866 kmem_free(resource, strlen(resource) + 1);
1811 1867 if (mountpt != NULL)
1812 1868 kmem_free(mountpt, strlen(mountpt) + 1);
1813 1869 /*
1814 1870 * It is possible we errored prior to adding to mount in progress
1815 1871 * table. Must free vnode we acquired with successful lookupname.
1816 1872 */
1817 1873 if (addmip)
1818 1874 VN_RELE(bvp);
1819 1875 if (delmip)
1820 1876 vfs_delmip(vfsp);
1821 1877 ASSERT(vswp != NULL);
1822 1878 vfs_unrefvfssw(vswp);
1823 1879 if (inargs != opts)
1824 1880 kmem_free(inargs, MAX_MNTOPT_STR);
1825 1881 if (copyout_error) {
1826 1882 lofi_remove(vfsp);
1827 1883 VFS_RELE(vfsp);
1828 1884 error = copyout_error;
1829 1885 }
1830 1886 return (error);
1831 1887 }
1832 1888
1833 1889 static void
1834 1890 vfs_setpath(
1835 1891 struct vfs *vfsp, /* vfs being updated */
1836 1892 refstr_t **refp, /* Ref-count string to contain the new path */
1837 1893 const char *newpath, /* Path to add to refp (above) */
1838 1894 uint32_t flag) /* flag */
1839 1895 {
1840 1896 size_t len;
1841 1897 refstr_t *ref;
1842 1898 zone_t *zone = curproc->p_zone;
1843 1899 char *sp;
1844 1900 int have_list_lock = 0;
1845 1901
1846 1902 ASSERT(!VFS_ON_LIST(vfsp) || vfs_lock_held(vfsp));
1847 1903
1848 1904 /*
1849 1905 * New path must be less than MAXPATHLEN because mntfs
1850 1906 * will only display up to MAXPATHLEN bytes. This is currently
1851 1907 * safe, because domount() uses pn_get(), and other callers
1852 1908 * similarly cap the size to fewer than MAXPATHLEN bytes.
1853 1909 */
1854 1910
1855 1911 ASSERT(strlen(newpath) < MAXPATHLEN);
1856 1912
1857 1913 /* mntfs requires consistency while vfs list lock is held */
1858 1914
1859 1915 if (VFS_ON_LIST(vfsp)) {
1860 1916 have_list_lock = 1;
1861 1917 vfs_list_lock();
1862 1918 }
1863 1919
1864 1920 if (*refp != NULL)
1865 1921 refstr_rele(*refp);
1866 1922
1867 1923 /*
1868 1924 * If we are in a non-global zone then we prefix the supplied path,
1869 1925 * newpath, with the zone's root path, with two exceptions. The first
1870 1926 * is where we have been explicitly directed to avoid doing so; this
1871 1927 * will be the case following a failed remount, where the path supplied
1872 1928 * will be a saved version which must now be restored. The second
1873 1929 * exception is where newpath is not a pathname but a descriptive name,
1874 1930 * e.g. "procfs".
1875 1931 */
1876 1932 if (zone == global_zone || (flag & VFSSP_VERBATIM) || *newpath != '/') {
1877 1933 ref = refstr_alloc(newpath);
1878 1934 goto out;
1879 1935 }
1880 1936
1881 1937 /*
1882 1938 * Truncate the trailing '/' in the zoneroot, and merge
1883 1939 * in the zone's rootpath with the "newpath" (resource
1884 1940 * or mountpoint) passed in.
1885 1941 *
1886 1942 * The size of the required buffer is thus the size of
1887 1943 * the buffer required for the passed-in newpath
1888 1944 * (strlen(newpath) + 1), plus the size of the buffer
1889 1945 * required to hold zone_rootpath (zone_rootpathlen)
1890 1946 * minus one for one of the now-superfluous NUL
1891 1947 * terminations, minus one for the trailing '/'.
1892 1948 *
1893 1949 * That gives us:
1894 1950 *
1895 1951 * (strlen(newpath) + 1) + zone_rootpathlen - 1 - 1
1896 1952 *
1897 1953 * Which is what we have below.
1898 1954 */
1899 1955
1900 1956 len = strlen(newpath) + zone->zone_rootpathlen - 1;
1901 1957 sp = kmem_alloc(len, KM_SLEEP);
1902 1958
1903 1959 /*
1904 1960 * Copy everything including the trailing slash, which
1905 1961 * we then overwrite with the NUL character.
1906 1962 */
1907 1963
1908 1964 (void) strcpy(sp, zone->zone_rootpath);
1909 1965 sp[zone->zone_rootpathlen - 2] = '\0';
1910 1966 (void) strcat(sp, newpath);
1911 1967
1912 1968 ref = refstr_alloc(sp);
1913 1969 kmem_free(sp, len);
1914 1970 out:
1915 1971 *refp = ref;
1916 1972
1917 1973 if (have_list_lock) {
1918 1974 vfs_mnttab_modtimeupd();
1919 1975 vfs_list_unlock();
1920 1976 }
1921 1977 }
1922 1978
1923 1979 /*
1924 1980 * Record a mounted resource name in a vfs structure.
1925 1981 * If vfsp is already mounted, caller must hold the vfs lock.
1926 1982 */
1927 1983 void
1928 1984 vfs_setresource(struct vfs *vfsp, const char *resource, uint32_t flag)
1929 1985 {
1930 1986 if (resource == NULL || resource[0] == '\0')
1931 1987 resource = VFS_NORESOURCE;
1932 1988 vfs_setpath(vfsp, &vfsp->vfs_resource, resource, flag);
1933 1989 }
1934 1990
1935 1991 /*
1936 1992 * Record a mount point name in a vfs structure.
1937 1993 * If vfsp is already mounted, caller must hold the vfs lock.
1938 1994 */
1939 1995 void
1940 1996 vfs_setmntpoint(struct vfs *vfsp, const char *mntpt, uint32_t flag)
1941 1997 {
1942 1998 if (mntpt == NULL || mntpt[0] == '\0')
1943 1999 mntpt = VFS_NOMNTPT;
1944 2000 vfs_setpath(vfsp, &vfsp->vfs_mntpt, mntpt, flag);
1945 2001 }
1946 2002
1947 2003 /* Returns the vfs_resource. Caller must call refstr_rele() when finished. */
1948 2004
1949 2005 refstr_t *
1950 2006 vfs_getresource(const struct vfs *vfsp)
1951 2007 {
1952 2008 refstr_t *resource;
1953 2009
1954 2010 vfs_list_read_lock();
1955 2011 resource = vfsp->vfs_resource;
1956 2012 refstr_hold(resource);
1957 2013 vfs_list_unlock();
1958 2014
1959 2015 return (resource);
1960 2016 }
1961 2017
1962 2018 /* Returns the vfs_mntpt. Caller must call refstr_rele() when finished. */
1963 2019
1964 2020 refstr_t *
1965 2021 vfs_getmntpoint(const struct vfs *vfsp)
1966 2022 {
1967 2023 refstr_t *mntpt;
1968 2024
1969 2025 vfs_list_read_lock();
1970 2026 mntpt = vfsp->vfs_mntpt;
1971 2027 refstr_hold(mntpt);
1972 2028 vfs_list_unlock();
1973 2029
1974 2030 return (mntpt);
1975 2031 }
1976 2032
1977 2033 /*
1978 2034 * Create an empty options table with enough empty slots to hold all
1979 2035 * The options in the options string passed as an argument.
1980 2036 * Potentially prepend another options table.
1981 2037 *
1982 2038 * Note: caller is responsible for locking the vfs list, if needed,
1983 2039 * to protect mops.
1984 2040 */
1985 2041 static void
1986 2042 vfs_createopttbl_extend(mntopts_t *mops, const char *opts,
1987 2043 const mntopts_t *mtmpl)
1988 2044 {
1989 2045 const char *s = opts;
1990 2046 uint_t count;
1991 2047
1992 2048 if (opts == NULL || *opts == '\0') {
1993 2049 count = 0;
1994 2050 } else {
1995 2051 count = 1;
1996 2052
1997 2053 /*
1998 2054 * Count number of options in the string
1999 2055 */
2000 2056 for (s = strchr(s, ','); s != NULL; s = strchr(s, ',')) {
2001 2057 count++;
2002 2058 s++;
2003 2059 }
2004 2060 }
2005 2061 vfs_copyopttbl_extend(mtmpl, mops, count);
2006 2062 }
2007 2063
2008 2064 /*
2009 2065 * Create an empty options table with enough empty slots to hold all
2010 2066 * The options in the options string passed as an argument.
2011 2067 *
2012 2068 * This function is *not* for general use by filesystems.
2013 2069 *
2014 2070 * Note: caller is responsible for locking the vfs list, if needed,
2015 2071 * to protect mops.
2016 2072 */
2017 2073 void
2018 2074 vfs_createopttbl(mntopts_t *mops, const char *opts)
2019 2075 {
2020 2076 vfs_createopttbl_extend(mops, opts, NULL);
2021 2077 }
2022 2078
2023 2079
2024 2080 /*
2025 2081 * Swap two mount options tables
2026 2082 */
2027 2083 static void
2028 2084 vfs_swapopttbl_nolock(mntopts_t *optbl1, mntopts_t *optbl2)
2029 2085 {
2030 2086 uint_t tmpcnt;
2031 2087 mntopt_t *tmplist;
2032 2088
2033 2089 tmpcnt = optbl2->mo_count;
2034 2090 tmplist = optbl2->mo_list;
2035 2091 optbl2->mo_count = optbl1->mo_count;
2036 2092 optbl2->mo_list = optbl1->mo_list;
2037 2093 optbl1->mo_count = tmpcnt;
2038 2094 optbl1->mo_list = tmplist;
2039 2095 }
2040 2096
2041 2097 static void
2042 2098 vfs_swapopttbl(mntopts_t *optbl1, mntopts_t *optbl2)
2043 2099 {
2044 2100 vfs_list_lock();
2045 2101 vfs_swapopttbl_nolock(optbl1, optbl2);
2046 2102 vfs_mnttab_modtimeupd();
2047 2103 vfs_list_unlock();
2048 2104 }
2049 2105
2050 2106 static char **
2051 2107 vfs_copycancelopt_extend(char **const moc, int extend)
2052 2108 {
2053 2109 int i = 0;
2054 2110 int j;
2055 2111 char **result;
2056 2112
2057 2113 if (moc != NULL) {
2058 2114 for (; moc[i] != NULL; i++)
2059 2115 /* count number of options to cancel */;
2060 2116 }
2061 2117
2062 2118 if (i + extend == 0)
2063 2119 return (NULL);
2064 2120
2065 2121 result = kmem_alloc((i + extend + 1) * sizeof (char *), KM_SLEEP);
2066 2122
2067 2123 for (j = 0; j < i; j++) {
2068 2124 result[j] = kmem_alloc(strlen(moc[j]) + 1, KM_SLEEP);
2069 2125 (void) strcpy(result[j], moc[j]);
2070 2126 }
2071 2127 for (; j <= i + extend; j++)
2072 2128 result[j] = NULL;
2073 2129
2074 2130 return (result);
2075 2131 }
2076 2132
2077 2133 static void
2078 2134 vfs_copyopt(const mntopt_t *s, mntopt_t *d)
2079 2135 {
2080 2136 char *sp, *dp;
2081 2137
2082 2138 d->mo_flags = s->mo_flags;
2083 2139 d->mo_data = s->mo_data;
2084 2140 sp = s->mo_name;
2085 2141 if (sp != NULL) {
2086 2142 dp = kmem_alloc(strlen(sp) + 1, KM_SLEEP);
2087 2143 (void) strcpy(dp, sp);
2088 2144 d->mo_name = dp;
2089 2145 } else {
2090 2146 d->mo_name = NULL; /* should never happen */
2091 2147 }
2092 2148
2093 2149 d->mo_cancel = vfs_copycancelopt_extend(s->mo_cancel, 0);
2094 2150
2095 2151 sp = s->mo_arg;
2096 2152 if (sp != NULL) {
2097 2153 dp = kmem_alloc(strlen(sp) + 1, KM_SLEEP);
2098 2154 (void) strcpy(dp, sp);
2099 2155 d->mo_arg = dp;
2100 2156 } else {
2101 2157 d->mo_arg = NULL;
2102 2158 }
2103 2159 }
2104 2160
2105 2161 /*
2106 2162 * Copy a mount options table, possibly allocating some spare
2107 2163 * slots at the end. It is permissible to copy_extend the NULL table.
2108 2164 */
2109 2165 static void
2110 2166 vfs_copyopttbl_extend(const mntopts_t *smo, mntopts_t *dmo, int extra)
2111 2167 {
2112 2168 uint_t i, count;
2113 2169 mntopt_t *motbl;
2114 2170
2115 2171 /*
2116 2172 * Clear out any existing stuff in the options table being initialized
2117 2173 */
2118 2174 vfs_freeopttbl(dmo);
2119 2175 count = (smo == NULL) ? 0 : smo->mo_count;
2120 2176 if ((count + extra) == 0) /* nothing to do */
2121 2177 return;
2122 2178 dmo->mo_count = count + extra;
2123 2179 motbl = kmem_zalloc((count + extra) * sizeof (mntopt_t), KM_SLEEP);
2124 2180 dmo->mo_list = motbl;
2125 2181 for (i = 0; i < count; i++) {
2126 2182 vfs_copyopt(&smo->mo_list[i], &motbl[i]);
2127 2183 }
2128 2184 for (i = count; i < count + extra; i++) {
2129 2185 motbl[i].mo_flags = MO_EMPTY;
2130 2186 }
2131 2187 }
2132 2188
2133 2189 /*
2134 2190 * Copy a mount options table.
2135 2191 *
2136 2192 * This function is *not* for general use by filesystems.
2137 2193 *
2138 2194 * Note: caller is responsible for locking the vfs list, if needed,
2139 2195 * to protect smo and dmo.
2140 2196 */
2141 2197 void
2142 2198 vfs_copyopttbl(const mntopts_t *smo, mntopts_t *dmo)
2143 2199 {
2144 2200 vfs_copyopttbl_extend(smo, dmo, 0);
2145 2201 }
2146 2202
2147 2203 static char **
2148 2204 vfs_mergecancelopts(const mntopt_t *mop1, const mntopt_t *mop2)
2149 2205 {
2150 2206 int c1 = 0;
2151 2207 int c2 = 0;
2152 2208 char **result;
2153 2209 char **sp1, **sp2, **dp;
2154 2210
2155 2211 /*
2156 2212 * First we count both lists of cancel options.
2157 2213 * If either is NULL or has no elements, we return a copy of
2158 2214 * the other.
2159 2215 */
2160 2216 if (mop1->mo_cancel != NULL) {
2161 2217 for (; mop1->mo_cancel[c1] != NULL; c1++)
2162 2218 /* count cancel options in mop1 */;
2163 2219 }
2164 2220
2165 2221 if (c1 == 0)
2166 2222 return (vfs_copycancelopt_extend(mop2->mo_cancel, 0));
2167 2223
2168 2224 if (mop2->mo_cancel != NULL) {
2169 2225 for (; mop2->mo_cancel[c2] != NULL; c2++)
2170 2226 /* count cancel options in mop2 */;
2171 2227 }
2172 2228
2173 2229 result = vfs_copycancelopt_extend(mop1->mo_cancel, c2);
2174 2230
2175 2231 if (c2 == 0)
2176 2232 return (result);
2177 2233
2178 2234 /*
2179 2235 * When we get here, we've got two sets of cancel options;
2180 2236 * we need to merge the two sets. We know that the result
2181 2237 * array has "c1+c2+1" entries and in the end we might shrink
2182 2238 * it.
2183 2239 * Result now has a copy of the c1 entries from mop1; we'll
2184 2240 * now lookup all the entries of mop2 in mop1 and copy it if
2185 2241 * it is unique.
2186 2242 * This operation is O(n^2) but it's only called once per
2187 2243 * filesystem per duplicate option. This is a situation
2188 2244 * which doesn't arise with the filesystems in ON and
2189 2245 * n is generally 1.
2190 2246 */
2191 2247
2192 2248 dp = &result[c1];
2193 2249 for (sp2 = mop2->mo_cancel; *sp2 != NULL; sp2++) {
2194 2250 for (sp1 = mop1->mo_cancel; *sp1 != NULL; sp1++) {
2195 2251 if (strcmp(*sp1, *sp2) == 0)
2196 2252 break;
2197 2253 }
2198 2254 if (*sp1 == NULL) {
2199 2255 /*
2200 2256 * Option *sp2 not found in mop1, so copy it.
2201 2257 * The calls to vfs_copycancelopt_extend()
2202 2258 * guarantee that there's enough room.
2203 2259 */
2204 2260 *dp = kmem_alloc(strlen(*sp2) + 1, KM_SLEEP);
2205 2261 (void) strcpy(*dp++, *sp2);
2206 2262 }
2207 2263 }
2208 2264 if (dp != &result[c1+c2]) {
2209 2265 size_t bytes = (dp - result + 1) * sizeof (char *);
2210 2266 char **nres = kmem_alloc(bytes, KM_SLEEP);
2211 2267
2212 2268 bcopy(result, nres, bytes);
2213 2269 kmem_free(result, (c1 + c2 + 1) * sizeof (char *));
2214 2270 result = nres;
2215 2271 }
2216 2272 return (result);
2217 2273 }
2218 2274
2219 2275 /*
2220 2276 * Merge two mount option tables (outer and inner) into one. This is very
2221 2277 * similar to "merging" global variables and automatic variables in C.
2222 2278 *
2223 2279 * This isn't (and doesn't have to be) fast.
2224 2280 *
2225 2281 * This function is *not* for general use by filesystems.
2226 2282 *
2227 2283 * Note: caller is responsible for locking the vfs list, if needed,
2228 2284 * to protect omo, imo & dmo.
2229 2285 */
2230 2286 void
2231 2287 vfs_mergeopttbl(const mntopts_t *omo, const mntopts_t *imo, mntopts_t *dmo)
2232 2288 {
2233 2289 uint_t i, count;
2234 2290 mntopt_t *mop, *motbl;
2235 2291 uint_t freeidx;
2236 2292
2237 2293 /*
2238 2294 * First determine how much space we need to allocate.
2239 2295 */
2240 2296 count = omo->mo_count;
2241 2297 for (i = 0; i < imo->mo_count; i++) {
2242 2298 if (imo->mo_list[i].mo_flags & MO_EMPTY)
2243 2299 continue;
2244 2300 if (vfs_hasopt(omo, imo->mo_list[i].mo_name) == NULL)
2245 2301 count++;
2246 2302 }
2247 2303 ASSERT(count >= omo->mo_count &&
2248 2304 count <= omo->mo_count + imo->mo_count);
2249 2305 motbl = kmem_alloc(count * sizeof (mntopt_t), KM_SLEEP);
2250 2306 for (i = 0; i < omo->mo_count; i++)
2251 2307 vfs_copyopt(&omo->mo_list[i], &motbl[i]);
2252 2308 freeidx = omo->mo_count;
2253 2309 for (i = 0; i < imo->mo_count; i++) {
2254 2310 if (imo->mo_list[i].mo_flags & MO_EMPTY)
2255 2311 continue;
2256 2312 if ((mop = vfs_hasopt(omo, imo->mo_list[i].mo_name)) != NULL) {
2257 2313 char **newcanp;
2258 2314 uint_t index = mop - omo->mo_list;
2259 2315
2260 2316 newcanp = vfs_mergecancelopts(mop, &motbl[index]);
2261 2317
2262 2318 vfs_freeopt(&motbl[index]);
2263 2319 vfs_copyopt(&imo->mo_list[i], &motbl[index]);
2264 2320
2265 2321 vfs_freecancelopt(motbl[index].mo_cancel);
2266 2322 motbl[index].mo_cancel = newcanp;
2267 2323 } else {
2268 2324 /*
2269 2325 * If it's a new option, just copy it over to the first
2270 2326 * free location.
2271 2327 */
2272 2328 vfs_copyopt(&imo->mo_list[i], &motbl[freeidx++]);
2273 2329 }
2274 2330 }
2275 2331 dmo->mo_count = count;
2276 2332 dmo->mo_list = motbl;
2277 2333 }
2278 2334
2279 2335 /*
2280 2336 * Functions to set and clear mount options in a mount options table.
2281 2337 */
2282 2338
2283 2339 /*
2284 2340 * Clear a mount option, if it exists.
2285 2341 *
2286 2342 * The update_mnttab arg indicates whether mops is part of a vfs that is on
2287 2343 * the vfs list.
2288 2344 */
2289 2345 static void
2290 2346 vfs_clearmntopt_nolock(mntopts_t *mops, const char *opt, int update_mnttab)
2291 2347 {
2292 2348 struct mntopt *mop;
2293 2349 uint_t i, count;
2294 2350
2295 2351 ASSERT(!update_mnttab || RW_WRITE_HELD(&vfslist));
2296 2352
2297 2353 count = mops->mo_count;
2298 2354 for (i = 0; i < count; i++) {
2299 2355 mop = &mops->mo_list[i];
2300 2356
2301 2357 if (mop->mo_flags & MO_EMPTY)
2302 2358 continue;
2303 2359 if (strcmp(opt, mop->mo_name))
2304 2360 continue;
2305 2361 mop->mo_flags &= ~MO_SET;
2306 2362 if (mop->mo_arg != NULL) {
2307 2363 kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2308 2364 }
2309 2365 mop->mo_arg = NULL;
2310 2366 if (update_mnttab)
2311 2367 vfs_mnttab_modtimeupd();
2312 2368 break;
2313 2369 }
2314 2370 }
2315 2371
2316 2372 void
2317 2373 vfs_clearmntopt(struct vfs *vfsp, const char *opt)
2318 2374 {
2319 2375 int gotlock = 0;
2320 2376
2321 2377 if (VFS_ON_LIST(vfsp)) {
2322 2378 gotlock = 1;
2323 2379 vfs_list_lock();
2324 2380 }
2325 2381 vfs_clearmntopt_nolock(&vfsp->vfs_mntopts, opt, gotlock);
2326 2382 if (gotlock)
2327 2383 vfs_list_unlock();
2328 2384 }
2329 2385
2330 2386
2331 2387 /*
2332 2388 * Set a mount option on. If it's not found in the table, it's silently
2333 2389 * ignored. If the option has MO_IGNORE set, it is still set unless the
2334 2390 * VFS_NOFORCEOPT bit is set in the flags. Also, VFS_DISPLAY/VFS_NODISPLAY flag
2335 2391 * bits can be used to toggle the MO_NODISPLAY bit for the option.
2336 2392 * If the VFS_CREATEOPT flag bit is set then the first option slot with
2337 2393 * MO_EMPTY set is created as the option passed in.
2338 2394 *
2339 2395 * The update_mnttab arg indicates whether mops is part of a vfs that is on
2340 2396 * the vfs list.
2341 2397 */
2342 2398 static void
2343 2399 vfs_setmntopt_nolock(mntopts_t *mops, const char *opt,
2344 2400 const char *arg, int flags, int update_mnttab)
2345 2401 {
2346 2402 mntopt_t *mop;
2347 2403 uint_t i, count;
2348 2404 char *sp;
2349 2405
2350 2406 ASSERT(!update_mnttab || RW_WRITE_HELD(&vfslist));
2351 2407
2352 2408 if (flags & VFS_CREATEOPT) {
2353 2409 if (vfs_hasopt(mops, opt) != NULL) {
2354 2410 flags &= ~VFS_CREATEOPT;
2355 2411 }
2356 2412 }
2357 2413 count = mops->mo_count;
2358 2414 for (i = 0; i < count; i++) {
2359 2415 mop = &mops->mo_list[i];
2360 2416
2361 2417 if (mop->mo_flags & MO_EMPTY) {
2362 2418 if ((flags & VFS_CREATEOPT) == 0)
2363 2419 continue;
2364 2420 sp = kmem_alloc(strlen(opt) + 1, KM_SLEEP);
2365 2421 (void) strcpy(sp, opt);
2366 2422 mop->mo_name = sp;
2367 2423 if (arg != NULL)
2368 2424 mop->mo_flags = MO_HASVALUE;
2369 2425 else
2370 2426 mop->mo_flags = 0;
2371 2427 } else if (strcmp(opt, mop->mo_name)) {
2372 2428 continue;
2373 2429 }
2374 2430 if ((mop->mo_flags & MO_IGNORE) && (flags & VFS_NOFORCEOPT))
2375 2431 break;
2376 2432 if (arg != NULL && (mop->mo_flags & MO_HASVALUE) != 0) {
2377 2433 sp = kmem_alloc(strlen(arg) + 1, KM_SLEEP);
2378 2434 (void) strcpy(sp, arg);
2379 2435 } else {
2380 2436 sp = NULL;
2381 2437 }
2382 2438 if (mop->mo_arg != NULL)
2383 2439 kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2384 2440 mop->mo_arg = sp;
2385 2441 if (flags & VFS_DISPLAY)
2386 2442 mop->mo_flags &= ~MO_NODISPLAY;
2387 2443 if (flags & VFS_NODISPLAY)
2388 2444 mop->mo_flags |= MO_NODISPLAY;
2389 2445 mop->mo_flags |= MO_SET;
2390 2446 if (mop->mo_cancel != NULL) {
2391 2447 char **cp;
2392 2448
2393 2449 for (cp = mop->mo_cancel; *cp != NULL; cp++)
2394 2450 vfs_clearmntopt_nolock(mops, *cp, 0);
2395 2451 }
2396 2452 if (update_mnttab)
2397 2453 vfs_mnttab_modtimeupd();
2398 2454 break;
2399 2455 }
2400 2456 }
2401 2457
2402 2458 void
2403 2459 vfs_setmntopt(struct vfs *vfsp, const char *opt, const char *arg, int flags)
2404 2460 {
2405 2461 int gotlock = 0;
2406 2462
2407 2463 if (VFS_ON_LIST(vfsp)) {
2408 2464 gotlock = 1;
2409 2465 vfs_list_lock();
2410 2466 }
2411 2467 vfs_setmntopt_nolock(&vfsp->vfs_mntopts, opt, arg, flags, gotlock);
2412 2468 if (gotlock)
2413 2469 vfs_list_unlock();
2414 2470 }
2415 2471
2416 2472
2417 2473 /*
2418 2474 * Add a "tag" option to a mounted file system's options list.
2419 2475 *
2420 2476 * Note: caller is responsible for locking the vfs list, if needed,
2421 2477 * to protect mops.
2422 2478 */
2423 2479 static mntopt_t *
2424 2480 vfs_addtag(mntopts_t *mops, const char *tag)
2425 2481 {
2426 2482 uint_t count;
2427 2483 mntopt_t *mop, *motbl;
2428 2484
2429 2485 count = mops->mo_count + 1;
2430 2486 motbl = kmem_zalloc(count * sizeof (mntopt_t), KM_SLEEP);
2431 2487 if (mops->mo_count) {
2432 2488 size_t len = (count - 1) * sizeof (mntopt_t);
2433 2489
2434 2490 bcopy(mops->mo_list, motbl, len);
2435 2491 kmem_free(mops->mo_list, len);
2436 2492 }
2437 2493 mops->mo_count = count;
2438 2494 mops->mo_list = motbl;
2439 2495 mop = &motbl[count - 1];
2440 2496 mop->mo_flags = MO_TAG;
2441 2497 mop->mo_name = kmem_alloc(strlen(tag) + 1, KM_SLEEP);
2442 2498 (void) strcpy(mop->mo_name, tag);
2443 2499 return (mop);
2444 2500 }
2445 2501
2446 2502 /*
2447 2503 * Allow users to set arbitrary "tags" in a vfs's mount options.
2448 2504 * Broader use within the kernel is discouraged.
2449 2505 */
2450 2506 int
2451 2507 vfs_settag(uint_t major, uint_t minor, const char *mntpt, const char *tag,
2452 2508 cred_t *cr)
2453 2509 {
2454 2510 vfs_t *vfsp;
2455 2511 mntopts_t *mops;
2456 2512 mntopt_t *mop;
2457 2513 int found = 0;
2458 2514 dev_t dev = makedevice(major, minor);
2459 2515 int err = 0;
2460 2516 char *buf = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
2461 2517
2462 2518 /*
2463 2519 * Find the desired mounted file system
2464 2520 */
2465 2521 vfs_list_lock();
2466 2522 vfsp = rootvfs;
2467 2523 do {
2468 2524 if (vfsp->vfs_dev == dev &&
2469 2525 strcmp(mntpt, refstr_value(vfsp->vfs_mntpt)) == 0) {
2470 2526 found = 1;
2471 2527 break;
2472 2528 }
2473 2529 vfsp = vfsp->vfs_next;
2474 2530 } while (vfsp != rootvfs);
2475 2531
2476 2532 if (!found) {
2477 2533 err = EINVAL;
2478 2534 goto out;
2479 2535 }
2480 2536 err = secpolicy_fs_config(cr, vfsp);
2481 2537 if (err != 0)
2482 2538 goto out;
2483 2539
2484 2540 mops = &vfsp->vfs_mntopts;
2485 2541 /*
2486 2542 * Add tag if it doesn't already exist
2487 2543 */
2488 2544 if ((mop = vfs_hasopt(mops, tag)) == NULL) {
2489 2545 int len;
2490 2546
2491 2547 (void) vfs_buildoptionstr(mops, buf, MAX_MNTOPT_STR);
2492 2548 len = strlen(buf);
2493 2549 if (len + strlen(tag) + 2 > MAX_MNTOPT_STR) {
2494 2550 err = ENAMETOOLONG;
2495 2551 goto out;
2496 2552 }
2497 2553 mop = vfs_addtag(mops, tag);
2498 2554 }
2499 2555 if ((mop->mo_flags & MO_TAG) == 0) {
2500 2556 err = EINVAL;
2501 2557 goto out;
2502 2558 }
2503 2559 vfs_setmntopt_nolock(mops, tag, NULL, 0, 1);
2504 2560 out:
2505 2561 vfs_list_unlock();
2506 2562 kmem_free(buf, MAX_MNTOPT_STR);
2507 2563 return (err);
2508 2564 }
2509 2565
2510 2566 /*
2511 2567 * Allow users to remove arbitrary "tags" in a vfs's mount options.
2512 2568 * Broader use within the kernel is discouraged.
2513 2569 */
2514 2570 int
2515 2571 vfs_clrtag(uint_t major, uint_t minor, const char *mntpt, const char *tag,
2516 2572 cred_t *cr)
2517 2573 {
2518 2574 vfs_t *vfsp;
2519 2575 mntopt_t *mop;
2520 2576 int found = 0;
2521 2577 dev_t dev = makedevice(major, minor);
2522 2578 int err = 0;
2523 2579
2524 2580 /*
2525 2581 * Find the desired mounted file system
2526 2582 */
2527 2583 vfs_list_lock();
2528 2584 vfsp = rootvfs;
2529 2585 do {
2530 2586 if (vfsp->vfs_dev == dev &&
2531 2587 strcmp(mntpt, refstr_value(vfsp->vfs_mntpt)) == 0) {
2532 2588 found = 1;
2533 2589 break;
2534 2590 }
2535 2591 vfsp = vfsp->vfs_next;
2536 2592 } while (vfsp != rootvfs);
2537 2593
2538 2594 if (!found) {
2539 2595 err = EINVAL;
2540 2596 goto out;
2541 2597 }
2542 2598 err = secpolicy_fs_config(cr, vfsp);
2543 2599 if (err != 0)
2544 2600 goto out;
2545 2601
2546 2602 if ((mop = vfs_hasopt(&vfsp->vfs_mntopts, tag)) == NULL) {
2547 2603 err = EINVAL;
2548 2604 goto out;
2549 2605 }
2550 2606 if ((mop->mo_flags & MO_TAG) == 0) {
2551 2607 err = EINVAL;
2552 2608 goto out;
2553 2609 }
2554 2610 vfs_clearmntopt_nolock(&vfsp->vfs_mntopts, tag, 1);
2555 2611 out:
2556 2612 vfs_list_unlock();
2557 2613 return (err);
2558 2614 }
2559 2615
2560 2616 /*
2561 2617 * Function to parse an option string and fill in a mount options table.
2562 2618 * Unknown options are silently ignored. The input option string is modified
2563 2619 * by replacing separators with nulls. If the create flag is set, options
2564 2620 * not found in the table are just added on the fly. The table must have
2565 2621 * an option slot marked MO_EMPTY to add an option on the fly.
2566 2622 *
2567 2623 * This function is *not* for general use by filesystems.
2568 2624 *
2569 2625 * Note: caller is responsible for locking the vfs list, if needed,
2570 2626 * to protect mops..
2571 2627 */
2572 2628 void
2573 2629 vfs_parsemntopts(mntopts_t *mops, char *osp, int create)
2574 2630 {
2575 2631 char *s = osp, *p, *nextop, *valp, *cp, *ep;
2576 2632 int setflg = VFS_NOFORCEOPT;
2577 2633
2578 2634 if (osp == NULL)
2579 2635 return;
2580 2636 while (*s != '\0') {
2581 2637 p = strchr(s, ','); /* find next option */
2582 2638 if (p == NULL) {
2583 2639 cp = NULL;
2584 2640 p = s + strlen(s);
2585 2641 } else {
2586 2642 cp = p; /* save location of comma */
2587 2643 *p++ = '\0'; /* mark end and point to next option */
2588 2644 }
2589 2645 nextop = p;
2590 2646 p = strchr(s, '='); /* look for value */
2591 2647 if (p == NULL) {
2592 2648 valp = NULL; /* no value supplied */
2593 2649 } else {
2594 2650 ep = p; /* save location of equals */
2595 2651 *p++ = '\0'; /* end option and point to value */
2596 2652 valp = p;
2597 2653 }
2598 2654 /*
2599 2655 * set option into options table
2600 2656 */
2601 2657 if (create)
2602 2658 setflg |= VFS_CREATEOPT;
2603 2659 vfs_setmntopt_nolock(mops, s, valp, setflg, 0);
2604 2660 if (cp != NULL)
2605 2661 *cp = ','; /* restore the comma */
2606 2662 if (valp != NULL)
2607 2663 *ep = '='; /* restore the equals */
2608 2664 s = nextop;
2609 2665 }
2610 2666 }
2611 2667
2612 2668 /*
2613 2669 * Function to inquire if an option exists in a mount options table.
2614 2670 * Returns a pointer to the option if it exists, else NULL.
2615 2671 *
2616 2672 * This function is *not* for general use by filesystems.
2617 2673 *
2618 2674 * Note: caller is responsible for locking the vfs list, if needed,
2619 2675 * to protect mops.
2620 2676 */
2621 2677 struct mntopt *
2622 2678 vfs_hasopt(const mntopts_t *mops, const char *opt)
2623 2679 {
2624 2680 struct mntopt *mop;
2625 2681 uint_t i, count;
2626 2682
2627 2683 count = mops->mo_count;
2628 2684 for (i = 0; i < count; i++) {
2629 2685 mop = &mops->mo_list[i];
2630 2686
2631 2687 if (mop->mo_flags & MO_EMPTY)
2632 2688 continue;
2633 2689 if (strcmp(opt, mop->mo_name) == 0)
2634 2690 return (mop);
2635 2691 }
2636 2692 return (NULL);
2637 2693 }
2638 2694
2639 2695 /*
2640 2696 * Function to inquire if an option is set in a mount options table.
2641 2697 * Returns non-zero if set and fills in the arg pointer with a pointer to
2642 2698 * the argument string or NULL if there is no argument string.
2643 2699 */
2644 2700 static int
2645 2701 vfs_optionisset_nolock(const mntopts_t *mops, const char *opt, char **argp)
2646 2702 {
2647 2703 struct mntopt *mop;
2648 2704 uint_t i, count;
2649 2705
2650 2706 count = mops->mo_count;
2651 2707 for (i = 0; i < count; i++) {
2652 2708 mop = &mops->mo_list[i];
2653 2709
2654 2710 if (mop->mo_flags & MO_EMPTY)
2655 2711 continue;
2656 2712 if (strcmp(opt, mop->mo_name))
2657 2713 continue;
2658 2714 if ((mop->mo_flags & MO_SET) == 0)
2659 2715 return (0);
2660 2716 if (argp != NULL && (mop->mo_flags & MO_HASVALUE) != 0)
2661 2717 *argp = mop->mo_arg;
2662 2718 return (1);
2663 2719 }
2664 2720 return (0);
2665 2721 }
2666 2722
2667 2723
2668 2724 int
2669 2725 vfs_optionisset(const struct vfs *vfsp, const char *opt, char **argp)
2670 2726 {
2671 2727 int ret;
2672 2728
2673 2729 vfs_list_read_lock();
2674 2730 ret = vfs_optionisset_nolock(&vfsp->vfs_mntopts, opt, argp);
2675 2731 vfs_list_unlock();
2676 2732 return (ret);
2677 2733 }
2678 2734
2679 2735
2680 2736 /*
2681 2737 * Construct a comma separated string of the options set in the given
2682 2738 * mount table, return the string in the given buffer. Return non-zero if
2683 2739 * the buffer would overflow.
2684 2740 *
2685 2741 * This function is *not* for general use by filesystems.
2686 2742 *
2687 2743 * Note: caller is responsible for locking the vfs list, if needed,
2688 2744 * to protect mp.
2689 2745 */
2690 2746 int
2691 2747 vfs_buildoptionstr(const mntopts_t *mp, char *buf, int len)
2692 2748 {
2693 2749 char *cp;
2694 2750 uint_t i;
2695 2751
2696 2752 buf[0] = '\0';
2697 2753 cp = buf;
2698 2754 for (i = 0; i < mp->mo_count; i++) {
2699 2755 struct mntopt *mop;
2700 2756
2701 2757 mop = &mp->mo_list[i];
2702 2758 if (mop->mo_flags & MO_SET) {
2703 2759 int optlen, comma = 0;
2704 2760
2705 2761 if (buf[0] != '\0')
2706 2762 comma = 1;
2707 2763 optlen = strlen(mop->mo_name);
2708 2764 if (strlen(buf) + comma + optlen + 1 > len)
2709 2765 goto err;
2710 2766 if (comma)
2711 2767 *cp++ = ',';
2712 2768 (void) strcpy(cp, mop->mo_name);
2713 2769 cp += optlen;
2714 2770 /*
2715 2771 * Append option value if there is one
2716 2772 */
2717 2773 if (mop->mo_arg != NULL) {
2718 2774 int arglen;
2719 2775
2720 2776 arglen = strlen(mop->mo_arg);
2721 2777 if (strlen(buf) + arglen + 2 > len)
2722 2778 goto err;
2723 2779 *cp++ = '=';
2724 2780 (void) strcpy(cp, mop->mo_arg);
2725 2781 cp += arglen;
2726 2782 }
2727 2783 }
2728 2784 }
2729 2785 return (0);
2730 2786 err:
2731 2787 return (EOVERFLOW);
2732 2788 }
2733 2789
2734 2790 static void
2735 2791 vfs_freecancelopt(char **moc)
2736 2792 {
2737 2793 if (moc != NULL) {
2738 2794 int ccnt = 0;
2739 2795 char **cp;
2740 2796
2741 2797 for (cp = moc; *cp != NULL; cp++) {
2742 2798 kmem_free(*cp, strlen(*cp) + 1);
2743 2799 ccnt++;
2744 2800 }
2745 2801 kmem_free(moc, (ccnt + 1) * sizeof (char *));
2746 2802 }
2747 2803 }
2748 2804
2749 2805 static void
2750 2806 vfs_freeopt(mntopt_t *mop)
2751 2807 {
2752 2808 if (mop->mo_name != NULL)
2753 2809 kmem_free(mop->mo_name, strlen(mop->mo_name) + 1);
2754 2810
2755 2811 vfs_freecancelopt(mop->mo_cancel);
2756 2812
2757 2813 if (mop->mo_arg != NULL)
2758 2814 kmem_free(mop->mo_arg, strlen(mop->mo_arg) + 1);
2759 2815 }
2760 2816
2761 2817 /*
2762 2818 * Free a mount options table
2763 2819 *
2764 2820 * This function is *not* for general use by filesystems.
2765 2821 *
2766 2822 * Note: caller is responsible for locking the vfs list, if needed,
2767 2823 * to protect mp.
2768 2824 */
2769 2825 void
2770 2826 vfs_freeopttbl(mntopts_t *mp)
2771 2827 {
2772 2828 uint_t i, count;
2773 2829
2774 2830 count = mp->mo_count;
2775 2831 for (i = 0; i < count; i++) {
2776 2832 vfs_freeopt(&mp->mo_list[i]);
2777 2833 }
2778 2834 if (count) {
2779 2835 kmem_free(mp->mo_list, sizeof (mntopt_t) * count);
2780 2836 mp->mo_count = 0;
2781 2837 mp->mo_list = NULL;
2782 2838 }
2783 2839 }
2784 2840
2785 2841
2786 2842 /* ARGSUSED */
2787 2843 static int
2788 2844 vfs_mntdummyread(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cred,
2789 2845 caller_context_t *ct)
2790 2846 {
2791 2847 return (0);
2792 2848 }
2793 2849
2794 2850 /* ARGSUSED */
2795 2851 static int
2796 2852 vfs_mntdummywrite(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cred,
2797 2853 caller_context_t *ct)
2798 2854 {
2799 2855 return (0);
2800 2856 }
2801 2857
2802 2858 /*
2803 2859 * The dummy vnode is currently used only by file events notification
2804 2860 * module which is just interested in the timestamps.
2805 2861 */
2806 2862 /* ARGSUSED */
2807 2863 static int
2808 2864 vfs_mntdummygetattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2809 2865 caller_context_t *ct)
2810 2866 {
2811 2867 bzero(vap, sizeof (vattr_t));
2812 2868 vap->va_type = VREG;
2813 2869 vap->va_nlink = 1;
2814 2870 vap->va_ctime = vfs_mnttab_ctime;
2815 2871 /*
2816 2872 * it is ok to just copy mtime as the time will be monotonically
2817 2873 * increasing.
2818 2874 */
2819 2875 vap->va_mtime = vfs_mnttab_mtime;
2820 2876 vap->va_atime = vap->va_mtime;
2821 2877 return (0);
2822 2878 }
2823 2879
2824 2880 static void
2825 2881 vfs_mnttabvp_setup(void)
2826 2882 {
2827 2883 vnode_t *tvp;
2828 2884 vnodeops_t *vfs_mntdummyvnops;
2829 2885 const fs_operation_def_t mnt_dummyvnodeops_template[] = {
2830 2886 VOPNAME_READ, { .vop_read = vfs_mntdummyread },
2831 2887 VOPNAME_WRITE, { .vop_write = vfs_mntdummywrite },
2832 2888 VOPNAME_GETATTR, { .vop_getattr = vfs_mntdummygetattr },
2833 2889 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
2834 2890 NULL, NULL
2835 2891 };
2836 2892
2837 2893 if (vn_make_ops("mnttab", mnt_dummyvnodeops_template,
2838 2894 &vfs_mntdummyvnops) != 0) {
2839 2895 cmn_err(CE_WARN, "vfs_mnttabvp_setup: vn_make_ops failed");
2840 2896 /* Shouldn't happen, but not bad enough to panic */
2841 2897 return;
2842 2898 }
2843 2899
2844 2900 /*
2845 2901 * A global dummy vnode is allocated to represent mntfs files.
2846 2902 * The mntfs file (/etc/mnttab) can be monitored for file events
2847 2903 * and receive an event when mnttab changes. Dummy VOP calls
2848 2904 * will be made on this vnode. The file events notification module
2849 2905 * intercepts this vnode and delivers relevant events.
2850 2906 */
2851 2907 tvp = vn_alloc(KM_SLEEP);
2852 2908 tvp->v_flag = VNOMOUNT|VNOMAP|VNOSWAP|VNOCACHE;
2853 2909 vn_setops(tvp, vfs_mntdummyvnops);
2854 2910 tvp->v_type = VREG;
2855 2911 /*
2856 2912 * The mnt dummy ops do not reference v_data.
2857 2913 * No other module intercepting this vnode should either.
2858 2914 * Just set it to point to itself.
2859 2915 */
2860 2916 tvp->v_data = (caddr_t)tvp;
2861 2917 tvp->v_vfsp = rootvfs;
2862 2918 vfs_mntdummyvp = tvp;
2863 2919 }
2864 2920
2865 2921 /*
2866 2922 * performs fake read/write ops
2867 2923 */
2868 2924 static void
2869 2925 vfs_mnttab_rwop(int rw)
2870 2926 {
2871 2927 struct uio uio;
2872 2928 struct iovec iov;
2873 2929 char buf[1];
2874 2930
2875 2931 if (vfs_mntdummyvp == NULL)
2876 2932 return;
2877 2933
2878 2934 bzero(&uio, sizeof (uio));
2879 2935 bzero(&iov, sizeof (iov));
2880 2936 iov.iov_base = buf;
2881 2937 iov.iov_len = 0;
2882 2938 uio.uio_iov = &iov;
2883 2939 uio.uio_iovcnt = 1;
2884 2940 uio.uio_loffset = 0;
2885 2941 uio.uio_segflg = UIO_SYSSPACE;
2886 2942 uio.uio_resid = 0;
2887 2943 if (rw) {
2888 2944 (void) VOP_WRITE(vfs_mntdummyvp, &uio, 0, kcred, NULL);
2889 2945 } else {
2890 2946 (void) VOP_READ(vfs_mntdummyvp, &uio, 0, kcred, NULL);
2891 2947 }
2892 2948 }
2893 2949
2894 2950 /*
2895 2951 * Generate a write operation.
2896 2952 */
2897 2953 void
2898 2954 vfs_mnttab_writeop(void)
2899 2955 {
2900 2956 vfs_mnttab_rwop(1);
2901 2957 }
2902 2958
2903 2959 /*
2904 2960 * Generate a read operation.
2905 2961 */
2906 2962 void
2907 2963 vfs_mnttab_readop(void)
2908 2964 {
2909 2965 vfs_mnttab_rwop(0);
2910 2966 }
2911 2967
2912 2968 /*
2913 2969 * Free any mnttab information recorded in the vfs struct.
2914 2970 * The vfs must not be on the vfs list.
2915 2971 */
2916 2972 static void
2917 2973 vfs_freemnttab(struct vfs *vfsp)
2918 2974 {
2919 2975 ASSERT(!VFS_ON_LIST(vfsp));
2920 2976
2921 2977 /*
2922 2978 * Free device and mount point information
2923 2979 */
2924 2980 if (vfsp->vfs_mntpt != NULL) {
2925 2981 refstr_rele(vfsp->vfs_mntpt);
2926 2982 vfsp->vfs_mntpt = NULL;
2927 2983 }
2928 2984 if (vfsp->vfs_resource != NULL) {
2929 2985 refstr_rele(vfsp->vfs_resource);
2930 2986 vfsp->vfs_resource = NULL;
2931 2987 }
2932 2988 /*
2933 2989 * Now free mount options information
2934 2990 */
2935 2991 vfs_freeopttbl(&vfsp->vfs_mntopts);
2936 2992 }
2937 2993
2938 2994 /*
2939 2995 * Return the last mnttab modification time
2940 2996 */
2941 2997 void
2942 2998 vfs_mnttab_modtime(timespec_t *ts)
2943 2999 {
2944 3000 ASSERT(RW_LOCK_HELD(&vfslist));
2945 3001 *ts = vfs_mnttab_mtime;
2946 3002 }
2947 3003
2948 3004 /*
2949 3005 * See if mnttab is changed
2950 3006 */
2951 3007 void
2952 3008 vfs_mnttab_poll(timespec_t *old, struct pollhead **phpp)
2953 3009 {
2954 3010 int changed;
2955 3011
2956 3012 *phpp = (struct pollhead *)NULL;
2957 3013
2958 3014 /*
2959 3015 * Note: don't grab vfs list lock before accessing vfs_mnttab_mtime.
2960 3016 * Can lead to deadlock against vfs_mnttab_modtimeupd(). It is safe
2961 3017 * to not grab the vfs list lock because tv_sec is monotonically
2962 3018 * increasing.
2963 3019 */
2964 3020
2965 3021 changed = (old->tv_nsec != vfs_mnttab_mtime.tv_nsec) ||
2966 3022 (old->tv_sec != vfs_mnttab_mtime.tv_sec);
2967 3023 if (!changed) {
2968 3024 *phpp = &vfs_pollhd;
2969 3025 }
2970 3026 }
2971 3027
2972 3028 /* Provide a unique and monotonically-increasing timestamp. */
2973 3029 void
2974 3030 vfs_mono_time(timespec_t *ts)
2975 3031 {
2976 3032 static volatile hrtime_t hrt; /* The saved time. */
2977 3033 hrtime_t newhrt, oldhrt; /* For effecting the CAS. */
2978 3034 timespec_t newts;
2979 3035
2980 3036 /*
2981 3037 * Try gethrestime() first, but be prepared to fabricate a sensible
2982 3038 * answer at the first sign of any trouble.
2983 3039 */
2984 3040 gethrestime(&newts);
2985 3041 newhrt = ts2hrt(&newts);
2986 3042 for (;;) {
2987 3043 oldhrt = hrt;
2988 3044 if (newhrt <= hrt)
2989 3045 newhrt = hrt + 1;
2990 3046 if (atomic_cas_64((uint64_t *)&hrt, oldhrt, newhrt) == oldhrt)
2991 3047 break;
2992 3048 }
2993 3049 hrt2ts(newhrt, ts);
2994 3050 }
2995 3051
2996 3052 /*
2997 3053 * Update the mnttab modification time and wake up any waiters for
2998 3054 * mnttab changes
2999 3055 */
3000 3056 void
3001 3057 vfs_mnttab_modtimeupd()
3002 3058 {
3003 3059 hrtime_t oldhrt, newhrt;
3004 3060
3005 3061 ASSERT(RW_WRITE_HELD(&vfslist));
3006 3062 oldhrt = ts2hrt(&vfs_mnttab_mtime);
3007 3063 gethrestime(&vfs_mnttab_mtime);
3008 3064 newhrt = ts2hrt(&vfs_mnttab_mtime);
3009 3065 if (oldhrt == (hrtime_t)0)
3010 3066 vfs_mnttab_ctime = vfs_mnttab_mtime;
3011 3067 /*
3012 3068 * Attempt to provide unique mtime (like uniqtime but not).
3013 3069 */
3014 3070 if (newhrt == oldhrt) {
3015 3071 newhrt++;
3016 3072 hrt2ts(newhrt, &vfs_mnttab_mtime);
3017 3073 }
3018 3074 pollwakeup(&vfs_pollhd, (short)POLLRDBAND);
3019 3075 vfs_mnttab_writeop();
3020 3076 }
3021 3077
3022 3078 int
3023 3079 dounmount(struct vfs *vfsp, int flag, cred_t *cr)
3024 3080 {
3025 3081 vnode_t *coveredvp;
3026 3082 int error;
3027 3083 extern void teardown_vopstats(vfs_t *);
3028 3084
3029 3085 /*
3030 3086 * Get covered vnode. This will be NULL if the vfs is not linked
3031 3087 * into the file system name space (i.e., domount() with MNT_NOSPICE).
3032 3088 */
3033 3089 coveredvp = vfsp->vfs_vnodecovered;
3034 3090 ASSERT(coveredvp == NULL || vn_vfswlock_held(coveredvp));
3035 3091
3036 3092 /*
3037 3093 * Purge all dnlc entries for this vfs.
3038 3094 */
3039 3095 (void) dnlc_purge_vfsp(vfsp, 0);
3040 3096
3041 3097 /* For forcible umount, skip VFS_SYNC() since it may hang */
3042 3098 if ((flag & MS_FORCE) == 0)
3043 3099 (void) VFS_SYNC(vfsp, 0, cr);
3044 3100
3045 3101 /*
3046 3102 * Lock the vfs to maintain fs status quo during unmount. This
3047 3103 * has to be done after the sync because ufs_update tries to acquire
3048 3104 * the vfs_reflock.
3049 3105 */
3050 3106 vfs_lock_wait(vfsp);
3051 3107
3052 3108 if (error = VFS_UNMOUNT(vfsp, flag, cr)) {
3053 3109 vfs_unlock(vfsp);
3054 3110 if (coveredvp != NULL)
3055 3111 vn_vfsunlock(coveredvp);
3056 3112 } else if (coveredvp != NULL) {
3057 3113 teardown_vopstats(vfsp);
3058 3114 /*
3059 3115 * vfs_remove() will do a VN_RELE(vfsp->vfs_vnodecovered)
3060 3116 * when it frees vfsp so we do a VN_HOLD() so we can
3061 3117 * continue to use coveredvp afterwards.
3062 3118 */
3063 3119 VN_HOLD(coveredvp);
3064 3120 vfs_remove(vfsp);
3065 3121 vn_vfsunlock(coveredvp);
3066 3122 VN_RELE(coveredvp);
3067 3123 } else {
3068 3124 teardown_vopstats(vfsp);
3069 3125 /*
3070 3126 * Release the reference to vfs that is not linked
3071 3127 * into the name space.
3072 3128 */
3073 3129 vfs_unlock(vfsp);
3074 3130 VFS_RELE(vfsp);
3075 3131 }
3076 3132 return (error);
3077 3133 }
3078 3134
3079 3135
3080 3136 /*
3081 3137 * Vfs_unmountall() is called by uadmin() to unmount all
3082 3138 * mounted file systems (except the root file system) during shutdown.
3083 3139 * It follows the existing locking protocol when traversing the vfs list
3084 3140 * to sync and unmount vfses. Even though there should be no
3085 3141 * other thread running while the system is shutting down, it is prudent
3086 3142 * to still follow the locking protocol.
3087 3143 */
3088 3144 void
3089 3145 vfs_unmountall(void)
3090 3146 {
3091 3147 struct vfs *vfsp;
3092 3148 struct vfs *prev_vfsp = NULL;
3093 3149 int error;
3094 3150
3095 3151 /*
3096 3152 * Toss all dnlc entries now so that the per-vfs sync
3097 3153 * and unmount operations don't have to slog through
3098 3154 * a bunch of uninteresting vnodes over and over again.
3099 3155 */
3100 3156 dnlc_purge();
3101 3157
3102 3158 vfs_list_lock();
3103 3159 for (vfsp = rootvfs->vfs_prev; vfsp != rootvfs; vfsp = prev_vfsp) {
3104 3160 prev_vfsp = vfsp->vfs_prev;
3105 3161
3106 3162 if (vfs_lock(vfsp) != 0)
3107 3163 continue;
3108 3164 error = vn_vfswlock(vfsp->vfs_vnodecovered);
3109 3165 vfs_unlock(vfsp);
3110 3166 if (error)
3111 3167 continue;
3112 3168
3113 3169 vfs_list_unlock();
3114 3170
3115 3171 (void) VFS_SYNC(vfsp, SYNC_CLOSE, CRED());
3116 3172 (void) dounmount(vfsp, 0, CRED());
3117 3173
3118 3174 /*
3119 3175 * Since we dropped the vfslist lock above we must
3120 3176 * verify that next_vfsp still exists, else start over.
3121 3177 */
3122 3178 vfs_list_lock();
3123 3179 for (vfsp = rootvfs->vfs_prev;
3124 3180 vfsp != rootvfs; vfsp = vfsp->vfs_prev)
3125 3181 if (vfsp == prev_vfsp)
3126 3182 break;
3127 3183 if (vfsp == rootvfs && prev_vfsp != rootvfs)
3128 3184 prev_vfsp = rootvfs->vfs_prev;
3129 3185 }
3130 3186 vfs_list_unlock();
3131 3187 }
3132 3188
3133 3189 /*
3134 3190 * Called to add an entry to the end of the vfs mount in progress list
3135 3191 */
3136 3192 void
3137 3193 vfs_addmip(dev_t dev, struct vfs *vfsp)
3138 3194 {
3139 3195 struct ipmnt *mipp;
3140 3196
3141 3197 mipp = (struct ipmnt *)kmem_alloc(sizeof (struct ipmnt), KM_SLEEP);
3142 3198 mipp->mip_next = NULL;
3143 3199 mipp->mip_dev = dev;
3144 3200 mipp->mip_vfsp = vfsp;
3145 3201 mutex_enter(&vfs_miplist_mutex);
3146 3202 if (vfs_miplist_end != NULL)
3147 3203 vfs_miplist_end->mip_next = mipp;
3148 3204 else
3149 3205 vfs_miplist = mipp;
3150 3206 vfs_miplist_end = mipp;
3151 3207 mutex_exit(&vfs_miplist_mutex);
3152 3208 }
3153 3209
3154 3210 /*
3155 3211 * Called to remove an entry from the mount in progress list
3156 3212 * Either because the mount completed or it failed.
3157 3213 */
3158 3214 void
3159 3215 vfs_delmip(struct vfs *vfsp)
3160 3216 {
3161 3217 struct ipmnt *mipp, *mipprev;
3162 3218
3163 3219 mutex_enter(&vfs_miplist_mutex);
3164 3220 mipprev = NULL;
3165 3221 for (mipp = vfs_miplist;
3166 3222 mipp && mipp->mip_vfsp != vfsp; mipp = mipp->mip_next) {
3167 3223 mipprev = mipp;
3168 3224 }
3169 3225 if (mipp == NULL)
3170 3226 return; /* shouldn't happen */
3171 3227 if (mipp == vfs_miplist_end)
3172 3228 vfs_miplist_end = mipprev;
3173 3229 if (mipprev == NULL)
3174 3230 vfs_miplist = mipp->mip_next;
3175 3231 else
3176 3232 mipprev->mip_next = mipp->mip_next;
3177 3233 mutex_exit(&vfs_miplist_mutex);
3178 3234 kmem_free(mipp, sizeof (struct ipmnt));
3179 3235 }
3180 3236
3181 3237 /*
3182 3238 * vfs_add is called by a specific filesystem's mount routine to add
3183 3239 * the new vfs into the vfs list/hash and to cover the mounted-on vnode.
3184 3240 * The vfs should already have been locked by the caller.
3185 3241 *
3186 3242 * coveredvp is NULL if this is the root.
3187 3243 */
3188 3244 void
3189 3245 vfs_add(vnode_t *coveredvp, struct vfs *vfsp, int mflag)
3190 3246 {
3191 3247 int newflag;
3192 3248
3193 3249 ASSERT(vfs_lock_held(vfsp));
3194 3250 VFS_HOLD(vfsp);
3195 3251 newflag = vfsp->vfs_flag;
3196 3252 if (mflag & MS_RDONLY)
3197 3253 newflag |= VFS_RDONLY;
3198 3254 else
3199 3255 newflag &= ~VFS_RDONLY;
3200 3256 if (mflag & MS_NOSUID)
3201 3257 newflag |= (VFS_NOSETUID|VFS_NODEVICES);
3202 3258 else
3203 3259 newflag &= ~(VFS_NOSETUID|VFS_NODEVICES);
3204 3260 if (mflag & MS_NOMNTTAB)
3205 3261 newflag |= VFS_NOMNTTAB;
3206 3262 else
3207 3263 newflag &= ~VFS_NOMNTTAB;
3208 3264
3209 3265 if (coveredvp != NULL) {
3210 3266 ASSERT(vn_vfswlock_held(coveredvp));
3211 3267 coveredvp->v_vfsmountedhere = vfsp;
3212 3268 VN_HOLD(coveredvp);
3213 3269 }
3214 3270 vfsp->vfs_vnodecovered = coveredvp;
3215 3271 vfsp->vfs_flag = newflag;
3216 3272
3217 3273 vfs_list_add(vfsp);
3218 3274 }
3219 3275
3220 3276 /*
3221 3277 * Remove a vfs from the vfs list, null out the pointer from the
3222 3278 * covered vnode to the vfs (v_vfsmountedhere), and null out the pointer
3223 3279 * from the vfs to the covered vnode (vfs_vnodecovered). Release the
3224 3280 * reference to the vfs and to the covered vnode.
3225 3281 *
3226 3282 * Called from dounmount after it's confirmed with the file system
3227 3283 * that the unmount is legal.
3228 3284 */
3229 3285 void
3230 3286 vfs_remove(struct vfs *vfsp)
3231 3287 {
3232 3288 vnode_t *vp;
3233 3289
3234 3290 ASSERT(vfs_lock_held(vfsp));
3235 3291
3236 3292 /*
3237 3293 * Can't unmount root. Should never happen because fs will
3238 3294 * be busy.
3239 3295 */
3240 3296 if (vfsp == rootvfs)
3241 3297 panic("vfs_remove: unmounting root");
3242 3298
3243 3299 vfs_list_remove(vfsp);
3244 3300
3245 3301 /*
3246 3302 * Unhook from the file system name space.
3247 3303 */
3248 3304 vp = vfsp->vfs_vnodecovered;
3249 3305 ASSERT(vn_vfswlock_held(vp));
3250 3306 vp->v_vfsmountedhere = NULL;
3251 3307 vfsp->vfs_vnodecovered = NULL;
3252 3308 VN_RELE(vp);
3253 3309
3254 3310 /*
3255 3311 * Release lock and wakeup anybody waiting.
3256 3312 */
3257 3313 vfs_unlock(vfsp);
3258 3314 VFS_RELE(vfsp);
3259 3315 }
3260 3316
3261 3317 /*
3262 3318 * Lock a filesystem to prevent access to it while mounting,
3263 3319 * unmounting and syncing. Return EBUSY immediately if lock
3264 3320 * can't be acquired.
3265 3321 */
3266 3322 int
3267 3323 vfs_lock(vfs_t *vfsp)
3268 3324 {
3269 3325 vn_vfslocks_entry_t *vpvfsentry;
3270 3326
3271 3327 vpvfsentry = vn_vfslocks_getlock(vfsp);
3272 3328 if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
3273 3329 return (0);
3274 3330
3275 3331 vn_vfslocks_rele(vpvfsentry);
3276 3332 return (EBUSY);
3277 3333 }
3278 3334
3279 3335 int
3280 3336 vfs_rlock(vfs_t *vfsp)
3281 3337 {
3282 3338 vn_vfslocks_entry_t *vpvfsentry;
3283 3339
3284 3340 vpvfsentry = vn_vfslocks_getlock(vfsp);
3285 3341
3286 3342 if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
3287 3343 return (0);
3288 3344
3289 3345 vn_vfslocks_rele(vpvfsentry);
3290 3346 return (EBUSY);
3291 3347 }
3292 3348
3293 3349 void
3294 3350 vfs_lock_wait(vfs_t *vfsp)
3295 3351 {
3296 3352 vn_vfslocks_entry_t *vpvfsentry;
3297 3353
3298 3354 vpvfsentry = vn_vfslocks_getlock(vfsp);
3299 3355 rwst_enter(&vpvfsentry->ve_lock, RW_WRITER);
3300 3356 }
3301 3357
3302 3358 void
3303 3359 vfs_rlock_wait(vfs_t *vfsp)
3304 3360 {
3305 3361 vn_vfslocks_entry_t *vpvfsentry;
3306 3362
3307 3363 vpvfsentry = vn_vfslocks_getlock(vfsp);
3308 3364 rwst_enter(&vpvfsentry->ve_lock, RW_READER);
3309 3365 }
3310 3366
3311 3367 /*
3312 3368 * Unlock a locked filesystem.
3313 3369 */
3314 3370 void
3315 3371 vfs_unlock(vfs_t *vfsp)
3316 3372 {
3317 3373 vn_vfslocks_entry_t *vpvfsentry;
3318 3374
3319 3375 /*
3320 3376 * vfs_unlock will mimic sema_v behaviour to fix 4748018.
3321 3377 * And these changes should remain for the patch changes as it is.
3322 3378 */
3323 3379 if (panicstr)
3324 3380 return;
3325 3381
3326 3382 /*
3327 3383 * ve_refcount needs to be dropped twice here.
3328 3384 * 1. To release refernce after a call to vfs_locks_getlock()
3329 3385 * 2. To release the reference from the locking routines like
3330 3386 * vfs_rlock_wait/vfs_wlock_wait/vfs_wlock etc,.
3331 3387 */
3332 3388
3333 3389 vpvfsentry = vn_vfslocks_getlock(vfsp);
3334 3390 vn_vfslocks_rele(vpvfsentry);
3335 3391
3336 3392 rwst_exit(&vpvfsentry->ve_lock);
3337 3393 vn_vfslocks_rele(vpvfsentry);
3338 3394 }
3339 3395
3340 3396 /*
3341 3397 * Utility routine that allows a filesystem to construct its
3342 3398 * fsid in "the usual way" - by munging some underlying dev_t and
3343 3399 * the filesystem type number into the 64-bit fsid. Note that
3344 3400 * this implicitly relies on dev_t persistence to make filesystem
3345 3401 * id's persistent.
3346 3402 *
3347 3403 * There's nothing to prevent an individual fs from constructing its
3348 3404 * fsid in a different way, and indeed they should.
3349 3405 *
3350 3406 * Since we want fsids to be 32-bit quantities (so that they can be
3351 3407 * exported identically by either 32-bit or 64-bit APIs, as well as
3352 3408 * the fact that fsid's are "known" to NFS), we compress the device
3353 3409 * number given down to 32-bits, and panic if that isn't possible.
3354 3410 */
3355 3411 void
3356 3412 vfs_make_fsid(fsid_t *fsi, dev_t dev, int val)
3357 3413 {
3358 3414 if (!cmpldev((dev32_t *)&fsi->val[0], dev))
3359 3415 panic("device number too big for fsid!");
3360 3416 fsi->val[1] = val;
3361 3417 }
3362 3418
3363 3419 int
3364 3420 vfs_lock_held(vfs_t *vfsp)
3365 3421 {
3366 3422 int held;
3367 3423 vn_vfslocks_entry_t *vpvfsentry;
3368 3424
3369 3425 /*
3370 3426 * vfs_lock_held will mimic sema_held behaviour
3371 3427 * if panicstr is set. And these changes should remain
3372 3428 * for the patch changes as it is.
3373 3429 */
3374 3430 if (panicstr)
3375 3431 return (1);
3376 3432
3377 3433 vpvfsentry = vn_vfslocks_getlock(vfsp);
3378 3434 held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);
3379 3435
3380 3436 vn_vfslocks_rele(vpvfsentry);
3381 3437 return (held);
3382 3438 }
3383 3439
3384 3440 struct _kthread *
3385 3441 vfs_lock_owner(vfs_t *vfsp)
3386 3442 {
3387 3443 struct _kthread *owner;
3388 3444 vn_vfslocks_entry_t *vpvfsentry;
3389 3445
3390 3446 /*
3391 3447 * vfs_wlock_held will mimic sema_held behaviour
3392 3448 * if panicstr is set. And these changes should remain
3393 3449 * for the patch changes as it is.
3394 3450 */
3395 3451 if (panicstr)
3396 3452 return (NULL);
3397 3453
3398 3454 vpvfsentry = vn_vfslocks_getlock(vfsp);
3399 3455 owner = rwst_owner(&vpvfsentry->ve_lock);
3400 3456
3401 3457 vn_vfslocks_rele(vpvfsentry);
3402 3458 return (owner);
3403 3459 }
3404 3460
3405 3461 /*
3406 3462 * vfs list locking.
3407 3463 *
3408 3464 * Rather than manipulate the vfslist lock directly, we abstract into lock
3409 3465 * and unlock routines to allow the locking implementation to be changed for
3410 3466 * clustering.
3411 3467 *
3412 3468 * Whenever the vfs list is modified through its hash links, the overall list
3413 3469 * lock must be obtained before locking the relevant hash bucket. But to see
3414 3470 * whether a given vfs is on the list, it suffices to obtain the lock for the
3415 3471 * hash bucket without getting the overall list lock. (See getvfs() below.)
3416 3472 */
3417 3473
3418 3474 void
3419 3475 vfs_list_lock()
3420 3476 {
3421 3477 rw_enter(&vfslist, RW_WRITER);
3422 3478 }
3423 3479
3424 3480 void
3425 3481 vfs_list_read_lock()
3426 3482 {
3427 3483 rw_enter(&vfslist, RW_READER);
3428 3484 }
3429 3485
3430 3486 void
3431 3487 vfs_list_unlock()
3432 3488 {
3433 3489 rw_exit(&vfslist);
3434 3490 }
3435 3491
3436 3492 /*
3437 3493 * Low level worker routines for adding entries to and removing entries from
3438 3494 * the vfs list.
3439 3495 */
3440 3496
3441 3497 static void
3442 3498 vfs_hash_add(struct vfs *vfsp, int insert_at_head)
3443 3499 {
3444 3500 int vhno;
3445 3501 struct vfs **hp;
3446 3502 dev_t dev;
3447 3503
3448 3504 ASSERT(RW_WRITE_HELD(&vfslist));
3449 3505
3450 3506 dev = expldev(vfsp->vfs_fsid.val[0]);
3451 3507 vhno = VFSHASH(getmajor(dev), getminor(dev));
3452 3508
3453 3509 mutex_enter(&rvfs_list[vhno].rvfs_lock);
3454 3510
3455 3511 /*
3456 3512 * Link into the hash table, inserting it at the end, so that LOFS
3457 3513 * with the same fsid as UFS (or other) file systems will not hide the
3458 3514 * UFS.
3459 3515 */
3460 3516 if (insert_at_head) {
3461 3517 vfsp->vfs_hash = rvfs_list[vhno].rvfs_head;
3462 3518 rvfs_list[vhno].rvfs_head = vfsp;
3463 3519 } else {
3464 3520 for (hp = &rvfs_list[vhno].rvfs_head; *hp != NULL;
3465 3521 hp = &(*hp)->vfs_hash)
3466 3522 continue;
3467 3523 /*
3468 3524 * hp now contains the address of the pointer to update
3469 3525 * to effect the insertion.
3470 3526 */
3471 3527 vfsp->vfs_hash = NULL;
3472 3528 *hp = vfsp;
3473 3529 }
3474 3530
3475 3531 rvfs_list[vhno].rvfs_len++;
3476 3532 mutex_exit(&rvfs_list[vhno].rvfs_lock);
3477 3533 }
3478 3534
3479 3535
3480 3536 static void
3481 3537 vfs_hash_remove(struct vfs *vfsp)
3482 3538 {
3483 3539 int vhno;
3484 3540 struct vfs *tvfsp;
3485 3541 dev_t dev;
3486 3542
3487 3543 ASSERT(RW_WRITE_HELD(&vfslist));
3488 3544
3489 3545 dev = expldev(vfsp->vfs_fsid.val[0]);
3490 3546 vhno = VFSHASH(getmajor(dev), getminor(dev));
3491 3547
3492 3548 mutex_enter(&rvfs_list[vhno].rvfs_lock);
3493 3549
3494 3550 /*
3495 3551 * Remove from hash.
3496 3552 */
3497 3553 if (rvfs_list[vhno].rvfs_head == vfsp) {
3498 3554 rvfs_list[vhno].rvfs_head = vfsp->vfs_hash;
3499 3555 rvfs_list[vhno].rvfs_len--;
3500 3556 goto foundit;
3501 3557 }
3502 3558 for (tvfsp = rvfs_list[vhno].rvfs_head; tvfsp != NULL;
3503 3559 tvfsp = tvfsp->vfs_hash) {
3504 3560 if (tvfsp->vfs_hash == vfsp) {
3505 3561 tvfsp->vfs_hash = vfsp->vfs_hash;
3506 3562 rvfs_list[vhno].rvfs_len--;
3507 3563 goto foundit;
3508 3564 }
3509 3565 }
3510 3566 cmn_err(CE_WARN, "vfs_list_remove: vfs not found in hash");
3511 3567
3512 3568 foundit:
3513 3569
3514 3570 mutex_exit(&rvfs_list[vhno].rvfs_lock);
3515 3571 }
3516 3572
3517 3573
3518 3574 void
3519 3575 vfs_list_add(struct vfs *vfsp)
3520 3576 {
3521 3577 zone_t *zone;
3522 3578
3523 3579 /*
3524 3580 * Typically, the vfs_t will have been created on behalf of the file
3525 3581 * system in vfs_init, where it will have been provided with a
3526 3582 * vfs_impl_t. This, however, might be lacking if the vfs_t was created
3527 3583 * by an unbundled file system. We therefore check for such an example
3528 3584 * before stamping the vfs_t with its creation time for the benefit of
3529 3585 * mntfs.
3530 3586 */
3531 3587 if (vfsp->vfs_implp == NULL)
3532 3588 vfsimpl_setup(vfsp);
3533 3589 vfs_mono_time(&vfsp->vfs_hrctime);
3534 3590
3535 3591 /*
3536 3592 * The zone that owns the mount is the one that performed the mount.
3537 3593 * Note that this isn't necessarily the same as the zone mounted into.
3538 3594 * The corresponding zone_rele_ref() will be done when the vfs_t
3539 3595 * is being free'd.
3540 3596 */
3541 3597 vfsp->vfs_zone = curproc->p_zone;
3542 3598 zone_init_ref(&vfsp->vfs_implp->vi_zone_ref);
3543 3599 zone_hold_ref(vfsp->vfs_zone, &vfsp->vfs_implp->vi_zone_ref,
3544 3600 ZONE_REF_VFS);
3545 3601
3546 3602 /*
3547 3603 * Find the zone mounted into, and put this mount on its vfs list.
3548 3604 */
3549 3605 zone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
3550 3606 ASSERT(zone != NULL);
3551 3607 /*
3552 3608 * Special casing for the root vfs. This structure is allocated
3553 3609 * statically and hooked onto rootvfs at link time. During the
3554 3610 * vfs_mountroot call at system startup time, the root file system's
3555 3611 * VFS_MOUNTROOT routine will call vfs_add with this root vfs struct
3556 3612 * as argument. The code below must detect and handle this special
3557 3613 * case. The only apparent justification for this special casing is
3558 3614 * to ensure that the root file system appears at the head of the
3559 3615 * list.
3560 3616 *
3561 3617 * XXX: I'm assuming that it's ok to do normal list locking when
3562 3618 * adding the entry for the root file system (this used to be
3563 3619 * done with no locks held).
3564 3620 */
3565 3621 vfs_list_lock();
3566 3622 /*
3567 3623 * Link into the vfs list proper.
3568 3624 */
3569 3625 if (vfsp == &root) {
3570 3626 /*
3571 3627 * Assert: This vfs is already on the list as its first entry.
3572 3628 * Thus, there's nothing to do.
3573 3629 */
3574 3630 ASSERT(rootvfs == vfsp);
3575 3631 /*
3576 3632 * Add it to the head of the global zone's vfslist.
3577 3633 */
3578 3634 ASSERT(zone == global_zone);
3579 3635 ASSERT(zone->zone_vfslist == NULL);
3580 3636 zone->zone_vfslist = vfsp;
3581 3637 } else {
3582 3638 /*
3583 3639 * Link to end of list using vfs_prev (as rootvfs is now a
3584 3640 * doubly linked circular list) so list is in mount order for
3585 3641 * mnttab use.
3586 3642 */
3587 3643 rootvfs->vfs_prev->vfs_next = vfsp;
3588 3644 vfsp->vfs_prev = rootvfs->vfs_prev;
3589 3645 rootvfs->vfs_prev = vfsp;
3590 3646 vfsp->vfs_next = rootvfs;
3591 3647
3592 3648 /*
3593 3649 * Do it again for the zone-private list (which may be NULL).
3594 3650 */
3595 3651 if (zone->zone_vfslist == NULL) {
3596 3652 ASSERT(zone != global_zone);
3597 3653 zone->zone_vfslist = vfsp;
3598 3654 } else {
3599 3655 zone->zone_vfslist->vfs_zone_prev->vfs_zone_next = vfsp;
3600 3656 vfsp->vfs_zone_prev = zone->zone_vfslist->vfs_zone_prev;
3601 3657 zone->zone_vfslist->vfs_zone_prev = vfsp;
3602 3658 vfsp->vfs_zone_next = zone->zone_vfslist;
3603 3659 }
↓ open down ↓ |
2773 lines elided |
↑ open up ↑ |
3604 3660 }
3605 3661
3606 3662 /*
3607 3663 * Link into the hash table, inserting it at the end, so that LOFS
3608 3664 * with the same fsid as UFS (or other) file systems will not hide
3609 3665 * the UFS.
3610 3666 */
3611 3667 vfs_hash_add(vfsp, 0);
3612 3668
3613 3669 /*
3670 + * Link into tree indexed by mntpoint, for vfs_mntpoint2vfsp
3671 + * mntix discerns entries with the same key
3672 + */
3673 + vfsp->vfs_mntix = ++vfs_curr_mntix;
3674 + avl_add(&vfs_by_dev, vfsp);
3675 +
3676 + /*
3677 + * Link into tree indexed by dev, for vfs_devismounted
3678 + */
3679 + avl_add(&vfs_by_mntpnt, vfsp);
3680 +
3681 + /*
3614 3682 * update the mnttab modification time
3615 3683 */
3616 3684 vfs_mnttab_modtimeupd();
3617 3685 vfs_list_unlock();
3618 3686 zone_rele(zone);
3619 3687 }
3620 3688
3621 3689 void
3622 3690 vfs_list_remove(struct vfs *vfsp)
3623 3691 {
3624 3692 zone_t *zone;
3625 3693
3626 3694 zone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
3627 3695 ASSERT(zone != NULL);
3628 3696 /*
3629 3697 * Callers are responsible for preventing attempts to unmount the
3630 3698 * root.
3631 3699 */
3632 3700 ASSERT(vfsp != rootvfs);
3633 3701
3634 3702 vfs_list_lock();
3635 3703
3636 3704 /*
3705 + * Remove from avl trees
3706 + */
3707 + avl_remove(&vfs_by_mntpnt, vfsp);
3708 + avl_remove(&vfs_by_dev, vfsp);
3709 +
3710 + /*
3637 3711 * Remove from hash.
3638 3712 */
3639 3713 vfs_hash_remove(vfsp);
3640 3714
3641 3715 /*
3642 3716 * Remove from vfs list.
3643 3717 */
3644 3718 vfsp->vfs_prev->vfs_next = vfsp->vfs_next;
3645 3719 vfsp->vfs_next->vfs_prev = vfsp->vfs_prev;
3646 3720 vfsp->vfs_next = vfsp->vfs_prev = NULL;
3647 3721
3648 3722 /*
3649 3723 * Remove from zone-specific vfs list.
3650 3724 */
3651 3725 if (zone->zone_vfslist == vfsp)
3652 3726 zone->zone_vfslist = vfsp->vfs_zone_next;
3653 3727
3654 3728 if (vfsp->vfs_zone_next == vfsp) {
3655 3729 ASSERT(vfsp->vfs_zone_prev == vfsp);
3656 3730 ASSERT(zone->zone_vfslist == vfsp);
3657 3731 zone->zone_vfslist = NULL;
3658 3732 }
3659 3733
3660 3734 vfsp->vfs_zone_prev->vfs_zone_next = vfsp->vfs_zone_next;
3661 3735 vfsp->vfs_zone_next->vfs_zone_prev = vfsp->vfs_zone_prev;
3662 3736 vfsp->vfs_zone_next = vfsp->vfs_zone_prev = NULL;
3663 3737
3664 3738 /*
3665 3739 * update the mnttab modification time
3666 3740 */
3667 3741 vfs_mnttab_modtimeupd();
3668 3742 vfs_list_unlock();
3669 3743 zone_rele(zone);
3670 3744 }
3671 3745
3672 3746 struct vfs *
3673 3747 getvfs(fsid_t *fsid)
3674 3748 {
3675 3749 struct vfs *vfsp;
3676 3750 int val0 = fsid->val[0];
3677 3751 int val1 = fsid->val[1];
3678 3752 dev_t dev = expldev(val0);
3679 3753 int vhno = VFSHASH(getmajor(dev), getminor(dev));
3680 3754 kmutex_t *hmp = &rvfs_list[vhno].rvfs_lock;
3681 3755
3682 3756 mutex_enter(hmp);
3683 3757 for (vfsp = rvfs_list[vhno].rvfs_head; vfsp; vfsp = vfsp->vfs_hash) {
3684 3758 if (vfsp->vfs_fsid.val[0] == val0 &&
3685 3759 vfsp->vfs_fsid.val[1] == val1) {
3686 3760 VFS_HOLD(vfsp);
3687 3761 mutex_exit(hmp);
3688 3762 return (vfsp);
3689 3763 }
3690 3764 }
3691 3765 mutex_exit(hmp);
3692 3766 return (NULL);
3693 3767 }
3694 3768
3695 3769 /*
3696 3770 * Search the vfs mount in progress list for a specified device/vfs entry.
3697 3771 * Returns 0 if the first entry in the list that the device matches has the
3698 3772 * given vfs pointer as well. If the device matches but a different vfs
3699 3773 * pointer is encountered in the list before the given vfs pointer then
3700 3774 * a 1 is returned.
3701 3775 */
3702 3776
3703 3777 int
3704 3778 vfs_devmounting(dev_t dev, struct vfs *vfsp)
3705 3779 {
3706 3780 int retval = 0;
3707 3781 struct ipmnt *mipp;
3708 3782
3709 3783 mutex_enter(&vfs_miplist_mutex);
3710 3784 for (mipp = vfs_miplist; mipp != NULL; mipp = mipp->mip_next) {
3711 3785 if (mipp->mip_dev == dev) {
3712 3786 if (mipp->mip_vfsp != vfsp)
3713 3787 retval = 1;
3714 3788 break;
3715 3789 }
3716 3790 }
3717 3791 mutex_exit(&vfs_miplist_mutex);
3718 3792 return (retval);
3719 3793 }
↓ open down ↓ |
73 lines elided |
↑ open up ↑ |
3720 3794
3721 3795 /*
3722 3796 * Search the vfs list for a specified device. Returns 1, if entry is found
3723 3797 * or 0 if no suitable entry is found.
3724 3798 */
3725 3799
3726 3800 int
3727 3801 vfs_devismounted(dev_t dev)
3728 3802 {
3729 3803 struct vfs *vfsp;
3730 - int found;
3804 + int found = 0;
3805 + struct vfs search;
3806 + avl_index_t index;
3807 +
3808 + search.vfs_dev = dev;
3809 + search.vfs_mntix = 0;
3731 3810
3732 3811 vfs_list_read_lock();
3733 - vfsp = rootvfs;
3734 - found = 0;
3735 - do {
3736 - if (vfsp->vfs_dev == dev) {
3737 - found = 1;
3738 - break;
3739 - }
3740 - vfsp = vfsp->vfs_next;
3741 - } while (vfsp != rootvfs);
3812 +
3813 + /*
3814 + * there might be several entries with the same dev in the tree,
3815 + * only discerned by mntix. To find the first, we start with a mntix
3816 + * of 0. The search will fail. The following avl_nearest will give
3817 + * us the actual first entry.
3818 + */
3819 + VERIFY(avl_find(&vfs_by_dev, &search, &index) == NULL);
3820 + vfsp = avl_nearest(&vfs_by_dev, index, AVL_AFTER);
3821 +
3822 + if (vfsp != NULL && vfsp->vfs_dev == dev)
3823 + found = 1;
3742 3824
3743 3825 vfs_list_unlock();
3744 3826 return (found);
3745 3827 }
3746 3828
3747 3829 /*
3748 3830 * Search the vfs list for a specified device. Returns a pointer to it
3749 3831 * or NULL if no suitable entry is found. The caller of this routine
3750 3832 * is responsible for releasing the returned vfs pointer.
3751 3833 */
3752 3834 struct vfs *
3753 3835 vfs_dev2vfsp(dev_t dev)
3754 3836 {
3755 3837 struct vfs *vfsp;
3756 3838 int found;
3839 + struct vfs search;
3840 + avl_index_t index;
3841 +
3842 + search.vfs_dev = dev;
3843 + search.vfs_mntix = 0;
3757 3844
3758 3845 vfs_list_read_lock();
3759 - vfsp = rootvfs;
3846 +
3847 + /*
3848 + * there might be several entries with the same dev in the tree,
3849 + * only discerned by mntix. To find the first, we start with a mntix
3850 + * of 0. The search will fail. The following avl_nearest will give
3851 + * us the actual first entry.
3852 + */
3853 + VERIFY(avl_find(&vfs_by_dev, &search, &index) == NULL);
3854 + vfsp = avl_nearest(&vfs_by_dev, index, AVL_AFTER);
3855 +
3760 3856 found = 0;
3761 - do {
3857 + while (vfsp != NULL && vfsp->vfs_dev == dev) {
3762 3858 /*
3763 3859 * The following could be made more efficient by making
3764 3860 * the entire loop use vfs_zone_next if the call is from
3765 3861 * a zone. The only callers, however, ustat(2) and
3766 3862 * umount2(2), don't seem to justify the added
3767 3863 * complexity at present.
3768 3864 */
3769 - if (vfsp->vfs_dev == dev &&
3770 - ZONE_PATH_VISIBLE(refstr_value(vfsp->vfs_mntpt),
3865 + if (ZONE_PATH_VISIBLE(refstr_value(vfsp->vfs_mntpt),
3771 3866 curproc->p_zone)) {
3772 3867 VFS_HOLD(vfsp);
3773 3868 found = 1;
3774 3869 break;
3775 3870 }
3776 - vfsp = vfsp->vfs_next;
3777 - } while (vfsp != rootvfs);
3871 + vfsp = AVL_NEXT(&vfs_by_dev, vfsp);
3872 + }
3778 3873 vfs_list_unlock();
3779 - return (found ? vfsp: NULL);
3874 + return (found ? vfsp : NULL);
3780 3875 }
3781 3876
3782 3877 /*
3783 3878 * Search the vfs list for a specified mntpoint. Returns a pointer to it
3784 3879 * or NULL if no suitable entry is found. The caller of this routine
3785 3880 * is responsible for releasing the returned vfs pointer.
3786 3881 *
3787 3882 * Note that if multiple mntpoints match, the last one matching is
3788 3883 * returned in an attempt to return the "top" mount when overlay
3789 3884 * mounts are covering the same mount point. This is accomplished by starting
3790 3885 * at the end of the list and working our way backwards, stopping at the first
3791 3886 * matching mount.
3792 3887 */
3793 3888 struct vfs *
3794 3889 vfs_mntpoint2vfsp(const char *mp)
3795 3890 {
↓ open down ↓ |
6 lines elided |
↑ open up ↑ |
3796 3891 struct vfs *vfsp;
3797 3892 struct vfs *retvfsp = NULL;
3798 3893 zone_t *zone = curproc->p_zone;
3799 3894 struct vfs *list;
3800 3895
3801 3896 vfs_list_read_lock();
3802 3897 if (getzoneid() == GLOBAL_ZONEID) {
3803 3898 /*
3804 3899 * The global zone may see filesystems in any zone.
3805 3900 */
3806 - vfsp = rootvfs->vfs_prev;
3807 - do {
3808 - if (strcmp(refstr_value(vfsp->vfs_mntpt), mp) == 0) {
3809 - retvfsp = vfsp;
3810 - break;
3811 - }
3812 - vfsp = vfsp->vfs_prev;
3813 - } while (vfsp != rootvfs->vfs_prev);
3901 + struct vfs search;
3902 + search.vfs_mntpt = refstr_alloc(mp);
3903 + search.vfs_mntix = UINT64_MAX;
3904 + avl_index_t index;
3905 +
3906 + /*
3907 + * there might be several entries with the same mntpnt in the
3908 + * tree, only discerned by mntix. To find the last, we start
3909 + * with a mntix of UINT64_MAX. The search will fail. The
3910 + * following avl_nearest will give us the actual last entry
3911 + * matching the mntpnt.
3912 + */
3913 + VERIFY(avl_find(&vfs_by_mntpnt, &search, &index) == 0);
3914 + vfsp = avl_nearest(&vfs_by_mntpnt, index, AVL_BEFORE);
3915 +
3916 + refstr_rele(search.vfs_mntpt);
3917 +
3918 + if (vfsp != NULL &&
3919 + strcmp(refstr_value(vfsp->vfs_mntpt), mp) == 0)
3920 + retvfsp = vfsp;
3814 3921 } else if ((list = zone->zone_vfslist) != NULL) {
3815 3922 const char *mntpt;
3816 3923
3817 3924 vfsp = list->vfs_zone_prev;
3818 3925 do {
3819 3926 mntpt = refstr_value(vfsp->vfs_mntpt);
3820 3927 mntpt = ZONE_PATH_TRANSLATE(mntpt, zone);
3821 3928 if (strcmp(mntpt, mp) == 0) {
3822 3929 retvfsp = vfsp;
3823 3930 break;
3824 3931 }
3825 3932 vfsp = vfsp->vfs_zone_prev;
3826 3933 } while (vfsp != list->vfs_zone_prev);
3827 3934 }
3828 3935 if (retvfsp)
3829 3936 VFS_HOLD(retvfsp);
3830 3937 vfs_list_unlock();
3831 3938 return (retvfsp);
3832 3939 }
3833 3940
3834 3941 /*
3835 3942 * Search the vfs list for a specified vfsops.
3836 3943 * if vfs entry is found then return 1, else 0.
3837 3944 */
3838 3945 int
3839 3946 vfs_opsinuse(vfsops_t *ops)
3840 3947 {
3841 3948 struct vfs *vfsp;
3842 3949 int found;
3843 3950
3844 3951 vfs_list_read_lock();
3845 3952 vfsp = rootvfs;
3846 3953 found = 0;
3847 3954 do {
3848 3955 if (vfs_getops(vfsp) == ops) {
3849 3956 found = 1;
3850 3957 break;
3851 3958 }
3852 3959 vfsp = vfsp->vfs_next;
3853 3960 } while (vfsp != rootvfs);
3854 3961 vfs_list_unlock();
3855 3962 return (found);
3856 3963 }
3857 3964
3858 3965 /*
3859 3966 * Allocate an entry in vfssw for a file system type
3860 3967 */
3861 3968 struct vfssw *
3862 3969 allocate_vfssw(const char *type)
3863 3970 {
3864 3971 struct vfssw *vswp;
3865 3972
3866 3973 if (type[0] == '\0' || strlen(type) + 1 > _ST_FSTYPSZ) {
3867 3974 /*
3868 3975 * The vfssw table uses the empty string to identify an
3869 3976 * available entry; we cannot add any type which has
3870 3977 * a leading NUL. The string length is limited to
3871 3978 * the size of the st_fstype array in struct stat.
3872 3979 */
3873 3980 return (NULL);
3874 3981 }
3875 3982
3876 3983 ASSERT(VFSSW_WRITE_LOCKED());
3877 3984 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++)
3878 3985 if (!ALLOCATED_VFSSW(vswp)) {
3879 3986 vswp->vsw_name = kmem_alloc(strlen(type) + 1, KM_SLEEP);
3880 3987 (void) strcpy(vswp->vsw_name, type);
3881 3988 ASSERT(vswp->vsw_count == 0);
3882 3989 vswp->vsw_count = 1;
3883 3990 mutex_init(&vswp->vsw_lock, NULL, MUTEX_DEFAULT, NULL);
3884 3991 return (vswp);
3885 3992 }
3886 3993 return (NULL);
3887 3994 }
3888 3995
3889 3996 /*
3890 3997 * Impose additional layer of translation between vfstype names
3891 3998 * and module names in the filesystem.
3892 3999 */
3893 4000 static const char *
3894 4001 vfs_to_modname(const char *vfstype)
3895 4002 {
3896 4003 if (strcmp(vfstype, "proc") == 0) {
3897 4004 vfstype = "procfs";
3898 4005 } else if (strcmp(vfstype, "fd") == 0) {
3899 4006 vfstype = "fdfs";
3900 4007 } else if (strncmp(vfstype, "nfs", 3) == 0) {
3901 4008 vfstype = "nfs";
3902 4009 }
3903 4010
3904 4011 return (vfstype);
3905 4012 }
3906 4013
3907 4014 /*
3908 4015 * Find a vfssw entry given a file system type name.
3909 4016 * Try to autoload the filesystem if it's not found.
3910 4017 * If it's installed, return the vfssw locked to prevent unloading.
3911 4018 */
3912 4019 struct vfssw *
3913 4020 vfs_getvfssw(const char *type)
3914 4021 {
3915 4022 struct vfssw *vswp;
3916 4023 const char *modname;
3917 4024
3918 4025 RLOCK_VFSSW();
3919 4026 vswp = vfs_getvfsswbyname(type);
3920 4027 modname = vfs_to_modname(type);
3921 4028
3922 4029 if (rootdir == NULL) {
3923 4030 /*
3924 4031 * If we haven't yet loaded the root file system, then our
3925 4032 * _init won't be called until later. Allocate vfssw entry,
3926 4033 * because mod_installfs won't be called.
3927 4034 */
3928 4035 if (vswp == NULL) {
3929 4036 RUNLOCK_VFSSW();
3930 4037 WLOCK_VFSSW();
3931 4038 if ((vswp = vfs_getvfsswbyname(type)) == NULL) {
3932 4039 if ((vswp = allocate_vfssw(type)) == NULL) {
3933 4040 WUNLOCK_VFSSW();
3934 4041 return (NULL);
3935 4042 }
3936 4043 }
3937 4044 WUNLOCK_VFSSW();
3938 4045 RLOCK_VFSSW();
3939 4046 }
3940 4047 if (!VFS_INSTALLED(vswp)) {
3941 4048 RUNLOCK_VFSSW();
3942 4049 (void) modloadonly("fs", modname);
3943 4050 } else
3944 4051 RUNLOCK_VFSSW();
3945 4052 return (vswp);
3946 4053 }
3947 4054
3948 4055 /*
3949 4056 * Try to load the filesystem. Before calling modload(), we drop
3950 4057 * our lock on the VFS switch table, and pick it up after the
3951 4058 * module is loaded. However, there is a potential race: the
3952 4059 * module could be unloaded after the call to modload() completes
3953 4060 * but before we pick up the lock and drive on. Therefore,
3954 4061 * we keep reloading the module until we've loaded the module
3955 4062 * _and_ we have the lock on the VFS switch table.
3956 4063 */
3957 4064 while (vswp == NULL || !VFS_INSTALLED(vswp)) {
3958 4065 RUNLOCK_VFSSW();
3959 4066 if (modload("fs", modname) == -1)
3960 4067 return (NULL);
3961 4068 RLOCK_VFSSW();
3962 4069 if (vswp == NULL)
3963 4070 if ((vswp = vfs_getvfsswbyname(type)) == NULL)
3964 4071 break;
3965 4072 }
3966 4073 RUNLOCK_VFSSW();
3967 4074
3968 4075 return (vswp);
3969 4076 }
3970 4077
3971 4078 /*
3972 4079 * Find a vfssw entry given a file system type name.
3973 4080 */
3974 4081 struct vfssw *
3975 4082 vfs_getvfsswbyname(const char *type)
3976 4083 {
3977 4084 struct vfssw *vswp;
3978 4085
3979 4086 ASSERT(VFSSW_LOCKED());
3980 4087 if (type == NULL || *type == '\0')
3981 4088 return (NULL);
3982 4089
3983 4090 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
3984 4091 if (strcmp(type, vswp->vsw_name) == 0) {
3985 4092 vfs_refvfssw(vswp);
3986 4093 return (vswp);
3987 4094 }
3988 4095 }
3989 4096
3990 4097 return (NULL);
3991 4098 }
3992 4099
3993 4100 /*
3994 4101 * Find a vfssw entry given a set of vfsops.
3995 4102 */
3996 4103 struct vfssw *
3997 4104 vfs_getvfsswbyvfsops(vfsops_t *vfsops)
3998 4105 {
3999 4106 struct vfssw *vswp;
4000 4107
4001 4108 RLOCK_VFSSW();
4002 4109 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
4003 4110 if (ALLOCATED_VFSSW(vswp) && &vswp->vsw_vfsops == vfsops) {
4004 4111 vfs_refvfssw(vswp);
4005 4112 RUNLOCK_VFSSW();
4006 4113 return (vswp);
4007 4114 }
4008 4115 }
4009 4116 RUNLOCK_VFSSW();
4010 4117
4011 4118 return (NULL);
4012 4119 }
4013 4120
4014 4121 /*
4015 4122 * Reference a vfssw entry.
4016 4123 */
4017 4124 void
4018 4125 vfs_refvfssw(struct vfssw *vswp)
4019 4126 {
4020 4127
4021 4128 mutex_enter(&vswp->vsw_lock);
4022 4129 vswp->vsw_count++;
4023 4130 mutex_exit(&vswp->vsw_lock);
4024 4131 }
4025 4132
4026 4133 /*
4027 4134 * Unreference a vfssw entry.
4028 4135 */
4029 4136 void
4030 4137 vfs_unrefvfssw(struct vfssw *vswp)
4031 4138 {
4032 4139
4033 4140 mutex_enter(&vswp->vsw_lock);
4034 4141 vswp->vsw_count--;
4035 4142 mutex_exit(&vswp->vsw_lock);
4036 4143 }
4037 4144
4038 4145 int sync_timeout = 30; /* timeout for syncing a page during panic */
4039 4146 int sync_timeleft; /* portion of sync_timeout remaining */
4040 4147
4041 4148 static int sync_retries = 20; /* number of retries when not making progress */
4042 4149 static int sync_triesleft; /* portion of sync_retries remaining */
4043 4150
4044 4151 static pgcnt_t old_pgcnt, new_pgcnt;
4045 4152 static int new_bufcnt, old_bufcnt;
4046 4153
4047 4154 /*
4048 4155 * Sync all of the mounted filesystems, and then wait for the actual i/o to
4049 4156 * complete. We wait by counting the number of dirty pages and buffers,
4050 4157 * pushing them out using bio_busy() and page_busy(), and then counting again.
4051 4158 * This routine is used during both the uadmin A_SHUTDOWN code as well as
4052 4159 * the SYNC phase of the panic code (see comments in panic.c). It should only
4053 4160 * be used after some higher-level mechanism has quiesced the system so that
4054 4161 * new writes are not being initiated while we are waiting for completion.
4055 4162 *
4056 4163 * To ensure finite running time, our algorithm uses two timeout mechanisms:
4057 4164 * sync_timeleft (a timer implemented by the omnipresent deadman() cyclic), and
4058 4165 * sync_triesleft (a progress counter used by the vfs_syncall() loop below).
4059 4166 * Together these ensure that syncing completes if our i/o paths are stuck.
4060 4167 * The counters are declared above so they can be found easily in the debugger.
4061 4168 *
4062 4169 * The sync_timeleft counter is reset by bio_busy() and page_busy() using the
4063 4170 * vfs_syncprogress() subroutine whenever we make progress through the lists of
4064 4171 * pages and buffers. It is decremented and expired by the deadman() cyclic.
4065 4172 * When vfs_syncall() decides it is done, we disable the deadman() counter by
4066 4173 * setting sync_timeleft to zero. This timer guards against vfs_syncall()
4067 4174 * deadlocking or hanging inside of a broken filesystem or driver routine.
4068 4175 *
4069 4176 * The sync_triesleft counter is updated by vfs_syncall() itself. If we make
4070 4177 * sync_retries consecutive calls to bio_busy() and page_busy() without
4071 4178 * decreasing either the number of dirty buffers or dirty pages below the
4072 4179 * lowest count we have seen so far, we give up and return from vfs_syncall().
4073 4180 *
4074 4181 * Each loop iteration ends with a call to delay() one second to allow time for
4075 4182 * i/o completion and to permit the user time to read our progress messages.
4076 4183 */
4077 4184 void
4078 4185 vfs_syncall(void)
4079 4186 {
4080 4187 if (rootdir == NULL && !modrootloaded)
4081 4188 return; /* panic during boot - no filesystems yet */
4082 4189
4083 4190 printf("syncing file systems...");
4084 4191 vfs_syncprogress();
4085 4192 sync();
4086 4193
4087 4194 vfs_syncprogress();
4088 4195 sync_triesleft = sync_retries;
4089 4196
4090 4197 old_bufcnt = new_bufcnt = INT_MAX;
4091 4198 old_pgcnt = new_pgcnt = ULONG_MAX;
4092 4199
4093 4200 while (sync_triesleft > 0) {
4094 4201 old_bufcnt = MIN(old_bufcnt, new_bufcnt);
4095 4202 old_pgcnt = MIN(old_pgcnt, new_pgcnt);
4096 4203
4097 4204 new_bufcnt = bio_busy(B_TRUE);
4098 4205 new_pgcnt = page_busy(B_TRUE);
4099 4206 vfs_syncprogress();
4100 4207
4101 4208 if (new_bufcnt == 0 && new_pgcnt == 0)
4102 4209 break;
4103 4210
4104 4211 if (new_bufcnt < old_bufcnt || new_pgcnt < old_pgcnt)
4105 4212 sync_triesleft = sync_retries;
4106 4213 else
4107 4214 sync_triesleft--;
4108 4215
4109 4216 if (new_bufcnt)
4110 4217 printf(" [%d]", new_bufcnt);
4111 4218 if (new_pgcnt)
4112 4219 printf(" %lu", new_pgcnt);
4113 4220
4114 4221 delay(hz);
4115 4222 }
4116 4223
4117 4224 if (new_bufcnt != 0 || new_pgcnt != 0)
4118 4225 printf(" done (not all i/o completed)\n");
4119 4226 else
4120 4227 printf(" done\n");
4121 4228
4122 4229 sync_timeleft = 0;
4123 4230 delay(hz);
4124 4231 }
4125 4232
4126 4233 /*
4127 4234 * If we are in the middle of the sync phase of panic, reset sync_timeleft to
4128 4235 * sync_timeout to indicate that we are making progress and the deadman()
4129 4236 * omnipresent cyclic should not yet time us out. Note that it is safe to
4130 4237 * store to sync_timeleft here since the deadman() is firing at high-level
4131 4238 * on top of us. If we are racing with the deadman(), either the deadman()
4132 4239 * will decrement the old value and then we will reset it, or we will
4133 4240 * reset it and then the deadman() will immediately decrement it. In either
4134 4241 * case, correct behavior results.
4135 4242 */
4136 4243 void
4137 4244 vfs_syncprogress(void)
4138 4245 {
4139 4246 if (panicstr)
4140 4247 sync_timeleft = sync_timeout;
4141 4248 }
4142 4249
4143 4250 /*
4144 4251 * Map VFS flags to statvfs flags. These shouldn't really be separate
4145 4252 * flags at all.
4146 4253 */
4147 4254 uint_t
4148 4255 vf_to_stf(uint_t vf)
4149 4256 {
4150 4257 uint_t stf = 0;
4151 4258
4152 4259 if (vf & VFS_RDONLY)
4153 4260 stf |= ST_RDONLY;
4154 4261 if (vf & VFS_NOSETUID)
4155 4262 stf |= ST_NOSUID;
4156 4263 if (vf & VFS_NOTRUNC)
4157 4264 stf |= ST_NOTRUNC;
4158 4265
4159 4266 return (stf);
4160 4267 }
4161 4268
4162 4269 /*
4163 4270 * Entries for (illegal) fstype 0.
4164 4271 */
4165 4272 /* ARGSUSED */
4166 4273 int
4167 4274 vfsstray_sync(struct vfs *vfsp, short arg, struct cred *cr)
4168 4275 {
4169 4276 cmn_err(CE_PANIC, "stray vfs operation");
4170 4277 return (0);
4171 4278 }
4172 4279
4173 4280 /*
4174 4281 * Entries for (illegal) fstype 0.
4175 4282 */
4176 4283 int
4177 4284 vfsstray(void)
4178 4285 {
4179 4286 cmn_err(CE_PANIC, "stray vfs operation");
4180 4287 return (0);
4181 4288 }
4182 4289
4183 4290 /*
4184 4291 * Support for dealing with forced UFS unmount and its interaction with
4185 4292 * LOFS. Could be used by any filesystem.
4186 4293 * See bug 1203132.
4187 4294 */
4188 4295 int
4189 4296 vfs_EIO(void)
4190 4297 {
4191 4298 return (EIO);
4192 4299 }
4193 4300
4194 4301 /*
4195 4302 * We've gotta define the op for sync separately, since the compiler gets
4196 4303 * confused if we mix and match ANSI and normal style prototypes when
4197 4304 * a "short" argument is present and spits out a warning.
4198 4305 */
4199 4306 /*ARGSUSED*/
4200 4307 int
4201 4308 vfs_EIO_sync(struct vfs *vfsp, short arg, struct cred *cr)
4202 4309 {
4203 4310 return (EIO);
4204 4311 }
4205 4312
4206 4313 vfs_t EIO_vfs;
4207 4314 vfsops_t *EIO_vfsops;
4208 4315
4209 4316 /*
4210 4317 * Called from startup() to initialize all loaded vfs's
4211 4318 */
4212 4319 void
4213 4320 vfsinit(void)
4214 4321 {
4215 4322 struct vfssw *vswp;
4216 4323 int error;
4217 4324 extern int vopstats_enabled;
4218 4325 extern void vopstats_startup();
4219 4326
4220 4327 static const fs_operation_def_t EIO_vfsops_template[] = {
4221 4328 VFSNAME_MOUNT, { .error = vfs_EIO },
4222 4329 VFSNAME_UNMOUNT, { .error = vfs_EIO },
4223 4330 VFSNAME_ROOT, { .error = vfs_EIO },
4224 4331 VFSNAME_STATVFS, { .error = vfs_EIO },
4225 4332 VFSNAME_SYNC, { .vfs_sync = vfs_EIO_sync },
4226 4333 VFSNAME_VGET, { .error = vfs_EIO },
4227 4334 VFSNAME_MOUNTROOT, { .error = vfs_EIO },
4228 4335 VFSNAME_FREEVFS, { .error = vfs_EIO },
4229 4336 VFSNAME_VNSTATE, { .error = vfs_EIO },
4230 4337 NULL, NULL
4231 4338 };
4232 4339
4233 4340 static const fs_operation_def_t stray_vfsops_template[] = {
4234 4341 VFSNAME_MOUNT, { .error = vfsstray },
4235 4342 VFSNAME_UNMOUNT, { .error = vfsstray },
4236 4343 VFSNAME_ROOT, { .error = vfsstray },
4237 4344 VFSNAME_STATVFS, { .error = vfsstray },
4238 4345 VFSNAME_SYNC, { .vfs_sync = vfsstray_sync },
4239 4346 VFSNAME_VGET, { .error = vfsstray },
4240 4347 VFSNAME_MOUNTROOT, { .error = vfsstray },
4241 4348 VFSNAME_FREEVFS, { .error = vfsstray },
4242 4349 VFSNAME_VNSTATE, { .error = vfsstray },
4243 4350 NULL, NULL
4244 4351 };
4245 4352
4246 4353 /* Create vfs cache */
4247 4354 vfs_cache = kmem_cache_create("vfs_cache", sizeof (struct vfs),
4248 4355 sizeof (uintptr_t), NULL, NULL, NULL, NULL, NULL, 0);
4249 4356
4250 4357 /* Initialize the vnode cache (file systems may use it during init). */
4251 4358 vn_create_cache();
4252 4359
4253 4360 /* Setup event monitor framework */
4254 4361 fem_init();
4255 4362
4256 4363 /* Initialize the dummy stray file system type. */
4257 4364 error = vfs_setfsops(0, stray_vfsops_template, NULL);
4258 4365
4259 4366 /* Initialize the dummy EIO file system. */
4260 4367 error = vfs_makefsops(EIO_vfsops_template, &EIO_vfsops);
4261 4368 if (error != 0) {
4262 4369 cmn_err(CE_WARN, "vfsinit: bad EIO vfs ops template");
4263 4370 /* Shouldn't happen, but not bad enough to panic */
4264 4371 }
4265 4372
4266 4373 VFS_INIT(&EIO_vfs, EIO_vfsops, (caddr_t)NULL);
4267 4374
4268 4375 /*
4269 4376 * Default EIO_vfs.vfs_flag to VFS_UNMOUNTED so a lookup
4270 4377 * on this vfs can immediately notice it's invalid.
4271 4378 */
4272 4379 EIO_vfs.vfs_flag |= VFS_UNMOUNTED;
4273 4380
4274 4381 /*
4275 4382 * Call the init routines of non-loadable filesystems only.
4276 4383 * Filesystems which are loaded as separate modules will be
4277 4384 * initialized by the module loading code instead.
4278 4385 */
4279 4386
4280 4387 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
4281 4388 RLOCK_VFSSW();
4282 4389 if (vswp->vsw_init != NULL)
4283 4390 (*vswp->vsw_init)(vswp - vfssw, vswp->vsw_name);
4284 4391 RUNLOCK_VFSSW();
4285 4392 }
4286 4393
4287 4394 vopstats_startup();
4288 4395
4289 4396 if (vopstats_enabled) {
4290 4397 /* EIO_vfs can collect stats, but we don't retrieve them */
4291 4398 initialize_vopstats(&EIO_vfs.vfs_vopstats);
4292 4399 EIO_vfs.vfs_fstypevsp = NULL;
4293 4400 EIO_vfs.vfs_vskap = NULL;
4294 4401 EIO_vfs.vfs_flag |= VFS_STATS;
4295 4402 }
4296 4403
4297 4404 xattr_init();
4298 4405
4299 4406 reparse_point_init();
4300 4407 }
4301 4408
4302 4409 vfs_t *
4303 4410 vfs_alloc(int kmflag)
4304 4411 {
4305 4412 vfs_t *vfsp;
4306 4413
4307 4414 vfsp = kmem_cache_alloc(vfs_cache, kmflag);
4308 4415
4309 4416 /*
4310 4417 * Do the simplest initialization here.
4311 4418 * Everything else gets done in vfs_init()
4312 4419 */
4313 4420 bzero(vfsp, sizeof (vfs_t));
4314 4421 return (vfsp);
4315 4422 }
4316 4423
4317 4424 void
4318 4425 vfs_free(vfs_t *vfsp)
4319 4426 {
4320 4427 /*
4321 4428 * One would be tempted to assert that "vfsp->vfs_count == 0".
4322 4429 * The problem is that this gets called out of domount() with
4323 4430 * a partially initialized vfs and a vfs_count of 1. This is
4324 4431 * also called from vfs_rele() with a vfs_count of 0. We can't
4325 4432 * call VFS_RELE() from domount() if VFS_MOUNT() hasn't successfully
4326 4433 * returned. This is because VFS_MOUNT() fully initializes the
4327 4434 * vfs structure and its associated data. VFS_RELE() will call
4328 4435 * VFS_FREEVFS() which may panic the system if the data structures
4329 4436 * aren't fully initialized from a successful VFS_MOUNT()).
4330 4437 */
4331 4438
4332 4439 /* If FEM was in use, make sure everything gets cleaned up */
4333 4440 if (vfsp->vfs_femhead) {
4334 4441 ASSERT(vfsp->vfs_femhead->femh_list == NULL);
4335 4442 mutex_destroy(&vfsp->vfs_femhead->femh_lock);
4336 4443 kmem_free(vfsp->vfs_femhead, sizeof (*(vfsp->vfs_femhead)));
4337 4444 vfsp->vfs_femhead = NULL;
4338 4445 }
4339 4446
4340 4447 if (vfsp->vfs_implp)
4341 4448 vfsimpl_teardown(vfsp);
4342 4449 sema_destroy(&vfsp->vfs_reflock);
4343 4450 kmem_cache_free(vfs_cache, vfsp);
4344 4451 }
4345 4452
4346 4453 /*
4347 4454 * Increments the vfs reference count by one atomically.
4348 4455 */
4349 4456 void
4350 4457 vfs_hold(vfs_t *vfsp)
4351 4458 {
4352 4459 atomic_inc_32(&vfsp->vfs_count);
4353 4460 ASSERT(vfsp->vfs_count != 0);
4354 4461 }
4355 4462
4356 4463 /*
4357 4464 * Decrements the vfs reference count by one atomically. When
4358 4465 * vfs reference count becomes zero, it calls the file system
4359 4466 * specific vfs_freevfs() to free up the resources.
4360 4467 */
4361 4468 void
4362 4469 vfs_rele(vfs_t *vfsp)
4363 4470 {
4364 4471 ASSERT(vfsp->vfs_count != 0);
4365 4472 if (atomic_dec_32_nv(&vfsp->vfs_count) == 0) {
4366 4473 VFS_FREEVFS(vfsp);
4367 4474 lofi_remove(vfsp);
4368 4475 if (vfsp->vfs_zone)
4369 4476 zone_rele_ref(&vfsp->vfs_implp->vi_zone_ref,
4370 4477 ZONE_REF_VFS);
4371 4478 vfs_freemnttab(vfsp);
4372 4479 vfs_free(vfsp);
4373 4480 }
4374 4481 }
4375 4482
4376 4483 /*
4377 4484 * Generic operations vector support.
4378 4485 *
4379 4486 * This is used to build operations vectors for both the vfs and vnode.
4380 4487 * It's normally called only when a file system is loaded.
4381 4488 *
4382 4489 * There are many possible algorithms for this, including the following:
4383 4490 *
4384 4491 * (1) scan the list of known operations; for each, see if the file system
4385 4492 * includes an entry for it, and fill it in as appropriate.
4386 4493 *
4387 4494 * (2) set up defaults for all known operations. scan the list of ops
4388 4495 * supplied by the file system; for each which is both supplied and
4389 4496 * known, fill it in.
4390 4497 *
4391 4498 * (3) sort the lists of known ops & supplied ops; scan the list, filling
4392 4499 * in entries as we go.
4393 4500 *
4394 4501 * we choose (1) for simplicity, and because performance isn't critical here.
4395 4502 * note that (2) could be sped up using a precomputed hash table on known ops.
4396 4503 * (3) could be faster than either, but only if the lists were very large or
4397 4504 * supplied in sorted order.
4398 4505 *
4399 4506 */
4400 4507
4401 4508 int
4402 4509 fs_build_vector(void *vector, int *unused_ops,
4403 4510 const fs_operation_trans_def_t *translation,
4404 4511 const fs_operation_def_t *operations)
4405 4512 {
4406 4513 int i, num_trans, num_ops, used;
4407 4514
4408 4515 /*
4409 4516 * Count the number of translations and the number of supplied
4410 4517 * operations.
4411 4518 */
4412 4519
4413 4520 {
4414 4521 const fs_operation_trans_def_t *p;
4415 4522
4416 4523 for (num_trans = 0, p = translation;
4417 4524 p->name != NULL;
4418 4525 num_trans++, p++)
4419 4526 ;
4420 4527 }
4421 4528
4422 4529 {
4423 4530 const fs_operation_def_t *p;
4424 4531
4425 4532 for (num_ops = 0, p = operations;
4426 4533 p->name != NULL;
4427 4534 num_ops++, p++)
4428 4535 ;
4429 4536 }
4430 4537
4431 4538 /* Walk through each operation known to our caller. There will be */
4432 4539 /* one entry in the supplied "translation table" for each. */
4433 4540
4434 4541 used = 0;
4435 4542
4436 4543 for (i = 0; i < num_trans; i++) {
4437 4544 int j, found;
4438 4545 char *curname;
4439 4546 fs_generic_func_p result;
4440 4547 fs_generic_func_p *location;
4441 4548
4442 4549 curname = translation[i].name;
4443 4550
4444 4551 /* Look for a matching operation in the list supplied by the */
4445 4552 /* file system. */
4446 4553
4447 4554 found = 0;
4448 4555
4449 4556 for (j = 0; j < num_ops; j++) {
4450 4557 if (strcmp(operations[j].name, curname) == 0) {
4451 4558 used++;
4452 4559 found = 1;
4453 4560 break;
4454 4561 }
4455 4562 }
4456 4563
4457 4564 /*
4458 4565 * If the file system is using a "placeholder" for default
4459 4566 * or error functions, grab the appropriate function out of
4460 4567 * the translation table. If the file system didn't supply
4461 4568 * this operation at all, use the default function.
4462 4569 */
4463 4570
4464 4571 if (found) {
4465 4572 result = operations[j].func.fs_generic;
4466 4573 if (result == fs_default) {
4467 4574 result = translation[i].defaultFunc;
4468 4575 } else if (result == fs_error) {
4469 4576 result = translation[i].errorFunc;
4470 4577 } else if (result == NULL) {
4471 4578 /* Null values are PROHIBITED */
4472 4579 return (EINVAL);
4473 4580 }
4474 4581 } else {
4475 4582 result = translation[i].defaultFunc;
4476 4583 }
4477 4584
4478 4585 /* Now store the function into the operations vector. */
4479 4586
4480 4587 location = (fs_generic_func_p *)
4481 4588 (((char *)vector) + translation[i].offset);
4482 4589
4483 4590 *location = result;
4484 4591 }
4485 4592
4486 4593 *unused_ops = num_ops - used;
4487 4594
4488 4595 return (0);
4489 4596 }
4490 4597
4491 4598 /* Placeholder functions, should never be called. */
4492 4599
4493 4600 int
4494 4601 fs_error(void)
4495 4602 {
4496 4603 cmn_err(CE_PANIC, "fs_error called");
4497 4604 return (0);
4498 4605 }
4499 4606
4500 4607 int
4501 4608 fs_default(void)
4502 4609 {
4503 4610 cmn_err(CE_PANIC, "fs_default called");
4504 4611 return (0);
4505 4612 }
4506 4613
4507 4614 #ifdef __sparc
4508 4615
4509 4616 /*
4510 4617 * Part of the implementation of booting off a mirrored root
4511 4618 * involves a change of dev_t for the root device. To
4512 4619 * accomplish this, first remove the existing hash table
4513 4620 * entry for the root device, convert to the new dev_t,
4514 4621 * then re-insert in the hash table at the head of the list.
4515 4622 */
4516 4623 void
4517 4624 vfs_root_redev(vfs_t *vfsp, dev_t ndev, int fstype)
4518 4625 {
4519 4626 vfs_list_lock();
4520 4627
4521 4628 vfs_hash_remove(vfsp);
4522 4629
4523 4630 vfsp->vfs_dev = ndev;
4524 4631 vfs_make_fsid(&vfsp->vfs_fsid, ndev, fstype);
4525 4632
4526 4633 vfs_hash_add(vfsp, 1);
4527 4634
4528 4635 vfs_list_unlock();
4529 4636 }
4530 4637
4531 4638 #else /* x86 NEWBOOT */
4532 4639
4533 4640 #if defined(__x86)
4534 4641 extern int hvmboot_rootconf();
4535 4642 #endif /* __x86 */
4536 4643
4537 4644 extern ib_boot_prop_t *iscsiboot_prop;
4538 4645
4539 4646 int
4540 4647 rootconf()
4541 4648 {
4542 4649 int error;
4543 4650 struct vfssw *vsw;
4544 4651 extern void pm_init();
4545 4652 char *fstyp, *fsmod;
4546 4653 int ret = -1;
4547 4654
4548 4655 getrootfs(&fstyp, &fsmod);
4549 4656
4550 4657 #if defined(__x86)
4551 4658 /*
4552 4659 * hvmboot_rootconf() is defined in the hvm_bootstrap misc module,
4553 4660 * which lives in /platform/i86hvm, and hence is only available when
4554 4661 * booted in an x86 hvm environment. If the hvm_bootstrap misc module
4555 4662 * is not available then the modstub for this function will return 0.
4556 4663 * If the hvm_bootstrap misc module is available it will be loaded
4557 4664 * and hvmboot_rootconf() will be invoked.
4558 4665 */
4559 4666 if (error = hvmboot_rootconf())
4560 4667 return (error);
4561 4668 #endif /* __x86 */
4562 4669
4563 4670 if (error = clboot_rootconf())
4564 4671 return (error);
4565 4672
4566 4673 if (modload("fs", fsmod) == -1)
4567 4674 panic("Cannot _init %s module", fsmod);
4568 4675
4569 4676 RLOCK_VFSSW();
4570 4677 vsw = vfs_getvfsswbyname(fstyp);
4571 4678 RUNLOCK_VFSSW();
4572 4679 if (vsw == NULL) {
4573 4680 cmn_err(CE_CONT, "Cannot find %s filesystem\n", fstyp);
4574 4681 return (ENXIO);
4575 4682 }
4576 4683 VFS_INIT(rootvfs, &vsw->vsw_vfsops, 0);
4577 4684 VFS_HOLD(rootvfs);
4578 4685
4579 4686 /* always mount readonly first */
4580 4687 rootvfs->vfs_flag |= VFS_RDONLY;
4581 4688
4582 4689 pm_init();
4583 4690
4584 4691 if (netboot && iscsiboot_prop) {
4585 4692 cmn_err(CE_WARN, "NFS boot and iSCSI boot"
4586 4693 " shouldn't happen in the same time");
4587 4694 return (EINVAL);
4588 4695 }
4589 4696
4590 4697 if (netboot || iscsiboot_prop) {
4591 4698 ret = strplumb();
4592 4699 if (ret != 0) {
4593 4700 cmn_err(CE_WARN, "Cannot plumb network device %d", ret);
4594 4701 return (EFAULT);
4595 4702 }
4596 4703 }
4597 4704
4598 4705 if ((ret == 0) && iscsiboot_prop) {
4599 4706 ret = modload("drv", "iscsi");
4600 4707 /* -1 indicates fail */
4601 4708 if (ret == -1) {
4602 4709 cmn_err(CE_WARN, "Failed to load iscsi module");
4603 4710 iscsi_boot_prop_free();
4604 4711 return (EINVAL);
4605 4712 } else {
4606 4713 if (!i_ddi_attach_pseudo_node("iscsi")) {
4607 4714 cmn_err(CE_WARN,
4608 4715 "Failed to attach iscsi driver");
4609 4716 iscsi_boot_prop_free();
4610 4717 return (ENODEV);
4611 4718 }
4612 4719 }
4613 4720 }
4614 4721
4615 4722 error = VFS_MOUNTROOT(rootvfs, ROOT_INIT);
4616 4723 vfs_unrefvfssw(vsw);
4617 4724 rootdev = rootvfs->vfs_dev;
4618 4725
4619 4726 if (error)
4620 4727 cmn_err(CE_CONT, "Cannot mount root on %s fstype %s\n",
4621 4728 rootfs.bo_name, fstyp);
4622 4729 else
4623 4730 cmn_err(CE_CONT, "?root on %s fstype %s\n",
4624 4731 rootfs.bo_name, fstyp);
4625 4732 return (error);
4626 4733 }
4627 4734
4628 4735 /*
4629 4736 * XXX this is called by nfs only and should probably be removed
4630 4737 * If booted with ASKNAME, prompt on the console for a filesystem
4631 4738 * name and return it.
4632 4739 */
4633 4740 void
4634 4741 getfsname(char *askfor, char *name, size_t namelen)
4635 4742 {
4636 4743 if (boothowto & RB_ASKNAME) {
4637 4744 printf("%s name: ", askfor);
4638 4745 console_gets(name, namelen);
4639 4746 }
4640 4747 }
4641 4748
4642 4749 /*
4643 4750 * Init the root filesystem type (rootfs.bo_fstype) from the "fstype"
4644 4751 * property.
4645 4752 *
4646 4753 * Filesystem types starting with the prefix "nfs" are diskless clients;
4647 4754 * init the root filename name (rootfs.bo_name), too.
4648 4755 *
4649 4756 * If we are booting via NFS we currently have these options:
4650 4757 * nfs - dynamically choose NFS V2, V3, or V4 (default)
4651 4758 * nfs2 - force NFS V2
4652 4759 * nfs3 - force NFS V3
4653 4760 * nfs4 - force NFS V4
4654 4761 * Because we need to maintain backward compatibility with the naming
4655 4762 * convention that the NFS V2 filesystem name is "nfs" (see vfs_conf.c)
4656 4763 * we need to map "nfs" => "nfsdyn" and "nfs2" => "nfs". The dynamic
4657 4764 * nfs module will map the type back to either "nfs", "nfs3", or "nfs4".
4658 4765 * This is only for root filesystems, all other uses such as cachefs
4659 4766 * will expect that "nfs" == NFS V2.
4660 4767 */
4661 4768 static void
4662 4769 getrootfs(char **fstypp, char **fsmodp)
4663 4770 {
4664 4771 extern char *strplumb_get_netdev_path(void);
4665 4772 char *propstr = NULL;
4666 4773
4667 4774 /*
4668 4775 * Check fstype property; for diskless it should be one of "nfs",
4669 4776 * "nfs2", "nfs3" or "nfs4".
4670 4777 */
4671 4778 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4672 4779 DDI_PROP_DONTPASS, "fstype", &propstr)
4673 4780 == DDI_SUCCESS) {
4674 4781 (void) strncpy(rootfs.bo_fstype, propstr, BO_MAXFSNAME);
4675 4782 ddi_prop_free(propstr);
4676 4783
4677 4784 /*
4678 4785 * if the boot property 'fstype' is not set, but 'zfs-bootfs' is set,
4679 4786 * assume the type of this root filesystem is 'zfs'.
4680 4787 */
4681 4788 } else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4682 4789 DDI_PROP_DONTPASS, "zfs-bootfs", &propstr)
4683 4790 == DDI_SUCCESS) {
4684 4791 (void) strncpy(rootfs.bo_fstype, "zfs", BO_MAXFSNAME);
4685 4792 ddi_prop_free(propstr);
4686 4793 }
4687 4794
4688 4795 if (strncmp(rootfs.bo_fstype, "nfs", 3) != 0) {
4689 4796 *fstypp = *fsmodp = rootfs.bo_fstype;
4690 4797 return;
4691 4798 }
4692 4799
4693 4800 ++netboot;
4694 4801
4695 4802 if (strcmp(rootfs.bo_fstype, "nfs2") == 0)
4696 4803 (void) strcpy(rootfs.bo_fstype, "nfs");
4697 4804 else if (strcmp(rootfs.bo_fstype, "nfs") == 0)
4698 4805 (void) strcpy(rootfs.bo_fstype, "nfsdyn");
4699 4806
4700 4807 /*
4701 4808 * check if path to network interface is specified in bootpath
4702 4809 * or by a hypervisor domain configuration file.
4703 4810 * XXPV - enable strlumb_get_netdev_path()
4704 4811 */
4705 4812 if (ddi_prop_exists(DDI_DEV_T_ANY, ddi_root_node(), DDI_PROP_DONTPASS,
4706 4813 "xpv-nfsroot")) {
4707 4814 (void) strcpy(rootfs.bo_name, "/xpvd/xnf@0");
4708 4815 } else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4709 4816 DDI_PROP_DONTPASS, "bootpath", &propstr)
4710 4817 == DDI_SUCCESS) {
4711 4818 (void) strncpy(rootfs.bo_name, propstr, BO_MAXOBJNAME);
4712 4819 ddi_prop_free(propstr);
4713 4820 } else {
4714 4821 /* attempt to determine netdev_path via boot_mac address */
4715 4822 netdev_path = strplumb_get_netdev_path();
4716 4823 if (netdev_path == NULL)
4717 4824 panic("cannot find boot network interface");
4718 4825 (void) strncpy(rootfs.bo_name, netdev_path, BO_MAXOBJNAME);
4719 4826 }
4720 4827 *fstypp = rootfs.bo_fstype;
4721 4828 *fsmodp = "nfs";
4722 4829 }
4723 4830 #endif
4724 4831
4725 4832 /*
4726 4833 * VFS feature routines
4727 4834 */
4728 4835
4729 4836 #define VFTINDEX(feature) (((feature) >> 32) & 0xFFFFFFFF)
4730 4837 #define VFTBITS(feature) ((feature) & 0xFFFFFFFFLL)
4731 4838
4732 4839 /* Register a feature in the vfs */
4733 4840 void
4734 4841 vfs_set_feature(vfs_t *vfsp, vfs_feature_t feature)
4735 4842 {
4736 4843 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4737 4844 if (vfsp->vfs_implp == NULL)
4738 4845 return;
4739 4846
4740 4847 vfsp->vfs_featureset[VFTINDEX(feature)] |= VFTBITS(feature);
4741 4848 }
4742 4849
4743 4850 void
4744 4851 vfs_clear_feature(vfs_t *vfsp, vfs_feature_t feature)
4745 4852 {
4746 4853 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4747 4854 if (vfsp->vfs_implp == NULL)
4748 4855 return;
4749 4856 vfsp->vfs_featureset[VFTINDEX(feature)] &= VFTBITS(~feature);
4750 4857 }
4751 4858
4752 4859 /*
4753 4860 * Query a vfs for a feature.
4754 4861 * Returns 1 if feature is present, 0 if not
4755 4862 */
4756 4863 int
4757 4864 vfs_has_feature(vfs_t *vfsp, vfs_feature_t feature)
4758 4865 {
4759 4866 int ret = 0;
4760 4867
4761 4868 /* Note that vfs_featureset[] is found in *vfsp->vfs_implp */
4762 4869 if (vfsp->vfs_implp == NULL)
4763 4870 return (ret);
4764 4871
4765 4872 if (vfsp->vfs_featureset[VFTINDEX(feature)] & VFTBITS(feature))
4766 4873 ret = 1;
4767 4874
4768 4875 return (ret);
4769 4876 }
4770 4877
4771 4878 /*
4772 4879 * Propagate feature set from one vfs to another
4773 4880 */
4774 4881 void
4775 4882 vfs_propagate_features(vfs_t *from, vfs_t *to)
4776 4883 {
4777 4884 int i;
4778 4885
4779 4886 if (to->vfs_implp == NULL || from->vfs_implp == NULL)
4780 4887 return;
4781 4888
4782 4889 for (i = 1; i <= to->vfs_featureset[0]; i++) {
4783 4890 to->vfs_featureset[i] = from->vfs_featureset[i];
4784 4891 }
4785 4892 }
4786 4893
4787 4894 #define LOFINODE_PATH "/dev/lofi/%d"
4788 4895
4789 4896 /*
4790 4897 * Return the vnode for the lofi node if there's a lofi mount in place.
4791 4898 * Returns -1 when there's no lofi node, 0 on success, and > 0 on
4792 4899 * failure.
4793 4900 */
4794 4901 int
4795 4902 vfs_get_lofi(vfs_t *vfsp, vnode_t **vpp)
4796 4903 {
4797 4904 char *path = NULL;
4798 4905 int strsize;
4799 4906 int err;
4800 4907
4801 4908 if (vfsp->vfs_lofi_minor == 0) {
4802 4909 *vpp = NULL;
4803 4910 return (-1);
4804 4911 }
4805 4912
4806 4913 strsize = snprintf(NULL, 0, LOFINODE_PATH, vfsp->vfs_lofi_minor);
4807 4914 path = kmem_alloc(strsize + 1, KM_SLEEP);
4808 4915 (void) snprintf(path, strsize + 1, LOFINODE_PATH, vfsp->vfs_lofi_minor);
4809 4916
4810 4917 /*
4811 4918 * We may be inside a zone, so we need to use the /dev path, but
4812 4919 * it's created asynchronously, so we wait here.
4813 4920 */
4814 4921 for (;;) {
4815 4922 err = lookupname(path, UIO_SYSSPACE, FOLLOW, NULLVPP, vpp);
4816 4923
4817 4924 if (err != ENOENT)
4818 4925 break;
4819 4926
4820 4927 if ((err = delay_sig(hz / 8)) == EINTR)
4821 4928 break;
4822 4929 }
4823 4930
4824 4931 if (err)
4825 4932 *vpp = NULL;
4826 4933
4827 4934 kmem_free(path, strsize + 1);
4828 4935 return (err);
4829 4936 }
↓ open down ↓ |
1006 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX