1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/hpfs/dir.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * directory VFS functions 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds 105a0e3ad6STejun Heo #include <linux/slab.h> 111da177e4SLinus Torvalds #include "hpfs_fn.h" 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds static int hpfs_dir_release(struct inode *inode, struct file *filp) 141da177e4SLinus Torvalds { 159a311b96SArnd Bergmann hpfs_lock(inode->i_sb); 161da177e4SLinus Torvalds hpfs_del_pos(inode, &filp->f_pos); 171da177e4SLinus Torvalds /*hpfs_write_if_changed(inode);*/ 189a311b96SArnd Bergmann hpfs_unlock(inode->i_sb); 191da177e4SLinus Torvalds return 0; 201da177e4SLinus Torvalds } 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds /* This is slow, but it's not used often */ 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds static loff_t hpfs_dir_lseek(struct file *filp, loff_t off, int whence) 251da177e4SLinus Torvalds { 261da177e4SLinus Torvalds loff_t new_off = off + (whence == 1 ? filp->f_pos : 0); 271da177e4SLinus Torvalds loff_t pos; 281da177e4SLinus Torvalds struct quad_buffer_head qbh; 29496ad9aaSAl Viro struct inode *i = file_inode(filp); 301da177e4SLinus Torvalds struct hpfs_inode_info *hpfs_inode = hpfs_i(i); 311da177e4SLinus Torvalds struct super_block *s = i->i_sb; 321da177e4SLinus Torvalds 3306222e49SJosef Bacik /* Somebody else will have to figure out what to do here */ 3406222e49SJosef Bacik if (whence == SEEK_DATA || whence == SEEK_HOLE) 3506222e49SJosef Bacik return -EINVAL; 3606222e49SJosef Bacik 375955102cSAl Viro inode_lock(i); 389a311b96SArnd Bergmann hpfs_lock(s); 391da177e4SLinus Torvalds 40b7cb1ce2SFabian Frederick /*pr_info("dir lseek\n");*/ 411da177e4SLinus Torvalds if (new_off == 0 || new_off == 1 || new_off == 11 || new_off == 12 || new_off == 13) goto ok; 421da177e4SLinus Torvalds pos = ((loff_t) hpfs_de_as_down_as_possible(s, hpfs_inode->i_dno) << 4) + 1; 431da177e4SLinus Torvalds while (pos != new_off) { 441da177e4SLinus Torvalds if (map_pos_dirent(i, &pos, &qbh)) hpfs_brelse4(&qbh); 451da177e4SLinus Torvalds else goto fail; 461da177e4SLinus Torvalds if (pos == 12) goto fail; 471da177e4SLinus Torvalds } 48e82c3147SAl Viro if (unlikely(hpfs_add_pos(i, &filp->f_pos) < 0)) { 49e82c3147SAl Viro hpfs_unlock(s); 50e82c3147SAl Viro inode_unlock(i); 51e82c3147SAl Viro return -ENOMEM; 52e82c3147SAl Viro } 531da177e4SLinus Torvalds ok: 5431abdab9SAl Viro filp->f_pos = new_off; 559a311b96SArnd Bergmann hpfs_unlock(s); 565955102cSAl Viro inode_unlock(i); 5731abdab9SAl Viro return new_off; 5831abdab9SAl Viro fail: 59b7cb1ce2SFabian Frederick /*pr_warn("illegal lseek: %016llx\n", new_off);*/ 609a311b96SArnd Bergmann hpfs_unlock(s); 615955102cSAl Viro inode_unlock(i); 621da177e4SLinus Torvalds return -ESPIPE; 631da177e4SLinus Torvalds } 641da177e4SLinus Torvalds 65568f8f5eSAl Viro static int hpfs_readdir(struct file *file, struct dir_context *ctx) 661da177e4SLinus Torvalds { 67568f8f5eSAl Viro struct inode *inode = file_inode(file); 681da177e4SLinus Torvalds struct hpfs_inode_info *hpfs_inode = hpfs_i(inode); 691da177e4SLinus Torvalds struct quad_buffer_head qbh; 701da177e4SLinus Torvalds struct hpfs_dirent *de; 711da177e4SLinus Torvalds int lc; 72568f8f5eSAl Viro loff_t next_pos; 737e7742eeSAl Viro unsigned char *tempname; 741da177e4SLinus Torvalds int c1, c2 = 0; 751da177e4SLinus Torvalds int ret = 0; 761da177e4SLinus Torvalds 779a311b96SArnd Bergmann hpfs_lock(inode->i_sb); 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds if (hpfs_sb(inode->i_sb)->sb_chk) { 801da177e4SLinus Torvalds if (hpfs_chk_sectors(inode->i_sb, inode->i_ino, 1, "dir_fnode")) { 811da177e4SLinus Torvalds ret = -EFSERROR; 821da177e4SLinus Torvalds goto out; 831da177e4SLinus Torvalds } 841da177e4SLinus Torvalds if (hpfs_chk_sectors(inode->i_sb, hpfs_inode->i_dno, 4, "dir_dnode")) { 851da177e4SLinus Torvalds ret = -EFSERROR; 861da177e4SLinus Torvalds goto out; 871da177e4SLinus Torvalds } 881da177e4SLinus Torvalds } 891da177e4SLinus Torvalds if (hpfs_sb(inode->i_sb)->sb_chk >= 2) { 901da177e4SLinus Torvalds struct buffer_head *bh; 911da177e4SLinus Torvalds struct fnode *fno; 921da177e4SLinus Torvalds int e = 0; 931da177e4SLinus Torvalds if (!(fno = hpfs_map_fnode(inode->i_sb, inode->i_ino, &bh))) { 941da177e4SLinus Torvalds ret = -EIOERROR; 951da177e4SLinus Torvalds goto out; 961da177e4SLinus Torvalds } 97c4c99543SAl Viro if (!fnode_is_dir(fno)) { 981da177e4SLinus Torvalds e = 1; 9918debbbcSRandy Dunlap hpfs_error(inode->i_sb, "not a directory, fnode %08lx", 10018debbbcSRandy Dunlap (unsigned long)inode->i_ino); 1011da177e4SLinus Torvalds } 1020b69760bSMikulas Patocka if (hpfs_inode->i_dno != le32_to_cpu(fno->u.external[0].disk_secno)) { 1031da177e4SLinus Torvalds e = 1; 1040b69760bSMikulas 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)); 1051da177e4SLinus Torvalds } 1061da177e4SLinus Torvalds brelse(bh); 1071da177e4SLinus Torvalds if (e) { 1081da177e4SLinus Torvalds ret = -EFSERROR; 1091da177e4SLinus Torvalds goto out; 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds lc = hpfs_sb(inode->i_sb)->sb_lowercase; 113568f8f5eSAl Viro if (ctx->pos == 12) { /* diff -r requires this (note, that diff -r */ 114568f8f5eSAl Viro ctx->pos = 13; /* also fails on msdos filesystem in 2.0) */ 1151da177e4SLinus Torvalds goto out; 1161da177e4SLinus Torvalds } 117568f8f5eSAl Viro if (ctx->pos == 13) { 1181da177e4SLinus Torvalds ret = -ENOENT; 1191da177e4SLinus Torvalds goto out; 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds while (1) { 1231da177e4SLinus Torvalds again: 1241da177e4SLinus Torvalds /* This won't work when cycle is longer than number of dirents 1251da177e4SLinus Torvalds accepted by filldir, but what can I do? 1261da177e4SLinus Torvalds maybe killall -9 ls helps */ 1271da177e4SLinus Torvalds if (hpfs_sb(inode->i_sb)->sb_chk) 128568f8f5eSAl Viro if (hpfs_stop_cycles(inode->i_sb, ctx->pos, &c1, &c2, "hpfs_readdir")) { 1291da177e4SLinus Torvalds ret = -EFSERROR; 1301da177e4SLinus Torvalds goto out; 1311da177e4SLinus Torvalds } 132568f8f5eSAl Viro if (ctx->pos == 12) 1331da177e4SLinus Torvalds goto out; 134568f8f5eSAl Viro if (ctx->pos == 3 || ctx->pos == 4 || ctx->pos == 5) { 135a19189e5SFabian Frederick pr_err("pos==%d\n", (int)ctx->pos); 1361da177e4SLinus Torvalds goto out; 1371da177e4SLinus Torvalds } 138568f8f5eSAl Viro if (ctx->pos == 0) { 139568f8f5eSAl Viro if (!dir_emit_dot(file, ctx)) 1401da177e4SLinus Torvalds goto out; 141568f8f5eSAl Viro ctx->pos = 11; 1421da177e4SLinus Torvalds } 143568f8f5eSAl Viro if (ctx->pos == 11) { 144568f8f5eSAl Viro if (!dir_emit(ctx, "..", 2, hpfs_inode->i_parent_dir, DT_DIR)) 1451da177e4SLinus Torvalds goto out; 146568f8f5eSAl Viro ctx->pos = 1; 1471da177e4SLinus Torvalds } 148568f8f5eSAl Viro if (ctx->pos == 1) { 149e82c3147SAl Viro ret = hpfs_add_pos(inode, &file->f_pos); 150e82c3147SAl Viro if (unlikely(ret < 0)) 151e82c3147SAl Viro goto out; 152568f8f5eSAl Viro ctx->pos = ((loff_t) hpfs_de_as_down_as_possible(inode->i_sb, hpfs_inode->i_dno) << 4) + 1; 1531da177e4SLinus Torvalds } 154568f8f5eSAl Viro next_pos = ctx->pos; 155568f8f5eSAl Viro if (!(de = map_pos_dirent(inode, &next_pos, &qbh))) { 156568f8f5eSAl Viro ctx->pos = next_pos; 1571da177e4SLinus Torvalds ret = -EIOERROR; 1581da177e4SLinus Torvalds goto out; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds if (de->first || de->last) { 1611da177e4SLinus Torvalds if (hpfs_sb(inode->i_sb)->sb_chk) { 16218debbbcSRandy Dunlap if (de->first && !de->last && (de->namelen != 2 16318debbbcSRandy Dunlap || de ->name[0] != 1 || de->name[1] != 1)) 164568f8f5eSAl Viro hpfs_error(inode->i_sb, "hpfs_readdir: bad ^A^A entry; pos = %08lx", (unsigned long)ctx->pos); 16518debbbcSRandy Dunlap if (de->last && (de->namelen != 1 || de ->name[0] != 255)) 166568f8f5eSAl Viro hpfs_error(inode->i_sb, "hpfs_readdir: bad \\377 entry; pos = %08lx", (unsigned long)ctx->pos); 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds hpfs_brelse4(&qbh); 169568f8f5eSAl Viro ctx->pos = next_pos; 1701da177e4SLinus Torvalds goto again; 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds tempname = hpfs_translate_name(inode->i_sb, de->name, de->namelen, lc, de->not_8x3); 173568f8f5eSAl Viro if (!dir_emit(ctx, tempname, de->namelen, le32_to_cpu(de->fnode), DT_UNKNOWN)) { 1747e7742eeSAl Viro if (tempname != de->name) kfree(tempname); 1751da177e4SLinus Torvalds hpfs_brelse4(&qbh); 1761da177e4SLinus Torvalds goto out; 1771da177e4SLinus Torvalds } 178568f8f5eSAl Viro ctx->pos = next_pos; 1797e7742eeSAl Viro if (tempname != de->name) kfree(tempname); 1801da177e4SLinus Torvalds hpfs_brelse4(&qbh); 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds out: 1839a311b96SArnd Bergmann hpfs_unlock(inode->i_sb); 1841da177e4SLinus Torvalds return ret; 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds /* 1881da177e4SLinus Torvalds * lookup. Search the specified directory for the specified name, set 1891da177e4SLinus Torvalds * *result to the corresponding inode. 1901da177e4SLinus Torvalds * 1911da177e4SLinus Torvalds * lookup uses the inode number to tell read_inode whether it is reading 1921da177e4SLinus Torvalds * the inode of a directory or a file -- file ino's are odd, directory 1931da177e4SLinus Torvalds * ino's are even. read_inode avoids i/o for file inodes; everything 1941da177e4SLinus Torvalds * needed is up here in the directory. (And file fnodes are out in 1951da177e4SLinus Torvalds * the boondocks.) 1961da177e4SLinus Torvalds * 1971da177e4SLinus Torvalds * - M.P.: this is over, sometimes we've got to read file's fnode for eas 1981da177e4SLinus Torvalds * inode numbers are just fnode sector numbers; iget lock is used 1991da177e4SLinus Torvalds * to tell read_inode to read fnode or not. 2001da177e4SLinus Torvalds */ 2011da177e4SLinus Torvalds 20200cd8dd3SAl Viro struct dentry *hpfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 2031da177e4SLinus Torvalds { 2047e7742eeSAl Viro const unsigned char *name = dentry->d_name.name; 2051da177e4SLinus Torvalds unsigned len = dentry->d_name.len; 2061da177e4SLinus Torvalds struct quad_buffer_head qbh; 2071da177e4SLinus Torvalds struct hpfs_dirent *de; 2081da177e4SLinus Torvalds ino_t ino; 2091da177e4SLinus Torvalds int err; 2101da177e4SLinus Torvalds struct inode *result = NULL; 2111da177e4SLinus Torvalds struct hpfs_inode_info *hpfs_result; 2121da177e4SLinus Torvalds 2139a311b96SArnd Bergmann hpfs_lock(dir->i_sb); 2147e7742eeSAl Viro if ((err = hpfs_chk_name(name, &len))) { 2151da177e4SLinus Torvalds if (err == -ENAMETOOLONG) { 2169a311b96SArnd Bergmann hpfs_unlock(dir->i_sb); 2171da177e4SLinus Torvalds return ERR_PTR(-ENAMETOOLONG); 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds goto end_add; 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds /* 2231da177e4SLinus Torvalds * '.' and '..' will never be passed here. 2241da177e4SLinus Torvalds */ 2251da177e4SLinus Torvalds 2267e7742eeSAl Viro de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, NULL, &qbh); 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds /* 2291da177e4SLinus Torvalds * This is not really a bailout, just means file not found. 2301da177e4SLinus Torvalds */ 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds if (!de) goto end; 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds /* 2351da177e4SLinus Torvalds * Get inode number, what we're after. 2361da177e4SLinus Torvalds */ 2371da177e4SLinus Torvalds 2380b69760bSMikulas Patocka ino = le32_to_cpu(de->fnode); 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds /* 2411da177e4SLinus Torvalds * Go find or make an inode. 2421da177e4SLinus Torvalds */ 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds result = iget_locked(dir->i_sb, ino); 2451da177e4SLinus Torvalds if (!result) { 2461da177e4SLinus Torvalds hpfs_error(dir->i_sb, "hpfs_lookup: can't get inode"); 247e876c445SAl Viro result = ERR_PTR(-ENOMEM); 2481da177e4SLinus Torvalds goto bail1; 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds if (result->i_state & I_NEW) { 2511da177e4SLinus Torvalds hpfs_init_inode(result); 2521da177e4SLinus Torvalds if (de->directory) 2531da177e4SLinus Torvalds hpfs_read_inode(result); 2540b69760bSMikulas Patocka else if (le32_to_cpu(de->ea_size) && hpfs_sb(dir->i_sb)->sb_eas) 2551da177e4SLinus Torvalds hpfs_read_inode(result); 2561da177e4SLinus Torvalds else { 2571da177e4SLinus Torvalds result->i_mode |= S_IFREG; 2581da177e4SLinus Torvalds result->i_mode &= ~0111; 2591da177e4SLinus Torvalds result->i_op = &hpfs_file_iops; 2601da177e4SLinus Torvalds result->i_fop = &hpfs_file_ops; 261bfe86848SMiklos Szeredi set_nlink(result, 1); 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds unlock_new_inode(result); 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds hpfs_result = hpfs_i(result); 2661da177e4SLinus Torvalds if (!de->directory) hpfs_result->i_parent_dir = dir->i_ino; 2671da177e4SLinus Torvalds 268bc98a42cSDavid Howells if (de->has_acl || de->has_xtd_perm) if (!sb_rdonly(dir->i_sb)) { 2691da177e4SLinus 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"); 270e876c445SAl Viro iput(result); 271e876c445SAl Viro result = ERR_PTR(-EINVAL); 2721da177e4SLinus Torvalds goto bail1; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds /* 2761da177e4SLinus Torvalds * Fill in the info from the directory if this is a newly created 2771da177e4SLinus Torvalds * inode. 2781da177e4SLinus Torvalds */ 2791da177e4SLinus Torvalds 280*3200d07dSJeff Layton if (!inode_get_ctime(result).tv_sec) { 281*3200d07dSJeff Layton time64_t csec = local_to_gmt(dir->i_sb, le32_to_cpu(de->creation_date)); 282*3200d07dSJeff Layton 283*3200d07dSJeff Layton inode_set_ctime(result, csec ? csec : 1, 0); 2840b69760bSMikulas Patocka result->i_mtime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(de->write_date)); 2851da177e4SLinus Torvalds result->i_mtime.tv_nsec = 0; 2860b69760bSMikulas Patocka result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(de->read_date)); 2871da177e4SLinus Torvalds result->i_atime.tv_nsec = 0; 2880b69760bSMikulas Patocka hpfs_result->i_ea_size = le32_to_cpu(de->ea_size); 2891da177e4SLinus Torvalds if (!hpfs_result->i_ea_mode && de->read_only) 2901da177e4SLinus Torvalds result->i_mode &= ~0222; 2911da177e4SLinus Torvalds if (!de->directory) { 2921da177e4SLinus Torvalds if (result->i_size == -1) { 2930b69760bSMikulas Patocka result->i_size = le32_to_cpu(de->file_size); 2941da177e4SLinus Torvalds result->i_data.a_ops = &hpfs_aops; 2951da177e4SLinus Torvalds hpfs_i(result)->mmu_private = result->i_size; 2961da177e4SLinus Torvalds /* 2971da177e4SLinus Torvalds * i_blocks should count the fnode and any anodes. 2981da177e4SLinus Torvalds * We count 1 for the fnode and don't bother about 2991da177e4SLinus Torvalds * anodes -- the disk heads are on the directory band 3001da177e4SLinus Torvalds * and we want them to stay there. 3011da177e4SLinus Torvalds */ 3021da177e4SLinus Torvalds result->i_blocks = 1 + ((result->i_size + 511) >> 9); 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 307e876c445SAl Viro bail1: 3081da177e4SLinus Torvalds hpfs_brelse4(&qbh); 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds /* 3111da177e4SLinus Torvalds * Made it. 3121da177e4SLinus Torvalds */ 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds end: 3151da177e4SLinus Torvalds end_add: 3169a311b96SArnd Bergmann hpfs_unlock(dir->i_sb); 317e876c445SAl Viro return d_splice_alias(result, dentry); 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 3204b6f5d20SArjan van de Ven const struct file_operations hpfs_dir_ops = 3211da177e4SLinus Torvalds { 3221da177e4SLinus Torvalds .llseek = hpfs_dir_lseek, 3231da177e4SLinus Torvalds .read = generic_read_dir, 3247d674b31SAl Viro .iterate_shared = hpfs_readdir, 3251da177e4SLinus Torvalds .release = hpfs_dir_release, 3261da177e4SLinus Torvalds .fsync = hpfs_file_fsync, 327a27b5b97SMikulas Patocka .unlocked_ioctl = hpfs_ioctl, 328314999dcSArnd Bergmann .compat_ioctl = compat_ptr_ioctl, 3291da177e4SLinus Torvalds }; 330