📄 open.c
字号:
err_code = r; return(NIL_INODE); } } else { /* Either last component exists, or there is some problem. */ if (rip != NIL_INODE) r = EEXIST; else r = err_code; } /* Return the directory inode and exit. */ put_inode(rlast_dir_ptr); err_code = r; return(rip);}/*===========================================================================* * pipe_open * *===========================================================================*/PRIVATE int pipe_open(register struct inode *rip, register mode_t bits, register int oflags){/* This function is called from common_open. It checks if * there is at least one reader/writer pair for the pipe, if not * it suspends the caller, otherwise it revives all other blocked * processes hanging on the pipe. */ rip->i_pipe = I_PIPE; if (find_filp(rip, bits & W_BIT ? R_BIT : W_BIT) == NIL_FILP) { if (oflags & O_NONBLOCK) { if (bits & W_BIT) return(ENXIO); } else { suspend(XPOPEN); /* suspend caller */ return(SUSPEND); } } else if (susp_count > 0) {/* revive blocked processes */ release(rip, OPEN, susp_count); release(rip, CREAT, susp_count); } return(OK);}/*===========================================================================* * do_mknod * *===========================================================================*/PUBLIC int do_mknod(){/* Perform the mknod(name, mode, addr) system call. */ register mode_t bits, mode_bits; struct inode *ip; /* Only the super_user may make nodes other than fifos. */ mode_bits = (mode_t) m_in.mk_mode; /* mode of the inode */ if (!super_user && ((mode_bits & I_TYPE) != I_NAMED_PIPE)) return(EPERM); if (fetch_name(m_in.name1, m_in.name1_length, M1) != OK) return(err_code); bits = (mode_bits & I_TYPE) | (mode_bits & ALL_MODES & fp->fp_umask); ip = new_node(user_path, bits, (zone_t) m_in.mk_z0); put_inode(ip); return(err_code);}/*===========================================================================* * do_mkdir * *===========================================================================*/PUBLIC int do_mkdir(){/* Perform the mkdir(name, mode) system call. */ int r1, r2; /* status codes */ ino_t dot, dotdot; /* inode numbers for . and .. */ mode_t bits; /* mode bits for the new inode */ char string[NAME_MAX]; /* last component of the new dir's path name */ register struct inode *rip, *ldirp; /* Check to see if it is possible to make another link in the parent dir. */ if (fetch_name(m_in.name1, m_in.name1_length, M1) != OK) return(err_code); ldirp = last_dir(user_path, string); /* pointer to new dir's parent */ if (ldirp == NIL_INODE) return(err_code); if (ldirp->i_nlinks >= (ldirp->i_sp->s_version == V1 ? CHAR_MAX : SHRT_MAX)) { put_inode(ldirp); /* return parent */ return(EMLINK); } /* Next make the inode. If that fails, return error code. */ bits = I_DIRECTORY | (m_in.mode & RWX_MODES & fp->fp_umask); rip = new_node(user_path, bits, (zone_t) 0); if (rip == NIL_INODE || err_code == EEXIST) { put_inode(rip); /* can't make dir: it already exists */ put_inode(ldirp); /* return parent too */ return(err_code); } /* Get the inode numbers for . and .. to enter in the directory. */ dotdot = ldirp->i_num; /* parent's inode number */ dot = rip->i_num; /* inode number of the new dir itself */ /* Now make dir entries for . and .. unless the disk is completely full. */ /* Use dot1 and dot2, so the mode of the directory isn't important. */ rip->i_mode = bits; /* set mode */ r1 = search_dir(rip, dot1, &dot, ENTER); /* enter . in the new dir */ r2 = search_dir(rip, dot2, &dotdot, ENTER); /* enter .. in the new dir */ /* If both . and .. were successfully entered, increment the link counts. */ if (r1 == OK && r2 == OK) { /* Normal case. It was possible to enter . and .. in the new dir. */ rip->i_nlinks++; /* this accounts for . */ ldirp->i_nlinks++; /* this accounts for .. */ ldirp->i_dirt = DIRTY; /* mark parent's inode as dirty */ } else { /* It was not possible to enter . or .. probably disk was full. */ (void) search_dir(ldirp, string, (ino_t *) 0, DELETE); rip->i_nlinks--; /* undo the increment done in new_node() */ } rip->i_dirt = DIRTY; /* either way, i_nlinks has changed */ put_inode(ldirp); /* return the inode of the parent dir */ put_inode(rip); /* return the inode of the newly made dir */ return(err_code); /* new_node() always sets 'err_code' */}/*===========================================================================* * do_close * *===========================================================================*/PUBLIC int do_close(){/* Perform the close(fd) system call. */ register struct filp *rfilp; register struct inode *rip; struct file_lock *flp; int rw, mode_word, lock_count; dev_t dev; /* First locate the inode that belongs to the file descriptor. */ if ( (rfilp = get_filp(m_in.fd)) == NIL_FILP) return(err_code); rip = rfilp->filp_ino; /* 'rip' points to the inode */ if (rfilp->filp_count - 1 == 0 && rfilp->filp_mode != FILP_CLOSED) { /* Check to see if the file is special. */ mode_word = rip->i_mode & I_TYPE; if (mode_word == I_CHAR_SPECIAL || mode_word == I_BLOCK_SPECIAL) { dev = (dev_t) rip->i_zone[0]; if (mode_word == I_BLOCK_SPECIAL) { /* Invalidate cache entries unless special is mounted * or ROOT */ if (!mounted(rip)) { (void) do_sync(); /* purge cache */ invalidate(dev); } } /* Do any special processing on device close. */ dev_close(dev); } } /* If the inode being closed is a pipe, release everyone hanging on it. */ if (rip->i_pipe == I_PIPE) { rw = (rfilp->filp_mode & R_BIT ? WRITE : READ); release(rip, rw, NR_PROCS); } /* If a write has been done, the inode is already marked as DIRTY. */ if (--rfilp->filp_count == 0) { if (rip->i_pipe == I_PIPE && rip->i_count > 1) { /* Save the file position in the i-node in case needed later. * The read and write positions are saved separately. The * last 3 zones in the i-node are not used for (named) pipes. */ if (rfilp->filp_mode == R_BIT) rip->i_zone[V2_NR_DZONES+0] = (zone_t) rfilp->filp_pos; else rip->i_zone[V2_NR_DZONES+1] = (zone_t) rfilp->filp_pos; } put_inode(rip); } fp->fp_cloexec &= ~(1L << m_in.fd); /* turn off close-on-exec bit */ fp->fp_filp[m_in.fd] = NIL_FILP; /* Check to see if the file is locked. If so, release all locks. */ if (nr_locks == 0) return(OK); lock_count = nr_locks; /* save count of locks */ for (flp = &file_lock[0]; flp < &file_lock[NR_LOCKS]; flp++) { if (flp->lock_type == 0) continue; /* slot not in use */ if (flp->lock_inode == rip && flp->lock_pid == fp->fp_pid) { flp->lock_type = 0; nr_locks--; } } if (nr_locks < lock_count) lock_revive(); /* lock released */ return(OK);}/*===========================================================================* * do_lseek * *===========================================================================*/PUBLIC int do_lseek(){/* Perform the lseek(ls_fd, offset, whence) system call. */ register struct filp *rfilp; register off_t pos; /* Check to see if the file descriptor is valid. */ if ( (rfilp = get_filp(m_in.ls_fd)) == NIL_FILP) return(err_code); /* No lseek on pipes. */ if (rfilp->filp_ino->i_pipe == I_PIPE) return(ESPIPE); /* The value of 'whence' determines the start position to use. */ switch(m_in.whence) { case 0: pos = 0; break; case 1: pos = rfilp->filp_pos; break; case 2: pos = rfilp->filp_ino->i_size; break; default: return(EINVAL); } /* Check for overflow. */ if (((long)m_in.offset > 0) && ((long)(pos + m_in.offset) < (long)pos)) return(EINVAL); if (((long)m_in.offset < 0) && ((long)(pos + m_in.offset) > (long)pos)) return(EINVAL); pos = pos + m_in.offset; if (pos != rfilp->filp_pos) rfilp->filp_ino->i_seek = ISEEK; /* inhibit read ahead */ rfilp->filp_pos = pos; m_out.reply_l1 = pos; /* insert the long into the output message */ return(OK);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -