OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [before_ORP/] [uclinux/] [uClinux-2.0.x/] [fs/] [ext/] [namei.c] - Diff between revs 199 and 901

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 199 Rev 901
/*
/*
 *  linux/fs/ext/namei.c
 *  linux/fs/ext/namei.c
 *
 *
 *  Copyright (C) 1992  Remy Card (card@masi.ibp.fr)
 *  Copyright (C) 1992  Remy Card (card@masi.ibp.fr)
 *
 *
 *  from
 *  from
 *
 *
 *  linux/fs/minix/namei.c
 *  linux/fs/minix/namei.c
 *
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *  Copyright (C) 1991, 1992  Linus Torvalds
 */
 */
 
 
#include <linux/sched.h>
#include <linux/sched.h>
#include <linux/ext_fs.h>
#include <linux/ext_fs.h>
#include <linux/kernel.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/string.h>
#include <linux/stat.h>
#include <linux/stat.h>
#include <linux/fcntl.h>
#include <linux/fcntl.h>
#include <linux/errno.h>
#include <linux/errno.h>
 
 
#include <asm/segment.h>
#include <asm/segment.h>
 
 
/*
/*
 * comment out this line if you want names > EXT_NAME_LEN chars to be
 * comment out this line if you want names > EXT_NAME_LEN chars to be
 * truncated. Else they will be disallowed.
 * truncated. Else they will be disallowed.
 */
 */
/* #define NO_TRUNCATE */
/* #define NO_TRUNCATE */
 
 
/*
/*
 * EXT_DIR_PAD defines the directory entries boundaries
 * EXT_DIR_PAD defines the directory entries boundaries
 *
 *
 * NOTE: It must be a power of 2 and must be greater or equal than 8
 * NOTE: It must be a power of 2 and must be greater or equal than 8
 * because a directory entry needs 8 bytes for its fixed part
 * because a directory entry needs 8 bytes for its fixed part
 * (4 bytes for the inode, 2 bytes for the entry length and 2 bytes
 * (4 bytes for the inode, 2 bytes for the entry length and 2 bytes
 * for the name length)
 * for the name length)
 */
 */
#define EXT_DIR_PAD 8
#define EXT_DIR_PAD 8
 
 
/*
/*
 *
 *
 * EXT_DIR_MIN_SIZE is the minimal size of a directory entry
 * EXT_DIR_MIN_SIZE is the minimal size of a directory entry
 *
 *
 * During allocations, a directory entry is split into 2 ones
 * During allocations, a directory entry is split into 2 ones
 * *ONLY* if the size of the unused part is greater than or
 * *ONLY* if the size of the unused part is greater than or
 * equal to EXT_DIR_MIN_SIZE
 * equal to EXT_DIR_MIN_SIZE
 */
 */
#define EXT_DIR_MIN_SIZE 12
#define EXT_DIR_MIN_SIZE 12
 
 
/*
/*
 * ok, we cannot use strncmp, as the name is not in our data space.
 * ok, we cannot use strncmp, as the name is not in our data space.
 * Thus we'll have to use ext_match. No big problem. Match also makes
 * Thus we'll have to use ext_match. No big problem. Match also makes
 * some sanity tests.
 * some sanity tests.
 *
 *
 * NOTE! unlike strncmp, ext_match returns 1 for success, 0 for failure.
 * NOTE! unlike strncmp, ext_match returns 1 for success, 0 for failure.
 */
 */
static int ext_match(int len,const char * name,struct ext_dir_entry * de)
static int ext_match(int len,const char * name,struct ext_dir_entry * de)
{
{
        if (!de || !de->inode || len > EXT_NAME_LEN)
        if (!de || !de->inode || len > EXT_NAME_LEN)
                return 0;
                return 0;
        /* "" means "." ---> so paths like "/usr/lib//libc.a" work */
        /* "" means "." ---> so paths like "/usr/lib//libc.a" work */
        if (!len && (de->name[0]=='.') && (de->name[1]=='\0'))
        if (!len && (de->name[0]=='.') && (de->name[1]=='\0'))
                return 1;
                return 1;
        if (len != de->name_len)
        if (len != de->name_len)
                return 0;
                return 0;
        return !memcmp(name, de->name, len);
        return !memcmp(name, de->name, len);
}
}
 
 
/*
/*
 *      ext_find_entry()
 *      ext_find_entry()
 *
 *
 * finds an entry in the specified directory with the wanted name. It
 * finds an entry in the specified directory with the wanted name. It
 * returns the cache buffer in which the entry was found, and the entry
 * returns the cache buffer in which the entry was found, and the entry
 * itself (as a parameter - res_dir). It does NOT read the inode of the
 * itself (as a parameter - res_dir). It does NOT read the inode of the
 * entry - you'll have to do that yourself if you want to.
 * entry - you'll have to do that yourself if you want to.
 *
 *
 * addition for the ext file system : this function returns the previous
 * addition for the ext file system : this function returns the previous
 * and next directory entries in the parameters prev_dir and next_dir
 * and next directory entries in the parameters prev_dir and next_dir
 */
 */
