Print this page
PSARC 2008/290 lofi mount
6384817 Need persistent lofi based mounts and direct mount(1m) support for lofi
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/fs/ufs/ufs_vfsops.c
+++ new/usr/src/uts/common/fs/ufs/ufs_vfsops.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.
|
↓ open down ↓ |
11 lines elided |
↑ open up ↑ |
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 - * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
22 + * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 27 /* All Rights Reserved */
28 28
29 29 /*
30 30 * University Copyright- Copyright (c) 1982, 1986, 1988
31 31 * The Regents of the University of California
32 32 * All Rights Reserved
33 33 *
34 34 * University Acknowledgment- Portions of this document are derived from
35 35 * software developed by the University of California, Berkeley, and its
36 36 * contributors.
37 37 */
38 38
39 39
40 -#pragma ident "@(#)ufs_vfsops.c 2.275 07/10/25 SMI"
40 +#pragma ident "@(#)ufs_vfsops.c 2.276 08/05/07 SMI"
41 41
42 42 #include <sys/types.h>
43 43 #include <sys/t_lock.h>
44 44 #include <sys/param.h>
45 45 #include <sys/systm.h>
46 46 #include <sys/bitmap.h>
47 47 #include <sys/sysmacros.h>
48 48 #include <sys/kmem.h>
49 49 #include <sys/signal.h>
50 50 #include <sys/user.h>
51 51 #include <sys/proc.h>
52 52 #include <sys/disp.h>
53 53 #include <sys/buf.h>
54 54 #include <sys/pathname.h>
55 55 #include <sys/vfs.h>
56 56 #include <sys/vfs_opreg.h>
57 57 #include <sys/vnode.h>
58 58 #include <sys/file.h>
59 59 #include <sys/atomic.h>
60 60 #include <sys/uio.h>
61 61 #include <sys/dkio.h>
62 62 #include <sys/cred.h>
63 63 #include <sys/conf.h>
64 64 #include <sys/dnlc.h>
65 65 #include <sys/kstat.h>
66 66 #include <sys/acl.h>
67 67 #include <sys/fs/ufs_fsdir.h>
68 68 #include <sys/fs/ufs_fs.h>
69 69 #include <sys/fs/ufs_inode.h>
70 70 #include <sys/fs/ufs_mount.h>
71 71 #include <sys/fs/ufs_acl.h>
72 72 #include <sys/fs/ufs_panic.h>
73 73 #include <sys/fs/ufs_bio.h>
74 74 #include <sys/fs/ufs_quota.h>
75 75 #include <sys/fs/ufs_log.h>
76 76 #undef NFS
77 77 #include <sys/statvfs.h>
78 78 #include <sys/mount.h>
79 79 #include <sys/mntent.h>
80 80 #include <sys/swap.h>
81 81 #include <sys/errno.h>
82 82 #include <sys/debug.h>
83 83 #include "fs/fs_subr.h"
84 84 #include <sys/cmn_err.h>
85 85 #include <sys/dnlc.h>
86 86 #include <sys/fssnap_if.h>
87 87 #include <sys/sunddi.h>
88 88 #include <sys/bootconf.h>
89 89 #include <sys/policy.h>
90 90 #include <sys/zone.h>
91 91
92 92 /*
93 93 * This is the loadable module wrapper.
94 94 */
95 95 #include <sys/modctl.h>
96 96
97 97 int ufsfstype;
98 98 vfsops_t *ufs_vfsops;
99 99 static int ufsinit(int, char *);
100 100 static int mountfs();
101 101 extern int highbit();
102 102 extern struct instats ins;
103 103 extern struct vnode *common_specvp(struct vnode *vp);
104 104 extern vfs_t EIO_vfs;
105 105
106 106 struct dquot *dquot, *dquotNDQUOT;
107 107
108 108 /*
109 109 * Cylinder group summary information handling tunable.
110 110 * This defines when these deltas get logged.
111 111 * If the number of cylinders in the file system is over the
112 112 * tunable then we log csum updates. Otherwise the updates are only
113 113 * done for performance on unmount. After a panic they can be
114 114 * quickly constructed during mounting. See ufs_construct_si()
115 115 * called from ufs_getsummaryinfo().
116 116 *
117 117 * This performance feature can of course be disabled by setting
118 118 * ufs_ncg_log to 0, and fully enabled by setting it to 0xffffffff.
119 119 */
120 120 #define UFS_LOG_NCG_DEFAULT 10000
121 121 uint32_t ufs_ncg_log = UFS_LOG_NCG_DEFAULT;
122 122
123 123 /*
124 124 * ufs_clean_root indicates whether the root fs went down cleanly
125 125 */
126 126 static int ufs_clean_root = 0;
127 127
128 128 /*
129 129 * UFS Mount options table
130 130 */
131 131 static char *intr_cancel[] = { MNTOPT_NOINTR, NULL };
132 132 static char *nointr_cancel[] = { MNTOPT_INTR, NULL };
133 133 static char *forcedirectio_cancel[] = { MNTOPT_NOFORCEDIRECTIO, NULL };
134 134 static char *noforcedirectio_cancel[] = { MNTOPT_FORCEDIRECTIO, NULL };
135 135 static char *largefiles_cancel[] = { MNTOPT_NOLARGEFILES, NULL };
136 136 static char *nolargefiles_cancel[] = { MNTOPT_LARGEFILES, NULL };
137 137 static char *logging_cancel[] = { MNTOPT_NOLOGGING, NULL };
138 138 static char *nologging_cancel[] = { MNTOPT_LOGGING, NULL };
139 139 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
140 140 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
141 141 static char *quota_cancel[] = { MNTOPT_NOQUOTA, NULL };
142 142 static char *noquota_cancel[] = { MNTOPT_QUOTA, NULL };
143 143 static char *dfratime_cancel[] = { MNTOPT_NODFRATIME, NULL };
144 144 static char *nodfratime_cancel[] = { MNTOPT_DFRATIME, NULL };
145 145
146 146 static mntopt_t mntopts[] = {
147 147 /*
148 148 * option name cancel option default arg flags
149 149 * ufs arg flag
150 150 */
151 151 { MNTOPT_INTR, intr_cancel, NULL, MO_DEFAULT,
152 152 (void *)0 },
153 153 { MNTOPT_NOINTR, nointr_cancel, NULL, 0,
154 154 (void *)UFSMNT_NOINTR },
155 155 { MNTOPT_SYNCDIR, NULL, NULL, 0,
156 156 (void *)UFSMNT_SYNCDIR },
157 157 { MNTOPT_FORCEDIRECTIO, forcedirectio_cancel, NULL, 0,
158 158 (void *)UFSMNT_FORCEDIRECTIO },
159 159 { MNTOPT_NOFORCEDIRECTIO, noforcedirectio_cancel, NULL, 0,
160 160 (void *)UFSMNT_NOFORCEDIRECTIO },
161 161 { MNTOPT_NOSETSEC, NULL, NULL, 0,
162 162 (void *)UFSMNT_NOSETSEC },
163 163 { MNTOPT_LARGEFILES, largefiles_cancel, NULL, MO_DEFAULT,
164 164 (void *)UFSMNT_LARGEFILES },
165 165 { MNTOPT_NOLARGEFILES, nolargefiles_cancel, NULL, 0,
166 166 (void *)0 },
167 167 { MNTOPT_LOGGING, logging_cancel, NULL, MO_TAG,
168 168 (void *)UFSMNT_LOGGING },
169 169 { MNTOPT_NOLOGGING, nologging_cancel, NULL,
170 170 MO_NODISPLAY|MO_DEFAULT|MO_TAG, (void *)0 },
171 171 { MNTOPT_QUOTA, quota_cancel, NULL, MO_IGNORE,
172 172 (void *)0 },
173 173 { MNTOPT_NOQUOTA, noquota_cancel, NULL,
174 174 MO_NODISPLAY|MO_DEFAULT, (void *)0 },
175 175 { MNTOPT_GLOBAL, NULL, NULL, 0,
176 176 (void *)0 },
177 177 { MNTOPT_XATTR, xattr_cancel, NULL, MO_DEFAULT,
178 178 (void *)0 },
179 179 { MNTOPT_NOXATTR, noxattr_cancel, NULL, 0,
180 180 (void *)0 },
181 181 { MNTOPT_NOATIME, NULL, NULL, 0,
182 182 (void *)UFSMNT_NOATIME },
183 183 { MNTOPT_DFRATIME, dfratime_cancel, NULL, 0,
184 184 (void *)0 },
185 185 { MNTOPT_NODFRATIME, nodfratime_cancel, NULL,
186 186 MO_NODISPLAY|MO_DEFAULT, (void *)UFSMNT_NODFRATIME },
187 187 { MNTOPT_ONERROR, NULL, UFSMNT_ONERROR_PANIC_STR,
188 188 MO_DEFAULT|MO_HASVALUE, (void *)0 },
189 189 };
190 190
191 191 static mntopts_t ufs_mntopts = {
192 192 sizeof (mntopts) / sizeof (mntopt_t),
193 193 mntopts
194 194 };
195 195
196 196 static vfsdef_t vfw = {
197 197 VFSDEF_VERSION,
198 198 "ufs",
199 199 ufsinit,
200 200 VSW_HASPROTO|VSW_CANREMOUNT|VSW_STATS,
201 201 &ufs_mntopts
202 202 };
203 203
204 204 /*
205 205 * Module linkage information for the kernel.
206 206 */
207 207 extern struct mod_ops mod_fsops;
208 208
209 209 static struct modlfs modlfs = {
210 210 &mod_fsops, "filesystem for ufs", &vfw
211 211 };
212 212
213 213 static struct modlinkage modlinkage = {
214 214 MODREV_1, (void *)&modlfs, NULL
215 215 };
216 216
217 217 /*
218 218 * An attempt has been made to make this module unloadable. In order to
219 219 * test it, we need a system in which the root fs is NOT ufs. THIS HAS NOT
220 220 * BEEN DONE
221 221 */
222 222
223 223 extern kstat_t *ufs_inode_kstat;
224 224 extern uint_t ufs_lockfs_key;
225 225 extern void ufs_lockfs_tsd_destructor(void *);
226 226 extern uint_t bypass_snapshot_throttle_key;
227 227
228 228 int
229 229 _init(void)
230 230 {
231 231 /*
232 232 * Create an index into the per thread array so that any thread doing
233 233 * VOP will have a lockfs mark on it.
234 234 */
235 235 tsd_create(&ufs_lockfs_key, ufs_lockfs_tsd_destructor);
236 236 tsd_create(&bypass_snapshot_throttle_key, NULL);
237 237 return (mod_install(&modlinkage));
238 238 }
239 239
240 240 int
241 241 _fini(void)
242 242 {
243 243 return (EBUSY);
244 244 }
245 245
246 246 int
247 247 _info(struct modinfo *modinfop)
248 248 {
249 249 return (mod_info(&modlinkage, modinfop));
250 250 }
251 251
252 252 extern struct vnode *makespecvp(dev_t dev, vtype_t type);
253 253
254 254 extern kmutex_t ufs_scan_lock;
255 255
256 256 static int mountfs(struct vfs *, enum whymountroot, struct vnode *, char *,
257 257 struct cred *, int, void *, int);
258 258
|
↓ open down ↓ |
208 lines elided |
↑ open up ↑ |
259 259
260 260 static int
261 261 ufs_mount(struct vfs *vfsp, struct vnode *mvp, struct mounta *uap,
262 262 struct cred *cr)
263 263
264 264 {
265 265 char *data = uap->dataptr;
266 266 int datalen = uap->datalen;
267 267 dev_t dev;
268 268 struct vnode *bvp;
269 + struct vnode *lvp = NULL;
270 + struct vnode *svp = NULL;
269 271 struct pathname dpn;
270 272 int error;
271 273 enum whymountroot why = ROOT_INIT;
272 274 struct ufs_args args;
273 275 int oflag, aflag;
274 276 int fromspace = (uap->flags & MS_SYSSPACE) ?
275 277 UIO_SYSSPACE : UIO_USERSPACE;
276 278
277 279 if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
278 280 return (error);
279 281
280 282 if (mvp->v_type != VDIR)
281 283 return (ENOTDIR);
282 284
283 285 mutex_enter(&mvp->v_lock);
284 286 if ((uap->flags & MS_REMOUNT) == 0 &&
285 287 (uap->flags & MS_OVERLAY) == 0 &&
286 288 (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
287 289 mutex_exit(&mvp->v_lock);
288 290 return (EBUSY);
289 291 }
290 292 mutex_exit(&mvp->v_lock);
291 293
292 294 /*
293 295 * Get arguments
294 296 */
295 297 bzero(&args, sizeof (args));
296 298 if ((uap->flags & MS_DATA) && data != NULL && datalen != 0) {
297 299 int copy_result = 0;
298 300
299 301 if (datalen > sizeof (args))
300 302 return (EINVAL);
|
↓ open down ↓ |
22 lines elided |
↑ open up ↑ |
301 303 if (uap->flags & MS_SYSSPACE)
302 304 bcopy(data, &args, datalen);
303 305 else
304 306 copy_result = copyin(data, &args, datalen);
305 307 if (copy_result)
306 308 return (EFAULT);
307 309 datalen = sizeof (struct ufs_args);
308 310 } else {
309 311 datalen = 0;
310 312 }
313 +
314 + if ((vfsp->vfs_flag & VFS_RDONLY) != 0 ||
315 + (uap->flags & MS_RDONLY) != 0) {
316 + oflag = FREAD;
317 + aflag = VREAD;
318 + } else {
319 + oflag = FREAD | FWRITE;
320 + aflag = VREAD | VWRITE;
321 + }
322 +
311 323 /*
312 324 * Read in the mount point pathname
313 325 * (so we can record the directory the file system was last mounted on).
314 326 */
315 327 if (error = pn_get(uap->dir, fromspace, &dpn))
316 328 return (error);
317 329
318 330 /*
319 331 * Resolve path name of special file being mounted.
320 332 */
321 - if (error = lookupname(uap->spec, fromspace, FOLLOW, NULL, &bvp)) {
333 + if (error = lookupname(uap->spec, fromspace, FOLLOW, NULL, &svp)) {
322 334 pn_free(&dpn);
323 335 return (error);
324 336 }
325 - if (bvp->v_type != VBLK) {
326 - VN_RELE(bvp);
337 +
338 + error = vfs_get_lofi(vfsp, &lvp);
339 +
340 + if (error > 0) {
341 + VN_RELE(svp);
327 342 pn_free(&dpn);
328 - return (ENOTBLK);
343 + return (error);
344 + } else if (error == 0) {
345 + bvp = lvp;
346 + } else {
347 + bvp = svp;
348 +
349 + if (svp->v_type != VBLK) {
350 + VN_RELE(svp);
351 + pn_free(&dpn);
352 + return (ENOTBLK);
353 + }
354 +
355 + if ((error = secpolicy_spec_open(cr, svp, oflag)) != 0) {
356 + VN_RELE(svp);
357 + pn_free(&dpn);
358 + return (error);
359 + }
329 360 }
361 +
330 362 dev = bvp->v_rdev;
331 363 if (getmajor(dev) >= devcnt) {
332 364 pn_free(&dpn);
333 - VN_RELE(bvp);
365 + if (lvp != NULL)
366 + VN_RELE(lvp);
367 + if (svp != NULL)
368 + VN_RELE(svp);
334 369 return (ENXIO);
335 370 }
336 371 if (uap->flags & MS_REMOUNT)
337 372 why = ROOT_REMOUNT;
338 373
339 374 /*
340 - * In SunCluster, requests to a global device are satisfied by
341 - * a local device. We substitute the global pxfs node with a
342 - * local spec node here.
375 + * In SunCluster, requests to a global device are satisfied by a
376 + * local device. We substitute the global pxfs node with a local
377 + * spec node here.
378 + *
379 + * Open device/file mounted on. We need this to check whether
380 + * the caller has sufficient rights to access the resource in
381 + * question. When bio is fixed for vnodes this can all be vnode
382 + * operations.
343 383 */
344 384 if (IS_PXFSVP(bvp)) {
345 - VN_RELE(bvp);
385 + ASSERT(lvp == NULL);
386 + VN_RELE(svp);
387 + svp = NULL;
346 388 bvp = makespecvp(dev, VBLK);
347 - }
348 -
349 - /*
350 - * Open block device mounted on. We need this to
351 - * check whether the caller has sufficient rights to
352 - * access the device in question.
353 - * When bio is fixed for vnodes this can all be vnode
354 - * operations.
355 - */
356 - if ((vfsp->vfs_flag & VFS_RDONLY) != 0 ||
357 - (uap->flags & MS_RDONLY) != 0) {
358 - oflag = FREAD;
359 - aflag = VREAD;
389 + error = VOP_ACCESS(bvp, aflag, 0, cr, NULL);
360 390 } else {
361 - oflag = FREAD | FWRITE;
362 - aflag = VREAD | VWRITE;
391 + error = VOP_ACCESS(svp, aflag, 0, cr, NULL);
363 392 }
364 - if ((error = VOP_ACCESS(bvp, aflag, 0, cr, NULL)) != 0 ||
365 - (error = secpolicy_spec_open(cr, bvp, oflag)) != 0) {
393 +
394 + if (error != 0) {
366 395 pn_free(&dpn);
367 - VN_RELE(bvp);
396 + if (lvp != NULL)
397 + VN_RELE(lvp);
398 + if (svp != NULL)
399 + VN_RELE(svp);
368 400 return (error);
369 401 }
370 402
371 403 /*
372 404 * Ensure that this device isn't already mounted or in progress on a
373 405 * mount unless this is a REMOUNT request or we are told to suppress
374 406 * mount checks. Global mounts require special handling.
375 407 */
376 408 if ((uap->flags & MS_NOCHECK) == 0) {
377 409 if ((uap->flags & MS_GLOBAL) == 0 &&
378 410 vfs_devmounting(dev, vfsp)) {
379 411 pn_free(&dpn);
380 - VN_RELE(bvp);
412 + if (lvp != NULL)
413 + VN_RELE(lvp);
414 + if (svp != NULL)
415 + VN_RELE(svp);
381 416 return (EBUSY);
382 417 }
383 418 if (vfs_devismounted(dev)) {
384 419 if ((uap->flags & MS_REMOUNT) == 0) {
385 420 pn_free(&dpn);
386 - VN_RELE(bvp);
421 + if (lvp != NULL)
422 + VN_RELE(lvp);
423 + if (svp != NULL)
424 + VN_RELE(svp);
387 425 return (EBUSY);
388 426 }
389 427 }
390 428 }
391 429
392 430 /*
393 431 * If the device is a tape, mount it read only
394 432 */
395 433 if (devopsp[getmajor(dev)]->devo_cb_ops->cb_flag & D_TAPE) {
396 434 vfsp->vfs_flag |= VFS_RDONLY;
397 435 vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
398 436 }
399 437 if (uap->flags & MS_RDONLY)
400 438 vfsp->vfs_flag |= VFS_RDONLY;
401 439
402 440 /*
403 441 * Mount the filesystem, free the device vnode on error.
404 442 */
405 443 error = mountfs(vfsp, why, bvp, dpn.pn_path, cr, 0, &args, datalen);
406 444 pn_free(&dpn);
445 +
407 446 if (error) {
408 - VN_RELE(bvp);
447 + if (lvp != NULL)
448 + VN_RELE(lvp);
449 + if (svp != NULL)
450 + VN_RELE(svp);
451 + } else {
452 + /*
453 + * If lofi, drop our reference to the original file.
454 + */
455 + if (lvp != NULL)
456 + VN_RELE(svp);
409 457 }
458 +
410 459 if (error == 0)
411 460 vfs_set_feature(vfsp, VFSFT_XVATTR);
412 461 return (error);
413 462 }
414 463 /*
415 464 * Mount root file system.
416 465 * "why" is ROOT_INIT on initial call ROOT_REMOUNT if called to
417 466 * remount the root file system, and ROOT_UNMOUNT if called to
418 467 * unmount the root (e.g., as part of a system shutdown).
419 468 *
420 469 * XXX - this may be partially machine-dependent; it, along with the VFS_SWAPVP
421 470 * operation, goes along with auto-configuration. A mechanism should be
422 471 * provided by which machine-INdependent code in the kernel can say "get me the
423 472 * right root file system" and "get me the right initial swap area", and have
424 473 * that done in what may well be a machine-dependent fashion.
425 474 * Unfortunately, it is also file-system-type dependent (NFS gets it via
426 475 * bootparams calls, UFS gets it from various and sundry machine-dependent
427 476 * mechanisms, as SPECFS does for swap).
428 477 */
429 478 static int
430 479 ufs_mountroot(struct vfs *vfsp, enum whymountroot why)
431 480 {
432 481 struct fs *fsp;
433 482 int error;
434 483 static int ufsrootdone = 0;
435 484 dev_t rootdev;
436 485 struct vnode *vp;
437 486 struct vnode *devvp = 0;
438 487 int ovflags;
439 488 int doclkset;
440 489 ufsvfs_t *ufsvfsp;
441 490
442 491 if (why == ROOT_INIT) {
443 492 if (ufsrootdone++)
444 493 return (EBUSY);
445 494 rootdev = getrootdev();
446 495 if (rootdev == (dev_t)NODEV)
447 496 return (ENODEV);
448 497 vfsp->vfs_dev = rootdev;
449 498 vfsp->vfs_flag |= VFS_RDONLY;
450 499 } else if (why == ROOT_REMOUNT) {
451 500 vp = ((struct ufsvfs *)vfsp->vfs_data)->vfs_devvp;
452 501 (void) dnlc_purge_vfsp(vfsp, 0);
453 502 vp = common_specvp(vp);
454 503 (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_INVAL,
455 504 CRED(), NULL);
456 505 (void) bfinval(vfsp->vfs_dev, 0);
457 506 fsp = getfs(vfsp);
458 507
459 508 ovflags = vfsp->vfs_flag;
460 509 vfsp->vfs_flag &= ~VFS_RDONLY;
461 510 vfsp->vfs_flag |= VFS_REMOUNT;
462 511 rootdev = vfsp->vfs_dev;
463 512 } else if (why == ROOT_UNMOUNT) {
464 513 if (vfs_lock(vfsp) == 0) {
465 514 (void) ufs_flush(vfsp);
466 515 /*
467 516 * Mark the log as fully rolled
468 517 */
469 518 ufsvfsp = (ufsvfs_t *)vfsp->vfs_data;
470 519 fsp = ufsvfsp->vfs_fs;
471 520 if (TRANS_ISTRANS(ufsvfsp) &&
472 521 !TRANS_ISERROR(ufsvfsp) &&
473 522 (fsp->fs_rolled == FS_NEED_ROLL)) {
474 523 ml_unit_t *ul = ufsvfsp->vfs_log;
475 524
476 525 error = ufs_putsummaryinfo(ul->un_dev,
477 526 ufsvfsp, fsp);
478 527 if (error == 0) {
479 528 fsp->fs_rolled = FS_ALL_ROLLED;
480 529 UFS_BWRITE2(NULL, ufsvfsp->vfs_bufp);
481 530 }
482 531 }
483 532 vfs_unlock(vfsp);
484 533 } else {
485 534 ufs_update(0);
486 535 }
487 536
488 537 vp = ((struct ufsvfs *)vfsp->vfs_data)->vfs_devvp;
489 538 (void) VOP_CLOSE(vp, FREAD|FWRITE, 1,
490 539 (offset_t)0, CRED(), NULL);
491 540 return (0);
492 541 }
493 542 error = vfs_lock(vfsp);
494 543 if (error)
495 544 return (error);
496 545
497 546 devvp = makespecvp(rootdev, VBLK);
498 547
499 548 /* If RO media, don't call clkset() (see below) */
500 549 doclkset = 1;
501 550 if (why == ROOT_INIT) {
502 551 error = VOP_OPEN(&devvp, FREAD|FWRITE, CRED(), NULL);
503 552 if (error == 0) {
504 553 (void) VOP_CLOSE(devvp, FREAD|FWRITE, 1,
505 554 (offset_t)0, CRED(), NULL);
506 555 } else {
507 556 doclkset = 0;
508 557 }
509 558 }
510 559
511 560 error = mountfs(vfsp, why, devvp, "/", CRED(), 1, NULL, 0);
512 561 /*
513 562 * XXX - assumes root device is not indirect, because we don't set
514 563 * rootvp. Is rootvp used for anything? If so, make another arg
515 564 * to mountfs.
516 565 */
517 566 if (error) {
518 567 vfs_unlock(vfsp);
519 568 if (why == ROOT_REMOUNT)
520 569 vfsp->vfs_flag = ovflags;
521 570 if (rootvp) {
522 571 VN_RELE(rootvp);
523 572 rootvp = (struct vnode *)0;
524 573 }
525 574 VN_RELE(devvp);
526 575 return (error);
527 576 }
528 577 if (why == ROOT_INIT)
529 578 vfs_add((struct vnode *)0, vfsp,
530 579 (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
531 580 vfs_unlock(vfsp);
532 581 fsp = getfs(vfsp);
533 582 clkset(doclkset ? fsp->fs_time : -1);
534 583 ufsvfsp = (ufsvfs_t *)vfsp->vfs_data;
535 584 if (ufsvfsp->vfs_log) {
536 585 vfs_setmntopt(vfsp, MNTOPT_LOGGING, NULL, 0);
537 586 }
538 587 return (0);
539 588 }
540 589
541 590 static int
542 591 remountfs(struct vfs *vfsp, dev_t dev, void *raw_argsp, int args_len)
543 592 {
544 593 struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
545 594 struct ulockfs *ulp = &ufsvfsp->vfs_ulockfs;
546 595 struct buf *bp = ufsvfsp->vfs_bufp;
547 596 struct fs *fsp = (struct fs *)bp->b_un.b_addr;
548 597 struct fs *fspt;
549 598 struct buf *tpt = 0;
550 599 int error = 0;
551 600 int flags = 0;
552 601
553 602 if (args_len == sizeof (struct ufs_args) && raw_argsp)
554 603 flags = ((struct ufs_args *)raw_argsp)->flags;
555 604
556 605 /* cannot remount to RDONLY */
557 606 if (vfsp->vfs_flag & VFS_RDONLY)
558 607 return (ENOTSUP);
559 608
560 609 /* whoops, wrong dev */
561 610 if (vfsp->vfs_dev != dev)
562 611 return (EINVAL);
563 612
564 613 /*
565 614 * synchronize w/ufs ioctls
566 615 */
567 616 mutex_enter(&ulp->ul_lock);
568 617 atomic_add_long(&ufs_quiesce_pend, 1);
569 618
570 619 /*
571 620 * reset options
572 621 */
573 622 ufsvfsp->vfs_nointr = flags & UFSMNT_NOINTR;
574 623 ufsvfsp->vfs_syncdir = flags & UFSMNT_SYNCDIR;
575 624 ufsvfsp->vfs_nosetsec = flags & UFSMNT_NOSETSEC;
576 625 ufsvfsp->vfs_noatime = flags & UFSMNT_NOATIME;
577 626 if ((flags & UFSMNT_NODFRATIME) || ufsvfsp->vfs_noatime)
578 627 ufsvfsp->vfs_dfritime &= ~UFS_DFRATIME;
579 628 else /* dfratime, default behavior */
580 629 ufsvfsp->vfs_dfritime |= UFS_DFRATIME;
581 630 if (flags & UFSMNT_FORCEDIRECTIO)
582 631 ufsvfsp->vfs_forcedirectio = 1;
583 632 else /* default is no direct I/O */
584 633 ufsvfsp->vfs_forcedirectio = 0;
585 634 ufsvfsp->vfs_iotstamp = lbolt;
586 635
587 636 /*
588 637 * set largefiles flag in ufsvfs equal to the
589 638 * value passed in by the mount command. If
590 639 * it is "nolargefiles", and the flag is set
591 640 * in the superblock, the mount fails.
592 641 */
593 642 if (!(flags & UFSMNT_LARGEFILES)) { /* "nolargefiles" */
594 643 if (fsp->fs_flags & FSLARGEFILES) {
595 644 error = EFBIG;
596 645 goto remounterr;
597 646 }
598 647 ufsvfsp->vfs_lfflags &= ~UFS_LARGEFILES;
599 648 } else /* "largefiles" */
600 649 ufsvfsp->vfs_lfflags |= UFS_LARGEFILES;
601 650 /*
602 651 * read/write to read/write; all done
603 652 */
604 653 if (fsp->fs_ronly == 0)
605 654 goto remounterr;
606 655
607 656 /*
608 657 * fix-on-panic assumes RO->RW remount implies system-critical fs
609 658 * if it is shortly after boot; so, don't attempt to lock and fix
610 659 * (unless the user explicitly asked for another action on error)
611 660 * XXX UFSMNT_ONERROR_RDONLY rather than UFSMNT_ONERROR_PANIC
612 661 */
613 662 #define BOOT_TIME_LIMIT (180*hz)
614 663 if (!(flags & UFSMNT_ONERROR_FLGMASK) && lbolt < BOOT_TIME_LIMIT) {
615 664 cmn_err(CE_WARN, "%s is required to be mounted onerror=%s",
616 665 ufsvfsp->vfs_fs->fs_fsmnt, UFSMNT_ONERROR_PANIC_STR);
617 666 flags |= UFSMNT_ONERROR_PANIC;
618 667 }
619 668
620 669 if ((error = ufsfx_mount(ufsvfsp, flags)) != 0)
621 670 goto remounterr;
622 671
623 672 /*
624 673 * quiesce the file system
625 674 */
626 675 error = ufs_quiesce(ulp);
627 676 if (error)
628 677 goto remounterr;
629 678
630 679 tpt = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, SBLOCK, SBSIZE);
631 680 if (tpt->b_flags & B_ERROR) {
632 681 error = EIO;
633 682 goto remounterr;
634 683 }
635 684 fspt = (struct fs *)tpt->b_un.b_addr;
636 685 if (((fspt->fs_magic != FS_MAGIC) &&
637 686 (fspt->fs_magic != MTB_UFS_MAGIC)) ||
638 687 (fspt->fs_magic == FS_MAGIC &&
639 688 (fspt->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
640 689 fspt->fs_version != UFS_VERSION_MIN)) ||
641 690 (fspt->fs_magic == MTB_UFS_MAGIC &&
642 691 (fspt->fs_version > MTB_UFS_VERSION_1 ||
643 692 fspt->fs_version < MTB_UFS_VERSION_MIN)) ||
644 693 fspt->fs_bsize > MAXBSIZE || fspt->fs_frag > MAXFRAG ||
645 694 fspt->fs_bsize < sizeof (struct fs) || fspt->fs_bsize < PAGESIZE) {
646 695 tpt->b_flags |= B_STALE | B_AGE;
647 696 error = EINVAL;
648 697 goto remounterr;
649 698 }
650 699
651 700 if (ufsvfsp->vfs_log && (ufsvfsp->vfs_log->un_flags & LDL_NOROLL)) {
652 701 ufsvfsp->vfs_log->un_flags &= ~LDL_NOROLL;
653 702 logmap_start_roll(ufsvfsp->vfs_log);
654 703 }
655 704
656 705 if (TRANS_ISERROR(ufsvfsp))
657 706 goto remounterr;
658 707 TRANS_DOMATAMAP(ufsvfsp);
659 708
660 709 if ((fspt->fs_state + fspt->fs_time == FSOKAY) &&
661 710 fspt->fs_clean == FSLOG && !TRANS_ISTRANS(ufsvfsp)) {
662 711 ufsvfsp->vfs_log = NULL;
663 712 ufsvfsp->vfs_domatamap = 0;
664 713 error = ENOSPC;
665 714 goto remounterr;
666 715 }
667 716
668 717 if (fspt->fs_state + fspt->fs_time == FSOKAY &&
669 718 (fspt->fs_clean == FSCLEAN ||
670 719 fspt->fs_clean == FSSTABLE ||
671 720 fspt->fs_clean == FSLOG)) {
672 721
673 722 /*
674 723 * Ensure that ufs_getsummaryinfo doesn't reconstruct
675 724 * the summary info.
676 725 */
677 726 error = ufs_getsummaryinfo(vfsp->vfs_dev, ufsvfsp, fspt);
678 727 if (error)
679 728 goto remounterr;
680 729
681 730 /* preserve mount name */
682 731 (void) strncpy(fspt->fs_fsmnt, fsp->fs_fsmnt, MAXMNTLEN);
683 732 /* free the old cg space */
684 733 kmem_free(fsp->fs_u.fs_csp, fsp->fs_cssize);
685 734 /* switch in the new superblock */
686 735 fspt->fs_rolled = FS_NEED_ROLL;
687 736 bcopy(tpt->b_un.b_addr, bp->b_un.b_addr, fspt->fs_sbsize);
688 737
689 738 fsp->fs_clean = FSSTABLE;
690 739 } /* superblock updated in memory */
691 740 tpt->b_flags |= B_STALE | B_AGE;
692 741 brelse(tpt);
693 742 tpt = 0;
694 743
695 744 if (fsp->fs_clean != FSSTABLE) {
696 745 error = ENOSPC;
697 746 goto remounterr;
698 747 }
699 748
700 749
701 750 if (TRANS_ISTRANS(ufsvfsp)) {
702 751 fsp->fs_clean = FSLOG;
703 752 ufsvfsp->vfs_dio = 0;
704 753 } else
705 754 if (ufsvfsp->vfs_dio)
706 755 fsp->fs_clean = FSSUSPEND;
707 756
708 757 TRANS_MATA_MOUNT(ufsvfsp);
709 758
710 759 fsp->fs_fmod = 0;
711 760 fsp->fs_ronly = 0;
712 761
713 762 atomic_add_long(&ufs_quiesce_pend, -1);
714 763 cv_broadcast(&ulp->ul_cv);
715 764 mutex_exit(&ulp->ul_lock);
716 765
717 766 if (TRANS_ISTRANS(ufsvfsp)) {
718 767
719 768 /*
720 769 * start the delete thread
721 770 */
722 771 ufs_thread_start(&ufsvfsp->vfs_delete, ufs_thread_delete, vfsp);
723 772
724 773 /*
725 774 * start the reclaim thread
726 775 */
727 776 if (fsp->fs_reclaim & (FS_RECLAIM|FS_RECLAIMING)) {
728 777 fsp->fs_reclaim &= ~FS_RECLAIM;
729 778 fsp->fs_reclaim |= FS_RECLAIMING;
730 779 ufs_thread_start(&ufsvfsp->vfs_reclaim,
731 780 ufs_thread_reclaim, vfsp);
732 781 }
733 782 }
734 783
735 784 TRANS_SBWRITE(ufsvfsp, TOP_MOUNT);
736 785
737 786 return (0);
738 787
739 788 remounterr:
740 789 if (tpt)
741 790 brelse(tpt);
742 791 atomic_add_long(&ufs_quiesce_pend, -1);
743 792 cv_broadcast(&ulp->ul_cv);
744 793 mutex_exit(&ulp->ul_lock);
745 794 return (error);
746 795 }
747 796
748 797 /*
749 798 * If the device maxtransfer size is not available, we use ufs_maxmaxphys
750 799 * along with the system value for maxphys to determine the value for
751 800 * maxtransfer.
752 801 */
753 802 int ufs_maxmaxphys = (1024 * 1024);
754 803
755 804 #include <sys/ddi.h> /* for delay(9f) */
756 805
757 806 int ufs_mount_error_delay = 20; /* default to 20ms */
758 807 int ufs_mount_timeout = 60000; /* default to 1 minute */
759 808
760 809 static int
761 810 mountfs(struct vfs *vfsp, enum whymountroot why, struct vnode *devvp,
762 811 char *path, cred_t *cr, int isroot, void *raw_argsp, int args_len)
763 812 {
764 813 dev_t dev = devvp->v_rdev;
765 814 struct fs *fsp;
766 815 struct ufsvfs *ufsvfsp = 0;
767 816 struct buf *bp = 0;
768 817 struct buf *tp = 0;
769 818 struct dk_cinfo ci;
770 819 int error = 0;
771 820 size_t len;
772 821 int needclose = 0;
773 822 int needtrans = 0;
774 823 struct inode *rip;
775 824 struct vnode *rvp = NULL;
776 825 int flags = 0;
777 826 kmutex_t *ihm;
778 827 int elapsed;
779 828 int status;
780 829 extern int maxphys;
781 830
782 831 if (args_len == sizeof (struct ufs_args) && raw_argsp)
783 832 flags = ((struct ufs_args *)raw_argsp)->flags;
784 833
785 834 ASSERT(vfs_lock_held(vfsp));
786 835
787 836 if (why == ROOT_INIT) {
788 837 /*
789 838 * Open block device mounted on.
790 839 * When bio is fixed for vnodes this can all be vnode
791 840 * operations.
792 841 */
793 842 error = VOP_OPEN(&devvp,
794 843 (vfsp->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE,
795 844 cr, NULL);
796 845 if (error)
797 846 goto out;
798 847 needclose = 1;
799 848
800 849 /*
801 850 * Refuse to go any further if this
802 851 * device is being used for swapping.
803 852 */
804 853 if (IS_SWAPVP(devvp)) {
805 854 error = EBUSY;
806 855 goto out;
807 856 }
808 857 }
809 858
810 859 /*
811 860 * check for dev already mounted on
812 861 */
813 862 if (vfsp->vfs_flag & VFS_REMOUNT) {
814 863 error = remountfs(vfsp, dev, raw_argsp, args_len);
815 864 if (error == 0)
816 865 VN_RELE(devvp);
817 866 return (error);
818 867 }
819 868
820 869 ASSERT(devvp != 0);
821 870
822 871 /*
823 872 * Flush back any dirty pages on the block device to
824 873 * try and keep the buffer cache in sync with the page
825 874 * cache if someone is trying to use block devices when
826 875 * they really should be using the raw device.
827 876 */
828 877 (void) VOP_PUTPAGE(common_specvp(devvp), (offset_t)0,
829 878 (size_t)0, B_INVAL, cr, NULL);
830 879
831 880 /*
832 881 * read in superblock
833 882 */
834 883 ufsvfsp = kmem_zalloc(sizeof (struct ufsvfs), KM_SLEEP);
835 884 tp = UFS_BREAD(ufsvfsp, dev, SBLOCK, SBSIZE);
836 885 if (tp->b_flags & B_ERROR)
837 886 goto out;
838 887 fsp = (struct fs *)tp->b_un.b_addr;
839 888
840 889 if ((fsp->fs_magic != FS_MAGIC) && (fsp->fs_magic != MTB_UFS_MAGIC)) {
841 890 cmn_err(CE_NOTE,
842 891 "mount: not a UFS magic number (0x%x)", fsp->fs_magic);
843 892 error = EINVAL;
844 893 goto out;
845 894 }
846 895
847 896 if ((fsp->fs_magic == FS_MAGIC) &&
848 897 (fsp->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
849 898 fsp->fs_version != UFS_VERSION_MIN)) {
850 899 cmn_err(CE_NOTE,
851 900 "mount: unrecognized version of UFS on-disk format: %d",
852 901 fsp->fs_version);
853 902 error = EINVAL;
854 903 goto out;
855 904 }
856 905
857 906 if ((fsp->fs_magic == MTB_UFS_MAGIC) &&
858 907 (fsp->fs_version > MTB_UFS_VERSION_1 ||
859 908 fsp->fs_version < MTB_UFS_VERSION_MIN)) {
860 909 cmn_err(CE_NOTE,
861 910 "mount: unrecognized version of UFS on-disk format: %d",
862 911 fsp->fs_version);
863 912 error = EINVAL;
864 913 goto out;
865 914 }
866 915
867 916 #ifndef _LP64
868 917 if (fsp->fs_magic == MTB_UFS_MAGIC) {
869 918 /*
870 919 * Find the size of the device in sectors. If the
871 920 * the size in sectors is greater than INT_MAX, it's
872 921 * a multi-terabyte file system, which can't be
873 922 * mounted by a 32-bit kernel. We can't use the
874 923 * fsbtodb() macro in the next line because the macro
875 924 * casts the intermediate values to daddr_t, which is
876 925 * a 32-bit quantity in a 32-bit kernel. Here we
877 926 * really do need the intermediate values to be held
878 927 * in 64-bit quantities because we're checking for
879 928 * overflow of a 32-bit field.
880 929 */
881 930 if ((((diskaddr_t)(fsp->fs_size)) << fsp->fs_fsbtodb)
882 931 > INT_MAX) {
883 932 cmn_err(CE_NOTE,
884 933 "mount: multi-terabyte UFS cannot be"
885 934 " mounted by a 32-bit kernel");
886 935 error = EINVAL;
887 936 goto out;
888 937 }
889 938
890 939 }
891 940 #endif
892 941
893 942 if (fsp->fs_bsize > MAXBSIZE || fsp->fs_frag > MAXFRAG ||
894 943 fsp->fs_bsize < sizeof (struct fs) || fsp->fs_bsize < PAGESIZE) {
895 944 error = EINVAL; /* also needs translation */
896 945 goto out;
897 946 }
898 947
899 948 /*
900 949 * Allocate VFS private data.
901 950 */
902 951 vfsp->vfs_bcount = 0;
903 952 vfsp->vfs_data = (caddr_t)ufsvfsp;
904 953 vfsp->vfs_fstype = ufsfstype;
905 954 vfsp->vfs_dev = dev;
906 955 vfsp->vfs_flag |= VFS_NOTRUNC;
907 956 vfs_make_fsid(&vfsp->vfs_fsid, dev, ufsfstype);
908 957 ufsvfsp->vfs_devvp = devvp;
909 958
910 959 /*
911 960 * Cross-link with vfs and add to instance list.
912 961 */
913 962 ufsvfsp->vfs_vfs = vfsp;
914 963 ufs_vfs_add(ufsvfsp);
915 964
916 965 ufsvfsp->vfs_dev = dev;
917 966 ufsvfsp->vfs_bufp = tp;
918 967
919 968 ufsvfsp->vfs_dirsize = INODESIZE + (4 * ALLOCSIZE) + fsp->fs_fsize;
920 969 ufsvfsp->vfs_minfrags =
921 970 (int)((int64_t)fsp->fs_dsize * fsp->fs_minfree / 100);
922 971 /*
923 972 * if mount allows largefiles, indicate so in ufsvfs
924 973 */
925 974 if (flags & UFSMNT_LARGEFILES)
926 975 ufsvfsp->vfs_lfflags |= UFS_LARGEFILES;
927 976 /*
928 977 * Initialize threads
929 978 */
930 979 ufs_delete_init(ufsvfsp, 1);
931 980 ufs_thread_init(&ufsvfsp->vfs_reclaim, 0);
932 981
933 982 /*
934 983 * Chicken and egg problem. The superblock may have deltas
935 984 * in the log. So after the log is scanned we reread the
936 985 * superblock. We guarantee that the fields needed to
937 986 * scan the log will not be in the log.
938 987 */
939 988 if (fsp->fs_logbno && fsp->fs_clean == FSLOG &&
940 989 (fsp->fs_state + fsp->fs_time == FSOKAY)) {
941 990 error = lufs_snarf(ufsvfsp, fsp, (vfsp->vfs_flag & VFS_RDONLY));
942 991 if (error) {
943 992 /*
944 993 * Allow a ro mount to continue even if the
945 994 * log cannot be processed - yet.
946 995 */
947 996 if (!(vfsp->vfs_flag & VFS_RDONLY)) {
948 997 cmn_err(CE_WARN, "Error accessing ufs "
949 998 "log for %s; Please run fsck(1M)", path);
950 999 goto out;
951 1000 }
952 1001 }
953 1002 tp->b_flags |= (B_AGE | B_STALE);
954 1003 brelse(tp);
955 1004 tp = UFS_BREAD(ufsvfsp, dev, SBLOCK, SBSIZE);
956 1005 fsp = (struct fs *)tp->b_un.b_addr;
957 1006 ufsvfsp->vfs_bufp = tp;
958 1007 if (tp->b_flags & B_ERROR)
959 1008 goto out;
960 1009 }
961 1010
962 1011 /*
963 1012 * Set logging mounted flag used by lockfs
964 1013 */
965 1014 ufsvfsp->vfs_validfs = UT_MOUNTED;
966 1015
967 1016 /*
968 1017 * Copy the super block into a buffer in its native size.
969 1018 * Use ngeteblk to allocate the buffer
970 1019 */
971 1020 bp = ngeteblk(fsp->fs_bsize);
972 1021 ufsvfsp->vfs_bufp = bp;
973 1022 bp->b_edev = dev;
974 1023 bp->b_dev = cmpdev(dev);
975 1024 bp->b_blkno = SBLOCK;
976 1025 bp->b_bcount = fsp->fs_sbsize;
977 1026 bcopy(tp->b_un.b_addr, bp->b_un.b_addr, fsp->fs_sbsize);
978 1027 tp->b_flags |= B_STALE | B_AGE;
979 1028 brelse(tp);
980 1029 tp = 0;
981 1030
982 1031 fsp = (struct fs *)bp->b_un.b_addr;
983 1032 /*
984 1033 * Mount fails if superblock flag indicates presence of large
985 1034 * files and filesystem is attempted to be mounted 'nolargefiles'.
986 1035 * The exception is for a read only mount of root, which we
987 1036 * always want to succeed, so fsck can fix potential problems.
988 1037 * The assumption is that we will remount root at some point,
989 1038 * and the remount will enforce the mount option.
990 1039 */
991 1040 if (!(isroot & (vfsp->vfs_flag & VFS_RDONLY)) &&
992 1041 (fsp->fs_flags & FSLARGEFILES) &&
993 1042 !(flags & UFSMNT_LARGEFILES)) {
994 1043 error = EFBIG;
995 1044 goto out;
996 1045 }
997 1046
998 1047 if (vfsp->vfs_flag & VFS_RDONLY) {
999 1048 fsp->fs_ronly = 1;
1000 1049 fsp->fs_fmod = 0;
1001 1050 if (((fsp->fs_state + fsp->fs_time) == FSOKAY) &&
1002 1051 ((fsp->fs_clean == FSCLEAN) ||
1003 1052 (fsp->fs_clean == FSSTABLE) ||
1004 1053 (fsp->fs_clean == FSLOG))) {
1005 1054 if (isroot) {
1006 1055 if (fsp->fs_clean == FSLOG) {
1007 1056 if (fsp->fs_rolled == FS_ALL_ROLLED) {
1008 1057 ufs_clean_root = 1;
1009 1058 }
1010 1059 } else {
1011 1060 ufs_clean_root = 1;
1012 1061 }
1013 1062 }
1014 1063 fsp->fs_clean = FSSTABLE;
1015 1064 } else {
1016 1065 fsp->fs_clean = FSBAD;
1017 1066 }
1018 1067 } else {
1019 1068
1020 1069 fsp->fs_fmod = 0;
1021 1070 fsp->fs_ronly = 0;
1022 1071
1023 1072 TRANS_DOMATAMAP(ufsvfsp);
1024 1073
1025 1074 if ((TRANS_ISERROR(ufsvfsp)) ||
1026 1075 (((fsp->fs_state + fsp->fs_time) == FSOKAY) &&
1027 1076 fsp->fs_clean == FSLOG && !TRANS_ISTRANS(ufsvfsp))) {
1028 1077 ufsvfsp->vfs_log = NULL;
1029 1078 ufsvfsp->vfs_domatamap = 0;
1030 1079 error = ENOSPC;
1031 1080 goto out;
1032 1081 }
1033 1082
1034 1083 if (((fsp->fs_state + fsp->fs_time) == FSOKAY) &&
1035 1084 (fsp->fs_clean == FSCLEAN ||
1036 1085 fsp->fs_clean == FSSTABLE ||
1037 1086 fsp->fs_clean == FSLOG))
1038 1087 fsp->fs_clean = FSSTABLE;
1039 1088 else {
1040 1089 if (isroot) {
1041 1090 /*
1042 1091 * allow root partition to be mounted even
1043 1092 * when fs_state is not ok
1044 1093 * will be fixed later by a remount root
1045 1094 */
1046 1095 fsp->fs_clean = FSBAD;
1047 1096 ufsvfsp->vfs_log = NULL;
1048 1097 ufsvfsp->vfs_domatamap = 0;
1049 1098 } else {
1050 1099 error = ENOSPC;
1051 1100 goto out;
1052 1101 }
1053 1102 }
1054 1103
1055 1104 if (fsp->fs_clean == FSSTABLE && TRANS_ISTRANS(ufsvfsp))
1056 1105 fsp->fs_clean = FSLOG;
1057 1106 }
1058 1107 TRANS_MATA_MOUNT(ufsvfsp);
1059 1108 needtrans = 1;
1060 1109
1061 1110 vfsp->vfs_bsize = fsp->fs_bsize;
1062 1111
1063 1112 /*
1064 1113 * Read in summary info
1065 1114 */
1066 1115 if (error = ufs_getsummaryinfo(dev, ufsvfsp, fsp))
1067 1116 goto out;
1068 1117
1069 1118 /*
1070 1119 * lastwhinetime is set to zero rather than lbolt, so that after
1071 1120 * mounting if the filesystem is found to be full, then immediately the
1072 1121 * "file system message" will be logged.
1073 1122 */
1074 1123 ufsvfsp->vfs_lastwhinetime = 0L;
1075 1124
1076 1125
1077 1126 mutex_init(&ufsvfsp->vfs_lock, NULL, MUTEX_DEFAULT, NULL);
1078 1127 (void) copystr(path, fsp->fs_fsmnt, sizeof (fsp->fs_fsmnt) - 1, &len);
1079 1128 bzero(fsp->fs_fsmnt + len, sizeof (fsp->fs_fsmnt) - len);
1080 1129
1081 1130 /*
1082 1131 * Sanity checks for old file systems
1083 1132 */
1084 1133 if (fsp->fs_postblformat == FS_42POSTBLFMT)
1085 1134 ufsvfsp->vfs_nrpos = 8;
1086 1135 else
1087 1136 ufsvfsp->vfs_nrpos = fsp->fs_nrpos;
1088 1137
1089 1138 /*
1090 1139 * Initialize lockfs structure to support file system locking
1091 1140 */
1092 1141 bzero(&ufsvfsp->vfs_ulockfs.ul_lockfs,
1093 1142 sizeof (struct lockfs));
1094 1143 ufsvfsp->vfs_ulockfs.ul_fs_lock = ULOCKFS_ULOCK;
1095 1144 mutex_init(&ufsvfsp->vfs_ulockfs.ul_lock, NULL,
1096 1145 MUTEX_DEFAULT, NULL);
1097 1146 cv_init(&ufsvfsp->vfs_ulockfs.ul_cv, NULL, CV_DEFAULT, NULL);
1098 1147
1099 1148 /*
1100 1149 * We don't need to grab vfs_dqrwlock for this ufs_iget() call.
1101 1150 * We are in the process of mounting the file system so there
1102 1151 * is no need to grab the quota lock. If a quota applies to the
1103 1152 * root inode, then it will be updated when quotas are enabled.
1104 1153 *
1105 1154 * However, we have an ASSERT(RW_LOCK_HELD(&ufsvfsp->vfs_dqrwlock))
1106 1155 * in getinoquota() that we want to keep so grab it anyway.
1107 1156 */
1108 1157 rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
1109 1158
1110 1159 error = ufs_iget_alloced(vfsp, UFSROOTINO, &rip, cr);
1111 1160
1112 1161 rw_exit(&ufsvfsp->vfs_dqrwlock);
1113 1162
1114 1163 if (error)
1115 1164 goto out;
1116 1165
1117 1166 /*
1118 1167 * make sure root inode is a directory. Returning ENOTDIR might
1119 1168 * be confused with the mount point not being a directory, so
1120 1169 * we use EIO instead.
1121 1170 */
1122 1171 if ((rip->i_mode & IFMT) != IFDIR) {
1123 1172 /*
1124 1173 * Mark this inode as subject for cleanup
1125 1174 * to avoid stray inodes in the cache.
1126 1175 */
1127 1176 rvp = ITOV(rip);
1128 1177 error = EIO;
1129 1178 goto out;
1130 1179 }
1131 1180
1132 1181 rvp = ITOV(rip);
1133 1182 mutex_enter(&rvp->v_lock);
1134 1183 rvp->v_flag |= VROOT;
1135 1184 mutex_exit(&rvp->v_lock);
1136 1185 ufsvfsp->vfs_root = rvp;
1137 1186 /* The buffer for the root inode does not contain a valid b_vp */
1138 1187 (void) bfinval(dev, 0);
1139 1188
1140 1189 /* options */
1141 1190 ufsvfsp->vfs_nosetsec = flags & UFSMNT_NOSETSEC;
1142 1191 ufsvfsp->vfs_nointr = flags & UFSMNT_NOINTR;
1143 1192 ufsvfsp->vfs_syncdir = flags & UFSMNT_SYNCDIR;
1144 1193 ufsvfsp->vfs_noatime = flags & UFSMNT_NOATIME;
1145 1194 if ((flags & UFSMNT_NODFRATIME) || ufsvfsp->vfs_noatime)
1146 1195 ufsvfsp->vfs_dfritime &= ~UFS_DFRATIME;
1147 1196 else /* dfratime, default behavior */
1148 1197 ufsvfsp->vfs_dfritime |= UFS_DFRATIME;
1149 1198 if (flags & UFSMNT_FORCEDIRECTIO)
1150 1199 ufsvfsp->vfs_forcedirectio = 1;
1151 1200 else if (flags & UFSMNT_NOFORCEDIRECTIO)
1152 1201 ufsvfsp->vfs_forcedirectio = 0;
1153 1202 ufsvfsp->vfs_iotstamp = lbolt;
1154 1203
1155 1204 ufsvfsp->vfs_nindiroffset = fsp->fs_nindir - 1;
1156 1205 ufsvfsp->vfs_nindirshift = highbit(ufsvfsp->vfs_nindiroffset);
1157 1206 ufsvfsp->vfs_ioclustsz = fsp->fs_bsize * fsp->fs_maxcontig;
1158 1207
1159 1208 if (cdev_ioctl(dev, DKIOCINFO, (intptr_t)&ci,
1160 1209 FKIOCTL|FNATIVE|FREAD, CRED(), &status) == 0) {
1161 1210 ufsvfsp->vfs_iotransz = ci.dki_maxtransfer * DEV_BSIZE;
1162 1211 } else {
1163 1212 ufsvfsp->vfs_iotransz = MIN(maxphys, ufs_maxmaxphys);
1164 1213 }
1165 1214
1166 1215 if (ufsvfsp->vfs_iotransz <= 0) {
1167 1216 ufsvfsp->vfs_iotransz = MIN(maxphys, ufs_maxmaxphys);
1168 1217 }
1169 1218
1170 1219 /*
1171 1220 * When logging, used to reserve log space for writes and truncs
1172 1221 */
1173 1222 ufsvfsp->vfs_avgbfree = fsp->fs_cstotal.cs_nbfree / fsp->fs_ncg;
1174 1223
1175 1224 /*
1176 1225 * Determine whether to log cylinder group summary info.
1177 1226 */
1178 1227 ufsvfsp->vfs_nolog_si = (fsp->fs_ncg < ufs_ncg_log);
1179 1228
1180 1229 if (TRANS_ISTRANS(ufsvfsp)) {
1181 1230 /*
1182 1231 * start the delete thread
1183 1232 */
1184 1233 ufs_thread_start(&ufsvfsp->vfs_delete, ufs_thread_delete, vfsp);
1185 1234
1186 1235 /*
1187 1236 * start reclaim thread if the filesystem was not mounted
1188 1237 * read only.
1189 1238 */
1190 1239 if (!fsp->fs_ronly && (fsp->fs_reclaim &
1191 1240 (FS_RECLAIM|FS_RECLAIMING))) {
1192 1241 fsp->fs_reclaim &= ~FS_RECLAIM;
1193 1242 fsp->fs_reclaim |= FS_RECLAIMING;
1194 1243 ufs_thread_start(&ufsvfsp->vfs_reclaim,
1195 1244 ufs_thread_reclaim, vfsp);
1196 1245 }
1197 1246
1198 1247 /* Mark the fs as unrolled */
1199 1248 fsp->fs_rolled = FS_NEED_ROLL;
1200 1249 } else if (!fsp->fs_ronly && (fsp->fs_reclaim &
1201 1250 (FS_RECLAIM|FS_RECLAIMING))) {
1202 1251 /*
1203 1252 * If a file system that is mounted nologging, after
1204 1253 * having previously been mounted logging, becomes
1205 1254 * unmounted whilst the reclaim thread is in the throes
1206 1255 * of reclaiming open/deleted inodes, a subsequent mount
1207 1256 * of such a file system with logging disabled could lead
1208 1257 * to inodes becoming lost. So, start reclaim now, even
1209 1258 * though logging was disabled for the previous mount, to
1210 1259 * tidy things up.
1211 1260 */
1212 1261 fsp->fs_reclaim &= ~FS_RECLAIM;
1213 1262 fsp->fs_reclaim |= FS_RECLAIMING;
1214 1263 ufs_thread_start(&ufsvfsp->vfs_reclaim,
1215 1264 ufs_thread_reclaim, vfsp);
1216 1265 }
1217 1266
1218 1267 if (!fsp->fs_ronly) {
1219 1268 TRANS_SBWRITE(ufsvfsp, TOP_MOUNT);
1220 1269 if (error = geterror(ufsvfsp->vfs_bufp))
1221 1270 goto out;
1222 1271 }
1223 1272
1224 1273 /* fix-on-panic initialization */
1225 1274 if (isroot && !(flags & UFSMNT_ONERROR_FLGMASK))
1226 1275 flags |= UFSMNT_ONERROR_PANIC; /* XXX ..._RDONLY */
1227 1276
1228 1277 if ((error = ufsfx_mount(ufsvfsp, flags)) != 0)
1229 1278 goto out;
1230 1279
1231 1280 if (why == ROOT_INIT && isroot)
1232 1281 rootvp = devvp;
1233 1282
1234 1283 return (0);
1235 1284 out:
1236 1285 if (error == 0)
1237 1286 error = EIO;
1238 1287 if (rvp) {
1239 1288 /* the following sequence is similar to ufs_unmount() */
1240 1289
1241 1290 /*
1242 1291 * There's a problem that ufs_iget() puts inodes into
1243 1292 * the inode cache before it returns them. If someone
1244 1293 * traverses that cache and gets a reference to our
1245 1294 * inode, there's a chance they'll still be using it
1246 1295 * after we've destroyed it. This is a hard race to
1247 1296 * hit, but it's happened (putting in a medium delay
1248 1297 * here, and a large delay in ufs_scan_inodes() for
1249 1298 * inodes on the device we're bailing out on, makes
1250 1299 * the race easy to demonstrate). The symptom is some
1251 1300 * other part of UFS faulting on bad inode contents,
1252 1301 * or when grabbing one of the locks inside the inode,
1253 1302 * etc. The usual victim is ufs_scan_inodes() or
1254 1303 * someone called by it.
1255 1304 */
1256 1305
1257 1306 /*
1258 1307 * First, isolate it so that no new references can be
1259 1308 * gotten via the inode cache.
1260 1309 */
1261 1310 ihm = &ih_lock[INOHASH(UFSROOTINO)];
1262 1311 mutex_enter(ihm);
1263 1312 remque(rip);
1264 1313 mutex_exit(ihm);
1265 1314
1266 1315 /*
1267 1316 * Now wait for all outstanding references except our
1268 1317 * own to drain. This could, in theory, take forever,
1269 1318 * so don't wait *too* long. If we time out, mark
1270 1319 * it stale and leak it, so we don't hit the problem
1271 1320 * described above.
1272 1321 *
1273 1322 * Note that v_count is an int, which means we can read
1274 1323 * it in one operation. Thus, there's no need to lock
1275 1324 * around our tests.
1276 1325 */
1277 1326 elapsed = 0;
1278 1327 while ((rvp->v_count > 1) && (elapsed < ufs_mount_timeout)) {
1279 1328 delay(ufs_mount_error_delay * drv_usectohz(1000));
1280 1329 elapsed += ufs_mount_error_delay;
1281 1330 }
1282 1331
1283 1332 if (rvp->v_count > 1) {
1284 1333 mutex_enter(&rip->i_tlock);
1285 1334 rip->i_flag |= ISTALE;
1286 1335 mutex_exit(&rip->i_tlock);
1287 1336 cmn_err(CE_WARN,
1288 1337 "Timed out while cleaning up after "
1289 1338 "failed mount of %s", path);
1290 1339 } else {
1291 1340
1292 1341 /*
1293 1342 * Now we're the only one with a handle left, so tear
1294 1343 * it down the rest of the way.
1295 1344 */
1296 1345 if (ufs_rmidle(rip))
1297 1346 VN_RELE(rvp);
1298 1347 ufs_si_del(rip);
1299 1348 rip->i_ufsvfs = NULL;
1300 1349 rvp->v_vfsp = NULL;
1301 1350 rvp->v_type = VBAD;
1302 1351 VN_RELE(rvp);
1303 1352 }
1304 1353 }
1305 1354 if (needtrans) {
1306 1355 TRANS_MATA_UMOUNT(ufsvfsp);
1307 1356 }
1308 1357 if (ufsvfsp) {
1309 1358 ufs_vfs_remove(ufsvfsp);
1310 1359 ufs_thread_exit(&ufsvfsp->vfs_delete);
1311 1360 ufs_thread_exit(&ufsvfsp->vfs_reclaim);
1312 1361 mutex_destroy(&ufsvfsp->vfs_lock);
1313 1362 if (ufsvfsp->vfs_log) {
1314 1363 lufs_unsnarf(ufsvfsp);
1315 1364 }
1316 1365 kmem_free(ufsvfsp, sizeof (struct ufsvfs));
1317 1366 }
1318 1367 if (bp) {
1319 1368 bp->b_flags |= (B_STALE|B_AGE);
1320 1369 brelse(bp);
1321 1370 }
1322 1371 if (tp) {
1323 1372 tp->b_flags |= (B_STALE|B_AGE);
1324 1373 brelse(tp);
1325 1374 }
1326 1375 if (needclose) {
1327 1376 (void) VOP_CLOSE(devvp, (vfsp->vfs_flag & VFS_RDONLY) ?
1328 1377 FREAD : FREAD|FWRITE, 1, (offset_t)0, cr, NULL);
1329 1378 bflush(dev);
1330 1379 (void) bfinval(dev, 1);
1331 1380 }
1332 1381 return (error);
1333 1382 }
1334 1383
1335 1384 /*
1336 1385 * vfs operations
1337 1386 */
1338 1387 static int
1339 1388 ufs_unmount(struct vfs *vfsp, int fflag, struct cred *cr)
1340 1389 {
1341 1390 dev_t dev = vfsp->vfs_dev;
1342 1391 struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
1343 1392 struct fs *fs = ufsvfsp->vfs_fs;
1344 1393 struct ulockfs *ulp = &ufsvfsp->vfs_ulockfs;
1345 1394 struct vnode *bvp, *vp;
1346 1395 struct buf *bp;
1347 1396 struct inode *ip, *inext, *rip;
1348 1397 union ihead *ih;
1349 1398 int error, flag, i;
1350 1399 struct lockfs lockfs;
1351 1400 int poll_events = POLLPRI;
1352 1401 extern struct pollhead ufs_pollhd;
1353 1402 refstr_t *mountpoint;
1354 1403
1355 1404 ASSERT(vfs_lock_held(vfsp));
1356 1405
1357 1406 if (secpolicy_fs_unmount(cr, vfsp) != 0)
1358 1407 return (EPERM);
1359 1408 /*
1360 1409 * Forced unmount is now supported through the
1361 1410 * lockfs protocol.
1362 1411 */
1363 1412 if (fflag & MS_FORCE) {
1364 1413 /*
1365 1414 * Mark the filesystem as being unmounted now in
1366 1415 * case of a forcible umount before we take any
1367 1416 * locks inside UFS to prevent racing with a VFS_VGET()
1368 1417 * request. Throw these VFS_VGET() requests away for
1369 1418 * the duration of the forcible umount so they won't
1370 1419 * use stale or even freed data later on when we're done.
1371 1420 * It may happen that the VFS has had a additional hold
1372 1421 * placed on it by someone other than UFS and thus will
1373 1422 * not get freed immediately once we're done with the
1374 1423 * umount by dounmount() - use VFS_UNMOUNTED to inform
1375 1424 * users of this still-alive VFS that its corresponding
1376 1425 * filesystem being gone so they can detect that and error
1377 1426 * out.
1378 1427 */
1379 1428 vfsp->vfs_flag |= VFS_UNMOUNTED;
1380 1429
1381 1430 ufs_thread_suspend(&ufsvfsp->vfs_delete);
1382 1431 mutex_enter(&ulp->ul_lock);
1383 1432 /*
1384 1433 * If file system is already hard locked,
1385 1434 * unmount the file system, otherwise
1386 1435 * hard lock it before unmounting.
1387 1436 */
1388 1437 if (!ULOCKFS_IS_HLOCK(ulp)) {
1389 1438 atomic_add_long(&ufs_quiesce_pend, 1);
1390 1439 lockfs.lf_lock = LOCKFS_HLOCK;
1391 1440 lockfs.lf_flags = 0;
1392 1441 lockfs.lf_key = ulp->ul_lockfs.lf_key + 1;
1393 1442 lockfs.lf_comlen = 0;
1394 1443 lockfs.lf_comment = NULL;
1395 1444 ufs_freeze(ulp, &lockfs);
1396 1445 ULOCKFS_SET_BUSY(ulp);
1397 1446 LOCKFS_SET_BUSY(&ulp->ul_lockfs);
1398 1447 (void) ufs_quiesce(ulp);
1399 1448 (void) ufs_flush(vfsp);
1400 1449 (void) ufs_thaw(vfsp, ufsvfsp, ulp);
1401 1450 atomic_add_long(&ufs_quiesce_pend, -1);
1402 1451 ULOCKFS_CLR_BUSY(ulp);
1403 1452 LOCKFS_CLR_BUSY(&ulp->ul_lockfs);
1404 1453 poll_events |= POLLERR;
1405 1454 pollwakeup(&ufs_pollhd, poll_events);
1406 1455 }
1407 1456 ufs_thread_continue(&ufsvfsp->vfs_delete);
1408 1457 mutex_exit(&ulp->ul_lock);
1409 1458 }
1410 1459
1411 1460 /* let all types of writes go through */
1412 1461 ufsvfsp->vfs_iotstamp = lbolt;
1413 1462
1414 1463 /* coordinate with global hlock thread */
1415 1464 if (TRANS_ISTRANS(ufsvfsp) && (ufsvfsp->vfs_validfs == UT_HLOCKING)) {
1416 1465 /*
1417 1466 * last possibility for a forced umount to fail hence clear
1418 1467 * VFS_UNMOUNTED if appropriate.
1419 1468 */
1420 1469 if (fflag & MS_FORCE)
1421 1470 vfsp->vfs_flag &= ~VFS_UNMOUNTED;
1422 1471 return (EAGAIN);
1423 1472 }
1424 1473
1425 1474 ufsvfsp->vfs_validfs = UT_UNMOUNTED;
1426 1475
1427 1476 /* kill the reclaim thread */
1428 1477 ufs_thread_exit(&ufsvfsp->vfs_reclaim);
1429 1478
1430 1479 /* suspend the delete thread */
1431 1480 ufs_thread_suspend(&ufsvfsp->vfs_delete);
1432 1481
1433 1482 /*
1434 1483 * drain the delete and idle queues
1435 1484 */
1436 1485 ufs_delete_drain(vfsp, -1, 1);
1437 1486 ufs_idle_drain(vfsp);
1438 1487
1439 1488 /*
1440 1489 * use the lockfs protocol to prevent new ops from starting
1441 1490 * a forcible umount can not fail beyond this point as
1442 1491 * we hard-locked the filesystem and drained all current consumers
1443 1492 * before.
1444 1493 */
1445 1494 mutex_enter(&ulp->ul_lock);
1446 1495
1447 1496 /*
1448 1497 * if the file system is busy; return EBUSY
1449 1498 */
1450 1499 if (ulp->ul_vnops_cnt || ulp->ul_falloc_cnt || ULOCKFS_IS_SLOCK(ulp)) {
1451 1500 error = EBUSY;
1452 1501 goto out;
1453 1502 }
1454 1503
1455 1504 /*
1456 1505 * if this is not a forced unmount (!hard/error locked), then
1457 1506 * get rid of every inode except the root and quota inodes
1458 1507 * also, commit any outstanding transactions
1459 1508 */
1460 1509 if (!ULOCKFS_IS_HLOCK(ulp) && !ULOCKFS_IS_ELOCK(ulp))
1461 1510 if (error = ufs_flush(vfsp))
1462 1511 goto out;
1463 1512
1464 1513 /*
1465 1514 * ignore inodes in the cache if fs is hard locked or error locked
1466 1515 */
1467 1516 rip = VTOI(ufsvfsp->vfs_root);
1468 1517 if (!ULOCKFS_IS_HLOCK(ulp) && !ULOCKFS_IS_ELOCK(ulp)) {
1469 1518 /*
1470 1519 * Otherwise, only the quota and root inodes are in the cache.
1471 1520 *
1472 1521 * Avoid racing with ufs_update() and ufs_sync().
1473 1522 */
1474 1523 mutex_enter(&ufs_scan_lock);
1475 1524
1476 1525 for (i = 0, ih = ihead; i < inohsz; i++, ih++) {
1477 1526 mutex_enter(&ih_lock[i]);
1478 1527 for (ip = ih->ih_chain[0];
1479 1528 ip != (struct inode *)ih;
1480 1529 ip = ip->i_forw) {
1481 1530 if (ip->i_ufsvfs != ufsvfsp)
1482 1531 continue;
1483 1532 if (ip == ufsvfsp->vfs_qinod)
1484 1533 continue;
1485 1534 if (ip == rip && ITOV(ip)->v_count == 1)
1486 1535 continue;
1487 1536 mutex_exit(&ih_lock[i]);
1488 1537 mutex_exit(&ufs_scan_lock);
1489 1538 error = EBUSY;
1490 1539 goto out;
1491 1540 }
1492 1541 mutex_exit(&ih_lock[i]);
1493 1542 }
1494 1543 mutex_exit(&ufs_scan_lock);
1495 1544 }
1496 1545
1497 1546 /*
1498 1547 * if a snapshot exists and this is a forced unmount, then delete
1499 1548 * the snapshot. Otherwise return EBUSY. This will insure the
1500 1549 * snapshot always belongs to a valid file system.
1501 1550 */
1502 1551 if (ufsvfsp->vfs_snapshot) {
1503 1552 if (ULOCKFS_IS_HLOCK(ulp) || ULOCKFS_IS_ELOCK(ulp)) {
1504 1553 (void) fssnap_delete(&ufsvfsp->vfs_snapshot);
1505 1554 } else {
1506 1555 error = EBUSY;
1507 1556 goto out;
1508 1557 }
1509 1558 }
1510 1559
1511 1560 /*
1512 1561 * Close the quota file and invalidate anything left in the quota
1513 1562 * cache for this file system. Pass kcred to allow all quota
1514 1563 * manipulations.
1515 1564 */
1516 1565 (void) closedq(ufsvfsp, kcred);
1517 1566 invalidatedq(ufsvfsp);
1518 1567 /*
1519 1568 * drain the delete and idle queues
1520 1569 */
1521 1570 ufs_delete_drain(vfsp, -1, 0);
1522 1571 ufs_idle_drain(vfsp);
1523 1572
1524 1573 /*
1525 1574 * discard the inodes for this fs (including root, shadow, and quota)
1526 1575 */
1527 1576 for (i = 0, ih = ihead; i < inohsz; i++, ih++) {
1528 1577 mutex_enter(&ih_lock[i]);
1529 1578 for (inext = 0, ip = ih->ih_chain[0];
1530 1579 ip != (struct inode *)ih;
1531 1580 ip = inext) {
1532 1581 inext = ip->i_forw;
1533 1582 if (ip->i_ufsvfs != ufsvfsp)
1534 1583 continue;
1535 1584
1536 1585 /*
1537 1586 * We've found the inode in the cache and as we
1538 1587 * hold the hash mutex the inode can not
1539 1588 * disappear from underneath us.
1540 1589 * We also know it must have at least a vnode
1541 1590 * reference count of 1.
1542 1591 * We perform an additional VN_HOLD so the VN_RELE
1543 1592 * in case we take the inode off the idle queue
1544 1593 * can not be the last one.
1545 1594 * It is safe to grab the writer contents lock here
1546 1595 * to prevent a race with ufs_iinactive() putting
1547 1596 * inodes into the idle queue while we operate on
1548 1597 * this inode.
1549 1598 */
1550 1599 rw_enter(&ip->i_contents, RW_WRITER);
1551 1600
1552 1601 vp = ITOV(ip);
1553 1602 VN_HOLD(vp)
1554 1603 remque(ip);
1555 1604 if (ufs_rmidle(ip))
1556 1605 VN_RELE(vp);
1557 1606 ufs_si_del(ip);
1558 1607 /*
1559 1608 * rip->i_ufsvfsp is needed by bflush()
1560 1609 */
1561 1610 if (ip != rip)
1562 1611 ip->i_ufsvfs = NULL;
1563 1612 /*
1564 1613 * Set vnode's vfsops to dummy ops, which return
1565 1614 * EIO. This is needed to forced unmounts to work
1566 1615 * with lofs/nfs properly.
1567 1616 */
1568 1617 if (ULOCKFS_IS_HLOCK(ulp) || ULOCKFS_IS_ELOCK(ulp))
1569 1618 vp->v_vfsp = &EIO_vfs;
1570 1619 else
1571 1620 vp->v_vfsp = NULL;
1572 1621 vp->v_type = VBAD;
1573 1622
1574 1623 rw_exit(&ip->i_contents);
1575 1624
1576 1625 VN_RELE(vp);
1577 1626 }
1578 1627 mutex_exit(&ih_lock[i]);
1579 1628 }
1580 1629 ufs_si_cache_flush(dev);
1581 1630
1582 1631 /*
1583 1632 * kill the delete thread and drain the idle queue
1584 1633 */
1585 1634 ufs_thread_exit(&ufsvfsp->vfs_delete);
1586 1635 ufs_idle_drain(vfsp);
1587 1636
1588 1637 bp = ufsvfsp->vfs_bufp;
1589 1638 bvp = ufsvfsp->vfs_devvp;
1590 1639 flag = !fs->fs_ronly;
1591 1640 if (flag) {
1592 1641 bflush(dev);
1593 1642 if (fs->fs_clean != FSBAD) {
1594 1643 if (fs->fs_clean == FSSTABLE)
1595 1644 fs->fs_clean = FSCLEAN;
1596 1645 fs->fs_reclaim &= ~FS_RECLAIM;
1597 1646 }
1598 1647 if (TRANS_ISTRANS(ufsvfsp) &&
1599 1648 !TRANS_ISERROR(ufsvfsp) &&
1600 1649 !ULOCKFS_IS_HLOCK(ulp) &&
1601 1650 (fs->fs_rolled == FS_NEED_ROLL)) {
1602 1651 /*
1603 1652 * ufs_flush() above has flushed the last Moby.
1604 1653 * This is needed to ensure the following superblock
1605 1654 * update really is the last metadata update
1606 1655 */
1607 1656 error = ufs_putsummaryinfo(dev, ufsvfsp, fs);
1608 1657 if (error == 0) {
1609 1658 fs->fs_rolled = FS_ALL_ROLLED;
1610 1659 }
1611 1660 }
1612 1661 TRANS_SBUPDATE(ufsvfsp, vfsp, TOP_SBUPDATE_UNMOUNT);
1613 1662 /*
1614 1663 * push this last transaction
1615 1664 */
1616 1665 curthread->t_flag |= T_DONTBLOCK;
1617 1666 TRANS_BEGIN_SYNC(ufsvfsp, TOP_COMMIT_UNMOUNT, TOP_COMMIT_SIZE,
1618 1667 error);
1619 1668 if (!error)
1620 1669 TRANS_END_SYNC(ufsvfsp, error, TOP_COMMIT_UNMOUNT,
1621 1670 TOP_COMMIT_SIZE);
1622 1671 curthread->t_flag &= ~T_DONTBLOCK;
1623 1672 }
1624 1673
1625 1674 TRANS_MATA_UMOUNT(ufsvfsp);
1626 1675 lufs_unsnarf(ufsvfsp); /* Release the in-memory structs */
1627 1676 ufsfx_unmount(ufsvfsp); /* fix-on-panic bookkeeping */
1628 1677 kmem_free(fs->fs_u.fs_csp, fs->fs_cssize);
1629 1678
1630 1679 bp->b_flags |= B_STALE|B_AGE;
1631 1680 ufsvfsp->vfs_bufp = NULL; /* don't point at freed buf */
1632 1681 brelse(bp); /* free the superblock buf */
1633 1682
1634 1683 (void) VOP_PUTPAGE(common_specvp(bvp), (offset_t)0, (size_t)0,
1635 1684 B_INVAL, cr, NULL);
1636 1685 (void) VOP_CLOSE(bvp, flag, 1, (offset_t)0, cr, NULL);
1637 1686 bflush(dev);
1638 1687 (void) bfinval(dev, 1);
1639 1688 VN_RELE(bvp);
1640 1689
1641 1690 /*
1642 1691 * It is now safe to NULL out the ufsvfs pointer and discard
1643 1692 * the root inode.
1644 1693 */
1645 1694 rip->i_ufsvfs = NULL;
1646 1695 VN_RELE(ITOV(rip));
1647 1696
1648 1697 /* free up lockfs comment structure, if any */
1649 1698 if (ulp->ul_lockfs.lf_comlen && ulp->ul_lockfs.lf_comment)
1650 1699 kmem_free(ulp->ul_lockfs.lf_comment, ulp->ul_lockfs.lf_comlen);
1651 1700
1652 1701 /*
1653 1702 * Remove from instance list.
1654 1703 */
1655 1704 ufs_vfs_remove(ufsvfsp);
1656 1705
1657 1706 /*
1658 1707 * For a forcible unmount, threads may be asleep in
1659 1708 * ufs_lockfs_begin/ufs_check_lockfs. These threads will need
1660 1709 * the ufsvfs structure so we don't free it, yet. ufs_update
1661 1710 * will free it up after awhile.
1662 1711 */
1663 1712 if (ULOCKFS_IS_HLOCK(ulp) || ULOCKFS_IS_ELOCK(ulp)) {
1664 1713 extern kmutex_t ufsvfs_mutex;
1665 1714 extern struct ufsvfs *ufsvfslist;
1666 1715
1667 1716 mutex_enter(&ufsvfs_mutex);
1668 1717 ufsvfsp->vfs_dontblock = 1;
1669 1718 ufsvfsp->vfs_next = ufsvfslist;
1670 1719 ufsvfslist = ufsvfsp;
1671 1720 mutex_exit(&ufsvfs_mutex);
1672 1721 /* wakeup any suspended threads */
1673 1722 cv_broadcast(&ulp->ul_cv);
1674 1723 mutex_exit(&ulp->ul_lock);
1675 1724 } else {
1676 1725 mutex_destroy(&ufsvfsp->vfs_lock);
1677 1726 kmem_free(ufsvfsp, sizeof (struct ufsvfs));
1678 1727 }
1679 1728
1680 1729 /*
1681 1730 * Now mark the filesystem as unmounted since we're done with it.
1682 1731 */
1683 1732 vfsp->vfs_flag |= VFS_UNMOUNTED;
1684 1733
1685 1734 return (0);
1686 1735 out:
1687 1736 /* open the fs to new ops */
1688 1737 cv_broadcast(&ulp->ul_cv);
1689 1738 mutex_exit(&ulp->ul_lock);
1690 1739
1691 1740 if (TRANS_ISTRANS(ufsvfsp)) {
1692 1741 /* allow the delete thread to continue */
1693 1742 ufs_thread_continue(&ufsvfsp->vfs_delete);
1694 1743 /* restart the reclaim thread */
1695 1744 ufs_thread_start(&ufsvfsp->vfs_reclaim, ufs_thread_reclaim,
1696 1745 vfsp);
1697 1746 /* coordinate with global hlock thread */
1698 1747 ufsvfsp->vfs_validfs = UT_MOUNTED;
1699 1748 /* check for trans errors during umount */
1700 1749 ufs_trans_onerror();
1701 1750
1702 1751 /*
1703 1752 * if we have a separate /usr it will never unmount
1704 1753 * when halting. In order to not re-read all the
1705 1754 * cylinder group summary info on mounting after
1706 1755 * reboot the logging of summary info is re-enabled
1707 1756 * and the super block written out.
1708 1757 */
1709 1758 mountpoint = vfs_getmntpoint(vfsp);
1710 1759 if ((fs->fs_si == FS_SI_OK) &&
1711 1760 (strcmp("/usr", refstr_value(mountpoint)) == 0)) {
1712 1761 ufsvfsp->vfs_nolog_si = 0;
1713 1762 UFS_BWRITE2(NULL, ufsvfsp->vfs_bufp);
1714 1763 }
1715 1764 refstr_rele(mountpoint);
1716 1765 }
1717 1766
1718 1767 return (error);
1719 1768 }
1720 1769
1721 1770 static int
1722 1771 ufs_root(struct vfs *vfsp, struct vnode **vpp)
1723 1772 {
1724 1773 struct ufsvfs *ufsvfsp;
1725 1774 struct vnode *vp;
1726 1775
1727 1776 if (!vfsp)
1728 1777 return (EIO);
1729 1778
1730 1779 ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
1731 1780 if (!ufsvfsp || !ufsvfsp->vfs_root)
1732 1781 return (EIO); /* forced unmount */
1733 1782
1734 1783 vp = ufsvfsp->vfs_root;
1735 1784 VN_HOLD(vp);
1736 1785 *vpp = vp;
1737 1786 return (0);
1738 1787 }
1739 1788
1740 1789 /*
1741 1790 * Get file system statistics.
1742 1791 */
1743 1792 static int
1744 1793 ufs_statvfs(struct vfs *vfsp, struct statvfs64 *sp)
1745 1794 {
1746 1795 struct fs *fsp;
1747 1796 struct ufsvfs *ufsvfsp;
1748 1797 int blk, i;
1749 1798 long max_avail, used;
1750 1799 dev32_t d32;
1751 1800
1752 1801 if (vfsp->vfs_flag & VFS_UNMOUNTED)
1753 1802 return (EIO);
1754 1803
1755 1804 ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
1756 1805 fsp = ufsvfsp->vfs_fs;
1757 1806 if ((fsp->fs_magic != FS_MAGIC) && (fsp->fs_magic != MTB_UFS_MAGIC))
1758 1807 return (EINVAL);
1759 1808 if (fsp->fs_magic == FS_MAGIC &&
1760 1809 (fsp->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
1761 1810 fsp->fs_version != UFS_VERSION_MIN))
1762 1811 return (EINVAL);
1763 1812 if (fsp->fs_magic == MTB_UFS_MAGIC &&
1764 1813 (fsp->fs_version > MTB_UFS_VERSION_1 ||
1765 1814 fsp->fs_version < MTB_UFS_VERSION_MIN))
1766 1815 return (EINVAL);
1767 1816
1768 1817 /*
1769 1818 * get the basic numbers
1770 1819 */
1771 1820 (void) bzero(sp, sizeof (*sp));
1772 1821
1773 1822 sp->f_bsize = fsp->fs_bsize;
1774 1823 sp->f_frsize = fsp->fs_fsize;
1775 1824 sp->f_blocks = (fsblkcnt64_t)fsp->fs_dsize;
1776 1825 sp->f_bfree = (fsblkcnt64_t)fsp->fs_cstotal.cs_nbfree * fsp->fs_frag +
1777 1826 fsp->fs_cstotal.cs_nffree;
1778 1827
1779 1828 sp->f_files = (fsfilcnt64_t)fsp->fs_ncg * fsp->fs_ipg;
1780 1829 sp->f_ffree = (fsfilcnt64_t)fsp->fs_cstotal.cs_nifree;
1781 1830
1782 1831 /*
1783 1832 * Adjust the numbers based on things waiting to be deleted.
1784 1833 * modifies f_bfree and f_ffree. Afterwards, everything we
1785 1834 * come up with will be self-consistent. By definition, this
1786 1835 * is a point-in-time snapshot, so the fact that the delete
1787 1836 * thread's probably already invalidated the results is not a
1788 1837 * problem. Note that if the delete thread is ever extended to
1789 1838 * non-logging ufs, this adjustment must always be made.
1790 1839 */
1791 1840 if (TRANS_ISTRANS(ufsvfsp))
1792 1841 ufs_delete_adjust_stats(ufsvfsp, sp);
1793 1842
1794 1843 /*
1795 1844 * avail = MAX(max_avail - used, 0)
1796 1845 */
1797 1846 max_avail = fsp->fs_dsize - ufsvfsp->vfs_minfrags;
1798 1847
1799 1848 used = (fsp->fs_dsize - sp->f_bfree);
1800 1849
1801 1850 if (max_avail > used)
1802 1851 sp->f_bavail = (fsblkcnt64_t)max_avail - used;
1803 1852 else
1804 1853 sp->f_bavail = (fsblkcnt64_t)0;
1805 1854
1806 1855 sp->f_favail = sp->f_ffree;
1807 1856 (void) cmpldev(&d32, vfsp->vfs_dev);
1808 1857 sp->f_fsid = d32;
1809 1858 (void) strcpy(sp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
1810 1859 sp->f_flag = vf_to_stf(vfsp->vfs_flag);
1811 1860
1812 1861 /* keep coordinated with ufs_l_pathconf() */
1813 1862 sp->f_namemax = MAXNAMLEN;
1814 1863
1815 1864 if (fsp->fs_cpc == 0) {
1816 1865 bzero(sp->f_fstr, 14);
1817 1866 return (0);
1818 1867 }
1819 1868 blk = fsp->fs_spc * fsp->fs_cpc / NSPF(fsp);
1820 1869 for (i = 0; i < blk; i += fsp->fs_frag) /* CSTYLED */
1821 1870 /* void */;
1822 1871 i -= fsp->fs_frag;
1823 1872 blk = i / fsp->fs_frag;
1824 1873 bcopy(&(fs_rotbl(fsp)[blk]), sp->f_fstr, 14);
1825 1874 return (0);
1826 1875 }
1827 1876
1828 1877 /*
1829 1878 * Flush any pending I/O to file system vfsp.
1830 1879 * The ufs_update() routine will only flush *all* ufs files.
1831 1880 * If vfsp is non-NULL, only sync this ufs (in preparation
1832 1881 * for a umount).
1833 1882 */
1834 1883 /*ARGSUSED*/
1835 1884 static int
1836 1885 ufs_sync(struct vfs *vfsp, short flag, struct cred *cr)
1837 1886 {
1838 1887 struct ufsvfs *ufsvfsp;
1839 1888 struct fs *fs;
1840 1889 int cheap = flag & SYNC_ATTR;
1841 1890 int error;
1842 1891
1843 1892 /*
1844 1893 * SYNC_CLOSE means we're rebooting. Toss everything
1845 1894 * on the idle queue so we don't have to slog through
1846 1895 * a bunch of uninteresting inodes over and over again.
1847 1896 */
1848 1897 if (flag & SYNC_CLOSE)
1849 1898 ufs_idle_drain(NULL);
1850 1899
1851 1900 if (vfsp == NULL) {
1852 1901 ufs_update(flag);
1853 1902 return (0);
1854 1903 }
1855 1904
1856 1905 /* Flush a single ufs */
1857 1906 if (!vfs_matchops(vfsp, ufs_vfsops) || vfs_lock(vfsp) != 0)
1858 1907 return (0);
1859 1908
1860 1909 ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
1861 1910 if (!ufsvfsp)
1862 1911 return (EIO);
1863 1912 fs = ufsvfsp->vfs_fs;
1864 1913 mutex_enter(&ufsvfsp->vfs_lock);
1865 1914
1866 1915 if (ufsvfsp->vfs_dio &&
1867 1916 fs->fs_ronly == 0 &&
1868 1917 fs->fs_clean != FSBAD &&
1869 1918 fs->fs_clean != FSLOG) {
1870 1919 /* turn off fast-io on unmount, so no fsck needed (4029401) */
1871 1920 ufsvfsp->vfs_dio = 0;
1872 1921 fs->fs_clean = FSACTIVE;
1873 1922 fs->fs_fmod = 1;
1874 1923 }
1875 1924
1876 1925 /* Write back modified superblock */
1877 1926 if (fs->fs_fmod == 0) {
1878 1927 mutex_exit(&ufsvfsp->vfs_lock);
1879 1928 } else {
1880 1929 if (fs->fs_ronly != 0) {
1881 1930 mutex_exit(&ufsvfsp->vfs_lock);
1882 1931 vfs_unlock(vfsp);
1883 1932 return (ufs_fault(ufsvfsp->vfs_root,
1884 1933 "fs = %s update: ro fs mod\n", fs->fs_fsmnt));
1885 1934 }
1886 1935 fs->fs_fmod = 0;
1887 1936 mutex_exit(&ufsvfsp->vfs_lock);
1888 1937
1889 1938 TRANS_SBUPDATE(ufsvfsp, vfsp, TOP_SBUPDATE_UPDATE);
1890 1939 }
1891 1940 vfs_unlock(vfsp);
1892 1941
1893 1942 /*
1894 1943 * Avoid racing with ufs_update() and ufs_unmount().
1895 1944 *
1896 1945 */
1897 1946 mutex_enter(&ufs_scan_lock);
1898 1947
1899 1948 (void) ufs_scan_inodes(1, ufs_sync_inode,
1900 1949 (void *)(uintptr_t)cheap, ufsvfsp);
1901 1950
1902 1951 mutex_exit(&ufs_scan_lock);
1903 1952
1904 1953 bflush((dev_t)vfsp->vfs_dev);
1905 1954
1906 1955 /*
1907 1956 * commit any outstanding async transactions
1908 1957 */
1909 1958 curthread->t_flag |= T_DONTBLOCK;
1910 1959 TRANS_BEGIN_SYNC(ufsvfsp, TOP_COMMIT_UPDATE, TOP_COMMIT_SIZE, error);
1911 1960 if (!error) {
1912 1961 TRANS_END_SYNC(ufsvfsp, error, TOP_COMMIT_UPDATE,
1913 1962 TOP_COMMIT_SIZE);
1914 1963 }
1915 1964 curthread->t_flag &= ~T_DONTBLOCK;
1916 1965
1917 1966 return (0);
1918 1967 }
1919 1968
1920 1969
1921 1970 void
1922 1971 sbupdate(struct vfs *vfsp)
1923 1972 {
1924 1973 struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
1925 1974 struct fs *fs = ufsvfsp->vfs_fs;
1926 1975 struct buf *bp;
1927 1976 int blks;
1928 1977 caddr_t space;
1929 1978 int i;
1930 1979 size_t size;
1931 1980
1932 1981 /*
1933 1982 * for ulockfs processing, limit the superblock writes
1934 1983 */
1935 1984 if ((ufsvfsp->vfs_ulockfs.ul_sbowner) &&
1936 1985 (curthread != ufsvfsp->vfs_ulockfs.ul_sbowner)) {
1937 1986 /* process later */
1938 1987 fs->fs_fmod = 1;
1939 1988 return;
1940 1989 }
1941 1990 ULOCKFS_SET_MOD((&ufsvfsp->vfs_ulockfs));
1942 1991
1943 1992 if (TRANS_ISTRANS(ufsvfsp)) {
1944 1993 mutex_enter(&ufsvfsp->vfs_lock);
1945 1994 ufs_sbwrite(ufsvfsp);
1946 1995 mutex_exit(&ufsvfsp->vfs_lock);
1947 1996 return;
1948 1997 }
1949 1998
1950 1999 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1951 2000 space = (caddr_t)fs->fs_u.fs_csp;
1952 2001 for (i = 0; i < blks; i += fs->fs_frag) {
1953 2002 size = fs->fs_bsize;
1954 2003 if (i + fs->fs_frag > blks)
1955 2004 size = (blks - i) * fs->fs_fsize;
1956 2005 bp = UFS_GETBLK(ufsvfsp, ufsvfsp->vfs_dev,
1957 2006 (daddr_t)(fsbtodb(fs, fs->fs_csaddr + i)),
1958 2007 fs->fs_bsize);
1959 2008 bcopy(space, bp->b_un.b_addr, size);
1960 2009 space += size;
1961 2010 bp->b_bcount = size;
1962 2011 UFS_BRWRITE(ufsvfsp, bp);
1963 2012 }
1964 2013 mutex_enter(&ufsvfsp->vfs_lock);
1965 2014 ufs_sbwrite(ufsvfsp);
1966 2015 mutex_exit(&ufsvfsp->vfs_lock);
1967 2016 }
1968 2017
1969 2018 int ufs_vget_idle_count = 2; /* Number of inodes to idle each time */
1970 2019 static int
1971 2020 ufs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
1972 2021 {
1973 2022 int error = 0;
1974 2023 struct ufid *ufid;
1975 2024 struct inode *ip;
1976 2025 struct ufsvfs *ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
1977 2026 struct ulockfs *ulp;
1978 2027
1979 2028 /*
1980 2029 * Check for unmounted filesystem.
1981 2030 */
1982 2031 if (vfsp->vfs_flag & VFS_UNMOUNTED) {
1983 2032 error = EIO;
1984 2033 goto errout;
1985 2034 }
1986 2035
1987 2036 /*
1988 2037 * Keep the idle queue from getting too long by
1989 2038 * idling an inode before attempting to allocate another.
1990 2039 * This operation must be performed before entering
1991 2040 * lockfs or a transaction.
1992 2041 */
1993 2042 if (ufs_idle_q.uq_ne > ufs_idle_q.uq_hiwat)
1994 2043 if ((curthread->t_flag & T_DONTBLOCK) == 0) {
1995 2044 ins.in_vidles.value.ul += ufs_vget_idle_count;
1996 2045 ufs_idle_some(ufs_vget_idle_count);
1997 2046 }
1998 2047
1999 2048 ufid = (struct ufid *)fidp;
2000 2049
2001 2050 if (error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_VGET_MASK))
2002 2051 goto errout;
2003 2052
2004 2053 rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
2005 2054
2006 2055 error = ufs_iget(vfsp, ufid->ufid_ino, &ip, CRED());
2007 2056
2008 2057 rw_exit(&ufsvfsp->vfs_dqrwlock);
2009 2058
2010 2059 ufs_lockfs_end(ulp);
2011 2060
2012 2061 if (error)
2013 2062 goto errout;
2014 2063
2015 2064 /*
2016 2065 * Check if the inode has been deleted or freed or is in transient state
2017 2066 * since the last VFS_VGET() request for it, release it and don't return
2018 2067 * it to the caller, presumably NFS, as it's no longer valid.
2019 2068 */
2020 2069 if (ip->i_gen != ufid->ufid_gen || ip->i_mode == 0 ||
2021 2070 (ip->i_flag & IDEL)) {
2022 2071 VN_RELE(ITOV(ip));
2023 2072 error = EINVAL;
2024 2073 goto errout;
2025 2074 }
2026 2075
2027 2076 *vpp = ITOV(ip);
2028 2077 return (0);
2029 2078
2030 2079 errout:
2031 2080 *vpp = NULL;
2032 2081 return (error);
2033 2082 }
2034 2083
2035 2084 static int
2036 2085 ufsinit(int fstype, char *name)
2037 2086 {
2038 2087 static const fs_operation_def_t ufs_vfsops_template[] = {
2039 2088 VFSNAME_MOUNT, { .vfs_mount = ufs_mount },
2040 2089 VFSNAME_UNMOUNT, { .vfs_unmount = ufs_unmount },
2041 2090 VFSNAME_ROOT, { .vfs_root = ufs_root },
2042 2091 VFSNAME_STATVFS, { .vfs_statvfs = ufs_statvfs },
2043 2092 VFSNAME_SYNC, { .vfs_sync = ufs_sync },
2044 2093 VFSNAME_VGET, { .vfs_vget = ufs_vget },
2045 2094 VFSNAME_MOUNTROOT, { .vfs_mountroot = ufs_mountroot },
2046 2095 NULL, NULL
2047 2096 };
2048 2097 int error;
2049 2098
2050 2099 ufsfstype = fstype;
2051 2100
2052 2101 error = vfs_setfsops(fstype, ufs_vfsops_template, &ufs_vfsops);
2053 2102 if (error != 0) {
2054 2103 cmn_err(CE_WARN, "ufsinit: bad vfs ops template");
2055 2104 return (error);
2056 2105 }
2057 2106
2058 2107 error = vn_make_ops(name, ufs_vnodeops_template, &ufs_vnodeops);
2059 2108 if (error != 0) {
2060 2109 (void) vfs_freevfsops_by_type(fstype);
2061 2110 cmn_err(CE_WARN, "ufsinit: bad vnode ops template");
2062 2111 return (error);
2063 2112 }
2064 2113
2065 2114 ufs_iinit();
2066 2115 return (0);
2067 2116 }
2068 2117
2069 2118 #ifdef __sparc
2070 2119
2071 2120 /*
2072 2121 * Mounting a mirrored SVM volume is only supported on ufs,
2073 2122 * this is special-case boot code to support that configuration.
2074 2123 * At this point, we have booted and mounted root on a
2075 2124 * single component of the mirror. Complete the boot
2076 2125 * by configuring SVM and converting the root to the
2077 2126 * dev_t of the mirrored root device. This dev_t conversion
2078 2127 * only works because the underlying device doesn't change.
2079 2128 */
2080 2129 int
2081 2130 ufs_remountroot(struct vfs *vfsp)
2082 2131 {
2083 2132 struct ufsvfs *ufsvfsp;
2084 2133 struct ulockfs *ulp;
2085 2134 dev_t new_rootdev;
2086 2135 dev_t old_rootdev;
2087 2136 struct vnode *old_rootvp;
2088 2137 struct vnode *new_rootvp;
2089 2138 int error, sberror = 0;
2090 2139 struct inode *ip;
2091 2140 union ihead *ih;
2092 2141 struct buf *bp;
2093 2142 int i;
2094 2143
2095 2144 old_rootdev = rootdev;
2096 2145 old_rootvp = rootvp;
2097 2146
2098 2147 new_rootdev = getrootdev();
2099 2148 if (new_rootdev == (dev_t)NODEV) {
2100 2149 return (ENODEV);
2101 2150 }
2102 2151
2103 2152 new_rootvp = makespecvp(new_rootdev, VBLK);
2104 2153
2105 2154 error = VOP_OPEN(&new_rootvp,
2106 2155 (vfsp->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE, CRED(), NULL);
2107 2156 if (error) {
2108 2157 cmn_err(CE_CONT,
2109 2158 "Cannot open mirrored root device, error %d\n", error);
2110 2159 return (error);
2111 2160 }
2112 2161
2113 2162 if (vfs_lock(vfsp) != 0) {
2114 2163 return (EBUSY);
2115 2164 }
2116 2165
2117 2166 ufsvfsp = (struct ufsvfs *)vfsp->vfs_data;
2118 2167 ulp = &ufsvfsp->vfs_ulockfs;
2119 2168
2120 2169 mutex_enter(&ulp->ul_lock);
2121 2170 atomic_add_long(&ufs_quiesce_pend, 1);
2122 2171
2123 2172 (void) ufs_quiesce(ulp);
2124 2173 (void) ufs_flush(vfsp);
2125 2174
2126 2175 /*
2127 2176 * Convert root vfs to new dev_t, including vfs hash
2128 2177 * table and fs id.
2129 2178 */
2130 2179 vfs_root_redev(vfsp, new_rootdev, ufsfstype);
2131 2180
2132 2181 ufsvfsp->vfs_devvp = new_rootvp;
2133 2182 ufsvfsp->vfs_dev = new_rootdev;
2134 2183
2135 2184 bp = ufsvfsp->vfs_bufp;
2136 2185 bp->b_edev = new_rootdev;
2137 2186 bp->b_dev = cmpdev(new_rootdev);
2138 2187
2139 2188 /*
2140 2189 * The buffer for the root inode does not contain a valid b_vp
2141 2190 */
2142 2191 (void) bfinval(new_rootdev, 0);
2143 2192
2144 2193 /*
2145 2194 * Here we hand-craft inodes with old root device
2146 2195 * references to refer to the new device instead.
2147 2196 */
2148 2197 mutex_enter(&ufs_scan_lock);
2149 2198
2150 2199 for (i = 0, ih = ihead; i < inohsz; i++, ih++) {
2151 2200 mutex_enter(&ih_lock[i]);
2152 2201 for (ip = ih->ih_chain[0];
2153 2202 ip != (struct inode *)ih;
2154 2203 ip = ip->i_forw) {
2155 2204 if (ip->i_ufsvfs != ufsvfsp)
2156 2205 continue;
2157 2206 if (ip == ufsvfsp->vfs_qinod)
2158 2207 continue;
2159 2208 if (ip->i_dev == old_rootdev) {
2160 2209 ip->i_dev = new_rootdev;
2161 2210 }
2162 2211
2163 2212 if (ip->i_devvp == old_rootvp) {
2164 2213 ip->i_devvp = new_rootvp;
2165 2214 }
2166 2215 }
2167 2216 mutex_exit(&ih_lock[i]);
2168 2217 }
2169 2218
2170 2219 mutex_exit(&ufs_scan_lock);
2171 2220
2172 2221 /*
2173 2222 * Make Sure logging structures are using the new device
2174 2223 * if logging is enabled. Also start any logging thread that
2175 2224 * needs to write to the device and couldn't earlier.
2176 2225 */
2177 2226 if (ufsvfsp->vfs_log) {
2178 2227 buf_t *bp, *tbp;
2179 2228 ml_unit_t *ul = ufsvfsp->vfs_log;
2180 2229 struct fs *fsp = ufsvfsp->vfs_fs;
2181 2230
2182 2231 /*
2183 2232 * Update the main logging structure.
2184 2233 */
2185 2234 ul->un_dev = new_rootdev;
2186 2235
2187 2236 /*
2188 2237 * Get a new bp for the on disk structures.
2189 2238 */
2190 2239 bp = ul->un_bp;
2191 2240 tbp = ngeteblk(dbtob(LS_SECTORS));
2192 2241 tbp->b_edev = new_rootdev;
2193 2242 tbp->b_dev = cmpdev(new_rootdev);
2194 2243 tbp->b_blkno = bp->b_blkno;
2195 2244 bcopy(bp->b_un.b_addr, tbp->b_un.b_addr, DEV_BSIZE);
2196 2245 bcopy(bp->b_un.b_addr, tbp->b_un.b_addr + DEV_BSIZE, DEV_BSIZE);
2197 2246 bp->b_flags |= (B_STALE | B_AGE);
2198 2247 brelse(bp);
2199 2248 ul->un_bp = tbp;
2200 2249
2201 2250 /*
2202 2251 * Allocate new circular buffers.
2203 2252 */
2204 2253 alloc_rdbuf(&ul->un_rdbuf, MAPBLOCKSIZE, MAPBLOCKSIZE);
2205 2254 alloc_wrbuf(&ul->un_wrbuf, ldl_bufsize(ul));
2206 2255
2207 2256 /*
2208 2257 * Clear the noroll bit which indicates that logging
2209 2258 * can't roll the log yet and start the logmap roll thread
2210 2259 * unless the filesystem is still read-only in which case
2211 2260 * remountfs() will do it when going to read-write.
2212 2261 */
2213 2262 ASSERT(ul->un_flags & LDL_NOROLL);
2214 2263
2215 2264 if (!fsp->fs_ronly) {
2216 2265 ul->un_flags &= ~LDL_NOROLL;
2217 2266 logmap_start_roll(ul);
2218 2267 }
2219 2268
2220 2269 /*
2221 2270 * Start the reclaim thread if needed.
2222 2271 */
2223 2272 if (!fsp->fs_ronly && (fsp->fs_reclaim &
2224 2273 (FS_RECLAIM|FS_RECLAIMING))) {
2225 2274 fsp->fs_reclaim &= ~FS_RECLAIM;
2226 2275 fsp->fs_reclaim |= FS_RECLAIMING;
2227 2276 ufs_thread_start(&ufsvfsp->vfs_reclaim,
2228 2277 ufs_thread_reclaim, vfsp);
2229 2278 TRANS_SBWRITE(ufsvfsp, TOP_SBUPDATE_UPDATE);
2230 2279 if (sberror = geterror(ufsvfsp->vfs_bufp)) {
2231 2280 refstr_t *mntpt;
2232 2281 mntpt = vfs_getmntpoint(vfsp);
2233 2282 cmn_err(CE_WARN,
2234 2283 "Remountroot failed to update Reclaim"
2235 2284 "state for filesystem %s "
2236 2285 "Error writing SuperBlock %d",
2237 2286 refstr_value(mntpt), error);
2238 2287 refstr_rele(mntpt);
2239 2288 }
2240 2289 }
2241 2290 }
2242 2291
2243 2292 rootdev = new_rootdev;
2244 2293 rootvp = new_rootvp;
2245 2294
2246 2295 atomic_add_long(&ufs_quiesce_pend, -1);
2247 2296 cv_broadcast(&ulp->ul_cv);
2248 2297 mutex_exit(&ulp->ul_lock);
2249 2298
2250 2299 vfs_unlock(vfsp);
2251 2300
2252 2301 error = VOP_CLOSE(old_rootvp, FREAD, 1, (offset_t)0, CRED(), NULL);
2253 2302 if (error) {
2254 2303 cmn_err(CE_CONT,
2255 2304 "close of root device component failed, error %d\n",
2256 2305 error);
2257 2306 }
2258 2307 VN_RELE(old_rootvp);
2259 2308
2260 2309 return (sberror ? sberror : error);
2261 2310 }
2262 2311
2263 2312 #endif /* __sparc */
|
↓ open down ↓ |
1844 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX