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

Subversion Repositories or1k_old

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

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

Rev 1765 Rev 1782
/*
/*
 *  linux/fs/xiafs/truncate.c
 *  linux/fs/xiafs/truncate.c
 *
 *
 *  Copyright (C) Q. Frank Xia, 1993.
 *  Copyright (C) Q. Frank Xia, 1993.
 *
 *
 *  Based on Linus' minix/truncate.c
 *  Based on Linus' minix/truncate.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/errno.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/sched.h>
#include <linux/xia_fs.h>
#include <linux/xia_fs.h>
#include <linux/stat.h>
#include <linux/stat.h>
#include <linux/fcntl.h>
#include <linux/fcntl.h>
 
 
#include "xiafs_mac.h"
#include "xiafs_mac.h"
 
 
/*
/*
 * Linus' comment:
 * Linus' comment:
 *
 *
 * Truncate has the most races in the whole filesystem: coding it is
 * Truncate has the most races in the whole filesystem: coding it is
 * a pain in the a**. Especially as I don't do any locking...
 * a pain in the a**. Especially as I don't do any locking...
 *
 *
 * The code may look a bit weird, but that's just because I've tried to
 * The code may look a bit weird, but that's just because I've tried to
 * handle things like file-size changes in a somewhat graceful manner.
 * handle things like file-size changes in a somewhat graceful manner.
 * Anyway, truncating a file at the same time somebody else writes to it
 * Anyway, truncating a file at the same time somebody else writes to it
 * is likely to result in pretty weird behaviour...
 * is likely to result in pretty weird behaviour...
 *
 *
 * The new code handles normal truncates (size = 0) as well as the more
 * The new code handles normal truncates (size = 0) as well as the more
 * general case (size = XXX). I hope.
 * general case (size = XXX). I hope.
 */
 */
 
 
#define DT_ZONE         ((inode->i_size + XIAFS_ZSIZE(inode->i_sb) - 1) \
#define DT_ZONE         ((inode->i_size + XIAFS_ZSIZE(inode->i_sb) - 1) \
                         >> XIAFS_ZSIZE_BITS(inode->i_sb) )
                         >> XIAFS_ZSIZE_BITS(inode->i_sb) )
 
 
