kern_descrip.c (cc43c38c8757a7d9b18141df42a04314f8ed6227) kern_descrip.c (397c19d1753d210247d77eb3ca33d1c7c1eb2fa9)
1/*-
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.

--- 81 unchanged lines hidden (view full) ---

90/* How to treat 'new' parameter when allocating a fd for do_dup(). */
91enum dup_type { DUP_VARIABLE, DUP_FIXED };
92
93static int do_dup(struct thread *td, enum dup_type type, int old, int new,
94 register_t *retval);
95static int fd_first_free(struct filedesc *, int, int);
96static int fd_last_used(struct filedesc *, int, int);
97static void fdgrowtable(struct filedesc *, int);
1/*-
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.

--- 81 unchanged lines hidden (view full) ---

90/* How to treat 'new' parameter when allocating a fd for do_dup(). */
91enum dup_type { DUP_VARIABLE, DUP_FIXED };
92
93static int do_dup(struct thread *td, enum dup_type type, int old, int new,
94 register_t *retval);
95static int fd_first_free(struct filedesc *, int, int);
96static int fd_last_used(struct filedesc *, int, int);
97static void fdgrowtable(struct filedesc *, int);
98static int fdrop_locked(struct file *fp, struct thread *td);
99static void fdunused(struct filedesc *fdp, int fd);
100static void fdused(struct filedesc *fdp, int fd);
101
102/*
103 * A process is initially started out with NDFILE descriptors stored within
104 * this structure, selected to be enough for typical applications based on
105 * the historical limit of 20 open files (and the usage of descriptors by
106 * shells). If these descriptors are exhausted, a larger descriptor table

--- 25 unchanged lines hidden (view full) ---

132 struct file *fd_dfiles[NDFILE];
133 char fd_dfileflags[NDFILE];
134 NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)];
135};
136
137/*
138 * Descriptor management.
139 */
98static void fdunused(struct filedesc *fdp, int fd);
99static void fdused(struct filedesc *fdp, int fd);
100
101/*
102 * A process is initially started out with NDFILE descriptors stored within
103 * this structure, selected to be enough for typical applications based on
104 * the historical limit of 20 open files (and the usage of descriptors by
105 * shells). If these descriptors are exhausted, a larger descriptor table

--- 25 unchanged lines hidden (view full) ---

131 struct file *fd_dfiles[NDFILE];
132 char fd_dfileflags[NDFILE];
133 NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)];
134};
135
136/*
137 * Descriptor management.
138 */
140struct filelist filehead; /* head of list of open files */
141int openfiles; /* actual number of open files */
142struct sx filelist_lock; /* sx to protect filelist */
139volatile int openfiles; /* actual number of open files */
143struct mtx sigio_lock; /* mtx to protect pointers to sigio */
144void (*mq_fdclose)(struct thread *td, int fd, struct file *fp);
145
146/* A mutex to protect the association between a proc and filedesc. */
147static struct mtx fdesc_mtx;
148
149/*
150 * Find the first zero bit in the given bitmap, starting at low and not

--- 272 unchanged lines hidden (view full) ---

423
424 case F_GETFL:
425 FILEDESC_SLOCK(fdp);
426 if ((fp = fdtofp(fd, fdp)) == NULL) {
427 FILEDESC_SUNLOCK(fdp);
428 error = EBADF;
429 break;
430 }
140struct mtx sigio_lock; /* mtx to protect pointers to sigio */
141void (*mq_fdclose)(struct thread *td, int fd, struct file *fp);
142
143/* A mutex to protect the association between a proc and filedesc. */
144static struct mtx fdesc_mtx;
145
146/*
147 * Find the first zero bit in the given bitmap, starting at low and not

--- 272 unchanged lines hidden (view full) ---

420
421 case F_GETFL:
422 FILEDESC_SLOCK(fdp);
423 if ((fp = fdtofp(fd, fdp)) == NULL) {
424 FILEDESC_SUNLOCK(fdp);
425 error = EBADF;
426 break;
427 }
431 FILE_LOCK(fp);
432 td->td_retval[0] = OFLAGS(fp->f_flag);
428 td->td_retval[0] = OFLAGS(fp->f_flag);
433 FILE_UNLOCK(fp);
434 FILEDESC_SUNLOCK(fdp);
435 break;
436
437 case F_SETFL:
438 FILEDESC_SLOCK(fdp);
439 if ((fp = fdtofp(fd, fdp)) == NULL) {
440 FILEDESC_SUNLOCK(fdp);
441 error = EBADF;
442 break;
443 }
429 FILEDESC_SUNLOCK(fdp);
430 break;
431
432 case F_SETFL:
433 FILEDESC_SLOCK(fdp);
434 if ((fp = fdtofp(fd, fdp)) == NULL) {
435 FILEDESC_SUNLOCK(fdp);
436 error = EBADF;
437 break;
438 }
444 FILE_LOCK(fp);
445 fhold_locked(fp);
446 fp->f_flag &= ~FCNTLFLAGS;
447 fp->f_flag |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
448 FILE_UNLOCK(fp);
439 fhold(fp);
449 FILEDESC_SUNLOCK(fdp);
440 FILEDESC_SUNLOCK(fdp);
441 do {
442 tmp = flg = fp->f_flag;
443 tmp &= ~FCNTLFLAGS;
444 tmp |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
445 } while(atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0);
450 tmp = fp->f_flag & FNONBLOCK;
451 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
452 if (error) {
453 fdrop(fp, td);
454 break;
455 }
456 tmp = fp->f_flag & FASYNC;
457 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
458 if (error == 0) {
459 fdrop(fp, td);
460 break;
461 }
446 tmp = fp->f_flag & FNONBLOCK;
447 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
448 if (error) {
449 fdrop(fp, td);
450 break;
451 }
452 tmp = fp->f_flag & FASYNC;
453 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
454 if (error == 0) {
455 fdrop(fp, td);
456 break;
457 }
462 FILE_LOCK(fp);
463 fp->f_flag &= ~FNONBLOCK;
464 FILE_UNLOCK(fp);
458 atomic_clear_int(&fp->f_flag, FNONBLOCK);
465 tmp = 0;
466 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
467 fdrop(fp, td);
468 break;
469
470 case F_GETOWN:
471 FILEDESC_SLOCK(fdp);
472 if ((fp = fdtofp(fd, fdp)) == NULL) {

--- 881 unchanged lines hidden (view full) ---

1354 * descriptor table and one reference for resultfp. This is to prevent us
1355 * being preempted and the entry in the descriptor table closed after we
1356 * release the FILEDESC lock.
1357 */
1358int
1359falloc(struct thread *td, struct file **resultfp, int *resultfd)
1360{
1361 struct proc *p = td->td_proc;
459 tmp = 0;
460 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
461 fdrop(fp, td);
462 break;
463
464 case F_GETOWN:
465 FILEDESC_SLOCK(fdp);
466 if ((fp = fdtofp(fd, fdp)) == NULL) {

--- 881 unchanged lines hidden (view full) ---

1348 * descriptor table and one reference for resultfp. This is to prevent us
1349 * being preempted and the entry in the descriptor table closed after we
1350 * release the FILEDESC lock.
1351 */
1352int
1353falloc(struct thread *td, struct file **resultfp, int *resultfd)
1354{
1355 struct proc *p = td->td_proc;
1362 struct file *fp, *fq;
1356 struct file *fp;
1363 int error, i;
1364 int maxuserfiles = maxfiles - (maxfiles / 20);
1365 static struct timeval lastfail;
1366 static int curfail;
1367
1368 fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
1357 int error, i;
1358 int maxuserfiles = maxfiles - (maxfiles / 20);
1359 static struct timeval lastfail;
1360 static int curfail;
1361
1362 fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
1369 sx_xlock(&filelist_lock);
1370
1371 if ((openfiles >= maxuserfiles &&
1372 priv_check(td, PRIV_MAXFILES) != 0) ||
1373 openfiles >= maxfiles) {
1374 if (ppsratecheck(&lastfail, &curfail, 1)) {
1375 printf("kern.maxfiles limit exceeded by uid %i, please see tuning(7).\n",
1376 td->td_ucred->cr_ruid);
1377 }
1363 if ((openfiles >= maxuserfiles &&
1364 priv_check(td, PRIV_MAXFILES) != 0) ||
1365 openfiles >= maxfiles) {
1366 if (ppsratecheck(&lastfail, &curfail, 1)) {
1367 printf("kern.maxfiles limit exceeded by uid %i, please see tuning(7).\n",
1368 td->td_ucred->cr_ruid);
1369 }
1378 sx_xunlock(&filelist_lock);
1379 uma_zfree(file_zone, fp);
1380 return (ENFILE);
1381 }
1370 uma_zfree(file_zone, fp);
1371 return (ENFILE);
1372 }
1382 openfiles++;
1373 atomic_add_int(&openfiles, 1);
1383
1384 /*
1385 * If the process has file descriptor zero open, add the new file
1386 * descriptor to the list of open files at that point, otherwise
1387 * put it at the front of the list of open files.
1388 */
1374
1375 /*
1376 * If the process has file descriptor zero open, add the new file
1377 * descriptor to the list of open files at that point, otherwise
1378 * put it at the front of the list of open files.
1379 */
1389 fp->f_mtxp = mtx_pool_alloc(mtxpool_sleep);
1390 fp->f_count = 1;
1391 if (resultfp)
1392 fp->f_count++;
1393 fp->f_cred = crhold(td->td_ucred);
1394 fp->f_ops = &badfileops;
1395 fp->f_data = NULL;
1396 fp->f_vnode = NULL;
1397 FILEDESC_XLOCK(p->p_fd);
1380 fp->f_count = 1;
1381 if (resultfp)
1382 fp->f_count++;
1383 fp->f_cred = crhold(td->td_ucred);
1384 fp->f_ops = &badfileops;
1385 fp->f_data = NULL;
1386 fp->f_vnode = NULL;
1387 FILEDESC_XLOCK(p->p_fd);
1398 if ((fq = p->p_fd->fd_ofiles[0])) {
1399 LIST_INSERT_AFTER(fq, fp, f_list);
1400 } else {
1401 LIST_INSERT_HEAD(&filehead, fp, f_list);
1402 }
1403 sx_xunlock(&filelist_lock);
1404 if ((error = fdalloc(td, 0, &i))) {
1405 FILEDESC_XUNLOCK(p->p_fd);
1406 fdrop(fp, td);
1407 if (resultfp)
1408 fdrop(fp, td);
1409 return (error);
1410 }
1411 p->p_fd->fd_ofiles[i] = fp;

--- 545 unchanged lines hidden (view full) ---

1957 FILEDESC_XUNLOCK(fdp);
1958 }
1959 VFS_UNLOCK_GIANT(vfslocked);
1960 }
1961 return (fdrop(fp, td));
1962}
1963
1964/*
1388 if ((error = fdalloc(td, 0, &i))) {
1389 FILEDESC_XUNLOCK(p->p_fd);
1390 fdrop(fp, td);
1391 if (resultfp)
1392 fdrop(fp, td);
1393 return (error);
1394 }
1395 p->p_fd->fd_ofiles[i] = fp;

--- 545 unchanged lines hidden (view full) ---

1941 FILEDESC_XUNLOCK(fdp);
1942 }
1943 VFS_UNLOCK_GIANT(vfslocked);
1944 }
1945 return (fdrop(fp, td));
1946}
1947
1948/*
1949 * Initialize the file pointer with the specified properties.
1950 *
1951 * The ops are set with release semantics to be certain that the flags, type,
1952 * and data are visible when ops is. This is to prevent ops methods from being
1953 * called with bad data.
1954 */
1955void
1956finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops)
1957{
1958 fp->f_data = data;
1959 fp->f_flag = flag;
1960 fp->f_type = type;
1961 atomic_store_rel_ptr((volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops);
1962}
1963
1964
1965/*
1965 * Extract the file pointer associated with the specified descriptor for the
1966 * current user process.
1967 *
1968 * If the descriptor doesn't exist, EBADF is returned.
1969 *
1970 * If the descriptor exists but doesn't match 'flags' then return EBADF for
1971 * read attempts and EINVAL for write attempts.
1972 *

--- 157 unchanged lines hidden (view full) ---

2130fputsock(struct socket *so)
2131{
2132
2133 ACCEPT_LOCK();
2134 SOCK_LOCK(so);
2135 sorele(so);
2136}
2137
1966 * Extract the file pointer associated with the specified descriptor for the
1967 * current user process.
1968 *
1969 * If the descriptor doesn't exist, EBADF is returned.
1970 *
1971 * If the descriptor exists but doesn't match 'flags' then return EBADF for
1972 * read attempts and EINVAL for write attempts.
1973 *

--- 157 unchanged lines hidden (view full) ---

2131fputsock(struct socket *so)
2132{
2133
2134 ACCEPT_LOCK();
2135 SOCK_LOCK(so);
2136 sorele(so);
2137}
2138
2138int
2139fdrop(struct file *fp, struct thread *td)
2140{
2141
2142 FILE_LOCK(fp);
2143 return (fdrop_locked(fp, td));
2144}
2145
2146/*
2139/*
2147 * Drop reference on struct file passed in, may call closef if the
2148 * reference hits zero.
2149 * Expects struct file locked, and will unlock it.
2140 * Handle the last reference to a file being closed.
2150 */
2141 */
2151static int
2152fdrop_locked(struct file *fp, struct thread *td)
2142int
2143_fdrop(struct file *fp, struct thread *td)
2153{
2154 int error;
2155
2144{
2145 int error;
2146
2156 FILE_LOCK_ASSERT(fp, MA_OWNED);
2157
2158 if (--fp->f_count > 0) {
2159 FILE_UNLOCK(fp);
2160 return (0);
2161 }
2162
2163 /*
2164 * We might have just dropped the last reference to a file
2165 * object that is for a UNIX domain socket whose message
2166 * buffers are being examined in unp_gc(). If that is the
2167 * case, FWAIT will be set in f_gcflag and we need to wait for
2168 * unp_gc() to finish its scan.
2169 */
2170 while (fp->f_gcflag & FWAIT)
2171 msleep(&fp->f_gcflag, fp->f_mtxp, 0, "fpdrop", 0);
2172
2173 /* We have the last ref so we can proceed without the file lock. */
2174 FILE_UNLOCK(fp);
2175 if (fp->f_count < 0)
2176 panic("fdrop: count < 0");
2147 error = 0;
2148 if (fp->f_count != 0)
2149 panic("fdrop: count %d", fp->f_count);
2177 if (fp->f_ops != &badfileops)
2178 error = fo_close(fp, td);
2150 if (fp->f_ops != &badfileops)
2151 error = fo_close(fp, td);
2179 else
2180 error = 0;
2181
2182 sx_xlock(&filelist_lock);
2183 LIST_REMOVE(fp, f_list);
2184 openfiles--;
2185 sx_xunlock(&filelist_lock);
2152 atomic_subtract_int(&openfiles, 1);
2186 crfree(fp->f_cred);
2187 uma_zfree(file_zone, fp);
2188
2189 return (error);
2190}
2191
2192/*
2193 * Apply an advisory lock on a file descriptor.

--- 26 unchanged lines hidden (view full) ---

2220
2221 vp = fp->f_vnode;
2222 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2223 lf.l_whence = SEEK_SET;
2224 lf.l_start = 0;
2225 lf.l_len = 0;
2226 if (uap->how & LOCK_UN) {
2227 lf.l_type = F_UNLCK;
2153 crfree(fp->f_cred);
2154 uma_zfree(file_zone, fp);
2155
2156 return (error);
2157}
2158
2159/*
2160 * Apply an advisory lock on a file descriptor.

--- 26 unchanged lines hidden (view full) ---

2187
2188 vp = fp->f_vnode;
2189 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2190 lf.l_whence = SEEK_SET;
2191 lf.l_start = 0;
2192 lf.l_len = 0;
2193 if (uap->how & LOCK_UN) {
2194 lf.l_type = F_UNLCK;
2228 FILE_LOCK(fp);
2229 fp->f_flag &= ~FHASLOCK;
2230 FILE_UNLOCK(fp);
2195 atomic_clear_int(&fp->f_flag, FHASLOCK);
2231 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
2232 goto done2;
2233 }
2234 if (uap->how & LOCK_EX)
2235 lf.l_type = F_WRLCK;
2236 else if (uap->how & LOCK_SH)
2237 lf.l_type = F_RDLCK;
2238 else {
2239 error = EBADF;
2240 goto done2;
2241 }
2196 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
2197 goto done2;
2198 }
2199 if (uap->how & LOCK_EX)
2200 lf.l_type = F_WRLCK;
2201 else if (uap->how & LOCK_SH)
2202 lf.l_type = F_RDLCK;
2203 else {
2204 error = EBADF;
2205 goto done2;
2206 }
2242 FILE_LOCK(fp);
2243 fp->f_flag |= FHASLOCK;
2244 FILE_UNLOCK(fp);
2207 atomic_set_int(&fp->f_flag, FHASLOCK);
2245 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
2246 (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
2247done2:
2248 fdrop(fp, td);
2249 VFS_UNLOCK_GIANT(vfslocked);
2250 return (error);
2251}
2252/*

--- 28 unchanged lines hidden (view full) ---

2281 * Any other error code is just returned.
2282 */
2283 switch (error) {
2284 case ENODEV:
2285 /*
2286 * Check that the mode the file is being opened for is a
2287 * subset of the mode of the existing descriptor.
2288 */
2208 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
2209 (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
2210done2:
2211 fdrop(fp, td);
2212 VFS_UNLOCK_GIANT(vfslocked);
2213 return (error);
2214}
2215/*

--- 28 unchanged lines hidden (view full) ---

2244 * Any other error code is just returned.
2245 */
2246 switch (error) {
2247 case ENODEV:
2248 /*
2249 * Check that the mode the file is being opened for is a
2250 * subset of the mode of the existing descriptor.
2251 */
2289 FILE_LOCK(wfp);
2290 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
2252 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
2291 FILE_UNLOCK(wfp);
2292 FILEDESC_XUNLOCK(fdp);
2293 return (EACCES);
2294 }
2295 fp = fdp->fd_ofiles[indx];
2296 fdp->fd_ofiles[indx] = wfp;
2297 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2298 if (fp == NULL)
2299 fdused(fdp, indx);
2253 FILEDESC_XUNLOCK(fdp);
2254 return (EACCES);
2255 }
2256 fp = fdp->fd_ofiles[indx];
2257 fdp->fd_ofiles[indx] = wfp;
2258 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2259 if (fp == NULL)
2260 fdused(fdp, indx);
2300 fhold_locked(wfp);
2301 FILE_UNLOCK(wfp);
2261 fhold(wfp);
2302 FILEDESC_XUNLOCK(fdp);
2303 if (fp != NULL)
2304 /*
2305 * We now own the reference to fp that the ofiles[]
2306 * array used to own. Release it.
2307 */
2308 fdrop(fp, td);
2309 return (0);

--- 104 unchanged lines hidden (view full) ---

2414sysctl_kern_file(SYSCTL_HANDLER_ARGS)
2415{
2416 struct xfile xf;
2417 struct filedesc *fdp;
2418 struct file *fp;
2419 struct proc *p;
2420 int error, n;
2421
2262 FILEDESC_XUNLOCK(fdp);
2263 if (fp != NULL)
2264 /*
2265 * We now own the reference to fp that the ofiles[]
2266 * array used to own. Release it.
2267 */
2268 fdrop(fp, td);
2269 return (0);

--- 104 unchanged lines hidden (view full) ---

2374sysctl_kern_file(SYSCTL_HANDLER_ARGS)
2375{
2376 struct xfile xf;
2377 struct filedesc *fdp;
2378 struct file *fp;
2379 struct proc *p;
2380 int error, n;
2381
2422 /*
2423 * Note: because the number of file descriptors is calculated
2424 * in different ways for sizing vs returning the data,
2425 * there is information leakage from the first loop. However,
2426 * it is of a similar order of magnitude to the leakage from
2427 * global system statistics such as kern.openfiles.
2428 */
2429 error = sysctl_wire_old_buffer(req, 0);
2430 if (error != 0)
2431 return (error);
2432 if (req->oldptr == NULL) {
2382 error = sysctl_wire_old_buffer(req, 0);
2383 if (error != 0)
2384 return (error);
2385 if (req->oldptr == NULL) {
2433 n = 16; /* A slight overestimate. */
2434 sx_slock(&filelist_lock);
2435 LIST_FOREACH(fp, &filehead, f_list) {
2436 /*
2437 * We should grab the lock, but this is an
2438 * estimate, so does it really matter?
2439 */
2440 /* mtx_lock(fp->f_mtxp); */
2441 n += fp->f_count;
2442 /* mtx_unlock(f->f_mtxp); */
2386 n = 0;
2387 sx_slock(&allproc_lock);
2388 FOREACH_PROC_IN_SYSTEM(p) {
2389 if (p->p_state == PRS_NEW)
2390 continue;
2391 fdp = fdhold(p);
2392 if (fdp == NULL)
2393 continue;
2394 /* overestimates sparse tables. */
2395 n += fdp->fd_lastfile;
2396 fddrop(fdp);
2443 }
2397 }
2444 sx_sunlock(&filelist_lock);
2398 sx_sunlock(&allproc_lock);
2445 return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
2446 }
2447 error = 0;
2448 bzero(&xf, sizeof(xf));
2449 xf.xf_size = sizeof(xf);
2450 sx_slock(&allproc_lock);
2451 FOREACH_PROC_IN_SYSTEM(p) {
2452 if (p->p_state == PRS_NEW)

--- 14 unchanged lines hidden (view full) ---

2467 if ((fp = fdp->fd_ofiles[n]) == NULL)
2468 continue;
2469 xf.xf_fd = n;
2470 xf.xf_file = fp;
2471 xf.xf_data = fp->f_data;
2472 xf.xf_vnode = fp->f_vnode;
2473 xf.xf_type = fp->f_type;
2474 xf.xf_count = fp->f_count;
2399 return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
2400 }
2401 error = 0;
2402 bzero(&xf, sizeof(xf));
2403 xf.xf_size = sizeof(xf);
2404 sx_slock(&allproc_lock);
2405 FOREACH_PROC_IN_SYSTEM(p) {
2406 if (p->p_state == PRS_NEW)

--- 14 unchanged lines hidden (view full) ---

2421 if ((fp = fdp->fd_ofiles[n]) == NULL)
2422 continue;
2423 xf.xf_fd = n;
2424 xf.xf_file = fp;
2425 xf.xf_data = fp->f_data;
2426 xf.xf_vnode = fp->f_vnode;
2427 xf.xf_type = fp->f_type;
2428 xf.xf_count = fp->f_count;
2475 xf.xf_msgcount = fp->f_msgcount;
2429 xf.xf_msgcount = 0;
2476 xf.xf_offset = fp->f_offset;
2477 xf.xf_flag = fp->f_flag;
2478 error = SYSCTL_OUT(req, &xf, sizeof(xf));
2479 if (error)
2480 break;
2481 }
2482 FILEDESC_SUNLOCK(fdp);
2483 fddrop(fdp);

--- 34 unchanged lines hidden (view full) ---

2518 PROC_UNLOCK(p);
2519 kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
2520 FILEDESC_SLOCK(fdp);
2521 for (i = 0; i < fdp->fd_nfiles; i++) {
2522 if ((fp = fdp->fd_ofiles[i]) == NULL)
2523 continue;
2524 bzero(kif, sizeof(*kif));
2525 kif->kf_structsize = sizeof(*kif);
2430 xf.xf_offset = fp->f_offset;
2431 xf.xf_flag = fp->f_flag;
2432 error = SYSCTL_OUT(req, &xf, sizeof(xf));
2433 if (error)
2434 break;
2435 }
2436 FILEDESC_SUNLOCK(fdp);
2437 fddrop(fdp);

--- 34 unchanged lines hidden (view full) ---

2472 PROC_UNLOCK(p);
2473 kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
2474 FILEDESC_SLOCK(fdp);
2475 for (i = 0; i < fdp->fd_nfiles; i++) {
2476 if ((fp = fdp->fd_ofiles[i]) == NULL)
2477 continue;
2478 bzero(kif, sizeof(*kif));
2479 kif->kf_structsize = sizeof(*kif);
2526 FILE_LOCK(fp);
2527 vp = NULL;
2528 so = NULL;
2529 kif->kf_fd = i;
2530 switch (fp->f_type) {
2531 case DTYPE_VNODE:
2532 kif->kf_type = KF_TYPE_VNODE;
2533 vp = fp->f_vnode;
2480 vp = NULL;
2481 so = NULL;
2482 kif->kf_fd = i;
2483 switch (fp->f_type) {
2484 case DTYPE_VNODE:
2485 kif->kf_type = KF_TYPE_VNODE;
2486 vp = fp->f_vnode;
2534 vref(vp);
2535 break;
2536
2537 case DTYPE_SOCKET:
2538 kif->kf_type = KF_TYPE_SOCKET;
2539 so = fp->f_data;
2540 break;
2541
2542 case DTYPE_PIPE:

--- 35 unchanged lines hidden (view full) ---

2578 kif->kf_flags |= KF_FLAG_FSYNC;
2579 if (fp->f_flag & FNONBLOCK)
2580 kif->kf_flags |= KF_FLAG_NONBLOCK;
2581 if (fp->f_flag & O_DIRECT)
2582 kif->kf_flags |= KF_FLAG_DIRECT;
2583 if (fp->f_flag & FHASLOCK)
2584 kif->kf_flags |= KF_FLAG_HASLOCK;
2585 kif->kf_offset = fp->f_offset;
2487 break;
2488
2489 case DTYPE_SOCKET:
2490 kif->kf_type = KF_TYPE_SOCKET;
2491 so = fp->f_data;
2492 break;
2493
2494 case DTYPE_PIPE:

--- 35 unchanged lines hidden (view full) ---

2530 kif->kf_flags |= KF_FLAG_FSYNC;
2531 if (fp->f_flag & FNONBLOCK)
2532 kif->kf_flags |= KF_FLAG_NONBLOCK;
2533 if (fp->f_flag & O_DIRECT)
2534 kif->kf_flags |= KF_FLAG_DIRECT;
2535 if (fp->f_flag & FHASLOCK)
2536 kif->kf_flags |= KF_FLAG_HASLOCK;
2537 kif->kf_offset = fp->f_offset;
2586 FILE_UNLOCK(fp);
2587 if (vp != NULL) {
2538 if (vp != NULL) {
2539 vref(vp);
2588 switch (vp->v_type) {
2589 case VNON:
2590 kif->kf_vnode_type = KF_VTYPE_VNON;
2591 break;
2592 case VREG:
2593 kif->kf_vnode_type = KF_VTYPE_VREG;
2594 break;
2595 case VDIR:

--- 135 unchanged lines hidden (view full) ---

2731
2732 if (header)
2733 db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n",
2734 "File", "Type", "Data", "Flag", "GCFl", "Count",
2735 "MCount", "Vnode", "FPID", "FCmd");
2736 p = file_to_first_proc(fp);
2737 db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp,
2738 file_type_to_name(fp->f_type), fp->f_data, fp->f_flag,
2540 switch (vp->v_type) {
2541 case VNON:
2542 kif->kf_vnode_type = KF_VTYPE_VNON;
2543 break;
2544 case VREG:
2545 kif->kf_vnode_type = KF_VTYPE_VREG;
2546 break;
2547 case VDIR:

--- 135 unchanged lines hidden (view full) ---

2683
2684 if (header)
2685 db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n",
2686 "File", "Type", "Data", "Flag", "GCFl", "Count",
2687 "MCount", "Vnode", "FPID", "FCmd");
2688 p = file_to_first_proc(fp);
2689 db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp,
2690 file_type_to_name(fp->f_type), fp->f_data, fp->f_flag,
2739 fp->f_gcflag, fp->f_count, fp->f_msgcount, fp->f_vnode,
2691 0, fp->f_count, 0, fp->f_vnode,
2740 p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-");
2741}
2742
2743DB_SHOW_COMMAND(file, db_show_file)
2744{
2745 struct file *fp;
2746
2747 if (!have_addr) {
2748 db_printf("usage: show file <addr>\n");
2749 return;
2750 }
2751 fp = (struct file *)addr;
2752 db_print_file(fp, 1);
2753}
2754
2755DB_SHOW_COMMAND(files, db_show_files)
2756{
2692 p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-");
2693}
2694
2695DB_SHOW_COMMAND(file, db_show_file)
2696{
2697 struct file *fp;
2698
2699 if (!have_addr) {
2700 db_printf("usage: show file <addr>\n");
2701 return;
2702 }
2703 fp = (struct file *)addr;
2704 db_print_file(fp, 1);
2705}
2706
2707DB_SHOW_COMMAND(files, db_show_files)
2708{
2709 struct filedesc *fdp;
2757 struct file *fp;
2710 struct file *fp;
2711 struct proc *p;
2758 int header;
2712 int header;
2713 int n;
2759
2760 header = 1;
2714
2715 header = 1;
2761 LIST_FOREACH(fp, &filehead, f_list) {
2762 db_print_file(fp, header);
2763 header = 0;
2716 FOREACH_PROC_IN_SYSTEM(p) {
2717 if (p->p_state == PRS_NEW)
2718 continue;
2719 if ((fdp = p->p_fd) == NULL)
2720 continue;
2721 for (n = 0; n < fdp->fd_nfiles; ++n) {
2722 if ((fp = fdp->fd_ofiles[n]) == NULL)
2723 continue;
2724 db_print_file(fp, header);
2725 header = 0;
2726 }
2764 }
2765}
2766#endif
2767
2768SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
2769 &maxfilesperproc, 0, "Maximum files allowed open per process");
2770
2771SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
2772 &maxfiles, 0, "Maximum number of files");
2773
2774SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
2727 }
2728}
2729#endif
2730
2731SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
2732 &maxfilesperproc, 0, "Maximum files allowed open per process");
2733
2734SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
2735 &maxfiles, 0, "Maximum number of files");
2736
2737SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
2775 &openfiles, 0, "System-wide number of open files");
2738 __DEVOLATILE(int *, &openfiles), 0, "System-wide number of open files");
2776
2777/* ARGSUSED*/
2778static void
2779filelistinit(void *dummy)
2780{
2781
2782 file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
2783 NULL, NULL, UMA_ALIGN_PTR, 0);
2739
2740/* ARGSUSED*/
2741static void
2742filelistinit(void *dummy)
2743{
2744
2745 file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
2746 NULL, NULL, UMA_ALIGN_PTR, 0);
2784 sx_init(&filelist_lock, "filelist lock");
2785 mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
2786 mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF);
2787}
2788SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL)
2789
2790/*-------------------------------------------------------------------*/
2791
2792static int

--- 103 unchanged lines hidden ---
2747 mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
2748 mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF);
2749}
2750SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL)
2751
2752/*-------------------------------------------------------------------*/
2753
2754static int

--- 103 unchanged lines hidden ---