GNU Linux-libre 4.19.211-gnu1
[releases.git] / fs / sysv / inode.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/sysv/inode.c
4  *
5  *  minix/inode.c
6  *  Copyright (C) 1991, 1992  Linus Torvalds
7  *
8  *  xenix/inode.c
9  *  Copyright (C) 1992  Doug Evans
10  *
11  *  coh/inode.c
12  *  Copyright (C) 1993  Pascal Haible, Bruno Haible
13  *
14  *  sysv/inode.c
15  *  Copyright (C) 1993  Paul B. Monday
16  *
17  *  sysv/inode.c
18  *  Copyright (C) 1993  Bruno Haible
19  *  Copyright (C) 1997, 1998  Krzysztof G. Baranowski
20  *
21  *  This file contains code for allocating/freeing inodes and for read/writing
22  *  the superblock.
23  */
24
25 #include <linux/highuid.h>
26 #include <linux/slab.h>
27 #include <linux/init.h>
28 #include <linux/buffer_head.h>
29 #include <linux/vfs.h>
30 #include <linux/writeback.h>
31 #include <linux/namei.h>
32 #include <asm/byteorder.h>
33 #include "sysv.h"
34
35 static int sysv_sync_fs(struct super_block *sb, int wait)
36 {
37         struct sysv_sb_info *sbi = SYSV_SB(sb);
38         u32 time = (u32)ktime_get_real_seconds(), old_time;
39
40         mutex_lock(&sbi->s_lock);
41
42         /*
43          * If we are going to write out the super block,
44          * then attach current time stamp.
45          * But if the filesystem was marked clean, keep it clean.
46          */
47         old_time = fs32_to_cpu(sbi, *sbi->s_sb_time);
48         if (sbi->s_type == FSTYPE_SYSV4) {
49                 if (*sbi->s_sb_state == cpu_to_fs32(sbi, 0x7c269d38u - old_time))
50                         *sbi->s_sb_state = cpu_to_fs32(sbi, 0x7c269d38u - time);
51                 *sbi->s_sb_time = cpu_to_fs32(sbi, time);
52                 mark_buffer_dirty(sbi->s_bh2);
53         }
54
55         mutex_unlock(&sbi->s_lock);
56
57         return 0;
58 }
59
60 static int sysv_remount(struct super_block *sb, int *flags, char *data)
61 {
62         struct sysv_sb_info *sbi = SYSV_SB(sb);
63
64         sync_filesystem(sb);
65         if (sbi->s_forced_ro)
66                 *flags |= SB_RDONLY;
67         return 0;
68 }
69
70 static void sysv_put_super(struct super_block *sb)
71 {
72         struct sysv_sb_info *sbi = SYSV_SB(sb);
73
74         if (!sb_rdonly(sb)) {
75                 /* XXX ext2 also updates the state here */
76                 mark_buffer_dirty(sbi->s_bh1);
77                 if (sbi->s_bh1 != sbi->s_bh2)
78                         mark_buffer_dirty(sbi->s_bh2);
79         }
80
81         brelse(sbi->s_bh1);
82         if (sbi->s_bh1 != sbi->s_bh2)
83                 brelse(sbi->s_bh2);
84
85         kfree(sbi);
86 }
87
88 static int sysv_statfs(struct dentry *dentry, struct kstatfs *buf)
89 {
90         struct super_block *sb = dentry->d_sb;
91         struct sysv_sb_info *sbi = SYSV_SB(sb);
92         u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
93
94         buf->f_type = sb->s_magic;
95         buf->f_bsize = sb->s_blocksize;
96         buf->f_blocks = sbi->s_ndatazones;
97         buf->f_bavail = buf->f_bfree = sysv_count_free_blocks(sb);
98         buf->f_files = sbi->s_ninodes;
99         buf->f_ffree = sysv_count_free_inodes(sb);
100         buf->f_namelen = SYSV_NAMELEN;
101         buf->f_fsid.val[0] = (u32)id;
102         buf->f_fsid.val[1] = (u32)(id >> 32);
103         return 0;
104 }
105
106 /* 
107  * NXI <-> N0XI for PDP, XIN <-> XIN0 for le32, NIX <-> 0NIX for be32
108  */
109 static inline void read3byte(struct sysv_sb_info *sbi,
110         unsigned char * from, unsigned char * to)
111 {
112         if (sbi->s_bytesex == BYTESEX_PDP) {
113                 to[0] = from[0];
114                 to[1] = 0;
115                 to[2] = from[1];
116                 to[3] = from[2];
117         } else if (sbi->s_bytesex == BYTESEX_LE) {
118                 to[0] = from[0];
119                 to[1] = from[1];
120                 to[2] = from[2];
121                 to[3] = 0;
122         } else {
123                 to[0] = 0;
124                 to[1] = from[0];
125                 to[2] = from[1];
126                 to[3] = from[2];
127         }
128 }
129
130 static inline void write3byte(struct sysv_sb_info *sbi,
131         unsigned char * from, unsigned char * to)
132 {
133         if (sbi->s_bytesex == BYTESEX_PDP) {
134                 to[0] = from[0];
135                 to[1] = from[2];
136                 to[2] = from[3];
137         } else if (sbi->s_bytesex == BYTESEX_LE) {
138                 to[0] = from[0];
139                 to[1] = from[1];
140                 to[2] = from[2];
141         } else {
142                 to[0] = from[1];
143                 to[1] = from[2];
144                 to[2] = from[3];
145         }
146 }
147
148 static const struct inode_operations sysv_symlink_inode_operations = {
149         .get_link       = page_get_link,
150         .getattr        = sysv_getattr,
151 };
152
153 void sysv_set_inode(struct inode *inode, dev_t rdev)
154 {
155         if (S_ISREG(inode->i_mode)) {
156                 inode->i_op = &sysv_file_inode_operations;
157                 inode->i_fop = &sysv_file_operations;
158                 inode->i_mapping->a_ops = &sysv_aops;
159         } else if (S_ISDIR(inode->i_mode)) {
160                 inode->i_op = &sysv_dir_inode_operations;
161                 inode->i_fop = &sysv_dir_operations;
162                 inode->i_mapping->a_ops = &sysv_aops;
163         } else if (S_ISLNK(inode->i_mode)) {
164                 inode->i_op = &sysv_symlink_inode_operations;
165                 inode_nohighmem(inode);
166                 inode->i_mapping->a_ops = &sysv_aops;
167         } else
168                 init_special_inode(inode, inode->i_mode, rdev);
169 }
170
171 struct inode *sysv_iget(struct super_block *sb, unsigned int ino)
172 {
173         struct sysv_sb_info * sbi = SYSV_SB(sb);
174         struct buffer_head * bh;
175         struct sysv_inode * raw_inode;
176         struct sysv_inode_info * si;
177         struct inode *inode;
178         unsigned int block;
179
180         if (!ino || ino > sbi->s_ninodes) {
181                 printk("Bad inode number on dev %s: %d is out of range\n",
182                        sb->s_id, ino);
183                 return ERR_PTR(-EIO);
184         }
185
186         inode = iget_locked(sb, ino);
187         if (!inode)
188                 return ERR_PTR(-ENOMEM);
189         if (!(inode->i_state & I_NEW))
190                 return inode;
191
192         raw_inode = sysv_raw_inode(sb, ino, &bh);
193         if (!raw_inode) {
194                 printk("Major problem: unable to read inode from dev %s\n",
195                        inode->i_sb->s_id);
196                 goto bad_inode;
197         }
198         /* SystemV FS: kludge permissions if ino==SYSV_ROOT_INO ?? */
199         inode->i_mode = fs16_to_cpu(sbi, raw_inode->i_mode);
200         i_uid_write(inode, (uid_t)fs16_to_cpu(sbi, raw_inode->i_uid));
201         i_gid_write(inode, (gid_t)fs16_to_cpu(sbi, raw_inode->i_gid));
202         set_nlink(inode, fs16_to_cpu(sbi, raw_inode->i_nlink));
203         inode->i_size = fs32_to_cpu(sbi, raw_inode->i_size);
204         inode->i_atime.tv_sec = fs32_to_cpu(sbi, raw_inode->i_atime);
205         inode->i_mtime.tv_sec = fs32_to_cpu(sbi, raw_inode->i_mtime);
206         inode->i_ctime.tv_sec = fs32_to_cpu(sbi, raw_inode->i_ctime);
207         inode->i_ctime.tv_nsec = 0;
208         inode->i_atime.tv_nsec = 0;
209         inode->i_mtime.tv_nsec = 0;
210         inode->i_blocks = 0;
211
212         si = SYSV_I(inode);
213         for (block = 0; block < 10+1+1+1; block++)
214                 read3byte(sbi, &raw_inode->i_data[3*block],
215                                 (u8 *)&si->i_data[block]);
216         brelse(bh);
217         si->i_dir_start_lookup = 0;
218         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
219                 sysv_set_inode(inode,
220                                old_decode_dev(fs32_to_cpu(sbi, si->i_data[0])));
221         else
222                 sysv_set_inode(inode, 0);
223         unlock_new_inode(inode);
224         return inode;
225
226 bad_inode:
227         iget_failed(inode);
228         return ERR_PTR(-EIO);
229 }
230
231 static int __sysv_write_inode(struct inode *inode, int wait)
232 {
233         struct super_block * sb = inode->i_sb;
234         struct sysv_sb_info * sbi = SYSV_SB(sb);
235         struct buffer_head * bh;
236         struct sysv_inode * raw_inode;
237         struct sysv_inode_info * si;
238         unsigned int ino, block;
239         int err = 0;
240
241         ino = inode->i_ino;
242         if (!ino || ino > sbi->s_ninodes) {
243                 printk("Bad inode number on dev %s: %d is out of range\n",
244                        inode->i_sb->s_id, ino);
245                 return -EIO;
246         }
247         raw_inode = sysv_raw_inode(sb, ino, &bh);
248         if (!raw_inode) {
249                 printk("unable to read i-node block\n");
250                 return -EIO;
251         }
252
253         raw_inode->i_mode = cpu_to_fs16(sbi, inode->i_mode);
254         raw_inode->i_uid = cpu_to_fs16(sbi, fs_high2lowuid(i_uid_read(inode)));
255         raw_inode->i_gid = cpu_to_fs16(sbi, fs_high2lowgid(i_gid_read(inode)));
256         raw_inode->i_nlink = cpu_to_fs16(sbi, inode->i_nlink);
257         raw_inode->i_size = cpu_to_fs32(sbi, inode->i_size);
258         raw_inode->i_atime = cpu_to_fs32(sbi, inode->i_atime.tv_sec);
259         raw_inode->i_mtime = cpu_to_fs32(sbi, inode->i_mtime.tv_sec);
260         raw_inode->i_ctime = cpu_to_fs32(sbi, inode->i_ctime.tv_sec);
261
262         si = SYSV_I(inode);
263         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
264                 si->i_data[0] = cpu_to_fs32(sbi, old_encode_dev(inode->i_rdev));
265         for (block = 0; block < 10+1+1+1; block++)
266                 write3byte(sbi, (u8 *)&si->i_data[block],
267                         &raw_inode->i_data[3*block]);
268         mark_buffer_dirty(bh);
269         if (wait) {
270                 sync_dirty_buffer(bh);
271                 if (buffer_req(bh) && !buffer_uptodate(bh)) {
272                         printk ("IO error syncing sysv inode [%s:%08x]\n",
273                                 sb->s_id, ino);
274                         err = -EIO;
275                 }
276         }
277         brelse(bh);
278         return err;
279 }
280
281 int sysv_write_inode(struct inode *inode, struct writeback_control *wbc)
282 {
283         return __sysv_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
284 }
285
286 int sysv_sync_inode(struct inode *inode)
287 {
288         return __sysv_write_inode(inode, 1);
289 }
290
291 static void sysv_evict_inode(struct inode *inode)
292 {
293         truncate_inode_pages_final(&inode->i_data);
294         if (!inode->i_nlink) {
295                 inode->i_size = 0;
296                 sysv_truncate(inode);
297         }
298         invalidate_inode_buffers(inode);
299         clear_inode(inode);
300         if (!inode->i_nlink)
301                 sysv_free_inode(inode);
302 }
303
304 static struct kmem_cache *sysv_inode_cachep;
305
306 static struct inode *sysv_alloc_inode(struct super_block *sb)
307 {
308         struct sysv_inode_info *si;
309
310         si = kmem_cache_alloc(sysv_inode_cachep, GFP_KERNEL);
311         if (!si)
312                 return NULL;
313         return &si->vfs_inode;
314 }
315
316 static void sysv_i_callback(struct rcu_head *head)
317 {
318         struct inode *inode = container_of(head, struct inode, i_rcu);
319         kmem_cache_free(sysv_inode_cachep, SYSV_I(inode));
320 }
321
322 static void sysv_destroy_inode(struct inode *inode)
323 {
324         call_rcu(&inode->i_rcu, sysv_i_callback);
325 }
326
327 static void init_once(void *p)
328 {
329         struct sysv_inode_info *si = (struct sysv_inode_info *)p;
330
331         inode_init_once(&si->vfs_inode);
332 }
333
334 const struct super_operations sysv_sops = {
335         .alloc_inode    = sysv_alloc_inode,
336         .destroy_inode  = sysv_destroy_inode,
337         .write_inode    = sysv_write_inode,
338         .evict_inode    = sysv_evict_inode,
339         .put_super      = sysv_put_super,
340         .sync_fs        = sysv_sync_fs,
341         .remount_fs     = sysv_remount,
342         .statfs         = sysv_statfs,
343 };
344
345 int __init sysv_init_icache(void)
346 {
347         sysv_inode_cachep = kmem_cache_create("sysv_inode_cache",
348                         sizeof(struct sysv_inode_info), 0,
349                         SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|SLAB_ACCOUNT,
350                         init_once);
351         if (!sysv_inode_cachep)
352                 return -ENOMEM;
353         return 0;
354 }
355
356 void sysv_destroy_icache(void)
357 {
358         /*
359          * Make sure all delayed rcu free inodes are flushed before we
360          * destroy cache.
361          */
362         rcu_barrier();
363         kmem_cache_destroy(sysv_inode_cachep);
364 }