fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / jfs / jfs_inode.c
1 /*
2  *   Copyright (C) International Business Machines Corp., 2000-2004
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 <linux/quotaops.h>
21 #include <linux/vs_dlimit.h>
22 #include <linux/vs_tag.h>
23 #include "jfs_incore.h"
24 #include "jfs_inode.h"
25 #include "jfs_filsys.h"
26 #include "jfs_imap.h"
27 #include "jfs_dinode.h"
28 #include "jfs_debug.h"
29
30
31 void jfs_set_inode_flags(struct inode *inode)
32 {
33         unsigned int flags = JFS_IP(inode)->mode2;
34
35         inode->i_flags &= ~(S_IMMUTABLE | S_IUNLINK | S_BARRIER |
36                 S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC);
37
38         if (flags & JFS_IMMUTABLE_FL)
39                 inode->i_flags |= S_IMMUTABLE;
40         if (flags & JFS_IUNLINK_FL)
41                 inode->i_flags |= S_IUNLINK;
42         if (flags & JFS_BARRIER_FL)
43                 inode->i_flags |= S_BARRIER;
44
45         if (flags & JFS_SYNC_FL)
46                 inode->i_flags |= S_SYNC;
47         if (flags & JFS_APPEND_FL)
48                 inode->i_flags |= S_APPEND;
49         if (flags & JFS_NOATIME_FL)
50                 inode->i_flags |= S_NOATIME;
51         if (flags & JFS_DIRSYNC_FL)
52                 inode->i_flags |= S_DIRSYNC;
53 }
54
55 int jfs_sync_flags(struct inode *inode)
56 {
57         unsigned int oldflags, newflags;
58
59         oldflags = JFS_IP(inode)->mode2;
60         newflags = oldflags & ~(JFS_IMMUTABLE_FL |
61                 JFS_IUNLINK_FL | JFS_BARRIER_FL);
62
63         if (IS_IMMUTABLE(inode))
64                 newflags |= JFS_IMMUTABLE_FL;
65         if (IS_IUNLINK(inode))
66                 newflags |= JFS_IUNLINK_FL;
67         if (IS_BARRIER(inode))
68                 newflags |= JFS_BARRIER_FL;
69
70         if (oldflags ^ newflags) {
71                 JFS_IP(inode)->mode2 = newflags;
72                 inode->i_ctime = CURRENT_TIME;
73                 mark_inode_dirty(inode);
74         }
75         return 0;
76 }
77
78 /*
79  * NAME:        ialloc()
80  *
81  * FUNCTION:    Allocate a new inode
82  *
83  */
84 struct inode *ialloc(struct inode *parent, umode_t mode)
85 {
86         struct super_block *sb = parent->i_sb;
87         struct inode *inode;
88         struct jfs_inode_info *jfs_inode;
89         int rc;
90
91         inode = new_inode(sb);
92         if (!inode) {
93                 jfs_warn("ialloc: new_inode returned NULL!");
94                 return ERR_PTR(-ENOMEM);
95         }
96
97         jfs_inode = JFS_IP(inode);
98
99         rc = diAlloc(parent, S_ISDIR(mode), inode);
100         if (rc) {
101                 jfs_warn("ialloc: diAlloc returned %d!", rc);
102                 if (rc == -EIO)
103                         make_bad_inode(inode);
104                 iput(inode);
105                 return ERR_PTR(rc);
106         }
107
108         inode->i_uid = current->fsuid;
109         if (parent->i_mode & S_ISGID) {
110                 inode->i_gid = parent->i_gid;
111                 if (S_ISDIR(mode))
112                         mode |= S_ISGID;
113         } else
114                 inode->i_gid = current->fsgid;
115
116         /*
117          * New inodes need to save sane values on disk when
118          * uid & gid mount options are used
119          */
120         jfs_inode->saved_uid = inode->i_uid;
121         jfs_inode->saved_gid = inode->i_gid;
122
123         inode->i_tag = dx_current_fstag(sb);
124         if (DLIMIT_ALLOC_INODE(inode)) {
125                 iput(inode);
126                 return NULL;
127         }
128
129         /*
130          * Allocate inode to quota.
131          */
132         if (DQUOT_ALLOC_INODE(inode)) {
133                 DLIMIT_FREE_INODE(inode);
134                 DQUOT_DROP(inode);
135                 inode->i_flags |= S_NOQUOTA;
136                 inode->i_nlink = 0;
137                 iput(inode);
138                 return ERR_PTR(-EDQUOT);
139         }
140
141         inode->i_mode = mode;
142         /* inherit flags from parent */
143         jfs_inode->mode2 = JFS_IP(parent)->mode2 & JFS_FL_INHERIT;
144
145         if (S_ISDIR(mode)) {
146                 jfs_inode->mode2 |= IDIRECTORY;
147                 jfs_inode->mode2 &= ~JFS_DIRSYNC_FL;
148         }
149         else {
150                 jfs_inode->mode2 |= INLINEEA | ISPARSE;
151                 if (S_ISLNK(mode))
152                         jfs_inode->mode2 &= ~(JFS_IMMUTABLE_FL|JFS_APPEND_FL);
153         }
154         jfs_inode->mode2 |= mode;
155
156         inode->i_blocks = 0;
157         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
158         jfs_inode->otime = inode->i_ctime.tv_sec;
159         inode->i_generation = JFS_SBI(sb)->gengen++;
160
161         jfs_inode->cflag = 0;
162
163         /* Zero remaining fields */
164         memset(&jfs_inode->acl, 0, sizeof(dxd_t));
165         memset(&jfs_inode->ea, 0, sizeof(dxd_t));
166         jfs_inode->next_index = 0;
167         jfs_inode->acltype = 0;
168         jfs_inode->btorder = 0;
169         jfs_inode->btindex = 0;
170         jfs_inode->bxflag = 0;
171         jfs_inode->blid = 0;
172         jfs_inode->atlhead = 0;
173         jfs_inode->atltail = 0;
174         jfs_inode->xtlid = 0;
175         jfs_set_inode_flags(inode);
176
177         jfs_info("ialloc returns inode = 0x%p\n", inode);
178
179         return inode;
180 }