Import 2.3.30pre7
[davej-history.git] / fs / ufs / dir.c
blobf2be33cfa5a088de4cd555869a37a3c23aaf6afe
1 /*
2 * linux/fs/ufs/ufs_dir.c
4 * Copyright (C) 1996
5 * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu)
6 * Laboratory for Computer Science Research Computing Facility
7 * Rutgers, The State University of New Jersey
9 * swab support by Francois-Rene Rideau <fare@tunes.org> 19970406
11 * 4.4BSD (FreeBSD) support added on February 1st 1998 by
12 * Niels Kristian Bech Jensen <nkbj@image.dk> partially based
13 * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>.
16 #include <linux/fs.h>
17 #include <linux/ufs_fs.h>
19 #include "swab.h"
20 #include "util.h"
22 #undef UFS_DIR_DEBUG
24 #ifdef UFS_DIR_DEBUG
25 #define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;
26 #else
27 #define UFSD(x)
28 #endif
31 * This is blatantly stolen from ext2fs
33 static int
34 ufs_readdir (struct file * filp, void * dirent, filldir_t filldir)
36 struct inode *inode = filp->f_dentry->d_inode;
37 int error = 0;
38 unsigned long offset, lblk, blk;
39 int i, stored;
40 struct buffer_head * bh;
41 struct ufs_dir_entry * de;
42 struct super_block * sb;
43 int de_reclen;
44 unsigned flags, swab;
46 sb = inode->i_sb;
47 swab = sb->u.ufs_sb.s_swab;
48 flags = sb->u.ufs_sb.s_flags;
50 UFSD(("ENTER, ino %lu f_pos %lu\n", inode->i_ino, (unsigned long) filp->f_pos))
52 stored = 0;
53 bh = NULL;
54 offset = filp->f_pos & (sb->s_blocksize - 1);
56 while (!error && !stored && filp->f_pos < inode->i_size) {
57 lblk = (filp->f_pos) >> sb->s_blocksize_bits;
58 blk = ufs_frag_map(inode, lblk);
59 if (!blk || !(bh = bread (sb->s_dev, blk, sb->s_blocksize))) {
60 /* XXX - error - skip to the next block */
61 printk("ufs_readdir: "
62 "dir inode %lu has a hole at offset %lu\n",
63 inode->i_ino, (unsigned long int)filp->f_pos);
64 filp->f_pos += sb->s_blocksize - offset;
65 continue;
68 revalidate:
69 /* If the dir block has changed since the last call to
70 * readdir(2), then we might be pointing to an invalid
71 * dirent right now. Scan from the start of the block
72 * to make sure. */
73 if (filp->f_version != inode->i_version) {
74 for (i = 0; i < sb->s_blocksize && i < offset; ) {
75 de = (struct ufs_dir_entry *)(bh->b_data + i);
76 /* It's too expensive to do a full
77 * dirent test each time round this
78 * loop, but we do have to test at
79 * least that it is non-zero. A
80 * failure will be detected in the
81 * dirent test below. */
82 de_reclen = SWAB16(de->d_reclen);
83 if (de_reclen < 1)
84 break;
85 i += de_reclen;
87 offset = i;
88 filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
89 | offset;
90 filp->f_version = inode->i_version;
93 while (!error && filp->f_pos < inode->i_size
94 && offset < sb->s_blocksize) {
95 de = (struct ufs_dir_entry *) (bh->b_data + offset);
96 /* XXX - put in a real ufs_check_dir_entry() */
97 if ((de->d_reclen == 0) || (ufs_get_de_namlen(de) == 0)) {
98 /* SWAB16() was unneeded -- compare to 0 */
99 filp->f_pos = (filp->f_pos &
100 (sb->s_blocksize - 1)) +
101 sb->s_blocksize;
102 brelse(bh);
103 return stored;
105 if (!ufs_check_dir_entry ("ufs_readdir", inode, de,
106 bh, offset)) {
107 /* On error, skip the f_pos to the
108 next block. */
109 filp->f_pos = (filp->f_pos &
110 (sb->s_blocksize - 1)) +
111 sb->s_blocksize;
112 brelse (bh);
113 return stored;
115 offset += SWAB16(de->d_reclen);
116 if (de->d_ino) {
117 /* SWAB16() was unneeded -- compare to 0 */
118 /* We might block in the next section
119 * if the data destination is
120 * currently swapped out. So, use a
121 * version stamp to detect whether or
122 * not the directory has been modified
123 * during the copy operation. */
124 unsigned long version = inode->i_version;
126 UFSD(("filldir(%s,%u)\n", de->d_name, SWAB32(de->d_ino)))
127 UFSD(("namlen %u\n", ufs_get_de_namlen(de)))
128 error = filldir(dirent, de->d_name, ufs_get_de_namlen(de),
129 filp->f_pos, SWAB32(de->d_ino));
130 if (error)
131 break;
132 if (version != inode->i_version)
133 goto revalidate;
134 stored ++;
136 filp->f_pos += SWAB16(de->d_reclen);
138 offset = 0;
139 brelse (bh);
141 UPDATE_ATIME(inode);
142 return 0;
145 int ufs_check_dir_entry (const char * function, struct inode * dir,
146 struct ufs_dir_entry * de, struct buffer_head * bh,
147 unsigned long offset)
149 struct super_block * sb;
150 const char * error_msg;
151 unsigned flags, swab;
153 sb = dir->i_sb;
154 flags = sb->u.ufs_sb.s_flags;
155 swab = sb->u.ufs_sb.s_swab;
156 error_msg = NULL;
158 if (SWAB16(de->d_reclen) < UFS_DIR_REC_LEN(1))
159 error_msg = "reclen is smaller than minimal";
160 else if (SWAB16(de->d_reclen) % 4 != 0)
161 error_msg = "reclen % 4 != 0";
162 else if (SWAB16(de->d_reclen) < UFS_DIR_REC_LEN(ufs_get_de_namlen(de)))
163 error_msg = "reclen is too small for namlen";
164 else if (dir && ((char *) de - bh->b_data) + SWAB16(de->d_reclen) >
165 dir->i_sb->s_blocksize)
166 error_msg = "directory entry across blocks";
167 else if (dir && SWAB32(de->d_ino) > (sb->u.ufs_sb.s_uspi->s_ipg * sb->u.ufs_sb.s_uspi->s_ncg))
168 error_msg = "inode out of bounds";
170 if (error_msg != NULL)
171 ufs_error (sb, function, "bad entry in directory #%lu, size %Lu: %s - "
172 "offset=%lu, inode=%lu, reclen=%d, namlen=%d",
173 dir->i_ino, dir->i_size, error_msg, offset,
174 (unsigned long) SWAB32(de->d_ino),
175 SWAB16(de->d_reclen), ufs_get_de_namlen(de));
177 return (error_msg == NULL ? 1 : 0);
180 static struct file_operations ufs_dir_operations = {
181 NULL, /* lseek */
182 NULL, /* read */
183 NULL, /* write */
184 ufs_readdir, /* readdir */
185 NULL, /* select */
186 NULL, /* ioctl */
187 NULL, /* mmap */
188 NULL, /* open */
189 NULL, /* flush */
190 NULL, /* release */
191 file_fsync, /* fsync */
192 NULL, /* fasync */
193 NULL, /* check_media_change */
194 NULL, /* revalidate */
197 struct inode_operations ufs_dir_inode_operations = {
198 &ufs_dir_operations, /* default directory file operations */
199 ufs_create, /* create */
200 ufs_lookup, /* lookup */
201 ufs_link, /* link */
202 ufs_unlink, /* unlink */
203 ufs_symlink, /* symlink */
204 ufs_mkdir, /* mkdir */
205 ufs_rmdir, /* rmdir */
206 ufs_mknod, /* mknod */
207 ufs_rename, /* rename */
208 NULL, /* readlink */
209 NULL, /* follow_link */
210 NULL, /* get_block */
211 NULL, /* readpage */
212 NULL, /* writepage */
213 NULL, /* truncate */
214 ufs_permission, /* permission */
215 NULL /* revalidate */