static int trunc_direct(struct inode * inode)
static int trunc_direct(struct inode * inode)
{
{
    u_long * lp;
    u_long * lp;
    struct buffer_head * bh;
    struct buffer_head * bh;
    int i, tmp;
    int i, tmp;
    int retry = 0;
    int retry = 0;
 
 
repeat:
repeat:
    for (i = DT_ZONE ; i < 8 ; i++) {
    for (i = DT_ZONE ; i < 8 ; i++) {
        if (i < DT_ZONE)
        if (i < DT_ZONE)
            goto repeat;
            goto repeat;
        lp=i + inode->u.xiafs_i.i_zone;
        lp=i + inode->u.xiafs_i.i_zone;
        if (!(tmp = *lp))
        if (!(tmp = *lp))
            continue;
            continue;
        bh = getblk(inode->i_dev, tmp, XIAFS_ZSIZE(inode->i_sb));
        bh = getblk(inode->i_dev, tmp, XIAFS_ZSIZE(inode->i_sb));
        if (i < DT_ZONE) {
        if (i < DT_ZONE) {
            brelse(bh);
            brelse(bh);
            goto repeat;
            goto repeat;
        }
        }
        if ((bh && bh->b_count != 1) || tmp != *lp)
        if ((bh && bh->b_count != 1) || tmp != *lp)
            retry = 1;
            retry = 1;
        else {
        else {
            *lp = 0;
            *lp = 0;
            inode->i_dirt = 1;
            inode->i_dirt = 1;
            inode->i_blocks-=2 << XIAFS_ZSHIFT(inode->i_sb);
            inode->i_blocks-=2 << XIAFS_ZSHIFT(inode->i_sb);
            xiafs_free_zone(inode->i_sb, tmp);
            xiafs_free_zone(inode->i_sb, tmp);
        }
        }
        brelse(bh);
        brelse(bh);
    }
    }
    return retry;
    return retry;
}
}
 
 
static int trunc_indirect(struct inode * inode, int addr_off, u_long * lp)
static int trunc_indirect(struct inode * inode, int addr_off, u_long * lp)
{
{
 
 
#define INDT_ZONE       (DT_ZONE - addr_off)
#define INDT_ZONE       (DT_ZONE - addr_off)
 
 
    struct buffer_head * bh, * ind_bh;
    struct buffer_head * bh, * ind_bh;
    int i, tmp;
    int i, tmp;
    u_long * indp;
    u_long * indp;
    int retry = 0;
    int retry = 0;
 
 
    if ( !(tmp=*lp) )
    if ( !(tmp=*lp) )
        return 0;
        return 0;
    ind_bh = bread(inode->i_dev, tmp, XIAFS_ZSIZE(inode->i_sb));
    ind_bh = bread(inode->i_dev, tmp, XIAFS_ZSIZE(inode->i_sb));
    if (tmp != *lp) {
    if (tmp != *lp) {
        brelse(ind_bh);
        brelse(ind_bh);
        return 1;
        return 1;
    }
    }
    if (!ind_bh) {
    if (!ind_bh) {
        *lp = 0;
        *lp = 0;
        return 0;
        return 0;
    }
    }
repeat:
repeat:
    for (i = INDT_ZONE<0?0:INDT_ZONE; i < XIAFS_ADDRS_PER_Z(inode->i_sb); i++) {
    for (i = INDT_ZONE<0?0:INDT_ZONE; i < XIAFS_ADDRS_PER_Z(inode->i_sb); i++) {
        if (i < INDT_ZONE)
        if (i < INDT_ZONE)
            goto repeat;
            goto repeat;
        indp = i+(u_long *) ind_bh->b_data;
        indp = i+(u_long *) ind_bh->b_data;
        if (!(tmp=*indp))
        if (!(tmp=*indp))
            continue;
            continue;
        bh = getblk(inode->i_dev, tmp, XIAFS_ZSIZE(inode->i_sb));
        bh = getblk(inode->i_dev, tmp, XIAFS_ZSIZE(inode->i_sb));
        if (i < INDT_ZONE) {
        if (i < INDT_ZONE) {
            brelse(bh);
            brelse(bh);
            goto repeat;
            goto repeat;
        }
        }
        if ((bh && bh->b_count != 1) || tmp != *indp)
        if ((bh && bh->b_count != 1) || tmp != *indp)
            retry = 1;
            retry = 1;
        else {
        else {
            *indp = 0;
            *indp = 0;
            mark_buffer_dirty(ind_bh, 1);
            mark_buffer_dirty(ind_bh, 1);
            inode->i_blocks-= 2 << XIAFS_ZSHIFT(inode->i_sb);
            inode->i_blocks-= 2 << XIAFS_ZSHIFT(inode->i_sb);
            xiafs_free_zone(inode->i_sb, tmp);
            xiafs_free_zone(inode->i_sb, tmp);
        }
        }
        brelse(bh);
        brelse(bh);
    }
    }
    indp = (u_long *) ind_bh->b_data;
    indp = (u_long *) ind_bh->b_data;
    for (i = 0; i < XIAFS_ADDRS_PER_Z(inode->i_sb) && !(*indp++); i++) ;
    for (i = 0; i < XIAFS_ADDRS_PER_Z(inode->i_sb) && !(*indp++); i++) ;
    if (i >= XIAFS_ADDRS_PER_Z(inode->i_sb)) {
    if (i >= XIAFS_ADDRS_PER_Z(inode->i_sb)) {
      if (ind_bh->b_count != 1)
      if (ind_bh->b_count != 1)
           retry = 1;
           retry = 1;
      else {
      else {
          tmp = *lp;
          tmp = *lp;
          *lp = 0;
          *lp = 0;
          inode->i_blocks-= 2 << XIAFS_ZSHIFT(inode->i_sb);
          inode->i_blocks-= 2 << XIAFS_ZSHIFT(inode->i_sb);
          xiafs_free_zone(inode->i_sb, tmp);
          xiafs_free_zone(inode->i_sb, tmp);
      }
      }
    }
    }
    brelse(ind_bh);
    brelse(ind_bh);
    return retry;
    return retry;
}
}
 
 
static int trunc_dindirect(struct inode * inode)
static int trunc_dindirect(struct inode * inode)
{
{
 
 
#define DINDT_ZONE \
#define DINDT_ZONE \
    ((DT_ZONE-XIAFS_ADDRS_PER_Z(inode->i_sb)-8)>>XIAFS_ADDRS_PER_Z_BITS(inode->i_sb))
    ((DT_ZONE-XIAFS_ADDRS_PER_Z(inode->i_sb)-8)>>XIAFS_ADDRS_PER_Z_BITS(inode->i_sb))
 
 
    int i, tmp;
    int i, tmp;
    struct buffer_head * dind_bh;
    struct buffer_head * dind_bh;
    u_long * dindp, * lp;
    u_long * dindp, * lp;
    int retry = 0;
    int retry = 0;
 
 
    lp = &(inode->u.xiafs_i.i_dind_zone);
    lp = &(inode->u.xiafs_i.i_dind_zone);
    if (!(tmp = *lp))
    if (!(tmp = *lp))
        return 0;
        return 0;
    dind_bh = bread(inode->i_dev, tmp, XIAFS_ZSIZE(inode->i_sb));
    dind_bh = bread(inode->i_dev, tmp, XIAFS_ZSIZE(inode->i_sb));
    if (tmp != *lp) {
    if (tmp != *lp) {
        brelse(dind_bh);
        brelse(dind_bh);
        return 1;
        return 1;
    }
    }
    if (!dind_bh) {
    if (!dind_bh) {
        *lp = 0;
        *lp = 0;
        return 0;
        return 0;
    }
    }
repeat:
repeat:
    for (i=DINDT_ZONE<0?0:DINDT_ZONE ; i < XIAFS_ADDRS_PER_Z(inode->i_sb) ; i ++) {
    for (i=DINDT_ZONE<0?0:DINDT_ZONE ; i < XIAFS_ADDRS_PER_Z(inode->i_sb) ; i ++) {
        if (i < DINDT_ZONE)
        if (i < DINDT_ZONE)
            goto repeat;
            goto repeat;
        dindp = i+(u_long *) dind_bh->b_data;
        dindp = i+(u_long *) dind_bh->b_data;
        retry |= trunc_indirect(inode,
        retry |= trunc_indirect(inode,
                                8+((1+i)<<XIAFS_ADDRS_PER_Z_BITS(inode->i_sb)),
                                8+((1+i)<<XIAFS_ADDRS_PER_Z_BITS(inode->i_sb)),
                                dindp);
                                dindp);
        mark_buffer_dirty(dind_bh, 1);
        mark_buffer_dirty(dind_bh, 1);
    }
    }
    dindp = (u_long *) dind_bh->b_data;
    dindp = (u_long *) dind_bh->b_data;
    for (i = 0; i < XIAFS_ADDRS_PER_Z(inode->i_sb) && !(*dindp++); i++);
    for (i = 0; i < XIAFS_ADDRS_PER_Z(inode->i_sb) && !(*dindp++); i++);
    if (i >= XIAFS_ADDRS_PER_Z(inode->i_sb)) {
    if (i >= XIAFS_ADDRS_PER_Z(inode->i_sb)) {
        if (dind_bh->b_count != 1)
        if (dind_bh->b_count != 1)
            retry = 1;
            retry = 1;
        else {
        else {
            tmp = *lp;
            tmp = *lp;
            *lp = 0;
            *lp = 0;
            inode->i_dirt = 1;
            inode->i_dirt = 1;
            inode->i_blocks-=2 << XIAFS_ZSHIFT(inode->i_sb);
            inode->i_blocks-=2 << XIAFS_ZSHIFT(inode->i_sb);
            xiafs_free_zone(inode->i_sb, tmp);
            xiafs_free_zone(inode->i_sb, tmp);
        }
        }
    }
    }
    brelse(dind_bh);
    brelse(dind_bh);
    return retry;
    return retry;
}
}
 
 
void xiafs_truncate(struct inode * inode)
void xiafs_truncate(struct inode * inode)
{
{
    int retry;
    int retry;
 
 
    if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
    if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
          S_ISLNK(inode->i_mode)))
          S_ISLNK(inode->i_mode)))
        return;
        return;
    while (1) {
    while (1) {
        retry = trunc_direct(inode);
        retry = trunc_direct(inode);
        retry |= trunc_indirect(inode, 8, &(inode->u.xiafs_i.i_ind_zone));
        retry |= trunc_indirect(inode, 8, &(inode->u.xiafs_i.i_ind_zone));
        retry |= trunc_dindirect(inode);
        retry |= trunc_dindirect(inode);
        if (!retry)
        if (!retry)
            break;
            break;
        current->counter = 0;
        current->counter = 0;
        schedule();
        schedule();
    }
    }
    inode->i_ctime = inode->i_mtime = CURRENT_TIME;
    inode->i_ctime = inode->i_mtime = CURRENT_TIME;
    inode->i_dirt = 1;
    inode->i_dirt = 1;
}
}
 
 

powered by: WebSVN 2.1.0

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