blob: ac8e279eccc668dcf1f6cd55c942569b9830e560 [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
68
Alexey Dobriyanf3e2a522009-12-15 16:46:51 -080069#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/module.h>
71#include <linux/bitops.h>
72
73#include <stdarg.h>
74
75#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#include <linux/errno.h>
78#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/slab.h>
80#include <linux/time.h>
81#include <linux/stat.h>
82#include <linux/string.h>
83#include <linux/blkdev.h>
84#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 Dushistov2a9807c02007-10-16 23:26:32 -070089#include <linux/mount.h>
90#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Mike Frysingere5420592008-02-08 04:21:31 -080092#include "ufs_fs.h"
Christoph Hellwigbcd6d4e2007-10-16 23:26:51 -070093#include "ufs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include "swab.h"
95#include "util.h"
96
Arnd Bergmann788257d2011-01-24 10:14:12 +010097void lock_ufs(struct super_block *sb)
98{
99#if defined(CONFIG_SMP) || defined (CONFIG_PREEMPT)
100 struct ufs_sb_info *sbi = UFS_SB(sb);
101
102 mutex_lock(&sbi->mutex);
103 sbi->mutex_owner = current;
104#endif
105}
106
107void unlock_ufs(struct super_block *sb)
108{
109#if defined(CONFIG_SMP) || defined (CONFIG_PREEMPT)
110 struct ufs_sb_info *sbi = UFS_SB(sb);
111
112 sbi->mutex_owner = NULL;
113 mutex_unlock(&sbi->mutex);
114#endif
115}
116
Alexey Dobriyanf3e2a522009-12-15 16:46:51 -0800117static struct inode *ufs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation)
118{
119 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
120 struct inode *inode;
121
122 if (ino < UFS_ROOTINO || ino > uspi->s_ncg * uspi->s_ipg)
123 return ERR_PTR(-ESTALE);
124
125 inode = ufs_iget(sb, ino);
126 if (IS_ERR(inode))
127 return ERR_CAST(inode);
128 if (generation && inode->i_generation != generation) {
129 iput(inode);
130 return ERR_PTR(-ESTALE);
131 }
132 return inode;
133}
134
135static struct dentry *ufs_fh_to_dentry(struct super_block *sb, struct fid *fid,
136 int fh_len, int fh_type)
137{
138 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, ufs_nfs_get_inode);
139}
140
141static struct dentry *ufs_fh_to_parent(struct super_block *sb, struct fid *fid,
142 int fh_len, int fh_type)
143{
144 return generic_fh_to_parent(sb, fid, fh_len, fh_type, ufs_nfs_get_inode);
145}
146
147static struct dentry *ufs_get_parent(struct dentry *child)
148{
149 struct qstr dot_dot = {
150 .name = "..",
151 .len = 2,
152 };
153 ino_t ino;
154
155 ino = ufs_inode_by_name(child->d_inode, &dot_dot);
156 if (!ino)
157 return ERR_PTR(-ENOENT);
158 return d_obtain_alias(ufs_iget(child->d_inode->i_sb, ino));
159}
160
161static const struct export_operations ufs_export_ops = {
162 .fh_to_dentry = ufs_fh_to_dentry,
163 .fh_to_parent = ufs_fh_to_parent,
164 .get_parent = ufs_get_parent,
165};
166
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700167#ifdef CONFIG_UFS_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/*
169 * Print contents of ufs_super_block, useful for debugging
170 */
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800171static void ufs_print_super_stuff(struct super_block *sb,
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700172 struct ufs_super_block_first *usb1,
173 struct ufs_super_block_second *usb2,
174 struct ufs_super_block_third *usb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800176 u32 magic = fs32_to_cpu(sb, usb3->fs_magic);
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 printk("ufs_print_super_stuff\n");
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800179 printk(" magic: 0x%x\n", magic);
180 if (fs32_to_cpu(sb, usb3->fs_magic) == UFS2_MAGIC) {
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700181 printk(" fs_size: %llu\n", (unsigned long long)
182 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size));
183 printk(" fs_dsize: %llu\n", (unsigned long long)
184 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize));
185 printk(" bsize: %u\n",
186 fs32_to_cpu(sb, usb1->fs_bsize));
187 printk(" fsize: %u\n",
188 fs32_to_cpu(sb, usb1->fs_fsize));
189 printk(" fs_volname: %s\n", usb2->fs_un.fs_u2.fs_volname);
190 printk(" fs_sblockloc: %llu\n", (unsigned long long)
191 fs64_to_cpu(sb, usb2->fs_un.fs_u2.fs_sblockloc));
192 printk(" cs_ndir(No of dirs): %llu\n", (unsigned long long)
193 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir));
194 printk(" cs_nbfree(No of free blocks): %llu\n",
195 (unsigned long long)
196 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800197 printk(KERN_INFO" cs_nifree(Num of free inodes): %llu\n",
198 (unsigned long long)
199 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree));
200 printk(KERN_INFO" cs_nffree(Num of free frags): %llu\n",
201 (unsigned long long)
202 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree));
Evgeniy Dushistov90b315a2008-02-08 04:20:16 -0800203 printk(KERN_INFO" fs_maxsymlinklen: %u\n",
204 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen));
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700205 } else {
206 printk(" sblkno: %u\n", fs32_to_cpu(sb, usb1->fs_sblkno));
207 printk(" cblkno: %u\n", fs32_to_cpu(sb, usb1->fs_cblkno));
208 printk(" iblkno: %u\n", fs32_to_cpu(sb, usb1->fs_iblkno));
209 printk(" dblkno: %u\n", fs32_to_cpu(sb, usb1->fs_dblkno));
210 printk(" cgoffset: %u\n",
211 fs32_to_cpu(sb, usb1->fs_cgoffset));
212 printk(" ~cgmask: 0x%x\n",
213 ~fs32_to_cpu(sb, usb1->fs_cgmask));
214 printk(" size: %u\n", fs32_to_cpu(sb, usb1->fs_size));
215 printk(" dsize: %u\n", fs32_to_cpu(sb, usb1->fs_dsize));
216 printk(" ncg: %u\n", fs32_to_cpu(sb, usb1->fs_ncg));
217 printk(" bsize: %u\n", fs32_to_cpu(sb, usb1->fs_bsize));
218 printk(" fsize: %u\n", fs32_to_cpu(sb, usb1->fs_fsize));
219 printk(" frag: %u\n", fs32_to_cpu(sb, usb1->fs_frag));
220 printk(" fragshift: %u\n",
221 fs32_to_cpu(sb, usb1->fs_fragshift));
222 printk(" ~fmask: %u\n", ~fs32_to_cpu(sb, usb1->fs_fmask));
223 printk(" fshift: %u\n", fs32_to_cpu(sb, usb1->fs_fshift));
224 printk(" sbsize: %u\n", fs32_to_cpu(sb, usb1->fs_sbsize));
225 printk(" spc: %u\n", fs32_to_cpu(sb, usb1->fs_spc));
226 printk(" cpg: %u\n", fs32_to_cpu(sb, usb1->fs_cpg));
227 printk(" ipg: %u\n", fs32_to_cpu(sb, usb1->fs_ipg));
228 printk(" fpg: %u\n", fs32_to_cpu(sb, usb1->fs_fpg));
229 printk(" csaddr: %u\n", fs32_to_cpu(sb, usb1->fs_csaddr));
230 printk(" cssize: %u\n", fs32_to_cpu(sb, usb1->fs_cssize));
231 printk(" cgsize: %u\n", fs32_to_cpu(sb, usb1->fs_cgsize));
232 printk(" fstodb: %u\n",
233 fs32_to_cpu(sb, usb1->fs_fsbtodb));
234 printk(" nrpos: %u\n", fs32_to_cpu(sb, usb3->fs_nrpos));
235 printk(" ndir %u\n",
236 fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir));
237 printk(" nifree %u\n",
238 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree));
239 printk(" nbfree %u\n",
240 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree));
241 printk(" nffree %u\n",
242 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree));
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 printk("\n");
245}
246
247/*
248 * Print contents of ufs_cylinder_group, useful for debugging
249 */
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700250static void ufs_print_cylinder_stuff(struct super_block *sb,
251 struct ufs_cylinder_group *cg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 printk("\nufs_print_cylinder_stuff\n");
Andrew Morton96710b22006-06-25 05:47:28 -0700254 printk("size of ucg: %zu\n", sizeof(struct ufs_cylinder_group));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 printk(" magic: %x\n", fs32_to_cpu(sb, cg->cg_magic));
256 printk(" time: %u\n", fs32_to_cpu(sb, cg->cg_time));
257 printk(" cgx: %u\n", fs32_to_cpu(sb, cg->cg_cgx));
258 printk(" ncyl: %u\n", fs16_to_cpu(sb, cg->cg_ncyl));
259 printk(" niblk: %u\n", fs16_to_cpu(sb, cg->cg_niblk));
260 printk(" ndblk: %u\n", fs32_to_cpu(sb, cg->cg_ndblk));
261 printk(" cs_ndir: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_ndir));
262 printk(" cs_nbfree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nbfree));
263 printk(" cs_nifree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nifree));
264 printk(" cs_nffree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nffree));
265 printk(" rotor: %u\n", fs32_to_cpu(sb, cg->cg_rotor));
266 printk(" frotor: %u\n", fs32_to_cpu(sb, cg->cg_frotor));
267 printk(" irotor: %u\n", fs32_to_cpu(sb, cg->cg_irotor));
268 printk(" frsum: %u, %u, %u, %u, %u, %u, %u, %u\n",
269 fs32_to_cpu(sb, cg->cg_frsum[0]), fs32_to_cpu(sb, cg->cg_frsum[1]),
270 fs32_to_cpu(sb, cg->cg_frsum[2]), fs32_to_cpu(sb, cg->cg_frsum[3]),
271 fs32_to_cpu(sb, cg->cg_frsum[4]), fs32_to_cpu(sb, cg->cg_frsum[5]),
272 fs32_to_cpu(sb, cg->cg_frsum[6]), fs32_to_cpu(sb, cg->cg_frsum[7]));
273 printk(" btotoff: %u\n", fs32_to_cpu(sb, cg->cg_btotoff));
274 printk(" boff: %u\n", fs32_to_cpu(sb, cg->cg_boff));
275 printk(" iuseoff: %u\n", fs32_to_cpu(sb, cg->cg_iusedoff));
276 printk(" freeoff: %u\n", fs32_to_cpu(sb, cg->cg_freeoff));
277 printk(" nextfreeoff: %u\n", fs32_to_cpu(sb, cg->cg_nextfreeoff));
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700278 printk(" clustersumoff %u\n",
279 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clustersumoff));
280 printk(" clusteroff %u\n",
281 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clusteroff));
282 printk(" nclusterblks %u\n",
283 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_nclusterblks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 printk("\n");
285}
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700286#else
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800287# define ufs_print_super_stuff(sb, usb1, usb2, usb3) /**/
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700288# define ufs_print_cylinder_stuff(sb, cg) /**/
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700289#endif /* CONFIG_UFS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800291static const struct super_operations ufs_super_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293static char error_buf[1024];
294
295void ufs_error (struct super_block * sb, const char * function,
296 const char * fmt, ...)
297{
298 struct ufs_sb_private_info * uspi;
299 struct ufs_super_block_first * usb1;
300 va_list args;
301
302 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300303 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (!(sb->s_flags & MS_RDONLY)) {
306 usb1->fs_clean = UFS_FSBAD;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700307 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 sb->s_dirt = 1;
309 sb->s_flags |= MS_RDONLY;
310 }
311 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800312 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 va_end (args);
314 switch (UFS_SB(sb)->s_mount_opt & UFS_MOUNT_ONERROR) {
315 case UFS_MOUNT_ONERROR_PANIC:
316 panic ("UFS-fs panic (device %s): %s: %s\n",
317 sb->s_id, function, error_buf);
318
319 case UFS_MOUNT_ONERROR_LOCK:
320 case UFS_MOUNT_ONERROR_UMOUNT:
321 case UFS_MOUNT_ONERROR_REPAIR:
322 printk (KERN_CRIT "UFS-fs error (device %s): %s: %s\n",
323 sb->s_id, function, error_buf);
324 }
325}
326
327void ufs_panic (struct super_block * sb, const char * function,
328 const char * fmt, ...)
329{
330 struct ufs_sb_private_info * uspi;
331 struct ufs_super_block_first * usb1;
332 va_list args;
333
334 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300335 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 if (!(sb->s_flags & MS_RDONLY)) {
338 usb1->fs_clean = UFS_FSBAD;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700339 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 sb->s_dirt = 1;
341 }
342 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800343 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 va_end (args);
345 sb->s_flags |= MS_RDONLY;
346 printk (KERN_CRIT "UFS-fs panic (device %s): %s: %s\n",
347 sb->s_id, function, error_buf);
348}
349
350void ufs_warning (struct super_block * sb, const char * function,
351 const char * fmt, ...)
352{
353 va_list args;
354
355 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800356 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 va_end (args);
358 printk (KERN_WARNING "UFS-fs warning (device %s): %s: %s\n",
359 sb->s_id, function, error_buf);
360}
361
362enum {
Evgeniy Dushistov2a9807c02007-10-16 23:26:32 -0700363 Opt_type_old = UFS_MOUNT_UFSTYPE_OLD,
364 Opt_type_sunx86 = UFS_MOUNT_UFSTYPE_SUNx86,
365 Opt_type_sun = UFS_MOUNT_UFSTYPE_SUN,
366 Opt_type_sunos = UFS_MOUNT_UFSTYPE_SUNOS,
367 Opt_type_44bsd = UFS_MOUNT_UFSTYPE_44BSD,
368 Opt_type_ufs2 = UFS_MOUNT_UFSTYPE_UFS2,
369 Opt_type_hp = UFS_MOUNT_UFSTYPE_HP,
370 Opt_type_nextstepcd = UFS_MOUNT_UFSTYPE_NEXTSTEP_CD,
371 Opt_type_nextstep = UFS_MOUNT_UFSTYPE_NEXTSTEP,
372 Opt_type_openstep = UFS_MOUNT_UFSTYPE_OPENSTEP,
373 Opt_onerror_panic = UFS_MOUNT_ONERROR_PANIC,
374 Opt_onerror_lock = UFS_MOUNT_ONERROR_LOCK,
375 Opt_onerror_umount = UFS_MOUNT_ONERROR_UMOUNT,
376 Opt_onerror_repair = UFS_MOUNT_ONERROR_REPAIR,
377 Opt_err
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378};
379
Steven Whitehousea447c092008-10-13 10:46:57 +0100380static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 {Opt_type_old, "ufstype=old"},
382 {Opt_type_sunx86, "ufstype=sunx86"},
383 {Opt_type_sun, "ufstype=sun"},
Mark Fortescue252e2112007-10-16 23:26:31 -0700384 {Opt_type_sunos, "ufstype=sunos"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 {Opt_type_44bsd, "ufstype=44bsd"},
386 {Opt_type_ufs2, "ufstype=ufs2"},
387 {Opt_type_ufs2, "ufstype=5xbsd"},
388 {Opt_type_hp, "ufstype=hp"},
389 {Opt_type_nextstepcd, "ufstype=nextstep-cd"},
390 {Opt_type_nextstep, "ufstype=nextstep"},
391 {Opt_type_openstep, "ufstype=openstep"},
Evgeniy Dushistov2a9807c02007-10-16 23:26:32 -0700392/*end of possible ufs types */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 {Opt_onerror_panic, "onerror=panic"},
394 {Opt_onerror_lock, "onerror=lock"},
395 {Opt_onerror_umount, "onerror=umount"},
396 {Opt_onerror_repair, "onerror=repair"},
397 {Opt_err, NULL}
398};
399
400static int ufs_parse_options (char * options, unsigned * mount_options)
401{
402 char * p;
403
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700404 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 if (!options)
407 return 1;
408
409 while ((p = strsep(&options, ",")) != NULL) {
410 substring_t args[MAX_OPT_ARGS];
411 int token;
412 if (!*p)
413 continue;
414
415 token = match_token(p, tokens, args);
416 switch (token) {
417 case Opt_type_old:
418 ufs_clear_opt (*mount_options, UFSTYPE);
419 ufs_set_opt (*mount_options, UFSTYPE_OLD);
420 break;
421 case Opt_type_sunx86:
422 ufs_clear_opt (*mount_options, UFSTYPE);
423 ufs_set_opt (*mount_options, UFSTYPE_SUNx86);
424 break;
425 case Opt_type_sun:
426 ufs_clear_opt (*mount_options, UFSTYPE);
427 ufs_set_opt (*mount_options, UFSTYPE_SUN);
428 break;
Mark Fortescue252e2112007-10-16 23:26:31 -0700429 case Opt_type_sunos:
430 ufs_clear_opt(*mount_options, UFSTYPE);
431 ufs_set_opt(*mount_options, UFSTYPE_SUNOS);
432 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 case Opt_type_44bsd:
434 ufs_clear_opt (*mount_options, UFSTYPE);
435 ufs_set_opt (*mount_options, UFSTYPE_44BSD);
436 break;
437 case Opt_type_ufs2:
438 ufs_clear_opt(*mount_options, UFSTYPE);
439 ufs_set_opt(*mount_options, UFSTYPE_UFS2);
440 break;
441 case Opt_type_hp:
442 ufs_clear_opt (*mount_options, UFSTYPE);
443 ufs_set_opt (*mount_options, UFSTYPE_HP);
444 break;
445 case Opt_type_nextstepcd:
446 ufs_clear_opt (*mount_options, UFSTYPE);
447 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP_CD);
448 break;
449 case Opt_type_nextstep:
450 ufs_clear_opt (*mount_options, UFSTYPE);
451 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP);
452 break;
453 case Opt_type_openstep:
454 ufs_clear_opt (*mount_options, UFSTYPE);
455 ufs_set_opt (*mount_options, UFSTYPE_OPENSTEP);
456 break;
457 case Opt_onerror_panic:
458 ufs_clear_opt (*mount_options, ONERROR);
459 ufs_set_opt (*mount_options, ONERROR_PANIC);
460 break;
461 case Opt_onerror_lock:
462 ufs_clear_opt (*mount_options, ONERROR);
463 ufs_set_opt (*mount_options, ONERROR_LOCK);
464 break;
465 case Opt_onerror_umount:
466 ufs_clear_opt (*mount_options, ONERROR);
467 ufs_set_opt (*mount_options, ONERROR_UMOUNT);
468 break;
469 case Opt_onerror_repair:
470 printk("UFS-fs: Unable to do repair on error, "
471 "will lock lock instead\n");
472 ufs_clear_opt (*mount_options, ONERROR);
473 ufs_set_opt (*mount_options, ONERROR_REPAIR);
474 break;
475 default:
476 printk("UFS-fs: Invalid option: \"%s\" "
477 "or missing value\n", p);
478 return 0;
479 }
480 }
481 return 1;
482}
483
484/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300485 * Different types of UFS hold fs_cstotal in different
486 * places, and use different data structure for it.
487 * To make things simpler we just copy fs_cstotal to ufs_sb_private_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 */
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700489static void ufs_setup_cstotal(struct super_block *sb)
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800490{
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700491 struct ufs_sb_info *sbi = UFS_SB(sb);
492 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700493 struct ufs_super_block_first *usb1;
494 struct ufs_super_block_second *usb2;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700495 struct ufs_super_block_third *usb3;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700496 unsigned mtype = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
497
498 UFSD("ENTER, mtype=%u\n", mtype);
499 usb1 = ubh_get_usb_first(uspi);
500 usb2 = ubh_get_usb_second(uspi);
501 usb3 = ubh_get_usb_third(uspi);
502
503 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
504 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
505 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
506 /*we have statistic in different place, then usual*/
507 uspi->cs_total.cs_ndir = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir);
508 uspi->cs_total.cs_nbfree = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree);
509 uspi->cs_total.cs_nifree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree);
510 uspi->cs_total.cs_nffree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree);
511 } else {
512 uspi->cs_total.cs_ndir = fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir);
513 uspi->cs_total.cs_nbfree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree);
514 uspi->cs_total.cs_nifree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree);
515 uspi->cs_total.cs_nffree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree);
516 }
517 UFSD("EXIT\n");
518}
519
520/*
521 * Read on-disk structures associated with cylinder groups
522 */
523static int ufs_read_cylinder_structures(struct super_block *sb)
524{
525 struct ufs_sb_info *sbi = UFS_SB(sb);
526 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 struct ufs_buffer_head * ubh;
528 unsigned char * base, * space;
529 unsigned size, blks, i;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700530 struct ufs_super_block_third *usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700532 UFSD("ENTER\n");
533
534 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /*
536 * Read cs structures from (usually) first data block
537 * on the device.
538 */
539 size = uspi->s_cssize;
540 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
Arnd Bergmann788257d2011-01-24 10:14:12 +0100541 base = space = kmalloc(size, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (!base)
543 goto failed;
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800544 sbi->s_csp = (struct ufs_csum *)space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 for (i = 0; i < blks; i += uspi->s_fpb) {
546 size = uspi->s_bsize;
547 if (i + uspi->s_fpb > blks)
548 size = (blks - i) * uspi->s_fsize;
549
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800550 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
Evgeniy7b4ee732006-01-14 11:42:06 +0300551
552 if (!ubh)
553 goto failed;
554
555 ubh_ubhcpymem (space, ubh, size);
Evgeniy7b4ee732006-01-14 11:42:06 +0300556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 space += size;
558 ubh_brelse (ubh);
559 ubh = NULL;
560 }
561
562 /*
563 * Read cylinder group (we read only first fragment from block
564 * at this time) and prepare internal data structures for cg caching.
565 */
Arnd Bergmann788257d2011-01-24 10:14:12 +0100566 if (!(sbi->s_ucg = kmalloc (sizeof(struct buffer_head *) * uspi->s_ncg, GFP_NOFS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 goto failed;
568 for (i = 0; i < uspi->s_ncg; i++)
569 sbi->s_ucg[i] = NULL;
570 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
571 sbi->s_ucpi[i] = NULL;
572 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
573 }
574 for (i = 0; i < uspi->s_ncg; i++) {
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700575 UFSD("read cg %u\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (!(sbi->s_ucg[i] = sb_bread(sb, ufs_cgcmin(i))))
577 goto failed;
578 if (!ufs_cg_chkmagic (sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data))
579 goto failed;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 ufs_print_cylinder_stuff(sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
Arnd Bergmann788257d2011-01-24 10:14:12 +0100584 if (!(sbi->s_ucpi[i] = kmalloc (sizeof(struct ufs_cg_private_info), GFP_NOFS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 goto failed;
586 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
587 }
588 sbi->s_cg_loaded = 0;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700589 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 1;
591
592failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800593 kfree (base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (sbi->s_ucg) {
595 for (i = 0; i < uspi->s_ncg; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800596 if (sbi->s_ucg[i])
597 brelse (sbi->s_ucg[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 kfree (sbi->s_ucg);
599 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800600 kfree (sbi->s_ucpi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700602 UFSD("EXIT (FAILED)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 0;
604}
605
606/*
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700607 * Sync our internal copy of fs_cstotal with disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700609static void ufs_put_cstotal(struct super_block *sb)
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800610{
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700611 unsigned mtype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
612 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
613 struct ufs_super_block_first *usb1;
614 struct ufs_super_block_second *usb2;
615 struct ufs_super_block_third *usb3;
616
617 UFSD("ENTER\n");
618 usb1 = ubh_get_usb_first(uspi);
619 usb2 = ubh_get_usb_second(uspi);
620 usb3 = ubh_get_usb_third(uspi);
621
622 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
623 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
624 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
625 /*we have statistic in different place, then usual*/
626 usb2->fs_un.fs_u2.cs_ndir =
627 cpu_to_fs64(sb, uspi->cs_total.cs_ndir);
628 usb2->fs_un.fs_u2.cs_nbfree =
629 cpu_to_fs64(sb, uspi->cs_total.cs_nbfree);
630 usb3->fs_un1.fs_u2.cs_nifree =
631 cpu_to_fs64(sb, uspi->cs_total.cs_nifree);
632 usb3->fs_un1.fs_u2.cs_nffree =
633 cpu_to_fs64(sb, uspi->cs_total.cs_nffree);
634 } else {
635 usb1->fs_cstotal.cs_ndir =
636 cpu_to_fs32(sb, uspi->cs_total.cs_ndir);
637 usb1->fs_cstotal.cs_nbfree =
638 cpu_to_fs32(sb, uspi->cs_total.cs_nbfree);
639 usb1->fs_cstotal.cs_nifree =
640 cpu_to_fs32(sb, uspi->cs_total.cs_nifree);
641 usb1->fs_cstotal.cs_nffree =
642 cpu_to_fs32(sb, uspi->cs_total.cs_nffree);
643 }
644 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800645 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700646 UFSD("EXIT\n");
647}
648
649/**
650 * ufs_put_super_internal() - put on-disk intrenal structures
651 * @sb: pointer to super_block structure
652 * Put on-disk structures associated with cylinder groups
653 * and write them back to disk, also update cs_total on disk
654 */
655static void ufs_put_super_internal(struct super_block *sb)
656{
657 struct ufs_sb_info *sbi = UFS_SB(sb);
658 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 struct ufs_buffer_head * ubh;
660 unsigned char * base, * space;
661 unsigned blks, size, i;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700664 UFSD("ENTER\n");
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200665
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700666 ufs_put_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 size = uspi->s_cssize;
668 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800669 base = space = (char*) sbi->s_csp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 for (i = 0; i < blks; i += uspi->s_fpb) {
671 size = uspi->s_bsize;
672 if (i + uspi->s_fpb > blks)
673 size = (blks - i) * uspi->s_fsize;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 ubh_memcpyubh (ubh, space, size);
678 space += size;
679 ubh_mark_buffer_uptodate (ubh, 1);
680 ubh_mark_buffer_dirty (ubh);
681 ubh_brelse (ubh);
682 }
683 for (i = 0; i < sbi->s_cg_loaded; i++) {
684 ufs_put_cylinder (sb, i);
685 kfree (sbi->s_ucpi[i]);
686 }
687 for (; i < UFS_MAX_GROUP_LOADED; i++)
688 kfree (sbi->s_ucpi[i]);
689 for (i = 0; i < uspi->s_ncg; i++)
690 brelse (sbi->s_ucg[i]);
691 kfree (sbi->s_ucg);
692 kfree (base);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200693
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700694 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697static int ufs_fill_super(struct super_block *sb, void *data, int silent)
698{
699 struct ufs_sb_info * sbi;
700 struct ufs_sb_private_info * uspi;
701 struct ufs_super_block_first * usb1;
702 struct ufs_super_block_second * usb2;
703 struct ufs_super_block_third * usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct ufs_buffer_head * ubh;
705 struct inode *inode;
706 unsigned block_size, super_block_size;
707 unsigned flags;
Evgeniy7b4ee732006-01-14 11:42:06 +0300708 unsigned super_block_offset;
Duane Griffin9e6766c2009-01-08 22:43:48 +0000709 unsigned maxsymlen;
David Howellsb55c4602008-02-07 00:15:48 -0800710 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 uspi = NULL;
713 ubh = NULL;
714 flags = 0;
715
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700716 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700718 sbi = kzalloc(sizeof(struct ufs_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (!sbi)
720 goto failed_nomem;
721 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700723 UFSD("flag %u\n", (int)(sb->s_flags & MS_RDONLY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725#ifndef CONFIG_UFS_FS_WRITE
726 if (!(sb->s_flags & MS_RDONLY)) {
727 printk("ufs was compiled with read-only support, "
728 "can't be mounted as read-write\n");
729 goto failed;
730 }
731#endif
Arnd Bergmann788257d2011-01-24 10:14:12 +0100732 mutex_init(&sbi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /*
734 * Set default mount options
735 * Parse mount options
736 */
737 sbi->s_mount_opt = 0;
738 ufs_set_opt (sbi->s_mount_opt, ONERROR_LOCK);
739 if (!ufs_parse_options ((char *) data, &sbi->s_mount_opt)) {
740 printk("wrong mount options\n");
741 goto failed;
742 }
743 if (!(sbi->s_mount_opt & UFS_MOUNT_UFSTYPE)) {
744 if (!silent)
745 printk("You didn't specify the type of your ufs filesystem\n\n"
746 "mount -t ufs -o ufstype="
Michael Owenc690a722006-03-24 18:21:44 +0100747 "sun|sunx86|44bsd|ufs2|5xbsd|old|hp|nextstep|nextstep-cd|openstep ...\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 ">>>WARNING<<< Wrong ufstype may corrupt your filesystem, "
749 "default is ufstype=old\n");
750 ufs_set_opt (sbi->s_mount_opt, UFSTYPE_OLD);
751 }
752
Mark Fortescue252e2112007-10-16 23:26:31 -0700753 uspi = kzalloc(sizeof(struct ufs_sb_private_info), GFP_KERNEL);
754 sbi->s_uspi = uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (!uspi)
756 goto failed;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800757 uspi->s_dirblksize = UFS_SECTOR_SIZE;
Evgeniy7b4ee732006-01-14 11:42:06 +0300758 super_block_offset=UFS_SBLOCK;
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /* Keep 2Gig file limit. Some UFS variants need to override
761 this but as I don't know which I'll let those in the know loosen
762 the rules */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 switch (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) {
764 case UFS_MOUNT_UFSTYPE_44BSD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700765 UFSD("ufstype=44bsd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 uspi->s_fsize = block_size = 512;
767 uspi->s_fmask = ~(512 - 1);
768 uspi->s_fshift = 9;
769 uspi->s_sbsize = super_block_size = 1536;
770 uspi->s_sbbase = 0;
771 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
772 break;
773 case UFS_MOUNT_UFSTYPE_UFS2:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700774 UFSD("ufstype=ufs2\n");
Evgeniy7b4ee732006-01-14 11:42:06 +0300775 super_block_offset=SBLOCK_UFS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 uspi->s_fsize = block_size = 512;
777 uspi->s_fmask = ~(512 - 1);
778 uspi->s_fshift = 9;
779 uspi->s_sbsize = super_block_size = 1536;
780 uspi->s_sbbase = 0;
781 flags |= UFS_TYPE_UFS2 | UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 break;
783
784 case UFS_MOUNT_UFSTYPE_SUN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700785 UFSD("ufstype=sun\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 uspi->s_fsize = block_size = 1024;
787 uspi->s_fmask = ~(1024 - 1);
788 uspi->s_fshift = 10;
789 uspi->s_sbsize = super_block_size = 2048;
790 uspi->s_sbbase = 0;
Mark Fortescue252e2112007-10-16 23:26:31 -0700791 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUN | UFS_CG_SUN;
793 break;
794
Mark Fortescue252e2112007-10-16 23:26:31 -0700795 case UFS_MOUNT_UFSTYPE_SUNOS:
796 UFSD(("ufstype=sunos\n"))
797 uspi->s_fsize = block_size = 1024;
798 uspi->s_fmask = ~(1024 - 1);
799 uspi->s_fshift = 10;
800 uspi->s_sbsize = 2048;
801 super_block_size = 2048;
802 uspi->s_sbbase = 0;
803 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
804 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_SUNOS | UFS_CG_SUN;
805 break;
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 case UFS_MOUNT_UFSTYPE_SUNx86:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700808 UFSD("ufstype=sunx86\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 uspi->s_fsize = block_size = 1024;
810 uspi->s_fmask = ~(1024 - 1);
811 uspi->s_fshift = 10;
812 uspi->s_sbsize = super_block_size = 2048;
813 uspi->s_sbbase = 0;
Mark Fortescue252e2112007-10-16 23:26:31 -0700814 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUNx86 | UFS_CG_SUN;
816 break;
817
818 case UFS_MOUNT_UFSTYPE_OLD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700819 UFSD("ufstype=old\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 uspi->s_fsize = block_size = 1024;
821 uspi->s_fmask = ~(1024 - 1);
822 uspi->s_fshift = 10;
823 uspi->s_sbsize = super_block_size = 2048;
824 uspi->s_sbbase = 0;
825 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
826 if (!(sb->s_flags & MS_RDONLY)) {
827 if (!silent)
828 printk(KERN_INFO "ufstype=old is supported read-only\n");
829 sb->s_flags |= MS_RDONLY;
830 }
831 break;
832
833 case UFS_MOUNT_UFSTYPE_NEXTSTEP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700834 UFSD("ufstype=nextstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 uspi->s_fsize = block_size = 1024;
836 uspi->s_fmask = ~(1024 - 1);
837 uspi->s_fshift = 10;
838 uspi->s_sbsize = super_block_size = 2048;
839 uspi->s_sbbase = 0;
Evgeniy Dushistov0c664f92007-12-04 23:45:06 -0800840 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
842 if (!(sb->s_flags & MS_RDONLY)) {
843 if (!silent)
844 printk(KERN_INFO "ufstype=nextstep is supported read-only\n");
845 sb->s_flags |= MS_RDONLY;
846 }
847 break;
848
849 case UFS_MOUNT_UFSTYPE_NEXTSTEP_CD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700850 UFSD("ufstype=nextstep-cd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 uspi->s_fsize = block_size = 2048;
852 uspi->s_fmask = ~(2048 - 1);
853 uspi->s_fshift = 11;
854 uspi->s_sbsize = super_block_size = 2048;
855 uspi->s_sbbase = 0;
Evgeniy Dushistov0c664f92007-12-04 23:45:06 -0800856 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
858 if (!(sb->s_flags & MS_RDONLY)) {
859 if (!silent)
860 printk(KERN_INFO "ufstype=nextstep-cd is supported read-only\n");
861 sb->s_flags |= MS_RDONLY;
862 }
863 break;
864
865 case UFS_MOUNT_UFSTYPE_OPENSTEP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700866 UFSD("ufstype=openstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 uspi->s_fsize = block_size = 1024;
868 uspi->s_fmask = ~(1024 - 1);
869 uspi->s_fshift = 10;
870 uspi->s_sbsize = super_block_size = 2048;
871 uspi->s_sbbase = 0;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800872 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
874 if (!(sb->s_flags & MS_RDONLY)) {
875 if (!silent)
876 printk(KERN_INFO "ufstype=openstep is supported read-only\n");
877 sb->s_flags |= MS_RDONLY;
878 }
879 break;
880
881 case UFS_MOUNT_UFSTYPE_HP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700882 UFSD("ufstype=hp\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 uspi->s_fsize = block_size = 1024;
884 uspi->s_fmask = ~(1024 - 1);
885 uspi->s_fshift = 10;
886 uspi->s_sbsize = super_block_size = 2048;
887 uspi->s_sbbase = 0;
888 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
889 if (!(sb->s_flags & MS_RDONLY)) {
890 if (!silent)
891 printk(KERN_INFO "ufstype=hp is supported read-only\n");
892 sb->s_flags |= MS_RDONLY;
893 }
894 break;
895 default:
896 if (!silent)
897 printk("unknown ufstype\n");
898 goto failed;
899 }
900
901again:
902 if (!sb_set_blocksize(sb, block_size)) {
903 printk(KERN_ERR "UFS: failed to set blocksize\n");
904 goto failed;
905 }
906
907 /*
908 * read ufs super block from device
909 */
Evgeniy7b4ee732006-01-14 11:42:06 +0300910
911 ubh = ubh_bread_uspi(uspi, sb, uspi->s_sbbase + super_block_offset/block_size, super_block_size);
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (!ubh)
914 goto failed;
915
Evgeniy7b4ee732006-01-14 11:42:06 +0300916 usb1 = ubh_get_usb_first(uspi);
917 usb2 = ubh_get_usb_second(uspi);
918 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Mark Fortescue252e2112007-10-16 23:26:31 -0700920 /* Sort out mod used on SunOS 4.1.3 for fs_state */
921 uspi->s_postblformat = fs32_to_cpu(sb, usb3->fs_postblformat);
922 if (((flags & UFS_ST_MASK) == UFS_ST_SUNOS) &&
923 (uspi->s_postblformat != UFS_42POSTBLFMT)) {
924 flags &= ~UFS_ST_MASK;
925 flags |= UFS_ST_SUN;
926 }
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /*
929 * Check ufs magic number
930 */
931 sbi->s_bytesex = BYTESEX_LE;
932 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
933 case UFS_MAGIC:
Thomas Stewartd27d7a92010-05-26 14:42:33 -0700934 case UFS_MAGIC_BW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 case UFS2_MAGIC:
936 case UFS_MAGIC_LFN:
937 case UFS_MAGIC_FEA:
938 case UFS_MAGIC_4GB:
939 goto magic_found;
940 }
941 sbi->s_bytesex = BYTESEX_BE;
942 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
943 case UFS_MAGIC:
Thomas Stewartd27d7a92010-05-26 14:42:33 -0700944 case UFS_MAGIC_BW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 case UFS2_MAGIC:
946 case UFS_MAGIC_LFN:
947 case UFS_MAGIC_FEA:
948 case UFS_MAGIC_4GB:
949 goto magic_found;
950 }
951
952 if ((((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP)
953 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP_CD)
954 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_OPENSTEP))
955 && uspi->s_sbbase < 256) {
956 ubh_brelse_uspi(uspi);
957 ubh = NULL;
958 uspi->s_sbbase += 8;
959 goto again;
960 }
961 if (!silent)
962 printk("ufs_read_super: bad magic number\n");
963 goto failed;
964
965magic_found:
966 /*
967 * Check block and fragment sizes
968 */
969 uspi->s_bsize = fs32_to_cpu(sb, usb1->fs_bsize);
970 uspi->s_fsize = fs32_to_cpu(sb, usb1->fs_fsize);
971 uspi->s_sbsize = fs32_to_cpu(sb, usb1->fs_sbsize);
972 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
973 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
974
vignesh babu9e8c4272007-07-15 23:41:36 -0700975 if (!is_power_of_2(uspi->s_fsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 printk(KERN_ERR "ufs_read_super: fragment size %u is not a power of 2\n",
977 uspi->s_fsize);
978 goto failed;
979 }
980 if (uspi->s_fsize < 512) {
981 printk(KERN_ERR "ufs_read_super: fragment size %u is too small\n",
982 uspi->s_fsize);
983 goto failed;
984 }
985 if (uspi->s_fsize > 4096) {
986 printk(KERN_ERR "ufs_read_super: fragment size %u is too large\n",
987 uspi->s_fsize);
988 goto failed;
989 }
vignesh babu9e8c4272007-07-15 23:41:36 -0700990 if (!is_power_of_2(uspi->s_bsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 printk(KERN_ERR "ufs_read_super: block size %u is not a power of 2\n",
992 uspi->s_bsize);
993 goto failed;
994 }
995 if (uspi->s_bsize < 4096) {
996 printk(KERN_ERR "ufs_read_super: block size %u is too small\n",
997 uspi->s_bsize);
998 goto failed;
999 }
1000 if (uspi->s_bsize / uspi->s_fsize > 8) {
1001 printk(KERN_ERR "ufs_read_super: too many fragments per block (%u)\n",
1002 uspi->s_bsize / uspi->s_fsize);
1003 goto failed;
1004 }
1005 if (uspi->s_fsize != block_size || uspi->s_sbsize != super_block_size) {
1006 ubh_brelse_uspi(uspi);
1007 ubh = NULL;
1008 block_size = uspi->s_fsize;
1009 super_block_size = uspi->s_sbsize;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001010 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 -07001011 goto again;
1012 }
1013
Andrew Mortonf664f1f2007-10-29 14:37:21 -07001014 sbi->s_flags = flags;/*after that line some functions use s_flags*/
Evgeniy Dushistov3313e292007-02-12 00:54:31 -08001015 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 /*
1018 * Check, if file system was correctly unmounted.
1019 * If not, make it read only.
1020 */
Andrew Mortonf664f1f2007-10-29 14:37:21 -07001021 if (((flags & UFS_ST_MASK) == UFS_ST_44BSD) ||
1022 ((flags & UFS_ST_MASK) == UFS_ST_OLD) ||
1023 (((flags & UFS_ST_MASK) == UFS_ST_SUN ||
1024 (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
1025 (flags & UFS_ST_MASK) == UFS_ST_SUNx86) &&
1026 (ufs_get_fs_state(sb, usb1, usb3) == (UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 switch(usb1->fs_clean) {
1028 case UFS_FSCLEAN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001029 UFSD("fs is clean\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 break;
1031 case UFS_FSSTABLE:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001032 UFSD("fs is stable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 break;
Alex Viskovatoffb3a0fd42010-03-10 15:21:53 -08001034 case UFS_FSLOG:
1035 UFSD("fs is logging fs\n");
1036 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 case UFS_FSOSF1:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001038 UFSD("fs is DEC OSF/1\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 break;
1040 case UFS_FSACTIVE:
1041 printk("ufs_read_super: fs is active\n");
1042 sb->s_flags |= MS_RDONLY;
1043 break;
1044 case UFS_FSBAD:
1045 printk("ufs_read_super: fs is bad\n");
1046 sb->s_flags |= MS_RDONLY;
1047 break;
1048 default:
1049 printk("ufs_read_super: can't grok fs_clean 0x%x\n", usb1->fs_clean);
1050 sb->s_flags |= MS_RDONLY;
1051 break;
1052 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001053 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 printk("ufs_read_super: fs needs fsck\n");
1055 sb->s_flags |= MS_RDONLY;
1056 }
1057
1058 /*
1059 * Read ufs_super_block into internal data structures
1060 */
1061 sb->s_op = &ufs_super_ops;
Alexey Dobriyanf3e2a522009-12-15 16:46:51 -08001062 sb->s_export_op = &ufs_export_ops;
Christoph Hellwig123e9ca2010-05-19 07:16:44 -04001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 sb->s_magic = fs32_to_cpu(sb, usb3->fs_magic);
1065
1066 uspi->s_sblkno = fs32_to_cpu(sb, usb1->fs_sblkno);
1067 uspi->s_cblkno = fs32_to_cpu(sb, usb1->fs_cblkno);
1068 uspi->s_iblkno = fs32_to_cpu(sb, usb1->fs_iblkno);
1069 uspi->s_dblkno = fs32_to_cpu(sb, usb1->fs_dblkno);
1070 uspi->s_cgoffset = fs32_to_cpu(sb, usb1->fs_cgoffset);
1071 uspi->s_cgmask = fs32_to_cpu(sb, usb1->fs_cgmask);
1072
1073 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001074 uspi->s_u2_size = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size);
1075 uspi->s_u2_dsize = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
1076 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 uspi->s_size = fs32_to_cpu(sb, usb1->fs_size);
1078 uspi->s_dsize = fs32_to_cpu(sb, usb1->fs_dsize);
1079 }
1080
1081 uspi->s_ncg = fs32_to_cpu(sb, usb1->fs_ncg);
1082 /* s_bsize already set */
1083 /* s_fsize already set */
1084 uspi->s_fpb = fs32_to_cpu(sb, usb1->fs_frag);
1085 uspi->s_minfree = fs32_to_cpu(sb, usb1->fs_minfree);
1086 uspi->s_bmask = fs32_to_cpu(sb, usb1->fs_bmask);
1087 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
1088 uspi->s_bshift = fs32_to_cpu(sb, usb1->fs_bshift);
1089 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001090 UFSD("uspi->s_bshift = %d,uspi->s_fshift = %d", uspi->s_bshift,
1091 uspi->s_fshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 uspi->s_fpbshift = fs32_to_cpu(sb, usb1->fs_fragshift);
1093 uspi->s_fsbtodb = fs32_to_cpu(sb, usb1->fs_fsbtodb);
1094 /* s_sbsize already set */
1095 uspi->s_csmask = fs32_to_cpu(sb, usb1->fs_csmask);
1096 uspi->s_csshift = fs32_to_cpu(sb, usb1->fs_csshift);
1097 uspi->s_nindir = fs32_to_cpu(sb, usb1->fs_nindir);
1098 uspi->s_inopb = fs32_to_cpu(sb, usb1->fs_inopb);
1099 uspi->s_nspf = fs32_to_cpu(sb, usb1->fs_nspf);
1100 uspi->s_npsect = ufs_get_fs_npsect(sb, usb1, usb3);
1101 uspi->s_interleave = fs32_to_cpu(sb, usb1->fs_interleave);
1102 uspi->s_trackskew = fs32_to_cpu(sb, usb1->fs_trackskew);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -08001103
1104 if (uspi->fs_magic == UFS2_MAGIC)
1105 uspi->s_csaddr = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_csaddr);
1106 else
1107 uspi->s_csaddr = fs32_to_cpu(sb, usb1->fs_csaddr);
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 uspi->s_cssize = fs32_to_cpu(sb, usb1->fs_cssize);
1110 uspi->s_cgsize = fs32_to_cpu(sb, usb1->fs_cgsize);
1111 uspi->s_ntrak = fs32_to_cpu(sb, usb1->fs_ntrak);
1112 uspi->s_nsect = fs32_to_cpu(sb, usb1->fs_nsect);
1113 uspi->s_spc = fs32_to_cpu(sb, usb1->fs_spc);
1114 uspi->s_ipg = fs32_to_cpu(sb, usb1->fs_ipg);
1115 uspi->s_fpg = fs32_to_cpu(sb, usb1->fs_fpg);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001116 uspi->s_cpc = fs32_to_cpu(sb, usb2->fs_un.fs_u1.fs_cpc);
1117 uspi->s_contigsumsize = fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_contigsumsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 uspi->s_qbmask = ufs_get_fs_qbmask(sb, usb3);
1119 uspi->s_qfmask = ufs_get_fs_qfmask(sb, usb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 uspi->s_nrpos = fs32_to_cpu(sb, usb3->fs_nrpos);
1121 uspi->s_postbloff = fs32_to_cpu(sb, usb3->fs_postbloff);
1122 uspi->s_rotbloff = fs32_to_cpu(sb, usb3->fs_rotbloff);
1123
1124 /*
1125 * Compute another frequently used values
1126 */
1127 uspi->s_fpbmask = uspi->s_fpb - 1;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001128 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 uspi->s_apbshift = uspi->s_bshift - 3;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001130 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 uspi->s_apbshift = uspi->s_bshift - 2;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 uspi->s_2apbshift = uspi->s_apbshift * 2;
1134 uspi->s_3apbshift = uspi->s_apbshift * 3;
1135 uspi->s_apb = 1 << uspi->s_apbshift;
1136 uspi->s_2apb = 1 << uspi->s_2apbshift;
1137 uspi->s_3apb = 1 << uspi->s_3apbshift;
1138 uspi->s_apbmask = uspi->s_apb - 1;
1139 uspi->s_nspfshift = uspi->s_fshift - UFS_SECTOR_BITS;
1140 uspi->s_nspb = uspi->s_nspf << uspi->s_fpbshift;
1141 uspi->s_inopf = uspi->s_inopb >> uspi->s_fpbshift;
1142 uspi->s_bpf = uspi->s_fsize << 3;
1143 uspi->s_bpfshift = uspi->s_fshift + 3;
1144 uspi->s_bpfmask = uspi->s_bpf - 1;
Evgeniy Dushistov90b315a2008-02-08 04:20:16 -08001145 if ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_44BSD ||
1146 (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 uspi->s_maxsymlinklen =
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001148 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Duane Griffin9e6766c2009-01-08 22:43:48 +00001150 if (uspi->fs_magic == UFS2_MAGIC)
1151 maxsymlen = 2 * 4 * (UFS_NDADDR + UFS_NINDIR);
1152 else
1153 maxsymlen = 4 * (UFS_NDADDR + UFS_NINDIR);
1154 if (uspi->s_maxsymlinklen > maxsymlen) {
1155 ufs_warning(sb, __func__, "ufs_read_super: excessive maximum "
1156 "fast symlink size (%u)\n", uspi->s_maxsymlinklen);
1157 uspi->s_maxsymlinklen = maxsymlen;
1158 }
Al Viro8de52772012-02-06 12:45:27 -05001159 sb->s_max_links = UFS_LINK_MAX;
Duane Griffin9e6766c2009-01-08 22:43:48 +00001160
David Howellsb55c4602008-02-07 00:15:48 -08001161 inode = ufs_iget(sb, UFS_ROOTINO);
1162 if (IS_ERR(inode)) {
1163 ret = PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 goto failed;
David Howellsb55c4602008-02-07 00:15:48 -08001165 }
Al Viro48fde702012-01-08 22:15:13 -05001166 sb->s_root = d_make_root(inode);
David Howellsb55c4602008-02-07 00:15:48 -08001167 if (!sb->s_root) {
1168 ret = -ENOMEM;
Al Viro48fde702012-01-08 22:15:13 -05001169 goto failed;
David Howellsb55c4602008-02-07 00:15:48 -08001170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001172 ufs_setup_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /*
1174 * Read cylinder group structures
1175 */
1176 if (!(sb->s_flags & MS_RDONLY))
1177 if (!ufs_read_cylinder_structures(sb))
1178 goto failed;
1179
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001180 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return 0;
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -08001184 if (ubh)
1185 ubh_brelse_uspi (uspi);
1186 kfree (uspi);
1187 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 sb->s_fs_info = NULL;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001189 UFSD("EXIT (FAILED)\n");
David Howellsb55c4602008-02-07 00:15:48 -08001190 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192failed_nomem:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001193 UFSD("EXIT (NOMEM)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return -ENOMEM;
1195}
1196
Christoph Hellwig8c800652009-06-08 10:08:05 +02001197static int ufs_sync_fs(struct super_block *sb, int wait)
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001198{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 struct ufs_sb_private_info * uspi;
1200 struct ufs_super_block_first * usb1;
1201 struct ufs_super_block_third * usb3;
1202 unsigned flags;
1203
Arnd Bergmann788257d2011-01-24 10:14:12 +01001204 lock_ufs(sb);
Christoph Hellwigebc1ac12009-05-11 23:35:03 +02001205 lock_super(sb);
Christoph Hellwig8c800652009-06-08 10:08:05 +02001206
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001207 UFSD("ENTER\n");
Christoph Hellwig8c800652009-06-08 10:08:05 +02001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 flags = UFS_SB(sb)->s_flags;
1210 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +03001211 usb1 = ubh_get_usb_first(uspi);
1212 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Christoph Hellwig8c800652009-06-08 10:08:05 +02001214 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1215 if ((flags & UFS_ST_MASK) == UFS_ST_SUN ||
1216 (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
1217 (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1218 ufs_set_fs_state(sb, usb1, usb3,
1219 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
1220 ufs_put_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 sb->s_dirt = 0;
Christoph Hellwig8c800652009-06-08 10:08:05 +02001222
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001223 UFSD("EXIT\n");
Christoph Hellwigebc1ac12009-05-11 23:35:03 +02001224 unlock_super(sb);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001225 unlock_ufs(sb);
Christoph Hellwig8c800652009-06-08 10:08:05 +02001226
1227 return 0;
1228}
1229
1230static void ufs_write_super(struct super_block *sb)
1231{
1232 if (!(sb->s_flags & MS_RDONLY))
1233 ufs_sync_fs(sb, 1);
1234 else
1235 sb->s_dirt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001238static void ufs_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 struct ufs_sb_info * sbi = UFS_SB(sb);
1241
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001242 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Christoph Hellwig8c85e122009-04-28 18:00:26 +02001244 if (sb->s_dirt)
1245 ufs_write_super(sb);
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (!(sb->s_flags & MS_RDONLY))
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001248 ufs_put_super_internal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 ubh_brelse_uspi (sbi->s_uspi);
1251 kfree (sbi->s_uspi);
1252 kfree (sbi);
1253 sb->s_fs_info = NULL;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001254 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return;
1256}
1257
1258
1259static int ufs_remount (struct super_block *sb, int *mount_flags, char *data)
1260{
1261 struct ufs_sb_private_info * uspi;
1262 struct ufs_super_block_first * usb1;
1263 struct ufs_super_block_third * usb3;
1264 unsigned new_mount_opt, ufstype;
1265 unsigned flags;
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001266
Arnd Bergmann788257d2011-01-24 10:14:12 +01001267 lock_ufs(sb);
Al Virobbd68512009-05-06 10:43:07 -04001268 lock_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 uspi = UFS_SB(sb)->s_uspi;
1270 flags = UFS_SB(sb)->s_flags;
Evgeniy7b4ee732006-01-14 11:42:06 +03001271 usb1 = ubh_get_usb_first(uspi);
1272 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 /*
1275 * Allow the "check" option to be passed as a remount option.
1276 * It is not possible to change ufstype option during remount
1277 */
1278 ufstype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
1279 new_mount_opt = 0;
1280 ufs_set_opt (new_mount_opt, ONERROR_LOCK);
Al Virobbd68512009-05-06 10:43:07 -04001281 if (!ufs_parse_options (data, &new_mount_opt)) {
1282 unlock_super(sb);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001283 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return -EINVAL;
Al Virobbd68512009-05-06 10:43:07 -04001285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (!(new_mount_opt & UFS_MOUNT_UFSTYPE)) {
1287 new_mount_opt |= ufstype;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001288 } else if ((new_mount_opt & UFS_MOUNT_UFSTYPE) != ufstype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 printk("ufstype can't be changed during remount\n");
Al Virobbd68512009-05-06 10:43:07 -04001290 unlock_super(sb);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001291 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return -EINVAL;
1293 }
1294
1295 if ((*mount_flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
1296 UFS_SB(sb)->s_mount_opt = new_mount_opt;
Al Virobbd68512009-05-06 10:43:07 -04001297 unlock_super(sb);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001298 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return 0;
1300 }
1301
1302 /*
1303 * fs was mouted as rw, remounting ro
1304 */
1305 if (*mount_flags & MS_RDONLY) {
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001306 ufs_put_super_internal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1308 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
Mark Fortescue252e2112007-10-16 23:26:31 -07001309 || (flags & UFS_ST_MASK) == UFS_ST_SUNOS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1311 ufs_set_fs_state(sb, usb1, usb3,
1312 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -07001313 ubh_mark_buffer_dirty (USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 sb->s_dirt = 0;
1315 sb->s_flags |= MS_RDONLY;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001316 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 /*
1318 * fs was mounted as ro, remounting rw
1319 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320#ifndef CONFIG_UFS_FS_WRITE
1321 printk("ufs was compiled with read-only support, "
1322 "can't be mounted as read-write\n");
Al Virobbd68512009-05-06 10:43:07 -04001323 unlock_super(sb);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001324 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return -EINVAL;
1326#else
1327 if (ufstype != UFS_MOUNT_UFSTYPE_SUN &&
Mark Fortescue252e2112007-10-16 23:26:31 -07001328 ufstype != UFS_MOUNT_UFSTYPE_SUNOS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 ufstype != UFS_MOUNT_UFSTYPE_44BSD &&
Evgeniy Dushistovcbcae392007-02-12 00:54:30 -08001330 ufstype != UFS_MOUNT_UFSTYPE_SUNx86 &&
1331 ufstype != UFS_MOUNT_UFSTYPE_UFS2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 printk("this ufstype is read-only supported\n");
Al Virobbd68512009-05-06 10:43:07 -04001333 unlock_super(sb);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001334 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return -EINVAL;
1336 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001337 if (!ufs_read_cylinder_structures(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 printk("failed during remounting\n");
Al Virobbd68512009-05-06 10:43:07 -04001339 unlock_super(sb);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001340 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return -EPERM;
1342 }
1343 sb->s_flags &= ~MS_RDONLY;
1344#endif
1345 }
1346 UFS_SB(sb)->s_mount_opt = new_mount_opt;
Al Virobbd68512009-05-06 10:43:07 -04001347 unlock_super(sb);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001348 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return 0;
1350}
1351
Al Viro34c80b12011-12-08 21:32:45 -05001352static int ufs_show_options(struct seq_file *seq, struct dentry *root)
Evgeniy Dushistov2a9807c02007-10-16 23:26:32 -07001353{
Al Viro34c80b12011-12-08 21:32:45 -05001354 struct ufs_sb_info *sbi = UFS_SB(root->d_sb);
Evgeniy Dushistov2a9807c02007-10-16 23:26:32 -07001355 unsigned mval = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
Steven Whitehousea447c092008-10-13 10:46:57 +01001356 const struct match_token *tp = tokens;
Evgeniy Dushistov2a9807c02007-10-16 23:26:32 -07001357
1358 while (tp->token != Opt_onerror_panic && tp->token != mval)
1359 ++tp;
1360 BUG_ON(tp->token == Opt_onerror_panic);
1361 seq_printf(seq, ",%s", tp->pattern);
1362
1363 mval = sbi->s_mount_opt & UFS_MOUNT_ONERROR;
1364 while (tp->token != Opt_err && tp->token != mval)
1365 ++tp;
1366 BUG_ON(tp->token == Opt_err);
1367 seq_printf(seq, ",%s", tp->pattern);
1368
1369 return 0;
1370}
1371
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001372static int ufs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
David Howells726c3342006-06-23 02:02:58 -07001374 struct super_block *sb = dentry->d_sb;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001375 struct ufs_sb_private_info *uspi= UFS_SB(sb)->s_uspi;
1376 unsigned flags = UFS_SB(sb)->s_flags;
1377 struct ufs_super_block_first *usb1;
1378 struct ufs_super_block_second *usb2;
1379 struct ufs_super_block_third *usb3;
Coly Li41d577a2009-04-02 16:59:44 -07001380 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Arnd Bergmann788257d2011-01-24 10:14:12 +01001382 lock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001384 usb1 = ubh_get_usb_first(uspi);
1385 usb2 = ubh_get_usb_second(uspi);
1386 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
1389 buf->f_type = UFS2_MAGIC;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001390 buf->f_blocks = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001391 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 buf->f_type = UFS_MAGIC;
1393 buf->f_blocks = uspi->s_dsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001395 buf->f_bfree = ufs_blkstofrags(uspi->cs_total.cs_nbfree) +
1396 uspi->cs_total.cs_nffree;
1397 buf->f_ffree = uspi->cs_total.cs_nifree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 buf->f_bsize = sb->s_blocksize;
1399 buf->f_bavail = (buf->f_bfree > (((long)buf->f_blocks / 100) * uspi->s_minfree))
1400 ? (buf->f_bfree - (((long)buf->f_blocks / 100) * uspi->s_minfree)) : 0;
1401 buf->f_files = uspi->s_ncg * uspi->s_ipg;
1402 buf->f_namelen = UFS_MAXNAMLEN;
Coly Li41d577a2009-04-02 16:59:44 -07001403 buf->f_fsid.val[0] = (u32)id;
1404 buf->f_fsid.val[1] = (u32)(id >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Arnd Bergmann788257d2011-01-24 10:14:12 +01001406 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 return 0;
1409}
1410
Christoph Lametere18b8902006-12-06 20:33:20 -08001411static struct kmem_cache * ufs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413static struct inode *ufs_alloc_inode(struct super_block *sb)
1414{
1415 struct ufs_inode_info *ei;
Arnd Bergmann788257d2011-01-24 10:14:12 +01001416 ei = (struct ufs_inode_info *)kmem_cache_alloc(ufs_inode_cachep, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (!ei)
1418 return NULL;
1419 ei->vfs_inode.i_version = 1;
1420 return &ei->vfs_inode;
1421}
1422
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001423static void ufs_i_callback(struct rcu_head *head)
1424{
1425 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001426 kmem_cache_free(ufs_inode_cachep, UFS_I(inode));
1427}
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429static void ufs_destroy_inode(struct inode *inode)
1430{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001431 call_rcu(&inode->i_rcu, ufs_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001434static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 struct ufs_inode_info *ei = (struct ufs_inode_info *) foo;
1437
Christoph Lametera35afb82007-05-16 22:10:57 -07001438 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
Paul Mundt20c2df82007-07-20 10:11:58 +09001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441static int init_inodecache(void)
1442{
1443 ufs_inode_cachep = kmem_cache_create("ufs_inode_cache",
1444 sizeof(struct ufs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001445 0, (SLAB_RECLAIM_ACCOUNT|
1446 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001447 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (ufs_inode_cachep == NULL)
1449 return -ENOMEM;
1450 return 0;
1451}
1452
1453static void destroy_inodecache(void)
1454{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001455 kmem_cache_destroy(ufs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001458static const struct super_operations ufs_super_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 .alloc_inode = ufs_alloc_inode,
1460 .destroy_inode = ufs_destroy_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 .write_inode = ufs_write_inode,
Al Viro58e82682010-06-05 19:40:56 -04001462 .evict_inode = ufs_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 .put_super = ufs_put_super,
1464 .write_super = ufs_write_super,
Christoph Hellwig8c800652009-06-08 10:08:05 +02001465 .sync_fs = ufs_sync_fs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 .statfs = ufs_statfs,
1467 .remount_fs = ufs_remount,
Evgeniy Dushistov2a9807c02007-10-16 23:26:32 -07001468 .show_options = ufs_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469};
1470
Al Viro152a0832010-07-25 00:46:55 +04001471static struct dentry *ufs_mount(struct file_system_type *fs_type,
1472 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Al Viro152a0832010-07-25 00:46:55 +04001474 return mount_bdev(fs_type, flags, dev_name, data, ufs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
1477static struct file_system_type ufs_fs_type = {
1478 .owner = THIS_MODULE,
1479 .name = "ufs",
Al Viro152a0832010-07-25 00:46:55 +04001480 .mount = ufs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 .kill_sb = kill_block_super,
1482 .fs_flags = FS_REQUIRES_DEV,
1483};
1484
1485static int __init init_ufs_fs(void)
1486{
1487 int err = init_inodecache();
1488 if (err)
1489 goto out1;
1490 err = register_filesystem(&ufs_fs_type);
1491 if (err)
1492 goto out;
1493 return 0;
1494out:
1495 destroy_inodecache();
1496out1:
1497 return err;
1498}
1499
1500static void __exit exit_ufs_fs(void)
1501{
1502 unregister_filesystem(&ufs_fs_type);
1503 destroy_inodecache();
1504}
1505
1506module_init(init_ufs_fs)
1507module_exit(exit_ufs_fs)
1508MODULE_LICENSE("GPL");