static struct buffer_head * ext_find_entry(struct inode * dir,
static struct buffer_head * ext_find_entry(struct inode * dir,
        const char * name, int namelen, struct ext_dir_entry ** res_dir,
        const char * name, int namelen, struct ext_dir_entry ** res_dir,
        struct ext_dir_entry ** prev_dir, struct ext_dir_entry ** next_dir)
        struct ext_dir_entry ** prev_dir, struct ext_dir_entry ** next_dir)
{
{
        long offset;
        long offset;
        struct buffer_head * bh;
        struct buffer_head * bh;
        struct ext_dir_entry * de;
        struct ext_dir_entry * de;
 
 
        *res_dir = NULL;
        *res_dir = NULL;
        if (!dir)
        if (!dir)
                return NULL;
                return NULL;
#ifdef NO_TRUNCATE
#ifdef NO_TRUNCATE
        if (namelen > EXT_NAME_LEN)
        if (namelen > EXT_NAME_LEN)
                return NULL;
                return NULL;
#else
#else
        if (namelen > EXT_NAME_LEN)
        if (namelen > EXT_NAME_LEN)
                namelen = EXT_NAME_LEN;
                namelen = EXT_NAME_LEN;
#endif
#endif
        bh = ext_bread(dir,0,0);
        bh = ext_bread(dir,0,0);
        if (!bh)
        if (!bh)
                return NULL;
                return NULL;
        if (prev_dir)
        if (prev_dir)
                *prev_dir = NULL;
                *prev_dir = NULL;
        if (next_dir)
        if (next_dir)
                *next_dir = NULL;
                *next_dir = NULL;
        offset = 0;
        offset = 0;
        de = (struct ext_dir_entry *) bh->b_data;
        de = (struct ext_dir_entry *) bh->b_data;
        while (offset < dir->i_size) {
        while (offset < dir->i_size) {
                if ((char *)de >= BLOCK_SIZE+bh->b_data) {
                if ((char *)de >= BLOCK_SIZE+bh->b_data) {
                        brelse(bh);
                        brelse(bh);
                        bh = NULL;
                        bh = NULL;
                        bh = ext_bread(dir,offset>>BLOCK_SIZE_BITS,0);
                        bh = ext_bread(dir,offset>>BLOCK_SIZE_BITS,0);
                        if (!bh)
                        if (!bh)
                                continue;
                                continue;
                        de = (struct ext_dir_entry *) bh->b_data;
                        de = (struct ext_dir_entry *) bh->b_data;
                        if (prev_dir)
                        if (prev_dir)
                                *prev_dir = NULL;
                                *prev_dir = NULL;
                }
                }
                if (de->rec_len < 8 || de->rec_len % 8 != 0 ||
                if (de->rec_len < 8 || de->rec_len % 8 != 0 ||
                    de->rec_len < de->name_len + 8 ||
                    de->rec_len < de->name_len + 8 ||
                    (((char *) de) + de->rec_len-1 >= BLOCK_SIZE+bh->b_data)) {
                    (((char *) de) + de->rec_len-1 >= BLOCK_SIZE+bh->b_data)) {
                        printk ("ext_find_entry: bad dir entry\n");
                        printk ("ext_find_entry: bad dir entry\n");
                        printk ("dev=%s, dir=%ld, offset=%ld, "
                        printk ("dev=%s, dir=%ld, offset=%ld, "
                                "rec_len=%d, name_len=%d\n",
                                "rec_len=%d, name_len=%d\n",
                                kdevname(dir->i_dev), dir->i_ino, offset,
                                kdevname(dir->i_dev), dir->i_ino, offset,
                                de->rec_len, de->name_len);
                                de->rec_len, de->name_len);
                        de = (struct ext_dir_entry *) (bh->b_data+BLOCK_SIZE);
                        de = (struct ext_dir_entry *) (bh->b_data+BLOCK_SIZE);
                        offset = ((offset / BLOCK_SIZE) + 1) * BLOCK_SIZE;
                        offset = ((offset / BLOCK_SIZE) + 1) * BLOCK_SIZE;
                        continue;
                        continue;
/*                      brelse (bh);
/*                      brelse (bh);
                        return NULL; */
                        return NULL; */
                }
                }
                if (ext_match(namelen,name,de)) {
                if (ext_match(namelen,name,de)) {
                        *res_dir = de;
                        *res_dir = de;
                        if (next_dir)
                        if (next_dir)
                                if (offset + de->rec_len < dir->i_size &&
                                if (offset + de->rec_len < dir->i_size &&
                                    ((char *)de) + de->rec_len < BLOCK_SIZE+bh->b_data)
                                    ((char *)de) + de->rec_len < BLOCK_SIZE+bh->b_data)
                                        *next_dir = (struct ext_dir_entry *)
                                        *next_dir = (struct ext_dir_entry *)
                                                ((char *) de + de->rec_len);
                                                ((char *) de + de->rec_len);
                                else
                                else
                                        *next_dir = NULL;
                                        *next_dir = NULL;
                        return bh;
                        return bh;
                }
                }
                offset += de->rec_len;
                offset += de->rec_len;
                if (prev_dir)
                if (prev_dir)
                        *prev_dir = de;
                        *prev_dir = de;
                de = (struct ext_dir_entry *) ((char *) de + de->rec_len);
                de = (struct ext_dir_entry *) ((char *) de + de->rec_len);
        }
        }
        brelse(bh);
        brelse(bh);
        return NULL;
        return NULL;
}
}
 
 
int ext_lookup(struct inode * dir,const char * name, int len,
int ext_lookup(struct inode * dir,const char * name, int len,
        struct inode ** result)
        struct inode ** result)
{
{
        int ino;
        int ino;
        struct ext_dir_entry * de;
        struct ext_dir_entry * de;
        struct buffer_head * bh;
        struct buffer_head * bh;
 
 
        *result = NULL;
        *result = NULL;
        if (!dir)
        if (!dir)
                return -ENOENT;
                return -ENOENT;
        if (!S_ISDIR(dir->i_mode)) {
        if (!S_ISDIR(dir->i_mode)) {
                iput(dir);
                iput(dir);
                return -ENOENT;
                return -ENOENT;
        }
        }
        if (!(bh = ext_find_entry(dir,name,len,&de,NULL,NULL))) {
        if (!(bh = ext_find_entry(dir,name,len,&de,NULL,NULL))) {
                iput(dir);
                iput(dir);
                return -ENOENT;
                return -ENOENT;
        }
        }
        ino = de->inode;
        ino = de->inode;
        brelse(bh);
        brelse(bh);
        if (!(*result = iget(dir->i_sb,ino))) {
        if (!(*result = iget(dir->i_sb,ino))) {
                iput(dir);
                iput(dir);
                return -EACCES;
                return -EACCES;
        }
        }
        iput(dir);
        iput(dir);
        return 0;
        return 0;
}
}
 
 
/*
/*
 *      ext_add_entry()
 *      ext_add_entry()
 *
 *
 * adds a file entry to the specified directory, using the same
 * adds a file entry to the specified directory, using the same
 * semantics as ext_find_entry(). It returns NULL if it failed.
 * semantics as ext_find_entry(). It returns NULL if it failed.
 *
 *
 * NOTE!! The inode part of 'de' is left at 0 - which means you
 * NOTE!! The inode part of 'de' is left at 0 - which means you
 * may not sleep between calling this and putting something into
 * may not sleep between calling this and putting something into
 * the entry, as someone else might have used it while you slept.
 * the entry, as someone else might have used it while you slept.
 */
 */
