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