xref: /openbmc/linux/fs/affs/inode.c (revision 8fed10be0029acda5564f03b9cc1fc4cb7470bae)
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 
16754661f1SArjan van de Ven extern const struct inode_operations affs_symlink_inode_operations;
171da177e4SLinus Torvalds extern struct timezone sys_tz;
181da177e4SLinus Torvalds 
19210f8559SDavid Howells struct inode *affs_iget(struct super_block *sb, unsigned long ino)
201da177e4SLinus Torvalds {
211da177e4SLinus Torvalds 	struct affs_sb_info	*sbi = AFFS_SB(sb);
221da177e4SLinus Torvalds 	struct buffer_head	*bh;
231da177e4SLinus Torvalds 	struct affs_head	*head;
241da177e4SLinus Torvalds 	struct affs_tail	*tail;
25210f8559SDavid Howells 	struct inode		*inode;
261da177e4SLinus Torvalds 	u32			 block;
271da177e4SLinus Torvalds 	u32			 size;
281da177e4SLinus Torvalds 	u32			 prot;
291da177e4SLinus Torvalds 	u16			 id;
301da177e4SLinus Torvalds 
31210f8559SDavid Howells 	inode = iget_locked(sb, ino);
32210f8559SDavid Howells 	if (!inode)
33210f8559SDavid Howells 		return ERR_PTR(-ENOMEM);
34210f8559SDavid Howells 	if (!(inode->i_state & I_NEW))
35210f8559SDavid Howells 		return inode;
36210f8559SDavid Howells 
37210f8559SDavid Howells 	pr_debug("AFFS: affs_iget(%lu)\n", inode->i_ino);
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds 	block = inode->i_ino;
401da177e4SLinus Torvalds 	bh = affs_bread(sb, block);
411da177e4SLinus Torvalds 	if (!bh) {
421da177e4SLinus Torvalds 		affs_warning(sb, "read_inode", "Cannot read block %d", block);
431da177e4SLinus Torvalds 		goto bad_inode;
441da177e4SLinus Torvalds 	}
451da177e4SLinus Torvalds 	if (affs_checksum_block(sb, bh) || be32_to_cpu(AFFS_HEAD(bh)->ptype) != T_SHORT) {
461da177e4SLinus Torvalds 		affs_warning(sb,"read_inode",
471da177e4SLinus Torvalds 			   "Checksum or type (ptype=%d) error on inode %d",
481da177e4SLinus Torvalds 			   AFFS_HEAD(bh)->ptype, block);
491da177e4SLinus Torvalds 		goto bad_inode;
501da177e4SLinus Torvalds 	}
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 	head = AFFS_HEAD(bh);
531da177e4SLinus Torvalds 	tail = AFFS_TAIL(sb, bh);
541da177e4SLinus Torvalds 	prot = be32_to_cpu(tail->protect);
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	inode->i_size = 0;
57bfe86848SMiklos Szeredi 	set_nlink(inode, 1);
581da177e4SLinus Torvalds 	inode->i_mode = 0;
591da177e4SLinus Torvalds 	AFFS_I(inode)->i_extcnt = 1;
601da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_last = ~1;
611da177e4SLinus Torvalds 	AFFS_I(inode)->i_protect = prot;
62dca3c336SRoman Zippel 	atomic_set(&AFFS_I(inode)->i_opencnt, 0);
631da177e4SLinus Torvalds 	AFFS_I(inode)->i_blkcnt = 0;
641da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc = NULL;
651da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_size = 0;
661da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_shift = 0;
671da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_mask = 0;
681da177e4SLinus Torvalds 	AFFS_I(inode)->i_ac = NULL;
691da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_bh = NULL;
701da177e4SLinus Torvalds 	AFFS_I(inode)->mmu_private = 0;
711da177e4SLinus Torvalds 	AFFS_I(inode)->i_lastalloc = 0;
721da177e4SLinus Torvalds 	AFFS_I(inode)->i_pa_cnt = 0;
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	if (sbi->s_flags & SF_SETMODE)
751da177e4SLinus Torvalds 		inode->i_mode = sbi->s_mode;
761da177e4SLinus Torvalds 	else
771da177e4SLinus Torvalds 		inode->i_mode = prot_to_mode(prot);
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	id = be16_to_cpu(tail->uid);
801da177e4SLinus Torvalds 	if (id == 0 || sbi->s_flags & SF_SETUID)
811da177e4SLinus Torvalds 		inode->i_uid = sbi->s_uid;
821da177e4SLinus Torvalds 	else if (id == 0xFFFF && sbi->s_flags & SF_MUFS)
83*8fed10beSEric W. Biederman 		i_uid_write(inode, 0);
841da177e4SLinus Torvalds 	else
85*8fed10beSEric W. Biederman 		i_uid_write(inode, id);
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	id = be16_to_cpu(tail->gid);
881da177e4SLinus Torvalds 	if (id == 0 || sbi->s_flags & SF_SETGID)
891da177e4SLinus Torvalds 		inode->i_gid = sbi->s_gid;
901da177e4SLinus Torvalds 	else if (id == 0xFFFF && sbi->s_flags & SF_MUFS)
91*8fed10beSEric W. Biederman 		i_gid_write(inode, 0);
921da177e4SLinus Torvalds 	else
93*8fed10beSEric W. Biederman 		i_gid_write(inode, id);
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	switch (be32_to_cpu(tail->stype)) {
961da177e4SLinus Torvalds 	case ST_ROOT:
971da177e4SLinus Torvalds 		inode->i_uid = sbi->s_uid;
981da177e4SLinus Torvalds 		inode->i_gid = sbi->s_gid;
991da177e4SLinus Torvalds 		/* fall through */
1001da177e4SLinus Torvalds 	case ST_USERDIR:
1011da177e4SLinus Torvalds 		if (be32_to_cpu(tail->stype) == ST_USERDIR ||
1021da177e4SLinus Torvalds 		    sbi->s_flags & SF_SETMODE) {
1031da177e4SLinus Torvalds 			if (inode->i_mode & S_IRUSR)
1041da177e4SLinus Torvalds 				inode->i_mode |= S_IXUSR;
1051da177e4SLinus Torvalds 			if (inode->i_mode & S_IRGRP)
1061da177e4SLinus Torvalds 				inode->i_mode |= S_IXGRP;
1071da177e4SLinus Torvalds 			if (inode->i_mode & S_IROTH)
1081da177e4SLinus Torvalds 				inode->i_mode |= S_IXOTH;
1091da177e4SLinus Torvalds 			inode->i_mode |= S_IFDIR;
1101da177e4SLinus Torvalds 		} else
1111da177e4SLinus Torvalds 			inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
1121da177e4SLinus Torvalds 		/* Maybe it should be controlled by mount parameter? */
1131da177e4SLinus Torvalds 		//inode->i_mode |= S_ISVTX;
1141da177e4SLinus Torvalds 		inode->i_op = &affs_dir_inode_operations;
1151da177e4SLinus Torvalds 		inode->i_fop = &affs_dir_operations;
1161da177e4SLinus Torvalds 		break;
1171da177e4SLinus Torvalds 	case ST_LINKDIR:
1181da177e4SLinus Torvalds #if 0
1191da177e4SLinus Torvalds 		affs_warning(sb, "read_inode", "inode is LINKDIR");
1201da177e4SLinus Torvalds 		goto bad_inode;
1211da177e4SLinus Torvalds #else
1221da177e4SLinus Torvalds 		inode->i_mode |= S_IFDIR;
123c765d479SAl Viro 		/* ... and leave ->i_op and ->i_fop pointing to empty */
1241da177e4SLinus Torvalds 		break;
1251da177e4SLinus Torvalds #endif
1261da177e4SLinus Torvalds 	case ST_LINKFILE:
1271da177e4SLinus Torvalds 		affs_warning(sb, "read_inode", "inode is LINKFILE");
1281da177e4SLinus Torvalds 		goto bad_inode;
1291da177e4SLinus Torvalds 	case ST_FILE:
1301da177e4SLinus Torvalds 		size = be32_to_cpu(tail->size);
1311da177e4SLinus Torvalds 		inode->i_mode |= S_IFREG;
1321da177e4SLinus Torvalds 		AFFS_I(inode)->mmu_private = inode->i_size = size;
1331da177e4SLinus Torvalds 		if (inode->i_size) {
1341da177e4SLinus Torvalds 			AFFS_I(inode)->i_blkcnt = (size - 1) /
1351da177e4SLinus Torvalds 					       sbi->s_data_blksize + 1;
1361da177e4SLinus Torvalds 			AFFS_I(inode)->i_extcnt = (AFFS_I(inode)->i_blkcnt - 1) /
1371da177e4SLinus Torvalds 					       sbi->s_hashsize + 1;
1381da177e4SLinus Torvalds 		}
1391da177e4SLinus Torvalds 		if (tail->link_chain)
140bfe86848SMiklos Szeredi 			set_nlink(inode, 2);
1411da177e4SLinus Torvalds 		inode->i_mapping->a_ops = (sbi->s_flags & SF_OFS) ? &affs_aops_ofs : &affs_aops;
1421da177e4SLinus Torvalds 		inode->i_op = &affs_file_inode_operations;
1431da177e4SLinus Torvalds 		inode->i_fop = &affs_file_operations;
1441da177e4SLinus Torvalds 		break;
1451da177e4SLinus Torvalds 	case ST_SOFTLINK:
1461da177e4SLinus Torvalds 		inode->i_mode |= S_IFLNK;
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 
1521da177e4SLinus Torvalds 	inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec
1531da177e4SLinus Torvalds 		       = (be32_to_cpu(tail->change.days) * (24 * 60 * 60) +
1541da177e4SLinus Torvalds 		         be32_to_cpu(tail->change.mins) * 60 +
1551da177e4SLinus Torvalds 			 be32_to_cpu(tail->change.ticks) / 50 +
1561da177e4SLinus Torvalds 			 ((8 * 365 + 2) * 24 * 60 * 60)) +
1571da177e4SLinus Torvalds 			 sys_tz.tz_minuteswest * 60;
1581da177e4SLinus Torvalds 	inode->i_mtime.tv_nsec = inode->i_ctime.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
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 
1781da177e4SLinus Torvalds 	pr_debug("AFFS: 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)) {
1901da177e4SLinus Torvalds 		secs_to_datestamp(inode->i_mtime.tv_sec,&AFFS_ROOT_TAIL(sb, bh)->root_change);
1911da177e4SLinus Torvalds 	} else {
1921da177e4SLinus Torvalds 		tail->protect = cpu_to_be32(AFFS_I(inode)->i_protect);
1931da177e4SLinus Torvalds 		tail->size = cpu_to_be32(inode->i_size);
1941da177e4SLinus Torvalds 		secs_to_datestamp(inode->i_mtime.tv_sec,&tail->change);
1951da177e4SLinus Torvalds 		if (!(inode->i_ino == AFFS_SB(sb)->s_root_block)) {
196*8fed10beSEric W. Biederman 			uid = i_uid_read(inode);
197*8fed10beSEric W. Biederman 			gid = i_gid_read(inode);
1981da177e4SLinus Torvalds 			if (AFFS_SB(sb)->s_flags & SF_MUFS) {
199*8fed10beSEric W. Biederman 				if (uid == 0 || uid == 0xFFFF)
200*8fed10beSEric W. Biederman 					uid = uid ^ ~0;
201*8fed10beSEric W. Biederman 				if (gid == 0 || gid == 0xFFFF)
202*8fed10beSEric W. Biederman 					gid = gid ^ ~0;
2031da177e4SLinus Torvalds 			}
2041da177e4SLinus Torvalds 			if (!(AFFS_SB(sb)->s_flags & SF_SETUID))
2051da177e4SLinus Torvalds 				tail->uid = cpu_to_be16(uid);
2061da177e4SLinus Torvalds 			if (!(AFFS_SB(sb)->s_flags & SF_SETGID))
2071da177e4SLinus Torvalds 				tail->gid = cpu_to_be16(gid);
2081da177e4SLinus Torvalds 		}
2091da177e4SLinus Torvalds 	}
2101da177e4SLinus Torvalds 	affs_fix_checksum(sb, bh);
2111da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
2121da177e4SLinus Torvalds 	affs_brelse(bh);
2131da177e4SLinus Torvalds 	affs_free_prealloc(inode);
2141da177e4SLinus Torvalds 	return 0;
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds int
2181da177e4SLinus Torvalds affs_notify_change(struct dentry *dentry, struct iattr *attr)
2191da177e4SLinus Torvalds {
2201da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
2211da177e4SLinus Torvalds 	int error;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	pr_debug("AFFS: notify_change(%lu,0x%x)\n",inode->i_ino,attr->ia_valid);
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	error = inode_change_ok(inode,attr);
2261da177e4SLinus Torvalds 	if (error)
2271da177e4SLinus Torvalds 		goto out;
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	if (((attr->ia_valid & ATTR_UID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETUID)) ||
2301da177e4SLinus Torvalds 	    ((attr->ia_valid & ATTR_GID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETGID)) ||
2311da177e4SLinus Torvalds 	    ((attr->ia_valid & ATTR_MODE) &&
2321da177e4SLinus Torvalds 	     (AFFS_SB(inode->i_sb)->s_flags & (SF_SETMODE | SF_IMMUTABLE)))) {
2331da177e4SLinus Torvalds 		if (!(AFFS_SB(inode->i_sb)->s_flags & SF_QUIET))
2341da177e4SLinus Torvalds 			error = -EPERM;
2351da177e4SLinus Torvalds 		goto out;
2361da177e4SLinus Torvalds 	}
2371da177e4SLinus Torvalds 
2381025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
2391025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
2401025774cSChristoph Hellwig 		error = vmtruncate(inode, attr->ia_size);
2411025774cSChristoph Hellwig 		if (error)
2421025774cSChristoph Hellwig 			return error;
2431025774cSChristoph Hellwig 	}
2441025774cSChristoph Hellwig 
2451025774cSChristoph Hellwig 	setattr_copy(inode, attr);
2461025774cSChristoph Hellwig 	mark_inode_dirty(inode);
2471025774cSChristoph Hellwig 
2481025774cSChristoph Hellwig 	if (attr->ia_valid & ATTR_MODE)
2491da177e4SLinus Torvalds 		mode_to_prot(inode);
2501da177e4SLinus Torvalds out:
2511da177e4SLinus Torvalds 	return error;
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds void
255f053dddeSAl Viro affs_evict_inode(struct inode *inode)
2561da177e4SLinus Torvalds {
257dca3c336SRoman Zippel 	unsigned long cache_page;
258f053dddeSAl Viro 	pr_debug("AFFS: evict_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink);
259f053dddeSAl Viro 	truncate_inode_pages(&inode->i_data, 0);
2601da177e4SLinus Torvalds 
261f053dddeSAl Viro 	if (!inode->i_nlink) {
262f053dddeSAl Viro 		inode->i_size = 0;
263f053dddeSAl Viro 		affs_truncate(inode);
264f053dddeSAl Viro 	}
265dca3c336SRoman Zippel 
266f053dddeSAl Viro 	invalidate_inode_buffers(inode);
267dbd5768fSJan Kara 	clear_inode(inode);
268dca3c336SRoman Zippel 	affs_free_prealloc(inode);
269dca3c336SRoman Zippel 	cache_page = (unsigned long)AFFS_I(inode)->i_lc;
2701da177e4SLinus Torvalds 	if (cache_page) {
2711da177e4SLinus Torvalds 		pr_debug("AFFS: freeing ext cache\n");
2721da177e4SLinus Torvalds 		AFFS_I(inode)->i_lc = NULL;
2731da177e4SLinus Torvalds 		AFFS_I(inode)->i_ac = NULL;
2741da177e4SLinus Torvalds 		free_page(cache_page);
2751da177e4SLinus Torvalds 	}
2761da177e4SLinus Torvalds 	affs_brelse(AFFS_I(inode)->i_ext_bh);
2771da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_last = ~1;
2781da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_bh = NULL;
279f053dddeSAl Viro 
280f053dddeSAl Viro 	if (!inode->i_nlink)
281f053dddeSAl Viro 		affs_free_block(inode->i_sb, inode->i_ino);
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds struct inode *
2851da177e4SLinus Torvalds affs_new_inode(struct inode *dir)
2861da177e4SLinus Torvalds {
2871da177e4SLinus Torvalds 	struct super_block	*sb = dir->i_sb;
2881da177e4SLinus Torvalds 	struct inode		*inode;
2891da177e4SLinus Torvalds 	u32			 block;
2901da177e4SLinus Torvalds 	struct buffer_head	*bh;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	if (!(inode = new_inode(sb)))
2931da177e4SLinus Torvalds 		goto err_inode;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	if (!(block = affs_alloc_block(dir, dir->i_ino)))
2961da177e4SLinus Torvalds 		goto err_block;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	bh = affs_getzeroblk(sb, block);
2991da177e4SLinus Torvalds 	if (!bh)
3001da177e4SLinus Torvalds 		goto err_bh;
3011da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
3021da177e4SLinus Torvalds 	affs_brelse(bh);
3031da177e4SLinus Torvalds 
30421559981SDavid Howells 	inode->i_uid     = current_fsuid();
30521559981SDavid Howells 	inode->i_gid     = current_fsgid();
3061da177e4SLinus Torvalds 	inode->i_ino     = block;
307bfe86848SMiklos Szeredi 	set_nlink(inode, 1);
3081da177e4SLinus Torvalds 	inode->i_mtime   = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
309dca3c336SRoman Zippel 	atomic_set(&AFFS_I(inode)->i_opencnt, 0);
3101da177e4SLinus Torvalds 	AFFS_I(inode)->i_blkcnt = 0;
3111da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc = NULL;
3121da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_size = 0;
3131da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_shift = 0;
3141da177e4SLinus Torvalds 	AFFS_I(inode)->i_lc_mask = 0;
3151da177e4SLinus Torvalds 	AFFS_I(inode)->i_ac = NULL;
3161da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_bh = NULL;
3171da177e4SLinus Torvalds 	AFFS_I(inode)->mmu_private = 0;
3181da177e4SLinus Torvalds 	AFFS_I(inode)->i_protect = 0;
3191da177e4SLinus Torvalds 	AFFS_I(inode)->i_lastalloc = 0;
3201da177e4SLinus Torvalds 	AFFS_I(inode)->i_pa_cnt = 0;
3211da177e4SLinus Torvalds 	AFFS_I(inode)->i_extcnt = 1;
3221da177e4SLinus Torvalds 	AFFS_I(inode)->i_ext_last = ~1;
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	insert_inode_hash(inode);
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	return inode;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds err_bh:
3291da177e4SLinus Torvalds 	affs_free_block(sb, block);
3301da177e4SLinus Torvalds err_block:
3311da177e4SLinus Torvalds 	iput(inode);
3321da177e4SLinus Torvalds err_inode:
3331da177e4SLinus Torvalds 	return NULL;
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds /*
3371da177e4SLinus Torvalds  * Add an entry to a directory. Create the header block
3381da177e4SLinus Torvalds  * and insert it into the hash table.
3391da177e4SLinus Torvalds  */
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds int
3421da177e4SLinus Torvalds affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	struct super_block *sb = dir->i_sb;
3451da177e4SLinus Torvalds 	struct buffer_head *inode_bh = NULL;
3461da177e4SLinus Torvalds 	struct buffer_head *bh = NULL;
3471da177e4SLinus Torvalds 	u32 block = 0;
3481da177e4SLinus Torvalds 	int retval;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	pr_debug("AFFS: add_entry(dir=%u, inode=%u, \"%*s\", type=%d)\n", (u32)dir->i_ino,
3511da177e4SLinus Torvalds 	         (u32)inode->i_ino, (int)dentry->d_name.len, dentry->d_name.name, type);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	retval = -EIO;
3541da177e4SLinus Torvalds 	bh = affs_bread(sb, inode->i_ino);
3551da177e4SLinus Torvalds 	if (!bh)
3561da177e4SLinus Torvalds 		goto done;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	affs_lock_link(inode);
3591da177e4SLinus Torvalds 	switch (type) {
3601da177e4SLinus Torvalds 	case ST_LINKFILE:
3611da177e4SLinus Torvalds 	case ST_LINKDIR:
3621da177e4SLinus Torvalds 		retval = -ENOSPC;
3631da177e4SLinus Torvalds 		block = affs_alloc_block(dir, dir->i_ino);
3641da177e4SLinus Torvalds 		if (!block)
3651da177e4SLinus Torvalds 			goto err;
3661da177e4SLinus Torvalds 		retval = -EIO;
367dca3c336SRoman Zippel 		inode_bh = bh;
3681da177e4SLinus Torvalds 		bh = affs_getzeroblk(sb, block);
3691da177e4SLinus Torvalds 		if (!bh)
3701da177e4SLinus Torvalds 			goto err;
3711da177e4SLinus Torvalds 		break;
3721da177e4SLinus Torvalds 	default:
3731da177e4SLinus Torvalds 		break;
3741da177e4SLinus Torvalds 	}
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT);
3771da177e4SLinus Torvalds 	AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr);
3781da177e4SLinus Torvalds 	affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry);
3791da177e4SLinus Torvalds 	AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type);
3801da177e4SLinus Torvalds 	AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	if (inode_bh) {
3831da177e4SLinus Torvalds 		__be32 chain;
3841da177e4SLinus Torvalds 	       	chain = AFFS_TAIL(sb, inode_bh)->link_chain;
3851da177e4SLinus Torvalds 		AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino);
3861da177e4SLinus Torvalds 		AFFS_TAIL(sb, bh)->link_chain = chain;
3871da177e4SLinus Torvalds 		AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block);
3881da177e4SLinus Torvalds 		affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain));
3891da177e4SLinus Torvalds 		mark_buffer_dirty_inode(inode_bh, inode);
390bfe86848SMiklos Szeredi 		set_nlink(inode, 2);
3917de9c6eeSAl Viro 		ihold(inode);
3921da177e4SLinus Torvalds 	}
3931da177e4SLinus Torvalds 	affs_fix_checksum(sb, bh);
3941da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
3951da177e4SLinus Torvalds 	dentry->d_fsdata = (void *)(long)bh->b_blocknr;
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	affs_lock_dir(dir);
3981da177e4SLinus Torvalds 	retval = affs_insert_hash(dir, bh);
3991da177e4SLinus Torvalds 	mark_buffer_dirty_inode(bh, inode);
4001da177e4SLinus Torvalds 	affs_unlock_dir(dir);
4011da177e4SLinus Torvalds 	affs_unlock_link(inode);
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
4041da177e4SLinus Torvalds done:
4051da177e4SLinus Torvalds 	affs_brelse(inode_bh);
4061da177e4SLinus Torvalds 	affs_brelse(bh);
4071da177e4SLinus Torvalds 	return retval;
4081da177e4SLinus Torvalds err:
4091da177e4SLinus Torvalds 	if (block)
4101da177e4SLinus Torvalds 		affs_free_block(sb, block);
4111da177e4SLinus Torvalds 	affs_unlock_link(inode);
4121da177e4SLinus Torvalds 	goto done;
4131da177e4SLinus Torvalds }
414