static struct buffer_head * ext_add_entry(struct inode * dir,
static struct buffer_head * ext_add_entry(struct inode * dir,
        const char * name, int namelen, struct ext_dir_entry ** res_dir)
        const char * name, int namelen, struct ext_dir_entry ** res_dir)
{
{
        int i;
        int i;
        long offset;
        long offset;
        unsigned short rec_len;
        unsigned short rec_len;
        struct buffer_head * bh;
        struct buffer_head * bh;
        struct ext_dir_entry * de, * de1;
        struct ext_dir_entry * de, * de1;
 
 
        *res_dir = NULL;
        *res_dir = NULL;
        if (!dir)
        if (!dir)
                return NULL;
                return NULL;
#ifdef NO_TRUNCATE
#ifdef NO_TRUNCATE
        if (namelen > EXT_NAME_LEN)
        if (namelen > EXT_NAME_LEN)
                return NULL;
                return NULL;
#else
#else
        if (namelen > EXT_NAME_LEN)
        if (namelen > EXT_NAME_LEN)
                namelen = EXT_NAME_LEN;
                namelen = EXT_NAME_LEN;
#endif
#endif
        if (!namelen)
        if (!namelen)
                return NULL;
                return NULL;
        bh = ext_bread(dir,0,0);
        bh = ext_bread(dir,0,0);
        if (!bh)
        if (!bh)
                return NULL;
                return NULL;
        rec_len = ((8 + namelen + EXT_DIR_PAD - 1) / EXT_DIR_PAD) * EXT_DIR_PAD;
        rec_len = ((8 + namelen + EXT_DIR_PAD - 1) / EXT_DIR_PAD) * EXT_DIR_PAD;
        offset = 0;
        offset = 0;
        de = (struct ext_dir_entry *) bh->b_data;
        de = (struct ext_dir_entry *) bh->b_data;
        while (1) {
        while (1) {
                if ((char *)de >= BLOCK_SIZE+bh->b_data && offset < dir->i_size) {
                if ((char *)de >= BLOCK_SIZE+bh->b_data && offset < dir->i_size) {
#ifdef EXTFS_DEBUG
#ifdef EXTFS_DEBUG
printk ("ext_add_entry: skipping to next block\n");
printk ("ext_add_entry: skipping to next block\n");
#endif
#endif
                        brelse(bh);
                        brelse(bh);
                        bh = NULL;
                        bh = NULL;
                        bh = ext_bread(dir,offset>>BLOCK_SIZE_BITS,0);
                        bh = ext_bread(dir,offset>>BLOCK_SIZE_BITS,0);
                        if (!bh)
                        if (!bh)
                                return NULL;
                                return NULL;
                        de = (struct ext_dir_entry *) bh->b_data;
                        de = (struct ext_dir_entry *) bh->b_data;
                }
                }
                if (offset >= dir->i_size) {
                if (offset >= dir->i_size) {
                        /* Check that the directory entry fits in the block */
                        /* Check that the directory entry fits in the block */
                        if (offset % BLOCK_SIZE == 0  ||
                        if (offset % BLOCK_SIZE == 0  ||
                            (BLOCK_SIZE - (offset % BLOCK_SIZE)) < rec_len) {
                            (BLOCK_SIZE - (offset % BLOCK_SIZE)) < rec_len) {
                                if ((offset % BLOCK_SIZE) != 0) {
                                if ((offset % BLOCK_SIZE) != 0) {
                                        /* If the entry does not fit in the
                                        /* If the entry does not fit in the
                                           block, the remainder of the block
                                           block, the remainder of the block
                                           becomes an unused entry */
                                           becomes an unused entry */
                                        de->inode = 0;
                                        de->inode = 0;
                                        de->rec_len = BLOCK_SIZE
                                        de->rec_len = BLOCK_SIZE
                                                - (offset & (BLOCK_SIZE - 1));
                                                - (offset & (BLOCK_SIZE - 1));
                                        de->name_len = 0;
                                        de->name_len = 0;
                                        offset += de->rec_len;
                                        offset += de->rec_len;
                                        dir->i_size += de->rec_len;
                                        dir->i_size += de->rec_len;
                                        dir->i_dirt = 1;
                                        dir->i_dirt = 1;
#if 0
#if 0
                                        dir->i_ctime = CURRENT_TIME;
                                        dir->i_ctime = CURRENT_TIME;
#endif
#endif
                                        mark_buffer_dirty(bh, 1);
                                        mark_buffer_dirty(bh, 1);
                                }
                                }
                                brelse (bh);
                                brelse (bh);
                                bh = NULL;
                                bh = NULL;
#ifdef EXTFS_DEBUG
#ifdef EXTFS_DEBUG
printk ("ext_add_entry : creating next block\n");
printk ("ext_add_entry : creating next block\n");
#endif
#endif
                                bh = ext_bread(dir,offset>>BLOCK_SIZE_BITS,1);
                                bh = ext_bread(dir,offset>>BLOCK_SIZE_BITS,1);
                                if (!bh)
                                if (!bh)
                                        return NULL; /* Other thing to do ??? */
                                        return NULL; /* Other thing to do ??? */
                                de = (struct ext_dir_entry *) bh->b_data;
                                de = (struct ext_dir_entry *) bh->b_data;
                        }
                        }
                        /* Allocate the entry */
                        /* Allocate the entry */
                        de->inode=0;
                        de->inode=0;
                        de->rec_len = rec_len;
                        de->rec_len = rec_len;
                        dir->i_size += de->rec_len;
                        dir->i_size += de->rec_len;
                        dir->i_dirt = 1;
                        dir->i_dirt = 1;
#if 0
#if 0
                        dir->i_ctime = CURRENT_TIME;
                        dir->i_ctime = CURRENT_TIME;
#endif
#endif
                }
                }
                if (de->rec_len < 8 || de->rec_len % 4 != 0 ||
                if (de->rec_len < 8 || de->rec_len % 4 != 0 ||
                    de->rec_len < de->name_len + 8 ||
                    de->rec_len < de->name_len + 8 ||
                    (((char *) de) + de->rec_len-1 >= BLOCK_SIZE+bh->b_data)) {
                    (((char *) de) + de->rec_len-1 >= BLOCK_SIZE+bh->b_data)) {
                        printk ("ext_addr_entry: bad dir entry\n");
                        printk ("ext_addr_entry: bad dir entry\n");
                        printk ("dev=%s, dir=%ld, offset=%ld, "
                        printk ("dev=%s, dir=%ld, offset=%ld, "
                                "rec_len=%d, name_len=%d\n",
                                "rec_len=%d, name_len=%d\n",
                                kdevname(dir->i_dev), dir->i_ino, offset,
                                kdevname(dir->i_dev), dir->i_ino, offset,
                                de->rec_len, de->name_len);
                                de->rec_len, de->name_len);
                        brelse (bh);
                        brelse (bh);
                        return NULL;
                        return NULL;
                }
                }
                if (!de->inode && de->rec_len >= rec_len) {
                if (!de->inode && de->rec_len >= rec_len) {
                        if (de->rec_len > rec_len
                        if (de->rec_len > rec_len
                            && de->rec_len - rec_len >= EXT_DIR_MIN_SIZE) {
                            && de->rec_len - rec_len >= EXT_DIR_MIN_SIZE) {
                                /* The found entry is too big : it is split
                                /* The found entry is too big : it is split
                                   into 2 ones :
                                   into 2 ones :
                                   - the 1st one will be used to hold the name,
                                   - the 1st one will be used to hold the name,
                                   - the 2nd one is unused */
                                   - the 2nd one is unused */
                                de1 = (struct ext_dir_entry *) ((char *) de + rec_len);
                                de1 = (struct ext_dir_entry *) ((char *) de + rec_len);
                                de1->inode = 0;
                                de1->inode = 0;
                                de1->rec_len = de->rec_len - rec_len;
                                de1->rec_len = de->rec_len - rec_len;
                                de1->name_len = 0;
                                de1->name_len = 0;
                                de->rec_len = rec_len;
                                de->rec_len = rec_len;
                        }
                        }
                        dir->i_mtime = dir->i_ctime = CURRENT_TIME;
                        dir->i_mtime = dir->i_ctime = CURRENT_TIME;
                        dir->i_dirt = 1;
                        dir->i_dirt = 1;
                        de->name_len = namelen;
                        de->name_len = namelen;
                        for (i=0; i < namelen ; i++)
                        for (i=0; i < namelen ; i++)
                                de->name[i] = name[i];
                                de->name[i] = name[i];
                        mark_buffer_dirty(bh, 1);
                        mark_buffer_dirty(bh, 1);
                        *res_dir = de;
                        *res_dir = de;
                        return bh;
                        return bh;
                }
                }
                offset += de->rec_len;
                offset += de->rec_len;
                de = (struct ext_dir_entry *) ((char *) de + de->rec_len);
                de = (struct ext_dir_entry *) ((char *) de + de->rec_len);
        }
        }
        brelse(bh);
        brelse(bh);
        return NULL;
        return NULL;
}
}
 
 
int ext_create(struct inode * dir,const char * name, int len, int mode,
int ext_create(struct inode * dir,const char * name, int len, int mode,
        struct inode ** result)
        struct inode ** result)
{
{
        struct inode * inode;
        struct inode * inode;
        struct buffer_head * bh;
        struct buffer_head * bh;
        struct ext_dir_entry * de;
        struct ext_dir_entry * de;
 
 
        *result = NULL;
        *result = NULL;
        if (!dir)
        if (!dir)
                return -ENOENT;
                return -ENOENT;
        inode = ext_new_inode(dir);
        inode = ext_new_inode(dir);
        if (!inode) {
        if (!inode) {
                iput(dir);
                iput(dir);
                return -ENOSPC;
                return -ENOSPC;
        }
        }
        inode->i_op = &ext_file_inode_operations;
        inode->i_op = &ext_file_inode_operations;
        inode->i_mode = mode;
        inode->i_mode = mode;
        inode->i_dirt = 1;
        inode->i_dirt = 1;
        bh = ext_add_entry(dir,name,len,&de);
        bh = ext_add_entry(dir,name,len,&de);
        if (!bh) {
        if (!bh) {
                inode->i_nlink--;
                inode->i_nlink--;
                inode->i_dirt = 1;
                inode->i_dirt = 1;
                iput(inode);
                iput(inode);
                iput(dir);
                iput(dir);
                return -ENOSPC;
                return -ENOSPC;
        }
        }
        de->inode = inode->i_ino;
        de->inode = inode->i_ino;
        mark_buffer_dirty(bh, 1);
        mark_buffer_dirty(bh, 1);
        brelse(bh);
        brelse(bh);
        iput(dir);
        iput(dir);
        *result = inode;
        *result = inode;
        return 0;
        return 0;
}
}
 
 
int ext_mknod(struct inode * dir, const char * name, int len, int mode, int rdev)
int ext_mknod(struct inode * dir, const char * name, int len, int mode, int rdev)
{
{
        struct inode * inode;
        struct inode * inode;
        struct buffer_head * bh;
        struct buffer_head * bh;
        struct ext_dir_entry * de;
        struct ext_dir_entry * de;
 
 
        if (!dir)
        if (!dir)
                return -ENOENT;
                return -ENOENT;
        bh = ext_find_entry(dir,name,len,&de,NULL,NULL);
        bh = ext_find_entry(dir,name,len,&de,NULL,NULL);
        if (bh) {
        if (bh) {
                brelse(bh);
                brelse(bh);
                iput(dir);
                iput(dir);
                return -EEXIST;
                return -EEXIST;
        }
        }
        inode = ext_new_inode(dir);
        inode = ext_new_inode(dir);
        if (!inode) {
        if (!inode) {
                iput(dir);
                iput(dir);
                return -ENOSPC;
                return -ENOSPC;
        }
        }
        inode->i_uid = current->fsuid;
        inode->i_uid = current->fsuid;
        inode->i_mode = mode;
        inode->i_mode = mode;
        inode->i_op = NULL;
        inode->i_op = NULL;
        if (S_ISREG(inode->i_mode))
        if (S_ISREG(inode->i_mode))
                inode->i_op = &ext_file_inode_operations;
                inode->i_op = &ext_file_inode_operations;
        else if (S_ISDIR(inode->i_mode)) {
        else if (S_ISDIR(inode->i_mode)) {
                inode->i_op = &ext_dir_inode_operations;
                inode->i_op = &ext_dir_inode_operations;
                if (dir->i_mode & S_ISGID)
                if (dir->i_mode & S_ISGID)
                        inode->i_mode |= S_ISGID;
                        inode->i_mode |= S_ISGID;
        }
        }
        else if (S_ISLNK(inode->i_mode))
        else if (S_ISLNK(inode->i_mode))
                inode->i_op = &ext_symlink_inode_operations;
                inode->i_op = &ext_symlink_inode_operations;
        else if (S_ISCHR(inode->i_mode))
        else if (S_ISCHR(inode->i_mode))
                inode->i_op = &chrdev_inode_operations;
                inode->i_op = &chrdev_inode_operations;
        else if (S_ISBLK(inode->i_mode))
        else if (S_ISBLK(inode->i_mode))
                inode->i_op = &blkdev_inode_operations;
                inode->i_op = &blkdev_inode_operations;
        else if (S_ISFIFO(inode->i_mode))
        else if (S_ISFIFO(inode->i_mode))
                init_fifo(inode);
                init_fifo(inode);
        if (S_ISBLK(mode) || S_ISCHR(mode))
        if (S_ISBLK(mode) || S_ISCHR(mode))
                inode->i_rdev = to_kdev_t(rdev);
                inode->i_rdev = to_kdev_t(rdev);
#if 0
#if 0
        inode->i_mtime = inode->i_atime = CURRENT_TIME;
        inode->i_mtime = inode->i_atime = CURRENT_TIME;
#endif
#endif
        inode->i_dirt = 1;
        inode->i_dirt = 1;
        bh = ext_add_entry(dir,name,len,&de);
        bh = ext_add_entry(dir,name,len,&de);
        if (!bh) {
        if (!bh) {
                inode->i_nlink--;
                inode->i_nlink--;
                inode->i_dirt = 1;
                inode->i_dirt = 1;
                iput(inode);
                iput(inode);
                iput(dir);
                iput(dir);
                return -ENOSPC;
                return -ENOSPC;
        }
        }
        de->inode = inode->i_ino;
        de->inode = inode->i_ino;
        mark_buffer_dirty(bh, 1);
        mark_buffer_dirty(bh, 1);
        brelse(bh);
        brelse(bh);
        iput(dir);
        iput(dir);
        iput(inode);
        iput(inode);
        return 0;
        return 0;
}
}
 
 
int ext_mkdir(struct inode * dir, const char * name, int len, int mode)
int ext_mkdir(struct inode * dir, const char * name, int len, int mode)
{
{
        struct inode * inode;
        struct inode * inode;
        struct buffer_head * bh, *dir_block;
        struct buffer_head * bh, *dir_block;
        struct ext_dir_entry * de;
        struct ext_dir_entry * de;
 
 
        bh = ext_find_entry(dir,name,len,&de,NULL,NULL);
        bh = ext_find_entry(dir,name,len,&de,NULL,NULL);
        if (bh) {
        if (bh) {
                brelse(bh);
                brelse(bh);
                iput(dir);
                iput(dir);
                return -EEXIST;
                return -EEXIST;
        }
        }
        inode = ext_new_inode(dir);
        inode = ext_new_inode(dir);
        if (!inode) {
        if (!inode) {
                iput(dir);
                iput(dir);
                return -ENOSPC;
                return -ENOSPC;
        }
        }
        inode->i_op = &ext_dir_inode_operations;
        inode->i_op = &ext_dir_inode_operations;
        inode->i_size = 2 * 16; /* Each entry is coded on 16 bytes for "." and ".."
        inode->i_size = 2 * 16; /* Each entry is coded on 16 bytes for "." and ".."
                                        - 4 bytes for the inode number,
                                        - 4 bytes for the inode number,
                                        - 2 bytes for the record length
                                        - 2 bytes for the record length
                                        - 2 bytes for the name length
                                        - 2 bytes for the name length
                                        - 8 bytes for the name */
                                        - 8 bytes for the name */
#if 0
#if 0
        inode->i_mtime = inode->i_atime = CURRENT_TIME;
        inode->i_mtime = inode->i_atime = CURRENT_TIME;
#endif
#endif
        dir_block = ext_bread(inode,0,1);
        dir_block = ext_bread(inode,0,1);
        if (!dir_block) {
        if (!dir_block) {
                iput(dir);
                iput(dir);
                inode->i_nlink--;
                inode->i_nlink--;
                inode->i_dirt = 1;
                inode->i_dirt = 1;
                iput(inode);
                iput(inode);
                return -ENOSPC;
                return -ENOSPC;
        }
        }
        de = (struct ext_dir_entry *) dir_block->b_data;
        de = (struct ext_dir_entry *) dir_block->b_data;
        de->inode=inode->i_ino;
        de->inode=inode->i_ino;
        de->rec_len=16;
        de->rec_len=16;
        de->name_len=1;
        de->name_len=1;
        strcpy(de->name,".");
        strcpy(de->name,".");
        de = (struct ext_dir_entry *) ((char *) de + de->rec_len);
        de = (struct ext_dir_entry *) ((char *) de + de->rec_len);
        de->inode = dir->i_ino;
        de->inode = dir->i_ino;
        de->rec_len=16;
        de->rec_len=16;
        de->name_len=2;
        de->name_len=2;
        strcpy(de->name,"..");
        strcpy(de->name,"..");
        inode->i_nlink = 2;
        inode->i_nlink = 2;
        mark_buffer_dirty(dir_block, 1);
        mark_buffer_dirty(dir_block, 1);
        brelse(dir_block);
        brelse(dir_block);
        inode->i_mode = S_IFDIR | (mode & 0777 & ~current->fs->umask);
        inode->i_mode = S_IFDIR | (mode & 0777 & ~current->fs->umask);
        if (dir->i_mode & S_ISGID)
        if (dir->i_mode & S_ISGID)
                inode->i_mode |= S_ISGID;
                inode->i_mode |= S_ISGID;
        inode->i_dirt = 1;
        inode->i_dirt = 1;
        bh = ext_add_entry(dir,name,len,&de);
        bh = ext_add_entry(dir,name,len,&de);
        if (!bh) {
        if (!bh) {
                iput(dir);
                iput(dir);
                inode->i_nlink=0;
                inode->i_nlink=0;
                iput(inode);
                iput(inode);
                return -ENOSPC;
                return -ENOSPC;
        }
        }
        de->inode = inode->i_ino;
        de->inode = inode->i_ino;
        mark_buffer_dirty(bh, 1);
        mark_buffer_dirty(bh, 1);
        dir->i_nlink++;
        dir->i_nlink++;
        dir->i_dirt = 1;
        dir->i_dirt = 1;
        iput(dir);
        iput(dir);
        iput(inode);
        iput(inode);
        brelse(bh);
        brelse(bh);
        return 0;
        return 0;
}
}
 
 
/*
/*
 * routine to check that the specified directory is empty (for rmdir)
 * routine to check that the specified directory is empty (for rmdir)
 */
 */
