11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/hfsplus/dir.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2001 51da177e4SLinus Torvalds * Brad Boyer (flar@allandria.com) 61da177e4SLinus Torvalds * (C) 2003 Ardis Technologies <roman@ardistech.com> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Handling of directories 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <linux/errno.h> 121da177e4SLinus Torvalds #include <linux/fs.h> 131da177e4SLinus Torvalds #include <linux/sched.h> 141da177e4SLinus Torvalds #include <linux/slab.h> 151da177e4SLinus Torvalds #include <linux/random.h> 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include "hfsplus_fs.h" 181da177e4SLinus Torvalds #include "hfsplus_raw.h" 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds static inline void hfsplus_instantiate(struct dentry *dentry, 211da177e4SLinus Torvalds struct inode *inode, u32 cnid) 221da177e4SLinus Torvalds { 231da177e4SLinus Torvalds dentry->d_fsdata = (void *)(unsigned long)cnid; 241da177e4SLinus Torvalds d_instantiate(dentry, inode); 251da177e4SLinus Torvalds } 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds /* Find the entry inside dir named dentry->d_name */ 281da177e4SLinus Torvalds static struct dentry *hfsplus_lookup(struct inode *dir, struct dentry *dentry, 291da177e4SLinus Torvalds struct nameidata *nd) 301da177e4SLinus Torvalds { 311da177e4SLinus Torvalds struct inode *inode = NULL; 321da177e4SLinus Torvalds struct hfs_find_data fd; 331da177e4SLinus Torvalds struct super_block *sb; 341da177e4SLinus Torvalds hfsplus_cat_entry entry; 351da177e4SLinus Torvalds int err; 361da177e4SLinus Torvalds u32 cnid, linkid = 0; 371da177e4SLinus Torvalds u16 type; 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds sb = dir->i_sb; 401da177e4SLinus Torvalds dentry->d_fsdata = NULL; 411da177e4SLinus Torvalds hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd); 421da177e4SLinus Torvalds hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, &dentry->d_name); 431da177e4SLinus Torvalds again: 441da177e4SLinus Torvalds err = hfs_brec_read(&fd, &entry, sizeof(entry)); 451da177e4SLinus Torvalds if (err) { 461da177e4SLinus Torvalds if (err == -ENOENT) { 471da177e4SLinus Torvalds hfs_find_exit(&fd); 481da177e4SLinus Torvalds /* No such entry */ 491da177e4SLinus Torvalds inode = NULL; 501da177e4SLinus Torvalds goto out; 511da177e4SLinus Torvalds } 521da177e4SLinus Torvalds goto fail; 531da177e4SLinus Torvalds } 541da177e4SLinus Torvalds type = be16_to_cpu(entry.type); 551da177e4SLinus Torvalds if (type == HFSPLUS_FOLDER) { 561da177e4SLinus Torvalds if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) { 571da177e4SLinus Torvalds err = -EIO; 581da177e4SLinus Torvalds goto fail; 591da177e4SLinus Torvalds } 601da177e4SLinus Torvalds cnid = be32_to_cpu(entry.folder.id); 611da177e4SLinus Torvalds dentry->d_fsdata = (void *)(unsigned long)cnid; 621da177e4SLinus Torvalds } else if (type == HFSPLUS_FILE) { 631da177e4SLinus Torvalds if (fd.entrylength < sizeof(struct hfsplus_cat_file)) { 641da177e4SLinus Torvalds err = -EIO; 651da177e4SLinus Torvalds goto fail; 661da177e4SLinus Torvalds } 671da177e4SLinus Torvalds cnid = be32_to_cpu(entry.file.id); 681da177e4SLinus Torvalds if (entry.file.user_info.fdType == cpu_to_be32(HFSP_HARDLINK_TYPE) && 69af8c85bbSRoman Zippel entry.file.user_info.fdCreator == cpu_to_be32(HFSP_HFSPLUS_CREATOR) && 70af8c85bbSRoman Zippel (entry.file.create_date == HFSPLUS_I(HFSPLUS_SB(sb).hidden_dir).create_date || 71af8c85bbSRoman Zippel entry.file.create_date == HFSPLUS_I(sb->s_root->d_inode).create_date) && 72af8c85bbSRoman Zippel HFSPLUS_SB(sb).hidden_dir) { 731da177e4SLinus Torvalds struct qstr str; 741da177e4SLinus Torvalds char name[32]; 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds if (dentry->d_fsdata) { 77af8c85bbSRoman Zippel /* 78af8c85bbSRoman Zippel * We found a link pointing to another link, 79af8c85bbSRoman Zippel * so ignore it and treat it as regular file. 80af8c85bbSRoman Zippel */ 81af8c85bbSRoman Zippel cnid = (unsigned long)dentry->d_fsdata; 82af8c85bbSRoman Zippel linkid = 0; 83af8c85bbSRoman Zippel } else { 841da177e4SLinus Torvalds dentry->d_fsdata = (void *)(unsigned long)cnid; 851da177e4SLinus Torvalds linkid = be32_to_cpu(entry.file.permissions.dev); 861da177e4SLinus Torvalds str.len = sprintf(name, "iNode%d", linkid); 871da177e4SLinus Torvalds str.name = name; 881da177e4SLinus Torvalds hfsplus_cat_build_key(sb, fd.search_key, HFSPLUS_SB(sb).hidden_dir->i_ino, &str); 891da177e4SLinus Torvalds goto again; 90af8c85bbSRoman Zippel } 911da177e4SLinus Torvalds } else if (!dentry->d_fsdata) 921da177e4SLinus Torvalds dentry->d_fsdata = (void *)(unsigned long)cnid; 931da177e4SLinus Torvalds } else { 94634725a9SRoman Zippel printk(KERN_ERR "hfs: invalid catalog entry type in lookup\n"); 951da177e4SLinus Torvalds err = -EIO; 961da177e4SLinus Torvalds goto fail; 971da177e4SLinus Torvalds } 981da177e4SLinus Torvalds hfs_find_exit(&fd); 991da177e4SLinus Torvalds inode = iget(dir->i_sb, cnid); 1001da177e4SLinus Torvalds if (!inode) 1011da177e4SLinus Torvalds return ERR_PTR(-EACCES); 1021da177e4SLinus Torvalds if (S_ISREG(inode->i_mode)) 1031da177e4SLinus Torvalds HFSPLUS_I(inode).dev = linkid; 1041da177e4SLinus Torvalds out: 1051da177e4SLinus Torvalds d_add(dentry, inode); 1061da177e4SLinus Torvalds return NULL; 1071da177e4SLinus Torvalds fail: 1081da177e4SLinus Torvalds hfs_find_exit(&fd); 1091da177e4SLinus Torvalds return ERR_PTR(err); 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds static int hfsplus_readdir(struct file *filp, void *dirent, filldir_t filldir) 1131da177e4SLinus Torvalds { 114f44ea031SJosef Sipek struct inode *inode = filp->f_path.dentry->d_inode; 1151da177e4SLinus Torvalds struct super_block *sb = inode->i_sb; 1161da177e4SLinus Torvalds int len, err; 1171da177e4SLinus Torvalds char strbuf[HFSPLUS_MAX_STRLEN + 1]; 1181da177e4SLinus Torvalds hfsplus_cat_entry entry; 1191da177e4SLinus Torvalds struct hfs_find_data fd; 1201da177e4SLinus Torvalds struct hfsplus_readdir_data *rd; 1211da177e4SLinus Torvalds u16 type; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds if (filp->f_pos >= inode->i_size) 1241da177e4SLinus Torvalds return 0; 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd); 1271da177e4SLinus Torvalds hfsplus_cat_build_key(sb, fd.search_key, inode->i_ino, NULL); 1281da177e4SLinus Torvalds err = hfs_brec_find(&fd); 1291da177e4SLinus Torvalds if (err) 1301da177e4SLinus Torvalds goto out; 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds switch ((u32)filp->f_pos) { 1331da177e4SLinus Torvalds case 0: 1341da177e4SLinus Torvalds /* This is completely artificial... */ 1351da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR)) 1361da177e4SLinus Torvalds goto out; 1371da177e4SLinus Torvalds filp->f_pos++; 1381da177e4SLinus Torvalds /* fall through */ 1391da177e4SLinus Torvalds case 1: 1401da177e4SLinus Torvalds hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength); 1411da177e4SLinus Torvalds if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) { 142634725a9SRoman Zippel printk(KERN_ERR "hfs: bad catalog folder thread\n"); 1431da177e4SLinus Torvalds err = -EIO; 1441da177e4SLinus Torvalds goto out; 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) { 147634725a9SRoman Zippel printk(KERN_ERR "hfs: truncated catalog thread\n"); 1481da177e4SLinus Torvalds err = -EIO; 1491da177e4SLinus Torvalds goto out; 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, 1521da177e4SLinus Torvalds be32_to_cpu(entry.thread.parentID), DT_DIR)) 1531da177e4SLinus Torvalds goto out; 1541da177e4SLinus Torvalds filp->f_pos++; 1551da177e4SLinus Torvalds /* fall through */ 1561da177e4SLinus Torvalds default: 1571da177e4SLinus Torvalds if (filp->f_pos >= inode->i_size) 1581da177e4SLinus Torvalds goto out; 1591da177e4SLinus Torvalds err = hfs_brec_goto(&fd, filp->f_pos - 1); 1601da177e4SLinus Torvalds if (err) 1611da177e4SLinus Torvalds goto out; 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds for (;;) { 1651da177e4SLinus Torvalds if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) { 166634725a9SRoman Zippel printk(KERN_ERR "hfs: walked past end of dir\n"); 1671da177e4SLinus Torvalds err = -EIO; 1681da177e4SLinus Torvalds goto out; 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength); 1711da177e4SLinus Torvalds type = be16_to_cpu(entry.type); 1721da177e4SLinus Torvalds len = HFSPLUS_MAX_STRLEN; 1731da177e4SLinus Torvalds err = hfsplus_uni2asc(sb, &fd.key->cat.name, strbuf, &len); 1741da177e4SLinus Torvalds if (err) 1751da177e4SLinus Torvalds goto out; 1761da177e4SLinus Torvalds if (type == HFSPLUS_FOLDER) { 1771da177e4SLinus Torvalds if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) { 178634725a9SRoman Zippel printk(KERN_ERR "hfs: small dir entry\n"); 1791da177e4SLinus Torvalds err = -EIO; 1801da177e4SLinus Torvalds goto out; 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds if (HFSPLUS_SB(sb).hidden_dir && 1831da177e4SLinus Torvalds HFSPLUS_SB(sb).hidden_dir->i_ino == be32_to_cpu(entry.folder.id)) 1841da177e4SLinus Torvalds goto next; 1851da177e4SLinus Torvalds if (filldir(dirent, strbuf, len, filp->f_pos, 1861da177e4SLinus Torvalds be32_to_cpu(entry.folder.id), DT_DIR)) 1871da177e4SLinus Torvalds break; 1881da177e4SLinus Torvalds } else if (type == HFSPLUS_FILE) { 1891da177e4SLinus Torvalds if (fd.entrylength < sizeof(struct hfsplus_cat_file)) { 190634725a9SRoman Zippel printk(KERN_ERR "hfs: small file entry\n"); 1911da177e4SLinus Torvalds err = -EIO; 1921da177e4SLinus Torvalds goto out; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds if (filldir(dirent, strbuf, len, filp->f_pos, 1951da177e4SLinus Torvalds be32_to_cpu(entry.file.id), DT_REG)) 1961da177e4SLinus Torvalds break; 1971da177e4SLinus Torvalds } else { 198634725a9SRoman Zippel printk(KERN_ERR "hfs: bad catalog entry type\n"); 1991da177e4SLinus Torvalds err = -EIO; 2001da177e4SLinus Torvalds goto out; 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds next: 2031da177e4SLinus Torvalds filp->f_pos++; 2041da177e4SLinus Torvalds if (filp->f_pos >= inode->i_size) 2051da177e4SLinus Torvalds goto out; 2061da177e4SLinus Torvalds err = hfs_brec_goto(&fd, 1); 2071da177e4SLinus Torvalds if (err) 2081da177e4SLinus Torvalds goto out; 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds rd = filp->private_data; 2111da177e4SLinus Torvalds if (!rd) { 2121da177e4SLinus Torvalds rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL); 2131da177e4SLinus Torvalds if (!rd) { 2141da177e4SLinus Torvalds err = -ENOMEM; 2151da177e4SLinus Torvalds goto out; 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds filp->private_data = rd; 2181da177e4SLinus Torvalds rd->file = filp; 2191da177e4SLinus Torvalds list_add(&rd->list, &HFSPLUS_I(inode).open_dir_list); 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key)); 2221da177e4SLinus Torvalds out: 2231da177e4SLinus Torvalds hfs_find_exit(&fd); 2241da177e4SLinus Torvalds return err; 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds static int hfsplus_dir_release(struct inode *inode, struct file *file) 2281da177e4SLinus Torvalds { 2291da177e4SLinus Torvalds struct hfsplus_readdir_data *rd = file->private_data; 2301da177e4SLinus Torvalds if (rd) { 2311da177e4SLinus Torvalds list_del(&rd->list); 2321da177e4SLinus Torvalds kfree(rd); 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds return 0; 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds static int hfsplus_create(struct inode *dir, struct dentry *dentry, int mode, 2381da177e4SLinus Torvalds struct nameidata *nd) 2391da177e4SLinus Torvalds { 2401da177e4SLinus Torvalds struct inode *inode; 2411da177e4SLinus Torvalds int res; 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds inode = hfsplus_new_inode(dir->i_sb, mode); 2441da177e4SLinus Torvalds if (!inode) 2451da177e4SLinus Torvalds return -ENOSPC; 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode); 2481da177e4SLinus Torvalds if (res) { 2491da177e4SLinus Torvalds inode->i_nlink = 0; 2501da177e4SLinus Torvalds hfsplus_delete_inode(inode); 2511da177e4SLinus Torvalds iput(inode); 2521da177e4SLinus Torvalds return res; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds hfsplus_instantiate(dentry, inode, inode->i_ino); 2551da177e4SLinus Torvalds mark_inode_dirty(inode); 2561da177e4SLinus Torvalds return 0; 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir, 2601da177e4SLinus Torvalds struct dentry *dst_dentry) 2611da177e4SLinus Torvalds { 2621da177e4SLinus Torvalds struct super_block *sb = dst_dir->i_sb; 2631da177e4SLinus Torvalds struct inode *inode = src_dentry->d_inode; 2641da177e4SLinus Torvalds struct inode *src_dir = src_dentry->d_parent->d_inode; 2651da177e4SLinus Torvalds struct qstr str; 2661da177e4SLinus Torvalds char name[32]; 2671da177e4SLinus Torvalds u32 cnid, id; 2681da177e4SLinus Torvalds int res; 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds if (HFSPLUS_IS_RSRC(inode)) 2711da177e4SLinus Torvalds return -EPERM; 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) { 2741da177e4SLinus Torvalds for (;;) { 2751da177e4SLinus Torvalds get_random_bytes(&id, sizeof(cnid)); 2761da177e4SLinus Torvalds id &= 0x3fffffff; 2771da177e4SLinus Torvalds str.name = name; 2781da177e4SLinus Torvalds str.len = sprintf(name, "iNode%d", id); 2791da177e4SLinus Torvalds res = hfsplus_rename_cat(inode->i_ino, 2801da177e4SLinus Torvalds src_dir, &src_dentry->d_name, 2811da177e4SLinus Torvalds HFSPLUS_SB(sb).hidden_dir, &str); 2821da177e4SLinus Torvalds if (!res) 2831da177e4SLinus Torvalds break; 2841da177e4SLinus Torvalds if (res != -EEXIST) 2851da177e4SLinus Torvalds return res; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds HFSPLUS_I(inode).dev = id; 2881da177e4SLinus Torvalds cnid = HFSPLUS_SB(sb).next_cnid++; 2891da177e4SLinus Torvalds src_dentry->d_fsdata = (void *)(unsigned long)cnid; 2901da177e4SLinus Torvalds res = hfsplus_create_cat(cnid, src_dir, &src_dentry->d_name, inode); 2911da177e4SLinus Torvalds if (res) 2921da177e4SLinus Torvalds /* panic? */ 2931da177e4SLinus Torvalds return res; 2941da177e4SLinus Torvalds HFSPLUS_SB(sb).file_count++; 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds cnid = HFSPLUS_SB(sb).next_cnid++; 2971da177e4SLinus Torvalds res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode); 2981da177e4SLinus Torvalds if (res) 2991da177e4SLinus Torvalds return res; 3001da177e4SLinus Torvalds 301d8c76e6fSDave Hansen inc_nlink(inode); 3021da177e4SLinus Torvalds hfsplus_instantiate(dst_dentry, inode, cnid); 3031da177e4SLinus Torvalds atomic_inc(&inode->i_count); 3041da177e4SLinus Torvalds inode->i_ctime = CURRENT_TIME_SEC; 3051da177e4SLinus Torvalds mark_inode_dirty(inode); 3061da177e4SLinus Torvalds HFSPLUS_SB(sb).file_count++; 3071da177e4SLinus Torvalds sb->s_dirt = 1; 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds return 0; 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds static int hfsplus_unlink(struct inode *dir, struct dentry *dentry) 3131da177e4SLinus Torvalds { 3141da177e4SLinus Torvalds struct super_block *sb = dir->i_sb; 3151da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 3161da177e4SLinus Torvalds struct qstr str; 3171da177e4SLinus Torvalds char name[32]; 3181da177e4SLinus Torvalds u32 cnid; 3191da177e4SLinus Torvalds int res; 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds if (HFSPLUS_IS_RSRC(inode)) 3221da177e4SLinus Torvalds return -EPERM; 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds cnid = (u32)(unsigned long)dentry->d_fsdata; 3251da177e4SLinus Torvalds if (inode->i_ino == cnid && 3261da177e4SLinus Torvalds atomic_read(&HFSPLUS_I(inode).opencnt)) { 3271da177e4SLinus Torvalds str.name = name; 3281da177e4SLinus Torvalds str.len = sprintf(name, "temp%lu", inode->i_ino); 3291da177e4SLinus Torvalds res = hfsplus_rename_cat(inode->i_ino, 3301da177e4SLinus Torvalds dir, &dentry->d_name, 3311da177e4SLinus Torvalds HFSPLUS_SB(sb).hidden_dir, &str); 3321da177e4SLinus Torvalds if (!res) 3331da177e4SLinus Torvalds inode->i_flags |= S_DEAD; 3341da177e4SLinus Torvalds return res; 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds res = hfsplus_delete_cat(cnid, dir, &dentry->d_name); 3371da177e4SLinus Torvalds if (res) 3381da177e4SLinus Torvalds return res; 3391da177e4SLinus Torvalds 340af8c85bbSRoman Zippel if (inode->i_nlink > 0) 3419a53c3a7SDave Hansen drop_nlink(inode); 3421da177e4SLinus Torvalds hfsplus_delete_inode(inode); 3431da177e4SLinus Torvalds if (inode->i_ino != cnid && !inode->i_nlink) { 3441da177e4SLinus Torvalds if (!atomic_read(&HFSPLUS_I(inode).opencnt)) { 3451da177e4SLinus Torvalds res = hfsplus_delete_cat(inode->i_ino, HFSPLUS_SB(sb).hidden_dir, NULL); 3461da177e4SLinus Torvalds if (!res) 3471da177e4SLinus Torvalds hfsplus_delete_inode(inode); 3481da177e4SLinus Torvalds } else 3491da177e4SLinus Torvalds inode->i_flags |= S_DEAD; 350af8c85bbSRoman Zippel } else 351ce71ec36SDave Hansen clear_nlink(inode); 3521da177e4SLinus Torvalds inode->i_ctime = CURRENT_TIME_SEC; 3531da177e4SLinus Torvalds mark_inode_dirty(inode); 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds return res; 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, int mode) 3591da177e4SLinus Torvalds { 3601da177e4SLinus Torvalds struct inode *inode; 3611da177e4SLinus Torvalds int res; 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds inode = hfsplus_new_inode(dir->i_sb, S_IFDIR | mode); 3641da177e4SLinus Torvalds if (!inode) 3651da177e4SLinus Torvalds return -ENOSPC; 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode); 3681da177e4SLinus Torvalds if (res) { 3691da177e4SLinus Torvalds inode->i_nlink = 0; 3701da177e4SLinus Torvalds hfsplus_delete_inode(inode); 3711da177e4SLinus Torvalds iput(inode); 3721da177e4SLinus Torvalds return res; 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds hfsplus_instantiate(dentry, inode, inode->i_ino); 3751da177e4SLinus Torvalds mark_inode_dirty(inode); 3761da177e4SLinus Torvalds return 0; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry) 3801da177e4SLinus Torvalds { 3811da177e4SLinus Torvalds struct inode *inode; 3821da177e4SLinus Torvalds int res; 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds inode = dentry->d_inode; 3851da177e4SLinus Torvalds if (inode->i_size != 2) 3861da177e4SLinus Torvalds return -ENOTEMPTY; 3871da177e4SLinus Torvalds res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name); 3881da177e4SLinus Torvalds if (res) 3891da177e4SLinus Torvalds return res; 390ce71ec36SDave Hansen clear_nlink(inode); 3911da177e4SLinus Torvalds inode->i_ctime = CURRENT_TIME_SEC; 3921da177e4SLinus Torvalds hfsplus_delete_inode(inode); 3931da177e4SLinus Torvalds mark_inode_dirty(inode); 3941da177e4SLinus Torvalds return 0; 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds static int hfsplus_symlink(struct inode *dir, struct dentry *dentry, 3981da177e4SLinus Torvalds const char *symname) 3991da177e4SLinus Torvalds { 4001da177e4SLinus Torvalds struct super_block *sb; 4011da177e4SLinus Torvalds struct inode *inode; 4021da177e4SLinus Torvalds int res; 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds sb = dir->i_sb; 4051da177e4SLinus Torvalds inode = hfsplus_new_inode(sb, S_IFLNK | S_IRWXUGO); 4061da177e4SLinus Torvalds if (!inode) 4071da177e4SLinus Torvalds return -ENOSPC; 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds res = page_symlink(inode, symname, strlen(symname) + 1); 4101da177e4SLinus Torvalds if (res) { 4111da177e4SLinus Torvalds inode->i_nlink = 0; 4121da177e4SLinus Torvalds hfsplus_delete_inode(inode); 4131da177e4SLinus Torvalds iput(inode); 4141da177e4SLinus Torvalds return res; 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds mark_inode_dirty(inode); 4181da177e4SLinus Torvalds res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode); 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds if (!res) { 4211da177e4SLinus Torvalds hfsplus_instantiate(dentry, inode, inode->i_ino); 4221da177e4SLinus Torvalds mark_inode_dirty(inode); 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds return res; 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds static int hfsplus_mknod(struct inode *dir, struct dentry *dentry, 4291da177e4SLinus Torvalds int mode, dev_t rdev) 4301da177e4SLinus Torvalds { 4311da177e4SLinus Torvalds struct super_block *sb; 4321da177e4SLinus Torvalds struct inode *inode; 4331da177e4SLinus Torvalds int res; 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds sb = dir->i_sb; 4361da177e4SLinus Torvalds inode = hfsplus_new_inode(sb, mode); 4371da177e4SLinus Torvalds if (!inode) 4381da177e4SLinus Torvalds return -ENOSPC; 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode); 4411da177e4SLinus Torvalds if (res) { 4421da177e4SLinus Torvalds inode->i_nlink = 0; 4431da177e4SLinus Torvalds hfsplus_delete_inode(inode); 4441da177e4SLinus Torvalds iput(inode); 4451da177e4SLinus Torvalds return res; 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds init_special_inode(inode, mode, rdev); 4481da177e4SLinus Torvalds hfsplus_instantiate(dentry, inode, inode->i_ino); 4491da177e4SLinus Torvalds mark_inode_dirty(inode); 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds return 0; 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry, 4551da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 4561da177e4SLinus Torvalds { 4571da177e4SLinus Torvalds int res; 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds /* Unlink destination if it already exists */ 4601da177e4SLinus Torvalds if (new_dentry->d_inode) { 4611da177e4SLinus Torvalds res = hfsplus_unlink(new_dir, new_dentry); 4621da177e4SLinus Torvalds if (res) 4631da177e4SLinus Torvalds return res; 4641da177e4SLinus Torvalds } 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata, 4671da177e4SLinus Torvalds old_dir, &old_dentry->d_name, 4681da177e4SLinus Torvalds new_dir, &new_dentry->d_name); 4691da177e4SLinus Torvalds if (!res) 4701da177e4SLinus Torvalds new_dentry->d_fsdata = old_dentry->d_fsdata; 4711da177e4SLinus Torvalds return res; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 474*92e1d5beSArjan van de Ven const struct inode_operations hfsplus_dir_inode_operations = { 4751da177e4SLinus Torvalds .lookup = hfsplus_lookup, 4761da177e4SLinus Torvalds .create = hfsplus_create, 4771da177e4SLinus Torvalds .link = hfsplus_link, 4781da177e4SLinus Torvalds .unlink = hfsplus_unlink, 4791da177e4SLinus Torvalds .mkdir = hfsplus_mkdir, 4801da177e4SLinus Torvalds .rmdir = hfsplus_rmdir, 4811da177e4SLinus Torvalds .symlink = hfsplus_symlink, 4821da177e4SLinus Torvalds .mknod = hfsplus_mknod, 4831da177e4SLinus Torvalds .rename = hfsplus_rename, 4841da177e4SLinus Torvalds }; 4851da177e4SLinus Torvalds 4864b6f5d20SArjan van de Ven const struct file_operations hfsplus_dir_operations = { 4871da177e4SLinus Torvalds .read = generic_read_dir, 4881da177e4SLinus Torvalds .readdir = hfsplus_readdir, 4891da177e4SLinus Torvalds .ioctl = hfsplus_ioctl, 4901da177e4SLinus Torvalds .llseek = generic_file_llseek, 4911da177e4SLinus Torvalds .release = hfsplus_dir_release, 4921da177e4SLinus Torvalds }; 493