OSDN Git Service

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