1 /*
2  *   Copyright (C) International Business Machines Corp., 2000-2005
3  *   Portions Copyright (C) Christoph Hellwig, 2001-2002
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #include <linux/config.h>
21 #include <linux/fs.h>
22 #include <linux/module.h>
23 #include <linux/blkdev.h>
24 #include <linux/completion.h>
25 #include <asm/uaccess.h>
26 #include "jfs_incore.h"
27 #include "jfs_filsys.h"
28 #include "jfs_metapage.h"
29 #include "jfs_superblock.h"
30 #include "jfs_dmap.h"
31 #include "jfs_imap.h"
32 #include "jfs_debug.h"
33 
34 MODULE_DESCRIPTION("The Journaled Filesystem (JFS)");
35 MODULE_AUTHOR("Steve Best/Dave Kleikamp/Barry Arndt, IBM");
36 MODULE_LICENSE("GPL");
37 
38 static struct super_operations jfs_super_operations;
39 static struct file_system_type jfs_fs_type;
40 
41 int jfs_stop_threads;
42 static pid_t jfsIOthread;
43 static pid_t jfsCommitThread;
44 static pid_t jfsSyncThread;
45 DECLARE_COMPLETION(jfsIOwait);
46 
47 #ifdef CONFIG_JFS_DEBUG
48 int jfsloglevel = JFS_LOGLEVEL_WARN;
49 MODULE_PARM(jfsloglevel, "i");
50 MODULE_PARM_DESC(jfsloglevel, "Specify JFS loglevel (0, 1 or 2)");
51 #endif
52 
53 /*
54  * External declarations
55  */
56 extern int jfs_mount(struct super_block *);
57 extern int jfs_mount_rw(struct super_block *, int);
58 extern int jfs_umount(struct super_block *);
59 extern int jfs_umount_rw(struct super_block *);
60 
61 extern int jfsIOWait(void *);
62 extern int jfs_lazycommit(void *);
63 extern int jfs_sync(void *);
64 extern void jfs_clear_inode(struct inode *inode);
65 extern void jfs_read_inode(struct inode *inode);
66 extern void jfs_dirty_inode(struct inode *inode);
67 extern void jfs_delete_inode(struct inode *inode);
68 extern void jfs_write_inode(struct inode *inode, int wait);
69 extern int jfs_extendfs(struct super_block *, s64, int);
70 
71 #ifdef PROC_FS_JFS		/* see jfs_debug.h */
72 extern void jfs_proc_init(void);
73 extern void jfs_proc_clean(void);
74 #endif
75 
76 extern wait_queue_head_t jfs_IO_thread_wait;
77 extern wait_queue_head_t jfs_commit_thread_wait;
78 extern wait_queue_head_t jfs_sync_thread_wait;
79 
jfs_handle_error(struct super_block * sb)80 static void jfs_handle_error(struct super_block *sb)
81 {
82 	struct jfs_sb_info *sbi = JFS_SBI(sb);
83 
84 	if (sb->s_flags & MS_RDONLY)
85 		return;
86 
87 	updateSuper(sb, FM_DIRTY);
88 
89 	if (sbi->flag & JFS_ERR_PANIC)
90 		panic("JFS (device %s): panic forced after error\n",
91 			bdevname(sb->s_dev));
92 	else if (sbi->flag & JFS_ERR_REMOUNT_RO) {
93 		jfs_err("ERROR: (device %s): remounting filesystem "
94 			"as read-only\n",
95 			bdevname(sb->s_dev));
96 		sb->s_flags |= MS_RDONLY;
97 	}
98 
99 	/* nothing is done for continue beyond marking the superblock dirty */
100 }
101 
jfs_error(struct super_block * sb,const char * function,...)102 void jfs_error(struct super_block *sb, const char * function, ...)
103 {
104 	static char error_buf[256];
105 	va_list args;
106 
107 	va_start(args, function);
108 	vsprintf(error_buf, function, args);
109 	va_end(args);
110 
111 	printk(KERN_ERR "ERROR: (device %s): %s\n", bdevname(sb->s_dev),
112 	       error_buf);
113 
114 	jfs_handle_error(sb);
115 }
116 
jfs_statfs(struct super_block * sb,struct statfs * buf)117 static int jfs_statfs(struct super_block *sb, struct statfs *buf)
118 {
119 	struct jfs_sb_info *sbi = JFS_SBI(sb);
120 	s64 maxinodes;
121 	struct inomap *imap = JFS_IP(sbi->ipimap)->i_imap;
122 
123 	jfs_info("In jfs_statfs");
124 	buf->f_type = JFS_SUPER_MAGIC;
125 	buf->f_bsize = sbi->bsize;
126 	buf->f_blocks = sbi->bmap->db_mapsize;
127 	buf->f_bfree = sbi->bmap->db_nfree;
128 	buf->f_bavail = sbi->bmap->db_nfree;
129 	/*
130 	 * If we really return the number of allocated & free inodes, some
131 	 * applications will fail because they won't see enough free inodes.
132 	 * We'll try to calculate some guess as to how may inodes we can
133 	 * really allocate
134 	 *
135 	 * buf->f_files = atomic_read(&imap->im_numinos);
136 	 * buf->f_ffree = atomic_read(&imap->im_numfree);
137 	 */
138 	maxinodes = min((s64) atomic_read(&imap->im_numinos) +
139 			((sbi->bmap->db_nfree >> imap->im_l2nbperiext)
140 			 << L2INOSPEREXT), (s64) 0xffffffffLL);
141 	buf->f_files = maxinodes;
142 	buf->f_ffree = maxinodes - (atomic_read(&imap->im_numinos) -
143 				    atomic_read(&imap->im_numfree));
144 
145 	buf->f_namelen = JFS_NAME_MAX;
146 	return 0;
147 }
148 
jfs_put_super(struct super_block * sb)149 static void jfs_put_super(struct super_block *sb)
150 {
151 	struct jfs_sb_info *sbi = JFS_SBI(sb);
152 	int rc;
153 
154 	jfs_info("In jfs_put_super");
155 	rc = jfs_umount(sb);
156 	if (rc)
157 		jfs_err("jfs_umount failed with return code %d", rc);
158 	if (sbi->nls_tab) {
159 		unload_nls(sbi->nls_tab);
160 		sbi->nls_tab = NULL;
161 	}
162 
163 	kfree(sbi);
164 }
165 
jfs_get_volume_size(struct super_block * sb)166 s64 jfs_get_volume_size(struct super_block *sb)
167 {
168 	uint blocks = 0;
169 	s64 bytes;
170 	kdev_t dev = sb->s_dev;
171 	int major = MAJOR(dev);
172 	int minor = MINOR(dev);
173 
174 	if (blk_size[major]) {
175 		blocks = blk_size[major][minor];
176 		if (blocks) {
177 			bytes = ((s64)blocks) << BLOCK_SIZE_BITS;
178 			return bytes >> sb->s_blocksize_bits;
179 		}
180 	}
181 	return 0;
182 }
183 
parse_options(char * options,struct super_block * sb,s64 * newLVSize,int * flag)184 static int parse_options(char *options, struct super_block *sb, s64 *newLVSize,
185 			 int *flag)
186 {
187 	void *nls_map = (void *)-1;	/* -1: no change;  NULL: none */
188 	char *this_char;
189 	char *value;
190 	struct jfs_sb_info *sbi = JFS_SBI(sb);
191 
192 	*newLVSize = 0;
193 
194 	if (!options)
195 		return 1;
196 	while ((this_char = strsep(&options, ",")) != NULL) {
197 		if (!*this_char)
198 			continue;
199 		if ((value = strchr(this_char, '=')) != NULL)
200 			*value++ = 0;
201 		if (!strcmp(this_char, "errors")) {
202 			if (!value || !*value)
203 				goto needs_arg;
204 			if (!strcmp(value, "continue")) {
205 				*flag &= ~JFS_ERR_REMOUNT_RO;
206 				*flag &= ~JFS_ERR_PANIC;
207 				*flag |= JFS_ERR_CONTINUE;
208 			} else if (!strcmp(value, "remount-ro")) {
209 				*flag &= ~JFS_ERR_CONTINUE;
210 				*flag &= ~JFS_ERR_PANIC;
211 				*flag |= JFS_ERR_REMOUNT_RO;
212 			} else if (!strcmp(value, "panic")) {
213 				*flag &= ~JFS_ERR_CONTINUE;
214 				*flag &= ~JFS_ERR_REMOUNT_RO;
215 				*flag |= JFS_ERR_PANIC;
216 			} else {
217 				printk(KERN_ERR "JFS: %s is an invalid error handler\n", value);
218 				goto cleanup;
219 			}
220 		} else if (!strcmp(this_char, "integrity")) {
221 			*flag &= ~JFS_NOINTEGRITY;
222 		} else 	if (!strcmp(this_char, "nointegrity")) {
223 			*flag |= JFS_NOINTEGRITY;
224 		} else if (!strcmp(this_char, "iocharset")) {
225 			if (!value || !*value)
226 				goto needs_arg;
227 			if (nls_map && nls_map != (void *) -1)
228 				unload_nls(nls_map);
229 			if (!strcmp(value, "none"))
230 				nls_map = NULL;
231 			else
232 				nls_map = load_nls(value);
233 		} else if (!strcmp(this_char, "resize")) {
234 			if (!value || !*value) {
235 				*newLVSize = jfs_get_volume_size(sb);
236 				if (*newLVSize == 0)
237 					printk(KERN_ERR
238 					 "JFS: Cannot determine volume size\n");
239 			} else
240 				*newLVSize = simple_strtoull(value, &value, 0);
241 
242 			/* Silently ignore the quota options */
243 		} else if (!strcmp(this_char, "grpquota")
244 			   || !strcmp(this_char, "noquota")
245 			   || !strcmp(this_char, "quota")
246 			   || !strcmp(this_char, "usrquota"))
247 			/* Don't do anything ;-) */ ;
248 		else {
249 			printk("jfs: Unrecognized mount option %s\n",
250 			       this_char);
251 			goto cleanup;
252 		}
253 	}
254 	if (nls_map != (void *) -1) {
255 		/* Discard old (if remount) */
256 		if (sbi->nls_tab && sbi->nls_tab != (void *) -1)
257 			unload_nls(sbi->nls_tab);
258 		sbi->nls_tab = nls_map;
259 	}
260 	return 1;
261 needs_arg:
262 	printk(KERN_ERR "JFS: %s needs an argument\n", this_char);
263 cleanup:
264 	if (nls_map && nls_map != (void *) -1)
265 		unload_nls(nls_map);
266 	return 0;
267 }
268 
jfs_remount(struct super_block * sb,int * flags,char * data)269 static int jfs_remount(struct super_block *sb, int *flags, char *data)
270 {
271 	s64 newLVSize = 0;
272 	int rc = 0;
273 	int flag = JFS_SBI(sb)->flag;
274 
275 	if (!parse_options(data, sb, &newLVSize, &flag)) {
276 		return -EINVAL;
277 	}
278 	if (newLVSize) {
279 		if (sb->s_flags & MS_RDONLY) {
280 			printk(KERN_ERR
281 		  "JFS: resize requires volume to be mounted read-write\n");
282 			return -EROFS;
283 		}
284 		rc = jfs_extendfs(sb, newLVSize, 0);
285 		if (rc)
286 			return rc;
287 	}
288 
289 	if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
290 		JFS_SBI(sb)->flag = flag;
291 		return jfs_mount_rw(sb, 1);
292 	}
293 	if ((!(sb->s_flags & MS_RDONLY)) && (*flags & MS_RDONLY)) {
294 		rc = jfs_umount_rw(sb);
295 		JFS_SBI(sb)->flag = flag;
296 		return rc;
297 	}
298 	if ((JFS_SBI(sb)->flag & JFS_NOINTEGRITY) != (flag & JFS_NOINTEGRITY))
299 		if (!(sb->s_flags & MS_RDONLY)) {
300 			rc = jfs_umount_rw(sb);
301 			if (rc)
302 				return rc;
303 			JFS_SBI(sb)->flag = flag;
304 			return jfs_mount_rw(sb, 1);
305 		}
306 	JFS_SBI(sb)->flag = flag;
307 
308 	return 0;
309 }
310 
jfs_read_super(struct super_block * sb,void * data,int silent)311 static struct super_block *jfs_read_super(struct super_block *sb,
312 					  void *data, int silent)
313 {
314 	struct jfs_sb_info *sbi;
315 	struct inode *inode;
316 	int rc;
317 	s64 newLVSize = 0;
318 	int flag;
319 
320 	jfs_info("In jfs_read_super s_dev=0x%x s_flags=0x%lx", sb->s_dev,
321 		 sb->s_flags);
322 
323 	sbi = kmalloc(sizeof (struct jfs_sb_info), GFP_KERNEL);
324 	if (!sbi)
325 		return NULL;
326 	memset(sbi, 0, sizeof (struct jfs_sb_info));
327 	sb->u.generic_sbp = sbi;
328 
329 	/* initialize the mount flag and determine the default error handler */
330 	flag = JFS_ERR_REMOUNT_RO;
331 
332 	/* nls_tab will be set to NULL if no character mapping is requested */
333 	sbi->nls_tab = (void *) -1;
334 	if (!parse_options((char *) data, sb, &newLVSize, &flag)) {
335 		kfree(sbi);
336 		return NULL;
337 	}
338 	sbi->flag = flag;
339 
340 	if (newLVSize) {
341 		printk(KERN_ERR "resize option for remount only\n");
342 		return NULL;
343 	}
344 
345 	/*
346 	 * Initialize blocksize to 4K.
347 	 */
348 	sb_set_blocksize(sb, PSIZE);
349 
350 	/*
351 	 * Set method vectors.
352 	 */
353 	sb->s_op = &jfs_super_operations;
354 
355 	rc = jfs_mount(sb);
356 	if (rc) {
357 		if (!silent) {
358 			jfs_err("jfs_mount failed w/return code = %d", rc);
359 		}
360 		goto out_kfree;
361 	}
362 	if (sb->s_flags & MS_RDONLY)
363 		sbi->log = 0;
364 	else {
365 		rc = jfs_mount_rw(sb, 0);
366 		if (rc) {
367 			if (!silent) {
368 				jfs_err("jfs_mount_rw failed, return code = %d",
369 					rc);
370 			}
371 			goto out_no_rw;
372 		}
373 	}
374 
375 	sb->s_magic = JFS_SUPER_MAGIC;
376 
377 	inode = iget(sb, ROOT_I);
378 	if (!inode || is_bad_inode(inode))
379 		goto out_no_root;
380 	sb->s_root = d_alloc_root(inode);
381 	if (!sb->s_root)
382 		goto out_no_root;
383 
384 	if (sbi->nls_tab == (void *) -1)
385 		sbi->nls_tab = load_nls_default();
386 
387 	/* logical blocks are represented by 40 bits in pxd_t, etc. */
388 	sb->s_maxbytes = ((u64) sb->s_blocksize) << 40;
389 #if BITS_PER_LONG == 32
390 	/*
391 	 * Page cache is indexed by long.
392 	 * I would use MAX_LFS_FILESIZE, but it's only half as big
393 	 */
394 	sb->s_maxbytes = min(((u64) PAGE_CACHE_SIZE << 32) - 1, sb->s_maxbytes);
395 #endif
396 
397 	return sb;
398 
399 out_no_root:
400 	jfs_err("jfs_read_super: get root inode failed");
401 	if (inode)
402 		iput(inode);
403 
404 out_no_rw:
405 	rc = jfs_umount(sb);
406 	if (rc) {
407 		jfs_err("jfs_umount failed with return code %d", rc);
408 	}
409 out_kfree:
410 	if (sbi->nls_tab && sbi->nls_tab != (void *) -1)
411 		unload_nls(sbi->nls_tab);
412 	kfree(sbi);
413 	return NULL;
414 }
415 
jfs_write_super_lockfs(struct super_block * sb)416 static void jfs_write_super_lockfs(struct super_block *sb)
417 {
418 	struct jfs_sb_info *sbi = JFS_SBI(sb);
419 	struct jfs_log *log = sbi->log;
420 
421 	if (!(sb->s_flags & MS_RDONLY)) {
422 		txQuiesce(sb);
423 		lmLogShutdown(log);
424 		updateSuper(sb, FM_CLEAN);
425 	}
426 }
427 
jfs_unlockfs(struct super_block * sb)428 static void jfs_unlockfs(struct super_block *sb)
429 {
430 	struct jfs_sb_info *sbi = JFS_SBI(sb);
431 	struct jfs_log *log = sbi->log;
432 	int rc = 0;
433 
434 	if (!(sb->s_flags & MS_RDONLY)) {
435 		updateSuper(sb, FM_MOUNT);
436 		if ((rc = lmLogInit(log)))
437 			jfs_err("jfs_unlock failed with return code %d", rc);
438 		else
439 			txResume(sb);
440 	}
441 }
442 
443 
jfs_sync_fs(struct super_block * sb)444 static int jfs_sync_fs(struct super_block *sb)
445 {
446 	struct jfs_log *log = JFS_SBI(sb)->log;
447 
448 	/* log == NULL indicates read-only mount */
449 	if (log)
450 		jfs_flush_journal(log, 1);
451 
452 	return 0;
453 }
454 
455 static struct super_operations jfs_super_operations = {
456 	.read_inode	= jfs_read_inode,
457 	.dirty_inode	= jfs_dirty_inode,
458 	.write_inode	= jfs_write_inode,
459 	.clear_inode	= jfs_clear_inode,
460 	.delete_inode	= jfs_delete_inode,
461 	.put_super	= jfs_put_super,
462 	.sync_fs	= jfs_sync_fs,
463 	.write_super_lockfs = jfs_write_super_lockfs,
464 	.unlockfs       = jfs_unlockfs,
465 	.statfs		= jfs_statfs,
466 	.remount_fs	= jfs_remount,
467 };
468 
469 static struct file_system_type jfs_fs_type = {
470 	.owner		= THIS_MODULE,
471 	.name		= "jfs",
472 	.read_super	= jfs_read_super,
473 	.fs_flags	= FS_REQUIRES_DEV,
474 };
475 
476 extern int metapage_init(void);
477 extern int txInit(void);
478 extern void txExit(void);
479 extern void metapage_exit(void);
480 
init_once(void * foo,kmem_cache_t * cachep,unsigned long flags)481 static void init_once(void *foo, kmem_cache_t * cachep, unsigned long flags)
482 {
483 	struct jfs_inode_info *jfs_ip = (struct jfs_inode_info *) foo;
484 
485 	if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
486 	    SLAB_CTOR_CONSTRUCTOR) {
487 		memset(jfs_ip, 0, sizeof(struct jfs_inode_info));
488 		INIT_LIST_HEAD(&jfs_ip->anon_inode_list);
489 		init_rwsem(&jfs_ip->rdwrlock);
490 		init_MUTEX(&jfs_ip->commit_sem);
491 		spin_lock_init(&jfs_ip->ag_lock);
492 		jfs_ip->active_ag = -1;
493 	}
494 }
495 
init_jfs_fs(void)496 static int __init init_jfs_fs(void)
497 {
498 	int rc;
499 
500 	jfs_inode_cachep =
501 	    kmem_cache_create("jfs_ip", sizeof (struct jfs_inode_info), 0, 0,
502 			      init_once, NULL);
503 	if (jfs_inode_cachep == NULL)
504 		return -ENOMEM;
505 
506 	/*
507 	 * Metapage initialization
508 	 */
509 	rc = metapage_init();
510 	if (rc) {
511 		jfs_err("metapage_init failed w/rc = %d", rc);
512 		goto free_slab;
513 	}
514 
515 	/*
516 	 * Transaction Manager initialization
517 	 */
518 	rc = txInit();
519 	if (rc) {
520 		jfs_err("txInit failed w/rc = %d", rc);
521 		goto free_metapage;
522 	}
523 
524 	/*
525 	 * I/O completion thread (endio)
526 	 */
527 	jfsIOthread = kernel_thread(jfsIOWait, 0,
528 				    CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
529 	if (jfsIOthread < 0) {
530 		jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsIOthread);
531 		goto end_txmngr;
532 	}
533 	wait_for_completion(&jfsIOwait);	/* Wait until thread starts */
534 
535 	jfsCommitThread = kernel_thread(jfs_lazycommit, 0,
536 					CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
537 	if (jfsCommitThread < 0) {
538 		jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsCommitThread);
539 		goto kill_iotask;
540 	}
541 	wait_for_completion(&jfsIOwait);	/* Wait until thread starts */
542 
543 	jfsSyncThread = kernel_thread(jfs_sync, 0,
544 				      CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
545 	if (jfsSyncThread < 0) {
546 		jfs_err("init_jfs_fs: fork failed w/rc = %d", jfsSyncThread);
547 		goto kill_committask;
548 	}
549 	wait_for_completion(&jfsIOwait);	/* Wait until thread starts */
550 
551 #ifdef PROC_FS_JFS
552 	jfs_proc_init();
553 #endif
554 
555 	return register_filesystem(&jfs_fs_type);
556 
557 kill_committask:
558 	jfs_stop_threads = 1;
559 	wake_up(&jfs_commit_thread_wait);
560 	wait_for_completion(&jfsIOwait);	/* Wait for thread exit */
561 kill_iotask:
562 	jfs_stop_threads = 1;
563 	wake_up(&jfs_IO_thread_wait);
564 	wait_for_completion(&jfsIOwait);	/* Wait for thread exit */
565 end_txmngr:
566 	txExit();
567 free_metapage:
568 	metapage_exit();
569 free_slab:
570 	kmem_cache_destroy(jfs_inode_cachep);
571 	return rc;
572 }
573 
exit_jfs_fs(void)574 static void __exit exit_jfs_fs(void)
575 {
576 	jfs_info("exit_jfs_fs called");
577 
578 	jfs_stop_threads = 1;
579 	txExit();
580 	metapage_exit();
581 	wake_up(&jfs_IO_thread_wait);
582 	wait_for_completion(&jfsIOwait);	/* Wait for IO thread exit */
583 	wake_up(&jfs_commit_thread_wait);
584 	wait_for_completion(&jfsIOwait);	/* Wait for Commit thread exit */
585 	wake_up(&jfs_sync_thread_wait);
586 	wait_for_completion(&jfsIOwait);	/* Wait for Sync thread exit */
587 #ifdef PROC_FS_JFS
588 	jfs_proc_clean();
589 #endif
590 	unregister_filesystem(&jfs_fs_type);
591 	kmem_cache_destroy(jfs_inode_cachep);
592 }
593 
594 EXPORT_NO_SYMBOLS;
595 
596 module_init(init_jfs_fs)
597 module_exit(exit_jfs_fs)
598