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