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