Import 2.3.49pre2
[davej-history.git] / fs / affs / inode.c
blob780b99ad3bf657b4e253c51b13117962aa763ed3
1 /*
2 * linux/fs/affs/inode.c
4 * (c) 1996 Hans-Joachim Widmaier - Rewritten
6 * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
8 * (C) 1992 Eric Youngdale Modified for ISO9660 filesystem.
10 * (C) 1991 Linus Torvalds - minix filesystem
13 #define DEBUG 0
14 #include <linux/errno.h>
15 #include <linux/fs.h>
16 #include <linux/malloc.h>
17 #include <linux/stat.h>
18 #include <linux/sched.h>
19 #include <linux/affs_fs.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/string.h>
23 #include <linux/locks.h>
24 #include <linux/genhd.h>
25 #include <linux/amigaffs.h>
26 #include <linux/major.h>
27 #include <linux/blkdev.h>
28 #include <linux/init.h>
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
32 extern int *blk_size[];
33 extern struct timezone sys_tz;
34 extern struct inode_operations affs_symlink_inode_operations;
36 #define MIN(a,b) (((a)<(b))?(a):(b))
38 unsigned long
39 affs_parent_ino(struct inode *dir)
41 int root_ino = (dir->i_sb->u.affs_sb.s_root_block);
43 if (!S_ISDIR(dir->i_mode)) {
44 affs_error(dir->i_sb,"parent_ino","Trying to get parent of non-directory");
45 return root_ino;
47 if (dir->i_ino == root_ino)
48 return root_ino;
49 return dir->u.affs_i.i_parent;
52 void
53 affs_read_inode(struct inode *inode)
55 struct buffer_head *bh;
56 struct file_front *file_front;
57 struct file_end *file_end;
58 s32 block;
59 unsigned long prot;
60 s32 ptype, stype;
61 unsigned short id;
63 pr_debug("AFFS: read_inode(%lu)\n",inode->i_ino);
65 block = inode->i_ino;
66 if (!(bh = affs_bread(inode->i_dev,block,AFFS_I2BSIZE(inode)))) {
67 affs_error(inode->i_sb,"read_inode","Cannot read block %d",block);
68 return;
70 if (affs_checksum_block(AFFS_I2BSIZE(inode),bh->b_data,&ptype,&stype) || ptype != T_SHORT) {
71 affs_error(inode->i_sb,"read_inode",
72 "Checksum or type (ptype=%d) error on inode %d",ptype,block);
73 affs_brelse(bh);
74 return;
77 file_front = (struct file_front *)bh->b_data;
78 file_end = GET_END_PTR(struct file_end, bh->b_data,AFFS_I2BSIZE(inode));
79 prot = (be32_to_cpu(file_end->protect) & ~0x10) ^ FIBF_OWNER;
81 inode->u.affs_i.i_protect = prot;
82 inode->u.affs_i.i_parent = be32_to_cpu(file_end->parent);
83 inode->u.affs_i.i_original = 0;
84 inode->u.affs_i.i_zone = 0;
85 inode->u.affs_i.i_hlink = 0;
86 inode->u.affs_i.i_pa_cnt = 0;
87 inode->u.affs_i.i_pa_next = 0;
88 inode->u.affs_i.i_pa_last = 0;
89 inode->u.affs_i.i_ec = NULL;
90 inode->u.affs_i.i_lastblock = -1;
91 inode->i_nlink = 1;
92 inode->i_mode = 0;
94 if (inode->i_sb->u.affs_sb.s_flags & SF_SETMODE)
95 inode->i_mode = inode->i_sb->u.affs_sb.s_mode;
96 else
97 inode->i_mode = prot_to_mode(prot);
99 if (inode->i_sb->u.affs_sb.s_flags & SF_SETUID)
100 inode->i_uid = inode->i_sb->u.affs_sb.s_uid;
101 id = be16_to_cpu(file_end->owner_uid);
102 if (id == 0 || inode->i_sb->u.affs_sb.s_flags & SF_SETUID)
103 inode->i_uid = inode->i_sb->u.affs_sb.s_uid;
104 else if (id == 0xFFFF && inode->i_sb->u.affs_sb.s_flags & SF_MUFS)
105 inode->i_uid = 0;
106 else
107 inode->i_uid = id;
109 id = be16_to_cpu(file_end->owner_gid);
110 if (id == 0 || inode->i_sb->u.affs_sb.s_flags & SF_SETGID)
111 inode->i_gid = inode->i_sb->u.affs_sb.s_gid;
112 else if (id == 0xFFFF && inode->i_sb->u.affs_sb.s_flags & SF_MUFS)
113 inode->i_gid = 0;
114 else
115 inode->i_gid = id;
117 switch (be32_to_cpu(file_end->secondary_type)) {
118 case ST_ROOT:
119 inode->i_uid = inode->i_sb->u.affs_sb.s_uid;
120 inode->i_gid = inode->i_sb->u.affs_sb.s_gid;
121 case ST_USERDIR:
122 if (be32_to_cpu(file_end->secondary_type) == ST_USERDIR ||
123 inode->i_sb->u.affs_sb.s_flags & SF_SETMODE) {
124 if (inode->i_mode & S_IRUSR)
125 inode->i_mode |= S_IXUSR;
126 if (inode->i_mode & S_IRGRP)
127 inode->i_mode |= S_IXGRP;
128 if (inode->i_mode & S_IROTH)
129 inode->i_mode |= S_IXOTH;
130 inode->i_mode |= S_IFDIR;
131 } else
132 inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
133 inode->i_size = 0;
134 break;
135 case ST_LINKDIR:
136 affs_error(inode->i_sb,"read_inode","inode is LINKDIR");
137 affs_brelse(bh);
138 return;
139 case ST_LINKFILE:
140 affs_error(inode->i_sb,"read_inode","inode is LINKFILE");
141 affs_brelse(bh);
142 return;
143 case ST_FILE:
144 inode->i_mode |= S_IFREG;
145 inode->i_size = be32_to_cpu(file_end->byte_size);
146 if (inode->i_sb->u.affs_sb.s_flags & SF_OFS)
147 block = AFFS_I2BSIZE(inode) - 24;
148 else
149 block = AFFS_I2BSIZE(inode);
150 inode->u.affs_i.i_lastblock = ((inode->i_size + block - 1) / block) - 1;
151 break;
152 case ST_SOFTLINK:
153 inode->i_mode |= S_IFLNK;
154 inode->i_size = 0;
155 break;
158 inode->i_mtime = inode->i_atime = inode->i_ctime
159 = (be32_to_cpu(file_end->created.ds_Days) * (24 * 60 * 60) +
160 be32_to_cpu(file_end->created.ds_Minute) * 60 +
161 be32_to_cpu(file_end->created.ds_Tick) / 50 +
162 ((8 * 365 + 2) * 24 * 60 * 60)) +
163 sys_tz.tz_minuteswest * 60;
164 affs_brelse(bh);
166 if (S_ISREG(inode->i_mode)) {
167 if (inode->i_sb->u.affs_sb.s_flags & SF_OFS) {
168 inode->i_op = &affs_file_inode_operations;
169 inode->i_fop = &affs_file_operations_ofs;
170 return;
172 inode->i_op = &affs_file_inode_operations;
173 inode->i_fop = &affs_file_operations;
174 inode->i_mapping->a_ops = &affs_aops;
175 inode->u.affs_i.mmu_private = inode->i_size;
176 } else if (S_ISDIR(inode->i_mode)) {
177 /* Maybe it should be controlled by mount parameter? */
178 inode->i_mode |= S_ISVTX;
179 inode->i_op = &affs_dir_inode_operations;
180 inode->i_fop = &affs_dir_operations;
182 else if (S_ISLNK(inode->i_mode)) {
183 inode->i_op = &affs_symlink_inode_operations;
184 inode->i_data.a_ops = &affs_symlink_aops;
188 void
189 affs_write_inode(struct inode *inode)
191 struct buffer_head *bh;
192 struct file_end *file_end;
193 uid_t uid;
194 gid_t gid;
196 pr_debug("AFFS: write_inode(%lu)\n",inode->i_ino);
198 if (!inode->i_nlink)
199 return;
200 if (!(bh = bread(inode->i_dev,inode->i_ino,AFFS_I2BSIZE(inode)))) {
201 affs_error(inode->i_sb,"write_inode","Cannot read block %lu",inode->i_ino);
202 return;
204 file_end = GET_END_PTR(struct file_end, bh->b_data,AFFS_I2BSIZE(inode));
205 if (file_end->secondary_type == be32_to_cpu(ST_ROOT)) {
206 secs_to_datestamp(inode->i_mtime,&ROOT_END(bh->b_data,inode)->disk_altered);
207 } else {
208 file_end->protect = cpu_to_be32(inode->u.affs_i.i_protect ^ FIBF_OWNER);
209 file_end->byte_size = cpu_to_be32(inode->i_size);
210 secs_to_datestamp(inode->i_mtime,&file_end->created);
211 if (!(inode->i_ino == inode->i_sb->u.affs_sb.s_root_block)) {
212 uid = inode->i_uid;
213 gid = inode->i_gid;
214 if (inode->i_sb->u.affs_sb.s_flags & SF_MUFS) {
215 if (inode->i_uid == 0 || inode->i_uid == 0xFFFF)
216 uid = inode->i_uid ^ ~0;
217 if (inode->i_gid == 0 || inode->i_gid == 0xFFFF)
218 gid = inode->i_gid ^ ~0;
220 if (!(inode->i_sb->u.affs_sb.s_flags & SF_SETUID))
221 file_end->owner_uid = cpu_to_be16(uid);
222 if (!(inode->i_sb->u.affs_sb.s_flags & SF_SETGID))
223 file_end->owner_gid = cpu_to_be16(gid);
226 affs_fix_checksum(AFFS_I2BSIZE(inode),bh->b_data,5);
227 mark_buffer_dirty(bh,1);
228 brelse(bh);
232 affs_notify_change(struct dentry *dentry, struct iattr *attr)
234 struct inode *inode = dentry->d_inode;
235 int error;
237 pr_debug("AFFS: notify_change(%lu,0x%x)\n",inode->i_ino,attr->ia_valid);
239 error = inode_change_ok(inode,attr);
240 if (error)
241 goto out;
243 if (((attr->ia_valid & ATTR_UID) && (inode->i_sb->u.affs_sb.s_flags & SF_SETUID)) ||
244 ((attr->ia_valid & ATTR_GID) && (inode->i_sb->u.affs_sb.s_flags & SF_SETGID)) ||
245 ((attr->ia_valid & ATTR_MODE) &&
246 (inode->i_sb->u.affs_sb.s_flags & (SF_SETMODE | SF_IMMUTABLE)))) {
247 if (!(inode->i_sb->u.affs_sb.s_flags & SF_QUIET))
248 error = -EPERM;
249 goto out;
252 if (attr->ia_valid & ATTR_MODE)
253 inode->u.affs_i.i_protect = mode_to_prot(attr->ia_mode);
255 error = 0;
256 inode_setattr(inode, attr);
257 out:
258 return error;
261 void
262 affs_put_inode(struct inode *inode)
264 pr_debug("AFFS: put_inode(ino=%lu, nlink=%u)\n",
265 inode->i_ino,inode->i_nlink);
267 affs_free_prealloc(inode);
268 if (inode->i_count == 1) {
269 unsigned long cache_page = (unsigned long) inode->u.affs_i.i_ec;
270 if (cache_page) {
271 pr_debug("AFFS: freeing ext cache\n");
272 inode->u.affs_i.i_ec = NULL;
273 free_page(cache_page);
278 void
279 affs_delete_inode(struct inode *inode)
281 pr_debug("AFFS: delete_inode(ino=%lu, nlink=%u)\n",inode->i_ino,inode->i_nlink);
282 inode->i_size = 0;
283 if (S_ISREG(inode->i_mode) && !inode->u.affs_i.i_hlink)
284 affs_truncate(inode);
285 affs_free_block(inode->i_sb,inode->i_ino);
286 clear_inode(inode);
289 struct inode *
290 affs_new_inode(const struct inode *dir)
292 struct inode *inode;
293 struct super_block *sb;
294 s32 block;
296 if (!dir || !(inode = get_empty_inode()))
297 return NULL;
299 sb = dir->i_sb;
300 inode->i_sb = sb;
302 if (!(block = affs_new_header((struct inode *)dir))) {
303 iput(inode);
304 return NULL;
307 inode->i_dev = sb->s_dev;
308 inode->i_uid = current->fsuid;
309 inode->i_gid = current->fsgid;
310 inode->i_ino = block;
311 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
313 inode->u.affs_i.i_original = 0;
314 inode->u.affs_i.i_parent = dir->i_ino;
315 inode->u.affs_i.i_zone = 0;
316 inode->u.affs_i.i_hlink = 0;
317 inode->u.affs_i.i_pa_cnt = 0;
318 inode->u.affs_i.i_pa_next = 0;
319 inode->u.affs_i.i_pa_last = 0;
320 inode->u.affs_i.i_ec = NULL;
321 inode->u.affs_i.i_lastblock = -1;
323 insert_inode_hash(inode);
324 mark_inode_dirty(inode);
326 return inode;
330 * Add an entry to a directory. Create the header block
331 * and insert it into the hash table.
335 affs_add_entry(struct inode *dir, struct inode *link, struct inode *inode,
336 struct dentry *dentry, int type)
338 struct buffer_head *dir_bh;
339 struct buffer_head *inode_bh;
340 struct buffer_head *link_bh;
341 int retval;
342 const unsigned char *name = dentry->d_name.name;
343 int len = dentry->d_name.len;
345 pr_debug("AFFS: add_entry(dir=%lu,inode=%lu,\"%*s\",type=%d)\n",dir->i_ino,inode->i_ino,
346 len,name,type);
348 if ((retval = affs_check_name(name,len)))
349 return retval;
350 if (len > 30)
351 len = 30;
353 dir_bh = affs_bread(dir->i_dev,dir->i_ino,AFFS_I2BSIZE(dir));
354 inode_bh = affs_bread(inode->i_dev,inode->i_ino,AFFS_I2BSIZE(inode));
355 link_bh = NULL;
356 retval = -EIO;
357 if (!dir_bh || !inode_bh)
358 goto addentry_done;
359 if (link) {
360 link_bh = affs_bread(link->i_dev,link->i_ino,AFFS_I2BSIZE(link));
361 if (!link_bh)
362 goto addentry_done;
364 ((struct dir_front *)inode_bh->b_data)->primary_type = cpu_to_be32(T_SHORT);
365 ((struct dir_front *)inode_bh->b_data)->own_key = cpu_to_be32(inode->i_ino);
366 DIR_END(inode_bh->b_data,inode)->dir_name[0] = len;
367 strncpy(DIR_END(inode_bh->b_data,inode)->dir_name + 1,name,len);
368 DIR_END(inode_bh->b_data,inode)->secondary_type = cpu_to_be32(type);
369 DIR_END(inode_bh->b_data,inode)->parent = cpu_to_be32(dir->i_ino);
371 lock_super(inode->i_sb);
372 retval = affs_insert_hash(dir->i_ino,inode_bh,dir);
374 if (link_bh) {
375 LINK_END(inode_bh->b_data,inode)->original = cpu_to_be32(link->i_ino);
376 LINK_END(inode_bh->b_data,inode)->link_chain =
377 FILE_END(link_bh->b_data,link)->link_chain;
378 FILE_END(link_bh->b_data,link)->link_chain = cpu_to_be32(inode->i_ino);
379 affs_fix_checksum(AFFS_I2BSIZE(link),link_bh->b_data,5);
380 link->i_version = ++event;
381 mark_inode_dirty(link);
382 mark_buffer_dirty(link_bh,1);
384 affs_fix_checksum(AFFS_I2BSIZE(inode),inode_bh->b_data,5);
385 affs_fix_checksum(AFFS_I2BSIZE(dir),dir_bh->b_data,5);
386 dir->i_version = ++event;
387 dir->i_mtime = dir->i_atime = dir->i_ctime = CURRENT_TIME;
388 unlock_super(inode->i_sb);
390 mark_inode_dirty(dir);
391 mark_inode_dirty(inode);
392 mark_buffer_dirty(dir_bh,1);
393 mark_buffer_dirty(inode_bh,1);
395 addentry_done:
396 affs_brelse(dir_bh);
397 affs_brelse(inode_bh);
398 affs_brelse(link_bh);
400 return retval;