Import 2.4.0-test2pre6
[davej-history.git] / fs / affs / inode.c
blob1551e613c7dace10942eaca9592c27c3812aeec9
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 <asm/div64.h>
15 #include <linux/errno.h>
16 #include <linux/fs.h>
17 #include <linux/malloc.h>
18 #include <linux/stat.h>
19 #include <linux/sched.h>
20 #include <linux/affs_fs.h>
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/string.h>
24 #include <linux/locks.h>
25 #include <linux/genhd.h>
26 #include <linux/amigaffs.h>
27 #include <linux/major.h>
28 #include <linux/blkdev.h>
29 #include <linux/init.h>
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
33 extern int *blk_size[];
34 extern struct timezone sys_tz;
35 extern struct inode_operations affs_symlink_inode_operations;
37 #define MIN(a,b) (((a)<(b))?(a):(b))
39 unsigned long
40 affs_parent_ino(struct inode *dir)
42 int root_ino = (dir->i_sb->u.affs_sb.s_root_block);
44 if (!S_ISDIR(dir->i_mode)) {
45 affs_error(dir->i_sb,"parent_ino","Trying to get parent of non-directory");
46 return root_ino;
48 if (dir->i_ino == root_ino)
49 return root_ino;
50 return dir->u.affs_i.i_parent;
53 void
54 affs_read_inode(struct inode *inode)
56 struct buffer_head *bh;
57 struct file_front *file_front;
58 struct file_end *file_end;
59 s32 block;
60 unsigned long prot;
61 s32 ptype, stype;
62 unsigned short id;
63 loff_t tmp;
65 pr_debug("AFFS: read_inode(%lu)\n",inode->i_ino);
67 block = inode->i_ino;
68 if (!(bh = affs_bread(inode->i_dev,block,AFFS_I2BSIZE(inode)))) {
69 affs_error(inode->i_sb,"read_inode","Cannot read block %d",block);
70 return;
72 if (affs_checksum_block(AFFS_I2BSIZE(inode),bh->b_data,&ptype,&stype) || ptype != T_SHORT) {
73 affs_error(inode->i_sb,"read_inode",
74 "Checksum or type (ptype=%d) error on inode %d",ptype,block);
75 affs_brelse(bh);
76 return;
79 file_front = (struct file_front *)bh->b_data;
80 file_end = GET_END_PTR(struct file_end, bh->b_data,AFFS_I2BSIZE(inode));
81 prot = (be32_to_cpu(file_end->protect) & ~0x10) ^ FIBF_OWNER;
83 inode->u.affs_i.i_protect = prot;
84 inode->u.affs_i.i_parent = be32_to_cpu(file_end->parent);
85 inode->u.affs_i.i_original = 0;
86 inode->u.affs_i.i_zone = 0;
87 inode->u.affs_i.i_hlink = 0;
88 inode->u.affs_i.i_pa_cnt = 0;
89 inode->u.affs_i.i_pa_next = 0;
90 inode->u.affs_i.i_pa_last = 0;
91 inode->u.affs_i.i_ec = NULL;
92 inode->u.affs_i.i_lastblock = -1;
93 inode->i_nlink = 1;
94 inode->i_mode = 0;
96 if (inode->i_sb->u.affs_sb.s_flags & SF_SETMODE)
97 inode->i_mode = inode->i_sb->u.affs_sb.s_mode;
98 else
99 inode->i_mode = prot_to_mode(prot);
101 if (inode->i_sb->u.affs_sb.s_flags & SF_SETUID)
102 inode->i_uid = inode->i_sb->u.affs_sb.s_uid;
103 id = be16_to_cpu(file_end->owner_uid);
104 if (id == 0 || inode->i_sb->u.affs_sb.s_flags & SF_SETUID)
105 inode->i_uid = inode->i_sb->u.affs_sb.s_uid;
106 else if (id == 0xFFFF && inode->i_sb->u.affs_sb.s_flags & SF_MUFS)
107 inode->i_uid = 0;
108 else
109 inode->i_uid = id;
111 id = be16_to_cpu(file_end->owner_gid);
112 if (id == 0 || inode->i_sb->u.affs_sb.s_flags & SF_SETGID)
113 inode->i_gid = inode->i_sb->u.affs_sb.s_gid;
114 else if (id == 0xFFFF && inode->i_sb->u.affs_sb.s_flags & SF_MUFS)
115 inode->i_gid = 0;
116 else
117 inode->i_gid = id;
119 switch (be32_to_cpu(file_end->secondary_type)) {
120 case ST_ROOT:
121 inode->i_uid = inode->i_sb->u.affs_sb.s_uid;
122 inode->i_gid = inode->i_sb->u.affs_sb.s_gid;
123 case ST_USERDIR:
124 if (be32_to_cpu(file_end->secondary_type) == ST_USERDIR ||
125 inode->i_sb->u.affs_sb.s_flags & SF_SETMODE) {
126 if (inode->i_mode & S_IRUSR)
127 inode->i_mode |= S_IXUSR;
128 if (inode->i_mode & S_IRGRP)
129 inode->i_mode |= S_IXGRP;
130 if (inode->i_mode & S_IROTH)
131 inode->i_mode |= S_IXOTH;
132 inode->i_mode |= S_IFDIR;
133 } else
134 inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
135 inode->i_size = 0;
136 break;
137 case ST_LINKDIR:
138 affs_error(inode->i_sb,"read_inode","inode is LINKDIR");
139 affs_brelse(bh);
140 return;
141 case ST_LINKFILE:
142 affs_error(inode->i_sb,"read_inode","inode is LINKFILE");
143 affs_brelse(bh);
144 return;
145 case ST_FILE:
146 inode->i_mode |= S_IFREG;
147 inode->i_size = be32_to_cpu(file_end->byte_size);
148 if (inode->i_sb->u.affs_sb.s_flags & SF_OFS)
149 block = AFFS_I2BSIZE(inode) - 24;
150 else
151 block = AFFS_I2BSIZE(inode);
152 tmp = inode->i_size + block -1;
153 do_div (tmp, block);
154 tmp--;
155 inode->u.affs_i.i_lastblock = tmp;
156 break;
157 case ST_SOFTLINK:
158 inode->i_mode |= S_IFLNK;
159 inode->i_size = 0;
160 break;
163 inode->i_mtime = inode->i_atime = inode->i_ctime
164 = (be32_to_cpu(file_end->created.ds_Days) * (24 * 60 * 60) +
165 be32_to_cpu(file_end->created.ds_Minute) * 60 +
166 be32_to_cpu(file_end->created.ds_Tick) / 50 +
167 ((8 * 365 + 2) * 24 * 60 * 60)) +
168 sys_tz.tz_minuteswest * 60;
169 affs_brelse(bh);
171 if (S_ISREG(inode->i_mode)) {
172 if (inode->i_sb->u.affs_sb.s_flags & SF_OFS) {
173 inode->i_op = &affs_file_inode_operations;
174 inode->i_fop = &affs_file_operations_ofs;
175 return;
177 inode->i_op = &affs_file_inode_operations;
178 inode->i_fop = &affs_file_operations;
179 inode->i_mapping->a_ops = &affs_aops;
180 inode->u.affs_i.mmu_private = inode->i_size;
181 } else if (S_ISDIR(inode->i_mode)) {
182 /* Maybe it should be controlled by mount parameter? */
183 inode->i_mode |= S_ISVTX;
184 inode->i_op = &affs_dir_inode_operations;
185 inode->i_fop = &affs_dir_operations;
187 else if (S_ISLNK(inode->i_mode)) {
188 inode->i_op = &affs_symlink_inode_operations;
189 inode->i_data.a_ops = &affs_symlink_aops;
193 void
194 affs_write_inode(struct inode *inode)
196 struct buffer_head *bh;
197 struct file_end *file_end;
198 uid_t uid;
199 gid_t gid;
201 pr_debug("AFFS: write_inode(%lu)\n",inode->i_ino);
203 if (!inode->i_nlink)
204 return;
205 if (!(bh = bread(inode->i_dev,inode->i_ino,AFFS_I2BSIZE(inode)))) {
206 affs_error(inode->i_sb,"write_inode","Cannot read block %lu",inode->i_ino);
207 return;
209 file_end = GET_END_PTR(struct file_end, bh->b_data,AFFS_I2BSIZE(inode));
210 if (file_end->secondary_type == be32_to_cpu(ST_ROOT)) {
211 secs_to_datestamp(inode->i_mtime,&ROOT_END(bh->b_data,inode)->disk_altered);
212 } else {
213 file_end->protect = cpu_to_be32(inode->u.affs_i.i_protect ^ FIBF_OWNER);
214 file_end->byte_size = cpu_to_be32(inode->i_size);
215 secs_to_datestamp(inode->i_mtime,&file_end->created);
216 if (!(inode->i_ino == inode->i_sb->u.affs_sb.s_root_block)) {
217 uid = inode->i_uid;
218 gid = inode->i_gid;
219 if (inode->i_sb->u.affs_sb.s_flags & SF_MUFS) {
220 if (inode->i_uid == 0 || inode->i_uid == 0xFFFF)
221 uid = inode->i_uid ^ ~0;
222 if (inode->i_gid == 0 || inode->i_gid == 0xFFFF)
223 gid = inode->i_gid ^ ~0;
225 if (!(inode->i_sb->u.affs_sb.s_flags & SF_SETUID))
226 file_end->owner_uid = cpu_to_be16(uid);
227 if (!(inode->i_sb->u.affs_sb.s_flags & SF_SETGID))
228 file_end->owner_gid = cpu_to_be16(gid);
231 affs_fix_checksum(AFFS_I2BSIZE(inode),bh->b_data,5);
232 mark_buffer_dirty(bh,1);
233 brelse(bh);
237 affs_notify_change(struct dentry *dentry, struct iattr *attr)
239 struct inode *inode = dentry->d_inode;
240 int error;
242 pr_debug("AFFS: notify_change(%lu,0x%x)\n",inode->i_ino,attr->ia_valid);
244 error = inode_change_ok(inode,attr);
245 if (error)
246 goto out;
248 if (((attr->ia_valid & ATTR_UID) && (inode->i_sb->u.affs_sb.s_flags & SF_SETUID)) ||
249 ((attr->ia_valid & ATTR_GID) && (inode->i_sb->u.affs_sb.s_flags & SF_SETGID)) ||
250 ((attr->ia_valid & ATTR_MODE) &&
251 (inode->i_sb->u.affs_sb.s_flags & (SF_SETMODE | SF_IMMUTABLE)))) {
252 if (!(inode->i_sb->u.affs_sb.s_flags & SF_QUIET))
253 error = -EPERM;
254 goto out;
257 if (attr->ia_valid & ATTR_MODE)
258 inode->u.affs_i.i_protect = mode_to_prot(attr->ia_mode);
260 error = 0;
261 inode_setattr(inode, attr);
262 out:
263 return error;
266 void
267 affs_put_inode(struct inode *inode)
269 pr_debug("AFFS: put_inode(ino=%lu, nlink=%u)\n",
270 inode->i_ino,inode->i_nlink);
272 affs_free_prealloc(inode);
273 if (atomic_read(&inode->i_count) == 1) {
274 unsigned long cache_page = (unsigned long) inode->u.affs_i.i_ec;
275 if (cache_page) {
276 pr_debug("AFFS: freeing ext cache\n");
277 inode->u.affs_i.i_ec = NULL;
278 free_page(cache_page);
283 void
284 affs_delete_inode(struct inode *inode)
286 pr_debug("AFFS: delete_inode(ino=%lu, nlink=%u)\n",inode->i_ino,inode->i_nlink);
287 inode->i_size = 0;
288 if (S_ISREG(inode->i_mode) && !inode->u.affs_i.i_hlink)
289 affs_truncate(inode);
290 affs_free_block(inode->i_sb,inode->i_ino);
291 clear_inode(inode);
294 struct inode *
295 affs_new_inode(const struct inode *dir)
297 struct inode *inode;
298 struct super_block *sb;
299 s32 block;
301 if (!dir || !(inode = get_empty_inode()))
302 return NULL;
304 sb = dir->i_sb;
305 inode->i_sb = sb;
307 if (!(block = affs_new_header((struct inode *)dir))) {
308 iput(inode);
309 return NULL;
312 inode->i_dev = sb->s_dev;
313 inode->i_uid = current->fsuid;
314 inode->i_gid = current->fsgid;
315 inode->i_ino = block;
316 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
318 inode->u.affs_i.i_original = 0;
319 inode->u.affs_i.i_parent = dir->i_ino;
320 inode->u.affs_i.i_zone = 0;
321 inode->u.affs_i.i_hlink = 0;
322 inode->u.affs_i.i_pa_cnt = 0;
323 inode->u.affs_i.i_pa_next = 0;
324 inode->u.affs_i.i_pa_last = 0;
325 inode->u.affs_i.i_ec = NULL;
326 inode->u.affs_i.i_lastblock = -1;
328 insert_inode_hash(inode);
329 mark_inode_dirty(inode);
331 return inode;
335 * Add an entry to a directory. Create the header block
336 * and insert it into the hash table.
340 affs_add_entry(struct inode *dir, struct inode *link, struct inode *inode,
341 struct dentry *dentry, int type)
343 struct buffer_head *dir_bh;
344 struct buffer_head *inode_bh;
345 struct buffer_head *link_bh;
346 int retval;
347 const unsigned char *name = dentry->d_name.name;
348 int len = dentry->d_name.len;
350 pr_debug("AFFS: add_entry(dir=%lu,inode=%lu,\"%*s\",type=%d)\n",dir->i_ino,inode->i_ino,
351 len,name,type);
353 if ((retval = affs_check_name(name,len)))
354 return retval;
355 if (len > 30)
356 len = 30;
358 dir_bh = affs_bread(dir->i_dev,dir->i_ino,AFFS_I2BSIZE(dir));
359 inode_bh = affs_bread(inode->i_dev,inode->i_ino,AFFS_I2BSIZE(inode));
360 link_bh = NULL;
361 retval = -EIO;
362 if (!dir_bh || !inode_bh)
363 goto addentry_done;
364 if (link) {
365 link_bh = affs_bread(link->i_dev,link->i_ino,AFFS_I2BSIZE(link));
366 if (!link_bh)
367 goto addentry_done;
369 ((struct dir_front *)inode_bh->b_data)->primary_type = cpu_to_be32(T_SHORT);
370 ((struct dir_front *)inode_bh->b_data)->own_key = cpu_to_be32(inode->i_ino);
371 DIR_END(inode_bh->b_data,inode)->dir_name[0] = len;
372 strncpy(DIR_END(inode_bh->b_data,inode)->dir_name + 1,name,len);
373 DIR_END(inode_bh->b_data,inode)->secondary_type = cpu_to_be32(type);
374 DIR_END(inode_bh->b_data,inode)->parent = cpu_to_be32(dir->i_ino);
376 lock_super(inode->i_sb);
377 retval = affs_insert_hash(dir->i_ino,inode_bh,dir);
379 if (link_bh) {
380 LINK_END(inode_bh->b_data,inode)->original = cpu_to_be32(link->i_ino);
381 LINK_END(inode_bh->b_data,inode)->link_chain =
382 FILE_END(link_bh->b_data,link)->link_chain;
383 FILE_END(link_bh->b_data,link)->link_chain = cpu_to_be32(inode->i_ino);
384 affs_fix_checksum(AFFS_I2BSIZE(link),link_bh->b_data,5);
385 link->i_version = ++event;
386 mark_inode_dirty(link);
387 mark_buffer_dirty(link_bh,1);
389 affs_fix_checksum(AFFS_I2BSIZE(inode),inode_bh->b_data,5);
390 affs_fix_checksum(AFFS_I2BSIZE(dir),dir_bh->b_data,5);
391 dir->i_version = ++event;
392 dir->i_mtime = dir->i_atime = dir->i_ctime = CURRENT_TIME;
393 unlock_super(inode->i_sb);
395 mark_inode_dirty(dir);
396 mark_inode_dirty(inode);
397 mark_buffer_dirty(dir_bh,1);
398 mark_buffer_dirty(inode_bh,1);
400 addentry_done:
401 affs_brelse(dir_bh);
402 affs_brelse(inode_bh);
403 affs_brelse(link_bh);
405 return retval;