* If (bufsize > 0 && bufseg == UIO_SYSSPACE) * The caller is responsible for freeing memory which will be allocated * in '*buf'. */
| 422 | * in '*buf'. |
| 423 | */ |
| 424 | int |
| 425 | kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize, |
| 426 | size_t *countp, enum uio_seg bufseg, int mode) |
| 427 | { |
| 428 | struct mount *mp, *nmp; |
| 429 | struct statfs *sfsp, *sp, *sptmp, *tofree; |
| 430 | size_t count, maxcount; |
| 431 | int error; |
| 432 | |
| 433 | switch (mode) { |
| 434 | case MNT_WAIT: |
| 435 | case MNT_NOWAIT: |
| 436 | break; |
| 437 | default: |
| 438 | if (bufseg == UIO_SYSSPACE) |
| 439 | *buf = NULL; |
| 440 | return (EINVAL); |
| 441 | } |
| 442 | restart: |
| 443 | maxcount = bufsize / sizeof(struct statfs); |
| 444 | if (bufsize == 0) { |
| 445 | sfsp = NULL; |
| 446 | tofree = NULL; |
| 447 | } else if (bufseg == UIO_USERSPACE) { |
| 448 | sfsp = *buf; |
| 449 | tofree = NULL; |
| 450 | } else /* if (bufseg == UIO_SYSSPACE) */ { |
| 451 | count = 0; |
| 452 | mtx_lock(&mountlist_mtx); |
| 453 | TAILQ_FOREACH(mp, &mountlist, mnt_list) { |
| 454 | count++; |
| 455 | } |
| 456 | mtx_unlock(&mountlist_mtx); |
| 457 | if (maxcount > count) |
| 458 | maxcount = count; |
| 459 | tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs), |
| 460 | M_STATFS, M_WAITOK); |
| 461 | } |
| 462 | |
| 463 | count = 0; |
| 464 | |
| 465 | /* |
| 466 | * If there is no target buffer they only want the count. |
| 467 | * |
| 468 | * This could be TAILQ_FOREACH but it is open-coded to match the original |
| 469 | * code below. |
| 470 | */ |
| 471 | if (sfsp == NULL) { |
| 472 | mtx_lock(&mountlist_mtx); |
| 473 | for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { |
| 474 | if (prison_canseemount(td->td_ucred, mp) != 0) { |
| 475 | nmp = TAILQ_NEXT(mp, mnt_list); |
| 476 | continue; |
| 477 | } |
| 478 | #ifdef MAC |
| 479 | if (mac_mount_check_stat(td->td_ucred, mp) != 0) { |
| 480 | nmp = TAILQ_NEXT(mp, mnt_list); |
| 481 | continue; |
no test coverage detected