| 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, | 
| Christoph Hellwig | 1b061d9 | 2010-05-26 17:53:41 +0200 | [diff] [blame] | 81 | .fsync		= generic_file_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 |  | 
| Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 87 | static int bfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 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); | 
| Akinobu Mita | 69b195b | 2011-03-21 08:32:53 -0400 | [diff] [blame] | 100 | ino = find_first_zero_bit(info->si_imap, info->si_lasti + 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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--; | 
| Dmitry Monakhov | e6ecdc7 | 2010-03-04 17:31:46 +0300 | [diff] [blame] | 108 | inode_init_owner(inode, dir, mode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | inode->i_ino = ino; | 
| Alexey Dobriyan | ce0fe7e | 2005-10-04 17:43:06 +0100 | [diff] [blame] | 115 | BFS_I(inode)->i_dsk_ino = ino; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | BFS_I(inode)->i_sblock = 0; | 
|  | 117 | BFS_I(inode)->i_eblock = 0; | 
|  | 118 | insert_inode_hash(inode); | 
|  | 119 | mark_inode_dirty(inode); | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 120 | dump_imap("create", s); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 |  | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 122 | err = bfs_add_entry(dir, dentry->d_name.name, dentry->d_name.len, | 
|  | 123 | inode->i_ino); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | if (err) { | 
| Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 125 | inode_dec_link_count(inode); | 
| Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 126 | mutex_unlock(&info->bfs_lock); | 
| Eric Sesterhenn | 1558182 | 2008-09-13 02:33:12 -0700 | [diff] [blame] | 127 | iput(inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | return err; | 
|  | 129 | } | 
| Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 130 | mutex_unlock(&info->bfs_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | d_instantiate(dentry, inode); | 
|  | 132 | return 0; | 
|  | 133 | } | 
|  | 134 |  | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 135 | static struct dentry *bfs_lookup(struct inode *dir, struct dentry *dentry, | 
|  | 136 | struct nameidata *nd) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 138 | struct inode *inode = NULL; | 
|  | 139 | struct buffer_head *bh; | 
|  | 140 | struct bfs_dirent *de; | 
| Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 141 | struct bfs_sb_info *info = BFS_SB(dir->i_sb); | 
| 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 |  | 
| Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 146 | mutex_lock(&info->bfs_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); | 
| David Howells | e33ab08 | 2008-02-07 00:15:32 -0800 | [diff] [blame] | 151 | inode = bfs_iget(dir->i_sb, ino); | 
|  | 152 | if (IS_ERR(inode)) { | 
| Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 153 | mutex_unlock(&info->bfs_lock); | 
| David Howells | e33ab08 | 2008-02-07 00:15:32 -0800 | [diff] [blame] | 154 | return ERR_CAST(inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } | 
|  | 156 | } | 
| Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 157 | mutex_unlock(&info->bfs_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; | 
| Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 166 | struct bfs_sb_info *info = BFS_SB(inode->i_sb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | int err; | 
|  | 168 |  | 
| Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 169 | mutex_lock(&info->bfs_lock); | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 170 | err = bfs_add_entry(dir, new->d_name.name, new->d_name.len, | 
|  | 171 | inode->i_ino); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | if (err) { | 
| Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 173 | mutex_unlock(&info->bfs_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | return err; | 
|  | 175 | } | 
| Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 176 | inc_nlink(inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | inode->i_ctime = CURRENT_TIME_SEC; | 
|  | 178 | mark_inode_dirty(inode); | 
| Al Viro | 7de9c6e | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 179 | ihold(inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | d_instantiate(new, inode); | 
| Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 181 | mutex_unlock(&info->bfs_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | return 0; | 
|  | 183 | } | 
|  | 184 |  | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 185 | static int bfs_unlink(struct inode *dir, struct dentry *dentry) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { | 
|  | 187 | int error = -ENOENT; | 
| Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 188 | struct inode *inode = dentry->d_inode; | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 189 | struct buffer_head *bh; | 
|  | 190 | struct bfs_dirent *de; | 
| Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 191 | struct bfs_sb_info *info = BFS_SB(inode->i_sb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 |  | 
| Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 193 | mutex_lock(&info->bfs_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | 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] | 195 | if (!bh || (le16_to_cpu(de->ino) != inode->i_ino)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | goto out_brelse; | 
|  | 197 |  | 
|  | 198 | if (!inode->i_nlink) { | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 199 | printf("unlinking non-existent file %s:%lu (nlink=%d)\n", | 
|  | 200 | inode->i_sb->s_id, inode->i_ino, | 
|  | 201 | inode->i_nlink); | 
| Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 202 | set_nlink(inode, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } | 
|  | 204 | de->ino = 0; | 
| Al Viro | 4427f0c | 2009-06-08 01:15:58 -0400 | [diff] [blame] | 205 | mark_buffer_dirty_inode(bh, dir); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC; | 
|  | 207 | mark_inode_dirty(dir); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | inode->i_ctime = dir->i_ctime; | 
| Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 209 | inode_dec_link_count(inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | error = 0; | 
|  | 211 |  | 
|  | 212 | out_brelse: | 
|  | 213 | brelse(bh); | 
| Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 214 | mutex_unlock(&info->bfs_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | return error; | 
|  | 216 | } | 
|  | 217 |  | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 218 | static int bfs_rename(struct inode *old_dir, struct dentry *old_dentry, | 
|  | 219 | struct inode *new_dir, struct dentry *new_dentry) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 221 | struct inode *old_inode, *new_inode; | 
|  | 222 | struct buffer_head *old_bh, *new_bh; | 
|  | 223 | struct bfs_dirent *old_de, *new_de; | 
| Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 224 | struct bfs_sb_info *info; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | int error = -ENOENT; | 
|  | 226 |  | 
|  | 227 | old_bh = new_bh = NULL; | 
|  | 228 | old_inode = old_dentry->d_inode; | 
|  | 229 | if (S_ISDIR(old_inode->i_mode)) | 
|  | 230 | return -EINVAL; | 
|  | 231 |  | 
| Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 232 | info = BFS_SB(old_inode->i_sb); | 
|  | 233 |  | 
|  | 234 | mutex_lock(&info->bfs_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | old_bh = bfs_find_entry(old_dir, | 
|  | 236 | old_dentry->d_name.name, | 
|  | 237 | old_dentry->d_name.len, &old_de); | 
|  | 238 |  | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 239 | 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] | 240 | goto end_rename; | 
|  | 241 |  | 
|  | 242 | error = -EPERM; | 
|  | 243 | new_inode = new_dentry->d_inode; | 
|  | 244 | new_bh = bfs_find_entry(new_dir, | 
|  | 245 | new_dentry->d_name.name, | 
|  | 246 | new_dentry->d_name.len, &new_de); | 
|  | 247 |  | 
|  | 248 | if (new_bh && !new_inode) { | 
|  | 249 | brelse(new_bh); | 
|  | 250 | new_bh = NULL; | 
|  | 251 | } | 
|  | 252 | if (!new_bh) { | 
|  | 253 | error = bfs_add_entry(new_dir, | 
|  | 254 | new_dentry->d_name.name, | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 255 | new_dentry->d_name.len, | 
|  | 256 | old_inode->i_ino); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | if (error) | 
|  | 258 | goto end_rename; | 
|  | 259 | } | 
|  | 260 | old_de->ino = 0; | 
|  | 261 | old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME_SEC; | 
|  | 262 | mark_inode_dirty(old_dir); | 
|  | 263 | if (new_inode) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | new_inode->i_ctime = CURRENT_TIME_SEC; | 
| Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 265 | inode_dec_link_count(new_inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } | 
| Al Viro | 4427f0c | 2009-06-08 01:15:58 -0400 | [diff] [blame] | 267 | mark_buffer_dirty_inode(old_bh, old_dir); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | error = 0; | 
|  | 269 |  | 
|  | 270 | end_rename: | 
| Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 271 | mutex_unlock(&info->bfs_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | brelse(old_bh); | 
|  | 273 | brelse(new_bh); | 
|  | 274 | return error; | 
|  | 275 | } | 
|  | 276 |  | 
| Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 277 | const struct inode_operations bfs_dir_inops = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | .create			= bfs_create, | 
|  | 279 | .lookup			= bfs_lookup, | 
|  | 280 | .link			= bfs_link, | 
|  | 281 | .unlink			= bfs_unlink, | 
|  | 282 | .rename			= bfs_rename, | 
|  | 283 | }; | 
|  | 284 |  | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 285 | static int bfs_add_entry(struct inode *dir, const unsigned char *name, | 
|  | 286 | int namelen, int ino) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | { | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 288 | struct buffer_head *bh; | 
|  | 289 | struct bfs_dirent *de; | 
|  | 290 | int block, sblock, eblock, off, pos; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | int i; | 
|  | 292 |  | 
|  | 293 | dprintf("name=%s, namelen=%d\n", name, namelen); | 
|  | 294 |  | 
|  | 295 | if (!namelen) | 
|  | 296 | return -ENOENT; | 
|  | 297 | if (namelen > BFS_NAMELEN) | 
|  | 298 | return -ENAMETOOLONG; | 
|  | 299 |  | 
|  | 300 | sblock = BFS_I(dir)->i_sblock; | 
|  | 301 | eblock = BFS_I(dir)->i_eblock; | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 302 | for (block = sblock; block <= eblock; block++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | bh = sb_bread(dir->i_sb, block); | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 304 | if (!bh) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | return -ENOSPC; | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 306 | for (off = 0; off < BFS_BSIZE; off += BFS_DIRENT_SIZE) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | de = (struct bfs_dirent *)(bh->b_data + off); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | if (!de->ino) { | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 309 | pos = (block - sblock) * BFS_BSIZE + off; | 
|  | 310 | if (pos >= dir->i_size) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | dir->i_size += BFS_DIRENT_SIZE; | 
|  | 312 | dir->i_ctime = CURRENT_TIME_SEC; | 
|  | 313 | } | 
|  | 314 | dir->i_mtime = CURRENT_TIME_SEC; | 
|  | 315 | mark_inode_dirty(dir); | 
| Andrew Stribblehill | fac92be | 2005-09-09 13:02:04 -0700 | [diff] [blame] | 316 | de->ino = cpu_to_le16((u16)ino); | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 317 | for (i = 0; i < BFS_NAMELEN; i++) | 
|  | 318 | de->name[i] = | 
|  | 319 | (i < namelen) ? name[i] : 0; | 
| Al Viro | 4427f0c | 2009-06-08 01:15:58 -0400 | [diff] [blame] | 320 | mark_buffer_dirty_inode(bh, dir); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | brelse(bh); | 
|  | 322 | return 0; | 
|  | 323 | } | 
|  | 324 | } | 
|  | 325 | brelse(bh); | 
|  | 326 | } | 
|  | 327 | return -ENOSPC; | 
|  | 328 | } | 
|  | 329 |  | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 330 | static inline int bfs_namecmp(int len, const unsigned char *name, | 
|  | 331 | const char *buffer) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 333 | if ((len < BFS_NAMELEN) && buffer[len]) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | return 0; | 
|  | 335 | return !memcmp(name, buffer, len); | 
|  | 336 | } | 
|  | 337 |  | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 338 | static struct buffer_head *bfs_find_entry(struct inode *dir, | 
|  | 339 | const unsigned char *name, int namelen, | 
|  | 340 | struct bfs_dirent **res_dir) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 342 | unsigned long block = 0, offset = 0; | 
|  | 343 | struct buffer_head *bh = NULL; | 
|  | 344 | struct bfs_dirent *de; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 |  | 
|  | 346 | *res_dir = NULL; | 
|  | 347 | if (namelen > BFS_NAMELEN) | 
|  | 348 | return NULL; | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 349 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | while (block * BFS_BSIZE + offset < dir->i_size) { | 
|  | 351 | if (!bh) { | 
|  | 352 | bh = sb_bread(dir->i_sb, BFS_I(dir)->i_sblock + block); | 
|  | 353 | if (!bh) { | 
|  | 354 | block++; | 
|  | 355 | continue; | 
|  | 356 | } | 
|  | 357 | } | 
|  | 358 | de = (struct bfs_dirent *)(bh->b_data + offset); | 
|  | 359 | offset += BFS_DIRENT_SIZE; | 
| Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 360 | if (le16_to_cpu(de->ino) && | 
|  | 361 | bfs_namecmp(namelen, name, de->name)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | *res_dir = de; | 
|  | 363 | return bh; | 
|  | 364 | } | 
|  | 365 | if (offset < bh->b_size) | 
|  | 366 | continue; | 
|  | 367 | brelse(bh); | 
|  | 368 | bh = NULL; | 
|  | 369 | offset = 0; | 
|  | 370 | block++; | 
|  | 371 | } | 
|  | 372 | brelse(bh); | 
|  | 373 | return NULL; | 
|  | 374 | } |