| 1 | |
| 2 | |
| 3 | * linux/fs/hfsplus/dir.c
|
| 4 | *
|
| 5 | * Copyright (C) 2001
|
| 6 | * Brad Boyer ([email protected])
|
| 7 | * (C) 2003 Ardis Technologies <[email protected]>
|
| 8 | *
|
| 9 | * Handling of directories
|
| 10 |
|
| 11 |
|
| 12 | #include <linux/errno.h>
|
| 13 | #include <linux/fs.h>
|
| 14 | #include <linux/slab.h>
|
| 15 | #include <linux/random.h>
|
| 16 | #include <linux/nls.h>
|
| 17 |
|
| 18 | #include "hfsplus_fs.h"
|
| 19 | #include "hfsplus_raw.h"
|
| 20 | #include "xattr.h"
|
| 21 |
|
| 22 | static inline void hfsplus_instantiate(struct dentry *dentry,
|
| 23 | struct inode *inode, u32 cnid)
|
| 24 | {
|
| 25 | dentry->d_fsdata = (void *)(unsigned long)cnid;
|
| 26 | d_instantiate(dentry, inode);
|
| 27 | }
|
| 28 |
|
| 29 |
|
| 30 | static struct dentry *hfsplus_lookup(struct inode *dir, struct dentry *dentry,
|
| 31 | unsigned int flags)
|
| 32 | {
|
| 33 | struct inode *inode = NULL;
|
| 34 | struct hfs_find_data fd;
|
| 35 | struct super_block *sb;
|
| 36 | hfsplus_cat_entry entry;
|
| 37 | int err;
|
| 38 | u32 cnid, linkid = 0;
|
| 39 | u16 type;
|
| 40 |
|
| 41 | sb = dir->i_sb;
|
| 42 |
|
| 43 | dentry->d_fsdata = NULL;
|
| 44 | err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
|
| 45 | if (err)
|
| 46 | return ERR_PTR(err);
|
| 47 | err = hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino,
|
| 48 | &dentry->d_name);
|
| 49 | if (unlikely(err < 0))
|
| 50 | goto fail;
|
| 51 | again:
|
| 52 | err = hfsplus_brec_read_cat(&fd, &entry);
|
| 53 | if (err) {
|
| 54 | if (err == -ENOENT) {
|
| 55 | hfs_find_exit(&fd);
|
| 56 |
|
| 57 | inode = NULL;
|
| 58 | goto out;
|
| 59 | }
|
| 60 | goto fail;
|
| 61 | }
|
| 62 | type = be16_to_cpu(entry.type);
|
| 63 | if (type == HFSPLUS_FOLDER) {
|
| 64 | if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
|
| 65 | err = -EIO;
|
| 66 | goto fail;
|
| 67 | }
|
| 68 | cnid = be32_to_cpu(entry.folder.id);
|
| 69 | dentry->d_fsdata = (void *)(unsigned long)cnid;
|
| 70 | } else if (type == HFSPLUS_FILE) {
|
| 71 | if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
|
| 72 | err = -EIO;
|
| 73 | goto fail;
|
| 74 | }
|
| 75 | cnid = be32_to_cpu(entry.file.id);
|
| 76 | if (entry.file.user_info.fdType ==
|
| 77 | cpu_to_be32(HFSP_HARDLINK_TYPE) &&
|
| 78 | entry.file.user_info.fdCreator ==
|
| 79 | cpu_to_be32(HFSP_HFSPLUS_CREATOR) &&
|
| 80 | HFSPLUS_SB(sb)->hidden_dir &&
|
| 81 | (entry.file.create_date ==
|
| 82 | HFSPLUS_I(HFSPLUS_SB(sb)->hidden_dir)->
|
| 83 | create_date ||
|
| 84 | entry.file.create_date ==
|
| 85 | HFSPLUS_I(d_inode(sb->s_root))->
|
| 86 | create_date)) {
|
| 87 | struct qstr str;
|
| 88 | char name[32];
|
| 89 |
|
| 90 | if (dentry->d_fsdata) {
|
| 91 | |
| 92 | * We found a link pointing to another link,
|
| 93 | * so ignore it and treat it as regular file.
|
| 94 |
|
| 95 | cnid = (unsigned long)dentry->d_fsdata;
|
| 96 | linkid = 0;
|
| 97 | } else {
|
| 98 | dentry->d_fsdata = (void *)(unsigned long)cnid;
|
| 99 | linkid =
|
| 100 | be32_to_cpu(entry.file.permissions.dev);
|
| 101 | str.len = sprintf(name, "iNode%d", linkid);
|
| 102 | str.name = name;
|
| 103 | err = hfsplus_cat_build_key(sb, fd.search_key,
|
| 104 | HFSPLUS_SB(sb)->hidden_dir->i_ino,
|
| 105 | &str);
|
| 106 | if (unlikely(err < 0))
|
| 107 | goto fail;
|
| 108 | goto again;
|
| 109 | }
|
| 110 | } else if (!dentry->d_fsdata)
|
| 111 | dentry->d_fsdata = (void *)(unsigned long)cnid;
|
| 112 | } else {
|
| 113 | pr_err("invalid catalog entry type in lookup\n");
|
| 114 | err = -EIO;
|
| 115 | goto fail;
|
| 116 | }
|
| 117 | hfs_find_exit(&fd);
|
| 118 | inode = hfsplus_iget(dir->i_sb, cnid);
|
| 119 | if (IS_ERR(inode))
|
| 120 | return ERR_CAST(inode);
|
| 121 | if (S_ISREG(inode->i_mode))
|
| 122 | HFSPLUS_I(inode)->linkid = linkid;
|
| 123 | out:
|
| 124 | return d_splice_alias(inode, dentry);
|
| 125 | fail:
|
| 126 | hfs_find_exit(&fd);
|
| 127 | return ERR_PTR(err);
|
| 128 | }
|
| 129 |
|
| 130 | static int hfsplus_readdir(struct file *file, struct dir_context *ctx)
|
| 131 | {
|
| 132 | struct inode *inode = file_inode(file);
|
| 133 | struct super_block *sb = inode->i_sb;
|
| 134 | int len, err;
|
| 135 | char *strbuf;
|
| 136 | hfsplus_cat_entry entry;
|
| 137 | struct hfs_find_data fd;
|
| 138 | struct hfsplus_readdir_data *rd;
|
| 139 | u16 type;
|
| 140 |
|
| 141 | if (file->f_pos >= inode->i_size)
|
| 142 | return 0;
|
| 143 |
|
| 144 | err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
|
| 145 | if (err)
|
| 146 | return err;
|
| 147 | strbuf = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_MAX_STRLEN + 1, GFP_KERNEL);
|
| 148 | if (!strbuf) {
|
| 149 | err = -ENOMEM;
|
| 150 | goto out;
|
| 151 | }
|
| 152 | hfsplus_cat_build_key_with_cnid(sb, fd.search_key, inode->i_ino);
|
| 153 | err = hfs_brec_find(&fd, hfs_find_rec_by_key);
|
| 154 | if (err)
|
| 155 | goto out;
|
| 156 |
|
| 157 | if (ctx->pos == 0) {
|
| 158 |
|
| 159 | if (!dir_emit_dot(file, ctx))
|
| 160 | goto out;
|
| 161 | ctx->pos = 1;
|
| 162 | }
|
| 163 | if (ctx->pos == 1) {
|
| 164 | if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
|
| 165 | err = -EIO;
|
| 166 | goto out;
|
| 167 | }
|
| 168 |
|
| 169 | hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
|
| 170 | fd.entrylength);
|
| 171 | if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
|
| 172 | pr_err("bad catalog folder thread\n");
|
| 173 | err = -EIO;
|
| 174 | goto out;
|
| 175 | }
|
| 176 | if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
|
| 177 | pr_err("truncated catalog thread\n");
|
| 178 | err = -EIO;
|
| 179 | goto out;
|
| 180 | }
|
| 181 | if (!dir_emit(ctx, "..", 2,
|
| 182 | be32_to_cpu(entry.thread.parentID), DT_DIR))
|
| 183 | goto out;
|
| 184 | ctx->pos = 2;
|
| 185 | }
|
| 186 | if (ctx->pos >= inode->i_size)
|
| 187 | goto out;
|
| 188 | rd = file->private_data;
|
| 189 | if (rd && rd->pos == ctx->pos) {
|
| 190 | memcpy(fd.search_key, &rd->key, sizeof(struct hfsplus_cat_key));
|
| 191 | err = hfs_brec_find(&fd, hfs_find_rec_by_key);
|
| 192 | if (err == -ENOENT)
|
| 193 | err = hfs_brec_goto(&fd, 1);
|
| 194 | } else {
|
| 195 | err = hfs_brec_goto(&fd, ctx->pos - 1);
|
| 196 | }
|
| 197 | if (err)
|
| 198 | goto out;
|
| 199 | for (;;) {
|
| 200 | if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
|
| 201 | pr_err("walked past end of dir\n");
|
| 202 | err = -EIO;
|
| 203 | goto out;
|
| 204 | }
|
| 205 |
|
| 206 | if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
|
| 207 | err = -EIO;
|
| 208 | goto out;
|
| 209 | }
|
| 210 |
|
| 211 | hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
|
| 212 | fd.entrylength);
|
| 213 | type = be16_to_cpu(entry.type);
|
| 214 | len = NLS_MAX_CHARSET_SIZE * HFSPLUS_MAX_STRLEN;
|
| 215 | err = hfsplus_uni2asc_str(sb, &fd.key->cat.name, strbuf, &len);
|
| 216 | if (err)
|
| 217 | goto out;
|
| 218 | if (type == HFSPLUS_FOLDER) {
|
| 219 | if (fd.entrylength <
|
| 220 | sizeof(struct hfsplus_cat_folder)) {
|
| 221 | pr_err("small dir entry\n");
|
| 222 | err = -EIO;
|
| 223 | goto out;
|
| 224 | }
|
| 225 | if (HFSPLUS_SB(sb)->hidden_dir &&
|
| 226 | HFSPLUS_SB(sb)->hidden_dir->i_ino ==
|
| 227 | be32_to_cpu(entry.folder.id))
|
| 228 | goto next;
|
| 229 | if (!dir_emit(ctx, strbuf, len,
|
| 230 | be32_to_cpu(entry.folder.id), DT_DIR))
|
| 231 | break;
|
| 232 | } else if (type == HFSPLUS_FILE) {
|
| 233 | u16 mode;
|
| 234 | unsigned type = DT_UNKNOWN;
|
| 235 |
|
| 236 | if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
|
| 237 | pr_err("small file entry\n");
|
| 238 | err = -EIO;
|
| 239 | goto out;
|
| 240 | }
|
| 241 |
|
| 242 | mode = be16_to_cpu(entry.file.permissions.mode);
|
| 243 | if (S_ISREG(mode))
|
| 244 | type = DT_REG;
|
| 245 | else if (S_ISLNK(mode))
|
| 246 | type = DT_LNK;
|
| 247 | else if (S_ISFIFO(mode))
|
| 248 | type = DT_FIFO;
|
| 249 | else if (S_ISCHR(mode))
|
| 250 | type = DT_CHR;
|
| 251 | else if (S_ISBLK(mode))
|
| 252 | type = DT_BLK;
|
| 253 | else if (S_ISSOCK(mode))
|
| 254 | type = DT_SOCK;
|
| 255 |
|
| 256 | if (!dir_emit(ctx, strbuf, len,
|
| 257 | be32_to_cpu(entry.file.id), type))
|
| 258 | break;
|
| 259 | } else {
|
| 260 | pr_err("bad catalog entry type\n");
|
| 261 | err = -EIO;
|
| 262 | goto out;
|
| 263 | }
|
| 264 | next:
|
| 265 | ctx->pos++;
|
| 266 | if (ctx->pos >= inode->i_size)
|
| 267 | goto out;
|
| 268 | err = hfs_brec_goto(&fd, 1);
|
| 269 | if (err)
|
| 270 | goto out;
|
| 271 | }
|
| 272 | if (!rd) {
|
| 273 | rd = kmalloc_obj(struct hfsplus_readdir_data);
|
| 274 | if (!rd) {
|
| 275 | err = -ENOMEM;
|
| 276 | goto out;
|
| 277 | }
|
| 278 | file->private_data = rd;
|
| 279 | }
|
| 280 | rd->pos = ctx->pos;
|
| 281 | memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key));
|
| 282 | out:
|
| 283 | kfree(strbuf);
|
| 284 | hfs_find_exit(&fd);
|
| 285 | return err;
|
| 286 | }
|
| 287 |
|
| 288 | static int hfsplus_dir_release(struct inode *inode, struct file *file)
|
| 289 | {
|
| 290 | kfree(file->private_data);
|
| 291 | return 0;
|
| 292 | }
|
| 293 |
|
| 294 | static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
|
| 295 | struct dentry *dst_dentry)
|
| 296 | {
|
| 297 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
|
| 298 | struct inode *inode = d_inode(src_dentry);
|
| 299 | struct inode *src_dir = d_inode(src_dentry->d_parent);
|
| 300 | struct qstr str;
|
| 301 | char name[32];
|
| 302 | u32 cnid, id;
|
| 303 | int res;
|
| 304 |
|
| 305 | if (HFSPLUS_IS_RSRC(inode))
|
| 306 | return -EPERM;
|
| 307 | if (!S_ISREG(inode->i_mode))
|
| 308 | return -EPERM;
|
| 309 |
|
| 310 | hfs_dbg("src_dir->i_ino %llu, dst_dir->i_ino %llu, inode->i_ino %llu\n",
|
| 311 | src_dir->i_ino, dst_dir->i_ino, inode->i_ino);
|
| 312 |
|
| 313 | mutex_lock(&sbi->vh_mutex);
|
| 314 | if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
|
| 315 | for (;;) {
|
| 316 | get_random_bytes(&id, sizeof(cnid));
|
| 317 | id &= 0x3fffffff;
|
| 318 | str.name = name;
|
| 319 | str.len = sprintf(name, "iNode%d", id);
|
| 320 | res = hfsplus_rename_cat(inode->i_ino,
|
| 321 | src_dir, &src_dentry->d_name,
|
| 322 | sbi->hidden_dir, &str);
|
| 323 | if (!res)
|
| 324 | break;
|
| 325 | if (res != -EEXIST)
|
| 326 | goto out;
|
| 327 | }
|
| 328 | HFSPLUS_I(inode)->linkid = id;
|
| 329 | cnid = sbi->next_cnid++;
|
| 330 | src_dentry->d_fsdata = (void *)(unsigned long)cnid;
|
| 331 | res = hfsplus_create_cat(cnid, src_dir,
|
| 332 | &src_dentry->d_name, inode);
|
| 333 | if (res)
|
| 334 |
|
| 335 | goto out;
|
| 336 | sbi->file_count++;
|
| 337 | }
|
| 338 | cnid = sbi->next_cnid++;
|
| 339 | res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
|
| 340 | if (res)
|
| 341 | goto out;
|
| 342 |
|
| 343 | inc_nlink(inode);
|
| 344 | hfsplus_instantiate(dst_dentry, inode, cnid);
|
| 345 | ihold(inode);
|
| 346 | inode_set_ctime_current(inode);
|
| 347 | mark_inode_dirty(inode);
|
| 348 | sbi->file_count++;
|
| 349 | hfsplus_mark_mdb_dirty(dst_dir->i_sb);
|
| 350 |
|
| 351 | res = hfsplus_cat_write_inode(src_dir);
|
| 352 | if (res)
|
| 353 | goto out;
|
| 354 |
|
| 355 | res = hfsplus_cat_write_inode(dst_dir);
|
| 356 | if (res)
|
| 357 | goto out;
|
| 358 |
|
| 359 | res = hfsplus_cat_write_inode(sbi->hidden_dir);
|
| 360 | if (res)
|
| 361 | goto out;
|
| 362 |
|
| 363 | res = hfsplus_cat_write_inode(inode);
|
| 364 |
|
| 365 | out:
|
| 366 | mutex_unlock(&sbi->vh_mutex);
|
| 367 | return res;
|
| 368 | }
|
| 369 |
|
| 370 | static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
|
| 371 | {
|
| 372 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
|
| 373 | struct inode *inode = d_inode(dentry);
|
| 374 | struct qstr str;
|
| 375 | char name[32];
|
| 376 | u32 cnid;
|
| 377 | int res;
|
| 378 |
|
| 379 | if (HFSPLUS_IS_RSRC(inode))
|
| 380 | return -EPERM;
|
| 381 |
|
| 382 | hfs_dbg("dir->i_ino %llu, inode->i_ino %llu\n",
|
| 383 | dir->i_ino, inode->i_ino);
|
| 384 |
|
| 385 | mutex_lock(&sbi->vh_mutex);
|
| 386 | cnid = (u32)(unsigned long)dentry->d_fsdata;
|
| 387 | if (inode->i_ino == cnid &&
|
| 388 | atomic_read(&HFSPLUS_I(inode)->opencnt)) {
|
| 389 | str.name = name;
|
| 390 | str.len = sprintf(name, "temp%llu", inode->i_ino);
|
| 391 | res = hfsplus_rename_cat(inode->i_ino,
|
| 392 | dir, &dentry->d_name,
|
| 393 | sbi->hidden_dir, &str);
|
| 394 | if (!res) {
|
| 395 | inode->i_flags |= S_DEAD;
|
| 396 | drop_nlink(inode);
|
| 397 | }
|
| 398 | goto out;
|
| 399 | }
|
| 400 | res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
|
| 401 | if (res)
|
| 402 | goto out;
|
| 403 |
|
| 404 | if (inode->i_nlink > 0)
|
| 405 | drop_nlink(inode);
|
| 406 | if (inode->i_ino == cnid)
|
| 407 | clear_nlink(inode);
|
| 408 | if (!inode->i_nlink) {
|
| 409 | if (inode->i_ino != cnid) {
|
| 410 | sbi->file_count--;
|
| 411 | if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
|
| 412 | res = hfsplus_delete_cat(inode->i_ino,
|
| 413 | sbi->hidden_dir,
|
| 414 | NULL);
|
| 415 | if (!res)
|
| 416 | hfsplus_delete_inode(inode);
|
| 417 | } else
|
| 418 | inode->i_flags |= S_DEAD;
|
| 419 | } else
|
| 420 | hfsplus_delete_inode(inode);
|
| 421 | } else
|
| 422 | sbi->file_count--;
|
| 423 | inode_set_ctime_current(inode);
|
| 424 | mark_inode_dirty(inode);
|
| 425 | out:
|
| 426 | if (!res) {
|
| 427 | res = hfsplus_cat_write_inode(dir);
|
| 428 | if (!res) {
|
| 429 | res = hfsplus_cat_write_inode(sbi->hidden_dir);
|
| 430 | if (!res)
|
| 431 | res = hfsplus_cat_write_inode(inode);
|
| 432 | }
|
| 433 | }
|
| 434 |
|
| 435 | mutex_unlock(&sbi->vh_mutex);
|
| 436 | return res;
|
| 437 | }
|
| 438 |
|
| 439 | static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
|
| 440 | {
|
| 441 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
|
| 442 | struct inode *inode = d_inode(dentry);
|
| 443 | int res;
|
| 444 |
|
| 445 | if (inode->i_size != 2)
|
| 446 | return -ENOTEMPTY;
|
| 447 |
|
| 448 | mutex_lock(&sbi->vh_mutex);
|
| 449 | res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
|
| 450 | if (res)
|
| 451 | goto out;
|
| 452 | clear_nlink(inode);
|
| 453 | inode_set_ctime_current(inode);
|
| 454 | hfsplus_delete_inode(inode);
|
| 455 | mark_inode_dirty(inode);
|
| 456 |
|
| 457 | res = hfsplus_cat_write_inode(dir);
|
| 458 | out:
|
| 459 | mutex_unlock(&sbi->vh_mutex);
|
| 460 | return res;
|
| 461 | }
|
| 462 |
|
| 463 | static int hfsplus_symlink(const struct mnt_idmap *idmap, struct inode *dir,
|
| 464 | struct dentry *dentry, const char *symname)
|
| 465 | {
|
| 466 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
|
| 467 | struct inode *inode;
|
| 468 | int res = -ENOMEM;
|
| 469 |
|
| 470 | mutex_lock(&sbi->vh_mutex);
|
| 471 | inode = hfsplus_new_inode(dir->i_sb, dir, S_IFLNK | S_IRWXUGO);
|
| 472 | if (!inode)
|
| 473 | goto out;
|
| 474 |
|
| 475 | hfs_dbg("dir->i_ino %llu, inode->i_ino %llu\n",
|
| 476 | dir->i_ino, inode->i_ino);
|
| 477 |
|
| 478 | res = page_symlink(inode, symname, strlen(symname) + 1);
|
| 479 | if (res)
|
| 480 | goto out_err;
|
| 481 |
|
| 482 | res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
|
| 483 | if (res)
|
| 484 | goto out_err;
|
| 485 |
|
| 486 | res = hfsplus_init_security(inode, dir, &dentry->d_name);
|
| 487 | if (res == -EOPNOTSUPP)
|
| 488 | res = 0;
|
| 489 | else if (res) {
|
| 490 |
|
| 491 | hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
|
| 492 | goto out_err;
|
| 493 | }
|
| 494 |
|
| 495 | hfsplus_instantiate(dentry, inode, inode->i_ino);
|
| 496 | mark_inode_dirty(inode);
|
| 497 |
|
| 498 | res = hfsplus_cat_write_inode(dir);
|
| 499 | if (res)
|
| 500 | goto out;
|
| 501 |
|
| 502 | res = hfsplus_cat_write_inode(inode);
|
| 503 | goto out;
|
| 504 |
|
| 505 | out_err:
|
| 506 | clear_nlink(inode);
|
| 507 | hfsplus_delete_inode(inode);
|
| 508 | iput(inode);
|
| 509 | out:
|
| 510 | mutex_unlock(&sbi->vh_mutex);
|
| 511 | return res;
|
| 512 | }
|
| 513 |
|
| 514 | static int hfsplus_mknod(const struct mnt_idmap *idmap, struct inode *dir,
|
| 515 | struct dentry *dentry, umode_t mode, dev_t rdev)
|
| 516 | {
|
| 517 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
|
| 518 | struct inode *inode;
|
| 519 | int res = -ENOMEM;
|
| 520 |
|
| 521 | mutex_lock(&sbi->vh_mutex);
|
| 522 | inode = hfsplus_new_inode(dir->i_sb, dir, mode);
|
| 523 | if (!inode)
|
| 524 | goto out;
|
| 525 |
|
| 526 | hfs_dbg("dir->i_ino %llu, inode->i_ino %llu\n",
|
| 527 | dir->i_ino, inode->i_ino);
|
| 528 |
|
| 529 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode))
|
| 530 | init_special_inode(inode, mode, rdev);
|
| 531 |
|
| 532 | res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
|
| 533 | if (res)
|
| 534 | goto failed_mknod;
|
| 535 |
|
| 536 | res = hfsplus_init_security(inode, dir, &dentry->d_name);
|
| 537 | if (res == -EOPNOTSUPP)
|
| 538 | res = 0;
|
| 539 | else if (res) {
|
| 540 |
|
| 541 | hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
|
| 542 | goto failed_mknod;
|
| 543 | }
|
| 544 |
|
| 545 | hfsplus_instantiate(dentry, inode, inode->i_ino);
|
| 546 | mark_inode_dirty(inode);
|
| 547 |
|
| 548 | res = hfsplus_cat_write_inode(dir);
|
| 549 | if (res)
|
| 550 | goto out;
|
| 551 |
|
| 552 | res = hfsplus_cat_write_inode(inode);
|
| 553 | goto out;
|
| 554 |
|
| 555 | failed_mknod:
|
| 556 | clear_nlink(inode);
|
| 557 | hfsplus_delete_inode(inode);
|
| 558 | iput(inode);
|
| 559 | out:
|
| 560 | mutex_unlock(&sbi->vh_mutex);
|
| 561 | return res;
|
| 562 | }
|
| 563 |
|
| 564 | static int hfsplus_create(const struct mnt_idmap *idmap, struct inode *dir,
|
| 565 | struct dentry *dentry, umode_t mode)
|
| 566 | {
|
| 567 | return hfsplus_mknod(&nop_mnt_idmap, dir, dentry, mode, 0);
|
| 568 | }
|
| 569 |
|
| 570 | static struct dentry *hfsplus_mkdir(const struct mnt_idmap *idmap, struct inode *dir,
|
| 571 | struct dentry *dentry, umode_t mode)
|
| 572 | {
|
| 573 | return ERR_PTR(hfsplus_mknod(&nop_mnt_idmap, dir, dentry, mode, 0));
|
| 574 | }
|
| 575 |
|
| 576 | static int hfsplus_rename(const struct mnt_idmap *idmap,
|
| 577 | struct inode *old_dir, struct dentry *old_dentry,
|
| 578 | struct inode *new_dir, struct dentry *new_dentry,
|
| 579 | unsigned int flags)
|
| 580 | {
|
| 581 | int res;
|
| 582 |
|
| 583 | if (flags & ~RENAME_NOREPLACE)
|
| 584 | return -EINVAL;
|
| 585 |
|
| 586 |
|
| 587 | if (d_really_is_positive(new_dentry)) {
|
| 588 | if (d_is_dir(new_dentry))
|
| 589 | res = hfsplus_rmdir(new_dir, new_dentry);
|
| 590 | else
|
| 591 | res = hfsplus_unlink(new_dir, new_dentry);
|
| 592 | if (res)
|
| 593 | return res;
|
| 594 | }
|
| 595 |
|
| 596 | res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
|
| 597 | old_dir, &old_dentry->d_name,
|
| 598 | new_dir, &new_dentry->d_name);
|
| 599 | if (!res) {
|
| 600 | struct inode *inode = d_inode(old_dentry);
|
| 601 |
|
| 602 | new_dentry->d_fsdata = old_dentry->d_fsdata;
|
| 603 |
|
| 604 | inode_set_ctime_current(inode);
|
| 605 | mark_inode_dirty(inode);
|
| 606 |
|
| 607 | res = hfsplus_cat_write_inode(old_dir);
|
| 608 | if (res)
|
| 609 | return res;
|
| 610 |
|
| 611 | res = hfsplus_cat_write_inode(new_dir);
|
| 612 | if (res)
|
| 613 | return res;
|
| 614 |
|
| 615 | res = hfsplus_cat_write_inode(inode);
|
| 616 | }
|
| 617 | return res;
|
| 618 | }
|
| 619 |
|
| 620 | const struct inode_operations hfsplus_dir_inode_operations = {
|
| 621 | .lookup = hfsplus_lookup,
|
| 622 | .create = hfsplus_create,
|
| 623 | .link = hfsplus_link,
|
| 624 | .unlink = hfsplus_unlink,
|
| 625 | .mkdir = hfsplus_mkdir,
|
| 626 | .rmdir = hfsplus_rmdir,
|
| 627 | .symlink = hfsplus_symlink,
|
| 628 | .mknod = hfsplus_mknod,
|
| 629 | .rename = hfsplus_rename,
|
| 630 | .getattr = hfsplus_getattr,
|
| 631 | .listxattr = hfsplus_listxattr,
|
| 632 | .fileattr_get = hfsplus_fileattr_get,
|
| 633 | .fileattr_set = hfsplus_fileattr_set,
|
| 634 | };
|
| 635 |
|
| 636 | const struct file_operations hfsplus_dir_operations = {
|
| 637 | .fsync = hfsplus_file_fsync,
|
| 638 | .read = generic_read_dir,
|
| 639 | .iterate_shared = hfsplus_readdir,
|
| 640 | .unlocked_ioctl = hfsplus_ioctl,
|
| 641 | .llseek = generic_file_llseek,
|
| 642 | .release = hfsplus_dir_release,
|
| 643 | };
|