Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Optimized MPEG FS - inode and super operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2006 Bob Copeland <me@bobcopeland.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/vfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/parser.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/crc-itu-t.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "omfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) MODULE_AUTHOR("Bob Copeland <me@bobcopeland.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) MODULE_DESCRIPTION("OMFS (ReplayTV/Karma) Filesystem for Linux");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) struct buffer_head *omfs_bread(struct super_block *sb, sector_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct omfs_sb_info *sbi = OMFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	if (block >= sbi->s_num_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	return sb_bread(sb, clus_to_blk(sbi, block));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) struct inode *omfs_new_inode(struct inode *dir, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	u64 new_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct omfs_sb_info *sbi = OMFS_SB(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	inode = new_inode(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	err = omfs_allocate_range(dir->i_sb, sbi->s_mirrors, sbi->s_mirrors,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 			&new_block, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	inode->i_ino = new_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	inode_init_owner(inode, NULL, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	inode->i_mapping->a_ops = &omfs_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	switch (mode & S_IFMT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	case S_IFDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		inode->i_op = &omfs_dir_inops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		inode->i_fop = &omfs_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		inode->i_size = sbi->s_sys_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		inc_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	case S_IFREG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		inode->i_op = &omfs_file_inops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		inode->i_fop = &omfs_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		inode->i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	insert_inode_hash(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	make_bad_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  * Update the header checksums for a dirty inode based on its contents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  * Caller is expected to hold the buffer head underlying oi and mark it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  * dirty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static void omfs_update_checksums(struct omfs_inode *oi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	int xor, i, ofs = 0, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	u16 crc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	unsigned char *ptr = (unsigned char *) oi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	count = be32_to_cpu(oi->i_head.h_body_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	ofs = sizeof(struct omfs_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	crc = crc_itu_t(crc, ptr + ofs, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	oi->i_head.h_crc = cpu_to_be16(crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	xor = ptr[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	for (i = 1; i < OMFS_XOR_COUNT; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		xor ^= ptr[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	oi->i_head.h_check_xor = xor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static int __omfs_write_inode(struct inode *inode, int wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct omfs_inode *oi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct buffer_head *bh, *bh2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	u64 ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	int ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	int sync_failed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	/* get current inode since we may have written sibling ptrs etc. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	bh = omfs_bread(inode->i_sb, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	oi = (struct omfs_inode *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	oi->i_head.h_self = cpu_to_be64(inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		oi->i_type = OMFS_DIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	else if (S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		oi->i_type = OMFS_FILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		printk(KERN_WARNING "omfs: unknown file type: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		goto out_brelse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	oi->i_head.h_body_size = cpu_to_be32(sbi->s_sys_blocksize -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		sizeof(struct omfs_header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	oi->i_head.h_version = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	oi->i_head.h_type = OMFS_INODE_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	oi->i_head.h_magic = OMFS_IMAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	oi->i_size = cpu_to_be64(inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	ctime = inode->i_ctime.tv_sec * 1000LL +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		((inode->i_ctime.tv_nsec + 999)/1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	oi->i_ctime = cpu_to_be64(ctime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	omfs_update_checksums(oi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (wait) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		sync_dirty_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		if (buffer_req(bh) && !buffer_uptodate(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			sync_failed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	/* if mirroring writes, copy to next fsblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	for (i = 1; i < sbi->s_mirrors; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		bh2 = omfs_bread(inode->i_sb, inode->i_ino + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		if (!bh2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			goto out_brelse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		memcpy(bh2->b_data, bh->b_data, bh->b_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		mark_buffer_dirty(bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		if (wait) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			sync_dirty_buffer(bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			if (buffer_req(bh2) && !buffer_uptodate(bh2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 				sync_failed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		brelse(bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	ret = (sync_failed) ? -EIO : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) out_brelse:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static int omfs_write_inode(struct inode *inode, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return __omfs_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int omfs_sync_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	return __omfs_write_inode(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  * called when an entry is deleted, need to clear the bits in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  * bitmaps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static void omfs_evict_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	truncate_inode_pages_final(&inode->i_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	clear_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (inode->i_nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (S_ISREG(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		inode->i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		omfs_shrink_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	omfs_clear_range(inode->i_sb, inode->i_ino, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct inode *omfs_iget(struct super_block *sb, ino_t ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	struct omfs_sb_info *sbi = OMFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	struct omfs_inode *oi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	u64 ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	unsigned long nsecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	inode = iget_locked(sb, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (!(inode->i_state & I_NEW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	bh = omfs_bread(inode->i_sb, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		goto iget_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	oi = (struct omfs_inode *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	/* check self */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (ino != be64_to_cpu(oi->i_head.h_self))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		goto fail_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	inode->i_uid = sbi->s_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	inode->i_gid = sbi->s_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	ctime = be64_to_cpu(oi->i_ctime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	nsecs = do_div(ctime, 1000) * 1000L;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	inode->i_atime.tv_sec = ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	inode->i_mtime.tv_sec = ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	inode->i_ctime.tv_sec = ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	inode->i_atime.tv_nsec = nsecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	inode->i_mtime.tv_nsec = nsecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	inode->i_ctime.tv_nsec = nsecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	inode->i_mapping->a_ops = &omfs_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	switch (oi->i_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	case OMFS_DIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		inode->i_mode = S_IFDIR | (S_IRWXUGO & ~sbi->s_dmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		inode->i_op = &omfs_dir_inops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		inode->i_fop = &omfs_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		inode->i_size = sbi->s_sys_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		inc_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	case OMFS_FILE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		inode->i_mode = S_IFREG | (S_IRWXUGO & ~sbi->s_fmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		inode->i_fop = &omfs_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		inode->i_size = be64_to_cpu(oi->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) fail_bh:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) iget_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	iget_failed(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static void omfs_put_super(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	struct omfs_sb_info *sbi = OMFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	kfree(sbi->s_imap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	kfree(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	sb->s_fs_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static int omfs_statfs(struct dentry *dentry, struct kstatfs *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	struct super_block *s = dentry->d_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	struct omfs_sb_info *sbi = OMFS_SB(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	u64 id = huge_encode_dev(s->s_bdev->bd_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	buf->f_type = OMFS_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	buf->f_bsize = sbi->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	buf->f_blocks = sbi->s_num_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	buf->f_files = sbi->s_num_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	buf->f_namelen = OMFS_NAMELEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	buf->f_fsid = u64_to_fsid(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	buf->f_bfree = buf->f_bavail = buf->f_ffree =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		omfs_count_free(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)  * Display the mount options in /proc/mounts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static int omfs_show_options(struct seq_file *m, struct dentry *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	struct omfs_sb_info *sbi = OMFS_SB(root->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	umode_t cur_umask = current_umask();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	if (!uid_eq(sbi->s_uid, current_uid()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		seq_printf(m, ",uid=%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			   from_kuid_munged(&init_user_ns, sbi->s_uid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	if (!gid_eq(sbi->s_gid, current_gid()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		seq_printf(m, ",gid=%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			   from_kgid_munged(&init_user_ns, sbi->s_gid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	if (sbi->s_dmask == sbi->s_fmask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		if (sbi->s_fmask != cur_umask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			seq_printf(m, ",umask=%o", sbi->s_fmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		if (sbi->s_dmask != cur_umask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			seq_printf(m, ",dmask=%o", sbi->s_dmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		if (sbi->s_fmask != cur_umask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			seq_printf(m, ",fmask=%o", sbi->s_fmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static const struct super_operations omfs_sops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	.write_inode	= omfs_write_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	.evict_inode	= omfs_evict_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	.put_super	= omfs_put_super,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	.statfs		= omfs_statfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	.show_options	= omfs_show_options,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)  * For Rio Karma, there is an on-disk free bitmap whose location is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)  * stored in the root block.  For ReplayTV, there is no such free bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)  * so we have to walk the tree.  Both inodes and file data are allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)  * from the same map.  This array can be big (300k) so we allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)  * in units of the blocksize.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static int omfs_get_imap(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	unsigned int bitmap_size, array_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	struct omfs_sb_info *sbi = OMFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	unsigned long **ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	sector_t block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	bitmap_size = DIV_ROUND_UP(sbi->s_num_blocks, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	array_size = DIV_ROUND_UP(bitmap_size, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	if (sbi->s_bitmap_ino == ~0ULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	sbi->s_imap_size = array_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	sbi->s_imap = kcalloc(array_size, sizeof(unsigned long *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	if (!sbi->s_imap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		goto nomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	block = clus_to_blk(sbi, sbi->s_bitmap_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	if (block >= sbi->s_num_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		goto nomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	ptr = sbi->s_imap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	for (count = bitmap_size; count > 0; count -= sb->s_blocksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		bh = sb_bread(sb, block++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 			goto nomem_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		*ptr = kmemdup(bh->b_data, sb->s_blocksize, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		if (!*ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			goto nomem_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		if (count < sb->s_blocksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			memset((void *)*ptr + count, 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 				sb->s_blocksize - count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		ptr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) nomem_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	for (count = 0; count < array_size; count++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		kfree(sbi->s_imap[count]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	kfree(sbi->s_imap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) nomem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	sbi->s_imap = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	sbi->s_imap_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	Opt_uid, Opt_gid, Opt_umask, Opt_dmask, Opt_fmask, Opt_err
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) static const match_table_t tokens = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	{Opt_uid, "uid=%u"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	{Opt_gid, "gid=%u"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	{Opt_umask, "umask=%o"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	{Opt_dmask, "dmask=%o"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	{Opt_fmask, "fmask=%o"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	{Opt_err, NULL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static int parse_options(char *options, struct omfs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	substring_t args[MAX_OPT_ARGS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	int option;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	if (!options)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	while ((p = strsep(&options, ",")) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		int token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		if (!*p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		token = match_token(p, tokens, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		switch (token) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		case Opt_uid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 			if (match_int(&args[0], &option))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 			sbi->s_uid = make_kuid(current_user_ns(), option);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 			if (!uid_valid(sbi->s_uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		case Opt_gid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 			if (match_int(&args[0], &option))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 			sbi->s_gid = make_kgid(current_user_ns(), option);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 			if (!gid_valid(sbi->s_gid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		case Opt_umask:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 			if (match_octal(&args[0], &option))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 			sbi->s_fmask = sbi->s_dmask = option;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		case Opt_dmask:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 			if (match_octal(&args[0], &option))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			sbi->s_dmask = option;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		case Opt_fmask:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			if (match_octal(&args[0], &option))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 			sbi->s_fmask = option;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) static int omfs_fill_super(struct super_block *sb, void *data, int silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	struct buffer_head *bh, *bh2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	struct omfs_super_block *omfs_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	struct omfs_root_block *omfs_rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	struct omfs_sb_info *sbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	struct inode *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	sbi = kzalloc(sizeof(struct omfs_sb_info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	if (!sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	sb->s_fs_info = sbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	sbi->s_uid = current_uid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	sbi->s_gid = current_gid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	sbi->s_dmask = sbi->s_fmask = current_umask();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	if (!parse_options((char *) data, sbi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	sb->s_maxbytes = 0xffffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	sb->s_time_gran = NSEC_PER_MSEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	sb->s_time_min = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	sb->s_time_max = U64_MAX / MSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	sb_set_blocksize(sb, 0x200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	bh = sb_bread(sb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	omfs_sb = (struct omfs_super_block *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	if (omfs_sb->s_magic != cpu_to_be32(OMFS_MAGIC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 			printk(KERN_ERR "omfs: Invalid superblock (%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 				   omfs_sb->s_magic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		goto out_brelse_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	sb->s_magic = OMFS_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	sbi->s_num_blocks = be64_to_cpu(omfs_sb->s_num_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	sbi->s_blocksize = be32_to_cpu(omfs_sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	sbi->s_mirrors = be32_to_cpu(omfs_sb->s_mirrors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	sbi->s_root_ino = be64_to_cpu(omfs_sb->s_root_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	sbi->s_sys_blocksize = be32_to_cpu(omfs_sb->s_sys_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	mutex_init(&sbi->s_bitmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	if (sbi->s_num_blocks > OMFS_MAX_BLOCKS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		printk(KERN_ERR "omfs: sysblock number (%llx) is out of range\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		       (unsigned long long)sbi->s_num_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		goto out_brelse_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	if (sbi->s_sys_blocksize > PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		printk(KERN_ERR "omfs: sysblock size (%d) is out of range\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 			sbi->s_sys_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		goto out_brelse_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	if (sbi->s_blocksize < sbi->s_sys_blocksize ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	    sbi->s_blocksize > OMFS_MAX_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		printk(KERN_ERR "omfs: block size (%d) is out of range\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 			sbi->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		goto out_brelse_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	 * Use sys_blocksize as the fs block since it is smaller than a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	 * page while the fs blocksize can be larger.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	sb_set_blocksize(sb, sbi->s_sys_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	 * ...and the difference goes into a shift.  sys_blocksize is always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	 * a power of two factor of blocksize.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	sbi->s_block_shift = get_bitmask_order(sbi->s_blocksize) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		get_bitmask_order(sbi->s_sys_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	bh2 = omfs_bread(sb, be64_to_cpu(omfs_sb->s_root_block));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	if (!bh2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		goto out_brelse_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	omfs_rb = (struct omfs_root_block *)bh2->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	sbi->s_bitmap_ino = be64_to_cpu(omfs_rb->r_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	sbi->s_clustersize = be32_to_cpu(omfs_rb->r_clustersize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	if (sbi->s_num_blocks != be64_to_cpu(omfs_rb->r_num_blocks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		printk(KERN_ERR "omfs: block count discrepancy between "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 			"super and root blocks (%llx, %llx)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 			(unsigned long long)sbi->s_num_blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 			(unsigned long long)be64_to_cpu(omfs_rb->r_num_blocks));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		goto out_brelse_bh2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	if (sbi->s_bitmap_ino != ~0ULL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	    sbi->s_bitmap_ino > sbi->s_num_blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		printk(KERN_ERR "omfs: free space bitmap location is corrupt "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 			"(%llx, total blocks %llx)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 			(unsigned long long) sbi->s_bitmap_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 			(unsigned long long) sbi->s_num_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		goto out_brelse_bh2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	if (sbi->s_clustersize < 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	    sbi->s_clustersize > OMFS_MAX_CLUSTER_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		printk(KERN_ERR "omfs: cluster size out of range (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 			sbi->s_clustersize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		goto out_brelse_bh2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	ret = omfs_get_imap(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		goto out_brelse_bh2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	sb->s_op = &omfs_sops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	root = omfs_iget(sb, be64_to_cpu(omfs_rb->r_root_dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	if (IS_ERR(root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		ret = PTR_ERR(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		goto out_brelse_bh2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	sb->s_root = d_make_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	if (!sb->s_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		goto out_brelse_bh2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	printk(KERN_DEBUG "omfs: Mounted volume %s\n", omfs_rb->r_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) out_brelse_bh2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	brelse(bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) out_brelse_bh:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 		kfree(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) static struct dentry *omfs_mount(struct file_system_type *fs_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 			int flags, const char *dev_name, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	return mount_bdev(fs_type, flags, dev_name, data, omfs_fill_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) static struct file_system_type omfs_fs_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	.name = "omfs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	.mount = omfs_mount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	.kill_sb = kill_block_super,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	.fs_flags = FS_REQUIRES_DEV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) MODULE_ALIAS_FS("omfs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) static int __init init_omfs_fs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	return register_filesystem(&omfs_fs_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) static void __exit exit_omfs_fs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	unregister_filesystem(&omfs_fs_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) module_init(init_omfs_fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) module_exit(exit_omfs_fs);