Merge of VFS by Balasz Gerofi with Minix trunk.
[minix3.git] / servers / vfs / filedes.c
blob4b137c969aeb30d33539983b796f555f8da677c1
1 /* This file contains the procedures that manipulate file descriptors.
3 * The entry points into this file are
4 * get_fd: look for free file descriptor and free filp slots
5 * get_filp: look up the filp entry for a given file descriptor
6 * find_filp: find a filp slot that points to a given inode
7 * inval_filp: invalidate a filp and associated fd's, only let close()
8 * happen on it
9 */
11 #include <sys/select.h>
13 #include "fs.h"
14 #include "file.h"
15 #include "fproc.h"
17 #include "vnode.h"
19 /*===========================================================================*
20 * get_fd *
21 *===========================================================================*/
22 PUBLIC int get_fd(int start, mode_t bits, int *k, struct filp **fpt)
24 /* Look for a free file descriptor and a free filp slot. Fill in the mode word
25 * in the latter, but don't claim either one yet, since the open() or creat()
26 * may yet fail.
29 register struct filp *f;
30 register int i;
32 *k = -1; /* we need a way to tell if file desc found */
34 /* Search the fproc fp_filp table for a free file descriptor. */
35 for (i = start; i < OPEN_MAX; i++) {
36 if (fp->fp_filp[i] == NIL_FILP && !FD_ISSET(i, &fp->fp_filp_inuse)) {
37 /* A file descriptor has been located. */
38 *k = i;
39 break;
43 /* Check to see if a file descriptor has been found. */
44 if (*k < 0) return(EMFILE); /* this is why we initialized k to -1 */
46 /* Now that a file descriptor has been found, look for a free filp slot. */
47 for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
48 if (f->filp_count == 0) {
49 f->filp_mode = bits;
50 f->filp_pos = 0L;
51 f->filp_selectors = 0;
52 f->filp_select_ops = 0;
53 f->filp_pipe_select_ops = 0;
54 f->filp_flags = 0;
55 *fpt = f;
56 return(OK);
60 /* If control passes here, the filp table must be full. Report that back. */
61 return(ENFILE);
64 /*===========================================================================*
65 * get_filp *
66 *===========================================================================*/
67 PUBLIC struct filp *get_filp(fild)
68 int fild; /* file descriptor */
70 /* See if 'fild' refers to a valid file descr. If so, return its filp ptr. */
72 return get_filp2(fp, fild);
75 /*===========================================================================*
76 * get_filp2 *
77 *===========================================================================*/
78 PUBLIC struct filp *get_filp2(rfp, fild)
79 register struct fproc *rfp;
80 int fild; /* file descriptor */
82 /* See if 'fild' refers to a valid file descr. If so, return its filp ptr. */
84 err_code = EBADF;
85 if (fild < 0 || fild >= OPEN_MAX ) return(NIL_FILP);
86 return(rfp->fp_filp[fild]); /* may also be NIL_FILP */
89 /*===========================================================================*
90 * find_filp *
91 *===========================================================================*/
92 PUBLIC struct filp *find_filp(register struct vnode *vp, mode_t bits)
94 /* Find a filp slot that refers to the inode 'rip' in a way as described
95 * by the mode bit 'bits'. Used for determining whether somebody is still
96 * interested in either end of a pipe. Also used when opening a FIFO to
97 * find partners to share a filp field with (to shared the file position).
98 * Like 'get_fd' it performs its job by linear search through the filp table.
101 register struct filp *f;
103 for (f = &filp[0]; f < &filp[NR_FILPS]; f++) {
104 if (f->filp_count != 0 && f->filp_vno == vp && (f->filp_mode & bits)){
105 return(f);
109 /* If control passes here, the filp wasn't there. Report that back. */
110 return(NIL_FILP);
113 /*===========================================================================*
114 * inval_filp *
115 *===========================================================================*/
116 PUBLIC int inval_filp(struct filp *fp)
118 int f, fd, n = 0;
119 for(f = 0; f < NR_PROCS; f++) {
120 if(fproc[f].fp_pid == PID_FREE) continue;
121 for(fd = 0; fd < OPEN_MAX; fd++) {
122 if(fproc[f].fp_filp[fd] && fproc[f].fp_filp[fd] == fp) {
123 fproc[f].fp_filp[fd] = NIL_FILP;
124 n++;
129 return n;