xref: /openbmc/linux/fs/affs/inode.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/affs/inode.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  (c) 1996  Hans-Joachim Widmaier - Rewritten
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  (C) 1993  Ray Burr - Modified for Amiga FFS filesystem.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  (C) 1992  Eric Youngdale Modified for ISO9660 filesystem.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *  (C) 1991  Linus Torvalds - minix filesystem
121da177e4SLinus Torvalds  */
13e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
145b825c3aSIngo Molnar #include <linux/cred.h>
155a0e3ad6STejun Heo #include <linux/gfp.h>
161da177e4SLinus Torvalds #include "affs.h"
171da177e4SLinus Torvalds 
affs_iget(struct super_block * sb,unsigned long ino)18210f8559SDavid Howells struct inode *affs_iget(struct super_block *sb, unsigned long ino)
191da177e4SLinus Torvalds {
201da177e4SLinus Torvalds 	struct affs_sb_info	*sbi = AFFS_SB(sb);
211da177e4SLinus Torvalds 	struct buffer_head	*bh;
221da177e4SLinus Torvalds 	struct affs_tail	*tail;
23210f8559SDavid Howells 	struct inode		*inode;
241da177e4SLinus Torvalds 	u32			 block;
251da177e4SLinus Torvalds 	u32			 size;
261da177e4SLinus Torvalds 	u32			 prot;
271da177e4SLinus Torvalds 	u16			 id;
281da177e4SLinus Torvalds 
29210f8559SDavid Howells 	inode = iget_locked(sb, ino);
30210f8559SDavid Howells 	if (!inode)
31210f8559SDavid Howells 		return ERR_PTR(-ENOMEM);
32210f8559SDavid Howells 	if (!(inode->i_state & I_NEW))
33210f8559SDavid Howells 		return inode;
34210f8559SDavid Howells 
359606d9aaSFabian Frederick 	pr_debug("affs_iget(%lu)\n", inode->i_ino);
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds 	block = inode->i_ino;
381da177e4SLinus Torvalds 	bh = affs_bread(sb, block);
391da177e4SLinus Torvalds 	if (!bh) {
401da177e4SLinus Torvalds 		affs_warning(sb, "read_inode", "Cannot read block %d", block);
411da177e4SLinus Torvalds 		goto bad_inode;
421da177e4SLinus Torvalds 	}
431da177e4SLinus Torvalds 	if (affs_checksum_block(sb, bh) || be32_to_cpu(AFFS_HEAD(bh)->ptype) != T_SHORT) {
441da177e4SLinus Torvalds 		affs_warning(sb,"read_inode",
451da177e4SLinus Torvalds 			   "Checksum or type (ptype=%d) error on inode %d",
461da177e4SLinus Torvalds 			   AFFS_HEAD(bh)->ptype, block);
471da177e4SLinus Torvalds 		goto bad_inode;
481da177e4SLinus Torvalds 	}
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 	tail = AFFS_TAIL(sb, bh);
511da177e4SLinus Torvalds 	prot = be32_to_cpu(tail->protect);
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	inode->i_size = 0;
54bfe86848SMiklos Szeredi 	set_nlink(inode, 1);
551da177e4SLinus Torvalds 	inode->i_mode = 0;
561da177e4SLinus Torvalds 	AFFS_I(inode)->i_extcnt = 1;
571da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_last = ~1;
581da177e4SLinus Torvalds 	AFFS_I(inode)->i_protect = prot;
59dca3c336SRoman Zippel 	atomic_set(&AFFS_I(inode)->i_opencnt, 0);
601da177e4SLinus Torvalds 	AFFS_I(inode)->i_blkcnt = 0;
611da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc = NULL;
621da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_size = 0;
631da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_shift = 0;
641da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_mask = 0;
651da177e4SLinus Torvalds 	AFFS_I(inode)->i_ac = NULL;
661da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_bh = NULL;
671da177e4SLinus Torvalds 	AFFS_I(inode)->mmu_private = 0;
681da177e4SLinus Torvalds 	AFFS_I(inode)->i_lastalloc = 0;
691da177e4SLinus Torvalds 	AFFS_I(inode)->i_pa_cnt = 0;
701da177e4SLinus Torvalds 
7179bda4d5SFabian Frederick 	if (affs_test_opt(sbi->s_flags, SF_SETMODE))
721da177e4SLinus Torvalds 		inode->i_mode = sbi->s_mode;
731da177e4SLinus Torvalds 	else
74c1618208SFabian Frederick 		inode->i_mode = affs_prot_to_mode(prot);
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	id = be16_to_cpu(tail->uid);
7779bda4d5SFabian Frederick 	if (id == 0 || affs_test_opt(sbi->s_flags, SF_SETUID))
781da177e4SLinus Torvalds 		inode->i_uid = sbi->s_uid;
7979bda4d5SFabian Frederick 	else if (id == 0xFFFF && affs_test_opt(sbi->s_flags, SF_MUFS))
808fed10beSEric W. Biederman 		i_uid_write(inode, 0);
811da177e4SLinus Torvalds 	else
828fed10beSEric W. Biederman 		i_uid_write(inode, id);
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	id = be16_to_cpu(tail->gid);
8579bda4d5SFabian Frederick 	if (id == 0 || affs_test_opt(sbi->s_flags, SF_SETGID))
861da177e4SLinus Torvalds 		inode->i_gid = sbi->s_gid;
8779bda4d5SFabian Frederick 	else if (id == 0xFFFF && affs_test_opt(sbi->s_flags, SF_MUFS))
888fed10beSEric W. Biederman 		i_gid_write(inode, 0);
891da177e4SLinus Torvalds 	else
908fed10beSEric W. Biederman 		i_gid_write(inode, id);
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	switch (be32_to_cpu(tail->stype)) {
931da177e4SLinus Torvalds 	case ST_ROOT:
941da177e4SLinus Torvalds 		inode->i_uid = sbi->s_uid;
951da177e4SLinus Torvalds 		inode->i_gid = sbi->s_gid;
96df561f66SGustavo A. R. Silva 		fallthrough;
971da177e4SLinus Torvalds 	case ST_USERDIR:
981da177e4SLinus Torvalds 		if (be32_to_cpu(tail->stype) == ST_USERDIR ||
9979bda4d5SFabian Frederick 		    affs_test_opt(sbi->s_flags, SF_SETMODE)) {
1001da177e4SLinus Torvalds 			if (inode->i_mode & S_IRUSR)
1011da177e4SLinus Torvalds 				inode->i_mode |= S_IXUSR;
1021da177e4SLinus Torvalds 			if (inode->i_mode & S_IRGRP)
1031da177e4SLinus Torvalds 				inode->i_mode |= S_IXGRP;
1041da177e4SLinus Torvalds 			if (inode->i_mode & S_IROTH)
1051da177e4SLinus Torvalds 				inode->i_mode |= S_IXOTH;
1061da177e4SLinus Torvalds 			inode->i_mode |= S_IFDIR;
1071da177e4SLinus Torvalds 		} else
1081da177e4SLinus Torvalds 			inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
1091da177e4SLinus Torvalds 		/* Maybe it should be controlled by mount parameter? */
1101da177e4SLinus Torvalds 		//inode->i_mode |= S_ISVTX;
1111da177e4SLinus Torvalds 		inode->i_op = &affs_dir_inode_operations;
1121da177e4SLinus Torvalds 		inode->i_fop = &affs_dir_operations;
1131da177e4SLinus Torvalds 		break;
1141da177e4SLinus Torvalds 	case ST_LINKDIR:
1151da177e4SLinus Torvalds #if 0
1161da177e4SLinus Torvalds 		affs_warning(sb, "read_inode", "inode is LINKDIR");
1171da177e4SLinus Torvalds 		goto bad_inode;
1181da177e4SLinus Torvalds #else
1191da177e4SLinus Torvalds 		inode->i_mode |= S_IFDIR;
120c765d479SAl Viro 		/* ... and leave ->i_op and ->i_fop pointing to empty */
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)
137bfe86848SMiklos Szeredi 			set_nlink(inode, 2);
13879bda4d5SFabian Frederick 		inode->i_mapping->a_ops = affs_test_opt(sbi->s_flags, SF_OFS) ?
139a0016ff2SFabian Frederick 					  &affs_aops_ofs : &affs_aops;
1401da177e4SLinus Torvalds 		inode->i_op = &affs_file_inode_operations;
1411da177e4SLinus Torvalds 		inode->i_fop = &affs_file_operations;
1421da177e4SLinus Torvalds 		break;
1431da177e4SLinus Torvalds 	case ST_SOFTLINK:
144f1bf9072SFabian Frederick 		inode->i_size = strlen((char *)AFFS_HEAD(bh)->table);
1451da177e4SLinus Torvalds 		inode->i_mode |= S_IFLNK;
14621fc61c7SAl Viro 		inode_nohighmem(inode);
1471da177e4SLinus Torvalds 		inode->i_op = &affs_symlink_inode_operations;
1481da177e4SLinus Torvalds 		inode->i_data.a_ops = &affs_symlink_aops;
1491da177e4SLinus Torvalds 		break;
1501da177e4SLinus Torvalds 	}
1511da177e4SLinus Torvalds 
152*f873e9d6SJeff Layton 	inode->i_mtime.tv_sec = inode->i_atime.tv_sec =
153*f873e9d6SJeff Layton 		inode_set_ctime(inode,
154*f873e9d6SJeff Layton 				(be32_to_cpu(tail->change.days) * 86400LL +
1551da177e4SLinus Torvalds 				 be32_to_cpu(tail->change.mins) * 60 +
156*f873e9d6SJeff Layton 				 be32_to_cpu(tail->change.ticks) / 50 + AFFS_EPOCH_DELTA)
157*f873e9d6SJeff Layton 				+ sys_tz.tz_minuteswest * 60, 0).tv_sec;
158*f873e9d6SJeff Layton 	inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = 0;
1591da177e4SLinus Torvalds 	affs_brelse(bh);
160210f8559SDavid Howells 	unlock_new_inode(inode);
161210f8559SDavid Howells 	return inode;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds bad_inode:
1641da177e4SLinus Torvalds 	affs_brelse(bh);
165210f8559SDavid Howells 	iget_failed(inode);
166210f8559SDavid Howells 	return ERR_PTR(-EIO);
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds int
affs_write_inode(struct inode * inode,struct writeback_control * wbc)170a9185b41SChristoph Hellwig affs_write_inode(struct inode *inode, struct writeback_control *wbc)
1711da177e4SLinus Torvalds {
1721da177e4SLinus Torvalds 	struct super_block	*sb = inode->i_sb;
1731da177e4SLinus Torvalds 	struct buffer_head	*bh;
1741da177e4SLinus Torvalds 	struct affs_tail	*tail;
1751da177e4SLinus Torvalds 	uid_t			 uid;
1761da177e4SLinus Torvalds 	gid_t			 gid;
1771da177e4SLinus Torvalds 
1789606d9aaSFabian Frederick 	pr_debug("write_inode(%lu)\n", inode->i_ino);
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	if (!inode->i_nlink)
1811da177e4SLinus Torvalds 		// possibly free block
1821da177e4SLinus Torvalds 		return 0;
1831da177e4SLinus Torvalds 	bh = affs_bread(sb, inode->i_ino);
1841da177e4SLinus Torvalds 	if (!bh) {
1851da177e4SLinus Torvalds 		affs_error(sb,"write_inode","Cannot read block %lu",inode->i_ino);
1861da177e4SLinus Torvalds 		return -EIO;
1871da177e4SLinus Torvalds 	}
1881da177e4SLinus Torvalds 	tail = AFFS_TAIL(sb, bh);
1891da177e4SLinus Torvalds 	if (tail->stype == cpu_to_be32(ST_ROOT)) {
190c1618208SFabian Frederick 		affs_secs_to_datestamp(inode->i_mtime.tv_sec,
191c1618208SFabian Frederick 				       &AFFS_ROOT_TAIL(sb, bh)->root_change);
1921da177e4SLinus Torvalds 	} else {
1931da177e4SLinus Torvalds 		tail->protect = cpu_to_be32(AFFS_I(inode)->i_protect);
1941da177e4SLinus Torvalds 		tail->size = cpu_to_be32(inode->i_size);
195c1618208SFabian Frederick 		affs_secs_to_datestamp(inode->i_mtime.tv_sec, &tail->change);
1961da177e4SLinus Torvalds 		if (!(inode->i_ino == AFFS_SB(sb)->s_root_block)) {
1978fed10beSEric W. Biederman 			uid = i_uid_read(inode);
1988fed10beSEric W. Biederman 			gid = i_gid_read(inode);
19979bda4d5SFabian Frederick 			if (affs_test_opt(AFFS_SB(sb)->s_flags, SF_MUFS)) {
2008fed10beSEric W. Biederman 				if (uid == 0 || uid == 0xFFFF)
2018fed10beSEric W. Biederman 					uid = uid ^ ~0;
2028fed10beSEric W. Biederman 				if (gid == 0 || gid == 0xFFFF)
2038fed10beSEric W. Biederman 					gid = gid ^ ~0;
2041da177e4SLinus Torvalds 			}
20579bda4d5SFabian Frederick 			if (!affs_test_opt(AFFS_SB(sb)->s_flags, SF_SETUID))
2061da177e4SLinus Torvalds 				tail->uid = cpu_to_be16(uid);
20779bda4d5SFabian Frederick 			if (!affs_test_opt(AFFS_SB(sb)->s_flags, SF_SETGID))
2081da177e4SLinus Torvalds 				tail->gid = cpu_to_be16(gid);
2091da177e4SLinus Torvalds 		}
2101da177e4SLinus Torvalds 	}
2111da177e4SLinus Torvalds 	affs_fix_checksum(sb, bh);
2121da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
2131da177e4SLinus Torvalds 	affs_brelse(bh);
2141da177e4SLinus Torvalds 	affs_free_prealloc(inode);
2151da177e4SLinus Torvalds 	return 0;
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds int
affs_notify_change(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)219c1632a0fSChristian Brauner affs_notify_change(struct mnt_idmap *idmap, struct dentry *dentry,
220549c7297SChristian Brauner 		   struct iattr *attr)
2211da177e4SLinus Torvalds {
2222b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
2231da177e4SLinus Torvalds 	int error;
2241da177e4SLinus Torvalds 
2259606d9aaSFabian Frederick 	pr_debug("notify_change(%lu,0x%x)\n", inode->i_ino, attr->ia_valid);
2261da177e4SLinus Torvalds 
227c1632a0fSChristian Brauner 	error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
2281da177e4SLinus Torvalds 	if (error)
2291da177e4SLinus Torvalds 		goto out;
2301da177e4SLinus Torvalds 
231a0016ff2SFabian Frederick 	if (((attr->ia_valid & ATTR_UID) &&
23279bda4d5SFabian Frederick 	      affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_SETUID)) ||
233a0016ff2SFabian Frederick 	    ((attr->ia_valid & ATTR_GID) &&
23479bda4d5SFabian Frederick 	      affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_SETGID)) ||
2351da177e4SLinus Torvalds 	    ((attr->ia_valid & ATTR_MODE) &&
236a0016ff2SFabian Frederick 	     (AFFS_SB(inode->i_sb)->s_flags &
237a0016ff2SFabian Frederick 	      (AFFS_MOUNT_SF_SETMODE | AFFS_MOUNT_SF_IMMUTABLE)))) {
23879bda4d5SFabian Frederick 		if (!affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_QUIET))
2391da177e4SLinus Torvalds 			error = -EPERM;
2401da177e4SLinus Torvalds 		goto out;
2411da177e4SLinus Torvalds 	}
2421da177e4SLinus Torvalds 
2431025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
2441025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
2451dc1834fSMarco Stornelli 		error = inode_newsize_ok(inode, attr->ia_size);
2461025774cSChristoph Hellwig 		if (error)
2471025774cSChristoph Hellwig 			return error;
2481dc1834fSMarco Stornelli 
2491dc1834fSMarco Stornelli 		truncate_setsize(inode, attr->ia_size);
2501dc1834fSMarco Stornelli 		affs_truncate(inode);
2511025774cSChristoph Hellwig 	}
2521025774cSChristoph Hellwig 
253c1632a0fSChristian Brauner 	setattr_copy(&nop_mnt_idmap, inode, attr);
2541025774cSChristoph Hellwig 	mark_inode_dirty(inode);
2551025774cSChristoph Hellwig 
2561025774cSChristoph Hellwig 	if (attr->ia_valid & ATTR_MODE)
257c1618208SFabian Frederick 		affs_mode_to_prot(inode);
2581da177e4SLinus Torvalds out:
2591da177e4SLinus Torvalds 	return error;
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds void
affs_evict_inode(struct inode * inode)263f053dddeSAl Viro affs_evict_inode(struct inode *inode)
2641da177e4SLinus Torvalds {
265dca3c336SRoman Zippel 	unsigned long cache_page;
2669606d9aaSFabian Frederick 	pr_debug("evict_inode(ino=%lu, nlink=%u)\n",
2679606d9aaSFabian Frederick 		 inode->i_ino, inode->i_nlink);
26891b0abe3SJohannes Weiner 	truncate_inode_pages_final(&inode->i_data);
2691da177e4SLinus Torvalds 
270f053dddeSAl Viro 	if (!inode->i_nlink) {
271f053dddeSAl Viro 		inode->i_size = 0;
272f053dddeSAl Viro 		affs_truncate(inode);
273f053dddeSAl Viro 	}
274dca3c336SRoman Zippel 
275f053dddeSAl Viro 	invalidate_inode_buffers(inode);
276dbd5768fSJan Kara 	clear_inode(inode);
277dca3c336SRoman Zippel 	affs_free_prealloc(inode);
278dca3c336SRoman Zippel 	cache_page = (unsigned long)AFFS_I(inode)->i_lc;
2791da177e4SLinus Torvalds 	if (cache_page) {
2809606d9aaSFabian Frederick 		pr_debug("freeing ext cache\n");
2811da177e4SLinus Torvalds 		AFFS_I(inode)->i_lc = NULL;
2821da177e4SLinus Torvalds 		AFFS_I(inode)->i_ac = NULL;
2831da177e4SLinus Torvalds 		free_page(cache_page);
2841da177e4SLinus Torvalds 	}
2851da177e4SLinus Torvalds 	affs_brelse(AFFS_I(inode)->i_ext_bh);
2861da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_last = ~1;
2871da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_bh = NULL;
288f053dddeSAl Viro 
289f053dddeSAl Viro 	if (!inode->i_nlink)
290f053dddeSAl Viro 		affs_free_block(inode->i_sb, inode->i_ino);
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds struct inode *
affs_new_inode(struct inode * dir)2941da177e4SLinus Torvalds affs_new_inode(struct inode *dir)
2951da177e4SLinus Torvalds {
2961da177e4SLinus Torvalds 	struct super_block	*sb = dir->i_sb;
2971da177e4SLinus Torvalds 	struct inode		*inode;
2981da177e4SLinus Torvalds 	u32			 block;
2991da177e4SLinus Torvalds 	struct buffer_head	*bh;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	if (!(inode = new_inode(sb)))
3021da177e4SLinus Torvalds 		goto err_inode;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	if (!(block = affs_alloc_block(dir, dir->i_ino)))
3051da177e4SLinus Torvalds 		goto err_block;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	bh = affs_getzeroblk(sb, block);
3081da177e4SLinus Torvalds 	if (!bh)
3091da177e4SLinus Torvalds 		goto err_bh;
3101da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
3111da177e4SLinus Torvalds 	affs_brelse(bh);
3121da177e4SLinus Torvalds 
31321559981SDavid Howells 	inode->i_uid     = current_fsuid();
31421559981SDavid Howells 	inode->i_gid     = current_fsgid();
3151da177e4SLinus Torvalds 	inode->i_ino     = block;
316bfe86848SMiklos Szeredi 	set_nlink(inode, 1);
317*f873e9d6SJeff Layton 	inode->i_mtime   = inode->i_atime = inode_set_ctime_current(inode);
318dca3c336SRoman Zippel 	atomic_set(&AFFS_I(inode)->i_opencnt, 0);
3191da177e4SLinus Torvalds 	AFFS_I(inode)->i_blkcnt = 0;
3201da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc = NULL;
3211da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_size = 0;
3221da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_shift = 0;
3231da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_mask = 0;
3241da177e4SLinus Torvalds 	AFFS_I(inode)->i_ac = NULL;
3251da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_bh = NULL;
3261da177e4SLinus Torvalds 	AFFS_I(inode)->mmu_private = 0;
3271da177e4SLinus Torvalds 	AFFS_I(inode)->i_protect = 0;
3281da177e4SLinus Torvalds 	AFFS_I(inode)->i_lastalloc = 0;
3291da177e4SLinus Torvalds 	AFFS_I(inode)->i_pa_cnt = 0;
3301da177e4SLinus Torvalds 	AFFS_I(inode)->i_extcnt = 1;
3311da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_last = ~1;
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	insert_inode_hash(inode);
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	return inode;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds err_bh:
3381da177e4SLinus Torvalds 	affs_free_block(sb, block);
3391da177e4SLinus Torvalds err_block:
3401da177e4SLinus Torvalds 	iput(inode);
3411da177e4SLinus Torvalds err_inode:
3421da177e4SLinus Torvalds 	return NULL;
3431da177e4SLinus Torvalds }
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds /*
3461da177e4SLinus Torvalds  * Add an entry to a directory. Create the header block
3471da177e4SLinus Torvalds  * and insert it into the hash table.
3481da177e4SLinus Torvalds  */
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds int
affs_add_entry(struct inode * dir,struct inode * inode,struct dentry * dentry,s32 type)3511da177e4SLinus Torvalds affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type)
3521da177e4SLinus Torvalds {
3531da177e4SLinus Torvalds 	struct super_block *sb = dir->i_sb;
3541da177e4SLinus Torvalds 	struct buffer_head *inode_bh = NULL;
35578f444f6SFabian Frederick 	struct buffer_head *bh;
3561da177e4SLinus Torvalds 	u32 block = 0;
3571da177e4SLinus Torvalds 	int retval;
3581da177e4SLinus Torvalds 
35908fe100dSGeert Uytterhoeven 	pr_debug("%s(dir=%lu, inode=%lu, \"%pd\", type=%d)\n", __func__,
36008fe100dSGeert Uytterhoeven 		 dir->i_ino, inode->i_ino, dentry, type);
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	retval = -EIO;
3631da177e4SLinus Torvalds 	bh = affs_bread(sb, inode->i_ino);
3641da177e4SLinus Torvalds 	if (!bh)
3651da177e4SLinus Torvalds 		goto done;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	affs_lock_link(inode);
3681da177e4SLinus Torvalds 	switch (type) {
3691da177e4SLinus Torvalds 	case ST_LINKFILE:
3701da177e4SLinus Torvalds 	case ST_LINKDIR:
3711da177e4SLinus Torvalds 		retval = -ENOSPC;
3721da177e4SLinus Torvalds 		block = affs_alloc_block(dir, dir->i_ino);
3731da177e4SLinus Torvalds 		if (!block)
3741da177e4SLinus Torvalds 			goto err;
3751da177e4SLinus Torvalds 		retval = -EIO;
376dca3c336SRoman Zippel 		inode_bh = bh;
3771da177e4SLinus Torvalds 		bh = affs_getzeroblk(sb, block);
3781da177e4SLinus Torvalds 		if (!bh)
3791da177e4SLinus Torvalds 			goto err;
3801da177e4SLinus Torvalds 		break;
3811da177e4SLinus Torvalds 	default:
3821da177e4SLinus Torvalds 		break;
3831da177e4SLinus Torvalds 	}
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT);
3861da177e4SLinus Torvalds 	AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr);
3871da177e4SLinus Torvalds 	affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry);
3881da177e4SLinus Torvalds 	AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type);
3891da177e4SLinus Torvalds 	AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	if (inode_bh) {
3921da177e4SLinus Torvalds 		__be32 chain;
3931da177e4SLinus Torvalds 	       	chain = AFFS_TAIL(sb, inode_bh)->link_chain;
3941da177e4SLinus Torvalds 		AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino);
3951da177e4SLinus Torvalds 		AFFS_TAIL(sb, bh)->link_chain = chain;
3961da177e4SLinus Torvalds 		AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block);
3971da177e4SLinus Torvalds 		affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain));
3981da177e4SLinus Torvalds 		mark_buffer_dirty_inode(inode_bh, inode);
399bfe86848SMiklos Szeredi 		set_nlink(inode, 2);
4007de9c6eeSAl Viro 		ihold(inode);
4011da177e4SLinus Torvalds 	}
4021da177e4SLinus Torvalds 	affs_fix_checksum(sb, bh);
4031da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
4041da177e4SLinus Torvalds 	dentry->d_fsdata = (void *)(long)bh->b_blocknr;
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	affs_lock_dir(dir);
4071da177e4SLinus Torvalds 	retval = affs_insert_hash(dir, bh);
4081da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
4091da177e4SLinus Torvalds 	affs_unlock_dir(dir);
4101da177e4SLinus Torvalds 	affs_unlock_link(inode);
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
4131da177e4SLinus Torvalds done:
4141da177e4SLinus Torvalds 	affs_brelse(inode_bh);
4151da177e4SLinus Torvalds 	affs_brelse(bh);
4161da177e4SLinus Torvalds 	return retval;
4171da177e4SLinus Torvalds err:
4181da177e4SLinus Torvalds 	if (block)
4191da177e4SLinus Torvalds 		affs_free_block(sb, block);
4201da177e4SLinus Torvalds 	affs_unlock_link(inode);
4211da177e4SLinus Torvalds 	goto done;
4221da177e4SLinus Torvalds }
423