1 /*
2 * QNX4 file system, Linux implementation.
3 *
4 * Version : 0.2.1
5 *
6 * Using parts of the xiafs filesystem.
7 *
8 * History :
9 *
10 * 01-06-1998 by Richard Frowijn : first release.
11 * 20-06-1998 by Frank Denis : Linux 2.1.99+ support, boot signature, misc.
12 * 30-06-1998 by Frank Denis : first step to write inodes.
13 */
14
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/highuid.h>
19 #include <linux/pagemap.h>
20 #include <linux/buffer_head.h>
21 #include <linux/writeback.h>
22 #include <linux/statfs.h>
23 #include "qnx4.h"
24
25 #define QNX4_VERSION 4
26 #define QNX4_BMNAME ".bitmap"
27
28 static const struct super_operations qnx4_sops;
29
30 static void qnx4_put_super(struct super_block *sb);
31 static struct inode *qnx4_alloc_inode(struct super_block *sb);
32 static void qnx4_destroy_inode(struct inode *inode);
33 static int qnx4_remount(struct super_block *sb, int *flags, char *data);
34 static int qnx4_statfs(struct dentry *, struct kstatfs *);
35
36 static const struct super_operations qnx4_sops =
37 {
38 .alloc_inode = qnx4_alloc_inode,
39 .destroy_inode = qnx4_destroy_inode,
40 .put_super = qnx4_put_super,
41 .statfs = qnx4_statfs,
42 .remount_fs = qnx4_remount,
43 };
44
qnx4_remount(struct super_block * sb,int * flags,char * data)45 static int qnx4_remount(struct super_block *sb, int *flags, char *data)
46 {
47 struct qnx4_sb_info *qs;
48
49 sync_filesystem(sb);
50 qs = qnx4_sb(sb);
51 qs->Version = QNX4_VERSION;
52 *flags |= MS_RDONLY;
53 return 0;
54 }
55
qnx4_get_block(struct inode * inode,sector_t iblock,struct buffer_head * bh,int create)56 static int qnx4_get_block( struct inode *inode, sector_t iblock, struct buffer_head *bh, int create )
57 {
58 unsigned long phys;
59
60 QNX4DEBUG((KERN_INFO "qnx4: qnx4_get_block inode=[%ld] iblock=[%ld]\n",inode->i_ino,iblock));
61
62 phys = qnx4_block_map( inode, iblock );
63 if ( phys ) {
64 // logical block is before EOF
65 map_bh(bh, inode->i_sb, phys);
66 }
67 return 0;
68 }
69
try_extent(qnx4_xtnt_t * extent,u32 * offset)70 static inline u32 try_extent(qnx4_xtnt_t *extent, u32 *offset)
71 {
72 u32 size = le32_to_cpu(extent->xtnt_size);
73 if (*offset < size)
74 return le32_to_cpu(extent->xtnt_blk) + *offset - 1;
75 *offset -= size;
76 return 0;
77 }
78
qnx4_block_map(struct inode * inode,long iblock)79 unsigned long qnx4_block_map( struct inode *inode, long iblock )
80 {
81 int ix;
82 long i_xblk;
83 struct buffer_head *bh = NULL;
84 struct qnx4_xblk *xblk = NULL;
85 struct qnx4_inode_entry *qnx4_inode = qnx4_raw_inode(inode);
86 u16 nxtnt = le16_to_cpu(qnx4_inode->di_num_xtnts);
87 u32 offset = iblock;
88 u32 block = try_extent(&qnx4_inode->di_first_xtnt, &offset);
89
90 if (block) {
91 // iblock is in the first extent. This is easy.
92 } else {
93 // iblock is beyond first extent. We have to follow the extent chain.
94 i_xblk = le32_to_cpu(qnx4_inode->di_xblk);
95 ix = 0;
96 while ( --nxtnt > 0 ) {
97 if ( ix == 0 ) {
98 // read next xtnt block.
99 bh = sb_bread(inode->i_sb, i_xblk - 1);
100 if ( !bh ) {
101 QNX4DEBUG((KERN_ERR "qnx4: I/O error reading xtnt block [%ld])\n", i_xblk - 1));
102 return -EIO;
103 }
104 xblk = (struct qnx4_xblk*)bh->b_data;
105 if ( memcmp( xblk->xblk_signature, "IamXblk", 7 ) ) {
106 QNX4DEBUG((KERN_ERR "qnx4: block at %ld is not a valid xtnt\n", qnx4_inode->i_xblk));
107 return -EIO;
108 }
109 }
110 block = try_extent(&xblk->xblk_xtnts[ix], &offset);
111 if (block) {
112 // got it!
113 break;
114 }
115 if ( ++ix >= xblk->xblk_num_xtnts ) {
116 i_xblk = le32_to_cpu(xblk->xblk_next_xblk);
117 ix = 0;
118 brelse( bh );
119 bh = NULL;
120 }
121 }
122 if ( bh )
123 brelse( bh );
124 }
125
126 QNX4DEBUG((KERN_INFO "qnx4: mapping block %ld of inode %ld = %ld\n",iblock,inode->i_ino,block));
127 return block;
128 }
129
qnx4_statfs(struct dentry * dentry,struct kstatfs * buf)130 static int qnx4_statfs(struct dentry *dentry, struct kstatfs *buf)
131 {
132 struct super_block *sb = dentry->d_sb;
133 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
134
135 buf->f_type = sb->s_magic;
136 buf->f_bsize = sb->s_blocksize;
137 buf->f_blocks = le32_to_cpu(qnx4_sb(sb)->BitMap->di_size) * 8;
138 buf->f_bfree = qnx4_count_free_blocks(sb);
139 buf->f_bavail = buf->f_bfree;
140 buf->f_namelen = QNX4_NAME_MAX;
141 buf->f_fsid.val[0] = (u32)id;
142 buf->f_fsid.val[1] = (u32)(id >> 32);
143
144 return 0;
145 }
146
147 /*
148 * Check the root directory of the filesystem to make sure
149 * it really _is_ a qnx4 filesystem, and to check the size
150 * of the directory entry.
151 */
qnx4_checkroot(struct super_block * sb)152 static const char *qnx4_checkroot(struct super_block *sb)
153 {
154 struct buffer_head *bh;
155 struct qnx4_inode_entry *rootdir;
156 int rd, rl;
157 int i, j;
158
159 if (*(qnx4_sb(sb)->sb->RootDir.di_fname) != '/')
160 return "no qnx4 filesystem (no root dir).";
161 QNX4DEBUG((KERN_NOTICE "QNX4 filesystem found on dev %s.\n", sb->s_id));
162 rd = le32_to_cpu(qnx4_sb(sb)->sb->RootDir.di_first_xtnt.xtnt_blk) - 1;
163 rl = le32_to_cpu(qnx4_sb(sb)->sb->RootDir.di_first_xtnt.xtnt_size);
164 for (j = 0; j < rl; j++) {
165 bh = sb_bread(sb, rd + j); /* root dir, first block */
166 if (bh == NULL)
167 return "unable to read root entry.";
168 rootdir = (struct qnx4_inode_entry *) bh->b_data;
169 for (i = 0; i < QNX4_INODES_PER_BLOCK; i++, rootdir++) {
170 QNX4DEBUG((KERN_INFO "rootdir entry found : [%s]\n", rootdir->di_fname));
171 if (strcmp(rootdir->di_fname, QNX4_BMNAME) != 0)
172 continue;
173 qnx4_sb(sb)->BitMap = kmemdup(rootdir,
174 sizeof(struct qnx4_inode_entry),
175 GFP_KERNEL);
176 brelse(bh);
177 if (!qnx4_sb(sb)->BitMap)
178 return "not enough memory for bitmap inode";
179 /* keep bitmap inode known */
180 return NULL;
181 }
182 brelse(bh);
183 }
184 return "bitmap file not found.";
185 }
186
qnx4_fill_super(struct super_block * s,void * data,int silent)187 static int qnx4_fill_super(struct super_block *s, void *data, int silent)
188 {
189 struct buffer_head *bh;
190 struct inode *root;
191 const char *errmsg;
192 struct qnx4_sb_info *qs;
193 int ret = -EINVAL;
194
195 qs = kzalloc(sizeof(struct qnx4_sb_info), GFP_KERNEL);
196 if (!qs)
197 return -ENOMEM;
198 s->s_fs_info = qs;
199
200 sb_set_blocksize(s, QNX4_BLOCK_SIZE);
201
202 /* Check the superblock signature. Since the qnx4 code is
203 dangerous, we should leave as quickly as possible
204 if we don't belong here... */
205 bh = sb_bread(s, 1);
206 if (!bh) {
207 printk(KERN_ERR "qnx4: unable to read the superblock\n");
208 goto outnobh;
209 }
210 if ( le32_to_cpup((__le32*) bh->b_data) != QNX4_SUPER_MAGIC ) {
211 if (!silent)
212 printk(KERN_ERR "qnx4: wrong fsid in superblock.\n");
213 goto out;
214 }
215 s->s_op = &qnx4_sops;
216 s->s_magic = QNX4_SUPER_MAGIC;
217 s->s_flags |= MS_RDONLY; /* Yup, read-only yet */
218 qnx4_sb(s)->sb_buf = bh;
219 qnx4_sb(s)->sb = (struct qnx4_super_block *) bh->b_data;
220
221
222 /* check before allocating dentries, inodes, .. */
223 errmsg = qnx4_checkroot(s);
224 if (errmsg != NULL) {
225 if (!silent)
226 printk(KERN_ERR "qnx4: %s\n", errmsg);
227 goto out;
228 }
229
230 /* does root not have inode number QNX4_ROOT_INO ?? */
231 root = qnx4_iget(s, QNX4_ROOT_INO * QNX4_INODES_PER_BLOCK);
232 if (IS_ERR(root)) {
233 printk(KERN_ERR "qnx4: get inode failed\n");
234 ret = PTR_ERR(root);
235 goto outb;
236 }
237
238 ret = -ENOMEM;
239 s->s_root = d_make_root(root);
240 if (s->s_root == NULL)
241 goto outb;
242
243 brelse(bh);
244 return 0;
245
246 outb:
247 kfree(qs->BitMap);
248 out:
249 brelse(bh);
250 outnobh:
251 kfree(qs);
252 s->s_fs_info = NULL;
253 return ret;
254 }
255
qnx4_put_super(struct super_block * sb)256 static void qnx4_put_super(struct super_block *sb)
257 {
258 struct qnx4_sb_info *qs = qnx4_sb(sb);
259 kfree( qs->BitMap );
260 kfree( qs );
261 sb->s_fs_info = NULL;
262 return;
263 }
264
qnx4_readpage(struct file * file,struct page * page)265 static int qnx4_readpage(struct file *file, struct page *page)
266 {
267 return block_read_full_page(page,qnx4_get_block);
268 }
269
qnx4_bmap(struct address_space * mapping,sector_t block)270 static sector_t qnx4_bmap(struct address_space *mapping, sector_t block)
271 {
272 return generic_block_bmap(mapping,block,qnx4_get_block);
273 }
274 static const struct address_space_operations qnx4_aops = {
275 .readpage = qnx4_readpage,
276 .bmap = qnx4_bmap
277 };
278
qnx4_iget(struct super_block * sb,unsigned long ino)279 struct inode *qnx4_iget(struct super_block *sb, unsigned long ino)
280 {
281 struct buffer_head *bh;
282 struct qnx4_inode_entry *raw_inode;
283 int block;
284 struct qnx4_inode_entry *qnx4_inode;
285 struct inode *inode;
286
287 inode = iget_locked(sb, ino);
288 if (!inode)
289 return ERR_PTR(-ENOMEM);
290 if (!(inode->i_state & I_NEW))
291 return inode;
292
293 qnx4_inode = qnx4_raw_inode(inode);
294 inode->i_mode = 0;
295
296 QNX4DEBUG((KERN_INFO "reading inode : [%d]\n", ino));
297 if (!ino) {
298 printk(KERN_ERR "qnx4: bad inode number on dev %s: %lu is "
299 "out of range\n",
300 sb->s_id, ino);
301 iget_failed(inode);
302 return ERR_PTR(-EIO);
303 }
304 block = ino / QNX4_INODES_PER_BLOCK;
305
306 if (!(bh = sb_bread(sb, block))) {
307 printk(KERN_ERR "qnx4: major problem: unable to read inode from dev "
308 "%s\n", sb->s_id);
309 iget_failed(inode);
310 return ERR_PTR(-EIO);
311 }
312 raw_inode = ((struct qnx4_inode_entry *) bh->b_data) +
313 (ino % QNX4_INODES_PER_BLOCK);
314
315 inode->i_mode = le16_to_cpu(raw_inode->di_mode);
316 i_uid_write(inode, (uid_t)le16_to_cpu(raw_inode->di_uid));
317 i_gid_write(inode, (gid_t)le16_to_cpu(raw_inode->di_gid));
318 set_nlink(inode, le16_to_cpu(raw_inode->di_nlink));
319 inode->i_size = le32_to_cpu(raw_inode->di_size);
320 inode->i_mtime.tv_sec = le32_to_cpu(raw_inode->di_mtime);
321 inode->i_mtime.tv_nsec = 0;
322 inode->i_atime.tv_sec = le32_to_cpu(raw_inode->di_atime);
323 inode->i_atime.tv_nsec = 0;
324 inode->i_ctime.tv_sec = le32_to_cpu(raw_inode->di_ctime);
325 inode->i_ctime.tv_nsec = 0;
326 inode->i_blocks = le32_to_cpu(raw_inode->di_first_xtnt.xtnt_size);
327
328 memcpy(qnx4_inode, raw_inode, QNX4_DIR_ENTRY_SIZE);
329 if (S_ISREG(inode->i_mode)) {
330 inode->i_fop = &generic_ro_fops;
331 inode->i_mapping->a_ops = &qnx4_aops;
332 qnx4_i(inode)->mmu_private = inode->i_size;
333 } else if (S_ISDIR(inode->i_mode)) {
334 inode->i_op = &qnx4_dir_inode_operations;
335 inode->i_fop = &qnx4_dir_operations;
336 } else if (S_ISLNK(inode->i_mode)) {
337 inode->i_op = &page_symlink_inode_operations;
338 inode->i_mapping->a_ops = &qnx4_aops;
339 qnx4_i(inode)->mmu_private = inode->i_size;
340 } else {
341 printk(KERN_ERR "qnx4: bad inode %lu on dev %s\n",
342 ino, sb->s_id);
343 iget_failed(inode);
344 brelse(bh);
345 return ERR_PTR(-EIO);
346 }
347 brelse(bh);
348 unlock_new_inode(inode);
349 return inode;
350 }
351
352 static struct kmem_cache *qnx4_inode_cachep;
353
qnx4_alloc_inode(struct super_block * sb)354 static struct inode *qnx4_alloc_inode(struct super_block *sb)
355 {
356 struct qnx4_inode_info *ei;
357 ei = kmem_cache_alloc(qnx4_inode_cachep, GFP_KERNEL);
358 if (!ei)
359 return NULL;
360 return &ei->vfs_inode;
361 }
362
qnx4_i_callback(struct rcu_head * head)363 static void qnx4_i_callback(struct rcu_head *head)
364 {
365 struct inode *inode = container_of(head, struct inode, i_rcu);
366 kmem_cache_free(qnx4_inode_cachep, qnx4_i(inode));
367 }
368
qnx4_destroy_inode(struct inode * inode)369 static void qnx4_destroy_inode(struct inode *inode)
370 {
371 call_rcu(&inode->i_rcu, qnx4_i_callback);
372 }
373
init_once(void * foo)374 static void init_once(void *foo)
375 {
376 struct qnx4_inode_info *ei = (struct qnx4_inode_info *) foo;
377
378 inode_init_once(&ei->vfs_inode);
379 }
380
init_inodecache(void)381 static int init_inodecache(void)
382 {
383 qnx4_inode_cachep = kmem_cache_create("qnx4_inode_cache",
384 sizeof(struct qnx4_inode_info),
385 0, (SLAB_RECLAIM_ACCOUNT|
386 SLAB_MEM_SPREAD),
387 init_once);
388 if (qnx4_inode_cachep == NULL)
389 return -ENOMEM;
390 return 0;
391 }
392
destroy_inodecache(void)393 static void destroy_inodecache(void)
394 {
395 /*
396 * Make sure all delayed rcu free inodes are flushed before we
397 * destroy cache.
398 */
399 rcu_barrier();
400 kmem_cache_destroy(qnx4_inode_cachep);
401 }
402
qnx4_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)403 static struct dentry *qnx4_mount(struct file_system_type *fs_type,
404 int flags, const char *dev_name, void *data)
405 {
406 return mount_bdev(fs_type, flags, dev_name, data, qnx4_fill_super);
407 }
408
409 static struct file_system_type qnx4_fs_type = {
410 .owner = THIS_MODULE,
411 .name = "qnx4",
412 .mount = qnx4_mount,
413 .kill_sb = kill_block_super,
414 .fs_flags = FS_REQUIRES_DEV,
415 };
416 MODULE_ALIAS_FS("qnx4");
417
init_qnx4_fs(void)418 static int __init init_qnx4_fs(void)
419 {
420 int err;
421
422 err = init_inodecache();
423 if (err)
424 return err;
425
426 err = register_filesystem(&qnx4_fs_type);
427 if (err) {
428 destroy_inodecache();
429 return err;
430 }
431
432 printk(KERN_INFO "QNX4 filesystem 0.2.3 registered.\n");
433 return 0;
434 }
435
exit_qnx4_fs(void)436 static void __exit exit_qnx4_fs(void)
437 {
438 unregister_filesystem(&qnx4_fs_type);
439 destroy_inodecache();
440 }
441
442 module_init(init_qnx4_fs)
443 module_exit(exit_qnx4_fs)
444 MODULE_LICENSE("GPL");
445
446