Linux Audio

Check our new training course

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