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