blob: b6ba80e05bff3ddadd4e492f0096b57666d7f665 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ufs/super.c
3 *
4 * Copyright (C) 1998
5 * Daniel Pirkl <daniel.pirkl@email.cz>
6 * Charles University, Faculty of Mathematics and Physics
7 */
8
9/* Derived from
10 *
11 * linux/fs/ext2/super.c
12 *
13 * Copyright (C) 1992, 1993, 1994, 1995
14 * Remy Card (card@masi.ibp.fr)
15 * Laboratoire MASI - Institut Blaise Pascal
16 * Universite Pierre et Marie Curie (Paris VI)
17 *
18 * from
19 *
20 * linux/fs/minix/inode.c
21 *
22 * Copyright (C) 1991, 1992 Linus Torvalds
23 *
24 * Big-endian to little-endian byte-swapping/bitmaps by
25 * David S. Miller (davem@caip.rutgers.edu), 1995
26 */
27
28/*
29 * Inspired by
30 *
31 * linux/fs/ufs/super.c
32 *
33 * Copyright (C) 1996
34 * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu)
35 * Laboratory for Computer Science Research Computing Facility
36 * Rutgers, The State University of New Jersey
37 *
38 * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
39 *
40 * Kernel module support added on 96/04/26 by
41 * Stefan Reinauer <stepan@home.culture.mipt.ru>
42 *
43 * Module usage counts added on 96/04/29 by
Gertjan van Wingerdef762dd62009-03-21 23:18:57 +010044 * Gertjan van Wingerde <gwingerde@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 *
46 * Clean swab support on 19970406 by
47 * Francois-Rene Rideau <fare@tunes.org>
48 *
49 * 4.4BSD (FreeBSD) support added on February 1st 1998 by
50 * Niels Kristian Bech Jensen <nkbj@image.dk> partially based
51 * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>.
52 *
53 * NeXTstep support added on February 5th 1998 by
54 * Niels Kristian Bech Jensen <nkbj@image.dk>.
55 *
56 * write support Daniel Pirkl <daniel.pirkl@email.cz> 1998
57 *
58 * HP/UX hfs filesystem support added by
59 * Martin K. Petersen <mkp@mkp.net>, August 1999
60 *
61 * UFS2 (of FreeBSD 5.x) support added by
62 * Niraj Kumar <niraj17@iitbombay.org>, Jan 2004
63 *
Evgeniy Dushistovcbcae392007-02-12 00:54:30 -080064 * UFS2 write support added by
65 * Evgeniy Dushistov <dushistov@mail.ru>, 2007
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 */
67
Alexey Dobriyanf3e2a522009-12-15 16:46:51 -080068#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/module.h>
70#include <linux/bitops.h>
71
72#include <stdarg.h>
73
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080074#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76#include <linux/errno.h>
77#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/slab.h>
79#include <linux/time.h>
80#include <linux/stat.h>
81#include <linux/string.h>
82#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040083#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/init.h>
85#include <linux/parser.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include <linux/buffer_head.h>
87#include <linux/vfs.h>
vignesh babu9e8c4272007-07-15 23:41:36 -070088#include <linux/log2.h>
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -070089#include <linux/mount.h>
90#include <linux/seq_file.h>
Jeff Laytonbb8c2d62017-12-11 06:35:18 -050091#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Mike Frysingere5420592008-02-08 04:21:31 -080093#include "ufs_fs.h"
Christoph Hellwigbcd6d4e2007-10-16 23:26:51 -070094#include "ufs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include "swab.h"
96#include "util.h"
97
Alexey Dobriyanf3e2a522009-12-15 16:46:51 -080098static struct inode *ufs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation)
99{
100 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
101 struct inode *inode;
102
103 if (ino < UFS_ROOTINO || ino > uspi->s_ncg * uspi->s_ipg)
104 return ERR_PTR(-ESTALE);
105
106 inode = ufs_iget(sb, ino);
107 if (IS_ERR(inode))
108 return ERR_CAST(inode);
109 if (generation && inode->i_generation != generation) {
110 iput(inode);
111 return ERR_PTR(-ESTALE);
112 }
113 return inode;
114}
115
116static struct dentry *ufs_fh_to_dentry(struct super_block *sb, struct fid *fid,
117 int fh_len, int fh_type)
118{
119 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, ufs_nfs_get_inode);
120}
121
122static struct dentry *ufs_fh_to_parent(struct super_block *sb, struct fid *fid,
123 int fh_len, int fh_type)
124{
125 return generic_fh_to_parent(sb, fid, fh_len, fh_type, ufs_nfs_get_inode);
126}
127
128static struct dentry *ufs_get_parent(struct dentry *child)
129{
Linus Torvalds26fe5752012-05-10 13:14:12 -0700130 struct qstr dot_dot = QSTR_INIT("..", 2);
Alexey Dobriyanf3e2a522009-12-15 16:46:51 -0800131 ino_t ino;
132
David Howells2b0143b2015-03-17 22:25:59 +0000133 ino = ufs_inode_by_name(d_inode(child), &dot_dot);
Alexey Dobriyanf3e2a522009-12-15 16:46:51 -0800134 if (!ino)
135 return ERR_PTR(-ENOENT);
Al Virofc640052016-04-10 01:33:30 -0400136 return d_obtain_alias(ufs_iget(child->d_sb, ino));
Alexey Dobriyanf3e2a522009-12-15 16:46:51 -0800137}
138
139static const struct export_operations ufs_export_ops = {
140 .fh_to_dentry = ufs_fh_to_dentry,
141 .fh_to_parent = ufs_fh_to_parent,
142 .get_parent = ufs_get_parent,
143};
144
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700145#ifdef CONFIG_UFS_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*
147 * Print contents of ufs_super_block, useful for debugging
148 */
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800149static void ufs_print_super_stuff(struct super_block *sb,
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700150 struct ufs_super_block_first *usb1,
151 struct ufs_super_block_second *usb2,
152 struct ufs_super_block_third *usb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800154 u32 magic = fs32_to_cpu(sb, usb3->fs_magic);
155
Fabian Fredericka9814c52014-08-08 14:20:57 -0700156 pr_debug("ufs_print_super_stuff\n");
157 pr_debug(" magic: 0x%x\n", magic);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800158 if (fs32_to_cpu(sb, usb3->fs_magic) == UFS2_MAGIC) {
Fabian Fredericka9814c52014-08-08 14:20:57 -0700159 pr_debug(" fs_size: %llu\n", (unsigned long long)
160 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size));
161 pr_debug(" fs_dsize: %llu\n", (unsigned long long)
162 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize));
163 pr_debug(" bsize: %u\n",
164 fs32_to_cpu(sb, usb1->fs_bsize));
165 pr_debug(" fsize: %u\n",
166 fs32_to_cpu(sb, usb1->fs_fsize));
167 pr_debug(" fs_volname: %s\n", usb2->fs_un.fs_u2.fs_volname);
168 pr_debug(" fs_sblockloc: %llu\n", (unsigned long long)
169 fs64_to_cpu(sb, usb2->fs_un.fs_u2.fs_sblockloc));
170 pr_debug(" cs_ndir(No of dirs): %llu\n", (unsigned long long)
171 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir));
172 pr_debug(" cs_nbfree(No of free blocks): %llu\n",
173 (unsigned long long)
174 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree));
175 pr_info(" cs_nifree(Num of free inodes): %llu\n",
176 (unsigned long long)
177 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree));
178 pr_info(" cs_nffree(Num of free frags): %llu\n",
179 (unsigned long long)
180 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree));
181 pr_info(" fs_maxsymlinklen: %u\n",
182 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen));
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700183 } else {
Fabian Fredericka9814c52014-08-08 14:20:57 -0700184 pr_debug(" sblkno: %u\n", fs32_to_cpu(sb, usb1->fs_sblkno));
185 pr_debug(" cblkno: %u\n", fs32_to_cpu(sb, usb1->fs_cblkno));
186 pr_debug(" iblkno: %u\n", fs32_to_cpu(sb, usb1->fs_iblkno));
187 pr_debug(" dblkno: %u\n", fs32_to_cpu(sb, usb1->fs_dblkno));
188 pr_debug(" cgoffset: %u\n",
189 fs32_to_cpu(sb, usb1->fs_cgoffset));
190 pr_debug(" ~cgmask: 0x%x\n",
191 ~fs32_to_cpu(sb, usb1->fs_cgmask));
192 pr_debug(" size: %u\n", fs32_to_cpu(sb, usb1->fs_size));
193 pr_debug(" dsize: %u\n", fs32_to_cpu(sb, usb1->fs_dsize));
194 pr_debug(" ncg: %u\n", fs32_to_cpu(sb, usb1->fs_ncg));
195 pr_debug(" bsize: %u\n", fs32_to_cpu(sb, usb1->fs_bsize));
196 pr_debug(" fsize: %u\n", fs32_to_cpu(sb, usb1->fs_fsize));
197 pr_debug(" frag: %u\n", fs32_to_cpu(sb, usb1->fs_frag));
198 pr_debug(" fragshift: %u\n",
199 fs32_to_cpu(sb, usb1->fs_fragshift));
200 pr_debug(" ~fmask: %u\n", ~fs32_to_cpu(sb, usb1->fs_fmask));
201 pr_debug(" fshift: %u\n", fs32_to_cpu(sb, usb1->fs_fshift));
202 pr_debug(" sbsize: %u\n", fs32_to_cpu(sb, usb1->fs_sbsize));
203 pr_debug(" spc: %u\n", fs32_to_cpu(sb, usb1->fs_spc));
204 pr_debug(" cpg: %u\n", fs32_to_cpu(sb, usb1->fs_cpg));
205 pr_debug(" ipg: %u\n", fs32_to_cpu(sb, usb1->fs_ipg));
206 pr_debug(" fpg: %u\n", fs32_to_cpu(sb, usb1->fs_fpg));
207 pr_debug(" csaddr: %u\n", fs32_to_cpu(sb, usb1->fs_csaddr));
208 pr_debug(" cssize: %u\n", fs32_to_cpu(sb, usb1->fs_cssize));
209 pr_debug(" cgsize: %u\n", fs32_to_cpu(sb, usb1->fs_cgsize));
210 pr_debug(" fstodb: %u\n",
211 fs32_to_cpu(sb, usb1->fs_fsbtodb));
212 pr_debug(" nrpos: %u\n", fs32_to_cpu(sb, usb3->fs_nrpos));
213 pr_debug(" ndir %u\n",
214 fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir));
215 pr_debug(" nifree %u\n",
216 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree));
217 pr_debug(" nbfree %u\n",
218 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree));
219 pr_debug(" nffree %u\n",
220 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree));
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700221 }
Fabian Fredericka9814c52014-08-08 14:20:57 -0700222 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225/*
226 * Print contents of ufs_cylinder_group, useful for debugging
227 */
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700228static void ufs_print_cylinder_stuff(struct super_block *sb,
229 struct ufs_cylinder_group *cg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Fabian Fredericka9814c52014-08-08 14:20:57 -0700231 pr_debug("\nufs_print_cylinder_stuff\n");
232 pr_debug("size of ucg: %zu\n", sizeof(struct ufs_cylinder_group));
233 pr_debug(" magic: %x\n", fs32_to_cpu(sb, cg->cg_magic));
234 pr_debug(" time: %u\n", fs32_to_cpu(sb, cg->cg_time));
235 pr_debug(" cgx: %u\n", fs32_to_cpu(sb, cg->cg_cgx));
236 pr_debug(" ncyl: %u\n", fs16_to_cpu(sb, cg->cg_ncyl));
237 pr_debug(" niblk: %u\n", fs16_to_cpu(sb, cg->cg_niblk));
238 pr_debug(" ndblk: %u\n", fs32_to_cpu(sb, cg->cg_ndblk));
239 pr_debug(" cs_ndir: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_ndir));
240 pr_debug(" cs_nbfree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nbfree));
241 pr_debug(" cs_nifree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nifree));
242 pr_debug(" cs_nffree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nffree));
243 pr_debug(" rotor: %u\n", fs32_to_cpu(sb, cg->cg_rotor));
244 pr_debug(" frotor: %u\n", fs32_to_cpu(sb, cg->cg_frotor));
245 pr_debug(" irotor: %u\n", fs32_to_cpu(sb, cg->cg_irotor));
246 pr_debug(" frsum: %u, %u, %u, %u, %u, %u, %u, %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 fs32_to_cpu(sb, cg->cg_frsum[0]), fs32_to_cpu(sb, cg->cg_frsum[1]),
248 fs32_to_cpu(sb, cg->cg_frsum[2]), fs32_to_cpu(sb, cg->cg_frsum[3]),
249 fs32_to_cpu(sb, cg->cg_frsum[4]), fs32_to_cpu(sb, cg->cg_frsum[5]),
250 fs32_to_cpu(sb, cg->cg_frsum[6]), fs32_to_cpu(sb, cg->cg_frsum[7]));
Fabian Fredericka9814c52014-08-08 14:20:57 -0700251 pr_debug(" btotoff: %u\n", fs32_to_cpu(sb, cg->cg_btotoff));
252 pr_debug(" boff: %u\n", fs32_to_cpu(sb, cg->cg_boff));
253 pr_debug(" iuseoff: %u\n", fs32_to_cpu(sb, cg->cg_iusedoff));
254 pr_debug(" freeoff: %u\n", fs32_to_cpu(sb, cg->cg_freeoff));
255 pr_debug(" nextfreeoff: %u\n", fs32_to_cpu(sb, cg->cg_nextfreeoff));
256 pr_debug(" clustersumoff %u\n",
257 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clustersumoff));
258 pr_debug(" clusteroff %u\n",
259 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clusteroff));
260 pr_debug(" nclusterblks %u\n",
261 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_nclusterblks));
262 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700264#else
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800265# define ufs_print_super_stuff(sb, usb1, usb2, usb3) /**/
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700266# define ufs_print_cylinder_stuff(sb, cg) /**/
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700267#endif /* CONFIG_UFS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800269static const struct super_operations ufs_super_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271void ufs_error (struct super_block * sb, const char * function,
272 const char * fmt, ...)
273{
274 struct ufs_sb_private_info * uspi;
275 struct ufs_super_block_first * usb1;
Fabian Frederick7e1e4162014-08-08 14:21:03 -0700276 struct va_format vaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 va_list args;
278
279 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300280 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
David Howellsbc98a422017-07-17 08:45:34 +0100282 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 usb1->fs_clean = UFS_FSBAD;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700284 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300285 ufs_mark_sb_dirty(sb);
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800286 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Fabian Frederick7e1e4162014-08-08 14:21:03 -0700288 va_start(args, fmt);
289 vaf.fmt = fmt;
290 vaf.va = &args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 switch (UFS_SB(sb)->s_mount_opt & UFS_MOUNT_ONERROR) {
292 case UFS_MOUNT_ONERROR_PANIC:
Fabian Frederick7e1e4162014-08-08 14:21:03 -0700293 panic("panic (device %s): %s: %pV\n",
294 sb->s_id, function, &vaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 case UFS_MOUNT_ONERROR_LOCK:
297 case UFS_MOUNT_ONERROR_UMOUNT:
298 case UFS_MOUNT_ONERROR_REPAIR:
Fabian Frederick7e1e4162014-08-08 14:21:03 -0700299 pr_crit("error (device %s): %s: %pV\n",
300 sb->s_id, function, &vaf);
301 }
302 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305void ufs_panic (struct super_block * sb, const char * function,
306 const char * fmt, ...)
307{
308 struct ufs_sb_private_info * uspi;
309 struct ufs_super_block_first * usb1;
Fabian Frederick7e1e4162014-08-08 14:21:03 -0700310 struct va_format vaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 va_list args;
312
313 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300314 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
David Howellsbc98a422017-07-17 08:45:34 +0100316 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 usb1->fs_clean = UFS_FSBAD;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700318 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300319 ufs_mark_sb_dirty(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Fabian Frederick7e1e4162014-08-08 14:21:03 -0700321 va_start(args, fmt);
322 vaf.fmt = fmt;
323 vaf.va = &args;
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800324 sb->s_flags |= SB_RDONLY;
Fabian Frederick7e1e4162014-08-08 14:21:03 -0700325 pr_crit("panic (device %s): %s: %pV\n",
326 sb->s_id, function, &vaf);
327 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330void ufs_warning (struct super_block * sb, const char * function,
331 const char * fmt, ...)
332{
Fabian Frederick7e1e4162014-08-08 14:21:03 -0700333 struct va_format vaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 va_list args;
335
Fabian Frederick7e1e4162014-08-08 14:21:03 -0700336 va_start(args, fmt);
337 vaf.fmt = fmt;
338 vaf.va = &args;
339 pr_warn("(device %s): %s: %pV\n",
340 sb->s_id, function, &vaf);
341 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
344enum {
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -0700345 Opt_type_old = UFS_MOUNT_UFSTYPE_OLD,
346 Opt_type_sunx86 = UFS_MOUNT_UFSTYPE_SUNx86,
347 Opt_type_sun = UFS_MOUNT_UFSTYPE_SUN,
348 Opt_type_sunos = UFS_MOUNT_UFSTYPE_SUNOS,
349 Opt_type_44bsd = UFS_MOUNT_UFSTYPE_44BSD,
350 Opt_type_ufs2 = UFS_MOUNT_UFSTYPE_UFS2,
351 Opt_type_hp = UFS_MOUNT_UFSTYPE_HP,
352 Opt_type_nextstepcd = UFS_MOUNT_UFSTYPE_NEXTSTEP_CD,
353 Opt_type_nextstep = UFS_MOUNT_UFSTYPE_NEXTSTEP,
354 Opt_type_openstep = UFS_MOUNT_UFSTYPE_OPENSTEP,
355 Opt_onerror_panic = UFS_MOUNT_ONERROR_PANIC,
356 Opt_onerror_lock = UFS_MOUNT_ONERROR_LOCK,
357 Opt_onerror_umount = UFS_MOUNT_ONERROR_UMOUNT,
358 Opt_onerror_repair = UFS_MOUNT_ONERROR_REPAIR,
359 Opt_err
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360};
361
Steven Whitehousea447c092008-10-13 10:46:57 +0100362static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 {Opt_type_old, "ufstype=old"},
364 {Opt_type_sunx86, "ufstype=sunx86"},
365 {Opt_type_sun, "ufstype=sun"},
Mark Fortescue252e2112007-10-16 23:26:31 -0700366 {Opt_type_sunos, "ufstype=sunos"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 {Opt_type_44bsd, "ufstype=44bsd"},
368 {Opt_type_ufs2, "ufstype=ufs2"},
369 {Opt_type_ufs2, "ufstype=5xbsd"},
370 {Opt_type_hp, "ufstype=hp"},
371 {Opt_type_nextstepcd, "ufstype=nextstep-cd"},
372 {Opt_type_nextstep, "ufstype=nextstep"},
373 {Opt_type_openstep, "ufstype=openstep"},
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -0700374/*end of possible ufs types */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 {Opt_onerror_panic, "onerror=panic"},
376 {Opt_onerror_lock, "onerror=lock"},
377 {Opt_onerror_umount, "onerror=umount"},
378 {Opt_onerror_repair, "onerror=repair"},
379 {Opt_err, NULL}
380};
381
382static int ufs_parse_options (char * options, unsigned * mount_options)
383{
384 char * p;
385
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700386 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 if (!options)
389 return 1;
390
391 while ((p = strsep(&options, ",")) != NULL) {
392 substring_t args[MAX_OPT_ARGS];
393 int token;
394 if (!*p)
395 continue;
396
397 token = match_token(p, tokens, args);
398 switch (token) {
399 case Opt_type_old:
400 ufs_clear_opt (*mount_options, UFSTYPE);
401 ufs_set_opt (*mount_options, UFSTYPE_OLD);
402 break;
403 case Opt_type_sunx86:
404 ufs_clear_opt (*mount_options, UFSTYPE);
405 ufs_set_opt (*mount_options, UFSTYPE_SUNx86);
406 break;
407 case Opt_type_sun:
408 ufs_clear_opt (*mount_options, UFSTYPE);
409 ufs_set_opt (*mount_options, UFSTYPE_SUN);
410 break;
Mark Fortescue252e2112007-10-16 23:26:31 -0700411 case Opt_type_sunos:
412 ufs_clear_opt(*mount_options, UFSTYPE);
413 ufs_set_opt(*mount_options, UFSTYPE_SUNOS);
414 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 case Opt_type_44bsd:
416 ufs_clear_opt (*mount_options, UFSTYPE);
417 ufs_set_opt (*mount_options, UFSTYPE_44BSD);
418 break;
419 case Opt_type_ufs2:
420 ufs_clear_opt(*mount_options, UFSTYPE);
421 ufs_set_opt(*mount_options, UFSTYPE_UFS2);
422 break;
423 case Opt_type_hp:
424 ufs_clear_opt (*mount_options, UFSTYPE);
425 ufs_set_opt (*mount_options, UFSTYPE_HP);
426 break;
427 case Opt_type_nextstepcd:
428 ufs_clear_opt (*mount_options, UFSTYPE);
429 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP_CD);
430 break;
431 case Opt_type_nextstep:
432 ufs_clear_opt (*mount_options, UFSTYPE);
433 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP);
434 break;
435 case Opt_type_openstep:
436 ufs_clear_opt (*mount_options, UFSTYPE);
437 ufs_set_opt (*mount_options, UFSTYPE_OPENSTEP);
438 break;
439 case Opt_onerror_panic:
440 ufs_clear_opt (*mount_options, ONERROR);
441 ufs_set_opt (*mount_options, ONERROR_PANIC);
442 break;
443 case Opt_onerror_lock:
444 ufs_clear_opt (*mount_options, ONERROR);
445 ufs_set_opt (*mount_options, ONERROR_LOCK);
446 break;
447 case Opt_onerror_umount:
448 ufs_clear_opt (*mount_options, ONERROR);
449 ufs_set_opt (*mount_options, ONERROR_UMOUNT);
450 break;
451 case Opt_onerror_repair:
Fabian Frederickde771bd2014-08-08 14:20:59 -0700452 pr_err("Unable to do repair on error, will lock lock instead\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 ufs_clear_opt (*mount_options, ONERROR);
454 ufs_set_opt (*mount_options, ONERROR_REPAIR);
455 break;
456 default:
Fabian Frederickde771bd2014-08-08 14:20:59 -0700457 pr_err("Invalid option: \"%s\" or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return 0;
459 }
460 }
461 return 1;
462}
463
464/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300465 * Different types of UFS hold fs_cstotal in different
466 * places, and use different data structure for it.
467 * To make things simpler we just copy fs_cstotal to ufs_sb_private_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 */
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700469static void ufs_setup_cstotal(struct super_block *sb)
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800470{
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700471 struct ufs_sb_info *sbi = UFS_SB(sb);
472 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700473 struct ufs_super_block_first *usb1;
474 struct ufs_super_block_second *usb2;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700475 struct ufs_super_block_third *usb3;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700476 unsigned mtype = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
477
478 UFSD("ENTER, mtype=%u\n", mtype);
479 usb1 = ubh_get_usb_first(uspi);
480 usb2 = ubh_get_usb_second(uspi);
481 usb3 = ubh_get_usb_third(uspi);
482
483 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
Al Viro96ecff12017-06-14 15:17:32 -0400484 (usb2->fs_un.fs_u2.fs_maxbsize == usb1->fs_bsize)) ||
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700485 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
486 /*we have statistic in different place, then usual*/
487 uspi->cs_total.cs_ndir = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir);
488 uspi->cs_total.cs_nbfree = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree);
489 uspi->cs_total.cs_nifree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree);
490 uspi->cs_total.cs_nffree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree);
491 } else {
492 uspi->cs_total.cs_ndir = fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir);
493 uspi->cs_total.cs_nbfree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree);
494 uspi->cs_total.cs_nifree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree);
495 uspi->cs_total.cs_nffree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree);
496 }
497 UFSD("EXIT\n");
498}
499
500/*
501 * Read on-disk structures associated with cylinder groups
502 */
503static int ufs_read_cylinder_structures(struct super_block *sb)
504{
505 struct ufs_sb_info *sbi = UFS_SB(sb);
506 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct ufs_buffer_head * ubh;
508 unsigned char * base, * space;
509 unsigned size, blks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700511 UFSD("ENTER\n");
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /*
514 * Read cs structures from (usually) first data block
515 * on the device.
516 */
517 size = uspi->s_cssize;
518 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
Arnd Bergmann788257d2011-01-24 10:14:12 +0100519 base = space = kmalloc(size, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (!base)
521 goto failed;
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800522 sbi->s_csp = (struct ufs_csum *)space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 for (i = 0; i < blks; i += uspi->s_fpb) {
524 size = uspi->s_bsize;
525 if (i + uspi->s_fpb > blks)
526 size = (blks - i) * uspi->s_fsize;
527
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800528 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
Evgeniy7b4ee732006-01-14 11:42:06 +0300529
530 if (!ubh)
531 goto failed;
532
533 ubh_ubhcpymem (space, ubh, size);
Evgeniy7b4ee732006-01-14 11:42:06 +0300534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 space += size;
536 ubh_brelse (ubh);
537 ubh = NULL;
538 }
539
540 /*
541 * Read cylinder group (we read only first fragment from block
542 * at this time) and prepare internal data structures for cg caching.
543 */
Arnd Bergmann788257d2011-01-24 10:14:12 +0100544 if (!(sbi->s_ucg = kmalloc (sizeof(struct buffer_head *) * uspi->s_ncg, GFP_NOFS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 goto failed;
546 for (i = 0; i < uspi->s_ncg; i++)
547 sbi->s_ucg[i] = NULL;
548 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
549 sbi->s_ucpi[i] = NULL;
550 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
551 }
552 for (i = 0; i < uspi->s_ncg; i++) {
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700553 UFSD("read cg %u\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (!(sbi->s_ucg[i] = sb_bread(sb, ufs_cgcmin(i))))
555 goto failed;
556 if (!ufs_cg_chkmagic (sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data))
557 goto failed;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 ufs_print_cylinder_stuff(sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
Arnd Bergmann788257d2011-01-24 10:14:12 +0100562 if (!(sbi->s_ucpi[i] = kmalloc (sizeof(struct ufs_cg_private_info), GFP_NOFS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 goto failed;
564 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
565 }
566 sbi->s_cg_loaded = 0;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700567 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return 1;
569
570failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800571 kfree (base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (sbi->s_ucg) {
573 for (i = 0; i < uspi->s_ncg; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800574 if (sbi->s_ucg[i])
575 brelse (sbi->s_ucg[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 kfree (sbi->s_ucg);
577 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800578 kfree (sbi->s_ucpi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700580 UFSD("EXIT (FAILED)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return 0;
582}
583
584/*
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700585 * Sync our internal copy of fs_cstotal with disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 */
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700587static void ufs_put_cstotal(struct super_block *sb)
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800588{
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700589 unsigned mtype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
590 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
591 struct ufs_super_block_first *usb1;
592 struct ufs_super_block_second *usb2;
593 struct ufs_super_block_third *usb3;
594
595 UFSD("ENTER\n");
596 usb1 = ubh_get_usb_first(uspi);
597 usb2 = ubh_get_usb_second(uspi);
598 usb3 = ubh_get_usb_third(uspi);
599
Al Viro96ecff12017-06-14 15:17:32 -0400600 if (mtype == UFS_MOUNT_UFSTYPE_UFS2) {
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700601 /*we have statistic in different place, then usual*/
602 usb2->fs_un.fs_u2.cs_ndir =
603 cpu_to_fs64(sb, uspi->cs_total.cs_ndir);
604 usb2->fs_un.fs_u2.cs_nbfree =
605 cpu_to_fs64(sb, uspi->cs_total.cs_nbfree);
606 usb3->fs_un1.fs_u2.cs_nifree =
607 cpu_to_fs64(sb, uspi->cs_total.cs_nifree);
608 usb3->fs_un1.fs_u2.cs_nffree =
609 cpu_to_fs64(sb, uspi->cs_total.cs_nffree);
Al Viro96ecff12017-06-14 15:17:32 -0400610 goto out;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700611 }
Al Viro96ecff12017-06-14 15:17:32 -0400612
613 if (mtype == UFS_MOUNT_UFSTYPE_44BSD &&
614 (usb2->fs_un.fs_u2.fs_maxbsize == usb1->fs_bsize)) {
615 /* store stats in both old and new places */
616 usb2->fs_un.fs_u2.cs_ndir =
617 cpu_to_fs64(sb, uspi->cs_total.cs_ndir);
618 usb2->fs_un.fs_u2.cs_nbfree =
619 cpu_to_fs64(sb, uspi->cs_total.cs_nbfree);
620 usb3->fs_un1.fs_u2.cs_nifree =
621 cpu_to_fs64(sb, uspi->cs_total.cs_nifree);
622 usb3->fs_un1.fs_u2.cs_nffree =
623 cpu_to_fs64(sb, uspi->cs_total.cs_nffree);
624 }
625 usb1->fs_cstotal.cs_ndir = cpu_to_fs32(sb, uspi->cs_total.cs_ndir);
626 usb1->fs_cstotal.cs_nbfree = cpu_to_fs32(sb, uspi->cs_total.cs_nbfree);
627 usb1->fs_cstotal.cs_nifree = cpu_to_fs32(sb, uspi->cs_total.cs_nifree);
628 usb1->fs_cstotal.cs_nffree = cpu_to_fs32(sb, uspi->cs_total.cs_nffree);
629out:
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700630 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800631 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700632 UFSD("EXIT\n");
633}
634
635/**
636 * ufs_put_super_internal() - put on-disk intrenal structures
637 * @sb: pointer to super_block structure
638 * Put on-disk structures associated with cylinder groups
639 * and write them back to disk, also update cs_total on disk
640 */
641static void ufs_put_super_internal(struct super_block *sb)
642{
643 struct ufs_sb_info *sbi = UFS_SB(sb);
644 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 struct ufs_buffer_head * ubh;
646 unsigned char * base, * space;
647 unsigned blks, size, i;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700650 UFSD("ENTER\n");
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200651
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700652 ufs_put_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 size = uspi->s_cssize;
654 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800655 base = space = (char*) sbi->s_csp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 for (i = 0; i < blks; i += uspi->s_fpb) {
657 size = uspi->s_bsize;
658 if (i + uspi->s_fpb > blks)
659 size = (blks - i) * uspi->s_fsize;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 ubh_memcpyubh (ubh, space, size);
664 space += size;
665 ubh_mark_buffer_uptodate (ubh, 1);
666 ubh_mark_buffer_dirty (ubh);
667 ubh_brelse (ubh);
668 }
669 for (i = 0; i < sbi->s_cg_loaded; i++) {
670 ufs_put_cylinder (sb, i);
671 kfree (sbi->s_ucpi[i]);
672 }
673 for (; i < UFS_MAX_GROUP_LOADED; i++)
674 kfree (sbi->s_ucpi[i]);
675 for (i = 0; i < uspi->s_ncg; i++)
676 brelse (sbi->s_ucg[i]);
677 kfree (sbi->s_ucg);
678 kfree (base);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200679
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700680 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Artem Bityutskiy7bd54ef72012-07-12 16:28:07 +0300683static int ufs_sync_fs(struct super_block *sb, int wait)
684{
685 struct ufs_sb_private_info * uspi;
686 struct ufs_super_block_first * usb1;
687 struct ufs_super_block_third * usb3;
688 unsigned flags;
689
Fabian Frederickcdd9eef2015-06-10 10:09:32 +1000690 mutex_lock(&UFS_SB(sb)->s_lock);
Artem Bityutskiy7bd54ef72012-07-12 16:28:07 +0300691
692 UFSD("ENTER\n");
693
694 flags = UFS_SB(sb)->s_flags;
695 uspi = UFS_SB(sb)->s_uspi;
696 usb1 = ubh_get_usb_first(uspi);
697 usb3 = ubh_get_usb_third(uspi);
698
699 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
700 if ((flags & UFS_ST_MASK) == UFS_ST_SUN ||
701 (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
702 (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
703 ufs_set_fs_state(sb, usb1, usb3,
704 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
705 ufs_put_cstotal(sb);
Artem Bityutskiy7bd54ef72012-07-12 16:28:07 +0300706
707 UFSD("EXIT\n");
Fabian Frederickcdd9eef2015-06-10 10:09:32 +1000708 mutex_unlock(&UFS_SB(sb)->s_lock);
Artem Bityutskiy7bd54ef72012-07-12 16:28:07 +0300709
710 return 0;
711}
712
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300713static void delayed_sync_fs(struct work_struct *work)
Artem Bityutskiy7bd54ef72012-07-12 16:28:07 +0300714{
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300715 struct ufs_sb_info *sbi;
716
717 sbi = container_of(work, struct ufs_sb_info, sync_work.work);
718
719 spin_lock(&sbi->work_lock);
720 sbi->work_queued = 0;
721 spin_unlock(&sbi->work_lock);
722
723 ufs_sync_fs(sbi->sb, 1);
724}
725
726void ufs_mark_sb_dirty(struct super_block *sb)
727{
728 struct ufs_sb_info *sbi = UFS_SB(sb);
729 unsigned long delay;
730
731 spin_lock(&sbi->work_lock);
732 if (!sbi->work_queued) {
733 delay = msecs_to_jiffies(dirty_writeback_interval * 10);
734 queue_delayed_work(system_long_wq, &sbi->sync_work, delay);
735 sbi->work_queued = 1;
736 }
737 spin_unlock(&sbi->work_lock);
Artem Bityutskiy7bd54ef72012-07-12 16:28:07 +0300738}
739
740static void ufs_put_super(struct super_block *sb)
741{
742 struct ufs_sb_info * sbi = UFS_SB(sb);
743
744 UFSD("ENTER\n");
745
David Howellsbc98a422017-07-17 08:45:34 +0100746 if (!sb_rdonly(sb))
Artem Bityutskiy7bd54ef72012-07-12 16:28:07 +0300747 ufs_put_super_internal(sb);
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300748 cancel_delayed_work_sync(&sbi->sync_work);
Artem Bityutskiy7bd54ef72012-07-12 16:28:07 +0300749
750 ubh_brelse_uspi (sbi->s_uspi);
751 kfree (sbi->s_uspi);
752 kfree (sbi);
753 sb->s_fs_info = NULL;
754 UFSD("EXIT\n");
755 return;
756}
757
Al Viro6b0d1442017-06-08 21:15:45 -0400758static u64 ufs_max_bytes(struct super_block *sb)
759{
760 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
761 int bits = uspi->s_apbshift;
762 u64 res;
763
764 if (bits > 21)
765 res = ~0ULL;
766 else
767 res = UFS_NDADDR + (1LL << bits) + (1LL << (2*bits)) +
768 (1LL << (3*bits));
769
770 if (res >= (MAX_LFS_FILESIZE >> uspi->s_bshift))
771 return MAX_LFS_FILESIZE;
772 return res << uspi->s_bshift;
773}
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775static int ufs_fill_super(struct super_block *sb, void *data, int silent)
776{
777 struct ufs_sb_info * sbi;
778 struct ufs_sb_private_info * uspi;
779 struct ufs_super_block_first * usb1;
780 struct ufs_super_block_second * usb2;
781 struct ufs_super_block_third * usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct ufs_buffer_head * ubh;
783 struct inode *inode;
784 unsigned block_size, super_block_size;
785 unsigned flags;
Evgeniy7b4ee732006-01-14 11:42:06 +0300786 unsigned super_block_offset;
Duane Griffin9e6766c2009-01-08 22:43:48 +0000787 unsigned maxsymlen;
David Howellsb55c4602008-02-07 00:15:48 -0800788 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 uspi = NULL;
791 ubh = NULL;
792 flags = 0;
793
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700794 UFSD("ENTER\n");
Fabian Frederick02447562014-06-06 14:38:34 -0700795
796#ifndef CONFIG_UFS_FS_WRITE
David Howellsbc98a422017-07-17 08:45:34 +0100797 if (!sb_rdonly(sb)) {
Fabian Fredericka9814c52014-08-08 14:20:57 -0700798 pr_err("ufs was compiled with read-only support, can't be mounted as read-write\n");
Fabian Frederick02447562014-06-06 14:38:34 -0700799 return -EROFS;
800 }
801#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700803 sbi = kzalloc(sizeof(struct ufs_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (!sbi)
805 goto failed_nomem;
806 sb->s_fs_info = sbi;
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300807 sbi->sb = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
David Howellsbc98a422017-07-17 08:45:34 +0100809 UFSD("flag %u\n", (int)(sb_rdonly(sb)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Fabian Fredericke4f95512015-06-17 18:15:45 +0200811 mutex_init(&sbi->s_lock);
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300812 spin_lock_init(&sbi->work_lock);
813 INIT_DELAYED_WORK(&sbi->sync_work, delayed_sync_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /*
815 * Set default mount options
816 * Parse mount options
817 */
818 sbi->s_mount_opt = 0;
819 ufs_set_opt (sbi->s_mount_opt, ONERROR_LOCK);
820 if (!ufs_parse_options ((char *) data, &sbi->s_mount_opt)) {
Fabian Fredericka9814c52014-08-08 14:20:57 -0700821 pr_err("wrong mount options\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 goto failed;
823 }
824 if (!(sbi->s_mount_opt & UFS_MOUNT_UFSTYPE)) {
825 if (!silent)
Fabian Fredericka9814c52014-08-08 14:20:57 -0700826 pr_err("You didn't specify the type of your ufs filesystem\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 "mount -t ufs -o ufstype="
Michael Owenc690a722006-03-24 18:21:44 +0100828 "sun|sunx86|44bsd|ufs2|5xbsd|old|hp|nextstep|nextstep-cd|openstep ...\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 ">>>WARNING<<< Wrong ufstype may corrupt your filesystem, "
830 "default is ufstype=old\n");
831 ufs_set_opt (sbi->s_mount_opt, UFSTYPE_OLD);
832 }
833
Mark Fortescue252e2112007-10-16 23:26:31 -0700834 uspi = kzalloc(sizeof(struct ufs_sb_private_info), GFP_KERNEL);
835 sbi->s_uspi = uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (!uspi)
837 goto failed;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800838 uspi->s_dirblksize = UFS_SECTOR_SIZE;
Evgeniy7b4ee732006-01-14 11:42:06 +0300839 super_block_offset=UFS_SBLOCK;
840
Richard Narron239e2502017-06-04 16:23:18 -0700841 sb->s_maxbytes = MAX_LFS_FILESIZE;
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 switch (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) {
844 case UFS_MOUNT_UFSTYPE_44BSD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700845 UFSD("ufstype=44bsd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 uspi->s_fsize = block_size = 512;
847 uspi->s_fmask = ~(512 - 1);
848 uspi->s_fshift = 9;
849 uspi->s_sbsize = super_block_size = 1536;
850 uspi->s_sbbase = 0;
851 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
852 break;
853 case UFS_MOUNT_UFSTYPE_UFS2:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700854 UFSD("ufstype=ufs2\n");
Evgeniy7b4ee732006-01-14 11:42:06 +0300855 super_block_offset=SBLOCK_UFS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 uspi->s_fsize = block_size = 512;
857 uspi->s_fmask = ~(512 - 1);
858 uspi->s_fshift = 9;
859 uspi->s_sbsize = super_block_size = 1536;
860 uspi->s_sbbase = 0;
861 flags |= UFS_TYPE_UFS2 | UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 break;
863
864 case UFS_MOUNT_UFSTYPE_SUN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700865 UFSD("ufstype=sun\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 uspi->s_fsize = block_size = 1024;
867 uspi->s_fmask = ~(1024 - 1);
868 uspi->s_fshift = 10;
869 uspi->s_sbsize = super_block_size = 2048;
870 uspi->s_sbbase = 0;
Mark Fortescue252e2112007-10-16 23:26:31 -0700871 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUN | UFS_CG_SUN;
873 break;
874
Mark Fortescue252e2112007-10-16 23:26:31 -0700875 case UFS_MOUNT_UFSTYPE_SUNOS:
Fabian Frederickd4beaab2014-08-08 14:21:05 -0700876 UFSD("ufstype=sunos\n");
Mark Fortescue252e2112007-10-16 23:26:31 -0700877 uspi->s_fsize = block_size = 1024;
878 uspi->s_fmask = ~(1024 - 1);
879 uspi->s_fshift = 10;
880 uspi->s_sbsize = 2048;
881 super_block_size = 2048;
882 uspi->s_sbbase = 0;
883 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
884 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_SUNOS | UFS_CG_SUN;
885 break;
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 case UFS_MOUNT_UFSTYPE_SUNx86:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700888 UFSD("ufstype=sunx86\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 uspi->s_fsize = block_size = 1024;
890 uspi->s_fmask = ~(1024 - 1);
891 uspi->s_fshift = 10;
892 uspi->s_sbsize = super_block_size = 2048;
893 uspi->s_sbbase = 0;
Mark Fortescue252e2112007-10-16 23:26:31 -0700894 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUNx86 | UFS_CG_SUN;
896 break;
897
898 case UFS_MOUNT_UFSTYPE_OLD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700899 UFSD("ufstype=old\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 uspi->s_fsize = block_size = 1024;
901 uspi->s_fmask = ~(1024 - 1);
902 uspi->s_fshift = 10;
903 uspi->s_sbsize = super_block_size = 2048;
904 uspi->s_sbbase = 0;
905 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
David Howellsbc98a422017-07-17 08:45:34 +0100906 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (!silent)
Fabian Fredericka9814c52014-08-08 14:20:57 -0700908 pr_info("ufstype=old is supported read-only\n");
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800909 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911 break;
912
913 case UFS_MOUNT_UFSTYPE_NEXTSTEP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700914 UFSD("ufstype=nextstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 uspi->s_fsize = block_size = 1024;
916 uspi->s_fmask = ~(1024 - 1);
917 uspi->s_fshift = 10;
918 uspi->s_sbsize = super_block_size = 2048;
919 uspi->s_sbbase = 0;
Evgeniy Dushistov0c664f92007-12-04 23:45:06 -0800920 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
David Howellsbc98a422017-07-17 08:45:34 +0100922 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (!silent)
Fabian Fredericka9814c52014-08-08 14:20:57 -0700924 pr_info("ufstype=nextstep is supported read-only\n");
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800925 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927 break;
928
929 case UFS_MOUNT_UFSTYPE_NEXTSTEP_CD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700930 UFSD("ufstype=nextstep-cd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 uspi->s_fsize = block_size = 2048;
932 uspi->s_fmask = ~(2048 - 1);
933 uspi->s_fshift = 11;
934 uspi->s_sbsize = super_block_size = 2048;
935 uspi->s_sbbase = 0;
Evgeniy Dushistov0c664f92007-12-04 23:45:06 -0800936 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
David Howellsbc98a422017-07-17 08:45:34 +0100938 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (!silent)
Fabian Fredericka9814c52014-08-08 14:20:57 -0700940 pr_info("ufstype=nextstep-cd is supported read-only\n");
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800941 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943 break;
944
945 case UFS_MOUNT_UFSTYPE_OPENSTEP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700946 UFSD("ufstype=openstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 uspi->s_fsize = block_size = 1024;
948 uspi->s_fmask = ~(1024 - 1);
949 uspi->s_fshift = 10;
950 uspi->s_sbsize = super_block_size = 2048;
951 uspi->s_sbbase = 0;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800952 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
David Howellsbc98a422017-07-17 08:45:34 +0100954 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (!silent)
Fabian Fredericka9814c52014-08-08 14:20:57 -0700956 pr_info("ufstype=openstep is supported read-only\n");
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800957 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959 break;
960
961 case UFS_MOUNT_UFSTYPE_HP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700962 UFSD("ufstype=hp\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 uspi->s_fsize = block_size = 1024;
964 uspi->s_fmask = ~(1024 - 1);
965 uspi->s_fshift = 10;
966 uspi->s_sbsize = super_block_size = 2048;
967 uspi->s_sbbase = 0;
968 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
David Howellsbc98a422017-07-17 08:45:34 +0100969 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (!silent)
Fabian Fredericka9814c52014-08-08 14:20:57 -0700971 pr_info("ufstype=hp is supported read-only\n");
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800972 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974 break;
975 default:
976 if (!silent)
Fabian Fredericka9814c52014-08-08 14:20:57 -0700977 pr_err("unknown ufstype\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 goto failed;
979 }
980
981again:
982 if (!sb_set_blocksize(sb, block_size)) {
Fabian Frederickde771bd2014-08-08 14:20:59 -0700983 pr_err("failed to set blocksize\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 goto failed;
985 }
986
987 /*
988 * read ufs super block from device
989 */
Evgeniy7b4ee732006-01-14 11:42:06 +0300990
991 ubh = ubh_bread_uspi(uspi, sb, uspi->s_sbbase + super_block_offset/block_size, super_block_size);
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (!ubh)
994 goto failed;
995
Evgeniy7b4ee732006-01-14 11:42:06 +0300996 usb1 = ubh_get_usb_first(uspi);
997 usb2 = ubh_get_usb_second(uspi);
998 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Mark Fortescue252e2112007-10-16 23:26:31 -07001000 /* Sort out mod used on SunOS 4.1.3 for fs_state */
1001 uspi->s_postblformat = fs32_to_cpu(sb, usb3->fs_postblformat);
1002 if (((flags & UFS_ST_MASK) == UFS_ST_SUNOS) &&
1003 (uspi->s_postblformat != UFS_42POSTBLFMT)) {
1004 flags &= ~UFS_ST_MASK;
1005 flags |= UFS_ST_SUN;
1006 }
1007
Al Viro96ecff12017-06-14 15:17:32 -04001008 if ((flags & UFS_ST_MASK) == UFS_ST_44BSD &&
1009 uspi->s_postblformat == UFS_42POSTBLFMT) {
1010 if (!silent)
1011 pr_err("this is not a 44bsd filesystem");
1012 goto failed;
1013 }
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /*
1016 * Check ufs magic number
1017 */
1018 sbi->s_bytesex = BYTESEX_LE;
1019 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
1020 case UFS_MAGIC:
Thomas Stewartd27d7a92010-05-26 14:42:33 -07001021 case UFS_MAGIC_BW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 case UFS2_MAGIC:
1023 case UFS_MAGIC_LFN:
1024 case UFS_MAGIC_FEA:
1025 case UFS_MAGIC_4GB:
1026 goto magic_found;
1027 }
1028 sbi->s_bytesex = BYTESEX_BE;
1029 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
1030 case UFS_MAGIC:
Thomas Stewartd27d7a92010-05-26 14:42:33 -07001031 case UFS_MAGIC_BW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 case UFS2_MAGIC:
1033 case UFS_MAGIC_LFN:
1034 case UFS_MAGIC_FEA:
1035 case UFS_MAGIC_4GB:
1036 goto magic_found;
1037 }
1038
1039 if ((((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP)
1040 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP_CD)
1041 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_OPENSTEP))
1042 && uspi->s_sbbase < 256) {
1043 ubh_brelse_uspi(uspi);
1044 ubh = NULL;
1045 uspi->s_sbbase += 8;
1046 goto again;
1047 }
1048 if (!silent)
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001049 pr_err("%s(): bad magic number\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 goto failed;
1051
1052magic_found:
1053 /*
1054 * Check block and fragment sizes
1055 */
1056 uspi->s_bsize = fs32_to_cpu(sb, usb1->fs_bsize);
1057 uspi->s_fsize = fs32_to_cpu(sb, usb1->fs_fsize);
1058 uspi->s_sbsize = fs32_to_cpu(sb, usb1->fs_sbsize);
1059 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
1060 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
1061
vignesh babu9e8c4272007-07-15 23:41:36 -07001062 if (!is_power_of_2(uspi->s_fsize)) {
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001063 pr_err("%s(): fragment size %u is not a power of 2\n",
1064 __func__, uspi->s_fsize);
Fabian Fredericka9814c52014-08-08 14:20:57 -07001065 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067 if (uspi->s_fsize < 512) {
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001068 pr_err("%s(): fragment size %u is too small\n",
1069 __func__, uspi->s_fsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 goto failed;
1071 }
1072 if (uspi->s_fsize > 4096) {
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001073 pr_err("%s(): fragment size %u is too large\n",
1074 __func__, uspi->s_fsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 goto failed;
1076 }
vignesh babu9e8c4272007-07-15 23:41:36 -07001077 if (!is_power_of_2(uspi->s_bsize)) {
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001078 pr_err("%s(): block size %u is not a power of 2\n",
1079 __func__, uspi->s_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 goto failed;
1081 }
1082 if (uspi->s_bsize < 4096) {
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001083 pr_err("%s(): block size %u is too small\n",
1084 __func__, uspi->s_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 goto failed;
1086 }
1087 if (uspi->s_bsize / uspi->s_fsize > 8) {
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001088 pr_err("%s(): too many fragments per block (%u)\n",
1089 __func__, uspi->s_bsize / uspi->s_fsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 goto failed;
1091 }
1092 if (uspi->s_fsize != block_size || uspi->s_sbsize != super_block_size) {
1093 ubh_brelse_uspi(uspi);
1094 ubh = NULL;
1095 block_size = uspi->s_fsize;
1096 super_block_size = uspi->s_sbsize;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001097 UFSD("another value of block_size or super_block_size %u, %u\n", block_size, super_block_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 goto again;
1099 }
1100
Andrew Mortonf664f1f2007-10-29 14:37:21 -07001101 sbi->s_flags = flags;/*after that line some functions use s_flags*/
Evgeniy Dushistov3313e292007-02-12 00:54:31 -08001102 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 /*
1105 * Check, if file system was correctly unmounted.
1106 * If not, make it read only.
1107 */
Andrew Mortonf664f1f2007-10-29 14:37:21 -07001108 if (((flags & UFS_ST_MASK) == UFS_ST_44BSD) ||
1109 ((flags & UFS_ST_MASK) == UFS_ST_OLD) ||
1110 (((flags & UFS_ST_MASK) == UFS_ST_SUN ||
1111 (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
1112 (flags & UFS_ST_MASK) == UFS_ST_SUNx86) &&
1113 (ufs_get_fs_state(sb, usb1, usb3) == (UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 switch(usb1->fs_clean) {
1115 case UFS_FSCLEAN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001116 UFSD("fs is clean\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 break;
1118 case UFS_FSSTABLE:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001119 UFSD("fs is stable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 break;
Alex Viskovatoffb3a0fd42010-03-10 15:21:53 -08001121 case UFS_FSLOG:
1122 UFSD("fs is logging fs\n");
1123 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 case UFS_FSOSF1:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001125 UFSD("fs is DEC OSF/1\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 break;
1127 case UFS_FSACTIVE:
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001128 pr_err("%s(): fs is active\n", __func__);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001129 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 break;
1131 case UFS_FSBAD:
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001132 pr_err("%s(): fs is bad\n", __func__);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001133 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 break;
1135 default:
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001136 pr_err("%s(): can't grok fs_clean 0x%x\n",
1137 __func__, usb1->fs_clean);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001138 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 break;
1140 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001141 } else {
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001142 pr_err("%s(): fs needs fsck\n", __func__);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001143 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
1145
1146 /*
1147 * Read ufs_super_block into internal data structures
1148 */
1149 sb->s_op = &ufs_super_ops;
Alexey Dobriyanf3e2a522009-12-15 16:46:51 -08001150 sb->s_export_op = &ufs_export_ops;
Christoph Hellwig123e9ca2010-05-19 07:16:44 -04001151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 sb->s_magic = fs32_to_cpu(sb, usb3->fs_magic);
1153
1154 uspi->s_sblkno = fs32_to_cpu(sb, usb1->fs_sblkno);
1155 uspi->s_cblkno = fs32_to_cpu(sb, usb1->fs_cblkno);
1156 uspi->s_iblkno = fs32_to_cpu(sb, usb1->fs_iblkno);
1157 uspi->s_dblkno = fs32_to_cpu(sb, usb1->fs_dblkno);
1158 uspi->s_cgoffset = fs32_to_cpu(sb, usb1->fs_cgoffset);
1159 uspi->s_cgmask = fs32_to_cpu(sb, usb1->fs_cgmask);
1160
1161 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
Al Viroc5969612017-06-14 16:36:29 -04001162 uspi->s_size = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size);
1163 uspi->s_dsize = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001164 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 uspi->s_size = fs32_to_cpu(sb, usb1->fs_size);
1166 uspi->s_dsize = fs32_to_cpu(sb, usb1->fs_dsize);
1167 }
1168
1169 uspi->s_ncg = fs32_to_cpu(sb, usb1->fs_ncg);
1170 /* s_bsize already set */
1171 /* s_fsize already set */
1172 uspi->s_fpb = fs32_to_cpu(sb, usb1->fs_frag);
1173 uspi->s_minfree = fs32_to_cpu(sb, usb1->fs_minfree);
1174 uspi->s_bmask = fs32_to_cpu(sb, usb1->fs_bmask);
1175 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
1176 uspi->s_bshift = fs32_to_cpu(sb, usb1->fs_bshift);
1177 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001178 UFSD("uspi->s_bshift = %d,uspi->s_fshift = %d", uspi->s_bshift,
1179 uspi->s_fshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 uspi->s_fpbshift = fs32_to_cpu(sb, usb1->fs_fragshift);
1181 uspi->s_fsbtodb = fs32_to_cpu(sb, usb1->fs_fsbtodb);
1182 /* s_sbsize already set */
1183 uspi->s_csmask = fs32_to_cpu(sb, usb1->fs_csmask);
1184 uspi->s_csshift = fs32_to_cpu(sb, usb1->fs_csshift);
1185 uspi->s_nindir = fs32_to_cpu(sb, usb1->fs_nindir);
1186 uspi->s_inopb = fs32_to_cpu(sb, usb1->fs_inopb);
1187 uspi->s_nspf = fs32_to_cpu(sb, usb1->fs_nspf);
1188 uspi->s_npsect = ufs_get_fs_npsect(sb, usb1, usb3);
1189 uspi->s_interleave = fs32_to_cpu(sb, usb1->fs_interleave);
1190 uspi->s_trackskew = fs32_to_cpu(sb, usb1->fs_trackskew);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -08001191
1192 if (uspi->fs_magic == UFS2_MAGIC)
1193 uspi->s_csaddr = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_csaddr);
1194 else
1195 uspi->s_csaddr = fs32_to_cpu(sb, usb1->fs_csaddr);
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 uspi->s_cssize = fs32_to_cpu(sb, usb1->fs_cssize);
1198 uspi->s_cgsize = fs32_to_cpu(sb, usb1->fs_cgsize);
1199 uspi->s_ntrak = fs32_to_cpu(sb, usb1->fs_ntrak);
1200 uspi->s_nsect = fs32_to_cpu(sb, usb1->fs_nsect);
1201 uspi->s_spc = fs32_to_cpu(sb, usb1->fs_spc);
1202 uspi->s_ipg = fs32_to_cpu(sb, usb1->fs_ipg);
1203 uspi->s_fpg = fs32_to_cpu(sb, usb1->fs_fpg);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001204 uspi->s_cpc = fs32_to_cpu(sb, usb2->fs_un.fs_u1.fs_cpc);
1205 uspi->s_contigsumsize = fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_contigsumsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 uspi->s_qbmask = ufs_get_fs_qbmask(sb, usb3);
1207 uspi->s_qfmask = ufs_get_fs_qfmask(sb, usb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 uspi->s_nrpos = fs32_to_cpu(sb, usb3->fs_nrpos);
1209 uspi->s_postbloff = fs32_to_cpu(sb, usb3->fs_postbloff);
1210 uspi->s_rotbloff = fs32_to_cpu(sb, usb3->fs_rotbloff);
1211
Al Viroc5969612017-06-14 16:36:29 -04001212 uspi->s_root_blocks = mul_u64_u32_div(uspi->s_dsize,
1213 uspi->s_minfree, 100);
Al Viro77e9ce32017-06-17 15:44:06 -04001214 if (uspi->s_minfree <= 5) {
1215 uspi->s_time_to_space = ~0ULL;
1216 uspi->s_space_to_time = 0;
1217 usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTSPACE);
1218 } else {
1219 uspi->s_time_to_space = (uspi->s_root_blocks / 2) + 1;
1220 uspi->s_space_to_time = mul_u64_u32_div(uspi->s_dsize,
1221 uspi->s_minfree - 2, 100) - 1;
1222 }
Al Viroc5969612017-06-14 16:36:29 -04001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 /*
1225 * Compute another frequently used values
1226 */
1227 uspi->s_fpbmask = uspi->s_fpb - 1;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001228 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 uspi->s_apbshift = uspi->s_bshift - 3;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001230 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 uspi->s_apbshift = uspi->s_bshift - 2;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 uspi->s_2apbshift = uspi->s_apbshift * 2;
1234 uspi->s_3apbshift = uspi->s_apbshift * 3;
1235 uspi->s_apb = 1 << uspi->s_apbshift;
1236 uspi->s_2apb = 1 << uspi->s_2apbshift;
1237 uspi->s_3apb = 1 << uspi->s_3apbshift;
1238 uspi->s_apbmask = uspi->s_apb - 1;
1239 uspi->s_nspfshift = uspi->s_fshift - UFS_SECTOR_BITS;
1240 uspi->s_nspb = uspi->s_nspf << uspi->s_fpbshift;
1241 uspi->s_inopf = uspi->s_inopb >> uspi->s_fpbshift;
1242 uspi->s_bpf = uspi->s_fsize << 3;
1243 uspi->s_bpfshift = uspi->s_fshift + 3;
1244 uspi->s_bpfmask = uspi->s_bpf - 1;
Evgeniy Dushistov90b315a2008-02-08 04:20:16 -08001245 if ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_44BSD ||
1246 (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 uspi->s_maxsymlinklen =
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001248 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Duane Griffin9e6766c2009-01-08 22:43:48 +00001250 if (uspi->fs_magic == UFS2_MAGIC)
1251 maxsymlen = 2 * 4 * (UFS_NDADDR + UFS_NINDIR);
1252 else
1253 maxsymlen = 4 * (UFS_NDADDR + UFS_NINDIR);
1254 if (uspi->s_maxsymlinklen > maxsymlen) {
1255 ufs_warning(sb, __func__, "ufs_read_super: excessive maximum "
1256 "fast symlink size (%u)\n", uspi->s_maxsymlinklen);
1257 uspi->s_maxsymlinklen = maxsymlen;
1258 }
Al Viro6b0d1442017-06-08 21:15:45 -04001259 sb->s_maxbytes = ufs_max_bytes(sb);
Al Viro8de52772012-02-06 12:45:27 -05001260 sb->s_max_links = UFS_LINK_MAX;
Duane Griffin9e6766c2009-01-08 22:43:48 +00001261
David Howellsb55c4602008-02-07 00:15:48 -08001262 inode = ufs_iget(sb, UFS_ROOTINO);
1263 if (IS_ERR(inode)) {
1264 ret = PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 goto failed;
David Howellsb55c4602008-02-07 00:15:48 -08001266 }
Al Viro48fde702012-01-08 22:15:13 -05001267 sb->s_root = d_make_root(inode);
David Howellsb55c4602008-02-07 00:15:48 -08001268 if (!sb->s_root) {
1269 ret = -ENOMEM;
Al Viro48fde702012-01-08 22:15:13 -05001270 goto failed;
David Howellsb55c4602008-02-07 00:15:48 -08001271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001273 ufs_setup_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 /*
1275 * Read cylinder group structures
1276 */
David Howellsbc98a422017-07-17 08:45:34 +01001277 if (!sb_rdonly(sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (!ufs_read_cylinder_structures(sb))
1279 goto failed;
1280
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001281 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return 0;
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -08001285 if (ubh)
1286 ubh_brelse_uspi (uspi);
1287 kfree (uspi);
1288 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 sb->s_fs_info = NULL;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001290 UFSD("EXIT (FAILED)\n");
David Howellsb55c4602008-02-07 00:15:48 -08001291 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293failed_nomem:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001294 UFSD("EXIT (NOMEM)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return -ENOMEM;
1296}
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298static int ufs_remount (struct super_block *sb, int *mount_flags, char *data)
1299{
1300 struct ufs_sb_private_info * uspi;
1301 struct ufs_super_block_first * usb1;
1302 struct ufs_super_block_third * usb3;
1303 unsigned new_mount_opt, ufstype;
1304 unsigned flags;
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001305
Theodore Ts'o02b99842014-03-13 10:14:33 -04001306 sync_filesystem(sb);
Fabian Frederickcdd9eef2015-06-10 10:09:32 +10001307 mutex_lock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 uspi = UFS_SB(sb)->s_uspi;
1309 flags = UFS_SB(sb)->s_flags;
Evgeniy7b4ee732006-01-14 11:42:06 +03001310 usb1 = ubh_get_usb_first(uspi);
1311 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 /*
1314 * Allow the "check" option to be passed as a remount option.
1315 * It is not possible to change ufstype option during remount
1316 */
1317 ufstype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
1318 new_mount_opt = 0;
1319 ufs_set_opt (new_mount_opt, ONERROR_LOCK);
Al Virobbd68512009-05-06 10:43:07 -04001320 if (!ufs_parse_options (data, &new_mount_opt)) {
Fabian Frederickcdd9eef2015-06-10 10:09:32 +10001321 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return -EINVAL;
Al Virobbd68512009-05-06 10:43:07 -04001323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (!(new_mount_opt & UFS_MOUNT_UFSTYPE)) {
1325 new_mount_opt |= ufstype;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001326 } else if ((new_mount_opt & UFS_MOUNT_UFSTYPE) != ufstype) {
Fabian Fredericka9814c52014-08-08 14:20:57 -07001327 pr_err("ufstype can't be changed during remount\n");
Fabian Frederickcdd9eef2015-06-10 10:09:32 +10001328 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return -EINVAL;
1330 }
1331
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001332 if ((bool)(*mount_flags & SB_RDONLY) == sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 UFS_SB(sb)->s_mount_opt = new_mount_opt;
Fabian Frederickcdd9eef2015-06-10 10:09:32 +10001334 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return 0;
1336 }
1337
1338 /*
1339 * fs was mouted as rw, remounting ro
1340 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001341 if (*mount_flags & SB_RDONLY) {
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001342 ufs_put_super_internal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1344 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
Mark Fortescue252e2112007-10-16 23:26:31 -07001345 || (flags & UFS_ST_MASK) == UFS_ST_SUNOS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1347 ufs_set_fs_state(sb, usb1, usb3,
1348 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -07001349 ubh_mark_buffer_dirty (USPI_UBH(uspi));
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001350 sb->s_flags |= SB_RDONLY;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001351 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 /*
1353 * fs was mounted as ro, remounting rw
1354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355#ifndef CONFIG_UFS_FS_WRITE
Fabian Fredericka9814c52014-08-08 14:20:57 -07001356 pr_err("ufs was compiled with read-only support, can't be mounted as read-write\n");
Fabian Frederickcdd9eef2015-06-10 10:09:32 +10001357 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return -EINVAL;
1359#else
1360 if (ufstype != UFS_MOUNT_UFSTYPE_SUN &&
Mark Fortescue252e2112007-10-16 23:26:31 -07001361 ufstype != UFS_MOUNT_UFSTYPE_SUNOS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 ufstype != UFS_MOUNT_UFSTYPE_44BSD &&
Evgeniy Dushistovcbcae392007-02-12 00:54:30 -08001363 ufstype != UFS_MOUNT_UFSTYPE_SUNx86 &&
1364 ufstype != UFS_MOUNT_UFSTYPE_UFS2) {
Fabian Fredericka9814c52014-08-08 14:20:57 -07001365 pr_err("this ufstype is read-only supported\n");
Fabian Frederickcdd9eef2015-06-10 10:09:32 +10001366 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return -EINVAL;
1368 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001369 if (!ufs_read_cylinder_structures(sb)) {
Fabian Fredericka9814c52014-08-08 14:20:57 -07001370 pr_err("failed during remounting\n");
Fabian Frederickcdd9eef2015-06-10 10:09:32 +10001371 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return -EPERM;
1373 }
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001374 sb->s_flags &= ~SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375#endif
1376 }
1377 UFS_SB(sb)->s_mount_opt = new_mount_opt;
Fabian Frederickcdd9eef2015-06-10 10:09:32 +10001378 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return 0;
1380}
1381
Al Viro34c80b12011-12-08 21:32:45 -05001382static int ufs_show_options(struct seq_file *seq, struct dentry *root)
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001383{
Al Viro34c80b12011-12-08 21:32:45 -05001384 struct ufs_sb_info *sbi = UFS_SB(root->d_sb);
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001385 unsigned mval = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
Steven Whitehousea447c092008-10-13 10:46:57 +01001386 const struct match_token *tp = tokens;
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001387
1388 while (tp->token != Opt_onerror_panic && tp->token != mval)
1389 ++tp;
1390 BUG_ON(tp->token == Opt_onerror_panic);
1391 seq_printf(seq, ",%s", tp->pattern);
1392
1393 mval = sbi->s_mount_opt & UFS_MOUNT_ONERROR;
1394 while (tp->token != Opt_err && tp->token != mval)
1395 ++tp;
1396 BUG_ON(tp->token == Opt_err);
1397 seq_printf(seq, ",%s", tp->pattern);
1398
1399 return 0;
1400}
1401
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001402static int ufs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
David Howells726c3342006-06-23 02:02:58 -07001404 struct super_block *sb = dentry->d_sb;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001405 struct ufs_sb_private_info *uspi= UFS_SB(sb)->s_uspi;
1406 unsigned flags = UFS_SB(sb)->s_flags;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001407 struct ufs_super_block_third *usb3;
Coly Li41d577a2009-04-02 16:59:44 -07001408 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Al Virodff7cfd2015-06-16 04:27:05 -04001410 mutex_lock(&UFS_SB(sb)->s_lock);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001411 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Al Viroc5969612017-06-14 16:36:29 -04001413 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 buf->f_type = UFS2_MAGIC;
Al Viroc5969612017-06-14 16:36:29 -04001415 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 buf->f_type = UFS_MAGIC;
Al Viroc5969612017-06-14 16:36:29 -04001417
1418 buf->f_blocks = uspi->s_dsize;
1419 buf->f_bfree = ufs_freefrags(uspi);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001420 buf->f_ffree = uspi->cs_total.cs_nifree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 buf->f_bsize = sb->s_blocksize;
Al Viroc5969612017-06-14 16:36:29 -04001422 buf->f_bavail = (buf->f_bfree > uspi->s_root_blocks)
1423 ? (buf->f_bfree - uspi->s_root_blocks) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 buf->f_files = uspi->s_ncg * uspi->s_ipg;
1425 buf->f_namelen = UFS_MAXNAMLEN;
Coly Li41d577a2009-04-02 16:59:44 -07001426 buf->f_fsid.val[0] = (u32)id;
1427 buf->f_fsid.val[1] = (u32)(id >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Al Virodff7cfd2015-06-16 04:27:05 -04001429 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 return 0;
1432}
1433
Christoph Lametere18b8902006-12-06 20:33:20 -08001434static struct kmem_cache * ufs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436static struct inode *ufs_alloc_inode(struct super_block *sb)
1437{
1438 struct ufs_inode_info *ei;
Fabian Frederick61da3ae2015-02-17 13:45:28 -08001439
1440 ei = kmem_cache_alloc(ufs_inode_cachep, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (!ei)
1442 return NULL;
Fabian Frederick61da3ae2015-02-17 13:45:28 -08001443
Jeff Laytonbb8c2d62017-12-11 06:35:18 -05001444 inode_set_iversion(&ei->vfs_inode, 1);
Al Viro724bb092015-06-17 12:02:56 -04001445 seqlock_init(&ei->meta_lock);
1446 mutex_init(&ei->truncate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return &ei->vfs_inode;
1448}
1449
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001450static void ufs_i_callback(struct rcu_head *head)
1451{
1452 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001453 kmem_cache_free(ufs_inode_cachep, UFS_I(inode));
1454}
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456static void ufs_destroy_inode(struct inode *inode)
1457{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001458 call_rcu(&inode->i_rcu, ufs_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001461static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
1463 struct ufs_inode_info *ei = (struct ufs_inode_info *) foo;
1464
Christoph Lametera35afb82007-05-16 22:10:57 -07001465 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
Paul Mundt20c2df82007-07-20 10:11:58 +09001467
Fabian Frederick76ee4732014-04-07 15:39:55 -07001468static int __init init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
1470 ufs_inode_cachep = kmem_cache_create("ufs_inode_cache",
1471 sizeof(struct ufs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001472 0, (SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -08001473 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +09001474 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 if (ufs_inode_cachep == NULL)
1476 return -ENOMEM;
1477 return 0;
1478}
1479
1480static void destroy_inodecache(void)
1481{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10001482 /*
1483 * Make sure all delayed rcu free inodes are flushed before we
1484 * destroy cache.
1485 */
1486 rcu_barrier();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001487 kmem_cache_destroy(ufs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001490static const struct super_operations ufs_super_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 .alloc_inode = ufs_alloc_inode,
1492 .destroy_inode = ufs_destroy_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 .write_inode = ufs_write_inode,
Al Viro58e82682010-06-05 19:40:56 -04001494 .evict_inode = ufs_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 .put_super = ufs_put_super,
Christoph Hellwig8c800652009-06-08 10:08:05 +02001496 .sync_fs = ufs_sync_fs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 .statfs = ufs_statfs,
1498 .remount_fs = ufs_remount,
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001499 .show_options = ufs_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500};
1501
Al Viro152a0832010-07-25 00:46:55 +04001502static struct dentry *ufs_mount(struct file_system_type *fs_type,
1503 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Al Viro152a0832010-07-25 00:46:55 +04001505 return mount_bdev(fs_type, flags, dev_name, data, ufs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507
1508static struct file_system_type ufs_fs_type = {
1509 .owner = THIS_MODULE,
1510 .name = "ufs",
Al Viro152a0832010-07-25 00:46:55 +04001511 .mount = ufs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 .kill_sb = kill_block_super,
1513 .fs_flags = FS_REQUIRES_DEV,
1514};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001515MODULE_ALIAS_FS("ufs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517static int __init init_ufs_fs(void)
1518{
1519 int err = init_inodecache();
1520 if (err)
1521 goto out1;
1522 err = register_filesystem(&ufs_fs_type);
1523 if (err)
1524 goto out;
1525 return 0;
1526out:
1527 destroy_inodecache();
1528out1:
1529 return err;
1530}
1531
1532static void __exit exit_ufs_fs(void)
1533{
1534 unregister_filesystem(&ufs_fs_type);
1535 destroy_inodecache();
1536}
1537
1538module_init(init_ufs_fs)
1539module_exit(exit_ufs_fs)
1540MODULE_LICENSE("GPL");