Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / fs / affs / inode.c
blobfcb7e103a56f3a919b72a93626f2858d4300732c
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 <linux/smp_lock.h>
31 #include <asm/system.h>
32 #include <asm/uaccess.h>
34 extern int *blk_size[];
35 extern struct timezone sys_tz;
36 extern struct inode_operations affs_symlink_inode_operations;
38 #define MIN(a,b) (((a)<(b))?(a):(b))
40 unsigned long
41 affs_parent_ino(struct inode *dir)
43 int root_ino = (dir->i_sb->u.affs_sb.s_root_block);
45 if (!S_ISDIR(dir->i_mode)) {
46 affs_error(dir->i_sb,"parent_ino","Trying to get parent of non-directory");
47 return root_ino;
49 if (dir->i_ino == root_ino)
50 return root_ino;
51 return dir->u.affs_i.i_parent;
54 void
55 affs_read_inode(struct inode *inode)
57 struct buffer_head *bh;
58 struct file_front *file_front;
59 struct file_end *file_end;
60 s32 block;
61 unsigned long prot;
62 s32 ptype, stype;
63 unsigned short id;
64 loff_t tmp;
66 pr_debug("AFFS: read_inode(%lu)\n",inode->i_ino);
68 block = inode->i_ino;
69 if (!(bh = affs_bread(inode->i_dev,block,AFFS_I2BSIZE(inode)))) {
70 affs_error(inode->i_sb,"read_inode","Cannot read block %d",block);
71 return;
73 if (affs_checksum_block(AFFS_I2BSIZE(inode),bh->b_data,&ptype,&stype) || ptype != T_SHORT) {
74 affs_error(inode->i_sb,"read_inode",
75 "Checksum or type (ptype=%d) error on inode %d",ptype,block);
76 affs_brelse(bh);
77 return;
80 file_front = (struct file_front *)bh->b_data;
81 file_end = GET_END_PTR(struct file_end, bh->b_data,AFFS_I2BSIZE(inode));
82 prot = (be32_to_cpu(file_end->protect) & ~0x10) ^ FIBF_OWNER;
84 inode->u.affs_i.i_protect = prot;
85 inode->u.affs_i.i_parent = be32_to_cpu(file_end->parent);
86 inode->u.affs_i.i_original = 0;
87 inode->u.affs_i.i_zone = 0;
88 inode->u.affs_i.i_hlink = 0;
89 inode->u.affs_i.i_pa_cnt = 0;
90 inode->u.affs_i.i_pa_next = 0;
91 inode->u.affs_i.i_pa_last = 0;
92 inode->u.affs_i.i_ec = NULL;
93 inode->u.affs_i.i_lastblock = -1;
94 inode->i_nlink = 1;
95 inode->i_mode = 0;
97 if (inode->i_sb->u.affs_sb.s_flags & SF_SETMODE)
98 inode->i_mode = inode->i_sb->u.affs_sb.s_mode;
99 else
100 inode->i_mode = prot_to_mode(prot);
102 if (inode->i_sb->u.affs_sb.s_flags & SF_SETUID)
103 inode->i_uid = inode->i_sb->u.affs_sb.s_uid;
104 id = be16_to_cpu(file_end->owner_uid);
105 if (id == 0 || inode->i_sb->u.affs_sb.s_flags & SF_SETUID)
106 inode->i_uid = inode->i_sb->u.affs_sb.s_uid;
107 else if (id == 0xFFFF && inode->i_sb->u.affs_sb.s_flags & SF_MUFS)
108 inode->i_uid = 0;
109 else
110 inode->i_uid = id;
112 id = be16_to_cpu(file_end->owner_gid);
113 if (id == 0 || inode->i_sb->u.affs_sb.s_flags & SF_SETGID)
114 inode->i_gid = inode->i_sb->u.affs_sb.s_gid;
115 else if (id == 0xFFFF && inode->i_sb->u.affs_sb.s_flags & SF_MUFS)
116 inode->i_gid = 0;
117 else
118 inode->i_gid = id;
120 switch (be32_to_cpu(file_end->secondary_type)) {
121 case ST_ROOT:
122 inode->i_uid = inode->i_sb->u.affs_sb.s_uid;
123 inode->i_gid = inode->i_sb->u.affs_sb.s_gid;
124 case ST_USERDIR:
125 if (be32_to_cpu(file_end->secondary_type) == ST_USERDIR ||
126 inode->i_sb->u.affs_sb.s_flags & SF_SETMODE) {
127 if (inode->i_mode & S_IRUSR)
128 inode->i_mode |= S_IXUSR;
129 if (inode->i_mode & S_IRGRP)
130 inode->i_mode |= S_IXGRP;
131 if (inode->i_mode & S_IROTH)
132 inode->i_mode |= S_IXOTH;
133 inode->i_mode |= S_IFDIR;
134 } else
135 inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
136 inode->i_size = 0;
137 break;
138 case ST_LINKDIR:
139 affs_error(inode->i_sb,"read_inode","inode is LINKDIR");
140 affs_brelse(bh);
141 return;
142 case ST_LINKFILE:
143 affs_error(inode->i_sb,"read_inode","inode is LINKFILE");
144 affs_brelse(bh);
145 return;
146 case ST_FILE:
147 inode->i_mode |= S_IFREG;
148 inode->i_size = be32_to_cpu(file_end->byte_size);
149 if (inode->i_sb->u.affs_sb.s_flags & SF_OFS)
150 block = AFFS_I2BSIZE(inode) - 24;
151 else
152 block = AFFS_I2BSIZE(inode);
153 tmp = inode->i_size + block -1;
154 do_div (tmp, block);
155 tmp--;
156 inode->u.affs_i.i_lastblock = tmp;
157 break;
158 case ST_SOFTLINK:
159 inode->i_mode |= S_IFLNK;
160 inode->i_size = 0;
161 break;
164 inode->i_mtime = inode->i_atime = inode->i_ctime
165 = (be32_to_cpu(file_end->created.ds_Days) * (24 * 60 * 60) +
166 be32_to_cpu(file_end->created.ds_Minute) * 60 +
167 be32_to_cpu(file_end->created.ds_Tick) / 50 +
168 ((8 * 365 + 2) * 24 * 60 * 60)) +
169 sys_tz.tz_minuteswest * 60;
170 affs_brelse(bh);
172 if (S_ISREG(inode->i_mode)) {
173 if (inode->i_sb->u.affs_sb.s_flags & SF_OFS) {
174 inode->i_op = &affs_file_inode_operations;
175 inode->i_fop = &affs_file_operations_ofs;
176 return;
178 inode->i_op = &affs_file_inode_operations;
179 inode->i_fop = &affs_file_operations;
180 inode->i_mapping->a_ops = &affs_aops;
181 inode->u.affs_i.mmu_private = inode->i_size;
182 } else if (S_ISDIR(inode->i_mode)) {
183 /* Maybe it should be controlled by mount parameter? */
184 inode->i_mode |= S_ISVTX;
185 inode->i_op = &affs_dir_inode_operations;
186 inode->i_fop = &affs_dir_operations;
188 else if (S_ISLNK(inode->i_mode)) {
189 inode->i_op = &affs_symlink_inode_operations;
190 inode->i_data.a_ops = &affs_symlink_aops;
194 void
195 affs_write_inode(struct inode *inode, int unused)
197 struct buffer_head *bh;
198 struct file_end *file_end;
199 uid_t uid;
200 gid_t gid;
202 pr_debug("AFFS: write_inode(%lu)\n",inode->i_ino);
204 if (!inode->i_nlink)
205 return;
206 lock_kernel();
207 if (!(bh = bread(inode->i_dev,inode->i_ino,AFFS_I2BSIZE(inode)))) {
208 affs_error(inode->i_sb,"write_inode","Cannot read block %lu",inode->i_ino);
209 unlock_kernel();
210 return;
212 file_end = GET_END_PTR(struct file_end, bh->b_data,AFFS_I2BSIZE(inode));
213 if (file_end->secondary_type == be32_to_cpu(ST_ROOT)) {
214 secs_to_datestamp(inode->i_mtime,&ROOT_END(bh->b_data,inode)->disk_altered);
215 } else {
216 file_end->protect = cpu_to_be32(inode->u.affs_i.i_protect ^ FIBF_OWNER);
217 file_end->byte_size = cpu_to_be32(inode->i_size);
218 secs_to_datestamp(inode->i_mtime,&file_end->created);
219 if (!(inode->i_ino == inode->i_sb->u.affs_sb.s_root_block)) {
220 uid = inode->i_uid;
221 gid = inode->i_gid;
222 if (inode->i_sb->u.affs_sb.s_flags & SF_MUFS) {
223 if (inode->i_uid == 0 || inode->i_uid == 0xFFFF)
224 uid = inode->i_uid ^ ~0;
225 if (inode->i_gid == 0 || inode->i_gid == 0xFFFF)
226 gid = inode->i_gid ^ ~0;
228 if (!(inode->i_sb->u.affs_sb.s_flags & SF_SETUID))
229 file_end->owner_uid = cpu_to_be16(uid);
230 if (!(inode->i_sb->u.affs_sb.s_flags & SF_SETGID))
231 file_end->owner_gid = cpu_to_be16(gid);
234 affs_fix_checksum(AFFS_I2BSIZE(inode),bh->b_data,5);
235 mark_buffer_dirty(bh);
236 brelse(bh);
237 unlock_kernel();
241 affs_notify_change(struct dentry *dentry, struct iattr *attr)
243 struct inode *inode = dentry->d_inode;
244 int error;
246 pr_debug("AFFS: notify_change(%lu,0x%x)\n",inode->i_ino,attr->ia_valid);
248 error = inode_change_ok(inode,attr);
249 if (error)
250 goto out;
252 if (((attr->ia_valid & ATTR_UID) && (inode->i_sb->u.affs_sb.s_flags & SF_SETUID)) ||
253 ((attr->ia_valid & ATTR_GID) && (inode->i_sb->u.affs_sb.s_flags & SF_SETGID)) ||
254 ((attr->ia_valid & ATTR_MODE) &&
255 (inode->i_sb->u.affs_sb.s_flags & (SF_SETMODE | SF_IMMUTABLE)))) {
256 if (!(inode->i_sb->u.affs_sb.s_flags & SF_QUIET))
257 error = -EPERM;
258 goto out;
261 if (attr->ia_valid & ATTR_MODE)
262 inode->u.affs_i.i_protect = mode_to_prot(attr->ia_mode);
264 error = 0;
265 inode_setattr(inode, attr);
266 out:
267 return error;
270 void
271 affs_put_inode(struct inode *inode)
273 pr_debug("AFFS: put_inode(ino=%lu, nlink=%u)\n",
274 inode->i_ino,inode->i_nlink);
276 lock_kernel();
277 affs_free_prealloc(inode);
278 if (atomic_read(&inode->i_count) == 1) {
279 unsigned long cache_page = (unsigned long) inode->u.affs_i.i_ec;
280 if (cache_page) {
281 pr_debug("AFFS: freeing ext cache\n");
282 inode->u.affs_i.i_ec = NULL;
283 free_page(cache_page);
286 unlock_kernel();
289 void
290 affs_delete_inode(struct inode *inode)
292 pr_debug("AFFS: delete_inode(ino=%lu, nlink=%u)\n",inode->i_ino,inode->i_nlink);
293 lock_kernel();
294 inode->i_size = 0;
295 if (S_ISREG(inode->i_mode) && !inode->u.affs_i.i_hlink)
296 affs_truncate(inode);
297 affs_free_block(inode->i_sb,inode->i_ino);
298 unlock_kernel();
299 clear_inode(inode);
302 struct inode *
303 affs_new_inode(const struct inode *dir)
305 struct inode *inode;
306 struct super_block *sb;
307 s32 block;
309 if (!dir)
310 return NULL;
312 sb = dir->i_sb;
313 inode = new_inode(sb);
314 if (!inode)
315 return NULL;
317 if (!(block = affs_new_header((struct inode *)dir))) {
318 iput(inode);
319 return NULL;
322 inode->i_uid = current->fsuid;
323 inode->i_gid = current->fsgid;
324 inode->i_ino = block;
325 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
327 inode->u.affs_i.i_original = 0;
328 inode->u.affs_i.i_parent = dir->i_ino;
329 inode->u.affs_i.i_zone = 0;
330 inode->u.affs_i.i_hlink = 0;
331 inode->u.affs_i.i_pa_cnt = 0;
332 inode->u.affs_i.i_pa_next = 0;
333 inode->u.affs_i.i_pa_last = 0;
334 inode->u.affs_i.i_ec = NULL;
335 inode->u.affs_i.i_lastblock = -1;
337 insert_inode_hash(inode);
338 mark_inode_dirty(inode);
340 return inode;
344 * Add an entry to a directory. Create the header block
345 * and insert it into the hash table.
349 affs_add_entry(struct inode *dir, struct inode *link, struct inode *inode,
350 struct dentry *dentry, int type)
352 struct buffer_head *dir_bh;
353 struct buffer_head *inode_bh;
354 struct buffer_head *link_bh;
355 int retval;
356 const unsigned char *name = dentry->d_name.name;
357 int len = dentry->d_name.len;
359 pr_debug("AFFS: add_entry(dir=%lu,inode=%lu,\"%*s\",type=%d)\n",dir->i_ino,inode->i_ino,
360 len,name,type);
362 if ((retval = affs_check_name(name,len)))
363 return retval;
364 if (len > 30)
365 len = 30;
367 dir_bh = affs_bread(dir->i_dev,dir->i_ino,AFFS_I2BSIZE(dir));
368 inode_bh = affs_bread(inode->i_dev,inode->i_ino,AFFS_I2BSIZE(inode));
369 link_bh = NULL;
370 retval = -EIO;
371 if (!dir_bh || !inode_bh)
372 goto addentry_done;
373 if (link) {
374 link_bh = affs_bread(link->i_dev,link->i_ino,AFFS_I2BSIZE(link));
375 if (!link_bh)
376 goto addentry_done;
378 ((struct dir_front *)inode_bh->b_data)->primary_type = cpu_to_be32(T_SHORT);
379 ((struct dir_front *)inode_bh->b_data)->own_key = cpu_to_be32(inode->i_ino);
380 DIR_END(inode_bh->b_data,inode)->dir_name[0] = len;
381 strncpy(DIR_END(inode_bh->b_data,inode)->dir_name + 1,name,len);
382 DIR_END(inode_bh->b_data,inode)->secondary_type = cpu_to_be32(type);
383 DIR_END(inode_bh->b_data,inode)->parent = cpu_to_be32(dir->i_ino);
385 lock_super(inode->i_sb);
386 retval = affs_insert_hash(dir->i_ino,inode_bh,dir);
388 if (link_bh) {
389 LINK_END(inode_bh->b_data,inode)->original = cpu_to_be32(link->i_ino);
390 LINK_END(inode_bh->b_data,inode)->link_chain =
391 FILE_END(link_bh->b_data,link)->link_chain;
392 FILE_END(link_bh->b_data,link)->link_chain = cpu_to_be32(inode->i_ino);
393 affs_fix_checksum(AFFS_I2BSIZE(link),link_bh->b_data,5);
394 link->i_version = ++event;
395 mark_inode_dirty(link);
396 mark_buffer_dirty(link_bh);
398 affs_fix_checksum(AFFS_I2BSIZE(inode),inode_bh->b_data,5);
399 affs_fix_checksum(AFFS_I2BSIZE(dir),dir_bh->b_data,5);
400 dir->i_version = ++event;
401 dir->i_mtime = dir->i_atime = dir->i_ctime = CURRENT_TIME;
402 unlock_super(inode->i_sb);
404 mark_inode_dirty(dir);
405 mark_inode_dirty(inode);
406 mark_buffer_dirty(dir_bh);
407 mark_buffer_dirty(inode_bh);
409 addentry_done:
410 affs_brelse(dir_bh);
411 affs_brelse(inode_bh);
412 affs_brelse(link_bh);
414 return retval;