Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/hpfs/namei.c |
| 4 | * |
| 5 | * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999 |
| 6 | * |
| 7 | * adding & removing files & directories |
| 8 | */ |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 9 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include "hpfs_fn.h" |
| 11 | |
Mikulas Patocka | f49a26e | 2015-09-02 22:51:53 +0200 | [diff] [blame] | 12 | static void hpfs_update_directory_times(struct inode *dir) |
| 13 | { |
Arnd Bergmann | f08957d0 | 2018-08-17 15:43:54 -0700 | [diff] [blame] | 14 | time64_t t = local_to_gmt(dir->i_sb, local_get_seconds(dir->i_sb)); |
Mikulas Patocka | f49a26e | 2015-09-02 22:51:53 +0200 | [diff] [blame] | 15 | if (t == dir->i_mtime.tv_sec && |
| 16 | t == dir->i_ctime.tv_sec) |
| 17 | return; |
| 18 | dir->i_mtime.tv_sec = dir->i_ctime.tv_sec = t; |
| 19 | dir->i_mtime.tv_nsec = dir->i_ctime.tv_nsec = 0; |
| 20 | hpfs_write_inode_nolock(dir); |
| 21 | } |
| 22 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 23 | static int hpfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, |
| 24 | struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | { |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 26 | const unsigned char *name = dentry->d_name.name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | unsigned len = dentry->d_name.len; |
| 28 | struct quad_buffer_head qbh0; |
| 29 | struct buffer_head *bh; |
| 30 | struct hpfs_dirent *de; |
| 31 | struct fnode *fnode; |
| 32 | struct dnode *dnode; |
| 33 | struct inode *result; |
| 34 | fnode_secno fno; |
| 35 | dnode_secno dno; |
| 36 | int r; |
| 37 | struct hpfs_dirent dee; |
| 38 | int err; |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 39 | if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err; |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 40 | hpfs_lock(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | err = -ENOSPC; |
| 42 | fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh); |
| 43 | if (!fnode) |
| 44 | goto bail; |
Mikulas Patocka | 7d23ce3 | 2011-05-08 20:43:06 +0200 | [diff] [blame] | 45 | dnode = hpfs_alloc_dnode(dir->i_sb, fno, &dno, &qbh0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | if (!dnode) |
| 47 | goto bail1; |
| 48 | memset(&dee, 0, sizeof dee); |
| 49 | dee.directory = 1; |
| 50 | if (!(mode & 0222)) dee.read_only = 1; |
| 51 | /*dee.archive = 0;*/ |
| 52 | dee.hidden = name[0] == '.'; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 53 | dee.fnode = cpu_to_le32(fno); |
Arnd Bergmann | f08957d0 | 2018-08-17 15:43:54 -0700 | [diff] [blame] | 54 | dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(local_get_seconds(dir->i_sb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | result = new_inode(dir->i_sb); |
| 56 | if (!result) |
| 57 | goto bail2; |
| 58 | hpfs_init_inode(result); |
| 59 | result->i_ino = fno; |
| 60 | hpfs_i(result)->i_parent_dir = dir->i_ino; |
| 61 | hpfs_i(result)->i_dno = dno; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 62 | result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | result->i_ctime.tv_nsec = 0; |
| 64 | result->i_mtime.tv_nsec = 0; |
| 65 | result->i_atime.tv_nsec = 0; |
| 66 | hpfs_i(result)->i_ea_size = 0; |
| 67 | result->i_mode |= S_IFDIR; |
| 68 | result->i_op = &hpfs_dir_iops; |
| 69 | result->i_fop = &hpfs_dir_ops; |
| 70 | result->i_blocks = 4; |
| 71 | result->i_size = 2048; |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 72 | set_nlink(result, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | if (dee.read_only) |
| 74 | result->i_mode &= ~0222; |
| 75 | |
Mikulas Patocka | 7d23ce3 | 2011-05-08 20:43:06 +0200 | [diff] [blame] | 76 | r = hpfs_add_dirent(dir, name, len, &dee); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | if (r == 1) |
| 78 | goto bail3; |
| 79 | if (r == -1) { |
| 80 | err = -EEXIST; |
| 81 | goto bail3; |
| 82 | } |
| 83 | fnode->len = len; |
| 84 | memcpy(fnode->name, name, len > 15 ? 15 : len); |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 85 | fnode->up = cpu_to_le32(dir->i_ino); |
Al Viro | c4c9954 | 2012-04-06 14:30:07 -0400 | [diff] [blame] | 86 | fnode->flags |= FNODE_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | fnode->btree.n_free_nodes = 7; |
| 88 | fnode->btree.n_used_nodes = 1; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 89 | fnode->btree.first_free = cpu_to_le16(0x14); |
| 90 | fnode->u.external[0].disk_secno = cpu_to_le32(dno); |
| 91 | fnode->u.external[0].file_secno = cpu_to_le32(-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | dnode->root_dnode = 1; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 93 | dnode->up = cpu_to_le32(fno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | de = hpfs_add_de(dir->i_sb, dnode, "\001\001", 2, 0); |
Arnd Bergmann | f08957d0 | 2018-08-17 15:43:54 -0700 | [diff] [blame] | 95 | de->creation_date = de->write_date = de->read_date = cpu_to_le32(local_get_seconds(dir->i_sb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | if (!(mode & 0222)) de->read_only = 1; |
| 97 | de->first = de->directory = 1; |
| 98 | /*de->hidden = de->system = 0;*/ |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 99 | de->fnode = cpu_to_le32(fno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | mark_buffer_dirty(bh); |
| 101 | brelse(bh); |
| 102 | hpfs_mark_4buffers_dirty(&qbh0); |
| 103 | hpfs_brelse4(&qbh0); |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 104 | inc_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | insert_inode_hash(result); |
| 106 | |
Eric W. Biederman | 0e1a43c | 2012-02-07 16:27:53 -0800 | [diff] [blame] | 107 | if (!uid_eq(result->i_uid, current_fsuid()) || |
| 108 | !gid_eq(result->i_gid, current_fsgid()) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | result->i_mode != (mode | S_IFDIR)) { |
David Howells | de395b8 | 2008-11-14 10:38:55 +1100 | [diff] [blame] | 110 | result->i_uid = current_fsuid(); |
| 111 | result->i_gid = current_fsgid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | result->i_mode = mode | S_IFDIR; |
| 113 | hpfs_write_inode_nolock(result); |
| 114 | } |
Mikulas Patocka | f49a26e | 2015-09-02 22:51:53 +0200 | [diff] [blame] | 115 | hpfs_update_directory_times(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | d_instantiate(dentry, result); |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 117 | hpfs_unlock(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return 0; |
| 119 | bail3: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | iput(result); |
| 121 | bail2: |
| 122 | hpfs_brelse4(&qbh0); |
| 123 | hpfs_free_dnode(dir->i_sb, dno); |
| 124 | bail1: |
| 125 | brelse(bh); |
| 126 | hpfs_free_sectors(dir->i_sb, fno, 1); |
| 127 | bail: |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 128 | hpfs_unlock(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | return err; |
| 130 | } |
| 131 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 132 | static int hpfs_create(struct user_namespace *mnt_userns, struct inode *dir, |
| 133 | struct dentry *dentry, umode_t mode, bool excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 135 | const unsigned char *name = dentry->d_name.name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | unsigned len = dentry->d_name.len; |
| 137 | struct inode *result = NULL; |
| 138 | struct buffer_head *bh; |
| 139 | struct fnode *fnode; |
| 140 | fnode_secno fno; |
| 141 | int r; |
| 142 | struct hpfs_dirent dee; |
| 143 | int err; |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 144 | if ((err = hpfs_chk_name(name, &len))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | return err==-ENOENT ? -EINVAL : err; |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 146 | hpfs_lock(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | err = -ENOSPC; |
| 148 | fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh); |
| 149 | if (!fnode) |
| 150 | goto bail; |
| 151 | memset(&dee, 0, sizeof dee); |
| 152 | if (!(mode & 0222)) dee.read_only = 1; |
| 153 | dee.archive = 1; |
| 154 | dee.hidden = name[0] == '.'; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 155 | dee.fnode = cpu_to_le32(fno); |
Arnd Bergmann | f08957d0 | 2018-08-17 15:43:54 -0700 | [diff] [blame] | 156 | dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(local_get_seconds(dir->i_sb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | result = new_inode(dir->i_sb); |
| 159 | if (!result) |
| 160 | goto bail1; |
| 161 | |
| 162 | hpfs_init_inode(result); |
| 163 | result->i_ino = fno; |
| 164 | result->i_mode |= S_IFREG; |
| 165 | result->i_mode &= ~0111; |
| 166 | result->i_op = &hpfs_file_iops; |
| 167 | result->i_fop = &hpfs_file_ops; |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 168 | set_nlink(result, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | hpfs_i(result)->i_parent_dir = dir->i_ino; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 170 | result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | result->i_ctime.tv_nsec = 0; |
| 172 | result->i_mtime.tv_nsec = 0; |
| 173 | result->i_atime.tv_nsec = 0; |
| 174 | hpfs_i(result)->i_ea_size = 0; |
| 175 | if (dee.read_only) |
| 176 | result->i_mode &= ~0222; |
| 177 | result->i_blocks = 1; |
| 178 | result->i_size = 0; |
| 179 | result->i_data.a_ops = &hpfs_aops; |
| 180 | hpfs_i(result)->mmu_private = 0; |
| 181 | |
Mikulas Patocka | 7d23ce3 | 2011-05-08 20:43:06 +0200 | [diff] [blame] | 182 | r = hpfs_add_dirent(dir, name, len, &dee); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | if (r == 1) |
| 184 | goto bail2; |
| 185 | if (r == -1) { |
| 186 | err = -EEXIST; |
| 187 | goto bail2; |
| 188 | } |
| 189 | fnode->len = len; |
| 190 | memcpy(fnode->name, name, len > 15 ? 15 : len); |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 191 | fnode->up = cpu_to_le32(dir->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | mark_buffer_dirty(bh); |
| 193 | brelse(bh); |
| 194 | |
| 195 | insert_inode_hash(result); |
| 196 | |
Eric W. Biederman | 0e1a43c | 2012-02-07 16:27:53 -0800 | [diff] [blame] | 197 | if (!uid_eq(result->i_uid, current_fsuid()) || |
| 198 | !gid_eq(result->i_gid, current_fsgid()) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | result->i_mode != (mode | S_IFREG)) { |
David Howells | de395b8 | 2008-11-14 10:38:55 +1100 | [diff] [blame] | 200 | result->i_uid = current_fsuid(); |
| 201 | result->i_gid = current_fsgid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | result->i_mode = mode | S_IFREG; |
| 203 | hpfs_write_inode_nolock(result); |
| 204 | } |
Mikulas Patocka | f49a26e | 2015-09-02 22:51:53 +0200 | [diff] [blame] | 205 | hpfs_update_directory_times(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | d_instantiate(dentry, result); |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 207 | hpfs_unlock(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | return 0; |
| 209 | |
| 210 | bail2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | iput(result); |
| 212 | bail1: |
| 213 | brelse(bh); |
| 214 | hpfs_free_sectors(dir->i_sb, fno, 1); |
| 215 | bail: |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 216 | hpfs_unlock(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | return err; |
| 218 | } |
| 219 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 220 | static int hpfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, |
| 221 | struct dentry *dentry, umode_t mode, dev_t rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 223 | const unsigned char *name = dentry->d_name.name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | unsigned len = dentry->d_name.len; |
| 225 | struct buffer_head *bh; |
| 226 | struct fnode *fnode; |
| 227 | fnode_secno fno; |
| 228 | int r; |
| 229 | struct hpfs_dirent dee; |
| 230 | struct inode *result = NULL; |
| 231 | int err; |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 232 | if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | if (hpfs_sb(dir->i_sb)->sb_eas < 2) return -EPERM; |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 234 | hpfs_lock(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | err = -ENOSPC; |
| 236 | fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh); |
| 237 | if (!fnode) |
| 238 | goto bail; |
| 239 | memset(&dee, 0, sizeof dee); |
| 240 | if (!(mode & 0222)) dee.read_only = 1; |
| 241 | dee.archive = 1; |
| 242 | dee.hidden = name[0] == '.'; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 243 | dee.fnode = cpu_to_le32(fno); |
Arnd Bergmann | f08957d0 | 2018-08-17 15:43:54 -0700 | [diff] [blame] | 244 | dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(local_get_seconds(dir->i_sb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
| 246 | result = new_inode(dir->i_sb); |
| 247 | if (!result) |
| 248 | goto bail1; |
| 249 | |
| 250 | hpfs_init_inode(result); |
| 251 | result->i_ino = fno; |
| 252 | hpfs_i(result)->i_parent_dir = dir->i_ino; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 253 | result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | result->i_ctime.tv_nsec = 0; |
| 255 | result->i_mtime.tv_nsec = 0; |
| 256 | result->i_atime.tv_nsec = 0; |
| 257 | hpfs_i(result)->i_ea_size = 0; |
David Howells | de395b8 | 2008-11-14 10:38:55 +1100 | [diff] [blame] | 258 | result->i_uid = current_fsuid(); |
| 259 | result->i_gid = current_fsgid(); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 260 | set_nlink(result, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | result->i_size = 0; |
| 262 | result->i_blocks = 1; |
| 263 | init_special_inode(result, mode, rdev); |
| 264 | |
Mikulas Patocka | 7d23ce3 | 2011-05-08 20:43:06 +0200 | [diff] [blame] | 265 | r = hpfs_add_dirent(dir, name, len, &dee); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | if (r == 1) |
| 267 | goto bail2; |
| 268 | if (r == -1) { |
| 269 | err = -EEXIST; |
| 270 | goto bail2; |
| 271 | } |
| 272 | fnode->len = len; |
| 273 | memcpy(fnode->name, name, len > 15 ? 15 : len); |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 274 | fnode->up = cpu_to_le32(dir->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | mark_buffer_dirty(bh); |
| 276 | |
| 277 | insert_inode_hash(result); |
| 278 | |
| 279 | hpfs_write_inode_nolock(result); |
Mikulas Patocka | f49a26e | 2015-09-02 22:51:53 +0200 | [diff] [blame] | 280 | hpfs_update_directory_times(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | d_instantiate(dentry, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | brelse(bh); |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 283 | hpfs_unlock(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | return 0; |
| 285 | bail2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | iput(result); |
| 287 | bail1: |
| 288 | brelse(bh); |
| 289 | hpfs_free_sectors(dir->i_sb, fno, 1); |
| 290 | bail: |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 291 | hpfs_unlock(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | return err; |
| 293 | } |
| 294 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 295 | static int hpfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, |
| 296 | struct dentry *dentry, const char *symlink) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | { |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 298 | const unsigned char *name = dentry->d_name.name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | unsigned len = dentry->d_name.len; |
| 300 | struct buffer_head *bh; |
| 301 | struct fnode *fnode; |
| 302 | fnode_secno fno; |
| 303 | int r; |
| 304 | struct hpfs_dirent dee; |
| 305 | struct inode *result; |
| 306 | int err; |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 307 | if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err; |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 308 | hpfs_lock(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | if (hpfs_sb(dir->i_sb)->sb_eas < 2) { |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 310 | hpfs_unlock(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | return -EPERM; |
| 312 | } |
| 313 | err = -ENOSPC; |
| 314 | fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh); |
| 315 | if (!fnode) |
| 316 | goto bail; |
| 317 | memset(&dee, 0, sizeof dee); |
| 318 | dee.archive = 1; |
| 319 | dee.hidden = name[0] == '.'; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 320 | dee.fnode = cpu_to_le32(fno); |
Arnd Bergmann | f08957d0 | 2018-08-17 15:43:54 -0700 | [diff] [blame] | 321 | dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(local_get_seconds(dir->i_sb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
| 323 | result = new_inode(dir->i_sb); |
| 324 | if (!result) |
| 325 | goto bail1; |
| 326 | result->i_ino = fno; |
| 327 | hpfs_init_inode(result); |
| 328 | hpfs_i(result)->i_parent_dir = dir->i_ino; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 329 | result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | result->i_ctime.tv_nsec = 0; |
| 331 | result->i_mtime.tv_nsec = 0; |
| 332 | result->i_atime.tv_nsec = 0; |
| 333 | hpfs_i(result)->i_ea_size = 0; |
| 334 | result->i_mode = S_IFLNK | 0777; |
David Howells | de395b8 | 2008-11-14 10:38:55 +1100 | [diff] [blame] | 335 | result->i_uid = current_fsuid(); |
| 336 | result->i_gid = current_fsgid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | result->i_blocks = 1; |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 338 | set_nlink(result, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | result->i_size = strlen(symlink); |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 340 | inode_nohighmem(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | result->i_op = &page_symlink_inode_operations; |
| 342 | result->i_data.a_ops = &hpfs_symlink_aops; |
| 343 | |
Mikulas Patocka | 7d23ce3 | 2011-05-08 20:43:06 +0200 | [diff] [blame] | 344 | r = hpfs_add_dirent(dir, name, len, &dee); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | if (r == 1) |
| 346 | goto bail2; |
| 347 | if (r == -1) { |
| 348 | err = -EEXIST; |
| 349 | goto bail2; |
| 350 | } |
| 351 | fnode->len = len; |
| 352 | memcpy(fnode->name, name, len > 15 ? 15 : len); |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 353 | fnode->up = cpu_to_le32(dir->i_ino); |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 354 | hpfs_set_ea(result, fnode, "SYMLINK", symlink, strlen(symlink)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | mark_buffer_dirty(bh); |
| 356 | brelse(bh); |
| 357 | |
| 358 | insert_inode_hash(result); |
| 359 | |
| 360 | hpfs_write_inode_nolock(result); |
Mikulas Patocka | f49a26e | 2015-09-02 22:51:53 +0200 | [diff] [blame] | 361 | hpfs_update_directory_times(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | d_instantiate(dentry, result); |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 363 | hpfs_unlock(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | return 0; |
| 365 | bail2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | iput(result); |
| 367 | bail1: |
| 368 | brelse(bh); |
| 369 | hpfs_free_sectors(dir->i_sb, fno, 1); |
| 370 | bail: |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 371 | hpfs_unlock(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | return err; |
| 373 | } |
| 374 | |
| 375 | static int hpfs_unlink(struct inode *dir, struct dentry *dentry) |
| 376 | { |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 377 | const unsigned char *name = dentry->d_name.name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | unsigned len = dentry->d_name.len; |
| 379 | struct quad_buffer_head qbh; |
| 380 | struct hpfs_dirent *de; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 381 | struct inode *inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | dnode_secno dno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | int err; |
| 385 | |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 386 | hpfs_lock(dir->i_sb); |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 387 | hpfs_adjust_length(name, &len); |
Mikulas Patocka | b6853f7 | 2016-02-25 18:17:38 +0100 | [diff] [blame] | 388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | err = -ENOENT; |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 390 | de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, &dno, &qbh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | if (!de) |
| 392 | goto out; |
| 393 | |
| 394 | err = -EPERM; |
| 395 | if (de->first) |
| 396 | goto out1; |
| 397 | |
| 398 | err = -EISDIR; |
| 399 | if (de->directory) |
| 400 | goto out1; |
| 401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | r = hpfs_remove_dirent(dir, dno, de, &qbh, 1); |
| 403 | switch (r) { |
| 404 | case 1: |
| 405 | hpfs_error(dir->i_sb, "there was error when removing dirent"); |
| 406 | err = -EFSERROR; |
| 407 | break; |
Mikulas Patocka | b6853f7 | 2016-02-25 18:17:38 +0100 | [diff] [blame] | 408 | case 2: /* no space for deleting */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | err = -ENOSPC; |
Mikulas Patocka | b6853f7 | 2016-02-25 18:17:38 +0100 | [diff] [blame] | 410 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | default: |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 412 | drop_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | err = 0; |
| 414 | } |
| 415 | goto out; |
| 416 | |
| 417 | out1: |
| 418 | hpfs_brelse4(&qbh); |
| 419 | out: |
Mikulas Patocka | f49a26e | 2015-09-02 22:51:53 +0200 | [diff] [blame] | 420 | if (!err) |
| 421 | hpfs_update_directory_times(dir); |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 422 | hpfs_unlock(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | return err; |
| 424 | } |
| 425 | |
| 426 | static int hpfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 427 | { |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 428 | const unsigned char *name = dentry->d_name.name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | unsigned len = dentry->d_name.len; |
| 430 | struct quad_buffer_head qbh; |
| 431 | struct hpfs_dirent *de; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 432 | struct inode *inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | dnode_secno dno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | int n_items = 0; |
| 435 | int err; |
| 436 | int r; |
| 437 | |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 438 | hpfs_adjust_length(name, &len); |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 439 | hpfs_lock(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | err = -ENOENT; |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 441 | de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, &dno, &qbh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | if (!de) |
| 443 | goto out; |
| 444 | |
| 445 | err = -EPERM; |
| 446 | if (de->first) |
| 447 | goto out1; |
| 448 | |
| 449 | err = -ENOTDIR; |
| 450 | if (!de->directory) |
| 451 | goto out1; |
| 452 | |
| 453 | hpfs_count_dnodes(dir->i_sb, hpfs_i(inode)->i_dno, NULL, NULL, &n_items); |
| 454 | err = -ENOTEMPTY; |
| 455 | if (n_items) |
| 456 | goto out1; |
| 457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | r = hpfs_remove_dirent(dir, dno, de, &qbh, 1); |
| 459 | switch (r) { |
| 460 | case 1: |
| 461 | hpfs_error(dir->i_sb, "there was error when removing dirent"); |
| 462 | err = -EFSERROR; |
| 463 | break; |
| 464 | case 2: |
| 465 | err = -ENOSPC; |
| 466 | break; |
| 467 | default: |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 468 | drop_nlink(dir); |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 469 | clear_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | err = 0; |
| 471 | } |
| 472 | goto out; |
| 473 | out1: |
| 474 | hpfs_brelse4(&qbh); |
| 475 | out: |
Mikulas Patocka | f49a26e | 2015-09-02 22:51:53 +0200 | [diff] [blame] | 476 | if (!err) |
| 477 | hpfs_update_directory_times(dir); |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 478 | hpfs_unlock(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | return err; |
| 480 | } |
| 481 | |
| 482 | static int hpfs_symlink_readpage(struct file *file, struct page *page) |
| 483 | { |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 484 | char *link = page_address(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | struct inode *i = page->mapping->host; |
| 486 | struct fnode *fnode; |
| 487 | struct buffer_head *bh; |
| 488 | int err; |
| 489 | |
| 490 | err = -EIO; |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 491 | hpfs_lock(i->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | if (!(fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) |
| 493 | goto fail; |
| 494 | err = hpfs_read_ea(i->i_sb, fnode, "SYMLINK", link, PAGE_SIZE); |
| 495 | brelse(bh); |
| 496 | if (err) |
| 497 | goto fail; |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 498 | hpfs_unlock(i->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | SetPageUptodate(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | unlock_page(page); |
| 501 | return 0; |
| 502 | |
| 503 | fail: |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 504 | hpfs_unlock(i->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | SetPageError(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | unlock_page(page); |
| 507 | return err; |
| 508 | } |
| 509 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 510 | const struct address_space_operations hpfs_symlink_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | .readpage = hpfs_symlink_readpage |
| 512 | }; |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 513 | |
| 514 | static int hpfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir, |
| 515 | struct dentry *old_dentry, struct inode *new_dir, |
| 516 | struct dentry *new_dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | { |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 518 | const unsigned char *old_name = old_dentry->d_name.name; |
| 519 | unsigned old_len = old_dentry->d_name.len; |
| 520 | const unsigned char *new_name = new_dentry->d_name.name; |
| 521 | unsigned new_len = new_dentry->d_name.len; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 522 | struct inode *i = d_inode(old_dentry); |
| 523 | struct inode *new_inode = d_inode(new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | struct quad_buffer_head qbh, qbh1; |
| 525 | struct hpfs_dirent *dep, *nde; |
| 526 | struct hpfs_dirent de; |
| 527 | dnode_secno dno; |
| 528 | int r; |
| 529 | struct buffer_head *bh; |
| 530 | struct fnode *fnode; |
| 531 | int err; |
Sage Weil | e4eaac0 | 2011-05-24 13:06:07 -0700 | [diff] [blame] | 532 | |
Miklos Szeredi | f03b8ad | 2016-09-27 11:03:57 +0200 | [diff] [blame] | 533 | if (flags & ~RENAME_NOREPLACE) |
| 534 | return -EINVAL; |
| 535 | |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 536 | if ((err = hpfs_chk_name(new_name, &new_len))) return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | err = 0; |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 538 | hpfs_adjust_length(old_name, &old_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 540 | hpfs_lock(i->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | /* order doesn't matter, due to VFS exclusion */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
| 543 | /* Erm? Moving over the empty non-busy directory is perfectly legal */ |
| 544 | if (new_inode && S_ISDIR(new_inode->i_mode)) { |
| 545 | err = -EINVAL; |
| 546 | goto end1; |
| 547 | } |
| 548 | |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 549 | if (!(dep = map_dirent(old_dir, hpfs_i(old_dir)->i_dno, old_name, old_len, &dno, &qbh))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | hpfs_error(i->i_sb, "lookup succeeded but map dirent failed"); |
| 551 | err = -ENOENT; |
| 552 | goto end1; |
| 553 | } |
| 554 | copy_de(&de, dep); |
| 555 | de.hidden = new_name[0] == '.'; |
| 556 | |
| 557 | if (new_inode) { |
| 558 | int r; |
| 559 | if ((r = hpfs_remove_dirent(old_dir, dno, dep, &qbh, 1)) != 2) { |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 560 | if ((nde = map_dirent(new_dir, hpfs_i(new_dir)->i_dno, new_name, new_len, NULL, &qbh1))) { |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 561 | clear_nlink(new_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | copy_de(nde, &de); |
| 563 | memcpy(nde->name, new_name, new_len); |
| 564 | hpfs_mark_4buffers_dirty(&qbh1); |
| 565 | hpfs_brelse4(&qbh1); |
| 566 | goto end; |
| 567 | } |
| 568 | hpfs_error(new_dir->i_sb, "hpfs_rename: could not find dirent"); |
| 569 | err = -EFSERROR; |
| 570 | goto end1; |
| 571 | } |
Colin Ian King | e0fcfe1 | 2018-08-25 12:24:31 +0200 | [diff] [blame] | 572 | err = -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | goto end1; |
| 574 | } |
| 575 | |
| 576 | if (new_dir == old_dir) hpfs_brelse4(&qbh); |
| 577 | |
Mikulas Patocka | 7d23ce3 | 2011-05-08 20:43:06 +0200 | [diff] [blame] | 578 | if ((r = hpfs_add_dirent(new_dir, new_name, new_len, &de))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | if (r == -1) hpfs_error(new_dir->i_sb, "hpfs_rename: dirent already exists!"); |
| 580 | err = r == 1 ? -ENOSPC : -EFSERROR; |
| 581 | if (new_dir != old_dir) hpfs_brelse4(&qbh); |
| 582 | goto end1; |
| 583 | } |
| 584 | |
| 585 | if (new_dir == old_dir) |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 586 | if (!(dep = map_dirent(old_dir, hpfs_i(old_dir)->i_dno, old_name, old_len, &dno, &qbh))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | hpfs_error(i->i_sb, "lookup succeeded but map dirent failed at #2"); |
| 588 | err = -ENOENT; |
| 589 | goto end1; |
| 590 | } |
| 591 | |
| 592 | if ((r = hpfs_remove_dirent(old_dir, dno, dep, &qbh, 0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | hpfs_error(i->i_sb, "hpfs_rename: could not remove dirent"); |
| 594 | err = r == 2 ? -ENOSPC : -EFSERROR; |
| 595 | goto end1; |
| 596 | } |
Mikulas Patocka | 7d23ce3 | 2011-05-08 20:43:06 +0200 | [diff] [blame] | 597 | |
Mikulas Patocka | f49a26e | 2015-09-02 22:51:53 +0200 | [diff] [blame] | 598 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | hpfs_i(i)->i_parent_dir = new_dir->i_ino; |
| 600 | if (S_ISDIR(i->i_mode)) { |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 601 | inc_nlink(new_dir); |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 602 | drop_nlink(old_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | if ((fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) { |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 605 | fnode->up = cpu_to_le32(new_dir->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | fnode->len = new_len; |
| 607 | memcpy(fnode->name, new_name, new_len>15?15:new_len); |
| 608 | if (new_len < 15) memset(&fnode->name[new_len], 0, 15 - new_len); |
| 609 | mark_buffer_dirty(bh); |
| 610 | brelse(bh); |
| 611 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | end1: |
Mikulas Patocka | f49a26e | 2015-09-02 22:51:53 +0200 | [diff] [blame] | 613 | if (!err) { |
| 614 | hpfs_update_directory_times(old_dir); |
| 615 | hpfs_update_directory_times(new_dir); |
| 616 | } |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 617 | hpfs_unlock(i->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | return err; |
| 619 | } |
| 620 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 621 | const struct inode_operations hpfs_dir_iops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | { |
| 623 | .create = hpfs_create, |
| 624 | .lookup = hpfs_lookup, |
| 625 | .unlink = hpfs_unlink, |
| 626 | .symlink = hpfs_symlink, |
| 627 | .mkdir = hpfs_mkdir, |
| 628 | .rmdir = hpfs_rmdir, |
| 629 | .mknod = hpfs_mknod, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 630 | .rename = hpfs_rename, |
Christoph Hellwig | ca30bc9 | 2008-08-11 00:27:59 +0200 | [diff] [blame] | 631 | .setattr = hpfs_setattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | }; |