dmaengine: sprd: Add interrupt support for 2-stage transfer
[linux-2.6-block.git] / fs / jfs / super.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) International Business Machines Corp., 2000-2004
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
63f83c9f 7 * the Free Software Foundation; either version 2 of the License, or
1da177e4 8 * (at your option) any later version.
63f83c9f 9 *
1da177e4
LT
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
63f83c9f 16 * along with this program; if not, write to the Free Software
1da177e4
LT
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/fs.h>
1da177e4
LT
21#include <linux/module.h>
22#include <linux/parser.h>
23#include <linux/completion.h>
24#include <linux/vfs.h>
74abb989 25#include <linux/quotaops.h>
8fc2751b 26#include <linux/mount.h>
1da177e4 27#include <linux/moduleparam.h>
91dbb4de 28#include <linux/kthread.h>
9a59f452 29#include <linux/posix_acl.h>
115ff50b 30#include <linux/buffer_head.h>
a5694255 31#include <linux/exportfs.h>
b5c816a4 32#include <linux/crc32.h>
5a0e3ad6 33#include <linux/slab.h>
7c0f6ba6 34#include <linux/uaccess.h>
8fc2751b 35#include <linux/seq_file.h>
b40c2e66 36#include <linux/blkdev.h>
1da177e4
LT
37
38#include "jfs_incore.h"
39#include "jfs_filsys.h"
1868f4aa 40#include "jfs_inode.h"
1da177e4
LT
41#include "jfs_metapage.h"
42#include "jfs_superblock.h"
43#include "jfs_dmap.h"
44#include "jfs_imap.h"
45#include "jfs_acl.h"
46#include "jfs_debug.h"
2cc6a5a0 47#include "jfs_xattr.h"
12fd086d 48#include "jfs_dinode.h"
1da177e4
LT
49
50MODULE_DESCRIPTION("The Journaled Filesystem (JFS)");
51MODULE_AUTHOR("Steve Best/Dave Kleikamp/Barry Arndt, IBM");
52MODULE_LICENSE("GPL");
53
789602e9 54static struct kmem_cache *jfs_inode_cachep;
1da177e4 55
ee9b6d61 56static const struct super_operations jfs_super_operations;
39655164 57static const struct export_operations jfs_export_operations;
1da177e4
LT
58static struct file_system_type jfs_fs_type;
59
60#define MAX_COMMIT_THREADS 64
789602e9 61static int commit_threads;
1da177e4
LT
62module_param(commit_threads, int, 0);
63MODULE_PARM_DESC(commit_threads, "Number of commit threads");
64
91dbb4de
CH
65static struct task_struct *jfsCommitThread[MAX_COMMIT_THREADS];
66struct task_struct *jfsIOthread;
67struct task_struct *jfsSyncThread;
1da177e4
LT
68
69#ifdef CONFIG_JFS_DEBUG
70int jfsloglevel = JFS_LOGLEVEL_WARN;
71module_param(jfsloglevel, int, 0644);
72MODULE_PARM_DESC(jfsloglevel, "Specify JFS loglevel (0, 1 or 2)");
73#endif
74
1da177e4
LT
75static void jfs_handle_error(struct super_block *sb)
76{
77 struct jfs_sb_info *sbi = JFS_SBI(sb);
78
bc98a42c 79 if (sb_rdonly(sb))
1da177e4
LT
80 return;
81
82 updateSuper(sb, FM_DIRTY);
83
84 if (sbi->flag & JFS_ERR_PANIC)
85 panic("JFS (device %s): panic forced after error\n",
86 sb->s_id);
87 else if (sbi->flag & JFS_ERR_REMOUNT_RO) {
b18db6de 88 jfs_err("ERROR: (device %s): remounting filesystem as read-only",
1da177e4 89 sb->s_id);
1751e8a6 90 sb->s_flags |= SB_RDONLY;
63f83c9f 91 }
1da177e4
LT
92
93 /* nothing is done for continue beyond marking the superblock dirty */
94}
95
eb8630d7 96void jfs_error(struct super_block *sb, const char *fmt, ...)
1da177e4 97{
eb8630d7 98 struct va_format vaf;
1da177e4
LT
99 va_list args;
100
eb8630d7
JP
101 va_start(args, fmt);
102
103 vaf.fmt = fmt;
104 vaf.va = &args;
1da177e4 105
7d2ac456 106 pr_err("ERROR: (device %s): %ps: %pV\n",
eb8630d7
JP
107 sb->s_id, __builtin_return_address(0), &vaf);
108
109 va_end(args);
1da177e4
LT
110
111 jfs_handle_error(sb);
112}
113
114static struct inode *jfs_alloc_inode(struct super_block *sb)
115{
116 struct jfs_inode_info *jfs_inode;
117
118 jfs_inode = kmem_cache_alloc(jfs_inode_cachep, GFP_NOFS);
119 if (!jfs_inode)
120 return NULL;
507e1fa6
JK
121#ifdef CONFIG_QUOTA
122 memset(&jfs_inode->i_dquot, 0, sizeof(jfs_inode->i_dquot));
123#endif
1da177e4
LT
124 return &jfs_inode->vfs_inode;
125}
126
b3b4a6e3 127static void jfs_free_inode(struct inode *inode)
fa0d7e3d 128{
b3b4a6e3 129 kmem_cache_free(jfs_inode_cachep, JFS_IP(inode));
1da177e4
LT
130}
131
726c3342 132static int jfs_statfs(struct dentry *dentry, struct kstatfs *buf)
1da177e4 133{
726c3342 134 struct jfs_sb_info *sbi = JFS_SBI(dentry->d_sb);
1da177e4
LT
135 s64 maxinodes;
136 struct inomap *imap = JFS_IP(sbi->ipimap)->i_imap;
137
138 jfs_info("In jfs_statfs");
139 buf->f_type = JFS_SUPER_MAGIC;
140 buf->f_bsize = sbi->bsize;
141 buf->f_blocks = sbi->bmap->db_mapsize;
142 buf->f_bfree = sbi->bmap->db_nfree;
143 buf->f_bavail = sbi->bmap->db_nfree;
144 /*
145 * If we really return the number of allocated & free inodes, some
146 * applications will fail because they won't see enough free inodes.
4de80273 147 * We'll try to calculate some guess as to how many inodes we can
1da177e4
LT
148 * really allocate
149 *
150 * buf->f_files = atomic_read(&imap->im_numinos);
151 * buf->f_ffree = atomic_read(&imap->im_numfree);
152 */
153 maxinodes = min((s64) atomic_read(&imap->im_numinos) +
154 ((sbi->bmap->db_nfree >> imap->im_l2nbperiext)
155 << L2INOSPEREXT), (s64) 0xffffffffLL);
156 buf->f_files = maxinodes;
157 buf->f_ffree = maxinodes - (atomic_read(&imap->im_numinos) -
158 atomic_read(&imap->im_numfree));
2e3bc612
AS
159 buf->f_fsid.val[0] = crc32_le(0, (char *)&sbi->uuid,
160 sizeof(sbi->uuid)/2);
161 buf->f_fsid.val[1] = crc32_le(0,
162 (char *)&sbi->uuid + sizeof(sbi->uuid)/2,
163 sizeof(sbi->uuid)/2);
1da177e4
LT
164
165 buf->f_namelen = JFS_NAME_MAX;
166 return 0;
167}
168
12fd086d
JK
169#ifdef CONFIG_QUOTA
170static int jfs_quota_off(struct super_block *sb, int type);
171static int jfs_quota_on(struct super_block *sb, int type, int format_id,
172 const struct path *path);
173
174static void jfs_quota_off_umount(struct super_block *sb)
175{
176 int type;
177
178 for (type = 0; type < MAXQUOTAS; type++)
179 jfs_quota_off(sb, type);
180}
181
182static const struct quotactl_ops jfs_quotactl_ops = {
183 .quota_on = jfs_quota_on,
184 .quota_off = jfs_quota_off,
185 .quota_sync = dquot_quota_sync,
186 .get_state = dquot_get_state,
187 .set_info = dquot_set_dqinfo,
188 .get_dqblk = dquot_get_dqblk,
189 .set_dqblk = dquot_set_dqblk,
190 .get_nextdqblk = dquot_get_next_dqblk,
191};
192#else
193static inline void jfs_quota_off_umount(struct super_block *sb)
194{
195}
196#endif
197
1da177e4
LT
198static void jfs_put_super(struct super_block *sb)
199{
200 struct jfs_sb_info *sbi = JFS_SBI(sb);
201 int rc;
202
203 jfs_info("In jfs_put_super");
6cfd0148 204
12fd086d 205 jfs_quota_off_umount(sb);
e0ccfd95 206
1da177e4
LT
207 rc = jfs_umount(sb);
208 if (rc)
209 jfs_err("jfs_umount failed with return code %d", rc);
6d729e44
TG
210
211 unload_nls(sbi->nls_tab);
1da177e4 212
7fab479b
DK
213 truncate_inode_pages(sbi->direct_inode->i_mapping, 0);
214 iput(sbi->direct_inode);
7fab479b 215
1da177e4
LT
216 kfree(sbi);
217}
218
219enum {
220 Opt_integrity, Opt_nointegrity, Opt_iocharset, Opt_resize,
8fc2751b 221 Opt_resize_nosize, Opt_errors, Opt_ignore, Opt_err, Opt_quota,
b40c2e66
TR
222 Opt_usrquota, Opt_grpquota, Opt_uid, Opt_gid, Opt_umask,
223 Opt_discard, Opt_nodiscard, Opt_discard_minblk
1da177e4
LT
224};
225
a447c093 226static const match_table_t tokens = {
1da177e4
LT
227 {Opt_integrity, "integrity"},
228 {Opt_nointegrity, "nointegrity"},
229 {Opt_iocharset, "iocharset=%s"},
230 {Opt_resize, "resize=%u"},
231 {Opt_resize_nosize, "resize"},
232 {Opt_errors, "errors=%s"},
233 {Opt_ignore, "noquota"},
02645bcd 234 {Opt_quota, "quota"},
8fc2751b
MB
235 {Opt_usrquota, "usrquota"},
236 {Opt_grpquota, "grpquota"},
69eb66d7
DK
237 {Opt_uid, "uid=%u"},
238 {Opt_gid, "gid=%u"},
239 {Opt_umask, "umask=%u"},
b40c2e66
TR
240 {Opt_discard, "discard"},
241 {Opt_nodiscard, "nodiscard"},
242 {Opt_discard_minblk, "discard=%u"},
1da177e4
LT
243 {Opt_err, NULL}
244};
245
246static int parse_options(char *options, struct super_block *sb, s64 *newLVSize,
247 int *flag)
248{
249 void *nls_map = (void *)-1; /* -1: no change; NULL: none */
250 char *p;
251 struct jfs_sb_info *sbi = JFS_SBI(sb);
252
253 *newLVSize = 0;
254
255 if (!options)
256 return 1;
257
258 while ((p = strsep(&options, ",")) != NULL) {
259 substring_t args[MAX_OPT_ARGS];
260 int token;
261 if (!*p)
262 continue;
263
264 token = match_token(p, tokens, args);
265 switch (token) {
266 case Opt_integrity:
267 *flag &= ~JFS_NOINTEGRITY;
268 break;
269 case Opt_nointegrity:
270 *flag |= JFS_NOINTEGRITY;
271 break;
272 case Opt_ignore:
273 /* Silently ignore the quota options */
274 /* Don't do anything ;-) */
275 break;
276 case Opt_iocharset:
277 if (nls_map && nls_map != (void *) -1)
278 unload_nls(nls_map);
279 if (!strcmp(args[0].from, "none"))
280 nls_map = NULL;
281 else {
282 nls_map = load_nls(args[0].from);
283 if (!nls_map) {
b40c2e66 284 pr_err("JFS: charset not found\n");
1da177e4
LT
285 goto cleanup;
286 }
287 }
288 break;
289 case Opt_resize:
290 {
291 char *resize = args[0].from;
bb5e50aa
FF
292 int rc = kstrtoll(resize, 0, newLVSize);
293
294 if (rc)
295 goto cleanup;
1da177e4
LT
296 break;
297 }
298 case Opt_resize_nosize:
299 {
684666e5 300 *newLVSize = i_size_read(sb->s_bdev->bd_inode) >>
1da177e4
LT
301 sb->s_blocksize_bits;
302 if (*newLVSize == 0)
b40c2e66 303 pr_err("JFS: Cannot determine volume size\n");
1da177e4
LT
304 break;
305 }
306 case Opt_errors:
307 {
308 char *errors = args[0].from;
309 if (!errors || !*errors)
310 goto cleanup;
311 if (!strcmp(errors, "continue")) {
312 *flag &= ~JFS_ERR_REMOUNT_RO;
313 *flag &= ~JFS_ERR_PANIC;
314 *flag |= JFS_ERR_CONTINUE;
315 } else if (!strcmp(errors, "remount-ro")) {
316 *flag &= ~JFS_ERR_CONTINUE;
317 *flag &= ~JFS_ERR_PANIC;
318 *flag |= JFS_ERR_REMOUNT_RO;
319 } else if (!strcmp(errors, "panic")) {
320 *flag &= ~JFS_ERR_CONTINUE;
321 *flag &= ~JFS_ERR_REMOUNT_RO;
322 *flag |= JFS_ERR_PANIC;
323 } else {
b40c2e66 324 pr_err("JFS: %s is an invalid error handler\n",
1da177e4
LT
325 errors);
326 goto cleanup;
327 }
328 break;
329 }
8fc2751b 330
115ff50b 331#ifdef CONFIG_QUOTA
8fc2751b
MB
332 case Opt_quota:
333 case Opt_usrquota:
334 *flag |= JFS_USRQUOTA;
335 break;
336 case Opt_grpquota:
337 *flag |= JFS_GRPQUOTA;
338 break;
339#else
340 case Opt_usrquota:
341 case Opt_grpquota:
342 case Opt_quota:
b40c2e66 343 pr_err("JFS: quota operations not supported\n");
8fc2751b
MB
344 break;
345#endif
69eb66d7
DK
346 case Opt_uid:
347 {
348 char *uid = args[0].from;
bb5e50aa
FF
349 uid_t val;
350 int rc = kstrtouint(uid, 0, &val);
351
352 if (rc)
353 goto cleanup;
c18cdc1a
EB
354 sbi->uid = make_kuid(current_user_ns(), val);
355 if (!uid_valid(sbi->uid))
356 goto cleanup;
69eb66d7
DK
357 break;
358 }
b40c2e66 359
69eb66d7
DK
360 case Opt_gid:
361 {
362 char *gid = args[0].from;
bb5e50aa
FF
363 gid_t val;
364 int rc = kstrtouint(gid, 0, &val);
365
366 if (rc)
367 goto cleanup;
c18cdc1a
EB
368 sbi->gid = make_kgid(current_user_ns(), val);
369 if (!gid_valid(sbi->gid))
370 goto cleanup;
69eb66d7
DK
371 break;
372 }
b40c2e66 373
69eb66d7
DK
374 case Opt_umask:
375 {
376 char *umask = args[0].from;
bb5e50aa
FF
377 int rc = kstrtouint(umask, 8, &sbi->umask);
378
379 if (rc)
380 goto cleanup;
69eb66d7 381 if (sbi->umask & ~0777) {
b40c2e66 382 pr_err("JFS: Invalid value of umask\n");
69eb66d7
DK
383 goto cleanup;
384 }
385 break;
386 }
b40c2e66
TR
387
388 case Opt_discard:
389 {
390 struct request_queue *q = bdev_get_queue(sb->s_bdev);
391 /* if set to 1, even copying files will cause
392 * trimming :O
393 * -> user has more control over the online trimming
394 */
395 sbi->minblks_trim = 64;
789602e9 396 if (blk_queue_discard(q))
b40c2e66 397 *flag |= JFS_DISCARD;
789602e9
FF
398 else
399 pr_err("JFS: discard option not supported on device\n");
b40c2e66
TR
400 break;
401 }
402
403 case Opt_nodiscard:
404 *flag &= ~JFS_DISCARD;
405 break;
406
407 case Opt_discard_minblk:
408 {
409 struct request_queue *q = bdev_get_queue(sb->s_bdev);
410 char *minblks_trim = args[0].from;
bb5e50aa 411 int rc;
b40c2e66
TR
412 if (blk_queue_discard(q)) {
413 *flag |= JFS_DISCARD;
bb5e50aa
FF
414 rc = kstrtouint(minblks_trim, 0,
415 &sbi->minblks_trim);
416 if (rc)
417 goto cleanup;
418 } else
789602e9 419 pr_err("JFS: discard option not supported on device\n");
b40c2e66
TR
420 break;
421 }
422
1da177e4 423 default:
789602e9
FF
424 printk("jfs: Unrecognized mount option \"%s\" or missing value\n",
425 p);
1da177e4
LT
426 goto cleanup;
427 }
428 }
429
430 if (nls_map != (void *) -1) {
431 /* Discard old (if remount) */
6d729e44 432 unload_nls(sbi->nls_tab);
1da177e4
LT
433 sbi->nls_tab = nls_map;
434 }
435 return 1;
436
437cleanup:
438 if (nls_map && nls_map != (void *) -1)
439 unload_nls(nls_map);
440 return 0;
441}
442
443static int jfs_remount(struct super_block *sb, int *flags, char *data)
444{
445 s64 newLVSize = 0;
446 int rc = 0;
447 int flag = JFS_SBI(sb)->flag;
337eb00a 448 int ret;
1da177e4 449
02b9984d 450 sync_filesystem(sb);
789602e9 451 if (!parse_options(data, sb, &newLVSize, &flag))
1da177e4 452 return -EINVAL;
22b26db6 453
1da177e4 454 if (newLVSize) {
bc98a42c 455 if (sb_rdonly(sb)) {
789602e9 456 pr_err("JFS: resize requires volume to be mounted read-write\n");
1da177e4
LT
457 return -EROFS;
458 }
459 rc = jfs_extendfs(sb, newLVSize, 0);
22b26db6 460 if (rc)
1da177e4
LT
461 return rc;
462 }
463
1751e8a6 464 if (sb_rdonly(sb) && !(*flags & SB_RDONLY)) {
7fab479b
DK
465 /*
466 * Invalidate any previously read metadata. fsck may have
467 * changed the on-disk data since we mounted r/o
468 */
469 truncate_inode_pages(JFS_SBI(sb)->direct_inode->i_mapping, 0);
470
1da177e4 471 JFS_SBI(sb)->flag = flag;
337eb00a 472 ret = jfs_mount_rw(sb, 1);
c79d967d
CH
473
474 /* mark the fs r/w for quota activity */
1751e8a6 475 sb->s_flags &= ~SB_RDONLY;
c79d967d 476
0f0dd62f 477 dquot_resume(sb, -1);
337eb00a 478 return ret;
1da177e4 479 }
1751e8a6 480 if (!sb_rdonly(sb) && (*flags & SB_RDONLY)) {
0f0dd62f 481 rc = dquot_suspend(sb, -1);
789602e9 482 if (rc < 0)
0f0dd62f 483 return rc;
1da177e4
LT
484 rc = jfs_umount_rw(sb);
485 JFS_SBI(sb)->flag = flag;
486 return rc;
487 }
488 if ((JFS_SBI(sb)->flag & JFS_NOINTEGRITY) != (flag & JFS_NOINTEGRITY))
bc98a42c 489 if (!sb_rdonly(sb)) {
1da177e4 490 rc = jfs_umount_rw(sb);
22b26db6 491 if (rc)
1da177e4 492 return rc;
22b26db6 493
1da177e4 494 JFS_SBI(sb)->flag = flag;
337eb00a 495 ret = jfs_mount_rw(sb, 1);
337eb00a 496 return ret;
1da177e4
LT
497 }
498 JFS_SBI(sb)->flag = flag;
499
500 return 0;
501}
502
503static int jfs_fill_super(struct super_block *sb, void *data, int silent)
504{
505 struct jfs_sb_info *sbi;
506 struct inode *inode;
507 int rc;
508 s64 newLVSize = 0;
eab1df71 509 int flag, ret = -EINVAL;
1da177e4
LT
510
511 jfs_info("In jfs_read_super: s_flags=0x%lx", sb->s_flags);
512
789602e9 513 sbi = kzalloc(sizeof(struct jfs_sb_info), GFP_KERNEL);
22b26db6 514 if (!sbi)
087387f9 515 return -ENOMEM;
22b26db6 516
1da177e4 517 sb->s_fs_info = sbi;
8de52778 518 sb->s_max_links = JFS_LINK_MAX;
1da177e4 519 sbi->sb = sb;
c18cdc1a
EB
520 sbi->uid = INVALID_UID;
521 sbi->gid = INVALID_GID;
522 sbi->umask = -1;
1da177e4
LT
523
524 /* initialize the mount flag and determine the default error handler */
525 flag = JFS_ERR_REMOUNT_RO;
526
684bdc7f
JB
527 if (!parse_options((char *) data, sb, &newLVSize, &flag))
528 goto out_kfree;
1da177e4
LT
529 sbi->flag = flag;
530
531#ifdef CONFIG_JFS_POSIX_ACL
1751e8a6 532 sb->s_flags |= SB_POSIXACL;
1da177e4
LT
533#endif
534
535 if (newLVSize) {
b40c2e66 536 pr_err("resize option for remount only\n");
684bdc7f 537 goto out_kfree;
1da177e4
LT
538 }
539
540 /*
541 * Initialize blocksize to 4K.
542 */
543 sb_set_blocksize(sb, PSIZE);
544
545 /*
546 * Set method vectors.
547 */
548 sb->s_op = &jfs_super_operations;
549 sb->s_export_op = &jfs_export_operations;
2cc6a5a0 550 sb->s_xattr = jfs_xattr_handlers;
123e9caf
CH
551#ifdef CONFIG_QUOTA
552 sb->dq_op = &dquot_operations;
12fd086d 553 sb->s_qcop = &jfs_quotactl_ops;
507e1fa6 554 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
123e9caf 555#endif
1da177e4 556
7fab479b
DK
557 /*
558 * Initialize direct-mapping inode/address-space
559 */
560 inode = new_inode(sb);
eab1df71
DH
561 if (inode == NULL) {
562 ret = -ENOMEM;
684bdc7f 563 goto out_unload;
eab1df71 564 }
7fab479b 565 inode->i_ino = 0;
684666e5 566 inode->i_size = i_size_read(sb->s_bdev->bd_inode);
7fab479b 567 inode->i_mapping->a_ops = &jfs_metapage_aops;
5bef9151 568 inode_fake_hash(inode);
7fab479b
DK
569 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
570
571 sbi->direct_inode = inode;
572
1da177e4
LT
573 rc = jfs_mount(sb);
574 if (rc) {
789602e9 575 if (!silent)
1da177e4 576 jfs_err("jfs_mount failed w/return code = %d", rc);
7fab479b 577 goto out_mount_failed;
1da177e4 578 }
bc98a42c 579 if (sb_rdonly(sb))
1da177e4
LT
580 sbi->log = NULL;
581 else {
582 rc = jfs_mount_rw(sb, 0);
583 if (rc) {
584 if (!silent) {
585 jfs_err("jfs_mount_rw failed, return code = %d",
586 rc);
587 }
588 goto out_no_rw;
589 }
590 }
591
592 sb->s_magic = JFS_SUPER_MAGIC;
593
94b77bd8
AV
594 if (sbi->mntflag & JFS_OS2)
595 sb->s_d_op = &jfs_ci_dentry_operations;
596
eab1df71
DH
597 inode = jfs_iget(sb, ROOT_I);
598 if (IS_ERR(inode)) {
599 ret = PTR_ERR(inode);
6536d289 600 goto out_no_rw;
eab1df71 601 }
48fde701 602 sb->s_root = d_make_root(inode);
1da177e4
LT
603 if (!sb->s_root)
604 goto out_no_root;
605
c227390c
DK
606 /* logical blocks are represented by 40 bits in pxd_t, etc.
607 * and page cache is indexed by long
1da177e4 608 */
c227390c 609 sb->s_maxbytes = min(((loff_t)sb->s_blocksize) << 40, MAX_LFS_FILESIZE);
1da177e4
LT
610 sb->s_time_gran = 1;
611 return 0;
612
613out_no_root:
6536d289 614 jfs_err("jfs_read_super: get root dentry failed");
1da177e4
LT
615
616out_no_rw:
617 rc = jfs_umount(sb);
789602e9 618 if (rc)
1da177e4 619 jfs_err("jfs_umount failed with return code %d", rc);
7fab479b 620out_mount_failed:
28fd1298 621 filemap_write_and_wait(sbi->direct_inode->i_mapping);
7fab479b
DK
622 truncate_inode_pages(sbi->direct_inode->i_mapping, 0);
623 make_bad_inode(sbi->direct_inode);
624 iput(sbi->direct_inode);
625 sbi->direct_inode = NULL;
684bdc7f 626out_unload:
648695c7 627 unload_nls(sbi->nls_tab);
684bdc7f 628out_kfree:
1da177e4 629 kfree(sbi);
eab1df71 630 return ret;
1da177e4
LT
631}
632
c4be0c1d 633static int jfs_freeze(struct super_block *sb)
1da177e4
LT
634{
635 struct jfs_sb_info *sbi = JFS_SBI(sb);
636 struct jfs_log *log = sbi->log;
e9b37667 637 int rc = 0;
1da177e4 638
bc98a42c 639 if (!sb_rdonly(sb)) {
1da177e4 640 txQuiesce(sb);
e9b37667
VM
641 rc = lmLogShutdown(log);
642 if (rc) {
eb8630d7 643 jfs_error(sb, "lmLogShutdown failed\n");
e9b37667
VM
644
645 /* let operations fail rather than hang */
646 txResume(sb);
647
648 return rc;
649 }
650 rc = updateSuper(sb, FM_CLEAN);
651 if (rc) {
b18db6de 652 jfs_err("jfs_freeze: updateSuper failed");
e9b37667
VM
653 /*
654 * Don't fail here. Everything succeeded except
655 * marking the superblock clean, so there's really
656 * no harm in leaving it frozen for now.
657 */
658 }
1da177e4 659 }
c4be0c1d 660 return 0;
1da177e4
LT
661}
662
c4be0c1d 663static int jfs_unfreeze(struct super_block *sb)
1da177e4
LT
664{
665 struct jfs_sb_info *sbi = JFS_SBI(sb);
666 struct jfs_log *log = sbi->log;
667 int rc = 0;
668
bc98a42c 669 if (!sb_rdonly(sb)) {
e9b37667
VM
670 rc = updateSuper(sb, FM_MOUNT);
671 if (rc) {
eb8630d7 672 jfs_error(sb, "updateSuper failed\n");
e9b37667
VM
673 goto out;
674 }
675 rc = lmLogInit(log);
676 if (rc)
eb8630d7 677 jfs_error(sb, "lmLogInit failed\n");
e9b37667
VM
678out:
679 txResume(sb);
1da177e4 680 }
e9b37667 681 return rc;
1da177e4
LT
682}
683
152a0836
AV
684static struct dentry *jfs_do_mount(struct file_system_type *fs_type,
685 int flags, const char *dev_name, void *data)
1da177e4 686{
152a0836 687 return mount_bdev(fs_type, flags, dev_name, data, jfs_fill_super);
1da177e4
LT
688}
689
690static int jfs_sync_fs(struct super_block *sb, int wait)
691{
692 struct jfs_log *log = JFS_SBI(sb)->log;
693
694 /* log == NULL indicates read-only mount */
1c627829 695 if (log) {
a1177825
JK
696 /*
697 * Write quota structures to quota file, sync_blockdev() will
698 * write them to disk later
699 */
700 dquot_writeback_dquots(sb, -1);
1da177e4 701 jfs_flush_journal(log, wait);
cbc3d65e 702 jfs_syncpt(log, 0);
1c627829 703 }
1da177e4
LT
704
705 return 0;
706}
707
34c80b1d 708static int jfs_show_options(struct seq_file *seq, struct dentry *root)
8fc2751b 709{
34c80b1d 710 struct jfs_sb_info *sbi = JFS_SBI(root->d_sb);
8fc2751b 711
c18cdc1a
EB
712 if (uid_valid(sbi->uid))
713 seq_printf(seq, ",uid=%d", from_kuid(&init_user_ns, sbi->uid));
714 if (gid_valid(sbi->gid))
715 seq_printf(seq, ",gid=%d", from_kgid(&init_user_ns, sbi->gid));
69eb66d7
DK
716 if (sbi->umask != -1)
717 seq_printf(seq, ",umask=%03o", sbi->umask);
8fc2751b
MB
718 if (sbi->flag & JFS_NOINTEGRITY)
719 seq_puts(seq, ",nointegrity");
b40c2e66
TR
720 if (sbi->flag & JFS_DISCARD)
721 seq_printf(seq, ",discard=%u", sbi->minblks_trim);
5c5e32ce
MS
722 if (sbi->nls_tab)
723 seq_printf(seq, ",iocharset=%s", sbi->nls_tab->charset);
724 if (sbi->flag & JFS_ERR_CONTINUE)
725 seq_printf(seq, ",errors=continue");
726 if (sbi->flag & JFS_ERR_PANIC)
727 seq_printf(seq, ",errors=panic");
8fc2751b 728
115ff50b 729#ifdef CONFIG_QUOTA
8fc2751b
MB
730 if (sbi->flag & JFS_USRQUOTA)
731 seq_puts(seq, ",usrquota");
732
733 if (sbi->flag & JFS_GRPQUOTA)
734 seq_puts(seq, ",grpquota");
735#endif
736
737 return 0;
738}
739
115ff50b
DK
740#ifdef CONFIG_QUOTA
741
742/* Read data from quotafile - avoid pagecache and such because we cannot afford
743 * acquiring the locks... As quota files are never truncated and quota code
25985edc 744 * itself serializes the operations (and no one else should touch the files)
115ff50b
DK
745 * we don't have to be afraid of races */
746static ssize_t jfs_quota_read(struct super_block *sb, int type, char *data,
747 size_t len, loff_t off)
748{
749 struct inode *inode = sb_dqopt(sb)->files[type];
750 sector_t blk = off >> sb->s_blocksize_bits;
751 int err = 0;
752 int offset = off & (sb->s_blocksize - 1);
753 int tocopy;
754 size_t toread;
755 struct buffer_head tmp_bh;
756 struct buffer_head *bh;
757 loff_t i_size = i_size_read(inode);
758
759 if (off > i_size)
760 return 0;
761 if (off+len > i_size)
762 len = i_size-off;
763 toread = len;
764 while (toread > 0) {
765 tocopy = sb->s_blocksize - offset < toread ?
766 sb->s_blocksize - offset : toread;
767
768 tmp_bh.b_state = 0;
93407472 769 tmp_bh.b_size = i_blocksize(inode);
115ff50b
DK
770 err = jfs_get_block(inode, blk, &tmp_bh, 0);
771 if (err)
772 return err;
773 if (!buffer_mapped(&tmp_bh)) /* A hole? */
774 memset(data, 0, tocopy);
775 else {
776 bh = sb_bread(sb, tmp_bh.b_blocknr);
777 if (!bh)
778 return -EIO;
779 memcpy(data, bh->b_data+offset, tocopy);
780 brelse(bh);
781 }
782 offset = 0;
783 toread -= tocopy;
784 data += tocopy;
785 blk++;
786 }
787 return len;
788}
789
790/* Write to quotafile */
791static ssize_t jfs_quota_write(struct super_block *sb, int type,
792 const char *data, size_t len, loff_t off)
793{
794 struct inode *inode = sb_dqopt(sb)->files[type];
795 sector_t blk = off >> sb->s_blocksize_bits;
796 int err = 0;
797 int offset = off & (sb->s_blocksize - 1);
798 int tocopy;
799 size_t towrite = len;
800 struct buffer_head tmp_bh;
801 struct buffer_head *bh;
802
5955102c 803 inode_lock(inode);
115ff50b
DK
804 while (towrite > 0) {
805 tocopy = sb->s_blocksize - offset < towrite ?
806 sb->s_blocksize - offset : towrite;
807
808 tmp_bh.b_state = 0;
93407472 809 tmp_bh.b_size = i_blocksize(inode);
115ff50b
DK
810 err = jfs_get_block(inode, blk, &tmp_bh, 1);
811 if (err)
812 goto out;
813 if (offset || tocopy != sb->s_blocksize)
814 bh = sb_bread(sb, tmp_bh.b_blocknr);
815 else
816 bh = sb_getblk(sb, tmp_bh.b_blocknr);
817 if (!bh) {
818 err = -EIO;
819 goto out;
820 }
821 lock_buffer(bh);
822 memcpy(bh->b_data+offset, data, tocopy);
823 flush_dcache_page(bh->b_page);
824 set_buffer_uptodate(bh);
825 mark_buffer_dirty(bh);
826 unlock_buffer(bh);
827 brelse(bh);
828 offset = 0;
829 towrite -= tocopy;
830 data += tocopy;
831 blk++;
832 }
833out:
9c83633a 834 if (len == towrite) {
5955102c 835 inode_unlock(inode);
115ff50b 836 return err;
9c83633a 837 }
115ff50b
DK
838 if (inode->i_size < off+len-towrite)
839 i_size_write(inode, off+len-towrite);
078cd827 840 inode->i_mtime = inode->i_ctime = current_time(inode);
115ff50b 841 mark_inode_dirty(inode);
5955102c 842 inode_unlock(inode);
115ff50b
DK
843 return len - towrite;
844}
845
507e1fa6
JK
846static struct dquot **jfs_get_dquots(struct inode *inode)
847{
848 return JFS_IP(inode)->i_dquot;
849}
12fd086d
JK
850
851static int jfs_quota_on(struct super_block *sb, int type, int format_id,
852 const struct path *path)
853{
854 int err;
855 struct inode *inode;
856
857 err = dquot_quota_on(sb, type, format_id, path);
858 if (err)
859 return err;
860
861 inode = d_inode(path->dentry);
862 inode_lock(inode);
863 JFS_IP(inode)->mode2 |= JFS_NOATIME_FL | JFS_IMMUTABLE_FL;
864 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
865 S_NOATIME | S_IMMUTABLE);
866 inode_unlock(inode);
867 mark_inode_dirty(inode);
868
869 return 0;
870}
871
872static int jfs_quota_off(struct super_block *sb, int type)
873{
874 struct inode *inode = sb_dqopt(sb)->files[type];
875 int err;
876
877 if (!inode || !igrab(inode))
878 goto out;
879
880 err = dquot_quota_off(sb, type);
881 if (err)
882 goto out_put;
883
884 inode_lock(inode);
885 JFS_IP(inode)->mode2 &= ~(JFS_NOATIME_FL | JFS_IMMUTABLE_FL);
886 inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
887 inode_unlock(inode);
888 mark_inode_dirty(inode);
889out_put:
890 iput(inode);
891 return err;
892out:
893 return dquot_quota_off(sb, type);
894}
115ff50b
DK
895#endif
896
ee9b6d61 897static const struct super_operations jfs_super_operations = {
1da177e4 898 .alloc_inode = jfs_alloc_inode,
b3b4a6e3 899 .free_inode = jfs_free_inode,
1da177e4
LT
900 .dirty_inode = jfs_dirty_inode,
901 .write_inode = jfs_write_inode,
62aff86f 902 .evict_inode = jfs_evict_inode,
1da177e4
LT
903 .put_super = jfs_put_super,
904 .sync_fs = jfs_sync_fs,
c4be0c1d
TS
905 .freeze_fs = jfs_freeze,
906 .unfreeze_fs = jfs_unfreeze,
1da177e4
LT
907 .statfs = jfs_statfs,
908 .remount_fs = jfs_remount,
115ff50b
DK
909 .show_options = jfs_show_options,
910#ifdef CONFIG_QUOTA
911 .quota_read = jfs_quota_read,
912 .quota_write = jfs_quota_write,
507e1fa6 913 .get_dquots = jfs_get_dquots,
115ff50b 914#endif
1da177e4
LT
915};
916
39655164 917static const struct export_operations jfs_export_operations = {
d425de70
CH
918 .fh_to_dentry = jfs_fh_to_dentry,
919 .fh_to_parent = jfs_fh_to_parent,
1da177e4
LT
920 .get_parent = jfs_get_parent,
921};
922
923static struct file_system_type jfs_fs_type = {
924 .owner = THIS_MODULE,
925 .name = "jfs",
152a0836 926 .mount = jfs_do_mount,
1da177e4
LT
927 .kill_sb = kill_block_super,
928 .fs_flags = FS_REQUIRES_DEV,
929};
7f78e035 930MODULE_ALIAS_FS("jfs");
1da177e4 931
51cc5068 932static void init_once(void *foo)
1da177e4
LT
933{
934 struct jfs_inode_info *jfs_ip = (struct jfs_inode_info *) foo;
935
a35afb83
CL
936 memset(jfs_ip, 0, sizeof(struct jfs_inode_info));
937 INIT_LIST_HEAD(&jfs_ip->anon_inode_list);
938 init_rwsem(&jfs_ip->rdwrlock);
939 mutex_init(&jfs_ip->commit_mutex);
940 init_rwsem(&jfs_ip->xattr_sem);
941 spin_lock_init(&jfs_ip->ag_lock);
942 jfs_ip->active_ag = -1;
a35afb83 943 inode_init_once(&jfs_ip->vfs_inode);
1da177e4
LT
944}
945
946static int __init init_jfs_fs(void)
947{
948 int i;
949 int rc;
950
951 jfs_inode_cachep =
8d2704d3
DW
952 kmem_cache_create_usercopy("jfs_ip", sizeof(struct jfs_inode_info),
953 0, SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|SLAB_ACCOUNT,
961b33c2 954 offsetof(struct jfs_inode_info, i_inline), IDATASIZE,
8d2704d3 955 init_once);
1da177e4
LT
956 if (jfs_inode_cachep == NULL)
957 return -ENOMEM;
958
959 /*
960 * Metapage initialization
961 */
962 rc = metapage_init();
963 if (rc) {
964 jfs_err("metapage_init failed w/rc = %d", rc);
965 goto free_slab;
966 }
967
968 /*
969 * Transaction Manager initialization
970 */
971 rc = txInit();
972 if (rc) {
973 jfs_err("txInit failed w/rc = %d", rc);
974 goto free_metapage;
975 }
976
977 /*
978 * I/O completion thread (endio)
979 */
91dbb4de
CH
980 jfsIOthread = kthread_run(jfsIOWait, NULL, "jfsIO");
981 if (IS_ERR(jfsIOthread)) {
982 rc = PTR_ERR(jfsIOthread);
983 jfs_err("init_jfs_fs: fork failed w/rc = %d", rc);
1da177e4
LT
984 goto end_txmngr;
985 }
1da177e4
LT
986
987 if (commit_threads < 1)
988 commit_threads = num_online_cpus();
989 if (commit_threads > MAX_COMMIT_THREADS)
990 commit_threads = MAX_COMMIT_THREADS;
991
992 for (i = 0; i < commit_threads; i++) {
789602e9
FF
993 jfsCommitThread[i] = kthread_run(jfs_lazycommit, NULL,
994 "jfsCommit");
91dbb4de
CH
995 if (IS_ERR(jfsCommitThread[i])) {
996 rc = PTR_ERR(jfsCommitThread[i]);
997 jfs_err("init_jfs_fs: fork failed w/rc = %d", rc);
1da177e4
LT
998 commit_threads = i;
999 goto kill_committask;
1000 }
1da177e4
LT
1001 }
1002
91dbb4de
CH
1003 jfsSyncThread = kthread_run(jfs_sync, NULL, "jfsSync");
1004 if (IS_ERR(jfsSyncThread)) {
1005 rc = PTR_ERR(jfsSyncThread);
1006 jfs_err("init_jfs_fs: fork failed w/rc = %d", rc);
1da177e4
LT
1007 goto kill_committask;
1008 }
1da177e4
LT
1009
1010#ifdef PROC_FS_JFS
1011 jfs_proc_init();
1012#endif
1013
76bf09fc
AV
1014 rc = register_filesystem(&jfs_fs_type);
1015 if (!rc)
1016 return 0;
1da177e4 1017
76bf09fc
AV
1018#ifdef PROC_FS_JFS
1019 jfs_proc_clean();
1020#endif
1021 kthread_stop(jfsSyncThread);
1da177e4 1022kill_committask:
1da177e4 1023 for (i = 0; i < commit_threads; i++)
91dbb4de
CH
1024 kthread_stop(jfsCommitThread[i]);
1025 kthread_stop(jfsIOthread);
1da177e4
LT
1026end_txmngr:
1027 txExit();
1028free_metapage:
1029 metapage_exit();
1030free_slab:
1031 kmem_cache_destroy(jfs_inode_cachep);
1032 return rc;
1033}
1034
1035static void __exit exit_jfs_fs(void)
1036{
1037 int i;
1038
1039 jfs_info("exit_jfs_fs called");
1040
1da177e4
LT
1041 txExit();
1042 metapage_exit();
91dbb4de
CH
1043
1044 kthread_stop(jfsIOthread);
1da177e4 1045 for (i = 0; i < commit_threads; i++)
91dbb4de
CH
1046 kthread_stop(jfsCommitThread[i]);
1047 kthread_stop(jfsSyncThread);
1da177e4
LT
1048#ifdef PROC_FS_JFS
1049 jfs_proc_clean();
1050#endif
1051 unregister_filesystem(&jfs_fs_type);
8c0a8537
KS
1052
1053 /*
1054 * Make sure all delayed rcu free inodes are flushed before we
1055 * destroy cache.
1056 */
1057 rcu_barrier();
1da177e4
LT
1058 kmem_cache_destroy(jfs_inode_cachep);
1059}
1060
1061module_init(init_jfs_fs)
1062module_exit(exit_jfs_fs)