blob: dc8e3a861d0fcddc79d3113b179672d7b59978e8 [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{
Linus Torvalds26fe5752012-05-10 13:14:12 -0700149 struct qstr dot_dot = QSTR_INIT("..", 2);
Alexey Dobriyanf3e2a522009-12-15 16:46:51 -0800150 ino_t ino;
151
152 ino = ufs_inode_by_name(child->d_inode, &dot_dot);
153 if (!ino)
154 return ERR_PTR(-ENOENT);
155 return d_obtain_alias(ufs_iget(child->d_inode->i_sb, ino));
156}
157
158static const struct export_operations ufs_export_ops = {
159 .fh_to_dentry = ufs_fh_to_dentry,
160 .fh_to_parent = ufs_fh_to_parent,
161 .get_parent = ufs_get_parent,
162};
163
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700164#ifdef CONFIG_UFS_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/*
166 * Print contents of ufs_super_block, useful for debugging
167 */
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800168static void ufs_print_super_stuff(struct super_block *sb,
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700169 struct ufs_super_block_first *usb1,
170 struct ufs_super_block_second *usb2,
171 struct ufs_super_block_third *usb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800173 u32 magic = fs32_to_cpu(sb, usb3->fs_magic);
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 printk("ufs_print_super_stuff\n");
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800176 printk(" magic: 0x%x\n", magic);
177 if (fs32_to_cpu(sb, usb3->fs_magic) == UFS2_MAGIC) {
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700178 printk(" fs_size: %llu\n", (unsigned long long)
179 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size));
180 printk(" fs_dsize: %llu\n", (unsigned long long)
181 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize));
182 printk(" bsize: %u\n",
183 fs32_to_cpu(sb, usb1->fs_bsize));
184 printk(" fsize: %u\n",
185 fs32_to_cpu(sb, usb1->fs_fsize));
186 printk(" fs_volname: %s\n", usb2->fs_un.fs_u2.fs_volname);
187 printk(" fs_sblockloc: %llu\n", (unsigned long long)
188 fs64_to_cpu(sb, usb2->fs_un.fs_u2.fs_sblockloc));
189 printk(" cs_ndir(No of dirs): %llu\n", (unsigned long long)
190 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir));
191 printk(" cs_nbfree(No of free blocks): %llu\n",
192 (unsigned long long)
193 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800194 printk(KERN_INFO" cs_nifree(Num of free inodes): %llu\n",
195 (unsigned long long)
196 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree));
197 printk(KERN_INFO" cs_nffree(Num of free frags): %llu\n",
198 (unsigned long long)
199 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree));
Evgeniy Dushistov90b315a2008-02-08 04:20:16 -0800200 printk(KERN_INFO" fs_maxsymlinklen: %u\n",
201 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen));
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700202 } else {
203 printk(" sblkno: %u\n", fs32_to_cpu(sb, usb1->fs_sblkno));
204 printk(" cblkno: %u\n", fs32_to_cpu(sb, usb1->fs_cblkno));
205 printk(" iblkno: %u\n", fs32_to_cpu(sb, usb1->fs_iblkno));
206 printk(" dblkno: %u\n", fs32_to_cpu(sb, usb1->fs_dblkno));
207 printk(" cgoffset: %u\n",
208 fs32_to_cpu(sb, usb1->fs_cgoffset));
209 printk(" ~cgmask: 0x%x\n",
210 ~fs32_to_cpu(sb, usb1->fs_cgmask));
211 printk(" size: %u\n", fs32_to_cpu(sb, usb1->fs_size));
212 printk(" dsize: %u\n", fs32_to_cpu(sb, usb1->fs_dsize));
213 printk(" ncg: %u\n", fs32_to_cpu(sb, usb1->fs_ncg));
214 printk(" bsize: %u\n", fs32_to_cpu(sb, usb1->fs_bsize));
215 printk(" fsize: %u\n", fs32_to_cpu(sb, usb1->fs_fsize));
216 printk(" frag: %u\n", fs32_to_cpu(sb, usb1->fs_frag));
217 printk(" fragshift: %u\n",
218 fs32_to_cpu(sb, usb1->fs_fragshift));
219 printk(" ~fmask: %u\n", ~fs32_to_cpu(sb, usb1->fs_fmask));
220 printk(" fshift: %u\n", fs32_to_cpu(sb, usb1->fs_fshift));
221 printk(" sbsize: %u\n", fs32_to_cpu(sb, usb1->fs_sbsize));
222 printk(" spc: %u\n", fs32_to_cpu(sb, usb1->fs_spc));
223 printk(" cpg: %u\n", fs32_to_cpu(sb, usb1->fs_cpg));
224 printk(" ipg: %u\n", fs32_to_cpu(sb, usb1->fs_ipg));
225 printk(" fpg: %u\n", fs32_to_cpu(sb, usb1->fs_fpg));
226 printk(" csaddr: %u\n", fs32_to_cpu(sb, usb1->fs_csaddr));
227 printk(" cssize: %u\n", fs32_to_cpu(sb, usb1->fs_cssize));
228 printk(" cgsize: %u\n", fs32_to_cpu(sb, usb1->fs_cgsize));
229 printk(" fstodb: %u\n",
230 fs32_to_cpu(sb, usb1->fs_fsbtodb));
231 printk(" nrpos: %u\n", fs32_to_cpu(sb, usb3->fs_nrpos));
232 printk(" ndir %u\n",
233 fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir));
234 printk(" nifree %u\n",
235 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree));
236 printk(" nbfree %u\n",
237 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree));
238 printk(" nffree %u\n",
239 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree));
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 printk("\n");
242}
243
244/*
245 * Print contents of ufs_cylinder_group, useful for debugging
246 */
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700247static void ufs_print_cylinder_stuff(struct super_block *sb,
248 struct ufs_cylinder_group *cg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 printk("\nufs_print_cylinder_stuff\n");
Andrew Morton96710b22006-06-25 05:47:28 -0700251 printk("size of ucg: %zu\n", sizeof(struct ufs_cylinder_group));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 printk(" magic: %x\n", fs32_to_cpu(sb, cg->cg_magic));
253 printk(" time: %u\n", fs32_to_cpu(sb, cg->cg_time));
254 printk(" cgx: %u\n", fs32_to_cpu(sb, cg->cg_cgx));
255 printk(" ncyl: %u\n", fs16_to_cpu(sb, cg->cg_ncyl));
256 printk(" niblk: %u\n", fs16_to_cpu(sb, cg->cg_niblk));
257 printk(" ndblk: %u\n", fs32_to_cpu(sb, cg->cg_ndblk));
258 printk(" cs_ndir: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_ndir));
259 printk(" cs_nbfree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nbfree));
260 printk(" cs_nifree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nifree));
261 printk(" cs_nffree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nffree));
262 printk(" rotor: %u\n", fs32_to_cpu(sb, cg->cg_rotor));
263 printk(" frotor: %u\n", fs32_to_cpu(sb, cg->cg_frotor));
264 printk(" irotor: %u\n", fs32_to_cpu(sb, cg->cg_irotor));
265 printk(" frsum: %u, %u, %u, %u, %u, %u, %u, %u\n",
266 fs32_to_cpu(sb, cg->cg_frsum[0]), fs32_to_cpu(sb, cg->cg_frsum[1]),
267 fs32_to_cpu(sb, cg->cg_frsum[2]), fs32_to_cpu(sb, cg->cg_frsum[3]),
268 fs32_to_cpu(sb, cg->cg_frsum[4]), fs32_to_cpu(sb, cg->cg_frsum[5]),
269 fs32_to_cpu(sb, cg->cg_frsum[6]), fs32_to_cpu(sb, cg->cg_frsum[7]));
270 printk(" btotoff: %u\n", fs32_to_cpu(sb, cg->cg_btotoff));
271 printk(" boff: %u\n", fs32_to_cpu(sb, cg->cg_boff));
272 printk(" iuseoff: %u\n", fs32_to_cpu(sb, cg->cg_iusedoff));
273 printk(" freeoff: %u\n", fs32_to_cpu(sb, cg->cg_freeoff));
274 printk(" nextfreeoff: %u\n", fs32_to_cpu(sb, cg->cg_nextfreeoff));
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700275 printk(" clustersumoff %u\n",
276 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clustersumoff));
277 printk(" clusteroff %u\n",
278 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clusteroff));
279 printk(" nclusterblks %u\n",
280 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_nclusterblks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 printk("\n");
282}
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700283#else
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800284# define ufs_print_super_stuff(sb, usb1, usb2, usb3) /**/
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700285# define ufs_print_cylinder_stuff(sb, cg) /**/
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700286#endif /* CONFIG_UFS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800288static const struct super_operations ufs_super_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290static char error_buf[1024];
291
292void ufs_error (struct super_block * sb, const char * function,
293 const char * fmt, ...)
294{
295 struct ufs_sb_private_info * uspi;
296 struct ufs_super_block_first * usb1;
297 va_list args;
298
299 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300300 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (!(sb->s_flags & MS_RDONLY)) {
303 usb1->fs_clean = UFS_FSBAD;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700304 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300305 ufs_mark_sb_dirty(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 sb->s_flags |= MS_RDONLY;
307 }
308 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800309 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 va_end (args);
311 switch (UFS_SB(sb)->s_mount_opt & UFS_MOUNT_ONERROR) {
312 case UFS_MOUNT_ONERROR_PANIC:
313 panic ("UFS-fs panic (device %s): %s: %s\n",
314 sb->s_id, function, error_buf);
315
316 case UFS_MOUNT_ONERROR_LOCK:
317 case UFS_MOUNT_ONERROR_UMOUNT:
318 case UFS_MOUNT_ONERROR_REPAIR:
319 printk (KERN_CRIT "UFS-fs error (device %s): %s: %s\n",
320 sb->s_id, function, error_buf);
321 }
322}
323
324void ufs_panic (struct super_block * sb, const char * function,
325 const char * fmt, ...)
326{
327 struct ufs_sb_private_info * uspi;
328 struct ufs_super_block_first * usb1;
329 va_list args;
330
331 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300332 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 if (!(sb->s_flags & MS_RDONLY)) {
335 usb1->fs_clean = UFS_FSBAD;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700336 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300337 ufs_mark_sb_dirty(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800340 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 va_end (args);
342 sb->s_flags |= MS_RDONLY;
343 printk (KERN_CRIT "UFS-fs panic (device %s): %s: %s\n",
344 sb->s_id, function, error_buf);
345}
346
347void ufs_warning (struct super_block * sb, const char * function,
348 const char * fmt, ...)
349{
350 va_list args;
351
352 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800353 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 va_end (args);
355 printk (KERN_WARNING "UFS-fs warning (device %s): %s: %s\n",
356 sb->s_id, function, error_buf);
357}
358
359enum {
Evgeniy Dushistov2a9807c02007-10-16 23:26:32 -0700360 Opt_type_old = UFS_MOUNT_UFSTYPE_OLD,
361 Opt_type_sunx86 = UFS_MOUNT_UFSTYPE_SUNx86,
362 Opt_type_sun = UFS_MOUNT_UFSTYPE_SUN,
363 Opt_type_sunos = UFS_MOUNT_UFSTYPE_SUNOS,
364 Opt_type_44bsd = UFS_MOUNT_UFSTYPE_44BSD,
365 Opt_type_ufs2 = UFS_MOUNT_UFSTYPE_UFS2,
366 Opt_type_hp = UFS_MOUNT_UFSTYPE_HP,
367 Opt_type_nextstepcd = UFS_MOUNT_UFSTYPE_NEXTSTEP_CD,
368 Opt_type_nextstep = UFS_MOUNT_UFSTYPE_NEXTSTEP,
369 Opt_type_openstep = UFS_MOUNT_UFSTYPE_OPENSTEP,
370 Opt_onerror_panic = UFS_MOUNT_ONERROR_PANIC,
371 Opt_onerror_lock = UFS_MOUNT_ONERROR_LOCK,
372 Opt_onerror_umount = UFS_MOUNT_ONERROR_UMOUNT,
373 Opt_onerror_repair = UFS_MOUNT_ONERROR_REPAIR,
374 Opt_err
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375};
376
Steven Whitehousea447c092008-10-13 10:46:57 +0100377static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 {Opt_type_old, "ufstype=old"},
379 {Opt_type_sunx86, "ufstype=sunx86"},
380 {Opt_type_sun, "ufstype=sun"},
Mark Fortescue252e2112007-10-16 23:26:31 -0700381 {Opt_type_sunos, "ufstype=sunos"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 {Opt_type_44bsd, "ufstype=44bsd"},
383 {Opt_type_ufs2, "ufstype=ufs2"},
384 {Opt_type_ufs2, "ufstype=5xbsd"},
385 {Opt_type_hp, "ufstype=hp"},
386 {Opt_type_nextstepcd, "ufstype=nextstep-cd"},
387 {Opt_type_nextstep, "ufstype=nextstep"},
388 {Opt_type_openstep, "ufstype=openstep"},
Evgeniy Dushistov2a9807c02007-10-16 23:26:32 -0700389/*end of possible ufs types */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 {Opt_onerror_panic, "onerror=panic"},
391 {Opt_onerror_lock, "onerror=lock"},
392 {Opt_onerror_umount, "onerror=umount"},
393 {Opt_onerror_repair, "onerror=repair"},
394 {Opt_err, NULL}
395};
396
397static int ufs_parse_options (char * options, unsigned * mount_options)
398{
399 char * p;
400
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700401 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 if (!options)
404 return 1;
405
406 while ((p = strsep(&options, ",")) != NULL) {
407 substring_t args[MAX_OPT_ARGS];
408 int token;
409 if (!*p)
410 continue;
411
412 token = match_token(p, tokens, args);
413 switch (token) {
414 case Opt_type_old:
415 ufs_clear_opt (*mount_options, UFSTYPE);
416 ufs_set_opt (*mount_options, UFSTYPE_OLD);
417 break;
418 case Opt_type_sunx86:
419 ufs_clear_opt (*mount_options, UFSTYPE);
420 ufs_set_opt (*mount_options, UFSTYPE_SUNx86);
421 break;
422 case Opt_type_sun:
423 ufs_clear_opt (*mount_options, UFSTYPE);
424 ufs_set_opt (*mount_options, UFSTYPE_SUN);
425 break;
Mark Fortescue252e2112007-10-16 23:26:31 -0700426 case Opt_type_sunos:
427 ufs_clear_opt(*mount_options, UFSTYPE);
428 ufs_set_opt(*mount_options, UFSTYPE_SUNOS);
429 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 case Opt_type_44bsd:
431 ufs_clear_opt (*mount_options, UFSTYPE);
432 ufs_set_opt (*mount_options, UFSTYPE_44BSD);
433 break;
434 case Opt_type_ufs2:
435 ufs_clear_opt(*mount_options, UFSTYPE);
436 ufs_set_opt(*mount_options, UFSTYPE_UFS2);
437 break;
438 case Opt_type_hp:
439 ufs_clear_opt (*mount_options, UFSTYPE);
440 ufs_set_opt (*mount_options, UFSTYPE_HP);
441 break;
442 case Opt_type_nextstepcd:
443 ufs_clear_opt (*mount_options, UFSTYPE);
444 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP_CD);
445 break;
446 case Opt_type_nextstep:
447 ufs_clear_opt (*mount_options, UFSTYPE);
448 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP);
449 break;
450 case Opt_type_openstep:
451 ufs_clear_opt (*mount_options, UFSTYPE);
452 ufs_set_opt (*mount_options, UFSTYPE_OPENSTEP);
453 break;
454 case Opt_onerror_panic:
455 ufs_clear_opt (*mount_options, ONERROR);
456 ufs_set_opt (*mount_options, ONERROR_PANIC);
457 break;
458 case Opt_onerror_lock:
459 ufs_clear_opt (*mount_options, ONERROR);
460 ufs_set_opt (*mount_options, ONERROR_LOCK);
461 break;
462 case Opt_onerror_umount:
463 ufs_clear_opt (*mount_options, ONERROR);
464 ufs_set_opt (*mount_options, ONERROR_UMOUNT);
465 break;
466 case Opt_onerror_repair:
467 printk("UFS-fs: Unable to do repair on error, "
468 "will lock lock instead\n");
469 ufs_clear_opt (*mount_options, ONERROR);
470 ufs_set_opt (*mount_options, ONERROR_REPAIR);
471 break;
472 default:
473 printk("UFS-fs: Invalid option: \"%s\" "
474 "or missing value\n", p);
475 return 0;
476 }
477 }
478 return 1;
479}
480
481/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300482 * Different types of UFS hold fs_cstotal in different
483 * places, and use different data structure for it.
484 * To make things simpler we just copy fs_cstotal to ufs_sb_private_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 */
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700486static void ufs_setup_cstotal(struct super_block *sb)
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800487{
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700488 struct ufs_sb_info *sbi = UFS_SB(sb);
489 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700490 struct ufs_super_block_first *usb1;
491 struct ufs_super_block_second *usb2;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700492 struct ufs_super_block_third *usb3;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700493 unsigned mtype = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
494
495 UFSD("ENTER, mtype=%u\n", mtype);
496 usb1 = ubh_get_usb_first(uspi);
497 usb2 = ubh_get_usb_second(uspi);
498 usb3 = ubh_get_usb_third(uspi);
499
500 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
501 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
502 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
503 /*we have statistic in different place, then usual*/
504 uspi->cs_total.cs_ndir = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir);
505 uspi->cs_total.cs_nbfree = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree);
506 uspi->cs_total.cs_nifree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree);
507 uspi->cs_total.cs_nffree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree);
508 } else {
509 uspi->cs_total.cs_ndir = fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir);
510 uspi->cs_total.cs_nbfree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree);
511 uspi->cs_total.cs_nifree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree);
512 uspi->cs_total.cs_nffree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree);
513 }
514 UFSD("EXIT\n");
515}
516
517/*
518 * Read on-disk structures associated with cylinder groups
519 */
520static int ufs_read_cylinder_structures(struct super_block *sb)
521{
522 struct ufs_sb_info *sbi = UFS_SB(sb);
523 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct ufs_buffer_head * ubh;
525 unsigned char * base, * space;
526 unsigned size, blks, i;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700527 struct ufs_super_block_third *usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700529 UFSD("ENTER\n");
530
531 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /*
533 * Read cs structures from (usually) first data block
534 * on the device.
535 */
536 size = uspi->s_cssize;
537 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
Arnd Bergmann788257d2011-01-24 10:14:12 +0100538 base = space = kmalloc(size, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (!base)
540 goto failed;
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800541 sbi->s_csp = (struct ufs_csum *)space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 for (i = 0; i < blks; i += uspi->s_fpb) {
543 size = uspi->s_bsize;
544 if (i + uspi->s_fpb > blks)
545 size = (blks - i) * uspi->s_fsize;
546
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800547 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
Evgeniy7b4ee732006-01-14 11:42:06 +0300548
549 if (!ubh)
550 goto failed;
551
552 ubh_ubhcpymem (space, ubh, size);
Evgeniy7b4ee732006-01-14 11:42:06 +0300553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 space += size;
555 ubh_brelse (ubh);
556 ubh = NULL;
557 }
558
559 /*
560 * Read cylinder group (we read only first fragment from block
561 * at this time) and prepare internal data structures for cg caching.
562 */
Arnd Bergmann788257d2011-01-24 10:14:12 +0100563 if (!(sbi->s_ucg = kmalloc (sizeof(struct buffer_head *) * uspi->s_ncg, GFP_NOFS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 goto failed;
565 for (i = 0; i < uspi->s_ncg; i++)
566 sbi->s_ucg[i] = NULL;
567 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
568 sbi->s_ucpi[i] = NULL;
569 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
570 }
571 for (i = 0; i < uspi->s_ncg; i++) {
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700572 UFSD("read cg %u\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (!(sbi->s_ucg[i] = sb_bread(sb, ufs_cgcmin(i))))
574 goto failed;
575 if (!ufs_cg_chkmagic (sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data))
576 goto failed;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 ufs_print_cylinder_stuff(sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
Arnd Bergmann788257d2011-01-24 10:14:12 +0100581 if (!(sbi->s_ucpi[i] = kmalloc (sizeof(struct ufs_cg_private_info), GFP_NOFS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 goto failed;
583 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
584 }
585 sbi->s_cg_loaded = 0;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700586 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return 1;
588
589failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800590 kfree (base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (sbi->s_ucg) {
592 for (i = 0; i < uspi->s_ncg; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800593 if (sbi->s_ucg[i])
594 brelse (sbi->s_ucg[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 kfree (sbi->s_ucg);
596 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800597 kfree (sbi->s_ucpi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700599 UFSD("EXIT (FAILED)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return 0;
601}
602
603/*
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700604 * Sync our internal copy of fs_cstotal with disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 */
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700606static void ufs_put_cstotal(struct super_block *sb)
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800607{
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700608 unsigned mtype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
609 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
610 struct ufs_super_block_first *usb1;
611 struct ufs_super_block_second *usb2;
612 struct ufs_super_block_third *usb3;
613
614 UFSD("ENTER\n");
615 usb1 = ubh_get_usb_first(uspi);
616 usb2 = ubh_get_usb_second(uspi);
617 usb3 = ubh_get_usb_third(uspi);
618
619 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
620 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
621 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
622 /*we have statistic in different place, then usual*/
623 usb2->fs_un.fs_u2.cs_ndir =
624 cpu_to_fs64(sb, uspi->cs_total.cs_ndir);
625 usb2->fs_un.fs_u2.cs_nbfree =
626 cpu_to_fs64(sb, uspi->cs_total.cs_nbfree);
627 usb3->fs_un1.fs_u2.cs_nifree =
628 cpu_to_fs64(sb, uspi->cs_total.cs_nifree);
629 usb3->fs_un1.fs_u2.cs_nffree =
630 cpu_to_fs64(sb, uspi->cs_total.cs_nffree);
631 } else {
632 usb1->fs_cstotal.cs_ndir =
633 cpu_to_fs32(sb, uspi->cs_total.cs_ndir);
634 usb1->fs_cstotal.cs_nbfree =
635 cpu_to_fs32(sb, uspi->cs_total.cs_nbfree);
636 usb1->fs_cstotal.cs_nifree =
637 cpu_to_fs32(sb, uspi->cs_total.cs_nifree);
638 usb1->fs_cstotal.cs_nffree =
639 cpu_to_fs32(sb, uspi->cs_total.cs_nffree);
640 }
641 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800642 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700643 UFSD("EXIT\n");
644}
645
646/**
647 * ufs_put_super_internal() - put on-disk intrenal structures
648 * @sb: pointer to super_block structure
649 * Put on-disk structures associated with cylinder groups
650 * and write them back to disk, also update cs_total on disk
651 */
652static void ufs_put_super_internal(struct super_block *sb)
653{
654 struct ufs_sb_info *sbi = UFS_SB(sb);
655 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 struct ufs_buffer_head * ubh;
657 unsigned char * base, * space;
658 unsigned blks, size, i;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700661 UFSD("ENTER\n");
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200662
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700663 ufs_put_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 size = uspi->s_cssize;
665 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800666 base = space = (char*) sbi->s_csp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 for (i = 0; i < blks; i += uspi->s_fpb) {
668 size = uspi->s_bsize;
669 if (i + uspi->s_fpb > blks)
670 size = (blks - i) * uspi->s_fsize;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 ubh_memcpyubh (ubh, space, size);
675 space += size;
676 ubh_mark_buffer_uptodate (ubh, 1);
677 ubh_mark_buffer_dirty (ubh);
678 ubh_brelse (ubh);
679 }
680 for (i = 0; i < sbi->s_cg_loaded; i++) {
681 ufs_put_cylinder (sb, i);
682 kfree (sbi->s_ucpi[i]);
683 }
684 for (; i < UFS_MAX_GROUP_LOADED; i++)
685 kfree (sbi->s_ucpi[i]);
686 for (i = 0; i < uspi->s_ncg; i++)
687 brelse (sbi->s_ucg[i]);
688 kfree (sbi->s_ucg);
689 kfree (base);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200690
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700691 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Artem Bityutskiy7bd54ef2012-07-12 16:28:07 +0300694static int ufs_sync_fs(struct super_block *sb, int wait)
695{
696 struct ufs_sb_private_info * uspi;
697 struct ufs_super_block_first * usb1;
698 struct ufs_super_block_third * usb3;
699 unsigned flags;
700
701 lock_ufs(sb);
Marco Stornellib6963322012-10-06 12:42:28 +0200702 mutex_lock(&UFS_SB(sb)->s_lock);
Artem Bityutskiy7bd54ef2012-07-12 16:28:07 +0300703
704 UFSD("ENTER\n");
705
706 flags = UFS_SB(sb)->s_flags;
707 uspi = UFS_SB(sb)->s_uspi;
708 usb1 = ubh_get_usb_first(uspi);
709 usb3 = ubh_get_usb_third(uspi);
710
711 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
712 if ((flags & UFS_ST_MASK) == UFS_ST_SUN ||
713 (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
714 (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
715 ufs_set_fs_state(sb, usb1, usb3,
716 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
717 ufs_put_cstotal(sb);
Artem Bityutskiy7bd54ef2012-07-12 16:28:07 +0300718
719 UFSD("EXIT\n");
Marco Stornellib6963322012-10-06 12:42:28 +0200720 mutex_unlock(&UFS_SB(sb)->s_lock);
Artem Bityutskiy7bd54ef2012-07-12 16:28:07 +0300721 unlock_ufs(sb);
722
723 return 0;
724}
725
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300726static void delayed_sync_fs(struct work_struct *work)
Artem Bityutskiy7bd54ef2012-07-12 16:28:07 +0300727{
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300728 struct ufs_sb_info *sbi;
729
730 sbi = container_of(work, struct ufs_sb_info, sync_work.work);
731
732 spin_lock(&sbi->work_lock);
733 sbi->work_queued = 0;
734 spin_unlock(&sbi->work_lock);
735
736 ufs_sync_fs(sbi->sb, 1);
737}
738
739void ufs_mark_sb_dirty(struct super_block *sb)
740{
741 struct ufs_sb_info *sbi = UFS_SB(sb);
742 unsigned long delay;
743
744 spin_lock(&sbi->work_lock);
745 if (!sbi->work_queued) {
746 delay = msecs_to_jiffies(dirty_writeback_interval * 10);
747 queue_delayed_work(system_long_wq, &sbi->sync_work, delay);
748 sbi->work_queued = 1;
749 }
750 spin_unlock(&sbi->work_lock);
Artem Bityutskiy7bd54ef2012-07-12 16:28:07 +0300751}
752
753static void ufs_put_super(struct super_block *sb)
754{
755 struct ufs_sb_info * sbi = UFS_SB(sb);
756
757 UFSD("ENTER\n");
758
759 if (!(sb->s_flags & MS_RDONLY))
760 ufs_put_super_internal(sb);
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300761 cancel_delayed_work_sync(&sbi->sync_work);
Artem Bityutskiy7bd54ef2012-07-12 16:28:07 +0300762
763 ubh_brelse_uspi (sbi->s_uspi);
764 kfree (sbi->s_uspi);
765 kfree (sbi);
766 sb->s_fs_info = NULL;
767 UFSD("EXIT\n");
768 return;
769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771static int ufs_fill_super(struct super_block *sb, void *data, int silent)
772{
773 struct ufs_sb_info * sbi;
774 struct ufs_sb_private_info * uspi;
775 struct ufs_super_block_first * usb1;
776 struct ufs_super_block_second * usb2;
777 struct ufs_super_block_third * usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct ufs_buffer_head * ubh;
779 struct inode *inode;
780 unsigned block_size, super_block_size;
781 unsigned flags;
Evgeniy7b4ee732006-01-14 11:42:06 +0300782 unsigned super_block_offset;
Duane Griffin9e6766c2009-01-08 22:43:48 +0000783 unsigned maxsymlen;
David Howellsb55c4602008-02-07 00:15:48 -0800784 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 uspi = NULL;
787 ubh = NULL;
788 flags = 0;
789
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700790 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700792 sbi = kzalloc(sizeof(struct ufs_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (!sbi)
794 goto failed_nomem;
795 sb->s_fs_info = sbi;
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300796 sbi->sb = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700798 UFSD("flag %u\n", (int)(sb->s_flags & MS_RDONLY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800#ifndef CONFIG_UFS_FS_WRITE
801 if (!(sb->s_flags & MS_RDONLY)) {
802 printk("ufs was compiled with read-only support, "
803 "can't be mounted as read-write\n");
804 goto failed;
805 }
806#endif
Arnd Bergmann788257d2011-01-24 10:14:12 +0100807 mutex_init(&sbi->mutex);
Marco Stornellib6963322012-10-06 12:42:28 +0200808 mutex_init(&sbi->s_lock);
Artem Bityutskiy9e9ad5f2012-07-12 16:28:08 +0300809 spin_lock_init(&sbi->work_lock);
810 INIT_DELAYED_WORK(&sbi->sync_work, delayed_sync_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /*
812 * Set default mount options
813 * Parse mount options
814 */
815 sbi->s_mount_opt = 0;
816 ufs_set_opt (sbi->s_mount_opt, ONERROR_LOCK);
817 if (!ufs_parse_options ((char *) data, &sbi->s_mount_opt)) {
818 printk("wrong mount options\n");
819 goto failed;
820 }
821 if (!(sbi->s_mount_opt & UFS_MOUNT_UFSTYPE)) {
822 if (!silent)
823 printk("You didn't specify the type of your ufs filesystem\n\n"
824 "mount -t ufs -o ufstype="
Michael Owenc690a722006-03-24 18:21:44 +0100825 "sun|sunx86|44bsd|ufs2|5xbsd|old|hp|nextstep|nextstep-cd|openstep ...\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 ">>>WARNING<<< Wrong ufstype may corrupt your filesystem, "
827 "default is ufstype=old\n");
828 ufs_set_opt (sbi->s_mount_opt, UFSTYPE_OLD);
829 }
830
Mark Fortescue252e2112007-10-16 23:26:31 -0700831 uspi = kzalloc(sizeof(struct ufs_sb_private_info), GFP_KERNEL);
832 sbi->s_uspi = uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (!uspi)
834 goto failed;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800835 uspi->s_dirblksize = UFS_SECTOR_SIZE;
Evgeniy7b4ee732006-01-14 11:42:06 +0300836 super_block_offset=UFS_SBLOCK;
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* Keep 2Gig file limit. Some UFS variants need to override
839 this but as I don't know which I'll let those in the know loosen
840 the rules */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 switch (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) {
842 case UFS_MOUNT_UFSTYPE_44BSD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700843 UFSD("ufstype=44bsd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 uspi->s_fsize = block_size = 512;
845 uspi->s_fmask = ~(512 - 1);
846 uspi->s_fshift = 9;
847 uspi->s_sbsize = super_block_size = 1536;
848 uspi->s_sbbase = 0;
849 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
850 break;
851 case UFS_MOUNT_UFSTYPE_UFS2:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700852 UFSD("ufstype=ufs2\n");
Evgeniy7b4ee732006-01-14 11:42:06 +0300853 super_block_offset=SBLOCK_UFS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 uspi->s_fsize = block_size = 512;
855 uspi->s_fmask = ~(512 - 1);
856 uspi->s_fshift = 9;
857 uspi->s_sbsize = super_block_size = 1536;
858 uspi->s_sbbase = 0;
859 flags |= UFS_TYPE_UFS2 | UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 break;
861
862 case UFS_MOUNT_UFSTYPE_SUN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700863 UFSD("ufstype=sun\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 uspi->s_fsize = block_size = 1024;
865 uspi->s_fmask = ~(1024 - 1);
866 uspi->s_fshift = 10;
867 uspi->s_sbsize = super_block_size = 2048;
868 uspi->s_sbbase = 0;
Mark Fortescue252e2112007-10-16 23:26:31 -0700869 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUN | UFS_CG_SUN;
871 break;
872
Mark Fortescue252e2112007-10-16 23:26:31 -0700873 case UFS_MOUNT_UFSTYPE_SUNOS:
874 UFSD(("ufstype=sunos\n"))
875 uspi->s_fsize = block_size = 1024;
876 uspi->s_fmask = ~(1024 - 1);
877 uspi->s_fshift = 10;
878 uspi->s_sbsize = 2048;
879 super_block_size = 2048;
880 uspi->s_sbbase = 0;
881 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
882 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_SUNOS | UFS_CG_SUN;
883 break;
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 case UFS_MOUNT_UFSTYPE_SUNx86:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700886 UFSD("ufstype=sunx86\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 uspi->s_fsize = block_size = 1024;
888 uspi->s_fmask = ~(1024 - 1);
889 uspi->s_fshift = 10;
890 uspi->s_sbsize = super_block_size = 2048;
891 uspi->s_sbbase = 0;
Mark Fortescue252e2112007-10-16 23:26:31 -0700892 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUNx86 | UFS_CG_SUN;
894 break;
895
896 case UFS_MOUNT_UFSTYPE_OLD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700897 UFSD("ufstype=old\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 uspi->s_fsize = block_size = 1024;
899 uspi->s_fmask = ~(1024 - 1);
900 uspi->s_fshift = 10;
901 uspi->s_sbsize = super_block_size = 2048;
902 uspi->s_sbbase = 0;
903 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
904 if (!(sb->s_flags & MS_RDONLY)) {
905 if (!silent)
906 printk(KERN_INFO "ufstype=old is supported read-only\n");
907 sb->s_flags |= MS_RDONLY;
908 }
909 break;
910
911 case UFS_MOUNT_UFSTYPE_NEXTSTEP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700912 UFSD("ufstype=nextstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 uspi->s_fsize = block_size = 1024;
914 uspi->s_fmask = ~(1024 - 1);
915 uspi->s_fshift = 10;
916 uspi->s_sbsize = super_block_size = 2048;
917 uspi->s_sbbase = 0;
Evgeniy Dushistov0c664f92007-12-04 23:45:06 -0800918 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
920 if (!(sb->s_flags & MS_RDONLY)) {
921 if (!silent)
922 printk(KERN_INFO "ufstype=nextstep is supported read-only\n");
923 sb->s_flags |= MS_RDONLY;
924 }
925 break;
926
927 case UFS_MOUNT_UFSTYPE_NEXTSTEP_CD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700928 UFSD("ufstype=nextstep-cd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 uspi->s_fsize = block_size = 2048;
930 uspi->s_fmask = ~(2048 - 1);
931 uspi->s_fshift = 11;
932 uspi->s_sbsize = super_block_size = 2048;
933 uspi->s_sbbase = 0;
Evgeniy Dushistov0c664f92007-12-04 23:45:06 -0800934 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
936 if (!(sb->s_flags & MS_RDONLY)) {
937 if (!silent)
938 printk(KERN_INFO "ufstype=nextstep-cd is supported read-only\n");
939 sb->s_flags |= MS_RDONLY;
940 }
941 break;
942
943 case UFS_MOUNT_UFSTYPE_OPENSTEP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700944 UFSD("ufstype=openstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 uspi->s_fsize = block_size = 1024;
946 uspi->s_fmask = ~(1024 - 1);
947 uspi->s_fshift = 10;
948 uspi->s_sbsize = super_block_size = 2048;
949 uspi->s_sbbase = 0;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800950 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
952 if (!(sb->s_flags & MS_RDONLY)) {
953 if (!silent)
954 printk(KERN_INFO "ufstype=openstep is supported read-only\n");
955 sb->s_flags |= MS_RDONLY;
956 }
957 break;
958
959 case UFS_MOUNT_UFSTYPE_HP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700960 UFSD("ufstype=hp\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 uspi->s_fsize = block_size = 1024;
962 uspi->s_fmask = ~(1024 - 1);
963 uspi->s_fshift = 10;
964 uspi->s_sbsize = super_block_size = 2048;
965 uspi->s_sbbase = 0;
966 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
967 if (!(sb->s_flags & MS_RDONLY)) {
968 if (!silent)
969 printk(KERN_INFO "ufstype=hp is supported read-only\n");
970 sb->s_flags |= MS_RDONLY;
971 }
972 break;
973 default:
974 if (!silent)
975 printk("unknown ufstype\n");
976 goto failed;
977 }
978
979again:
980 if (!sb_set_blocksize(sb, block_size)) {
981 printk(KERN_ERR "UFS: failed to set blocksize\n");
982 goto failed;
983 }
984
985 /*
986 * read ufs super block from device
987 */
Evgeniy7b4ee732006-01-14 11:42:06 +0300988
989 ubh = ubh_bread_uspi(uspi, sb, uspi->s_sbbase + super_block_offset/block_size, super_block_size);
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (!ubh)
992 goto failed;
993
Evgeniy7b4ee732006-01-14 11:42:06 +0300994 usb1 = ubh_get_usb_first(uspi);
995 usb2 = ubh_get_usb_second(uspi);
996 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Mark Fortescue252e2112007-10-16 23:26:31 -0700998 /* Sort out mod used on SunOS 4.1.3 for fs_state */
999 uspi->s_postblformat = fs32_to_cpu(sb, usb3->fs_postblformat);
1000 if (((flags & UFS_ST_MASK) == UFS_ST_SUNOS) &&
1001 (uspi->s_postblformat != UFS_42POSTBLFMT)) {
1002 flags &= ~UFS_ST_MASK;
1003 flags |= UFS_ST_SUN;
1004 }
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /*
1007 * Check ufs magic number
1008 */
1009 sbi->s_bytesex = BYTESEX_LE;
1010 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
1011 case UFS_MAGIC:
Thomas Stewartd27d7a92010-05-26 14:42:33 -07001012 case UFS_MAGIC_BW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 case UFS2_MAGIC:
1014 case UFS_MAGIC_LFN:
1015 case UFS_MAGIC_FEA:
1016 case UFS_MAGIC_4GB:
1017 goto magic_found;
1018 }
1019 sbi->s_bytesex = BYTESEX_BE;
1020 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
1021 case UFS_MAGIC:
Thomas Stewartd27d7a92010-05-26 14:42:33 -07001022 case UFS_MAGIC_BW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 case UFS2_MAGIC:
1024 case UFS_MAGIC_LFN:
1025 case UFS_MAGIC_FEA:
1026 case UFS_MAGIC_4GB:
1027 goto magic_found;
1028 }
1029
1030 if ((((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP)
1031 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP_CD)
1032 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_OPENSTEP))
1033 && uspi->s_sbbase < 256) {
1034 ubh_brelse_uspi(uspi);
1035 ubh = NULL;
1036 uspi->s_sbbase += 8;
1037 goto again;
1038 }
1039 if (!silent)
1040 printk("ufs_read_super: bad magic number\n");
1041 goto failed;
1042
1043magic_found:
1044 /*
1045 * Check block and fragment sizes
1046 */
1047 uspi->s_bsize = fs32_to_cpu(sb, usb1->fs_bsize);
1048 uspi->s_fsize = fs32_to_cpu(sb, usb1->fs_fsize);
1049 uspi->s_sbsize = fs32_to_cpu(sb, usb1->fs_sbsize);
1050 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
1051 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
1052
vignesh babu9e8c4272007-07-15 23:41:36 -07001053 if (!is_power_of_2(uspi->s_fsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 printk(KERN_ERR "ufs_read_super: fragment size %u is not a power of 2\n",
1055 uspi->s_fsize);
1056 goto failed;
1057 }
1058 if (uspi->s_fsize < 512) {
1059 printk(KERN_ERR "ufs_read_super: fragment size %u is too small\n",
1060 uspi->s_fsize);
1061 goto failed;
1062 }
1063 if (uspi->s_fsize > 4096) {
1064 printk(KERN_ERR "ufs_read_super: fragment size %u is too large\n",
1065 uspi->s_fsize);
1066 goto failed;
1067 }
vignesh babu9e8c4272007-07-15 23:41:36 -07001068 if (!is_power_of_2(uspi->s_bsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 printk(KERN_ERR "ufs_read_super: block size %u is not a power of 2\n",
1070 uspi->s_bsize);
1071 goto failed;
1072 }
1073 if (uspi->s_bsize < 4096) {
1074 printk(KERN_ERR "ufs_read_super: block size %u is too small\n",
1075 uspi->s_bsize);
1076 goto failed;
1077 }
1078 if (uspi->s_bsize / uspi->s_fsize > 8) {
1079 printk(KERN_ERR "ufs_read_super: too many fragments per block (%u)\n",
1080 uspi->s_bsize / uspi->s_fsize);
1081 goto failed;
1082 }
1083 if (uspi->s_fsize != block_size || uspi->s_sbsize != super_block_size) {
1084 ubh_brelse_uspi(uspi);
1085 ubh = NULL;
1086 block_size = uspi->s_fsize;
1087 super_block_size = uspi->s_sbsize;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001088 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 -07001089 goto again;
1090 }
1091
Andrew Mortonf664f1f2007-10-29 14:37:21 -07001092 sbi->s_flags = flags;/*after that line some functions use s_flags*/
Evgeniy Dushistov3313e292007-02-12 00:54:31 -08001093 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 /*
1096 * Check, if file system was correctly unmounted.
1097 * If not, make it read only.
1098 */
Andrew Mortonf664f1f2007-10-29 14:37:21 -07001099 if (((flags & UFS_ST_MASK) == UFS_ST_44BSD) ||
1100 ((flags & UFS_ST_MASK) == UFS_ST_OLD) ||
1101 (((flags & UFS_ST_MASK) == UFS_ST_SUN ||
1102 (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
1103 (flags & UFS_ST_MASK) == UFS_ST_SUNx86) &&
1104 (ufs_get_fs_state(sb, usb1, usb3) == (UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 switch(usb1->fs_clean) {
1106 case UFS_FSCLEAN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001107 UFSD("fs is clean\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 break;
1109 case UFS_FSSTABLE:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001110 UFSD("fs is stable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 break;
Alex Viskovatoffb3a0fd42010-03-10 15:21:53 -08001112 case UFS_FSLOG:
1113 UFSD("fs is logging fs\n");
1114 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 case UFS_FSOSF1:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001116 UFSD("fs is DEC OSF/1\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 break;
1118 case UFS_FSACTIVE:
1119 printk("ufs_read_super: fs is active\n");
1120 sb->s_flags |= MS_RDONLY;
1121 break;
1122 case UFS_FSBAD:
1123 printk("ufs_read_super: fs is bad\n");
1124 sb->s_flags |= MS_RDONLY;
1125 break;
1126 default:
1127 printk("ufs_read_super: can't grok fs_clean 0x%x\n", usb1->fs_clean);
1128 sb->s_flags |= MS_RDONLY;
1129 break;
1130 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001131 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 printk("ufs_read_super: fs needs fsck\n");
1133 sb->s_flags |= MS_RDONLY;
1134 }
1135
1136 /*
1137 * Read ufs_super_block into internal data structures
1138 */
1139 sb->s_op = &ufs_super_ops;
Alexey Dobriyanf3e2a522009-12-15 16:46:51 -08001140 sb->s_export_op = &ufs_export_ops;
Christoph Hellwig123e9ca2010-05-19 07:16:44 -04001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 sb->s_magic = fs32_to_cpu(sb, usb3->fs_magic);
1143
1144 uspi->s_sblkno = fs32_to_cpu(sb, usb1->fs_sblkno);
1145 uspi->s_cblkno = fs32_to_cpu(sb, usb1->fs_cblkno);
1146 uspi->s_iblkno = fs32_to_cpu(sb, usb1->fs_iblkno);
1147 uspi->s_dblkno = fs32_to_cpu(sb, usb1->fs_dblkno);
1148 uspi->s_cgoffset = fs32_to_cpu(sb, usb1->fs_cgoffset);
1149 uspi->s_cgmask = fs32_to_cpu(sb, usb1->fs_cgmask);
1150
1151 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001152 uspi->s_u2_size = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size);
1153 uspi->s_u2_dsize = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
1154 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 uspi->s_size = fs32_to_cpu(sb, usb1->fs_size);
1156 uspi->s_dsize = fs32_to_cpu(sb, usb1->fs_dsize);
1157 }
1158
1159 uspi->s_ncg = fs32_to_cpu(sb, usb1->fs_ncg);
1160 /* s_bsize already set */
1161 /* s_fsize already set */
1162 uspi->s_fpb = fs32_to_cpu(sb, usb1->fs_frag);
1163 uspi->s_minfree = fs32_to_cpu(sb, usb1->fs_minfree);
1164 uspi->s_bmask = fs32_to_cpu(sb, usb1->fs_bmask);
1165 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
1166 uspi->s_bshift = fs32_to_cpu(sb, usb1->fs_bshift);
1167 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001168 UFSD("uspi->s_bshift = %d,uspi->s_fshift = %d", uspi->s_bshift,
1169 uspi->s_fshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 uspi->s_fpbshift = fs32_to_cpu(sb, usb1->fs_fragshift);
1171 uspi->s_fsbtodb = fs32_to_cpu(sb, usb1->fs_fsbtodb);
1172 /* s_sbsize already set */
1173 uspi->s_csmask = fs32_to_cpu(sb, usb1->fs_csmask);
1174 uspi->s_csshift = fs32_to_cpu(sb, usb1->fs_csshift);
1175 uspi->s_nindir = fs32_to_cpu(sb, usb1->fs_nindir);
1176 uspi->s_inopb = fs32_to_cpu(sb, usb1->fs_inopb);
1177 uspi->s_nspf = fs32_to_cpu(sb, usb1->fs_nspf);
1178 uspi->s_npsect = ufs_get_fs_npsect(sb, usb1, usb3);
1179 uspi->s_interleave = fs32_to_cpu(sb, usb1->fs_interleave);
1180 uspi->s_trackskew = fs32_to_cpu(sb, usb1->fs_trackskew);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -08001181
1182 if (uspi->fs_magic == UFS2_MAGIC)
1183 uspi->s_csaddr = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_csaddr);
1184 else
1185 uspi->s_csaddr = fs32_to_cpu(sb, usb1->fs_csaddr);
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 uspi->s_cssize = fs32_to_cpu(sb, usb1->fs_cssize);
1188 uspi->s_cgsize = fs32_to_cpu(sb, usb1->fs_cgsize);
1189 uspi->s_ntrak = fs32_to_cpu(sb, usb1->fs_ntrak);
1190 uspi->s_nsect = fs32_to_cpu(sb, usb1->fs_nsect);
1191 uspi->s_spc = fs32_to_cpu(sb, usb1->fs_spc);
1192 uspi->s_ipg = fs32_to_cpu(sb, usb1->fs_ipg);
1193 uspi->s_fpg = fs32_to_cpu(sb, usb1->fs_fpg);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001194 uspi->s_cpc = fs32_to_cpu(sb, usb2->fs_un.fs_u1.fs_cpc);
1195 uspi->s_contigsumsize = fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_contigsumsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 uspi->s_qbmask = ufs_get_fs_qbmask(sb, usb3);
1197 uspi->s_qfmask = ufs_get_fs_qfmask(sb, usb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 uspi->s_nrpos = fs32_to_cpu(sb, usb3->fs_nrpos);
1199 uspi->s_postbloff = fs32_to_cpu(sb, usb3->fs_postbloff);
1200 uspi->s_rotbloff = fs32_to_cpu(sb, usb3->fs_rotbloff);
1201
1202 /*
1203 * Compute another frequently used values
1204 */
1205 uspi->s_fpbmask = uspi->s_fpb - 1;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001206 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 uspi->s_apbshift = uspi->s_bshift - 3;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001208 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 uspi->s_apbshift = uspi->s_bshift - 2;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 uspi->s_2apbshift = uspi->s_apbshift * 2;
1212 uspi->s_3apbshift = uspi->s_apbshift * 3;
1213 uspi->s_apb = 1 << uspi->s_apbshift;
1214 uspi->s_2apb = 1 << uspi->s_2apbshift;
1215 uspi->s_3apb = 1 << uspi->s_3apbshift;
1216 uspi->s_apbmask = uspi->s_apb - 1;
1217 uspi->s_nspfshift = uspi->s_fshift - UFS_SECTOR_BITS;
1218 uspi->s_nspb = uspi->s_nspf << uspi->s_fpbshift;
1219 uspi->s_inopf = uspi->s_inopb >> uspi->s_fpbshift;
1220 uspi->s_bpf = uspi->s_fsize << 3;
1221 uspi->s_bpfshift = uspi->s_fshift + 3;
1222 uspi->s_bpfmask = uspi->s_bpf - 1;
Evgeniy Dushistov90b315a2008-02-08 04:20:16 -08001223 if ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_44BSD ||
1224 (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 uspi->s_maxsymlinklen =
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001226 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Duane Griffin9e6766c2009-01-08 22:43:48 +00001228 if (uspi->fs_magic == UFS2_MAGIC)
1229 maxsymlen = 2 * 4 * (UFS_NDADDR + UFS_NINDIR);
1230 else
1231 maxsymlen = 4 * (UFS_NDADDR + UFS_NINDIR);
1232 if (uspi->s_maxsymlinklen > maxsymlen) {
1233 ufs_warning(sb, __func__, "ufs_read_super: excessive maximum "
1234 "fast symlink size (%u)\n", uspi->s_maxsymlinklen);
1235 uspi->s_maxsymlinklen = maxsymlen;
1236 }
Al Viro8de52772012-02-06 12:45:27 -05001237 sb->s_max_links = UFS_LINK_MAX;
Duane Griffin9e6766c2009-01-08 22:43:48 +00001238
David Howellsb55c4602008-02-07 00:15:48 -08001239 inode = ufs_iget(sb, UFS_ROOTINO);
1240 if (IS_ERR(inode)) {
1241 ret = PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 goto failed;
David Howellsb55c4602008-02-07 00:15:48 -08001243 }
Al Viro48fde702012-01-08 22:15:13 -05001244 sb->s_root = d_make_root(inode);
David Howellsb55c4602008-02-07 00:15:48 -08001245 if (!sb->s_root) {
1246 ret = -ENOMEM;
Al Viro48fde702012-01-08 22:15:13 -05001247 goto failed;
David Howellsb55c4602008-02-07 00:15:48 -08001248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001250 ufs_setup_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /*
1252 * Read cylinder group structures
1253 */
1254 if (!(sb->s_flags & MS_RDONLY))
1255 if (!ufs_read_cylinder_structures(sb))
1256 goto failed;
1257
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001258 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return 0;
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -08001262 if (ubh)
1263 ubh_brelse_uspi (uspi);
1264 kfree (uspi);
1265 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 sb->s_fs_info = NULL;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001267 UFSD("EXIT (FAILED)\n");
David Howellsb55c4602008-02-07 00:15:48 -08001268 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270failed_nomem:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001271 UFSD("EXIT (NOMEM)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return -ENOMEM;
1273}
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275static int ufs_remount (struct super_block *sb, int *mount_flags, char *data)
1276{
1277 struct ufs_sb_private_info * uspi;
1278 struct ufs_super_block_first * usb1;
1279 struct ufs_super_block_third * usb3;
1280 unsigned new_mount_opt, ufstype;
1281 unsigned flags;
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001282
Arnd Bergmann788257d2011-01-24 10:14:12 +01001283 lock_ufs(sb);
Marco Stornellib6963322012-10-06 12:42:28 +02001284 mutex_lock(&UFS_SB(sb)->s_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 uspi = UFS_SB(sb)->s_uspi;
1286 flags = UFS_SB(sb)->s_flags;
Evgeniy7b4ee732006-01-14 11:42:06 +03001287 usb1 = ubh_get_usb_first(uspi);
1288 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 /*
1291 * Allow the "check" option to be passed as a remount option.
1292 * It is not possible to change ufstype option during remount
1293 */
1294 ufstype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
1295 new_mount_opt = 0;
1296 ufs_set_opt (new_mount_opt, ONERROR_LOCK);
Al Virobbd68512009-05-06 10:43:07 -04001297 if (!ufs_parse_options (data, &new_mount_opt)) {
Marco Stornellib6963322012-10-06 12:42:28 +02001298 mutex_unlock(&UFS_SB(sb)->s_lock);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001299 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return -EINVAL;
Al Virobbd68512009-05-06 10:43:07 -04001301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (!(new_mount_opt & UFS_MOUNT_UFSTYPE)) {
1303 new_mount_opt |= ufstype;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001304 } else if ((new_mount_opt & UFS_MOUNT_UFSTYPE) != ufstype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 printk("ufstype can't be changed during remount\n");
Marco Stornellib6963322012-10-06 12:42:28 +02001306 mutex_unlock(&UFS_SB(sb)->s_lock);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001307 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return -EINVAL;
1309 }
1310
1311 if ((*mount_flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
1312 UFS_SB(sb)->s_mount_opt = new_mount_opt;
Marco Stornellib6963322012-10-06 12:42:28 +02001313 mutex_unlock(&UFS_SB(sb)->s_lock);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001314 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return 0;
1316 }
1317
1318 /*
1319 * fs was mouted as rw, remounting ro
1320 */
1321 if (*mount_flags & MS_RDONLY) {
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001322 ufs_put_super_internal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1324 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
Mark Fortescue252e2112007-10-16 23:26:31 -07001325 || (flags & UFS_ST_MASK) == UFS_ST_SUNOS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1327 ufs_set_fs_state(sb, usb1, usb3,
1328 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -07001329 ubh_mark_buffer_dirty (USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 sb->s_flags |= MS_RDONLY;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001331 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 /*
1333 * fs was mounted as ro, remounting rw
1334 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335#ifndef CONFIG_UFS_FS_WRITE
1336 printk("ufs was compiled with read-only support, "
1337 "can't be mounted as read-write\n");
Marco Stornellib6963322012-10-06 12:42:28 +02001338 mutex_unlock(&UFS_SB(sb)->s_lock);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001339 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return -EINVAL;
1341#else
1342 if (ufstype != UFS_MOUNT_UFSTYPE_SUN &&
Mark Fortescue252e2112007-10-16 23:26:31 -07001343 ufstype != UFS_MOUNT_UFSTYPE_SUNOS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 ufstype != UFS_MOUNT_UFSTYPE_44BSD &&
Evgeniy Dushistovcbcae392007-02-12 00:54:30 -08001345 ufstype != UFS_MOUNT_UFSTYPE_SUNx86 &&
1346 ufstype != UFS_MOUNT_UFSTYPE_UFS2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 printk("this ufstype is read-only supported\n");
Marco Stornellib6963322012-10-06 12:42:28 +02001348 mutex_unlock(&UFS_SB(sb)->s_lock);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001349 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return -EINVAL;
1351 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001352 if (!ufs_read_cylinder_structures(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 printk("failed during remounting\n");
Marco Stornellib6963322012-10-06 12:42:28 +02001354 mutex_unlock(&UFS_SB(sb)->s_lock);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001355 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return -EPERM;
1357 }
1358 sb->s_flags &= ~MS_RDONLY;
1359#endif
1360 }
1361 UFS_SB(sb)->s_mount_opt = new_mount_opt;
Marco Stornellib6963322012-10-06 12:42:28 +02001362 mutex_unlock(&UFS_SB(sb)->s_lock);
Arnd Bergmann788257d2011-01-24 10:14:12 +01001363 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return 0;
1365}
1366
Al Viro34c80b12011-12-08 21:32:45 -05001367static int ufs_show_options(struct seq_file *seq, struct dentry *root)
Evgeniy Dushistov2a9807c02007-10-16 23:26:32 -07001368{
Al Viro34c80b12011-12-08 21:32:45 -05001369 struct ufs_sb_info *sbi = UFS_SB(root->d_sb);
Evgeniy Dushistov2a9807c02007-10-16 23:26:32 -07001370 unsigned mval = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
Steven Whitehousea447c092008-10-13 10:46:57 +01001371 const struct match_token *tp = tokens;
Evgeniy Dushistov2a9807c02007-10-16 23:26:32 -07001372
1373 while (tp->token != Opt_onerror_panic && tp->token != mval)
1374 ++tp;
1375 BUG_ON(tp->token == Opt_onerror_panic);
1376 seq_printf(seq, ",%s", tp->pattern);
1377
1378 mval = sbi->s_mount_opt & UFS_MOUNT_ONERROR;
1379 while (tp->token != Opt_err && tp->token != mval)
1380 ++tp;
1381 BUG_ON(tp->token == Opt_err);
1382 seq_printf(seq, ",%s", tp->pattern);
1383
1384 return 0;
1385}
1386
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001387static int ufs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
David Howells726c3342006-06-23 02:02:58 -07001389 struct super_block *sb = dentry->d_sb;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001390 struct ufs_sb_private_info *uspi= UFS_SB(sb)->s_uspi;
1391 unsigned flags = UFS_SB(sb)->s_flags;
1392 struct ufs_super_block_first *usb1;
1393 struct ufs_super_block_second *usb2;
1394 struct ufs_super_block_third *usb3;
Coly Li41d577a2009-04-02 16:59:44 -07001395 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Arnd Bergmann788257d2011-01-24 10:14:12 +01001397 lock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001399 usb1 = ubh_get_usb_first(uspi);
1400 usb2 = ubh_get_usb_second(uspi);
1401 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
1404 buf->f_type = UFS2_MAGIC;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001405 buf->f_blocks = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001406 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 buf->f_type = UFS_MAGIC;
1408 buf->f_blocks = uspi->s_dsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001410 buf->f_bfree = ufs_blkstofrags(uspi->cs_total.cs_nbfree) +
1411 uspi->cs_total.cs_nffree;
1412 buf->f_ffree = uspi->cs_total.cs_nifree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 buf->f_bsize = sb->s_blocksize;
1414 buf->f_bavail = (buf->f_bfree > (((long)buf->f_blocks / 100) * uspi->s_minfree))
1415 ? (buf->f_bfree - (((long)buf->f_blocks / 100) * uspi->s_minfree)) : 0;
1416 buf->f_files = uspi->s_ncg * uspi->s_ipg;
1417 buf->f_namelen = UFS_MAXNAMLEN;
Coly Li41d577a2009-04-02 16:59:44 -07001418 buf->f_fsid.val[0] = (u32)id;
1419 buf->f_fsid.val[1] = (u32)(id >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Arnd Bergmann788257d2011-01-24 10:14:12 +01001421 unlock_ufs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 return 0;
1424}
1425
Christoph Lametere18b8902006-12-06 20:33:20 -08001426static struct kmem_cache * ufs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428static struct inode *ufs_alloc_inode(struct super_block *sb)
1429{
1430 struct ufs_inode_info *ei;
Arnd Bergmann788257d2011-01-24 10:14:12 +01001431 ei = (struct ufs_inode_info *)kmem_cache_alloc(ufs_inode_cachep, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (!ei)
1433 return NULL;
1434 ei->vfs_inode.i_version = 1;
1435 return &ei->vfs_inode;
1436}
1437
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001438static void ufs_i_callback(struct rcu_head *head)
1439{
1440 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001441 kmem_cache_free(ufs_inode_cachep, UFS_I(inode));
1442}
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444static void ufs_destroy_inode(struct inode *inode)
1445{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001446 call_rcu(&inode->i_rcu, ufs_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001449static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 struct ufs_inode_info *ei = (struct ufs_inode_info *) foo;
1452
Christoph Lametera35afb82007-05-16 22:10:57 -07001453 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
Paul Mundt20c2df82007-07-20 10:11:58 +09001455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456static int init_inodecache(void)
1457{
1458 ufs_inode_cachep = kmem_cache_create("ufs_inode_cache",
1459 sizeof(struct ufs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001460 0, (SLAB_RECLAIM_ACCOUNT|
1461 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001462 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (ufs_inode_cachep == NULL)
1464 return -ENOMEM;
1465 return 0;
1466}
1467
1468static void destroy_inodecache(void)
1469{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10001470 /*
1471 * Make sure all delayed rcu free inodes are flushed before we
1472 * destroy cache.
1473 */
1474 rcu_barrier();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001475 kmem_cache_destroy(ufs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001478static const struct super_operations ufs_super_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 .alloc_inode = ufs_alloc_inode,
1480 .destroy_inode = ufs_destroy_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 .write_inode = ufs_write_inode,
Al Viro58e82682010-06-05 19:40:56 -04001482 .evict_inode = ufs_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 .put_super = ufs_put_super,
Christoph Hellwig8c800652009-06-08 10:08:05 +02001484 .sync_fs = ufs_sync_fs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 .statfs = ufs_statfs,
1486 .remount_fs = ufs_remount,
Evgeniy Dushistov2a9807c02007-10-16 23:26:32 -07001487 .show_options = ufs_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488};
1489
Al Viro152a0832010-07-25 00:46:55 +04001490static struct dentry *ufs_mount(struct file_system_type *fs_type,
1491 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Al Viro152a0832010-07-25 00:46:55 +04001493 return mount_bdev(fs_type, flags, dev_name, data, ufs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
1496static struct file_system_type ufs_fs_type = {
1497 .owner = THIS_MODULE,
1498 .name = "ufs",
Al Viro152a0832010-07-25 00:46:55 +04001499 .mount = ufs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 .kill_sb = kill_block_super,
1501 .fs_flags = FS_REQUIRES_DEV,
1502};
1503
1504static int __init init_ufs_fs(void)
1505{
1506 int err = init_inodecache();
1507 if (err)
1508 goto out1;
1509 err = register_filesystem(&ufs_fs_type);
1510 if (err)
1511 goto out;
1512 return 0;
1513out:
1514 destroy_inodecache();
1515out1:
1516 return err;
1517}
1518
1519static void __exit exit_ufs_fs(void)
1520{
1521 unregister_filesystem(&ufs_fs_type);
1522 destroy_inodecache();
1523}
1524
1525module_init(init_ufs_fs)
1526module_exit(exit_ufs_fs)
1527MODULE_LICENSE("GPL");