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