xref: /linux/fs/qnx4/inode.c (revision 5e8d780d745c1619aba81fe7166c5a4b5cad2b84)
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/config.h>
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/string.h>
19 #include <linux/errno.h>
20 #include <linux/slab.h>
21 #include <linux/fs.h>
22 #include <linux/qnx4_fs.h>
23 #include <linux/init.h>
24 #include <linux/highuid.h>
25 #include <linux/smp_lock.h>
26 #include <linux/pagemap.h>
27 #include <linux/buffer_head.h>
28 #include <linux/vfs.h>
29 #include <asm/uaccess.h>
30 
31 #define QNX4_VERSION  4
32 #define QNX4_BMNAME   ".bitmap"
33 
34 static struct super_operations qnx4_sops;
35 
36 #ifdef CONFIG_QNX4FS_RW
37 
38 int qnx4_sync_inode(struct inode *inode)
39 {
40 	int err = 0;
41 # if 0
42 	struct buffer_head *bh;
43 
44    	bh = qnx4_update_inode(inode);
45 	if (bh && buffer_dirty(bh))
46 	{
47 		sync_dirty_buffer(bh);
48 		if (buffer_req(bh) && !buffer_uptodate(bh))
49 		{
50 			printk ("IO error syncing qnx4 inode [%s:%08lx]\n",
51 				inode->i_sb->s_id, inode->i_ino);
52 			err = -1;
53 		}
54 	        brelse (bh);
55 	} else if (!bh) {
56 		err = -1;
57 	}
58 # endif
59 
60 	return err;
61 }
62 
63 static void qnx4_delete_inode(struct inode *inode)
64 {
65 	QNX4DEBUG(("qnx4: deleting inode [%lu]\n", (unsigned long) inode->i_ino));
66 	truncate_inode_pages(&inode->i_data, 0);
67 	inode->i_size = 0;
68 	qnx4_truncate(inode);
69 	lock_kernel();
70 	qnx4_free_inode(inode);
71 	unlock_kernel();
72 }
73 
74 static void qnx4_write_super(struct super_block *sb)
75 {
76 	lock_kernel();
77 	QNX4DEBUG(("qnx4: write_super\n"));
78 	sb->s_dirt = 0;
79 	unlock_kernel();
80 }
81 
82 static int qnx4_write_inode(struct inode *inode, int unused)
83 {
84 	struct qnx4_inode_entry *raw_inode;
85 	int block, ino;
86 	struct buffer_head *bh;
87 	ino = inode->i_ino;
88 
89 	QNX4DEBUG(("qnx4: write inode 1.\n"));
90 	if (inode->i_nlink == 0) {
91 		return 0;
92 	}
93 	if (!ino) {
94 		printk("qnx4: bad inode number on dev %s: %d is out of range\n",
95 		       inode->i_sb->s_id, ino);
96 		return -EIO;
97 	}
98 	QNX4DEBUG(("qnx4: write inode 2.\n"));
99 	block = ino / QNX4_INODES_PER_BLOCK;
100 	lock_kernel();
101 	if (!(bh = sb_bread(inode->i_sb, block))) {
102 		printk("qnx4: major problem: unable to read inode from dev "
103 		       "%s\n", inode->i_sb->s_id);
104 		unlock_kernel();
105 		return -EIO;
106 	}
107 	raw_inode = ((struct qnx4_inode_entry *) bh->b_data) +
108 	    (ino % QNX4_INODES_PER_BLOCK);
109 	raw_inode->di_mode  = cpu_to_le16(inode->i_mode);
110 	raw_inode->di_uid   = cpu_to_le16(fs_high2lowuid(inode->i_uid));
111 	raw_inode->di_gid   = cpu_to_le16(fs_high2lowgid(inode->i_gid));
112 	raw_inode->di_nlink = cpu_to_le16(inode->i_nlink);
113 	raw_inode->di_size  = cpu_to_le32(inode->i_size);
114 	raw_inode->di_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
115 	raw_inode->di_atime = cpu_to_le32(inode->i_atime.tv_sec);
116 	raw_inode->di_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
117 	raw_inode->di_first_xtnt.xtnt_size = cpu_to_le32(inode->i_blocks);
118 	mark_buffer_dirty(bh);
119 	brelse(bh);
120 	unlock_kernel();
121 	return 0;
122 }
123 
124 #endif
125 
126 static void qnx4_put_super(struct super_block *sb);
127 static struct inode *qnx4_alloc_inode(struct super_block *sb);
128 static void qnx4_destroy_inode(struct inode *inode);
129 static void qnx4_read_inode(struct inode *);
130 static int qnx4_remount(struct super_block *sb, int *flags, char *data);
131 static int qnx4_statfs(struct dentry *, struct kstatfs *);
132 
133 static struct super_operations qnx4_sops =
134 {
135 	.alloc_inode	= qnx4_alloc_inode,
136 	.destroy_inode	= qnx4_destroy_inode,
137 	.read_inode	= qnx4_read_inode,
138 	.put_super	= qnx4_put_super,
139 	.statfs		= qnx4_statfs,
140 	.remount_fs	= qnx4_remount,
141 #ifdef CONFIG_QNX4FS_RW
142 	.write_inode	= qnx4_write_inode,
143 	.delete_inode	= qnx4_delete_inode,
144 	.write_super	= qnx4_write_super,
145 #endif
146 };
147 
148 static int qnx4_remount(struct super_block *sb, int *flags, char *data)
149 {
150 	struct qnx4_sb_info *qs;
151 
152 	qs = qnx4_sb(sb);
153 	qs->Version = QNX4_VERSION;
154 #ifndef CONFIG_QNX4FS_RW
155 	*flags |= MS_RDONLY;
156 #endif
157 	if (*flags & MS_RDONLY) {
158 		return 0;
159 	}
160 
161 	mark_buffer_dirty(qs->sb_buf);
162 
163 	return 0;
164 }
165 
166 static struct buffer_head *qnx4_getblk(struct inode *inode, int nr,
167 				       int create)
168 {
169 	struct buffer_head *result = NULL;
170 
171 	if ( nr >= 0 )
172 		nr = qnx4_block_map( inode, nr );
173 	if (nr) {
174 		result = sb_getblk(inode->i_sb, nr);
175 		return result;
176 	}
177 	if (!create) {
178 		return NULL;
179 	}
180 #if 0
181 	tmp = qnx4_new_block(inode->i_sb);
182 	if (!tmp) {
183 		return NULL;
184 	}
185 	result = sb_getblk(inode->i_sb, tmp);
186 	if (tst) {
187 		qnx4_free_block(inode->i_sb, tmp);
188 		brelse(result);
189 		goto repeat;
190 	}
191 	tst = tmp;
192 #endif
193 	inode->i_ctime = CURRENT_TIME_SEC;
194 	mark_inode_dirty(inode);
195 	return result;
196 }
197 
198 struct buffer_head *qnx4_bread(struct inode *inode, int block, int create)
199 {
200 	struct buffer_head *bh;
201 
202 	bh = qnx4_getblk(inode, block, create);
203 	if (!bh || buffer_uptodate(bh)) {
204 		return bh;
205 	}
206 	ll_rw_block(READ, 1, &bh);
207 	wait_on_buffer(bh);
208 	if (buffer_uptodate(bh)) {
209 		return bh;
210 	}
211 	brelse(bh);
212 
213 	return NULL;
214 }
215 
216 static int qnx4_get_block( struct inode *inode, sector_t iblock, struct buffer_head *bh, int create )
217 {
218 	unsigned long phys;
219 
220 	QNX4DEBUG(("qnx4: qnx4_get_block inode=[%ld] iblock=[%ld]\n",inode->i_ino,iblock));
221 
222 	phys = qnx4_block_map( inode, iblock );
223 	if ( phys ) {
224 		// logical block is before EOF
225 		map_bh(bh, inode->i_sb, phys);
226 	} else if ( create ) {
227 		// to be done.
228 	}
229 	return 0;
230 }
231 
232 unsigned long qnx4_block_map( struct inode *inode, long iblock )
233 {
234 	int ix;
235 	long offset, i_xblk;
236 	unsigned long block = 0;
237 	struct buffer_head *bh = NULL;
238 	struct qnx4_xblk *xblk = NULL;
239 	struct qnx4_inode_entry *qnx4_inode = qnx4_raw_inode(inode);
240 	u16 nxtnt = le16_to_cpu(qnx4_inode->di_num_xtnts);
241 
242 	if ( iblock < le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_size) ) {
243 		// iblock is in the first extent. This is easy.
244 		block = le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_blk) + iblock - 1;
245 	} else {
246 		// iblock is beyond first extent. We have to follow the extent chain.
247 		i_xblk = le32_to_cpu(qnx4_inode->di_xblk);
248 		offset = iblock - le32_to_cpu(qnx4_inode->di_first_xtnt.xtnt_size);
249 		ix = 0;
250 		while ( --nxtnt > 0 ) {
251 			if ( ix == 0 ) {
252 				// read next xtnt block.
253 				bh = sb_bread(inode->i_sb, i_xblk - 1);
254 				if ( !bh ) {
255 					QNX4DEBUG(("qnx4: I/O error reading xtnt block [%ld])\n", i_xblk - 1));
256 					return -EIO;
257 				}
258 				xblk = (struct qnx4_xblk*)bh->b_data;
259 				if ( memcmp( xblk->xblk_signature, "IamXblk", 7 ) ) {
260 					QNX4DEBUG(("qnx4: block at %ld is not a valid xtnt\n", qnx4_inode->i_xblk));
261 					return -EIO;
262 				}
263 			}
264 			if ( offset < le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_size) ) {
265 				// got it!
266 				block = le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_blk) + offset - 1;
267 				break;
268 			}
269 			offset -= le32_to_cpu(xblk->xblk_xtnts[ix].xtnt_size);
270 			if ( ++ix >= xblk->xblk_num_xtnts ) {
271 				i_xblk = le32_to_cpu(xblk->xblk_next_xblk);
272 				ix = 0;
273 				brelse( bh );
274 				bh = NULL;
275 			}
276 		}
277 		if ( bh )
278 			brelse( bh );
279 	}
280 
281 	QNX4DEBUG(("qnx4: mapping block %ld of inode %ld = %ld\n",iblock,inode->i_ino,block));
282 	return block;
283 }
284 
285 static int qnx4_statfs(struct dentry *dentry, struct kstatfs *buf)
286 {
287 	struct super_block *sb = dentry->d_sb;
288 
289 	lock_kernel();
290 
291 	buf->f_type    = sb->s_magic;
292 	buf->f_bsize   = sb->s_blocksize;
293 	buf->f_blocks  = le32_to_cpu(qnx4_sb(sb)->BitMap->di_size) * 8;
294 	buf->f_bfree   = qnx4_count_free_blocks(sb);
295 	buf->f_bavail  = buf->f_bfree;
296 	buf->f_namelen = QNX4_NAME_MAX;
297 
298 	unlock_kernel();
299 
300 	return 0;
301 }
302 
303 /*
304  * Check the root directory of the filesystem to make sure
305  * it really _is_ a qnx4 filesystem, and to check the size
306  * of the directory entry.
307  */
308 static const char *qnx4_checkroot(struct super_block *sb)
309 {
310 	struct buffer_head *bh;
311 	struct qnx4_inode_entry *rootdir;
312 	int rd, rl;
313 	int i, j;
314 	int found = 0;
315 
316 	if (*(qnx4_sb(sb)->sb->RootDir.di_fname) != '/') {
317 		return "no qnx4 filesystem (no root dir).";
318 	} else {
319 		QNX4DEBUG(("QNX4 filesystem found on dev %s.\n", sb->s_id));
320 		rd = le32_to_cpu(qnx4_sb(sb)->sb->RootDir.di_first_xtnt.xtnt_blk) - 1;
321 		rl = le32_to_cpu(qnx4_sb(sb)->sb->RootDir.di_first_xtnt.xtnt_size);
322 		for (j = 0; j < rl; j++) {
323 			bh = sb_bread(sb, rd + j);	/* root dir, first block */
324 			if (bh == NULL) {
325 				return "unable to read root entry.";
326 			}
327 			for (i = 0; i < QNX4_INODES_PER_BLOCK; i++) {
328 				rootdir = (struct qnx4_inode_entry *) (bh->b_data + i * QNX4_DIR_ENTRY_SIZE);
329 				if (rootdir->di_fname != NULL) {
330 					QNX4DEBUG(("Rootdir entry found : [%s]\n", rootdir->di_fname));
331 					if (!strncmp(rootdir->di_fname, QNX4_BMNAME, sizeof QNX4_BMNAME)) {
332 						found = 1;
333 						qnx4_sb(sb)->BitMap = kmalloc( sizeof( struct qnx4_inode_entry ), GFP_KERNEL );
334 						if (!qnx4_sb(sb)->BitMap) {
335 							brelse (bh);
336 							return "not enough memory for bitmap inode";
337 						}
338 						memcpy( qnx4_sb(sb)->BitMap, rootdir, sizeof( struct qnx4_inode_entry ) );	/* keep bitmap inode known */
339 						break;
340 					}
341 				}
342 			}
343 			brelse(bh);
344 			if (found != 0) {
345 				break;
346 			}
347 		}
348 		if (found == 0) {
349 			return "bitmap file not found.";
350 		}
351 	}
352 	return NULL;
353 }
354 
355 static int qnx4_fill_super(struct super_block *s, void *data, int silent)
356 {
357 	struct buffer_head *bh;
358 	struct inode *root;
359 	const char *errmsg;
360 	struct qnx4_sb_info *qs;
361 
362 	qs = kmalloc(sizeof(struct qnx4_sb_info), GFP_KERNEL);
363 	if (!qs)
364 		return -ENOMEM;
365 	s->s_fs_info = qs;
366 	memset(qs, 0, sizeof(struct qnx4_sb_info));
367 
368 	sb_set_blocksize(s, QNX4_BLOCK_SIZE);
369 
370 	/* Check the superblock signature. Since the qnx4 code is
371 	   dangerous, we should leave as quickly as possible
372 	   if we don't belong here... */
373 	bh = sb_bread(s, 1);
374 	if (!bh) {
375 		printk("qnx4: unable to read the superblock\n");
376 		goto outnobh;
377 	}
378 	if ( le32_to_cpup((__le32*) bh->b_data) != QNX4_SUPER_MAGIC ) {
379 		if (!silent)
380 			printk("qnx4: wrong fsid in superblock.\n");
381 		goto out;
382 	}
383 	s->s_op = &qnx4_sops;
384 	s->s_magic = QNX4_SUPER_MAGIC;
385 #ifndef CONFIG_QNX4FS_RW
386 	s->s_flags |= MS_RDONLY;	/* Yup, read-only yet */
387 #endif
388 	qnx4_sb(s)->sb_buf = bh;
389 	qnx4_sb(s)->sb = (struct qnx4_super_block *) bh->b_data;
390 
391 
392  	/* check before allocating dentries, inodes, .. */
393 	errmsg = qnx4_checkroot(s);
394 	if (errmsg != NULL) {
395  		if (!silent)
396  			printk("qnx4: %s\n", errmsg);
397 		goto out;
398 	}
399 
400  	/* does root not have inode number QNX4_ROOT_INO ?? */
401  	root = iget(s, QNX4_ROOT_INO * QNX4_INODES_PER_BLOCK);
402  	if (!root) {
403  		printk("qnx4: get inode failed\n");
404  		goto out;
405  	}
406 
407  	s->s_root = d_alloc_root(root);
408  	if (s->s_root == NULL)
409  		goto outi;
410 
411 	brelse(bh);
412 
413 	return 0;
414 
415       outi:
416 	iput(root);
417       out:
418 	brelse(bh);
419       outnobh:
420 	kfree(qs);
421 	s->s_fs_info = NULL;
422 	return -EINVAL;
423 }
424 
425 static void qnx4_put_super(struct super_block *sb)
426 {
427 	struct qnx4_sb_info *qs = qnx4_sb(sb);
428 	kfree( qs->BitMap );
429 	kfree( qs );
430 	sb->s_fs_info = NULL;
431 	return;
432 }
433 
434 static int qnx4_writepage(struct page *page, struct writeback_control *wbc)
435 {
436 	return block_write_full_page(page,qnx4_get_block, wbc);
437 }
438 static int qnx4_readpage(struct file *file, struct page *page)
439 {
440 	return block_read_full_page(page,qnx4_get_block);
441 }
442 static int qnx4_prepare_write(struct file *file, struct page *page,
443 			      unsigned from, unsigned to)
444 {
445 	struct qnx4_inode_info *qnx4_inode = qnx4_i(page->mapping->host);
446 	return cont_prepare_write(page, from, to, qnx4_get_block,
447 				  &qnx4_inode->mmu_private);
448 }
449 static sector_t qnx4_bmap(struct address_space *mapping, sector_t block)
450 {
451 	return generic_block_bmap(mapping,block,qnx4_get_block);
452 }
453 static const struct address_space_operations qnx4_aops = {
454 	.readpage	= qnx4_readpage,
455 	.writepage	= qnx4_writepage,
456 	.sync_page	= block_sync_page,
457 	.prepare_write	= qnx4_prepare_write,
458 	.commit_write	= generic_commit_write,
459 	.bmap		= qnx4_bmap
460 };
461 
462 static void qnx4_read_inode(struct inode *inode)
463 {
464 	struct buffer_head *bh;
465 	struct qnx4_inode_entry *raw_inode;
466 	int block, ino;
467 	struct super_block *sb = inode->i_sb;
468 	struct qnx4_inode_entry *qnx4_inode = qnx4_raw_inode(inode);
469 
470 	ino = inode->i_ino;
471 	inode->i_mode = 0;
472 
473 	QNX4DEBUG(("Reading inode : [%d]\n", ino));
474 	if (!ino) {
475 		printk("qnx4: bad inode number on dev %s: %d is out of range\n",
476 		       sb->s_id, ino);
477 		return;
478 	}
479 	block = ino / QNX4_INODES_PER_BLOCK;
480 
481 	if (!(bh = sb_bread(sb, block))) {
482 		printk("qnx4: major problem: unable to read inode from dev "
483 		       "%s\n", sb->s_id);
484 		return;
485 	}
486 	raw_inode = ((struct qnx4_inode_entry *) bh->b_data) +
487 	    (ino % QNX4_INODES_PER_BLOCK);
488 
489 	inode->i_mode    = le16_to_cpu(raw_inode->di_mode);
490 	inode->i_uid     = (uid_t)le16_to_cpu(raw_inode->di_uid);
491 	inode->i_gid     = (gid_t)le16_to_cpu(raw_inode->di_gid);
492 	inode->i_nlink   = le16_to_cpu(raw_inode->di_nlink);
493 	inode->i_size    = le32_to_cpu(raw_inode->di_size);
494 	inode->i_mtime.tv_sec   = le32_to_cpu(raw_inode->di_mtime);
495 	inode->i_mtime.tv_nsec = 0;
496 	inode->i_atime.tv_sec   = le32_to_cpu(raw_inode->di_atime);
497 	inode->i_atime.tv_nsec = 0;
498 	inode->i_ctime.tv_sec   = le32_to_cpu(raw_inode->di_ctime);
499 	inode->i_ctime.tv_nsec = 0;
500 	inode->i_blocks  = le32_to_cpu(raw_inode->di_first_xtnt.xtnt_size);
501 	inode->i_blksize = QNX4_DIR_ENTRY_SIZE;
502 
503 	memcpy(qnx4_inode, raw_inode, QNX4_DIR_ENTRY_SIZE);
504 	if (S_ISREG(inode->i_mode)) {
505 		inode->i_op = &qnx4_file_inode_operations;
506 		inode->i_fop = &qnx4_file_operations;
507 		inode->i_mapping->a_ops = &qnx4_aops;
508 		qnx4_i(inode)->mmu_private = inode->i_size;
509 	} else if (S_ISDIR(inode->i_mode)) {
510 		inode->i_op = &qnx4_dir_inode_operations;
511 		inode->i_fop = &qnx4_dir_operations;
512 	} else if (S_ISLNK(inode->i_mode)) {
513 		inode->i_op = &page_symlink_inode_operations;
514 		inode->i_mapping->a_ops = &qnx4_aops;
515 		qnx4_i(inode)->mmu_private = inode->i_size;
516 	} else
517 		printk("qnx4: bad inode %d on dev %s\n",ino,sb->s_id);
518 	brelse(bh);
519 }
520 
521 static kmem_cache_t *qnx4_inode_cachep;
522 
523 static struct inode *qnx4_alloc_inode(struct super_block *sb)
524 {
525 	struct qnx4_inode_info *ei;
526 	ei = kmem_cache_alloc(qnx4_inode_cachep, SLAB_KERNEL);
527 	if (!ei)
528 		return NULL;
529 	return &ei->vfs_inode;
530 }
531 
532 static void qnx4_destroy_inode(struct inode *inode)
533 {
534 	kmem_cache_free(qnx4_inode_cachep, qnx4_i(inode));
535 }
536 
537 static void init_once(void *foo, kmem_cache_t * cachep,
538 		      unsigned long flags)
539 {
540 	struct qnx4_inode_info *ei = (struct qnx4_inode_info *) foo;
541 
542 	if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
543 	    SLAB_CTOR_CONSTRUCTOR)
544 		inode_init_once(&ei->vfs_inode);
545 }
546 
547 static int init_inodecache(void)
548 {
549 	qnx4_inode_cachep = kmem_cache_create("qnx4_inode_cache",
550 					     sizeof(struct qnx4_inode_info),
551 					     0, (SLAB_RECLAIM_ACCOUNT|
552 						SLAB_MEM_SPREAD),
553 					     init_once, NULL);
554 	if (qnx4_inode_cachep == NULL)
555 		return -ENOMEM;
556 	return 0;
557 }
558 
559 static void destroy_inodecache(void)
560 {
561 	if (kmem_cache_destroy(qnx4_inode_cachep))
562 		printk(KERN_INFO
563 		       "qnx4_inode_cache: not all structures were freed\n");
564 }
565 
566 static int qnx4_get_sb(struct file_system_type *fs_type,
567 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
568 {
569 	return get_sb_bdev(fs_type, flags, dev_name, data, qnx4_fill_super,
570 			   mnt);
571 }
572 
573 static struct file_system_type qnx4_fs_type = {
574 	.owner		= THIS_MODULE,
575 	.name		= "qnx4",
576 	.get_sb		= qnx4_get_sb,
577 	.kill_sb	= kill_block_super,
578 	.fs_flags	= FS_REQUIRES_DEV,
579 };
580 
581 static int __init init_qnx4_fs(void)
582 {
583 	int err;
584 
585 	err = init_inodecache();
586 	if (err)
587 		return err;
588 
589 	err = register_filesystem(&qnx4_fs_type);
590 	if (err) {
591 		destroy_inodecache();
592 		return err;
593 	}
594 
595 	printk("QNX4 filesystem 0.2.3 registered.\n");
596 	return 0;
597 }
598 
599 static void __exit exit_qnx4_fs(void)
600 {
601 	unregister_filesystem(&qnx4_fs_type);
602 	destroy_inodecache();
603 }
604 
605 module_init(init_qnx4_fs)
606 module_exit(exit_qnx4_fs)
607 MODULE_LICENSE("GPL");
608 
609