11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/hpfs/dir.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * directory VFS functions 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 95a0e3ad6STejun Heo #include <linux/slab.h> 101da177e4SLinus Torvalds #include "hpfs_fn.h" 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds static int hpfs_dir_release(struct inode *inode, struct file *filp) 131da177e4SLinus Torvalds { 149a311b96SArnd Bergmann hpfs_lock(inode->i_sb); 151da177e4SLinus Torvalds hpfs_del_pos(inode, &filp->f_pos); 161da177e4SLinus Torvalds /*hpfs_write_if_changed(inode);*/ 179a311b96SArnd Bergmann hpfs_unlock(inode->i_sb); 181da177e4SLinus Torvalds return 0; 191da177e4SLinus Torvalds } 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds /* This is slow, but it's not used often */ 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds static loff_t hpfs_dir_lseek(struct file *filp, loff_t off, int whence) 241da177e4SLinus Torvalds { 251da177e4SLinus Torvalds loff_t new_off = off + (whence == 1 ? filp->f_pos : 0); 261da177e4SLinus Torvalds loff_t pos; 271da177e4SLinus Torvalds struct quad_buffer_head qbh; 28496ad9aaSAl Viro struct inode *i = file_inode(filp); 291da177e4SLinus Torvalds struct hpfs_inode_info *hpfs_inode = hpfs_i(i); 301da177e4SLinus Torvalds struct super_block *s = i->i_sb; 311da177e4SLinus Torvalds 3206222e49SJosef Bacik /* Somebody else will have to figure out what to do here */ 3306222e49SJosef Bacik if (whence == SEEK_DATA || whence == SEEK_HOLE) 3406222e49SJosef Bacik return -EINVAL; 3506222e49SJosef Bacik 3631abdab9SAl Viro mutex_lock(&i->i_mutex); 379a311b96SArnd Bergmann hpfs_lock(s); 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds /*printk("dir lseek\n");*/ 401da177e4SLinus Torvalds if (new_off == 0 || new_off == 1 || new_off == 11 || new_off == 12 || new_off == 13) goto ok; 411da177e4SLinus Torvalds pos = ((loff_t) hpfs_de_as_down_as_possible(s, hpfs_inode->i_dno) << 4) + 1; 421da177e4SLinus Torvalds while (pos != new_off) { 431da177e4SLinus Torvalds if (map_pos_dirent(i, &pos, &qbh)) hpfs_brelse4(&qbh); 441da177e4SLinus Torvalds else goto fail; 451da177e4SLinus Torvalds if (pos == 12) goto fail; 461da177e4SLinus Torvalds } 4731abdab9SAl Viro hpfs_add_pos(i, &filp->f_pos); 481da177e4SLinus Torvalds ok: 4931abdab9SAl Viro filp->f_pos = new_off; 509a311b96SArnd Bergmann hpfs_unlock(s); 511b1dcc1bSJes Sorensen mutex_unlock(&i->i_mutex); 5231abdab9SAl Viro return new_off; 5331abdab9SAl Viro fail: 541da177e4SLinus Torvalds /*printk("illegal lseek: %016llx\n", new_off);*/ 559a311b96SArnd Bergmann hpfs_unlock(s); 5631abdab9SAl Viro mutex_unlock(&i->i_mutex); 571da177e4SLinus Torvalds return -ESPIPE; 581da177e4SLinus Torvalds } 591da177e4SLinus Torvalds 60*568f8f5eSAl Viro static int hpfs_readdir(struct file *file, struct dir_context *ctx) 611da177e4SLinus Torvalds { 62*568f8f5eSAl Viro struct inode *inode = file_inode(file); 631da177e4SLinus Torvalds struct hpfs_inode_info *hpfs_inode = hpfs_i(inode); 641da177e4SLinus Torvalds struct quad_buffer_head qbh; 651da177e4SLinus Torvalds struct hpfs_dirent *de; 661da177e4SLinus Torvalds int lc; 67*568f8f5eSAl Viro loff_t next_pos; 687e7742eeSAl Viro unsigned char *tempname; 691da177e4SLinus Torvalds int c1, c2 = 0; 701da177e4SLinus Torvalds int ret = 0; 711da177e4SLinus Torvalds 729a311b96SArnd Bergmann hpfs_lock(inode->i_sb); 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds if (hpfs_sb(inode->i_sb)->sb_chk) { 751da177e4SLinus Torvalds if (hpfs_chk_sectors(inode->i_sb, inode->i_ino, 1, "dir_fnode")) { 761da177e4SLinus Torvalds ret = -EFSERROR; 771da177e4SLinus Torvalds goto out; 781da177e4SLinus Torvalds } 791da177e4SLinus Torvalds if (hpfs_chk_sectors(inode->i_sb, hpfs_inode->i_dno, 4, "dir_dnode")) { 801da177e4SLinus Torvalds ret = -EFSERROR; 811da177e4SLinus Torvalds goto out; 821da177e4SLinus Torvalds } 831da177e4SLinus Torvalds } 841da177e4SLinus Torvalds if (hpfs_sb(inode->i_sb)->sb_chk >= 2) { 851da177e4SLinus Torvalds struct buffer_head *bh; 861da177e4SLinus Torvalds struct fnode *fno; 871da177e4SLinus Torvalds int e = 0; 881da177e4SLinus Torvalds if (!(fno = hpfs_map_fnode(inode->i_sb, inode->i_ino, &bh))) { 891da177e4SLinus Torvalds ret = -EIOERROR; 901da177e4SLinus Torvalds goto out; 911da177e4SLinus Torvalds } 92c4c99543SAl Viro if (!fnode_is_dir(fno)) { 931da177e4SLinus Torvalds e = 1; 9418debbbcSRandy Dunlap hpfs_error(inode->i_sb, "not a directory, fnode %08lx", 9518debbbcSRandy Dunlap (unsigned long)inode->i_ino); 961da177e4SLinus Torvalds } 970b69760bSMikulas Patocka if (hpfs_inode->i_dno != le32_to_cpu(fno->u.external[0].disk_secno)) { 981da177e4SLinus Torvalds e = 1; 990b69760bSMikulas Patocka hpfs_error(inode->i_sb, "corrupted inode: i_dno == %08x, fnode -> dnode == %08x", hpfs_inode->i_dno, le32_to_cpu(fno->u.external[0].disk_secno)); 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds brelse(bh); 1021da177e4SLinus Torvalds if (e) { 1031da177e4SLinus Torvalds ret = -EFSERROR; 1041da177e4SLinus Torvalds goto out; 1051da177e4SLinus Torvalds } 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds lc = hpfs_sb(inode->i_sb)->sb_lowercase; 108*568f8f5eSAl Viro if (ctx->pos == 12) { /* diff -r requires this (note, that diff -r */ 109*568f8f5eSAl Viro ctx->pos = 13; /* also fails on msdos filesystem in 2.0) */ 1101da177e4SLinus Torvalds goto out; 1111da177e4SLinus Torvalds } 112*568f8f5eSAl Viro if (ctx->pos == 13) { 1131da177e4SLinus Torvalds ret = -ENOENT; 1141da177e4SLinus Torvalds goto out; 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds while (1) { 1181da177e4SLinus Torvalds again: 1191da177e4SLinus Torvalds /* This won't work when cycle is longer than number of dirents 1201da177e4SLinus Torvalds accepted by filldir, but what can I do? 1211da177e4SLinus Torvalds maybe killall -9 ls helps */ 1221da177e4SLinus Torvalds if (hpfs_sb(inode->i_sb)->sb_chk) 123*568f8f5eSAl Viro if (hpfs_stop_cycles(inode->i_sb, ctx->pos, &c1, &c2, "hpfs_readdir")) { 1241da177e4SLinus Torvalds ret = -EFSERROR; 1251da177e4SLinus Torvalds goto out; 1261da177e4SLinus Torvalds } 127*568f8f5eSAl Viro if (ctx->pos == 12) 1281da177e4SLinus Torvalds goto out; 129*568f8f5eSAl Viro if (ctx->pos == 3 || ctx->pos == 4 || ctx->pos == 5) { 130*568f8f5eSAl Viro printk("HPFS: warning: pos==%d\n",(int)ctx->pos); 1311da177e4SLinus Torvalds goto out; 1321da177e4SLinus Torvalds } 133*568f8f5eSAl Viro if (ctx->pos == 0) { 134*568f8f5eSAl Viro if (!dir_emit_dot(file, ctx)) 1351da177e4SLinus Torvalds goto out; 136*568f8f5eSAl Viro ctx->pos = 11; 1371da177e4SLinus Torvalds } 138*568f8f5eSAl Viro if (ctx->pos == 11) { 139*568f8f5eSAl Viro if (!dir_emit(ctx, "..", 2, hpfs_inode->i_parent_dir, DT_DIR)) 1401da177e4SLinus Torvalds goto out; 141*568f8f5eSAl Viro ctx->pos = 1; 1421da177e4SLinus Torvalds } 143*568f8f5eSAl Viro if (ctx->pos == 1) { 144*568f8f5eSAl Viro ctx->pos = ((loff_t) hpfs_de_as_down_as_possible(inode->i_sb, hpfs_inode->i_dno) << 4) + 1; 145*568f8f5eSAl Viro hpfs_add_pos(inode, &file->f_pos); 146*568f8f5eSAl Viro file->f_version = inode->i_version; 1471da177e4SLinus Torvalds } 148*568f8f5eSAl Viro next_pos = ctx->pos; 149*568f8f5eSAl Viro if (!(de = map_pos_dirent(inode, &next_pos, &qbh))) { 150*568f8f5eSAl Viro ctx->pos = next_pos; 1511da177e4SLinus Torvalds ret = -EIOERROR; 1521da177e4SLinus Torvalds goto out; 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds if (de->first || de->last) { 1551da177e4SLinus Torvalds if (hpfs_sb(inode->i_sb)->sb_chk) { 15618debbbcSRandy Dunlap if (de->first && !de->last && (de->namelen != 2 15718debbbcSRandy Dunlap || de ->name[0] != 1 || de->name[1] != 1)) 158*568f8f5eSAl Viro hpfs_error(inode->i_sb, "hpfs_readdir: bad ^A^A entry; pos = %08lx", (unsigned long)ctx->pos); 15918debbbcSRandy Dunlap if (de->last && (de->namelen != 1 || de ->name[0] != 255)) 160*568f8f5eSAl Viro hpfs_error(inode->i_sb, "hpfs_readdir: bad \\377 entry; pos = %08lx", (unsigned long)ctx->pos); 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds hpfs_brelse4(&qbh); 163*568f8f5eSAl Viro ctx->pos = next_pos; 1641da177e4SLinus Torvalds goto again; 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds tempname = hpfs_translate_name(inode->i_sb, de->name, de->namelen, lc, de->not_8x3); 167*568f8f5eSAl Viro if (!dir_emit(ctx, tempname, de->namelen, le32_to_cpu(de->fnode), DT_UNKNOWN)) { 1687e7742eeSAl Viro if (tempname != de->name) kfree(tempname); 1691da177e4SLinus Torvalds hpfs_brelse4(&qbh); 1701da177e4SLinus Torvalds goto out; 1711da177e4SLinus Torvalds } 172*568f8f5eSAl Viro ctx->pos = next_pos; 1737e7742eeSAl Viro if (tempname != de->name) kfree(tempname); 1741da177e4SLinus Torvalds hpfs_brelse4(&qbh); 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds out: 1779a311b96SArnd Bergmann hpfs_unlock(inode->i_sb); 1781da177e4SLinus Torvalds return ret; 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds /* 1821da177e4SLinus Torvalds * lookup. Search the specified directory for the specified name, set 1831da177e4SLinus Torvalds * *result to the corresponding inode. 1841da177e4SLinus Torvalds * 1851da177e4SLinus Torvalds * lookup uses the inode number to tell read_inode whether it is reading 1861da177e4SLinus Torvalds * the inode of a directory or a file -- file ino's are odd, directory 1871da177e4SLinus Torvalds * ino's are even. read_inode avoids i/o for file inodes; everything 1881da177e4SLinus Torvalds * needed is up here in the directory. (And file fnodes are out in 1891da177e4SLinus Torvalds * the boondocks.) 1901da177e4SLinus Torvalds * 1911da177e4SLinus Torvalds * - M.P.: this is over, sometimes we've got to read file's fnode for eas 1921da177e4SLinus Torvalds * inode numbers are just fnode sector numbers; iget lock is used 1931da177e4SLinus Torvalds * to tell read_inode to read fnode or not. 1941da177e4SLinus Torvalds */ 1951da177e4SLinus Torvalds 19600cd8dd3SAl Viro struct dentry *hpfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 1971da177e4SLinus Torvalds { 1987e7742eeSAl Viro const unsigned char *name = dentry->d_name.name; 1991da177e4SLinus Torvalds unsigned len = dentry->d_name.len; 2001da177e4SLinus Torvalds struct quad_buffer_head qbh; 2011da177e4SLinus Torvalds struct hpfs_dirent *de; 2021da177e4SLinus Torvalds ino_t ino; 2031da177e4SLinus Torvalds int err; 2041da177e4SLinus Torvalds struct inode *result = NULL; 2051da177e4SLinus Torvalds struct hpfs_inode_info *hpfs_result; 2061da177e4SLinus Torvalds 2079a311b96SArnd Bergmann hpfs_lock(dir->i_sb); 2087e7742eeSAl Viro if ((err = hpfs_chk_name(name, &len))) { 2091da177e4SLinus Torvalds if (err == -ENAMETOOLONG) { 2109a311b96SArnd Bergmann hpfs_unlock(dir->i_sb); 2111da177e4SLinus Torvalds return ERR_PTR(-ENAMETOOLONG); 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds goto end_add; 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds /* 2171da177e4SLinus Torvalds * '.' and '..' will never be passed here. 2181da177e4SLinus Torvalds */ 2191da177e4SLinus Torvalds 2207e7742eeSAl Viro de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, NULL, &qbh); 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds /* 2231da177e4SLinus Torvalds * This is not really a bailout, just means file not found. 2241da177e4SLinus Torvalds */ 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds if (!de) goto end; 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds /* 2291da177e4SLinus Torvalds * Get inode number, what we're after. 2301da177e4SLinus Torvalds */ 2311da177e4SLinus Torvalds 2320b69760bSMikulas Patocka ino = le32_to_cpu(de->fnode); 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds /* 2351da177e4SLinus Torvalds * Go find or make an inode. 2361da177e4SLinus Torvalds */ 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds result = iget_locked(dir->i_sb, ino); 2391da177e4SLinus Torvalds if (!result) { 2401da177e4SLinus Torvalds hpfs_error(dir->i_sb, "hpfs_lookup: can't get inode"); 2411da177e4SLinus Torvalds goto bail1; 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds if (result->i_state & I_NEW) { 2441da177e4SLinus Torvalds hpfs_init_inode(result); 2451da177e4SLinus Torvalds if (de->directory) 2461da177e4SLinus Torvalds hpfs_read_inode(result); 2470b69760bSMikulas Patocka else if (le32_to_cpu(de->ea_size) && hpfs_sb(dir->i_sb)->sb_eas) 2481da177e4SLinus Torvalds hpfs_read_inode(result); 2491da177e4SLinus Torvalds else { 2501da177e4SLinus Torvalds result->i_mode |= S_IFREG; 2511da177e4SLinus Torvalds result->i_mode &= ~0111; 2521da177e4SLinus Torvalds result->i_op = &hpfs_file_iops; 2531da177e4SLinus Torvalds result->i_fop = &hpfs_file_ops; 254bfe86848SMiklos Szeredi set_nlink(result, 1); 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds unlock_new_inode(result); 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds hpfs_result = hpfs_i(result); 2591da177e4SLinus Torvalds if (!de->directory) hpfs_result->i_parent_dir = dir->i_ino; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds if (de->has_acl || de->has_xtd_perm) if (!(dir->i_sb->s_flags & MS_RDONLY)) { 2621da177e4SLinus Torvalds hpfs_error(result->i_sb, "ACLs or XPERM found. This is probably HPFS386. This driver doesn't support it now. Send me some info on these structures"); 2631da177e4SLinus Torvalds goto bail1; 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds /* 2671da177e4SLinus Torvalds * Fill in the info from the directory if this is a newly created 2681da177e4SLinus Torvalds * inode. 2691da177e4SLinus Torvalds */ 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds if (!result->i_ctime.tv_sec) { 2720b69760bSMikulas Patocka if (!(result->i_ctime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(de->creation_date)))) 2731da177e4SLinus Torvalds result->i_ctime.tv_sec = 1; 2741da177e4SLinus Torvalds result->i_ctime.tv_nsec = 0; 2750b69760bSMikulas Patocka result->i_mtime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(de->write_date)); 2761da177e4SLinus Torvalds result->i_mtime.tv_nsec = 0; 2770b69760bSMikulas Patocka result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(de->read_date)); 2781da177e4SLinus Torvalds result->i_atime.tv_nsec = 0; 2790b69760bSMikulas Patocka hpfs_result->i_ea_size = le32_to_cpu(de->ea_size); 2801da177e4SLinus Torvalds if (!hpfs_result->i_ea_mode && de->read_only) 2811da177e4SLinus Torvalds result->i_mode &= ~0222; 2821da177e4SLinus Torvalds if (!de->directory) { 2831da177e4SLinus Torvalds if (result->i_size == -1) { 2840b69760bSMikulas Patocka result->i_size = le32_to_cpu(de->file_size); 2851da177e4SLinus Torvalds result->i_data.a_ops = &hpfs_aops; 2861da177e4SLinus Torvalds hpfs_i(result)->mmu_private = result->i_size; 2871da177e4SLinus Torvalds /* 2881da177e4SLinus Torvalds * i_blocks should count the fnode and any anodes. 2891da177e4SLinus Torvalds * We count 1 for the fnode and don't bother about 2901da177e4SLinus Torvalds * anodes -- the disk heads are on the directory band 2911da177e4SLinus Torvalds * and we want them to stay there. 2921da177e4SLinus Torvalds */ 2931da177e4SLinus Torvalds result->i_blocks = 1 + ((result->i_size + 511) >> 9); 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds hpfs_brelse4(&qbh); 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds /* 3011da177e4SLinus Torvalds * Made it. 3021da177e4SLinus Torvalds */ 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds end: 3051da177e4SLinus Torvalds end_add: 3069a311b96SArnd Bergmann hpfs_unlock(dir->i_sb); 3071da177e4SLinus Torvalds d_add(dentry, result); 3081da177e4SLinus Torvalds return NULL; 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds /* 3111da177e4SLinus Torvalds * Didn't. 3121da177e4SLinus Torvalds */ 3131da177e4SLinus Torvalds bail1: 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds hpfs_brelse4(&qbh); 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds /*bail:*/ 3181da177e4SLinus Torvalds 3199a311b96SArnd Bergmann hpfs_unlock(dir->i_sb); 3201da177e4SLinus Torvalds return ERR_PTR(-ENOENT); 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 3234b6f5d20SArjan van de Ven const struct file_operations hpfs_dir_ops = 3241da177e4SLinus Torvalds { 3251da177e4SLinus Torvalds .llseek = hpfs_dir_lseek, 3261da177e4SLinus Torvalds .read = generic_read_dir, 327*568f8f5eSAl Viro .iterate = hpfs_readdir, 3281da177e4SLinus Torvalds .release = hpfs_dir_release, 3291da177e4SLinus Torvalds .fsync = hpfs_file_fsync, 3301da177e4SLinus Torvalds }; 331