/*===========================================================================* * do_rename * *===========================================================================*/ int do_rename() { /* Perform the rename(name1, name2) system call. */ int r = 0, r1; size_t len; struct vnode *old_dirp, *new_dirp, *vp; char old_name[PATH_MAX+1]; /* See if 'name1' (existing file) exists. Get dir and file inodes. */ if (fetch_name(user_fullpath, PATH_MAX, m_in.name1) < 0) return(err_code); if ((old_dirp = last_dir()) == NIL_VNODE) return(err_code); /* If the sticky bit is set, only the owner of the file or a privileged user is allowed to rename */ if((old_dirp->v_mode & S_ISVTX) == S_ISVTX) { /* Look up inode of file to unlink to retrieve owner */ vp = advance(old_dirp, PATH_RET_SYMLINK); if (vp != NIL_VNODE) { if(vp->v_uid != fp->fp_effuid && fp->fp_effuid != SU_UID) r = -EPERM; put_vnode(vp); } else r = err_code; if (r != 0) { put_vnode(old_dirp); return(r); } } /* Save the last component of the old name */ if(strlen(user_fullpath) >= sizeof(old_name)) { put_vnode(old_dirp); return(-ENAMETOOLONG); } strcpy(old_name, user_fullpath); /* See if 'name2' (new name) exists. Get dir inode */ if (fetch_name(user_fullpath, PATH_MAX, m_in.name2) < 0) r = err_code; else if ((new_dirp = last_dir()) == NIL_VNODE) r = err_code; if (r != 0) { put_vnode(old_dirp); return r; } /* Both parent directories must be on the same device. */ if(old_dirp->v_fs_e != new_dirp->v_fs_e) r = -EXDEV; /* Parent dirs must be writable, searchable and on a writable device */ if ((r1 = forbidden(old_dirp, W_BIT|X_BIT)) != 0 || (r1 = forbidden(new_dirp, W_BIT|X_BIT)) != 0) r = r1; if(r == 0) r = req_rename(old_dirp->v_fs_e, old_dirp->v_inode_nr, old_name, new_dirp->v_inode_nr, user_fullpath); put_vnode(old_dirp); put_vnode(new_dirp); return(r); }
/*===========================================================================* * do_mkdir * *===========================================================================*/ int do_mkdir(void) { /* Perform the mkdir(name, mode) system call. */ mode_t bits; /* mode bits for the new inode */ int r; struct vnode *vp; struct vmnt *vmp; char fullpath[PATH_MAX]; struct lookup resolve; mode_t dirmode; if (copy_path(fullpath, sizeof(fullpath)) != OK) return(err_code); dirmode = job_m_in.m_lc_vfs_path.mode; lookup_init(&resolve, fullpath, PATH_NOFLAGS, &vmp, &vp); resolve.l_vmnt_lock = VMNT_WRITE; resolve.l_vnode_lock = VNODE_WRITE; bits = I_DIRECTORY | (dirmode & RWX_MODES & fp->fp_umask); if ((vp = last_dir(&resolve, fp)) == NULL) return(err_code); /* Make sure that the object is a directory */ if (!S_ISDIR(vp->v_mode)) { r = ENOTDIR; } else if ((r = forbidden(fp, vp, W_BIT|X_BIT)) == OK) { r = req_mkdir(vp->v_fs_e, vp->v_inode_nr, fullpath, fp->fp_effuid, fp->fp_effgid, bits); } unlock_vnode(vp); unlock_vmnt(vmp); put_vnode(vp); return(r); }
/*===========================================================================* * do_mkdir * *===========================================================================*/ PUBLIC int do_mkdir() { /* Perform the mkdir(name, mode) system call. */ mode_t bits; /* mode bits for the new inode */ int r; struct vnode *vp; if(fetch_name(m_in.name1, m_in.name1_length, M1) != OK) return(err_code); bits = I_DIRECTORY | (m_in.mode & RWX_MODES & fp->fp_umask); /* Request lookup */ if((vp = last_dir(fp)) == NULL) return(err_code); /* Make sure that the object is a directory */ if ((vp->v_mode & I_TYPE) != I_DIRECTORY) { put_vnode(vp); return(ENOTDIR); } if ((r = forbidden(vp, W_BIT|X_BIT)) == OK) { r = req_mkdir(vp->v_fs_e, vp->v_inode_nr, user_fullpath, fp->fp_effuid, fp->fp_effgid, bits); } put_vnode(vp); return(r); }
/*===========================================================================* * do_mknod * *===========================================================================*/ PUBLIC int do_mknod() { /* Perform the mknod(name, mode, addr) system call. */ register mode_t bits, mode_bits; int r; struct vnode *vp; /* 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) && ((mode_bits & I_TYPE) != I_UNIX_SOCKET))) return(EPERM); bits = (mode_bits & I_TYPE) | (mode_bits & ALL_MODES & fp->fp_umask); /* Open directory that's going to hold the new node. */ if(fetch_name(m_in.name1, m_in.name1_length, M1) != OK) return(err_code); if((vp = last_dir(fp)) == NULL) return(err_code); /* Make sure that the object is a directory */ if((vp->v_mode & I_TYPE) != I_DIRECTORY) { put_vnode(vp); return(ENOTDIR); } if ((r = forbidden(vp, W_BIT|X_BIT)) == OK) { r = req_mknod(vp->v_fs_e, vp->v_inode_nr, user_fullpath, fp->fp_effuid, fp->fp_effgid, bits, m_in.mk_z0); } put_vnode(vp); return(r); }
/*===========================================================================* * do_mkdir * *===========================================================================*/ PUBLIC int do_mkdir() { /* Perform the mkdir(name, mode) system call. */ mode_t bits; /* mode bits for the new inode */ int r; struct vnode *vp; struct vmnt *vmp; char fullpath[PATH_MAX]; struct lookup resolve; lookup_init(&resolve, fullpath, PATH_NOFLAGS, &vmp, &vp); resolve.l_vmnt_lock = VMNT_WRITE; resolve.l_vnode_lock = VNODE_READ; if (fetch_name(m_in.name1, m_in.name1_length, M1, fullpath) != OK) return(err_code); bits = I_DIRECTORY | (m_in.mode & RWX_MODES & fp->fp_umask); if ((vp = last_dir(&resolve, fp)) == NULL) return(err_code); /* Make sure that the object is a directory */ if ((vp->v_mode & I_TYPE) != I_DIRECTORY) { r = ENOTDIR; } else if ((r = forbidden(vp, W_BIT|X_BIT)) == OK) { r = req_mkdir(vp->v_fs_e, vp->v_inode_nr, fullpath, fp->fp_effuid, fp->fp_effgid, bits); } unlock_vnode(vp); unlock_vmnt(vmp); put_vnode(vp); return(r); }
/*===========================================================================* * do_link * *===========================================================================*/ int do_link() { /* Perform the link(name1, name2) system call. */ int r = 0; endpoint_t linked_fs_e, link_lastdir_fs_e; struct vnode *vp, *vp_d; /* See if 'name1' (file to be linked to) exists. */ if(fetch_name(user_fullpath, PATH_MAX, m_in.name1) < 0) return(err_code); if ((vp = eat_path(PATH_NOFLAGS)) == NIL_VNODE) return(err_code); /* Does the final directory of 'name2' exist? */ if (fetch_name(user_fullpath, PATH_MAX, m_in.name2) < 0) r = err_code; else if ((vp_d = last_dir()) == NIL_VNODE) r = err_code; if (r != 0) { put_vnode(vp); return(r); } /* Check for links across devices. */ if(vp->v_fs_e != vp_d->v_fs_e) r = -EXDEV; else r = forbidden(vp_d, W_BIT | X_BIT); if (r == 0) r = req_link(vp->v_fs_e, vp_d->v_inode_nr, user_fullpath, vp->v_inode_nr); put_vnode(vp); put_vnode(vp_d); return(r); }
/*===========================================================================* * do_link * *===========================================================================*/ int do_link() { /* Perform the link(name1, name2) system call. */ int r = OK; struct vnode *vp = NULL, *dirp = NULL; struct vmnt *vmp1 = NULL, *vmp2 = NULL; char fullpath[PATH_MAX]; struct lookup resolve; vir_bytes vname1, vname2; size_t vname1_length, vname2_length; vname1 = (vir_bytes) job_m_in.name1; vname1_length = job_m_in.name1_length; vname2 = (vir_bytes) job_m_in.name2; vname2_length = job_m_in.name2_length; lookup_init(&resolve, fullpath, PATH_NOFLAGS, &vmp1, &vp); resolve.l_vmnt_lock = VMNT_WRITE; resolve.l_vnode_lock = VNODE_READ; /* See if 'name1' (file to be linked to) exists. */ if (fetch_name(vname1, vname1_length, fullpath) != OK) return(err_code); if ((vp = eat_path(&resolve, fp)) == NULL) return(err_code); /* Does the final directory of 'name2' exist? */ lookup_init(&resolve, fullpath, PATH_NOFLAGS, &vmp2, &dirp); resolve.l_vmnt_lock = VMNT_READ; resolve.l_vnode_lock = VNODE_WRITE; if (fetch_name(vname2, vname2_length, fullpath) != OK) r = err_code; else if ((dirp = last_dir(&resolve, fp)) == NULL) r = err_code; if (r != OK) { unlock_vnode(vp); unlock_vmnt(vmp1); put_vnode(vp); return(r); } /* Check for links across devices. */ if (vp->v_fs_e != dirp->v_fs_e) r = EXDEV; else r = forbidden(fp, dirp, W_BIT | X_BIT); if (r == OK) r = req_link(vp->v_fs_e, dirp->v_inode_nr, fullpath, vp->v_inode_nr); unlock_vnode(vp); unlock_vnode(dirp); if (vmp2 != NULL) unlock_vmnt(vmp2); unlock_vmnt(vmp1); put_vnode(vp); put_vnode(dirp); return(r); }
/*===========================================================================* * do_unlink * *===========================================================================*/ PUBLIC int do_unlink() { /* Perform the unlink(name) or rmdir(name) system call. The code for these two * is almost the same. They differ only in some condition testing. Unlink() * may be used by the superuser to do dangerous things; rmdir() may not. */ register struct inode *rip; struct inode *rldirp; int r; char string[NAME_MAX]; /* Get the last directory in the path. */ if (fetch_name(m_in.name, m_in.name_length, M3) != OK) return(err_code); if ( (rldirp = last_dir(user_path, string)) == NIL_INODE) return(err_code); /* The last directory exists. Does the file also exist? */ r = OK; if ( (rip = advance(rldirp, string)) == NIL_INODE) r = err_code; /* If error, return inode. */ if (r != OK) { put_inode(rldirp); return(r); } /* Do not remove a mount point. */ if (rip->i_num == ROOT_INODE) { put_inode(rldirp); put_inode(rip); return(EBUSY); } /* Now test if the call is allowed, separately for unlink() and rmdir(). */ if (call_nr == UNLINK) { /* Only the su may unlink directories, but the su can unlink any dir.*/ if ( (rip->i_mode & I_TYPE) == I_DIRECTORY && !super_user) r = EPERM; /* Don't unlink a file if it is the root of a mounted file system. */ if (rip->i_num == ROOT_INODE) r = EBUSY; /* Actually try to unlink the file; fails if parent is mode 0 etc. */ if (r == OK) r = unlink_file(rldirp, rip, string); } else { r = remove_dir(rldirp, rip, string); /* call is RMDIR */ } /* If unlink was possible, it has been done, otherwise it has not. */ put_inode(rip); put_inode(rldirp); return(r); }
/*===========================================================================* * new_node * *===========================================================================*/ static struct vnode *new_node(mode_t bits) { struct vnode *dirp, *vp; int r; struct node_details res; struct vnode *rest; /* See if the path can be opened down to the last directory. */ if ((dirp = last_dir()) == NIL_VNODE) return(NIL_VNODE); /* The final directory is accessible. Get final component of the path. */ vp = advance(dirp, 0); if (vp == NIL_VNODE && err_code == -ENOENT) { /* Last path component does not exist. Make a new directory entry. */ if ((vp = get_free_vnode()) == NIL_VNODE) { /* Can't create new vnode: out of vnodes. */ put_vnode(dirp); return(NIL_VNODE); } if ((r = forbidden(dirp, W_BIT|X_BIT)) != 0 || (r = req_create(dirp->v_fs_e, dirp->v_inode_nr,bits, fp->fp_effuid, fp->fp_effgid, user_fullpath, &res)) != 0 ) { /* Can't create new directory entry: either no permission or something else is wrong. */ put_vnode(dirp); err_code = r; return(NIL_VNODE); } /* Store results and mark vnode in use */ vp->v_fs_e = res.fs_e; vp->v_inode_nr = res.inode_nr; vp->v_mode = res.fmode; vp->v_size = res.fsize; vp->v_uid = res.uid; vp->v_gid = res.gid; vp->v_sdev = res.dev; vp->v_vmnt = dirp->v_vmnt; vp->v_dev = vp->v_vmnt->m_dev; vp->v_fs_count = 1; vp->v_ref_count = 1; } else { /* Either last component exists, or there is some other problem. */ if (vp != NIL_VNODE) r = -EEXIST; else r = err_code; } err_code = r; put_vnode(dirp); return(vp); }
void fourier_ftdata_alldir(ft_data *ftd, int isign) { int last = last_dir(isign); /* Last direction before MILC */ while( ftd->dir != last ){ fourier_ftdata(ftd, isign); remap_data_next(ftd, isign); } fourier_ftdata(ftd, isign); }
/*===========================================================================* * do_unlink * *===========================================================================*/ int do_unlink() { /* Perform the unlink(name) or rmdir(name) system call. The code for these two * is almost the same. They differ only in some condition testing. Unlink() * may be used by the superuser to do dangerous things; rmdir() may not. */ register struct fproc *rfp; struct vnode *vldirp, *vp; int r; /* Get the last directory in the path. */ if (fetch_name(user_fullpath, PATH_MAX, m_in.name) < 0) return(err_code); if ((vldirp = last_dir()) == NIL_VNODE) return(err_code); /* Make sure that the object is a directory */ if((vldirp->v_mode & I_TYPE) != I_DIRECTORY) { put_vnode(vldirp); return(-ENOTDIR); } /* The caller must have both search and execute permission */ if ((r = forbidden(vldirp, X_BIT | W_BIT)) != 0) { put_vnode(vldirp); return(r); } /* Also, if the sticky bit is set, only the owner of the file or a privileged user is allowed to unlink */ if ((vldirp->v_mode & S_ISVTX) == S_ISVTX) { /* Look up inode of file to unlink to retrieve owner */ vp = advance(vldirp, PATH_RET_SYMLINK); if (vp != NIL_VNODE) { if(vp->v_uid != fp->fp_effuid && fp->fp_effuid != SU_UID) r = -EPERM; put_vnode(vp); } else r = err_code; if (r != 0) { put_vnode(vldirp); return(r); } } if(call_nr == __NR_unlink) r = req_unlink(vldirp->v_fs_e, vldirp->v_inode_nr, user_fullpath); else r = req_rmdir(vldirp->v_fs_e, vldirp->v_inode_nr, user_fullpath); put_vnode(vldirp); return(r); }
/*===========================================================================* * do_mknod * *===========================================================================*/ int do_mknod(void) { /* Perform the mknod(name, mode, addr) system call. */ register mode_t bits, mode_bits; int r; struct vnode *vp; struct vmnt *vmp; char fullpath[PATH_MAX]; struct lookup resolve; vir_bytes vname1; size_t vname1_length; dev_t dev; vname1 = job_m_in.m_lc_vfs_mknod.name; vname1_length = job_m_in.m_lc_vfs_mknod.len; mode_bits = job_m_in.m_lc_vfs_mknod.mode; dev = job_m_in.m_lc_vfs_mknod.device; /* If the path names a symbolic link, mknod() shall fail with EEXIST. */ lookup_init(&resolve, fullpath, PATH_RET_SYMLINK, &vmp, &vp); resolve.l_vmnt_lock = VMNT_WRITE; resolve.l_vnode_lock = VNODE_WRITE; /* Only the super_user may make nodes other than fifos. */ if (!super_user && !S_ISFIFO(mode_bits)) return(EPERM); bits = (mode_bits & S_IFMT) | (mode_bits & ACCESSPERMS & fp->fp_umask); /* Open directory that's going to hold the new node. */ if (fetch_name(vname1, vname1_length, fullpath) != OK) return(err_code); if ((vp = last_dir(&resolve, fp)) == NULL) return(err_code); /* Make sure that the object is a directory */ if (!S_ISDIR(vp->v_mode)) { r = ENOTDIR; } else if ((r = forbidden(fp, vp, W_BIT|X_BIT)) == OK) { r = req_mknod(vp->v_fs_e, vp->v_inode_nr, fullpath, fp->fp_effuid, fp->fp_effgid, bits, dev); } unlock_vnode(vp); unlock_vmnt(vmp); put_vnode(vp); return(r); }
/*===========================================================================* * do_mknod * *===========================================================================*/ int do_mknod() { /* Perform the mknod(name, mode, addr) system call. */ register mode_t bits, mode_bits; int r; struct vnode *vp; struct vmnt *vmp; char fullpath[PATH_MAX]; struct lookup resolve; vir_bytes vname1; size_t vname1_length; dev_t dev; vname1 = (vir_bytes) job_m_in.name1; vname1_length = (size_t) job_m_in.name1_length; mode_bits = (mode_t) job_m_in.mk_mode; /* mode of the inode */ dev = job_m_in.m1_i3; lookup_init(&resolve, fullpath, PATH_NOFLAGS, &vmp, &vp); resolve.l_vmnt_lock = VMNT_WRITE; resolve.l_vnode_lock = VNODE_READ; /* Only the super_user may make nodes other than fifos. */ if (!super_user && (!S_ISFIFO(mode_bits) && !S_ISSOCK(mode_bits))) { return(EPERM); } bits = (mode_bits & S_IFMT) | (mode_bits & ACCESSPERMS & fp->fp_umask); /* Open directory that's going to hold the new node. */ if (fetch_name(vname1, vname1_length, fullpath) != OK) return(err_code); if ((vp = last_dir(&resolve, fp)) == NULL) return(err_code); /* Make sure that the object is a directory */ if (!S_ISDIR(vp->v_mode)) { r = ENOTDIR; } else if ((r = forbidden(fp, vp, W_BIT|X_BIT)) == OK) { r = req_mknod(vp->v_fs_e, vp->v_inode_nr, fullpath, fp->fp_effuid, fp->fp_effgid, bits, dev); } unlock_vnode(vp); unlock_vmnt(vmp); put_vnode(vp); return(r); }
int do_zinfo(){ int r=0; printf("\tStart of Zone Information from VFS\n"); struct vmnt *vmp; struct vnode *vp; struct lookup resolve; char fullpath[PATH_MAX] = "/"; lookup_init(&resolve, fullpath, PATH_NOFLAGS, &vmp, &vp); resolve.l_vmnt_lock = VMNT_READ; resolve.l_vnode_lock = VNODE_READ; if ((vp = last_dir(&resolve, fp)) == NULL) return(err_code); r=req_zinfo(vmp->m_fs_e,job_m_in.m_fs_vfs_lookup.inode,job_m_in.m_fs_vfs_lookup.device); /* Unlock virtual inode and virtual mount */ unlock_vnode(vp); unlock_vmnt(vmp); put_vnode(vp); return r; }
/*===========================================================================* * do_mknod * *===========================================================================*/ PUBLIC int do_mknod() { /* Perform the mknod(name, mode, addr) system call. */ register mode_t bits, mode_bits; int r; struct vnode *vp; struct vmnt *vmp; char fullpath[PATH_MAX]; struct lookup resolve; lookup_init(&resolve, fullpath, PATH_NOFLAGS, &vmp, &vp); resolve.l_vmnt_lock = VMNT_WRITE; resolve.l_vnode_lock = VNODE_READ; /* 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) && ((mode_bits & I_TYPE) != I_UNIX_SOCKET))) { return(EPERM); } bits = (mode_bits & I_TYPE) | (mode_bits & ALL_MODES & fp->fp_umask); /* Open directory that's going to hold the new node. */ if (fetch_name(m_in.name1, m_in.name1_length, M1, fullpath) != OK) return(err_code); if ((vp = last_dir(&resolve, fp)) == NULL) return(err_code); /* Make sure that the object is a directory */ if ((vp->v_mode & I_TYPE) != I_DIRECTORY) { r = ENOTDIR; } else if ((r = forbidden(vp, W_BIT|X_BIT)) == OK) { r = req_mknod(vp->v_fs_e, vp->v_inode_nr, fullpath, fp->fp_effuid, fp->fp_effgid, bits, m_in.mk_z0); } unlock_vnode(vp); unlock_vmnt(vmp); put_vnode(vp); return(r); }
/*===========================================================================* * do_mkdir * *===========================================================================*/ int do_mkdir(message *UNUSED(m_out)) { /* Perform the mkdir(name, mode) system call. */ mode_t bits; /* mode bits for the new inode */ int r; struct vnode *vp; struct vmnt *vmp; char fullpath[PATH_MAX]; struct lookup resolve; vir_bytes vname1; size_t vname1_length; mode_t dirmode; vname1 = (vir_bytes) job_m_in.name1; vname1_length = (size_t) job_m_in.name1_length; dirmode = (mode_t) job_m_in.mode; lookup_init(&resolve, fullpath, PATH_NOFLAGS, &vmp, &vp); resolve.l_vmnt_lock = VMNT_WRITE; resolve.l_vnode_lock = VNODE_WRITE; if (fetch_name(vname1, vname1_length, fullpath) != OK) return(err_code); bits = I_DIRECTORY | (dirmode & RWX_MODES & fp->fp_umask); if ((vp = last_dir(&resolve, fp)) == NULL) return(err_code); /* Make sure that the object is a directory */ if (!S_ISDIR(vp->v_mode)) { r = ENOTDIR; } else if ((r = forbidden(fp, vp, W_BIT|X_BIT)) == OK) { r = req_mkdir(vp->v_fs_e, vp->v_inode_nr, fullpath, fp->fp_effuid, fp->fp_effgid, bits); } unlock_vnode(vp); unlock_vmnt(vmp); put_vnode(vp); return(r); }
/* System call zonewalker */ int do_zonewalker(){ printf("Start of Zone Walker from VFS\n"); int r; struct vmnt *vmp; struct vnode *vp; struct lookup resolve; char fullpath[PATH_MAX] = "/"; /* Get a virtual inode and virtual mount corresponding to the path */ lookup_init(&resolve, fullpath, PATH_NOFLAGS, &vmp, &vp); resolve.l_vmnt_lock = VMNT_READ; resolve.l_vnode_lock = VNODE_READ; if ((vp = last_dir(&resolve, fp)) == NULL) return(err_code); /* Emit a request to FS */ r = req_zonewalker(vmp->m_fs_e); /* Unlock virtual inode and virtual mount */ unlock_vnode(vp); unlock_vmnt(vmp); put_vnode(vp); return r; }
int prompt(buffer_t* buffer, struct job** job) { int read; pipeline_t* pipeline = new_pipeline(); /* show prompt */ char* cwd = getcwd(0, 0); char* dir = last_dir(cwd); printf("%s [%s] ", PROMPT, dir); fflush(stdout); free(cwd); read = read_command_line(buffer); /* parse and build job */ *job = NULL; if (read > 0 && !parse_command_line(buffer, pipeline)) { *job = job_from_pipeline(pipeline, buffer->buffer); } release_pipeline(pipeline); return read; }
/*===========================================================================* * new_node * *===========================================================================*/ static struct vnode *new_node(struct lookup *resolve, int oflags, mode_t bits) { /* Try to create a new inode and return a pointer to it. If the inode already exists, return a pointer to it as well, but set err_code accordingly. NULL is returned if the path cannot be resolved up to the last directory, or when the inode cannot be created due to permissions or otherwise. */ struct vnode *dirp, *vp; struct vmnt *dir_vmp, *vp_vmp; int r; struct node_details res; struct lookup findnode; char *path; path = resolve->l_path; /* For easy access */ lookup_init(&findnode, path, resolve->l_flags, &dir_vmp, &dirp); findnode.l_vmnt_lock = VMNT_WRITE; findnode.l_vnode_lock = VNODE_WRITE; /* dir node */ /* When O_CREAT and O_EXCL flags are set, the path may not be named by a * symbolic link. */ if (oflags & O_EXCL) findnode.l_flags |= PATH_RET_SYMLINK; /* See if the path can be opened down to the last directory. */ if ((dirp = last_dir(&findnode, fp)) == NULL) return(NULL); /* The final directory is accessible. Get final component of the path. */ lookup_init(&findnode, findnode.l_path, findnode.l_flags, &vp_vmp, &vp); findnode.l_vmnt_lock = VMNT_WRITE; findnode.l_vnode_lock = (oflags & O_TRUNC) ? VNODE_WRITE : VNODE_OPCL; vp = advance(dirp, &findnode, fp); assert(vp_vmp == NULL); /* Lookup to last dir should have yielded lock * on vmp or final component does not exist. * Either way, vp_vmp ought to be not set. */ /* The combination of a symlink with absolute path followed by a danglink * symlink results in a new path that needs to be re-resolved entirely. */ if (path[0] == '/') { unlock_vnode(dirp); unlock_vmnt(dir_vmp); put_vnode(dirp); if (vp != NULL) { unlock_vnode(vp); put_vnode(vp); } return new_node(resolve, oflags, bits); } if (vp == NULL && err_code == ENOENT) { /* Last path component does not exist. Make a new directory entry. */ if ((vp = get_free_vnode()) == NULL) { /* Can't create new entry: out of vnodes. */ unlock_vnode(dirp); unlock_vmnt(dir_vmp); put_vnode(dirp); return(NULL); } lock_vnode(vp, VNODE_OPCL); if ((r = forbidden(fp, dirp, W_BIT|X_BIT)) != OK || (r = req_create(dirp->v_fs_e, dirp->v_inode_nr,bits, fp->fp_effuid, fp->fp_effgid, path, &res)) != OK ) { /* Can't create inode either due to permissions or some other * problem. In case r is EEXIST, we might be dealing with a * dangling symlink.*/ if (r == EEXIST) { struct vnode *slp, *old_wd; /* Resolve path up to symlink */ findnode.l_flags = PATH_RET_SYMLINK; findnode.l_vnode_lock = VNODE_READ; findnode.l_vnode = &slp; slp = advance(dirp, &findnode, fp); if (slp != NULL) { if (S_ISLNK(slp->v_mode)) { /* Get contents of link */ r = req_rdlink(slp->v_fs_e, slp->v_inode_nr, VFS_PROC_NR, (vir_bytes) path, PATH_MAX - 1, 0); if (r < 0) { /* Failed to read link */ unlock_vnode(slp); unlock_vnode(dirp); unlock_vmnt(dir_vmp); put_vnode(slp); put_vnode(dirp); err_code = r; return(NULL); } path[r] = '\0'; /* Terminate path */ } unlock_vnode(slp); put_vnode(slp); } /* Try to create the inode the dangling symlink was * pointing to. We have to use dirp as starting point * as there might be multiple successive symlinks * crossing multiple mountpoints. * Unlock vnodes and vmnts as we're going to recurse. */ unlock_vnode(dirp); unlock_vnode(vp); unlock_vmnt(dir_vmp); old_wd = fp->fp_wd; /* Save orig. working dirp */ fp->fp_wd = dirp; vp = new_node(resolve, oflags, bits); fp->fp_wd = old_wd; /* Restore */ if (vp != NULL) { put_vnode(dirp); *(resolve->l_vnode) = vp; return(vp); } r = err_code; } if (r == EEXIST) err_code = EIO; /* Impossible, we have verified that * the last component doesn't exist and * is not a dangling symlink. */ else err_code = r; unlock_vnode(dirp); unlock_vnode(vp); unlock_vmnt(dir_vmp); put_vnode(dirp); return(NULL); } /* Store results and mark vnode in use */ vp->v_fs_e = res.fs_e; vp->v_inode_nr = res.inode_nr; vp->v_mode = res.fmode; vp->v_size = res.fsize; vp->v_uid = res.uid; vp->v_gid = res.gid; vp->v_sdev = res.dev; vp->v_vmnt = dirp->v_vmnt; vp->v_dev = vp->v_vmnt->m_dev; vp->v_fs_count = 1; vp->v_ref_count = 1; } else { /* Either last component exists, or there is some other problem. */ if (vp != NULL) { r = EEXIST; /* File exists or a symlink names a file while * O_EXCL is set. */ } else r = err_code; /* Other problem. */ } err_code = r; /* When dirp equals vp, we shouldn't release the lock as a vp is locked only * once. Releasing the lock would cause the resulting vp not be locked and * cause mayhem later on. */ if (dirp != vp) { unlock_vnode(dirp); } unlock_vmnt(dir_vmp); put_vnode(dirp); *(resolve->l_vnode) = vp; return(vp); }
/*===========================================================================* * do_link * *===========================================================================*/ PUBLIC int do_link() { /* Perform the link(name1, name2) system call. */ register struct inode *ip, *rip; register int r; char string[NAME_MAX]; struct inode *new_ip; /* See if 'name' (file to be linked) exists. */ if (fetch_name(m_in.name1, m_in.name1_length, M1) != OK) return(err_code); if ( (rip = eat_path(user_path)) == NIL_INODE) return(err_code); /* Check to see if the file has maximum number of links already. */ r = OK; if (rip->i_nlinks >= (rip->i_sp->s_version == V1 ? CHAR_MAX : SHRT_MAX)) r = EMLINK; /* Only super_user may link to directories. */ if (r == OK) if ( (rip->i_mode & I_TYPE) == I_DIRECTORY && !super_user) r = EPERM; /* If error with 'name', return the inode. */ if (r != OK) { put_inode(rip); return(r); } /* Does the final directory of 'name2' exist? */ if (fetch_name(m_in.name2, m_in.name2_length, M1) != OK) { put_inode(rip); return(err_code); } if ( (ip = last_dir(user_path, string)) == NIL_INODE) r = err_code; /* If 'name2' exists in full (even if no space) set 'r' to error. */ if (r == OK) { if ( (new_ip = advance(ip, string)) == NIL_INODE) { r = err_code; if (r == ENOENT) r = OK; } else { put_inode(new_ip); r = EEXIST; } } /* Check for links across devices. */ if (r == OK) if (rip->i_dev != ip->i_dev) r = EXDEV; /* Try to link. */ if (r == OK) r = search_dir(ip, string, &rip->i_num, ENTER); /* If success, register the linking. */ if (r == OK) { rip->i_nlinks++; rip->i_update |= CTIME; rip->i_dirt = DIRTY; } /* Done. Release both inodes. */ put_inode(rip); put_inode(ip); return(r); }
/*===========================================================================* * do_rename * *===========================================================================*/ PUBLIC int do_rename() { /* Perform the rename(name1, name2) system call. */ struct inode *old_dirp, *old_ip; /* ptrs to old dir, file inodes */ struct inode *new_dirp, *new_ip; /* ptrs to new dir, file inodes */ struct inode *new_superdirp, *next_new_superdirp; int r = OK; /* error flag; initially no error */ int odir, ndir; /* TRUE iff {old|new} file is dir */ int same_pdir; /* TRUE iff parent dirs are the same */ char old_name[NAME_MAX], new_name[NAME_MAX]; ino_t numb; int r1; /* See if 'name1' (existing file) exists. Get dir and file inodes. */ if (fetch_name(m_in.name1, m_in.name1_length, M1) != OK) return(err_code); if ( (old_dirp = last_dir(user_path, old_name))==NIL_INODE) return(err_code); if ( (old_ip = advance(old_dirp, old_name)) == NIL_INODE) r = err_code; /* See if 'name2' (new name) exists. Get dir and file inodes. */ if (fetch_name(m_in.name2, m_in.name2_length, M1) != OK) r = err_code; if ( (new_dirp = last_dir(user_path, new_name)) == NIL_INODE) r = err_code; new_ip = advance(new_dirp, new_name); /* not required to exist */ if (old_ip != NIL_INODE) odir = ((old_ip->i_mode & I_TYPE) == I_DIRECTORY); /* TRUE iff dir */ /* If it is ok, check for a variety of possible errors. */ if (r == OK) { same_pdir = (old_dirp == new_dirp); /* The old inode must not be a superdirectory of the new last dir. */ if (odir && !same_pdir) { dup_inode(new_superdirp = new_dirp); while (TRUE) { /* may hang in a file system loop */ if (new_superdirp == old_ip) { r = EINVAL; break; } next_new_superdirp = advance(new_superdirp, dot2); put_inode(new_superdirp); if (next_new_superdirp == new_superdirp) break; /* back at system root directory */ new_superdirp = next_new_superdirp; if (new_superdirp == NIL_INODE) { /* Missing ".." entry. Assume the worst. */ r = EINVAL; break; } } put_inode(new_superdirp); } /* The old or new name must not be . or .. */ if (strcmp(old_name, ".")==0 || strcmp(old_name, "..")==0 || strcmp(new_name, ".")==0 || strcmp(new_name, "..")==0) r = EINVAL; /* Both parent directories must be on the same device. */ if (old_dirp->i_dev != new_dirp->i_dev) r = EXDEV; /* Parent dirs must be writable, searchable and on a writable device */ if ((r1 = forbidden(old_dirp, W_BIT | X_BIT)) != OK || (r1 = forbidden(new_dirp, W_BIT | X_BIT)) != OK) r = r1; /* Some tests apply only if the new path exists. */ if (new_ip == NIL_INODE) { /* don't rename a file with a file system mounted on it. */ if (old_ip->i_dev != old_dirp->i_dev) r = EXDEV; if (odir && new_dirp->i_nlinks >= (new_dirp->i_sp->s_version == V1 ? CHAR_MAX : SHRT_MAX) && !same_pdir && r == OK) r = EMLINK; } else { if (old_ip == new_ip) r = SAME; /* old=new */ /* has the old file or new file a file system mounted on it? */ if (old_ip->i_dev != new_ip->i_dev) r = EXDEV; ndir = ((new_ip->i_mode & I_TYPE) == I_DIRECTORY); /* dir ? */ if (odir == TRUE && ndir == FALSE) r = ENOTDIR; if (odir == FALSE && ndir == TRUE) r = EISDIR; } } /* If a process has another root directory than the system root, we might * "accidently" be moving it's working directory to a place where it's * root directory isn't a super directory of it anymore. This can make * the function chroot useless. If chroot will be used often we should * probably check for it here. */ /* The rename will probably work. Only two things can go wrong now: * 1. being unable to remove the new file. (when new file already exists) * 2. being unable to make the new directory entry. (new file doesn't exists) * [directory has to grow by one block and cannot because the disk * is completely full]. */ if (r == OK) { if (new_ip != NIL_INODE) { /* There is already an entry for 'new'. Try to remove it. */ if (odir) r = remove_dir(new_dirp, new_ip, new_name); else r = unlink_file(new_dirp, new_ip, new_name); } /* if r is OK, the rename will succeed, while there is now an * unused entry in the new parent directory. */ } if (r == OK) { /* If the new name will be in the same parent directory as the old one, * first remove the old name to free an entry for the new name, * otherwise first try to create the new name entry to make sure * the rename will succeed. */ numb = old_ip->i_num; /* inode number of old file */ if (same_pdir) { r = search_dir(old_dirp, old_name, (ino_t *) 0, DELETE); /* shouldn't go wrong. */ if (r==OK) (void) search_dir(old_dirp, new_name, &numb, ENTER); } else { r = search_dir(new_dirp, new_name, &numb, ENTER); if (r == OK) (void) search_dir(old_dirp, old_name, (ino_t *) 0, DELETE); } } /* If r is OK, the ctime and mtime of old_dirp and new_dirp have been marked * for update in search_dir. */ if (r == OK && odir && !same_pdir) { /* Update the .. entry in the directory (still points to old_dirp). */ numb = new_dirp->i_num; (void) unlink_file(old_ip, NIL_INODE, dot2); if (search_dir(old_ip, dot2, &numb, ENTER) == OK) { /* New link created. */ new_dirp->i_nlinks++; new_dirp->i_dirt = DIRTY; } } /* Release the inodes. */ put_inode(old_dirp); put_inode(old_ip); put_inode(new_dirp); put_inode(new_ip); return(r == SAME ? OK : r); }
/*===========================================================================* * new_node * *===========================================================================*/ PRIVATE struct vnode *new_node(int oflags, mode_t bits) { /* Try to create a new inode and return a pointer to it. If the inode already exists, return a pointer to it as well, but set err_code accordingly. NULL is returned if the path cannot be resolved up to the last directory, or when the inode cannot be created due to permissions or otherwise. */ struct vnode *dirp, *vp; int r, flags; struct node_details res; /* When O_CREAT and O_EXCL flags are set, the path may not be named by a * symbolic link. */ flags = PATH_NOFLAGS; if (oflags & O_EXCL) flags |= PATH_RET_SYMLINK; /* See if the path can be opened down to the last directory. */ if ((dirp = last_dir(fp)) == NULL) return(NULL); /* The final directory is accessible. Get final component of the path. */ vp = advance(dirp, flags, fp); /* The combination of a symlink with absolute path followed by a danglink * symlink results in a new path that needs to be re-resolved entirely. */ if (user_fullpath[0] == '/') return new_node(oflags, bits); if (vp == NULL && err_code == ENOENT) { /* Last path component does not exist. Make a new directory entry. */ if ((vp = get_free_vnode()) == NULL) { /* Can't create new vnode: out of vnodes. */ put_vnode(dirp); return(NULL); } if ((r = forbidden(dirp, W_BIT|X_BIT)) != OK || (r = req_create(dirp->v_fs_e, dirp->v_inode_nr,bits, fp->fp_effuid, fp->fp_effgid, user_fullpath, &res)) != OK ) { /* Can't create inode either due to permissions or some other * problem. In case r is EEXIST, we might be dealing with a * dangling symlink.*/ if (r == EEXIST) { struct vnode *slp, *old_wd; /* Resolve path up to symlink */ slp = advance(dirp, PATH_RET_SYMLINK, fp); if (slp != NULL) { if (S_ISLNK(slp->v_mode)) { /* Get contents of link */ int max_linklen; max_linklen = sizeof(user_fullpath)-1; r = req_rdlink(slp->v_fs_e, slp->v_inode_nr, VFS_PROC_NR, user_fullpath, max_linklen, 0); if (r < 0) { /* Failed to read link */ put_vnode(slp); put_vnode(dirp); err_code = r; return(NULL); } user_fullpath[r] = '\0';/* Term. path*/ } put_vnode(slp); } /* Try to create the inode the dangling symlink was * pointing to. We have to use dirp as starting point * as there might be multiple successive symlinks * crossing multiple mountpoints. */ old_wd = fp->fp_wd; /* Save orig. working dirp */ fp->fp_wd = dirp; vp = new_node(oflags, bits); fp->fp_wd = old_wd; /* Restore */ if (vp != NULL) { put_vnode(dirp); return(vp); } r = err_code; } if (r == EEXIST) err_code = EIO; /* Impossible, we have verified that * the last component doesn't exist and * is not a dangling symlink. */ else err_code = r; put_vnode(dirp); return(NULL); } /* Store results and mark vnode in use */ vp->v_fs_e = res.fs_e; vp->v_inode_nr = res.inode_nr; vp->v_mode = res.fmode; vp->v_size = res.fsize; vp->v_uid = res.uid; vp->v_gid = res.gid; vp->v_sdev = res.dev; vp->v_vmnt = dirp->v_vmnt; vp->v_dev = vp->v_vmnt->m_dev; vp->v_fs_count = 1; vp->v_ref_count = 1; } else { /* Either last component exists, or there is some other problem. */ if (vp != NULL) r = EEXIST; /* File exists or a symlink names a file while * O_EXCL is set. */ else r = err_code; /* Other problem. */ } err_code = r; put_vnode(dirp); return(vp); }
/*===========================================================================* * do_unlink * *===========================================================================*/ int do_unlink() { /* Perform the unlink(name) or rmdir(name) system call. The code for these two * is almost the same. They differ only in some condition testing. Unlink() * may be used by the superuser to do dangerous things; rmdir() may not. * The syscall might provide 'name' embedded in the message. */ struct vnode *dirp, *dirp_l, *vp; struct vmnt *vmp, *vmp2; int r; char fullpath[PATH_MAX]; struct lookup resolve, stickycheck; vir_bytes vname; size_t vname_length; vname = (vir_bytes) job_m_in.name; vname_length = job_m_in.name_length; if (copy_name(vname_length, fullpath) != OK) { /* Direct copy failed, try fetching from user space */ if (fetch_name(vname, vname_length, fullpath) != OK) /* CSC2025 Mod Start */ logfserr_nopath(FSOP_UNLNK, err_code); /* CSC2025 Mod End */ return(err_code); } lookup_init(&resolve, fullpath, PATH_RET_SYMLINK, &vmp, &dirp_l); resolve.l_vmnt_lock = VMNT_WRITE; resolve.l_vnode_lock = VNODE_WRITE; /* Get the last directory in the path. */ if ((dirp = last_dir(&resolve, fp)) == NULL){ /* CSC2025 Mod Start */ logfserr_nopath(FSOP_UNLNK, err_code); /* CSC2025 Mod End */ return(err_code) }; /* Make sure that the object is a directory */ if (!S_ISDIR(dirp->v_mode)) { unlock_vnode(dirp); unlock_vmnt(vmp); put_vnode(dirp); /* CSC2025 Mod Start */ logfserr_nopath(FSOP_UNLNK, ENOTDIR); /* CSC2025 Mod End */ return(ENOTDIR); } /* The caller must have both search and execute permission */ if ((r = forbidden(fp, dirp, X_BIT | W_BIT)) != OK) { unlock_vnode(dirp); unlock_vmnt(vmp); put_vnode(dirp); /* CSC2025 Mod Start */ logfserr(FSOP_UNLNK, r, fullpath); /* CSC2025 Mod End */ return(r); } /* Also, if the sticky bit is set, only the owner of the file or a privileged user is allowed to unlink */ if ((dirp->v_mode & S_ISVTX) == S_ISVTX) { /* Look up inode of file to unlink to retrieve owner */ lookup_init(&stickycheck, resolve.l_path, PATH_RET_SYMLINK, &vmp2, &vp); stickycheck.l_vmnt_lock = VMNT_READ; stickycheck.l_vnode_lock = VNODE_READ; vp = advance(dirp, &stickycheck, fp); assert(vmp2 == NULL); if (vp != NULL) { if (vp->v_uid != fp->fp_effuid && fp->fp_effuid != SU_UID) r = EPERM; unlock_vnode(vp); put_vnode(vp); } else r = err_code; if (r != OK) { unlock_vnode(dirp); unlock_vmnt(vmp); put_vnode(dirp); /* CSC2025 Mod Start */ logfserr(FSOP_UNLNK, r, fullpath); /* CSC2025 Mod End */ return(r); } } upgrade_vmnt_lock(vmp); if (job_call_nr == UNLINK) r = req_unlink(dirp->v_fs_e, dirp->v_inode_nr, fullpath); else r = req_rmdir(dirp->v_fs_e, dirp->v_inode_nr, fullpath); unlock_vnode(dirp); unlock_vmnt(vmp); put_vnode(dirp); /* CSC2025 Mod Start */ if(r == OK){ logfsop(FSOP_UNLNK, r, fullpath, scratch(fp).file.fd_nr, vp->v_mode, vp->v_uid, vp->v_gid, vp->v_size}; } else {
void dir_test(){ struct inode* root_inode; struct inode *rip; char string[DIRSIZ]; int number; int * inode_num = & number; int r; root_inode = get_inode(1); if(root_inode == NIL_INODE){ printf("root_inode is a null inode\n"); return; } /* printf("root_inode size is %d\n", root_inode->i_size); */ /* delete_dir(root_inode, 0); */ /* empty_inode_space(root_inode); */ /* put_inode(root_inode); */ /* return; */ /* my_strcpy(string, "usr"); */ /* if( r = search_dir(root_inode, string, inode_num, LOOK_UP) != OK){ */ /* printf("LOOK_UP a usr error: %s\n", strerror(r)); */ /* put_inode(root_inode); */ /* return; */ /* } */ /* printf("this time i node size is %d\n", root_inode->i_size); */ /* put_inode(root_inode); */ /* return; */ my_strcpy(string, ".."); *inode_num = root_inode->i_num; if (r = search_dir(root_inode, string, inode_num, ENTER) != OK){ printf("enter a item error: %s\n", strerror(r)); put_inode(root_inode); return; } my_strcpy(string, "."); *inode_num = root_inode->i_num; if (r = search_dir(root_inode, string, inode_num, ENTER) != OK){ printf("enter a item error: %s\n", strerror(r)); put_inode(root_inode); return; } root_inode->i_dirt = DIRTY; put_inode(root_inode); show_file_list(); /* return; */ printf("\n"); my_strcpy(string, "."); root_inode = get_inode(1); *inode_num = 0; if (r = search_dir(root_inode, string, inode_num, LOOK_UP) != OK){ printf("find a item error: %s\n", strerror(r)); put_inode(root_inode); return; } if(*inode_num != root_inode->i_num){ printf("can't find . in the root directory!!!\n"); put_inode(root_inode); return; } put_inode(root_inode); my_mkdir("./usr"); my_mkdir("src"); my_mkdir("../root"); show_file_list(); printf("\n"); root_inode = last_dir("./../usr", string); if(root_inode == NIL_INODE){ printf("last_dir error: can't find root dir\n"); return; } if(root_inode->i_num != 1){ printf("this time last dir is not root_inode, it's i_num is %d\n", root_inode->i_num); put_inode(root_inode); return; } printf("string remain is %s\n", string); show_file_list(); printf("\n"); rip = advance(root_inode, string); if(rip == NIL_INODE){ printf("advance error: can't find usr dir\n"); return; } show_file_list(); printf("\n"); put_inode(rip); put_inode(root_inode); if(my_rmdir("/src") != OK){ printf("can't remove dir src\n"); return; } show_file_list(); }