blob: 84c0c5178cd28f408bd69baba34d1f14c56392fd [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 */
Kees Cook6da2ec52018-06-12 13:55:00 -0700544 sbi->s_ucg = kmalloc_array(uspi->s_ncg, sizeof(struct buffer_head *),
545 GFP_NOFS);
546 if (!sbi->s_ucg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 goto failed;
548 for (i = 0; i < uspi->s_ncg; i++)
549 sbi->s_ucg[i] = NULL;
550 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
551 sbi->s_ucpi[i] = NULL;
552 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
553 }
554 for (i = 0; i < uspi->s_ncg; i++) {
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700555 UFSD("read cg %u\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (!(sbi->s_ucg[i] = sb_bread(sb, ufs_cgcmin(i))))
557 goto failed;
558 if (!ufs_cg_chkmagic (sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data))
559 goto failed;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 ufs_print_cylinder_stuff(sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
Arnd Bergmann788257d2011-01-24 10:14:12 +0100564 if (!(sbi->s_ucpi[i] = kmalloc (sizeof(struct ufs_cg_private_info), GFP_NOFS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto failed;
566 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
567 }
568 sbi->s_cg_loaded = 0;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700569 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return 1;
571
572failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800573 kfree (base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (sbi->s_ucg) {
575 for (i = 0; i < uspi->s_ncg; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800576 if (sbi->s_ucg[i])
577 brelse (sbi->s_ucg[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 kfree (sbi->s_ucg);
579 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800580 kfree (sbi->s_ucpi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700582 UFSD("EXIT (FAILED)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return 0;
584}
585
586/*
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700587 * Sync our internal copy of fs_cstotal with disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 */
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700589static void ufs_put_cstotal(struct super_block *sb)
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800590{
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700591 unsigned mtype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
592 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
593 struct ufs_super_block_first *usb1;
594 struct ufs_super_block_second *usb2;
595 struct ufs_super_block_third *usb3;
596
597 UFSD("ENTER\n");
598 usb1 = ubh_get_usb_first(uspi);
599 usb2 = ubh_get_usb_second(uspi);
600 usb3 = ubh_get_usb_third(uspi);
601
Al Viro96ecff12017-06-14 15:17:32 -0400602 if (mtype == UFS_MOUNT_UFSTYPE_UFS2) {
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700603 /*we have statistic in different place, then usual*/
604 usb2->fs_un.fs_u2.cs_ndir =
605 cpu_to_fs64(sb, uspi->cs_total.cs_ndir);
606 usb2->fs_un.fs_u2.cs_nbfree =
607 cpu_to_fs64(sb, uspi->cs_total.cs_nbfree);
608 usb3->fs_un1.fs_u2.cs_nifree =
609 cpu_to_fs64(sb, uspi->cs_total.cs_nifree);
610 usb3->fs_un1.fs_u2.cs_nffree =
611 cpu_to_fs64(sb, uspi->cs_total.cs_nffree);
Al Viro96ecff12017-06-14 15:17:32 -0400612 goto out;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700613 }
Al Viro96ecff12017-06-14 15:17:32 -0400614
615 if (mtype == UFS_MOUNT_UFSTYPE_44BSD &&
616 (usb2->fs_un.fs_u2.fs_maxbsize == usb1->fs_bsize)) {
617 /* store stats in both old and new places */
618 usb2->fs_un.fs_u2.cs_ndir =
619 cpu_to_fs64(sb, uspi->cs_total.cs_ndir);
620 usb2->fs_un.fs_u2.cs_nbfree =
621 cpu_to_fs64(sb, uspi->cs_total.cs_nbfree);
622 usb3->fs_un1.fs_u2.cs_nifree =
623 cpu_to_fs64(sb, uspi->cs_total.cs_nifree);
624 usb3->fs_un1.fs_u2.cs_nffree =
625 cpu_to_fs64(sb, uspi->cs_total.cs_nffree);
626 }
627 usb1->fs_cstotal.cs_ndir = cpu_to_fs32(sb, uspi->cs_total.cs_ndir);
628 usb1->fs_cstotal.cs_nbfree = cpu_to_fs32(sb, uspi->cs_total.cs_nbfree);
629 usb1->fs_cstotal.cs_nifree = cpu_to_fs32(sb, uspi->cs_total.cs_nifree);
630 usb1->fs_cstotal.cs_nffree = cpu_to_fs32(sb, uspi->cs_total.cs_nffree);
631out:
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700632 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800633 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700634 UFSD("EXIT\n");
635}
636
637/**
638 * ufs_put_super_internal() - put on-disk intrenal structures
639 * @sb: pointer to super_block structure
640 * Put on-disk structures associated with cylinder groups
641 * and write them back to disk, also update cs_total on disk
642 */
643static void ufs_put_super_internal(struct super_block *sb)
644{
645 struct ufs_sb_info *sbi = UFS_SB(sb);
646 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 struct ufs_buffer_head * ubh;
648 unsigned char * base, * space;
649 unsigned blks, size, i;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700652 UFSD("ENTER\n");
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200653
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700654 ufs_put_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 size = uspi->s_cssize;
656 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800657 base = space = (char*) sbi->s_csp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 for (i = 0; i < blks; i += uspi->s_fpb) {
659 size = uspi->s_bsize;
660 if (i + uspi->s_fpb > blks)
661 size = (blks - i) * uspi->s_fsize;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 ubh_memcpyubh (ubh, space, size);
666 space += size;
667 ubh_mark_buffer_uptodate (ubh, 1);
668 ubh_mark_buffer_dirty (ubh);
669 ubh_brelse (ubh);
670 }
671 for (i = 0; i < sbi->s_cg_loaded; i++) {
672 ufs_put_cylinder (sb, i);
673 kfree (sbi->s_ucpi[i]);
674 }
675 for (; i < UFS_MAX_GROUP_LOADED; i++)
676 kfree (sbi->s_ucpi[i]);
677 for (i = 0; i < uspi->s_ncg; i++)
678 brelse (sbi->s_ucg[i]);
679 kfree (sbi->s_ucg);
680 kfree (base);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200681
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700682 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Artem Bityutskiy7bd54ef72012-07-12 16:28:07 +0300685static int ufs_sync_fs(struct super_block *sb, int wait)
686{
687 struct ufs_sb_private_info * uspi;
688 struct ufs_super_block_first * usb1;
689 struct ufs_super_block_third * usb3;
690 unsigned flags;
691
Fabian Frederickcdd9eef2015-06-10 10:09:32 +1000692 mutex_lock(&UFS_SB(sb)->s_lock);
Artem Bityutskiy7bd54ef72012-07-12 16:28:07 +0300693
694 UFSD("ENTER\n");
695
696 flags = UFS_SB(sb)->s_flags;
697 uspi = UFS_SB(sb)->s_uspi;
698 usb1 = ubh_get_usb_first(uspi);
699 usb3 = ubh_get_usb_third(uspi);
700
Arnd Bergmanna3fda0f2018-08-17 15:43:47 -0700701 usb1->fs_time = ufs_get_seconds(sb);
Artem Bityutskiy7bd54ef72012-07-12 16:28:07 +0300702 if ((flags & UFS_ST_MASK) == UFS_ST_SUN ||
703 (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
704 (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
705 ufs_set_fs_state(sb, usb1, usb3,
706 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
707 ufs_put_cstotal(sb);
Artem Bityutskiy7bd54ef72012-07-12 16:28:07 +0300708
709 UFSD("EXIT\n");
Fabian Frederickcdd9eef2015-06-10 10:09:32 +1000710 mutex_unlock(&UFS_SB(sb)->s_lock);
Artem Bityutskiy7bd54ef72012-07-12 16:28:07 +0300711
712 return 0;
713}
714
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300715static void delayed_sync_fs(struct work_struct *work)
Artem Bityutskiy7bd54ef72012-07-12 16:28:07 +0300716{
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300717 struct ufs_sb_info *sbi;
718
719 sbi = container_of(work, struct ufs_sb_info, sync_work.work);
720
721 spin_lock(&sbi->work_lock);
722 sbi->work_queued = 0;
723 spin_unlock(&sbi->work_lock);
724
725 ufs_sync_fs(sbi->sb, 1);
726}
727
728void ufs_mark_sb_dirty(struct super_block *sb)
729{
730 struct ufs_sb_info *sbi = UFS_SB(sb);
731 unsigned long delay;
732
733 spin_lock(&sbi->work_lock);
734 if (!sbi->work_queued) {
735 delay = msecs_to_jiffies(dirty_writeback_interval * 10);
736 queue_delayed_work(system_long_wq, &sbi->sync_work, delay);
737 sbi->work_queued = 1;
738 }
739 spin_unlock(&sbi->work_lock);
Artem Bityutskiy7bd54ef72012-07-12 16:28:07 +0300740}
741
742static void ufs_put_super(struct super_block *sb)
743{
744 struct ufs_sb_info * sbi = UFS_SB(sb);
745
746 UFSD("ENTER\n");
747
David Howellsbc98a422017-07-17 08:45:34 +0100748 if (!sb_rdonly(sb))
Artem Bityutskiy7bd54ef72012-07-12 16:28:07 +0300749 ufs_put_super_internal(sb);
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300750 cancel_delayed_work_sync(&sbi->sync_work);
Artem Bityutskiy7bd54ef72012-07-12 16:28:07 +0300751
752 ubh_brelse_uspi (sbi->s_uspi);
753 kfree (sbi->s_uspi);
754 kfree (sbi);
755 sb->s_fs_info = NULL;
756 UFSD("EXIT\n");
757 return;
758}
759
Al Viro6b0d1442017-06-08 21:15:45 -0400760static u64 ufs_max_bytes(struct super_block *sb)
761{
762 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
763 int bits = uspi->s_apbshift;
764 u64 res;
765
766 if (bits > 21)
767 res = ~0ULL;
768 else
769 res = UFS_NDADDR + (1LL << bits) + (1LL << (2*bits)) +
770 (1LL << (3*bits));
771
772 if (res >= (MAX_LFS_FILESIZE >> uspi->s_bshift))
773 return MAX_LFS_FILESIZE;
774 return res << uspi->s_bshift;
775}
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777static int ufs_fill_super(struct super_block *sb, void *data, int silent)
778{
779 struct ufs_sb_info * sbi;
780 struct ufs_sb_private_info * uspi;
781 struct ufs_super_block_first * usb1;
782 struct ufs_super_block_second * usb2;
783 struct ufs_super_block_third * usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 struct ufs_buffer_head * ubh;
785 struct inode *inode;
786 unsigned block_size, super_block_size;
787 unsigned flags;
Evgeniy7b4ee732006-01-14 11:42:06 +0300788 unsigned super_block_offset;
Duane Griffin9e6766c2009-01-08 22:43:48 +0000789 unsigned maxsymlen;
David Howellsb55c4602008-02-07 00:15:48 -0800790 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 uspi = NULL;
793 ubh = NULL;
794 flags = 0;
795
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700796 UFSD("ENTER\n");
Fabian Frederick02447562014-06-06 14:38:34 -0700797
798#ifndef CONFIG_UFS_FS_WRITE
David Howellsbc98a422017-07-17 08:45:34 +0100799 if (!sb_rdonly(sb)) {
Fabian Fredericka9814c52014-08-08 14:20:57 -0700800 pr_err("ufs was compiled with read-only support, can't be mounted as read-write\n");
Fabian Frederick02447562014-06-06 14:38:34 -0700801 return -EROFS;
802 }
803#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700805 sbi = kzalloc(sizeof(struct ufs_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (!sbi)
807 goto failed_nomem;
808 sb->s_fs_info = sbi;
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300809 sbi->sb = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
David Howellsbc98a422017-07-17 08:45:34 +0100811 UFSD("flag %u\n", (int)(sb_rdonly(sb)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Fabian Fredericke4f95512015-06-17 18:15:45 +0200813 mutex_init(&sbi->s_lock);
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300814 spin_lock_init(&sbi->work_lock);
815 INIT_DELAYED_WORK(&sbi->sync_work, delayed_sync_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /*
817 * Set default mount options
818 * Parse mount options
819 */
820 sbi->s_mount_opt = 0;
821 ufs_set_opt (sbi->s_mount_opt, ONERROR_LOCK);
822 if (!ufs_parse_options ((char *) data, &sbi->s_mount_opt)) {
Fabian Fredericka9814c52014-08-08 14:20:57 -0700823 pr_err("wrong mount options\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 goto failed;
825 }
826 if (!(sbi->s_mount_opt & UFS_MOUNT_UFSTYPE)) {
827 if (!silent)
Fabian Fredericka9814c52014-08-08 14:20:57 -0700828 pr_err("You didn't specify the type of your ufs filesystem\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 "mount -t ufs -o ufstype="
Michael Owenc690a722006-03-24 18:21:44 +0100830 "sun|sunx86|44bsd|ufs2|5xbsd|old|hp|nextstep|nextstep-cd|openstep ...\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 ">>>WARNING<<< Wrong ufstype may corrupt your filesystem, "
832 "default is ufstype=old\n");
833 ufs_set_opt (sbi->s_mount_opt, UFSTYPE_OLD);
834 }
835
Mark Fortescue252e2112007-10-16 23:26:31 -0700836 uspi = kzalloc(sizeof(struct ufs_sb_private_info), GFP_KERNEL);
837 sbi->s_uspi = uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (!uspi)
839 goto failed;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800840 uspi->s_dirblksize = UFS_SECTOR_SIZE;
Evgeniy7b4ee732006-01-14 11:42:06 +0300841 super_block_offset=UFS_SBLOCK;
842
Richard Narron239e2502017-06-04 16:23:18 -0700843 sb->s_maxbytes = MAX_LFS_FILESIZE;
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 switch (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) {
846 case UFS_MOUNT_UFSTYPE_44BSD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700847 UFSD("ufstype=44bsd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 uspi->s_fsize = block_size = 512;
849 uspi->s_fmask = ~(512 - 1);
850 uspi->s_fshift = 9;
851 uspi->s_sbsize = super_block_size = 1536;
852 uspi->s_sbbase = 0;
853 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
854 break;
855 case UFS_MOUNT_UFSTYPE_UFS2:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700856 UFSD("ufstype=ufs2\n");
Evgeniy7b4ee732006-01-14 11:42:06 +0300857 super_block_offset=SBLOCK_UFS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 uspi->s_fsize = block_size = 512;
859 uspi->s_fmask = ~(512 - 1);
860 uspi->s_fshift = 9;
861 uspi->s_sbsize = super_block_size = 1536;
862 uspi->s_sbbase = 0;
863 flags |= UFS_TYPE_UFS2 | UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 break;
865
866 case UFS_MOUNT_UFSTYPE_SUN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700867 UFSD("ufstype=sun\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 uspi->s_fsize = block_size = 1024;
869 uspi->s_fmask = ~(1024 - 1);
870 uspi->s_fshift = 10;
871 uspi->s_sbsize = super_block_size = 2048;
872 uspi->s_sbbase = 0;
Mark Fortescue252e2112007-10-16 23:26:31 -0700873 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUN | UFS_CG_SUN;
875 break;
876
Mark Fortescue252e2112007-10-16 23:26:31 -0700877 case UFS_MOUNT_UFSTYPE_SUNOS:
Fabian Frederickd4beaab2014-08-08 14:21:05 -0700878 UFSD("ufstype=sunos\n");
Mark Fortescue252e2112007-10-16 23:26:31 -0700879 uspi->s_fsize = block_size = 1024;
880 uspi->s_fmask = ~(1024 - 1);
881 uspi->s_fshift = 10;
882 uspi->s_sbsize = 2048;
883 super_block_size = 2048;
884 uspi->s_sbbase = 0;
885 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
886 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_SUNOS | UFS_CG_SUN;
887 break;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 case UFS_MOUNT_UFSTYPE_SUNx86:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700890 UFSD("ufstype=sunx86\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 uspi->s_fsize = block_size = 1024;
892 uspi->s_fmask = ~(1024 - 1);
893 uspi->s_fshift = 10;
894 uspi->s_sbsize = super_block_size = 2048;
895 uspi->s_sbbase = 0;
Mark Fortescue252e2112007-10-16 23:26:31 -0700896 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUNx86 | UFS_CG_SUN;
898 break;
899
900 case UFS_MOUNT_UFSTYPE_OLD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700901 UFSD("ufstype=old\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 uspi->s_fsize = block_size = 1024;
903 uspi->s_fmask = ~(1024 - 1);
904 uspi->s_fshift = 10;
905 uspi->s_sbsize = super_block_size = 2048;
906 uspi->s_sbbase = 0;
907 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
David Howellsbc98a422017-07-17 08:45:34 +0100908 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (!silent)
Fabian Fredericka9814c52014-08-08 14:20:57 -0700910 pr_info("ufstype=old is supported read-only\n");
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800911 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913 break;
914
915 case UFS_MOUNT_UFSTYPE_NEXTSTEP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700916 UFSD("ufstype=nextstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 uspi->s_fsize = block_size = 1024;
918 uspi->s_fmask = ~(1024 - 1);
919 uspi->s_fshift = 10;
920 uspi->s_sbsize = super_block_size = 2048;
921 uspi->s_sbbase = 0;
Evgeniy Dushistov0c664f92007-12-04 23:45:06 -0800922 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
David Howellsbc98a422017-07-17 08:45:34 +0100924 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (!silent)
Fabian Fredericka9814c52014-08-08 14:20:57 -0700926 pr_info("ufstype=nextstep is supported read-only\n");
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800927 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929 break;
930
931 case UFS_MOUNT_UFSTYPE_NEXTSTEP_CD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700932 UFSD("ufstype=nextstep-cd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 uspi->s_fsize = block_size = 2048;
934 uspi->s_fmask = ~(2048 - 1);
935 uspi->s_fshift = 11;
936 uspi->s_sbsize = super_block_size = 2048;
937 uspi->s_sbbase = 0;
Evgeniy Dushistov0c664f92007-12-04 23:45:06 -0800938 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
David Howellsbc98a422017-07-17 08:45:34 +0100940 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (!silent)
Fabian Fredericka9814c52014-08-08 14:20:57 -0700942 pr_info("ufstype=nextstep-cd is supported read-only\n");
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800943 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945 break;
946
947 case UFS_MOUNT_UFSTYPE_OPENSTEP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700948 UFSD("ufstype=openstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 uspi->s_fsize = block_size = 1024;
950 uspi->s_fmask = ~(1024 - 1);
951 uspi->s_fshift = 10;
952 uspi->s_sbsize = super_block_size = 2048;
953 uspi->s_sbbase = 0;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800954 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
David Howellsbc98a422017-07-17 08:45:34 +0100956 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (!silent)
Fabian Fredericka9814c52014-08-08 14:20:57 -0700958 pr_info("ufstype=openstep is supported read-only\n");
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800959 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961 break;
962
963 case UFS_MOUNT_UFSTYPE_HP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700964 UFSD("ufstype=hp\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 uspi->s_fsize = block_size = 1024;
966 uspi->s_fmask = ~(1024 - 1);
967 uspi->s_fshift = 10;
968 uspi->s_sbsize = super_block_size = 2048;
969 uspi->s_sbbase = 0;
970 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
David Howellsbc98a422017-07-17 08:45:34 +0100971 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (!silent)
Fabian Fredericka9814c52014-08-08 14:20:57 -0700973 pr_info("ufstype=hp is supported read-only\n");
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800974 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976 break;
977 default:
978 if (!silent)
Fabian Fredericka9814c52014-08-08 14:20:57 -0700979 pr_err("unknown ufstype\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 goto failed;
981 }
982
983again:
984 if (!sb_set_blocksize(sb, block_size)) {
Fabian Frederickde771bd2014-08-08 14:20:59 -0700985 pr_err("failed to set blocksize\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 goto failed;
987 }
988
989 /*
990 * read ufs super block from device
991 */
Evgeniy7b4ee732006-01-14 11:42:06 +0300992
993 ubh = ubh_bread_uspi(uspi, sb, uspi->s_sbbase + super_block_offset/block_size, super_block_size);
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (!ubh)
996 goto failed;
997
Evgeniy7b4ee732006-01-14 11:42:06 +0300998 usb1 = ubh_get_usb_first(uspi);
999 usb2 = ubh_get_usb_second(uspi);
1000 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Mark Fortescue252e2112007-10-16 23:26:31 -07001002 /* Sort out mod used on SunOS 4.1.3 for fs_state */
1003 uspi->s_postblformat = fs32_to_cpu(sb, usb3->fs_postblformat);
1004 if (((flags & UFS_ST_MASK) == UFS_ST_SUNOS) &&
1005 (uspi->s_postblformat != UFS_42POSTBLFMT)) {
1006 flags &= ~UFS_ST_MASK;
1007 flags |= UFS_ST_SUN;
1008 }
1009
Al Viro96ecff12017-06-14 15:17:32 -04001010 if ((flags & UFS_ST_MASK) == UFS_ST_44BSD &&
1011 uspi->s_postblformat == UFS_42POSTBLFMT) {
1012 if (!silent)
1013 pr_err("this is not a 44bsd filesystem");
1014 goto failed;
1015 }
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 /*
1018 * Check ufs magic number
1019 */
1020 sbi->s_bytesex = BYTESEX_LE;
1021 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
1022 case UFS_MAGIC:
Thomas Stewartd27d7a92010-05-26 14:42:33 -07001023 case UFS_MAGIC_BW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 case UFS2_MAGIC:
1025 case UFS_MAGIC_LFN:
1026 case UFS_MAGIC_FEA:
1027 case UFS_MAGIC_4GB:
1028 goto magic_found;
1029 }
1030 sbi->s_bytesex = BYTESEX_BE;
1031 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
1032 case UFS_MAGIC:
Thomas Stewartd27d7a92010-05-26 14:42:33 -07001033 case UFS_MAGIC_BW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 case UFS2_MAGIC:
1035 case UFS_MAGIC_LFN:
1036 case UFS_MAGIC_FEA:
1037 case UFS_MAGIC_4GB:
1038 goto magic_found;
1039 }
1040
1041 if ((((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP)
1042 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP_CD)
1043 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_OPENSTEP))
1044 && uspi->s_sbbase < 256) {
1045 ubh_brelse_uspi(uspi);
1046 ubh = NULL;
1047 uspi->s_sbbase += 8;
1048 goto again;
1049 }
1050 if (!silent)
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001051 pr_err("%s(): bad magic number\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 goto failed;
1053
1054magic_found:
1055 /*
1056 * Check block and fragment sizes
1057 */
1058 uspi->s_bsize = fs32_to_cpu(sb, usb1->fs_bsize);
1059 uspi->s_fsize = fs32_to_cpu(sb, usb1->fs_fsize);
1060 uspi->s_sbsize = fs32_to_cpu(sb, usb1->fs_sbsize);
1061 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
1062 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
1063
vignesh babu9e8c4272007-07-15 23:41:36 -07001064 if (!is_power_of_2(uspi->s_fsize)) {
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001065 pr_err("%s(): fragment size %u is not a power of 2\n",
1066 __func__, uspi->s_fsize);
Fabian Fredericka9814c52014-08-08 14:20:57 -07001067 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069 if (uspi->s_fsize < 512) {
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001070 pr_err("%s(): fragment size %u is too small\n",
1071 __func__, uspi->s_fsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 goto failed;
1073 }
1074 if (uspi->s_fsize > 4096) {
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001075 pr_err("%s(): fragment size %u is too large\n",
1076 __func__, uspi->s_fsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 goto failed;
1078 }
vignesh babu9e8c4272007-07-15 23:41:36 -07001079 if (!is_power_of_2(uspi->s_bsize)) {
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001080 pr_err("%s(): block size %u is not a power of 2\n",
1081 __func__, uspi->s_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 goto failed;
1083 }
1084 if (uspi->s_bsize < 4096) {
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001085 pr_err("%s(): block size %u is too small\n",
1086 __func__, uspi->s_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 goto failed;
1088 }
1089 if (uspi->s_bsize / uspi->s_fsize > 8) {
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001090 pr_err("%s(): too many fragments per block (%u)\n",
1091 __func__, uspi->s_bsize / uspi->s_fsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 goto failed;
1093 }
1094 if (uspi->s_fsize != block_size || uspi->s_sbsize != super_block_size) {
1095 ubh_brelse_uspi(uspi);
1096 ubh = NULL;
1097 block_size = uspi->s_fsize;
1098 super_block_size = uspi->s_sbsize;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001099 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 -07001100 goto again;
1101 }
1102
Andrew Mortonf664f1f2007-10-29 14:37:21 -07001103 sbi->s_flags = flags;/*after that line some functions use s_flags*/
Evgeniy Dushistov3313e292007-02-12 00:54:31 -08001104 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 /*
1107 * Check, if file system was correctly unmounted.
1108 * If not, make it read only.
1109 */
Andrew Mortonf664f1f2007-10-29 14:37:21 -07001110 if (((flags & UFS_ST_MASK) == UFS_ST_44BSD) ||
1111 ((flags & UFS_ST_MASK) == UFS_ST_OLD) ||
1112 (((flags & UFS_ST_MASK) == UFS_ST_SUN ||
1113 (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
1114 (flags & UFS_ST_MASK) == UFS_ST_SUNx86) &&
1115 (ufs_get_fs_state(sb, usb1, usb3) == (UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 switch(usb1->fs_clean) {
1117 case UFS_FSCLEAN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001118 UFSD("fs is clean\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 break;
1120 case UFS_FSSTABLE:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001121 UFSD("fs is stable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 break;
Alex Viskovatoffb3a0fd42010-03-10 15:21:53 -08001123 case UFS_FSLOG:
1124 UFSD("fs is logging fs\n");
1125 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 case UFS_FSOSF1:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001127 UFSD("fs is DEC OSF/1\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 break;
1129 case UFS_FSACTIVE:
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001130 pr_err("%s(): fs is active\n", __func__);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001131 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 break;
1133 case UFS_FSBAD:
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001134 pr_err("%s(): fs is bad\n", __func__);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001135 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 break;
1137 default:
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001138 pr_err("%s(): can't grok fs_clean 0x%x\n",
1139 __func__, usb1->fs_clean);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001140 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 break;
1142 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001143 } else {
Fabian Frederick07bc94f2014-08-08 14:21:01 -07001144 pr_err("%s(): fs needs fsck\n", __func__);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001145 sb->s_flags |= SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147
1148 /*
1149 * Read ufs_super_block into internal data structures
1150 */
1151 sb->s_op = &ufs_super_ops;
Alexey Dobriyanf3e2a522009-12-15 16:46:51 -08001152 sb->s_export_op = &ufs_export_ops;
Christoph Hellwig123e9ca2010-05-19 07:16:44 -04001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 sb->s_magic = fs32_to_cpu(sb, usb3->fs_magic);
1155
1156 uspi->s_sblkno = fs32_to_cpu(sb, usb1->fs_sblkno);
1157 uspi->s_cblkno = fs32_to_cpu(sb, usb1->fs_cblkno);
1158 uspi->s_iblkno = fs32_to_cpu(sb, usb1->fs_iblkno);
1159 uspi->s_dblkno = fs32_to_cpu(sb, usb1->fs_dblkno);
1160 uspi->s_cgoffset = fs32_to_cpu(sb, usb1->fs_cgoffset);
1161 uspi->s_cgmask = fs32_to_cpu(sb, usb1->fs_cgmask);
1162
1163 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
Al Viroc5969612017-06-14 16:36:29 -04001164 uspi->s_size = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size);
1165 uspi->s_dsize = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001166 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 uspi->s_size = fs32_to_cpu(sb, usb1->fs_size);
1168 uspi->s_dsize = fs32_to_cpu(sb, usb1->fs_dsize);
1169 }
1170
1171 uspi->s_ncg = fs32_to_cpu(sb, usb1->fs_ncg);
1172 /* s_bsize already set */
1173 /* s_fsize already set */
1174 uspi->s_fpb = fs32_to_cpu(sb, usb1->fs_frag);
1175 uspi->s_minfree = fs32_to_cpu(sb, usb1->fs_minfree);
1176 uspi->s_bmask = fs32_to_cpu(sb, usb1->fs_bmask);
1177 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
1178 uspi->s_bshift = fs32_to_cpu(sb, usb1->fs_bshift);
1179 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001180 UFSD("uspi->s_bshift = %d,uspi->s_fshift = %d", uspi->s_bshift,
1181 uspi->s_fshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 uspi->s_fpbshift = fs32_to_cpu(sb, usb1->fs_fragshift);
1183 uspi->s_fsbtodb = fs32_to_cpu(sb, usb1->fs_fsbtodb);
1184 /* s_sbsize already set */
1185 uspi->s_csmask = fs32_to_cpu(sb, usb1->fs_csmask);
1186 uspi->s_csshift = fs32_to_cpu(sb, usb1->fs_csshift);
1187 uspi->s_nindir = fs32_to_cpu(sb, usb1->fs_nindir);
1188 uspi->s_inopb = fs32_to_cpu(sb, usb1->fs_inopb);
1189 uspi->s_nspf = fs32_to_cpu(sb, usb1->fs_nspf);
1190 uspi->s_npsect = ufs_get_fs_npsect(sb, usb1, usb3);
1191 uspi->s_interleave = fs32_to_cpu(sb, usb1->fs_interleave);
1192 uspi->s_trackskew = fs32_to_cpu(sb, usb1->fs_trackskew);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -08001193
1194 if (uspi->fs_magic == UFS2_MAGIC)
1195 uspi->s_csaddr = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_csaddr);
1196 else
1197 uspi->s_csaddr = fs32_to_cpu(sb, usb1->fs_csaddr);
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 uspi->s_cssize = fs32_to_cpu(sb, usb1->fs_cssize);
1200 uspi->s_cgsize = fs32_to_cpu(sb, usb1->fs_cgsize);
1201 uspi->s_ntrak = fs32_to_cpu(sb, usb1->fs_ntrak);
1202 uspi->s_nsect = fs32_to_cpu(sb, usb1->fs_nsect);
1203 uspi->s_spc = fs32_to_cpu(sb, usb1->fs_spc);
1204 uspi->s_ipg = fs32_to_cpu(sb, usb1->fs_ipg);
1205 uspi->s_fpg = fs32_to_cpu(sb, usb1->fs_fpg);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001206 uspi->s_cpc = fs32_to_cpu(sb, usb2->fs_un.fs_u1.fs_cpc);
1207 uspi->s_contigsumsize = fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_contigsumsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 uspi->s_qbmask = ufs_get_fs_qbmask(sb, usb3);
1209 uspi->s_qfmask = ufs_get_fs_qfmask(sb, usb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 uspi->s_nrpos = fs32_to_cpu(sb, usb3->fs_nrpos);
1211 uspi->s_postbloff = fs32_to_cpu(sb, usb3->fs_postbloff);
1212 uspi->s_rotbloff = fs32_to_cpu(sb, usb3->fs_rotbloff);
1213
Al Viroc5969612017-06-14 16:36:29 -04001214 uspi->s_root_blocks = mul_u64_u32_div(uspi->s_dsize,
1215 uspi->s_minfree, 100);
Al Viro77e9ce32017-06-17 15:44:06 -04001216 if (uspi->s_minfree <= 5) {
1217 uspi->s_time_to_space = ~0ULL;
1218 uspi->s_space_to_time = 0;
1219 usb1->fs_optim = cpu_to_fs32(sb, UFS_OPTSPACE);
1220 } else {
1221 uspi->s_time_to_space = (uspi->s_root_blocks / 2) + 1;
1222 uspi->s_space_to_time = mul_u64_u32_div(uspi->s_dsize,
1223 uspi->s_minfree - 2, 100) - 1;
1224 }
Al Viroc5969612017-06-14 16:36:29 -04001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 /*
1227 * Compute another frequently used values
1228 */
1229 uspi->s_fpbmask = uspi->s_fpb - 1;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001230 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 uspi->s_apbshift = uspi->s_bshift - 3;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001232 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 uspi->s_apbshift = uspi->s_bshift - 2;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 uspi->s_2apbshift = uspi->s_apbshift * 2;
1236 uspi->s_3apbshift = uspi->s_apbshift * 3;
1237 uspi->s_apb = 1 << uspi->s_apbshift;
1238 uspi->s_2apb = 1 << uspi->s_2apbshift;
1239 uspi->s_3apb = 1 << uspi->s_3apbshift;
1240 uspi->s_apbmask = uspi->s_apb - 1;
1241 uspi->s_nspfshift = uspi->s_fshift - UFS_SECTOR_BITS;
1242 uspi->s_nspb = uspi->s_nspf << uspi->s_fpbshift;
1243 uspi->s_inopf = uspi->s_inopb >> uspi->s_fpbshift;
1244 uspi->s_bpf = uspi->s_fsize << 3;
1245 uspi->s_bpfshift = uspi->s_fshift + 3;
1246 uspi->s_bpfmask = uspi->s_bpf - 1;
Evgeniy Dushistov90b315a2008-02-08 04:20:16 -08001247 if ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_44BSD ||
1248 (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 uspi->s_maxsymlinklen =
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001250 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Duane Griffin9e6766c2009-01-08 22:43:48 +00001252 if (uspi->fs_magic == UFS2_MAGIC)
1253 maxsymlen = 2 * 4 * (UFS_NDADDR + UFS_NINDIR);
1254 else
1255 maxsymlen = 4 * (UFS_NDADDR + UFS_NINDIR);
1256 if (uspi->s_maxsymlinklen > maxsymlen) {
1257 ufs_warning(sb, __func__, "ufs_read_super: excessive maximum "
1258 "fast symlink size (%u)\n", uspi->s_maxsymlinklen);
1259 uspi->s_maxsymlinklen = maxsymlen;
1260 }
Al Viro6b0d1442017-06-08 21:15:45 -04001261 sb->s_maxbytes = ufs_max_bytes(sb);
Al Viro8de52772012-02-06 12:45:27 -05001262 sb->s_max_links = UFS_LINK_MAX;
Duane Griffin9e6766c2009-01-08 22:43:48 +00001263
David Howellsb55c4602008-02-07 00:15:48 -08001264 inode = ufs_iget(sb, UFS_ROOTINO);
1265 if (IS_ERR(inode)) {
1266 ret = PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 goto failed;
David Howellsb55c4602008-02-07 00:15:48 -08001268 }
Al Viro48fde702012-01-08 22:15:13 -05001269 sb->s_root = d_make_root(inode);
David Howellsb55c4602008-02-07 00:15:48 -08001270 if (!sb->s_root) {
1271 ret = -ENOMEM;
Al Viro48fde702012-01-08 22:15:13 -05001272 goto failed;
David Howellsb55c4602008-02-07 00:15:48 -08001273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001275 ufs_setup_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 /*
1277 * Read cylinder group structures
1278 */
David Howellsbc98a422017-07-17 08:45:34 +01001279 if (!sb_rdonly(sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (!ufs_read_cylinder_structures(sb))
1281 goto failed;
1282
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001283 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return 0;
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -08001287 if (ubh)
1288 ubh_brelse_uspi (uspi);
1289 kfree (uspi);
1290 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 sb->s_fs_info = NULL;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001292 UFSD("EXIT (FAILED)\n");
David Howellsb55c4602008-02-07 00:15:48 -08001293 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295failed_nomem:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001296 UFSD("EXIT (NOMEM)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return -ENOMEM;
1298}
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300static int ufs_remount (struct super_block *sb, int *mount_flags, char *data)
1301{
1302 struct ufs_sb_private_info * uspi;
1303 struct ufs_super_block_first * usb1;
1304 struct ufs_super_block_third * usb3;
1305 unsigned new_mount_opt, ufstype;
1306 unsigned flags;
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001307
Theodore Ts'o02b99842014-03-13 10:14:33 -04001308 sync_filesystem(sb);
Fabian Frederickcdd9eef2015-06-10 10:09:32 +10001309 mutex_lock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 uspi = UFS_SB(sb)->s_uspi;
1311 flags = UFS_SB(sb)->s_flags;
Evgeniy7b4ee732006-01-14 11:42:06 +03001312 usb1 = ubh_get_usb_first(uspi);
1313 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 /*
1316 * Allow the "check" option to be passed as a remount option.
1317 * It is not possible to change ufstype option during remount
1318 */
1319 ufstype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
1320 new_mount_opt = 0;
1321 ufs_set_opt (new_mount_opt, ONERROR_LOCK);
Al Virobbd68512009-05-06 10:43:07 -04001322 if (!ufs_parse_options (data, &new_mount_opt)) {
Fabian Frederickcdd9eef2015-06-10 10:09:32 +10001323 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return -EINVAL;
Al Virobbd68512009-05-06 10:43:07 -04001325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (!(new_mount_opt & UFS_MOUNT_UFSTYPE)) {
1327 new_mount_opt |= ufstype;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001328 } else if ((new_mount_opt & UFS_MOUNT_UFSTYPE) != ufstype) {
Fabian Fredericka9814c52014-08-08 14:20:57 -07001329 pr_err("ufstype can't be changed during remount\n");
Fabian Frederickcdd9eef2015-06-10 10:09:32 +10001330 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return -EINVAL;
1332 }
1333
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001334 if ((bool)(*mount_flags & SB_RDONLY) == sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 UFS_SB(sb)->s_mount_opt = new_mount_opt;
Fabian Frederickcdd9eef2015-06-10 10:09:32 +10001336 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return 0;
1338 }
1339
1340 /*
1341 * fs was mouted as rw, remounting ro
1342 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001343 if (*mount_flags & SB_RDONLY) {
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001344 ufs_put_super_internal(sb);
Arnd Bergmanna3fda0f2018-08-17 15:43:47 -07001345 usb1->fs_time = ufs_get_seconds(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
Mark Fortescue252e2112007-10-16 23:26:31 -07001347 || (flags & UFS_ST_MASK) == UFS_ST_SUNOS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1349 ufs_set_fs_state(sb, usb1, usb3,
1350 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -07001351 ubh_mark_buffer_dirty (USPI_UBH(uspi));
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001352 sb->s_flags |= SB_RDONLY;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001353 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /*
1355 * fs was mounted as ro, remounting rw
1356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357#ifndef CONFIG_UFS_FS_WRITE
Fabian Fredericka9814c52014-08-08 14:20:57 -07001358 pr_err("ufs was compiled with read-only support, can't be mounted as read-write\n");
Fabian Frederickcdd9eef2015-06-10 10:09:32 +10001359 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return -EINVAL;
1361#else
1362 if (ufstype != UFS_MOUNT_UFSTYPE_SUN &&
Mark Fortescue252e2112007-10-16 23:26:31 -07001363 ufstype != UFS_MOUNT_UFSTYPE_SUNOS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 ufstype != UFS_MOUNT_UFSTYPE_44BSD &&
Evgeniy Dushistovcbcae392007-02-12 00:54:30 -08001365 ufstype != UFS_MOUNT_UFSTYPE_SUNx86 &&
1366 ufstype != UFS_MOUNT_UFSTYPE_UFS2) {
Fabian Fredericka9814c52014-08-08 14:20:57 -07001367 pr_err("this ufstype is read-only supported\n");
Fabian Frederickcdd9eef2015-06-10 10:09:32 +10001368 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return -EINVAL;
1370 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001371 if (!ufs_read_cylinder_structures(sb)) {
Fabian Fredericka9814c52014-08-08 14:20:57 -07001372 pr_err("failed during remounting\n");
Fabian Frederickcdd9eef2015-06-10 10:09:32 +10001373 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return -EPERM;
1375 }
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001376 sb->s_flags &= ~SB_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377#endif
1378 }
1379 UFS_SB(sb)->s_mount_opt = new_mount_opt;
Fabian Frederickcdd9eef2015-06-10 10:09:32 +10001380 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return 0;
1382}
1383
Al Viro34c80b12011-12-08 21:32:45 -05001384static int ufs_show_options(struct seq_file *seq, struct dentry *root)
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001385{
Al Viro34c80b12011-12-08 21:32:45 -05001386 struct ufs_sb_info *sbi = UFS_SB(root->d_sb);
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001387 unsigned mval = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
Steven Whitehousea447c092008-10-13 10:46:57 +01001388 const struct match_token *tp = tokens;
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001389
1390 while (tp->token != Opt_onerror_panic && tp->token != mval)
1391 ++tp;
1392 BUG_ON(tp->token == Opt_onerror_panic);
1393 seq_printf(seq, ",%s", tp->pattern);
1394
1395 mval = sbi->s_mount_opt & UFS_MOUNT_ONERROR;
1396 while (tp->token != Opt_err && tp->token != mval)
1397 ++tp;
1398 BUG_ON(tp->token == Opt_err);
1399 seq_printf(seq, ",%s", tp->pattern);
1400
1401 return 0;
1402}
1403
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001404static int ufs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
David Howells726c3342006-06-23 02:02:58 -07001406 struct super_block *sb = dentry->d_sb;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001407 struct ufs_sb_private_info *uspi= UFS_SB(sb)->s_uspi;
1408 unsigned flags = UFS_SB(sb)->s_flags;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001409 struct ufs_super_block_third *usb3;
Coly Li41d577a2009-04-02 16:59:44 -07001410 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Al Virodff7cfd2015-06-16 04:27:05 -04001412 mutex_lock(&UFS_SB(sb)->s_lock);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001413 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Al Viroc5969612017-06-14 16:36:29 -04001415 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 buf->f_type = UFS2_MAGIC;
Al Viroc5969612017-06-14 16:36:29 -04001417 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 buf->f_type = UFS_MAGIC;
Al Viroc5969612017-06-14 16:36:29 -04001419
1420 buf->f_blocks = uspi->s_dsize;
1421 buf->f_bfree = ufs_freefrags(uspi);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001422 buf->f_ffree = uspi->cs_total.cs_nifree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 buf->f_bsize = sb->s_blocksize;
Al Viroc5969612017-06-14 16:36:29 -04001424 buf->f_bavail = (buf->f_bfree > uspi->s_root_blocks)
1425 ? (buf->f_bfree - uspi->s_root_blocks) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 buf->f_files = uspi->s_ncg * uspi->s_ipg;
1427 buf->f_namelen = UFS_MAXNAMLEN;
Coly Li41d577a2009-04-02 16:59:44 -07001428 buf->f_fsid.val[0] = (u32)id;
1429 buf->f_fsid.val[1] = (u32)(id >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Al Virodff7cfd2015-06-16 04:27:05 -04001431 mutex_unlock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 return 0;
1434}
1435
Christoph Lametere18b8902006-12-06 20:33:20 -08001436static struct kmem_cache * ufs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438static struct inode *ufs_alloc_inode(struct super_block *sb)
1439{
1440 struct ufs_inode_info *ei;
Fabian Frederick61da3ae2015-02-17 13:45:28 -08001441
1442 ei = kmem_cache_alloc(ufs_inode_cachep, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (!ei)
1444 return NULL;
Fabian Frederick61da3ae2015-02-17 13:45:28 -08001445
Jeff Laytonbb8c2d62017-12-11 06:35:18 -05001446 inode_set_iversion(&ei->vfs_inode, 1);
Al Viro724bb092015-06-17 12:02:56 -04001447 seqlock_init(&ei->meta_lock);
1448 mutex_init(&ei->truncate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return &ei->vfs_inode;
1450}
1451
Al Viro98835e82019-04-15 22:29:33 -04001452static void ufs_free_in_core_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001453{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001454 kmem_cache_free(ufs_inode_cachep, UFS_I(inode));
1455}
1456
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001457static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 struct ufs_inode_info *ei = (struct ufs_inode_info *) foo;
1460
Christoph Lametera35afb82007-05-16 22:10:57 -07001461 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
Paul Mundt20c2df82007-07-20 10:11:58 +09001463
Fabian Frederick76ee4732014-04-07 15:39:55 -07001464static int __init init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
David Windsordf5f3cf2017-06-10 22:50:40 -04001466 ufs_inode_cachep = kmem_cache_create_usercopy("ufs_inode_cache",
1467 sizeof(struct ufs_inode_info), 0,
1468 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
1469 SLAB_ACCOUNT),
1470 offsetof(struct ufs_inode_info, i_u1.i_symlink),
1471 sizeof_field(struct ufs_inode_info,
1472 i_u1.i_symlink),
1473 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (ufs_inode_cachep == NULL)
1475 return -ENOMEM;
1476 return 0;
1477}
1478
1479static void destroy_inodecache(void)
1480{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10001481 /*
1482 * Make sure all delayed rcu free inodes are flushed before we
1483 * destroy cache.
1484 */
1485 rcu_barrier();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001486 kmem_cache_destroy(ufs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001489static const struct super_operations ufs_super_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 .alloc_inode = ufs_alloc_inode,
Al Viro98835e82019-04-15 22:29:33 -04001491 .free_inode = ufs_free_in_core_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 .write_inode = ufs_write_inode,
Al Viro58e82682010-06-05 19:40:56 -04001493 .evict_inode = ufs_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 .put_super = ufs_put_super,
Christoph Hellwig8c800652009-06-08 10:08:05 +02001495 .sync_fs = ufs_sync_fs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 .statfs = ufs_statfs,
1497 .remount_fs = ufs_remount,
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001498 .show_options = ufs_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499};
1500
Al Viro152a0832010-07-25 00:46:55 +04001501static struct dentry *ufs_mount(struct file_system_type *fs_type,
1502 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Al Viro152a0832010-07-25 00:46:55 +04001504 return mount_bdev(fs_type, flags, dev_name, data, ufs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
1507static struct file_system_type ufs_fs_type = {
1508 .owner = THIS_MODULE,
1509 .name = "ufs",
Al Viro152a0832010-07-25 00:46:55 +04001510 .mount = ufs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 .kill_sb = kill_block_super,
1512 .fs_flags = FS_REQUIRES_DEV,
1513};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001514MODULE_ALIAS_FS("ufs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516static int __init init_ufs_fs(void)
1517{
1518 int err = init_inodecache();
1519 if (err)
1520 goto out1;
1521 err = register_filesystem(&ufs_fs_type);
1522 if (err)
1523 goto out;
1524 return 0;
1525out:
1526 destroy_inodecache();
1527out1:
1528 return err;
1529}
1530
1531static void __exit exit_ufs_fs(void)
1532{
1533 unregister_filesystem(&ufs_fs_type);
1534 destroy_inodecache();
1535}
1536
1537module_init(init_ufs_fs)
1538module_exit(exit_ufs_fs)
1539MODULE_LICENSE("GPL");