📄 namei.c
字号:
inode->i_mode |= S_ISGID; inode->i_dirt = 1; bh = ext_add_entry(dir,name,len,&de); if (!bh) { iput(dir); inode->i_nlink=0; iput(inode); return -ENOSPC; } de->inode = inode->i_ino; bh->b_dirt = 1; dir->i_nlink++; dir->i_dirt = 1; iput(dir); iput(inode); brelse(bh); return 0;}/* * routine to check that the specified directory is empty (for rmdir) */static int empty_dir(struct inode * inode){ unsigned long offset; struct buffer_head * bh; struct ext_dir_entry * de, * de1; if (inode->i_size < 2 * 12 || !(bh = ext_bread(inode,0,0))) { printk("warning - bad directory on dev %04x\n",inode->i_dev); return 1; } de = (struct ext_dir_entry *) bh->b_data; de1 = (struct ext_dir_entry *) ((char *) de + de->rec_len); if (de->inode != inode->i_ino || !de1->inode || strcmp(".",de->name) || strcmp("..",de1->name)) { printk("warning - bad directory on dev %04x\n",inode->i_dev); return 1; } offset = de->rec_len + de1->rec_len; de = (struct ext_dir_entry *) ((char *) de1 + de1->rec_len); while (offset < inode->i_size ) { if ((void *) de >= (void *) (bh->b_data+BLOCK_SIZE)) { brelse(bh); bh = ext_bread(inode, offset >> BLOCK_SIZE_BITS,1); if (!bh) { offset += BLOCK_SIZE; continue; } de = (struct ext_dir_entry *) bh->b_data; } if (de->rec_len < 8 || de->rec_len %4 != 0 || de->rec_len < de->name_len + 8) { printk ("empty_dir: bad dir entry\n"); printk ("dev=%d, dir=%d, offset=%d, rec_len=%d, name_len=%d\n", inode->i_dev, inode->i_ino, offset, de->rec_len, de->name_len); brelse (bh); return 1; } if (de->inode) { brelse(bh); return 0; } offset += de->rec_len; de = (struct ext_dir_entry *) ((char *) de + de->rec_len); } brelse(bh); return 1;}static inline void ext_merge_entries (struct ext_dir_entry * de, struct ext_dir_entry * pde, struct ext_dir_entry * nde){ if (nde && !nde->inode) de->rec_len += nde->rec_len; if (pde && !pde->inode) pde->rec_len += de->rec_len;}int ext_rmdir(struct inode * dir, const char * name, int len){ int retval; struct inode * inode; struct buffer_head * bh; struct ext_dir_entry * de, * pde, * nde; inode = NULL; bh = ext_find_entry(dir,name,len,&de,&pde,&nde); retval = -ENOENT; if (!bh) goto end_rmdir; retval = -EPERM; if (!(inode = iget(dir->i_sb, de->inode))) goto end_rmdir; if ((dir->i_mode & S_ISVTX) && current->euid && inode->i_uid != current->euid) goto end_rmdir; if (inode->i_dev != dir->i_dev) goto end_rmdir; if (inode == dir) /* we may not delete ".", but "../dir" is ok */ goto end_rmdir; if (!S_ISDIR(inode->i_mode)) { retval = -ENOTDIR; goto end_rmdir; } if (!empty_dir(inode)) { retval = -ENOTEMPTY; goto end_rmdir; } if (inode->i_count > 1) { retval = -EBUSY; goto end_rmdir; } if (inode->i_nlink != 2) printk("empty directory has nlink!=2 (%d)\n",inode->i_nlink); de->inode = 0; de->name_len = 0; ext_merge_entries (de, pde, nde); bh->b_dirt = 1; inode->i_nlink=0; inode->i_dirt=1; dir->i_nlink--; inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; dir->i_dirt=1; retval = 0;end_rmdir: iput(dir); iput(inode); brelse(bh); return retval;}int ext_unlink(struct inode * dir, const char * name, int len){ int retval; struct inode * inode; struct buffer_head * bh; struct ext_dir_entry * de, * pde, * nde; retval = -ENOENT; inode = NULL; bh = ext_find_entry(dir,name,len,&de,&pde,&nde); if (!bh) goto end_unlink; if (!(inode = iget(dir->i_sb, de->inode))) goto end_unlink; retval = -EPERM; if ((dir->i_mode & S_ISVTX) && !suser() && current->euid != inode->i_uid && current->euid != dir->i_uid) goto end_unlink; if (S_ISDIR(inode->i_mode)) goto end_unlink; if (!inode->i_nlink) { printk("Deleting nonexistent file (%04x:%d), %d\n", inode->i_dev,inode->i_ino,inode->i_nlink); inode->i_nlink=1; } de->inode = 0; de->name_len = 0; ext_merge_entries (de, pde, nde); bh->b_dirt = 1; inode->i_nlink--; inode->i_dirt = 1; inode->i_ctime = CURRENT_TIME; dir->i_ctime = dir->i_mtime = inode->i_ctime; dir->i_dirt = 1; retval = 0;end_unlink: brelse(bh); iput(inode); iput(dir); return retval;}int ext_symlink(struct inode * dir, const char * name, int len, const char * symname){ struct ext_dir_entry * de; struct inode * inode = NULL; struct buffer_head * bh = NULL, * name_block = NULL; int i; char c; if (!(inode = ext_new_inode(dir))) { iput(dir); return -ENOSPC; } inode->i_mode = S_IFLNK | 0777; inode->i_op = &ext_symlink_inode_operations; name_block = ext_bread(inode,0,1); if (!name_block) { iput(dir); inode->i_nlink--; inode->i_dirt = 1; iput(inode); return -ENOSPC; } i = 0; while (i < 1023 && (c = *(symname++))) name_block->b_data[i++] = c; name_block->b_data[i] = 0; name_block->b_dirt = 1; brelse(name_block); inode->i_size = i; inode->i_dirt = 1; bh = ext_find_entry(dir,name,len,&de,NULL,NULL); if (bh) { inode->i_nlink--; inode->i_dirt = 1; iput(inode); brelse(bh); iput(dir); return -EEXIST; } bh = ext_add_entry(dir,name,len,&de); if (!bh) { inode->i_nlink--; inode->i_dirt = 1; iput(inode); iput(dir); return -ENOSPC; } de->inode = inode->i_ino; bh->b_dirt = 1; brelse(bh); iput(dir); iput(inode); return 0;}int ext_link(struct inode * oldinode, struct inode * dir, const char * name, int len){ struct ext_dir_entry * de; struct buffer_head * bh; if (S_ISDIR(oldinode->i_mode)) { iput(oldinode); iput(dir); return -EPERM; } if (oldinode->i_nlink > 32000) { iput(oldinode); iput(dir); return -EMLINK; } bh = ext_find_entry(dir,name,len,&de,NULL,NULL); if (bh) { brelse(bh); iput(dir); iput(oldinode); return -EEXIST; } bh = ext_add_entry(dir,name,len,&de); if (!bh) { iput(dir); iput(oldinode); return -ENOSPC; } de->inode = oldinode->i_ino; bh->b_dirt = 1; brelse(bh); iput(dir); oldinode->i_nlink++; oldinode->i_ctime = CURRENT_TIME; oldinode->i_dirt = 1; iput(oldinode); return 0;}static int subdir(struct inode * new_inode, struct inode * old_inode){ int ino; int result; new_inode->i_count++; result = 0; for (;;) { if (new_inode == old_inode) { result = 1; break; } if (new_inode->i_dev != old_inode->i_dev) break; ino = new_inode->i_ino; if (ext_lookup(new_inode,"..",2,&new_inode)) break; if (new_inode->i_ino == ino) break; } iput(new_inode); return result;}#define PARENT_INO(buffer) \((struct ext_dir_entry *) ((char *) buffer + \((struct ext_dir_entry *) buffer)->rec_len))->inode#define PARENT_NAME(buffer) \((struct ext_dir_entry *) ((char *) buffer + \((struct ext_dir_entry *) buffer)->rec_len))->name/* * rename uses retrying to avoid race-conditions: at least they should be minimal. * it tries to allocate all the blocks, then sanity-checks, and if the sanity- * checks fail, it tries to restart itself again. Very practical - no changes * are done until we know everything works ok.. and then all the changes can be * done in one fell swoop when we have claimed all the buffers needed. * * Anybody can rename anything with this: the permission checks are left to the * higher-level routines. */static int do_ext_rename(struct inode * old_dir, const char * old_name, int old_len, struct inode * new_dir, const char * new_name, int new_len){ struct inode * old_inode, * new_inode; struct buffer_head * old_bh, * new_bh, * dir_bh; struct ext_dir_entry * old_de, * new_de, * pde, * nde; int retval; goto start_up;try_again: brelse(old_bh); brelse(new_bh); brelse(dir_bh); iput(old_inode); iput(new_inode); current->counter = 0; schedule();start_up: old_inode = new_inode = NULL; old_bh = new_bh = dir_bh = NULL; old_bh = ext_find_entry(old_dir,old_name,old_len,&old_de,&pde,&nde); retval = -ENOENT; if (!old_bh) goto end_rename; old_inode = __iget(old_dir->i_sb, old_de->inode,0); /* don't cross mnt-points */ if (!old_inode) goto end_rename; retval = -EPERM; if ((old_dir->i_mode & S_ISVTX) && current->euid != old_inode->i_uid && current->euid != old_dir->i_uid && !suser()) goto end_rename; new_bh = ext_find_entry(new_dir,new_name,new_len,&new_de,NULL,NULL); if (new_bh) { new_inode = __iget(new_dir->i_sb, new_de->inode,0); /* don't cross mnt-points */ if (!new_inode) { brelse(new_bh); new_bh = NULL; } } if (new_inode == old_inode) { retval = 0; goto end_rename; } if (new_inode && S_ISDIR(new_inode->i_mode)) { retval = -EEXIST; goto end_rename; } retval = -EPERM; if (new_inode && (new_dir->i_mode & S_ISVTX) && current->euid != new_inode->i_uid && current->euid != new_dir->i_uid && !suser()) goto end_rename; if (S_ISDIR(old_inode->i_mode)) { retval = -EEXIST; if (new_bh) goto end_rename; retval = -EACCES; if (!permission(old_inode, MAY_WRITE)) goto end_rename; retval = -EINVAL; if (subdir(new_dir, old_inode)) goto end_rename; retval = -EIO; dir_bh = ext_bread(old_inode,0,0); if (!dir_bh) goto end_rename; if (PARENT_INO(dir_bh->b_data) != old_dir->i_ino) goto end_rename; } if (!new_bh) new_bh = ext_add_entry(new_dir,new_name,new_len,&new_de); retval = -ENOSPC; if (!new_bh) goto end_rename;/* sanity checking before doing the rename - avoid races */ if (new_inode && (new_de->inode != new_inode->i_ino)) goto try_again; if (new_de->inode && !new_inode) goto try_again; if (old_de->inode != old_inode->i_ino) goto try_again;/* ok, that's it */ old_de->inode = 0; old_de->name_len = 0; new_de->inode = old_inode->i_ino; ext_merge_entries (old_de, pde, nde); if (new_inode) { new_inode->i_nlink--; new_inode->i_dirt = 1; } old_bh->b_dirt = 1; new_bh->b_dirt = 1; if (dir_bh) { PARENT_INO(dir_bh->b_data) = new_dir->i_ino; dir_bh->b_dirt = 1; old_dir->i_nlink--; new_dir->i_nlink++; old_dir->i_dirt = 1; new_dir->i_dirt = 1; } retval = 0;end_rename: brelse(dir_bh); brelse(old_bh); brelse(new_bh); iput(old_inode); iput(new_inode); iput(old_dir); iput(new_dir); return retval;}/* * Ok, rename also locks out other renames, as they can change the parent of * a directory, and we don't want any races. Other races are checked for by * "do_rename()", which restarts if there are inconsistencies. * * Note that there is no race between different filesystems: it's only within * the same device that races occur: many renames can happen at once, as long * as they are on different partitions. */int ext_rename(struct inode * old_dir, const char * old_name, int old_len, struct inode * new_dir, const char * new_name, int new_len){ static struct wait_queue * wait = NULL; static int lock = 0; int result; while (lock) sleep_on(&wait); lock = 1; result = do_ext_rename(old_dir, old_name, old_len, new_dir, new_name, new_len); lock = 0; wake_up(&wait); return result;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -