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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [fs/] [xiafs/] [namei.c] - Diff between revs 1765 and 1782

Only display areas with differences | Details | Blame | View Log

Rev 1765 Rev 1782
/*
/*
 *  Linux/fs/xiafs/namei.c
 *  Linux/fs/xiafs/namei.c
 *
 *
 *  Copyright (C) Q. Frank Xia, 1993.
 *  Copyright (C) Q. Frank Xia, 1993.
 *
 *
 *  Based on Linus' minix/namei.c
 *  Based on Linus' minix/namei.c
 *  Copyright (C) Linus Torvalds, 1991, 1992.
 *  Copyright (C) Linus Torvalds, 1991, 1992.
 *
 *
 *  This software may be redistributed per Linux Copyright.
 *  This software may be redistributed per Linux Copyright.
 */
 */
 
 
#include <linux/sched.h>
#include <linux/sched.h>
#include <linux/xia_fs.h>
#include <linux/xia_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>
 
 
#include "xiafs_mac.h"
#include "xiafs_mac.h"
 
 
#define RNDUP4(x)       ((3+(u_long)(x)) & ~3)
#define RNDUP4(x)       ((3+(u_long)(x)) & ~3)
/*
/*
 * 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 xiafs_match. No big problem. Match also makes
 * Thus we'll have to use xiafs_match. No big problem. Match also makes
 * some sanity tests.
 * some sanity tests.
 *
 *
 * NOTE! unlike strncmp, xiafs_match returns 1 for success, 0 for failure.
 * NOTE! unlike strncmp, xiafs_match returns 1 for success, 0 for failure.
 */
 */
static int xiafs_match(int len, const char * name, struct xiafs_direct * dep)
static int xiafs_match(int len, const char * name, struct xiafs_direct * dep)
{
{
    int i;
    int i;
 
 
    if (!dep || !dep->d_ino || len > _XIAFS_NAME_LEN)
    if (!dep || !dep->d_ino || len > _XIAFS_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 && (dep->d_name[0]=='.') && (dep->d_name[1]=='\0'))
    if (!len && (dep->d_name[0]=='.') && (dep->d_name[1]=='\0'))
        return 1;
        return 1;
    if (len != dep->d_name_len)
    if (len != dep->d_name_len)
        return 0;
        return 0;
    for (i=0; i < len; i++)
    for (i=0; i < len; i++)
        if (*name++ != dep->d_name[i])
        if (*name++ != dep->d_name[i])
            return 0;
            return 0;
    return 1;
    return 1;
}
}
 
 
/*
/*
 *      xiafs_find_entry()
 *      xiafs_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.
 */
 */
static struct buffer_head *
static struct buffer_head *
xiafs_find_entry(struct inode * inode, const char * name, int namelen,
xiafs_find_entry(struct inode * inode, const char * name, int namelen,
               struct xiafs_direct ** res_dir, struct xiafs_direct ** res_pre)
               struct xiafs_direct ** res_dir, struct xiafs_direct ** res_pre)
{
{
    int i, zones, pos;
    int i, zones, pos;
    struct buffer_head * bh;
    struct buffer_head * bh;
    struct xiafs_direct * dep, * dep_pre;
    struct xiafs_direct * dep, * dep_pre;
 
 
    *res_dir = NULL;
    *res_dir = NULL;
    if (!inode)
    if (!inode)
        return NULL;
        return NULL;
    if (namelen > _XIAFS_NAME_LEN)
    if (namelen > _XIAFS_NAME_LEN)
        return NULL;
        return NULL;
 
 
    if (inode->i_size & (XIAFS_ZSIZE(inode->i_sb) - 1)) {
    if (inode->i_size & (XIAFS_ZSIZE(inode->i_sb) - 1)) {
        printk("XIA-FS: bad dir size (%s %d)\n", WHERE_ERR);
        printk("XIA-FS: bad dir size (%s %d)\n", WHERE_ERR);
        return NULL;
        return NULL;
    }
    }
    zones=inode->i_size >> XIAFS_ZSIZE_BITS(inode->i_sb);
    zones=inode->i_size >> XIAFS_ZSIZE_BITS(inode->i_sb);
    for (i=0; i < zones; i++ ) {
    for (i=0; i < zones; i++ ) {
        bh = xiafs_bread(inode, i, 0);
        bh = xiafs_bread(inode, i, 0);
        if (!bh)
        if (!bh)
            continue;
            continue;
        dep_pre=dep=(struct xiafs_direct *)bh->b_data;
        dep_pre=dep=(struct xiafs_direct *)bh->b_data;
        if (!i && (dep->d_rec_len != 12 || !dep->d_ino ||
        if (!i && (dep->d_rec_len != 12 || !dep->d_ino ||
                   dep->d_name_len != 1 || strcmp(dep->d_name, "."))) {
                   dep->d_name_len != 1 || strcmp(dep->d_name, "."))) {
            printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
            printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
            brelse(bh);
            brelse(bh);
            return NULL;
            return NULL;
        }
        }
        pos = 0;
        pos = 0;
        while ( pos < XIAFS_ZSIZE(inode->i_sb) ) {
        while ( pos < XIAFS_ZSIZE(inode->i_sb) ) {
            if (dep->d_ino > inode->i_sb->u.xiafs_sb.s_ninodes ||
            if (dep->d_ino > inode->i_sb->u.xiafs_sb.s_ninodes ||
                dep->d_rec_len < 12 ||
                dep->d_rec_len < 12 ||
                dep->d_rec_len+(char *)dep > bh->b_data+XIAFS_ZSIZE(inode->i_sb) ||
                dep->d_rec_len+(char *)dep > bh->b_data+XIAFS_ZSIZE(inode->i_sb) ||
                dep->d_name_len + 8 > dep->d_rec_len || dep->d_name_len <= 0 ||
                dep->d_name_len + 8 > dep->d_rec_len || dep->d_name_len <= 0 ||
                dep->d_name[dep->d_name_len] ) {
                dep->d_name[dep->d_name_len] ) {
                brelse(bh);
                brelse(bh);
                return NULL;
                return NULL;
            }
            }
            if (xiafs_match(namelen, name, dep)) {
            if (xiafs_match(namelen, name, dep)) {
                *res_dir=dep;
                *res_dir=dep;
                if (res_pre)
                if (res_pre)
                    *res_pre=dep_pre;
                    *res_pre=dep_pre;
                return bh;
                return bh;
            }
            }
            pos += dep->d_rec_len;
            pos += dep->d_rec_len;
            dep_pre=dep;
            dep_pre=dep;
            dep=(struct xiafs_direct *)(bh->b_data + pos);
            dep=(struct xiafs_direct *)(bh->b_data + pos);
        }
        }
        brelse(bh);
        brelse(bh);
        if (pos > XIAFS_ZSIZE(inode->i_sb)) {
        if (pos > XIAFS_ZSIZE(inode->i_sb)) {
            printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
            printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
            return NULL;
            return NULL;
        }
        }
    }
    }
    return NULL;
    return NULL;
}
}
 
 
int xiafs_lookup(struct inode * dir, const char * name, int len,
int xiafs_lookup(struct inode * dir, const char * name, int len,
               struct inode ** result)
               struct inode ** result)
{
{
    int ino;
    int ino;
    struct xiafs_direct * dep;
    struct xiafs_direct * dep;
    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 = xiafs_find_entry(dir, name, len, &dep, NULL))) {
    if (!(bh = xiafs_find_entry(dir, name, len, &dep, NULL))) {
        iput(dir);
        iput(dir);
        return -ENOENT;
        return -ENOENT;
    }
    }
    ino = dep->d_ino;
    ino = dep->d_ino;
    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;
}
}
 
 
/*
/*
 *      xiafs_add_entry()
 *      xiafs_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 xiafs_find_entry(). It returns NULL if it failed.
 * semantics as xiafs_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 * xiafs_add_entry(struct inode * dir,
static struct buffer_head * xiafs_add_entry(struct inode * dir,
        const char * name, int namelen, struct xiafs_direct ** res_dir,
        const char * name, int namelen, struct xiafs_direct ** res_dir,
        struct xiafs_direct ** res_pre)
        struct xiafs_direct ** res_pre)
{
{
    int i, pos, offset;
    int i, pos, offset;
    struct buffer_head * bh;
    struct buffer_head * bh;
    struct xiafs_direct * de, * de_pre;
    struct xiafs_direct * de, * de_pre;
 
 
    *res_dir = NULL;
    *res_dir = NULL;
    if (!dir || !namelen || namelen > _XIAFS_NAME_LEN)
    if (!dir || !namelen || namelen > _XIAFS_NAME_LEN)
        return NULL;
        return NULL;
 
 
    if (dir->i_size & (XIAFS_ZSIZE(dir->i_sb) - 1)) {
    if (dir->i_size & (XIAFS_ZSIZE(dir->i_sb) - 1)) {
        printk("XIA-FS: bad dir size (%s %d)\n", WHERE_ERR);
        printk("XIA-FS: bad dir size (%s %d)\n", WHERE_ERR);
        return NULL;
        return NULL;
    }
    }
    pos=0;
    pos=0;
    for ( ; ; ) {
    for ( ; ; ) {
        bh =  xiafs_bread(dir, pos >> XIAFS_ZSIZE_BITS(dir->i_sb), pos ? 1:0);
        bh =  xiafs_bread(dir, pos >> XIAFS_ZSIZE_BITS(dir->i_sb), pos ? 1:0);
        if (!bh)
        if (!bh)
            return NULL;
            return NULL;
        de_pre=de=(struct xiafs_direct *)bh->b_data;
        de_pre=de=(struct xiafs_direct *)bh->b_data;
        if (!pos) {
        if (!pos) {
            if (de->d_rec_len != 12 || !de->d_ino || de->d_name_len != 1 ||
            if (de->d_rec_len != 12 || !de->d_ino || de->d_name_len != 1 ||
                strcmp(de->d_name, ".")) {
                strcmp(de->d_name, ".")) {
                printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
                printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
                brelse(bh);
                brelse(bh);
                return NULL;
                return NULL;
            }
            }
            offset = 12;
            offset = 12;
            de_pre=de=(struct xiafs_direct *)(bh->b_data+12);
            de_pre=de=(struct xiafs_direct *)(bh->b_data+12);
        } else
        } else
            offset = 0;
            offset = 0;
        while (offset < XIAFS_ZSIZE(dir->i_sb)) {
        while (offset < XIAFS_ZSIZE(dir->i_sb)) {
            if (pos >= dir->i_size) {
            if (pos >= dir->i_size) {
                de->d_ino=0;
                de->d_ino=0;
                de->d_name_len=0;
                de->d_name_len=0;
                de->d_name[0]=0;
                de->d_name[0]=0;
                de->d_rec_len=XIAFS_ZSIZE(dir->i_sb);
                de->d_rec_len=XIAFS_ZSIZE(dir->i_sb);
                dir->i_size += XIAFS_ZSIZE(dir->i_sb);
                dir->i_size += XIAFS_ZSIZE(dir->i_sb);
                dir->i_dirt = 1;
                dir->i_dirt = 1;
            } else {
            } else {
                if (de->d_ino > dir->i_sb->u.xiafs_sb.s_ninodes ||
                if (de->d_ino > dir->i_sb->u.xiafs_sb.s_ninodes ||
                    de->d_rec_len < 12 ||
                    de->d_rec_len < 12 ||
                    (char *)de+de->d_rec_len > bh->b_data+XIAFS_ZSIZE(dir->i_sb) ||
                    (char *)de+de->d_rec_len > bh->b_data+XIAFS_ZSIZE(dir->i_sb) ||
                    de->d_name_len + 8 > de->d_rec_len ||
                    de->d_name_len + 8 > de->d_rec_len ||
                    de->d_name[de->d_name_len]) {
                    de->d_name[de->d_name_len]) {
                    printk("XIA-FS: bad directory entry (%s %d)\n", WHERE_ERR);
                    printk("XIA-FS: bad directory entry (%s %d)\n", WHERE_ERR);
                    brelse(bh);
                    brelse(bh);
                    return NULL;
                    return NULL;
                }
                }
                if (de->d_ino &&
                if (de->d_ino &&
                    RNDUP4(de->d_name_len)+RNDUP4(namelen)+16<=de->d_rec_len) {
                    RNDUP4(de->d_name_len)+RNDUP4(namelen)+16<=de->d_rec_len) {
                    i=RNDUP4(de->d_name_len)+8;
                    i=RNDUP4(de->d_name_len)+8;
                    de_pre=de;
                    de_pre=de;
                    de=(struct xiafs_direct *)(i+(u_char *)de_pre);
                    de=(struct xiafs_direct *)(i+(u_char *)de_pre);
                    de->d_ino=0;
                    de->d_ino=0;
                    de->d_rec_len=de_pre->d_rec_len-i;
                    de->d_rec_len=de_pre->d_rec_len-i;
                    de_pre->d_rec_len=i;
                    de_pre->d_rec_len=i;
                }
                }
            }
            }
            if (!de->d_ino && RNDUP4(namelen)+8 <= de->d_rec_len) {
            if (!de->d_ino && RNDUP4(namelen)+8 <= de->d_rec_len) {
                /*
                /*
                 * XXX all times should be set by caller upon successful
                 * XXX all times should be set by caller upon successful
                 * completion.
                 * completion.
                 */
                 */
                dir->i_ctime = dir->i_mtime = CURRENT_TIME;
                dir->i_ctime = dir->i_mtime = CURRENT_TIME;
                dir->i_dirt = 1;
                dir->i_dirt = 1;
                memcpy(de->d_name, name, namelen);
                memcpy(de->d_name, name, namelen);
                de->d_name[namelen]=0;
                de->d_name[namelen]=0;
                de->d_name_len=namelen;
                de->d_name_len=namelen;
                mark_buffer_dirty(bh, 1);
                mark_buffer_dirty(bh, 1);
                *res_dir = de;
                *res_dir = de;
                if (res_pre)
                if (res_pre)
                    *res_pre = de_pre;
                    *res_pre = de_pre;
                return bh;
                return bh;
            }
            }
            offset+=de->d_rec_len;
            offset+=de->d_rec_len;
            de_pre=de;
            de_pre=de;
            de=(struct xiafs_direct *)(bh->b_data+offset);
            de=(struct xiafs_direct *)(bh->b_data+offset);
        }
        }
        brelse(bh);
        brelse(bh);
        if (offset > XIAFS_ZSIZE(dir->i_sb)) {
        if (offset > XIAFS_ZSIZE(dir->i_sb)) {
            printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
            printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
            return NULL;
            return NULL;
        }
        }
        pos+=XIAFS_ZSIZE(dir->i_sb);
        pos+=XIAFS_ZSIZE(dir->i_sb);
    }
    }
    return NULL;
    return NULL;
}
}
 
 
int xiafs_create(struct inode * dir, const char * name, int len, int mode,
int xiafs_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 xiafs_direct * de;
    struct xiafs_direct * de;
 
 
    *result = NULL;
    *result = NULL;
    if (!dir)
    if (!dir)
        return -ENOENT;
        return -ENOENT;
    inode = xiafs_new_inode(dir);
    inode = xiafs_new_inode(dir);
    if (!inode) {
    if (!inode) {
        iput(dir);
        iput(dir);
        return -ENOSPC;
        return -ENOSPC;
    }
    }
    inode->i_op = &xiafs_file_inode_operations;
    inode->i_op = &xiafs_file_inode_operations;
    inode->i_mode = mode;
    inode->i_mode = mode;
    inode->i_dirt = 1;
    inode->i_dirt = 1;
    bh = xiafs_add_entry(dir, name, len, &de, NULL);
    bh = xiafs_add_entry(dir, name, len, &de, NULL);
    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->d_ino = inode->i_ino;
    de->d_ino = 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 xiafs_mknod(struct inode *dir, const char *name, int len, int mode, int rdev)
int xiafs_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 xiafs_direct * de;
    struct xiafs_direct * de;
 
 
    if (!dir)
    if (!dir)
        return -ENOENT;
        return -ENOENT;
    bh = xiafs_find_entry(dir,name,len,&de, NULL);
    bh = xiafs_find_entry(dir,name,len,&de, NULL);
    if (bh) {
    if (bh) {
        brelse(bh);
        brelse(bh);
        iput(dir);
        iput(dir);
        return -EEXIST;
        return -EEXIST;
    }
    }
    inode = xiafs_new_inode(dir);
    inode = xiafs_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 = &xiafs_file_inode_operations;
        inode->i_op = &xiafs_file_inode_operations;
    else if (S_ISDIR(inode->i_mode)) {
    else if (S_ISDIR(inode->i_mode)) {
        inode->i_op = &xiafs_dir_inode_operations;
        inode->i_op = &xiafs_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 = &xiafs_symlink_inode_operations;
        inode->i_op = &xiafs_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);
    inode->i_atime = inode->i_ctime = inode->i_atime = CURRENT_TIME;
    inode->i_atime = inode->i_ctime = inode->i_atime = CURRENT_TIME;
    inode->i_dirt = 1;
    inode->i_dirt = 1;
    bh = xiafs_add_entry(dir, name, len, &de, NULL);
    bh = xiafs_add_entry(dir, name, len, &de, NULL);
    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->d_ino = inode->i_ino;
    de->d_ino = 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 xiafs_mkdir(struct inode * dir, const char * name, int len, int mode)
int xiafs_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 xiafs_direct * de;
    struct xiafs_direct * de;
 
 
    bh = xiafs_find_entry(dir,name,len,&de, NULL);
    bh = xiafs_find_entry(dir,name,len,&de, NULL);
    if (bh) {
    if (bh) {
        brelse(bh);
        brelse(bh);
        iput(dir);
        iput(dir);
        return -EEXIST;
        return -EEXIST;
    }
    }
    if (dir->i_nlink > 64000) {
    if (dir->i_nlink > 64000) {
        iput(dir);
        iput(dir);
        return -EMLINK;
        return -EMLINK;
    }
    }
    inode = xiafs_new_inode(dir);
    inode = xiafs_new_inode(dir);
    if (!inode) {
    if (!inode) {
        iput(dir);
        iput(dir);
        return -ENOSPC;
        return -ENOSPC;
    }
    }
    inode->i_op = &xiafs_dir_inode_operations;
    inode->i_op = &xiafs_dir_inode_operations;
    inode->i_size = XIAFS_ZSIZE(dir->i_sb);
    inode->i_size = XIAFS_ZSIZE(dir->i_sb);
    inode->i_atime = inode->i_ctime = inode->i_mtime = CURRENT_TIME;
    inode->i_atime = inode->i_ctime = inode->i_mtime = CURRENT_TIME;
    inode->i_dirt = 1;
    inode->i_dirt = 1;
    dir_block = xiafs_bread(inode,0,1);
    dir_block = xiafs_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 xiafs_direct *) dir_block->b_data;
    de = (struct xiafs_direct *) dir_block->b_data;
    de->d_ino=inode->i_ino;
    de->d_ino=inode->i_ino;
    strcpy(de->d_name,".");
    strcpy(de->d_name,".");
    de->d_name_len=1;
    de->d_name_len=1;
    de->d_rec_len=12;
    de->d_rec_len=12;
    de =(struct xiafs_direct *)(12 + dir_block->b_data);
    de =(struct xiafs_direct *)(12 + dir_block->b_data);
    de->d_ino = dir->i_ino;
    de->d_ino = dir->i_ino;
    strcpy(de->d_name,"..");
    strcpy(de->d_name,"..");
    de->d_name_len=2;
    de->d_name_len=2;
    de->d_rec_len=XIAFS_ZSIZE(dir->i_sb)-12;
    de->d_rec_len=XIAFS_ZSIZE(dir->i_sb)-12;
    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 & S_IRWXUGO & ~current->fs->umask);
    inode->i_mode = S_IFDIR | (mode & S_IRWXUGO & ~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 = xiafs_add_entry(dir, name, len, &de, NULL);
    bh = xiafs_add_entry(dir, name, len, &de, NULL);
    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->d_ino = inode->i_ino;
    de->d_ino = 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)
{
{
    int i, zones, offset;
    int i, zones, offset;
    struct buffer_head * bh;
    struct buffer_head * bh;
    struct xiafs_direct * de;
    struct xiafs_direct * de;
 
 
    if (inode->i_size & (XIAFS_ZSIZE(inode->i_sb)-1) ) {
    if (inode->i_size & (XIAFS_ZSIZE(inode->i_sb)-1) ) {
        printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
        printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
        return 1;
        return 1;
    }
    }
 
 
    zones=inode->i_size >> XIAFS_ZSIZE_BITS(inode->i_sb);
    zones=inode->i_size >> XIAFS_ZSIZE_BITS(inode->i_sb);
    for (i=0; i < zones; i++) {
    for (i=0; i < zones; i++) {
        bh =  xiafs_bread(inode, i, 0);
        bh =  xiafs_bread(inode, i, 0);
        if (!i) {
        if (!i) {
            if (!bh) {
            if (!bh) {
                printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
                printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
                return 1;
                return 1;
            }
            }
            de=(struct xiafs_direct *)bh->b_data;
            de=(struct xiafs_direct *)bh->b_data;
            if (de->d_ino != inode->i_ino || strcmp(".", de->d_name) ||
            if (de->d_ino != inode->i_ino || strcmp(".", de->d_name) ||
                    de->d_rec_len != 12 ) {
                    de->d_rec_len != 12 ) {
                printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
                printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
                brelse(bh);
                brelse(bh);
                return 1;
                return 1;
            }
            }
            de=(struct xiafs_direct *)(12 + bh->b_data);
            de=(struct xiafs_direct *)(12 + bh->b_data);
            if (!de->d_ino || strcmp("..", de->d_name)) {
            if (!de->d_ino || strcmp("..", de->d_name)) {
                printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
                printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
                brelse(bh);
                brelse(bh);
                return 1;
                return 1;
            }
            }
            offset=de->d_rec_len+12;
            offset=de->d_rec_len+12;
        }
        }
        else
        else
            offset = 0;
            offset = 0;
        if (!bh)
        if (!bh)
            continue;
            continue;
        while (offset < XIAFS_ZSIZE(inode->i_sb)) {
        while (offset < XIAFS_ZSIZE(inode->i_sb)) {
            de=(struct xiafs_direct *)(bh->b_data+offset);
            de=(struct xiafs_direct *)(bh->b_data+offset);
            if (de->d_ino > inode->i_sb->u.xiafs_sb.s_ninodes ||
            if (de->d_ino > inode->i_sb->u.xiafs_sb.s_ninodes ||
                de->d_rec_len < 12 ||
                de->d_rec_len < 12 ||
                (char *)de+de->d_rec_len > bh->b_data+XIAFS_ZSIZE(inode->i_sb) ||
                (char *)de+de->d_rec_len > bh->b_data+XIAFS_ZSIZE(inode->i_sb) ||
                de->d_name_len + 8 > de->d_rec_len ||
                de->d_name_len + 8 > de->d_rec_len ||
                de->d_name[de->d_name_len]) {
                de->d_name[de->d_name_len]) {
                printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
                printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR);
                brelse(bh);
                brelse(bh);
                return 1;
                return 1;
            }
            }
            if (de->d_ino) {
            if (de->d_ino) {
                brelse(bh);
                brelse(bh);
                return 0;
                return 0;
            }
            }
            offset+=de->d_rec_len;
            offset+=de->d_rec_len;
        }
        }
        brelse(bh);
        brelse(bh);
    }
    }
    return 1;
    return 1;
}
}
 
 
static void xiafs_rm_entry(struct xiafs_direct *de, struct xiafs_direct * de_pre)
static void xiafs_rm_entry(struct xiafs_direct *de, struct xiafs_direct * de_pre)
{
{
    if (de==de_pre) {
    if (de==de_pre) {
        de->d_ino=0;
        de->d_ino=0;
        return;
        return;
    }
    }
    while (de_pre->d_rec_len+(u_char *)de_pre < (u_char *)de) {
    while (de_pre->d_rec_len+(u_char *)de_pre < (u_char *)de) {
        if (de_pre->d_rec_len < 12) {
        if (de_pre->d_rec_len < 12) {
            printk("XIA-FS: bad directory entry (%s %d)\n", WHERE_ERR);
            printk("XIA-FS: bad directory entry (%s %d)\n", WHERE_ERR);
            return;
            return;
        }
        }
        de_pre=(struct xiafs_direct *)(de_pre->d_rec_len+(u_char *)de_pre);
        de_pre=(struct xiafs_direct *)(de_pre->d_rec_len+(u_char *)de_pre);
    }
    }
    if (de_pre->d_rec_len+(u_char *)de_pre > (u_char *)de) {
    if (de_pre->d_rec_len+(u_char *)de_pre > (u_char *)de) {
        printk("XIA-FS: bad directory entry (%s %d)\n", WHERE_ERR);
        printk("XIA-FS: bad directory entry (%s %d)\n", WHERE_ERR);
        return;
        return;
    }
    }
    de_pre->d_rec_len+=de->d_rec_len;
    de_pre->d_rec_len+=de->d_rec_len;
}
}
 
 
int xiafs_rmdir(struct inode * dir, const char * name, int len)
int xiafs_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 xiafs_direct * de, * de_pre;
    struct xiafs_direct * de, * de_pre;
 
 
    inode = NULL;
    inode = NULL;
    bh = xiafs_find_entry(dir, name, len, &de, &de_pre);
    bh = xiafs_find_entry(dir, name, len, &de, &de_pre);
    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->d_ino)))
    if (!(inode = iget(dir->i_sb, de->d_ino)))
        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("XIA-FS: empty directory has nlink!=2 (%s %d)\n", WHERE_ERR);
        printk("XIA-FS: empty directory has nlink!=2 (%s %d)\n", WHERE_ERR);
    xiafs_rm_entry(de, de_pre);
    xiafs_rm_entry(de, de_pre);
    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 xiafs_unlink(struct inode * dir, const char * name, int len)
