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