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