int xiafs_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 xiafs_direct * de, * de_pre;
    struct xiafs_direct * de, * de_pre;
 
 
repeat:
repeat:
    retval = -ENOENT;
    retval = -ENOENT;
    inode = NULL;
    inode = NULL;
    bh = xiafs_find_entry(dir, name, len, &de, &de_pre);
    bh = xiafs_find_entry(dir, name, len, &de, &de_pre);
    if (!bh)
    if (!bh)
        goto end_unlink;
        goto end_unlink;
    if (!(inode = iget(dir->i_sb, de->d_ino)))
    if (!(inode = iget(dir->i_sb, de->d_ino)))
        goto end_unlink;
        goto end_unlink;
    retval = -EPERM;
    retval = -EPERM;
    if (S_ISDIR(inode->i_mode))
    if (S_ISDIR(inode->i_mode))
        goto end_unlink;
        goto end_unlink;
    if (de->d_ino != inode->i_ino) {
    if (de->d_ino != inode->i_ino) {
        iput(inode);
        iput(inode);
        brelse(bh);
        brelse(bh);
        current->counter = 0;
        current->counter = 0;
        schedule();
        schedule();
        goto repeat;
        goto repeat;
    }
    }
    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 (!inode->i_nlink) {
    if (!inode->i_nlink) {
        printk("XIA-FS: Deleting nonexistent file (%s %d)\n", WHERE_ERR);
        printk("XIA-FS: Deleting nonexistent file (%s %d)\n", WHERE_ERR);
        inode->i_nlink=1;
        inode->i_nlink=1;
    }
    }
    xiafs_rm_entry(de, de_pre);
    xiafs_rm_entry(de, de_pre);
    mark_buffer_dirty(bh, 1);
    mark_buffer_dirty(bh, 1);
    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;
    inode->i_nlink--;
    inode->i_nlink--;
    inode->i_dirt = 1;
    inode->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 xiafs_symlink(struct inode * dir, const char * name,
int xiafs_symlink(struct inode * dir, const char * name,
                int len, const char * symname)
                int len, const char * symname)
{
{
    struct xiafs_direct * de;
    struct xiafs_direct * 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;
 
 
    bh = xiafs_find_entry(dir,name,len, &de, NULL);
    bh = xiafs_find_entry(dir,name,len, &de, NULL);
    if (bh) {
    if (bh) {
        brelse(bh);
        brelse(bh);
        iput(dir);
        iput(dir);
        return -EEXIST;
        return -EEXIST;
    }
    }
    if (!(inode = xiafs_new_inode(dir))) {
    if (!(inode = xiafs_new_inode(dir))) {
        iput(dir);
        iput(dir);
        return -ENOSPC;
        return -ENOSPC;
    }
    }
    inode->i_mode = S_IFLNK | S_IRWXUGO;
    inode->i_mode = S_IFLNK | S_IRWXUGO;
    inode->i_op = &xiafs_symlink_inode_operations;
    inode->i_op = &xiafs_symlink_inode_operations;
    name_block = xiafs_bread(inode,0,1);
    name_block = xiafs_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;
    }
    }
    for (i = 0; i < BLOCK_SIZE-1 && (c=*symname++); i++)
    for (i = 0; i < BLOCK_SIZE-1 && (c=*symname++); i++)
        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 = xiafs_add_entry(dir, name, len, &de, NULL);
    bh = xiafs_add_entry(dir, name, len, &de, NULL);
    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->d_ino = inode->i_ino;
    de->d_ino = 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 xiafs_link(struct inode * oldinode, struct inode * dir,
int xiafs_link(struct inode * oldinode, struct inode * dir,
             const char * name, int len)
             const char * name, int len)
{
{
    struct xiafs_direct * de;
    struct xiafs_direct * 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 > 64000) {
    if (oldinode->i_nlink > 64000) {
        iput(oldinode);
        iput(oldinode);
        iput(dir);
        iput(dir);
        return -EMLINK;
        return -EMLINK;
    }
    }
    bh = xiafs_find_entry(dir, name, len, &de, NULL);
    bh = xiafs_find_entry(dir, name, len, &de, NULL);
    if (bh) {
    if (bh) {
        brelse(bh);
        brelse(bh);
        iput(dir);
        iput(dir);
        iput(oldinode);
        iput(oldinode);
        return -EEXIST;
        return -EEXIST;
    }
    }
    bh = xiafs_add_entry(dir, name, len, &de, NULL);
    bh = xiafs_add_entry(dir, name, len, &de, NULL);
    if (!bh) {
    if (!bh) {
        iput(dir);
        iput(dir);
        iput(oldinode);
        iput(oldinode);
        return -ENOSPC;
        return -ENOSPC;
    }
    }
    de->d_ino = oldinode->i_ino;
    de->d_ino = 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 (xiafs_lookup(new_inode,"..",2,&new_inode))
        if (xiafs_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 xiafs_direct *) ((u_char *)(buffer) + 12))->d_ino)
    (((struct xiafs_direct *) ((u_char *)(buffer) + 12))->d_ino)
 
 
