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