blob: 85b22b5977fa3d5c1d3eda3874742baf07a7d480 [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
44 * Gertjan van Wingerde <gertjan@cs.vu.nl>
45 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/module.h>
70#include <linux/bitops.h>
71
72#include <stdarg.h>
73
74#include <asm/uaccess.h>
75#include <asm/system.h>
76
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>
86#include <linux/smp_lock.h>
87#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
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070098#ifdef CONFIG_UFS_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
100 * Print contents of ufs_super_block, useful for debugging
101 */
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800102static void ufs_print_super_stuff(struct super_block *sb,
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700103 struct ufs_super_block_first *usb1,
104 struct ufs_super_block_second *usb2,
105 struct ufs_super_block_third *usb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800107 u32 magic = fs32_to_cpu(sb, usb3->fs_magic);
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 printk("ufs_print_super_stuff\n");
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800110 printk(" magic: 0x%x\n", magic);
111 if (fs32_to_cpu(sb, usb3->fs_magic) == UFS2_MAGIC) {
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700112 printk(" fs_size: %llu\n", (unsigned long long)
113 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size));
114 printk(" fs_dsize: %llu\n", (unsigned long long)
115 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize));
116 printk(" bsize: %u\n",
117 fs32_to_cpu(sb, usb1->fs_bsize));
118 printk(" fsize: %u\n",
119 fs32_to_cpu(sb, usb1->fs_fsize));
120 printk(" fs_volname: %s\n", usb2->fs_un.fs_u2.fs_volname);
121 printk(" fs_sblockloc: %llu\n", (unsigned long long)
122 fs64_to_cpu(sb, usb2->fs_un.fs_u2.fs_sblockloc));
123 printk(" cs_ndir(No of dirs): %llu\n", (unsigned long long)
124 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir));
125 printk(" cs_nbfree(No of free blocks): %llu\n",
126 (unsigned long long)
127 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800128 printk(KERN_INFO" cs_nifree(Num of free inodes): %llu\n",
129 (unsigned long long)
130 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree));
131 printk(KERN_INFO" cs_nffree(Num of free frags): %llu\n",
132 (unsigned long long)
133 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree));
Evgeniy Dushistov90b315a2008-02-08 04:20:16 -0800134 printk(KERN_INFO" fs_maxsymlinklen: %u\n",
135 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen));
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700136 } else {
137 printk(" sblkno: %u\n", fs32_to_cpu(sb, usb1->fs_sblkno));
138 printk(" cblkno: %u\n", fs32_to_cpu(sb, usb1->fs_cblkno));
139 printk(" iblkno: %u\n", fs32_to_cpu(sb, usb1->fs_iblkno));
140 printk(" dblkno: %u\n", fs32_to_cpu(sb, usb1->fs_dblkno));
141 printk(" cgoffset: %u\n",
142 fs32_to_cpu(sb, usb1->fs_cgoffset));
143 printk(" ~cgmask: 0x%x\n",
144 ~fs32_to_cpu(sb, usb1->fs_cgmask));
145 printk(" size: %u\n", fs32_to_cpu(sb, usb1->fs_size));
146 printk(" dsize: %u\n", fs32_to_cpu(sb, usb1->fs_dsize));
147 printk(" ncg: %u\n", fs32_to_cpu(sb, usb1->fs_ncg));
148 printk(" bsize: %u\n", fs32_to_cpu(sb, usb1->fs_bsize));
149 printk(" fsize: %u\n", fs32_to_cpu(sb, usb1->fs_fsize));
150 printk(" frag: %u\n", fs32_to_cpu(sb, usb1->fs_frag));
151 printk(" fragshift: %u\n",
152 fs32_to_cpu(sb, usb1->fs_fragshift));
153 printk(" ~fmask: %u\n", ~fs32_to_cpu(sb, usb1->fs_fmask));
154 printk(" fshift: %u\n", fs32_to_cpu(sb, usb1->fs_fshift));
155 printk(" sbsize: %u\n", fs32_to_cpu(sb, usb1->fs_sbsize));
156 printk(" spc: %u\n", fs32_to_cpu(sb, usb1->fs_spc));
157 printk(" cpg: %u\n", fs32_to_cpu(sb, usb1->fs_cpg));
158 printk(" ipg: %u\n", fs32_to_cpu(sb, usb1->fs_ipg));
159 printk(" fpg: %u\n", fs32_to_cpu(sb, usb1->fs_fpg));
160 printk(" csaddr: %u\n", fs32_to_cpu(sb, usb1->fs_csaddr));
161 printk(" cssize: %u\n", fs32_to_cpu(sb, usb1->fs_cssize));
162 printk(" cgsize: %u\n", fs32_to_cpu(sb, usb1->fs_cgsize));
163 printk(" fstodb: %u\n",
164 fs32_to_cpu(sb, usb1->fs_fsbtodb));
165 printk(" nrpos: %u\n", fs32_to_cpu(sb, usb3->fs_nrpos));
166 printk(" ndir %u\n",
167 fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir));
168 printk(" nifree %u\n",
169 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree));
170 printk(" nbfree %u\n",
171 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree));
172 printk(" nffree %u\n",
173 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree));
174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 printk("\n");
176}
177
178/*
179 * Print contents of ufs_cylinder_group, useful for debugging
180 */
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700181static void ufs_print_cylinder_stuff(struct super_block *sb,
182 struct ufs_cylinder_group *cg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 printk("\nufs_print_cylinder_stuff\n");
Andrew Morton96710b22006-06-25 05:47:28 -0700185 printk("size of ucg: %zu\n", sizeof(struct ufs_cylinder_group));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 printk(" magic: %x\n", fs32_to_cpu(sb, cg->cg_magic));
187 printk(" time: %u\n", fs32_to_cpu(sb, cg->cg_time));
188 printk(" cgx: %u\n", fs32_to_cpu(sb, cg->cg_cgx));
189 printk(" ncyl: %u\n", fs16_to_cpu(sb, cg->cg_ncyl));
190 printk(" niblk: %u\n", fs16_to_cpu(sb, cg->cg_niblk));
191 printk(" ndblk: %u\n", fs32_to_cpu(sb, cg->cg_ndblk));
192 printk(" cs_ndir: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_ndir));
193 printk(" cs_nbfree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nbfree));
194 printk(" cs_nifree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nifree));
195 printk(" cs_nffree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nffree));
196 printk(" rotor: %u\n", fs32_to_cpu(sb, cg->cg_rotor));
197 printk(" frotor: %u\n", fs32_to_cpu(sb, cg->cg_frotor));
198 printk(" irotor: %u\n", fs32_to_cpu(sb, cg->cg_irotor));
199 printk(" frsum: %u, %u, %u, %u, %u, %u, %u, %u\n",
200 fs32_to_cpu(sb, cg->cg_frsum[0]), fs32_to_cpu(sb, cg->cg_frsum[1]),
201 fs32_to_cpu(sb, cg->cg_frsum[2]), fs32_to_cpu(sb, cg->cg_frsum[3]),
202 fs32_to_cpu(sb, cg->cg_frsum[4]), fs32_to_cpu(sb, cg->cg_frsum[5]),
203 fs32_to_cpu(sb, cg->cg_frsum[6]), fs32_to_cpu(sb, cg->cg_frsum[7]));
204 printk(" btotoff: %u\n", fs32_to_cpu(sb, cg->cg_btotoff));
205 printk(" boff: %u\n", fs32_to_cpu(sb, cg->cg_boff));
206 printk(" iuseoff: %u\n", fs32_to_cpu(sb, cg->cg_iusedoff));
207 printk(" freeoff: %u\n", fs32_to_cpu(sb, cg->cg_freeoff));
208 printk(" nextfreeoff: %u\n", fs32_to_cpu(sb, cg->cg_nextfreeoff));
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700209 printk(" clustersumoff %u\n",
210 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clustersumoff));
211 printk(" clusteroff %u\n",
212 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clusteroff));
213 printk(" nclusterblks %u\n",
214 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_nclusterblks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 printk("\n");
216}
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700217#else
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800218# define ufs_print_super_stuff(sb, usb1, usb2, usb3) /**/
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700219# define ufs_print_cylinder_stuff(sb, cg) /**/
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700220#endif /* CONFIG_UFS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800222static const struct super_operations ufs_super_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224static char error_buf[1024];
225
226void ufs_error (struct super_block * sb, const char * function,
227 const char * fmt, ...)
228{
229 struct ufs_sb_private_info * uspi;
230 struct ufs_super_block_first * usb1;
231 va_list args;
232
233 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300234 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 if (!(sb->s_flags & MS_RDONLY)) {
237 usb1->fs_clean = UFS_FSBAD;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700238 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 sb->s_dirt = 1;
240 sb->s_flags |= MS_RDONLY;
241 }
242 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800243 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 va_end (args);
245 switch (UFS_SB(sb)->s_mount_opt & UFS_MOUNT_ONERROR) {
246 case UFS_MOUNT_ONERROR_PANIC:
247 panic ("UFS-fs panic (device %s): %s: %s\n",
248 sb->s_id, function, error_buf);
249
250 case UFS_MOUNT_ONERROR_LOCK:
251 case UFS_MOUNT_ONERROR_UMOUNT:
252 case UFS_MOUNT_ONERROR_REPAIR:
253 printk (KERN_CRIT "UFS-fs error (device %s): %s: %s\n",
254 sb->s_id, function, error_buf);
255 }
256}
257
258void ufs_panic (struct super_block * sb, const char * function,
259 const char * fmt, ...)
260{
261 struct ufs_sb_private_info * uspi;
262 struct ufs_super_block_first * usb1;
263 va_list args;
264
265 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300266 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (!(sb->s_flags & MS_RDONLY)) {
269 usb1->fs_clean = UFS_FSBAD;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700270 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 sb->s_dirt = 1;
272 }
273 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800274 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 va_end (args);
276 sb->s_flags |= MS_RDONLY;
277 printk (KERN_CRIT "UFS-fs panic (device %s): %s: %s\n",
278 sb->s_id, function, error_buf);
279}
280
281void ufs_warning (struct super_block * sb, const char * function,
282 const char * fmt, ...)
283{
284 va_list args;
285
286 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800287 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 va_end (args);
289 printk (KERN_WARNING "UFS-fs warning (device %s): %s: %s\n",
290 sb->s_id, function, error_buf);
291}
292
293enum {
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -0700294 Opt_type_old = UFS_MOUNT_UFSTYPE_OLD,
295 Opt_type_sunx86 = UFS_MOUNT_UFSTYPE_SUNx86,
296 Opt_type_sun = UFS_MOUNT_UFSTYPE_SUN,
297 Opt_type_sunos = UFS_MOUNT_UFSTYPE_SUNOS,
298 Opt_type_44bsd = UFS_MOUNT_UFSTYPE_44BSD,
299 Opt_type_ufs2 = UFS_MOUNT_UFSTYPE_UFS2,
300 Opt_type_hp = UFS_MOUNT_UFSTYPE_HP,
301 Opt_type_nextstepcd = UFS_MOUNT_UFSTYPE_NEXTSTEP_CD,
302 Opt_type_nextstep = UFS_MOUNT_UFSTYPE_NEXTSTEP,
303 Opt_type_openstep = UFS_MOUNT_UFSTYPE_OPENSTEP,
304 Opt_onerror_panic = UFS_MOUNT_ONERROR_PANIC,
305 Opt_onerror_lock = UFS_MOUNT_ONERROR_LOCK,
306 Opt_onerror_umount = UFS_MOUNT_ONERROR_UMOUNT,
307 Opt_onerror_repair = UFS_MOUNT_ONERROR_REPAIR,
308 Opt_err
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309};
310
311static match_table_t tokens = {
312 {Opt_type_old, "ufstype=old"},
313 {Opt_type_sunx86, "ufstype=sunx86"},
314 {Opt_type_sun, "ufstype=sun"},
Mark Fortescue252e2112007-10-16 23:26:31 -0700315 {Opt_type_sunos, "ufstype=sunos"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 {Opt_type_44bsd, "ufstype=44bsd"},
317 {Opt_type_ufs2, "ufstype=ufs2"},
318 {Opt_type_ufs2, "ufstype=5xbsd"},
319 {Opt_type_hp, "ufstype=hp"},
320 {Opt_type_nextstepcd, "ufstype=nextstep-cd"},
321 {Opt_type_nextstep, "ufstype=nextstep"},
322 {Opt_type_openstep, "ufstype=openstep"},
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -0700323/*end of possible ufs types */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 {Opt_onerror_panic, "onerror=panic"},
325 {Opt_onerror_lock, "onerror=lock"},
326 {Opt_onerror_umount, "onerror=umount"},
327 {Opt_onerror_repair, "onerror=repair"},
328 {Opt_err, NULL}
329};
330
331static int ufs_parse_options (char * options, unsigned * mount_options)
332{
333 char * p;
334
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700335 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 if (!options)
338 return 1;
339
340 while ((p = strsep(&options, ",")) != NULL) {
341 substring_t args[MAX_OPT_ARGS];
342 int token;
343 if (!*p)
344 continue;
345
346 token = match_token(p, tokens, args);
347 switch (token) {
348 case Opt_type_old:
349 ufs_clear_opt (*mount_options, UFSTYPE);
350 ufs_set_opt (*mount_options, UFSTYPE_OLD);
351 break;
352 case Opt_type_sunx86:
353 ufs_clear_opt (*mount_options, UFSTYPE);
354 ufs_set_opt (*mount_options, UFSTYPE_SUNx86);
355 break;
356 case Opt_type_sun:
357 ufs_clear_opt (*mount_options, UFSTYPE);
358 ufs_set_opt (*mount_options, UFSTYPE_SUN);
359 break;
Mark Fortescue252e2112007-10-16 23:26:31 -0700360 case Opt_type_sunos:
361 ufs_clear_opt(*mount_options, UFSTYPE);
362 ufs_set_opt(*mount_options, UFSTYPE_SUNOS);
363 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 case Opt_type_44bsd:
365 ufs_clear_opt (*mount_options, UFSTYPE);
366 ufs_set_opt (*mount_options, UFSTYPE_44BSD);
367 break;
368 case Opt_type_ufs2:
369 ufs_clear_opt(*mount_options, UFSTYPE);
370 ufs_set_opt(*mount_options, UFSTYPE_UFS2);
371 break;
372 case Opt_type_hp:
373 ufs_clear_opt (*mount_options, UFSTYPE);
374 ufs_set_opt (*mount_options, UFSTYPE_HP);
375 break;
376 case Opt_type_nextstepcd:
377 ufs_clear_opt (*mount_options, UFSTYPE);
378 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP_CD);
379 break;
380 case Opt_type_nextstep:
381 ufs_clear_opt (*mount_options, UFSTYPE);
382 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP);
383 break;
384 case Opt_type_openstep:
385 ufs_clear_opt (*mount_options, UFSTYPE);
386 ufs_set_opt (*mount_options, UFSTYPE_OPENSTEP);
387 break;
388 case Opt_onerror_panic:
389 ufs_clear_opt (*mount_options, ONERROR);
390 ufs_set_opt (*mount_options, ONERROR_PANIC);
391 break;
392 case Opt_onerror_lock:
393 ufs_clear_opt (*mount_options, ONERROR);
394 ufs_set_opt (*mount_options, ONERROR_LOCK);
395 break;
396 case Opt_onerror_umount:
397 ufs_clear_opt (*mount_options, ONERROR);
398 ufs_set_opt (*mount_options, ONERROR_UMOUNT);
399 break;
400 case Opt_onerror_repair:
401 printk("UFS-fs: Unable to do repair on error, "
402 "will lock lock instead\n");
403 ufs_clear_opt (*mount_options, ONERROR);
404 ufs_set_opt (*mount_options, ONERROR_REPAIR);
405 break;
406 default:
407 printk("UFS-fs: Invalid option: \"%s\" "
408 "or missing value\n", p);
409 return 0;
410 }
411 }
412 return 1;
413}
414
415/*
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700416 * Diffrent types of UFS hold fs_cstotal in different
417 * places, and use diffrent data structure for it.
418 * To make things simplier we just copy fs_cstotal to ufs_sb_private_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 */
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700420static void ufs_setup_cstotal(struct super_block *sb)
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800421{
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700422 struct ufs_sb_info *sbi = UFS_SB(sb);
423 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700424 struct ufs_super_block_first *usb1;
425 struct ufs_super_block_second *usb2;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700426 struct ufs_super_block_third *usb3;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700427 unsigned mtype = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
428
429 UFSD("ENTER, mtype=%u\n", mtype);
430 usb1 = ubh_get_usb_first(uspi);
431 usb2 = ubh_get_usb_second(uspi);
432 usb3 = ubh_get_usb_third(uspi);
433
434 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
435 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
436 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
437 /*we have statistic in different place, then usual*/
438 uspi->cs_total.cs_ndir = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir);
439 uspi->cs_total.cs_nbfree = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree);
440 uspi->cs_total.cs_nifree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree);
441 uspi->cs_total.cs_nffree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree);
442 } else {
443 uspi->cs_total.cs_ndir = fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir);
444 uspi->cs_total.cs_nbfree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree);
445 uspi->cs_total.cs_nifree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree);
446 uspi->cs_total.cs_nffree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree);
447 }
448 UFSD("EXIT\n");
449}
450
451/*
452 * Read on-disk structures associated with cylinder groups
453 */
454static int ufs_read_cylinder_structures(struct super_block *sb)
455{
456 struct ufs_sb_info *sbi = UFS_SB(sb);
457 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 struct ufs_buffer_head * ubh;
459 unsigned char * base, * space;
460 unsigned size, blks, i;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700461 struct ufs_super_block_third *usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700463 UFSD("ENTER\n");
464
465 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /*
467 * Read cs structures from (usually) first data block
468 * on the device.
469 */
470 size = uspi->s_cssize;
471 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
472 base = space = kmalloc(size, GFP_KERNEL);
473 if (!base)
474 goto failed;
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800475 sbi->s_csp = (struct ufs_csum *)space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 for (i = 0; i < blks; i += uspi->s_fpb) {
477 size = uspi->s_bsize;
478 if (i + uspi->s_fpb > blks)
479 size = (blks - i) * uspi->s_fsize;
480
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800481 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
Evgeniy7b4ee732006-01-14 11:42:06 +0300482
483 if (!ubh)
484 goto failed;
485
486 ubh_ubhcpymem (space, ubh, size);
Evgeniy7b4ee732006-01-14 11:42:06 +0300487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 space += size;
489 ubh_brelse (ubh);
490 ubh = NULL;
491 }
492
493 /*
494 * Read cylinder group (we read only first fragment from block
495 * at this time) and prepare internal data structures for cg caching.
496 */
497 if (!(sbi->s_ucg = kmalloc (sizeof(struct buffer_head *) * uspi->s_ncg, GFP_KERNEL)))
498 goto failed;
499 for (i = 0; i < uspi->s_ncg; i++)
500 sbi->s_ucg[i] = NULL;
501 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
502 sbi->s_ucpi[i] = NULL;
503 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
504 }
505 for (i = 0; i < uspi->s_ncg; i++) {
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700506 UFSD("read cg %u\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (!(sbi->s_ucg[i] = sb_bread(sb, ufs_cgcmin(i))))
508 goto failed;
509 if (!ufs_cg_chkmagic (sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data))
510 goto failed;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 ufs_print_cylinder_stuff(sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
515 if (!(sbi->s_ucpi[i] = kmalloc (sizeof(struct ufs_cg_private_info), GFP_KERNEL)))
516 goto failed;
517 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
518 }
519 sbi->s_cg_loaded = 0;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700520 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return 1;
522
523failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800524 kfree (base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (sbi->s_ucg) {
526 for (i = 0; i < uspi->s_ncg; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800527 if (sbi->s_ucg[i])
528 brelse (sbi->s_ucg[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 kfree (sbi->s_ucg);
530 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800531 kfree (sbi->s_ucpi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700533 UFSD("EXIT (FAILED)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return 0;
535}
536
537/*
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700538 * Sync our internal copy of fs_cstotal with disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700540static void ufs_put_cstotal(struct super_block *sb)
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800541{
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700542 unsigned mtype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
543 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
544 struct ufs_super_block_first *usb1;
545 struct ufs_super_block_second *usb2;
546 struct ufs_super_block_third *usb3;
547
548 UFSD("ENTER\n");
549 usb1 = ubh_get_usb_first(uspi);
550 usb2 = ubh_get_usb_second(uspi);
551 usb3 = ubh_get_usb_third(uspi);
552
553 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
554 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
555 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
556 /*we have statistic in different place, then usual*/
557 usb2->fs_un.fs_u2.cs_ndir =
558 cpu_to_fs64(sb, uspi->cs_total.cs_ndir);
559 usb2->fs_un.fs_u2.cs_nbfree =
560 cpu_to_fs64(sb, uspi->cs_total.cs_nbfree);
561 usb3->fs_un1.fs_u2.cs_nifree =
562 cpu_to_fs64(sb, uspi->cs_total.cs_nifree);
563 usb3->fs_un1.fs_u2.cs_nffree =
564 cpu_to_fs64(sb, uspi->cs_total.cs_nffree);
565 } else {
566 usb1->fs_cstotal.cs_ndir =
567 cpu_to_fs32(sb, uspi->cs_total.cs_ndir);
568 usb1->fs_cstotal.cs_nbfree =
569 cpu_to_fs32(sb, uspi->cs_total.cs_nbfree);
570 usb1->fs_cstotal.cs_nifree =
571 cpu_to_fs32(sb, uspi->cs_total.cs_nifree);
572 usb1->fs_cstotal.cs_nffree =
573 cpu_to_fs32(sb, uspi->cs_total.cs_nffree);
574 }
575 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800576 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700577 UFSD("EXIT\n");
578}
579
580/**
581 * ufs_put_super_internal() - put on-disk intrenal structures
582 * @sb: pointer to super_block structure
583 * Put on-disk structures associated with cylinder groups
584 * and write them back to disk, also update cs_total on disk
585 */
586static void ufs_put_super_internal(struct super_block *sb)
587{
588 struct ufs_sb_info *sbi = UFS_SB(sb);
589 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct ufs_buffer_head * ubh;
591 unsigned char * base, * space;
592 unsigned blks, size, i;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700595 UFSD("ENTER\n");
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700596 ufs_put_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 size = uspi->s_cssize;
598 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800599 base = space = (char*) sbi->s_csp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 for (i = 0; i < blks; i += uspi->s_fpb) {
601 size = uspi->s_bsize;
602 if (i + uspi->s_fpb > blks)
603 size = (blks - i) * uspi->s_fsize;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 ubh_memcpyubh (ubh, space, size);
608 space += size;
609 ubh_mark_buffer_uptodate (ubh, 1);
610 ubh_mark_buffer_dirty (ubh);
611 ubh_brelse (ubh);
612 }
613 for (i = 0; i < sbi->s_cg_loaded; i++) {
614 ufs_put_cylinder (sb, i);
615 kfree (sbi->s_ucpi[i]);
616 }
617 for (; i < UFS_MAX_GROUP_LOADED; i++)
618 kfree (sbi->s_ucpi[i]);
619 for (i = 0; i < uspi->s_ncg; i++)
620 brelse (sbi->s_ucg[i]);
621 kfree (sbi->s_ucg);
622 kfree (base);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700623 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
626static int ufs_fill_super(struct super_block *sb, void *data, int silent)
627{
628 struct ufs_sb_info * sbi;
629 struct ufs_sb_private_info * uspi;
630 struct ufs_super_block_first * usb1;
631 struct ufs_super_block_second * usb2;
632 struct ufs_super_block_third * usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct ufs_buffer_head * ubh;
634 struct inode *inode;
635 unsigned block_size, super_block_size;
636 unsigned flags;
Evgeniy7b4ee732006-01-14 11:42:06 +0300637 unsigned super_block_offset;
David Howellsb55c4602008-02-07 00:15:48 -0800638 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 uspi = NULL;
641 ubh = NULL;
642 flags = 0;
643
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700644 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700646 sbi = kzalloc(sizeof(struct ufs_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (!sbi)
648 goto failed_nomem;
649 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700651 UFSD("flag %u\n", (int)(sb->s_flags & MS_RDONLY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653#ifndef CONFIG_UFS_FS_WRITE
654 if (!(sb->s_flags & MS_RDONLY)) {
655 printk("ufs was compiled with read-only support, "
656 "can't be mounted as read-write\n");
657 goto failed;
658 }
659#endif
660 /*
661 * Set default mount options
662 * Parse mount options
663 */
664 sbi->s_mount_opt = 0;
665 ufs_set_opt (sbi->s_mount_opt, ONERROR_LOCK);
666 if (!ufs_parse_options ((char *) data, &sbi->s_mount_opt)) {
667 printk("wrong mount options\n");
668 goto failed;
669 }
670 if (!(sbi->s_mount_opt & UFS_MOUNT_UFSTYPE)) {
671 if (!silent)
672 printk("You didn't specify the type of your ufs filesystem\n\n"
673 "mount -t ufs -o ufstype="
Michael Owenc690a722006-03-24 18:21:44 +0100674 "sun|sunx86|44bsd|ufs2|5xbsd|old|hp|nextstep|nextstep-cd|openstep ...\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 ">>>WARNING<<< Wrong ufstype may corrupt your filesystem, "
676 "default is ufstype=old\n");
677 ufs_set_opt (sbi->s_mount_opt, UFSTYPE_OLD);
678 }
679
Mark Fortescue252e2112007-10-16 23:26:31 -0700680 uspi = kzalloc(sizeof(struct ufs_sb_private_info), GFP_KERNEL);
681 sbi->s_uspi = uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (!uspi)
683 goto failed;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800684 uspi->s_dirblksize = UFS_SECTOR_SIZE;
Evgeniy7b4ee732006-01-14 11:42:06 +0300685 super_block_offset=UFS_SBLOCK;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* Keep 2Gig file limit. Some UFS variants need to override
688 this but as I don't know which I'll let those in the know loosen
689 the rules */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 switch (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) {
691 case UFS_MOUNT_UFSTYPE_44BSD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700692 UFSD("ufstype=44bsd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 uspi->s_fsize = block_size = 512;
694 uspi->s_fmask = ~(512 - 1);
695 uspi->s_fshift = 9;
696 uspi->s_sbsize = super_block_size = 1536;
697 uspi->s_sbbase = 0;
698 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
699 break;
700 case UFS_MOUNT_UFSTYPE_UFS2:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700701 UFSD("ufstype=ufs2\n");
Evgeniy7b4ee732006-01-14 11:42:06 +0300702 super_block_offset=SBLOCK_UFS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 uspi->s_fsize = block_size = 512;
704 uspi->s_fmask = ~(512 - 1);
705 uspi->s_fshift = 9;
706 uspi->s_sbsize = super_block_size = 1536;
707 uspi->s_sbbase = 0;
708 flags |= UFS_TYPE_UFS2 | UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 break;
710
711 case UFS_MOUNT_UFSTYPE_SUN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700712 UFSD("ufstype=sun\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 uspi->s_fsize = block_size = 1024;
714 uspi->s_fmask = ~(1024 - 1);
715 uspi->s_fshift = 10;
716 uspi->s_sbsize = super_block_size = 2048;
717 uspi->s_sbbase = 0;
Mark Fortescue252e2112007-10-16 23:26:31 -0700718 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUN | UFS_CG_SUN;
720 break;
721
Mark Fortescue252e2112007-10-16 23:26:31 -0700722 case UFS_MOUNT_UFSTYPE_SUNOS:
723 UFSD(("ufstype=sunos\n"))
724 uspi->s_fsize = block_size = 1024;
725 uspi->s_fmask = ~(1024 - 1);
726 uspi->s_fshift = 10;
727 uspi->s_sbsize = 2048;
728 super_block_size = 2048;
729 uspi->s_sbbase = 0;
730 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
731 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_SUNOS | UFS_CG_SUN;
732 break;
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 case UFS_MOUNT_UFSTYPE_SUNx86:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700735 UFSD("ufstype=sunx86\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 uspi->s_fsize = block_size = 1024;
737 uspi->s_fmask = ~(1024 - 1);
738 uspi->s_fshift = 10;
739 uspi->s_sbsize = super_block_size = 2048;
740 uspi->s_sbbase = 0;
Mark Fortescue252e2112007-10-16 23:26:31 -0700741 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUNx86 | UFS_CG_SUN;
743 break;
744
745 case UFS_MOUNT_UFSTYPE_OLD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700746 UFSD("ufstype=old\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 uspi->s_fsize = block_size = 1024;
748 uspi->s_fmask = ~(1024 - 1);
749 uspi->s_fshift = 10;
750 uspi->s_sbsize = super_block_size = 2048;
751 uspi->s_sbbase = 0;
752 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
753 if (!(sb->s_flags & MS_RDONLY)) {
754 if (!silent)
755 printk(KERN_INFO "ufstype=old is supported read-only\n");
756 sb->s_flags |= MS_RDONLY;
757 }
758 break;
759
760 case UFS_MOUNT_UFSTYPE_NEXTSTEP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700761 UFSD("ufstype=nextstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 uspi->s_fsize = block_size = 1024;
763 uspi->s_fmask = ~(1024 - 1);
764 uspi->s_fshift = 10;
765 uspi->s_sbsize = super_block_size = 2048;
766 uspi->s_sbbase = 0;
Evgeniy Dushistov0c664f92007-12-04 23:45:06 -0800767 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
769 if (!(sb->s_flags & MS_RDONLY)) {
770 if (!silent)
771 printk(KERN_INFO "ufstype=nextstep is supported read-only\n");
772 sb->s_flags |= MS_RDONLY;
773 }
774 break;
775
776 case UFS_MOUNT_UFSTYPE_NEXTSTEP_CD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700777 UFSD("ufstype=nextstep-cd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 uspi->s_fsize = block_size = 2048;
779 uspi->s_fmask = ~(2048 - 1);
780 uspi->s_fshift = 11;
781 uspi->s_sbsize = super_block_size = 2048;
782 uspi->s_sbbase = 0;
Evgeniy Dushistov0c664f92007-12-04 23:45:06 -0800783 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
785 if (!(sb->s_flags & MS_RDONLY)) {
786 if (!silent)
787 printk(KERN_INFO "ufstype=nextstep-cd is supported read-only\n");
788 sb->s_flags |= MS_RDONLY;
789 }
790 break;
791
792 case UFS_MOUNT_UFSTYPE_OPENSTEP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700793 UFSD("ufstype=openstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 uspi->s_fsize = block_size = 1024;
795 uspi->s_fmask = ~(1024 - 1);
796 uspi->s_fshift = 10;
797 uspi->s_sbsize = super_block_size = 2048;
798 uspi->s_sbbase = 0;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800799 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
801 if (!(sb->s_flags & MS_RDONLY)) {
802 if (!silent)
803 printk(KERN_INFO "ufstype=openstep is supported read-only\n");
804 sb->s_flags |= MS_RDONLY;
805 }
806 break;
807
808 case UFS_MOUNT_UFSTYPE_HP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700809 UFSD("ufstype=hp\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;
815 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
816 if (!(sb->s_flags & MS_RDONLY)) {
817 if (!silent)
818 printk(KERN_INFO "ufstype=hp is supported read-only\n");
819 sb->s_flags |= MS_RDONLY;
820 }
821 break;
822 default:
823 if (!silent)
824 printk("unknown ufstype\n");
825 goto failed;
826 }
827
828again:
829 if (!sb_set_blocksize(sb, block_size)) {
830 printk(KERN_ERR "UFS: failed to set blocksize\n");
831 goto failed;
832 }
833
834 /*
835 * read ufs super block from device
836 */
Evgeniy7b4ee732006-01-14 11:42:06 +0300837
838 ubh = ubh_bread_uspi(uspi, sb, uspi->s_sbbase + super_block_offset/block_size, super_block_size);
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (!ubh)
841 goto failed;
842
Evgeniy7b4ee732006-01-14 11:42:06 +0300843 usb1 = ubh_get_usb_first(uspi);
844 usb2 = ubh_get_usb_second(uspi);
845 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Mark Fortescue252e2112007-10-16 23:26:31 -0700847 /* Sort out mod used on SunOS 4.1.3 for fs_state */
848 uspi->s_postblformat = fs32_to_cpu(sb, usb3->fs_postblformat);
849 if (((flags & UFS_ST_MASK) == UFS_ST_SUNOS) &&
850 (uspi->s_postblformat != UFS_42POSTBLFMT)) {
851 flags &= ~UFS_ST_MASK;
852 flags |= UFS_ST_SUN;
853 }
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /*
856 * Check ufs magic number
857 */
858 sbi->s_bytesex = BYTESEX_LE;
859 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
860 case UFS_MAGIC:
861 case UFS2_MAGIC:
862 case UFS_MAGIC_LFN:
863 case UFS_MAGIC_FEA:
864 case UFS_MAGIC_4GB:
865 goto magic_found;
866 }
867 sbi->s_bytesex = BYTESEX_BE;
868 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
869 case UFS_MAGIC:
870 case UFS2_MAGIC:
871 case UFS_MAGIC_LFN:
872 case UFS_MAGIC_FEA:
873 case UFS_MAGIC_4GB:
874 goto magic_found;
875 }
876
877 if ((((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP)
878 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP_CD)
879 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_OPENSTEP))
880 && uspi->s_sbbase < 256) {
881 ubh_brelse_uspi(uspi);
882 ubh = NULL;
883 uspi->s_sbbase += 8;
884 goto again;
885 }
886 if (!silent)
887 printk("ufs_read_super: bad magic number\n");
888 goto failed;
889
890magic_found:
891 /*
892 * Check block and fragment sizes
893 */
894 uspi->s_bsize = fs32_to_cpu(sb, usb1->fs_bsize);
895 uspi->s_fsize = fs32_to_cpu(sb, usb1->fs_fsize);
896 uspi->s_sbsize = fs32_to_cpu(sb, usb1->fs_sbsize);
897 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
898 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
899
vignesh babu9e8c4272007-07-15 23:41:36 -0700900 if (!is_power_of_2(uspi->s_fsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 printk(KERN_ERR "ufs_read_super: fragment size %u is not a power of 2\n",
902 uspi->s_fsize);
903 goto failed;
904 }
905 if (uspi->s_fsize < 512) {
906 printk(KERN_ERR "ufs_read_super: fragment size %u is too small\n",
907 uspi->s_fsize);
908 goto failed;
909 }
910 if (uspi->s_fsize > 4096) {
911 printk(KERN_ERR "ufs_read_super: fragment size %u is too large\n",
912 uspi->s_fsize);
913 goto failed;
914 }
vignesh babu9e8c4272007-07-15 23:41:36 -0700915 if (!is_power_of_2(uspi->s_bsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 printk(KERN_ERR "ufs_read_super: block size %u is not a power of 2\n",
917 uspi->s_bsize);
918 goto failed;
919 }
920 if (uspi->s_bsize < 4096) {
921 printk(KERN_ERR "ufs_read_super: block size %u is too small\n",
922 uspi->s_bsize);
923 goto failed;
924 }
925 if (uspi->s_bsize / uspi->s_fsize > 8) {
926 printk(KERN_ERR "ufs_read_super: too many fragments per block (%u)\n",
927 uspi->s_bsize / uspi->s_fsize);
928 goto failed;
929 }
930 if (uspi->s_fsize != block_size || uspi->s_sbsize != super_block_size) {
931 ubh_brelse_uspi(uspi);
932 ubh = NULL;
933 block_size = uspi->s_fsize;
934 super_block_size = uspi->s_sbsize;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700935 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 -0700936 goto again;
937 }
938
Andrew Mortonf664f1f2007-10-29 14:37:21 -0700939 sbi->s_flags = flags;/*after that line some functions use s_flags*/
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800940 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 /*
943 * Check, if file system was correctly unmounted.
944 * If not, make it read only.
945 */
Andrew Mortonf664f1f2007-10-29 14:37:21 -0700946 if (((flags & UFS_ST_MASK) == UFS_ST_44BSD) ||
947 ((flags & UFS_ST_MASK) == UFS_ST_OLD) ||
948 (((flags & UFS_ST_MASK) == UFS_ST_SUN ||
949 (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
950 (flags & UFS_ST_MASK) == UFS_ST_SUNx86) &&
951 (ufs_get_fs_state(sb, usb1, usb3) == (UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 switch(usb1->fs_clean) {
953 case UFS_FSCLEAN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700954 UFSD("fs is clean\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 break;
956 case UFS_FSSTABLE:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700957 UFSD("fs is stable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 break;
959 case UFS_FSOSF1:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700960 UFSD("fs is DEC OSF/1\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 break;
962 case UFS_FSACTIVE:
963 printk("ufs_read_super: fs is active\n");
964 sb->s_flags |= MS_RDONLY;
965 break;
966 case UFS_FSBAD:
967 printk("ufs_read_super: fs is bad\n");
968 sb->s_flags |= MS_RDONLY;
969 break;
970 default:
971 printk("ufs_read_super: can't grok fs_clean 0x%x\n", usb1->fs_clean);
972 sb->s_flags |= MS_RDONLY;
973 break;
974 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700975 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 printk("ufs_read_super: fs needs fsck\n");
977 sb->s_flags |= MS_RDONLY;
978 }
979
980 /*
981 * Read ufs_super_block into internal data structures
982 */
983 sb->s_op = &ufs_super_ops;
984 sb->dq_op = NULL; /***/
985 sb->s_magic = fs32_to_cpu(sb, usb3->fs_magic);
986
987 uspi->s_sblkno = fs32_to_cpu(sb, usb1->fs_sblkno);
988 uspi->s_cblkno = fs32_to_cpu(sb, usb1->fs_cblkno);
989 uspi->s_iblkno = fs32_to_cpu(sb, usb1->fs_iblkno);
990 uspi->s_dblkno = fs32_to_cpu(sb, usb1->fs_dblkno);
991 uspi->s_cgoffset = fs32_to_cpu(sb, usb1->fs_cgoffset);
992 uspi->s_cgmask = fs32_to_cpu(sb, usb1->fs_cgmask);
993
994 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700995 uspi->s_u2_size = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size);
996 uspi->s_u2_dsize = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
997 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 uspi->s_size = fs32_to_cpu(sb, usb1->fs_size);
999 uspi->s_dsize = fs32_to_cpu(sb, usb1->fs_dsize);
1000 }
1001
1002 uspi->s_ncg = fs32_to_cpu(sb, usb1->fs_ncg);
1003 /* s_bsize already set */
1004 /* s_fsize already set */
1005 uspi->s_fpb = fs32_to_cpu(sb, usb1->fs_frag);
1006 uspi->s_minfree = fs32_to_cpu(sb, usb1->fs_minfree);
1007 uspi->s_bmask = fs32_to_cpu(sb, usb1->fs_bmask);
1008 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
1009 uspi->s_bshift = fs32_to_cpu(sb, usb1->fs_bshift);
1010 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001011 UFSD("uspi->s_bshift = %d,uspi->s_fshift = %d", uspi->s_bshift,
1012 uspi->s_fshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 uspi->s_fpbshift = fs32_to_cpu(sb, usb1->fs_fragshift);
1014 uspi->s_fsbtodb = fs32_to_cpu(sb, usb1->fs_fsbtodb);
1015 /* s_sbsize already set */
1016 uspi->s_csmask = fs32_to_cpu(sb, usb1->fs_csmask);
1017 uspi->s_csshift = fs32_to_cpu(sb, usb1->fs_csshift);
1018 uspi->s_nindir = fs32_to_cpu(sb, usb1->fs_nindir);
1019 uspi->s_inopb = fs32_to_cpu(sb, usb1->fs_inopb);
1020 uspi->s_nspf = fs32_to_cpu(sb, usb1->fs_nspf);
1021 uspi->s_npsect = ufs_get_fs_npsect(sb, usb1, usb3);
1022 uspi->s_interleave = fs32_to_cpu(sb, usb1->fs_interleave);
1023 uspi->s_trackskew = fs32_to_cpu(sb, usb1->fs_trackskew);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -08001024
1025 if (uspi->fs_magic == UFS2_MAGIC)
1026 uspi->s_csaddr = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_csaddr);
1027 else
1028 uspi->s_csaddr = fs32_to_cpu(sb, usb1->fs_csaddr);
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 uspi->s_cssize = fs32_to_cpu(sb, usb1->fs_cssize);
1031 uspi->s_cgsize = fs32_to_cpu(sb, usb1->fs_cgsize);
1032 uspi->s_ntrak = fs32_to_cpu(sb, usb1->fs_ntrak);
1033 uspi->s_nsect = fs32_to_cpu(sb, usb1->fs_nsect);
1034 uspi->s_spc = fs32_to_cpu(sb, usb1->fs_spc);
1035 uspi->s_ipg = fs32_to_cpu(sb, usb1->fs_ipg);
1036 uspi->s_fpg = fs32_to_cpu(sb, usb1->fs_fpg);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001037 uspi->s_cpc = fs32_to_cpu(sb, usb2->fs_un.fs_u1.fs_cpc);
1038 uspi->s_contigsumsize = fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_contigsumsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 uspi->s_qbmask = ufs_get_fs_qbmask(sb, usb3);
1040 uspi->s_qfmask = ufs_get_fs_qfmask(sb, usb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 uspi->s_nrpos = fs32_to_cpu(sb, usb3->fs_nrpos);
1042 uspi->s_postbloff = fs32_to_cpu(sb, usb3->fs_postbloff);
1043 uspi->s_rotbloff = fs32_to_cpu(sb, usb3->fs_rotbloff);
1044
1045 /*
1046 * Compute another frequently used values
1047 */
1048 uspi->s_fpbmask = uspi->s_fpb - 1;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001049 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 uspi->s_apbshift = uspi->s_bshift - 3;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001051 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 uspi->s_apbshift = uspi->s_bshift - 2;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 uspi->s_2apbshift = uspi->s_apbshift * 2;
1055 uspi->s_3apbshift = uspi->s_apbshift * 3;
1056 uspi->s_apb = 1 << uspi->s_apbshift;
1057 uspi->s_2apb = 1 << uspi->s_2apbshift;
1058 uspi->s_3apb = 1 << uspi->s_3apbshift;
1059 uspi->s_apbmask = uspi->s_apb - 1;
1060 uspi->s_nspfshift = uspi->s_fshift - UFS_SECTOR_BITS;
1061 uspi->s_nspb = uspi->s_nspf << uspi->s_fpbshift;
1062 uspi->s_inopf = uspi->s_inopb >> uspi->s_fpbshift;
1063 uspi->s_bpf = uspi->s_fsize << 3;
1064 uspi->s_bpfshift = uspi->s_fshift + 3;
1065 uspi->s_bpfmask = uspi->s_bpf - 1;
Evgeniy Dushistov90b315a2008-02-08 04:20:16 -08001066 if ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_44BSD ||
1067 (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 uspi->s_maxsymlinklen =
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001069 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
David Howellsb55c4602008-02-07 00:15:48 -08001071 inode = ufs_iget(sb, UFS_ROOTINO);
1072 if (IS_ERR(inode)) {
1073 ret = PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 goto failed;
David Howellsb55c4602008-02-07 00:15:48 -08001075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 sb->s_root = d_alloc_root(inode);
David Howellsb55c4602008-02-07 00:15:48 -08001077 if (!sb->s_root) {
1078 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 goto dalloc_failed;
David Howellsb55c4602008-02-07 00:15:48 -08001080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001082 ufs_setup_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /*
1084 * Read cylinder group structures
1085 */
1086 if (!(sb->s_flags & MS_RDONLY))
1087 if (!ufs_read_cylinder_structures(sb))
1088 goto failed;
1089
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001090 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return 0;
1092
1093dalloc_failed:
1094 iput(inode);
1095failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -08001096 if (ubh)
1097 ubh_brelse_uspi (uspi);
1098 kfree (uspi);
1099 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 sb->s_fs_info = NULL;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001101 UFSD("EXIT (FAILED)\n");
David Howellsb55c4602008-02-07 00:15:48 -08001102 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104failed_nomem:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001105 UFSD("EXIT (NOMEM)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return -ENOMEM;
1107}
1108
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001109static void ufs_write_super(struct super_block *sb)
1110{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct ufs_sb_private_info * uspi;
1112 struct ufs_super_block_first * usb1;
1113 struct ufs_super_block_third * usb3;
1114 unsigned flags;
1115
1116 lock_kernel();
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001117 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 flags = UFS_SB(sb)->s_flags;
1119 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +03001120 usb1 = ubh_get_usb_first(uspi);
1121 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 if (!(sb->s_flags & MS_RDONLY)) {
1124 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1125 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
Mark Fortescue252e2112007-10-16 23:26:31 -07001126 || (flags & UFS_ST_MASK) == UFS_ST_SUNOS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1128 ufs_set_fs_state(sb, usb1, usb3,
1129 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001130 ufs_put_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132 sb->s_dirt = 0;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001133 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 unlock_kernel();
1135}
1136
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001137static void ufs_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
1139 struct ufs_sb_info * sbi = UFS_SB(sb);
1140
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001141 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 if (!(sb->s_flags & MS_RDONLY))
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001144 ufs_put_super_internal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 ubh_brelse_uspi (sbi->s_uspi);
1147 kfree (sbi->s_uspi);
1148 kfree (sbi);
1149 sb->s_fs_info = NULL;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001150 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return;
1152}
1153
1154
1155static int ufs_remount (struct super_block *sb, int *mount_flags, char *data)
1156{
1157 struct ufs_sb_private_info * uspi;
1158 struct ufs_super_block_first * usb1;
1159 struct ufs_super_block_third * usb3;
1160 unsigned new_mount_opt, ufstype;
1161 unsigned flags;
1162
1163 uspi = UFS_SB(sb)->s_uspi;
1164 flags = UFS_SB(sb)->s_flags;
Evgeniy7b4ee732006-01-14 11:42:06 +03001165 usb1 = ubh_get_usb_first(uspi);
1166 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 /*
1169 * Allow the "check" option to be passed as a remount option.
1170 * It is not possible to change ufstype option during remount
1171 */
1172 ufstype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
1173 new_mount_opt = 0;
1174 ufs_set_opt (new_mount_opt, ONERROR_LOCK);
1175 if (!ufs_parse_options (data, &new_mount_opt))
1176 return -EINVAL;
1177 if (!(new_mount_opt & UFS_MOUNT_UFSTYPE)) {
1178 new_mount_opt |= ufstype;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001179 } else if ((new_mount_opt & UFS_MOUNT_UFSTYPE) != ufstype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 printk("ufstype can't be changed during remount\n");
1181 return -EINVAL;
1182 }
1183
1184 if ((*mount_flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
1185 UFS_SB(sb)->s_mount_opt = new_mount_opt;
1186 return 0;
1187 }
1188
1189 /*
1190 * fs was mouted as rw, remounting ro
1191 */
1192 if (*mount_flags & MS_RDONLY) {
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001193 ufs_put_super_internal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1195 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
Mark Fortescue252e2112007-10-16 23:26:31 -07001196 || (flags & UFS_ST_MASK) == UFS_ST_SUNOS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1198 ufs_set_fs_state(sb, usb1, usb3,
1199 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -07001200 ubh_mark_buffer_dirty (USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 sb->s_dirt = 0;
1202 sb->s_flags |= MS_RDONLY;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001203 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /*
1205 * fs was mounted as ro, remounting rw
1206 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207#ifndef CONFIG_UFS_FS_WRITE
1208 printk("ufs was compiled with read-only support, "
1209 "can't be mounted as read-write\n");
1210 return -EINVAL;
1211#else
1212 if (ufstype != UFS_MOUNT_UFSTYPE_SUN &&
Mark Fortescue252e2112007-10-16 23:26:31 -07001213 ufstype != UFS_MOUNT_UFSTYPE_SUNOS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 ufstype != UFS_MOUNT_UFSTYPE_44BSD &&
Evgeniy Dushistovcbcae392007-02-12 00:54:30 -08001215 ufstype != UFS_MOUNT_UFSTYPE_SUNx86 &&
1216 ufstype != UFS_MOUNT_UFSTYPE_UFS2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 printk("this ufstype is read-only supported\n");
1218 return -EINVAL;
1219 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001220 if (!ufs_read_cylinder_structures(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 printk("failed during remounting\n");
1222 return -EPERM;
1223 }
1224 sb->s_flags &= ~MS_RDONLY;
1225#endif
1226 }
1227 UFS_SB(sb)->s_mount_opt = new_mount_opt;
1228 return 0;
1229}
1230
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001231static int ufs_show_options(struct seq_file *seq, struct vfsmount *vfs)
1232{
1233 struct ufs_sb_info *sbi = UFS_SB(vfs->mnt_sb);
1234 unsigned mval = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
1235 struct match_token *tp = tokens;
1236
1237 while (tp->token != Opt_onerror_panic && tp->token != mval)
1238 ++tp;
1239 BUG_ON(tp->token == Opt_onerror_panic);
1240 seq_printf(seq, ",%s", tp->pattern);
1241
1242 mval = sbi->s_mount_opt & UFS_MOUNT_ONERROR;
1243 while (tp->token != Opt_err && tp->token != mval)
1244 ++tp;
1245 BUG_ON(tp->token == Opt_err);
1246 seq_printf(seq, ",%s", tp->pattern);
1247
1248 return 0;
1249}
1250
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001251static int ufs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
David Howells726c3342006-06-23 02:02:58 -07001253 struct super_block *sb = dentry->d_sb;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001254 struct ufs_sb_private_info *uspi= UFS_SB(sb)->s_uspi;
1255 unsigned flags = UFS_SB(sb)->s_flags;
1256 struct ufs_super_block_first *usb1;
1257 struct ufs_super_block_second *usb2;
1258 struct ufs_super_block_third *usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 lock_kernel();
1261
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001262 usb1 = ubh_get_usb_first(uspi);
1263 usb2 = ubh_get_usb_second(uspi);
1264 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
1267 buf->f_type = UFS2_MAGIC;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001268 buf->f_blocks = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001269 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 buf->f_type = UFS_MAGIC;
1271 buf->f_blocks = uspi->s_dsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001273 buf->f_bfree = ufs_blkstofrags(uspi->cs_total.cs_nbfree) +
1274 uspi->cs_total.cs_nffree;
1275 buf->f_ffree = uspi->cs_total.cs_nifree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 buf->f_bsize = sb->s_blocksize;
1277 buf->f_bavail = (buf->f_bfree > (((long)buf->f_blocks / 100) * uspi->s_minfree))
1278 ? (buf->f_bfree - (((long)buf->f_blocks / 100) * uspi->s_minfree)) : 0;
1279 buf->f_files = uspi->s_ncg * uspi->s_ipg;
1280 buf->f_namelen = UFS_MAXNAMLEN;
1281
1282 unlock_kernel();
1283
1284 return 0;
1285}
1286
Christoph Lametere18b8902006-12-06 20:33:20 -08001287static struct kmem_cache * ufs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289static struct inode *ufs_alloc_inode(struct super_block *sb)
1290{
1291 struct ufs_inode_info *ei;
Christoph Lametere94b1762006-12-06 20:33:17 -08001292 ei = (struct ufs_inode_info *)kmem_cache_alloc(ufs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (!ei)
1294 return NULL;
1295 ei->vfs_inode.i_version = 1;
1296 return &ei->vfs_inode;
1297}
1298
1299static void ufs_destroy_inode(struct inode *inode)
1300{
1301 kmem_cache_free(ufs_inode_cachep, UFS_I(inode));
1302}
1303
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07001304static void init_once(struct kmem_cache * cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 struct ufs_inode_info *ei = (struct ufs_inode_info *) foo;
1307
Christoph Lametera35afb82007-05-16 22:10:57 -07001308 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
Paul Mundt20c2df82007-07-20 10:11:58 +09001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311static int init_inodecache(void)
1312{
1313 ufs_inode_cachep = kmem_cache_create("ufs_inode_cache",
1314 sizeof(struct ufs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001315 0, (SLAB_RECLAIM_ACCOUNT|
1316 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001317 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (ufs_inode_cachep == NULL)
1319 return -ENOMEM;
1320 return 0;
1321}
1322
1323static void destroy_inodecache(void)
1324{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001325 kmem_cache_destroy(ufs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
1328#ifdef CONFIG_QUOTA
1329static ssize_t ufs_quota_read(struct super_block *, int, char *,size_t, loff_t);
1330static ssize_t ufs_quota_write(struct super_block *, int, const char *, size_t, loff_t);
1331#endif
1332
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001333static const struct super_operations ufs_super_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 .alloc_inode = ufs_alloc_inode,
1335 .destroy_inode = ufs_destroy_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 .write_inode = ufs_write_inode,
1337 .delete_inode = ufs_delete_inode,
1338 .put_super = ufs_put_super,
1339 .write_super = ufs_write_super,
1340 .statfs = ufs_statfs,
1341 .remount_fs = ufs_remount,
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001342 .show_options = ufs_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343#ifdef CONFIG_QUOTA
1344 .quota_read = ufs_quota_read,
1345 .quota_write = ufs_quota_write,
1346#endif
1347};
1348
1349#ifdef CONFIG_QUOTA
1350
1351/* Read data from quotafile - avoid pagecache and such because we cannot afford
1352 * acquiring the locks... As quota files are never truncated and quota code
1353 * itself serializes the operations (and noone else should touch the files)
1354 * we don't have to be afraid of races */
1355static ssize_t ufs_quota_read(struct super_block *sb, int type, char *data,
1356 size_t len, loff_t off)
1357{
1358 struct inode *inode = sb_dqopt(sb)->files[type];
1359 sector_t blk = off >> sb->s_blocksize_bits;
1360 int err = 0;
1361 int offset = off & (sb->s_blocksize - 1);
1362 int tocopy;
1363 size_t toread;
1364 struct buffer_head *bh;
1365 loff_t i_size = i_size_read(inode);
1366
1367 if (off > i_size)
1368 return 0;
1369 if (off+len > i_size)
1370 len = i_size-off;
1371 toread = len;
1372 while (toread > 0) {
1373 tocopy = sb->s_blocksize - offset < toread ?
1374 sb->s_blocksize - offset : toread;
1375
1376 bh = ufs_bread(inode, blk, 0, &err);
1377 if (err)
1378 return err;
1379 if (!bh) /* A hole? */
1380 memset(data, 0, tocopy);
1381 else {
1382 memcpy(data, bh->b_data+offset, tocopy);
1383 brelse(bh);
1384 }
1385 offset = 0;
1386 toread -= tocopy;
1387 data += tocopy;
1388 blk++;
1389 }
1390 return len;
1391}
1392
1393/* Write to quotafile */
1394static ssize_t ufs_quota_write(struct super_block *sb, int type,
1395 const char *data, size_t len, loff_t off)
1396{
1397 struct inode *inode = sb_dqopt(sb)->files[type];
1398 sector_t blk = off >> sb->s_blocksize_bits;
1399 int err = 0;
1400 int offset = off & (sb->s_blocksize - 1);
1401 int tocopy;
1402 size_t towrite = len;
1403 struct buffer_head *bh;
1404
Arjan van de Ven5c81a412006-07-03 00:25:20 -07001405 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 while (towrite > 0) {
1407 tocopy = sb->s_blocksize - offset < towrite ?
1408 sb->s_blocksize - offset : towrite;
1409
1410 bh = ufs_bread(inode, blk, 1, &err);
1411 if (!bh)
1412 goto out;
1413 lock_buffer(bh);
1414 memcpy(bh->b_data+offset, data, tocopy);
1415 flush_dcache_page(bh->b_page);
1416 set_buffer_uptodate(bh);
1417 mark_buffer_dirty(bh);
1418 unlock_buffer(bh);
1419 brelse(bh);
1420 offset = 0;
1421 towrite -= tocopy;
1422 data += tocopy;
1423 blk++;
1424 }
1425out:
Evgeniy Polyakov15b2fe32006-01-08 01:04:51 -08001426 if (len == towrite) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001427 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return err;
Evgeniy Polyakov15b2fe32006-01-08 01:04:51 -08001429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (inode->i_size < off+len-towrite)
1431 i_size_write(inode, off+len-towrite);
1432 inode->i_version++;
1433 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
1434 mark_inode_dirty(inode);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001435 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return len - towrite;
1437}
1438
1439#endif
1440
David Howells454e2392006-06-23 02:02:57 -07001441static int ufs_get_sb(struct file_system_type *fs_type,
1442 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
David Howells454e2392006-06-23 02:02:57 -07001444 return get_sb_bdev(fs_type, flags, dev_name, data, ufs_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
1447static struct file_system_type ufs_fs_type = {
1448 .owner = THIS_MODULE,
1449 .name = "ufs",
1450 .get_sb = ufs_get_sb,
1451 .kill_sb = kill_block_super,
1452 .fs_flags = FS_REQUIRES_DEV,
1453};
1454
1455static int __init init_ufs_fs(void)
1456{
1457 int err = init_inodecache();
1458 if (err)
1459 goto out1;
1460 err = register_filesystem(&ufs_fs_type);
1461 if (err)
1462 goto out;
1463 return 0;
1464out:
1465 destroy_inodecache();
1466out1:
1467 return err;
1468}
1469
1470static void __exit exit_ufs_fs(void)
1471{
1472 unregister_filesystem(&ufs_fs_type);
1473 destroy_inodecache();
1474}
1475
1476module_init(init_ufs_fs)
1477module_exit(exit_ufs_fs)
1478MODULE_LICENSE("GPL");