xref: /linux/fs/hpfs/dir.c (revision 18debbbcce1306f0bbb1c71cf587fd90413acab6)
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 
91da177e4SLinus Torvalds #include "hpfs_fn.h"
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds static int hpfs_dir_release(struct inode *inode, struct file *filp)
121da177e4SLinus Torvalds {
131da177e4SLinus Torvalds 	lock_kernel();
141da177e4SLinus Torvalds 	hpfs_del_pos(inode, &filp->f_pos);
151da177e4SLinus Torvalds 	/*hpfs_write_if_changed(inode);*/
161da177e4SLinus Torvalds 	unlock_kernel();
171da177e4SLinus Torvalds 	return 0;
181da177e4SLinus Torvalds }
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds /* This is slow, but it's not used often */
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds static loff_t hpfs_dir_lseek(struct file *filp, loff_t off, int whence)
231da177e4SLinus Torvalds {
241da177e4SLinus Torvalds 	loff_t new_off = off + (whence == 1 ? filp->f_pos : 0);
251da177e4SLinus Torvalds 	loff_t pos;
261da177e4SLinus Torvalds 	struct quad_buffer_head qbh;
271da177e4SLinus Torvalds 	struct inode *i = filp->f_dentry->d_inode;
281da177e4SLinus Torvalds 	struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
291da177e4SLinus Torvalds 	struct super_block *s = i->i_sb;
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds 	lock_kernel();
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds 	/*printk("dir lseek\n");*/
341da177e4SLinus Torvalds 	if (new_off == 0 || new_off == 1 || new_off == 11 || new_off == 12 || new_off == 13) goto ok;
351b1dcc1bSJes Sorensen 	mutex_lock(&i->i_mutex);
361da177e4SLinus Torvalds 	pos = ((loff_t) hpfs_de_as_down_as_possible(s, hpfs_inode->i_dno) << 4) + 1;
371da177e4SLinus Torvalds 	while (pos != new_off) {
381da177e4SLinus Torvalds 		if (map_pos_dirent(i, &pos, &qbh)) hpfs_brelse4(&qbh);
391da177e4SLinus Torvalds 		else goto fail;
401da177e4SLinus Torvalds 		if (pos == 12) goto fail;
411da177e4SLinus Torvalds 	}
421b1dcc1bSJes Sorensen 	mutex_unlock(&i->i_mutex);
431da177e4SLinus Torvalds ok:
441da177e4SLinus Torvalds 	unlock_kernel();
451da177e4SLinus Torvalds 	return filp->f_pos = new_off;
461da177e4SLinus Torvalds fail:
471b1dcc1bSJes Sorensen 	mutex_unlock(&i->i_mutex);
481da177e4SLinus Torvalds 	/*printk("illegal lseek: %016llx\n", new_off);*/
491da177e4SLinus Torvalds 	unlock_kernel();
501da177e4SLinus Torvalds 	return -ESPIPE;
511da177e4SLinus Torvalds }
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds static int hpfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
541da177e4SLinus Torvalds {
551da177e4SLinus Torvalds 	struct inode *inode = filp->f_dentry->d_inode;
561da177e4SLinus Torvalds 	struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
571da177e4SLinus Torvalds 	struct quad_buffer_head qbh;
581da177e4SLinus Torvalds 	struct hpfs_dirent *de;
591da177e4SLinus Torvalds 	int lc;
601da177e4SLinus Torvalds 	long old_pos;
611da177e4SLinus Torvalds 	char *tempname;
621da177e4SLinus Torvalds 	int c1, c2 = 0;
631da177e4SLinus Torvalds 	int ret = 0;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	lock_kernel();
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 	if (hpfs_sb(inode->i_sb)->sb_chk) {
681da177e4SLinus Torvalds 		if (hpfs_chk_sectors(inode->i_sb, inode->i_ino, 1, "dir_fnode")) {
691da177e4SLinus Torvalds 			ret = -EFSERROR;
701da177e4SLinus Torvalds 			goto out;
711da177e4SLinus Torvalds 		}
721da177e4SLinus Torvalds 		if (hpfs_chk_sectors(inode->i_sb, hpfs_inode->i_dno, 4, "dir_dnode")) {
731da177e4SLinus Torvalds 			ret = -EFSERROR;
741da177e4SLinus Torvalds 			goto out;
751da177e4SLinus Torvalds 		}
761da177e4SLinus Torvalds 	}
771da177e4SLinus Torvalds 	if (hpfs_sb(inode->i_sb)->sb_chk >= 2) {
781da177e4SLinus Torvalds 		struct buffer_head *bh;
791da177e4SLinus Torvalds 		struct fnode *fno;
801da177e4SLinus Torvalds 		int e = 0;
811da177e4SLinus Torvalds 		if (!(fno = hpfs_map_fnode(inode->i_sb, inode->i_ino, &bh))) {
821da177e4SLinus Torvalds 			ret = -EIOERROR;
831da177e4SLinus Torvalds 			goto out;
841da177e4SLinus Torvalds 		}
851da177e4SLinus Torvalds 		if (!fno->dirflag) {
861da177e4SLinus Torvalds 			e = 1;
87*18debbbcSRandy Dunlap 			hpfs_error(inode->i_sb, "not a directory, fnode %08lx",
88*18debbbcSRandy Dunlap 					(unsigned long)inode->i_ino);
891da177e4SLinus Torvalds 		}
901da177e4SLinus Torvalds 		if (hpfs_inode->i_dno != fno->u.external[0].disk_secno) {
911da177e4SLinus Torvalds 			e = 1;
921da177e4SLinus Torvalds 			hpfs_error(inode->i_sb, "corrupted inode: i_dno == %08x, fnode -> dnode == %08x", hpfs_inode->i_dno, fno->u.external[0].disk_secno);
931da177e4SLinus Torvalds 		}
941da177e4SLinus Torvalds 		brelse(bh);
951da177e4SLinus Torvalds 		if (e) {
961da177e4SLinus Torvalds 			ret = -EFSERROR;
971da177e4SLinus Torvalds 			goto out;
981da177e4SLinus Torvalds 		}
991da177e4SLinus Torvalds 	}
1001da177e4SLinus Torvalds 	lc = hpfs_sb(inode->i_sb)->sb_lowercase;
1011da177e4SLinus Torvalds 	if (filp->f_pos == 12) { /* diff -r requires this (note, that diff -r */
1021da177e4SLinus Torvalds 		filp->f_pos = 13; /* also fails on msdos filesystem in 2.0) */
1031da177e4SLinus Torvalds 		goto out;
1041da177e4SLinus Torvalds 	}
1051da177e4SLinus Torvalds 	if (filp->f_pos == 13) {
1061da177e4SLinus Torvalds 		ret = -ENOENT;
1071da177e4SLinus Torvalds 		goto out;
1081da177e4SLinus Torvalds 	}
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	while (1) {
1111da177e4SLinus Torvalds 		again:
1121da177e4SLinus Torvalds 		/* This won't work when cycle is longer than number of dirents
1131da177e4SLinus Torvalds 		   accepted by filldir, but what can I do?
1141da177e4SLinus Torvalds 		   maybe killall -9 ls helps */
1151da177e4SLinus Torvalds 		if (hpfs_sb(inode->i_sb)->sb_chk)
1161da177e4SLinus Torvalds 			if (hpfs_stop_cycles(inode->i_sb, filp->f_pos, &c1, &c2, "hpfs_readdir")) {
1171da177e4SLinus Torvalds 				ret = -EFSERROR;
1181da177e4SLinus Torvalds 				goto out;
1191da177e4SLinus Torvalds 			}
1201da177e4SLinus Torvalds 		if (filp->f_pos == 12)
1211da177e4SLinus Torvalds 			goto out;
1221da177e4SLinus Torvalds 		if (filp->f_pos == 3 || filp->f_pos == 4 || filp->f_pos == 5) {
1231da177e4SLinus Torvalds 			printk("HPFS: warning: pos==%d\n",(int)filp->f_pos);
1241da177e4SLinus Torvalds 			goto out;
1251da177e4SLinus Torvalds 		}
1261da177e4SLinus Torvalds 		if (filp->f_pos == 0) {
1271da177e4SLinus Torvalds 			if (filldir(dirent, ".", 1, filp->f_pos, inode->i_ino, DT_DIR) < 0)
1281da177e4SLinus Torvalds 				goto out;
1291da177e4SLinus Torvalds 			filp->f_pos = 11;
1301da177e4SLinus Torvalds 		}
1311da177e4SLinus Torvalds 		if (filp->f_pos == 11) {
1321da177e4SLinus Torvalds 			if (filldir(dirent, "..", 2, filp->f_pos, hpfs_inode->i_parent_dir, DT_DIR) < 0)
1331da177e4SLinus Torvalds 				goto out;
1341da177e4SLinus Torvalds 			filp->f_pos = 1;
1351da177e4SLinus Torvalds 		}
1361da177e4SLinus Torvalds 		if (filp->f_pos == 1) {
1371da177e4SLinus Torvalds 			filp->f_pos = ((loff_t) hpfs_de_as_down_as_possible(inode->i_sb, hpfs_inode->i_dno) << 4) + 1;
1381da177e4SLinus Torvalds 			hpfs_add_pos(inode, &filp->f_pos);
1391da177e4SLinus Torvalds 			filp->f_version = inode->i_version;
1401da177e4SLinus Torvalds 		}
1411da177e4SLinus Torvalds 		old_pos = filp->f_pos;
1421da177e4SLinus Torvalds 		if (!(de = map_pos_dirent(inode, &filp->f_pos, &qbh))) {
1431da177e4SLinus Torvalds 			ret = -EIOERROR;
1441da177e4SLinus Torvalds 			goto out;
1451da177e4SLinus Torvalds 		}
1461da177e4SLinus Torvalds 		if (de->first || de->last) {
1471da177e4SLinus Torvalds 			if (hpfs_sb(inode->i_sb)->sb_chk) {
148*18debbbcSRandy Dunlap 				if (de->first && !de->last && (de->namelen != 2
149*18debbbcSRandy Dunlap 				    || de ->name[0] != 1 || de->name[1] != 1))
150*18debbbcSRandy Dunlap 					hpfs_error(inode->i_sb, "hpfs_readdir: bad ^A^A entry; pos = %08lx", old_pos);
151*18debbbcSRandy Dunlap 				if (de->last && (de->namelen != 1 || de ->name[0] != 255))
152*18debbbcSRandy Dunlap 					hpfs_error(inode->i_sb, "hpfs_readdir: bad \\377 entry; pos = %08lx", old_pos);
1531da177e4SLinus Torvalds 			}
1541da177e4SLinus Torvalds 			hpfs_brelse4(&qbh);
1551da177e4SLinus Torvalds 			goto again;
1561da177e4SLinus Torvalds 		}
1571da177e4SLinus Torvalds 		tempname = hpfs_translate_name(inode->i_sb, de->name, de->namelen, lc, de->not_8x3);
1581da177e4SLinus Torvalds 		if (filldir(dirent, tempname, de->namelen, old_pos, de->fnode, DT_UNKNOWN) < 0) {
1591da177e4SLinus Torvalds 			filp->f_pos = old_pos;
1601da177e4SLinus Torvalds 			if (tempname != (char *)de->name) kfree(tempname);
1611da177e4SLinus Torvalds 			hpfs_brelse4(&qbh);
1621da177e4SLinus Torvalds 			goto out;
1631da177e4SLinus Torvalds 		}
1641da177e4SLinus Torvalds 		if (tempname != (char *)de->name) kfree(tempname);
1651da177e4SLinus Torvalds 		hpfs_brelse4(&qbh);
1661da177e4SLinus Torvalds 	}
1671da177e4SLinus Torvalds out:
1681da177e4SLinus Torvalds 	unlock_kernel();
1691da177e4SLinus Torvalds 	return ret;
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds /*
1731da177e4SLinus Torvalds  * lookup.  Search the specified directory for the specified name, set
1741da177e4SLinus Torvalds  * *result to the corresponding inode.
1751da177e4SLinus Torvalds  *
1761da177e4SLinus Torvalds  * lookup uses the inode number to tell read_inode whether it is reading
1771da177e4SLinus Torvalds  * the inode of a directory or a file -- file ino's are odd, directory
1781da177e4SLinus Torvalds  * ino's are even.  read_inode avoids i/o for file inodes; everything
1791da177e4SLinus Torvalds  * needed is up here in the directory.  (And file fnodes are out in
1801da177e4SLinus Torvalds  * the boondocks.)
1811da177e4SLinus Torvalds  *
1821da177e4SLinus Torvalds  *    - M.P.: this is over, sometimes we've got to read file's fnode for eas
1831da177e4SLinus Torvalds  *	      inode numbers are just fnode sector numbers; iget lock is used
1841da177e4SLinus Torvalds  *	      to tell read_inode to read fnode or not.
1851da177e4SLinus Torvalds  */
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds struct dentry *hpfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds 	const char *name = dentry->d_name.name;
1901da177e4SLinus Torvalds 	unsigned len = dentry->d_name.len;
1911da177e4SLinus Torvalds 	struct quad_buffer_head qbh;
1921da177e4SLinus Torvalds 	struct hpfs_dirent *de;
1931da177e4SLinus Torvalds 	ino_t ino;
1941da177e4SLinus Torvalds 	int err;
1951da177e4SLinus Torvalds 	struct inode *result = NULL;
1961da177e4SLinus Torvalds 	struct hpfs_inode_info *hpfs_result;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	lock_kernel();
1991da177e4SLinus Torvalds 	if ((err = hpfs_chk_name((char *)name, &len))) {
2001da177e4SLinus Torvalds 		if (err == -ENAMETOOLONG) {
2011da177e4SLinus Torvalds 			unlock_kernel();
2021da177e4SLinus Torvalds 			return ERR_PTR(-ENAMETOOLONG);
2031da177e4SLinus Torvalds 		}
2041da177e4SLinus Torvalds 		goto end_add;
2051da177e4SLinus Torvalds 	}
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	/*
2081da177e4SLinus Torvalds 	 * '.' and '..' will never be passed here.
2091da177e4SLinus Torvalds 	 */
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	de = map_dirent(dir, hpfs_i(dir)->i_dno, (char *) name, len, NULL, &qbh);
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	/*
2141da177e4SLinus Torvalds 	 * This is not really a bailout, just means file not found.
2151da177e4SLinus Torvalds 	 */
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	if (!de) goto end;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	/*
2201da177e4SLinus Torvalds 	 * Get inode number, what we're after.
2211da177e4SLinus Torvalds 	 */
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	ino = de->fnode;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	/*
2261da177e4SLinus Torvalds 	 * Go find or make an inode.
2271da177e4SLinus Torvalds 	 */
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	result = iget_locked(dir->i_sb, ino);
2301da177e4SLinus Torvalds 	if (!result) {
2311da177e4SLinus Torvalds 		hpfs_error(dir->i_sb, "hpfs_lookup: can't get inode");
2321da177e4SLinus Torvalds 		goto bail1;
2331da177e4SLinus Torvalds 	}
2341da177e4SLinus Torvalds 	if (result->i_state & I_NEW) {
2351da177e4SLinus Torvalds 		hpfs_init_inode(result);
2361da177e4SLinus Torvalds 		if (de->directory)
2371da177e4SLinus Torvalds 			hpfs_read_inode(result);
2381da177e4SLinus Torvalds 		else if (de->ea_size && hpfs_sb(dir->i_sb)->sb_eas)
2391da177e4SLinus Torvalds 			hpfs_read_inode(result);
2401da177e4SLinus Torvalds 		else {
2411da177e4SLinus Torvalds 			result->i_mode |= S_IFREG;
2421da177e4SLinus Torvalds 			result->i_mode &= ~0111;
2431da177e4SLinus Torvalds 			result->i_op = &hpfs_file_iops;
2441da177e4SLinus Torvalds 			result->i_fop = &hpfs_file_ops;
2451da177e4SLinus Torvalds 			result->i_nlink = 1;
2461da177e4SLinus Torvalds 		}
2471da177e4SLinus Torvalds 		unlock_new_inode(result);
2481da177e4SLinus Torvalds 	}
2491da177e4SLinus Torvalds 	hpfs_result = hpfs_i(result);
2501da177e4SLinus Torvalds 	if (!de->directory) hpfs_result->i_parent_dir = dir->i_ino;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	hpfs_decide_conv(result, (char *)name, len);
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	if (de->has_acl || de->has_xtd_perm) if (!(dir->i_sb->s_flags & MS_RDONLY)) {
2551da177e4SLinus 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");
2561da177e4SLinus Torvalds 		goto bail1;
2571da177e4SLinus Torvalds 	}
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	/*
2601da177e4SLinus Torvalds 	 * Fill in the info from the directory if this is a newly created
2611da177e4SLinus Torvalds 	 * inode.
2621da177e4SLinus Torvalds 	 */
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	if (!result->i_ctime.tv_sec) {
2651da177e4SLinus Torvalds 		if (!(result->i_ctime.tv_sec = local_to_gmt(dir->i_sb, de->creation_date)))
2661da177e4SLinus Torvalds 			result->i_ctime.tv_sec = 1;
2671da177e4SLinus Torvalds 		result->i_ctime.tv_nsec = 0;
2681da177e4SLinus Torvalds 		result->i_mtime.tv_sec = local_to_gmt(dir->i_sb, de->write_date);
2691da177e4SLinus Torvalds 		result->i_mtime.tv_nsec = 0;
2701da177e4SLinus Torvalds 		result->i_atime.tv_sec = local_to_gmt(dir->i_sb, de->read_date);
2711da177e4SLinus Torvalds 		result->i_atime.tv_nsec = 0;
2721da177e4SLinus Torvalds 		hpfs_result->i_ea_size = de->ea_size;
2731da177e4SLinus Torvalds 		if (!hpfs_result->i_ea_mode && de->read_only)
2741da177e4SLinus Torvalds 			result->i_mode &= ~0222;
2751da177e4SLinus Torvalds 		if (!de->directory) {
2761da177e4SLinus Torvalds 			if (result->i_size == -1) {
2771da177e4SLinus Torvalds 				result->i_size = de->file_size;
2781da177e4SLinus Torvalds 				result->i_data.a_ops = &hpfs_aops;
2791da177e4SLinus Torvalds 				hpfs_i(result)->mmu_private = result->i_size;
2801da177e4SLinus Torvalds 			/*
2811da177e4SLinus Torvalds 			 * i_blocks should count the fnode and any anodes.
2821da177e4SLinus Torvalds 			 * We count 1 for the fnode and don't bother about
2831da177e4SLinus Torvalds 			 * anodes -- the disk heads are on the directory band
2841da177e4SLinus Torvalds 			 * and we want them to stay there.
2851da177e4SLinus Torvalds 			 */
2861da177e4SLinus Torvalds 				result->i_blocks = 1 + ((result->i_size + 511) >> 9);
2871da177e4SLinus Torvalds 			}
2881da177e4SLinus Torvalds 		}
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	hpfs_brelse4(&qbh);
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	/*
2941da177e4SLinus Torvalds 	 * Made it.
2951da177e4SLinus Torvalds 	 */
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	end:
2981da177e4SLinus Torvalds 	end_add:
2991da177e4SLinus Torvalds 	hpfs_set_dentry_operations(dentry);
3001da177e4SLinus Torvalds 	unlock_kernel();
3011da177e4SLinus Torvalds 	d_add(dentry, result);
3021da177e4SLinus Torvalds 	return NULL;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	/*
3051da177e4SLinus Torvalds 	 * Didn't.
3061da177e4SLinus Torvalds 	 */
3071da177e4SLinus Torvalds 	bail1:
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	hpfs_brelse4(&qbh);
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	/*bail:*/
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	unlock_kernel();
3141da177e4SLinus Torvalds 	return ERR_PTR(-ENOENT);
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
3174b6f5d20SArjan van de Ven const struct file_operations hpfs_dir_ops =
3181da177e4SLinus Torvalds {
3191da177e4SLinus Torvalds 	.llseek		= hpfs_dir_lseek,
3201da177e4SLinus Torvalds 	.read		= generic_read_dir,
3211da177e4SLinus Torvalds 	.readdir	= hpfs_readdir,
3221da177e4SLinus Torvalds 	.release	= hpfs_dir_release,
3231da177e4SLinus Torvalds 	.fsync		= hpfs_file_fsync,
3241da177e4SLinus Torvalds };
325