📄 open.c
字号:
23148 /* New inode acquired. Try to make directory entry. */
23149 if ((r = search_dir(rlast_dir_ptr, string, &rip->i_num,ENTER)) != OK) {
23150 put_inode(rlast_dir_ptr);
23151 rip->i_nlinks--; /* pity, have to free disk inode */
23152 rip->i_dirt = DIRTY; /* dirty inodes are written out */
23153 put_inode(rip); /* this call frees the inode */
23154 err_code = r;
23155 return(NIL_INODE);
23156 }
23157
23158 } else {
23159 /* Either last component exists, or there is some problem. */
23160 if (rip != NIL_INODE)
23161 r = EEXIST;
23162 else
23163 r = err_code;
23164 }
23165
23166 /* Return the directory inode and exit. */
23167 put_inode(rlast_dir_ptr);
23168 err_code = r;
23169 return(rip);
23170 }
23173 /*===========================================================================*
23174 * pipe_open *
23175 *===========================================================================*/
23176 PRIVATE int pipe_open(rip, bits, oflags)
23177 register struct inode *rip;
23178 register mode_t bits;
23179 register int oflags;
23180 {
23181 /* This function is called from common_open. It checks if
23182 * there is at least one reader/writer pair for the pipe, if not
23183 * it suspends the caller, otherwise it revives all other blocked
23184 * processes hanging on the pipe.
23185 */
23186
23187 if (find_filp(rip, bits & W_BIT ? R_BIT : W_BIT) == NIL_FILP) {
23188 if (oflags & O_NONBLOCK) {
23189 if (bits & W_BIT) return(ENXIO);
23190 } else
23191 suspend(XPOPEN); /* suspend caller */
23192 } else if (susp_count > 0) {/* revive blocked processes */
23193 release(rip, OPEN, susp_count);
23194 release(rip, CREAT, susp_count);
23195 }
23196 rip->i_pipe = I_PIPE;
23197
23198 return(OK);
23199 }
23202 /*===========================================================================*
23203 * do_mknod *
23204 *===========================================================================*/
23205 PUBLIC int do_mknod()
23206 {
23207 /* Perform the mknod(name, mode, addr) system call. */
23208
23209 register mode_t bits, mode_bits;
23210 struct inode *ip;
23211
23212 /* Only the super_user may make nodes other than fifos. */
23213 mode_bits = (mode_t) m.m1_i2; /* mode of the inode */
23214 if (!super_user && ((mode_bits & I_TYPE) != I_NAMED_PIPE)) return(EPERM);
23215 if (fetch_name(m.m1_p1, m.m1_i1, M1) != OK) return(err_code);
23216 bits = (mode_bits & I_TYPE) | (mode_bits & ALL_MODES & fp->fp_umask);
23217 ip = new_node(user_path, bits, (zone_t) m.m1_i3);
23218 put_inode(ip);
23219 return(err_code);
23220 }
23223 /*===========================================================================*
23224 * do_mkdir *
23225 *===========================================================================*/
23226 PUBLIC int do_mkdir()
23227 {
23228 /* Perform the mkdir(name, mode) system call. */
23229
23230 int r1, r2; /* status codes */
23231 ino_t dot, dotdot; /* inode numbers for . and .. */
23232 mode_t bits; /* mode bits for the new inode */
23233 char string[NAME_MAX]; /* last component of the new dir's path name */
23234 register struct inode *rip, *ldirp;
23235
23236 /* Check to see if it is possible to make another link in the parent dir. */
23237 if (fetch_name(name1, name1_length, M1) != OK) return(err_code);
23238 ldirp = last_dir(user_path, string); /* pointer to new dir's parent */
23239 if (ldirp == NIL_INODE) return(err_code);
23240 if ( (ldirp->i_nlinks & BYTE) >= LINK_MAX) {
23241 put_inode(ldirp); /* return parent */
23242 return(EMLINK);
23243 }
23244
23245 /* Next make the inode. If that fails, return error code. */
23246 bits = I_DIRECTORY | (mode & RWX_MODES & fp->fp_umask);
23247 rip = new_node(user_path, bits, (zone_t) 0);
23248 if (rip == NIL_INODE || err_code == EEXIST) {
23249 put_inode(rip); /* can't make dir: it already exists */
23250 put_inode(ldirp); /* return parent too */
23251 return(err_code);
23252 }
23253
23254 /* Get the inode numbers for . and .. to enter in the directory. */
23255 dotdot = ldirp->i_num; /* parent's inode number */
23256 dot = rip->i_num; /* inode number of the new dir itself */
23257
23258 /* Now make dir entries for . and .. unless the disk is completely full. */
23259 /* Use dot1 and dot2, so the mode of the directory isn't important. */
23260 rip->i_mode = bits; /* set mode */
23261 r1 = search_dir(rip, dot1, &dot, ENTER); /* enter . in the new dir */
23262 r2 = search_dir(rip, dot2, &dotdot, ENTER); /* enter .. in the new dir */
23263
23264 /* If both . and .. were successfully entered, increment the link counts. */
23265 if (r1 == OK && r2 == OK) {
23266 /* Normal case. It was possible to enter . and .. in the new dir. */
23267 rip->i_nlinks++; /* this accounts for . */
23268 ldirp->i_nlinks++; /* this accounts for .. */
23269 ldirp->i_dirt = DIRTY; /* mark parent's inode as dirty */
23270 } else {
23271 /* It was not possible to enter . or .. probably disk was full. */
23272 (void) search_dir(ldirp, string, (ino_t *) 0, DELETE);
23273 rip->i_nlinks--; /* undo the increment done in new_node() */
23274 }
23275 rip->i_dirt = DIRTY; /* either way, i_nlinks has changed */
23276
23277 put_inode(ldirp); /* return the inode of the parent dir */
23278 put_inode(rip); /* return the inode of the newly made dir */
23279 return(err_code); /* new_node() always sets 'err_code' */
23280 }
23283 /*===========================================================================*
23284 * do_close *
23285 *===========================================================================*/
23286 PUBLIC int do_close()
23287 {
23288 /* Perform the close(fd) system call. */
23289
23290 register struct filp *rfilp;
23291 register struct inode *rip;
23292 struct file_lock *flp;
23293 int rw, mode_word, major, task, lock_count;
23294 dev_t dev;
23295
23296 /* First locate the inode that belongs to the file descriptor. */
23297 if ( (rfilp = get_filp(fd)) == NIL_FILP) return(err_code);
23298 rip = rfilp->filp_ino; /* 'rip' points to the inode */
23299
23300 if (rfilp->filp_count - 1 == 0 && rfilp->filp_mode != FILP_CLOSED) {
23301 /* Check to see if the file is special. */
23302 mode_word = rip->i_mode & I_TYPE;
23303 if (mode_word == I_CHAR_SPECIAL || mode_word == I_BLOCK_SPECIAL) {
23304 dev = (dev_t) rip->i_zone[0];
23305 if (mode_word == I_BLOCK_SPECIAL) {
23306 /* Invalidate cache entries unless special is mounted
23307 * or ROOT
23308 */
23309 if (!mounted(rip)) {
23310 (void) do_sync(); /* purge cache */
23311 invalidate(dev);
23312 }
23313 }
23314 /* Use the dmap_close entry to do any special processing
23315 * required.
23316 */
23317 dev_mess.m_type = DEV_CLOSE;
23318 dev_mess.DEVICE = dev;
23319 major = (dev >> MAJOR) & BYTE; /* major device nr */
23320 task = dmap[major].dmap_task; /* device task nr */
23321 (*dmap[major].dmap_close)(task, &dev_mess);
23322 }
23323 }
23324
23325 /* If the inode being closed is a pipe, release everyone hanging on it. */
23326 if (rip->i_pipe == I_PIPE) {
23327 rw = (rfilp->filp_mode & R_BIT ? WRITE : READ);
23328 release(rip, rw, NR_PROCS);
23329 }
23330
23331 /* If a write has been done, the inode is already marked as DIRTY. */
23332 if (--rfilp->filp_count == 0) {
23333 if (rip->i_pipe == I_PIPE && rip->i_count > 1) {
23334 /* Save the file position in the i-node in case needed later.
23335 * The read and write positions are saved separately. The
23336 * last 3 zones in the i-node are not used for (named) pipes.
23337 */
23338 if (rfilp->filp_mode == R_BIT)
23339 rip->i_zone[V2_NR_DZONES+1] = (zone_t) rfilp->filp_pos;
23340 else
23341 rip->i_zone[V2_NR_DZONES+2] = (zone_t) rfilp->filp_pos;
23342 }
23343 put_inode(rip);
23344 }
23345
23346 fp->fp_cloexec &= ~(1L << fd); /* turn off close-on-exec bit */
23347 fp->fp_filp[fd] = NIL_FILP;
23348
23349 /* Check to see if the file is locked. If so, release all locks. */
23350 if (nr_locks == 0) return(OK);
23351 lock_count = nr_locks; /* save count of locks */
23352 for (flp = &file_lock[0]; flp < &file_lock[NR_LOCKS]; flp++) {
23353 if (flp->lock_type == 0) continue; /* slot not in use */
23354 if (flp->lock_inode == rip && flp->lock_pid == fp->fp_pid) {
23355 flp->lock_type = 0;
23356 nr_locks--;
23357 }
23358 }
23359 if (nr_locks < lock_count) lock_revive(); /* lock released */
23360 return(OK);
23361 }
23364 /*===========================================================================*
23365 * do_lseek *
23366 *===========================================================================*/
23367 PUBLIC int do_lseek()
23368 {
23369 /* Perform the lseek(ls_fd, offset, whence) system call. */
23370
23371 register struct filp *rfilp;
23372 register off_t pos;
23373
23374 /* Check to see if the file descriptor is valid. */
23375 if ( (rfilp = get_filp(ls_fd)) == NIL_FILP) return(err_code);
23376
23377 /* No lseek on pipes. */
23378 if (rfilp->filp_ino->i_pipe == I_PIPE) return(ESPIPE);
23379
23380 /* The value of 'whence' determines the start position to use. */
23381 switch(whence) {
23382 case 0: pos = 0; break;
23383 case 1: pos = rfilp->filp_pos; break;
23384 case 2: pos = rfilp->filp_ino->i_size; break;
23385 default: return(EINVAL);
23386 }
23387
23388 /* Check for overflow. */
23389 if (((long)offset > 0) && ((long)(pos + offset) < (long)pos)) return(EINVAL);
23390 if (((long)offset < 0) && ((long)(pos + offset) > (long)pos)) return(EINVAL);
23391 pos = pos + offset;
23392
23393 if (pos != rfilp->filp_pos)
23394 rfilp->filp_ino->i_seek = ISEEK; /* inhibit read ahead */
23395 rfilp->filp_pos = pos;
23396 reply_l1 = pos; /* insert the long into the output message */
23397 return(OK);
23398 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -