xref: /openbmc/linux/fs/affs/inode.c (revision c1618208511f070883a3d079ef7f78024e7faf90)
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 
6979bda4d5SFabian Frederick 	if (affs_test_opt(sbi->s_flags, SF_SETMODE))
701da177e4SLinus Torvalds 		inode->i_mode = sbi->s_mode;
711da177e4SLinus Torvalds 	else
72*c1618208SFabian Frederick 		inode->i_mode = affs_prot_to_mode(prot);
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	id = be16_to_cpu(tail->uid);
7579bda4d5SFabian Frederick 	if (id == 0 || affs_test_opt(sbi->s_flags, SF_SETUID))
761da177e4SLinus Torvalds 		inode->i_uid = sbi->s_uid;
7779bda4d5SFabian Frederick 	else if (id == 0xFFFF && affs_test_opt(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);
8379bda4d5SFabian Frederick 	if (id == 0 || affs_test_opt(sbi->s_flags, SF_SETGID))
841da177e4SLinus Torvalds 		inode->i_gid = sbi->s_gid;
8579bda4d5SFabian Frederick 	else if (id == 0xFFFF && affs_test_opt(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 ||
9779bda4d5SFabian Frederick 		    affs_test_opt(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);
13679bda4d5SFabian Frederick 		inode->i_mapping->a_ops = affs_test_opt(sbi->s_flags, SF_OFS) ?
137a0016ff2SFabian Frederick 					  &affs_aops_ofs : &affs_aops;
1381da177e4SLinus Torvalds 		inode->i_op = &affs_file_inode_operations;
1391da177e4SLinus Torvalds 		inode->i_fop = &affs_file_operations;
1401da177e4SLinus Torvalds 		break;
1411da177e4SLinus Torvalds 	case ST_SOFTLINK:
1421da177e4SLinus Torvalds 		inode->i_mode |= S_IFLNK;
14321fc61c7SAl Viro 		inode_nohighmem(inode);
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);
157210f8559SDavid Howells 	unlock_new_inode(inode);
158210f8559SDavid Howells 	return inode;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds bad_inode:
1611da177e4SLinus Torvalds 	affs_brelse(bh);
162210f8559SDavid Howells 	iget_failed(inode);
163210f8559SDavid Howells 	return ERR_PTR(-EIO);
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds int
167a9185b41SChristoph Hellwig affs_write_inode(struct inode *inode, struct writeback_control *wbc)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	struct super_block	*sb = inode->i_sb;
1701da177e4SLinus Torvalds 	struct buffer_head	*bh;
1711da177e4SLinus Torvalds 	struct affs_tail	*tail;
1721da177e4SLinus Torvalds 	uid_t			 uid;
1731da177e4SLinus Torvalds 	gid_t			 gid;
1741da177e4SLinus Torvalds 
1759606d9aaSFabian Frederick 	pr_debug("write_inode(%lu)\n", inode->i_ino);
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	if (!inode->i_nlink)
1781da177e4SLinus Torvalds 		// possibly free block
1791da177e4SLinus Torvalds 		return 0;
1801da177e4SLinus Torvalds 	bh = affs_bread(sb, inode->i_ino);
1811da177e4SLinus Torvalds 	if (!bh) {
1821da177e4SLinus Torvalds 		affs_error(sb,"write_inode","Cannot read block %lu",inode->i_ino);
1831da177e4SLinus Torvalds 		return -EIO;
1841da177e4SLinus Torvalds 	}
1851da177e4SLinus Torvalds 	tail = AFFS_TAIL(sb, bh);
1861da177e4SLinus Torvalds 	if (tail->stype == cpu_to_be32(ST_ROOT)) {
187*c1618208SFabian Frederick 		affs_secs_to_datestamp(inode->i_mtime.tv_sec,
188*c1618208SFabian Frederick 				       &AFFS_ROOT_TAIL(sb, bh)->root_change);
1891da177e4SLinus Torvalds 	} else {
1901da177e4SLinus Torvalds 		tail->protect = cpu_to_be32(AFFS_I(inode)->i_protect);
1911da177e4SLinus Torvalds 		tail->size = cpu_to_be32(inode->i_size);
192*c1618208SFabian Frederick 		affs_secs_to_datestamp(inode->i_mtime.tv_sec, &tail->change);
1931da177e4SLinus Torvalds 		if (!(inode->i_ino == AFFS_SB(sb)->s_root_block)) {
1948fed10beSEric W. Biederman 			uid = i_uid_read(inode);
1958fed10beSEric W. Biederman 			gid = i_gid_read(inode);
19679bda4d5SFabian Frederick 			if (affs_test_opt(AFFS_SB(sb)->s_flags, SF_MUFS)) {
1978fed10beSEric W. Biederman 				if (uid == 0 || uid == 0xFFFF)
1988fed10beSEric W. Biederman 					uid = uid ^ ~0;
1998fed10beSEric W. Biederman 				if (gid == 0 || gid == 0xFFFF)
2008fed10beSEric W. Biederman 					gid = gid ^ ~0;
2011da177e4SLinus Torvalds 			}
20279bda4d5SFabian Frederick 			if (!affs_test_opt(AFFS_SB(sb)->s_flags, SF_SETUID))
2031da177e4SLinus Torvalds 				tail->uid = cpu_to_be16(uid);
20479bda4d5SFabian Frederick 			if (!affs_test_opt(AFFS_SB(sb)->s_flags, SF_SETGID))
2051da177e4SLinus Torvalds 				tail->gid = cpu_to_be16(gid);
2061da177e4SLinus Torvalds 		}
2071da177e4SLinus Torvalds 	}
2081da177e4SLinus Torvalds 	affs_fix_checksum(sb, bh);
2091da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
2101da177e4SLinus Torvalds 	affs_brelse(bh);
2111da177e4SLinus Torvalds 	affs_free_prealloc(inode);
2121da177e4SLinus Torvalds 	return 0;
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds int
2161da177e4SLinus Torvalds affs_notify_change(struct dentry *dentry, struct iattr *attr)
2171da177e4SLinus Torvalds {
2182b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
2191da177e4SLinus Torvalds 	int error;
2201da177e4SLinus Torvalds 
2219606d9aaSFabian Frederick 	pr_debug("notify_change(%lu,0x%x)\n", inode->i_ino, attr->ia_valid);
2221da177e4SLinus Torvalds 
22331051c85SJan Kara 	error = setattr_prepare(dentry, attr);
2241da177e4SLinus Torvalds 	if (error)
2251da177e4SLinus Torvalds 		goto out;
2261da177e4SLinus Torvalds 
227a0016ff2SFabian Frederick 	if (((attr->ia_valid & ATTR_UID) &&
22879bda4d5SFabian Frederick 	      affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_SETUID)) ||
229a0016ff2SFabian Frederick 	    ((attr->ia_valid & ATTR_GID) &&
23079bda4d5SFabian Frederick 	      affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_SETGID)) ||
2311da177e4SLinus Torvalds 	    ((attr->ia_valid & ATTR_MODE) &&
232a0016ff2SFabian Frederick 	     (AFFS_SB(inode->i_sb)->s_flags &
233a0016ff2SFabian Frederick 	      (AFFS_MOUNT_SF_SETMODE | AFFS_MOUNT_SF_IMMUTABLE)))) {
23479bda4d5SFabian Frederick 		if (!affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_QUIET))
2351da177e4SLinus Torvalds 			error = -EPERM;
2361da177e4SLinus Torvalds 		goto out;
2371da177e4SLinus Torvalds 	}
2381da177e4SLinus Torvalds 
2391025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
2401025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
2411dc1834fSMarco Stornelli 		error = inode_newsize_ok(inode, attr->ia_size);
2421025774cSChristoph Hellwig 		if (error)
2431025774cSChristoph Hellwig 			return error;
2441dc1834fSMarco Stornelli 
2451dc1834fSMarco Stornelli 		truncate_setsize(inode, attr->ia_size);
2461dc1834fSMarco Stornelli 		affs_truncate(inode);
2471025774cSChristoph Hellwig 	}
2481025774cSChristoph Hellwig 
2491025774cSChristoph Hellwig 	setattr_copy(inode, attr);
2501025774cSChristoph Hellwig 	mark_inode_dirty(inode);
2511025774cSChristoph Hellwig 
2521025774cSChristoph Hellwig 	if (attr->ia_valid & ATTR_MODE)
253*c1618208SFabian Frederick 		affs_mode_to_prot(inode);
2541da177e4SLinus Torvalds out:
2551da177e4SLinus Torvalds 	return error;
2561da177e4SLinus Torvalds }
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds void
259f053dddeSAl Viro affs_evict_inode(struct inode *inode)
2601da177e4SLinus Torvalds {
261dca3c336SRoman Zippel 	unsigned long cache_page;
2629606d9aaSFabian Frederick 	pr_debug("evict_inode(ino=%lu, nlink=%u)\n",
2639606d9aaSFabian Frederick 		 inode->i_ino, inode->i_nlink);
26491b0abe3SJohannes Weiner 	truncate_inode_pages_final(&inode->i_data);
2651da177e4SLinus Torvalds 
266f053dddeSAl Viro 	if (!inode->i_nlink) {
267f053dddeSAl Viro 		inode->i_size = 0;
268f053dddeSAl Viro 		affs_truncate(inode);
269f053dddeSAl Viro 	}
270dca3c336SRoman Zippel 
271f053dddeSAl Viro 	invalidate_inode_buffers(inode);
272dbd5768fSJan Kara 	clear_inode(inode);
273dca3c336SRoman Zippel 	affs_free_prealloc(inode);
274dca3c336SRoman Zippel 	cache_page = (unsigned long)AFFS_I(inode)->i_lc;
2751da177e4SLinus Torvalds 	if (cache_page) {
2769606d9aaSFabian Frederick 		pr_debug("freeing ext cache\n");
2771da177e4SLinus Torvalds 		AFFS_I(inode)->i_lc = NULL;
2781da177e4SLinus Torvalds 		AFFS_I(inode)->i_ac = NULL;
2791da177e4SLinus Torvalds 		free_page(cache_page);
2801da177e4SLinus Torvalds 	}
2811da177e4SLinus Torvalds 	affs_brelse(AFFS_I(inode)->i_ext_bh);
2821da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_last = ~1;
2831da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_bh = NULL;
284f053dddeSAl Viro 
285f053dddeSAl Viro 	if (!inode->i_nlink)
286f053dddeSAl Viro 		affs_free_block(inode->i_sb, inode->i_ino);
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds struct inode *
2901da177e4SLinus Torvalds affs_new_inode(struct inode *dir)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	struct super_block	*sb = dir->i_sb;
2931da177e4SLinus Torvalds 	struct inode		*inode;
2941da177e4SLinus Torvalds 	u32			 block;
2951da177e4SLinus Torvalds 	struct buffer_head	*bh;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	if (!(inode = new_inode(sb)))
2981da177e4SLinus Torvalds 		goto err_inode;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	if (!(block = affs_alloc_block(dir, dir->i_ino)))
3011da177e4SLinus Torvalds 		goto err_block;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	bh = affs_getzeroblk(sb, block);
3041da177e4SLinus Torvalds 	if (!bh)
3051da177e4SLinus Torvalds 		goto err_bh;
3061da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
3071da177e4SLinus Torvalds 	affs_brelse(bh);
3081da177e4SLinus Torvalds 
30921559981SDavid Howells 	inode->i_uid     = current_fsuid();
31021559981SDavid Howells 	inode->i_gid     = current_fsgid();
3111da177e4SLinus Torvalds 	inode->i_ino     = block;
312bfe86848SMiklos Szeredi 	set_nlink(inode, 1);
31302027d42SDeepa Dinamani 	inode->i_mtime   = inode->i_atime = inode->i_ctime = current_time(inode);
314dca3c336SRoman Zippel 	atomic_set(&AFFS_I(inode)->i_opencnt, 0);
3151da177e4SLinus Torvalds 	AFFS_I(inode)->i_blkcnt = 0;
3161da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc = NULL;
3171da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_size = 0;
3181da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_shift = 0;
3191da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_mask = 0;
3201da177e4SLinus Torvalds 	AFFS_I(inode)->i_ac = NULL;
3211da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_bh = NULL;
3221da177e4SLinus Torvalds 	AFFS_I(inode)->mmu_private = 0;
3231da177e4SLinus Torvalds 	AFFS_I(inode)->i_protect = 0;
3241da177e4SLinus Torvalds 	AFFS_I(inode)->i_lastalloc = 0;
3251da177e4SLinus Torvalds 	AFFS_I(inode)->i_pa_cnt = 0;
3261da177e4SLinus Torvalds 	AFFS_I(inode)->i_extcnt = 1;
3271da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_last = ~1;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	insert_inode_hash(inode);
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	return inode;
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds err_bh:
3341da177e4SLinus Torvalds 	affs_free_block(sb, block);
3351da177e4SLinus Torvalds err_block:
3361da177e4SLinus Torvalds 	iput(inode);
3371da177e4SLinus Torvalds err_inode:
3381da177e4SLinus Torvalds 	return NULL;
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds /*
3421da177e4SLinus Torvalds  * Add an entry to a directory. Create the header block
3431da177e4SLinus Torvalds  * and insert it into the hash table.
3441da177e4SLinus Torvalds  */
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds int
3471da177e4SLinus Torvalds affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	struct super_block *sb = dir->i_sb;
3501da177e4SLinus Torvalds 	struct buffer_head *inode_bh = NULL;
35178f444f6SFabian Frederick 	struct buffer_head *bh;
3521da177e4SLinus Torvalds 	u32 block = 0;
3531da177e4SLinus Torvalds 	int retval;
3541da177e4SLinus Torvalds 
35508fe100dSGeert Uytterhoeven 	pr_debug("%s(dir=%lu, inode=%lu, \"%pd\", type=%d)\n", __func__,
35608fe100dSGeert Uytterhoeven 		 dir->i_ino, inode->i_ino, dentry, type);
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	retval = -EIO;
3591da177e4SLinus Torvalds 	bh = affs_bread(sb, inode->i_ino);
3601da177e4SLinus Torvalds 	if (!bh)
3611da177e4SLinus Torvalds 		goto done;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	affs_lock_link(inode);
3641da177e4SLinus Torvalds 	switch (type) {
3651da177e4SLinus Torvalds 	case ST_LINKFILE:
3661da177e4SLinus Torvalds 	case ST_LINKDIR:
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;
372dca3c336SRoman Zippel 		inode_bh = bh;
3731da177e4SLinus Torvalds 		bh = affs_getzeroblk(sb, block);
3741da177e4SLinus Torvalds 		if (!bh)
3751da177e4SLinus Torvalds 			goto err;
3761da177e4SLinus Torvalds 		break;
3771da177e4SLinus Torvalds 	default:
3781da177e4SLinus Torvalds 		break;
3791da177e4SLinus Torvalds 	}
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT);
3821da177e4SLinus Torvalds 	AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr);
3831da177e4SLinus Torvalds 	affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry);
3841da177e4SLinus Torvalds 	AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type);
3851da177e4SLinus Torvalds 	AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	if (inode_bh) {
3881da177e4SLinus Torvalds 		__be32 chain;
3891da177e4SLinus Torvalds 	       	chain = AFFS_TAIL(sb, inode_bh)->link_chain;
3901da177e4SLinus Torvalds 		AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino);
3911da177e4SLinus Torvalds 		AFFS_TAIL(sb, bh)->link_chain = chain;
3921da177e4SLinus Torvalds 		AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block);
3931da177e4SLinus Torvalds 		affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain));
3941da177e4SLinus Torvalds 		mark_buffer_dirty_inode(inode_bh, inode);
395bfe86848SMiklos Szeredi 		set_nlink(inode, 2);
3967de9c6eeSAl Viro 		ihold(inode);
3971da177e4SLinus Torvalds 	}
3981da177e4SLinus Torvalds 	affs_fix_checksum(sb, bh);
3991da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
4001da177e4SLinus Torvalds 	dentry->d_fsdata = (void *)(long)bh->b_blocknr;
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	affs_lock_dir(dir);
4031da177e4SLinus Torvalds 	retval = affs_insert_hash(dir, bh);
4041da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
4051da177e4SLinus Torvalds 	affs_unlock_dir(dir);
4061da177e4SLinus Torvalds 	affs_unlock_link(inode);
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
4091da177e4SLinus Torvalds done:
4101da177e4SLinus Torvalds 	affs_brelse(inode_bh);
4111da177e4SLinus Torvalds 	affs_brelse(bh);
4121da177e4SLinus Torvalds 	return retval;
4131da177e4SLinus Torvalds err:
4141da177e4SLinus Torvalds 	if (block)
4151da177e4SLinus Torvalds 		affs_free_block(sb, block);
4161da177e4SLinus Torvalds 	affs_unlock_link(inode);
4171da177e4SLinus Torvalds 	goto done;
4181da177e4SLinus Torvalds }
419