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