Import 2.3.26pre2
[davej-history.git] / fs / affs / inode.c
blob9b05ec062ae1bff5e8530320a7cb0d511fc0dd5f
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;
35 #define MIN(a,b) (((a)<(b))?(a):(b))
37 unsigned long
38 affs_parent_ino(struct inode *dir)
40 int root_ino = (dir->i_sb->u.affs_sb.s_root_block);
42 if (!S_ISDIR(dir->i_mode)) {
43 affs_error(dir->i_sb,"parent_ino","Trying to get parent of non-directory");
44 return root_ino;
46 if (dir->i_ino == root_ino)
47 return root_ino;
48 return dir->u.affs_i.i_parent;
51 void
52 affs_read_inode(struct inode *inode)
54 struct buffer_head *bh;
55 struct file_front *file_front;
56 struct file_end *file_end;
57 s32 block;
58 unsigned long prot;
59 s32 ptype, stype;
60 unsigned short id;
62 pr_debug("AFFS: read_inode(%lu)\n",inode->i_ino);
64 block = inode->i_ino;
65 if (!(bh = affs_bread(inode->i_dev,block,AFFS_I2BSIZE(inode)))) {
66 affs_error(inode->i_sb,"read_inode","Cannot read block %d",block);
67 return;
69 if (affs_checksum_block(AFFS_I2BSIZE(inode),bh->b_data,&ptype,&stype) || ptype != T_SHORT) {
70 affs_error(inode->i_sb,"read_inode",
71 "Checksum or type (ptype=%d) error on inode %d",ptype,block);
72 affs_brelse(bh);
73 return;
76 file_front = (struct file_front *)bh->b_data;
77 file_end = GET_END_PTR(struct file_end, bh->b_data,AFFS_I2BSIZE(inode));
78 prot = (be32_to_cpu(file_end->protect) & ~0x10) ^ FIBF_OWNER;
80 inode->u.affs_i.i_protect = prot;
81 inode->u.affs_i.i_parent = be32_to_cpu(file_end->parent);
82 inode->u.affs_i.i_original = 0;
83 inode->u.affs_i.i_zone = 0;
84 inode->u.affs_i.i_hlink = 0;
85 inode->u.affs_i.i_pa_cnt = 0;
86 inode->u.affs_i.i_pa_next = 0;
87 inode->u.affs_i.i_pa_last = 0;
88 inode->u.affs_i.i_ec = NULL;
89 inode->u.affs_i.i_lastblock = -1;
90 inode->i_nlink = 1;
91 inode->i_mode = 0;
93 if (inode->i_sb->u.affs_sb.s_flags & SF_SETMODE)
94 inode->i_mode = inode->i_sb->u.affs_sb.s_mode;
95 else
96 inode->i_mode = prot_to_mode(prot);
98 if (inode->i_sb->u.affs_sb.s_flags & SF_SETUID)
99 inode->i_uid = inode->i_sb->u.affs_sb.s_uid;
100 id = be16_to_cpu(file_end->owner_uid);
101 if (id == 0 || inode->i_sb->u.affs_sb.s_flags & SF_SETUID)
102 inode->i_uid = inode->i_sb->u.affs_sb.s_uid;
103 else if (id == 0xFFFF && inode->i_sb->u.affs_sb.s_flags & SF_MUFS)
104 inode->i_uid = 0;
105 else
106 inode->i_uid = id;
108 id = be16_to_cpu(file_end->owner_gid);
109 if (id == 0 || inode->i_sb->u.affs_sb.s_flags & SF_SETGID)
110 inode->i_gid = inode->i_sb->u.affs_sb.s_gid;
111 else if (id == 0xFFFF && inode->i_sb->u.affs_sb.s_flags & SF_MUFS)
112 inode->i_gid = 0;
113 else
114 inode->i_gid = id;
116 switch (be32_to_cpu(file_end->secondary_type)) {
117 case ST_ROOT:
118 inode->i_uid = inode->i_sb->u.affs_sb.s_uid;
119 inode->i_gid = inode->i_sb->u.affs_sb.s_gid;
120 case ST_USERDIR:
121 if (be32_to_cpu(file_end->secondary_type) == ST_USERDIR ||
122 inode->i_sb->u.affs_sb.s_flags & SF_SETMODE) {
123 if (inode->i_mode & S_IRUSR)
124 inode->i_mode |= S_IXUSR;
125 if (inode->i_mode & S_IRGRP)
126 inode->i_mode |= S_IXGRP;
127 if (inode->i_mode & S_IROTH)
128 inode->i_mode |= S_IXOTH;
129 inode->i_mode |= S_IFDIR;
130 } else
131 inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
132 inode->i_size = 0;
133 break;
134 case ST_LINKDIR:
135 affs_error(inode->i_sb,"read_inode","inode is LINKDIR");
136 affs_brelse(bh);
137 return;
138 case ST_LINKFILE:
139 affs_error(inode->i_sb,"read_inode","inode is LINKFILE");
140 affs_brelse(bh);
141 return;
142 case ST_FILE:
143 inode->i_mode |= S_IFREG;
144 inode->i_size = be32_to_cpu(file_end->byte_size);
145 if (inode->i_sb->u.affs_sb.s_flags & SF_OFS)
146 block = AFFS_I2BSIZE(inode) - 24;
147 else
148 block = AFFS_I2BSIZE(inode);
149 inode->u.affs_i.i_lastblock = ((inode->i_size + block - 1) / block) - 1;
150 break;
151 case ST_SOFTLINK:
152 inode->i_mode |= S_IFLNK;
153 inode->i_size = 0;
154 break;
157 inode->i_mtime = inode->i_atime = inode->i_ctime
158 = (be32_to_cpu(file_end->created.ds_Days) * (24 * 60 * 60) +
159 be32_to_cpu(file_end->created.ds_Minute) * 60 +
160 be32_to_cpu(file_end->created.ds_Tick) / 50 +
161 ((8 * 365 + 2) * 24 * 60 * 60)) +
162 sys_tz.tz_minuteswest * 60;
163 affs_brelse(bh);
165 inode->i_op = NULL;
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_ofs;
169 } else {
170 inode->i_op = &affs_file_inode_operations;
172 } else if (S_ISDIR(inode->i_mode)) {
173 /* Maybe it should be controlled by mount parameter? */
174 inode->i_mode |= S_ISVTX;
175 inode->i_op = &affs_dir_inode_operations;
177 else if (S_ISLNK(inode->i_mode))
178 inode->i_op = &affs_symlink_inode_operations;
181 void
182 affs_write_inode(struct inode *inode)
184 struct buffer_head *bh;
185 struct file_end *file_end;
186 uid_t uid;
187 gid_t gid;
189 pr_debug("AFFS: write_inode(%lu)\n",inode->i_ino);
191 if (!inode->i_nlink)
192 return;
193 if (!(bh = bread(inode->i_dev,inode->i_ino,AFFS_I2BSIZE(inode)))) {
194 affs_error(inode->i_sb,"write_inode","Cannot read block %lu",inode->i_ino);
195 return;
197 file_end = GET_END_PTR(struct file_end, bh->b_data,AFFS_I2BSIZE(inode));
198 if (file_end->secondary_type == be32_to_cpu(ST_ROOT)) {
199 secs_to_datestamp(inode->i_mtime,&ROOT_END(bh->b_data,inode)->disk_altered);
200 } else {
201 file_end->protect = cpu_to_be32(inode->u.affs_i.i_protect ^ FIBF_OWNER);
202 file_end->byte_size = cpu_to_be32(inode->i_size);
203 secs_to_datestamp(inode->i_mtime,&file_end->created);
204 if (!(inode->i_ino == inode->i_sb->u.affs_sb.s_root_block)) {
205 uid = inode->i_uid;
206 gid = inode->i_gid;
207 if (inode->i_sb->u.affs_sb.s_flags & SF_MUFS) {
208 if (inode->i_uid == 0 || inode->i_uid == 0xFFFF)
209 uid = inode->i_uid ^ ~0;
210 if (inode->i_gid == 0 || inode->i_gid == 0xFFFF)
211 gid = inode->i_gid ^ ~0;
213 if (!(inode->i_sb->u.affs_sb.s_flags & SF_SETUID))
214 file_end->owner_uid = cpu_to_be16(uid);
215 if (!(inode->i_sb->u.affs_sb.s_flags & SF_SETGID))
216 file_end->owner_gid = cpu_to_be16(gid);
219 affs_fix_checksum(AFFS_I2BSIZE(inode),bh->b_data,5);
220 mark_buffer_dirty(bh,1);
221 brelse(bh);
225 affs_notify_change(struct dentry *dentry, struct iattr *attr)
227 struct inode *inode = dentry->d_inode;
228 int error;
230 pr_debug("AFFS: notify_change(%lu,0x%x)\n",inode->i_ino,attr->ia_valid);
232 error = inode_change_ok(inode,attr);
233 if (error)
234 goto out;
236 if (((attr->ia_valid & ATTR_UID) && (inode->i_sb->u.affs_sb.s_flags & SF_SETUID)) ||
237 ((attr->ia_valid & ATTR_GID) && (inode->i_sb->u.affs_sb.s_flags & SF_SETGID)) ||
238 ((attr->ia_valid & ATTR_MODE) &&
239 (inode->i_sb->u.affs_sb.s_flags & (SF_SETMODE | SF_IMMUTABLE)))) {
240 if (!(inode->i_sb->u.affs_sb.s_flags & SF_QUIET))
241 error = -EPERM;
242 goto out;
245 if (attr->ia_valid & ATTR_MODE)
246 inode->u.affs_i.i_protect = mode_to_prot(attr->ia_mode);
248 inode_setattr(inode, attr);
249 mark_inode_dirty(inode);
250 error = 0;
251 out:
252 return error;
255 void
256 affs_put_inode(struct inode *inode)
258 pr_debug("AFFS: put_inode(ino=%lu, nlink=%u)\n",
259 inode->i_ino,inode->i_nlink);
261 affs_free_prealloc(inode);
262 if (inode->i_count == 1) {
263 unsigned long cache_page = (unsigned long) inode->u.affs_i.i_ec;
264 if (cache_page) {
265 pr_debug("AFFS: freeing ext cache\n");
266 inode->u.affs_i.i_ec = NULL;
267 free_page(cache_page);
272 void
273 affs_delete_inode(struct inode *inode)
275 pr_debug("AFFS: delete_inode(ino=%lu, nlink=%u)\n",inode->i_ino,inode->i_nlink);
276 inode->i_size = 0;
277 if (S_ISREG(inode->i_mode) && !inode->u.affs_i.i_hlink)
278 affs_truncate(inode);
279 affs_free_block(inode->i_sb,inode->i_ino);
280 clear_inode(inode);
283 struct inode *
284 affs_new_inode(const struct inode *dir)
286 struct inode *inode;
287 struct super_block *sb;
288 s32 block;
290 if (!dir || !(inode = get_empty_inode()))
291 return NULL;
293 sb = dir->i_sb;
294 inode->i_sb = sb;
295 inode->i_flags = 0;
297 if (!(block = affs_new_header((struct inode *)dir))) {
298 iput(inode);
299 return NULL;
302 inode->i_count = 1;
303 inode->i_nlink = 1;
304 inode->i_dev = sb->s_dev;
305 inode->i_uid = current->fsuid;
306 inode->i_gid = current->fsgid;
307 inode->i_ino = block;
308 inode->i_op = NULL;
309 inode->i_blocks = 0;
310 inode->i_size = 0;
311 inode->i_mode = 0;
312 inode->i_blksize = 0;
313 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
315 inode->u.affs_i.i_original = 0;
316 inode->u.affs_i.i_parent = dir->i_ino;
317 inode->u.affs_i.i_zone = 0;
318 inode->u.affs_i.i_hlink = 0;
319 inode->u.affs_i.i_pa_cnt = 0;
320 inode->u.affs_i.i_pa_next = 0;
321 inode->u.affs_i.i_pa_last = 0;
322 inode->u.affs_i.i_ec = NULL;
323 inode->u.affs_i.i_lastblock = -1;
325 insert_inode_hash(inode);
326 mark_inode_dirty(inode);
328 return inode;
332 * Add an entry to a directory. Create the header block
333 * and insert it into the hash table.
337 affs_add_entry(struct inode *dir, struct inode *link, struct inode *inode,
338 struct dentry *dentry, int type)
340 struct buffer_head *dir_bh;
341 struct buffer_head *inode_bh;
342 struct buffer_head *link_bh;
343 int retval;
344 const unsigned char *name = dentry->d_name.name;
345 int len = dentry->d_name.len;
347 pr_debug("AFFS: add_entry(dir=%lu,inode=%lu,\"%*s\",type=%d)\n",dir->i_ino,inode->i_ino,
348 len,name,type);
350 if ((retval = affs_check_name(name,len)))
351 return retval;
352 if (len > 30)
353 len = 30;
355 dir_bh = affs_bread(dir->i_dev,dir->i_ino,AFFS_I2BSIZE(dir));
356 inode_bh = affs_bread(inode->i_dev,inode->i_ino,AFFS_I2BSIZE(inode));
357 link_bh = NULL;
358 retval = -EIO;
359 if (!dir_bh || !inode_bh)
360 goto addentry_done;
361 if (link) {
362 link_bh = affs_bread(link->i_dev,link->i_ino,AFFS_I2BSIZE(link));
363 if (!link_bh)
364 goto addentry_done;
366 ((struct dir_front *)inode_bh->b_data)->primary_type = cpu_to_be32(T_SHORT);
367 ((struct dir_front *)inode_bh->b_data)->own_key = cpu_to_be32(inode->i_ino);
368 DIR_END(inode_bh->b_data,inode)->dir_name[0] = len;
369 strncpy(DIR_END(inode_bh->b_data,inode)->dir_name + 1,name,len);
370 DIR_END(inode_bh->b_data,inode)->secondary_type = cpu_to_be32(type);
371 DIR_END(inode_bh->b_data,inode)->parent = cpu_to_be32(dir->i_ino);
373 lock_super(inode->i_sb);
374 retval = affs_insert_hash(dir->i_ino,inode_bh,dir);
376 if (link_bh) {
377 LINK_END(inode_bh->b_data,inode)->original = cpu_to_be32(link->i_ino);
378 LINK_END(inode_bh->b_data,inode)->link_chain =
379 FILE_END(link_bh->b_data,link)->link_chain;
380 FILE_END(link_bh->b_data,link)->link_chain = cpu_to_be32(inode->i_ino);
381 affs_fix_checksum(AFFS_I2BSIZE(link),link_bh->b_data,5);
382 link->i_version = ++event;
383 mark_inode_dirty(link);
384 mark_buffer_dirty(link_bh,1);
386 affs_fix_checksum(AFFS_I2BSIZE(inode),inode_bh->b_data,5);
387 affs_fix_checksum(AFFS_I2BSIZE(dir),dir_bh->b_data,5);
388 dir->i_version = ++event;
389 dir->i_mtime = dir->i_atime = dir->i_ctime = CURRENT_TIME;
390 unlock_super(inode->i_sb);
392 mark_inode_dirty(dir);
393 mark_inode_dirty(inode);
394 mark_buffer_dirty(dir_bh,1);
395 mark_buffer_dirty(inode_bh,1);
397 addentry_done:
398 affs_brelse(dir_bh);
399 affs_brelse(inode_bh);
400 affs_brelse(link_bh);
402 return retval;