1 /*
2  *  linux/fs/sysv/inode.c
3  *
4  *  minix/inode.c
5  *  Copyright (C) 1991, 1992  Linus Torvalds
6  *
7  *  xenix/inode.c
8  *  Copyright (C) 1992  Doug Evans
9  *
10  *  coh/inode.c
11  *  Copyright (C) 1993  Pascal Haible, Bruno Haible
12  *
13  *  sysv/inode.c
14  *  Copyright (C) 1993  Paul B. Monday
15  *
16  *  sysv/inode.c
17  *  Copyright (C) 1993  Bruno Haible
18  *  Copyright (C) 1997, 1998  Krzysztof G. Baranowski
19  *
20  *  This file contains code for allocating/freeing inodes and for read/writing
21  *  the superblock.
22  */
23 
24 #include <linux/fs.h>
25 #include <linux/sysv_fs.h>
26 #include <linux/locks.h>
27 #include <linux/smp_lock.h>
28 #include <linux/highuid.h>
29 #include <asm/byteorder.h>
30 
31 /* This is only called on sync() and umount(), when s_dirt=1. */
sysv_write_super(struct super_block * sb)32 static void sysv_write_super(struct super_block *sb)
33 {
34 	if (!(sb->s_flags & MS_RDONLY)) {
35 		/* If we are going to write out the super block,
36 		   then attach current time stamp.
37 		   But if the filesystem was marked clean, keep it clean. */
38 		unsigned long time = CURRENT_TIME;
39 		unsigned long old_time = fs32_to_cpu(sb, *sb->sv_sb_time);
40 		if (sb->sv_type == FSTYPE_SYSV4)
41 			if (*sb->sv_sb_state == cpu_to_fs32(sb, 0x7c269d38 - old_time))
42 				*sb->sv_sb_state = cpu_to_fs32(sb, 0x7c269d38 - time);
43 		*sb->sv_sb_time = cpu_to_fs32(sb, time);
44 		mark_buffer_dirty(sb->sv_bh2);
45 	}
46 	sb->s_dirt = 0;
47 }
48 
sysv_put_super(struct super_block * sb)49 static void sysv_put_super(struct super_block *sb)
50 {
51 	if (!(sb->s_flags & MS_RDONLY)) {
52 		/* XXX ext2 also updates the state here */
53 		mark_buffer_dirty(sb->sv_bh1);
54 		if (sb->sv_bh1 != sb->sv_bh2)
55 			mark_buffer_dirty(sb->sv_bh2);
56 	}
57 
58 	brelse(sb->sv_bh1);
59 	if (sb->sv_bh1 != sb->sv_bh2)
60 		brelse(sb->sv_bh2);
61 }
62 
sysv_statfs(struct super_block * sb,struct statfs * buf)63 static int sysv_statfs(struct super_block *sb, struct statfs *buf)
64 {
65 	buf->f_type = sb->s_magic;
66 	buf->f_bsize = sb->s_blocksize;
67 	buf->f_blocks = sb->sv_ndatazones;
68 	buf->f_bavail = buf->f_bfree = sysv_count_free_blocks(sb);
69 	buf->f_files = sb->sv_ninodes;
70 	buf->f_ffree = sysv_count_free_inodes(sb);
71 	buf->f_namelen = SYSV_NAMELEN;
72 	return 0;
73 }
74 
75 /*
76  * NXI <-> N0XI for PDP, XIN <-> XIN0 for le32, NIX <-> 0NIX for be32
77  */
read3byte(struct super_block * sb,unsigned char * from,unsigned char * to)78 static inline void read3byte(struct super_block *sb,
79 	unsigned char * from, unsigned char * to)
80 {
81 	if (sb->sv_bytesex == BYTESEX_PDP) {
82 		to[0] = from[0];
83 		to[1] = 0;
84 		to[2] = from[1];
85 		to[3] = from[2];
86 	} else if (sb->sv_bytesex == BYTESEX_LE) {
87 		to[0] = from[0];
88 		to[1] = from[1];
89 		to[2] = from[2];
90 		to[3] = 0;
91 	} else {
92 		to[0] = 0;
93 		to[1] = from[0];
94 		to[2] = from[1];
95 		to[3] = from[2];
96 	}
97 }
98 
write3byte(struct super_block * sb,unsigned char * from,unsigned char * to)99 static inline void write3byte(struct super_block *sb,
100 	unsigned char * from, unsigned char * to)
101 {
102 	if (sb->sv_bytesex == BYTESEX_PDP) {
103 		to[0] = from[0];
104 		to[1] = from[2];
105 		to[2] = from[3];
106 	} else if (sb->sv_bytesex == BYTESEX_LE) {
107 		to[0] = from[0];
108 		to[1] = from[1];
109 		to[2] = from[2];
110 	} else {
111 		to[0] = from[1];
112 		to[1] = from[2];
113 		to[2] = from[3];
114 	}
115 }
116 
117 static struct inode_operations sysv_symlink_inode_operations = {
118 	readlink:	page_readlink,
119 	follow_link:	page_follow_link,
120 };
121 
sysv_set_inode(struct inode * inode,dev_t rdev)122 void sysv_set_inode(struct inode *inode, dev_t rdev)
123 {
124 	if (S_ISREG(inode->i_mode)) {
125 		inode->i_op = &sysv_file_inode_operations;
126 		inode->i_fop = &sysv_file_operations;
127 		inode->i_mapping->a_ops = &sysv_aops;
128 	} else if (S_ISDIR(inode->i_mode)) {
129 		inode->i_op = &sysv_dir_inode_operations;
130 		inode->i_fop = &sysv_dir_operations;
131 		inode->i_mapping->a_ops = &sysv_aops;
132 	} else if (S_ISLNK(inode->i_mode)) {
133 		if (inode->i_blocks) {
134 			inode->i_op = &sysv_symlink_inode_operations;
135 			inode->i_mapping->a_ops = &sysv_aops;
136 		} else
137 			inode->i_op = &sysv_fast_symlink_inode_operations;
138 	} else
139 		init_special_inode(inode, inode->i_mode, rdev);
140 }
141 
sysv_read_inode(struct inode * inode)142 static void sysv_read_inode(struct inode *inode)
143 {
144 	struct super_block * sb = inode->i_sb;
145 	struct buffer_head * bh;
146 	struct sysv_inode * raw_inode;
147 	unsigned int block, ino;
148 	dev_t rdev = 0;
149 
150 	ino = inode->i_ino;
151 	if (!ino || ino > sb->sv_ninodes) {
152 		printk("Bad inode number on dev %s"
153 		       ": %d is out of range\n",
154 		       kdevname(inode->i_dev), ino);
155 		goto bad_inode;
156 	}
157 	raw_inode = sysv_raw_inode(sb, ino, &bh);
158 	if (!raw_inode) {
159 		printk("Major problem: unable to read inode from dev %s\n",
160 		       bdevname(inode->i_dev));
161 		goto bad_inode;
162 	}
163 	/* SystemV FS: kludge permissions if ino==SYSV_ROOT_INO ?? */
164 	inode->i_mode = fs16_to_cpu(sb, raw_inode->i_mode);
165 	inode->i_uid = (uid_t)fs16_to_cpu(sb, raw_inode->i_uid);
166 	inode->i_gid = (gid_t)fs16_to_cpu(sb, raw_inode->i_gid);
167 	inode->i_nlink = fs16_to_cpu(sb, raw_inode->i_nlink);
168 	inode->i_size = fs32_to_cpu(sb, raw_inode->i_size);
169 	inode->i_atime = fs32_to_cpu(sb, raw_inode->i_atime);
170 	inode->i_mtime = fs32_to_cpu(sb, raw_inode->i_mtime);
171 	inode->i_ctime = fs32_to_cpu(sb, raw_inode->i_ctime);
172 	inode->i_blocks = inode->i_blksize = 0;
173 	for (block = 0; block < 10+1+1+1; block++)
174 		read3byte(sb, &raw_inode->i_a.i_addb[3*block],
175 			(unsigned char*)&inode->u.sysv_i.i_data[block]);
176 	brelse(bh);
177 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
178 		rdev = (u16)fs32_to_cpu(sb, inode->u.sysv_i.i_data[0]);
179 	inode->u.sysv_i.i_dir_start_lookup = 0;
180 	sysv_set_inode(inode, rdev);
181 	return;
182 
183 bad_inode:
184 	make_bad_inode(inode);
185 	return;
186 }
187 
sysv_update_inode(struct inode * inode)188 static struct buffer_head * sysv_update_inode(struct inode * inode)
189 {
190 	struct super_block * sb = inode->i_sb;
191 	struct buffer_head * bh;
192 	struct sysv_inode * raw_inode;
193 	unsigned int ino, block;
194 
195 	ino = inode->i_ino;
196 	if (!ino || ino > sb->sv_ninodes) {
197 		printk("Bad inode number on dev %s: %d is out of range\n",
198 		       bdevname(inode->i_dev), ino);
199 		return 0;
200 	}
201 	raw_inode = sysv_raw_inode(sb, ino, &bh);
202 	if (!raw_inode) {
203 		printk("unable to read i-node block\n");
204 		return 0;
205 	}
206 
207 	raw_inode->i_mode = cpu_to_fs16(sb, inode->i_mode);
208 	raw_inode->i_uid = cpu_to_fs16(sb, fs_high2lowuid(inode->i_uid));
209 	raw_inode->i_gid = cpu_to_fs16(sb, fs_high2lowgid(inode->i_gid));
210 	raw_inode->i_nlink = cpu_to_fs16(sb, inode->i_nlink);
211 	raw_inode->i_size = cpu_to_fs32(sb, inode->i_size);
212 	raw_inode->i_atime = cpu_to_fs32(sb, inode->i_atime);
213 	raw_inode->i_mtime = cpu_to_fs32(sb, inode->i_mtime);
214 	raw_inode->i_ctime = cpu_to_fs32(sb, inode->i_ctime);
215 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
216 		inode->u.sysv_i.i_data[0] =
217 			cpu_to_fs32(sb, kdev_t_to_nr(inode->i_rdev));
218 	for (block = 0; block < 10+1+1+1; block++)
219 		write3byte(sb, (unsigned char*)&inode->u.sysv_i.i_data[block],
220 			&raw_inode->i_a.i_addb[3*block]);
221 	mark_buffer_dirty(bh);
222 	return bh;
223 }
224 
sysv_write_inode(struct inode * inode,int wait)225 void sysv_write_inode(struct inode * inode, int wait)
226 {
227 	struct buffer_head *bh;
228 	lock_kernel();
229 	bh = sysv_update_inode(inode);
230 	brelse(bh);
231 	unlock_kernel();
232 }
233 
sysv_sync_inode(struct inode * inode)234 int sysv_sync_inode(struct inode * inode)
235 {
236         int err = 0;
237         struct buffer_head *bh;
238 
239         bh = sysv_update_inode(inode);
240         if (bh && buffer_dirty(bh)) {
241                 ll_rw_block(WRITE, 1, &bh);
242                 wait_on_buffer(bh);
243                 if (buffer_req(bh) && !buffer_uptodate(bh)) {
244                         printk ("IO error syncing sysv inode [%s:%08lx]\n",
245                                 bdevname(inode->i_dev), inode->i_ino);
246                         err = -1;
247                 }
248         }
249         else if (!bh)
250                 err = -1;
251         brelse (bh);
252         return err;
253 }
254 
sysv_delete_inode(struct inode * inode)255 static void sysv_delete_inode(struct inode *inode)
256 {
257 	lock_kernel();
258 	inode->i_size = 0;
259 	sysv_truncate(inode);
260 	sysv_free_inode(inode);
261 	unlock_kernel();
262 }
263 
264 struct super_operations sysv_sops = {
265 	read_inode:	sysv_read_inode,
266 	write_inode:	sysv_write_inode,
267 	delete_inode:	sysv_delete_inode,
268 	put_super:	sysv_put_super,
269 	write_super:	sysv_write_super,
270 	statfs:		sysv_statfs,
271 };
272