static int empty_dir(struct inode * inode)
static int empty_dir(struct inode * inode)
{
{
        unsigned long offset;
        unsigned long offset;
        struct buffer_head * bh;
        struct buffer_head * bh;
        struct ext_dir_entry * de, * de1;
        struct ext_dir_entry * de, * de1;
 
 
        if (inode->i_size < 2 * 12 || !(bh = ext_bread(inode,0,0))) {
        if (inode->i_size < 2 * 12 || !(bh = ext_bread(inode,0,0))) {
                printk("warning - bad directory on dev %s\n",
                printk("warning - bad directory on dev %s\n",
                       kdevname(inode->i_dev));
                       kdevname(inode->i_dev));
                return 1;
                return 1;
        }
        }
        de = (struct ext_dir_entry *) bh->b_data;
        de = (struct ext_dir_entry *) bh->b_data;
        de1 = (struct ext_dir_entry *) ((char *) de + de->rec_len);
        de1 = (struct ext_dir_entry *) ((char *) de + de->rec_len);
        if (de->inode != inode->i_ino || !de1->inode ||
        if (de->inode != inode->i_ino || !de1->inode ||
            strcmp(".",de->name) || strcmp("..",de1->name)) {
            strcmp(".",de->name) || strcmp("..",de1->name)) {
                printk("warning - bad directory on dev %s\n",
                printk("warning - bad directory on dev %s\n",
                       kdevname(inode->i_dev));
                       kdevname(inode->i_dev));
                return 1;
                return 1;
        }
        }
        offset = de->rec_len + de1->rec_len;
        offset = de->rec_len + de1->rec_len;
        de = (struct ext_dir_entry *) ((char *) de1 + de1->rec_len);
        de = (struct ext_dir_entry *) ((char *) de1 + de1->rec_len);
        while (offset < inode->i_size ) {
        while (offset < inode->i_size ) {
                if ((void *) de >= (void *) (bh->b_data+BLOCK_SIZE)) {
                if ((void *) de >= (void *) (bh->b_data+BLOCK_SIZE)) {
                        brelse(bh);
                        brelse(bh);
                        bh = ext_bread(inode, offset >> BLOCK_SIZE_BITS,1);
                        bh = ext_bread(inode, offset >> BLOCK_SIZE_BITS,1);
                        if (!bh) {
                        if (!bh) {
                                offset += BLOCK_SIZE;
                                offset += BLOCK_SIZE;
                                continue;
                                continue;
                        }
                        }
                        de = (struct ext_dir_entry *) bh->b_data;
                        de = (struct ext_dir_entry *) bh->b_data;
                }
                }
                if (de->rec_len < 8 || de->rec_len %4 != 0 ||
                if (de->rec_len < 8 || de->rec_len %4 != 0 ||
                    de->rec_len < de->name_len + 8) {
                    de->rec_len < de->name_len + 8) {
                        printk ("empty_dir: bad dir entry\n");
                        printk ("empty_dir: bad dir entry\n");
                        printk ("dev=%s, dir=%ld, offset=%ld, "
                        printk ("dev=%s, dir=%ld, offset=%ld, "
                                "rec_len=%d, name_len=%d\n",
                                "rec_len=%d, name_len=%d\n",
                                kdevname(inode->i_dev), inode->i_ino,
                                kdevname(inode->i_dev), inode->i_ino,
                                offset, de->rec_len, de->name_len);
                                offset, de->rec_len, de->name_len);
                        brelse (bh);
                        brelse (bh);
                        return 1;
                        return 1;
                }
                }
                if (de->inode) {
                if (de->inode) {
                        brelse(bh);
                        brelse(bh);
                        return 0;
                        return 0;
                }
                }
                offset += de->rec_len;
                offset += de->rec_len;
                de = (struct ext_dir_entry *) ((char *) de + de->rec_len);
                de = (struct ext_dir_entry *) ((char *) de + de->rec_len);
        }
        }
        brelse(bh);
        brelse(bh);
        return 1;
        return 1;
}
}
 
 
static inline void ext_merge_entries (struct ext_dir_entry * de,
static inline void ext_merge_entries (struct ext_dir_entry * de,
        struct ext_dir_entry * pde, struct ext_dir_entry * nde)
        struct ext_dir_entry * pde, struct ext_dir_entry * nde)
{
{
        if (nde && !nde->inode)
        if (nde && !nde->inode)
                de->rec_len += nde->rec_len;
                de->rec_len += nde->rec_len;
        if (pde && !pde->inode)
        if (pde && !pde->inode)
                pde->rec_len += de->rec_len;
                pde->rec_len += de->rec_len;
}
}
 
 
int ext_rmdir(struct inode * dir, const char * name, int len)
int ext_rmdir(struct inode * dir, const char * name, int len)
{
{
        int retval;
        int retval;
        struct inode * inode;
        struct inode * inode;
        struct buffer_head * bh;
        struct buffer_head * bh;
        struct ext_dir_entry * de, * pde, * nde;
        struct ext_dir_entry * de, * pde, * nde;
 
 
        inode = NULL;
        inode = NULL;
        bh = ext_find_entry(dir,name,len,&de,&pde,&nde);
        bh = ext_find_entry(dir,name,len,&de,&pde,&nde);
        retval = -ENOENT;
        retval = -ENOENT;
        if (!bh)
        if (!bh)
                goto end_rmdir;
                goto end_rmdir;
        retval = -EPERM;
        retval = -EPERM;
        if (!(inode = iget(dir->i_sb, de->inode)))
        if (!(inode = iget(dir->i_sb, de->inode)))
                goto end_rmdir;
                goto end_rmdir;
        if ((dir->i_mode & S_ISVTX) && !fsuser() &&
        if ((dir->i_mode & S_ISVTX) && !fsuser() &&
            current->fsuid != inode->i_uid &&
            current->fsuid != inode->i_uid &&
            current->fsuid != dir->i_uid)
            current->fsuid != dir->i_uid)
                goto end_rmdir;
                goto end_rmdir;
        if (inode->i_dev != dir->i_dev)
        if (inode->i_dev != dir->i_dev)
                goto end_rmdir;
                goto end_rmdir;
        if (inode == dir)       /* we may not delete ".", but "../dir" is ok */
        if (inode == dir)       /* we may not delete ".", but "../dir" is ok */
                goto end_rmdir;
                goto end_rmdir;
        if (!S_ISDIR(inode->i_mode)) {
        if (!S_ISDIR(inode->i_mode)) {
                retval = -ENOTDIR;
                retval = -ENOTDIR;
                goto end_rmdir;
                goto end_rmdir;
        }
        }
        if (!empty_dir(inode)) {
        if (!empty_dir(inode)) {
                retval = -ENOTEMPTY;
                retval = -ENOTEMPTY;
                goto end_rmdir;
                goto end_rmdir;
        }
        }
        if (inode->i_count > 1) {
        if (inode->i_count > 1) {
                retval = -EBUSY;
                retval = -EBUSY;
                goto end_rmdir;
                goto end_rmdir;
        }
        }
        if (inode->i_nlink != 2)
        if (inode->i_nlink != 2)
                printk("empty directory has nlink!=2 (%d)\n",inode->i_nlink);
                printk("empty directory has nlink!=2 (%d)\n",inode->i_nlink);
        de->inode = 0;
        de->inode = 0;
        de->name_len = 0;
        de->name_len = 0;
        ext_merge_entries (de, pde, nde);
        ext_merge_entries (de, pde, nde);
        mark_buffer_dirty(bh, 1);
        mark_buffer_dirty(bh, 1);
        inode->i_nlink=0;
        inode->i_nlink=0;
        inode->i_dirt=1;
        inode->i_dirt=1;
        dir->i_nlink--;
        dir->i_nlink--;
        inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
        inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
        dir->i_dirt=1;
        dir->i_dirt=1;
        retval = 0;
        retval = 0;
end_rmdir:
end_rmdir:
        iput(dir);
        iput(dir);
        iput(inode);
        iput(inode);
        brelse(bh);
        brelse(bh);
        return retval;
        return retval;
}
}
 
 
int ext_unlink(struct inode * dir, const char * name, int len)
int ext_unlink(struct inode * dir, const char * name, int len)
{
{
        int retval;
        int retval;
        struct inode * inode;
        struct inode * inode;
        struct buffer_head * bh;
        struct buffer_head * bh;
        struct ext_dir_entry * de, * pde, * nde;
        struct ext_dir_entry * de, * pde, * nde;
 
 
        retval = -ENOENT;
        retval = -ENOENT;
        inode = NULL;
        inode = NULL;
        bh = ext_find_entry(dir,name,len,&de,&pde,&nde);
        bh = ext_find_entry(dir,name,len,&de,&pde,&nde);
        if (!bh)
        if (!bh)
                goto end_unlink;
                goto end_unlink;
        if (!(inode = iget(dir->i_sb, de->inode)))
        if (!(inode = iget(dir->i_sb, de->inode)))
                goto end_unlink;
                goto end_unlink;
        retval = -EPERM;
        retval = -EPERM;
        if ((dir->i_mode & S_ISVTX) && !fsuser() &&
        if ((dir->i_mode & S_ISVTX) && !fsuser() &&
            current->fsuid != inode->i_uid &&
            current->fsuid != inode->i_uid &&
            current->fsuid != dir->i_uid)
            current->fsuid != dir->i_uid)
                goto end_unlink;
                goto end_unlink;
        if (S_ISDIR(inode->i_mode))
        if (S_ISDIR(inode->i_mode))
                goto end_unlink;
                goto end_unlink;
        if (!inode->i_nlink) {
        if (!inode->i_nlink) {
                printk("Deleting nonexistent file (%s:%ld), %d\n",
                printk("Deleting nonexistent file (%s:%ld), %d\n",
                       kdevname(inode->i_dev), inode->i_ino,
                       kdevname(inode->i_dev), inode->i_ino,
                       inode->i_nlink);
                       inode->i_nlink);
                inode->i_nlink=1;
                inode->i_nlink=1;
        }
        }
        de->inode = 0;
        de->inode = 0;
        de->name_len = 0;
        de->name_len = 0;
        ext_merge_entries (de, pde, nde);
        ext_merge_entries (de, pde, nde);
        mark_buffer_dirty(bh, 1);
        mark_buffer_dirty(bh, 1);
        inode->i_nlink--;
        inode->i_nlink--;
        inode->i_dirt = 1;
        inode->i_dirt = 1;
        inode->i_ctime = CURRENT_TIME;
        inode->i_ctime = CURRENT_TIME;
        dir->i_ctime = dir->i_mtime = inode->i_ctime;
        dir->i_ctime = dir->i_mtime = inode->i_ctime;
        dir->i_dirt = 1;
        dir->i_dirt = 1;
        retval = 0;
        retval = 0;
end_unlink:
end_unlink:
        brelse(bh);
        brelse(bh);
        iput(inode);
        iput(inode);
        iput(dir);
        iput(dir);
        return retval;
        return retval;
}
}
 
 
int ext_symlink(struct inode * dir, const char * name, int len, const char * symname)
int ext_symlink(struct inode * dir, const char * name, int len, const char * symname)
{
{
        struct ext_dir_entry * de;
        struct ext_dir_entry * de;
        struct inode * inode = NULL;
        struct inode * inode = NULL;
        struct buffer_head * bh = NULL, * name_block = NULL;
        struct buffer_head * bh = NULL, * name_block = NULL;
        int i;
        int i;
        char c;
        char c;
 
 
        if (!(inode = ext_new_inode(dir))) {
        if (!(inode = ext_new_inode(dir))) {
                iput(dir);
                iput(dir);
                return -ENOSPC;
                return -ENOSPC;
        }
        }
        inode->i_mode = S_IFLNK | 0777;
        inode->i_mode = S_IFLNK | 0777;
        inode->i_op = &ext_symlink_inode_operations;
        inode->i_op = &ext_symlink_inode_operations;
        name_block = ext_bread(inode,0,1);
        name_block = ext_bread(inode,0,1);
        if (!name_block) {
        if (!name_block) {
                iput(dir);
                iput(dir);
                inode->i_nlink--;
                inode->i_nlink--;
                inode->i_dirt = 1;
                inode->i_dirt = 1;
                iput(inode);
                iput(inode);
                return -ENOSPC;
                return -ENOSPC;
        }
        }
        i = 0;
        i = 0;
        while (i < 1023 && (c = *(symname++)))
        while (i < 1023 && (c = *(symname++)))
                name_block->b_data[i++] = c;
                name_block->b_data[i++] = c;
        name_block->b_data[i] = 0;
        name_block->b_data[i] = 0;
        mark_buffer_dirty(name_block, 1);
        mark_buffer_dirty(name_block, 1);
        brelse(name_block);
        brelse(name_block);
        inode->i_size = i;
        inode->i_size = i;
        inode->i_dirt = 1;
        inode->i_dirt = 1;
        bh = ext_find_entry(dir,name,len,&de,NULL,NULL);
        bh = ext_find_entry(dir,name,len,&de,NULL,NULL);
        if (bh) {
        if (bh) {
                inode->i_nlink--;
                inode->i_nlink--;
                inode->i_dirt = 1;
                inode->i_dirt = 1;
                iput(inode);
                iput(inode);
                brelse(bh);
                brelse(bh);
                iput(dir);
                iput(dir);
                return -EEXIST;
                return -EEXIST;
        }
        }
        bh = ext_add_entry(dir,name,len,&de);
        bh = ext_add_entry(dir,name,len,&de);
        if (!bh) {
        if (!bh) {
                inode->i_nlink--;
                inode->i_nlink--;
                inode->i_dirt = 1;
                inode->i_dirt = 1;
                iput(inode);
                iput(inode);
                iput(dir);
                iput(dir);
                return -ENOSPC;
                return -ENOSPC;
        }
        }
        de->inode = inode->i_ino;
        de->inode = inode->i_ino;
        mark_buffer_dirty(bh, 1);
        mark_buffer_dirty(bh, 1);
        brelse(bh);
        brelse(bh);
        iput(dir);
        iput(dir);
        iput(inode);
        iput(inode);
        return 0;
        return 0;
}
}
 
 
int ext_link(struct inode * oldinode, struct inode * dir, const char * name, int len)
int ext_link(struct inode * oldinode, struct inode * dir, const char * name, int len)
{
{
        struct ext_dir_entry * de;
        struct ext_dir_entry * de;
        struct buffer_head * bh;
        struct buffer_head * bh;
 
 
        if (S_ISDIR(oldinode->i_mode)) {
        if (S_ISDIR(oldinode->i_mode)) {
                iput(oldinode);
                iput(oldinode);
                iput(dir);
                iput(dir);
                return -EPERM;
                return -EPERM;
        }
        }
        if (oldinode->i_nlink > 32000) {
        if (oldinode->i_nlink > 32000) {
                iput(oldinode);
                iput(oldinode);
                iput(dir);
                iput(dir);
                return -EMLINK;
                return -EMLINK;
        }
        }
        bh = ext_find_entry(dir,name,len,&de,NULL,NULL);
        bh = ext_find_entry(dir,name,len,&de,NULL,NULL);
        if (bh) {
        if (bh) {
                brelse(bh);
                brelse(bh);
                iput(dir);
                iput(dir);
                iput(oldinode);
                iput(oldinode);
                return -EEXIST;
                return -EEXIST;
        }
        }
        bh = ext_add_entry(dir,name,len,&de);
        bh = ext_add_entry(dir,name,len,&de);
        if (!bh) {
        if (!bh) {
                iput(dir);
                iput(dir);
                iput(oldinode);
                iput(oldinode);
                return -ENOSPC;
                return -ENOSPC;
        }
        }
        de->inode = oldinode->i_ino;
        de->inode = oldinode->i_ino;
        mark_buffer_dirty(bh, 1);
        mark_buffer_dirty(bh, 1);
        brelse(bh);
        brelse(bh);
        iput(dir);
        iput(dir);
        oldinode->i_nlink++;
        oldinode->i_nlink++;
        oldinode->i_ctime = CURRENT_TIME;
        oldinode->i_ctime = CURRENT_TIME;
        oldinode->i_dirt = 1;
        oldinode->i_dirt = 1;
        iput(oldinode);
        iput(oldinode);
        return 0;
        return 0;
}
}
 
 
static int subdir(struct inode * new_inode, struct inode * old_inode)
static int subdir(struct inode * new_inode, struct inode * old_inode)
{
{
        int ino;
        int ino;
        int result;
        int result;
 
 
        new_inode->i_count++;
        new_inode->i_count++;
        result = 0;
        result = 0;
        for (;;) {
        for (;;) {
                if (new_inode == old_inode) {
                if (new_inode == old_inode) {
                        result = 1;
                        result = 1;
                        break;
                        break;
                }
                }
                if (new_inode->i_dev != old_inode->i_dev)
                if (new_inode->i_dev != old_inode->i_dev)
                        break;
                        break;
                ino = new_inode->i_ino;
                ino = new_inode->i_ino;
                if (ext_lookup(new_inode,"..",2,&new_inode))
                if (ext_lookup(new_inode,"..",2,&new_inode))
                        break;
                        break;
                if (new_inode->i_ino == ino)
                if (new_inode->i_ino == ino)
                        break;
                        break;
        }
        }
        iput(new_inode);
        iput(new_inode);
        return result;
        return result;
}
}
 
 
#define PARENT_INO(buffer) \
#define PARENT_INO(buffer) \
((struct ext_dir_entry *) ((char *) buffer + \
((struct ext_dir_entry *) ((char *) buffer + \
((struct ext_dir_entry *) buffer)->rec_len))->inode
((struct ext_dir_entry *) buffer)->rec_len))->inode
 
 
#define PARENT_NAME(buffer) \
#define PARENT_NAME(buffer) \
((struct ext_dir_entry *) ((char *) buffer + \
((struct ext_dir_entry *) ((char *) buffer + \
((struct ext_dir_entry *) buffer)->rec_len))->name
((struct ext_dir_entry *) buffer)->rec_len))->name
 
 
/*
/*
 * rename uses retrying to avoid race-conditions: at least they should be minimal.
 * 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-
 * 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
 * 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
 * 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.
 * 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
 * Anybody can rename anything with this: the permission checks are left to the
 * higher-level routines.
 * higher-level routines.
 */
 */
