File Index
Files and directories
Sign in
FILE

inode.c

File preview with syntax highlighting.

linux-6.19.14/fs/bfs/inode.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 *	fs/bfs/inode.c
  4 *	BFS superblock and inode operations.
  5 *	Copyright (C) 1999-2018 Tigran Aivazian <aivazian.tigran@gmail.com>
  6 *	From fs/minix, Copyright (C) 1991, 1992 Linus Torvalds.
  7 *	Made endianness-clean by Andrew Stribblehill <ads@wompom.org>, 2005.
  8 */
  9
 10#include <linux/module.h>
 11#include <linux/mm.h>
 12#include <linux/slab.h>
 13#include <linux/init.h>
 14#include <linux/fs.h>
 15#include <linux/buffer_head.h>
 16#include <linux/vfs.h>
 17#include <linux/writeback.h>
 18#include <linux/uio.h>
 19#include <linux/uaccess.h>
 20#include <linux/fs_context.h>
 21#include "bfs.h"
 22
 23MODULE_AUTHOR("Tigran Aivazian <aivazian.tigran@gmail.com>");
 24MODULE_DESCRIPTION("SCO UnixWare BFS filesystem for Linux");
 25MODULE_LICENSE("GPL");
 26
 27#undef DEBUG
 28
 29#ifdef DEBUG
 30#define dprintf(x...)	printf(x)
 31#else
 32#define dprintf(x...)
 33#endif
 34
 35struct inode *bfs_iget(struct super_block *sb, unsigned long ino)
 36{
 37	struct bfs_inode *di;
 38	struct inode *inode;
 39	struct buffer_head *bh;
 40	int block, off;
 41
 42	inode = iget_locked(sb, ino);
 43	if (!inode)
 44		return ERR_PTR(-ENOMEM);
 45	if (!(inode_state_read_once(inode) & I_NEW))
 46		return inode;
 47
 48	if ((ino < BFS_ROOT_INO) || (ino > BFS_SB(inode->i_sb)->si_lasti)) {
 49		printf("Bad inode number %s:%08lx\n", inode->i_sb->s_id, ino);
 50		goto error;
 51	}
 52
 53	block = (ino - BFS_ROOT_INO) / BFS_INODES_PER_BLOCK + 1;
 54	bh = sb_bread(inode->i_sb, block);
 55	if (!bh) {
 56		printf("Unable to read inode %s:%08lx\n", inode->i_sb->s_id,
 57									ino);
 58		goto error;
 59	}
 60
 61	off = (ino - BFS_ROOT_INO) % BFS_INODES_PER_BLOCK;
 62	di = (struct bfs_inode *)bh->b_data + off;
 63
 64	/*
 65	 * https://martin.hinner.info/fs/bfs/bfs-structure.html explains that
 66	 * BFS in SCO UnixWare environment used only lower 9 bits of di->i_mode
 67	 * value. This means that, although bfs_write_inode() saves whole
 68	 * inode->i_mode bits (which include S_IFMT bits and S_IS{UID,GID,VTX}
 69	 * bits), middle 7 bits of di->i_mode value can be garbage when these
 70	 * bits were not saved by bfs_write_inode().
 71	 * Since we can't tell whether middle 7 bits are garbage, use only
 72	 * lower 12 bits (i.e. tolerate S_IS{UID,GID,VTX} bits possibly being
 73	 * garbage) and reconstruct S_IFMT bits for Linux environment from
 74	 * di->i_vtype value.
 75	 */
 76	inode->i_mode = 0x00000FFF & le32_to_cpu(di->i_mode);
 77	if (le32_to_cpu(di->i_vtype) == BFS_VDIR) {
 78		inode->i_mode |= S_IFDIR;
 79		inode->i_op = &bfs_dir_inops;
 80		inode->i_fop = &bfs_dir_operations;
 81	} else if (le32_to_cpu(di->i_vtype) == BFS_VREG) {
 82		inode->i_mode |= S_IFREG;
 83		inode->i_op = &bfs_file_inops;
 84		inode->i_fop = &bfs_file_operations;
 85		inode->i_mapping->a_ops = &bfs_aops;
 86	} else {
 87		brelse(bh);
 88		printf("Unknown vtype=%u %s:%08lx\n",
 89		       le32_to_cpu(di->i_vtype), inode->i_sb->s_id, ino);
 90		goto error;
 91	}
 92
 93	BFS_I(inode)->i_sblock =  le32_to_cpu(di->i_sblock);
 94	BFS_I(inode)->i_eblock =  le32_to_cpu(di->i_eblock);
 95	BFS_I(inode)->i_dsk_ino = le16_to_cpu(di->i_ino);
 96	i_uid_write(inode, le32_to_cpu(di->i_uid));
 97	i_gid_write(inode,  le32_to_cpu(di->i_gid));
 98	set_nlink(inode, le32_to_cpu(di->i_nlink));
 99	inode->i_size = BFS_FILESIZE(di);
100	inode->i_blocks = BFS_FILEBLOCKS(di);
101	inode_set_atime(inode, le32_to_cpu(di->i_atime), 0);
102	inode_set_mtime(inode, le32_to_cpu(di->i_mtime), 0);
103	inode_set_ctime(inode, le32_to_cpu(di->i_ctime), 0);
104
105	brelse(bh);
106	unlock_new_inode(inode);
107	return inode;
108
109error:
110	iget_failed(inode);
111	return ERR_PTR(-EIO);
112}
113
114static struct bfs_inode *find_inode(struct super_block *sb, u16 ino, struct buffer_head **p)
115{
116	if ((ino < BFS_ROOT_INO) || (ino > BFS_SB(sb)->si_lasti)) {
117		printf("Bad inode number %s:%08x\n", sb->s_id, ino);
118		return ERR_PTR(-EIO);
119	}
120
121	ino -= BFS_ROOT_INO;
122
123	*p = sb_bread(sb, 1 + ino / BFS_INODES_PER_BLOCK);
124	if (!*p) {
125		printf("Unable to read inode %s:%08x\n", sb->s_id, ino);
126		return ERR_PTR(-EIO);
127	}
128
129	return (struct bfs_inode *)(*p)->b_data +  ino % BFS_INODES_PER_BLOCK;
130}
131
132static int bfs_write_inode(struct inode *inode, struct writeback_control *wbc)
133{
134	struct bfs_sb_info *info = BFS_SB(inode->i_sb);
135	unsigned int ino = (u16)inode->i_ino;
136	unsigned long i_sblock;
137	struct bfs_inode *di;
138	struct buffer_head *bh;
139	int err = 0;
140
141	dprintf("ino=%08x\n", ino);
142
143	di = find_inode(inode->i_sb, ino, &bh);
144	if (IS_ERR(di))
145		return PTR_ERR(di);
146
147	mutex_lock(&info->bfs_lock);
148
149	if (ino == BFS_ROOT_INO)
150		di->i_vtype = cpu_to_le32(BFS_VDIR);
151	else
152		di->i_vtype = cpu_to_le32(BFS_VREG);
153
154	di->i_ino = cpu_to_le16(ino);
155	di->i_mode = cpu_to_le32(inode->i_mode);
156	di->i_uid = cpu_to_le32(i_uid_read(inode));
157	di->i_gid = cpu_to_le32(i_gid_read(inode));
158	di->i_nlink = cpu_to_le32(inode->i_nlink);
159	di->i_atime = cpu_to_le32(inode_get_atime_sec(inode));
160	di->i_mtime = cpu_to_le32(inode_get_mtime_sec(inode));
161	di->i_ctime = cpu_to_le32(inode_get_ctime_sec(inode));
162	i_sblock = BFS_I(inode)->i_sblock;
163	di->i_sblock = cpu_to_le32(i_sblock);
164	di->i_eblock = cpu_to_le32(BFS_I(inode)->i_eblock);
165	di->i_eoffset = cpu_to_le32(i_sblock * BFS_BSIZE + inode->i_size - 1);
166
167	mark_buffer_dirty(bh);
168	if (wbc->sync_mode == WB_SYNC_ALL) {
169		sync_dirty_buffer(bh);
170		if (buffer_req(bh) && !buffer_uptodate(bh))
171			err = -EIO;
172	}
173	brelse(bh);
174	mutex_unlock(&info->bfs_lock);
175	return err;
176}
177
178static void bfs_evict_inode(struct inode *inode)
179{
180	unsigned long ino = inode->i_ino;
181	struct bfs_inode *di;
182	struct buffer_head *bh;
183	struct super_block *s = inode->i_sb;
184	struct bfs_sb_info *info = BFS_SB(s);
185	struct bfs_inode_info *bi = BFS_I(inode);
186
187	dprintf("ino=%08lx\n", ino);
188
189	truncate_inode_pages_final(&inode->i_data);
190	invalidate_inode_buffers(inode);
191	clear_inode(inode);
192
193	if (inode->i_nlink)
194		return;
195
196	di = find_inode(s, inode->i_ino, &bh);
197	if (IS_ERR(di))
198		return;
199
200	mutex_lock(&info->bfs_lock);
201	/* clear on-disk inode */
202	memset(di, 0, sizeof(struct bfs_inode));
203	mark_buffer_dirty(bh);
204	brelse(bh);
205
206	if (bi->i_dsk_ino) {
207		if (bi->i_sblock)
208			info->si_freeb += bi->i_eblock + 1 - bi->i_sblock;
209		info->si_freei++;
210		clear_bit(ino, info->si_imap);
211		bfs_dump_imap("evict_inode", s);
212	}
213
214	/*
215	 * If this was the last file, make the previous block
216	 * "last block of the last file" even if there is no
217	 * real file there, saves us 1 gap.
218	 */
219	if (info->si_lf_eblk == bi->i_eblock)
220		info->si_lf_eblk = bi->i_sblock - 1;
221	mutex_unlock(&info->bfs_lock);
222}
223
224static void bfs_put_super(struct super_block *s)
225{
226	struct bfs_sb_info *info = BFS_SB(s);
227
228	if (!info)
229		return;
230
231	mutex_destroy(&info->bfs_lock);
232	kfree(info);
233	s->s_fs_info = NULL;
234}
235
236static int bfs_statfs(struct dentry *dentry, struct kstatfs *buf)
237{
238	struct super_block *s = dentry->d_sb;
239	struct bfs_sb_info *info = BFS_SB(s);
240	u64 id = huge_encode_dev(s->s_bdev->bd_dev);
241	buf->f_type = BFS_MAGIC;
242	buf->f_bsize = s->s_blocksize;
243	buf->f_blocks = info->si_blocks;
244	buf->f_bfree = buf->f_bavail = info->si_freeb;
245	buf->f_files = info->si_lasti + 1 - BFS_ROOT_INO;
246	buf->f_ffree = info->si_freei;
247	buf->f_fsid = u64_to_fsid(id);
248	buf->f_namelen = BFS_NAMELEN;
249	return 0;
250}
251
252static struct kmem_cache *bfs_inode_cachep;
253
254static struct inode *bfs_alloc_inode(struct super_block *sb)
255{
256	struct bfs_inode_info *bi;
257	bi = alloc_inode_sb(sb, bfs_inode_cachep, GFP_KERNEL);
258	if (!bi)
259		return NULL;
260	return &bi->vfs_inode;
261}
262
263static void bfs_free_inode(struct inode *inode)
264{
265	kmem_cache_free(bfs_inode_cachep, BFS_I(inode));
266}
267
268static void init_once(void *foo)
269{
270	struct bfs_inode_info *bi = foo;
271
272	inode_init_once(&bi->vfs_inode);
273}
274
275static int __init init_inodecache(void)
276{
277	bfs_inode_cachep = kmem_cache_create("bfs_inode_cache",
278					     sizeof(struct bfs_inode_info),
279					     0, (SLAB_RECLAIM_ACCOUNT|
280						SLAB_ACCOUNT),
281					     init_once);
282	if (bfs_inode_cachep == NULL)
283		return -ENOMEM;
284	return 0;
285}
286
287static void destroy_inodecache(void)
288{
289	/*
290	 * Make sure all delayed rcu free inodes are flushed before we
291	 * destroy cache.
292	 */
293	rcu_barrier();
294	kmem_cache_destroy(bfs_inode_cachep);
295}
296
297static const struct super_operations bfs_sops = {
298	.alloc_inode	= bfs_alloc_inode,
299	.free_inode	= bfs_free_inode,
300	.write_inode	= bfs_write_inode,
301	.evict_inode	= bfs_evict_inode,
302	.put_super	= bfs_put_super,
303	.statfs		= bfs_statfs,
304};
305
306void bfs_dump_imap(const char *prefix, struct super_block *s)
307{
308#ifdef DEBUG
309	int i;
310	char *tmpbuf = (char *)get_zeroed_page(GFP_KERNEL);
311
312	if (!tmpbuf)
313		return;
314	for (i = BFS_SB(s)->si_lasti; i >= 0; i--) {
315		if (i > PAGE_SIZE - 100) break;
316		if (test_bit(i, BFS_SB(s)->si_imap))
317			strcat(tmpbuf, "1");
318		else
319			strcat(tmpbuf, "0");
320	}
321	printf("%s: lasti=%08lx <%s>\n", prefix, BFS_SB(s)->si_lasti, tmpbuf);
322	free_page((unsigned long)tmpbuf);
323#endif
324}
325
326static int bfs_fill_super(struct super_block *s, struct fs_context *fc)
327{
328	struct buffer_head *bh, *sbh;
329	struct bfs_super_block *bfs_sb;
330	struct inode *inode;
331	unsigned i;
332	struct bfs_sb_info *info;
333	int ret = -EINVAL;
334	unsigned long i_sblock, i_eblock, i_eoff, s_size;
335	int silent = fc->sb_flags & SB_SILENT;
336
337	info = kzalloc(sizeof(*info), GFP_KERNEL);
338	if (!info)
339		return -ENOMEM;
340	mutex_init(&info->bfs_lock);
341	s->s_fs_info = info;
342	s->s_time_min = 0;
343	s->s_time_max = U32_MAX;
344
345	sb_set_blocksize(s, BFS_BSIZE);
346
347	sbh = sb_bread(s, 0);
348	if (!sbh)
349		goto out;
350	bfs_sb = (struct bfs_super_block *)sbh->b_data;
351	if (le32_to_cpu(bfs_sb->s_magic) != BFS_MAGIC) {
352		if (!silent)
353			printf("No BFS filesystem on %s (magic=%08x)\n", s->s_id,  le32_to_cpu(bfs_sb->s_magic));
354		goto out1;
355	}
356	if (BFS_UNCLEAN(bfs_sb, s) && !silent)
357		printf("%s is unclean, continuing\n", s->s_id);
358
359	s->s_magic = BFS_MAGIC;
360
361	if (le32_to_cpu(bfs_sb->s_start) > le32_to_cpu(bfs_sb->s_end) ||
362	    le32_to_cpu(bfs_sb->s_start) < sizeof(struct bfs_super_block) + sizeof(struct bfs_dirent)) {
363		printf("Superblock is corrupted on %s\n", s->s_id);
364		goto out1;
365	}
366
367	info->si_lasti = (le32_to_cpu(bfs_sb->s_start) - BFS_BSIZE) / sizeof(struct bfs_inode) + BFS_ROOT_INO - 1;
368	if (info->si_lasti == BFS_MAX_LASTI)
369		printf("NOTE: filesystem %s was created with 512 inodes, the real maximum is 511, mounting anyway\n", s->s_id);
370	else if (info->si_lasti > BFS_MAX_LASTI) {
371		printf("Impossible last inode number %lu > %d on %s\n", info->si_lasti, BFS_MAX_LASTI, s->s_id);
372		goto out1;
373	}
374	for (i = 0; i < BFS_ROOT_INO; i++)
375		set_bit(i, info->si_imap);
376
377	s->s_op = &bfs_sops;
378	inode = bfs_iget(s, BFS_ROOT_INO);
379	if (IS_ERR(inode)) {
380		ret = PTR_ERR(inode);
381		goto out1;
382	}
383	s->s_root = d_make_root(inode);
384	if (!s->s_root) {
385		ret = -ENOMEM;
386		goto out1;
387	}
388
389	info->si_blocks = (le32_to_cpu(bfs_sb->s_end) + 1) >> BFS_BSIZE_BITS;
390	info->si_freeb = (le32_to_cpu(bfs_sb->s_end) + 1 - le32_to_cpu(bfs_sb->s_start)) >> BFS_BSIZE_BITS;
391	info->si_freei = 0;
392	info->si_lf_eblk = 0;
393
394	/* can we read the last block? */
395	bh = sb_bread(s, info->si_blocks - 1);
396	if (!bh) {
397		printf("Last block not available on %s: %lu\n", s->s_id, info->si_blocks - 1);
398		ret = -EIO;
399		goto out2;
400	}
401	brelse(bh);
402
403	bh = NULL;
404	for (i = BFS_ROOT_INO; i <= info->si_lasti; i++) {
405		struct bfs_inode *di;
406		int block = (i - BFS_ROOT_INO) / BFS_INODES_PER_BLOCK + 1;
407		int off = (i - BFS_ROOT_INO) % BFS_INODES_PER_BLOCK;
408		unsigned long eblock;
409
410		if (!off) {
411			brelse(bh);
412			bh = sb_bread(s, block);
413		}
414
415		if (!bh)
416			continue;
417
418		di = (struct bfs_inode *)bh->b_data + off;
419
420		/* test if filesystem is not corrupted */
421
422		i_eoff = le32_to_cpu(di->i_eoffset);
423		i_sblock = le32_to_cpu(di->i_sblock);
424		i_eblock = le32_to_cpu(di->i_eblock);
425		s_size = le32_to_cpu(bfs_sb->s_end);
426
427		if (i_sblock > info->si_blocks ||
428			i_eblock > info->si_blocks ||
429			i_sblock > i_eblock ||
430			(i_eoff != le32_to_cpu(-1) && i_eoff > s_size) ||
431			i_sblock * BFS_BSIZE > i_eoff) {
432
433			printf("Inode 0x%08x corrupted on %s\n", i, s->s_id);
434
435			brelse(bh);
436			ret = -EIO;
437			goto out2;
438		}
439
440		if (!di->i_ino) {
441			info->si_freei++;
442			continue;
443		}
444		set_bit(i, info->si_imap);
445		info->si_freeb -= BFS_FILEBLOCKS(di);
446
447		eblock =  le32_to_cpu(di->i_eblock);
448		if (eblock > info->si_lf_eblk)
449			info->si_lf_eblk = eblock;
450	}
451	brelse(bh);
452	brelse(sbh);
453	bfs_dump_imap("fill_super", s);
454	return 0;
455
456out2:
457	dput(s->s_root);
458	s->s_root = NULL;
459out1:
460	brelse(sbh);
461out:
462	mutex_destroy(&info->bfs_lock);
463	kfree(info);
464	s->s_fs_info = NULL;
465	return ret;
466}
467
468static int bfs_get_tree(struct fs_context *fc)
469{
470	return get_tree_bdev(fc, bfs_fill_super);
471}
472
473static const struct fs_context_operations bfs_context_ops = {
474	.get_tree = bfs_get_tree,
475};
476
477static int bfs_init_fs_context(struct fs_context *fc)
478{
479	fc->ops = &bfs_context_ops;
480
481	return 0;
482}
483
484static struct file_system_type bfs_fs_type = {
485	.owner			= THIS_MODULE,
486	.name			= "bfs",
487	.init_fs_context	= bfs_init_fs_context,
488	.kill_sb		= kill_block_super,
489	.fs_flags		= FS_REQUIRES_DEV,
490};
491MODULE_ALIAS_FS("bfs");
492
493static int __init init_bfs_fs(void)
494{
495	int err = init_inodecache();
496	if (err)
497		goto out1;
498	err = register_filesystem(&bfs_fs_type);
499	if (err)
500		goto out;
501	return 0;
502out:
503	destroy_inodecache();
504out1:
505	return err;
506}
507
508static void __exit exit_bfs_fs(void)
509{
510	unregister_filesystem(&bfs_fs_type);
511	destroy_inodecache();
512}
513
514module_init(init_bfs_fs)
515module_exit(exit_bfs_fs)
~/ Read-only file preview.