xref: /openbmc/linux/fs/affs/inode.c (revision e8edc6e03a5c8562dc70a6d969f732bdb355a7e7)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/affs/inode.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  (c) 1996  Hans-Joachim Widmaier - Rewritten
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  (C) 1993  Ray Burr - Modified for Amiga FFS filesystem.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  (C) 1992  Eric Youngdale Modified for ISO9660 filesystem.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  (C) 1991  Linus Torvalds - minix filesystem
111da177e4SLinus Torvalds  */
12*e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
131da177e4SLinus Torvalds #include "affs.h"
141da177e4SLinus Torvalds 
15754661f1SArjan van de Ven extern const struct inode_operations affs_symlink_inode_operations;
161da177e4SLinus Torvalds extern struct timezone sys_tz;
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds void
191da177e4SLinus Torvalds affs_read_inode(struct inode *inode)
201da177e4SLinus Torvalds {
211da177e4SLinus Torvalds 	struct super_block	*sb = inode->i_sb;
221da177e4SLinus Torvalds 	struct affs_sb_info	*sbi = AFFS_SB(sb);
231da177e4SLinus Torvalds 	struct buffer_head	*bh;
241da177e4SLinus Torvalds 	struct affs_head	*head;
251da177e4SLinus Torvalds 	struct affs_tail	*tail;
261da177e4SLinus Torvalds 	u32			 block;
271da177e4SLinus Torvalds 	u32			 size;
281da177e4SLinus Torvalds 	u32			 prot;
291da177e4SLinus Torvalds 	u16			 id;
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds 	pr_debug("AFFS: read_inode(%lu)\n",inode->i_ino);
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds 	block = inode->i_ino;
341da177e4SLinus Torvalds 	bh = affs_bread(sb, block);
351da177e4SLinus Torvalds 	if (!bh) {
361da177e4SLinus Torvalds 		affs_warning(sb, "read_inode", "Cannot read block %d", block);
371da177e4SLinus Torvalds 		goto bad_inode;
381da177e4SLinus Torvalds 	}
391da177e4SLinus Torvalds 	if (affs_checksum_block(sb, bh) || be32_to_cpu(AFFS_HEAD(bh)->ptype) != T_SHORT) {
401da177e4SLinus Torvalds 		affs_warning(sb,"read_inode",
411da177e4SLinus Torvalds 			   "Checksum or type (ptype=%d) error on inode %d",
421da177e4SLinus Torvalds 			   AFFS_HEAD(bh)->ptype, block);
431da177e4SLinus Torvalds 		goto bad_inode;
441da177e4SLinus Torvalds 	}
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds 	head = AFFS_HEAD(bh);
471da177e4SLinus Torvalds 	tail = AFFS_TAIL(sb, bh);
481da177e4SLinus Torvalds 	prot = be32_to_cpu(tail->protect);
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 	inode->i_size = 0;
511da177e4SLinus Torvalds 	inode->i_nlink = 1;
521da177e4SLinus Torvalds 	inode->i_mode = 0;
531da177e4SLinus Torvalds 	AFFS_I(inode)->i_extcnt = 1;
541da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_last = ~1;
551da177e4SLinus Torvalds 	AFFS_I(inode)->i_protect = prot;
561da177e4SLinus Torvalds 	AFFS_I(inode)->i_opencnt = 0;
571da177e4SLinus Torvalds 	AFFS_I(inode)->i_blkcnt = 0;
581da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc = NULL;
591da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_size = 0;
601da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_shift = 0;
611da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_mask = 0;
621da177e4SLinus Torvalds 	AFFS_I(inode)->i_ac = NULL;
631da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_bh = NULL;
641da177e4SLinus Torvalds 	AFFS_I(inode)->mmu_private = 0;
651da177e4SLinus Torvalds 	AFFS_I(inode)->i_lastalloc = 0;
661da177e4SLinus Torvalds 	AFFS_I(inode)->i_pa_cnt = 0;
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 	if (sbi->s_flags & SF_SETMODE)
691da177e4SLinus Torvalds 		inode->i_mode = sbi->s_mode;
701da177e4SLinus Torvalds 	else
711da177e4SLinus Torvalds 		inode->i_mode = prot_to_mode(prot);
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 	id = be16_to_cpu(tail->uid);
741da177e4SLinus Torvalds 	if (id == 0 || sbi->s_flags & SF_SETUID)
751da177e4SLinus Torvalds 		inode->i_uid = sbi->s_uid;
761da177e4SLinus Torvalds 	else if (id == 0xFFFF && sbi->s_flags & SF_MUFS)
771da177e4SLinus Torvalds 		inode->i_uid = 0;
781da177e4SLinus Torvalds 	else
791da177e4SLinus Torvalds 		inode->i_uid = id;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	id = be16_to_cpu(tail->gid);
821da177e4SLinus Torvalds 	if (id == 0 || sbi->s_flags & SF_SETGID)
831da177e4SLinus Torvalds 		inode->i_gid = sbi->s_gid;
841da177e4SLinus Torvalds 	else if (id == 0xFFFF && sbi->s_flags & SF_MUFS)
851da177e4SLinus Torvalds 		inode->i_gid = 0;
861da177e4SLinus Torvalds 	else
871da177e4SLinus Torvalds 		inode->i_gid = id;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	switch (be32_to_cpu(tail->stype)) {
901da177e4SLinus Torvalds 	case ST_ROOT:
911da177e4SLinus Torvalds 		inode->i_uid = sbi->s_uid;
921da177e4SLinus Torvalds 		inode->i_gid = sbi->s_gid;
931da177e4SLinus Torvalds 		/* fall through */
941da177e4SLinus Torvalds 	case ST_USERDIR:
951da177e4SLinus Torvalds 		if (be32_to_cpu(tail->stype) == ST_USERDIR ||
961da177e4SLinus Torvalds 		    sbi->s_flags & SF_SETMODE) {
971da177e4SLinus Torvalds 			if (inode->i_mode & S_IRUSR)
981da177e4SLinus Torvalds 				inode->i_mode |= S_IXUSR;
991da177e4SLinus Torvalds 			if (inode->i_mode & S_IRGRP)
1001da177e4SLinus Torvalds 				inode->i_mode |= S_IXGRP;
1011da177e4SLinus Torvalds 			if (inode->i_mode & S_IROTH)
1021da177e4SLinus Torvalds 				inode->i_mode |= S_IXOTH;
1031da177e4SLinus Torvalds 			inode->i_mode |= S_IFDIR;
1041da177e4SLinus Torvalds 		} else
1051da177e4SLinus Torvalds 			inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
1061da177e4SLinus Torvalds 		if (tail->link_chain)
1071da177e4SLinus Torvalds 			inode->i_nlink = 2;
1081da177e4SLinus Torvalds 		/* Maybe it should be controlled by mount parameter? */
1091da177e4SLinus Torvalds 		//inode->i_mode |= S_ISVTX;
1101da177e4SLinus Torvalds 		inode->i_op = &affs_dir_inode_operations;
1111da177e4SLinus Torvalds 		inode->i_fop = &affs_dir_operations;
1121da177e4SLinus Torvalds 		break;
1131da177e4SLinus Torvalds 	case ST_LINKDIR:
1141da177e4SLinus Torvalds #if 0
1151da177e4SLinus Torvalds 		affs_warning(sb, "read_inode", "inode is LINKDIR");
1161da177e4SLinus Torvalds 		goto bad_inode;
1171da177e4SLinus Torvalds #else
1181da177e4SLinus Torvalds 		inode->i_mode |= S_IFDIR;
1191da177e4SLinus Torvalds 		inode->i_op = NULL;
1201da177e4SLinus Torvalds 		inode->i_fop = NULL;
1211da177e4SLinus Torvalds 		break;
1221da177e4SLinus Torvalds #endif
1231da177e4SLinus Torvalds 	case ST_LINKFILE:
1241da177e4SLinus Torvalds 		affs_warning(sb, "read_inode", "inode is LINKFILE");
1251da177e4SLinus Torvalds 		goto bad_inode;
1261da177e4SLinus Torvalds 	case ST_FILE:
1271da177e4SLinus Torvalds 		size = be32_to_cpu(tail->size);
1281da177e4SLinus Torvalds 		inode->i_mode |= S_IFREG;
1291da177e4SLinus Torvalds 		AFFS_I(inode)->mmu_private = inode->i_size = size;
1301da177e4SLinus Torvalds 		if (inode->i_size) {
1311da177e4SLinus Torvalds 			AFFS_I(inode)->i_blkcnt = (size - 1) /
1321da177e4SLinus Torvalds 					       sbi->s_data_blksize + 1;
1331da177e4SLinus Torvalds 			AFFS_I(inode)->i_extcnt = (AFFS_I(inode)->i_blkcnt - 1) /
1341da177e4SLinus Torvalds 					       sbi->s_hashsize + 1;
1351da177e4SLinus Torvalds 		}
1361da177e4SLinus Torvalds 		if (tail->link_chain)
1371da177e4SLinus Torvalds 			inode->i_nlink = 2;
1381da177e4SLinus Torvalds 		inode->i_mapping->a_ops = (sbi->s_flags & SF_OFS) ? &affs_aops_ofs : &affs_aops;
1391da177e4SLinus Torvalds 		inode->i_op = &affs_file_inode_operations;
1401da177e4SLinus Torvalds 		inode->i_fop = &affs_file_operations;
1411da177e4SLinus Torvalds 		break;
1421da177e4SLinus Torvalds 	case ST_SOFTLINK:
1431da177e4SLinus Torvalds 		inode->i_mode |= S_IFLNK;
1441da177e4SLinus Torvalds 		inode->i_op = &affs_symlink_inode_operations;
1451da177e4SLinus Torvalds 		inode->i_data.a_ops = &affs_symlink_aops;
1461da177e4SLinus Torvalds 		break;
1471da177e4SLinus Torvalds 	}
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec
1501da177e4SLinus Torvalds 		       = (be32_to_cpu(tail->change.days) * (24 * 60 * 60) +
1511da177e4SLinus Torvalds 		         be32_to_cpu(tail->change.mins) * 60 +
1521da177e4SLinus Torvalds 			 be32_to_cpu(tail->change.ticks) / 50 +
1531da177e4SLinus Torvalds 			 ((8 * 365 + 2) * 24 * 60 * 60)) +
1541da177e4SLinus Torvalds 			 sys_tz.tz_minuteswest * 60;
1551da177e4SLinus Torvalds 	inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_atime.tv_nsec = 0;
1561da177e4SLinus Torvalds 	affs_brelse(bh);
1571da177e4SLinus Torvalds 	return;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds bad_inode:
1601da177e4SLinus Torvalds 	make_bad_inode(inode);
1611da177e4SLinus Torvalds 	affs_brelse(bh);
1621da177e4SLinus Torvalds 	return;
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds int
1661da177e4SLinus Torvalds affs_write_inode(struct inode *inode, int unused)
1671da177e4SLinus Torvalds {
1681da177e4SLinus Torvalds 	struct super_block	*sb = inode->i_sb;
1691da177e4SLinus Torvalds 	struct buffer_head	*bh;
1701da177e4SLinus Torvalds 	struct affs_tail	*tail;
1711da177e4SLinus Torvalds 	uid_t			 uid;
1721da177e4SLinus Torvalds 	gid_t			 gid;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	pr_debug("AFFS: write_inode(%lu)\n",inode->i_ino);
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	if (!inode->i_nlink)
1771da177e4SLinus Torvalds 		// possibly free block
1781da177e4SLinus Torvalds 		return 0;
1791da177e4SLinus Torvalds 	bh = affs_bread(sb, inode->i_ino);
1801da177e4SLinus Torvalds 	if (!bh) {
1811da177e4SLinus Torvalds 		affs_error(sb,"write_inode","Cannot read block %lu",inode->i_ino);
1821da177e4SLinus Torvalds 		return -EIO;
1831da177e4SLinus Torvalds 	}
1841da177e4SLinus Torvalds 	tail = AFFS_TAIL(sb, bh);
1851da177e4SLinus Torvalds 	if (tail->stype == cpu_to_be32(ST_ROOT)) {
1861da177e4SLinus Torvalds 		secs_to_datestamp(inode->i_mtime.tv_sec,&AFFS_ROOT_TAIL(sb, bh)->root_change);
1871da177e4SLinus Torvalds 	} else {
1881da177e4SLinus Torvalds 		tail->protect = cpu_to_be32(AFFS_I(inode)->i_protect);
1891da177e4SLinus Torvalds 		tail->size = cpu_to_be32(inode->i_size);
1901da177e4SLinus Torvalds 		secs_to_datestamp(inode->i_mtime.tv_sec,&tail->change);
1911da177e4SLinus Torvalds 		if (!(inode->i_ino == AFFS_SB(sb)->s_root_block)) {
1921da177e4SLinus Torvalds 			uid = inode->i_uid;
1931da177e4SLinus Torvalds 			gid = inode->i_gid;
1941da177e4SLinus Torvalds 			if (AFFS_SB(sb)->s_flags & SF_MUFS) {
1951da177e4SLinus Torvalds 				if (inode->i_uid == 0 || inode->i_uid == 0xFFFF)
1961da177e4SLinus Torvalds 					uid = inode->i_uid ^ ~0;
1971da177e4SLinus Torvalds 				if (inode->i_gid == 0 || inode->i_gid == 0xFFFF)
1981da177e4SLinus Torvalds 					gid = inode->i_gid ^ ~0;
1991da177e4SLinus Torvalds 			}
2001da177e4SLinus Torvalds 			if (!(AFFS_SB(sb)->s_flags & SF_SETUID))
2011da177e4SLinus Torvalds 				tail->uid = cpu_to_be16(uid);
2021da177e4SLinus Torvalds 			if (!(AFFS_SB(sb)->s_flags & SF_SETGID))
2031da177e4SLinus Torvalds 				tail->gid = cpu_to_be16(gid);
2041da177e4SLinus Torvalds 		}
2051da177e4SLinus Torvalds 	}
2061da177e4SLinus Torvalds 	affs_fix_checksum(sb, bh);
2071da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
2081da177e4SLinus Torvalds 	affs_brelse(bh);
2091da177e4SLinus Torvalds 	affs_free_prealloc(inode);
2101da177e4SLinus Torvalds 	return 0;
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds int
2141da177e4SLinus Torvalds affs_notify_change(struct dentry *dentry, struct iattr *attr)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2171da177e4SLinus Torvalds 	int error;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	pr_debug("AFFS: notify_change(%lu,0x%x)\n",inode->i_ino,attr->ia_valid);
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	error = inode_change_ok(inode,attr);
2221da177e4SLinus Torvalds 	if (error)
2231da177e4SLinus Torvalds 		goto out;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	if (((attr->ia_valid & ATTR_UID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETUID)) ||
2261da177e4SLinus Torvalds 	    ((attr->ia_valid & ATTR_GID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETGID)) ||
2271da177e4SLinus Torvalds 	    ((attr->ia_valid & ATTR_MODE) &&
2281da177e4SLinus Torvalds 	     (AFFS_SB(inode->i_sb)->s_flags & (SF_SETMODE | SF_IMMUTABLE)))) {
2291da177e4SLinus Torvalds 		if (!(AFFS_SB(inode->i_sb)->s_flags & SF_QUIET))
2301da177e4SLinus Torvalds 			error = -EPERM;
2311da177e4SLinus Torvalds 		goto out;
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	error = inode_setattr(inode, attr);
2351da177e4SLinus Torvalds 	if (!error && (attr->ia_valid & ATTR_MODE))
2361da177e4SLinus Torvalds 		mode_to_prot(inode);
2371da177e4SLinus Torvalds out:
2381da177e4SLinus Torvalds 	return error;
2391da177e4SLinus Torvalds }
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds void
2421da177e4SLinus Torvalds affs_put_inode(struct inode *inode)
2431da177e4SLinus Torvalds {
2441da177e4SLinus Torvalds 	pr_debug("AFFS: put_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink);
2451da177e4SLinus Torvalds 	affs_free_prealloc(inode);
2462be3c790SChristoph Hellwig }
2472be3c790SChristoph Hellwig 
2482be3c790SChristoph Hellwig void
2492be3c790SChristoph Hellwig affs_drop_inode(struct inode *inode)
2502be3c790SChristoph Hellwig {
2511b1dcc1bSJes Sorensen 	mutex_lock(&inode->i_mutex);
2521da177e4SLinus Torvalds 	if (inode->i_size != AFFS_I(inode)->mmu_private)
2531da177e4SLinus Torvalds 		affs_truncate(inode);
2541b1dcc1bSJes Sorensen 	mutex_unlock(&inode->i_mutex);
2552be3c790SChristoph Hellwig 
2562be3c790SChristoph Hellwig 	generic_drop_inode(inode);
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds void
2601da177e4SLinus Torvalds affs_delete_inode(struct inode *inode)
2611da177e4SLinus Torvalds {
2621da177e4SLinus Torvalds 	pr_debug("AFFS: delete_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink);
263fef26658SMark Fasheh 	truncate_inode_pages(&inode->i_data, 0);
2641da177e4SLinus Torvalds 	inode->i_size = 0;
2651da177e4SLinus Torvalds 	if (S_ISREG(inode->i_mode))
2661da177e4SLinus Torvalds 		affs_truncate(inode);
2671da177e4SLinus Torvalds 	clear_inode(inode);
2681da177e4SLinus Torvalds 	affs_free_block(inode->i_sb, inode->i_ino);
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds void
2721da177e4SLinus Torvalds affs_clear_inode(struct inode *inode)
2731da177e4SLinus Torvalds {
2741da177e4SLinus Torvalds 	unsigned long cache_page = (unsigned long) AFFS_I(inode)->i_lc;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	pr_debug("AFFS: clear_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink);
2771da177e4SLinus Torvalds 	if (cache_page) {
2781da177e4SLinus Torvalds 		pr_debug("AFFS: freeing ext cache\n");
2791da177e4SLinus Torvalds 		AFFS_I(inode)->i_lc = NULL;
2801da177e4SLinus Torvalds 		AFFS_I(inode)->i_ac = NULL;
2811da177e4SLinus Torvalds 		free_page(cache_page);
2821da177e4SLinus Torvalds 	}
2831da177e4SLinus Torvalds 	affs_brelse(AFFS_I(inode)->i_ext_bh);
2841da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_last = ~1;
2851da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_bh = NULL;
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds struct inode *
2891da177e4SLinus Torvalds affs_new_inode(struct inode *dir)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	struct super_block	*sb = dir->i_sb;
2921da177e4SLinus Torvalds 	struct inode		*inode;
2931da177e4SLinus Torvalds 	u32			 block;
2941da177e4SLinus Torvalds 	struct buffer_head	*bh;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	if (!(inode = new_inode(sb)))
2971da177e4SLinus Torvalds 		goto err_inode;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	if (!(block = affs_alloc_block(dir, dir->i_ino)))
3001da177e4SLinus Torvalds 		goto err_block;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	bh = affs_getzeroblk(sb, block);
3031da177e4SLinus Torvalds 	if (!bh)
3041da177e4SLinus Torvalds 		goto err_bh;
3051da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
3061da177e4SLinus Torvalds 	affs_brelse(bh);
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	inode->i_uid     = current->fsuid;
3091da177e4SLinus Torvalds 	inode->i_gid     = current->fsgid;
3101da177e4SLinus Torvalds 	inode->i_ino     = block;
3111da177e4SLinus Torvalds 	inode->i_nlink   = 1;
3121da177e4SLinus Torvalds 	inode->i_mtime   = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
3131da177e4SLinus Torvalds 	AFFS_I(inode)->i_opencnt = 0;
3141da177e4SLinus Torvalds 	AFFS_I(inode)->i_blkcnt = 0;
3151da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc = NULL;
3161da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_size = 0;
3171da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_shift = 0;
3181da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_mask = 0;
3191da177e4SLinus Torvalds 	AFFS_I(inode)->i_ac = NULL;
3201da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_bh = NULL;
3211da177e4SLinus Torvalds 	AFFS_I(inode)->mmu_private = 0;
3221da177e4SLinus Torvalds 	AFFS_I(inode)->i_protect = 0;
3231da177e4SLinus Torvalds 	AFFS_I(inode)->i_lastalloc = 0;
3241da177e4SLinus Torvalds 	AFFS_I(inode)->i_pa_cnt = 0;
3251da177e4SLinus Torvalds 	AFFS_I(inode)->i_extcnt = 1;
3261da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_last = ~1;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	insert_inode_hash(inode);
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	return inode;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds err_bh:
3331da177e4SLinus Torvalds 	affs_free_block(sb, block);
3341da177e4SLinus Torvalds err_block:
3351da177e4SLinus Torvalds 	iput(inode);
3361da177e4SLinus Torvalds err_inode:
3371da177e4SLinus Torvalds 	return NULL;
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds /*
3411da177e4SLinus Torvalds  * Add an entry to a directory. Create the header block
3421da177e4SLinus Torvalds  * and insert it into the hash table.
3431da177e4SLinus Torvalds  */
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds int
3461da177e4SLinus Torvalds affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type)
3471da177e4SLinus Torvalds {
3481da177e4SLinus Torvalds 	struct super_block *sb = dir->i_sb;
3491da177e4SLinus Torvalds 	struct buffer_head *inode_bh = NULL;
3501da177e4SLinus Torvalds 	struct buffer_head *bh = NULL;
3511da177e4SLinus Torvalds 	u32 block = 0;
3521da177e4SLinus Torvalds 	int retval;
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	pr_debug("AFFS: add_entry(dir=%u, inode=%u, \"%*s\", type=%d)\n", (u32)dir->i_ino,
3551da177e4SLinus Torvalds 	         (u32)inode->i_ino, (int)dentry->d_name.len, dentry->d_name.name, type);
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	retval = -EIO;
3581da177e4SLinus Torvalds 	bh = affs_bread(sb, inode->i_ino);
3591da177e4SLinus Torvalds 	if (!bh)
3601da177e4SLinus Torvalds 		goto done;
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	affs_lock_link(inode);
3631da177e4SLinus Torvalds 	switch (type) {
3641da177e4SLinus Torvalds 	case ST_LINKFILE:
3651da177e4SLinus Torvalds 	case ST_LINKDIR:
3661da177e4SLinus Torvalds 		inode_bh = bh;
3671da177e4SLinus Torvalds 		retval = -ENOSPC;
3681da177e4SLinus Torvalds 		block = affs_alloc_block(dir, dir->i_ino);
3691da177e4SLinus Torvalds 		if (!block)
3701da177e4SLinus Torvalds 			goto err;
3711da177e4SLinus Torvalds 		retval = -EIO;
3721da177e4SLinus Torvalds 		bh = affs_getzeroblk(sb, block);
3731da177e4SLinus Torvalds 		if (!bh)
3741da177e4SLinus Torvalds 			goto err;
3751da177e4SLinus Torvalds 		break;
3761da177e4SLinus Torvalds 	default:
3771da177e4SLinus Torvalds 		break;
3781da177e4SLinus Torvalds 	}
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT);
3811da177e4SLinus Torvalds 	AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr);
3821da177e4SLinus Torvalds 	affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry);
3831da177e4SLinus Torvalds 	AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type);
3841da177e4SLinus Torvalds 	AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	if (inode_bh) {
3871da177e4SLinus Torvalds 		__be32 chain;
3881da177e4SLinus Torvalds 	       	chain = AFFS_TAIL(sb, inode_bh)->link_chain;
3891da177e4SLinus Torvalds 		AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino);
3901da177e4SLinus Torvalds 		AFFS_TAIL(sb, bh)->link_chain = chain;
3911da177e4SLinus Torvalds 		AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block);
3921da177e4SLinus Torvalds 		affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain));
3931da177e4SLinus Torvalds 		mark_buffer_dirty_inode(inode_bh, inode);
3941da177e4SLinus Torvalds 		inode->i_nlink = 2;
3951da177e4SLinus Torvalds 		atomic_inc(&inode->i_count);
3961da177e4SLinus Torvalds 	}
3971da177e4SLinus Torvalds 	affs_fix_checksum(sb, bh);
3981da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
3991da177e4SLinus Torvalds 	dentry->d_fsdata = (void *)(long)bh->b_blocknr;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	affs_lock_dir(dir);
4021da177e4SLinus Torvalds 	retval = affs_insert_hash(dir, bh);
4031da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
4041da177e4SLinus Torvalds 	affs_unlock_dir(dir);
4051da177e4SLinus Torvalds 	affs_unlock_link(inode);
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
4081da177e4SLinus Torvalds done:
4091da177e4SLinus Torvalds 	affs_brelse(inode_bh);
4101da177e4SLinus Torvalds 	affs_brelse(bh);
4111da177e4SLinus Torvalds 	return retval;
4121da177e4SLinus Torvalds err:
4131da177e4SLinus Torvalds 	if (block)
4141da177e4SLinus Torvalds 		affs_free_block(sb, block);
4151da177e4SLinus Torvalds 	affs_unlock_link(inode);
4161da177e4SLinus Torvalds 	goto done;
4171da177e4SLinus Torvalds }
418