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