static int do_ext_rename(struct inode * old_dir, const char * old_name, int old_len,
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 * new_dir, const char * new_name, int new_len)
{
{
        struct inode * old_inode, * new_inode;
        struct inode * old_inode, * new_inode;
        struct buffer_head * old_bh, * new_bh, * dir_bh;
        struct buffer_head * old_bh, * new_bh, * dir_bh;
        struct ext_dir_entry * old_de, * new_de, * pde, * nde;
        struct ext_dir_entry * old_de, * new_de, * pde, * nde;
        int retval;
        int retval;
 
 
        goto start_up;
        goto start_up;
try_again:
try_again:
        brelse(old_bh);
        brelse(old_bh);
        brelse(new_bh);
        brelse(new_bh);
        brelse(dir_bh);
        brelse(dir_bh);
        iput(old_inode);
        iput(old_inode);
        iput(new_inode);
        iput(new_inode);
        current->counter = 0;
        current->counter = 0;
        schedule();
        schedule();
start_up:
start_up:
        old_inode = new_inode = NULL;
        old_inode = new_inode = NULL;
        old_bh = new_bh = dir_bh = NULL;
        old_bh = new_bh = dir_bh = NULL;
        old_bh = ext_find_entry(old_dir,old_name,old_len,&old_de,&pde,&nde);
        old_bh = ext_find_entry(old_dir,old_name,old_len,&old_de,&pde,&nde);
        retval = -ENOENT;
        retval = -ENOENT;
        if (!old_bh)
        if (!old_bh)
                goto end_rename;
                goto end_rename;
        old_inode = __iget(old_dir->i_sb, old_de->inode,0); /* don't cross mnt-points */
        old_inode = __iget(old_dir->i_sb, old_de->inode,0); /* don't cross mnt-points */
        if (!old_inode)
        if (!old_inode)
                goto end_rename;
                goto end_rename;
        retval = -EPERM;
        retval = -EPERM;
        if ((old_dir->i_mode & S_ISVTX) &&
        if ((old_dir->i_mode & S_ISVTX) &&
            current->fsuid != old_inode->i_uid &&
            current->fsuid != old_inode->i_uid &&
            current->fsuid != old_dir->i_uid && !fsuser())
            current->fsuid != old_dir->i_uid && !fsuser())
                goto end_rename;
                goto end_rename;
        new_bh = ext_find_entry(new_dir,new_name,new_len,&new_de,NULL,NULL);
        new_bh = ext_find_entry(new_dir,new_name,new_len,&new_de,NULL,NULL);
        if (new_bh) {
        if (new_bh) {
                new_inode = __iget(new_dir->i_sb, new_de->inode,0); /* don't cross mnt-points */
                new_inode = __iget(new_dir->i_sb, new_de->inode,0); /* don't cross mnt-points */
                if (!new_inode) {
                if (!new_inode) {
                        brelse(new_bh);
                        brelse(new_bh);
                        new_bh = NULL;
                        new_bh = NULL;
                }
                }
        }
        }
        if (new_inode == old_inode) {
        if (new_inode == old_inode) {
                retval = 0;
                retval = 0;
                goto end_rename;
                goto end_rename;
        }
        }
        if (new_inode && S_ISDIR(new_inode->i_mode)) {
        if (new_inode && S_ISDIR(new_inode->i_mode)) {
                retval = -EEXIST;
                retval = -EEXIST;
                goto end_rename;
                goto end_rename;
        }
        }
        retval = -EPERM;
        retval = -EPERM;
        if (new_inode && (new_dir->i_mode & S_ISVTX) &&
        if (new_inode && (new_dir->i_mode & S_ISVTX) &&
            current->fsuid != new_inode->i_uid &&
            current->fsuid != new_inode->i_uid &&
            current->fsuid != new_dir->i_uid && !fsuser())
            current->fsuid != new_dir->i_uid && !fsuser())
                goto end_rename;
                goto end_rename;
        if (S_ISDIR(old_inode->i_mode)) {
        if (S_ISDIR(old_inode->i_mode)) {
                retval = -EEXIST;
                retval = -EEXIST;
                if (new_bh)
                if (new_bh)
                        goto end_rename;
                        goto end_rename;
                if ((retval = permission(old_inode, MAY_WRITE)) != 0)
                if ((retval = permission(old_inode, MAY_WRITE)) != 0)
                        goto end_rename;
                        goto end_rename;
                retval = -EINVAL;
                retval = -EINVAL;
                if (subdir(new_dir, old_inode))
                if (subdir(new_dir, old_inode))
                        goto end_rename;
                        goto end_rename;
                retval = -EIO;
                retval = -EIO;
                dir_bh = ext_bread(old_inode,0,0);
                dir_bh = ext_bread(old_inode,0,0);
                if (!dir_bh)
                if (!dir_bh)
                        goto end_rename;
                        goto end_rename;
                if (PARENT_INO(dir_bh->b_data) != old_dir->i_ino)
                if (PARENT_INO(dir_bh->b_data) != old_dir->i_ino)
                        goto end_rename;
                        goto end_rename;
        }
        }
        if (!new_bh)
        if (!new_bh)
                new_bh = ext_add_entry(new_dir,new_name,new_len,&new_de);
                new_bh = ext_add_entry(new_dir,new_name,new_len,&new_de);
        retval = -ENOSPC;
        retval = -ENOSPC;
        if (!new_bh)
        if (!new_bh)
                goto end_rename;
                goto end_rename;
/* sanity checking before doing the rename - avoid races */
/* sanity checking before doing the rename - avoid races */
        if (new_inode && (new_de->inode != new_inode->i_ino))
        if (new_inode && (new_de->inode != new_inode->i_ino))
                goto try_again;
                goto try_again;
        if (new_de->inode && !new_inode)
        if (new_de->inode && !new_inode)
                goto try_again;
                goto try_again;
        if (old_de->inode != old_inode->i_ino)
        if (old_de->inode != old_inode->i_ino)
                goto try_again;
                goto try_again;
/* ok, that's it */
/* ok, that's it */
        old_de->inode = 0;
        old_de->inode = 0;
        old_de->name_len = 0;
        old_de->name_len = 0;
        new_de->inode = old_inode->i_ino;
        new_de->inode = old_inode->i_ino;
        ext_merge_entries (old_de, pde, nde);
        ext_merge_entries (old_de, pde, nde);
        if (new_inode) {
        if (new_inode) {
                new_inode->i_nlink--;
                new_inode->i_nlink--;
                new_inode->i_dirt = 1;
                new_inode->i_dirt = 1;
        }
        }
        mark_buffer_dirty(old_bh, 1);
        mark_buffer_dirty(old_bh, 1);
        mark_buffer_dirty(new_bh, 1);
        mark_buffer_dirty(new_bh, 1);
        if (dir_bh) {
        if (dir_bh) {
                PARENT_INO(dir_bh->b_data) = new_dir->i_ino;
                PARENT_INO(dir_bh->b_data) = new_dir->i_ino;
                mark_buffer_dirty(dir_bh, 1);
                mark_buffer_dirty(dir_bh, 1);
                old_dir->i_nlink--;
                old_dir->i_nlink--;
                new_dir->i_nlink++;
                new_dir->i_nlink++;
                old_dir->i_dirt = 1;
                old_dir->i_dirt = 1;
                new_dir->i_dirt = 1;
                new_dir->i_dirt = 1;
        }
        }
        retval = 0;
        retval = 0;
end_rename:
end_rename:
        brelse(dir_bh);
        brelse(dir_bh);
        brelse(old_bh);
        brelse(old_bh);
        brelse(new_bh);
        brelse(new_bh);
        iput(old_inode);
        iput(old_inode);
        iput(new_inode);
        iput(new_inode);
        iput(old_dir);
        iput(old_dir);
        iput(new_dir);
        iput(new_dir);
        return retval;
        return retval;
}
}
 
 
/*
/*
 * Ok, rename also locks out other renames, as they can change the parent of
 * 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
 * a directory, and we don't want any races. Other races are checked for by
 * "do_rename()", which restarts if there are inconsistencies.
 * "do_rename()", which restarts if there are inconsistencies.
 *
 *
 * Note that there is no race between different filesystems: it's only within
 * 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
 * the same device that races occur: many renames can happen at once, as long
 * as they are on different partitions.
 * as they are on different partitions.
 */
 */
int ext_rename(struct inode * old_dir, const char * old_name, int old_len,
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,
        struct inode * new_dir, const char * new_name, int new_len,
        int must_be_dir)
        int must_be_dir)
{
{
        static struct wait_queue * wait = NULL;
        static struct wait_queue * wait = NULL;
        static int lock = 0;
        static int lock = 0;
        int result;
        int result;
 
 
        while (lock)
        while (lock)
                sleep_on(&wait);
                sleep_on(&wait);
        lock = 1;
        lock = 1;
        result = do_ext_rename(old_dir, old_name, old_len,
        result = do_ext_rename(old_dir, old_name, old_len,
                new_dir, new_name, new_len);
                new_dir, new_name, new_len);
        lock = 0;
        lock = 0;
        wake_up(&wait);
        wake_up(&wait);
        return result;
        return result;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.