1 /*
2  *   Copyright (c) International Business Machines Corp., 2000-2002
3  *
4  *   This program is free software;  you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License as published by
6  *   the Free Software Foundation; either version 2 of the License, or
7  *   (at your option) any later version.
8  *
9  *   This program is distributed in the hope that it will be useful,
10  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12  *   the GNU General Public License for more details.
13  *
14  *   You should have received a copy of the GNU General Public License
15  *   along with this program;  if not, write to the Free Software
16  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include <linux/fs.h>
20 #include "jfs_incore.h"
21 #include "jfs_filsys.h"
22 #include "jfs_imap.h"
23 #include "jfs_dinode.h"
24 #include "jfs_debug.h"
25 
26 kmem_cache_t *jfs_inode_cachep;
27 
28 /*
29  * NAME:	ialloc()
30  *
31  * FUNCTION:	Allocate a new inode
32  *
33  */
ialloc(struct inode * parent,umode_t mode)34 struct inode *ialloc(struct inode *parent, umode_t mode)
35 {
36 	struct super_block *sb = parent->i_sb;
37 	struct inode *inode;
38 	struct jfs_inode_info *jfs_inode;
39 	int rc;
40 
41 	inode = new_inode(sb);
42 	if (!inode) {
43 		jfs_warn("ialloc: new_inode returned NULL!");
44 		return inode;
45 	}
46 
47 	rc = alloc_jfs_inode(inode);
48 	if (rc) {
49 		make_bad_inode(inode);
50 		iput(inode);
51 		return NULL;
52 	}
53 	jfs_inode = JFS_IP(inode);
54 
55 	rc = diAlloc(parent, S_ISDIR(mode), inode);
56 	if (rc) {
57 		jfs_warn("ialloc: diAlloc returned %d!", rc);
58 		free_jfs_inode(inode);
59 		make_bad_inode(inode);
60 		iput(inode);
61 		return NULL;
62 	}
63 
64 	inode->i_uid = current->fsuid;
65 	if (parent->i_mode & S_ISGID) {
66 		inode->i_gid = parent->i_gid;
67 		if (S_ISDIR(mode))
68 			mode |= S_ISGID;
69 	} else
70 		inode->i_gid = current->fsgid;
71 
72 	inode->i_mode = mode;
73 	if (S_ISDIR(mode))
74 		jfs_inode->mode2 = IDIRECTORY | mode;
75 	else
76 		jfs_inode->mode2 = INLINEEA | ISPARSE | mode;
77 	inode->i_blksize = sb->s_blocksize;
78 	inode->i_blocks = 0;
79 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
80 	jfs_inode->otime = inode->i_ctime;
81 	inode->i_generation = JFS_SBI(sb)->gengen++;
82 
83 	jfs_inode->cflag = 0;
84 	set_cflag(COMMIT_New, inode);
85 
86 	/* Zero remaining fields */
87 	memset(&jfs_inode->acl, 0, sizeof(dxd_t));
88 	memset(&jfs_inode->ea, 0, sizeof(dxd_t));
89 	jfs_inode->next_index = 0;
90 	jfs_inode->acltype = 0;
91 	jfs_inode->btorder = 0;
92 	jfs_inode->btindex = 0;
93 	jfs_inode->bxflag = 0;
94 	jfs_inode->blid = 0;
95 	jfs_inode->atlhead = 0;
96 	jfs_inode->atltail = 0;
97 	jfs_inode->xtlid = 0;
98 
99 	jfs_info("ialloc returns inode = 0x%p\n", inode);
100 
101 	return inode;
102 }
103 
104 /*
105  * NAME:	alloc_jfs_inode()
106  *
107  * FUNCTION:	Allocate jfs portion of in-memory inode
108  *
109  */
alloc_jfs_inode(struct inode * inode)110 int alloc_jfs_inode(struct inode *inode)
111 {
112 	struct jfs_inode_info *jfs_inode;
113 
114 	jfs_inode = kmem_cache_alloc(jfs_inode_cachep, GFP_NOFS);
115 	inode->u.generic_ip = jfs_inode;
116 	if (!jfs_inode)
117 		return -ENOSPC;
118 	jfs_inode->inode = inode;
119 
120 	return 0;
121 }
122