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