/*
/*
 * rename uses retry to avoid race-conditions: at least they should be minimal.
 * rename uses retry 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_xiafs_rename(struct inode * old_dir, const char * old_name,
static int do_xiafs_rename(struct inode * old_dir, const char * old_name,
                         int old_len, struct inode * new_dir,
                         int old_len, struct inode * new_dir,
                         const char * new_name, int new_len,
                         const char * new_name, int new_len,
                         int must_be_dir)
                         int must_be_dir)
{
{
    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 xiafs_direct * old_de, * old_de_pre, * new_de, * new_de_pre;
    struct xiafs_direct * old_de, * old_de_pre, * new_de, * new_de_pre;
    int retval;
    int retval;
 
 
try_again:
try_again:
    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 = xiafs_find_entry(old_dir, old_name, old_len, &old_de, &old_de_pre);
    old_bh = xiafs_find_entry(old_dir, old_name, old_len, &old_de, &old_de_pre);
    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->d_ino, 0); /* don't cross mnt-points */
    old_inode = __iget(old_dir->i_sb, old_de->d_ino, 0); /* don't cross mnt-points */
    if (!old_inode)
    if (!old_inode)
        goto end_rename;
        goto end_rename;
    if (must_be_dir && !S_ISDIR(old_inode->i_mode))
    if (must_be_dir && !S_ISDIR(old_inode->i_mode))
        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 = xiafs_find_entry(new_dir, new_name, new_len, &new_de, NULL);
    new_bh = xiafs_find_entry(new_dir, new_name, new_len, &new_de, NULL);
    if (new_bh) {
    if (new_bh) {
        new_inode = __iget(new_dir->i_sb, new_de->d_ino, 0);
        new_inode = __iget(new_dir->i_sb, new_de->d_ino, 0);
        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 = xiafs_bread(old_inode,0,0);
        dir_bh = xiafs_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;
        retval = -EMLINK;
        retval = -EMLINK;
        if (new_dir->i_nlink > 64000)
        if (new_dir->i_nlink > 64000)
            goto end_rename;
            goto end_rename;
    }
    }
    if (!new_bh)
    if (!new_bh)
        new_bh = xiafs_add_entry(new_dir, new_name, new_len, &new_de, &new_de_pre);
        new_bh = xiafs_add_entry(new_dir, new_name, new_len, &new_de, &new_de_pre);
    retval = -ENOSPC;
    retval = -ENOSPC;
    if (!new_bh)
    if (!new_bh)
        goto end_rename;
        goto end_rename;
    /* sanity checking */
    /* sanity checking */
    if ( (new_inode && (new_de->d_ino != new_inode->i_ino))
    if ( (new_inode && (new_de->d_ino != new_inode->i_ino))
            || (new_de->d_ino && !new_inode)
            || (new_de->d_ino && !new_inode)
            || (old_de->d_ino != old_inode->i_ino)) {
            || (old_de->d_ino != old_inode->i_ino)) {
        xiafs_rm_entry(new_de, new_de_pre);
        xiafs_rm_entry(new_de, new_de_pre);
        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();
        goto try_again;
        goto try_again;
    }
    }
    xiafs_rm_entry(old_de, old_de_pre);
    xiafs_rm_entry(old_de, old_de_pre);
    new_de->d_ino = old_inode->i_ino;
    new_de->d_ino = old_inode->i_ino;
    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 xiafs_rename(struct inode * old_dir, const char * old_name, int old_len,
int xiafs_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_xiafs_rename(old_dir, old_name, old_len,
    result = do_xiafs_rename(old_dir, old_name, old_len,
                           new_dir, new_name, new_len,
                           new_dir, new_name, new_len,
                           must_be_dir);
                           must_be_dir);
    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.