blob: 73deff475e6362326ab6e6785f89a93ad5b3f158 [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>
79#include <linux/ufs_fs.h>
80#include <linux/slab.h>
81#include <linux/time.h>
82#include <linux/stat.h>
83#include <linux/string.h>
84#include <linux/blkdev.h>
85#include <linux/init.h>
86#include <linux/parser.h>
87#include <linux/smp_lock.h>
88#include <linux/buffer_head.h>
89#include <linux/vfs.h>
vignesh babu9e8c4272007-07-15 23:41:36 -070090#include <linux/log2.h>
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -070091#include <linux/mount.h>
92#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
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 Dushistov647b7e82006-06-25 05:47:29 -0700134 } else {
135 printk(" sblkno: %u\n", fs32_to_cpu(sb, usb1->fs_sblkno));
136 printk(" cblkno: %u\n", fs32_to_cpu(sb, usb1->fs_cblkno));
137 printk(" iblkno: %u\n", fs32_to_cpu(sb, usb1->fs_iblkno));
138 printk(" dblkno: %u\n", fs32_to_cpu(sb, usb1->fs_dblkno));
139 printk(" cgoffset: %u\n",
140 fs32_to_cpu(sb, usb1->fs_cgoffset));
141 printk(" ~cgmask: 0x%x\n",
142 ~fs32_to_cpu(sb, usb1->fs_cgmask));
143 printk(" size: %u\n", fs32_to_cpu(sb, usb1->fs_size));
144 printk(" dsize: %u\n", fs32_to_cpu(sb, usb1->fs_dsize));
145 printk(" ncg: %u\n", fs32_to_cpu(sb, usb1->fs_ncg));
146 printk(" bsize: %u\n", fs32_to_cpu(sb, usb1->fs_bsize));
147 printk(" fsize: %u\n", fs32_to_cpu(sb, usb1->fs_fsize));
148 printk(" frag: %u\n", fs32_to_cpu(sb, usb1->fs_frag));
149 printk(" fragshift: %u\n",
150 fs32_to_cpu(sb, usb1->fs_fragshift));
151 printk(" ~fmask: %u\n", ~fs32_to_cpu(sb, usb1->fs_fmask));
152 printk(" fshift: %u\n", fs32_to_cpu(sb, usb1->fs_fshift));
153 printk(" sbsize: %u\n", fs32_to_cpu(sb, usb1->fs_sbsize));
154 printk(" spc: %u\n", fs32_to_cpu(sb, usb1->fs_spc));
155 printk(" cpg: %u\n", fs32_to_cpu(sb, usb1->fs_cpg));
156 printk(" ipg: %u\n", fs32_to_cpu(sb, usb1->fs_ipg));
157 printk(" fpg: %u\n", fs32_to_cpu(sb, usb1->fs_fpg));
158 printk(" csaddr: %u\n", fs32_to_cpu(sb, usb1->fs_csaddr));
159 printk(" cssize: %u\n", fs32_to_cpu(sb, usb1->fs_cssize));
160 printk(" cgsize: %u\n", fs32_to_cpu(sb, usb1->fs_cgsize));
161 printk(" fstodb: %u\n",
162 fs32_to_cpu(sb, usb1->fs_fsbtodb));
163 printk(" nrpos: %u\n", fs32_to_cpu(sb, usb3->fs_nrpos));
164 printk(" ndir %u\n",
165 fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir));
166 printk(" nifree %u\n",
167 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree));
168 printk(" nbfree %u\n",
169 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree));
170 printk(" nffree %u\n",
171 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree));
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 printk("\n");
174}
175
176/*
177 * Print contents of ufs_cylinder_group, useful for debugging
178 */
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700179static void ufs_print_cylinder_stuff(struct super_block *sb,
180 struct ufs_cylinder_group *cg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 printk("\nufs_print_cylinder_stuff\n");
Andrew Morton96710b22006-06-25 05:47:28 -0700183 printk("size of ucg: %zu\n", sizeof(struct ufs_cylinder_group));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 printk(" magic: %x\n", fs32_to_cpu(sb, cg->cg_magic));
185 printk(" time: %u\n", fs32_to_cpu(sb, cg->cg_time));
186 printk(" cgx: %u\n", fs32_to_cpu(sb, cg->cg_cgx));
187 printk(" ncyl: %u\n", fs16_to_cpu(sb, cg->cg_ncyl));
188 printk(" niblk: %u\n", fs16_to_cpu(sb, cg->cg_niblk));
189 printk(" ndblk: %u\n", fs32_to_cpu(sb, cg->cg_ndblk));
190 printk(" cs_ndir: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_ndir));
191 printk(" cs_nbfree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nbfree));
192 printk(" cs_nifree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nifree));
193 printk(" cs_nffree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nffree));
194 printk(" rotor: %u\n", fs32_to_cpu(sb, cg->cg_rotor));
195 printk(" frotor: %u\n", fs32_to_cpu(sb, cg->cg_frotor));
196 printk(" irotor: %u\n", fs32_to_cpu(sb, cg->cg_irotor));
197 printk(" frsum: %u, %u, %u, %u, %u, %u, %u, %u\n",
198 fs32_to_cpu(sb, cg->cg_frsum[0]), fs32_to_cpu(sb, cg->cg_frsum[1]),
199 fs32_to_cpu(sb, cg->cg_frsum[2]), fs32_to_cpu(sb, cg->cg_frsum[3]),
200 fs32_to_cpu(sb, cg->cg_frsum[4]), fs32_to_cpu(sb, cg->cg_frsum[5]),
201 fs32_to_cpu(sb, cg->cg_frsum[6]), fs32_to_cpu(sb, cg->cg_frsum[7]));
202 printk(" btotoff: %u\n", fs32_to_cpu(sb, cg->cg_btotoff));
203 printk(" boff: %u\n", fs32_to_cpu(sb, cg->cg_boff));
204 printk(" iuseoff: %u\n", fs32_to_cpu(sb, cg->cg_iusedoff));
205 printk(" freeoff: %u\n", fs32_to_cpu(sb, cg->cg_freeoff));
206 printk(" nextfreeoff: %u\n", fs32_to_cpu(sb, cg->cg_nextfreeoff));
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700207 printk(" clustersumoff %u\n",
208 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clustersumoff));
209 printk(" clusteroff %u\n",
210 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clusteroff));
211 printk(" nclusterblks %u\n",
212 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_nclusterblks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 printk("\n");
214}
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700215#else
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800216# define ufs_print_super_stuff(sb, usb1, usb2, usb3) /**/
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700217# define ufs_print_cylinder_stuff(sb, cg) /**/
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700218#endif /* CONFIG_UFS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800220static const struct super_operations ufs_super_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222static char error_buf[1024];
223
224void ufs_error (struct super_block * sb, const char * function,
225 const char * fmt, ...)
226{
227 struct ufs_sb_private_info * uspi;
228 struct ufs_super_block_first * usb1;
229 va_list args;
230
231 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300232 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (!(sb->s_flags & MS_RDONLY)) {
235 usb1->fs_clean = UFS_FSBAD;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700236 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 sb->s_dirt = 1;
238 sb->s_flags |= MS_RDONLY;
239 }
240 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800241 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 va_end (args);
243 switch (UFS_SB(sb)->s_mount_opt & UFS_MOUNT_ONERROR) {
244 case UFS_MOUNT_ONERROR_PANIC:
245 panic ("UFS-fs panic (device %s): %s: %s\n",
246 sb->s_id, function, error_buf);
247
248 case UFS_MOUNT_ONERROR_LOCK:
249 case UFS_MOUNT_ONERROR_UMOUNT:
250 case UFS_MOUNT_ONERROR_REPAIR:
251 printk (KERN_CRIT "UFS-fs error (device %s): %s: %s\n",
252 sb->s_id, function, error_buf);
253 }
254}
255
256void ufs_panic (struct super_block * sb, const char * function,
257 const char * fmt, ...)
258{
259 struct ufs_sb_private_info * uspi;
260 struct ufs_super_block_first * usb1;
261 va_list args;
262
263 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300264 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 if (!(sb->s_flags & MS_RDONLY)) {
267 usb1->fs_clean = UFS_FSBAD;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700268 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 sb->s_dirt = 1;
270 }
271 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800272 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 va_end (args);
274 sb->s_flags |= MS_RDONLY;
275 printk (KERN_CRIT "UFS-fs panic (device %s): %s: %s\n",
276 sb->s_id, function, error_buf);
277}
278
279void ufs_warning (struct super_block * sb, const char * function,
280 const char * fmt, ...)
281{
282 va_list args;
283
284 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800285 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 va_end (args);
287 printk (KERN_WARNING "UFS-fs warning (device %s): %s: %s\n",
288 sb->s_id, function, error_buf);
289}
290
291enum {
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -0700292 Opt_type_old = UFS_MOUNT_UFSTYPE_OLD,
293 Opt_type_sunx86 = UFS_MOUNT_UFSTYPE_SUNx86,
294 Opt_type_sun = UFS_MOUNT_UFSTYPE_SUN,
295 Opt_type_sunos = UFS_MOUNT_UFSTYPE_SUNOS,
296 Opt_type_44bsd = UFS_MOUNT_UFSTYPE_44BSD,
297 Opt_type_ufs2 = UFS_MOUNT_UFSTYPE_UFS2,
298 Opt_type_hp = UFS_MOUNT_UFSTYPE_HP,
299 Opt_type_nextstepcd = UFS_MOUNT_UFSTYPE_NEXTSTEP_CD,
300 Opt_type_nextstep = UFS_MOUNT_UFSTYPE_NEXTSTEP,
301 Opt_type_openstep = UFS_MOUNT_UFSTYPE_OPENSTEP,
302 Opt_onerror_panic = UFS_MOUNT_ONERROR_PANIC,
303 Opt_onerror_lock = UFS_MOUNT_ONERROR_LOCK,
304 Opt_onerror_umount = UFS_MOUNT_ONERROR_UMOUNT,
305 Opt_onerror_repair = UFS_MOUNT_ONERROR_REPAIR,
306 Opt_err
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307};
308
309static match_table_t tokens = {
310 {Opt_type_old, "ufstype=old"},
311 {Opt_type_sunx86, "ufstype=sunx86"},
312 {Opt_type_sun, "ufstype=sun"},
Mark Fortescue252e2112007-10-16 23:26:31 -0700313 {Opt_type_sunos, "ufstype=sunos"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 {Opt_type_44bsd, "ufstype=44bsd"},
315 {Opt_type_ufs2, "ufstype=ufs2"},
316 {Opt_type_ufs2, "ufstype=5xbsd"},
317 {Opt_type_hp, "ufstype=hp"},
318 {Opt_type_nextstepcd, "ufstype=nextstep-cd"},
319 {Opt_type_nextstep, "ufstype=nextstep"},
320 {Opt_type_openstep, "ufstype=openstep"},
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -0700321/*end of possible ufs types */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 {Opt_onerror_panic, "onerror=panic"},
323 {Opt_onerror_lock, "onerror=lock"},
324 {Opt_onerror_umount, "onerror=umount"},
325 {Opt_onerror_repair, "onerror=repair"},
326 {Opt_err, NULL}
327};
328
329static int ufs_parse_options (char * options, unsigned * mount_options)
330{
331 char * p;
332
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700333 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 if (!options)
336 return 1;
337
338 while ((p = strsep(&options, ",")) != NULL) {
339 substring_t args[MAX_OPT_ARGS];
340 int token;
341 if (!*p)
342 continue;
343
344 token = match_token(p, tokens, args);
345 switch (token) {
346 case Opt_type_old:
347 ufs_clear_opt (*mount_options, UFSTYPE);
348 ufs_set_opt (*mount_options, UFSTYPE_OLD);
349 break;
350 case Opt_type_sunx86:
351 ufs_clear_opt (*mount_options, UFSTYPE);
352 ufs_set_opt (*mount_options, UFSTYPE_SUNx86);
353 break;
354 case Opt_type_sun:
355 ufs_clear_opt (*mount_options, UFSTYPE);
356 ufs_set_opt (*mount_options, UFSTYPE_SUN);
357 break;
Mark Fortescue252e2112007-10-16 23:26:31 -0700358 case Opt_type_sunos:
359 ufs_clear_opt(*mount_options, UFSTYPE);
360 ufs_set_opt(*mount_options, UFSTYPE_SUNOS);
361 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 case Opt_type_44bsd:
363 ufs_clear_opt (*mount_options, UFSTYPE);
364 ufs_set_opt (*mount_options, UFSTYPE_44BSD);
365 break;
366 case Opt_type_ufs2:
367 ufs_clear_opt(*mount_options, UFSTYPE);
368 ufs_set_opt(*mount_options, UFSTYPE_UFS2);
369 break;
370 case Opt_type_hp:
371 ufs_clear_opt (*mount_options, UFSTYPE);
372 ufs_set_opt (*mount_options, UFSTYPE_HP);
373 break;
374 case Opt_type_nextstepcd:
375 ufs_clear_opt (*mount_options, UFSTYPE);
376 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP_CD);
377 break;
378 case Opt_type_nextstep:
379 ufs_clear_opt (*mount_options, UFSTYPE);
380 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP);
381 break;
382 case Opt_type_openstep:
383 ufs_clear_opt (*mount_options, UFSTYPE);
384 ufs_set_opt (*mount_options, UFSTYPE_OPENSTEP);
385 break;
386 case Opt_onerror_panic:
387 ufs_clear_opt (*mount_options, ONERROR);
388 ufs_set_opt (*mount_options, ONERROR_PANIC);
389 break;
390 case Opt_onerror_lock:
391 ufs_clear_opt (*mount_options, ONERROR);
392 ufs_set_opt (*mount_options, ONERROR_LOCK);
393 break;
394 case Opt_onerror_umount:
395 ufs_clear_opt (*mount_options, ONERROR);
396 ufs_set_opt (*mount_options, ONERROR_UMOUNT);
397 break;
398 case Opt_onerror_repair:
399 printk("UFS-fs: Unable to do repair on error, "
400 "will lock lock instead\n");
401 ufs_clear_opt (*mount_options, ONERROR);
402 ufs_set_opt (*mount_options, ONERROR_REPAIR);
403 break;
404 default:
405 printk("UFS-fs: Invalid option: \"%s\" "
406 "or missing value\n", p);
407 return 0;
408 }
409 }
410 return 1;
411}
412
413/*
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700414 * Diffrent types of UFS hold fs_cstotal in different
415 * places, and use diffrent data structure for it.
416 * To make things simplier we just copy fs_cstotal to ufs_sb_private_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700418static void ufs_setup_cstotal(struct super_block *sb)
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800419{
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700420 struct ufs_sb_info *sbi = UFS_SB(sb);
421 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700422 struct ufs_super_block_first *usb1;
423 struct ufs_super_block_second *usb2;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700424 struct ufs_super_block_third *usb3;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700425 unsigned mtype = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
426
427 UFSD("ENTER, mtype=%u\n", mtype);
428 usb1 = ubh_get_usb_first(uspi);
429 usb2 = ubh_get_usb_second(uspi);
430 usb3 = ubh_get_usb_third(uspi);
431
432 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
433 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
434 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
435 /*we have statistic in different place, then usual*/
436 uspi->cs_total.cs_ndir = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir);
437 uspi->cs_total.cs_nbfree = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree);
438 uspi->cs_total.cs_nifree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree);
439 uspi->cs_total.cs_nffree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree);
440 } else {
441 uspi->cs_total.cs_ndir = fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir);
442 uspi->cs_total.cs_nbfree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree);
443 uspi->cs_total.cs_nifree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree);
444 uspi->cs_total.cs_nffree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree);
445 }
446 UFSD("EXIT\n");
447}
448
449/*
450 * Read on-disk structures associated with cylinder groups
451 */
452static int ufs_read_cylinder_structures(struct super_block *sb)
453{
454 struct ufs_sb_info *sbi = UFS_SB(sb);
455 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct ufs_buffer_head * ubh;
457 unsigned char * base, * space;
458 unsigned size, blks, i;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700459 struct ufs_super_block_third *usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700461 UFSD("ENTER\n");
462
463 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /*
465 * Read cs structures from (usually) first data block
466 * on the device.
467 */
468 size = uspi->s_cssize;
469 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
470 base = space = kmalloc(size, GFP_KERNEL);
471 if (!base)
472 goto failed;
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800473 sbi->s_csp = (struct ufs_csum *)space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 for (i = 0; i < blks; i += uspi->s_fpb) {
475 size = uspi->s_bsize;
476 if (i + uspi->s_fpb > blks)
477 size = (blks - i) * uspi->s_fsize;
478
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800479 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
Evgeniy7b4ee732006-01-14 11:42:06 +0300480
481 if (!ubh)
482 goto failed;
483
484 ubh_ubhcpymem (space, ubh, size);
Evgeniy7b4ee732006-01-14 11:42:06 +0300485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 space += size;
487 ubh_brelse (ubh);
488 ubh = NULL;
489 }
490
491 /*
492 * Read cylinder group (we read only first fragment from block
493 * at this time) and prepare internal data structures for cg caching.
494 */
495 if (!(sbi->s_ucg = kmalloc (sizeof(struct buffer_head *) * uspi->s_ncg, GFP_KERNEL)))
496 goto failed;
497 for (i = 0; i < uspi->s_ncg; i++)
498 sbi->s_ucg[i] = NULL;
499 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
500 sbi->s_ucpi[i] = NULL;
501 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
502 }
503 for (i = 0; i < uspi->s_ncg; i++) {
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700504 UFSD("read cg %u\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (!(sbi->s_ucg[i] = sb_bread(sb, ufs_cgcmin(i))))
506 goto failed;
507 if (!ufs_cg_chkmagic (sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data))
508 goto failed;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 ufs_print_cylinder_stuff(sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
513 if (!(sbi->s_ucpi[i] = kmalloc (sizeof(struct ufs_cg_private_info), GFP_KERNEL)))
514 goto failed;
515 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
516 }
517 sbi->s_cg_loaded = 0;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700518 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return 1;
520
521failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800522 kfree (base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (sbi->s_ucg) {
524 for (i = 0; i < uspi->s_ncg; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800525 if (sbi->s_ucg[i])
526 brelse (sbi->s_ucg[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 kfree (sbi->s_ucg);
528 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800529 kfree (sbi->s_ucpi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700531 UFSD("EXIT (FAILED)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return 0;
533}
534
535/*
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700536 * Sync our internal copy of fs_cstotal with disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700538static void ufs_put_cstotal(struct super_block *sb)
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800539{
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700540 unsigned mtype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
541 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
542 struct ufs_super_block_first *usb1;
543 struct ufs_super_block_second *usb2;
544 struct ufs_super_block_third *usb3;
545
546 UFSD("ENTER\n");
547 usb1 = ubh_get_usb_first(uspi);
548 usb2 = ubh_get_usb_second(uspi);
549 usb3 = ubh_get_usb_third(uspi);
550
551 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
552 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
553 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
554 /*we have statistic in different place, then usual*/
555 usb2->fs_un.fs_u2.cs_ndir =
556 cpu_to_fs64(sb, uspi->cs_total.cs_ndir);
557 usb2->fs_un.fs_u2.cs_nbfree =
558 cpu_to_fs64(sb, uspi->cs_total.cs_nbfree);
559 usb3->fs_un1.fs_u2.cs_nifree =
560 cpu_to_fs64(sb, uspi->cs_total.cs_nifree);
561 usb3->fs_un1.fs_u2.cs_nffree =
562 cpu_to_fs64(sb, uspi->cs_total.cs_nffree);
563 } else {
564 usb1->fs_cstotal.cs_ndir =
565 cpu_to_fs32(sb, uspi->cs_total.cs_ndir);
566 usb1->fs_cstotal.cs_nbfree =
567 cpu_to_fs32(sb, uspi->cs_total.cs_nbfree);
568 usb1->fs_cstotal.cs_nifree =
569 cpu_to_fs32(sb, uspi->cs_total.cs_nifree);
570 usb1->fs_cstotal.cs_nffree =
571 cpu_to_fs32(sb, uspi->cs_total.cs_nffree);
572 }
573 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800574 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700575 UFSD("EXIT\n");
576}
577
578/**
579 * ufs_put_super_internal() - put on-disk intrenal structures
580 * @sb: pointer to super_block structure
581 * Put on-disk structures associated with cylinder groups
582 * and write them back to disk, also update cs_total on disk
583 */
584static void ufs_put_super_internal(struct super_block *sb)
585{
586 struct ufs_sb_info *sbi = UFS_SB(sb);
587 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 struct ufs_buffer_head * ubh;
589 unsigned char * base, * space;
590 unsigned blks, size, i;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700593 UFSD("ENTER\n");
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700594 ufs_put_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 size = uspi->s_cssize;
596 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800597 base = space = (char*) sbi->s_csp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 for (i = 0; i < blks; i += uspi->s_fpb) {
599 size = uspi->s_bsize;
600 if (i + uspi->s_fpb > blks)
601 size = (blks - i) * uspi->s_fsize;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 ubh_memcpyubh (ubh, space, size);
606 space += size;
607 ubh_mark_buffer_uptodate (ubh, 1);
608 ubh_mark_buffer_dirty (ubh);
609 ubh_brelse (ubh);
610 }
611 for (i = 0; i < sbi->s_cg_loaded; i++) {
612 ufs_put_cylinder (sb, i);
613 kfree (sbi->s_ucpi[i]);
614 }
615 for (; i < UFS_MAX_GROUP_LOADED; i++)
616 kfree (sbi->s_ucpi[i]);
617 for (i = 0; i < uspi->s_ncg; i++)
618 brelse (sbi->s_ucg[i]);
619 kfree (sbi->s_ucg);
620 kfree (base);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700621 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624static int ufs_fill_super(struct super_block *sb, void *data, int silent)
625{
626 struct ufs_sb_info * sbi;
627 struct ufs_sb_private_info * uspi;
628 struct ufs_super_block_first * usb1;
629 struct ufs_super_block_second * usb2;
630 struct ufs_super_block_third * usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 struct ufs_buffer_head * ubh;
632 struct inode *inode;
633 unsigned block_size, super_block_size;
634 unsigned flags;
Evgeniy7b4ee732006-01-14 11:42:06 +0300635 unsigned super_block_offset;
David Howellsb55c4602008-02-07 00:15:48 -0800636 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 uspi = NULL;
639 ubh = NULL;
640 flags = 0;
641
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700642 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700644 sbi = kzalloc(sizeof(struct ufs_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (!sbi)
646 goto failed_nomem;
647 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700649 UFSD("flag %u\n", (int)(sb->s_flags & MS_RDONLY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651#ifndef CONFIG_UFS_FS_WRITE
652 if (!(sb->s_flags & MS_RDONLY)) {
653 printk("ufs was compiled with read-only support, "
654 "can't be mounted as read-write\n");
655 goto failed;
656 }
657#endif
658 /*
659 * Set default mount options
660 * Parse mount options
661 */
662 sbi->s_mount_opt = 0;
663 ufs_set_opt (sbi->s_mount_opt, ONERROR_LOCK);
664 if (!ufs_parse_options ((char *) data, &sbi->s_mount_opt)) {
665 printk("wrong mount options\n");
666 goto failed;
667 }
668 if (!(sbi->s_mount_opt & UFS_MOUNT_UFSTYPE)) {
669 if (!silent)
670 printk("You didn't specify the type of your ufs filesystem\n\n"
671 "mount -t ufs -o ufstype="
Michael Owenc690a722006-03-24 18:21:44 +0100672 "sun|sunx86|44bsd|ufs2|5xbsd|old|hp|nextstep|nextstep-cd|openstep ...\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 ">>>WARNING<<< Wrong ufstype may corrupt your filesystem, "
674 "default is ufstype=old\n");
675 ufs_set_opt (sbi->s_mount_opt, UFSTYPE_OLD);
676 }
677
Mark Fortescue252e2112007-10-16 23:26:31 -0700678 uspi = kzalloc(sizeof(struct ufs_sb_private_info), GFP_KERNEL);
679 sbi->s_uspi = uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (!uspi)
681 goto failed;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800682 uspi->s_dirblksize = UFS_SECTOR_SIZE;
Evgeniy7b4ee732006-01-14 11:42:06 +0300683 super_block_offset=UFS_SBLOCK;
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /* Keep 2Gig file limit. Some UFS variants need to override
686 this but as I don't know which I'll let those in the know loosen
687 the rules */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 switch (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) {
689 case UFS_MOUNT_UFSTYPE_44BSD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700690 UFSD("ufstype=44bsd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 uspi->s_fsize = block_size = 512;
692 uspi->s_fmask = ~(512 - 1);
693 uspi->s_fshift = 9;
694 uspi->s_sbsize = super_block_size = 1536;
695 uspi->s_sbbase = 0;
696 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
697 break;
698 case UFS_MOUNT_UFSTYPE_UFS2:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700699 UFSD("ufstype=ufs2\n");
Evgeniy7b4ee732006-01-14 11:42:06 +0300700 super_block_offset=SBLOCK_UFS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 uspi->s_fsize = block_size = 512;
702 uspi->s_fmask = ~(512 - 1);
703 uspi->s_fshift = 9;
704 uspi->s_sbsize = super_block_size = 1536;
705 uspi->s_sbbase = 0;
706 flags |= UFS_TYPE_UFS2 | UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 break;
708
709 case UFS_MOUNT_UFSTYPE_SUN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700710 UFSD("ufstype=sun\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 uspi->s_fsize = block_size = 1024;
712 uspi->s_fmask = ~(1024 - 1);
713 uspi->s_fshift = 10;
714 uspi->s_sbsize = super_block_size = 2048;
715 uspi->s_sbbase = 0;
Mark Fortescue252e2112007-10-16 23:26:31 -0700716 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUN | UFS_CG_SUN;
718 break;
719
Mark Fortescue252e2112007-10-16 23:26:31 -0700720 case UFS_MOUNT_UFSTYPE_SUNOS:
721 UFSD(("ufstype=sunos\n"))
722 uspi->s_fsize = block_size = 1024;
723 uspi->s_fmask = ~(1024 - 1);
724 uspi->s_fshift = 10;
725 uspi->s_sbsize = 2048;
726 super_block_size = 2048;
727 uspi->s_sbbase = 0;
728 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
729 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_SUNOS | UFS_CG_SUN;
730 break;
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 case UFS_MOUNT_UFSTYPE_SUNx86:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700733 UFSD("ufstype=sunx86\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 uspi->s_fsize = block_size = 1024;
735 uspi->s_fmask = ~(1024 - 1);
736 uspi->s_fshift = 10;
737 uspi->s_sbsize = super_block_size = 2048;
738 uspi->s_sbbase = 0;
Mark Fortescue252e2112007-10-16 23:26:31 -0700739 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUNx86 | UFS_CG_SUN;
741 break;
742
743 case UFS_MOUNT_UFSTYPE_OLD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700744 UFSD("ufstype=old\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 uspi->s_fsize = block_size = 1024;
746 uspi->s_fmask = ~(1024 - 1);
747 uspi->s_fshift = 10;
748 uspi->s_sbsize = super_block_size = 2048;
749 uspi->s_sbbase = 0;
750 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
751 if (!(sb->s_flags & MS_RDONLY)) {
752 if (!silent)
753 printk(KERN_INFO "ufstype=old is supported read-only\n");
754 sb->s_flags |= MS_RDONLY;
755 }
756 break;
757
758 case UFS_MOUNT_UFSTYPE_NEXTSTEP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700759 UFSD("ufstype=nextstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 uspi->s_fsize = block_size = 1024;
761 uspi->s_fmask = ~(1024 - 1);
762 uspi->s_fshift = 10;
763 uspi->s_sbsize = super_block_size = 2048;
764 uspi->s_sbbase = 0;
Evgeniy Dushistov0c664f92007-12-04 23:45:06 -0800765 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
767 if (!(sb->s_flags & MS_RDONLY)) {
768 if (!silent)
769 printk(KERN_INFO "ufstype=nextstep is supported read-only\n");
770 sb->s_flags |= MS_RDONLY;
771 }
772 break;
773
774 case UFS_MOUNT_UFSTYPE_NEXTSTEP_CD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700775 UFSD("ufstype=nextstep-cd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 uspi->s_fsize = block_size = 2048;
777 uspi->s_fmask = ~(2048 - 1);
778 uspi->s_fshift = 11;
779 uspi->s_sbsize = super_block_size = 2048;
780 uspi->s_sbbase = 0;
Evgeniy Dushistov0c664f92007-12-04 23:45:06 -0800781 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
783 if (!(sb->s_flags & MS_RDONLY)) {
784 if (!silent)
785 printk(KERN_INFO "ufstype=nextstep-cd is supported read-only\n");
786 sb->s_flags |= MS_RDONLY;
787 }
788 break;
789
790 case UFS_MOUNT_UFSTYPE_OPENSTEP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700791 UFSD("ufstype=openstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 uspi->s_fsize = block_size = 1024;
793 uspi->s_fmask = ~(1024 - 1);
794 uspi->s_fshift = 10;
795 uspi->s_sbsize = super_block_size = 2048;
796 uspi->s_sbbase = 0;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800797 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
799 if (!(sb->s_flags & MS_RDONLY)) {
800 if (!silent)
801 printk(KERN_INFO "ufstype=openstep is supported read-only\n");
802 sb->s_flags |= MS_RDONLY;
803 }
804 break;
805
806 case UFS_MOUNT_UFSTYPE_HP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700807 UFSD("ufstype=hp\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 uspi->s_fsize = block_size = 1024;
809 uspi->s_fmask = ~(1024 - 1);
810 uspi->s_fshift = 10;
811 uspi->s_sbsize = super_block_size = 2048;
812 uspi->s_sbbase = 0;
813 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
814 if (!(sb->s_flags & MS_RDONLY)) {
815 if (!silent)
816 printk(KERN_INFO "ufstype=hp is supported read-only\n");
817 sb->s_flags |= MS_RDONLY;
818 }
819 break;
820 default:
821 if (!silent)
822 printk("unknown ufstype\n");
823 goto failed;
824 }
825
826again:
827 if (!sb_set_blocksize(sb, block_size)) {
828 printk(KERN_ERR "UFS: failed to set blocksize\n");
829 goto failed;
830 }
831
832 /*
833 * read ufs super block from device
834 */
Evgeniy7b4ee732006-01-14 11:42:06 +0300835
836 ubh = ubh_bread_uspi(uspi, sb, uspi->s_sbbase + super_block_offset/block_size, super_block_size);
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (!ubh)
839 goto failed;
840
Evgeniy7b4ee732006-01-14 11:42:06 +0300841 usb1 = ubh_get_usb_first(uspi);
842 usb2 = ubh_get_usb_second(uspi);
843 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Mark Fortescue252e2112007-10-16 23:26:31 -0700845 /* Sort out mod used on SunOS 4.1.3 for fs_state */
846 uspi->s_postblformat = fs32_to_cpu(sb, usb3->fs_postblformat);
847 if (((flags & UFS_ST_MASK) == UFS_ST_SUNOS) &&
848 (uspi->s_postblformat != UFS_42POSTBLFMT)) {
849 flags &= ~UFS_ST_MASK;
850 flags |= UFS_ST_SUN;
851 }
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /*
854 * Check ufs magic number
855 */
856 sbi->s_bytesex = BYTESEX_LE;
857 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
858 case UFS_MAGIC:
859 case UFS2_MAGIC:
860 case UFS_MAGIC_LFN:
861 case UFS_MAGIC_FEA:
862 case UFS_MAGIC_4GB:
863 goto magic_found;
864 }
865 sbi->s_bytesex = BYTESEX_BE;
866 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
867 case UFS_MAGIC:
868 case UFS2_MAGIC:
869 case UFS_MAGIC_LFN:
870 case UFS_MAGIC_FEA:
871 case UFS_MAGIC_4GB:
872 goto magic_found;
873 }
874
875 if ((((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP)
876 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP_CD)
877 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_OPENSTEP))
878 && uspi->s_sbbase < 256) {
879 ubh_brelse_uspi(uspi);
880 ubh = NULL;
881 uspi->s_sbbase += 8;
882 goto again;
883 }
884 if (!silent)
885 printk("ufs_read_super: bad magic number\n");
886 goto failed;
887
888magic_found:
889 /*
890 * Check block and fragment sizes
891 */
892 uspi->s_bsize = fs32_to_cpu(sb, usb1->fs_bsize);
893 uspi->s_fsize = fs32_to_cpu(sb, usb1->fs_fsize);
894 uspi->s_sbsize = fs32_to_cpu(sb, usb1->fs_sbsize);
895 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
896 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
897
vignesh babu9e8c4272007-07-15 23:41:36 -0700898 if (!is_power_of_2(uspi->s_fsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 printk(KERN_ERR "ufs_read_super: fragment size %u is not a power of 2\n",
900 uspi->s_fsize);
901 goto failed;
902 }
903 if (uspi->s_fsize < 512) {
904 printk(KERN_ERR "ufs_read_super: fragment size %u is too small\n",
905 uspi->s_fsize);
906 goto failed;
907 }
908 if (uspi->s_fsize > 4096) {
909 printk(KERN_ERR "ufs_read_super: fragment size %u is too large\n",
910 uspi->s_fsize);
911 goto failed;
912 }
vignesh babu9e8c4272007-07-15 23:41:36 -0700913 if (!is_power_of_2(uspi->s_bsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 printk(KERN_ERR "ufs_read_super: block size %u is not a power of 2\n",
915 uspi->s_bsize);
916 goto failed;
917 }
918 if (uspi->s_bsize < 4096) {
919 printk(KERN_ERR "ufs_read_super: block size %u is too small\n",
920 uspi->s_bsize);
921 goto failed;
922 }
923 if (uspi->s_bsize / uspi->s_fsize > 8) {
924 printk(KERN_ERR "ufs_read_super: too many fragments per block (%u)\n",
925 uspi->s_bsize / uspi->s_fsize);
926 goto failed;
927 }
928 if (uspi->s_fsize != block_size || uspi->s_sbsize != super_block_size) {
929 ubh_brelse_uspi(uspi);
930 ubh = NULL;
931 block_size = uspi->s_fsize;
932 super_block_size = uspi->s_sbsize;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700933 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 -0700934 goto again;
935 }
936
Andrew Mortonf664f1f2007-10-29 14:37:21 -0700937 sbi->s_flags = flags;/*after that line some functions use s_flags*/
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800938 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 /*
941 * Check, if file system was correctly unmounted.
942 * If not, make it read only.
943 */
Andrew Mortonf664f1f2007-10-29 14:37:21 -0700944 if (((flags & UFS_ST_MASK) == UFS_ST_44BSD) ||
945 ((flags & UFS_ST_MASK) == UFS_ST_OLD) ||
946 (((flags & UFS_ST_MASK) == UFS_ST_SUN ||
947 (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
948 (flags & UFS_ST_MASK) == UFS_ST_SUNx86) &&
949 (ufs_get_fs_state(sb, usb1, usb3) == (UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 switch(usb1->fs_clean) {
951 case UFS_FSCLEAN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700952 UFSD("fs is clean\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 break;
954 case UFS_FSSTABLE:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700955 UFSD("fs is stable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 break;
957 case UFS_FSOSF1:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700958 UFSD("fs is DEC OSF/1\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 break;
960 case UFS_FSACTIVE:
961 printk("ufs_read_super: fs is active\n");
962 sb->s_flags |= MS_RDONLY;
963 break;
964 case UFS_FSBAD:
965 printk("ufs_read_super: fs is bad\n");
966 sb->s_flags |= MS_RDONLY;
967 break;
968 default:
969 printk("ufs_read_super: can't grok fs_clean 0x%x\n", usb1->fs_clean);
970 sb->s_flags |= MS_RDONLY;
971 break;
972 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700973 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 printk("ufs_read_super: fs needs fsck\n");
975 sb->s_flags |= MS_RDONLY;
976 }
977
978 /*
979 * Read ufs_super_block into internal data structures
980 */
981 sb->s_op = &ufs_super_ops;
982 sb->dq_op = NULL; /***/
983 sb->s_magic = fs32_to_cpu(sb, usb3->fs_magic);
984
985 uspi->s_sblkno = fs32_to_cpu(sb, usb1->fs_sblkno);
986 uspi->s_cblkno = fs32_to_cpu(sb, usb1->fs_cblkno);
987 uspi->s_iblkno = fs32_to_cpu(sb, usb1->fs_iblkno);
988 uspi->s_dblkno = fs32_to_cpu(sb, usb1->fs_dblkno);
989 uspi->s_cgoffset = fs32_to_cpu(sb, usb1->fs_cgoffset);
990 uspi->s_cgmask = fs32_to_cpu(sb, usb1->fs_cgmask);
991
992 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700993 uspi->s_u2_size = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size);
994 uspi->s_u2_dsize = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
995 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 uspi->s_size = fs32_to_cpu(sb, usb1->fs_size);
997 uspi->s_dsize = fs32_to_cpu(sb, usb1->fs_dsize);
998 }
999
1000 uspi->s_ncg = fs32_to_cpu(sb, usb1->fs_ncg);
1001 /* s_bsize already set */
1002 /* s_fsize already set */
1003 uspi->s_fpb = fs32_to_cpu(sb, usb1->fs_frag);
1004 uspi->s_minfree = fs32_to_cpu(sb, usb1->fs_minfree);
1005 uspi->s_bmask = fs32_to_cpu(sb, usb1->fs_bmask);
1006 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
1007 uspi->s_bshift = fs32_to_cpu(sb, usb1->fs_bshift);
1008 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001009 UFSD("uspi->s_bshift = %d,uspi->s_fshift = %d", uspi->s_bshift,
1010 uspi->s_fshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 uspi->s_fpbshift = fs32_to_cpu(sb, usb1->fs_fragshift);
1012 uspi->s_fsbtodb = fs32_to_cpu(sb, usb1->fs_fsbtodb);
1013 /* s_sbsize already set */
1014 uspi->s_csmask = fs32_to_cpu(sb, usb1->fs_csmask);
1015 uspi->s_csshift = fs32_to_cpu(sb, usb1->fs_csshift);
1016 uspi->s_nindir = fs32_to_cpu(sb, usb1->fs_nindir);
1017 uspi->s_inopb = fs32_to_cpu(sb, usb1->fs_inopb);
1018 uspi->s_nspf = fs32_to_cpu(sb, usb1->fs_nspf);
1019 uspi->s_npsect = ufs_get_fs_npsect(sb, usb1, usb3);
1020 uspi->s_interleave = fs32_to_cpu(sb, usb1->fs_interleave);
1021 uspi->s_trackskew = fs32_to_cpu(sb, usb1->fs_trackskew);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -08001022
1023 if (uspi->fs_magic == UFS2_MAGIC)
1024 uspi->s_csaddr = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_csaddr);
1025 else
1026 uspi->s_csaddr = fs32_to_cpu(sb, usb1->fs_csaddr);
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 uspi->s_cssize = fs32_to_cpu(sb, usb1->fs_cssize);
1029 uspi->s_cgsize = fs32_to_cpu(sb, usb1->fs_cgsize);
1030 uspi->s_ntrak = fs32_to_cpu(sb, usb1->fs_ntrak);
1031 uspi->s_nsect = fs32_to_cpu(sb, usb1->fs_nsect);
1032 uspi->s_spc = fs32_to_cpu(sb, usb1->fs_spc);
1033 uspi->s_ipg = fs32_to_cpu(sb, usb1->fs_ipg);
1034 uspi->s_fpg = fs32_to_cpu(sb, usb1->fs_fpg);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001035 uspi->s_cpc = fs32_to_cpu(sb, usb2->fs_un.fs_u1.fs_cpc);
1036 uspi->s_contigsumsize = fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_contigsumsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 uspi->s_qbmask = ufs_get_fs_qbmask(sb, usb3);
1038 uspi->s_qfmask = ufs_get_fs_qfmask(sb, usb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 uspi->s_nrpos = fs32_to_cpu(sb, usb3->fs_nrpos);
1040 uspi->s_postbloff = fs32_to_cpu(sb, usb3->fs_postbloff);
1041 uspi->s_rotbloff = fs32_to_cpu(sb, usb3->fs_rotbloff);
1042
1043 /*
1044 * Compute another frequently used values
1045 */
1046 uspi->s_fpbmask = uspi->s_fpb - 1;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001047 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 uspi->s_apbshift = uspi->s_bshift - 3;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001049 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 uspi->s_apbshift = uspi->s_bshift - 2;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 uspi->s_2apbshift = uspi->s_apbshift * 2;
1053 uspi->s_3apbshift = uspi->s_apbshift * 3;
1054 uspi->s_apb = 1 << uspi->s_apbshift;
1055 uspi->s_2apb = 1 << uspi->s_2apbshift;
1056 uspi->s_3apb = 1 << uspi->s_3apbshift;
1057 uspi->s_apbmask = uspi->s_apb - 1;
1058 uspi->s_nspfshift = uspi->s_fshift - UFS_SECTOR_BITS;
1059 uspi->s_nspb = uspi->s_nspf << uspi->s_fpbshift;
1060 uspi->s_inopf = uspi->s_inopb >> uspi->s_fpbshift;
1061 uspi->s_bpf = uspi->s_fsize << 3;
1062 uspi->s_bpfshift = uspi->s_fshift + 3;
1063 uspi->s_bpfmask = uspi->s_bpf - 1;
1064 if ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) ==
1065 UFS_MOUNT_UFSTYPE_44BSD)
1066 uspi->s_maxsymlinklen =
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001067 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
David Howellsb55c4602008-02-07 00:15:48 -08001069 inode = ufs_iget(sb, UFS_ROOTINO);
1070 if (IS_ERR(inode)) {
1071 ret = PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 goto failed;
David Howellsb55c4602008-02-07 00:15:48 -08001073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 sb->s_root = d_alloc_root(inode);
David Howellsb55c4602008-02-07 00:15:48 -08001075 if (!sb->s_root) {
1076 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 goto dalloc_failed;
David Howellsb55c4602008-02-07 00:15:48 -08001078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001080 ufs_setup_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 /*
1082 * Read cylinder group structures
1083 */
1084 if (!(sb->s_flags & MS_RDONLY))
1085 if (!ufs_read_cylinder_structures(sb))
1086 goto failed;
1087
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001088 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return 0;
1090
1091dalloc_failed:
1092 iput(inode);
1093failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -08001094 if (ubh)
1095 ubh_brelse_uspi (uspi);
1096 kfree (uspi);
1097 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 sb->s_fs_info = NULL;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001099 UFSD("EXIT (FAILED)\n");
David Howellsb55c4602008-02-07 00:15:48 -08001100 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102failed_nomem:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001103 UFSD("EXIT (NOMEM)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return -ENOMEM;
1105}
1106
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001107static void ufs_write_super(struct super_block *sb)
1108{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct ufs_sb_private_info * uspi;
1110 struct ufs_super_block_first * usb1;
1111 struct ufs_super_block_third * usb3;
1112 unsigned flags;
1113
1114 lock_kernel();
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001115 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 flags = UFS_SB(sb)->s_flags;
1117 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +03001118 usb1 = ubh_get_usb_first(uspi);
1119 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 if (!(sb->s_flags & MS_RDONLY)) {
1122 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1123 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
Mark Fortescue252e2112007-10-16 23:26:31 -07001124 || (flags & UFS_ST_MASK) == UFS_ST_SUNOS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1126 ufs_set_fs_state(sb, usb1, usb3,
1127 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001128 ufs_put_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130 sb->s_dirt = 0;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001131 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 unlock_kernel();
1133}
1134
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001135static void ufs_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 struct ufs_sb_info * sbi = UFS_SB(sb);
1138
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001139 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 if (!(sb->s_flags & MS_RDONLY))
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001142 ufs_put_super_internal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 ubh_brelse_uspi (sbi->s_uspi);
1145 kfree (sbi->s_uspi);
1146 kfree (sbi);
1147 sb->s_fs_info = NULL;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001148 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return;
1150}
1151
1152
1153static int ufs_remount (struct super_block *sb, int *mount_flags, char *data)
1154{
1155 struct ufs_sb_private_info * uspi;
1156 struct ufs_super_block_first * usb1;
1157 struct ufs_super_block_third * usb3;
1158 unsigned new_mount_opt, ufstype;
1159 unsigned flags;
1160
1161 uspi = UFS_SB(sb)->s_uspi;
1162 flags = UFS_SB(sb)->s_flags;
Evgeniy7b4ee732006-01-14 11:42:06 +03001163 usb1 = ubh_get_usb_first(uspi);
1164 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 /*
1167 * Allow the "check" option to be passed as a remount option.
1168 * It is not possible to change ufstype option during remount
1169 */
1170 ufstype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
1171 new_mount_opt = 0;
1172 ufs_set_opt (new_mount_opt, ONERROR_LOCK);
1173 if (!ufs_parse_options (data, &new_mount_opt))
1174 return -EINVAL;
1175 if (!(new_mount_opt & UFS_MOUNT_UFSTYPE)) {
1176 new_mount_opt |= ufstype;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001177 } else if ((new_mount_opt & UFS_MOUNT_UFSTYPE) != ufstype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 printk("ufstype can't be changed during remount\n");
1179 return -EINVAL;
1180 }
1181
1182 if ((*mount_flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
1183 UFS_SB(sb)->s_mount_opt = new_mount_opt;
1184 return 0;
1185 }
1186
1187 /*
1188 * fs was mouted as rw, remounting ro
1189 */
1190 if (*mount_flags & MS_RDONLY) {
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001191 ufs_put_super_internal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1193 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
Mark Fortescue252e2112007-10-16 23:26:31 -07001194 || (flags & UFS_ST_MASK) == UFS_ST_SUNOS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1196 ufs_set_fs_state(sb, usb1, usb3,
1197 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -07001198 ubh_mark_buffer_dirty (USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 sb->s_dirt = 0;
1200 sb->s_flags |= MS_RDONLY;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001201 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /*
1203 * fs was mounted as ro, remounting rw
1204 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205#ifndef CONFIG_UFS_FS_WRITE
1206 printk("ufs was compiled with read-only support, "
1207 "can't be mounted as read-write\n");
1208 return -EINVAL;
1209#else
1210 if (ufstype != UFS_MOUNT_UFSTYPE_SUN &&
Mark Fortescue252e2112007-10-16 23:26:31 -07001211 ufstype != UFS_MOUNT_UFSTYPE_SUNOS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 ufstype != UFS_MOUNT_UFSTYPE_44BSD &&
Evgeniy Dushistovcbcae392007-02-12 00:54:30 -08001213 ufstype != UFS_MOUNT_UFSTYPE_SUNx86 &&
1214 ufstype != UFS_MOUNT_UFSTYPE_UFS2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 printk("this ufstype is read-only supported\n");
1216 return -EINVAL;
1217 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001218 if (!ufs_read_cylinder_structures(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 printk("failed during remounting\n");
1220 return -EPERM;
1221 }
1222 sb->s_flags &= ~MS_RDONLY;
1223#endif
1224 }
1225 UFS_SB(sb)->s_mount_opt = new_mount_opt;
1226 return 0;
1227}
1228
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001229static int ufs_show_options(struct seq_file *seq, struct vfsmount *vfs)
1230{
1231 struct ufs_sb_info *sbi = UFS_SB(vfs->mnt_sb);
1232 unsigned mval = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
1233 struct match_token *tp = tokens;
1234
1235 while (tp->token != Opt_onerror_panic && tp->token != mval)
1236 ++tp;
1237 BUG_ON(tp->token == Opt_onerror_panic);
1238 seq_printf(seq, ",%s", tp->pattern);
1239
1240 mval = sbi->s_mount_opt & UFS_MOUNT_ONERROR;
1241 while (tp->token != Opt_err && tp->token != mval)
1242 ++tp;
1243 BUG_ON(tp->token == Opt_err);
1244 seq_printf(seq, ",%s", tp->pattern);
1245
1246 return 0;
1247}
1248
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001249static int ufs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
David Howells726c3342006-06-23 02:02:58 -07001251 struct super_block *sb = dentry->d_sb;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001252 struct ufs_sb_private_info *uspi= UFS_SB(sb)->s_uspi;
1253 unsigned flags = UFS_SB(sb)->s_flags;
1254 struct ufs_super_block_first *usb1;
1255 struct ufs_super_block_second *usb2;
1256 struct ufs_super_block_third *usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 lock_kernel();
1259
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001260 usb1 = ubh_get_usb_first(uspi);
1261 usb2 = ubh_get_usb_second(uspi);
1262 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
1265 buf->f_type = UFS2_MAGIC;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001266 buf->f_blocks = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001267 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 buf->f_type = UFS_MAGIC;
1269 buf->f_blocks = uspi->s_dsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001271 buf->f_bfree = ufs_blkstofrags(uspi->cs_total.cs_nbfree) +
1272 uspi->cs_total.cs_nffree;
1273 buf->f_ffree = uspi->cs_total.cs_nifree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 buf->f_bsize = sb->s_blocksize;
1275 buf->f_bavail = (buf->f_bfree > (((long)buf->f_blocks / 100) * uspi->s_minfree))
1276 ? (buf->f_bfree - (((long)buf->f_blocks / 100) * uspi->s_minfree)) : 0;
1277 buf->f_files = uspi->s_ncg * uspi->s_ipg;
1278 buf->f_namelen = UFS_MAXNAMLEN;
1279
1280 unlock_kernel();
1281
1282 return 0;
1283}
1284
Christoph Lametere18b8902006-12-06 20:33:20 -08001285static struct kmem_cache * ufs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287static struct inode *ufs_alloc_inode(struct super_block *sb)
1288{
1289 struct ufs_inode_info *ei;
Christoph Lametere94b1762006-12-06 20:33:17 -08001290 ei = (struct ufs_inode_info *)kmem_cache_alloc(ufs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (!ei)
1292 return NULL;
1293 ei->vfs_inode.i_version = 1;
1294 return &ei->vfs_inode;
1295}
1296
1297static void ufs_destroy_inode(struct inode *inode)
1298{
1299 kmem_cache_free(ufs_inode_cachep, UFS_I(inode));
1300}
1301
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -07001302static void init_once(struct kmem_cache * cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct ufs_inode_info *ei = (struct ufs_inode_info *) foo;
1305
Christoph Lametera35afb82007-05-16 22:10:57 -07001306 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
Paul Mundt20c2df82007-07-20 10:11:58 +09001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309static int init_inodecache(void)
1310{
1311 ufs_inode_cachep = kmem_cache_create("ufs_inode_cache",
1312 sizeof(struct ufs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001313 0, (SLAB_RECLAIM_ACCOUNT|
1314 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001315 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (ufs_inode_cachep == NULL)
1317 return -ENOMEM;
1318 return 0;
1319}
1320
1321static void destroy_inodecache(void)
1322{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001323 kmem_cache_destroy(ufs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
1326#ifdef CONFIG_QUOTA
1327static ssize_t ufs_quota_read(struct super_block *, int, char *,size_t, loff_t);
1328static ssize_t ufs_quota_write(struct super_block *, int, const char *, size_t, loff_t);
1329#endif
1330
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001331static const struct super_operations ufs_super_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 .alloc_inode = ufs_alloc_inode,
1333 .destroy_inode = ufs_destroy_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 .write_inode = ufs_write_inode,
1335 .delete_inode = ufs_delete_inode,
1336 .put_super = ufs_put_super,
1337 .write_super = ufs_write_super,
1338 .statfs = ufs_statfs,
1339 .remount_fs = ufs_remount,
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001340 .show_options = ufs_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341#ifdef CONFIG_QUOTA
1342 .quota_read = ufs_quota_read,
1343 .quota_write = ufs_quota_write,
1344#endif
1345};
1346
1347#ifdef CONFIG_QUOTA
1348
1349/* Read data from quotafile - avoid pagecache and such because we cannot afford
1350 * acquiring the locks... As quota files are never truncated and quota code
1351 * itself serializes the operations (and noone else should touch the files)
1352 * we don't have to be afraid of races */
1353static ssize_t ufs_quota_read(struct super_block *sb, int type, char *data,
1354 size_t len, loff_t off)
1355{
1356 struct inode *inode = sb_dqopt(sb)->files[type];
1357 sector_t blk = off >> sb->s_blocksize_bits;
1358 int err = 0;
1359 int offset = off & (sb->s_blocksize - 1);
1360 int tocopy;
1361 size_t toread;
1362 struct buffer_head *bh;
1363 loff_t i_size = i_size_read(inode);
1364
1365 if (off > i_size)
1366 return 0;
1367 if (off+len > i_size)
1368 len = i_size-off;
1369 toread = len;
1370 while (toread > 0) {
1371 tocopy = sb->s_blocksize - offset < toread ?
1372 sb->s_blocksize - offset : toread;
1373
1374 bh = ufs_bread(inode, blk, 0, &err);
1375 if (err)
1376 return err;
1377 if (!bh) /* A hole? */
1378 memset(data, 0, tocopy);
1379 else {
1380 memcpy(data, bh->b_data+offset, tocopy);
1381 brelse(bh);
1382 }
1383 offset = 0;
1384 toread -= tocopy;
1385 data += tocopy;
1386 blk++;
1387 }
1388 return len;
1389}
1390
1391/* Write to quotafile */
1392static ssize_t ufs_quota_write(struct super_block *sb, int type,
1393 const char *data, size_t len, loff_t off)
1394{
1395 struct inode *inode = sb_dqopt(sb)->files[type];
1396 sector_t blk = off >> sb->s_blocksize_bits;
1397 int err = 0;
1398 int offset = off & (sb->s_blocksize - 1);
1399 int tocopy;
1400 size_t towrite = len;
1401 struct buffer_head *bh;
1402
Arjan van de Ven5c81a412006-07-03 00:25:20 -07001403 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 while (towrite > 0) {
1405 tocopy = sb->s_blocksize - offset < towrite ?
1406 sb->s_blocksize - offset : towrite;
1407
1408 bh = ufs_bread(inode, blk, 1, &err);
1409 if (!bh)
1410 goto out;
1411 lock_buffer(bh);
1412 memcpy(bh->b_data+offset, data, tocopy);
1413 flush_dcache_page(bh->b_page);
1414 set_buffer_uptodate(bh);
1415 mark_buffer_dirty(bh);
1416 unlock_buffer(bh);
1417 brelse(bh);
1418 offset = 0;
1419 towrite -= tocopy;
1420 data += tocopy;
1421 blk++;
1422 }
1423out:
Evgeniy Polyakov15b2fe32006-01-08 01:04:51 -08001424 if (len == towrite) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001425 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return err;
Evgeniy Polyakov15b2fe32006-01-08 01:04:51 -08001427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (inode->i_size < off+len-towrite)
1429 i_size_write(inode, off+len-towrite);
1430 inode->i_version++;
1431 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
1432 mark_inode_dirty(inode);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001433 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return len - towrite;
1435}
1436
1437#endif
1438
David Howells454e2392006-06-23 02:02:57 -07001439static int ufs_get_sb(struct file_system_type *fs_type,
1440 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
David Howells454e2392006-06-23 02:02:57 -07001442 return get_sb_bdev(fs_type, flags, dev_name, data, ufs_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
1445static struct file_system_type ufs_fs_type = {
1446 .owner = THIS_MODULE,
1447 .name = "ufs",
1448 .get_sb = ufs_get_sb,
1449 .kill_sb = kill_block_super,
1450 .fs_flags = FS_REQUIRES_DEV,
1451};
1452
1453static int __init init_ufs_fs(void)
1454{
1455 int err = init_inodecache();
1456 if (err)
1457 goto out1;
1458 err = register_filesystem(&ufs_fs_type);
1459 if (err)
1460 goto out;
1461 return 0;
1462out:
1463 destroy_inodecache();
1464out1:
1465 return err;
1466}
1467
1468static void __exit exit_ufs_fs(void)
1469{
1470 unregister_filesystem(&ufs_fs_type);
1471 destroy_inodecache();
1472}
1473
1474module_init(init_ufs_fs)
1475module_exit(exit_ufs_fs)
1476MODULE_LICENSE("GPL");