vserver 1.9.3
[linux-2.6.git] / fs / affs / amigaffs.c
1 /*
2  *  linux/fs/affs/amigaffs.c
3  *
4  *  (c) 1996  Hans-Joachim Widmaier - Rewritten
5  *
6  *  (C) 1993  Ray Burr - Amiga FFS filesystem.
7  *
8  *  Please send bug reports to: hjw@zvw.de
9  */
10
11 #include <stdarg.h>
12 #include <linux/stat.h>
13 #include <linux/time.h>
14 #include <linux/affs_fs.h>
15 #include <linux/string.h>
16 #include <linux/mm.h>
17 #include <linux/amigaffs.h>
18 #include <linux/buffer_head.h>
19
20 extern struct timezone sys_tz;
21
22 static char ErrorBuffer[256];
23
24 /*
25  * Functions for accessing Amiga-FFS structures.
26  */
27
28
29 /* Insert a header block bh into the directory dir
30  * caller must hold AFFS_DIR->i_hash_lock!
31  */
32
33 int
34 affs_insert_hash(struct inode *dir, struct buffer_head *bh)
35 {
36         struct super_block *sb = dir->i_sb;
37         struct buffer_head *dir_bh;
38         u32 ino, hash_ino;
39         int offset;
40
41         ino = bh->b_blocknr;
42         offset = affs_hash_name(sb, AFFS_TAIL(sb, bh)->name + 1, AFFS_TAIL(sb, bh)->name[0]);
43
44         pr_debug("AFFS: insert_hash(dir=%u, ino=%d)\n", (u32)dir->i_ino, ino);
45
46         dir_bh = affs_bread(sb, dir->i_ino);
47         if (!dir_bh)
48                 return -EIO;
49
50         hash_ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[offset]);
51         while (hash_ino) {
52                 affs_brelse(dir_bh);
53                 dir_bh = affs_bread(sb, hash_ino);
54                 if (!dir_bh)
55                         return -EIO;
56                 hash_ino = be32_to_cpu(AFFS_TAIL(sb, dir_bh)->hash_chain);
57         }
58         AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
59         AFFS_TAIL(sb, bh)->hash_chain = 0;
60         affs_fix_checksum(sb, bh);
61
62         if (dir->i_ino == dir_bh->b_blocknr)
63                 AFFS_HEAD(dir_bh)->table[offset] = cpu_to_be32(ino);
64         else
65                 AFFS_TAIL(sb, dir_bh)->hash_chain = cpu_to_be32(ino);
66
67         affs_adjust_checksum(dir_bh, ino);
68         mark_buffer_dirty_inode(dir_bh, dir);
69         affs_brelse(dir_bh);
70
71         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
72         dir->i_version++;
73         mark_inode_dirty(dir);
74
75         return 0;
76 }
77
78 /* Remove a header block from its directory.
79  * caller must hold AFFS_DIR->i_hash_lock!
80  */
81
82 int
83 affs_remove_hash(struct inode *dir, struct buffer_head *rem_bh)
84 {
85         struct super_block *sb;
86         struct buffer_head *bh;
87         u32 rem_ino, hash_ino;
88         __be32 ino;
89         int offset, retval;
90
91         sb = dir->i_sb;
92         rem_ino = rem_bh->b_blocknr;
93         offset = affs_hash_name(sb, AFFS_TAIL(sb, rem_bh)->name+1, AFFS_TAIL(sb, rem_bh)->name[0]);
94         pr_debug("AFFS: remove_hash(dir=%d, ino=%d, hashval=%d)\n", (u32)dir->i_ino, rem_ino, offset);
95
96         bh = affs_bread(sb, dir->i_ino);
97         if (!bh)
98                 return -EIO;
99
100         retval = -ENOENT;
101         hash_ino = be32_to_cpu(AFFS_HEAD(bh)->table[offset]);
102         while (hash_ino) {
103                 if (hash_ino == rem_ino) {
104                         ino = AFFS_TAIL(sb, rem_bh)->hash_chain;
105                         if (dir->i_ino == bh->b_blocknr)
106                                 AFFS_HEAD(bh)->table[offset] = ino;
107                         else
108                                 AFFS_TAIL(sb, bh)->hash_chain = ino;
109                         affs_adjust_checksum(bh, be32_to_cpu(ino) - hash_ino);
110                         mark_buffer_dirty_inode(bh, dir);
111                         AFFS_TAIL(sb, rem_bh)->parent = 0;
112                         retval = 0;
113                         break;
114                 }
115                 affs_brelse(bh);
116                 bh = affs_bread(sb, hash_ino);
117                 if (!bh)
118                         return -EIO;
119                 hash_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
120         }
121
122         affs_brelse(bh);
123
124         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
125         dir->i_version++;
126         mark_inode_dirty(dir);
127
128         return retval;
129 }
130
131 static void
132 affs_fix_dcache(struct dentry *dentry, u32 entry_ino)
133 {
134         struct inode *inode = dentry->d_inode;
135         void *data = dentry->d_fsdata;
136         struct list_head *head, *next;
137
138         spin_lock(&dcache_lock);
139         head = &inode->i_dentry;
140         next = head->next;
141         while (next != head) {
142                 dentry = list_entry(next, struct dentry, d_alias);
143                 if (entry_ino == (u32)(long)dentry->d_fsdata) {
144                         dentry->d_fsdata = data;
145                         break;
146                 }
147                 next = next->next;
148         }
149         spin_unlock(&dcache_lock);
150 }
151
152
153 /* Remove header from link chain */
154
155 static int
156 affs_remove_link(struct dentry *dentry)
157 {
158         struct inode *dir, *inode = dentry->d_inode;
159         struct super_block *sb = inode->i_sb;
160         struct buffer_head *bh = NULL, *link_bh = NULL;
161         u32 link_ino, ino;
162         int retval;
163
164         pr_debug("AFFS: remove_link(key=%ld)\n", inode->i_ino);
165         retval = -EIO;
166         bh = affs_bread(sb, inode->i_ino);
167         if (!bh)
168                 goto done;
169
170         link_ino = (u32)(long)dentry->d_fsdata;
171         if (inode->i_ino == link_ino) {
172                 /* we can't remove the head of the link, as its blocknr is still used as ino,
173                  * so we remove the block of the first link instead.
174                  */ 
175                 link_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain);
176                 link_bh = affs_bread(sb, link_ino);
177                 if (!link_bh)
178                         goto done;
179
180                 dir = iget(sb, be32_to_cpu(AFFS_TAIL(sb, link_bh)->parent));
181                 if (!dir)
182                         goto done;
183
184                 affs_lock_dir(dir);
185                 affs_fix_dcache(dentry, link_ino);
186                 retval = affs_remove_hash(dir, link_bh);
187                 if (retval)
188                         goto done;
189                 mark_buffer_dirty_inode(link_bh, inode);
190
191                 memcpy(AFFS_TAIL(sb, bh)->name, AFFS_TAIL(sb, link_bh)->name, 32);
192                 retval = affs_insert_hash(dir, bh);
193                 if (retval)
194                         goto done;
195                 mark_buffer_dirty_inode(bh, inode);
196
197                 affs_unlock_dir(dir);
198                 iput(dir);
199         } else {
200                 link_bh = affs_bread(sb, link_ino);
201                 if (!link_bh)
202                         goto done;
203         }
204
205         while ((ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain)) != 0) {
206                 if (ino == link_ino) {
207                         __be32 ino2 = AFFS_TAIL(sb, link_bh)->link_chain;
208                         AFFS_TAIL(sb, bh)->link_chain = ino2;
209                         affs_adjust_checksum(bh, be32_to_cpu(ino2) - link_ino);
210                         mark_buffer_dirty_inode(bh, inode);
211                         retval = 0;
212                         /* Fix the link count, if bh is a normal header block without links */
213                         switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
214                         case ST_LINKDIR:
215                         case ST_LINKFILE:
216                                 break;
217                         default:
218                                 if (!AFFS_TAIL(sb, bh)->link_chain)
219                                         inode->i_nlink = 1;
220                         }
221                         affs_free_block(sb, link_ino);
222                         goto done;
223                 }
224                 affs_brelse(bh);
225                 bh = affs_bread(sb, ino);
226                 if (!bh)
227                         goto done;
228         }
229         retval = -ENOENT;
230 done:
231         affs_brelse(link_bh);
232         affs_brelse(bh);
233         return retval;
234 }
235
236
237 static int
238 affs_empty_dir(struct inode *inode)
239 {
240         struct super_block *sb = inode->i_sb;
241         struct buffer_head *bh;
242         int retval, size;
243
244         retval = -EIO;
245         bh = affs_bread(sb, inode->i_ino);
246         if (!bh)
247                 goto done;
248
249         retval = -ENOTEMPTY;
250         for (size = AFFS_SB(sb)->s_hashsize - 1; size >= 0; size--)
251                 if (AFFS_HEAD(bh)->table[size])
252                         goto not_empty;
253         retval = 0;
254 not_empty:
255         affs_brelse(bh);
256 done:
257         return retval;
258 }
259
260
261 /* Remove a filesystem object. If the object to be removed has
262  * links to it, one of the links must be changed to inherit
263  * the file or directory. As above, any inode will do.
264  * The buffer will not be freed. If the header is a link, the
265  * block will be marked as free.
266  * This function returns a negative error number in case of
267  * an error, else 0 if the inode is to be deleted or 1 if not.
268  */
269
270 int
271 affs_remove_header(struct dentry *dentry)
272 {
273         struct super_block *sb;
274         struct inode *inode, *dir;
275         struct buffer_head *bh = NULL;
276         int retval;
277
278         dir = dentry->d_parent->d_inode;
279         sb = dir->i_sb;
280
281         retval = -ENOENT;
282         inode = dentry->d_inode;
283         if (!inode)
284                 goto done;
285
286         pr_debug("AFFS: remove_header(key=%ld)\n", inode->i_ino);
287         retval = -EIO;
288         bh = affs_bread(sb, (u32)(long)dentry->d_fsdata);
289         if (!bh)
290                 goto done;
291
292         affs_lock_link(inode);
293         affs_lock_dir(dir);
294         switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
295         case ST_USERDIR:
296                 /* if we ever want to support links to dirs
297                  * i_hash_lock of the inode must only be
298                  * taken after some checks
299                  */
300                 affs_lock_dir(inode);
301                 retval = affs_empty_dir(inode);
302                 affs_unlock_dir(inode);
303                 if (retval)
304                         goto done_unlock;
305                 break;
306         default:
307                 break;
308         }
309
310         retval = affs_remove_hash(dir, bh);
311         if (retval)
312                 goto done_unlock;
313         mark_buffer_dirty_inode(bh, inode);
314
315         affs_unlock_dir(dir);
316
317         if (inode->i_nlink > 1)
318                 retval = affs_remove_link(dentry);
319         else
320                 inode->i_nlink = 0;
321         affs_unlock_link(inode);
322         inode->i_ctime = CURRENT_TIME;
323         mark_inode_dirty(inode);
324
325 done:
326         affs_brelse(bh);
327         return retval;
328
329 done_unlock:
330         affs_unlock_dir(dir);
331         affs_unlock_link(inode);
332         goto done;
333 }
334
335 /* Checksum a block, do various consistency checks and optionally return
336    the blocks type number.  DATA points to the block.  If their pointers
337    are non-null, *PTYPE and *STYPE are set to the primary and secondary
338    block types respectively, *HASHSIZE is set to the size of the hashtable
339    (which lets us calculate the block size).
340    Returns non-zero if the block is not consistent. */
341
342 u32
343 affs_checksum_block(struct super_block *sb, struct buffer_head *bh)
344 {
345         __be32 *ptr = (__be32 *)bh->b_data;
346         u32 sum;
347         int bsize;
348
349         sum = 0;
350         for (bsize = sb->s_blocksize / sizeof(__be32); bsize > 0; bsize--)
351                 sum += be32_to_cpu(*ptr++);
352         return sum;
353 }
354
355 /*
356  * Calculate the checksum of a disk block and store it
357  * at the indicated position.
358  */
359
360 void
361 affs_fix_checksum(struct super_block *sb, struct buffer_head *bh)
362 {
363         int cnt = sb->s_blocksize / sizeof(__be32);
364         __be32 *ptr = (__be32 *)bh->b_data;
365         u32 checksum;
366         __be32 *checksumptr;
367
368         checksumptr = ptr + 5;
369         *checksumptr = 0;
370         for (checksum = 0; cnt > 0; ptr++, cnt--)
371                 checksum += be32_to_cpu(*ptr);
372         *checksumptr = cpu_to_be32(-checksum);
373 }
374
375 void
376 secs_to_datestamp(time_t secs, struct affs_date *ds)
377 {
378         u32      days;
379         u32      minute;
380
381         secs -= sys_tz.tz_minuteswest * 60 + ((8 * 365 + 2) * 24 * 60 * 60);
382         if (secs < 0)
383                 secs = 0;
384         days    = secs / 86400;
385         secs   -= days * 86400;
386         minute  = secs / 60;
387         secs   -= minute * 60;
388
389         ds->days = cpu_to_be32(days);
390         ds->mins = cpu_to_be32(minute);
391         ds->ticks = cpu_to_be32(secs * 50);
392 }
393
394 mode_t
395 prot_to_mode(u32 prot)
396 {
397         int mode = 0;
398
399         if (!(prot & FIBF_NOWRITE))
400                 mode |= S_IWUSR;
401         if (!(prot & FIBF_NOREAD))
402                 mode |= S_IRUSR;
403         if (!(prot & FIBF_NOEXECUTE))
404                 mode |= S_IXUSR;
405         if (prot & FIBF_GRP_WRITE)
406                 mode |= S_IWGRP;
407         if (prot & FIBF_GRP_READ)
408                 mode |= S_IRGRP;
409         if (prot & FIBF_GRP_EXECUTE)
410                 mode |= S_IXGRP;
411         if (prot & FIBF_OTR_WRITE)
412                 mode |= S_IWOTH;
413         if (prot & FIBF_OTR_READ)
414                 mode |= S_IROTH;
415         if (prot & FIBF_OTR_EXECUTE)
416                 mode |= S_IXOTH;
417
418         return mode;
419 }
420
421 void
422 mode_to_prot(struct inode *inode)
423 {
424         u32 prot = AFFS_I(inode)->i_protect;
425         mode_t mode = inode->i_mode;
426
427         if (!(mode & S_IXUSR))
428                 prot |= FIBF_NOEXECUTE;
429         if (!(mode & S_IRUSR))
430                 prot |= FIBF_NOREAD;
431         if (!(mode & S_IWUSR))
432                 prot |= FIBF_NOWRITE;
433         if (mode & S_IXGRP)
434                 prot |= FIBF_GRP_EXECUTE;
435         if (mode & S_IRGRP)
436                 prot |= FIBF_GRP_READ;
437         if (mode & S_IWGRP)
438                 prot |= FIBF_GRP_WRITE;
439         if (mode & S_IXOTH)
440                 prot |= FIBF_OTR_EXECUTE;
441         if (mode & S_IROTH)
442                 prot |= FIBF_OTR_READ;
443         if (mode & S_IWOTH)
444                 prot |= FIBF_OTR_WRITE;
445
446         AFFS_I(inode)->i_protect = prot;
447 }
448
449 void
450 affs_error(struct super_block *sb, const char *function, const char *fmt, ...)
451 {
452         va_list  args;
453
454         va_start(args,fmt);
455         vsprintf(ErrorBuffer,fmt,args);
456         va_end(args);
457
458         printk(KERN_CRIT "AFFS error (device %s): %s(): %s\n", sb->s_id,
459                 function,ErrorBuffer);
460         if (!(sb->s_flags & MS_RDONLY))
461                 printk(KERN_WARNING "AFFS: Remounting filesystem read-only\n");
462         sb->s_flags |= MS_RDONLY;
463 }
464
465 void
466 affs_warning(struct super_block *sb, const char *function, const char *fmt, ...)
467 {
468         va_list  args;
469
470         va_start(args,fmt);
471         vsprintf(ErrorBuffer,fmt,args);
472         va_end(args);
473
474         printk(KERN_WARNING "AFFS warning (device %s): %s(): %s\n", sb->s_id,
475                 function,ErrorBuffer);
476 }
477
478 /* Check if the name is valid for a affs object. */
479
480 int
481 affs_check_name(const unsigned char *name, int len)
482 {
483         int      i;
484
485         if (len > 30)
486 #ifdef AFFS_NO_TRUNCATE
487                 return -ENAMETOOLONG;
488 #else
489                 len = 30;
490 #endif
491
492         for (i = 0; i < len; i++) {
493                 if (name[i] < ' ' || name[i] == ':'
494                     || (name[i] > 0x7e && name[i] < 0xa0))
495                         return -EINVAL;
496         }
497
498         return 0;
499 }
500
501 /* This function copies name to bstr, with at most 30
502  * characters length. The bstr will be prepended by
503  * a length byte.
504  * NOTE: The name will must be already checked by
505  *       affs_check_name()!
506  */
507
508 int
509 affs_copy_name(unsigned char *bstr, struct dentry *dentry)
510 {
511         int len = min(dentry->d_name.len, 30u);
512
513         *bstr++ = len;
514         memcpy(bstr, dentry->d_name.name, len);
515         return len;
516 }