blob: 7693d62934044b6adeec14d857a3d7702906e4db [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>
76#include <asm/system.h>
77
78#include <linux/errno.h>
79#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/slab.h>
81#include <linux/time.h>
82#include <linux/stat.h>
83#include <linux/string.h>
84#include <linux/blkdev.h>
85#include <linux/init.h>
86#include <linux/parser.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#include <linux/buffer_head.h>
88#include <linux/vfs.h>
vignesh babu9e8c4272007-07-15 23:41:36 -070089#include <linux/log2.h>
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -070090#include <linux/mount.h>
91#include <linux/seq_file.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
Arnd Bergmann788257d2011-01-24 10:14:12 +010098void lock_ufs(struct super_block *sb)
99{
100#if defined(CONFIG_SMP) || defined (CONFIG_PREEMPT)
101 struct ufs_sb_info *sbi = UFS_SB(sb);
102
103 mutex_lock(&sbi->mutex);
104 sbi->mutex_owner = current;
105#endif
106}
107
108void unlock_ufs(struct super_block *sb)
109{
110#if defined(CONFIG_SMP) || defined (CONFIG_PREEMPT)
111 struct ufs_sb_info *sbi = UFS_SB(sb);
112
113 sbi->mutex_owner = NULL;
114 mutex_unlock(&sbi->mutex);
115#endif
116}
117
Alexey Dobriyanf3e2a522009-12-15 16:46:51 -0800118static struct inode *ufs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation)
119{
120 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
121 struct inode *inode;
122
123 if (ino < UFS_ROOTINO || ino > uspi->s_ncg * uspi->s_ipg)
124 return ERR_PTR(-ESTALE);
125
126 inode = ufs_iget(sb, ino);
127 if (IS_ERR(inode))
128 return ERR_CAST(inode);
129 if (generation && inode->i_generation != generation) {
130 iput(inode);
131 return ERR_PTR(-ESTALE);
132 }
133 return inode;
134}
135
136static struct dentry *ufs_fh_to_dentry(struct super_block *sb, struct fid *fid,
137 int fh_len, int fh_type)
138{
139 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, ufs_nfs_get_inode);
140}
141
142static struct dentry *ufs_fh_to_parent(struct super_block *sb, struct fid *fid,
143 int fh_len, int fh_type)
144{
145 return generic_fh_to_parent(sb, fid, fh_len, fh_type, ufs_nfs_get_inode);
146}
147
148static struct dentry *ufs_get_parent(struct dentry *child)
149{
150 struct qstr dot_dot = {
151 .name = "..",
152 .len = 2,
153 };
154 ino_t ino;
155
156 ino = ufs_inode_by_name(child->d_inode, &dot_dot);
157 if (!ino)
158 return ERR_PTR(-ENOENT);
159 return d_obtain_alias(ufs_iget(child->d_inode->i_sb, ino));
160}
161
162static const struct export_operations ufs_export_ops = {
163 .fh_to_dentry = ufs_fh_to_dentry,
164 .fh_to_parent = ufs_fh_to_parent,
165 .get_parent = ufs_get_parent,
166};
167
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700168#ifdef CONFIG_UFS_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/*
170 * Print contents of ufs_super_block, useful for debugging
171 */
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800172static void ufs_print_super_stuff(struct super_block *sb,
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700173 struct ufs_super_block_first *usb1,
174 struct ufs_super_block_second *usb2,
175 struct ufs_super_block_third *usb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800177 u32 magic = fs32_to_cpu(sb, usb3->fs_magic);
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 printk("ufs_print_super_stuff\n");
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800180 printk(" magic: 0x%x\n", magic);
181 if (fs32_to_cpu(sb, usb3->fs_magic) == UFS2_MAGIC) {
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700182 printk(" fs_size: %llu\n", (unsigned long long)
183 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size));
184 printk(" fs_dsize: %llu\n", (unsigned long long)
185 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize));
186 printk(" bsize: %u\n",
187 fs32_to_cpu(sb, usb1->fs_bsize));
188 printk(" fsize: %u\n",
189 fs32_to_cpu(sb, usb1->fs_fsize));
190 printk(" fs_volname: %s\n", usb2->fs_un.fs_u2.fs_volname);
191 printk(" fs_sblockloc: %llu\n", (unsigned long long)
192 fs64_to_cpu(sb, usb2->fs_un.fs_u2.fs_sblockloc));
193 printk(" cs_ndir(No of dirs): %llu\n", (unsigned long long)
194 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir));
195 printk(" cs_nbfree(No of free blocks): %llu\n",
196 (unsigned long long)
197 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800198 printk(KERN_INFO" cs_nifree(Num of free inodes): %llu\n",
199 (unsigned long long)
200 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree));
201 printk(KERN_INFO" cs_nffree(Num of free frags): %llu\n",
202 (unsigned long long)
203 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree));
Evgeniy Dushistov90b315a2008-02-08 04:20:16 -0800204 printk(KERN_INFO" fs_maxsymlinklen: %u\n",
205 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen));
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700206 } else {
207 printk(" sblkno: %u\n", fs32_to_cpu(sb, usb1->fs_sblkno));
208 printk(" cblkno: %u\n", fs32_to_cpu(sb, usb1->fs_cblkno));
209 printk(" iblkno: %u\n", fs32_to_cpu(sb, usb1->fs_iblkno));
210 printk(" dblkno: %u\n", fs32_to_cpu(sb, usb1->fs_dblkno));
211 printk(" cgoffset: %u\n",
212 fs32_to_cpu(sb, usb1->fs_cgoffset));
213 printk(" ~cgmask: 0x%x\n",
214 ~fs32_to_cpu(sb, usb1->fs_cgmask));
215 printk(" size: %u\n", fs32_to_cpu(sb, usb1->fs_size));
216 printk(" dsize: %u\n", fs32_to_cpu(sb, usb1->fs_dsize));
217 printk(" ncg: %u\n", fs32_to_cpu(sb, usb1->fs_ncg));
218 printk(" bsize: %u\n", fs32_to_cpu(sb, usb1->fs_bsize));
219 printk(" fsize: %u\n", fs32_to_cpu(sb, usb1->fs_fsize));
220 printk(" frag: %u\n", fs32_to_cpu(sb, usb1->fs_frag));
221 printk(" fragshift: %u\n",
222 fs32_to_cpu(sb, usb1->fs_fragshift));
223 printk(" ~fmask: %u\n", ~fs32_to_cpu(sb, usb1->fs_fmask));
224 printk(" fshift: %u\n", fs32_to_cpu(sb, usb1->fs_fshift));
225 printk(" sbsize: %u\n", fs32_to_cpu(sb, usb1->fs_sbsize));
226 printk(" spc: %u\n", fs32_to_cpu(sb, usb1->fs_spc));
227 printk(" cpg: %u\n", fs32_to_cpu(sb, usb1->fs_cpg));
228 printk(" ipg: %u\n", fs32_to_cpu(sb, usb1->fs_ipg));
229 printk(" fpg: %u\n", fs32_to_cpu(sb, usb1->fs_fpg));
230 printk(" csaddr: %u\n", fs32_to_cpu(sb, usb1->fs_csaddr));
231 printk(" cssize: %u\n", fs32_to_cpu(sb, usb1->fs_cssize));
232 printk(" cgsize: %u\n", fs32_to_cpu(sb, usb1->fs_cgsize));
233 printk(" fstodb: %u\n",
234 fs32_to_cpu(sb, usb1->fs_fsbtodb));
235 printk(" nrpos: %u\n", fs32_to_cpu(sb, usb3->fs_nrpos));
236 printk(" ndir %u\n",
237 fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir));
238 printk(" nifree %u\n",
239 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree));
240 printk(" nbfree %u\n",
241 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree));
242 printk(" nffree %u\n",
243 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree));
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 printk("\n");
246}
247
248/*
249 * Print contents of ufs_cylinder_group, useful for debugging
250 */
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700251static void ufs_print_cylinder_stuff(struct super_block *sb,
252 struct ufs_cylinder_group *cg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 printk("\nufs_print_cylinder_stuff\n");
Andrew Morton96710b22006-06-25 05:47:28 -0700255 printk("size of ucg: %zu\n", sizeof(struct ufs_cylinder_group));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 printk(" magic: %x\n", fs32_to_cpu(sb, cg->cg_magic));
257 printk(" time: %u\n", fs32_to_cpu(sb, cg->cg_time));
258 printk(" cgx: %u\n", fs32_to_cpu(sb, cg->cg_cgx));
259 printk(" ncyl: %u\n", fs16_to_cpu(sb, cg->cg_ncyl));
260 printk(" niblk: %u\n", fs16_to_cpu(sb, cg->cg_niblk));
261 printk(" ndblk: %u\n", fs32_to_cpu(sb, cg->cg_ndblk));
262 printk(" cs_ndir: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_ndir));
263 printk(" cs_nbfree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nbfree));
264 printk(" cs_nifree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nifree));
265 printk(" cs_nffree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nffree));
266 printk(" rotor: %u\n", fs32_to_cpu(sb, cg->cg_rotor));
267 printk(" frotor: %u\n", fs32_to_cpu(sb, cg->cg_frotor));
268 printk(" irotor: %u\n", fs32_to_cpu(sb, cg->cg_irotor));
269 printk(" frsum: %u, %u, %u, %u, %u, %u, %u, %u\n",
270 fs32_to_cpu(sb, cg->cg_frsum[0]), fs32_to_cpu(sb, cg->cg_frsum[1]),
271 fs32_to_cpu(sb, cg->cg_frsum[2]), fs32_to_cpu(sb, cg->cg_frsum[3]),
272 fs32_to_cpu(sb, cg->cg_frsum[4]), fs32_to_cpu(sb, cg->cg_frsum[5]),
273 fs32_to_cpu(sb, cg->cg_frsum[6]), fs32_to_cpu(sb, cg->cg_frsum[7]));
274 printk(" btotoff: %u\n", fs32_to_cpu(sb, cg->cg_btotoff));
275 printk(" boff: %u\n", fs32_to_cpu(sb, cg->cg_boff));
276 printk(" iuseoff: %u\n", fs32_to_cpu(sb, cg->cg_iusedoff));
277 printk(" freeoff: %u\n", fs32_to_cpu(sb, cg->cg_freeoff));
278 printk(" nextfreeoff: %u\n", fs32_to_cpu(sb, cg->cg_nextfreeoff));
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700279 printk(" clustersumoff %u\n",
280 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clustersumoff));
281 printk(" clusteroff %u\n",
282 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clusteroff));
283 printk(" nclusterblks %u\n",
284 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_nclusterblks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 printk("\n");
286}
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700287#else
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800288# define ufs_print_super_stuff(sb, usb1, usb2, usb3) /**/
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700289# define ufs_print_cylinder_stuff(sb, cg) /**/
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700290#endif /* CONFIG_UFS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800292static const struct super_operations ufs_super_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294static char error_buf[1024];
295
296void ufs_error (struct super_block * sb, const char * function,
297 const char * fmt, ...)
298{
299 struct ufs_sb_private_info * uspi;
300 struct ufs_super_block_first * usb1;
301 va_list args;
302
303 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300304 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 if (!(sb->s_flags & MS_RDONLY)) {
307 usb1->fs_clean = UFS_FSBAD;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700308 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 sb->s_dirt = 1;
310 sb->s_flags |= MS_RDONLY;
311 }
312 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800313 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 va_end (args);
315 switch (UFS_SB(sb)->s_mount_opt & UFS_MOUNT_ONERROR) {
316 case UFS_MOUNT_ONERROR_PANIC:
317 panic ("UFS-fs panic (device %s): %s: %s\n",
318 sb->s_id, function, error_buf);
319
320 case UFS_MOUNT_ONERROR_LOCK:
321 case UFS_MOUNT_ONERROR_UMOUNT:
322 case UFS_MOUNT_ONERROR_REPAIR:
323 printk (KERN_CRIT "UFS-fs error (device %s): %s: %s\n",
324 sb->s_id, function, error_buf);
325 }
326}
327
328void ufs_panic (struct super_block * sb, const char * function,
329 const char * fmt, ...)
330{
331 struct ufs_sb_private_info * uspi;
332 struct ufs_super_block_first * usb1;
333 va_list args;
334
335 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300336 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if (!(sb->s_flags & MS_RDONLY)) {
339 usb1->fs_clean = UFS_FSBAD;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700340 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 sb->s_dirt = 1;
342 }
343 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800344 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 va_end (args);
346 sb->s_flags |= MS_RDONLY;
347 printk (KERN_CRIT "UFS-fs panic (device %s): %s: %s\n",
348 sb->s_id, function, error_buf);
349}
350
351void ufs_warning (struct super_block * sb, const char * function,
352 const char * fmt, ...)
353{
354 va_list args;
355
356 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800357 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 va_end (args);
359 printk (KERN_WARNING "UFS-fs warning (device %s): %s: %s\n",
360 sb->s_id, function, error_buf);
361}
362
363enum {
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -0700364 Opt_type_old = UFS_MOUNT_UFSTYPE_OLD,
365 Opt_type_sunx86 = UFS_MOUNT_UFSTYPE_SUNx86,
366 Opt_type_sun = UFS_MOUNT_UFSTYPE_SUN,
367 Opt_type_sunos = UFS_MOUNT_UFSTYPE_SUNOS,
368 Opt_type_44bsd = UFS_MOUNT_UFSTYPE_44BSD,
369 Opt_type_ufs2 = UFS_MOUNT_UFSTYPE_UFS2,
370 Opt_type_hp = UFS_MOUNT_UFSTYPE_HP,
371 Opt_type_nextstepcd = UFS_MOUNT_UFSTYPE_NEXTSTEP_CD,
372 Opt_type_nextstep = UFS_MOUNT_UFSTYPE_NEXTSTEP,
373 Opt_type_openstep = UFS_MOUNT_UFSTYPE_OPENSTEP,
374 Opt_onerror_panic = UFS_MOUNT_ONERROR_PANIC,
375 Opt_onerror_lock = UFS_MOUNT_ONERROR_LOCK,
376 Opt_onerror_umount = UFS_MOUNT_ONERROR_UMOUNT,
377 Opt_onerror_repair = UFS_MOUNT_ONERROR_REPAIR,
378 Opt_err
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379};
380
Steven Whitehousea447c092008-10-13 10:46:57 +0100381static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 {Opt_type_old, "ufstype=old"},
383 {Opt_type_sunx86, "ufstype=sunx86"},
384 {Opt_type_sun, "ufstype=sun"},
Mark Fortescue252e2112007-10-16 23:26:31 -0700385 {Opt_type_sunos, "ufstype=sunos"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 {Opt_type_44bsd, "ufstype=44bsd"},
387 {Opt_type_ufs2, "ufstype=ufs2"},
388 {Opt_type_ufs2, "ufstype=5xbsd"},
389 {Opt_type_hp, "ufstype=hp"},
390 {Opt_type_nextstepcd, "ufstype=nextstep-cd"},
391 {Opt_type_nextstep, "ufstype=nextstep"},
392 {Opt_type_openstep, "ufstype=openstep"},
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -0700393/*end of possible ufs types */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 {Opt_onerror_panic, "onerror=panic"},
395 {Opt_onerror_lock, "onerror=lock"},
396 {Opt_onerror_umount, "onerror=umount"},
397 {Opt_onerror_repair, "onerror=repair"},
398 {Opt_err, NULL}
399};
400
401static int ufs_parse_options (char * options, unsigned * mount_options)
402{
403 char * p;
404
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700405 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 if (!options)
408 return 1;
409
410 while ((p = strsep(&options, ",")) != NULL) {
411 substring_t args[MAX_OPT_ARGS];
412 int token;
413 if (!*p)
414 continue;
415
416 token = match_token(p, tokens, args);
417 switch (token) {
418 case Opt_type_old:
419 ufs_clear_opt (*mount_options, UFSTYPE);
420 ufs_set_opt (*mount_options, UFSTYPE_OLD);
421 break;
422 case Opt_type_sunx86:
423 ufs_clear_opt (*mount_options, UFSTYPE);
424 ufs_set_opt (*mount_options, UFSTYPE_SUNx86);
425 break;
426 case Opt_type_sun:
427 ufs_clear_opt (*mount_options, UFSTYPE);
428 ufs_set_opt (*mount_options, UFSTYPE_SUN);
429 break;
Mark Fortescue252e2112007-10-16 23:26:31 -0700430 case Opt_type_sunos:
431 ufs_clear_opt(*mount_options, UFSTYPE);
432 ufs_set_opt(*mount_options, UFSTYPE_SUNOS);
433 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 case Opt_type_44bsd:
435 ufs_clear_opt (*mount_options, UFSTYPE);
436 ufs_set_opt (*mount_options, UFSTYPE_44BSD);
437 break;
438 case Opt_type_ufs2:
439 ufs_clear_opt(*mount_options, UFSTYPE);
440 ufs_set_opt(*mount_options, UFSTYPE_UFS2);
441 break;
442 case Opt_type_hp:
443 ufs_clear_opt (*mount_options, UFSTYPE);
444 ufs_set_opt (*mount_options, UFSTYPE_HP);
445 break;
446 case Opt_type_nextstepcd:
447 ufs_clear_opt (*mount_options, UFSTYPE);
448 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP_CD);
449 break;
450 case Opt_type_nextstep:
451 ufs_clear_opt (*mount_options, UFSTYPE);
452 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP);
453 break;
454 case Opt_type_openstep:
455 ufs_clear_opt (*mount_options, UFSTYPE);
456 ufs_set_opt (*mount_options, UFSTYPE_OPENSTEP);
457 break;
458 case Opt_onerror_panic:
459 ufs_clear_opt (*mount_options, ONERROR);
460 ufs_set_opt (*mount_options, ONERROR_PANIC);
461 break;
462 case Opt_onerror_lock:
463 ufs_clear_opt (*mount_options, ONERROR);
464 ufs_set_opt (*mount_options, ONERROR_LOCK);
465 break;
466 case Opt_onerror_umount:
467 ufs_clear_opt (*mount_options, ONERROR);
468 ufs_set_opt (*mount_options, ONERROR_UMOUNT);
469 break;
470 case Opt_onerror_repair:
471 printk("UFS-fs: Unable to do repair on error, "
472 "will lock lock instead\n");
473 ufs_clear_opt (*mount_options, ONERROR);
474 ufs_set_opt (*mount_options, ONERROR_REPAIR);
475 break;
476 default:
477 printk("UFS-fs: Invalid option: \"%s\" "
478 "or missing value\n", p);
479 return 0;
480 }
481 }
482 return 1;
483}
484
485/*
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700486 * Diffrent types of UFS hold fs_cstotal in different
487 * places, and use diffrent data structure for it.
488 * To make things simplier we just copy fs_cstotal to ufs_sb_private_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 */
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700490static void ufs_setup_cstotal(struct super_block *sb)
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800491{
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700492 struct ufs_sb_info *sbi = UFS_SB(sb);
493 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700494 struct ufs_super_block_first *usb1;
495 struct ufs_super_block_second *usb2;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700496 struct ufs_super_block_third *usb3;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700497 unsigned mtype = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
498
499 UFSD("ENTER, mtype=%u\n", mtype);
500 usb1 = ubh_get_usb_first(uspi);
501 usb2 = ubh_get_usb_second(uspi);
502 usb3 = ubh_get_usb_third(uspi);
503
504 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
505 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
506 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
507 /*we have statistic in different place, then usual*/
508 uspi->cs_total.cs_ndir = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir);
509 uspi->cs_total.cs_nbfree = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree);
510 uspi->cs_total.cs_nifree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree);
511 uspi->cs_total.cs_nffree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree);
512 } else {
513 uspi->cs_total.cs_ndir = fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir);
514 uspi->cs_total.cs_nbfree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree);
515 uspi->cs_total.cs_nifree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree);
516 uspi->cs_total.cs_nffree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree);
517 }
518 UFSD("EXIT\n");
519}
520
521/*
522 * Read on-disk structures associated with cylinder groups
523 */
524static int ufs_read_cylinder_structures(struct super_block *sb)
525{
526 struct ufs_sb_info *sbi = UFS_SB(sb);
527 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct ufs_buffer_head * ubh;
529 unsigned char * base, * space;
530 unsigned size, blks, i;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700531 struct ufs_super_block_third *usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700533 UFSD("ENTER\n");
534
535 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /*
537 * Read cs structures from (usually) first data block
538 * on the device.
539 */
540 size = uspi->s_cssize;
541 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
Arnd Bergmann788257d2011-01-24 10:14:12 +0100542 base = space = kmalloc(size, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (!base)
544 goto failed;
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800545 sbi->s_csp = (struct ufs_csum *)space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 for (i = 0; i < blks; i += uspi->s_fpb) {
547 size = uspi->s_bsize;
548 if (i + uspi->s_fpb > blks)
549 size = (blks - i) * uspi->s_fsize;
550
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800551 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
Evgeniy7b4ee732006-01-14 11:42:06 +0300552
553 if (!ubh)
554 goto failed;
555
556 ubh_ubhcpymem (space, ubh, size);
Evgeniy7b4ee732006-01-14 11:42:06 +0300557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 space += size;
559 ubh_brelse (ubh);
560 ubh = NULL;
561 }
562
563 /*
564 * Read cylinder group (we read only first fragment from block
565 * at this time) and prepare internal data structures for cg caching.
566 */
Arnd Bergmann788257d2011-01-24 10:14:12 +0100567 if (!(sbi->s_ucg = kmalloc (sizeof(struct buffer_head *) * uspi->s_ncg, GFP_NOFS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto failed;
569 for (i = 0; i < uspi->s_ncg; i++)
570 sbi->s_ucg[i] = NULL;
571 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
572 sbi->s_ucpi[i] = NULL;
573 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
574 }
575 for (i = 0; i < uspi->s_ncg; i++) {
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700576 UFSD("read cg %u\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (!(sbi->s_ucg[i] = sb_bread(sb, ufs_cgcmin(i))))
578 goto failed;
579 if (!ufs_cg_chkmagic (sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data))
580 goto failed;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 ufs_print_cylinder_stuff(sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
Arnd Bergmann788257d2011-01-24 10:14:12 +0100585 if (!(sbi->s_ucpi[i] = kmalloc (sizeof(struct ufs_cg_private_info), GFP_NOFS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto failed;
587 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
588 }
589 sbi->s_cg_loaded = 0;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700590 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return 1;
592
593failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800594 kfree (base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (sbi->s_ucg) {
596 for (i = 0; i < uspi->s_ncg; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800597 if (sbi->s_ucg[i])
598 brelse (sbi->s_ucg[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 kfree (sbi->s_ucg);
600 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800601 kfree (sbi->s_ucpi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700603 UFSD("EXIT (FAILED)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return 0;
605}
606
607/*
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700608 * Sync our internal copy of fs_cstotal with disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700610static void ufs_put_cstotal(struct super_block *sb)
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800611{
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700612 unsigned mtype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
613 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
614 struct ufs_super_block_first *usb1;
615 struct ufs_super_block_second *usb2;
616 struct ufs_super_block_third *usb3;
617
618 UFSD("ENTER\n");
619 usb1 = ubh_get_usb_first(uspi);
620 usb2 = ubh_get_usb_second(uspi);
621 usb3 = ubh_get_usb_third(uspi);
622
623 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
624 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
625 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
626 /*we have statistic in different place, then usual*/
627 usb2->fs_un.fs_u2.cs_ndir =
628 cpu_to_fs64(sb, uspi->cs_total.cs_ndir);
629 usb2->fs_un.fs_u2.cs_nbfree =
630 cpu_to_fs64(sb, uspi->cs_total.cs_nbfree);
631 usb3->fs_un1.fs_u2.cs_nifree =
632 cpu_to_fs64(sb, uspi->cs_total.cs_nifree);
633 usb3->fs_un1.fs_u2.cs_nffree =
634 cpu_to_fs64(sb, uspi->cs_total.cs_nffree);
635 } else {
636 usb1->fs_cstotal.cs_ndir =
637 cpu_to_fs32(sb, uspi->cs_total.cs_ndir);
638 usb1->fs_cstotal.cs_nbfree =
639 cpu_to_fs32(sb, uspi->cs_total.cs_nbfree);
640 usb1->fs_cstotal.cs_nifree =
641 cpu_to_fs32(sb, uspi->cs_total.cs_nifree);
642 usb1->fs_cstotal.cs_nffree =
643 cpu_to_fs32(sb, uspi->cs_total.cs_nffree);
644 }
645 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800646 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700647 UFSD("EXIT\n");
648}
649
650/**
651 * ufs_put_super_internal() - put on-disk intrenal structures
652 * @sb: pointer to super_block structure
653 * Put on-disk structures associated with cylinder groups
654 * and write them back to disk, also update cs_total on disk
655 */
656static void ufs_put_super_internal(struct super_block *sb)
657{
658 struct ufs_sb_info *sbi = UFS_SB(sb);
659 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 struct ufs_buffer_head * ubh;
661 unsigned char * base, * space;
662 unsigned blks, size, i;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700665 UFSD("ENTER\n");
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200666
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700667 ufs_put_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 size = uspi->s_cssize;
669 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800670 base = space = (char*) sbi->s_csp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 for (i = 0; i < blks; i += uspi->s_fpb) {
672 size = uspi->s_bsize;
673 if (i + uspi->s_fpb > blks)
674 size = (blks - i) * uspi->s_fsize;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 ubh_memcpyubh (ubh, space, size);
679 space += size;
680 ubh_mark_buffer_uptodate (ubh, 1);
681 ubh_mark_buffer_dirty (ubh);
682 ubh_brelse (ubh);
683 }
684 for (i = 0; i < sbi->s_cg_loaded; i++) {
685 ufs_put_cylinder (sb, i);
686 kfree (sbi->s_ucpi[i]);
687 }
688 for (; i < UFS_MAX_GROUP_LOADED; i++)
689 kfree (sbi->s_ucpi[i]);
690 for (i = 0; i < uspi->s_ncg; i++)
691 brelse (sbi->s_ucg[i]);
692 kfree (sbi->s_ucg);
693 kfree (base);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200694
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700695 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
698static int ufs_fill_super(struct super_block *sb, void *data, int silent)
699{
700 struct ufs_sb_info * sbi;
701 struct ufs_sb_private_info * uspi;
702 struct ufs_super_block_first * usb1;
703 struct ufs_super_block_second * usb2;
704 struct ufs_super_block_third * usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 struct ufs_buffer_head * ubh;
706 struct inode *inode;
707 unsigned block_size, super_block_size;
708 unsigned flags;
Evgeniy7b4ee732006-01-14 11:42:06 +0300709 unsigned super_block_offset;
Duane Griffin9e6766c2009-01-08 22:43:48 +0000710 unsigned maxsymlen;
David Howellsb55c4602008-02-07 00:15:48 -0800711 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 uspi = NULL;
714 ubh = NULL;
715 flags = 0;
716
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700717 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700719 sbi = kzalloc(sizeof(struct ufs_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (!sbi)
721 goto failed_nomem;
722 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700724 UFSD("flag %u\n", (int)(sb->s_flags & MS_RDONLY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726#ifndef CONFIG_UFS_FS_WRITE
727 if (!(sb->s_flags & MS_RDONLY)) {
728 printk("ufs was compiled with read-only support, "
729 "can't be mounted as read-write\n");
730 goto failed;
731 }
732#endif
Arnd Bergmann788257d2011-01-24 10:14:12 +0100733 mutex_init(&sbi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /*
735 * Set default mount options
736 * Parse mount options
737 */
738 sbi->s_mount_opt = 0;
739 ufs_set_opt (sbi->s_mount_opt, ONERROR_LOCK);
740 if (!ufs_parse_options ((char *) data, &sbi->s_mount_opt)) {
741 printk("wrong mount options\n");
742 goto failed;
743 }
744 if (!(sbi->s_mount_opt & UFS_MOUNT_UFSTYPE)) {
745 if (!silent)
746 printk("You didn't specify the type of your ufs filesystem\n\n"
747 "mount -t ufs -o ufstype="
Michael Owenc690a722006-03-24 18:21:44 +0100748 "sun|sunx86|44bsd|ufs2|5xbsd|old|hp|nextstep|nextstep-cd|openstep ...\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 ">>>WARNING<<< Wrong ufstype may corrupt your filesystem, "
750 "default is ufstype=old\n");
751 ufs_set_opt (sbi->s_mount_opt, UFSTYPE_OLD);
752 }
753
Mark Fortescue252e2112007-10-16 23:26:31 -0700754 uspi = kzalloc(sizeof(struct ufs_sb_private_info), GFP_KERNEL);
755 sbi->s_uspi = uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (!uspi)
757 goto failed;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800758 uspi->s_dirblksize = UFS_SECTOR_SIZE;
Evgeniy7b4ee732006-01-14 11:42:06 +0300759 super_block_offset=UFS_SBLOCK;
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* Keep 2Gig file limit. Some UFS variants need to override
762 this but as I don't know which I'll let those in the know loosen
763 the rules */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 switch (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) {
765 case UFS_MOUNT_UFSTYPE_44BSD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700766 UFSD("ufstype=44bsd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 uspi->s_fsize = block_size = 512;
768 uspi->s_fmask = ~(512 - 1);
769 uspi->s_fshift = 9;
770 uspi->s_sbsize = super_block_size = 1536;
771 uspi->s_sbbase = 0;
772 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
773 break;
774 case UFS_MOUNT_UFSTYPE_UFS2:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700775 UFSD("ufstype=ufs2\n");
Evgeniy7b4ee732006-01-14 11:42:06 +0300776 super_block_offset=SBLOCK_UFS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 uspi->s_fsize = block_size = 512;
778 uspi->s_fmask = ~(512 - 1);
779 uspi->s_fshift = 9;
780 uspi->s_sbsize = super_block_size = 1536;
781 uspi->s_sbbase = 0;
782 flags |= UFS_TYPE_UFS2 | UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 break;
784
785 case UFS_MOUNT_UFSTYPE_SUN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700786 UFSD("ufstype=sun\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 uspi->s_fsize = block_size = 1024;
788 uspi->s_fmask = ~(1024 - 1);
789 uspi->s_fshift = 10;
790 uspi->s_sbsize = super_block_size = 2048;
791 uspi->s_sbbase = 0;
Mark Fortescue252e2112007-10-16 23:26:31 -0700792 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUN | UFS_CG_SUN;
794 break;
795
Mark Fortescue252e2112007-10-16 23:26:31 -0700796 case UFS_MOUNT_UFSTYPE_SUNOS:
797 UFSD(("ufstype=sunos\n"))
798 uspi->s_fsize = block_size = 1024;
799 uspi->s_fmask = ~(1024 - 1);
800 uspi->s_fshift = 10;
801 uspi->s_sbsize = 2048;
802 super_block_size = 2048;
803 uspi->s_sbbase = 0;
804 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
805 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_SUNOS | UFS_CG_SUN;
806 break;
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 case UFS_MOUNT_UFSTYPE_SUNx86:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700809 UFSD("ufstype=sunx86\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 uspi->s_fsize = block_size = 1024;
811 uspi->s_fmask = ~(1024 - 1);
812 uspi->s_fshift = 10;
813 uspi->s_sbsize = super_block_size = 2048;
814 uspi->s_sbbase = 0;
Mark Fortescue252e2112007-10-16 23:26:31 -0700815 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUNx86 | UFS_CG_SUN;
817 break;
818
819 case UFS_MOUNT_UFSTYPE_OLD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700820 UFSD("ufstype=old\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 uspi->s_fsize = block_size = 1024;
822 uspi->s_fmask = ~(1024 - 1);
823 uspi->s_fshift = 10;
824 uspi->s_sbsize = super_block_size = 2048;
825 uspi->s_sbbase = 0;
826 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
827 if (!(sb->s_flags & MS_RDONLY)) {
828 if (!silent)
829 printk(KERN_INFO "ufstype=old is supported read-only\n");
830 sb->s_flags |= MS_RDONLY;
831 }
832 break;
833
834 case UFS_MOUNT_UFSTYPE_NEXTSTEP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700835 UFSD("ufstype=nextstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 uspi->s_fsize = block_size = 1024;
837 uspi->s_fmask = ~(1024 - 1);
838 uspi->s_fshift = 10;
839 uspi->s_sbsize = super_block_size = 2048;
840 uspi->s_sbbase = 0;
Evgeniy Dushistov0c664f92007-12-04 23:45:06 -0800841 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
843 if (!(sb->s_flags & MS_RDONLY)) {
844 if (!silent)
845 printk(KERN_INFO "ufstype=nextstep is supported read-only\n");
846 sb->s_flags |= MS_RDONLY;
847 }
848 break;
849
850 case UFS_MOUNT_UFSTYPE_NEXTSTEP_CD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700851 UFSD("ufstype=nextstep-cd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 uspi->s_fsize = block_size = 2048;
853 uspi->s_fmask = ~(2048 - 1);
854 uspi->s_fshift = 11;
855 uspi->s_sbsize = super_block_size = 2048;
856 uspi->s_sbbase = 0;
Evgeniy Dushistov0c664f92007-12-04 23:45:06 -0800857 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
859 if (!(sb->s_flags & MS_RDONLY)) {
860 if (!silent)
861 printk(KERN_INFO "ufstype=nextstep-cd is supported read-only\n");
862 sb->s_flags |= MS_RDONLY;
863 }
864 break;
865
866 case UFS_MOUNT_UFSTYPE_OPENSTEP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700867 UFSD("ufstype=openstep\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;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800873 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
875 if (!(sb->s_flags & MS_RDONLY)) {
876 if (!silent)
877 printk(KERN_INFO "ufstype=openstep is supported read-only\n");
878 sb->s_flags |= MS_RDONLY;
879 }
880 break;
881
882 case UFS_MOUNT_UFSTYPE_HP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700883 UFSD("ufstype=hp\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 uspi->s_fsize = block_size = 1024;
885 uspi->s_fmask = ~(1024 - 1);
886 uspi->s_fshift = 10;
887 uspi->s_sbsize = super_block_size = 2048;
888 uspi->s_sbbase = 0;
889 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
890 if (!(sb->s_flags & MS_RDONLY)) {
891 if (!silent)
892 printk(KERN_INFO "ufstype=hp is supported read-only\n");
893 sb->s_flags |= MS_RDONLY;
894 }
895 break;
896 default:
897 if (!silent)
898 printk("unknown ufstype\n");
899 goto failed;
900 }
901
902again:
903 if (!sb_set_blocksize(sb, block_size)) {
904 printk(KERN_ERR "UFS: failed to set blocksize\n");
905 goto failed;
906 }
907
908 /*
909 * read ufs super block from device
910 */
Evgeniy7b4ee732006-01-14 11:42:06 +0300911
912 ubh = ubh_bread_uspi(uspi, sb, uspi->s_sbbase + super_block_offset/block_size, super_block_size);
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (!ubh)
915 goto failed;
916
Evgeniy7b4ee732006-01-14 11:42:06 +0300917 usb1 = ubh_get_usb_first(uspi);
918 usb2 = ubh_get_usb_second(uspi);
919 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Mark Fortescue252e2112007-10-16 23:26:31 -0700921 /* Sort out mod used on SunOS 4.1.3 for fs_state */
922 uspi->s_postblformat = fs32_to_cpu(sb, usb3->fs_postblformat);
923 if (((flags & UFS_ST_MASK) == UFS_ST_SUNOS) &&
924 (uspi->s_postblformat != UFS_42POSTBLFMT)) {
925 flags &= ~UFS_ST_MASK;
926 flags |= UFS_ST_SUN;
927 }
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 /*
930 * Check ufs magic number
931 */
932 sbi->s_bytesex = BYTESEX_LE;
933 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
934 case UFS_MAGIC:
Thomas Stewartd27d7a92010-05-26 14:42:33 -0700935 case UFS_MAGIC_BW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 case UFS2_MAGIC:
937 case UFS_MAGIC_LFN:
938 case UFS_MAGIC_FEA:
939 case UFS_MAGIC_4GB:
940 goto magic_found;
941 }
942 sbi->s_bytesex = BYTESEX_BE;
943 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
944 case UFS_MAGIC:
Thomas Stewartd27d7a92010-05-26 14:42:33 -0700945 case UFS_MAGIC_BW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 case UFS2_MAGIC:
947 case UFS_MAGIC_LFN:
948 case UFS_MAGIC_FEA:
949 case UFS_MAGIC_4GB:
950 goto magic_found;
951 }
952
953 if ((((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP)
954 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP_CD)
955 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_OPENSTEP))
956 && uspi->s_sbbase < 256) {
957 ubh_brelse_uspi(uspi);
958 ubh = NULL;
959 uspi->s_sbbase += 8;
960 goto again;
961 }
962 if (!silent)
963 printk("ufs_read_super: bad magic number\n");
964 goto failed;
965
966magic_found:
967 /*
968 * Check block and fragment sizes
969 */
970 uspi->s_bsize = fs32_to_cpu(sb, usb1->fs_bsize);
971 uspi->s_fsize = fs32_to_cpu(sb, usb1->fs_fsize);
972 uspi->s_sbsize = fs32_to_cpu(sb, usb1->fs_sbsize);
973 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
974 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
975
vignesh babu9e8c4272007-07-15 23:41:36 -0700976 if (!is_power_of_2(uspi->s_fsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 printk(KERN_ERR "ufs_read_super: fragment size %u is not a power of 2\n",
978 uspi->s_fsize);
979 goto failed;
980 }
981 if (uspi->s_fsize < 512) {
982 printk(KERN_ERR "ufs_read_super: fragment size %u is too small\n",
983 uspi->s_fsize);
984 goto failed;
985 }
986 if (uspi->s_fsize > 4096) {
987 printk(KERN_ERR "ufs_read_super: fragment size %u is too large\n",
988 uspi->s_fsize);
989 goto failed;
990 }
vignesh babu9e8c4272007-07-15 23:41:36 -0700991 if (!is_power_of_2(uspi->s_bsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 printk(KERN_ERR "ufs_read_super: block size %u is not a power of 2\n",
993 uspi->s_bsize);
994 goto failed;
995 }
996 if (uspi->s_bsize < 4096) {
997 printk(KERN_ERR "ufs_read_super: block size %u is too small\n",
998 uspi->s_bsize);
999 goto failed;
1000 }
1001 if (uspi->s_bsize / uspi->s_fsize > 8) {
1002 printk(KERN_ERR "ufs_read_super: too many fragments per block (%u)\n",
1003 uspi->s_bsize / uspi->s_fsize);
1004 goto failed;
1005 }
1006 if (uspi->s_fsize != block_size || uspi->s_sbsize != super_block_size) {
1007 ubh_brelse_uspi(uspi);
1008 ubh = NULL;
1009 block_size = uspi->s_fsize;
1010 super_block_size = uspi->s_sbsize;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001011 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 -07001012 goto again;
1013 }
1014
Andrew Mortonf664f1f2007-10-29 14:37:21 -07001015 sbi->s_flags = flags;/*after that line some functions use s_flags*/
Evgeniy Dushistov3313e292007-02-12 00:54:31 -08001016 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 /*
1019 * Check, if file system was correctly unmounted.
1020 * If not, make it read only.
1021 */
Andrew Mortonf664f1f2007-10-29 14:37:21 -07001022 if (((flags & UFS_ST_MASK) == UFS_ST_44BSD) ||
1023 ((flags & UFS_ST_MASK) == UFS_ST_OLD) ||
1024 (((flags & UFS_ST_MASK) == UFS_ST_SUN ||
1025 (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
1026 (flags & UFS_ST_MASK) == UFS_ST_SUNx86) &&
1027 (ufs_get_fs_state(sb, usb1, usb3) == (UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 switch(usb1->fs_clean) {
1029 case UFS_FSCLEAN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001030 UFSD("fs is clean\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 break;
1032 case UFS_FSSTABLE:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001033 UFSD("fs is stable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 break;
Alex Viskovatoffb3a0fd42010-03-10 15:21:53 -08001035 case UFS_FSLOG:
1036 UFSD("fs is logging fs\n");
1037 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 case UFS_FSOSF1:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001039 UFSD("fs is DEC OSF/1\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 break;
1041 case UFS_FSACTIVE:
1042 printk("ufs_read_super: fs is active\n");
1043 sb->s_flags |= MS_RDONLY;
1044 break;
1045 case UFS_FSBAD:
1046 printk("ufs_read_super: fs is bad\n");
1047 sb->s_flags |= MS_RDONLY;
1048 break;
1049 default:
1050 printk("ufs_read_super: can't grok fs_clean 0x%x\n", usb1->fs_clean);
1051 sb->s_flags |= MS_RDONLY;
1052 break;
1053 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001054 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 printk("ufs_read_super: fs needs fsck\n");
1056 sb->s_flags |= MS_RDONLY;
1057 }
1058
1059 /*
1060 * Read ufs_super_block into internal data structures
1061 */
1062 sb->s_op = &ufs_super_ops;
Alexey Dobriyanf3e2a522009-12-15 16:46:51 -08001063 sb->s_export_op = &ufs_export_ops;
Christoph Hellwig123e9ca2010-05-19 07:16:44 -04001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 sb->s_magic = fs32_to_cpu(sb, usb3->fs_magic);
1066
1067 uspi->s_sblkno = fs32_to_cpu(sb, usb1->fs_sblkno);
1068 uspi->s_cblkno = fs32_to_cpu(sb, usb1->fs_cblkno);
1069 uspi->s_iblkno = fs32_to_cpu(sb, usb1->fs_iblkno);
1070 uspi->s_dblkno = fs32_to_cpu(sb, usb1->fs_dblkno);
1071 uspi->s_cgoffset = fs32_to_cpu(sb, usb1->fs_cgoffset);
1072 uspi->s_cgmask = fs32_to_cpu(sb, usb1->fs_cgmask);
1073
1074 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001075 uspi->s_u2_size = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size);
1076 uspi->s_u2_dsize = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
1077 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 uspi->s_size = fs32_to_cpu(sb, usb1->fs_size);
1079 uspi->s_dsize = fs32_to_cpu(sb, usb1->fs_dsize);
1080 }
1081
1082 uspi->s_ncg = fs32_to_cpu(sb, usb1->fs_ncg);
1083 /* s_bsize already set */
1084 /* s_fsize already set */
1085 uspi->s_fpb = fs32_to_cpu(sb, usb1->fs_frag);
1086 uspi->s_minfree = fs32_to_cpu(sb, usb1->fs_minfree);
1087 uspi->s_bmask = fs32_to_cpu(sb, usb1->fs_bmask);
1088 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
1089 uspi->s_bshift = fs32_to_cpu(sb, usb1->fs_bshift);
1090 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001091 UFSD("uspi->s_bshift = %d,uspi->s_fshift = %d", uspi->s_bshift,
1092 uspi->s_fshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 uspi->s_fpbshift = fs32_to_cpu(sb, usb1->fs_fragshift);
1094 uspi->s_fsbtodb = fs32_to_cpu(sb, usb1->fs_fsbtodb);
1095 /* s_sbsize already set */
1096 uspi->s_csmask = fs32_to_cpu(sb, usb1->fs_csmask);
1097 uspi->s_csshift = fs32_to_cpu(sb, usb1->fs_csshift);
1098 uspi->s_nindir = fs32_to_cpu(sb, usb1->fs_nindir);
1099 uspi->s_inopb = fs32_to_cpu(sb, usb1->fs_inopb);
1100 uspi->s_nspf = fs32_to_cpu(sb, usb1->fs_nspf);
1101 uspi->s_npsect = ufs_get_fs_npsect(sb, usb1, usb3);
1102 uspi->s_interleave = fs32_to_cpu(sb, usb1->fs_interleave);
1103 uspi->s_trackskew = fs32_to_cpu(sb, usb1->fs_trackskew);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -08001104
1105 if (uspi->fs_magic == UFS2_MAGIC)
1106 uspi->s_csaddr = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_csaddr);
1107 else
1108 uspi->s_csaddr = fs32_to_cpu(sb, usb1->fs_csaddr);
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 uspi->s_cssize = fs32_to_cpu(sb, usb1->fs_cssize);
1111 uspi->s_cgsize = fs32_to_cpu(sb, usb1->fs_cgsize);
1112 uspi->s_ntrak = fs32_to_cpu(sb, usb1->fs_ntrak);
1113 uspi->s_nsect = fs32_to_cpu(sb, usb1->fs_nsect);
1114 uspi->s_spc = fs32_to_cpu(sb, usb1->fs_spc);
1115 uspi->s_ipg = fs32_to_cpu(sb, usb1->fs_ipg);
1116 uspi->s_fpg = fs32_to_cpu(sb, usb1->fs_fpg);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001117 uspi->s_cpc = fs32_to_cpu(sb, usb2->fs_un.fs_u1.fs_cpc);
1118 uspi->s_contigsumsize = fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_contigsumsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 uspi->s_qbmask = ufs_get_fs_qbmask(sb, usb3);
1120 uspi->s_qfmask = ufs_get_fs_qfmask(sb, usb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 uspi->s_nrpos = fs32_to_cpu(sb, usb3->fs_nrpos);
1122 uspi->s_postbloff = fs32_to_cpu(sb, usb3->fs_postbloff);
1123 uspi->s_rotbloff = fs32_to_cpu(sb, usb3->fs_rotbloff);
1124
1125 /*
1126 * Compute another frequently used values
1127 */
1128 uspi->s_fpbmask = uspi->s_fpb - 1;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001129 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 uspi->s_apbshift = uspi->s_bshift - 3;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001131 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 uspi->s_apbshift = uspi->s_bshift - 2;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 uspi->s_2apbshift = uspi->s_apbshift * 2;
1135 uspi->s_3apbshift = uspi->s_apbshift * 3;
1136 uspi->s_apb = 1 << uspi->s_apbshift;
1137 uspi->s_2apb = 1 << uspi->s_2apbshift;
1138 uspi->s_3apb = 1 << uspi->s_3apbshift;
1139 uspi->s_apbmask = uspi->s_apb - 1;
1140 uspi->s_nspfshift = uspi->s_fshift - UFS_SECTOR_BITS;
1141 uspi->s_nspb = uspi->s_nspf << uspi->s_fpbshift;
1142 uspi->s_inopf = uspi->s_inopb >> uspi->s_fpbshift;
1143 uspi->s_bpf = uspi->s_fsize << 3;
1144 uspi->s_bpfshift = uspi->s_fshift + 3;
1145 uspi->s_bpfmask = uspi->s_bpf - 1;
Evgeniy Dushistov90b315a2008-02-08 04:20:16 -08001146 if ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_44BSD ||
1147 (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 uspi->s_maxsymlinklen =
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001149 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Duane Griffin9e6766c2009-01-08 22:43:48 +00001151 if (uspi->fs_magic == UFS2_MAGIC)
1152 maxsymlen = 2 * 4 * (UFS_NDADDR + UFS_NINDIR);
1153 else
1154 maxsymlen = 4 * (UFS_NDADDR + UFS_NINDIR);
1155 if (uspi->s_maxsymlinklen > maxsymlen) {
1156 ufs_warning(sb, __func__, "ufs_read_super: excessive maximum "
1157 "fast symlink size (%u)\n", uspi->s_maxsymlinklen);
1158 uspi->s_maxsymlinklen = maxsymlen;
1159 }
1160
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 sb->s_root = d_alloc_root(inode);
David Howellsb55c4602008-02-07 00:15:48 -08001167 if (!sb->s_root) {
1168 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 goto dalloc_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
1183dalloc_failed:
1184 iput(inode);
1185failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -08001186 if (ubh)
1187 ubh_brelse_uspi (uspi);
1188 kfree (uspi);
1189 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 sb->s_fs_info = NULL;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001191 UFSD("EXIT (FAILED)\n");
David Howellsb55c4602008-02-07 00:15:48 -08001192 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194failed_nomem:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001195 UFSD("EXIT (NOMEM)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return -ENOMEM;
1197}
1198
Christoph Hellwig8c800652009-06-08 10:08:05 +02001199static int ufs_sync_fs(struct super_block *sb, int wait)
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001200{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 struct ufs_sb_private_info * uspi;
1202 struct ufs_super_block_first * usb1;
1203 struct ufs_super_block_third * usb3;
1204 unsigned flags;
1205
Arnd Bergmann788257d2011-01-24 10:14:12 +01001206 lock_ufs(sb);
Christoph Hellwigebc1ac12009-05-11 23:35:03 +02001207 lock_super(sb);
Christoph Hellwig8c800652009-06-08 10:08:05 +02001208
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001209 UFSD("ENTER\n");
Christoph Hellwig8c800652009-06-08 10:08:05 +02001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 flags = UFS_SB(sb)->s_flags;
1212 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +03001213 usb1 = ubh_get_usb_first(uspi);
1214 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Christoph Hellwig8c800652009-06-08 10:08:05 +02001216 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1217 if ((flags & UFS_ST_MASK) == UFS_ST_SUN ||
1218 (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
1219 (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1220 ufs_set_fs_state(sb, usb1, usb3,
1221 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
1222 ufs_put_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 sb->s_dirt = 0;
Christoph Hellwig8c800652009-06-08 10:08:05 +02001224
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001225 UFSD("EXIT\n");
Christoph Hellwigebc1ac12009-05-11 23:35:03 +02001226 unlock_super(sb);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001227 unlock_ufs(sb);
Christoph Hellwig8c800652009-06-08 10:08:05 +02001228
1229 return 0;
1230}
1231
1232static void ufs_write_super(struct super_block *sb)
1233{
1234 if (!(sb->s_flags & MS_RDONLY))
1235 ufs_sync_fs(sb, 1);
1236 else
1237 sb->s_dirt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001240static void ufs_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 struct ufs_sb_info * sbi = UFS_SB(sb);
1243
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001244 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Christoph Hellwig8c85e122009-04-28 18:00:26 +02001246 if (sb->s_dirt)
1247 ufs_write_super(sb);
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (!(sb->s_flags & MS_RDONLY))
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001250 ufs_put_super_internal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 ubh_brelse_uspi (sbi->s_uspi);
1253 kfree (sbi->s_uspi);
1254 kfree (sbi);
1255 sb->s_fs_info = NULL;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001256 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return;
1258}
1259
1260
1261static int ufs_remount (struct super_block *sb, int *mount_flags, char *data)
1262{
1263 struct ufs_sb_private_info * uspi;
1264 struct ufs_super_block_first * usb1;
1265 struct ufs_super_block_third * usb3;
1266 unsigned new_mount_opt, ufstype;
1267 unsigned flags;
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001268
Arnd Bergmann788257d2011-01-24 10:14:12 +01001269 lock_ufs(sb);
Al Virobbd68512009-05-06 10:43:07 -04001270 lock_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 uspi = UFS_SB(sb)->s_uspi;
1272 flags = UFS_SB(sb)->s_flags;
Evgeniy7b4ee732006-01-14 11:42:06 +03001273 usb1 = ubh_get_usb_first(uspi);
1274 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 /*
1277 * Allow the "check" option to be passed as a remount option.
1278 * It is not possible to change ufstype option during remount
1279 */
1280 ufstype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
1281 new_mount_opt = 0;
1282 ufs_set_opt (new_mount_opt, ONERROR_LOCK);
Al Virobbd68512009-05-06 10:43:07 -04001283 if (!ufs_parse_options (data, &new_mount_opt)) {
1284 unlock_super(sb);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001285 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return -EINVAL;
Al Virobbd68512009-05-06 10:43:07 -04001287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (!(new_mount_opt & UFS_MOUNT_UFSTYPE)) {
1289 new_mount_opt |= ufstype;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001290 } else if ((new_mount_opt & UFS_MOUNT_UFSTYPE) != ufstype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 printk("ufstype can't be changed during remount\n");
Al Virobbd68512009-05-06 10:43:07 -04001292 unlock_super(sb);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001293 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return -EINVAL;
1295 }
1296
1297 if ((*mount_flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
1298 UFS_SB(sb)->s_mount_opt = new_mount_opt;
Al Virobbd68512009-05-06 10:43:07 -04001299 unlock_super(sb);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001300 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return 0;
1302 }
1303
1304 /*
1305 * fs was mouted as rw, remounting ro
1306 */
1307 if (*mount_flags & MS_RDONLY) {
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001308 ufs_put_super_internal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1310 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
Mark Fortescue252e2112007-10-16 23:26:31 -07001311 || (flags & UFS_ST_MASK) == UFS_ST_SUNOS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1313 ufs_set_fs_state(sb, usb1, usb3,
1314 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -07001315 ubh_mark_buffer_dirty (USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 sb->s_dirt = 0;
1317 sb->s_flags |= MS_RDONLY;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001318 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /*
1320 * fs was mounted as ro, remounting rw
1321 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322#ifndef CONFIG_UFS_FS_WRITE
1323 printk("ufs was compiled with read-only support, "
1324 "can't be mounted as read-write\n");
Al Virobbd68512009-05-06 10:43:07 -04001325 unlock_super(sb);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001326 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return -EINVAL;
1328#else
1329 if (ufstype != UFS_MOUNT_UFSTYPE_SUN &&
Mark Fortescue252e2112007-10-16 23:26:31 -07001330 ufstype != UFS_MOUNT_UFSTYPE_SUNOS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 ufstype != UFS_MOUNT_UFSTYPE_44BSD &&
Evgeniy Dushistovcbcae392007-02-12 00:54:30 -08001332 ufstype != UFS_MOUNT_UFSTYPE_SUNx86 &&
1333 ufstype != UFS_MOUNT_UFSTYPE_UFS2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 printk("this ufstype is read-only supported\n");
Al Virobbd68512009-05-06 10:43:07 -04001335 unlock_super(sb);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001336 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return -EINVAL;
1338 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001339 if (!ufs_read_cylinder_structures(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 printk("failed during remounting\n");
Al Virobbd68512009-05-06 10:43:07 -04001341 unlock_super(sb);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001342 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return -EPERM;
1344 }
1345 sb->s_flags &= ~MS_RDONLY;
1346#endif
1347 }
1348 UFS_SB(sb)->s_mount_opt = new_mount_opt;
Al Virobbd68512009-05-06 10:43:07 -04001349 unlock_super(sb);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001350 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return 0;
1352}
1353
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001354static int ufs_show_options(struct seq_file *seq, struct vfsmount *vfs)
1355{
1356 struct ufs_sb_info *sbi = UFS_SB(vfs->mnt_sb);
1357 unsigned mval = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
Steven Whitehousea447c092008-10-13 10:46:57 +01001358 const struct match_token *tp = tokens;
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001359
1360 while (tp->token != Opt_onerror_panic && tp->token != mval)
1361 ++tp;
1362 BUG_ON(tp->token == Opt_onerror_panic);
1363 seq_printf(seq, ",%s", tp->pattern);
1364
1365 mval = sbi->s_mount_opt & UFS_MOUNT_ONERROR;
1366 while (tp->token != Opt_err && tp->token != mval)
1367 ++tp;
1368 BUG_ON(tp->token == Opt_err);
1369 seq_printf(seq, ",%s", tp->pattern);
1370
1371 return 0;
1372}
1373
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001374static int ufs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
David Howells726c3342006-06-23 02:02:58 -07001376 struct super_block *sb = dentry->d_sb;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001377 struct ufs_sb_private_info *uspi= UFS_SB(sb)->s_uspi;
1378 unsigned flags = UFS_SB(sb)->s_flags;
1379 struct ufs_super_block_first *usb1;
1380 struct ufs_super_block_second *usb2;
1381 struct ufs_super_block_third *usb3;
Coly Li41d577a2009-04-02 16:59:44 -07001382 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Arnd Bergmann788257d2011-01-24 10:14:12 +01001384 lock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001386 usb1 = ubh_get_usb_first(uspi);
1387 usb2 = ubh_get_usb_second(uspi);
1388 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
1391 buf->f_type = UFS2_MAGIC;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001392 buf->f_blocks = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001393 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 buf->f_type = UFS_MAGIC;
1395 buf->f_blocks = uspi->s_dsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001397 buf->f_bfree = ufs_blkstofrags(uspi->cs_total.cs_nbfree) +
1398 uspi->cs_total.cs_nffree;
1399 buf->f_ffree = uspi->cs_total.cs_nifree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 buf->f_bsize = sb->s_blocksize;
1401 buf->f_bavail = (buf->f_bfree > (((long)buf->f_blocks / 100) * uspi->s_minfree))
1402 ? (buf->f_bfree - (((long)buf->f_blocks / 100) * uspi->s_minfree)) : 0;
1403 buf->f_files = uspi->s_ncg * uspi->s_ipg;
1404 buf->f_namelen = UFS_MAXNAMLEN;
Coly Li41d577a2009-04-02 16:59:44 -07001405 buf->f_fsid.val[0] = (u32)id;
1406 buf->f_fsid.val[1] = (u32)(id >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Arnd Bergmann788257d2011-01-24 10:14:12 +01001408 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 return 0;
1411}
1412
Christoph Lametere18b8902006-12-06 20:33:20 -08001413static struct kmem_cache * ufs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415static struct inode *ufs_alloc_inode(struct super_block *sb)
1416{
1417 struct ufs_inode_info *ei;
Arnd Bergmann788257d2011-01-24 10:14:12 +01001418 ei = (struct ufs_inode_info *)kmem_cache_alloc(ufs_inode_cachep, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (!ei)
1420 return NULL;
1421 ei->vfs_inode.i_version = 1;
1422 return &ei->vfs_inode;
1423}
1424
Nick Pigginfa0d7e32011-01-07 17:49:49 +11001425static void ufs_i_callback(struct rcu_head *head)
1426{
1427 struct inode *inode = container_of(head, struct inode, i_rcu);
1428 INIT_LIST_HEAD(&inode->i_dentry);
1429 kmem_cache_free(ufs_inode_cachep, UFS_I(inode));
1430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432static void ufs_destroy_inode(struct inode *inode)
1433{
Nick Pigginfa0d7e32011-01-07 17:49:49 +11001434 call_rcu(&inode->i_rcu, ufs_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001437static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 struct ufs_inode_info *ei = (struct ufs_inode_info *) foo;
1440
Christoph Lametera35afb82007-05-16 22:10:57 -07001441 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
Paul Mundt20c2df82007-07-20 10:11:58 +09001443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444static int init_inodecache(void)
1445{
1446 ufs_inode_cachep = kmem_cache_create("ufs_inode_cache",
1447 sizeof(struct ufs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001448 0, (SLAB_RECLAIM_ACCOUNT|
1449 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001450 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (ufs_inode_cachep == NULL)
1452 return -ENOMEM;
1453 return 0;
1454}
1455
1456static void destroy_inodecache(void)
1457{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001458 kmem_cache_destroy(ufs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001461static const struct super_operations ufs_super_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 .alloc_inode = ufs_alloc_inode,
1463 .destroy_inode = ufs_destroy_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 .write_inode = ufs_write_inode,
Al Viro58e82682010-06-05 19:40:56 -04001465 .evict_inode = ufs_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 .put_super = ufs_put_super,
1467 .write_super = ufs_write_super,
Christoph Hellwig8c800652009-06-08 10:08:05 +02001468 .sync_fs = ufs_sync_fs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 .statfs = ufs_statfs,
1470 .remount_fs = ufs_remount,
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001471 .show_options = ufs_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472};
1473
Al Viro152a0832010-07-25 00:46:55 +04001474static struct dentry *ufs_mount(struct file_system_type *fs_type,
1475 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Al Viro152a0832010-07-25 00:46:55 +04001477 return mount_bdev(fs_type, flags, dev_name, data, ufs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480static struct file_system_type ufs_fs_type = {
1481 .owner = THIS_MODULE,
1482 .name = "ufs",
Al Viro152a0832010-07-25 00:46:55 +04001483 .mount = ufs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 .kill_sb = kill_block_super,
1485 .fs_flags = FS_REQUIRES_DEV,
1486};
1487
1488static int __init init_ufs_fs(void)
1489{
1490 int err = init_inodecache();
1491 if (err)
1492 goto out1;
1493 err = register_filesystem(&ufs_fs_type);
1494 if (err)
1495 goto out;
1496 return 0;
1497out:
1498 destroy_inodecache();
1499out1:
1500 return err;
1501}
1502
1503static void __exit exit_ufs_fs(void)
1504{
1505 unregister_filesystem(&ufs_fs_type);
1506 destroy_inodecache();
1507}
1508
1509module_init(init_ufs_fs)
1510module_exit(exit_ufs_fs)
1511MODULE_LICENSE("GPL");