Print this page
NEX-17446 cleanup of hot unplugged disks fails intermittently
Reviewed by: Dan Fields <dan.fields@nexenta.com>
Reviewed by: Evan Layton <evan.layton@nexenta.com>
Reviewed by: Rick McNeal <rick.mcneal@nexenta.com>
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/fs/devfs/devfs_subr.c
+++ new/usr/src/uts/common/fs/devfs/devfs_subr.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
|
↓ open down ↓ |
10 lines elided |
↑ open up ↑ |
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 23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 + * Copyright 2018 Nexenta Systems, Inc.
23 25 */
24 26
25 27 /*
26 28 * miscellaneous routines for the devfs
27 29 */
28 30
29 31 #include <sys/types.h>
30 32 #include <sys/param.h>
31 33 #include <sys/t_lock.h>
32 34 #include <sys/systm.h>
33 35 #include <sys/sysmacros.h>
34 36 #include <sys/user.h>
35 37 #include <sys/time.h>
36 38 #include <sys/vfs.h>
37 39 #include <sys/vnode.h>
38 40 #include <sys/file.h>
39 41 #include <sys/fcntl.h>
40 42 #include <sys/flock.h>
41 43 #include <sys/kmem.h>
42 44 #include <sys/uio.h>
43 45 #include <sys/errno.h>
44 46 #include <sys/stat.h>
45 47 #include <sys/cred.h>
46 48 #include <sys/dirent.h>
47 49 #include <sys/pathname.h>
48 50 #include <sys/cmn_err.h>
49 51 #include <sys/debug.h>
50 52 #include <sys/modctl.h>
51 53 #include <fs/fs_subr.h>
52 54 #include <sys/fs/dv_node.h>
53 55 #include <sys/fs/snode.h>
54 56 #include <sys/sunndi.h>
55 57 #include <sys/sunmdi.h>
56 58 #include <sys/conf.h>
57 59
58 60 #ifdef DEBUG
59 61 int devfs_debug = 0x0;
60 62 #endif
61 63
62 64 const char dvnm[] = "devfs";
63 65 kmem_cache_t *dv_node_cache; /* dv_node cache */
64 66
|
↓ open down ↓ |
32 lines elided |
↑ open up ↑ |
65 67 /*
66 68 * The devfs_clean_key is taken during a devfs_clean operation: it is used to
67 69 * prevent unnecessary code execution and for detection of potential deadlocks.
68 70 */
69 71 uint_t devfs_clean_key;
70 72
71 73 struct dv_node *dvroot;
72 74
73 75 /* prototype memory vattrs */
74 76 vattr_t dv_vattr_dir = {
75 - AT_TYPE|AT_MODE|AT_UID|AT_GID, /* va_mask */
77 + AT_TYPE|AT_MODE|AT_UID|AT_GID, /* va_mask */
76 78 VDIR, /* va_type */
77 79 DV_DIRMODE_DEFAULT, /* va_mode */
78 80 DV_UID_DEFAULT, /* va_uid */
79 81 DV_GID_DEFAULT, /* va_gid */
80 82 0, /* va_fsid; */
81 83 0, /* va_nodeid; */
82 84 0, /* va_nlink; */
83 85 0, /* va_size; */
84 86 0, /* va_atime; */
85 87 0, /* va_mtime; */
86 88 0, /* va_ctime; */
87 89 0, /* va_rdev; */
88 90 0, /* va_blksize; */
89 91 0, /* va_nblocks; */
90 92 0, /* va_seq; */
91 93 };
92 94
93 95 vattr_t dv_vattr_file = {
94 96 AT_TYPE|AT_MODE|AT_SIZE|AT_UID|AT_GID|AT_RDEV, /* va_mask */
95 97 0, /* va_type */
96 98 DV_DEVMODE_DEFAULT, /* va_mode */
97 99 DV_UID_DEFAULT, /* va_uid */
98 100 DV_GID_DEFAULT, /* va_gid */
99 101 0, /* va_fsid; */
100 102 0, /* va_nodeid; */
101 103 0, /* va_nlink; */
102 104 0, /* va_size; */
103 105 0, /* va_atime; */
104 106 0, /* va_mtime; */
105 107 0, /* va_ctime; */
106 108 0, /* va_rdev; */
107 109 0, /* va_blksize; */
108 110 0, /* va_nblocks; */
109 111 0, /* va_seq; */
110 112 };
111 113
112 114 vattr_t dv_vattr_priv = {
113 115 AT_TYPE|AT_MODE|AT_SIZE|AT_UID|AT_GID|AT_RDEV, /* va_mask */
114 116 0, /* va_type */
115 117 DV_DEVMODE_PRIV, /* va_mode */
116 118 DV_UID_DEFAULT, /* va_uid */
117 119 DV_GID_DEFAULT, /* va_gid */
118 120 0, /* va_fsid; */
119 121 0, /* va_nodeid; */
120 122 0, /* va_nlink; */
121 123 0, /* va_size; */
122 124 0, /* va_atime; */
123 125 0, /* va_mtime; */
124 126 0, /* va_ctime; */
125 127 0, /* va_rdev; */
126 128 0, /* va_blksize; */
127 129 0, /* va_nblocks; */
128 130 0, /* va_seq; */
129 131 };
130 132
131 133 extern dev_info_t *clone_dip;
132 134 extern major_t clone_major;
133 135 extern struct dev_ops *ddi_hold_driver(major_t);
134 136
135 137 /* dv_node node constructor for kmem cache */
136 138 static int
137 139 i_dv_node_ctor(void *buf, void *cfarg, int flag)
138 140 {
139 141 _NOTE(ARGUNUSED(cfarg, flag))
140 142 struct dv_node *dv = (struct dv_node *)buf;
141 143 struct vnode *vp;
142 144
143 145 bzero(buf, sizeof (struct dv_node));
144 146 vp = dv->dv_vnode = vn_alloc(flag);
145 147 if (vp == NULL) {
146 148 return (-1);
147 149 }
148 150 vp->v_data = dv;
149 151 rw_init(&dv->dv_contents, NULL, RW_DEFAULT, NULL);
150 152 return (0);
151 153 }
152 154
153 155 /* dv_node node destructor for kmem cache */
154 156 static void
155 157 i_dv_node_dtor(void *buf, void *arg)
156 158 {
157 159 _NOTE(ARGUNUSED(arg))
158 160 struct dv_node *dv = (struct dv_node *)buf;
159 161 struct vnode *vp = DVTOV(dv);
160 162
161 163 rw_destroy(&dv->dv_contents);
162 164 vn_invalid(vp);
163 165 vn_free(vp);
164 166 }
165 167
166 168
167 169 /* initialize dv_node node cache */
168 170 void
169 171 dv_node_cache_init()
170 172 {
171 173 ASSERT(dv_node_cache == NULL);
172 174 dv_node_cache = kmem_cache_create("dv_node_cache",
173 175 sizeof (struct dv_node), 0, i_dv_node_ctor, i_dv_node_dtor,
174 176 NULL, NULL, NULL, 0);
175 177
176 178 tsd_create(&devfs_clean_key, NULL);
177 179 }
178 180
179 181 /* destroy dv_node node cache */
180 182 void
181 183 dv_node_cache_fini()
182 184 {
183 185 ASSERT(dv_node_cache != NULL);
184 186 kmem_cache_destroy(dv_node_cache);
185 187 dv_node_cache = NULL;
186 188
187 189 tsd_destroy(&devfs_clean_key);
188 190 }
189 191
190 192 /*
191 193 * dv_mkino - Generate a unique inode number for devfs nodes.
192 194 *
193 195 * Although ino_t is 64 bits, the inode number is truncated to 32 bits for 32
194 196 * bit non-LARGEFILE applications. This means that there is a requirement to
195 197 * maintain the inode number as a 32 bit value or applications will have
196 198 * stat(2) calls fail with EOVERFLOW. We form a 32 bit inode number from the
197 199 * dev_t. but if the minor number is larger than L_MAXMIN32 we fold extra minor
198 200 *
199 201 * To generate inode numbers for directories, we assume that we will never use
200 202 * more than half the major space - this allows for ~8190 drivers. We use this
201 203 * upper major number space to allocate inode numbers for directories by
202 204 * encoding the major and instance into this space.
203 205 *
204 206 * We also skew the result so that inode 2 is reserved for the root of the file
205 207 * system.
206 208 *
207 209 * As part of the future support for 64-bit dev_t APIs, the upper minor bits
208 210 * should be folded into the high inode bits by adding the following code
209 211 * after "ino |= 1":
210 212 *
211 213 * #if (L_BITSMINOR32 != L_BITSMINOR)
212 214 * |* fold overflow minor bits into high bits of inode number *|
213 215 * ino |= ((ino_t)(minor >> L_BITSMINOR32)) << L_BITSMINOR;
214 216 * #endif |* (L_BITSMINOR32 != L_BITSMINOR) *|
215 217 *
216 218 * This way only applications that use devices that overflow their minor
217 219 * space will have an application level impact.
218 220 */
219 221 static ino_t
220 222 dv_mkino(dev_info_t *devi, vtype_t typ, dev_t dev)
221 223 {
222 224 major_t major;
223 225 minor_t minor;
224 226 ino_t ino;
225 227 static int warn;
226 228
227 229 if (typ == VDIR) {
228 230 major = ((L_MAXMAJ32 + 1) >> 1) + DEVI(devi)->devi_major;
229 231 minor = ddi_get_instance(devi);
230 232
231 233 /* makedevice32 in high half of major number space */
232 234 ino = (ino_t)((major << L_BITSMINOR32) | (minor & L_MAXMIN32));
233 235
234 236 major = DEVI(devi)->devi_major;
235 237 } else {
236 238 major = getmajor(dev);
237 239 minor = getminor(dev);
238 240
239 241 /* makedevice32 */
240 242 ino = (ino_t)((major << L_BITSMINOR32) | (minor & L_MAXMIN32));
241 243
242 244 /* make ino for VCHR different than VBLK */
243 245 ino <<= 1;
244 246 if (typ == VCHR)
245 247 ino |= 1;
246 248 }
247 249
248 250 ino += DV_ROOTINO + 1; /* skew */
249 251
250 252 /*
251 253 * diagnose things a little early because adding the skew to a large
252 254 * minor number could roll over the major.
253 255 */
254 256 if ((major >= (L_MAXMAJ32 >> 1)) && (warn == 0)) {
255 257 warn = 1;
256 258 cmn_err(CE_WARN, "%s: inode numbers are not unique", dvnm);
257 259 }
258 260
259 261 return (ino);
260 262 }
261 263
262 264 /*
263 265 * Compare two nodes lexographically to balance avl tree
264 266 */
265 267 static int
266 268 dv_compare_nodes(const struct dv_node *dv1, const struct dv_node *dv2)
267 269 {
268 270 int rv;
269 271
270 272 if ((rv = strcmp(dv1->dv_name, dv2->dv_name)) == 0)
271 273 return (0);
272 274 return ((rv < 0) ? -1 : 1);
273 275 }
274 276
275 277 /*
276 278 * dv_mkroot
277 279 *
278 280 * Build the first VDIR dv_node.
279 281 */
280 282 struct dv_node *
281 283 dv_mkroot(struct vfs *vfsp, dev_t devfsdev)
282 284 {
283 285 struct dv_node *dv;
284 286 struct vnode *vp;
285 287
286 288 ASSERT(ddi_root_node() != NULL);
287 289 ASSERT(dv_node_cache != NULL);
288 290
289 291 dcmn_err3(("dv_mkroot\n"));
290 292 dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP);
291 293 vp = DVTOV(dv);
292 294 vn_reinit(vp);
293 295 vp->v_flag = VROOT;
294 296 vp->v_vfsp = vfsp;
295 297 vp->v_type = VDIR;
296 298 vp->v_rdev = devfsdev;
297 299 vn_setops(vp, dv_vnodeops);
298 300 vn_exists(vp);
299 301
300 302 dvroot = dv;
301 303
302 304 dv->dv_name = NULL; /* not needed */
303 305 dv->dv_namelen = 0;
304 306
305 307 dv->dv_devi = ddi_root_node();
306 308
307 309 dv->dv_ino = DV_ROOTINO;
308 310 dv->dv_nlink = 2; /* name + . (no dv_insert) */
309 311 dv->dv_dotdot = dv; /* .. == self */
310 312 dv->dv_attrvp = NULLVP;
311 313 dv->dv_attr = NULL;
312 314 dv->dv_flags = DV_BUILD;
313 315 dv->dv_priv = NULL;
314 316 dv->dv_busy = 0;
315 317 dv->dv_dflt_mode = 0;
316 318
317 319 avl_create(&dv->dv_entries,
318 320 (int (*)(const void *, const void *))dv_compare_nodes,
319 321 sizeof (struct dv_node), offsetof(struct dv_node, dv_avllink));
320 322
321 323 return (dv);
322 324 }
323 325
324 326 /*
325 327 * dv_mkdir
326 328 *
327 329 * Given an probed or attached nexus node, create a VDIR dv_node.
328 330 * No dv_attrvp is created at this point.
329 331 */
330 332 struct dv_node *
331 333 dv_mkdir(struct dv_node *ddv, dev_info_t *devi, char *nm)
332 334 {
333 335 struct dv_node *dv;
334 336 struct vnode *vp;
335 337 size_t nmlen;
336 338
337 339 ASSERT((devi));
338 340 dcmn_err4(("dv_mkdir: %s\n", nm));
339 341
340 342 dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP);
341 343 nmlen = strlen(nm) + 1;
342 344 dv->dv_name = kmem_alloc(nmlen, KM_SLEEP);
343 345 bcopy(nm, dv->dv_name, nmlen);
344 346 dv->dv_namelen = nmlen - 1; /* '\0' not included */
345 347
346 348 vp = DVTOV(dv);
347 349 vn_reinit(vp);
348 350 vp->v_flag = 0;
349 351 vp->v_vfsp = DVTOV(ddv)->v_vfsp;
350 352 vp->v_type = VDIR;
351 353 vp->v_rdev = DVTOV(ddv)->v_rdev;
352 354 vn_setops(vp, vn_getops(DVTOV(ddv)));
353 355 vn_exists(vp);
354 356
355 357 dv->dv_devi = devi;
356 358 ndi_hold_devi(devi);
357 359
358 360 dv->dv_ino = dv_mkino(devi, VDIR, NODEV);
359 361 dv->dv_nlink = 0; /* updated on insert */
360 362 dv->dv_dotdot = ddv;
361 363 dv->dv_attrvp = NULLVP;
362 364 dv->dv_attr = NULL;
363 365 dv->dv_flags = DV_BUILD;
364 366 dv->dv_priv = NULL;
365 367 dv->dv_busy = 0;
366 368 dv->dv_dflt_mode = 0;
367 369
368 370 avl_create(&dv->dv_entries,
369 371 (int (*)(const void *, const void *))dv_compare_nodes,
370 372 sizeof (struct dv_node), offsetof(struct dv_node, dv_avllink));
371 373
372 374 return (dv);
|
↓ open down ↓ |
287 lines elided |
↑ open up ↑ |
373 375 }
374 376
375 377 /*
376 378 * dv_mknod
377 379 *
378 380 * Given a minor node, create a VCHR or VBLK dv_node.
379 381 * No dv_attrvp is created at this point.
380 382 */
381 383 static struct dv_node *
382 384 dv_mknod(struct dv_node *ddv, dev_info_t *devi, char *nm,
383 - struct ddi_minor_data *dmd)
385 + struct ddi_minor_data *dmd)
384 386 {
385 387 struct dv_node *dv;
386 388 struct vnode *vp;
387 389 size_t nmlen;
388 390
389 391 dcmn_err4(("dv_mknod: %s\n", nm));
390 392
391 393 dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP);
392 394 nmlen = strlen(nm) + 1;
393 395 dv->dv_name = kmem_alloc(nmlen, KM_SLEEP);
394 396 bcopy(nm, dv->dv_name, nmlen);
395 397 dv->dv_namelen = nmlen - 1; /* no '\0' */
|
↓ open down ↓ |
2 lines elided |
↑ open up ↑ |
396 398
397 399 vp = DVTOV(dv);
398 400 vn_reinit(vp);
399 401 vp->v_flag = 0;
400 402 vp->v_vfsp = DVTOV(ddv)->v_vfsp;
401 403 vp->v_type = dmd->ddm_spec_type == S_IFCHR ? VCHR : VBLK;
402 404 vp->v_rdev = dmd->ddm_dev;
403 405 vn_setops(vp, vn_getops(DVTOV(ddv)));
404 406 vn_exists(vp);
405 407
406 - /* increment dev_ref with devi_lock held */
407 408 ASSERT(DEVI_BUSY_OWNED(devi));
408 - mutex_enter(&DEVI(devi)->devi_lock);
409 - dv->dv_devi = devi;
410 - DEVI(devi)->devi_ref++; /* ndi_hold_devi(dip) */
411 - mutex_exit(&DEVI(devi)->devi_lock);
409 + ndi_hold_devi(devi);
412 410
411 + dv->dv_devi = devi;
413 412 dv->dv_ino = dv_mkino(devi, vp->v_type, vp->v_rdev);
414 413 dv->dv_nlink = 0; /* updated on insert */
415 414 dv->dv_dotdot = ddv;
416 415 dv->dv_attrvp = NULLVP;
417 416 dv->dv_attr = NULL;
418 417 dv->dv_flags = 0;
419 418
420 419 if (dmd->type == DDM_INTERNAL_PATH)
421 420 dv->dv_flags |= DV_INTERNAL;
422 421 if (dmd->ddm_flags & DM_NO_FSPERM)
423 422 dv->dv_flags |= DV_NO_FSPERM;
424 423
425 424 dv->dv_priv = dmd->ddm_node_priv;
426 425 if (dv->dv_priv)
427 426 dphold(dv->dv_priv);
428 427
429 428 /*
430 429 * Minors created with ddi_create_priv_minor_node can specify
431 430 * a default mode permission other than the devfs default.
432 431 */
433 432 if (dv->dv_priv || dv->dv_flags & DV_NO_FSPERM) {
434 433 dcmn_err5(("%s: dv_mknod default priv mode 0%o\n",
435 434 dv->dv_name, dmd->ddm_priv_mode));
436 435 dv->dv_flags |= DV_DFLT_MODE;
437 436 dv->dv_dflt_mode = dmd->ddm_priv_mode & S_IAMB;
438 437 }
439 438
440 439 return (dv);
441 440 }
442 441
443 442 /*
444 443 * dv_destroy
445 444 *
446 445 * Destroy what we created in dv_mkdir or dv_mknod.
447 446 * In the case of a *referenced* directory, do nothing.
448 447 */
449 448 void
450 449 dv_destroy(struct dv_node *dv, uint_t flags)
451 450 {
452 451 vnode_t *vp = DVTOV(dv);
453 452 ASSERT(dv->dv_nlink == 0); /* no references */
454 453
455 454 dcmn_err4(("dv_destroy: %s\n", dv->dv_name));
456 455
457 456 /*
458 457 * We may be asked to unlink referenced directories.
459 458 * In this case, there is nothing to be done.
460 459 * The eventual memory free will be done in
461 460 * devfs_inactive.
462 461 */
463 462 if (vp->v_count != 0) {
464 463 ASSERT(vp->v_type == VDIR);
465 464 ASSERT(flags & DV_CLEAN_FORCE);
466 465 ASSERT(DV_STALE(dv));
467 466 return;
468 467 }
469 468
470 469 if (vp->v_type == VDIR) {
471 470 ASSERT(DV_FIRST_ENTRY(dv) == NULL);
472 471 avl_destroy(&dv->dv_entries);
473 472 }
474 473
475 474 if (dv->dv_attrvp != NULLVP)
476 475 VN_RELE(dv->dv_attrvp);
477 476 if (dv->dv_attr != NULL)
478 477 kmem_free(dv->dv_attr, sizeof (struct vattr));
479 478 if (dv->dv_name != NULL)
480 479 kmem_free(dv->dv_name, dv->dv_namelen + 1);
481 480 if (dv->dv_devi != NULL) {
482 481 ndi_rele_devi(dv->dv_devi);
483 482 }
484 483 if (dv->dv_priv != NULL) {
485 484 dpfree(dv->dv_priv);
486 485 }
487 486
488 487 kmem_cache_free(dv_node_cache, dv);
489 488 }
490 489
491 490 /*
492 491 * Find and hold dv_node by name
493 492 */
494 493 static struct dv_node *
495 494 dv_findbyname(struct dv_node *ddv, char *nm)
496 495 {
497 496 struct dv_node *dv;
498 497 avl_index_t where;
499 498 struct dv_node dvtmp;
500 499
501 500 ASSERT(RW_LOCK_HELD(&ddv->dv_contents));
502 501 dcmn_err3(("dv_findbyname: %s\n", nm));
503 502
504 503 dvtmp.dv_name = nm;
505 504 dv = avl_find(&ddv->dv_entries, &dvtmp, &where);
506 505 if (dv) {
507 506 ASSERT(dv->dv_dotdot == ddv);
508 507 ASSERT(strcmp(dv->dv_name, nm) == 0);
509 508 VN_HOLD(DVTOV(dv));
510 509 return (dv);
511 510 }
512 511 return (NULL);
513 512 }
514 513
515 514 /*
516 515 * Inserts a new dv_node in a parent directory
517 516 */
518 517 void
519 518 dv_insert(struct dv_node *ddv, struct dv_node *dv)
520 519 {
521 520 avl_index_t where;
522 521
523 522 ASSERT(RW_WRITE_HELD(&ddv->dv_contents));
524 523 ASSERT(DVTOV(ddv)->v_type == VDIR);
525 524 ASSERT(ddv->dv_nlink >= 2);
526 525 ASSERT(dv->dv_nlink == 0);
527 526
528 527 dcmn_err3(("dv_insert: %s\n", dv->dv_name));
529 528
530 529 dv->dv_dotdot = ddv;
531 530 if (DVTOV(dv)->v_type == VDIR) {
532 531 ddv->dv_nlink++; /* .. to containing directory */
533 532 dv->dv_nlink = 2; /* name + . */
534 533 } else {
535 534 dv->dv_nlink = 1; /* name */
536 535 }
537 536
538 537 /* enter node in the avl tree */
539 538 VERIFY(avl_find(&ddv->dv_entries, dv, &where) == NULL);
540 539 avl_insert(&ddv->dv_entries, dv, where);
541 540 }
542 541
543 542 /*
544 543 * Unlink a dv_node from a perent directory
545 544 */
546 545 void
547 546 dv_unlink(struct dv_node *ddv, struct dv_node *dv)
548 547 {
549 548 /* verify linkage of arguments */
550 549 ASSERT(ddv && dv);
551 550 ASSERT(dv->dv_dotdot == ddv);
552 551 ASSERT(RW_WRITE_HELD(&ddv->dv_contents));
553 552 ASSERT(DVTOV(ddv)->v_type == VDIR);
554 553
555 554 dcmn_err3(("dv_unlink: %s\n", dv->dv_name));
556 555
557 556 if (DVTOV(dv)->v_type == VDIR) {
558 557 ddv->dv_nlink--; /* .. to containing directory */
559 558 dv->dv_nlink -= 2; /* name + . */
560 559 } else {
561 560 dv->dv_nlink -= 1; /* name */
562 561 }
563 562 ASSERT(ddv->dv_nlink >= 2);
564 563 ASSERT(dv->dv_nlink == 0);
565 564
566 565 dv->dv_dotdot = NULL;
567 566
568 567 /* remove from avl tree */
569 568 avl_remove(&ddv->dv_entries, dv);
570 569 }
571 570
572 571 /*
573 572 * Merge devfs node specific information into an attribute structure.
574 573 *
575 574 * NOTE: specfs provides ATIME,MTIME,CTIME,SIZE,BLKSIZE,NBLOCKS on leaf node.
576 575 */
577 576 void
578 577 dv_vattr_merge(struct dv_node *dv, struct vattr *vap)
579 578 {
580 579 struct vnode *vp = DVTOV(dv);
581 580
582 581 vap->va_nodeid = dv->dv_ino;
583 582 vap->va_nlink = dv->dv_nlink;
584 583
585 584 if (vp->v_type == VDIR) {
586 585 vap->va_rdev = 0;
587 586 vap->va_fsid = vp->v_rdev;
588 587 } else {
589 588 vap->va_rdev = vp->v_rdev;
590 589 vap->va_fsid = DVTOV(dv->dv_dotdot)->v_rdev;
591 590 vap->va_type = vp->v_type;
592 591 /* don't trust the shadow file type */
593 592 vap->va_mode &= ~S_IFMT;
594 593 if (vap->va_type == VCHR)
595 594 vap->va_mode |= S_IFCHR;
596 595 else
597 596 vap->va_mode |= S_IFBLK;
598 597 }
599 598 }
600 599
601 600 /*
602 601 * Get default device permission by consulting rules in
603 602 * privilege specification in minor node and /etc/minor_perm.
604 603 *
605 604 * This function is called from the devname filesystem to get default
606 605 * permissions for a device exported to a non-global zone.
607 606 */
608 607 void
609 608 devfs_get_defattr(struct vnode *vp, struct vattr *vap, int *no_fs_perm)
610 609 {
611 610 mperm_t mp;
612 611 struct dv_node *dv;
613 612
614 613 /* If vp isn't a dv_node, return something sensible */
615 614 if (!vn_matchops(vp, dv_vnodeops)) {
616 615 if (no_fs_perm)
617 616 *no_fs_perm = 0;
618 617 *vap = dv_vattr_file;
619 618 return;
620 619 }
621 620
622 621 /*
623 622 * For minors not created by ddi_create_priv_minor_node(),
624 623 * use devfs defaults.
625 624 */
626 625 dv = VTODV(vp);
627 626 if (vp->v_type == VDIR) {
628 627 *vap = dv_vattr_dir;
629 628 } else if (dv->dv_flags & DV_NO_FSPERM) {
630 629 if (no_fs_perm)
631 630 *no_fs_perm = 1;
632 631 *vap = dv_vattr_priv;
633 632 } else {
634 633 /*
635 634 * look up perm bits from minor_perm
636 635 */
637 636 *vap = dv_vattr_file;
638 637 if (dev_minorperm(dv->dv_devi, dv->dv_name, &mp) == 0) {
639 638 VATTR_MP_MERGE((*vap), mp);
640 639 dcmn_err5(("%s: minor perm mode 0%o\n",
641 640 dv->dv_name, vap->va_mode));
642 641 } else if (dv->dv_flags & DV_DFLT_MODE) {
643 642 ASSERT((dv->dv_dflt_mode & ~S_IAMB) == 0);
644 643 vap->va_mode &= ~S_IAMB;
645 644 vap->va_mode |= dv->dv_dflt_mode;
646 645 dcmn_err5(("%s: priv mode 0%o\n",
647 646 dv->dv_name, vap->va_mode));
648 647 }
649 648 }
650 649 }
651 650
652 651 /*
653 652 * dv_shadow_node
654 653 *
655 654 * Given a VDIR dv_node, find/create the associated VDIR
656 655 * node in the shadow attribute filesystem.
657 656 *
658 657 * Given a VCHR/VBLK dv_node, find the associated VREG
659 658 * node in the shadow attribute filesystem. These nodes
660 659 * are only created to persist non-default attributes.
661 660 * Lack of such a node implies the default permissions
662 661 * are sufficient.
663 662 *
664 663 * Managing the attribute file entries is slightly tricky (mostly
665 664 * because we can't intercept VN_HOLD and VN_RELE except on the last
666 665 * release).
667 666 *
668 667 * We assert that if the dv_attrvp pointer is non-NULL, it points
669 668 * to a singly-held (by us) vnode that represents the shadow entry
670 669 * in the underlying filesystem. To avoid store-ordering issues,
671 670 * we assert that the pointer can only be tested under the dv_contents
672 671 * READERS lock.
673 672 */
674 673
675 674 void
676 675 dv_shadow_node(
677 676 struct vnode *dvp, /* devfs parent directory vnode */
678 677 char *nm, /* name component */
679 678 struct vnode *vp, /* devfs vnode */
680 679 struct pathname *pnp, /* the path .. */
681 680 struct vnode *rdir, /* the root .. */
682 681 struct cred *cred, /* who's asking? */
683 682 int flags) /* optionally create shadow node */
684 683 {
685 684 struct dv_node *dv; /* dv_node of named directory */
686 685 struct vnode *rdvp; /* shadow parent directory vnode */
687 686 struct vnode *rvp; /* shadow vnode */
688 687 struct vnode *rrvp; /* realvp of shadow vnode */
689 688 struct vattr vattr;
690 689 int create_tried;
691 690 int error;
692 691
693 692 ASSERT(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK);
694 693 dv = VTODV(vp);
695 694 dcmn_err3(("dv_shadow_node: name %s attr %p\n",
696 695 nm, (void *)dv->dv_attrvp));
697 696
698 697 if ((flags & DV_SHADOW_WRITE_HELD) == 0) {
699 698 ASSERT(RW_READ_HELD(&dv->dv_contents));
700 699 if (dv->dv_attrvp != NULLVP)
701 700 return;
702 701 if (!rw_tryupgrade(&dv->dv_contents)) {
703 702 rw_exit(&dv->dv_contents);
704 703 rw_enter(&dv->dv_contents, RW_WRITER);
705 704 if (dv->dv_attrvp != NULLVP) {
706 705 rw_downgrade(&dv->dv_contents);
707 706 return;
708 707 }
709 708 }
710 709 } else {
711 710 ASSERT(RW_WRITE_HELD(&dv->dv_contents));
712 711 if (dv->dv_attrvp != NULLVP)
713 712 return;
714 713 }
715 714
716 715 ASSERT(RW_WRITE_HELD(&dv->dv_contents) && dv->dv_attrvp == NULL);
717 716
718 717 rdvp = VTODV(dvp)->dv_attrvp;
719 718 create_tried = 0;
720 719 lookup:
721 720 if (rdvp && (dv->dv_flags & DV_NO_FSPERM) == 0) {
722 721 error = VOP_LOOKUP(rdvp, nm, &rvp, pnp, LOOKUP_DIR, rdir, cred,
723 722 NULL, NULL, NULL);
724 723
725 724 /* factor out the snode since we only want the attribute node */
726 725 if ((error == 0) && (VOP_REALVP(rvp, &rrvp, NULL) == 0)) {
727 726 VN_HOLD(rrvp);
728 727 VN_RELE(rvp);
729 728 rvp = rrvp;
730 729 }
731 730 } else
732 731 error = EROFS; /* no parent, no entry */
733 732
734 733 /*
735 734 * All we want is the permissions (and maybe ACLs and
736 735 * extended attributes), and we want to perform lookups
737 736 * by name. Drivers occasionally change their minor
738 737 * number space. If something changes, there's no
739 738 * much we can do about it here.
740 739 */
741 740
742 741 /* The shadow node checks out. We are done */
743 742 if (error == 0) {
744 743 dv->dv_attrvp = rvp; /* with one hold */
745 744
746 745 /*
747 746 * Determine if we have non-trivial ACLs on this node.
748 747 * It is not necessary to VOP_RWLOCK since fs_acl_nontrivial
749 748 * only does VOP_GETSECATTR.
750 749 */
751 750 dv->dv_flags &= ~DV_ACL;
752 751
753 752 if (fs_acl_nontrivial(rvp, cred))
754 753 dv->dv_flags |= DV_ACL;
755 754
756 755 /*
757 756 * If we have synced out the memory attributes, free
758 757 * them and switch back to using the persistent store.
759 758 */
760 759 if (rvp && dv->dv_attr) {
761 760 kmem_free(dv->dv_attr, sizeof (struct vattr));
762 761 dv->dv_attr = NULL;
763 762 }
764 763 if ((flags & DV_SHADOW_WRITE_HELD) == 0)
765 764 rw_downgrade(&dv->dv_contents);
766 765 ASSERT(RW_LOCK_HELD(&dv->dv_contents));
767 766 return;
768 767 }
769 768
770 769 /*
771 770 * Failed to find attribute in persistent backing store,
772 771 * get default permission bits.
773 772 */
774 773 devfs_get_defattr(vp, &vattr, NULL);
775 774
776 775 dv_vattr_merge(dv, &vattr);
777 776 gethrestime(&vattr.va_atime);
778 777 vattr.va_mtime = vattr.va_atime;
779 778 vattr.va_ctime = vattr.va_atime;
780 779
781 780 /*
782 781 * Try to create shadow dir. This is necessary in case
783 782 * we need to create a shadow leaf node later, when user
784 783 * executes chmod.
785 784 */
786 785 if ((error == ENOENT) && !create_tried) {
787 786 switch (vp->v_type) {
788 787 case VDIR:
789 788 error = VOP_MKDIR(rdvp, nm, &vattr, &rvp, kcred,
790 789 NULL, 0, NULL);
791 790 dsysdebug(error, ("vop_mkdir %s %s %d\n",
792 791 VTODV(dvp)->dv_name, nm, error));
793 792 create_tried = 1;
794 793 break;
795 794
796 795 case VCHR:
797 796 case VBLK:
798 797 /*
799 798 * Shadow nodes are only created on demand
800 799 */
801 800 if (flags & DV_SHADOW_CREATE) {
802 801 error = VOP_CREATE(rdvp, nm, &vattr, NONEXCL,
803 802 VREAD|VWRITE, &rvp, kcred, 0, NULL, NULL);
804 803 dsysdebug(error, ("vop_create %s %s %d\n",
805 804 VTODV(dvp)->dv_name, nm, error));
806 805 create_tried = 1;
807 806 }
808 807 break;
809 808
810 809 default:
811 810 cmn_err(CE_PANIC, "devfs: %s: create", dvnm);
812 811 /*NOTREACHED*/
813 812 }
814 813
815 814 if (create_tried &&
816 815 (error == 0) || (error == EEXIST)) {
817 816 VN_RELE(rvp);
818 817 goto lookup;
819 818 }
820 819 }
821 820
822 821 /* Store attribute in memory */
823 822 if (dv->dv_attr == NULL) {
824 823 dv->dv_attr = kmem_alloc(sizeof (struct vattr), KM_SLEEP);
825 824 *(dv->dv_attr) = vattr;
826 825 }
827 826
828 827 if ((flags & DV_SHADOW_WRITE_HELD) == 0)
829 828 rw_downgrade(&dv->dv_contents);
830 829 ASSERT(RW_LOCK_HELD(&dv->dv_contents));
831 830 }
832 831
833 832 /*
834 833 * Given a devinfo node, and a name, returns the appropriate
835 834 * minor information for that named node, if it exists.
836 835 */
837 836 static int
838 837 dv_find_leafnode(dev_info_t *devi, char *minor_nm, struct ddi_minor_data *r_mi)
839 838 {
840 839 struct ddi_minor_data *dmd;
841 840
842 841 ASSERT(i_ddi_devi_attached(devi));
843 842
844 843 dcmn_err3(("dv_find_leafnode: %s\n", minor_nm));
845 844 ASSERT(DEVI_BUSY_OWNED(devi));
846 845 for (dmd = DEVI(devi)->devi_minor; dmd; dmd = dmd->next) {
847 846
848 847 /*
849 848 * Skip alias nodes and nodes without a name.
850 849 */
851 850 if ((dmd->type == DDM_ALIAS) || (dmd->ddm_name == NULL))
852 851 continue;
853 852
854 853 dcmn_err4(("dv_find_leafnode: (%s,%s)\n",
855 854 minor_nm, dmd->ddm_name));
856 855 if (strcmp(minor_nm, dmd->ddm_name) == 0) {
857 856 r_mi->ddm_dev = dmd->ddm_dev;
858 857 r_mi->ddm_spec_type = dmd->ddm_spec_type;
859 858 r_mi->type = dmd->type;
860 859 r_mi->ddm_flags = dmd->ddm_flags;
861 860 r_mi->ddm_node_priv = dmd->ddm_node_priv;
862 861 r_mi->ddm_priv_mode = dmd->ddm_priv_mode;
863 862 if (r_mi->ddm_node_priv)
864 863 dphold(r_mi->ddm_node_priv);
865 864 return (0);
866 865 }
867 866 }
868 867
869 868 dcmn_err3(("dv_find_leafnode: %s: ENOENT\n", minor_nm));
870 869 return (ENOENT);
871 870 }
872 871
873 872 /*
874 873 * Special handling for clone node:
875 874 * Clone minor name is a driver name, the minor number will
876 875 * be the major number of the driver. There is no minor
877 876 * node under the clone driver, so we'll manufacture the
878 877 * dev_t.
879 878 */
880 879 static struct dv_node *
881 880 dv_clone_mknod(struct dv_node *ddv, char *drvname)
882 881 {
883 882 major_t major;
884 883 struct dv_node *dvp;
885 884 char *devnm;
886 885 struct ddi_minor_data *dmd;
887 886
888 887 /*
889 888 * Make sure drvname is a STREAMS driver. We load the driver,
890 889 * but don't attach to any instances. This makes stat(2)
891 890 * relatively cheap.
892 891 */
893 892 major = ddi_name_to_major(drvname);
894 893 if (major == DDI_MAJOR_T_NONE)
895 894 return (NULL);
896 895
897 896 if (ddi_hold_driver(major) == NULL)
898 897 return (NULL);
899 898
900 899 if (STREAMSTAB(major) == NULL) {
901 900 ddi_rele_driver(major);
902 901 return (NULL);
903 902 }
904 903
905 904 ddi_rele_driver(major);
906 905 devnm = kmem_alloc(MAXNAMELEN, KM_SLEEP);
907 906 (void) snprintf(devnm, MAXNAMELEN, "clone@0:%s", drvname);
908 907 dmd = kmem_zalloc(sizeof (*dmd), KM_SLEEP);
909 908 dmd->ddm_dev = makedevice(clone_major, (minor_t)major);
910 909 dmd->ddm_spec_type = S_IFCHR;
911 910 dvp = dv_mknod(ddv, clone_dip, devnm, dmd);
912 911 kmem_free(dmd, sizeof (*dmd));
913 912 kmem_free(devnm, MAXNAMELEN);
914 913 return (dvp);
|
↓ open down ↓ |
492 lines elided |
↑ open up ↑ |
915 914 }
916 915
917 916 /*
918 917 * Given the parent directory node, and a name in it, returns the
919 918 * named dv_node to the caller (as a vnode).
920 919 *
921 920 * (We need pnp and rdir for doing shadow lookups; they can be NULL)
922 921 */
923 922 int
924 923 dv_find(struct dv_node *ddv, char *nm, struct vnode **vpp, struct pathname *pnp,
925 - struct vnode *rdir, struct cred *cred, uint_t ndi_flags)
924 + struct vnode *rdir, struct cred *cred, uint_t ndi_flags)
926 925 {
927 926 extern int isminiroot; /* see modctl.c */
928 927
929 928 int circ;
930 929 int rv = 0, was_busy = 0, nmlen, write_held = 0;
931 930 struct vnode *vp;
932 931 struct dv_node *dv, *dup;
933 932 dev_info_t *pdevi, *devi = NULL;
934 933 char *mnm;
935 934 struct ddi_minor_data *dmd;
936 935
937 936 dcmn_err3(("dv_find %s\n", nm));
938 937
939 938 if (!rw_tryenter(&ddv->dv_contents, RW_READER)) {
940 939 if (tsd_get(devfs_clean_key))
941 940 return (EBUSY);
942 941 rw_enter(&ddv->dv_contents, RW_READER);
943 942 }
944 943 start:
945 944 if (DV_STALE(ddv)) {
946 945 rw_exit(&ddv->dv_contents);
947 946 return (ESTALE);
948 947 }
949 948
950 949 /*
951 950 * Empty name or ., return node itself.
952 951 */
953 952 nmlen = strlen(nm);
954 953 if ((nmlen == 0) || ((nmlen == 1) && (nm[0] == '.'))) {
955 954 *vpp = DVTOV(ddv);
956 955 rw_exit(&ddv->dv_contents);
957 956 VN_HOLD(*vpp);
958 957 return (0);
959 958 }
960 959
961 960 /*
962 961 * .., return the parent directory
963 962 */
964 963 if ((nmlen == 2) && (strcmp(nm, "..") == 0)) {
965 964 *vpp = DVTOV(ddv->dv_dotdot);
966 965 rw_exit(&ddv->dv_contents);
967 966 VN_HOLD(*vpp);
968 967 return (0);
969 968 }
970 969
971 970 /*
972 971 * Fail anything without a valid device name component
973 972 */
974 973 if (nm[0] == '@' || nm[0] == ':') {
975 974 dcmn_err3(("devfs: no driver '%s'\n", nm));
976 975 rw_exit(&ddv->dv_contents);
977 976 return (ENOENT);
978 977 }
979 978
980 979 /*
981 980 * So, now we have to deal with the trickier stuff.
982 981 *
983 982 * (a) search the existing list of dv_nodes on this directory
984 983 */
985 984 if ((dv = dv_findbyname(ddv, nm)) != NULL) {
986 985 founddv:
987 986 ASSERT(RW_LOCK_HELD(&ddv->dv_contents));
988 987
989 988 if (!rw_tryenter(&dv->dv_contents, RW_READER)) {
990 989 if (tsd_get(devfs_clean_key)) {
991 990 VN_RELE(DVTOV(dv));
992 991 rw_exit(&ddv->dv_contents);
993 992 return (EBUSY);
994 993 }
995 994 rw_enter(&dv->dv_contents, RW_READER);
996 995 }
997 996
998 997 vp = DVTOV(dv);
999 998 if ((dv->dv_attrvp != NULLVP) ||
1000 999 (vp->v_type != VDIR && dv->dv_attr != NULL)) {
1001 1000 /*
1002 1001 * Common case - we already have attributes
1003 1002 */
1004 1003 rw_exit(&dv->dv_contents);
1005 1004 rw_exit(&ddv->dv_contents);
1006 1005 goto found;
1007 1006 }
1008 1007
1009 1008 /*
1010 1009 * No attribute vp, try and build one.
1011 1010 *
1012 1011 * dv_shadow_node() can briefly drop &dv->dv_contents lock
1013 1012 * if it is unable to upgrade it to a write lock. If the
1014 1013 * current thread has come in through the bottom-up device
1015 1014 * configuration devfs_clean() path, we may deadlock against
1016 1015 * a thread performing top-down device configuration if it
1017 1016 * grabs the contents lock. To avoid this, when we are on the
1018 1017 * devfs_clean() path we attempt to upgrade the dv_contents
1019 1018 * lock before we call dv_shadow_node().
1020 1019 */
1021 1020 if (tsd_get(devfs_clean_key)) {
1022 1021 if (!rw_tryupgrade(&dv->dv_contents)) {
1023 1022 VN_RELE(DVTOV(dv));
1024 1023 rw_exit(&dv->dv_contents);
1025 1024 rw_exit(&ddv->dv_contents);
1026 1025 return (EBUSY);
1027 1026 }
1028 1027
1029 1028 write_held = DV_SHADOW_WRITE_HELD;
1030 1029 }
1031 1030
1032 1031 dv_shadow_node(DVTOV(ddv), nm, vp, pnp, rdir, cred,
1033 1032 write_held);
1034 1033
1035 1034 rw_exit(&dv->dv_contents);
1036 1035 rw_exit(&ddv->dv_contents);
1037 1036 goto found;
1038 1037 }
1039 1038
1040 1039 /*
1041 1040 * (b) Search the child devinfo nodes of our parent directory,
1042 1041 * looking for the named node. If we find it, build a new
1043 1042 * node, then grab the writers lock, search the directory
1044 1043 * if it's still not there, then insert it.
1045 1044 *
1046 1045 * We drop the devfs locks before accessing the device tree.
1047 1046 * Take care to mark the node BUSY so that a forced devfs_clean
1048 1047 * doesn't mark the directory node stale.
1049 1048 *
1050 1049 * Also, check if we are called as part of devfs_clean or
1051 1050 * reset_perm. If so, simply return not found because there
1052 1051 * is nothing to clean.
1053 1052 */
1054 1053 if (tsd_get(devfs_clean_key)) {
1055 1054 rw_exit(&ddv->dv_contents);
1056 1055 return (ENOENT);
1057 1056 }
1058 1057
1059 1058 /*
1060 1059 * We could be either READ or WRITE locked at
1061 1060 * this point. Upgrade if we are read locked.
1062 1061 */
1063 1062 ASSERT(RW_LOCK_HELD(&ddv->dv_contents));
1064 1063 if (rw_read_locked(&ddv->dv_contents) &&
1065 1064 !rw_tryupgrade(&ddv->dv_contents)) {
1066 1065 rw_exit(&ddv->dv_contents);
1067 1066 rw_enter(&ddv->dv_contents, RW_WRITER);
1068 1067 /*
1069 1068 * Things may have changed when we dropped
1070 1069 * the contents lock, so start from top again
1071 1070 */
1072 1071 goto start;
1073 1072 }
1074 1073 ddv->dv_busy++; /* mark busy before dropping lock */
1075 1074 was_busy++;
1076 1075 rw_exit(&ddv->dv_contents);
1077 1076
1078 1077 pdevi = ddv->dv_devi;
1079 1078 ASSERT(pdevi != NULL);
1080 1079
1081 1080 mnm = strchr(nm, ':');
1082 1081 if (mnm)
1083 1082 *mnm = (char)0;
1084 1083
1085 1084 /*
1086 1085 * Configure one nexus child, will call nexus's bus_ops
1087 1086 * If successful, devi is held upon returning.
1088 1087 * Note: devfs lookup should not be configuring grandchildren.
1089 1088 */
1090 1089 ASSERT((ndi_flags & NDI_CONFIG) == 0);
1091 1090
1092 1091 rv = ndi_devi_config_one(pdevi, nm, &devi, ndi_flags | NDI_NO_EVENT);
1093 1092 if (mnm)
1094 1093 *mnm = ':';
1095 1094 if (rv != NDI_SUCCESS) {
1096 1095 rv = ENOENT;
1097 1096 goto notfound;
1098 1097 }
1099 1098
1100 1099 ASSERT(devi);
1101 1100
1102 1101 /* Check if this is a path alias */
1103 1102 if (ddi_aliases_present == B_TRUE && ddi_get_parent(devi) != pdevi) {
1104 1103 char *curr = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1105 1104
1106 1105 (void) ddi_pathname(devi, curr);
1107 1106
1108 1107 vp = NULL;
1109 1108 if (devfs_lookupname(curr, NULL, &vp) == 0 && vp) {
1110 1109 dv = VTODV(vp);
1111 1110 kmem_free(curr, MAXPATHLEN);
1112 1111 goto found;
1113 1112 }
1114 1113 kmem_free(curr, MAXPATHLEN);
1115 1114 }
1116 1115
1117 1116 /*
1118 1117 * If we configured a hidden node, consider it notfound.
1119 1118 */
1120 1119 if (ndi_dev_is_hidden_node(devi)) {
1121 1120 ndi_rele_devi(devi);
1122 1121 rv = ENOENT;
1123 1122 goto notfound;
1124 1123 }
1125 1124
1126 1125 /*
1127 1126 * Don't make vhci clients visible under phci, unless we
1128 1127 * are in miniroot.
1129 1128 */
1130 1129 if (isminiroot == 0 && ddi_get_parent(devi) != pdevi) {
1131 1130 ndi_rele_devi(devi);
1132 1131 rv = ENOENT;
1133 1132 goto notfound;
1134 1133 }
1135 1134
1136 1135 ASSERT(devi && i_ddi_devi_attached(devi));
1137 1136
1138 1137 /*
1139 1138 * Invalidate cache to notice newly created minor nodes.
1140 1139 */
1141 1140 rw_enter(&ddv->dv_contents, RW_WRITER);
1142 1141 ddv->dv_flags |= DV_BUILD;
1143 1142 rw_exit(&ddv->dv_contents);
1144 1143
1145 1144 /*
1146 1145 * mkdir for nexus drivers and leaf nodes as well. If we are racing
1147 1146 * and create a duplicate, the duplicate will be destroyed below.
1148 1147 */
1149 1148 if (mnm == NULL) {
1150 1149 dv = dv_mkdir(ddv, devi, nm);
1151 1150 } else {
1152 1151 /*
1153 1152 * Allocate dmd first to avoid KM_SLEEP with active
1154 1153 * ndi_devi_enter.
1155 1154 */
1156 1155 dmd = kmem_zalloc(sizeof (*dmd), KM_SLEEP);
1157 1156 ndi_devi_enter(devi, &circ);
1158 1157 if (devi == clone_dip) {
1159 1158 /*
1160 1159 * For clone minors, load the driver indicated by
1161 1160 * minor name.
1162 1161 */
1163 1162 dv = dv_clone_mknod(ddv, mnm + 1);
1164 1163 } else {
1165 1164 /*
1166 1165 * Find minor node and make a dv_node
1167 1166 */
1168 1167 if (dv_find_leafnode(devi, mnm + 1, dmd) == 0) {
1169 1168 dv = dv_mknod(ddv, devi, nm, dmd);
1170 1169 if (dmd->ddm_node_priv)
1171 1170 dpfree(dmd->ddm_node_priv);
1172 1171 }
1173 1172 }
1174 1173 ndi_devi_exit(devi, circ);
1175 1174 kmem_free(dmd, sizeof (*dmd));
1176 1175 }
1177 1176 /*
1178 1177 * Release hold from ndi_devi_config_one()
1179 1178 */
1180 1179 ndi_rele_devi(devi);
1181 1180
1182 1181 if (dv == NULL) {
1183 1182 rv = ENOENT;
1184 1183 goto notfound;
1185 1184 }
1186 1185
1187 1186 /*
1188 1187 * We have released the dv_contents lock, need to check
1189 1188 * if another thread already created a duplicate node
1190 1189 */
1191 1190 rw_enter(&ddv->dv_contents, RW_WRITER);
1192 1191 if ((dup = dv_findbyname(ddv, nm)) == NULL) {
1193 1192 dv_insert(ddv, dv);
1194 1193 } else {
1195 1194 /*
1196 1195 * Duplicate found, use the existing node
1197 1196 */
1198 1197 VN_RELE(DVTOV(dv));
1199 1198 dv_destroy(dv, 0);
1200 1199 dv = dup;
1201 1200 }
1202 1201 goto founddv;
1203 1202 /*NOTREACHED*/
1204 1203
1205 1204 found:
1206 1205 /*
1207 1206 * Fail lookup of device that has now become hidden (typically via
1208 1207 * hot removal of open device).
1209 1208 */
1210 1209 if (dv->dv_devi && ndi_dev_is_hidden_node(dv->dv_devi)) {
1211 1210 dcmn_err2(("dv_find: nm %s failed: hidden/removed\n", nm));
1212 1211 VN_RELE(vp);
1213 1212 rv = ENOENT;
1214 1213 goto notfound;
1215 1214 }
1216 1215
1217 1216 /*
1218 1217 * Skip non-kernel lookups of internal nodes.
1219 1218 * This use of kcred to distinguish between user and
1220 1219 * internal kernel lookups is unfortunate. The information
1221 1220 * provided by the seg argument to lookupnameat should
1222 1221 * evolve into a lookup flag for filesystems that need
1223 1222 * this distinction.
1224 1223 */
1225 1224 if ((dv->dv_flags & DV_INTERNAL) && (cred != kcred)) {
1226 1225 dcmn_err2(("dv_find: nm %s failed: internal\n", nm));
1227 1226 VN_RELE(vp);
1228 1227 rv = ENOENT;
1229 1228 goto notfound;
1230 1229 }
1231 1230
1232 1231 dcmn_err2(("dv_find: returning vp for nm %s\n", nm));
1233 1232 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1234 1233 /*
1235 1234 * If vnode is a device, return special vnode instead
1236 1235 * (though it knows all about -us- via sp->s_realvp,
1237 1236 * sp->s_devvp, and sp->s_dip)
1238 1237 */
1239 1238 *vpp = specvp_devfs(vp, vp->v_rdev, vp->v_type, cred,
1240 1239 dv->dv_devi);
1241 1240 VN_RELE(vp);
1242 1241 if (*vpp == NULLVP)
1243 1242 rv = ENOSYS;
1244 1243 } else
1245 1244 *vpp = vp;
1246 1245
1247 1246 notfound:
1248 1247 if (was_busy) {
1249 1248 /*
1250 1249 * Non-zero was_busy tells us that we are not in the
1251 1250 * devfs_clean() path which in turn means that we can afford
1252 1251 * to take the contents lock unconditionally.
1253 1252 */
1254 1253 rw_enter(&ddv->dv_contents, RW_WRITER);
1255 1254 ddv->dv_busy--;
1256 1255 rw_exit(&ddv->dv_contents);
1257 1256 }
1258 1257 return (rv);
1259 1258 }
1260 1259
1261 1260 /*
1262 1261 * The given directory node is out-of-date; that is, it has been
1263 1262 * marked as needing to be rebuilt, possibly because some new devinfo
1264 1263 * node has come into existence, or possibly because this is the first
1265 1264 * time we've been here.
1266 1265 */
1267 1266 void
1268 1267 dv_filldir(struct dv_node *ddv)
1269 1268 {
1270 1269 struct dv_node *dv;
1271 1270 dev_info_t *devi, *pdevi;
1272 1271 struct ddi_minor_data *dmd;
1273 1272 char devnm[MAXNAMELEN];
1274 1273 int circ, ccirc;
1275 1274
1276 1275 ASSERT(DVTOV(ddv)->v_type == VDIR);
1277 1276 ASSERT(RW_WRITE_HELD(&ddv->dv_contents));
1278 1277 ASSERT(ddv->dv_flags & DV_BUILD);
1279 1278
1280 1279 dcmn_err3(("dv_filldir: %s\n", ddv->dv_name));
1281 1280 if (DV_STALE(ddv))
1282 1281 return;
1283 1282 pdevi = ddv->dv_devi;
1284 1283
1285 1284 if (ndi_devi_config(pdevi, NDI_NO_EVENT) != NDI_SUCCESS) {
1286 1285 dcmn_err3(("dv_filldir: config error %s\n", ddv->dv_name));
1287 1286 }
1288 1287
1289 1288 ndi_devi_enter(pdevi, &circ);
1290 1289 for (devi = ddi_get_child(pdevi); devi;
1291 1290 devi = ddi_get_next_sibling(devi)) {
1292 1291 /*
1293 1292 * While we know enough to create a directory at DS_INITIALIZED,
1294 1293 * the directory will be empty until DS_ATTACHED. The existence
1295 1294 * of an empty directory dv_node will cause a devi_ref, which
1296 1295 * has caused problems for existing code paths doing offline/DR
1297 1296 * type operations - making devfs_clean coordination even more
1298 1297 * sensitive and error prone. Given this, the 'continue' below
1299 1298 * is checking for DS_ATTACHED instead of DS_INITIALIZED.
1300 1299 */
1301 1300 if (i_ddi_node_state(devi) < DS_ATTACHED)
1302 1301 continue;
1303 1302
1304 1303 /* skip hidden nodes */
1305 1304 if (ndi_dev_is_hidden_node(devi))
1306 1305 continue;
1307 1306
1308 1307 dcmn_err3(("dv_filldir: node %s\n", ddi_node_name(devi)));
1309 1308
1310 1309 ndi_devi_enter(devi, &ccirc);
1311 1310 for (dmd = DEVI(devi)->devi_minor; dmd; dmd = dmd->next) {
1312 1311 char *addr;
1313 1312
1314 1313 /*
1315 1314 * Skip alias nodes, internal nodes, and nodes
1316 1315 * without a name. We allow DDM_DEFAULT nodes
1317 1316 * to appear in readdir.
1318 1317 */
1319 1318 if ((dmd->type == DDM_ALIAS) ||
1320 1319 (dmd->type == DDM_INTERNAL_PATH) ||
1321 1320 (dmd->ddm_name == NULL))
1322 1321 continue;
1323 1322
1324 1323 addr = ddi_get_name_addr(devi);
1325 1324 if (addr && *addr)
1326 1325 (void) sprintf(devnm, "%s@%s:%s",
1327 1326 ddi_node_name(devi), addr, dmd->ddm_name);
1328 1327 else
1329 1328 (void) sprintf(devnm, "%s:%s",
1330 1329 ddi_node_name(devi), dmd->ddm_name);
1331 1330
1332 1331 if ((dv = dv_findbyname(ddv, devnm)) != NULL) {
1333 1332 /* dv_node already exists */
1334 1333 VN_RELE(DVTOV(dv));
1335 1334 continue;
1336 1335 }
1337 1336
1338 1337 dv = dv_mknod(ddv, devi, devnm, dmd);
1339 1338 dv_insert(ddv, dv);
1340 1339 VN_RELE(DVTOV(dv));
1341 1340 }
1342 1341 ndi_devi_exit(devi, ccirc);
1343 1342
1344 1343 (void) ddi_deviname(devi, devnm);
1345 1344 if ((dv = dv_findbyname(ddv, devnm + 1)) == NULL) {
1346 1345 /* directory doesn't exist */
1347 1346 dv = dv_mkdir(ddv, devi, devnm + 1);
1348 1347 dv_insert(ddv, dv);
1349 1348 }
1350 1349 VN_RELE(DVTOV(dv));
1351 1350 }
1352 1351 ndi_devi_exit(pdevi, circ);
1353 1352
1354 1353 ddv->dv_flags &= ~DV_BUILD;
1355 1354 }
1356 1355
1357 1356 /*
1358 1357 * Given a directory node, clean out all the nodes beneath.
1359 1358 *
1360 1359 * VDIR: Reinvoke to clean them, then delete the directory.
1361 1360 * VCHR, VBLK: Just blow them away.
1362 1361 *
1363 1362 * Mark the directories touched as in need of a rebuild, in case
1364 1363 * we fall over part way through. When DV_CLEAN_FORCE is specified,
1365 1364 * we mark referenced empty directories as stale to facilitate DR.
1366 1365 */
1367 1366 int
1368 1367 dv_cleandir(struct dv_node *ddv, char *devnm, uint_t flags)
1369 1368 {
1370 1369 struct dv_node *dv;
1371 1370 struct dv_node *next;
1372 1371 struct vnode *vp;
1373 1372 int busy = 0;
1374 1373
1375 1374 /*
1376 1375 * We should always be holding the tsd_clean_key here: dv_cleandir()
1377 1376 * will be called as a result of a devfs_clean request and the
1378 1377 * tsd_clean_key will be set in either in devfs_clean() itself or in
1379 1378 * devfs_clean_vhci().
1380 1379 *
1381 1380 * Since we are on the devfs_clean path, we return EBUSY if we cannot
1382 1381 * get the contents lock: if we blocked here we might deadlock against
1383 1382 * a thread performing top-down device configuration.
1384 1383 */
1385 1384 ASSERT(tsd_get(devfs_clean_key));
1386 1385
1387 1386 dcmn_err3(("dv_cleandir: %s\n", ddv->dv_name));
1388 1387
1389 1388 if (!(flags & DV_CLEANDIR_LCK) &&
1390 1389 !rw_tryenter(&ddv->dv_contents, RW_WRITER))
1391 1390 return (EBUSY);
1392 1391
1393 1392 for (dv = DV_FIRST_ENTRY(ddv); dv; dv = next) {
1394 1393 next = DV_NEXT_ENTRY(ddv, dv);
1395 1394
1396 1395 /*
1397 1396 * If devnm is specified, the non-minor portion of the
1398 1397 * name must match devnm.
1399 1398 */
1400 1399 if (devnm &&
1401 1400 (strncmp(devnm, dv->dv_name, strlen(devnm)) ||
1402 1401 (dv->dv_name[strlen(devnm)] != ':' &&
1403 1402 dv->dv_name[strlen(devnm)] != '\0')))
1404 1403 continue;
1405 1404
1406 1405 /* check type of what we are cleaning */
1407 1406 vp = DVTOV(dv);
1408 1407 if (vp->v_type == VDIR) {
1409 1408 /* recurse on directories */
1410 1409 rw_enter(&dv->dv_contents, RW_WRITER);
1411 1410 if (dv_cleandir(dv, NULL,
1412 1411 flags | DV_CLEANDIR_LCK) == EBUSY) {
1413 1412 rw_exit(&dv->dv_contents);
1414 1413 goto set_busy;
1415 1414 }
1416 1415
1417 1416 /* A clean directory is an empty directory... */
1418 1417 ASSERT(dv->dv_nlink == 2);
1419 1418 mutex_enter(&vp->v_lock);
1420 1419 if (vp->v_count > 0) {
1421 1420 /*
1422 1421 * ... but an empty directory can still have
1423 1422 * references to it. If we have dv_busy or
1424 1423 * DV_CLEAN_FORCE is *not* specified then a
1425 1424 * referenced directory is considered busy.
1426 1425 */
1427 1426 if (dv->dv_busy || !(flags & DV_CLEAN_FORCE)) {
1428 1427 mutex_exit(&vp->v_lock);
1429 1428 rw_exit(&dv->dv_contents);
1430 1429 goto set_busy;
1431 1430 }
1432 1431
1433 1432 /*
1434 1433 * Mark referenced directory stale so that DR
1435 1434 * will succeed even if a shell has
1436 1435 * /devices/xxx as current directory (causing
1437 1436 * VN_HOLD reference to an empty directory).
|
↓ open down ↓ |
502 lines elided |
↑ open up ↑ |
1438 1437 */
1439 1438 ASSERT(!DV_STALE(dv));
1440 1439 ndi_rele_devi(dv->dv_devi);
1441 1440 dv->dv_devi = NULL; /* mark DV_STALE */
1442 1441 }
1443 1442 } else {
1444 1443 ASSERT((vp->v_type == VCHR) || (vp->v_type == VBLK));
1445 1444 ASSERT(dv->dv_nlink == 1); /* no hard links */
1446 1445 mutex_enter(&vp->v_lock);
1447 1446 if (vp->v_count > 0) {
1448 - mutex_exit(&vp->v_lock);
1449 - goto set_busy;
1447 + /*
1448 + * The file still has references to it. If
1449 + * DEVI_GONE is *not* set on the devi referenced
1450 + * file is considered busy.
1451 + */
1452 + if (!DEVI_IS_GONE(dv->dv_devi)) {
1453 + mutex_exit(&vp->v_lock);
1454 + goto set_busy;
1455 + }
1456 +
1457 + /*
1458 + * Mark referenced file stale so that DR will
1459 + * succeed even if there are userland opens.
1460 + */
1461 + ASSERT(!DV_STALE(dv));
1462 + ndi_rele_devi(dv->dv_devi);
1463 + dv->dv_devi = NULL;
1450 1464 }
1451 1465 }
1452 1466
1453 1467 /* unlink from directory */
1454 1468 dv_unlink(ddv, dv);
1455 1469
1456 1470 /* drop locks */
1457 1471 mutex_exit(&vp->v_lock);
1458 1472 if (vp->v_type == VDIR)
1459 1473 rw_exit(&dv->dv_contents);
1460 1474
1461 1475 /* destroy vnode if ref count is zero */
1462 1476 if (vp->v_count == 0)
1463 1477 dv_destroy(dv, flags);
1464 1478
1465 1479 continue;
1466 1480
1467 1481 /*
1468 1482 * If devnm is not NULL we return immediately on busy,
1469 1483 * otherwise we continue destroying unused dv_node's.
1470 1484 */
1471 1485 set_busy: busy++;
1472 1486 if (devnm)
1473 1487 break;
1474 1488 }
1475 1489
1476 1490 /*
1477 1491 * This code may be invoked to inform devfs that a new node has
1478 1492 * been created in the kernel device tree. So we always set
1479 1493 * the DV_BUILD flag to allow the next dv_filldir() to pick
1480 1494 * the new devinfo nodes.
1481 1495 */
1482 1496 ddv->dv_flags |= DV_BUILD;
1483 1497
1484 1498 if (!(flags & DV_CLEANDIR_LCK))
1485 1499 rw_exit(&ddv->dv_contents);
1486 1500
1487 1501 return (busy ? EBUSY : 0);
1488 1502 }
1489 1503
1490 1504 /*
1491 1505 * Walk through the devfs hierarchy, correcting the permissions of
1492 1506 * devices with default permissions that do not match those specified
1493 1507 * by minor perm. This can only be done for all drivers for now.
1494 1508 */
1495 1509 static int
1496 1510 dv_reset_perm_dir(struct dv_node *ddv, uint_t flags)
1497 1511 {
1498 1512 struct dv_node *dv;
1499 1513 struct vnode *vp;
1500 1514 int retval = 0;
1501 1515 struct vattr *attrp;
1502 1516 mperm_t mp;
1503 1517 char *nm;
1504 1518 uid_t old_uid;
1505 1519 gid_t old_gid;
1506 1520 mode_t old_mode;
1507 1521
1508 1522 rw_enter(&ddv->dv_contents, RW_WRITER);
1509 1523 for (dv = DV_FIRST_ENTRY(ddv); dv; dv = DV_NEXT_ENTRY(ddv, dv)) {
1510 1524 int error = 0;
1511 1525 nm = dv->dv_name;
1512 1526
1513 1527 rw_enter(&dv->dv_contents, RW_READER);
1514 1528 vp = DVTOV(dv);
1515 1529 if (vp->v_type == VDIR) {
1516 1530 rw_exit(&dv->dv_contents);
1517 1531 if (dv_reset_perm_dir(dv, flags) != 0) {
1518 1532 error = EBUSY;
1519 1533 }
1520 1534 } else {
1521 1535 ASSERT(vp->v_type == VCHR || vp->v_type == VBLK);
1522 1536
1523 1537 /*
1524 1538 * Check for permissions from minor_perm
1525 1539 * If there are none, we're done
1526 1540 */
1527 1541 rw_exit(&dv->dv_contents);
1528 1542 if (dev_minorperm(dv->dv_devi, nm, &mp) != 0)
1529 1543 continue;
1530 1544
1531 1545 rw_enter(&dv->dv_contents, RW_READER);
1532 1546
1533 1547 /*
1534 1548 * Allow a node's permissions to be altered
1535 1549 * permanently from the defaults by chmod,
1536 1550 * using the shadow node as backing store.
1537 1551 * Otherwise, update node to minor_perm permissions.
1538 1552 */
1539 1553 if (dv->dv_attrvp == NULLVP) {
1540 1554 /*
1541 1555 * No attribute vp, try to find one.
1542 1556 */
1543 1557 dv_shadow_node(DVTOV(ddv), nm, vp,
1544 1558 NULL, NULLVP, kcred, 0);
1545 1559 }
1546 1560 if (dv->dv_attrvp != NULLVP || dv->dv_attr == NULL) {
1547 1561 rw_exit(&dv->dv_contents);
1548 1562 continue;
1549 1563 }
1550 1564
1551 1565 attrp = dv->dv_attr;
1552 1566
1553 1567 if (VATTRP_MP_CMP(attrp, mp) == 0) {
1554 1568 dcmn_err5(("%s: no perm change: "
1555 1569 "%d %d 0%o\n", nm, attrp->va_uid,
1556 1570 attrp->va_gid, attrp->va_mode));
1557 1571 rw_exit(&dv->dv_contents);
1558 1572 continue;
1559 1573 }
1560 1574
1561 1575 old_uid = attrp->va_uid;
1562 1576 old_gid = attrp->va_gid;
1563 1577 old_mode = attrp->va_mode;
1564 1578
1565 1579 VATTRP_MP_MERGE(attrp, mp);
1566 1580 mutex_enter(&vp->v_lock);
1567 1581 if (vp->v_count > 0) {
1568 1582 error = EBUSY;
1569 1583 }
1570 1584 mutex_exit(&vp->v_lock);
1571 1585
1572 1586 dcmn_err5(("%s: perm %d/%d/0%o -> %d/%d/0%o (%d)\n",
1573 1587 nm, old_uid, old_gid, old_mode, attrp->va_uid,
1574 1588 attrp->va_gid, attrp->va_mode, error));
1575 1589
1576 1590 rw_exit(&dv->dv_contents);
1577 1591 }
1578 1592
1579 1593 if (error != 0) {
1580 1594 retval = error;
1581 1595 }
1582 1596 }
1583 1597
1584 1598 ddv->dv_flags |= DV_BUILD;
1585 1599
1586 1600 rw_exit(&ddv->dv_contents);
1587 1601
1588 1602 return (retval);
1589 1603 }
1590 1604
1591 1605 int
1592 1606 devfs_reset_perm(uint_t flags)
1593 1607 {
1594 1608 struct dv_node *dvp;
1595 1609 int rval;
1596 1610
1597 1611 if ((dvp = devfs_dip_to_dvnode(ddi_root_node())) == NULL)
1598 1612 return (0);
1599 1613
1600 1614 VN_HOLD(DVTOV(dvp));
1601 1615 rval = dv_reset_perm_dir(dvp, flags);
1602 1616 VN_RELE(DVTOV(dvp));
1603 1617 return (rval);
1604 1618 }
1605 1619
1606 1620 /*
1607 1621 * Clean up dangling devfs shadow nodes for removed
1608 1622 * drivers so that, in the event the driver is re-added
1609 1623 * to the system, newly created nodes won't incorrectly
1610 1624 * pick up these stale shadow node permissions.
1611 1625 *
1612 1626 * This is accomplished by walking down the pathname
1613 1627 * to the directory, starting at the root's attribute
1614 1628 * node, then removing all minors matching the specified
1615 1629 * node name. Care must be taken to remove all entries
1616 1630 * in a directory before the directory itself, so that
1617 1631 * the clean-up associated with rem_drv'ing a nexus driver
1618 1632 * does not inadvertently result in an inconsistent
1619 1633 * filesystem underlying devfs.
1620 1634 */
1621 1635
1622 1636 static int
1623 1637 devfs_remdrv_rmdir(vnode_t *dirvp, const char *dir, vnode_t *rvp)
1624 1638 {
1625 1639 int error;
1626 1640 vnode_t *vp;
1627 1641 int eof;
1628 1642 struct iovec iov;
1629 1643 struct uio uio;
1630 1644 struct dirent64 *dp;
1631 1645 dirent64_t *dbuf;
1632 1646 size_t dlen;
1633 1647 size_t dbuflen;
1634 1648 int ndirents = 64;
1635 1649 char *nm;
1636 1650
1637 1651 VN_HOLD(dirvp);
1638 1652
1639 1653 dlen = ndirents * (sizeof (*dbuf));
1640 1654 dbuf = kmem_alloc(dlen, KM_SLEEP);
1641 1655
1642 1656 uio.uio_iov = &iov;
1643 1657 uio.uio_iovcnt = 1;
1644 1658 uio.uio_segflg = UIO_SYSSPACE;
1645 1659 uio.uio_fmode = 0;
1646 1660 uio.uio_extflg = UIO_COPY_CACHED;
1647 1661 uio.uio_loffset = 0;
1648 1662 uio.uio_llimit = MAXOFFSET_T;
1649 1663
1650 1664 eof = 0;
1651 1665 error = 0;
1652 1666 while (!error && !eof) {
1653 1667 uio.uio_resid = dlen;
1654 1668 iov.iov_base = (char *)dbuf;
1655 1669 iov.iov_len = dlen;
1656 1670
1657 1671 (void) VOP_RWLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
1658 1672 error = VOP_READDIR(dirvp, &uio, kcred, &eof, NULL, 0);
1659 1673 VOP_RWUNLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
1660 1674
1661 1675 dbuflen = dlen - uio.uio_resid;
1662 1676
1663 1677 if (error || dbuflen == 0)
1664 1678 break;
1665 1679
1666 1680 for (dp = dbuf; ((intptr_t)dp < (intptr_t)dbuf + dbuflen);
1667 1681 dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen)) {
1668 1682
1669 1683 nm = dp->d_name;
1670 1684
1671 1685 if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0)
1672 1686 continue;
1673 1687
1674 1688 error = VOP_LOOKUP(dirvp, nm,
1675 1689 &vp, NULL, 0, NULL, kcred, NULL, NULL, NULL);
1676 1690
1677 1691 dsysdebug(error,
1678 1692 ("rem_drv %s/%s lookup (%d)\n",
1679 1693 dir, nm, error));
1680 1694
1681 1695 if (error)
1682 1696 continue;
1683 1697
1684 1698 ASSERT(vp->v_type == VDIR ||
1685 1699 vp->v_type == VCHR || vp->v_type == VBLK);
1686 1700
1687 1701 if (vp->v_type == VDIR) {
1688 1702 error = devfs_remdrv_rmdir(vp, nm, rvp);
1689 1703 if (error == 0) {
1690 1704 error = VOP_RMDIR(dirvp,
1691 1705 (char *)nm, rvp, kcred, NULL, 0);
1692 1706 dsysdebug(error,
1693 1707 ("rem_drv %s/%s rmdir (%d)\n",
1694 1708 dir, nm, error));
1695 1709 }
1696 1710 } else {
1697 1711 error = VOP_REMOVE(dirvp, (char *)nm, kcred,
1698 1712 NULL, 0);
1699 1713 dsysdebug(error,
1700 1714 ("rem_drv %s/%s remove (%d)\n",
1701 1715 dir, nm, error));
1702 1716 }
1703 1717
1704 1718 VN_RELE(vp);
1705 1719 if (error) {
1706 1720 goto exit;
1707 1721 }
1708 1722 }
1709 1723 }
1710 1724
1711 1725 exit:
1712 1726 VN_RELE(dirvp);
1713 1727 kmem_free(dbuf, dlen);
1714 1728
1715 1729 return (error);
1716 1730 }
1717 1731
1718 1732 int
1719 1733 devfs_remdrv_cleanup(const char *dir, const char *nodename)
1720 1734 {
1721 1735 int error;
1722 1736 vnode_t *vp;
1723 1737 vnode_t *dirvp;
1724 1738 int eof;
1725 1739 struct iovec iov;
1726 1740 struct uio uio;
1727 1741 struct dirent64 *dp;
1728 1742 dirent64_t *dbuf;
1729 1743 size_t dlen;
1730 1744 size_t dbuflen;
1731 1745 int ndirents = 64;
1732 1746 int nodenamelen = strlen(nodename);
1733 1747 char *nm;
1734 1748 struct pathname pn;
1735 1749 vnode_t *rvp; /* root node of the underlying attribute fs */
1736 1750
1737 1751 dcmn_err5(("devfs_remdrv_cleanup: %s %s\n", dir, nodename));
1738 1752
1739 1753 if (error = pn_get((char *)dir, UIO_SYSSPACE, &pn))
1740 1754 return (0);
1741 1755
1742 1756 rvp = dvroot->dv_attrvp;
1743 1757 ASSERT(rvp != NULL);
1744 1758 VN_HOLD(rvp);
1745 1759
1746 1760 pn_skipslash(&pn);
1747 1761 dirvp = rvp;
1748 1762 VN_HOLD(dirvp);
1749 1763
1750 1764 nm = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1751 1765
1752 1766 while (pn_pathleft(&pn)) {
1753 1767 ASSERT(dirvp->v_type == VDIR);
1754 1768 (void) pn_getcomponent(&pn, nm);
1755 1769 ASSERT((strcmp(nm, ".") != 0) && (strcmp(nm, "..") != 0));
1756 1770 error = VOP_LOOKUP(dirvp, nm, &vp, NULL, 0, rvp, kcred,
1757 1771 NULL, NULL, NULL);
1758 1772 if (error) {
1759 1773 dcmn_err5(("remdrv_cleanup %s lookup error %d\n",
1760 1774 nm, error));
1761 1775 VN_RELE(dirvp);
1762 1776 if (dirvp != rvp)
1763 1777 VN_RELE(rvp);
1764 1778 pn_free(&pn);
1765 1779 kmem_free(nm, MAXNAMELEN);
1766 1780 return (0);
1767 1781 }
1768 1782 VN_RELE(dirvp);
1769 1783 dirvp = vp;
1770 1784 pn_skipslash(&pn);
1771 1785 }
1772 1786
1773 1787 ASSERT(dirvp->v_type == VDIR);
1774 1788 if (dirvp != rvp)
1775 1789 VN_RELE(rvp);
1776 1790 pn_free(&pn);
1777 1791 kmem_free(nm, MAXNAMELEN);
1778 1792
1779 1793 dlen = ndirents * (sizeof (*dbuf));
1780 1794 dbuf = kmem_alloc(dlen, KM_SLEEP);
1781 1795
1782 1796 uio.uio_iov = &iov;
1783 1797 uio.uio_iovcnt = 1;
1784 1798 uio.uio_segflg = UIO_SYSSPACE;
1785 1799 uio.uio_fmode = 0;
1786 1800 uio.uio_extflg = UIO_COPY_CACHED;
1787 1801 uio.uio_loffset = 0;
1788 1802 uio.uio_llimit = MAXOFFSET_T;
1789 1803
1790 1804 eof = 0;
1791 1805 error = 0;
1792 1806 while (!error && !eof) {
1793 1807 uio.uio_resid = dlen;
1794 1808 iov.iov_base = (char *)dbuf;
1795 1809 iov.iov_len = dlen;
1796 1810
1797 1811 (void) VOP_RWLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
1798 1812 error = VOP_READDIR(dirvp, &uio, kcred, &eof, NULL, 0);
1799 1813 VOP_RWUNLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
1800 1814
1801 1815 dbuflen = dlen - uio.uio_resid;
1802 1816
1803 1817 if (error || dbuflen == 0)
1804 1818 break;
1805 1819
1806 1820 for (dp = dbuf; ((intptr_t)dp < (intptr_t)dbuf + dbuflen);
1807 1821 dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen)) {
1808 1822
1809 1823 nm = dp->d_name;
1810 1824
1811 1825 if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0)
1812 1826 continue;
1813 1827
1814 1828 if (strncmp(nm, nodename, nodenamelen) != 0)
1815 1829 continue;
1816 1830
1817 1831 error = VOP_LOOKUP(dirvp, nm, &vp,
1818 1832 NULL, 0, NULL, kcred, NULL, NULL, NULL);
1819 1833
1820 1834 dsysdebug(error,
1821 1835 ("rem_drv %s/%s lookup (%d)\n",
1822 1836 dir, nm, error));
1823 1837
1824 1838 if (error)
1825 1839 continue;
1826 1840
1827 1841 ASSERT(vp->v_type == VDIR ||
1828 1842 vp->v_type == VCHR || vp->v_type == VBLK);
1829 1843
1830 1844 if (vp->v_type == VDIR) {
1831 1845 error = devfs_remdrv_rmdir(vp, nm, rvp);
1832 1846 if (error == 0) {
1833 1847 error = VOP_RMDIR(dirvp, (char *)nm,
1834 1848 rvp, kcred, NULL, 0);
1835 1849 dsysdebug(error,
1836 1850 ("rem_drv %s/%s rmdir (%d)\n",
1837 1851 dir, nm, error));
1838 1852 }
1839 1853 } else {
1840 1854 error = VOP_REMOVE(dirvp, (char *)nm, kcred,
1841 1855 NULL, 0);
1842 1856 dsysdebug(error,
1843 1857 ("rem_drv %s/%s remove (%d)\n",
1844 1858 dir, nm, error));
1845 1859 }
1846 1860
1847 1861 VN_RELE(vp);
1848 1862 if (error)
1849 1863 goto exit;
1850 1864 }
1851 1865 }
1852 1866
1853 1867 exit:
1854 1868 VN_RELE(dirvp);
1855 1869
1856 1870 kmem_free(dbuf, dlen);
1857 1871
1858 1872 return (0);
1859 1873 }
1860 1874
1861 1875 struct dv_list {
1862 1876 struct dv_node *dv;
1863 1877 struct dv_list *next;
1864 1878 };
1865 1879
1866 1880 void
1867 1881 dv_walk(
1868 1882 struct dv_node *ddv,
1869 1883 char *devnm,
1870 1884 void (*callback)(struct dv_node *, void *),
1871 1885 void *arg)
1872 1886 {
1873 1887 struct vnode *dvp;
1874 1888 struct dv_node *dv;
1875 1889 struct dv_list *head, *tail, *next;
1876 1890 int len;
1877 1891
1878 1892 dcmn_err3(("dv_walk: ddv = %s, devnm = %s\n",
1879 1893 ddv->dv_name, devnm ? devnm : "<null>"));
1880 1894
1881 1895 dvp = DVTOV(ddv);
1882 1896
1883 1897 ASSERT(dvp->v_type == VDIR);
1884 1898
1885 1899 head = tail = next = NULL;
1886 1900
1887 1901 rw_enter(&ddv->dv_contents, RW_READER);
1888 1902 mutex_enter(&dvp->v_lock);
1889 1903 for (dv = DV_FIRST_ENTRY(ddv); dv; dv = DV_NEXT_ENTRY(ddv, dv)) {
1890 1904 /*
1891 1905 * If devnm is not NULL and is not the empty string,
1892 1906 * select only dv_nodes with matching non-minor name
1893 1907 */
1894 1908 if (devnm && (len = strlen(devnm)) &&
1895 1909 (strncmp(devnm, dv->dv_name, len) ||
1896 1910 (dv->dv_name[len] != ':' && dv->dv_name[len] != '\0')))
1897 1911 continue;
1898 1912
1899 1913 callback(dv, arg);
1900 1914
1901 1915 if (DVTOV(dv)->v_type != VDIR)
1902 1916 continue;
1903 1917
1904 1918 next = kmem_zalloc(sizeof (*next), KM_SLEEP);
1905 1919 next->dv = dv;
1906 1920
1907 1921 if (tail)
1908 1922 tail->next = next;
1909 1923 else
1910 1924 head = next;
1911 1925
1912 1926 tail = next;
1913 1927 }
1914 1928
1915 1929 while (head) {
1916 1930 dv_walk(head->dv, NULL, callback, arg);
1917 1931 next = head->next;
1918 1932 kmem_free(head, sizeof (*head));
1919 1933 head = next;
1920 1934 }
1921 1935 rw_exit(&ddv->dv_contents);
1922 1936 mutex_exit(&dvp->v_lock);
1923 1937 }
|
↓ open down ↓ |
464 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX