xref: /openbmc/linux/fs/affs/inode.c (revision 2b0143b5c986be1ce8408b3aadc4709e0a94429d)
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  */
12e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
135a0e3ad6STejun Heo #include <linux/gfp.h>
141da177e4SLinus Torvalds #include "affs.h"
151da177e4SLinus Torvalds 
16210f8559SDavid Howells struct inode *affs_iget(struct super_block *sb, unsigned long ino)
171da177e4SLinus Torvalds {
181da177e4SLinus Torvalds 	struct affs_sb_info	*sbi = AFFS_SB(sb);
191da177e4SLinus Torvalds 	struct buffer_head	*bh;
201da177e4SLinus Torvalds 	struct affs_tail	*tail;
21210f8559SDavid Howells 	struct inode		*inode;
221da177e4SLinus Torvalds 	u32			 block;
231da177e4SLinus Torvalds 	u32			 size;
241da177e4SLinus Torvalds 	u32			 prot;
251da177e4SLinus Torvalds 	u16			 id;
261da177e4SLinus Torvalds 
27210f8559SDavid Howells 	inode = iget_locked(sb, ino);
28210f8559SDavid Howells 	if (!inode)
29210f8559SDavid Howells 		return ERR_PTR(-ENOMEM);
30210f8559SDavid Howells 	if (!(inode->i_state & I_NEW))
31210f8559SDavid Howells 		return inode;
32210f8559SDavid Howells 
339606d9aaSFabian Frederick 	pr_debug("affs_iget(%lu)\n", inode->i_ino);
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds 	block = inode->i_ino;
361da177e4SLinus Torvalds 	bh = affs_bread(sb, block);
371da177e4SLinus Torvalds 	if (!bh) {
381da177e4SLinus Torvalds 		affs_warning(sb, "read_inode", "Cannot read block %d", block);
391da177e4SLinus Torvalds 		goto bad_inode;
401da177e4SLinus Torvalds 	}
411da177e4SLinus Torvalds 	if (affs_checksum_block(sb, bh) || be32_to_cpu(AFFS_HEAD(bh)->ptype) != T_SHORT) {
421da177e4SLinus Torvalds 		affs_warning(sb,"read_inode",
431da177e4SLinus Torvalds 			   "Checksum or type (ptype=%d) error on inode %d",
441da177e4SLinus Torvalds 			   AFFS_HEAD(bh)->ptype, block);
451da177e4SLinus Torvalds 		goto bad_inode;
461da177e4SLinus Torvalds 	}
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 	tail = AFFS_TAIL(sb, bh);
491da177e4SLinus Torvalds 	prot = be32_to_cpu(tail->protect);
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 	inode->i_size = 0;
52bfe86848SMiklos Szeredi 	set_nlink(inode, 1);
531da177e4SLinus Torvalds 	inode->i_mode = 0;
541da177e4SLinus Torvalds 	AFFS_I(inode)->i_extcnt = 1;
551da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_last = ~1;
561da177e4SLinus Torvalds 	AFFS_I(inode)->i_protect = prot;
57dca3c336SRoman Zippel 	atomic_set(&AFFS_I(inode)->i_opencnt, 0);
581da177e4SLinus Torvalds 	AFFS_I(inode)->i_blkcnt = 0;
591da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc = NULL;
601da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_size = 0;
611da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_shift = 0;
621da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_mask = 0;
631da177e4SLinus Torvalds 	AFFS_I(inode)->i_ac = NULL;
641da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_bh = NULL;
651da177e4SLinus Torvalds 	AFFS_I(inode)->mmu_private = 0;
661da177e4SLinus Torvalds 	AFFS_I(inode)->i_lastalloc = 0;
671da177e4SLinus Torvalds 	AFFS_I(inode)->i_pa_cnt = 0;
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	if (sbi->s_flags & SF_SETMODE)
701da177e4SLinus Torvalds 		inode->i_mode = sbi->s_mode;
711da177e4SLinus Torvalds 	else
721da177e4SLinus Torvalds 		inode->i_mode = prot_to_mode(prot);
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	id = be16_to_cpu(tail->uid);
751da177e4SLinus Torvalds 	if (id == 0 || sbi->s_flags & SF_SETUID)
761da177e4SLinus Torvalds 		inode->i_uid = sbi->s_uid;
771da177e4SLinus Torvalds 	else if (id == 0xFFFF && sbi->s_flags & SF_MUFS)
788fed10beSEric W. Biederman 		i_uid_write(inode, 0);
791da177e4SLinus Torvalds 	else
808fed10beSEric W. Biederman 		i_uid_write(inode, id);
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	id = be16_to_cpu(tail->gid);
831da177e4SLinus Torvalds 	if (id == 0 || sbi->s_flags & SF_SETGID)
841da177e4SLinus Torvalds 		inode->i_gid = sbi->s_gid;
851da177e4SLinus Torvalds 	else if (id == 0xFFFF && sbi->s_flags & SF_MUFS)
868fed10beSEric W. Biederman 		i_gid_write(inode, 0);
871da177e4SLinus Torvalds 	else
888fed10beSEric W. Biederman 		i_gid_write(inode, id);
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	switch (be32_to_cpu(tail->stype)) {
911da177e4SLinus Torvalds 	case ST_ROOT:
921da177e4SLinus Torvalds 		inode->i_uid = sbi->s_uid;
931da177e4SLinus Torvalds 		inode->i_gid = sbi->s_gid;
941da177e4SLinus Torvalds 		/* fall through */
951da177e4SLinus Torvalds 	case ST_USERDIR:
961da177e4SLinus Torvalds 		if (be32_to_cpu(tail->stype) == ST_USERDIR ||
971da177e4SLinus Torvalds 		    sbi->s_flags & SF_SETMODE) {
981da177e4SLinus Torvalds 			if (inode->i_mode & S_IRUSR)
991da177e4SLinus Torvalds 				inode->i_mode |= S_IXUSR;
1001da177e4SLinus Torvalds 			if (inode->i_mode & S_IRGRP)
1011da177e4SLinus Torvalds 				inode->i_mode |= S_IXGRP;
1021da177e4SLinus Torvalds 			if (inode->i_mode & S_IROTH)
1031da177e4SLinus Torvalds 				inode->i_mode |= S_IXOTH;
1041da177e4SLinus Torvalds 			inode->i_mode |= S_IFDIR;
1051da177e4SLinus Torvalds 		} else
1061da177e4SLinus Torvalds 			inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
1071da177e4SLinus Torvalds 		/* Maybe it should be controlled by mount parameter? */
1081da177e4SLinus Torvalds 		//inode->i_mode |= S_ISVTX;
1091da177e4SLinus Torvalds 		inode->i_op = &affs_dir_inode_operations;
1101da177e4SLinus Torvalds 		inode->i_fop = &affs_dir_operations;
1111da177e4SLinus Torvalds 		break;
1121da177e4SLinus Torvalds 	case ST_LINKDIR:
1131da177e4SLinus Torvalds #if 0
1141da177e4SLinus Torvalds 		affs_warning(sb, "read_inode", "inode is LINKDIR");
1151da177e4SLinus Torvalds 		goto bad_inode;
1161da177e4SLinus Torvalds #else
1171da177e4SLinus Torvalds 		inode->i_mode |= S_IFDIR;
118c765d479SAl Viro 		/* ... and leave ->i_op and ->i_fop pointing to empty */
1191da177e4SLinus Torvalds 		break;
1201da177e4SLinus Torvalds #endif
1211da177e4SLinus Torvalds 	case ST_LINKFILE:
1221da177e4SLinus Torvalds 		affs_warning(sb, "read_inode", "inode is LINKFILE");
1231da177e4SLinus Torvalds 		goto bad_inode;
1241da177e4SLinus Torvalds 	case ST_FILE:
1251da177e4SLinus Torvalds 		size = be32_to_cpu(tail->size);
1261da177e4SLinus Torvalds 		inode->i_mode |= S_IFREG;
1271da177e4SLinus Torvalds 		AFFS_I(inode)->mmu_private = inode->i_size = size;
1281da177e4SLinus Torvalds 		if (inode->i_size) {
1291da177e4SLinus Torvalds 			AFFS_I(inode)->i_blkcnt = (size - 1) /
1301da177e4SLinus Torvalds 					       sbi->s_data_blksize + 1;
1311da177e4SLinus Torvalds 			AFFS_I(inode)->i_extcnt = (AFFS_I(inode)->i_blkcnt - 1) /
1321da177e4SLinus Torvalds 					       sbi->s_hashsize + 1;
1331da177e4SLinus Torvalds 		}
1341da177e4SLinus Torvalds 		if (tail->link_chain)
135bfe86848SMiklos Szeredi 			set_nlink(inode, 2);
1361da177e4SLinus Torvalds 		inode->i_mapping->a_ops = (sbi->s_flags & SF_OFS) ? &affs_aops_ofs : &affs_aops;
1371da177e4SLinus Torvalds 		inode->i_op = &affs_file_inode_operations;
1381da177e4SLinus Torvalds 		inode->i_fop = &affs_file_operations;
1391da177e4SLinus Torvalds 		break;
1401da177e4SLinus Torvalds 	case ST_SOFTLINK:
1411da177e4SLinus Torvalds 		inode->i_mode |= S_IFLNK;
1421da177e4SLinus Torvalds 		inode->i_op = &affs_symlink_inode_operations;
1431da177e4SLinus Torvalds 		inode->i_data.a_ops = &affs_symlink_aops;
1441da177e4SLinus Torvalds 		break;
1451da177e4SLinus Torvalds 	}
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec
1481da177e4SLinus Torvalds 		       = (be32_to_cpu(tail->change.days) * (24 * 60 * 60) +
1491da177e4SLinus Torvalds 		         be32_to_cpu(tail->change.mins) * 60 +
1501da177e4SLinus Torvalds 			 be32_to_cpu(tail->change.ticks) / 50 +
1511da177e4SLinus Torvalds 			 ((8 * 365 + 2) * 24 * 60 * 60)) +
1521da177e4SLinus Torvalds 			 sys_tz.tz_minuteswest * 60;
1531da177e4SLinus Torvalds 	inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_atime.tv_nsec = 0;
1541da177e4SLinus Torvalds 	affs_brelse(bh);
155210f8559SDavid Howells 	unlock_new_inode(inode);
156210f8559SDavid Howells 	return inode;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds bad_inode:
1591da177e4SLinus Torvalds 	affs_brelse(bh);
160210f8559SDavid Howells 	iget_failed(inode);
161210f8559SDavid Howells 	return ERR_PTR(-EIO);
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds int
165a9185b41SChristoph Hellwig affs_write_inode(struct inode *inode, struct writeback_control *wbc)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	struct super_block	*sb = inode->i_sb;
1681da177e4SLinus Torvalds 	struct buffer_head	*bh;
1691da177e4SLinus Torvalds 	struct affs_tail	*tail;
1701da177e4SLinus Torvalds 	uid_t			 uid;
1711da177e4SLinus Torvalds 	gid_t			 gid;
1721da177e4SLinus Torvalds 
1739606d9aaSFabian Frederick 	pr_debug("write_inode(%lu)\n", inode->i_ino);
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	if (!inode->i_nlink)
1761da177e4SLinus Torvalds 		// possibly free block
1771da177e4SLinus Torvalds 		return 0;
1781da177e4SLinus Torvalds 	bh = affs_bread(sb, inode->i_ino);
1791da177e4SLinus Torvalds 	if (!bh) {
1801da177e4SLinus Torvalds 		affs_error(sb,"write_inode","Cannot read block %lu",inode->i_ino);
1811da177e4SLinus Torvalds 		return -EIO;
1821da177e4SLinus Torvalds 	}
1831da177e4SLinus Torvalds 	tail = AFFS_TAIL(sb, bh);
1841da177e4SLinus Torvalds 	if (tail->stype == cpu_to_be32(ST_ROOT)) {
1851da177e4SLinus Torvalds 		secs_to_datestamp(inode->i_mtime.tv_sec,&AFFS_ROOT_TAIL(sb, bh)->root_change);
1861da177e4SLinus Torvalds 	} else {
1871da177e4SLinus Torvalds 		tail->protect = cpu_to_be32(AFFS_I(inode)->i_protect);
1881da177e4SLinus Torvalds 		tail->size = cpu_to_be32(inode->i_size);
1891da177e4SLinus Torvalds 		secs_to_datestamp(inode->i_mtime.tv_sec,&tail->change);
1901da177e4SLinus Torvalds 		if (!(inode->i_ino == AFFS_SB(sb)->s_root_block)) {
1918fed10beSEric W. Biederman 			uid = i_uid_read(inode);
1928fed10beSEric W. Biederman 			gid = i_gid_read(inode);
1931da177e4SLinus Torvalds 			if (AFFS_SB(sb)->s_flags & SF_MUFS) {
1948fed10beSEric W. Biederman 				if (uid == 0 || uid == 0xFFFF)
1958fed10beSEric W. Biederman 					uid = uid ^ ~0;
1968fed10beSEric W. Biederman 				if (gid == 0 || gid == 0xFFFF)
1978fed10beSEric W. Biederman 					gid = gid ^ ~0;
1981da177e4SLinus Torvalds 			}
1991da177e4SLinus Torvalds 			if (!(AFFS_SB(sb)->s_flags & SF_SETUID))
2001da177e4SLinus Torvalds 				tail->uid = cpu_to_be16(uid);
2011da177e4SLinus Torvalds 			if (!(AFFS_SB(sb)->s_flags & SF_SETGID))
2021da177e4SLinus Torvalds 				tail->gid = cpu_to_be16(gid);
2031da177e4SLinus Torvalds 		}
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds 	affs_fix_checksum(sb, bh);
2061da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
2071da177e4SLinus Torvalds 	affs_brelse(bh);
2081da177e4SLinus Torvalds 	affs_free_prealloc(inode);
2091da177e4SLinus Torvalds 	return 0;
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds int
2131da177e4SLinus Torvalds affs_notify_change(struct dentry *dentry, struct iattr *attr)
2141da177e4SLinus Torvalds {
215*2b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
2161da177e4SLinus Torvalds 	int error;
2171da177e4SLinus Torvalds 
2189606d9aaSFabian Frederick 	pr_debug("notify_change(%lu,0x%x)\n", inode->i_ino, attr->ia_valid);
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	error = inode_change_ok(inode,attr);
2211da177e4SLinus Torvalds 	if (error)
2221da177e4SLinus Torvalds 		goto out;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	if (((attr->ia_valid & ATTR_UID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETUID)) ||
2251da177e4SLinus Torvalds 	    ((attr->ia_valid & ATTR_GID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETGID)) ||
2261da177e4SLinus Torvalds 	    ((attr->ia_valid & ATTR_MODE) &&
2271da177e4SLinus Torvalds 	     (AFFS_SB(inode->i_sb)->s_flags & (SF_SETMODE | SF_IMMUTABLE)))) {
2281da177e4SLinus Torvalds 		if (!(AFFS_SB(inode->i_sb)->s_flags & SF_QUIET))
2291da177e4SLinus Torvalds 			error = -EPERM;
2301da177e4SLinus Torvalds 		goto out;
2311da177e4SLinus Torvalds 	}
2321da177e4SLinus Torvalds 
2331025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
2341025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
2351dc1834fSMarco Stornelli 		error = inode_newsize_ok(inode, attr->ia_size);
2361025774cSChristoph Hellwig 		if (error)
2371025774cSChristoph Hellwig 			return error;
2381dc1834fSMarco Stornelli 
2391dc1834fSMarco Stornelli 		truncate_setsize(inode, attr->ia_size);
2401dc1834fSMarco Stornelli 		affs_truncate(inode);
2411025774cSChristoph Hellwig 	}
2421025774cSChristoph Hellwig 
2431025774cSChristoph Hellwig 	setattr_copy(inode, attr);
2441025774cSChristoph Hellwig 	mark_inode_dirty(inode);
2451025774cSChristoph Hellwig 
2461025774cSChristoph Hellwig 	if (attr->ia_valid & ATTR_MODE)
2471da177e4SLinus Torvalds 		mode_to_prot(inode);
2481da177e4SLinus Torvalds out:
2491da177e4SLinus Torvalds 	return error;
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds void
253f053dddeSAl Viro affs_evict_inode(struct inode *inode)
2541da177e4SLinus Torvalds {
255dca3c336SRoman Zippel 	unsigned long cache_page;
2569606d9aaSFabian Frederick 	pr_debug("evict_inode(ino=%lu, nlink=%u)\n",
2579606d9aaSFabian Frederick 		 inode->i_ino, inode->i_nlink);
25891b0abe3SJohannes Weiner 	truncate_inode_pages_final(&inode->i_data);
2591da177e4SLinus Torvalds 
260f053dddeSAl Viro 	if (!inode->i_nlink) {
261f053dddeSAl Viro 		inode->i_size = 0;
262f053dddeSAl Viro 		affs_truncate(inode);
263f053dddeSAl Viro 	}
264dca3c336SRoman Zippel 
265f053dddeSAl Viro 	invalidate_inode_buffers(inode);
266dbd5768fSJan Kara 	clear_inode(inode);
267dca3c336SRoman Zippel 	affs_free_prealloc(inode);
268dca3c336SRoman Zippel 	cache_page = (unsigned long)AFFS_I(inode)->i_lc;
2691da177e4SLinus Torvalds 	if (cache_page) {
2709606d9aaSFabian Frederick 		pr_debug("freeing ext cache\n");
2711da177e4SLinus Torvalds 		AFFS_I(inode)->i_lc = NULL;
2721da177e4SLinus Torvalds 		AFFS_I(inode)->i_ac = NULL;
2731da177e4SLinus Torvalds 		free_page(cache_page);
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 	affs_brelse(AFFS_I(inode)->i_ext_bh);
2761da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_last = ~1;
2771da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_bh = NULL;
278f053dddeSAl Viro 
279f053dddeSAl Viro 	if (!inode->i_nlink)
280f053dddeSAl Viro 		affs_free_block(inode->i_sb, inode->i_ino);
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds struct inode *
2841da177e4SLinus Torvalds affs_new_inode(struct inode *dir)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 	struct super_block	*sb = dir->i_sb;
2871da177e4SLinus Torvalds 	struct inode		*inode;
2881da177e4SLinus Torvalds 	u32			 block;
2891da177e4SLinus Torvalds 	struct buffer_head	*bh;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	if (!(inode = new_inode(sb)))
2921da177e4SLinus Torvalds 		goto err_inode;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	if (!(block = affs_alloc_block(dir, dir->i_ino)))
2951da177e4SLinus Torvalds 		goto err_block;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	bh = affs_getzeroblk(sb, block);
2981da177e4SLinus Torvalds 	if (!bh)
2991da177e4SLinus Torvalds 		goto err_bh;
3001da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
3011da177e4SLinus Torvalds 	affs_brelse(bh);
3021da177e4SLinus Torvalds 
30321559981SDavid Howells 	inode->i_uid     = current_fsuid();
30421559981SDavid Howells 	inode->i_gid     = current_fsgid();
3051da177e4SLinus Torvalds 	inode->i_ino     = block;
306bfe86848SMiklos Szeredi 	set_nlink(inode, 1);
3071da177e4SLinus Torvalds 	inode->i_mtime   = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
308dca3c336SRoman Zippel 	atomic_set(&AFFS_I(inode)->i_opencnt, 0);
3091da177e4SLinus Torvalds 	AFFS_I(inode)->i_blkcnt = 0;
3101da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc = NULL;
3111da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_size = 0;
3121da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_shift = 0;
3131da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_mask = 0;
3141da177e4SLinus Torvalds 	AFFS_I(inode)->i_ac = NULL;
3151da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_bh = NULL;
3161da177e4SLinus Torvalds 	AFFS_I(inode)->mmu_private = 0;
3171da177e4SLinus Torvalds 	AFFS_I(inode)->i_protect = 0;
3181da177e4SLinus Torvalds 	AFFS_I(inode)->i_lastalloc = 0;
3191da177e4SLinus Torvalds 	AFFS_I(inode)->i_pa_cnt = 0;
3201da177e4SLinus Torvalds 	AFFS_I(inode)->i_extcnt = 1;
3211da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_last = ~1;
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	insert_inode_hash(inode);
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	return inode;
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds err_bh:
3281da177e4SLinus Torvalds 	affs_free_block(sb, block);
3291da177e4SLinus Torvalds err_block:
3301da177e4SLinus Torvalds 	iput(inode);
3311da177e4SLinus Torvalds err_inode:
3321da177e4SLinus Torvalds 	return NULL;
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds /*
3361da177e4SLinus Torvalds  * Add an entry to a directory. Create the header block
3371da177e4SLinus Torvalds  * and insert it into the hash table.
3381da177e4SLinus Torvalds  */
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds int
3411da177e4SLinus Torvalds affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type)
3421da177e4SLinus Torvalds {
3431da177e4SLinus Torvalds 	struct super_block *sb = dir->i_sb;
3441da177e4SLinus Torvalds 	struct buffer_head *inode_bh = NULL;
3451da177e4SLinus Torvalds 	struct buffer_head *bh = NULL;
3461da177e4SLinus Torvalds 	u32 block = 0;
3471da177e4SLinus Torvalds 	int retval;
3481da177e4SLinus Torvalds 
34908fe100dSGeert Uytterhoeven 	pr_debug("%s(dir=%lu, inode=%lu, \"%pd\", type=%d)\n", __func__,
35008fe100dSGeert Uytterhoeven 		 dir->i_ino, inode->i_ino, dentry, type);
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	retval = -EIO;
3531da177e4SLinus Torvalds 	bh = affs_bread(sb, inode->i_ino);
3541da177e4SLinus Torvalds 	if (!bh)
3551da177e4SLinus Torvalds 		goto done;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	affs_lock_link(inode);
3581da177e4SLinus Torvalds 	switch (type) {
3591da177e4SLinus Torvalds 	case ST_LINKFILE:
3601da177e4SLinus Torvalds 	case ST_LINKDIR:
3611da177e4SLinus Torvalds 		retval = -ENOSPC;
3621da177e4SLinus Torvalds 		block = affs_alloc_block(dir, dir->i_ino);
3631da177e4SLinus Torvalds 		if (!block)
3641da177e4SLinus Torvalds 			goto err;
3651da177e4SLinus Torvalds 		retval = -EIO;
366dca3c336SRoman Zippel 		inode_bh = bh;
3671da177e4SLinus Torvalds 		bh = affs_getzeroblk(sb, block);
3681da177e4SLinus Torvalds 		if (!bh)
3691da177e4SLinus Torvalds 			goto err;
3701da177e4SLinus Torvalds 		break;
3711da177e4SLinus Torvalds 	default:
3721da177e4SLinus Torvalds 		break;
3731da177e4SLinus Torvalds 	}
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT);
3761da177e4SLinus Torvalds 	AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr);
3771da177e4SLinus Torvalds 	affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry);
3781da177e4SLinus Torvalds 	AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type);
3791da177e4SLinus Torvalds 	AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	if (inode_bh) {
3821da177e4SLinus Torvalds 		__be32 chain;
3831da177e4SLinus Torvalds 	       	chain = AFFS_TAIL(sb, inode_bh)->link_chain;
3841da177e4SLinus Torvalds 		AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino);
3851da177e4SLinus Torvalds 		AFFS_TAIL(sb, bh)->link_chain = chain;
3861da177e4SLinus Torvalds 		AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block);
3871da177e4SLinus Torvalds 		affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain));
3881da177e4SLinus Torvalds 		mark_buffer_dirty_inode(inode_bh, inode);
389bfe86848SMiklos Szeredi 		set_nlink(inode, 2);
3907de9c6eeSAl Viro 		ihold(inode);
3911da177e4SLinus Torvalds 	}
3921da177e4SLinus Torvalds 	affs_fix_checksum(sb, bh);
3931da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
3941da177e4SLinus Torvalds 	dentry->d_fsdata = (void *)(long)bh->b_blocknr;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	affs_lock_dir(dir);
3971da177e4SLinus Torvalds 	retval = affs_insert_hash(dir, bh);
3981da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
3991da177e4SLinus Torvalds 	affs_unlock_dir(dir);
4001da177e4SLinus Torvalds 	affs_unlock_link(inode);
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
4031da177e4SLinus Torvalds done:
4041da177e4SLinus Torvalds 	affs_brelse(inode_bh);
4051da177e4SLinus Torvalds 	affs_brelse(bh);
4061da177e4SLinus Torvalds 	return retval;
4071da177e4SLinus Torvalds err:
4081da177e4SLinus Torvalds 	if (block)
4091da177e4SLinus Torvalds 		affs_free_block(sb, block);
4101da177e4SLinus Torvalds 	affs_unlock_link(inode);
4111da177e4SLinus Torvalds 	goto done;
4121da177e4SLinus Torvalds }
413