blob: 1c7fd7928d1f40907196a5627995c6597d498c41 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
13extern struct timezone sys_tz;
14
15static 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
26int
27affs_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
75int
76affs_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
124static void
125affs_fix_dcache(struct dentry *dentry, u32 entry_ino)
126{
127 struct inode *inode = dentry->d_inode;
128 void *data = dentry->d_fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Nick Piggin873feea2011-01-07 17:50:06 +1100130 spin_lock(&inode->i_lock);
Al Viro3084ee92012-06-09 13:03:04 -0400131 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (entry_ino == (u32)(long)dentry->d_fsdata) {
133 dentry->d_fsdata = data;
134 break;
135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
Nick Piggin873feea2011-01-07 17:50:06 +1100137 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140
141/* Remove header from link chain */
142
143static int
144affs_remove_link(struct dentry *dentry)
145{
146 struct inode *dir, *inode = dentry->d_inode;
147 struct super_block *sb = inode->i_sb;
148 struct buffer_head *bh = NULL, *link_bh = NULL;
149 u32 link_ino, ino;
150 int retval;
151
152 pr_debug("AFFS: remove_link(key=%ld)\n", inode->i_ino);
153 retval = -EIO;
154 bh = affs_bread(sb, inode->i_ino);
155 if (!bh)
156 goto done;
157
158 link_ino = (u32)(long)dentry->d_fsdata;
159 if (inode->i_ino == link_ino) {
160 /* we can't remove the head of the link, as its blocknr is still used as ino,
161 * so we remove the block of the first link instead.
162 */
163 link_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain);
164 link_bh = affs_bread(sb, link_ino);
165 if (!link_bh)
166 goto done;
167
David Howells210f8552008-02-07 00:15:29 -0800168 dir = affs_iget(sb, be32_to_cpu(AFFS_TAIL(sb, link_bh)->parent));
169 if (IS_ERR(dir)) {
170 retval = PTR_ERR(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 goto done;
David Howells210f8552008-02-07 00:15:29 -0800172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 affs_lock_dir(dir);
175 affs_fix_dcache(dentry, link_ino);
176 retval = affs_remove_hash(dir, link_bh);
Christoph Hellwigec1ab0a2008-05-09 12:35:29 +0200177 if (retval) {
178 affs_unlock_dir(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 goto done;
Christoph Hellwigec1ab0a2008-05-09 12:35:29 +0200180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 mark_buffer_dirty_inode(link_bh, inode);
182
183 memcpy(AFFS_TAIL(sb, bh)->name, AFFS_TAIL(sb, link_bh)->name, 32);
184 retval = affs_insert_hash(dir, bh);
Christoph Hellwigec1ab0a2008-05-09 12:35:29 +0200185 if (retval) {
186 affs_unlock_dir(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 goto done;
Christoph Hellwigec1ab0a2008-05-09 12:35:29 +0200188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 mark_buffer_dirty_inode(bh, inode);
190
191 affs_unlock_dir(dir);
192 iput(dir);
193 } else {
194 link_bh = affs_bread(sb, link_ino);
195 if (!link_bh)
196 goto done;
197 }
198
199 while ((ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain)) != 0) {
200 if (ino == link_ino) {
201 __be32 ino2 = AFFS_TAIL(sb, link_bh)->link_chain;
202 AFFS_TAIL(sb, bh)->link_chain = ino2;
203 affs_adjust_checksum(bh, be32_to_cpu(ino2) - link_ino);
204 mark_buffer_dirty_inode(bh, inode);
205 retval = 0;
206 /* Fix the link count, if bh is a normal header block without links */
207 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
208 case ST_LINKDIR:
209 case ST_LINKFILE:
210 break;
211 default:
212 if (!AFFS_TAIL(sb, bh)->link_chain)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200213 set_nlink(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215 affs_free_block(sb, link_ino);
216 goto done;
217 }
218 affs_brelse(bh);
219 bh = affs_bread(sb, ino);
220 if (!bh)
221 goto done;
222 }
223 retval = -ENOENT;
224done:
225 affs_brelse(link_bh);
226 affs_brelse(bh);
227 return retval;
228}
229
230
231static int
232affs_empty_dir(struct inode *inode)
233{
234 struct super_block *sb = inode->i_sb;
235 struct buffer_head *bh;
236 int retval, size;
237
238 retval = -EIO;
239 bh = affs_bread(sb, inode->i_ino);
240 if (!bh)
241 goto done;
242
243 retval = -ENOTEMPTY;
244 for (size = AFFS_SB(sb)->s_hashsize - 1; size >= 0; size--)
245 if (AFFS_HEAD(bh)->table[size])
246 goto not_empty;
247 retval = 0;
248not_empty:
249 affs_brelse(bh);
250done:
251 return retval;
252}
253
254
255/* Remove a filesystem object. If the object to be removed has
256 * links to it, one of the links must be changed to inherit
257 * the file or directory. As above, any inode will do.
258 * The buffer will not be freed. If the header is a link, the
259 * block will be marked as free.
260 * This function returns a negative error number in case of
261 * an error, else 0 if the inode is to be deleted or 1 if not.
262 */
263
264int
265affs_remove_header(struct dentry *dentry)
266{
267 struct super_block *sb;
268 struct inode *inode, *dir;
269 struct buffer_head *bh = NULL;
270 int retval;
271
272 dir = dentry->d_parent->d_inode;
273 sb = dir->i_sb;
274
275 retval = -ENOENT;
276 inode = dentry->d_inode;
277 if (!inode)
278 goto done;
279
280 pr_debug("AFFS: remove_header(key=%ld)\n", inode->i_ino);
281 retval = -EIO;
282 bh = affs_bread(sb, (u32)(long)dentry->d_fsdata);
283 if (!bh)
284 goto done;
285
286 affs_lock_link(inode);
287 affs_lock_dir(dir);
288 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
289 case ST_USERDIR:
290 /* if we ever want to support links to dirs
291 * i_hash_lock of the inode must only be
292 * taken after some checks
293 */
294 affs_lock_dir(inode);
295 retval = affs_empty_dir(inode);
296 affs_unlock_dir(inode);
297 if (retval)
298 goto done_unlock;
299 break;
300 default:
301 break;
302 }
303
304 retval = affs_remove_hash(dir, bh);
305 if (retval)
306 goto done_unlock;
307 mark_buffer_dirty_inode(bh, inode);
308
309 affs_unlock_dir(dir);
310
311 if (inode->i_nlink > 1)
312 retval = affs_remove_link(dentry);
313 else
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200314 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 affs_unlock_link(inode);
316 inode->i_ctime = CURRENT_TIME_SEC;
317 mark_inode_dirty(inode);
318
319done:
320 affs_brelse(bh);
321 return retval;
322
323done_unlock:
324 affs_unlock_dir(dir);
325 affs_unlock_link(inode);
326 goto done;
327}
328
329/* Checksum a block, do various consistency checks and optionally return
330 the blocks type number. DATA points to the block. If their pointers
331 are non-null, *PTYPE and *STYPE are set to the primary and secondary
332 block types respectively, *HASHSIZE is set to the size of the hashtable
333 (which lets us calculate the block size).
334 Returns non-zero if the block is not consistent. */
335
336u32
337affs_checksum_block(struct super_block *sb, struct buffer_head *bh)
338{
339 __be32 *ptr = (__be32 *)bh->b_data;
340 u32 sum;
341 int bsize;
342
343 sum = 0;
344 for (bsize = sb->s_blocksize / sizeof(__be32); bsize > 0; bsize--)
345 sum += be32_to_cpu(*ptr++);
346 return sum;
347}
348
349/*
350 * Calculate the checksum of a disk block and store it
351 * at the indicated position.
352 */
353
354void
355affs_fix_checksum(struct super_block *sb, struct buffer_head *bh)
356{
357 int cnt = sb->s_blocksize / sizeof(__be32);
358 __be32 *ptr = (__be32 *)bh->b_data;
359 u32 checksum;
360 __be32 *checksumptr;
361
362 checksumptr = ptr + 5;
363 *checksumptr = 0;
364 for (checksum = 0; cnt > 0; ptr++, cnt--)
365 checksum += be32_to_cpu(*ptr);
366 *checksumptr = cpu_to_be32(-checksum);
367}
368
369void
370secs_to_datestamp(time_t secs, struct affs_date *ds)
371{
372 u32 days;
373 u32 minute;
374
375 secs -= sys_tz.tz_minuteswest * 60 + ((8 * 365 + 2) * 24 * 60 * 60);
376 if (secs < 0)
377 secs = 0;
378 days = secs / 86400;
379 secs -= days * 86400;
380 minute = secs / 60;
381 secs -= minute * 60;
382
383 ds->days = cpu_to_be32(days);
384 ds->mins = cpu_to_be32(minute);
385 ds->ticks = cpu_to_be32(secs * 50);
386}
387
Al Viroa760b032011-07-26 03:04:30 -0400388umode_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389prot_to_mode(u32 prot)
390{
Al Viroa760b032011-07-26 03:04:30 -0400391 umode_t mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 if (!(prot & FIBF_NOWRITE))
394 mode |= S_IWUSR;
395 if (!(prot & FIBF_NOREAD))
396 mode |= S_IRUSR;
397 if (!(prot & FIBF_NOEXECUTE))
398 mode |= S_IXUSR;
399 if (prot & FIBF_GRP_WRITE)
400 mode |= S_IWGRP;
401 if (prot & FIBF_GRP_READ)
402 mode |= S_IRGRP;
403 if (prot & FIBF_GRP_EXECUTE)
404 mode |= S_IXGRP;
405 if (prot & FIBF_OTR_WRITE)
406 mode |= S_IWOTH;
407 if (prot & FIBF_OTR_READ)
408 mode |= S_IROTH;
409 if (prot & FIBF_OTR_EXECUTE)
410 mode |= S_IXOTH;
411
412 return mode;
413}
414
415void
416mode_to_prot(struct inode *inode)
417{
418 u32 prot = AFFS_I(inode)->i_protect;
Al Viroa760b032011-07-26 03:04:30 -0400419 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 if (!(mode & S_IXUSR))
422 prot |= FIBF_NOEXECUTE;
423 if (!(mode & S_IRUSR))
424 prot |= FIBF_NOREAD;
425 if (!(mode & S_IWUSR))
426 prot |= FIBF_NOWRITE;
427 if (mode & S_IXGRP)
428 prot |= FIBF_GRP_EXECUTE;
429 if (mode & S_IRGRP)
430 prot |= FIBF_GRP_READ;
431 if (mode & S_IWGRP)
432 prot |= FIBF_GRP_WRITE;
433 if (mode & S_IXOTH)
434 prot |= FIBF_OTR_EXECUTE;
435 if (mode & S_IROTH)
436 prot |= FIBF_OTR_READ;
437 if (mode & S_IWOTH)
438 prot |= FIBF_OTR_WRITE;
439
440 AFFS_I(inode)->i_protect = prot;
441}
442
443void
444affs_error(struct super_block *sb, const char *function, const char *fmt, ...)
445{
446 va_list args;
447
448 va_start(args,fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800449 vsnprintf(ErrorBuffer,sizeof(ErrorBuffer),fmt,args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 va_end(args);
451
452 printk(KERN_CRIT "AFFS error (device %s): %s(): %s\n", sb->s_id,
453 function,ErrorBuffer);
454 if (!(sb->s_flags & MS_RDONLY))
455 printk(KERN_WARNING "AFFS: Remounting filesystem read-only\n");
456 sb->s_flags |= MS_RDONLY;
457}
458
459void
460affs_warning(struct super_block *sb, const char *function, const char *fmt, ...)
461{
462 va_list args;
463
464 va_start(args,fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800465 vsnprintf(ErrorBuffer,sizeof(ErrorBuffer),fmt,args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 va_end(args);
467
468 printk(KERN_WARNING "AFFS warning (device %s): %s(): %s\n", sb->s_id,
469 function,ErrorBuffer);
470}
471
472/* Check if the name is valid for a affs object. */
473
474int
475affs_check_name(const unsigned char *name, int len)
476{
477 int i;
478
479 if (len > 30)
480#ifdef AFFS_NO_TRUNCATE
481 return -ENAMETOOLONG;
482#else
483 len = 30;
484#endif
485
486 for (i = 0; i < len; i++) {
487 if (name[i] < ' ' || name[i] == ':'
488 || (name[i] > 0x7e && name[i] < 0xa0))
489 return -EINVAL;
490 }
491
492 return 0;
493}
494
495/* This function copies name to bstr, with at most 30
496 * characters length. The bstr will be prepended by
497 * a length byte.
498 * NOTE: The name will must be already checked by
499 * affs_check_name()!
500 */
501
502int
503affs_copy_name(unsigned char *bstr, struct dentry *dentry)
504{
505 int len = min(dentry->d_name.len, 30u);
506
507 *bstr++ = len;
508 memcpy(bstr, dentry->d_name.name, len);
509 return len;
510}