bootmem, x86: Add weak version of reserve_bootmem_generic
[wandboard.git] / fs / ufs / super.c
blob3ec5a9eb6efb3ff17654c01b6d00ae3d6bb4e44e
1 /*
2 * linux/fs/ufs/super.c
4 * Copyright (C) 1998
5 * Daniel Pirkl <daniel.pirkl@email.cz>
6 * Charles University, Faculty of Mathematics and Physics
7 */
9 /* Derived from
11 * linux/fs/ext2/super.c
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)
18 * from
20 * linux/fs/minix/inode.c
22 * Copyright (C) 1991, 1992 Linus Torvalds
24 * Big-endian to little-endian byte-swapping/bitmaps by
25 * David S. Miller (davem@caip.rutgers.edu), 1995
29 * Inspired by
31 * linux/fs/ufs/super.c
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
38 * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
40 * Kernel module support added on 96/04/26 by
41 * Stefan Reinauer <stepan@home.culture.mipt.ru>
43 * Module usage counts added on 96/04/29 by
44 * Gertjan van Wingerde <gwingerde@gmail.com>
46 * Clean swab support on 19970406 by
47 * Francois-Rene Rideau <fare@tunes.org>
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>.
53 * NeXTstep support added on February 5th 1998 by
54 * Niels Kristian Bech Jensen <nkbj@image.dk>.
56 * write support Daniel Pirkl <daniel.pirkl@email.cz> 1998
58 * HP/UX hfs filesystem support added by
59 * Martin K. Petersen <mkp@mkp.net>, August 1999
61 * UFS2 (of FreeBSD 5.x) support added by
62 * Niraj Kumar <niraj17@iitbombay.org>, Jan 2004
64 * UFS2 write support added by
65 * Evgeniy Dushistov <dushistov@mail.ru>, 2007
69 #include <linux/exportfs.h>
70 #include <linux/module.h>
71 #include <linux/bitops.h>
73 #include <stdarg.h>
75 #include <asm/uaccess.h>
76 #include <asm/system.h>
78 #include <linux/errno.h>
79 #include <linux/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>
90 #include <linux/log2.h>
91 #include <linux/mount.h>
92 #include <linux/seq_file.h>
94 #include "ufs_fs.h"
95 #include "ufs.h"
96 #include "swab.h"
97 #include "util.h"
99 static struct inode *ufs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation)
101 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
102 struct inode *inode;
104 if (ino < UFS_ROOTINO || ino > uspi->s_ncg * uspi->s_ipg)
105 return ERR_PTR(-ESTALE);
107 inode = ufs_iget(sb, ino);
108 if (IS_ERR(inode))
109 return ERR_CAST(inode);
110 if (generation && inode->i_generation != generation) {
111 iput(inode);
112 return ERR_PTR(-ESTALE);
114 return inode;
117 static struct dentry *ufs_fh_to_dentry(struct super_block *sb, struct fid *fid,
118 int fh_len, int fh_type)
120 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, ufs_nfs_get_inode);
123 static struct dentry *ufs_fh_to_parent(struct super_block *sb, struct fid *fid,
124 int fh_len, int fh_type)
126 return generic_fh_to_parent(sb, fid, fh_len, fh_type, ufs_nfs_get_inode);
129 static struct dentry *ufs_get_parent(struct dentry *child)
131 struct qstr dot_dot = {
132 .name = "..",
133 .len = 2,
135 ino_t ino;
137 ino = ufs_inode_by_name(child->d_inode, &dot_dot);
138 if (!ino)
139 return ERR_PTR(-ENOENT);
140 return d_obtain_alias(ufs_iget(child->d_inode->i_sb, ino));
143 static const struct export_operations ufs_export_ops = {
144 .fh_to_dentry = ufs_fh_to_dentry,
145 .fh_to_parent = ufs_fh_to_parent,
146 .get_parent = ufs_get_parent,
149 #ifdef CONFIG_UFS_DEBUG
151 * Print contents of ufs_super_block, useful for debugging
153 static void ufs_print_super_stuff(struct super_block *sb,
154 struct ufs_super_block_first *usb1,
155 struct ufs_super_block_second *usb2,
156 struct ufs_super_block_third *usb3)
158 u32 magic = fs32_to_cpu(sb, usb3->fs_magic);
160 printk("ufs_print_super_stuff\n");
161 printk(" magic: 0x%x\n", magic);
162 if (fs32_to_cpu(sb, usb3->fs_magic) == UFS2_MAGIC) {
163 printk(" fs_size: %llu\n", (unsigned long long)
164 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size));
165 printk(" fs_dsize: %llu\n", (unsigned long long)
166 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize));
167 printk(" bsize: %u\n",
168 fs32_to_cpu(sb, usb1->fs_bsize));
169 printk(" fsize: %u\n",
170 fs32_to_cpu(sb, usb1->fs_fsize));
171 printk(" fs_volname: %s\n", usb2->fs_un.fs_u2.fs_volname);
172 printk(" fs_sblockloc: %llu\n", (unsigned long long)
173 fs64_to_cpu(sb, usb2->fs_un.fs_u2.fs_sblockloc));
174 printk(" cs_ndir(No of dirs): %llu\n", (unsigned long long)
175 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir));
176 printk(" cs_nbfree(No of free blocks): %llu\n",
177 (unsigned long long)
178 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree));
179 printk(KERN_INFO" cs_nifree(Num of free inodes): %llu\n",
180 (unsigned long long)
181 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree));
182 printk(KERN_INFO" cs_nffree(Num of free frags): %llu\n",
183 (unsigned long long)
184 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree));
185 printk(KERN_INFO" fs_maxsymlinklen: %u\n",
186 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen));
187 } else {
188 printk(" sblkno: %u\n", fs32_to_cpu(sb, usb1->fs_sblkno));
189 printk(" cblkno: %u\n", fs32_to_cpu(sb, usb1->fs_cblkno));
190 printk(" iblkno: %u\n", fs32_to_cpu(sb, usb1->fs_iblkno));
191 printk(" dblkno: %u\n", fs32_to_cpu(sb, usb1->fs_dblkno));
192 printk(" cgoffset: %u\n",
193 fs32_to_cpu(sb, usb1->fs_cgoffset));
194 printk(" ~cgmask: 0x%x\n",
195 ~fs32_to_cpu(sb, usb1->fs_cgmask));
196 printk(" size: %u\n", fs32_to_cpu(sb, usb1->fs_size));
197 printk(" dsize: %u\n", fs32_to_cpu(sb, usb1->fs_dsize));
198 printk(" ncg: %u\n", fs32_to_cpu(sb, usb1->fs_ncg));
199 printk(" bsize: %u\n", fs32_to_cpu(sb, usb1->fs_bsize));
200 printk(" fsize: %u\n", fs32_to_cpu(sb, usb1->fs_fsize));
201 printk(" frag: %u\n", fs32_to_cpu(sb, usb1->fs_frag));
202 printk(" fragshift: %u\n",
203 fs32_to_cpu(sb, usb1->fs_fragshift));
204 printk(" ~fmask: %u\n", ~fs32_to_cpu(sb, usb1->fs_fmask));
205 printk(" fshift: %u\n", fs32_to_cpu(sb, usb1->fs_fshift));
206 printk(" sbsize: %u\n", fs32_to_cpu(sb, usb1->fs_sbsize));
207 printk(" spc: %u\n", fs32_to_cpu(sb, usb1->fs_spc));
208 printk(" cpg: %u\n", fs32_to_cpu(sb, usb1->fs_cpg));
209 printk(" ipg: %u\n", fs32_to_cpu(sb, usb1->fs_ipg));
210 printk(" fpg: %u\n", fs32_to_cpu(sb, usb1->fs_fpg));
211 printk(" csaddr: %u\n", fs32_to_cpu(sb, usb1->fs_csaddr));
212 printk(" cssize: %u\n", fs32_to_cpu(sb, usb1->fs_cssize));
213 printk(" cgsize: %u\n", fs32_to_cpu(sb, usb1->fs_cgsize));
214 printk(" fstodb: %u\n",
215 fs32_to_cpu(sb, usb1->fs_fsbtodb));
216 printk(" nrpos: %u\n", fs32_to_cpu(sb, usb3->fs_nrpos));
217 printk(" ndir %u\n",
218 fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir));
219 printk(" nifree %u\n",
220 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree));
221 printk(" nbfree %u\n",
222 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree));
223 printk(" nffree %u\n",
224 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree));
226 printk("\n");
230 * Print contents of ufs_cylinder_group, useful for debugging
232 static void ufs_print_cylinder_stuff(struct super_block *sb,
233 struct ufs_cylinder_group *cg)
235 printk("\nufs_print_cylinder_stuff\n");
236 printk("size of ucg: %zu\n", sizeof(struct ufs_cylinder_group));
237 printk(" magic: %x\n", fs32_to_cpu(sb, cg->cg_magic));
238 printk(" time: %u\n", fs32_to_cpu(sb, cg->cg_time));
239 printk(" cgx: %u\n", fs32_to_cpu(sb, cg->cg_cgx));
240 printk(" ncyl: %u\n", fs16_to_cpu(sb, cg->cg_ncyl));
241 printk(" niblk: %u\n", fs16_to_cpu(sb, cg->cg_niblk));
242 printk(" ndblk: %u\n", fs32_to_cpu(sb, cg->cg_ndblk));
243 printk(" cs_ndir: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_ndir));
244 printk(" cs_nbfree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nbfree));
245 printk(" cs_nifree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nifree));
246 printk(" cs_nffree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nffree));
247 printk(" rotor: %u\n", fs32_to_cpu(sb, cg->cg_rotor));
248 printk(" frotor: %u\n", fs32_to_cpu(sb, cg->cg_frotor));
249 printk(" irotor: %u\n", fs32_to_cpu(sb, cg->cg_irotor));
250 printk(" frsum: %u, %u, %u, %u, %u, %u, %u, %u\n",
251 fs32_to_cpu(sb, cg->cg_frsum[0]), fs32_to_cpu(sb, cg->cg_frsum[1]),
252 fs32_to_cpu(sb, cg->cg_frsum[2]), fs32_to_cpu(sb, cg->cg_frsum[3]),
253 fs32_to_cpu(sb, cg->cg_frsum[4]), fs32_to_cpu(sb, cg->cg_frsum[5]),
254 fs32_to_cpu(sb, cg->cg_frsum[6]), fs32_to_cpu(sb, cg->cg_frsum[7]));
255 printk(" btotoff: %u\n", fs32_to_cpu(sb, cg->cg_btotoff));
256 printk(" boff: %u\n", fs32_to_cpu(sb, cg->cg_boff));
257 printk(" iuseoff: %u\n", fs32_to_cpu(sb, cg->cg_iusedoff));
258 printk(" freeoff: %u\n", fs32_to_cpu(sb, cg->cg_freeoff));
259 printk(" nextfreeoff: %u\n", fs32_to_cpu(sb, cg->cg_nextfreeoff));
260 printk(" clustersumoff %u\n",
261 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clustersumoff));
262 printk(" clusteroff %u\n",
263 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clusteroff));
264 printk(" nclusterblks %u\n",
265 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_nclusterblks));
266 printk("\n");
268 #else
269 # define ufs_print_super_stuff(sb, usb1, usb2, usb3) /**/
270 # define ufs_print_cylinder_stuff(sb, cg) /**/
271 #endif /* CONFIG_UFS_DEBUG */
273 static const struct super_operations ufs_super_ops;
275 static char error_buf[1024];
277 void ufs_error (struct super_block * sb, const char * function,
278 const char * fmt, ...)
280 struct ufs_sb_private_info * uspi;
281 struct ufs_super_block_first * usb1;
282 va_list args;
284 uspi = UFS_SB(sb)->s_uspi;
285 usb1 = ubh_get_usb_first(uspi);
287 if (!(sb->s_flags & MS_RDONLY)) {
288 usb1->fs_clean = UFS_FSBAD;
289 ubh_mark_buffer_dirty(USPI_UBH(uspi));
290 sb->s_dirt = 1;
291 sb->s_flags |= MS_RDONLY;
293 va_start (args, fmt);
294 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
295 va_end (args);
296 switch (UFS_SB(sb)->s_mount_opt & UFS_MOUNT_ONERROR) {
297 case UFS_MOUNT_ONERROR_PANIC:
298 panic ("UFS-fs panic (device %s): %s: %s\n",
299 sb->s_id, function, error_buf);
301 case UFS_MOUNT_ONERROR_LOCK:
302 case UFS_MOUNT_ONERROR_UMOUNT:
303 case UFS_MOUNT_ONERROR_REPAIR:
304 printk (KERN_CRIT "UFS-fs error (device %s): %s: %s\n",
305 sb->s_id, function, error_buf);
309 void ufs_panic (struct super_block * sb, const char * function,
310 const char * fmt, ...)
312 struct ufs_sb_private_info * uspi;
313 struct ufs_super_block_first * usb1;
314 va_list args;
316 lock_kernel();
317 uspi = UFS_SB(sb)->s_uspi;
318 usb1 = ubh_get_usb_first(uspi);
320 if (!(sb->s_flags & MS_RDONLY)) {
321 usb1->fs_clean = UFS_FSBAD;
322 ubh_mark_buffer_dirty(USPI_UBH(uspi));
323 sb->s_dirt = 1;
325 va_start (args, fmt);
326 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
327 va_end (args);
328 sb->s_flags |= MS_RDONLY;
329 printk (KERN_CRIT "UFS-fs panic (device %s): %s: %s\n",
330 sb->s_id, function, error_buf);
333 void ufs_warning (struct super_block * sb, const char * function,
334 const char * fmt, ...)
336 va_list args;
338 va_start (args, fmt);
339 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
340 va_end (args);
341 printk (KERN_WARNING "UFS-fs warning (device %s): %s: %s\n",
342 sb->s_id, function, error_buf);
345 enum {
346 Opt_type_old = UFS_MOUNT_UFSTYPE_OLD,
347 Opt_type_sunx86 = UFS_MOUNT_UFSTYPE_SUNx86,
348 Opt_type_sun = UFS_MOUNT_UFSTYPE_SUN,
349 Opt_type_sunos = UFS_MOUNT_UFSTYPE_SUNOS,
350 Opt_type_44bsd = UFS_MOUNT_UFSTYPE_44BSD,
351 Opt_type_ufs2 = UFS_MOUNT_UFSTYPE_UFS2,
352 Opt_type_hp = UFS_MOUNT_UFSTYPE_HP,
353 Opt_type_nextstepcd = UFS_MOUNT_UFSTYPE_NEXTSTEP_CD,
354 Opt_type_nextstep = UFS_MOUNT_UFSTYPE_NEXTSTEP,
355 Opt_type_openstep = UFS_MOUNT_UFSTYPE_OPENSTEP,
356 Opt_onerror_panic = UFS_MOUNT_ONERROR_PANIC,
357 Opt_onerror_lock = UFS_MOUNT_ONERROR_LOCK,
358 Opt_onerror_umount = UFS_MOUNT_ONERROR_UMOUNT,
359 Opt_onerror_repair = UFS_MOUNT_ONERROR_REPAIR,
360 Opt_err
363 static const match_table_t tokens = {
364 {Opt_type_old, "ufstype=old"},
365 {Opt_type_sunx86, "ufstype=sunx86"},
366 {Opt_type_sun, "ufstype=sun"},
367 {Opt_type_sunos, "ufstype=sunos"},
368 {Opt_type_44bsd, "ufstype=44bsd"},
369 {Opt_type_ufs2, "ufstype=ufs2"},
370 {Opt_type_ufs2, "ufstype=5xbsd"},
371 {Opt_type_hp, "ufstype=hp"},
372 {Opt_type_nextstepcd, "ufstype=nextstep-cd"},
373 {Opt_type_nextstep, "ufstype=nextstep"},
374 {Opt_type_openstep, "ufstype=openstep"},
375 /*end of possible ufs types */
376 {Opt_onerror_panic, "onerror=panic"},
377 {Opt_onerror_lock, "onerror=lock"},
378 {Opt_onerror_umount, "onerror=umount"},
379 {Opt_onerror_repair, "onerror=repair"},
380 {Opt_err, NULL}
383 static int ufs_parse_options (char * options, unsigned * mount_options)
385 char * p;
387 UFSD("ENTER\n");
389 if (!options)
390 return 1;
392 while ((p = strsep(&options, ",")) != NULL) {
393 substring_t args[MAX_OPT_ARGS];
394 int token;
395 if (!*p)
396 continue;
398 token = match_token(p, tokens, args);
399 switch (token) {
400 case Opt_type_old:
401 ufs_clear_opt (*mount_options, UFSTYPE);
402 ufs_set_opt (*mount_options, UFSTYPE_OLD);
403 break;
404 case Opt_type_sunx86:
405 ufs_clear_opt (*mount_options, UFSTYPE);
406 ufs_set_opt (*mount_options, UFSTYPE_SUNx86);
407 break;
408 case Opt_type_sun:
409 ufs_clear_opt (*mount_options, UFSTYPE);
410 ufs_set_opt (*mount_options, UFSTYPE_SUN);
411 break;
412 case Opt_type_sunos:
413 ufs_clear_opt(*mount_options, UFSTYPE);
414 ufs_set_opt(*mount_options, UFSTYPE_SUNOS);
415 break;
416 case Opt_type_44bsd:
417 ufs_clear_opt (*mount_options, UFSTYPE);
418 ufs_set_opt (*mount_options, UFSTYPE_44BSD);
419 break;
420 case Opt_type_ufs2:
421 ufs_clear_opt(*mount_options, UFSTYPE);
422 ufs_set_opt(*mount_options, UFSTYPE_UFS2);
423 break;
424 case Opt_type_hp:
425 ufs_clear_opt (*mount_options, UFSTYPE);
426 ufs_set_opt (*mount_options, UFSTYPE_HP);
427 break;
428 case Opt_type_nextstepcd:
429 ufs_clear_opt (*mount_options, UFSTYPE);
430 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP_CD);
431 break;
432 case Opt_type_nextstep:
433 ufs_clear_opt (*mount_options, UFSTYPE);
434 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP);
435 break;
436 case Opt_type_openstep:
437 ufs_clear_opt (*mount_options, UFSTYPE);
438 ufs_set_opt (*mount_options, UFSTYPE_OPENSTEP);
439 break;
440 case Opt_onerror_panic:
441 ufs_clear_opt (*mount_options, ONERROR);
442 ufs_set_opt (*mount_options, ONERROR_PANIC);
443 break;
444 case Opt_onerror_lock:
445 ufs_clear_opt (*mount_options, ONERROR);
446 ufs_set_opt (*mount_options, ONERROR_LOCK);
447 break;
448 case Opt_onerror_umount:
449 ufs_clear_opt (*mount_options, ONERROR);
450 ufs_set_opt (*mount_options, ONERROR_UMOUNT);
451 break;
452 case Opt_onerror_repair:
453 printk("UFS-fs: Unable to do repair on error, "
454 "will lock lock instead\n");
455 ufs_clear_opt (*mount_options, ONERROR);
456 ufs_set_opt (*mount_options, ONERROR_REPAIR);
457 break;
458 default:
459 printk("UFS-fs: Invalid option: \"%s\" "
460 "or missing value\n", p);
461 return 0;
464 return 1;
468 * Diffrent types of UFS hold fs_cstotal in different
469 * places, and use diffrent data structure for it.
470 * To make things simplier we just copy fs_cstotal to ufs_sb_private_info
472 static void ufs_setup_cstotal(struct super_block *sb)
474 struct ufs_sb_info *sbi = UFS_SB(sb);
475 struct ufs_sb_private_info *uspi = sbi->s_uspi;
476 struct ufs_super_block_first *usb1;
477 struct ufs_super_block_second *usb2;
478 struct ufs_super_block_third *usb3;
479 unsigned mtype = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
481 UFSD("ENTER, mtype=%u\n", mtype);
482 usb1 = ubh_get_usb_first(uspi);
483 usb2 = ubh_get_usb_second(uspi);
484 usb3 = ubh_get_usb_third(uspi);
486 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
487 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
488 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
489 /*we have statistic in different place, then usual*/
490 uspi->cs_total.cs_ndir = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir);
491 uspi->cs_total.cs_nbfree = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree);
492 uspi->cs_total.cs_nifree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree);
493 uspi->cs_total.cs_nffree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree);
494 } else {
495 uspi->cs_total.cs_ndir = fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir);
496 uspi->cs_total.cs_nbfree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree);
497 uspi->cs_total.cs_nifree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree);
498 uspi->cs_total.cs_nffree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree);
500 UFSD("EXIT\n");
504 * Read on-disk structures associated with cylinder groups
506 static int ufs_read_cylinder_structures(struct super_block *sb)
508 struct ufs_sb_info *sbi = UFS_SB(sb);
509 struct ufs_sb_private_info *uspi = sbi->s_uspi;
510 struct ufs_buffer_head * ubh;
511 unsigned char * base, * space;
512 unsigned size, blks, i;
513 struct ufs_super_block_third *usb3;
515 UFSD("ENTER\n");
517 usb3 = ubh_get_usb_third(uspi);
519 * Read cs structures from (usually) first data block
520 * on the device.
522 size = uspi->s_cssize;
523 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
524 base = space = kmalloc(size, GFP_KERNEL);
525 if (!base)
526 goto failed;
527 sbi->s_csp = (struct ufs_csum *)space;
528 for (i = 0; i < blks; i += uspi->s_fpb) {
529 size = uspi->s_bsize;
530 if (i + uspi->s_fpb > blks)
531 size = (blks - i) * uspi->s_fsize;
533 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
535 if (!ubh)
536 goto failed;
538 ubh_ubhcpymem (space, ubh, size);
540 space += size;
541 ubh_brelse (ubh);
542 ubh = NULL;
546 * Read cylinder group (we read only first fragment from block
547 * at this time) and prepare internal data structures for cg caching.
549 if (!(sbi->s_ucg = kmalloc (sizeof(struct buffer_head *) * uspi->s_ncg, GFP_KERNEL)))
550 goto failed;
551 for (i = 0; i < uspi->s_ncg; i++)
552 sbi->s_ucg[i] = NULL;
553 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
554 sbi->s_ucpi[i] = NULL;
555 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
557 for (i = 0; i < uspi->s_ncg; i++) {
558 UFSD("read cg %u\n", i);
559 if (!(sbi->s_ucg[i] = sb_bread(sb, ufs_cgcmin(i))))
560 goto failed;
561 if (!ufs_cg_chkmagic (sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data))
562 goto failed;
564 ufs_print_cylinder_stuff(sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data);
566 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
567 if (!(sbi->s_ucpi[i] = kmalloc (sizeof(struct ufs_cg_private_info), GFP_KERNEL)))
568 goto failed;
569 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
571 sbi->s_cg_loaded = 0;
572 UFSD("EXIT\n");
573 return 1;
575 failed:
576 kfree (base);
577 if (sbi->s_ucg) {
578 for (i = 0; i < uspi->s_ncg; i++)
579 if (sbi->s_ucg[i])
580 brelse (sbi->s_ucg[i]);
581 kfree (sbi->s_ucg);
582 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++)
583 kfree (sbi->s_ucpi[i]);
585 UFSD("EXIT (FAILED)\n");
586 return 0;
590 * Sync our internal copy of fs_cstotal with disk
592 static void ufs_put_cstotal(struct super_block *sb)
594 unsigned mtype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
595 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
596 struct ufs_super_block_first *usb1;
597 struct ufs_super_block_second *usb2;
598 struct ufs_super_block_third *usb3;
600 UFSD("ENTER\n");
601 usb1 = ubh_get_usb_first(uspi);
602 usb2 = ubh_get_usb_second(uspi);
603 usb3 = ubh_get_usb_third(uspi);
605 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
606 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
607 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
608 /*we have statistic in different place, then usual*/
609 usb2->fs_un.fs_u2.cs_ndir =
610 cpu_to_fs64(sb, uspi->cs_total.cs_ndir);
611 usb2->fs_un.fs_u2.cs_nbfree =
612 cpu_to_fs64(sb, uspi->cs_total.cs_nbfree);
613 usb3->fs_un1.fs_u2.cs_nifree =
614 cpu_to_fs64(sb, uspi->cs_total.cs_nifree);
615 usb3->fs_un1.fs_u2.cs_nffree =
616 cpu_to_fs64(sb, uspi->cs_total.cs_nffree);
617 } else {
618 usb1->fs_cstotal.cs_ndir =
619 cpu_to_fs32(sb, uspi->cs_total.cs_ndir);
620 usb1->fs_cstotal.cs_nbfree =
621 cpu_to_fs32(sb, uspi->cs_total.cs_nbfree);
622 usb1->fs_cstotal.cs_nifree =
623 cpu_to_fs32(sb, uspi->cs_total.cs_nifree);
624 usb1->fs_cstotal.cs_nffree =
625 cpu_to_fs32(sb, uspi->cs_total.cs_nffree);
627 ubh_mark_buffer_dirty(USPI_UBH(uspi));
628 ufs_print_super_stuff(sb, usb1, usb2, usb3);
629 UFSD("EXIT\n");
633 * ufs_put_super_internal() - put on-disk intrenal structures
634 * @sb: pointer to super_block structure
635 * Put on-disk structures associated with cylinder groups
636 * and write them back to disk, also update cs_total on disk
638 static void ufs_put_super_internal(struct super_block *sb)
640 struct ufs_sb_info *sbi = UFS_SB(sb);
641 struct ufs_sb_private_info *uspi = sbi->s_uspi;
642 struct ufs_buffer_head * ubh;
643 unsigned char * base, * space;
644 unsigned blks, size, i;
647 UFSD("ENTER\n");
649 lock_kernel();
651 ufs_put_cstotal(sb);
652 size = uspi->s_cssize;
653 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
654 base = space = (char*) sbi->s_csp;
655 for (i = 0; i < blks; i += uspi->s_fpb) {
656 size = uspi->s_bsize;
657 if (i + uspi->s_fpb > blks)
658 size = (blks - i) * uspi->s_fsize;
660 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
662 ubh_memcpyubh (ubh, space, size);
663 space += size;
664 ubh_mark_buffer_uptodate (ubh, 1);
665 ubh_mark_buffer_dirty (ubh);
666 ubh_brelse (ubh);
668 for (i = 0; i < sbi->s_cg_loaded; i++) {
669 ufs_put_cylinder (sb, i);
670 kfree (sbi->s_ucpi[i]);
672 for (; i < UFS_MAX_GROUP_LOADED; i++)
673 kfree (sbi->s_ucpi[i]);
674 for (i = 0; i < uspi->s_ncg; i++)
675 brelse (sbi->s_ucg[i]);
676 kfree (sbi->s_ucg);
677 kfree (base);
679 unlock_kernel();
681 UFSD("EXIT\n");
684 static int ufs_fill_super(struct super_block *sb, void *data, int silent)
686 struct ufs_sb_info * sbi;
687 struct ufs_sb_private_info * uspi;
688 struct ufs_super_block_first * usb1;
689 struct ufs_super_block_second * usb2;
690 struct ufs_super_block_third * usb3;
691 struct ufs_buffer_head * ubh;
692 struct inode *inode;
693 unsigned block_size, super_block_size;
694 unsigned flags;
695 unsigned super_block_offset;
696 unsigned maxsymlen;
697 int ret = -EINVAL;
699 uspi = NULL;
700 ubh = NULL;
701 flags = 0;
703 UFSD("ENTER\n");
705 sbi = kzalloc(sizeof(struct ufs_sb_info), GFP_KERNEL);
706 if (!sbi)
707 goto failed_nomem;
708 sb->s_fs_info = sbi;
710 UFSD("flag %u\n", (int)(sb->s_flags & MS_RDONLY));
712 #ifndef CONFIG_UFS_FS_WRITE
713 if (!(sb->s_flags & MS_RDONLY)) {
714 printk("ufs was compiled with read-only support, "
715 "can't be mounted as read-write\n");
716 goto failed;
718 #endif
720 * Set default mount options
721 * Parse mount options
723 sbi->s_mount_opt = 0;
724 ufs_set_opt (sbi->s_mount_opt, ONERROR_LOCK);
725 if (!ufs_parse_options ((char *) data, &sbi->s_mount_opt)) {
726 printk("wrong mount options\n");
727 goto failed;
729 if (!(sbi->s_mount_opt & UFS_MOUNT_UFSTYPE)) {
730 if (!silent)
731 printk("You didn't specify the type of your ufs filesystem\n\n"
732 "mount -t ufs -o ufstype="
733 "sun|sunx86|44bsd|ufs2|5xbsd|old|hp|nextstep|nextstep-cd|openstep ...\n\n"
734 ">>>WARNING<<< Wrong ufstype may corrupt your filesystem, "
735 "default is ufstype=old\n");
736 ufs_set_opt (sbi->s_mount_opt, UFSTYPE_OLD);
739 uspi = kzalloc(sizeof(struct ufs_sb_private_info), GFP_KERNEL);
740 sbi->s_uspi = uspi;
741 if (!uspi)
742 goto failed;
743 uspi->s_dirblksize = UFS_SECTOR_SIZE;
744 super_block_offset=UFS_SBLOCK;
746 /* Keep 2Gig file limit. Some UFS variants need to override
747 this but as I don't know which I'll let those in the know loosen
748 the rules */
749 switch (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) {
750 case UFS_MOUNT_UFSTYPE_44BSD:
751 UFSD("ufstype=44bsd\n");
752 uspi->s_fsize = block_size = 512;
753 uspi->s_fmask = ~(512 - 1);
754 uspi->s_fshift = 9;
755 uspi->s_sbsize = super_block_size = 1536;
756 uspi->s_sbbase = 0;
757 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
758 break;
759 case UFS_MOUNT_UFSTYPE_UFS2:
760 UFSD("ufstype=ufs2\n");
761 super_block_offset=SBLOCK_UFS2;
762 uspi->s_fsize = block_size = 512;
763 uspi->s_fmask = ~(512 - 1);
764 uspi->s_fshift = 9;
765 uspi->s_sbsize = super_block_size = 1536;
766 uspi->s_sbbase = 0;
767 flags |= UFS_TYPE_UFS2 | UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
768 break;
770 case UFS_MOUNT_UFSTYPE_SUN:
771 UFSD("ufstype=sun\n");
772 uspi->s_fsize = block_size = 1024;
773 uspi->s_fmask = ~(1024 - 1);
774 uspi->s_fshift = 10;
775 uspi->s_sbsize = super_block_size = 2048;
776 uspi->s_sbbase = 0;
777 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
778 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUN | UFS_CG_SUN;
779 break;
781 case UFS_MOUNT_UFSTYPE_SUNOS:
782 UFSD(("ufstype=sunos\n"))
783 uspi->s_fsize = block_size = 1024;
784 uspi->s_fmask = ~(1024 - 1);
785 uspi->s_fshift = 10;
786 uspi->s_sbsize = 2048;
787 super_block_size = 2048;
788 uspi->s_sbbase = 0;
789 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
790 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_SUNOS | UFS_CG_SUN;
791 break;
793 case UFS_MOUNT_UFSTYPE_SUNx86:
794 UFSD("ufstype=sunx86\n");
795 uspi->s_fsize = block_size = 1024;
796 uspi->s_fmask = ~(1024 - 1);
797 uspi->s_fshift = 10;
798 uspi->s_sbsize = super_block_size = 2048;
799 uspi->s_sbbase = 0;
800 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
801 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUNx86 | UFS_CG_SUN;
802 break;
804 case UFS_MOUNT_UFSTYPE_OLD:
805 UFSD("ufstype=old\n");
806 uspi->s_fsize = block_size = 1024;
807 uspi->s_fmask = ~(1024 - 1);
808 uspi->s_fshift = 10;
809 uspi->s_sbsize = super_block_size = 2048;
810 uspi->s_sbbase = 0;
811 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
812 if (!(sb->s_flags & MS_RDONLY)) {
813 if (!silent)
814 printk(KERN_INFO "ufstype=old is supported read-only\n");
815 sb->s_flags |= MS_RDONLY;
817 break;
819 case UFS_MOUNT_UFSTYPE_NEXTSTEP:
820 UFSD("ufstype=nextstep\n");
821 uspi->s_fsize = block_size = 1024;
822 uspi->s_fmask = ~(1024 - 1);
823 uspi->s_fshift = 10;
824 uspi->s_sbsize = super_block_size = 2048;
825 uspi->s_sbbase = 0;
826 uspi->s_dirblksize = 1024;
827 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
828 if (!(sb->s_flags & MS_RDONLY)) {
829 if (!silent)
830 printk(KERN_INFO "ufstype=nextstep is supported read-only\n");
831 sb->s_flags |= MS_RDONLY;
833 break;
835 case UFS_MOUNT_UFSTYPE_NEXTSTEP_CD:
836 UFSD("ufstype=nextstep-cd\n");
837 uspi->s_fsize = block_size = 2048;
838 uspi->s_fmask = ~(2048 - 1);
839 uspi->s_fshift = 11;
840 uspi->s_sbsize = super_block_size = 2048;
841 uspi->s_sbbase = 0;
842 uspi->s_dirblksize = 1024;
843 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
844 if (!(sb->s_flags & MS_RDONLY)) {
845 if (!silent)
846 printk(KERN_INFO "ufstype=nextstep-cd is supported read-only\n");
847 sb->s_flags |= MS_RDONLY;
849 break;
851 case UFS_MOUNT_UFSTYPE_OPENSTEP:
852 UFSD("ufstype=openstep\n");
853 uspi->s_fsize = block_size = 1024;
854 uspi->s_fmask = ~(1024 - 1);
855 uspi->s_fshift = 10;
856 uspi->s_sbsize = super_block_size = 2048;
857 uspi->s_sbbase = 0;
858 uspi->s_dirblksize = 1024;
859 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
860 if (!(sb->s_flags & MS_RDONLY)) {
861 if (!silent)
862 printk(KERN_INFO "ufstype=openstep is supported read-only\n");
863 sb->s_flags |= MS_RDONLY;
865 break;
867 case UFS_MOUNT_UFSTYPE_HP:
868 UFSD("ufstype=hp\n");
869 uspi->s_fsize = block_size = 1024;
870 uspi->s_fmask = ~(1024 - 1);
871 uspi->s_fshift = 10;
872 uspi->s_sbsize = super_block_size = 2048;
873 uspi->s_sbbase = 0;
874 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
875 if (!(sb->s_flags & MS_RDONLY)) {
876 if (!silent)
877 printk(KERN_INFO "ufstype=hp is supported read-only\n");
878 sb->s_flags |= MS_RDONLY;
880 break;
881 default:
882 if (!silent)
883 printk("unknown ufstype\n");
884 goto failed;
887 again:
888 if (!sb_set_blocksize(sb, block_size)) {
889 printk(KERN_ERR "UFS: failed to set blocksize\n");
890 goto failed;
894 * read ufs super block from device
897 ubh = ubh_bread_uspi(uspi, sb, uspi->s_sbbase + super_block_offset/block_size, super_block_size);
899 if (!ubh)
900 goto failed;
902 usb1 = ubh_get_usb_first(uspi);
903 usb2 = ubh_get_usb_second(uspi);
904 usb3 = ubh_get_usb_third(uspi);
906 /* Sort out mod used on SunOS 4.1.3 for fs_state */
907 uspi->s_postblformat = fs32_to_cpu(sb, usb3->fs_postblformat);
908 if (((flags & UFS_ST_MASK) == UFS_ST_SUNOS) &&
909 (uspi->s_postblformat != UFS_42POSTBLFMT)) {
910 flags &= ~UFS_ST_MASK;
911 flags |= UFS_ST_SUN;
915 * Check ufs magic number
917 sbi->s_bytesex = BYTESEX_LE;
918 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
919 case UFS_MAGIC:
920 case UFS_MAGIC_BW:
921 case UFS2_MAGIC:
922 case UFS_MAGIC_LFN:
923 case UFS_MAGIC_FEA:
924 case UFS_MAGIC_4GB:
925 goto magic_found;
927 sbi->s_bytesex = BYTESEX_BE;
928 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
929 case UFS_MAGIC:
930 case UFS_MAGIC_BW:
931 case UFS2_MAGIC:
932 case UFS_MAGIC_LFN:
933 case UFS_MAGIC_FEA:
934 case UFS_MAGIC_4GB:
935 goto magic_found;
938 if ((((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP)
939 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP_CD)
940 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_OPENSTEP))
941 && uspi->s_sbbase < 256) {
942 ubh_brelse_uspi(uspi);
943 ubh = NULL;
944 uspi->s_sbbase += 8;
945 goto again;
947 if (!silent)
948 printk("ufs_read_super: bad magic number\n");
949 goto failed;
951 magic_found:
953 * Check block and fragment sizes
955 uspi->s_bsize = fs32_to_cpu(sb, usb1->fs_bsize);
956 uspi->s_fsize = fs32_to_cpu(sb, usb1->fs_fsize);
957 uspi->s_sbsize = fs32_to_cpu(sb, usb1->fs_sbsize);
958 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
959 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
961 if (!is_power_of_2(uspi->s_fsize)) {
962 printk(KERN_ERR "ufs_read_super: fragment size %u is not a power of 2\n",
963 uspi->s_fsize);
964 goto failed;
966 if (uspi->s_fsize < 512) {
967 printk(KERN_ERR "ufs_read_super: fragment size %u is too small\n",
968 uspi->s_fsize);
969 goto failed;
971 if (uspi->s_fsize > 4096) {
972 printk(KERN_ERR "ufs_read_super: fragment size %u is too large\n",
973 uspi->s_fsize);
974 goto failed;
976 if (!is_power_of_2(uspi->s_bsize)) {
977 printk(KERN_ERR "ufs_read_super: block size %u is not a power of 2\n",
978 uspi->s_bsize);
979 goto failed;
981 if (uspi->s_bsize < 4096) {
982 printk(KERN_ERR "ufs_read_super: block size %u is too small\n",
983 uspi->s_bsize);
984 goto failed;
986 if (uspi->s_bsize / uspi->s_fsize > 8) {
987 printk(KERN_ERR "ufs_read_super: too many fragments per block (%u)\n",
988 uspi->s_bsize / uspi->s_fsize);
989 goto failed;
991 if (uspi->s_fsize != block_size || uspi->s_sbsize != super_block_size) {
992 ubh_brelse_uspi(uspi);
993 ubh = NULL;
994 block_size = uspi->s_fsize;
995 super_block_size = uspi->s_sbsize;
996 UFSD("another value of block_size or super_block_size %u, %u\n", block_size, super_block_size);
997 goto again;
1000 sbi->s_flags = flags;/*after that line some functions use s_flags*/
1001 ufs_print_super_stuff(sb, usb1, usb2, usb3);
1004 * Check, if file system was correctly unmounted.
1005 * If not, make it read only.
1007 if (((flags & UFS_ST_MASK) == UFS_ST_44BSD) ||
1008 ((flags & UFS_ST_MASK) == UFS_ST_OLD) ||
1009 (((flags & UFS_ST_MASK) == UFS_ST_SUN ||
1010 (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
1011 (flags & UFS_ST_MASK) == UFS_ST_SUNx86) &&
1012 (ufs_get_fs_state(sb, usb1, usb3) == (UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time))))) {
1013 switch(usb1->fs_clean) {
1014 case UFS_FSCLEAN:
1015 UFSD("fs is clean\n");
1016 break;
1017 case UFS_FSSTABLE:
1018 UFSD("fs is stable\n");
1019 break;
1020 case UFS_FSLOG:
1021 UFSD("fs is logging fs\n");
1022 break;
1023 case UFS_FSOSF1:
1024 UFSD("fs is DEC OSF/1\n");
1025 break;
1026 case UFS_FSACTIVE:
1027 printk("ufs_read_super: fs is active\n");
1028 sb->s_flags |= MS_RDONLY;
1029 break;
1030 case UFS_FSBAD:
1031 printk("ufs_read_super: fs is bad\n");
1032 sb->s_flags |= MS_RDONLY;
1033 break;
1034 default:
1035 printk("ufs_read_super: can't grok fs_clean 0x%x\n", usb1->fs_clean);
1036 sb->s_flags |= MS_RDONLY;
1037 break;
1039 } else {
1040 printk("ufs_read_super: fs needs fsck\n");
1041 sb->s_flags |= MS_RDONLY;
1045 * Read ufs_super_block into internal data structures
1047 sb->s_op = &ufs_super_ops;
1048 sb->s_export_op = &ufs_export_ops;
1050 sb->s_magic = fs32_to_cpu(sb, usb3->fs_magic);
1052 uspi->s_sblkno = fs32_to_cpu(sb, usb1->fs_sblkno);
1053 uspi->s_cblkno = fs32_to_cpu(sb, usb1->fs_cblkno);
1054 uspi->s_iblkno = fs32_to_cpu(sb, usb1->fs_iblkno);
1055 uspi->s_dblkno = fs32_to_cpu(sb, usb1->fs_dblkno);
1056 uspi->s_cgoffset = fs32_to_cpu(sb, usb1->fs_cgoffset);
1057 uspi->s_cgmask = fs32_to_cpu(sb, usb1->fs_cgmask);
1059 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
1060 uspi->s_u2_size = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size);
1061 uspi->s_u2_dsize = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
1062 } else {
1063 uspi->s_size = fs32_to_cpu(sb, usb1->fs_size);
1064 uspi->s_dsize = fs32_to_cpu(sb, usb1->fs_dsize);
1067 uspi->s_ncg = fs32_to_cpu(sb, usb1->fs_ncg);
1068 /* s_bsize already set */
1069 /* s_fsize already set */
1070 uspi->s_fpb = fs32_to_cpu(sb, usb1->fs_frag);
1071 uspi->s_minfree = fs32_to_cpu(sb, usb1->fs_minfree);
1072 uspi->s_bmask = fs32_to_cpu(sb, usb1->fs_bmask);
1073 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
1074 uspi->s_bshift = fs32_to_cpu(sb, usb1->fs_bshift);
1075 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
1076 UFSD("uspi->s_bshift = %d,uspi->s_fshift = %d", uspi->s_bshift,
1077 uspi->s_fshift);
1078 uspi->s_fpbshift = fs32_to_cpu(sb, usb1->fs_fragshift);
1079 uspi->s_fsbtodb = fs32_to_cpu(sb, usb1->fs_fsbtodb);
1080 /* s_sbsize already set */
1081 uspi->s_csmask = fs32_to_cpu(sb, usb1->fs_csmask);
1082 uspi->s_csshift = fs32_to_cpu(sb, usb1->fs_csshift);
1083 uspi->s_nindir = fs32_to_cpu(sb, usb1->fs_nindir);
1084 uspi->s_inopb = fs32_to_cpu(sb, usb1->fs_inopb);
1085 uspi->s_nspf = fs32_to_cpu(sb, usb1->fs_nspf);
1086 uspi->s_npsect = ufs_get_fs_npsect(sb, usb1, usb3);
1087 uspi->s_interleave = fs32_to_cpu(sb, usb1->fs_interleave);
1088 uspi->s_trackskew = fs32_to_cpu(sb, usb1->fs_trackskew);
1090 if (uspi->fs_magic == UFS2_MAGIC)
1091 uspi->s_csaddr = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_csaddr);
1092 else
1093 uspi->s_csaddr = fs32_to_cpu(sb, usb1->fs_csaddr);
1095 uspi->s_cssize = fs32_to_cpu(sb, usb1->fs_cssize);
1096 uspi->s_cgsize = fs32_to_cpu(sb, usb1->fs_cgsize);
1097 uspi->s_ntrak = fs32_to_cpu(sb, usb1->fs_ntrak);
1098 uspi->s_nsect = fs32_to_cpu(sb, usb1->fs_nsect);
1099 uspi->s_spc = fs32_to_cpu(sb, usb1->fs_spc);
1100 uspi->s_ipg = fs32_to_cpu(sb, usb1->fs_ipg);
1101 uspi->s_fpg = fs32_to_cpu(sb, usb1->fs_fpg);
1102 uspi->s_cpc = fs32_to_cpu(sb, usb2->fs_un.fs_u1.fs_cpc);
1103 uspi->s_contigsumsize = fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_contigsumsize);
1104 uspi->s_qbmask = ufs_get_fs_qbmask(sb, usb3);
1105 uspi->s_qfmask = ufs_get_fs_qfmask(sb, usb3);
1106 uspi->s_nrpos = fs32_to_cpu(sb, usb3->fs_nrpos);
1107 uspi->s_postbloff = fs32_to_cpu(sb, usb3->fs_postbloff);
1108 uspi->s_rotbloff = fs32_to_cpu(sb, usb3->fs_rotbloff);
1111 * Compute another frequently used values
1113 uspi->s_fpbmask = uspi->s_fpb - 1;
1114 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
1115 uspi->s_apbshift = uspi->s_bshift - 3;
1116 else
1117 uspi->s_apbshift = uspi->s_bshift - 2;
1119 uspi->s_2apbshift = uspi->s_apbshift * 2;
1120 uspi->s_3apbshift = uspi->s_apbshift * 3;
1121 uspi->s_apb = 1 << uspi->s_apbshift;
1122 uspi->s_2apb = 1 << uspi->s_2apbshift;
1123 uspi->s_3apb = 1 << uspi->s_3apbshift;
1124 uspi->s_apbmask = uspi->s_apb - 1;
1125 uspi->s_nspfshift = uspi->s_fshift - UFS_SECTOR_BITS;
1126 uspi->s_nspb = uspi->s_nspf << uspi->s_fpbshift;
1127 uspi->s_inopf = uspi->s_inopb >> uspi->s_fpbshift;
1128 uspi->s_bpf = uspi->s_fsize << 3;
1129 uspi->s_bpfshift = uspi->s_fshift + 3;
1130 uspi->s_bpfmask = uspi->s_bpf - 1;
1131 if ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_44BSD ||
1132 (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_UFS2)
1133 uspi->s_maxsymlinklen =
1134 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen);
1136 if (uspi->fs_magic == UFS2_MAGIC)
1137 maxsymlen = 2 * 4 * (UFS_NDADDR + UFS_NINDIR);
1138 else
1139 maxsymlen = 4 * (UFS_NDADDR + UFS_NINDIR);
1140 if (uspi->s_maxsymlinklen > maxsymlen) {
1141 ufs_warning(sb, __func__, "ufs_read_super: excessive maximum "
1142 "fast symlink size (%u)\n", uspi->s_maxsymlinklen);
1143 uspi->s_maxsymlinklen = maxsymlen;
1146 inode = ufs_iget(sb, UFS_ROOTINO);
1147 if (IS_ERR(inode)) {
1148 ret = PTR_ERR(inode);
1149 goto failed;
1151 sb->s_root = d_alloc_root(inode);
1152 if (!sb->s_root) {
1153 ret = -ENOMEM;
1154 goto dalloc_failed;
1157 ufs_setup_cstotal(sb);
1159 * Read cylinder group structures
1161 if (!(sb->s_flags & MS_RDONLY))
1162 if (!ufs_read_cylinder_structures(sb))
1163 goto failed;
1165 UFSD("EXIT\n");
1166 return 0;
1168 dalloc_failed:
1169 iput(inode);
1170 failed:
1171 if (ubh)
1172 ubh_brelse_uspi (uspi);
1173 kfree (uspi);
1174 kfree(sbi);
1175 sb->s_fs_info = NULL;
1176 UFSD("EXIT (FAILED)\n");
1177 return ret;
1179 failed_nomem:
1180 UFSD("EXIT (NOMEM)\n");
1181 return -ENOMEM;
1184 static int ufs_sync_fs(struct super_block *sb, int wait)
1186 struct ufs_sb_private_info * uspi;
1187 struct ufs_super_block_first * usb1;
1188 struct ufs_super_block_third * usb3;
1189 unsigned flags;
1191 lock_super(sb);
1192 lock_kernel();
1194 UFSD("ENTER\n");
1196 flags = UFS_SB(sb)->s_flags;
1197 uspi = UFS_SB(sb)->s_uspi;
1198 usb1 = ubh_get_usb_first(uspi);
1199 usb3 = ubh_get_usb_third(uspi);
1201 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1202 if ((flags & UFS_ST_MASK) == UFS_ST_SUN ||
1203 (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
1204 (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1205 ufs_set_fs_state(sb, usb1, usb3,
1206 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
1207 ufs_put_cstotal(sb);
1208 sb->s_dirt = 0;
1210 UFSD("EXIT\n");
1211 unlock_kernel();
1212 unlock_super(sb);
1214 return 0;
1217 static void ufs_write_super(struct super_block *sb)
1219 if (!(sb->s_flags & MS_RDONLY))
1220 ufs_sync_fs(sb, 1);
1221 else
1222 sb->s_dirt = 0;
1225 static void ufs_put_super(struct super_block *sb)
1227 struct ufs_sb_info * sbi = UFS_SB(sb);
1229 UFSD("ENTER\n");
1231 if (sb->s_dirt)
1232 ufs_write_super(sb);
1234 if (!(sb->s_flags & MS_RDONLY))
1235 ufs_put_super_internal(sb);
1237 ubh_brelse_uspi (sbi->s_uspi);
1238 kfree (sbi->s_uspi);
1239 kfree (sbi);
1240 sb->s_fs_info = NULL;
1241 UFSD("EXIT\n");
1242 return;
1246 static int ufs_remount (struct super_block *sb, int *mount_flags, char *data)
1248 struct ufs_sb_private_info * uspi;
1249 struct ufs_super_block_first * usb1;
1250 struct ufs_super_block_third * usb3;
1251 unsigned new_mount_opt, ufstype;
1252 unsigned flags;
1254 lock_kernel();
1255 lock_super(sb);
1256 uspi = UFS_SB(sb)->s_uspi;
1257 flags = UFS_SB(sb)->s_flags;
1258 usb1 = ubh_get_usb_first(uspi);
1259 usb3 = ubh_get_usb_third(uspi);
1262 * Allow the "check" option to be passed as a remount option.
1263 * It is not possible to change ufstype option during remount
1265 ufstype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
1266 new_mount_opt = 0;
1267 ufs_set_opt (new_mount_opt, ONERROR_LOCK);
1268 if (!ufs_parse_options (data, &new_mount_opt)) {
1269 unlock_super(sb);
1270 unlock_kernel();
1271 return -EINVAL;
1273 if (!(new_mount_opt & UFS_MOUNT_UFSTYPE)) {
1274 new_mount_opt |= ufstype;
1275 } else if ((new_mount_opt & UFS_MOUNT_UFSTYPE) != ufstype) {
1276 printk("ufstype can't be changed during remount\n");
1277 unlock_super(sb);
1278 unlock_kernel();
1279 return -EINVAL;
1282 if ((*mount_flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
1283 UFS_SB(sb)->s_mount_opt = new_mount_opt;
1284 unlock_super(sb);
1285 unlock_kernel();
1286 return 0;
1290 * fs was mouted as rw, remounting ro
1292 if (*mount_flags & MS_RDONLY) {
1293 ufs_put_super_internal(sb);
1294 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1295 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
1296 || (flags & UFS_ST_MASK) == UFS_ST_SUNOS
1297 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1298 ufs_set_fs_state(sb, usb1, usb3,
1299 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
1300 ubh_mark_buffer_dirty (USPI_UBH(uspi));
1301 sb->s_dirt = 0;
1302 sb->s_flags |= MS_RDONLY;
1303 } else {
1305 * fs was mounted as ro, remounting rw
1307 #ifndef CONFIG_UFS_FS_WRITE
1308 printk("ufs was compiled with read-only support, "
1309 "can't be mounted as read-write\n");
1310 unlock_super(sb);
1311 unlock_kernel();
1312 return -EINVAL;
1313 #else
1314 if (ufstype != UFS_MOUNT_UFSTYPE_SUN &&
1315 ufstype != UFS_MOUNT_UFSTYPE_SUNOS &&
1316 ufstype != UFS_MOUNT_UFSTYPE_44BSD &&
1317 ufstype != UFS_MOUNT_UFSTYPE_SUNx86 &&
1318 ufstype != UFS_MOUNT_UFSTYPE_UFS2) {
1319 printk("this ufstype is read-only supported\n");
1320 unlock_super(sb);
1321 unlock_kernel();
1322 return -EINVAL;
1324 if (!ufs_read_cylinder_structures(sb)) {
1325 printk("failed during remounting\n");
1326 unlock_super(sb);
1327 unlock_kernel();
1328 return -EPERM;
1330 sb->s_flags &= ~MS_RDONLY;
1331 #endif
1333 UFS_SB(sb)->s_mount_opt = new_mount_opt;
1334 unlock_super(sb);
1335 unlock_kernel();
1336 return 0;
1339 static int ufs_show_options(struct seq_file *seq, struct vfsmount *vfs)
1341 struct ufs_sb_info *sbi = UFS_SB(vfs->mnt_sb);
1342 unsigned mval = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
1343 const struct match_token *tp = tokens;
1345 while (tp->token != Opt_onerror_panic && tp->token != mval)
1346 ++tp;
1347 BUG_ON(tp->token == Opt_onerror_panic);
1348 seq_printf(seq, ",%s", tp->pattern);
1350 mval = sbi->s_mount_opt & UFS_MOUNT_ONERROR;
1351 while (tp->token != Opt_err && tp->token != mval)
1352 ++tp;
1353 BUG_ON(tp->token == Opt_err);
1354 seq_printf(seq, ",%s", tp->pattern);
1356 return 0;
1359 static int ufs_statfs(struct dentry *dentry, struct kstatfs *buf)
1361 struct super_block *sb = dentry->d_sb;
1362 struct ufs_sb_private_info *uspi= UFS_SB(sb)->s_uspi;
1363 unsigned flags = UFS_SB(sb)->s_flags;
1364 struct ufs_super_block_first *usb1;
1365 struct ufs_super_block_second *usb2;
1366 struct ufs_super_block_third *usb3;
1367 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1369 lock_kernel();
1371 usb1 = ubh_get_usb_first(uspi);
1372 usb2 = ubh_get_usb_second(uspi);
1373 usb3 = ubh_get_usb_third(uspi);
1375 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
1376 buf->f_type = UFS2_MAGIC;
1377 buf->f_blocks = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
1378 } else {
1379 buf->f_type = UFS_MAGIC;
1380 buf->f_blocks = uspi->s_dsize;
1382 buf->f_bfree = ufs_blkstofrags(uspi->cs_total.cs_nbfree) +
1383 uspi->cs_total.cs_nffree;
1384 buf->f_ffree = uspi->cs_total.cs_nifree;
1385 buf->f_bsize = sb->s_blocksize;
1386 buf->f_bavail = (buf->f_bfree > (((long)buf->f_blocks / 100) * uspi->s_minfree))
1387 ? (buf->f_bfree - (((long)buf->f_blocks / 100) * uspi->s_minfree)) : 0;
1388 buf->f_files = uspi->s_ncg * uspi->s_ipg;
1389 buf->f_namelen = UFS_MAXNAMLEN;
1390 buf->f_fsid.val[0] = (u32)id;
1391 buf->f_fsid.val[1] = (u32)(id >> 32);
1393 unlock_kernel();
1395 return 0;
1398 static struct kmem_cache * ufs_inode_cachep;
1400 static struct inode *ufs_alloc_inode(struct super_block *sb)
1402 struct ufs_inode_info *ei;
1403 ei = (struct ufs_inode_info *)kmem_cache_alloc(ufs_inode_cachep, GFP_KERNEL);
1404 if (!ei)
1405 return NULL;
1406 ei->vfs_inode.i_version = 1;
1407 return &ei->vfs_inode;
1410 static void ufs_destroy_inode(struct inode *inode)
1412 kmem_cache_free(ufs_inode_cachep, UFS_I(inode));
1415 static void init_once(void *foo)
1417 struct ufs_inode_info *ei = (struct ufs_inode_info *) foo;
1419 inode_init_once(&ei->vfs_inode);
1422 static int init_inodecache(void)
1424 ufs_inode_cachep = kmem_cache_create("ufs_inode_cache",
1425 sizeof(struct ufs_inode_info),
1426 0, (SLAB_RECLAIM_ACCOUNT|
1427 SLAB_MEM_SPREAD),
1428 init_once);
1429 if (ufs_inode_cachep == NULL)
1430 return -ENOMEM;
1431 return 0;
1434 static void destroy_inodecache(void)
1436 kmem_cache_destroy(ufs_inode_cachep);
1439 static const struct super_operations ufs_super_ops = {
1440 .alloc_inode = ufs_alloc_inode,
1441 .destroy_inode = ufs_destroy_inode,
1442 .write_inode = ufs_write_inode,
1443 .delete_inode = ufs_delete_inode,
1444 .put_super = ufs_put_super,
1445 .write_super = ufs_write_super,
1446 .sync_fs = ufs_sync_fs,
1447 .statfs = ufs_statfs,
1448 .remount_fs = ufs_remount,
1449 .show_options = ufs_show_options,
1452 static int ufs_get_sb(struct file_system_type *fs_type,
1453 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1455 return get_sb_bdev(fs_type, flags, dev_name, data, ufs_fill_super, mnt);
1458 static struct file_system_type ufs_fs_type = {
1459 .owner = THIS_MODULE,
1460 .name = "ufs",
1461 .get_sb = ufs_get_sb,
1462 .kill_sb = kill_block_super,
1463 .fs_flags = FS_REQUIRES_DEV,
1466 static int __init init_ufs_fs(void)
1468 int err = init_inodecache();
1469 if (err)
1470 goto out1;
1471 err = register_filesystem(&ufs_fs_type);
1472 if (err)
1473 goto out;
1474 return 0;
1475 out:
1476 destroy_inodecache();
1477 out1:
1478 return err;
1481 static void __exit exit_ufs_fs(void)
1483 unregister_filesystem(&ufs_fs_type);
1484 destroy_inodecache();
1487 module_init(init_ufs_fs)
1488 module_exit(exit_ufs_fs)
1489 MODULE_LICENSE("GPL");