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