Merge tag 'drm-fixes-2019-05-24-1' of git://anongit.freedesktop.org/drm/drm
[linux-2.6-microblaze.git] / fs / jfs / super.c
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
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/fs.h>
21 #include <linux/module.h>
22 #include <linux/parser.h>
23 #include <linux/completion.h>
24 #include <linux/vfs.h>
25 #include <linux/quotaops.h>
26 #include <linux/mount.h>
27 #include <linux/moduleparam.h>
28 #include <linux/kthread.h>
29 #include <linux/posix_acl.h>
30 #include <linux/buffer_head.h>
31 #include <linux/exportfs.h>
32 #include <linux/crc32.h>
33 #include <linux/slab.h>
34 #include <linux/uaccess.h>
35 #include <linux/seq_file.h>
36 #include <linux/blkdev.h>
37
38 #include "jfs_incore.h"
39 #include "jfs_filsys.h"
40 #include "jfs_inode.h"
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"
47 #include "jfs_xattr.h"
48 #include "jfs_dinode.h"
49
50 MODULE_DESCRIPTION("The Journaled Filesystem (JFS)");
51 MODULE_AUTHOR("Steve Best/Dave Kleikamp/Barry Arndt, IBM");
52 MODULE_LICENSE("GPL");
53
54 static struct kmem_cache *jfs_inode_cachep;
55
56 static const struct super_operations jfs_super_operations;
57 static const struct export_operations jfs_export_operations;
58 static struct file_system_type jfs_fs_type;
59
60 #define MAX_COMMIT_THREADS 64
61 static int commit_threads;
62 module_param(commit_threads, int, 0);
63 MODULE_PARM_DESC(commit_threads, "Number of commit threads");
64
65 static struct task_struct *jfsCommitThread[MAX_COMMIT_THREADS];
66 struct task_struct *jfsIOthread;
67 struct task_struct *jfsSyncThread;
68
69 #ifdef CONFIG_JFS_DEBUG
70 int jfsloglevel = JFS_LOGLEVEL_WARN;
71 module_param(jfsloglevel, int, 0644);
72 MODULE_PARM_DESC(jfsloglevel, "Specify JFS loglevel (0, 1 or 2)");
73 #endif
74
75 static void jfs_handle_error(struct super_block *sb)
76 {
77         struct jfs_sb_info *sbi = JFS_SBI(sb);
78
79         if (sb_rdonly(sb))
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) {
88                 jfs_err("ERROR: (device %s): remounting filesystem as read-only",
89                         sb->s_id);
90                 sb->s_flags |= SB_RDONLY;
91         }
92
93         /* nothing is done for continue beyond marking the superblock dirty */
94 }
95
96 void jfs_error(struct super_block *sb, const char *fmt, ...)
97 {
98         struct va_format vaf;
99         va_list args;
100
101         va_start(args, fmt);
102
103         vaf.fmt = fmt;
104         vaf.va = &args;
105
106         pr_err("ERROR: (device %s): %ps: %pV\n",
107                sb->s_id, __builtin_return_address(0), &vaf);
108
109         va_end(args);
110
111         jfs_handle_error(sb);
112 }
113
114 static 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;
121 #ifdef CONFIG_QUOTA
122         memset(&jfs_inode->i_dquot, 0, sizeof(jfs_inode->i_dquot));
123 #endif
124         return &jfs_inode->vfs_inode;
125 }
126
127 static void jfs_free_inode(struct inode *inode)
128 {
129         kmem_cache_free(jfs_inode_cachep, JFS_IP(inode));
130 }
131
132 static int jfs_statfs(struct dentry *dentry, struct kstatfs *buf)
133 {
134         struct jfs_sb_info *sbi = JFS_SBI(dentry->d_sb);
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.
147          * We'll try to calculate some guess as to how many inodes we can
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));
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);
164
165         buf->f_namelen = JFS_NAME_MAX;
166         return 0;
167 }
168
169 #ifdef CONFIG_QUOTA
170 static int jfs_quota_off(struct super_block *sb, int type);
171 static int jfs_quota_on(struct super_block *sb, int type, int format_id,
172                         const struct path *path);
173
174 static 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
182 static 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
193 static inline void jfs_quota_off_umount(struct super_block *sb)
194 {
195 }
196 #endif
197
198 static 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");
204
205         jfs_quota_off_umount(sb);
206
207         rc = jfs_umount(sb);
208         if (rc)
209                 jfs_err("jfs_umount failed with return code %d", rc);
210
211         unload_nls(sbi->nls_tab);
212
213         truncate_inode_pages(sbi->direct_inode->i_mapping, 0);
214         iput(sbi->direct_inode);
215
216         kfree(sbi);
217 }
218
219 enum {
220         Opt_integrity, Opt_nointegrity, Opt_iocharset, Opt_resize,
221         Opt_resize_nosize, Opt_errors, Opt_ignore, Opt_err, Opt_quota,
222         Opt_usrquota, Opt_grpquota, Opt_uid, Opt_gid, Opt_umask,
223         Opt_discard, Opt_nodiscard, Opt_discard_minblk
224 };
225
226 static const match_table_t tokens = {
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"},
234         {Opt_quota, "quota"},
235         {Opt_usrquota, "usrquota"},
236         {Opt_grpquota, "grpquota"},
237         {Opt_uid, "uid=%u"},
238         {Opt_gid, "gid=%u"},
239         {Opt_umask, "umask=%u"},
240         {Opt_discard, "discard"},
241         {Opt_nodiscard, "nodiscard"},
242         {Opt_discard_minblk, "discard=%u"},
243         {Opt_err, NULL}
244 };
245
246 static 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) {
284                                         pr_err("JFS: charset not found\n");
285                                         goto cleanup;
286                                 }
287                         }
288                         break;
289                 case Opt_resize:
290                 {
291                         char *resize = args[0].from;
292                         int rc = kstrtoll(resize, 0, newLVSize);
293
294                         if (rc)
295                                 goto cleanup;
296                         break;
297                 }
298                 case Opt_resize_nosize:
299                 {
300                         *newLVSize = i_size_read(sb->s_bdev->bd_inode) >>
301                                 sb->s_blocksize_bits;
302                         if (*newLVSize == 0)
303                                 pr_err("JFS: Cannot determine volume size\n");
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 {
324                                 pr_err("JFS: %s is an invalid error handler\n",
325                                        errors);
326                                 goto cleanup;
327                         }
328                         break;
329                 }
330
331 #ifdef CONFIG_QUOTA
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:
343                         pr_err("JFS: quota operations not supported\n");
344                         break;
345 #endif
346                 case Opt_uid:
347                 {
348                         char *uid = args[0].from;
349                         uid_t val;
350                         int rc = kstrtouint(uid, 0, &val);
351
352                         if (rc)
353                                 goto cleanup;
354                         sbi->uid = make_kuid(current_user_ns(), val);
355                         if (!uid_valid(sbi->uid))
356                                 goto cleanup;
357                         break;
358                 }
359
360                 case Opt_gid:
361                 {
362                         char *gid = args[0].from;
363                         gid_t val;
364                         int rc = kstrtouint(gid, 0, &val);
365
366                         if (rc)
367                                 goto cleanup;
368                         sbi->gid = make_kgid(current_user_ns(), val);
369                         if (!gid_valid(sbi->gid))
370                                 goto cleanup;
371                         break;
372                 }
373
374                 case Opt_umask:
375                 {
376                         char *umask = args[0].from;
377                         int rc = kstrtouint(umask, 8, &sbi->umask);
378
379                         if (rc)
380                                 goto cleanup;
381                         if (sbi->umask & ~0777) {
382                                 pr_err("JFS: Invalid value of umask\n");
383                                 goto cleanup;
384                         }
385                         break;
386                 }
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;
396                         if (blk_queue_discard(q))
397                                 *flag |= JFS_DISCARD;
398                         else
399                                 pr_err("JFS: discard option not supported on device\n");
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;
411                         int rc;
412                         if (blk_queue_discard(q)) {
413                                 *flag |= JFS_DISCARD;
414                                 rc = kstrtouint(minblks_trim, 0,
415                                                 &sbi->minblks_trim);
416                                 if (rc)
417                                         goto cleanup;
418                         } else
419                                 pr_err("JFS: discard option not supported on device\n");
420                         break;
421                 }
422
423                 default:
424                         printk("jfs: Unrecognized mount option \"%s\" or missing value\n",
425                                p);
426                         goto cleanup;
427                 }
428         }
429
430         if (nls_map != (void *) -1) {
431                 /* Discard old (if remount) */
432                 unload_nls(sbi->nls_tab);
433                 sbi->nls_tab = nls_map;
434         }
435         return 1;
436
437 cleanup:
438         if (nls_map && nls_map != (void *) -1)
439                 unload_nls(nls_map);
440         return 0;
441 }
442
443 static 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;
448         int ret;
449
450         sync_filesystem(sb);
451         if (!parse_options(data, sb, &newLVSize, &flag))
452                 return -EINVAL;
453
454         if (newLVSize) {
455                 if (sb_rdonly(sb)) {
456                         pr_err("JFS: resize requires volume to be mounted read-write\n");
457                         return -EROFS;
458                 }
459                 rc = jfs_extendfs(sb, newLVSize, 0);
460                 if (rc)
461                         return rc;
462         }
463
464         if (sb_rdonly(sb) && !(*flags & SB_RDONLY)) {
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
471                 JFS_SBI(sb)->flag = flag;
472                 ret = jfs_mount_rw(sb, 1);
473
474                 /* mark the fs r/w for quota activity */
475                 sb->s_flags &= ~SB_RDONLY;
476
477                 dquot_resume(sb, -1);
478                 return ret;
479         }
480         if (!sb_rdonly(sb) && (*flags & SB_RDONLY)) {
481                 rc = dquot_suspend(sb, -1);
482                 if (rc < 0)
483                         return rc;
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))
489                 if (!sb_rdonly(sb)) {
490                         rc = jfs_umount_rw(sb);
491                         if (rc)
492                                 return rc;
493
494                         JFS_SBI(sb)->flag = flag;
495                         ret = jfs_mount_rw(sb, 1);
496                         return ret;
497                 }
498         JFS_SBI(sb)->flag = flag;
499
500         return 0;
501 }
502
503 static 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;
509         int flag, ret = -EINVAL;
510
511         jfs_info("In jfs_read_super: s_flags=0x%lx", sb->s_flags);
512
513         sbi = kzalloc(sizeof(struct jfs_sb_info), GFP_KERNEL);
514         if (!sbi)
515                 return -ENOMEM;
516
517         sb->s_fs_info = sbi;
518         sb->s_max_links = JFS_LINK_MAX;
519         sbi->sb = sb;
520         sbi->uid = INVALID_UID;
521         sbi->gid = INVALID_GID;
522         sbi->umask = -1;
523
524         /* initialize the mount flag and determine the default error handler */
525         flag = JFS_ERR_REMOUNT_RO;
526
527         if (!parse_options((char *) data, sb, &newLVSize, &flag))
528                 goto out_kfree;
529         sbi->flag = flag;
530
531 #ifdef CONFIG_JFS_POSIX_ACL
532         sb->s_flags |= SB_POSIXACL;
533 #endif
534
535         if (newLVSize) {
536                 pr_err("resize option for remount only\n");
537                 goto out_kfree;
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;
550         sb->s_xattr = jfs_xattr_handlers;
551 #ifdef CONFIG_QUOTA
552         sb->dq_op = &dquot_operations;
553         sb->s_qcop = &jfs_quotactl_ops;
554         sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
555 #endif
556
557         /*
558          * Initialize direct-mapping inode/address-space
559          */
560         inode = new_inode(sb);
561         if (inode == NULL) {
562                 ret = -ENOMEM;
563                 goto out_unload;
564         }
565         inode->i_ino = 0;
566         inode->i_size = i_size_read(sb->s_bdev->bd_inode);
567         inode->i_mapping->a_ops = &jfs_metapage_aops;
568         inode_fake_hash(inode);
569         mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
570
571         sbi->direct_inode = inode;
572
573         rc = jfs_mount(sb);
574         if (rc) {
575                 if (!silent)
576                         jfs_err("jfs_mount failed w/return code = %d", rc);
577                 goto out_mount_failed;
578         }
579         if (sb_rdonly(sb))
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
594         if (sbi->mntflag & JFS_OS2)
595                 sb->s_d_op = &jfs_ci_dentry_operations;
596
597         inode = jfs_iget(sb, ROOT_I);
598         if (IS_ERR(inode)) {
599                 ret = PTR_ERR(inode);
600                 goto out_no_rw;
601         }
602         sb->s_root = d_make_root(inode);
603         if (!sb->s_root)
604                 goto out_no_root;
605
606         /* logical blocks are represented by 40 bits in pxd_t, etc.
607          * and page cache is indexed by long
608          */
609         sb->s_maxbytes = min(((loff_t)sb->s_blocksize) << 40, MAX_LFS_FILESIZE);
610         sb->s_time_gran = 1;
611         return 0;
612
613 out_no_root:
614         jfs_err("jfs_read_super: get root dentry failed");
615
616 out_no_rw:
617         rc = jfs_umount(sb);
618         if (rc)
619                 jfs_err("jfs_umount failed with return code %d", rc);
620 out_mount_failed:
621         filemap_write_and_wait(sbi->direct_inode->i_mapping);
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;
626 out_unload:
627         unload_nls(sbi->nls_tab);
628 out_kfree:
629         kfree(sbi);
630         return ret;
631 }
632
633 static int jfs_freeze(struct super_block *sb)
634 {
635         struct jfs_sb_info *sbi = JFS_SBI(sb);
636         struct jfs_log *log = sbi->log;
637         int rc = 0;
638
639         if (!sb_rdonly(sb)) {
640                 txQuiesce(sb);
641                 rc = lmLogShutdown(log);
642                 if (rc) {
643                         jfs_error(sb, "lmLogShutdown failed\n");
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) {
652                         jfs_err("jfs_freeze: updateSuper failed");
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                 }
659         }
660         return 0;
661 }
662
663 static int jfs_unfreeze(struct super_block *sb)
664 {
665         struct jfs_sb_info *sbi = JFS_SBI(sb);
666         struct jfs_log *log = sbi->log;
667         int rc = 0;
668
669         if (!sb_rdonly(sb)) {
670                 rc = updateSuper(sb, FM_MOUNT);
671                 if (rc) {
672                         jfs_error(sb, "updateSuper failed\n");
673                         goto out;
674                 }
675                 rc = lmLogInit(log);
676                 if (rc)
677                         jfs_error(sb, "lmLogInit failed\n");
678 out:
679                 txResume(sb);
680         }
681         return rc;
682 }
683
684 static struct dentry *jfs_do_mount(struct file_system_type *fs_type,
685         int flags, const char *dev_name, void *data)
686 {
687         return mount_bdev(fs_type, flags, dev_name, data, jfs_fill_super);
688 }
689
690 static 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 */
695         if (log) {
696                 /*
697                  * Write quota structures to quota file, sync_blockdev() will
698                  * write them to disk later
699                  */
700                 dquot_writeback_dquots(sb, -1);
701                 jfs_flush_journal(log, wait);
702                 jfs_syncpt(log, 0);
703         }
704
705         return 0;
706 }
707
708 static int jfs_show_options(struct seq_file *seq, struct dentry *root)
709 {
710         struct jfs_sb_info *sbi = JFS_SBI(root->d_sb);
711
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));
716         if (sbi->umask != -1)
717                 seq_printf(seq, ",umask=%03o", sbi->umask);
718         if (sbi->flag & JFS_NOINTEGRITY)
719                 seq_puts(seq, ",nointegrity");
720         if (sbi->flag & JFS_DISCARD)
721                 seq_printf(seq, ",discard=%u", sbi->minblks_trim);
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");
728
729 #ifdef CONFIG_QUOTA
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
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
744  * itself serializes the operations (and no one else should touch the files)
745  * we don't have to be afraid of races */
746 static 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;
769                 tmp_bh.b_size = i_blocksize(inode);
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 */
791 static 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
803         inode_lock(inode);
804         while (towrite > 0) {
805                 tocopy = sb->s_blocksize - offset < towrite ?
806                                 sb->s_blocksize - offset : towrite;
807
808                 tmp_bh.b_state = 0;
809                 tmp_bh.b_size = i_blocksize(inode);
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         }
833 out:
834         if (len == towrite) {
835                 inode_unlock(inode);
836                 return err;
837         }
838         if (inode->i_size < off+len-towrite)
839                 i_size_write(inode, off+len-towrite);
840         inode->i_mtime = inode->i_ctime = current_time(inode);
841         mark_inode_dirty(inode);
842         inode_unlock(inode);
843         return len - towrite;
844 }
845
846 static struct dquot **jfs_get_dquots(struct inode *inode)
847 {
848         return JFS_IP(inode)->i_dquot;
849 }
850
851 static 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
872 static 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);
889 out_put:
890         iput(inode);
891         return err;
892 out:
893         return dquot_quota_off(sb, type);
894 }
895 #endif
896
897 static const struct super_operations jfs_super_operations = {
898         .alloc_inode    = jfs_alloc_inode,
899         .free_inode     = jfs_free_inode,
900         .dirty_inode    = jfs_dirty_inode,
901         .write_inode    = jfs_write_inode,
902         .evict_inode    = jfs_evict_inode,
903         .put_super      = jfs_put_super,
904         .sync_fs        = jfs_sync_fs,
905         .freeze_fs      = jfs_freeze,
906         .unfreeze_fs    = jfs_unfreeze,
907         .statfs         = jfs_statfs,
908         .remount_fs     = jfs_remount,
909         .show_options   = jfs_show_options,
910 #ifdef CONFIG_QUOTA
911         .quota_read     = jfs_quota_read,
912         .quota_write    = jfs_quota_write,
913         .get_dquots     = jfs_get_dquots,
914 #endif
915 };
916
917 static const struct export_operations jfs_export_operations = {
918         .fh_to_dentry   = jfs_fh_to_dentry,
919         .fh_to_parent   = jfs_fh_to_parent,
920         .get_parent     = jfs_get_parent,
921 };
922
923 static struct file_system_type jfs_fs_type = {
924         .owner          = THIS_MODULE,
925         .name           = "jfs",
926         .mount          = jfs_do_mount,
927         .kill_sb        = kill_block_super,
928         .fs_flags       = FS_REQUIRES_DEV,
929 };
930 MODULE_ALIAS_FS("jfs");
931
932 static void init_once(void *foo)
933 {
934         struct jfs_inode_info *jfs_ip = (struct jfs_inode_info *) foo;
935
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;
943         inode_init_once(&jfs_ip->vfs_inode);
944 }
945
946 static int __init init_jfs_fs(void)
947 {
948         int i;
949         int rc;
950
951         jfs_inode_cachep =
952             kmem_cache_create_usercopy("jfs_ip", sizeof(struct jfs_inode_info),
953                         0, SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|SLAB_ACCOUNT,
954                         offsetof(struct jfs_inode_info, i_inline), IDATASIZE,
955                         init_once);
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          */
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);
984                 goto end_txmngr;
985         }
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++) {
993                 jfsCommitThread[i] = kthread_run(jfs_lazycommit, NULL,
994                                                  "jfsCommit");
995                 if (IS_ERR(jfsCommitThread[i])) {
996                         rc = PTR_ERR(jfsCommitThread[i]);
997                         jfs_err("init_jfs_fs: fork failed w/rc = %d", rc);
998                         commit_threads = i;
999                         goto kill_committask;
1000                 }
1001         }
1002
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);
1007                 goto kill_committask;
1008         }
1009
1010 #ifdef PROC_FS_JFS
1011         jfs_proc_init();
1012 #endif
1013
1014         rc = register_filesystem(&jfs_fs_type);
1015         if (!rc)
1016                 return 0;
1017
1018 #ifdef PROC_FS_JFS
1019         jfs_proc_clean();
1020 #endif
1021         kthread_stop(jfsSyncThread);
1022 kill_committask:
1023         for (i = 0; i < commit_threads; i++)
1024                 kthread_stop(jfsCommitThread[i]);
1025         kthread_stop(jfsIOthread);
1026 end_txmngr:
1027         txExit();
1028 free_metapage:
1029         metapage_exit();
1030 free_slab:
1031         kmem_cache_destroy(jfs_inode_cachep);
1032         return rc;
1033 }
1034
1035 static void __exit exit_jfs_fs(void)
1036 {
1037         int i;
1038
1039         jfs_info("exit_jfs_fs called");
1040
1041         txExit();
1042         metapage_exit();
1043
1044         kthread_stop(jfsIOthread);
1045         for (i = 0; i < commit_threads; i++)
1046                 kthread_stop(jfsCommitThread[i]);
1047         kthread_stop(jfsSyncThread);
1048 #ifdef PROC_FS_JFS
1049         jfs_proc_clean();
1050 #endif
1051         unregister_filesystem(&jfs_fs_type);
1052
1053         /*
1054          * Make sure all delayed rcu free inodes are flushed before we
1055          * destroy cache.
1056          */
1057         rcu_barrier();
1058         kmem_cache_destroy(jfs_inode_cachep);
1059 }
1060
1061 module_init(init_jfs_fs)
1062 module_exit(exit_jfs_fs)