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