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