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