blob: a5ecabfdc9761476a72e04d24c4ea13b53f69484 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ufs/super.c
3 *
4 * Copyright (C) 1998
5 * Daniel Pirkl <daniel.pirkl@email.cz>
6 * Charles University, Faculty of Mathematics and Physics
7 */
8
9/* Derived from
10 *
11 * linux/fs/ext2/super.c
12 *
13 * Copyright (C) 1992, 1993, 1994, 1995
14 * Remy Card (card@masi.ibp.fr)
15 * Laboratoire MASI - Institut Blaise Pascal
16 * Universite Pierre et Marie Curie (Paris VI)
17 *
18 * from
19 *
20 * linux/fs/minix/inode.c
21 *
22 * Copyright (C) 1991, 1992 Linus Torvalds
23 *
24 * Big-endian to little-endian byte-swapping/bitmaps by
25 * David S. Miller (davem@caip.rutgers.edu), 1995
26 */
27
28/*
29 * Inspired by
30 *
31 * linux/fs/ufs/super.c
32 *
33 * Copyright (C) 1996
34 * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu)
35 * Laboratory for Computer Science Research Computing Facility
36 * Rutgers, The State University of New Jersey
37 *
38 * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
39 *
40 * Kernel module support added on 96/04/26 by
41 * Stefan Reinauer <stepan@home.culture.mipt.ru>
42 *
43 * Module usage counts added on 96/04/29 by
Gertjan van Wingerdef762dd62009-03-21 23:18:57 +010044 * Gertjan van Wingerde <gwingerde@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 *
46 * Clean swab support on 19970406 by
47 * Francois-Rene Rideau <fare@tunes.org>
48 *
49 * 4.4BSD (FreeBSD) support added on February 1st 1998 by
50 * Niels Kristian Bech Jensen <nkbj@image.dk> partially based
51 * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>.
52 *
53 * NeXTstep support added on February 5th 1998 by
54 * Niels Kristian Bech Jensen <nkbj@image.dk>.
55 *
56 * write support Daniel Pirkl <daniel.pirkl@email.cz> 1998
57 *
58 * HP/UX hfs filesystem support added by
59 * Martin K. Petersen <mkp@mkp.net>, August 1999
60 *
61 * UFS2 (of FreeBSD 5.x) support added by
62 * Niraj Kumar <niraj17@iitbombay.org>, Jan 2004
63 *
Evgeniy Dushistovcbcae392007-02-12 00:54:30 -080064 * UFS2 write support added by
65 * Evgeniy Dushistov <dushistov@mail.ru>, 2007
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 */
67
68
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>
Jan Kara74abb982008-07-25 01:46:51 -070079#include <linux/quotaops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/slab.h>
81#include <linux/time.h>
82#include <linux/stat.h>
83#include <linux/string.h>
84#include <linux/blkdev.h>
85#include <linux/init.h>
86#include <linux/parser.h>
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
Mike Frysingere5420592008-02-08 04:21:31 -080094#include "ufs_fs.h"
Christoph Hellwigbcd6d4e2007-10-16 23:26:51 -070095#include "ufs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include "swab.h"
97#include "util.h"
98
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070099#ifdef CONFIG_UFS_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
101 * Print contents of ufs_super_block, useful for debugging
102 */
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800103static void ufs_print_super_stuff(struct super_block *sb,
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700104 struct ufs_super_block_first *usb1,
105 struct ufs_super_block_second *usb2,
106 struct ufs_super_block_third *usb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800108 u32 magic = fs32_to_cpu(sb, usb3->fs_magic);
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 printk("ufs_print_super_stuff\n");
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800111 printk(" magic: 0x%x\n", magic);
112 if (fs32_to_cpu(sb, usb3->fs_magic) == UFS2_MAGIC) {
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700113 printk(" fs_size: %llu\n", (unsigned long long)
114 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size));
115 printk(" fs_dsize: %llu\n", (unsigned long long)
116 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize));
117 printk(" bsize: %u\n",
118 fs32_to_cpu(sb, usb1->fs_bsize));
119 printk(" fsize: %u\n",
120 fs32_to_cpu(sb, usb1->fs_fsize));
121 printk(" fs_volname: %s\n", usb2->fs_un.fs_u2.fs_volname);
122 printk(" fs_sblockloc: %llu\n", (unsigned long long)
123 fs64_to_cpu(sb, usb2->fs_un.fs_u2.fs_sblockloc));
124 printk(" cs_ndir(No of dirs): %llu\n", (unsigned long long)
125 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir));
126 printk(" cs_nbfree(No of free blocks): %llu\n",
127 (unsigned long long)
128 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800129 printk(KERN_INFO" cs_nifree(Num of free inodes): %llu\n",
130 (unsigned long long)
131 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree));
132 printk(KERN_INFO" cs_nffree(Num of free frags): %llu\n",
133 (unsigned long long)
134 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree));
Evgeniy Dushistov90b315a2008-02-08 04:20:16 -0800135 printk(KERN_INFO" fs_maxsymlinklen: %u\n",
136 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen));
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700137 } else {
138 printk(" sblkno: %u\n", fs32_to_cpu(sb, usb1->fs_sblkno));
139 printk(" cblkno: %u\n", fs32_to_cpu(sb, usb1->fs_cblkno));
140 printk(" iblkno: %u\n", fs32_to_cpu(sb, usb1->fs_iblkno));
141 printk(" dblkno: %u\n", fs32_to_cpu(sb, usb1->fs_dblkno));
142 printk(" cgoffset: %u\n",
143 fs32_to_cpu(sb, usb1->fs_cgoffset));
144 printk(" ~cgmask: 0x%x\n",
145 ~fs32_to_cpu(sb, usb1->fs_cgmask));
146 printk(" size: %u\n", fs32_to_cpu(sb, usb1->fs_size));
147 printk(" dsize: %u\n", fs32_to_cpu(sb, usb1->fs_dsize));
148 printk(" ncg: %u\n", fs32_to_cpu(sb, usb1->fs_ncg));
149 printk(" bsize: %u\n", fs32_to_cpu(sb, usb1->fs_bsize));
150 printk(" fsize: %u\n", fs32_to_cpu(sb, usb1->fs_fsize));
151 printk(" frag: %u\n", fs32_to_cpu(sb, usb1->fs_frag));
152 printk(" fragshift: %u\n",
153 fs32_to_cpu(sb, usb1->fs_fragshift));
154 printk(" ~fmask: %u\n", ~fs32_to_cpu(sb, usb1->fs_fmask));
155 printk(" fshift: %u\n", fs32_to_cpu(sb, usb1->fs_fshift));
156 printk(" sbsize: %u\n", fs32_to_cpu(sb, usb1->fs_sbsize));
157 printk(" spc: %u\n", fs32_to_cpu(sb, usb1->fs_spc));
158 printk(" cpg: %u\n", fs32_to_cpu(sb, usb1->fs_cpg));
159 printk(" ipg: %u\n", fs32_to_cpu(sb, usb1->fs_ipg));
160 printk(" fpg: %u\n", fs32_to_cpu(sb, usb1->fs_fpg));
161 printk(" csaddr: %u\n", fs32_to_cpu(sb, usb1->fs_csaddr));
162 printk(" cssize: %u\n", fs32_to_cpu(sb, usb1->fs_cssize));
163 printk(" cgsize: %u\n", fs32_to_cpu(sb, usb1->fs_cgsize));
164 printk(" fstodb: %u\n",
165 fs32_to_cpu(sb, usb1->fs_fsbtodb));
166 printk(" nrpos: %u\n", fs32_to_cpu(sb, usb3->fs_nrpos));
167 printk(" ndir %u\n",
168 fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir));
169 printk(" nifree %u\n",
170 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree));
171 printk(" nbfree %u\n",
172 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree));
173 printk(" nffree %u\n",
174 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree));
175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 printk("\n");
177}
178
179/*
180 * Print contents of ufs_cylinder_group, useful for debugging
181 */
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700182static void ufs_print_cylinder_stuff(struct super_block *sb,
183 struct ufs_cylinder_group *cg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 printk("\nufs_print_cylinder_stuff\n");
Andrew Morton96710b22006-06-25 05:47:28 -0700186 printk("size of ucg: %zu\n", sizeof(struct ufs_cylinder_group));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 printk(" magic: %x\n", fs32_to_cpu(sb, cg->cg_magic));
188 printk(" time: %u\n", fs32_to_cpu(sb, cg->cg_time));
189 printk(" cgx: %u\n", fs32_to_cpu(sb, cg->cg_cgx));
190 printk(" ncyl: %u\n", fs16_to_cpu(sb, cg->cg_ncyl));
191 printk(" niblk: %u\n", fs16_to_cpu(sb, cg->cg_niblk));
192 printk(" ndblk: %u\n", fs32_to_cpu(sb, cg->cg_ndblk));
193 printk(" cs_ndir: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_ndir));
194 printk(" cs_nbfree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nbfree));
195 printk(" cs_nifree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nifree));
196 printk(" cs_nffree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nffree));
197 printk(" rotor: %u\n", fs32_to_cpu(sb, cg->cg_rotor));
198 printk(" frotor: %u\n", fs32_to_cpu(sb, cg->cg_frotor));
199 printk(" irotor: %u\n", fs32_to_cpu(sb, cg->cg_irotor));
200 printk(" frsum: %u, %u, %u, %u, %u, %u, %u, %u\n",
201 fs32_to_cpu(sb, cg->cg_frsum[0]), fs32_to_cpu(sb, cg->cg_frsum[1]),
202 fs32_to_cpu(sb, cg->cg_frsum[2]), fs32_to_cpu(sb, cg->cg_frsum[3]),
203 fs32_to_cpu(sb, cg->cg_frsum[4]), fs32_to_cpu(sb, cg->cg_frsum[5]),
204 fs32_to_cpu(sb, cg->cg_frsum[6]), fs32_to_cpu(sb, cg->cg_frsum[7]));
205 printk(" btotoff: %u\n", fs32_to_cpu(sb, cg->cg_btotoff));
206 printk(" boff: %u\n", fs32_to_cpu(sb, cg->cg_boff));
207 printk(" iuseoff: %u\n", fs32_to_cpu(sb, cg->cg_iusedoff));
208 printk(" freeoff: %u\n", fs32_to_cpu(sb, cg->cg_freeoff));
209 printk(" nextfreeoff: %u\n", fs32_to_cpu(sb, cg->cg_nextfreeoff));
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700210 printk(" clustersumoff %u\n",
211 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clustersumoff));
212 printk(" clusteroff %u\n",
213 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clusteroff));
214 printk(" nclusterblks %u\n",
215 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_nclusterblks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 printk("\n");
217}
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700218#else
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800219# define ufs_print_super_stuff(sb, usb1, usb2, usb3) /**/
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700220# define ufs_print_cylinder_stuff(sb, cg) /**/
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700221#endif /* CONFIG_UFS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800223static const struct super_operations ufs_super_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225static char error_buf[1024];
226
227void ufs_error (struct super_block * sb, const char * function,
228 const char * fmt, ...)
229{
230 struct ufs_sb_private_info * uspi;
231 struct ufs_super_block_first * usb1;
232 va_list args;
233
234 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300235 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 if (!(sb->s_flags & MS_RDONLY)) {
238 usb1->fs_clean = UFS_FSBAD;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700239 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 sb->s_dirt = 1;
241 sb->s_flags |= MS_RDONLY;
242 }
243 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800244 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 va_end (args);
246 switch (UFS_SB(sb)->s_mount_opt & UFS_MOUNT_ONERROR) {
247 case UFS_MOUNT_ONERROR_PANIC:
248 panic ("UFS-fs panic (device %s): %s: %s\n",
249 sb->s_id, function, error_buf);
250
251 case UFS_MOUNT_ONERROR_LOCK:
252 case UFS_MOUNT_ONERROR_UMOUNT:
253 case UFS_MOUNT_ONERROR_REPAIR:
254 printk (KERN_CRIT "UFS-fs error (device %s): %s: %s\n",
255 sb->s_id, function, error_buf);
256 }
257}
258
259void ufs_panic (struct super_block * sb, const char * function,
260 const char * fmt, ...)
261{
262 struct ufs_sb_private_info * uspi;
263 struct ufs_super_block_first * usb1;
264 va_list args;
265
266 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300267 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 if (!(sb->s_flags & MS_RDONLY)) {
270 usb1->fs_clean = UFS_FSBAD;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700271 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 sb->s_dirt = 1;
273 }
274 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800275 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 va_end (args);
277 sb->s_flags |= MS_RDONLY;
278 printk (KERN_CRIT "UFS-fs panic (device %s): %s: %s\n",
279 sb->s_id, function, error_buf);
280}
281
282void ufs_warning (struct super_block * sb, const char * function,
283 const char * fmt, ...)
284{
285 va_list args;
286
287 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800288 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 va_end (args);
290 printk (KERN_WARNING "UFS-fs warning (device %s): %s: %s\n",
291 sb->s_id, function, error_buf);
292}
293
294enum {
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -0700295 Opt_type_old = UFS_MOUNT_UFSTYPE_OLD,
296 Opt_type_sunx86 = UFS_MOUNT_UFSTYPE_SUNx86,
297 Opt_type_sun = UFS_MOUNT_UFSTYPE_SUN,
298 Opt_type_sunos = UFS_MOUNT_UFSTYPE_SUNOS,
299 Opt_type_44bsd = UFS_MOUNT_UFSTYPE_44BSD,
300 Opt_type_ufs2 = UFS_MOUNT_UFSTYPE_UFS2,
301 Opt_type_hp = UFS_MOUNT_UFSTYPE_HP,
302 Opt_type_nextstepcd = UFS_MOUNT_UFSTYPE_NEXTSTEP_CD,
303 Opt_type_nextstep = UFS_MOUNT_UFSTYPE_NEXTSTEP,
304 Opt_type_openstep = UFS_MOUNT_UFSTYPE_OPENSTEP,
305 Opt_onerror_panic = UFS_MOUNT_ONERROR_PANIC,
306 Opt_onerror_lock = UFS_MOUNT_ONERROR_LOCK,
307 Opt_onerror_umount = UFS_MOUNT_ONERROR_UMOUNT,
308 Opt_onerror_repair = UFS_MOUNT_ONERROR_REPAIR,
309 Opt_err
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
Steven Whitehousea447c092008-10-13 10:46:57 +0100312static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 {Opt_type_old, "ufstype=old"},
314 {Opt_type_sunx86, "ufstype=sunx86"},
315 {Opt_type_sun, "ufstype=sun"},
Mark Fortescue252e2112007-10-16 23:26:31 -0700316 {Opt_type_sunos, "ufstype=sunos"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 {Opt_type_44bsd, "ufstype=44bsd"},
318 {Opt_type_ufs2, "ufstype=ufs2"},
319 {Opt_type_ufs2, "ufstype=5xbsd"},
320 {Opt_type_hp, "ufstype=hp"},
321 {Opt_type_nextstepcd, "ufstype=nextstep-cd"},
322 {Opt_type_nextstep, "ufstype=nextstep"},
323 {Opt_type_openstep, "ufstype=openstep"},
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -0700324/*end of possible ufs types */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 {Opt_onerror_panic, "onerror=panic"},
326 {Opt_onerror_lock, "onerror=lock"},
327 {Opt_onerror_umount, "onerror=umount"},
328 {Opt_onerror_repair, "onerror=repair"},
329 {Opt_err, NULL}
330};
331
332static int ufs_parse_options (char * options, unsigned * mount_options)
333{
334 char * p;
335
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700336 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if (!options)
339 return 1;
340
341 while ((p = strsep(&options, ",")) != NULL) {
342 substring_t args[MAX_OPT_ARGS];
343 int token;
344 if (!*p)
345 continue;
346
347 token = match_token(p, tokens, args);
348 switch (token) {
349 case Opt_type_old:
350 ufs_clear_opt (*mount_options, UFSTYPE);
351 ufs_set_opt (*mount_options, UFSTYPE_OLD);
352 break;
353 case Opt_type_sunx86:
354 ufs_clear_opt (*mount_options, UFSTYPE);
355 ufs_set_opt (*mount_options, UFSTYPE_SUNx86);
356 break;
357 case Opt_type_sun:
358 ufs_clear_opt (*mount_options, UFSTYPE);
359 ufs_set_opt (*mount_options, UFSTYPE_SUN);
360 break;
Mark Fortescue252e2112007-10-16 23:26:31 -0700361 case Opt_type_sunos:
362 ufs_clear_opt(*mount_options, UFSTYPE);
363 ufs_set_opt(*mount_options, UFSTYPE_SUNOS);
364 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 case Opt_type_44bsd:
366 ufs_clear_opt (*mount_options, UFSTYPE);
367 ufs_set_opt (*mount_options, UFSTYPE_44BSD);
368 break;
369 case Opt_type_ufs2:
370 ufs_clear_opt(*mount_options, UFSTYPE);
371 ufs_set_opt(*mount_options, UFSTYPE_UFS2);
372 break;
373 case Opt_type_hp:
374 ufs_clear_opt (*mount_options, UFSTYPE);
375 ufs_set_opt (*mount_options, UFSTYPE_HP);
376 break;
377 case Opt_type_nextstepcd:
378 ufs_clear_opt (*mount_options, UFSTYPE);
379 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP_CD);
380 break;
381 case Opt_type_nextstep:
382 ufs_clear_opt (*mount_options, UFSTYPE);
383 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP);
384 break;
385 case Opt_type_openstep:
386 ufs_clear_opt (*mount_options, UFSTYPE);
387 ufs_set_opt (*mount_options, UFSTYPE_OPENSTEP);
388 break;
389 case Opt_onerror_panic:
390 ufs_clear_opt (*mount_options, ONERROR);
391 ufs_set_opt (*mount_options, ONERROR_PANIC);
392 break;
393 case Opt_onerror_lock:
394 ufs_clear_opt (*mount_options, ONERROR);
395 ufs_set_opt (*mount_options, ONERROR_LOCK);
396 break;
397 case Opt_onerror_umount:
398 ufs_clear_opt (*mount_options, ONERROR);
399 ufs_set_opt (*mount_options, ONERROR_UMOUNT);
400 break;
401 case Opt_onerror_repair:
402 printk("UFS-fs: Unable to do repair on error, "
403 "will lock lock instead\n");
404 ufs_clear_opt (*mount_options, ONERROR);
405 ufs_set_opt (*mount_options, ONERROR_REPAIR);
406 break;
407 default:
408 printk("UFS-fs: Invalid option: \"%s\" "
409 "or missing value\n", p);
410 return 0;
411 }
412 }
413 return 1;
414}
415
416/*
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700417 * Diffrent types of UFS hold fs_cstotal in different
418 * places, and use diffrent data structure for it.
419 * To make things simplier we just copy fs_cstotal to ufs_sb_private_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 */
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700421static void ufs_setup_cstotal(struct super_block *sb)
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800422{
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700423 struct ufs_sb_info *sbi = UFS_SB(sb);
424 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700425 struct ufs_super_block_first *usb1;
426 struct ufs_super_block_second *usb2;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700427 struct ufs_super_block_third *usb3;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700428 unsigned mtype = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
429
430 UFSD("ENTER, mtype=%u\n", mtype);
431 usb1 = ubh_get_usb_first(uspi);
432 usb2 = ubh_get_usb_second(uspi);
433 usb3 = ubh_get_usb_third(uspi);
434
435 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
436 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
437 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
438 /*we have statistic in different place, then usual*/
439 uspi->cs_total.cs_ndir = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir);
440 uspi->cs_total.cs_nbfree = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree);
441 uspi->cs_total.cs_nifree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree);
442 uspi->cs_total.cs_nffree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree);
443 } else {
444 uspi->cs_total.cs_ndir = fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir);
445 uspi->cs_total.cs_nbfree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree);
446 uspi->cs_total.cs_nifree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree);
447 uspi->cs_total.cs_nffree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree);
448 }
449 UFSD("EXIT\n");
450}
451
452/*
453 * Read on-disk structures associated with cylinder groups
454 */
455static int ufs_read_cylinder_structures(struct super_block *sb)
456{
457 struct ufs_sb_info *sbi = UFS_SB(sb);
458 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 struct ufs_buffer_head * ubh;
460 unsigned char * base, * space;
461 unsigned size, blks, i;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700462 struct ufs_super_block_third *usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700464 UFSD("ENTER\n");
465
466 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /*
468 * Read cs structures from (usually) first data block
469 * on the device.
470 */
471 size = uspi->s_cssize;
472 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
473 base = space = kmalloc(size, GFP_KERNEL);
474 if (!base)
475 goto failed;
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800476 sbi->s_csp = (struct ufs_csum *)space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 for (i = 0; i < blks; i += uspi->s_fpb) {
478 size = uspi->s_bsize;
479 if (i + uspi->s_fpb > blks)
480 size = (blks - i) * uspi->s_fsize;
481
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800482 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
Evgeniy7b4ee732006-01-14 11:42:06 +0300483
484 if (!ubh)
485 goto failed;
486
487 ubh_ubhcpymem (space, ubh, size);
Evgeniy7b4ee732006-01-14 11:42:06 +0300488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 space += size;
490 ubh_brelse (ubh);
491 ubh = NULL;
492 }
493
494 /*
495 * Read cylinder group (we read only first fragment from block
496 * at this time) and prepare internal data structures for cg caching.
497 */
498 if (!(sbi->s_ucg = kmalloc (sizeof(struct buffer_head *) * uspi->s_ncg, GFP_KERNEL)))
499 goto failed;
500 for (i = 0; i < uspi->s_ncg; i++)
501 sbi->s_ucg[i] = NULL;
502 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
503 sbi->s_ucpi[i] = NULL;
504 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
505 }
506 for (i = 0; i < uspi->s_ncg; i++) {
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700507 UFSD("read cg %u\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (!(sbi->s_ucg[i] = sb_bread(sb, ufs_cgcmin(i))))
509 goto failed;
510 if (!ufs_cg_chkmagic (sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data))
511 goto failed;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 ufs_print_cylinder_stuff(sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
516 if (!(sbi->s_ucpi[i] = kmalloc (sizeof(struct ufs_cg_private_info), GFP_KERNEL)))
517 goto failed;
518 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
519 }
520 sbi->s_cg_loaded = 0;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700521 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return 1;
523
524failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800525 kfree (base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (sbi->s_ucg) {
527 for (i = 0; i < uspi->s_ncg; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800528 if (sbi->s_ucg[i])
529 brelse (sbi->s_ucg[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 kfree (sbi->s_ucg);
531 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800532 kfree (sbi->s_ucpi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700534 UFSD("EXIT (FAILED)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return 0;
536}
537
538/*
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700539 * Sync our internal copy of fs_cstotal with disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700541static void ufs_put_cstotal(struct super_block *sb)
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800542{
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700543 unsigned mtype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
544 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
545 struct ufs_super_block_first *usb1;
546 struct ufs_super_block_second *usb2;
547 struct ufs_super_block_third *usb3;
548
549 UFSD("ENTER\n");
550 usb1 = ubh_get_usb_first(uspi);
551 usb2 = ubh_get_usb_second(uspi);
552 usb3 = ubh_get_usb_third(uspi);
553
554 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
555 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
556 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
557 /*we have statistic in different place, then usual*/
558 usb2->fs_un.fs_u2.cs_ndir =
559 cpu_to_fs64(sb, uspi->cs_total.cs_ndir);
560 usb2->fs_un.fs_u2.cs_nbfree =
561 cpu_to_fs64(sb, uspi->cs_total.cs_nbfree);
562 usb3->fs_un1.fs_u2.cs_nifree =
563 cpu_to_fs64(sb, uspi->cs_total.cs_nifree);
564 usb3->fs_un1.fs_u2.cs_nffree =
565 cpu_to_fs64(sb, uspi->cs_total.cs_nffree);
566 } else {
567 usb1->fs_cstotal.cs_ndir =
568 cpu_to_fs32(sb, uspi->cs_total.cs_ndir);
569 usb1->fs_cstotal.cs_nbfree =
570 cpu_to_fs32(sb, uspi->cs_total.cs_nbfree);
571 usb1->fs_cstotal.cs_nifree =
572 cpu_to_fs32(sb, uspi->cs_total.cs_nifree);
573 usb1->fs_cstotal.cs_nffree =
574 cpu_to_fs32(sb, uspi->cs_total.cs_nffree);
575 }
576 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800577 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700578 UFSD("EXIT\n");
579}
580
581/**
582 * ufs_put_super_internal() - put on-disk intrenal structures
583 * @sb: pointer to super_block structure
584 * Put on-disk structures associated with cylinder groups
585 * and write them back to disk, also update cs_total on disk
586 */
587static void ufs_put_super_internal(struct super_block *sb)
588{
589 struct ufs_sb_info *sbi = UFS_SB(sb);
590 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 struct ufs_buffer_head * ubh;
592 unsigned char * base, * space;
593 unsigned blks, size, i;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700596 UFSD("ENTER\n");
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200597
598 lock_kernel();
599
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700600 ufs_put_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 size = uspi->s_cssize;
602 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800603 base = space = (char*) sbi->s_csp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 for (i = 0; i < blks; i += uspi->s_fpb) {
605 size = uspi->s_bsize;
606 if (i + uspi->s_fpb > blks)
607 size = (blks - i) * uspi->s_fsize;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 ubh_memcpyubh (ubh, space, size);
612 space += size;
613 ubh_mark_buffer_uptodate (ubh, 1);
614 ubh_mark_buffer_dirty (ubh);
615 ubh_brelse (ubh);
616 }
617 for (i = 0; i < sbi->s_cg_loaded; i++) {
618 ufs_put_cylinder (sb, i);
619 kfree (sbi->s_ucpi[i]);
620 }
621 for (; i < UFS_MAX_GROUP_LOADED; i++)
622 kfree (sbi->s_ucpi[i]);
623 for (i = 0; i < uspi->s_ncg; i++)
624 brelse (sbi->s_ucg[i]);
625 kfree (sbi->s_ucg);
626 kfree (base);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200627
628 unlock_kernel();
629
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700630 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633static int ufs_fill_super(struct super_block *sb, void *data, int silent)
634{
635 struct ufs_sb_info * sbi;
636 struct ufs_sb_private_info * uspi;
637 struct ufs_super_block_first * usb1;
638 struct ufs_super_block_second * usb2;
639 struct ufs_super_block_third * usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct ufs_buffer_head * ubh;
641 struct inode *inode;
642 unsigned block_size, super_block_size;
643 unsigned flags;
Evgeniy7b4ee732006-01-14 11:42:06 +0300644 unsigned super_block_offset;
Duane Griffin9e6766c2009-01-08 22:43:48 +0000645 unsigned maxsymlen;
David Howellsb55c4602008-02-07 00:15:48 -0800646 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 uspi = NULL;
649 ubh = NULL;
650 flags = 0;
651
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700652 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700654 sbi = kzalloc(sizeof(struct ufs_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (!sbi)
656 goto failed_nomem;
657 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700659 UFSD("flag %u\n", (int)(sb->s_flags & MS_RDONLY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661#ifndef CONFIG_UFS_FS_WRITE
662 if (!(sb->s_flags & MS_RDONLY)) {
663 printk("ufs was compiled with read-only support, "
664 "can't be mounted as read-write\n");
665 goto failed;
666 }
667#endif
668 /*
669 * Set default mount options
670 * Parse mount options
671 */
672 sbi->s_mount_opt = 0;
673 ufs_set_opt (sbi->s_mount_opt, ONERROR_LOCK);
674 if (!ufs_parse_options ((char *) data, &sbi->s_mount_opt)) {
675 printk("wrong mount options\n");
676 goto failed;
677 }
678 if (!(sbi->s_mount_opt & UFS_MOUNT_UFSTYPE)) {
679 if (!silent)
680 printk("You didn't specify the type of your ufs filesystem\n\n"
681 "mount -t ufs -o ufstype="
Michael Owenc690a722006-03-24 18:21:44 +0100682 "sun|sunx86|44bsd|ufs2|5xbsd|old|hp|nextstep|nextstep-cd|openstep ...\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 ">>>WARNING<<< Wrong ufstype may corrupt your filesystem, "
684 "default is ufstype=old\n");
685 ufs_set_opt (sbi->s_mount_opt, UFSTYPE_OLD);
686 }
687
Mark Fortescue252e2112007-10-16 23:26:31 -0700688 uspi = kzalloc(sizeof(struct ufs_sb_private_info), GFP_KERNEL);
689 sbi->s_uspi = uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (!uspi)
691 goto failed;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800692 uspi->s_dirblksize = UFS_SECTOR_SIZE;
Evgeniy7b4ee732006-01-14 11:42:06 +0300693 super_block_offset=UFS_SBLOCK;
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* Keep 2Gig file limit. Some UFS variants need to override
696 this but as I don't know which I'll let those in the know loosen
697 the rules */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 switch (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) {
699 case UFS_MOUNT_UFSTYPE_44BSD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700700 UFSD("ufstype=44bsd\n");
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_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
707 break;
708 case UFS_MOUNT_UFSTYPE_UFS2:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700709 UFSD("ufstype=ufs2\n");
Evgeniy7b4ee732006-01-14 11:42:06 +0300710 super_block_offset=SBLOCK_UFS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 uspi->s_fsize = block_size = 512;
712 uspi->s_fmask = ~(512 - 1);
713 uspi->s_fshift = 9;
714 uspi->s_sbsize = super_block_size = 1536;
715 uspi->s_sbbase = 0;
716 flags |= UFS_TYPE_UFS2 | UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 break;
718
719 case UFS_MOUNT_UFSTYPE_SUN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700720 UFSD("ufstype=sun\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 uspi->s_fsize = block_size = 1024;
722 uspi->s_fmask = ~(1024 - 1);
723 uspi->s_fshift = 10;
724 uspi->s_sbsize = super_block_size = 2048;
725 uspi->s_sbbase = 0;
Mark Fortescue252e2112007-10-16 23:26:31 -0700726 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUN | UFS_CG_SUN;
728 break;
729
Mark Fortescue252e2112007-10-16 23:26:31 -0700730 case UFS_MOUNT_UFSTYPE_SUNOS:
731 UFSD(("ufstype=sunos\n"))
732 uspi->s_fsize = block_size = 1024;
733 uspi->s_fmask = ~(1024 - 1);
734 uspi->s_fshift = 10;
735 uspi->s_sbsize = 2048;
736 super_block_size = 2048;
737 uspi->s_sbbase = 0;
738 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
739 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_SUNOS | UFS_CG_SUN;
740 break;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 case UFS_MOUNT_UFSTYPE_SUNx86:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700743 UFSD("ufstype=sunx86\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 uspi->s_fsize = block_size = 1024;
745 uspi->s_fmask = ~(1024 - 1);
746 uspi->s_fshift = 10;
747 uspi->s_sbsize = super_block_size = 2048;
748 uspi->s_sbbase = 0;
Mark Fortescue252e2112007-10-16 23:26:31 -0700749 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUNx86 | UFS_CG_SUN;
751 break;
752
753 case UFS_MOUNT_UFSTYPE_OLD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700754 UFSD("ufstype=old\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 uspi->s_fsize = block_size = 1024;
756 uspi->s_fmask = ~(1024 - 1);
757 uspi->s_fshift = 10;
758 uspi->s_sbsize = super_block_size = 2048;
759 uspi->s_sbbase = 0;
760 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
761 if (!(sb->s_flags & MS_RDONLY)) {
762 if (!silent)
763 printk(KERN_INFO "ufstype=old is supported read-only\n");
764 sb->s_flags |= MS_RDONLY;
765 }
766 break;
767
768 case UFS_MOUNT_UFSTYPE_NEXTSTEP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700769 UFSD("ufstype=nextstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 uspi->s_fsize = block_size = 1024;
771 uspi->s_fmask = ~(1024 - 1);
772 uspi->s_fshift = 10;
773 uspi->s_sbsize = super_block_size = 2048;
774 uspi->s_sbbase = 0;
Evgeniy Dushistov0c664f92007-12-04 23:45:06 -0800775 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
777 if (!(sb->s_flags & MS_RDONLY)) {
778 if (!silent)
779 printk(KERN_INFO "ufstype=nextstep is supported read-only\n");
780 sb->s_flags |= MS_RDONLY;
781 }
782 break;
783
784 case UFS_MOUNT_UFSTYPE_NEXTSTEP_CD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700785 UFSD("ufstype=nextstep-cd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 uspi->s_fsize = block_size = 2048;
787 uspi->s_fmask = ~(2048 - 1);
788 uspi->s_fshift = 11;
789 uspi->s_sbsize = super_block_size = 2048;
790 uspi->s_sbbase = 0;
Evgeniy Dushistov0c664f92007-12-04 23:45:06 -0800791 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
793 if (!(sb->s_flags & MS_RDONLY)) {
794 if (!silent)
795 printk(KERN_INFO "ufstype=nextstep-cd is supported read-only\n");
796 sb->s_flags |= MS_RDONLY;
797 }
798 break;
799
800 case UFS_MOUNT_UFSTYPE_OPENSTEP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700801 UFSD("ufstype=openstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 uspi->s_fsize = block_size = 1024;
803 uspi->s_fmask = ~(1024 - 1);
804 uspi->s_fshift = 10;
805 uspi->s_sbsize = super_block_size = 2048;
806 uspi->s_sbbase = 0;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800807 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
809 if (!(sb->s_flags & MS_RDONLY)) {
810 if (!silent)
811 printk(KERN_INFO "ufstype=openstep is supported read-only\n");
812 sb->s_flags |= MS_RDONLY;
813 }
814 break;
815
816 case UFS_MOUNT_UFSTYPE_HP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700817 UFSD("ufstype=hp\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 uspi->s_fsize = block_size = 1024;
819 uspi->s_fmask = ~(1024 - 1);
820 uspi->s_fshift = 10;
821 uspi->s_sbsize = super_block_size = 2048;
822 uspi->s_sbbase = 0;
823 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
824 if (!(sb->s_flags & MS_RDONLY)) {
825 if (!silent)
826 printk(KERN_INFO "ufstype=hp is supported read-only\n");
827 sb->s_flags |= MS_RDONLY;
828 }
829 break;
830 default:
831 if (!silent)
832 printk("unknown ufstype\n");
833 goto failed;
834 }
835
836again:
837 if (!sb_set_blocksize(sb, block_size)) {
838 printk(KERN_ERR "UFS: failed to set blocksize\n");
839 goto failed;
840 }
841
842 /*
843 * read ufs super block from device
844 */
Evgeniy7b4ee732006-01-14 11:42:06 +0300845
846 ubh = ubh_bread_uspi(uspi, sb, uspi->s_sbbase + super_block_offset/block_size, super_block_size);
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (!ubh)
849 goto failed;
850
Evgeniy7b4ee732006-01-14 11:42:06 +0300851 usb1 = ubh_get_usb_first(uspi);
852 usb2 = ubh_get_usb_second(uspi);
853 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Mark Fortescue252e2112007-10-16 23:26:31 -0700855 /* Sort out mod used on SunOS 4.1.3 for fs_state */
856 uspi->s_postblformat = fs32_to_cpu(sb, usb3->fs_postblformat);
857 if (((flags & UFS_ST_MASK) == UFS_ST_SUNOS) &&
858 (uspi->s_postblformat != UFS_42POSTBLFMT)) {
859 flags &= ~UFS_ST_MASK;
860 flags |= UFS_ST_SUN;
861 }
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /*
864 * Check ufs magic number
865 */
866 sbi->s_bytesex = BYTESEX_LE;
867 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
868 case UFS_MAGIC:
869 case UFS2_MAGIC:
870 case UFS_MAGIC_LFN:
871 case UFS_MAGIC_FEA:
872 case UFS_MAGIC_4GB:
873 goto magic_found;
874 }
875 sbi->s_bytesex = BYTESEX_BE;
876 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
877 case UFS_MAGIC:
878 case UFS2_MAGIC:
879 case UFS_MAGIC_LFN:
880 case UFS_MAGIC_FEA:
881 case UFS_MAGIC_4GB:
882 goto magic_found;
883 }
884
885 if ((((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP)
886 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP_CD)
887 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_OPENSTEP))
888 && uspi->s_sbbase < 256) {
889 ubh_brelse_uspi(uspi);
890 ubh = NULL;
891 uspi->s_sbbase += 8;
892 goto again;
893 }
894 if (!silent)
895 printk("ufs_read_super: bad magic number\n");
896 goto failed;
897
898magic_found:
899 /*
900 * Check block and fragment sizes
901 */
902 uspi->s_bsize = fs32_to_cpu(sb, usb1->fs_bsize);
903 uspi->s_fsize = fs32_to_cpu(sb, usb1->fs_fsize);
904 uspi->s_sbsize = fs32_to_cpu(sb, usb1->fs_sbsize);
905 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
906 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
907
vignesh babu9e8c4272007-07-15 23:41:36 -0700908 if (!is_power_of_2(uspi->s_fsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 printk(KERN_ERR "ufs_read_super: fragment size %u is not a power of 2\n",
910 uspi->s_fsize);
911 goto failed;
912 }
913 if (uspi->s_fsize < 512) {
914 printk(KERN_ERR "ufs_read_super: fragment size %u is too small\n",
915 uspi->s_fsize);
916 goto failed;
917 }
918 if (uspi->s_fsize > 4096) {
919 printk(KERN_ERR "ufs_read_super: fragment size %u is too large\n",
920 uspi->s_fsize);
921 goto failed;
922 }
vignesh babu9e8c4272007-07-15 23:41:36 -0700923 if (!is_power_of_2(uspi->s_bsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 printk(KERN_ERR "ufs_read_super: block size %u is not a power of 2\n",
925 uspi->s_bsize);
926 goto failed;
927 }
928 if (uspi->s_bsize < 4096) {
929 printk(KERN_ERR "ufs_read_super: block size %u is too small\n",
930 uspi->s_bsize);
931 goto failed;
932 }
933 if (uspi->s_bsize / uspi->s_fsize > 8) {
934 printk(KERN_ERR "ufs_read_super: too many fragments per block (%u)\n",
935 uspi->s_bsize / uspi->s_fsize);
936 goto failed;
937 }
938 if (uspi->s_fsize != block_size || uspi->s_sbsize != super_block_size) {
939 ubh_brelse_uspi(uspi);
940 ubh = NULL;
941 block_size = uspi->s_fsize;
942 super_block_size = uspi->s_sbsize;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700943 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 -0700944 goto again;
945 }
946
Andrew Mortonf664f1f2007-10-29 14:37:21 -0700947 sbi->s_flags = flags;/*after that line some functions use s_flags*/
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800948 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 /*
951 * Check, if file system was correctly unmounted.
952 * If not, make it read only.
953 */
Andrew Mortonf664f1f2007-10-29 14:37:21 -0700954 if (((flags & UFS_ST_MASK) == UFS_ST_44BSD) ||
955 ((flags & UFS_ST_MASK) == UFS_ST_OLD) ||
956 (((flags & UFS_ST_MASK) == UFS_ST_SUN ||
957 (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
958 (flags & UFS_ST_MASK) == UFS_ST_SUNx86) &&
959 (ufs_get_fs_state(sb, usb1, usb3) == (UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 switch(usb1->fs_clean) {
961 case UFS_FSCLEAN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700962 UFSD("fs is clean\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 break;
964 case UFS_FSSTABLE:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700965 UFSD("fs is stable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 break;
967 case UFS_FSOSF1:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700968 UFSD("fs is DEC OSF/1\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 break;
970 case UFS_FSACTIVE:
971 printk("ufs_read_super: fs is active\n");
972 sb->s_flags |= MS_RDONLY;
973 break;
974 case UFS_FSBAD:
975 printk("ufs_read_super: fs is bad\n");
976 sb->s_flags |= MS_RDONLY;
977 break;
978 default:
979 printk("ufs_read_super: can't grok fs_clean 0x%x\n", usb1->fs_clean);
980 sb->s_flags |= MS_RDONLY;
981 break;
982 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700983 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 printk("ufs_read_super: fs needs fsck\n");
985 sb->s_flags |= MS_RDONLY;
986 }
987
988 /*
989 * Read ufs_super_block into internal data structures
990 */
991 sb->s_op = &ufs_super_ops;
992 sb->dq_op = NULL; /***/
993 sb->s_magic = fs32_to_cpu(sb, usb3->fs_magic);
994
995 uspi->s_sblkno = fs32_to_cpu(sb, usb1->fs_sblkno);
996 uspi->s_cblkno = fs32_to_cpu(sb, usb1->fs_cblkno);
997 uspi->s_iblkno = fs32_to_cpu(sb, usb1->fs_iblkno);
998 uspi->s_dblkno = fs32_to_cpu(sb, usb1->fs_dblkno);
999 uspi->s_cgoffset = fs32_to_cpu(sb, usb1->fs_cgoffset);
1000 uspi->s_cgmask = fs32_to_cpu(sb, usb1->fs_cgmask);
1001
1002 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001003 uspi->s_u2_size = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size);
1004 uspi->s_u2_dsize = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
1005 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 uspi->s_size = fs32_to_cpu(sb, usb1->fs_size);
1007 uspi->s_dsize = fs32_to_cpu(sb, usb1->fs_dsize);
1008 }
1009
1010 uspi->s_ncg = fs32_to_cpu(sb, usb1->fs_ncg);
1011 /* s_bsize already set */
1012 /* s_fsize already set */
1013 uspi->s_fpb = fs32_to_cpu(sb, usb1->fs_frag);
1014 uspi->s_minfree = fs32_to_cpu(sb, usb1->fs_minfree);
1015 uspi->s_bmask = fs32_to_cpu(sb, usb1->fs_bmask);
1016 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
1017 uspi->s_bshift = fs32_to_cpu(sb, usb1->fs_bshift);
1018 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001019 UFSD("uspi->s_bshift = %d,uspi->s_fshift = %d", uspi->s_bshift,
1020 uspi->s_fshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 uspi->s_fpbshift = fs32_to_cpu(sb, usb1->fs_fragshift);
1022 uspi->s_fsbtodb = fs32_to_cpu(sb, usb1->fs_fsbtodb);
1023 /* s_sbsize already set */
1024 uspi->s_csmask = fs32_to_cpu(sb, usb1->fs_csmask);
1025 uspi->s_csshift = fs32_to_cpu(sb, usb1->fs_csshift);
1026 uspi->s_nindir = fs32_to_cpu(sb, usb1->fs_nindir);
1027 uspi->s_inopb = fs32_to_cpu(sb, usb1->fs_inopb);
1028 uspi->s_nspf = fs32_to_cpu(sb, usb1->fs_nspf);
1029 uspi->s_npsect = ufs_get_fs_npsect(sb, usb1, usb3);
1030 uspi->s_interleave = fs32_to_cpu(sb, usb1->fs_interleave);
1031 uspi->s_trackskew = fs32_to_cpu(sb, usb1->fs_trackskew);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -08001032
1033 if (uspi->fs_magic == UFS2_MAGIC)
1034 uspi->s_csaddr = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_csaddr);
1035 else
1036 uspi->s_csaddr = fs32_to_cpu(sb, usb1->fs_csaddr);
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 uspi->s_cssize = fs32_to_cpu(sb, usb1->fs_cssize);
1039 uspi->s_cgsize = fs32_to_cpu(sb, usb1->fs_cgsize);
1040 uspi->s_ntrak = fs32_to_cpu(sb, usb1->fs_ntrak);
1041 uspi->s_nsect = fs32_to_cpu(sb, usb1->fs_nsect);
1042 uspi->s_spc = fs32_to_cpu(sb, usb1->fs_spc);
1043 uspi->s_ipg = fs32_to_cpu(sb, usb1->fs_ipg);
1044 uspi->s_fpg = fs32_to_cpu(sb, usb1->fs_fpg);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001045 uspi->s_cpc = fs32_to_cpu(sb, usb2->fs_un.fs_u1.fs_cpc);
1046 uspi->s_contigsumsize = fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_contigsumsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 uspi->s_qbmask = ufs_get_fs_qbmask(sb, usb3);
1048 uspi->s_qfmask = ufs_get_fs_qfmask(sb, usb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 uspi->s_nrpos = fs32_to_cpu(sb, usb3->fs_nrpos);
1050 uspi->s_postbloff = fs32_to_cpu(sb, usb3->fs_postbloff);
1051 uspi->s_rotbloff = fs32_to_cpu(sb, usb3->fs_rotbloff);
1052
1053 /*
1054 * Compute another frequently used values
1055 */
1056 uspi->s_fpbmask = uspi->s_fpb - 1;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001057 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 uspi->s_apbshift = uspi->s_bshift - 3;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001059 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 uspi->s_apbshift = uspi->s_bshift - 2;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 uspi->s_2apbshift = uspi->s_apbshift * 2;
1063 uspi->s_3apbshift = uspi->s_apbshift * 3;
1064 uspi->s_apb = 1 << uspi->s_apbshift;
1065 uspi->s_2apb = 1 << uspi->s_2apbshift;
1066 uspi->s_3apb = 1 << uspi->s_3apbshift;
1067 uspi->s_apbmask = uspi->s_apb - 1;
1068 uspi->s_nspfshift = uspi->s_fshift - UFS_SECTOR_BITS;
1069 uspi->s_nspb = uspi->s_nspf << uspi->s_fpbshift;
1070 uspi->s_inopf = uspi->s_inopb >> uspi->s_fpbshift;
1071 uspi->s_bpf = uspi->s_fsize << 3;
1072 uspi->s_bpfshift = uspi->s_fshift + 3;
1073 uspi->s_bpfmask = uspi->s_bpf - 1;
Evgeniy Dushistov90b315a2008-02-08 04:20:16 -08001074 if ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_44BSD ||
1075 (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 uspi->s_maxsymlinklen =
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001077 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Duane Griffin9e6766c2009-01-08 22:43:48 +00001079 if (uspi->fs_magic == UFS2_MAGIC)
1080 maxsymlen = 2 * 4 * (UFS_NDADDR + UFS_NINDIR);
1081 else
1082 maxsymlen = 4 * (UFS_NDADDR + UFS_NINDIR);
1083 if (uspi->s_maxsymlinklen > maxsymlen) {
1084 ufs_warning(sb, __func__, "ufs_read_super: excessive maximum "
1085 "fast symlink size (%u)\n", uspi->s_maxsymlinklen);
1086 uspi->s_maxsymlinklen = maxsymlen;
1087 }
1088
David Howellsb55c4602008-02-07 00:15:48 -08001089 inode = ufs_iget(sb, UFS_ROOTINO);
1090 if (IS_ERR(inode)) {
1091 ret = PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 goto failed;
David Howellsb55c4602008-02-07 00:15:48 -08001093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 sb->s_root = d_alloc_root(inode);
David Howellsb55c4602008-02-07 00:15:48 -08001095 if (!sb->s_root) {
1096 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 goto dalloc_failed;
David Howellsb55c4602008-02-07 00:15:48 -08001098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001100 ufs_setup_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /*
1102 * Read cylinder group structures
1103 */
1104 if (!(sb->s_flags & MS_RDONLY))
1105 if (!ufs_read_cylinder_structures(sb))
1106 goto failed;
1107
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001108 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return 0;
1110
1111dalloc_failed:
1112 iput(inode);
1113failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -08001114 if (ubh)
1115 ubh_brelse_uspi (uspi);
1116 kfree (uspi);
1117 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 sb->s_fs_info = NULL;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001119 UFSD("EXIT (FAILED)\n");
David Howellsb55c4602008-02-07 00:15:48 -08001120 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122failed_nomem:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001123 UFSD("EXIT (NOMEM)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return -ENOMEM;
1125}
1126
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001127static void ufs_write_super(struct super_block *sb)
1128{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 struct ufs_sb_private_info * uspi;
1130 struct ufs_super_block_first * usb1;
1131 struct ufs_super_block_third * usb3;
1132 unsigned flags;
1133
1134 lock_kernel();
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001135 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 flags = UFS_SB(sb)->s_flags;
1137 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +03001138 usb1 = ubh_get_usb_first(uspi);
1139 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 if (!(sb->s_flags & MS_RDONLY)) {
1142 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1143 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
Mark Fortescue252e2112007-10-16 23:26:31 -07001144 || (flags & UFS_ST_MASK) == UFS_ST_SUNOS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1146 ufs_set_fs_state(sb, usb1, usb3,
1147 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001148 ufs_put_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150 sb->s_dirt = 0;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001151 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 unlock_kernel();
1153}
1154
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001155static void ufs_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 struct ufs_sb_info * sbi = UFS_SB(sb);
1158
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001159 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Christoph Hellwig8c85e122009-04-28 18:00:26 +02001161 if (sb->s_dirt)
1162 ufs_write_super(sb);
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (!(sb->s_flags & MS_RDONLY))
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001165 ufs_put_super_internal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 ubh_brelse_uspi (sbi->s_uspi);
1168 kfree (sbi->s_uspi);
1169 kfree (sbi);
1170 sb->s_fs_info = NULL;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001171 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return;
1173}
1174
1175
1176static int ufs_remount (struct super_block *sb, int *mount_flags, char *data)
1177{
1178 struct ufs_sb_private_info * uspi;
1179 struct ufs_super_block_first * usb1;
1180 struct ufs_super_block_third * usb3;
1181 unsigned new_mount_opt, ufstype;
1182 unsigned flags;
1183
Al Virobbd68512009-05-06 10:43:07 -04001184 lock_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 uspi = UFS_SB(sb)->s_uspi;
1186 flags = UFS_SB(sb)->s_flags;
Evgeniy7b4ee732006-01-14 11:42:06 +03001187 usb1 = ubh_get_usb_first(uspi);
1188 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /*
1191 * Allow the "check" option to be passed as a remount option.
1192 * It is not possible to change ufstype option during remount
1193 */
1194 ufstype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
1195 new_mount_opt = 0;
1196 ufs_set_opt (new_mount_opt, ONERROR_LOCK);
Al Virobbd68512009-05-06 10:43:07 -04001197 if (!ufs_parse_options (data, &new_mount_opt)) {
1198 unlock_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return -EINVAL;
Al Virobbd68512009-05-06 10:43:07 -04001200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (!(new_mount_opt & UFS_MOUNT_UFSTYPE)) {
1202 new_mount_opt |= ufstype;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001203 } else if ((new_mount_opt & UFS_MOUNT_UFSTYPE) != ufstype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 printk("ufstype can't be changed during remount\n");
Al Virobbd68512009-05-06 10:43:07 -04001205 unlock_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return -EINVAL;
1207 }
1208
1209 if ((*mount_flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
1210 UFS_SB(sb)->s_mount_opt = new_mount_opt;
Al Virobbd68512009-05-06 10:43:07 -04001211 unlock_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return 0;
1213 }
1214
1215 /*
1216 * fs was mouted as rw, remounting ro
1217 */
1218 if (*mount_flags & MS_RDONLY) {
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001219 ufs_put_super_internal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1221 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
Mark Fortescue252e2112007-10-16 23:26:31 -07001222 || (flags & UFS_ST_MASK) == UFS_ST_SUNOS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1224 ufs_set_fs_state(sb, usb1, usb3,
1225 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -07001226 ubh_mark_buffer_dirty (USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 sb->s_dirt = 0;
1228 sb->s_flags |= MS_RDONLY;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001229 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 /*
1231 * fs was mounted as ro, remounting rw
1232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233#ifndef CONFIG_UFS_FS_WRITE
1234 printk("ufs was compiled with read-only support, "
1235 "can't be mounted as read-write\n");
Al Virobbd68512009-05-06 10:43:07 -04001236 unlock_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return -EINVAL;
1238#else
1239 if (ufstype != UFS_MOUNT_UFSTYPE_SUN &&
Mark Fortescue252e2112007-10-16 23:26:31 -07001240 ufstype != UFS_MOUNT_UFSTYPE_SUNOS &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 ufstype != UFS_MOUNT_UFSTYPE_44BSD &&
Evgeniy Dushistovcbcae392007-02-12 00:54:30 -08001242 ufstype != UFS_MOUNT_UFSTYPE_SUNx86 &&
1243 ufstype != UFS_MOUNT_UFSTYPE_UFS2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 printk("this ufstype is read-only supported\n");
Al Virobbd68512009-05-06 10:43:07 -04001245 unlock_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return -EINVAL;
1247 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001248 if (!ufs_read_cylinder_structures(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 printk("failed during remounting\n");
Al Virobbd68512009-05-06 10:43:07 -04001250 unlock_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return -EPERM;
1252 }
1253 sb->s_flags &= ~MS_RDONLY;
1254#endif
1255 }
1256 UFS_SB(sb)->s_mount_opt = new_mount_opt;
Al Virobbd68512009-05-06 10:43:07 -04001257 unlock_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return 0;
1259}
1260
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001261static int ufs_show_options(struct seq_file *seq, struct vfsmount *vfs)
1262{
1263 struct ufs_sb_info *sbi = UFS_SB(vfs->mnt_sb);
1264 unsigned mval = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
Steven Whitehousea447c092008-10-13 10:46:57 +01001265 const struct match_token *tp = tokens;
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001266
1267 while (tp->token != Opt_onerror_panic && tp->token != mval)
1268 ++tp;
1269 BUG_ON(tp->token == Opt_onerror_panic);
1270 seq_printf(seq, ",%s", tp->pattern);
1271
1272 mval = sbi->s_mount_opt & UFS_MOUNT_ONERROR;
1273 while (tp->token != Opt_err && tp->token != mval)
1274 ++tp;
1275 BUG_ON(tp->token == Opt_err);
1276 seq_printf(seq, ",%s", tp->pattern);
1277
1278 return 0;
1279}
1280
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001281static int ufs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
David Howells726c3342006-06-23 02:02:58 -07001283 struct super_block *sb = dentry->d_sb;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001284 struct ufs_sb_private_info *uspi= UFS_SB(sb)->s_uspi;
1285 unsigned flags = UFS_SB(sb)->s_flags;
1286 struct ufs_super_block_first *usb1;
1287 struct ufs_super_block_second *usb2;
1288 struct ufs_super_block_third *usb3;
Coly Li41d577a2009-04-02 16:59:44 -07001289 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 lock_kernel();
1292
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001293 usb1 = ubh_get_usb_first(uspi);
1294 usb2 = ubh_get_usb_second(uspi);
1295 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
1298 buf->f_type = UFS2_MAGIC;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001299 buf->f_blocks = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001300 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 buf->f_type = UFS_MAGIC;
1302 buf->f_blocks = uspi->s_dsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001304 buf->f_bfree = ufs_blkstofrags(uspi->cs_total.cs_nbfree) +
1305 uspi->cs_total.cs_nffree;
1306 buf->f_ffree = uspi->cs_total.cs_nifree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 buf->f_bsize = sb->s_blocksize;
1308 buf->f_bavail = (buf->f_bfree > (((long)buf->f_blocks / 100) * uspi->s_minfree))
1309 ? (buf->f_bfree - (((long)buf->f_blocks / 100) * uspi->s_minfree)) : 0;
1310 buf->f_files = uspi->s_ncg * uspi->s_ipg;
1311 buf->f_namelen = UFS_MAXNAMLEN;
Coly Li41d577a2009-04-02 16:59:44 -07001312 buf->f_fsid.val[0] = (u32)id;
1313 buf->f_fsid.val[1] = (u32)(id >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 unlock_kernel();
1316
1317 return 0;
1318}
1319
Christoph Lametere18b8902006-12-06 20:33:20 -08001320static struct kmem_cache * ufs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322static struct inode *ufs_alloc_inode(struct super_block *sb)
1323{
1324 struct ufs_inode_info *ei;
Christoph Lametere94b1762006-12-06 20:33:17 -08001325 ei = (struct ufs_inode_info *)kmem_cache_alloc(ufs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (!ei)
1327 return NULL;
1328 ei->vfs_inode.i_version = 1;
1329 return &ei->vfs_inode;
1330}
1331
1332static void ufs_destroy_inode(struct inode *inode)
1333{
1334 kmem_cache_free(ufs_inode_cachep, UFS_I(inode));
1335}
1336
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001337static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 struct ufs_inode_info *ei = (struct ufs_inode_info *) foo;
1340
Christoph Lametera35afb82007-05-16 22:10:57 -07001341 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
Paul Mundt20c2df82007-07-20 10:11:58 +09001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344static int init_inodecache(void)
1345{
1346 ufs_inode_cachep = kmem_cache_create("ufs_inode_cache",
1347 sizeof(struct ufs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001348 0, (SLAB_RECLAIM_ACCOUNT|
1349 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001350 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (ufs_inode_cachep == NULL)
1352 return -ENOMEM;
1353 return 0;
1354}
1355
1356static void destroy_inodecache(void)
1357{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001358 kmem_cache_destroy(ufs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
1361#ifdef CONFIG_QUOTA
1362static ssize_t ufs_quota_read(struct super_block *, int, char *,size_t, loff_t);
1363static ssize_t ufs_quota_write(struct super_block *, int, const char *, size_t, loff_t);
1364#endif
1365
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001366static const struct super_operations ufs_super_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 .alloc_inode = ufs_alloc_inode,
1368 .destroy_inode = ufs_destroy_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 .write_inode = ufs_write_inode,
1370 .delete_inode = ufs_delete_inode,
1371 .put_super = ufs_put_super,
1372 .write_super = ufs_write_super,
1373 .statfs = ufs_statfs,
1374 .remount_fs = ufs_remount,
Evgeniy Dushistov2a9807c2007-10-16 23:26:32 -07001375 .show_options = ufs_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376#ifdef CONFIG_QUOTA
1377 .quota_read = ufs_quota_read,
1378 .quota_write = ufs_quota_write,
1379#endif
1380};
1381
1382#ifdef CONFIG_QUOTA
1383
1384/* Read data from quotafile - avoid pagecache and such because we cannot afford
1385 * acquiring the locks... As quota files are never truncated and quota code
1386 * itself serializes the operations (and noone else should touch the files)
1387 * we don't have to be afraid of races */
1388static ssize_t ufs_quota_read(struct super_block *sb, int type, char *data,
1389 size_t len, loff_t off)
1390{
1391 struct inode *inode = sb_dqopt(sb)->files[type];
1392 sector_t blk = off >> sb->s_blocksize_bits;
1393 int err = 0;
1394 int offset = off & (sb->s_blocksize - 1);
1395 int tocopy;
1396 size_t toread;
1397 struct buffer_head *bh;
1398 loff_t i_size = i_size_read(inode);
1399
1400 if (off > i_size)
1401 return 0;
1402 if (off+len > i_size)
1403 len = i_size-off;
1404 toread = len;
1405 while (toread > 0) {
1406 tocopy = sb->s_blocksize - offset < toread ?
1407 sb->s_blocksize - offset : toread;
1408
1409 bh = ufs_bread(inode, blk, 0, &err);
1410 if (err)
1411 return err;
1412 if (!bh) /* A hole? */
1413 memset(data, 0, tocopy);
1414 else {
1415 memcpy(data, bh->b_data+offset, tocopy);
1416 brelse(bh);
1417 }
1418 offset = 0;
1419 toread -= tocopy;
1420 data += tocopy;
1421 blk++;
1422 }
1423 return len;
1424}
1425
1426/* Write to quotafile */
1427static ssize_t ufs_quota_write(struct super_block *sb, int type,
1428 const char *data, size_t len, loff_t off)
1429{
1430 struct inode *inode = sb_dqopt(sb)->files[type];
1431 sector_t blk = off >> sb->s_blocksize_bits;
1432 int err = 0;
1433 int offset = off & (sb->s_blocksize - 1);
1434 int tocopy;
1435 size_t towrite = len;
1436 struct buffer_head *bh;
1437
Arjan van de Ven5c81a412006-07-03 00:25:20 -07001438 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 while (towrite > 0) {
1440 tocopy = sb->s_blocksize - offset < towrite ?
1441 sb->s_blocksize - offset : towrite;
1442
1443 bh = ufs_bread(inode, blk, 1, &err);
1444 if (!bh)
1445 goto out;
1446 lock_buffer(bh);
1447 memcpy(bh->b_data+offset, data, tocopy);
1448 flush_dcache_page(bh->b_page);
1449 set_buffer_uptodate(bh);
1450 mark_buffer_dirty(bh);
1451 unlock_buffer(bh);
1452 brelse(bh);
1453 offset = 0;
1454 towrite -= tocopy;
1455 data += tocopy;
1456 blk++;
1457 }
1458out:
Evgeniy Polyakov15b2fe32006-01-08 01:04:51 -08001459 if (len == towrite) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001460 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return err;
Evgeniy Polyakov15b2fe32006-01-08 01:04:51 -08001462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (inode->i_size < off+len-towrite)
1464 i_size_write(inode, off+len-towrite);
1465 inode->i_version++;
1466 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
1467 mark_inode_dirty(inode);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001468 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return len - towrite;
1470}
1471
1472#endif
1473
David Howells454e2392006-06-23 02:02:57 -07001474static int ufs_get_sb(struct file_system_type *fs_type,
1475 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
David Howells454e2392006-06-23 02:02:57 -07001477 return get_sb_bdev(fs_type, flags, dev_name, data, ufs_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480static struct file_system_type ufs_fs_type = {
1481 .owner = THIS_MODULE,
1482 .name = "ufs",
1483 .get_sb = ufs_get_sb,
1484 .kill_sb = kill_block_super,
1485 .fs_flags = FS_REQUIRES_DEV,
1486};
1487
1488static int __init init_ufs_fs(void)
1489{
1490 int err = init_inodecache();
1491 if (err)
1492 goto out1;
1493 err = register_filesystem(&ufs_fs_type);
1494 if (err)
1495 goto out;
1496 return 0;
1497out:
1498 destroy_inodecache();
1499out1:
1500 return err;
1501}
1502
1503static void __exit exit_ufs_fs(void)
1504{
1505 unregister_filesystem(&ufs_fs_type);
1506 destroy_inodecache();
1507}
1508
1509module_init(init_ufs_fs)
1510module_exit(exit_ufs_fs)
1511MODULE_LICENSE("GPL");