ftp.nice.ch/pub/next/unix/disk/ufs-NeXT.0.3.s.tar.gz#/linux/fs/ufs/dir.c

This is dir.c in view mode; [Download] [Up]

/*
 *  linux/fs/ufs/dir.c
 *
 *  Copyright (C) 1994 Yossi Gottlieb (yogo@math.tau.ac.il)
 *
 *  Based on code and information:
 *	BSD code,  Copyright (c) 1982, 1986 Regents of the University of California.
 *		   All rights reserved.
 *	linux/fs/minix	Copyright (C) 1991, 1992, 1993  Linus Torvalds
 *
 */

#include <linux/fs.h>

#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/stat.h>
#include <linux/locks.h>

#include <asm/system.h>
#include <asm/segment.h>
#include <asm/bitops.h>

#include <linux/ufs_fs.h>
#include "ufs.h"

#define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
#define ROUND_UP(x) (((x)+3) & ~3)



struct file_operations ufs_dir_operations = {
	NULL,			/* lseek - none */
	NULL,			/* read - none */
	NULL,			/* write - none */
	ufs_readdir,
	NULL,			/* select - none */
	NULL,			/* ioctl - none */
	NULL,			/* mmap - none */
	NULL,			/* open - none */
	NULL,			/* release - none */
	file_fsync,		/* fsync - none */
	NULL,			/* fasync - none */
	NULL,
	NULL
};

struct inode_operations ufs_dir_inode_operations = {
	&ufs_dir_operations,
	NULL,			/* create - none */
	ufs_lookup,		/* lookup - none */
	NULL,			/* link - none */
	NULL,			/* unlink - none */
	NULL,			/* symlink - none */
	NULL,			/* mkdir - none */
	NULL,			/* rmdir - none */
	NULL,			/* mknod - none */
	NULL,			/* rename - none */
	NULL,			/* readlink - none */
	NULL,			/* follow_link - none */
	NULL,			/* bmap - none */
	NULL,			/* truncate - none */
	NULL,			/* permission - none */
	NULL			/* smap - none */
};


int ufs_readdir(struct inode *inode, struct file *filp, struct dirent *dirent, int count)
{
	struct ufs_sb_info *sb = &inode->i_sb->u.ufs_sb;
	int frsize = sb->fs_fsize;
	int offset;
	struct buffer_head *bh;
	struct ufs_dir_entry *de;
	u_long ino;
	u_short namlen;
	int i;
	char c;

	if (!inode || !inode->i_sb || !S_ISDIR(inode->i_mode))
		return -EBADF;
	while (filp->f_pos < inode->i_size) {
		offset = filp->f_pos & (frsize-1);
		bh = ufs_bread(inode, numfrags(sb, filp->f_pos));
		if (!bh) {
			filp->f_pos += frsize-offset;
			continue;
		}
		while (offset < frsize && filp->f_pos < inode->i_size) {
			de = (struct ufs_dir_entry *) (offset + bh->b_data);
			offset += dtohs(sb,de->d_reclen);
			filp->f_pos += dtohs(sb,de->d_reclen);
			ino = dtohl(sb,de->d_ino);
			namlen = dtohs(sb,de->d_namlen);
			if (ino) {
				for (i = 0; i < namlen; i++)
					if ((c = de->d_name[i]) != 0)
						put_fs_byte(c,i+dirent->d_name);
					else
						break;
				if (i) {
					put_fs_long(ino,&dirent->d_ino);
					put_fs_byte(0,i+dirent->d_name);
					put_fs_word(i,&dirent->d_reclen);
					brelse(bh);
					return ROUND_UP(NAME_OFFSET(dirent) + i + 1);
				}
			}
		}
		brelse(bh);
	}
	return 0;
}

static int ufs_follow_link(struct inode * dir, struct inode * inode,
	int flag, int mode, struct inode ** res_inode)
{
	int error;
	struct buffer_head *bh;
	int flink;

	*res_inode = NULL;
	if (!dir) {
		dir = current->fs->root;
		dir->i_count++;
	}
	if (!inode) {
		iput(dir);
		return -ENOENT;
	}
	if (!S_ISLNK(inode->i_mode)) {
		iput(dir);
		*res_inode = inode;
		return 0;
	}
	if (current->link_count > 5) {
		iput(inode);
		iput(dir);
		return -ELOOP;
	}
	flink = DFASTLINK(inode);
	if (!flink) {
		if (!(bh = ufs_bread(inode, 0))) {
			iput(inode);
			iput(dir);
			return -EIO;
		}
		iput(inode);
		current->link_count++;
		error = open_namei(bh->b_data,flag,mode,res_inode,dir);
		current->link_count--;
		brelse(bh);
	} else {
		iput(inode);
		current->link_count++;
		error = open_namei(inode->u.ufs_i.i_un.i_usymlink,flag,mode,res_inode,dir);
		current->link_count--;
	}
	return error;
}

static int ufs_readlink(struct inode * inode, char * buffer, int buflen)
{
	struct buffer_head *bh;
	char c;
	char *inbuf;
	int flink;
	int i;

	if (!S_ISLNK(inode->i_mode)) {
		iput(inode);
		return -EINVAL;
	}
	if (buflen > inode->i_sb->s_blocksize - 1)
		buflen = inode->i_sb->s_blocksize - 1;

	if (!(flink = DFASTLINK(inode))) {
		bh = ufs_bread(inode, 0);
		if (!bh) {
			iput(inode);
			return 0;
		}
		inbuf = bh->b_data;
	} else {
		bh = NULL;
		inbuf = inode->u.ufs_i.i_un.i_usymlink;
	}

	i = 0;
	while (i<buflen && (c = *(inbuf++))) {
		i++;
		put_fs_byte(c,buffer++);
	}

	if (bh)
		brelse(bh);
	iput(inode);
	return i;
}



struct inode_operations ufs_symlink_inode_operations = {
	NULL,
	NULL,			/* create - none */
	NULL,			/* lookup - none */
	NULL,			/* link - none */
	NULL,			/* unlink - none */
	NULL,			/* symlink - none */
	NULL,			/* mkdir - none */
	NULL,			/* rmdir - none */
	NULL,			/* mknod - none */
	NULL,			/* rename - none */
	ufs_readlink,		/* readlink - none */
	ufs_follow_link,	/* follow_link - none */
	NULL,			/* bmap - none */
	NULL,			/* truncate - none */
	NULL,			/* permission - none */
	NULL			/* smap - none */
};

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.