1 /*
2  *	fs/bfs/dir.c
3  *	BFS directory operations.
4  *	Copyright (C) 1999,2000  Tigran Aivazian <tigran@veritas.com>
5  */
6 
7 #include <linux/sched.h>
8 #include <linux/string.h>
9 #include <linux/bfs_fs.h>
10 #include <linux/locks.h>
11 
12 #include "bfs_defs.h"
13 
14 #undef DEBUG
15 
16 #ifdef DEBUG
17 #define dprintf(x...)	printf(x)
18 #else
19 #define dprintf(x...)
20 #endif
21 
22 static int bfs_add_entry(struct inode * dir, const char * name, int namelen, int ino);
23 static struct buffer_head * bfs_find_entry(struct inode * dir,
24 	const char * name, int namelen, struct bfs_dirent ** res_dir);
25 
bfs_readdir(struct file * f,void * dirent,filldir_t filldir)26 static int bfs_readdir(struct file * f, void * dirent, filldir_t filldir)
27 {
28 	struct inode * dir = f->f_dentry->d_inode;
29 	struct buffer_head * bh;
30 	struct bfs_dirent * de;
31 	kdev_t dev = dir->i_dev;
32 	unsigned int offset;
33 	int block;
34 
35 	if (f->f_pos & (BFS_DIRENT_SIZE-1)) {
36 		printf("Bad f_pos=%08lx for %s:%08lx\n", (unsigned long)f->f_pos,
37 			bdevname(dev), dir->i_ino);
38 		return -EBADF;
39 	}
40 
41 	while (f->f_pos < dir->i_size) {
42 		offset = f->f_pos & (BFS_BSIZE-1);
43 		block = dir->iu_sblock + (f->f_pos >> BFS_BSIZE_BITS);
44 		bh = sb_bread(dir->i_sb, block);
45 		if (!bh) {
46 			f->f_pos += BFS_BSIZE - offset;
47 			continue;
48 		}
49 		do {
50 			de = (struct bfs_dirent *)(bh->b_data + offset);
51 			if (de->ino) {
52 				int size = strnlen(de->name, BFS_NAMELEN);
53 				if (filldir(dirent, de->name, size, f->f_pos, de->ino, DT_UNKNOWN) < 0) {
54 					brelse(bh);
55 					return 0;
56 				}
57 			}
58 			offset += BFS_DIRENT_SIZE;
59 			f->f_pos += BFS_DIRENT_SIZE;
60 		} while (offset < BFS_BSIZE && f->f_pos < dir->i_size);
61 		brelse(bh);
62 	}
63 
64 	UPDATE_ATIME(dir);
65 	return 0;
66 }
67 
68 struct file_operations bfs_dir_operations = {
69 	read:		generic_read_dir,
70 	readdir:	bfs_readdir,
71 	fsync:		file_fsync,
72 };
73 
74 extern void dump_imap(const char *, struct super_block *);
75 
bfs_create(struct inode * dir,struct dentry * dentry,int mode)76 static int bfs_create(struct inode * dir, struct dentry * dentry, int mode)
77 {
78 	int err;
79 	struct inode * inode;
80 	struct super_block * s = dir->i_sb;
81 	unsigned long ino;
82 
83 	inode = new_inode(s);
84 	if (!inode)
85 		return -ENOSPC;
86 	ino = find_first_zero_bit(s->su_imap, s->su_lasti);
87 	if (ino > s->su_lasti) {
88 		iput(inode);
89 		return -ENOSPC;
90 	}
91 	set_bit(ino, s->su_imap);
92 	s->su_freei--;
93 	inode->i_uid = current->fsuid;
94 	inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
95 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
96 	inode->i_blocks = inode->i_blksize = 0;
97 	inode->i_op = &bfs_file_inops;
98 	inode->i_fop = &bfs_file_operations;
99 	inode->i_mapping->a_ops = &bfs_aops;
100 	inode->i_mode = mode;
101 	inode->i_ino = inode->iu_dsk_ino = ino;
102 	inode->iu_sblock = inode->iu_eblock = 0;
103 	insert_inode_hash(inode);
104         mark_inode_dirty(inode);
105 	dump_imap("create",s);
106 
107 	err = bfs_add_entry(dir, dentry->d_name.name, dentry->d_name.len, inode->i_ino);
108 	if (err) {
109 		inode->i_nlink--;
110 		mark_inode_dirty(inode);
111 		iput(inode);
112 		return err;
113 	}
114 	d_instantiate(dentry, inode);
115 	return 0;
116 }
117 
bfs_lookup(struct inode * dir,struct dentry * dentry)118 static struct dentry * bfs_lookup(struct inode * dir, struct dentry * dentry)
119 {
120 	struct inode * inode = NULL;
121 	struct buffer_head * bh;
122 	struct bfs_dirent * de;
123 
124 	if (dentry->d_name.len > BFS_NAMELEN)
125 		return ERR_PTR(-ENAMETOOLONG);
126 
127 	bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de);
128 	if (bh) {
129 		unsigned long ino = le32_to_cpu(de->ino);
130 		brelse(bh);
131 		inode = iget(dir->i_sb, ino);
132 		if (!inode)
133 			return ERR_PTR(-EACCES);
134 	}
135 	d_add(dentry, inode);
136 	return NULL;
137 }
138 
bfs_link(struct dentry * old,struct inode * dir,struct dentry * new)139 static int bfs_link(struct dentry * old, struct inode * dir, struct dentry * new)
140 {
141 	struct inode * inode = old->d_inode;
142 	int err;
143 
144 	if (S_ISDIR(inode->i_mode))
145 		return -EPERM;
146 
147 	err = bfs_add_entry(dir, new->d_name.name, new->d_name.len, inode->i_ino);
148 	if (err)
149 		return err;
150 	inode->i_nlink++;
151 	inode->i_ctime = CURRENT_TIME;
152 	mark_inode_dirty(inode);
153 	atomic_inc(&inode->i_count);
154 	d_instantiate(new, inode);
155 	return 0;
156 }
157 
158 
bfs_unlink(struct inode * dir,struct dentry * dentry)159 static int bfs_unlink(struct inode * dir, struct dentry * dentry)
160 {
161 	int error = -ENOENT;
162 	struct inode * inode;
163 	struct buffer_head * bh;
164 	struct bfs_dirent * de;
165 
166 	inode = dentry->d_inode;
167 	bh = bfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len, &de);
168 	if (!bh || de->ino != inode->i_ino)
169 		goto out_brelse;
170 
171 	if (!inode->i_nlink) {
172 		printf("unlinking non-existent file %s:%lu (nlink=%d)\n", bdevname(inode->i_dev),
173 				inode->i_ino, inode->i_nlink);
174 		inode->i_nlink = 1;
175 	}
176 	de->ino = 0;
177 	dir->i_version = ++event;
178 	mark_buffer_dirty(bh);
179 	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
180 	mark_inode_dirty(dir);
181 	inode->i_nlink--;
182 	inode->i_ctime = dir->i_ctime;
183 	mark_inode_dirty(inode);
184 	error = 0;
185 
186 out_brelse:
187 	brelse(bh);
188 	return error;
189 }
190 
bfs_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)191 static int bfs_rename(struct inode * old_dir, struct dentry * old_dentry,
192 			struct inode * new_dir, struct dentry * new_dentry)
193 {
194 	struct inode * old_inode, * new_inode;
195 	struct buffer_head * old_bh, * new_bh;
196 	struct bfs_dirent * old_de, * new_de;
197 	int error = -ENOENT;
198 
199 	old_bh = new_bh = NULL;
200 	old_inode = old_dentry->d_inode;
201 	if (S_ISDIR(old_inode->i_mode))
202 		return -EINVAL;
203 
204 	old_bh = bfs_find_entry(old_dir,
205 				old_dentry->d_name.name,
206 				old_dentry->d_name.len, &old_de);
207 
208 	if (!old_bh || old_de->ino != old_inode->i_ino)
209 		goto end_rename;
210 
211 	error = -EPERM;
212 	new_inode = new_dentry->d_inode;
213 	new_bh = bfs_find_entry(new_dir,
214 				new_dentry->d_name.name,
215 				new_dentry->d_name.len, &new_de);
216 
217 	if (new_bh && !new_inode) {
218 		brelse(new_bh);
219 		new_bh = NULL;
220 	}
221 	if (!new_bh) {
222 		error = bfs_add_entry(new_dir,
223 					new_dentry->d_name.name,
224 			 		new_dentry->d_name.len, old_inode->i_ino);
225 		if (error)
226 			goto end_rename;
227 	}
228 	old_de->ino = 0;
229 	old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
230 	old_dir->i_version = ++event;
231 	mark_inode_dirty(old_dir);
232 	if (new_inode) {
233 		new_inode->i_nlink--;
234 		new_inode->i_ctime = CURRENT_TIME;
235 		mark_inode_dirty(new_inode);
236 	}
237 	mark_buffer_dirty(old_bh);
238 	error = 0;
239 
240 end_rename:
241 	brelse(old_bh);
242 	brelse(new_bh);
243 	return error;
244 }
245 
246 struct inode_operations bfs_dir_inops = {
247 	create:			bfs_create,
248 	lookup:			bfs_lookup,
249 	link:			bfs_link,
250 	unlink:			bfs_unlink,
251 	rename:			bfs_rename,
252 };
253 
bfs_add_entry(struct inode * dir,const char * name,int namelen,int ino)254 static int bfs_add_entry(struct inode * dir, const char * name, int namelen, int ino)
255 {
256 	struct buffer_head * bh;
257 	struct bfs_dirent * de;
258 	int block, sblock, eblock, off;
259 	int i;
260 
261 	dprintf("name=%s, namelen=%d\n", name, namelen);
262 
263 	if (!namelen)
264 		return -ENOENT;
265 	if (namelen > BFS_NAMELEN)
266 		return -ENAMETOOLONG;
267 
268 	sblock = dir->iu_sblock;
269 	eblock = dir->iu_eblock;
270 	for (block=sblock; block<=eblock; block++) {
271 		bh = sb_bread(dir->i_sb, block);
272 		if(!bh)
273 			return -ENOSPC;
274 		for (off=0; off<BFS_BSIZE; off+=BFS_DIRENT_SIZE) {
275 			de = (struct bfs_dirent *)(bh->b_data + off);
276 			if (!de->ino) {
277 				if ((block-sblock)*BFS_BSIZE + off >= dir->i_size) {
278 					dir->i_size += BFS_DIRENT_SIZE;
279 					dir->i_ctime = CURRENT_TIME;
280 				}
281 				dir->i_mtime = CURRENT_TIME;
282 				mark_inode_dirty(dir);
283 				dir->i_version = ++event;
284 				de->ino = ino;
285 				for (i=0; i<BFS_NAMELEN; i++)
286 					de->name[i] = (i < namelen) ? name[i] : 0;
287 				mark_buffer_dirty(bh);
288 				brelse(bh);
289 				return 0;
290 			}
291 		}
292 		brelse(bh);
293 	}
294 	return -ENOSPC;
295 }
296 
bfs_namecmp(int len,const char * name,const char * buffer)297 static inline int bfs_namecmp(int len, const char * name, const char * buffer)
298 {
299 	if (len < BFS_NAMELEN && buffer[len])
300 		return 0;
301 	return !memcmp(name, buffer, len);
302 }
303 
bfs_find_entry(struct inode * dir,const char * name,int namelen,struct bfs_dirent ** res_dir)304 static struct buffer_head * bfs_find_entry(struct inode * dir,
305 	const char * name, int namelen, struct bfs_dirent ** res_dir)
306 {
307 	unsigned long block, offset;
308 	struct buffer_head * bh;
309 	struct bfs_dirent * de;
310 
311 	*res_dir = NULL;
312 	if (namelen > BFS_NAMELEN)
313 		return NULL;
314 	bh = NULL;
315 	block = offset = 0;
316 	while (block * BFS_BSIZE + offset < dir->i_size) {
317 		if (!bh) {
318 			bh = sb_bread(dir->i_sb, dir->iu_sblock + block);
319 			if (!bh) {
320 				block++;
321 				continue;
322 			}
323 		}
324 		de = (struct bfs_dirent *)(bh->b_data + offset);
325 		offset += BFS_DIRENT_SIZE;
326 		if (de->ino && bfs_namecmp(namelen, name, de->name)) {
327 			*res_dir = de;
328 			return bh;
329 		}
330 		if (offset < bh->b_size)
331 			continue;
332 		brelse(bh);
333 		bh = NULL;
334 		offset = 0;
335 		block++;
336 	}
337 	brelse(bh);
338 	return NULL;
339 }
340