Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/hfsplus/dir.c |
| 3 | * |
| 4 | * Copyright (C) 2001 |
| 5 | * Brad Boyer (flar@allandria.com) |
| 6 | * (C) 2003 Ardis Technologies <roman@ardistech.com> |
| 7 | * |
| 8 | * Handling of directories |
| 9 | */ |
| 10 | |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/slab.h> |
| 14 | #include <linux/random.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | #include "hfsplus_fs.h" |
| 17 | #include "hfsplus_raw.h" |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 18 | #include "xattr.h" |
Vyacheslav Dubeyko | b4c1107 | 2013-09-11 14:24:30 -0700 | [diff] [blame] | 19 | #include "acl.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | static inline void hfsplus_instantiate(struct dentry *dentry, |
| 22 | struct inode *inode, u32 cnid) |
| 23 | { |
| 24 | dentry->d_fsdata = (void *)(unsigned long)cnid; |
| 25 | d_instantiate(dentry, inode); |
| 26 | } |
| 27 | |
| 28 | /* Find the entry inside dir named dentry->d_name */ |
| 29 | static struct dentry *hfsplus_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 30 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | { |
| 32 | struct inode *inode = NULL; |
| 33 | struct hfs_find_data fd; |
| 34 | struct super_block *sb; |
| 35 | hfsplus_cat_entry entry; |
| 36 | int err; |
| 37 | u32 cnid, linkid = 0; |
| 38 | u16 type; |
| 39 | |
| 40 | sb = dir->i_sb; |
Duane Griffin | d45bce8 | 2007-07-15 23:41:23 -0700 | [diff] [blame] | 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | dentry->d_fsdata = NULL; |
Alexey Khoroshilov | 5bd9d99 | 2011-07-06 02:29:59 +0400 | [diff] [blame] | 43 | err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd); |
| 44 | if (err) |
| 45 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, &dentry->d_name); |
| 47 | again: |
| 48 | err = hfs_brec_read(&fd, &entry, sizeof(entry)); |
| 49 | if (err) { |
| 50 | if (err == -ENOENT) { |
| 51 | hfs_find_exit(&fd); |
| 52 | /* No such entry */ |
| 53 | inode = NULL; |
| 54 | goto out; |
| 55 | } |
| 56 | goto fail; |
| 57 | } |
| 58 | type = be16_to_cpu(entry.type); |
| 59 | if (type == HFSPLUS_FOLDER) { |
| 60 | if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) { |
| 61 | err = -EIO; |
| 62 | goto fail; |
| 63 | } |
| 64 | cnid = be32_to_cpu(entry.folder.id); |
| 65 | dentry->d_fsdata = (void *)(unsigned long)cnid; |
| 66 | } else if (type == HFSPLUS_FILE) { |
| 67 | if (fd.entrylength < sizeof(struct hfsplus_cat_file)) { |
| 68 | err = -EIO; |
| 69 | goto fail; |
| 70 | } |
| 71 | cnid = be32_to_cpu(entry.file.id); |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 72 | if (entry.file.user_info.fdType == |
| 73 | cpu_to_be32(HFSP_HARDLINK_TYPE) && |
| 74 | entry.file.user_info.fdCreator == |
| 75 | cpu_to_be32(HFSP_HFSPLUS_CREATOR) && |
| 76 | (entry.file.create_date == |
| 77 | HFSPLUS_I(HFSPLUS_SB(sb)->hidden_dir)-> |
| 78 | create_date || |
| 79 | entry.file.create_date == |
| 80 | HFSPLUS_I(sb->s_root->d_inode)-> |
| 81 | create_date) && |
| 82 | HFSPLUS_SB(sb)->hidden_dir) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | struct qstr str; |
| 84 | char name[32]; |
| 85 | |
| 86 | if (dentry->d_fsdata) { |
Roman Zippel | af8c85b | 2006-01-18 17:43:10 -0800 | [diff] [blame] | 87 | /* |
| 88 | * We found a link pointing to another link, |
| 89 | * so ignore it and treat it as regular file. |
| 90 | */ |
| 91 | cnid = (unsigned long)dentry->d_fsdata; |
| 92 | linkid = 0; |
| 93 | } else { |
| 94 | dentry->d_fsdata = (void *)(unsigned long)cnid; |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 95 | linkid = |
| 96 | be32_to_cpu(entry.file.permissions.dev); |
Roman Zippel | af8c85b | 2006-01-18 17:43:10 -0800 | [diff] [blame] | 97 | str.len = sprintf(name, "iNode%d", linkid); |
| 98 | str.name = name; |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 99 | hfsplus_cat_build_key(sb, fd.search_key, |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 100 | HFSPLUS_SB(sb)->hidden_dir->i_ino, |
| 101 | &str); |
Roman Zippel | af8c85b | 2006-01-18 17:43:10 -0800 | [diff] [blame] | 102 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } else if (!dentry->d_fsdata) |
| 105 | dentry->d_fsdata = (void *)(unsigned long)cnid; |
| 106 | } else { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 107 | pr_err("invalid catalog entry type in lookup\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | err = -EIO; |
| 109 | goto fail; |
| 110 | } |
| 111 | hfs_find_exit(&fd); |
David Howells | 6352539 | 2008-02-07 00:15:40 -0800 | [diff] [blame] | 112 | inode = hfsplus_iget(dir->i_sb, cnid); |
| 113 | if (IS_ERR(inode)) |
| 114 | return ERR_CAST(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | if (S_ISREG(inode->i_mode)) |
Christoph Hellwig | f6089ff | 2010-10-14 09:54:28 -0400 | [diff] [blame] | 116 | HFSPLUS_I(inode)->linkid = linkid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | out: |
| 118 | d_add(dentry, inode); |
| 119 | return NULL; |
| 120 | fail: |
| 121 | hfs_find_exit(&fd); |
| 122 | return ERR_PTR(err); |
| 123 | } |
| 124 | |
Al Viro | e72514e | 2013-05-22 14:59:39 -0400 | [diff] [blame] | 125 | static int hfsplus_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Al Viro | e72514e | 2013-05-22 14:59:39 -0400 | [diff] [blame] | 127 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | struct super_block *sb = inode->i_sb; |
| 129 | int len, err; |
| 130 | char strbuf[HFSPLUS_MAX_STRLEN + 1]; |
| 131 | hfsplus_cat_entry entry; |
| 132 | struct hfs_find_data fd; |
| 133 | struct hfsplus_readdir_data *rd; |
| 134 | u16 type; |
| 135 | |
Al Viro | e72514e | 2013-05-22 14:59:39 -0400 | [diff] [blame] | 136 | if (file->f_pos >= inode->i_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | return 0; |
| 138 | |
Alexey Khoroshilov | 5bd9d99 | 2011-07-06 02:29:59 +0400 | [diff] [blame] | 139 | err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd); |
| 140 | if (err) |
| 141 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | hfsplus_cat_build_key(sb, fd.search_key, inode->i_ino, NULL); |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 143 | err = hfs_brec_find(&fd, hfs_find_rec_by_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | if (err) |
| 145 | goto out; |
| 146 | |
Al Viro | e72514e | 2013-05-22 14:59:39 -0400 | [diff] [blame] | 147 | if (ctx->pos == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | /* This is completely artificial... */ |
Al Viro | e72514e | 2013-05-22 14:59:39 -0400 | [diff] [blame] | 149 | if (!dir_emit_dot(file, ctx)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | goto out; |
Al Viro | e72514e | 2013-05-22 14:59:39 -0400 | [diff] [blame] | 151 | ctx->pos = 1; |
| 152 | } |
| 153 | if (ctx->pos == 1) { |
Greg Kroah-Hartman | 6f24f89 | 2012-05-04 12:09:39 -0700 | [diff] [blame] | 154 | if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) { |
| 155 | err = -EIO; |
| 156 | goto out; |
| 157 | } |
| 158 | |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 159 | hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, |
| 160 | fd.entrylength); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 162 | pr_err("bad catalog folder thread\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | err = -EIO; |
| 164 | goto out; |
| 165 | } |
| 166 | if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 167 | pr_err("truncated catalog thread\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | err = -EIO; |
| 169 | goto out; |
| 170 | } |
Al Viro | e72514e | 2013-05-22 14:59:39 -0400 | [diff] [blame] | 171 | if (!dir_emit(ctx, "..", 2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | be32_to_cpu(entry.thread.parentID), DT_DIR)) |
| 173 | goto out; |
Al Viro | e72514e | 2013-05-22 14:59:39 -0400 | [diff] [blame] | 174 | ctx->pos = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
Al Viro | e72514e | 2013-05-22 14:59:39 -0400 | [diff] [blame] | 176 | if (ctx->pos >= inode->i_size) |
| 177 | goto out; |
| 178 | err = hfs_brec_goto(&fd, ctx->pos - 1); |
| 179 | if (err) |
| 180 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | for (;;) { |
| 182 | if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 183 | pr_err("walked past end of dir\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | err = -EIO; |
| 185 | goto out; |
| 186 | } |
Greg Kroah-Hartman | 6f24f89 | 2012-05-04 12:09:39 -0700 | [diff] [blame] | 187 | |
| 188 | if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) { |
| 189 | err = -EIO; |
| 190 | goto out; |
| 191 | } |
| 192 | |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 193 | hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, |
| 194 | fd.entrylength); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | type = be16_to_cpu(entry.type); |
| 196 | len = HFSPLUS_MAX_STRLEN; |
| 197 | err = hfsplus_uni2asc(sb, &fd.key->cat.name, strbuf, &len); |
| 198 | if (err) |
| 199 | goto out; |
| 200 | if (type == HFSPLUS_FOLDER) { |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 201 | if (fd.entrylength < |
| 202 | sizeof(struct hfsplus_cat_folder)) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 203 | pr_err("small dir entry\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | err = -EIO; |
| 205 | goto out; |
| 206 | } |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 207 | if (HFSPLUS_SB(sb)->hidden_dir && |
| 208 | HFSPLUS_SB(sb)->hidden_dir->i_ino == |
| 209 | be32_to_cpu(entry.folder.id)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | goto next; |
Al Viro | e72514e | 2013-05-22 14:59:39 -0400 | [diff] [blame] | 211 | if (!dir_emit(ctx, strbuf, len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | be32_to_cpu(entry.folder.id), DT_DIR)) |
| 213 | break; |
| 214 | } else if (type == HFSPLUS_FILE) { |
| 215 | if (fd.entrylength < sizeof(struct hfsplus_cat_file)) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 216 | pr_err("small file entry\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | err = -EIO; |
| 218 | goto out; |
| 219 | } |
Al Viro | e72514e | 2013-05-22 14:59:39 -0400 | [diff] [blame] | 220 | if (!dir_emit(ctx, strbuf, len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | be32_to_cpu(entry.file.id), DT_REG)) |
| 222 | break; |
| 223 | } else { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 224 | pr_err("bad catalog entry type\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | err = -EIO; |
| 226 | goto out; |
| 227 | } |
Anton Salikhmetov | 20b7643 | 2010-12-16 18:08:40 +0200 | [diff] [blame] | 228 | next: |
Al Viro | e72514e | 2013-05-22 14:59:39 -0400 | [diff] [blame] | 229 | ctx->pos++; |
| 230 | if (ctx->pos >= inode->i_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | goto out; |
| 232 | err = hfs_brec_goto(&fd, 1); |
| 233 | if (err) |
| 234 | goto out; |
| 235 | } |
Al Viro | e72514e | 2013-05-22 14:59:39 -0400 | [diff] [blame] | 236 | rd = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | if (!rd) { |
| 238 | rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL); |
| 239 | if (!rd) { |
| 240 | err = -ENOMEM; |
| 241 | goto out; |
| 242 | } |
Al Viro | e72514e | 2013-05-22 14:59:39 -0400 | [diff] [blame] | 243 | file->private_data = rd; |
| 244 | rd->file = file; |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 245 | list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
| 247 | memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key)); |
| 248 | out: |
| 249 | hfs_find_exit(&fd); |
| 250 | return err; |
| 251 | } |
| 252 | |
| 253 | static int hfsplus_dir_release(struct inode *inode, struct file *file) |
| 254 | { |
| 255 | struct hfsplus_readdir_data *rd = file->private_data; |
| 256 | if (rd) { |
Christoph Hellwig | 89755dc | 2010-10-01 05:45:25 +0200 | [diff] [blame] | 257 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | list_del(&rd->list); |
Christoph Hellwig | 89755dc | 2010-10-01 05:45:25 +0200 | [diff] [blame] | 259 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | kfree(rd); |
| 261 | } |
| 262 | return 0; |
| 263 | } |
| 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir, |
| 266 | struct dentry *dst_dentry) |
| 267 | { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 268 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | struct inode *inode = src_dentry->d_inode; |
| 270 | struct inode *src_dir = src_dentry->d_parent->d_inode; |
| 271 | struct qstr str; |
| 272 | char name[32]; |
| 273 | u32 cnid, id; |
| 274 | int res; |
| 275 | |
| 276 | if (HFSPLUS_IS_RSRC(inode)) |
| 277 | return -EPERM; |
Christoph Hellwig | f6089ff | 2010-10-14 09:54:28 -0400 | [diff] [blame] | 278 | if (!S_ISREG(inode->i_mode)) |
| 279 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 281 | mutex_lock(&sbi->vh_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) { |
| 283 | for (;;) { |
| 284 | get_random_bytes(&id, sizeof(cnid)); |
| 285 | id &= 0x3fffffff; |
| 286 | str.name = name; |
| 287 | str.len = sprintf(name, "iNode%d", id); |
| 288 | res = hfsplus_rename_cat(inode->i_ino, |
| 289 | src_dir, &src_dentry->d_name, |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 290 | sbi->hidden_dir, &str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | if (!res) |
| 292 | break; |
| 293 | if (res != -EEXIST) |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 294 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
Christoph Hellwig | f6089ff | 2010-10-14 09:54:28 -0400 | [diff] [blame] | 296 | HFSPLUS_I(inode)->linkid = id; |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 297 | cnid = sbi->next_cnid++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | src_dentry->d_fsdata = (void *)(unsigned long)cnid; |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 299 | res = hfsplus_create_cat(cnid, src_dir, |
| 300 | &src_dentry->d_name, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | if (res) |
| 302 | /* panic? */ |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 303 | goto out; |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 304 | sbi->file_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 306 | cnid = sbi->next_cnid++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode); |
| 308 | if (res) |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 309 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 311 | inc_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | hfsplus_instantiate(dst_dentry, inode, cnid); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 313 | ihold(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | inode->i_ctime = CURRENT_TIME_SEC; |
| 315 | mark_inode_dirty(inode); |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 316 | sbi->file_count++; |
Artem Bityutskiy | 9e6c582 | 2012-07-12 17:26:31 +0300 | [diff] [blame] | 317 | hfsplus_mark_mdb_dirty(dst_dir->i_sb); |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 318 | out: |
| 319 | mutex_unlock(&sbi->vh_mutex); |
| 320 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | static int hfsplus_unlink(struct inode *dir, struct dentry *dentry) |
| 324 | { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 325 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | struct inode *inode = dentry->d_inode; |
| 327 | struct qstr str; |
| 328 | char name[32]; |
| 329 | u32 cnid; |
| 330 | int res; |
| 331 | |
| 332 | if (HFSPLUS_IS_RSRC(inode)) |
| 333 | return -EPERM; |
| 334 | |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 335 | mutex_lock(&sbi->vh_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | cnid = (u32)(unsigned long)dentry->d_fsdata; |
| 337 | if (inode->i_ino == cnid && |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 338 | atomic_read(&HFSPLUS_I(inode)->opencnt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | str.name = name; |
| 340 | str.len = sprintf(name, "temp%lu", inode->i_ino); |
| 341 | res = hfsplus_rename_cat(inode->i_ino, |
| 342 | dir, &dentry->d_name, |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 343 | sbi->hidden_dir, &str); |
Christoph Hellwig | 85b8fe8 | 2010-10-27 13:45:50 +0200 | [diff] [blame] | 344 | if (!res) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | inode->i_flags |= S_DEAD; |
Christoph Hellwig | 85b8fe8 | 2010-10-27 13:45:50 +0200 | [diff] [blame] | 346 | drop_nlink(inode); |
| 347 | } |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 348 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | res = hfsplus_delete_cat(cnid, dir, &dentry->d_name); |
| 351 | if (res) |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 352 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
Roman Zippel | af8c85b | 2006-01-18 17:43:10 -0800 | [diff] [blame] | 354 | if (inode->i_nlink > 0) |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 355 | drop_nlink(inode); |
Roman Zippel | 76b0c26 | 2008-04-09 17:44:07 +0200 | [diff] [blame] | 356 | if (inode->i_ino == cnid) |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 357 | clear_nlink(inode); |
Roman Zippel | 76b0c26 | 2008-04-09 17:44:07 +0200 | [diff] [blame] | 358 | if (!inode->i_nlink) { |
| 359 | if (inode->i_ino != cnid) { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 360 | sbi->file_count--; |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 361 | if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) { |
Roman Zippel | 76b0c26 | 2008-04-09 17:44:07 +0200 | [diff] [blame] | 362 | res = hfsplus_delete_cat(inode->i_ino, |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 363 | sbi->hidden_dir, |
Roman Zippel | 76b0c26 | 2008-04-09 17:44:07 +0200 | [diff] [blame] | 364 | NULL); |
| 365 | if (!res) |
| 366 | hfsplus_delete_inode(inode); |
| 367 | } else |
| 368 | inode->i_flags |= S_DEAD; |
| 369 | } else |
| 370 | hfsplus_delete_inode(inode); |
| 371 | } else |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 372 | sbi->file_count--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | inode->i_ctime = CURRENT_TIME_SEC; |
| 374 | mark_inode_dirty(inode); |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 375 | out: |
| 376 | mutex_unlock(&sbi->vh_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | return res; |
| 378 | } |
| 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry) |
| 381 | { |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 382 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb); |
| 383 | struct inode *inode = dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | int res; |
| 385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | if (inode->i_size != 2) |
| 387 | return -ENOTEMPTY; |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 388 | |
| 389 | mutex_lock(&sbi->vh_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name); |
| 391 | if (res) |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 392 | goto out; |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 393 | clear_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | inode->i_ctime = CURRENT_TIME_SEC; |
| 395 | hfsplus_delete_inode(inode); |
| 396 | mark_inode_dirty(inode); |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 397 | out: |
| 398 | mutex_unlock(&sbi->vh_mutex); |
| 399 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static int hfsplus_symlink(struct inode *dir, struct dentry *dentry, |
| 403 | const char *symname) |
| 404 | { |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 405 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | struct inode *inode; |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 407 | int res = -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 409 | mutex_lock(&sbi->vh_mutex); |
Christoph Hellwig | f17c89b | 2010-10-01 05:43:54 +0200 | [diff] [blame] | 410 | inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | if (!inode) |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 412 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
| 414 | res = page_symlink(inode, symname, strlen(symname) + 1); |
Christoph Hellwig | f17c89b | 2010-10-01 05:43:54 +0200 | [diff] [blame] | 415 | if (res) |
| 416 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode); |
Christoph Hellwig | f17c89b | 2010-10-01 05:43:54 +0200 | [diff] [blame] | 419 | if (res) |
| 420 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 422 | res = hfsplus_init_inode_security(inode, dir, &dentry->d_name); |
| 423 | if (res == -EOPNOTSUPP) |
| 424 | res = 0; /* Operation is not supported. */ |
| 425 | else if (res) { |
| 426 | /* Try to delete anyway without error analysis. */ |
| 427 | hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name); |
| 428 | goto out_err; |
| 429 | } |
| 430 | |
Christoph Hellwig | f17c89b | 2010-10-01 05:43:54 +0200 | [diff] [blame] | 431 | hfsplus_instantiate(dentry, inode, inode->i_ino); |
| 432 | mark_inode_dirty(inode); |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 433 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Christoph Hellwig | f17c89b | 2010-10-01 05:43:54 +0200 | [diff] [blame] | 435 | out_err: |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 436 | clear_nlink(inode); |
Christoph Hellwig | f17c89b | 2010-10-01 05:43:54 +0200 | [diff] [blame] | 437 | hfsplus_delete_inode(inode); |
| 438 | iput(inode); |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 439 | out: |
| 440 | mutex_unlock(&sbi->vh_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | return res; |
| 442 | } |
| 443 | |
| 444 | static int hfsplus_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 445 | umode_t mode, dev_t rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 447 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | struct inode *inode; |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 449 | int res = -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 451 | mutex_lock(&sbi->vh_mutex); |
Christoph Hellwig | 30d3abb | 2010-10-01 05:43:50 +0200 | [diff] [blame] | 452 | inode = hfsplus_new_inode(dir->i_sb, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | if (!inode) |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 454 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
Christoph Hellwig | 90e6169 | 2010-10-14 09:54:39 -0400 | [diff] [blame] | 456 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) |
| 457 | init_special_inode(inode, mode, rdev); |
| 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode); |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 460 | if (res) |
| 461 | goto failed_mknod; |
| 462 | |
| 463 | res = hfsplus_init_inode_security(inode, dir, &dentry->d_name); |
| 464 | if (res == -EOPNOTSUPP) |
| 465 | res = 0; /* Operation is not supported. */ |
| 466 | else if (res) { |
| 467 | /* Try to delete anyway without error analysis. */ |
| 468 | hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name); |
| 469 | goto failed_mknod; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } |
Christoph Hellwig | 30d3abb | 2010-10-01 05:43:50 +0200 | [diff] [blame] | 471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | hfsplus_instantiate(dentry, inode, inode->i_ino); |
| 473 | mark_inode_dirty(inode); |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 474 | goto out; |
| 475 | |
| 476 | failed_mknod: |
| 477 | clear_nlink(inode); |
| 478 | hfsplus_delete_inode(inode); |
| 479 | iput(inode); |
Christoph Hellwig | 7ac9fb9 | 2010-10-01 05:45:08 +0200 | [diff] [blame] | 480 | out: |
| 481 | mutex_unlock(&sbi->vh_mutex); |
| 482 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 485 | static int hfsplus_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 486 | bool excl) |
Christoph Hellwig | 30d3abb | 2010-10-01 05:43:50 +0200 | [diff] [blame] | 487 | { |
| 488 | return hfsplus_mknod(dir, dentry, mode, 0); |
| 489 | } |
| 490 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 491 | static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Christoph Hellwig | 30d3abb | 2010-10-01 05:43:50 +0200 | [diff] [blame] | 492 | { |
| 493 | return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0); |
| 494 | } |
| 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 497 | struct inode *new_dir, struct dentry *new_dentry) |
| 498 | { |
| 499 | int res; |
| 500 | |
| 501 | /* Unlink destination if it already exists */ |
| 502 | if (new_dentry->d_inode) { |
Sage Weil | e391178 | 2011-05-27 13:42:06 -0700 | [diff] [blame] | 503 | if (S_ISDIR(new_dentry->d_inode->i_mode)) |
Christoph Hellwig | 40de9a7 | 2010-10-01 09:12:08 +0200 | [diff] [blame] | 504 | res = hfsplus_rmdir(new_dir, new_dentry); |
Sage Weil | e391178 | 2011-05-27 13:42:06 -0700 | [diff] [blame] | 505 | else |
Christoph Hellwig | 40de9a7 | 2010-10-01 09:12:08 +0200 | [diff] [blame] | 506 | res = hfsplus_unlink(new_dir, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | if (res) |
| 508 | return res; |
| 509 | } |
| 510 | |
| 511 | res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata, |
| 512 | old_dir, &old_dentry->d_name, |
| 513 | new_dir, &new_dentry->d_name); |
| 514 | if (!res) |
| 515 | new_dentry->d_fsdata = old_dentry->d_fsdata; |
| 516 | return res; |
| 517 | } |
| 518 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 519 | const struct inode_operations hfsplus_dir_inode_operations = { |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 520 | .lookup = hfsplus_lookup, |
| 521 | .create = hfsplus_create, |
| 522 | .link = hfsplus_link, |
| 523 | .unlink = hfsplus_unlink, |
| 524 | .mkdir = hfsplus_mkdir, |
| 525 | .rmdir = hfsplus_rmdir, |
| 526 | .symlink = hfsplus_symlink, |
| 527 | .mknod = hfsplus_mknod, |
| 528 | .rename = hfsplus_rename, |
| 529 | .setxattr = generic_setxattr, |
| 530 | .getxattr = generic_getxattr, |
| 531 | .listxattr = hfsplus_listxattr, |
| 532 | .removexattr = hfsplus_removexattr, |
Vyacheslav Dubeyko | b4c1107 | 2013-09-11 14:24:30 -0700 | [diff] [blame] | 533 | #ifdef CONFIG_HFSPLUS_FS_POSIX_ACL |
| 534 | .get_acl = hfsplus_get_posix_acl, |
| 535 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | }; |
| 537 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 538 | const struct file_operations hfsplus_dir_operations = { |
Christoph Hellwig | eb29d66 | 2010-11-23 14:38:10 +0100 | [diff] [blame] | 539 | .fsync = hfsplus_file_fsync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | .read = generic_read_dir, |
Al Viro | e72514e | 2013-05-22 14:59:39 -0400 | [diff] [blame] | 541 | .iterate = hfsplus_readdir, |
Arnd Bergmann | 7cc4bcc | 2010-04-27 16:24:20 +0200 | [diff] [blame] | 542 | .unlocked_ioctl = hfsplus_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | .llseek = generic_file_llseek, |
| 544 | .release = hfsplus_dir_release, |
| 545 | }; |