spi-topcliff-pch: Fix issue for transmitting over 4KByte
[zen-stable.git] / fs / ufs / super.c
blob5246ee3e56078b358609bc7115fca093543ea946
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/buffer_head.h>
88 #include <linux/vfs.h>
89 #include <linux/log2.h>
90 #include <linux/mount.h>
91 #include <linux/seq_file.h>
93 #include "ufs_fs.h"
94 #include "ufs.h"
95 #include "swab.h"
96 #include "util.h"
98 void lock_ufs(struct super_block *sb)
100 #if defined(CONFIG_SMP) || defined (CONFIG_PREEMPT)
101 struct ufs_sb_info *sbi = UFS_SB(sb);
103 mutex_lock(&sbi->mutex);
104 sbi->mutex_owner = current;
105 #endif
108 void unlock_ufs(struct super_block *sb)
110 #if defined(CONFIG_SMP) || defined (CONFIG_PREEMPT)
111 struct ufs_sb_info *sbi = UFS_SB(sb);
113 sbi->mutex_owner = NULL;
114 mutex_unlock(&sbi->mutex);
115 #endif
118 static struct inode *ufs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation)
120 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
121 struct inode *inode;
123 if (ino < UFS_ROOTINO || ino > uspi->s_ncg * uspi->s_ipg)
124 return ERR_PTR(-ESTALE);
126 inode = ufs_iget(sb, ino);
127 if (IS_ERR(inode))
128 return ERR_CAST(inode);
129 if (generation && inode->i_generation != generation) {
130 iput(inode);
131 return ERR_PTR(-ESTALE);
133 return inode;
136 static struct dentry *ufs_fh_to_dentry(struct super_block *sb, struct fid *fid,
137 int fh_len, int fh_type)
139 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, ufs_nfs_get_inode);
142 static struct dentry *ufs_fh_to_parent(struct super_block *sb, struct fid *fid,
143 int fh_len, int fh_type)
145 return generic_fh_to_parent(sb, fid, fh_len, fh_type, ufs_nfs_get_inode);
148 static struct dentry *ufs_get_parent(struct dentry *child)
150 struct qstr dot_dot = {
151 .name = "..",
152 .len = 2,
154 ino_t ino;
156 ino = ufs_inode_by_name(child->d_inode, &dot_dot);
157 if (!ino)
158 return ERR_PTR(-ENOENT);
159 return d_obtain_alias(ufs_iget(child->d_inode->i_sb, ino));
162 static const struct export_operations ufs_export_ops = {
163 .fh_to_dentry = ufs_fh_to_dentry,
164 .fh_to_parent = ufs_fh_to_parent,
165 .get_parent = ufs_get_parent,
168 #ifdef CONFIG_UFS_DEBUG
170 * Print contents of ufs_super_block, useful for debugging
172 static void ufs_print_super_stuff(struct super_block *sb,
173 struct ufs_super_block_first *usb1,
174 struct ufs_super_block_second *usb2,
175 struct ufs_super_block_third *usb3)
177 u32 magic = fs32_to_cpu(sb, usb3->fs_magic);
179 printk("ufs_print_super_stuff\n");
180 printk(" magic: 0x%x\n", magic);
181 if (fs32_to_cpu(sb, usb3->fs_magic) == UFS2_MAGIC) {
182 printk(" fs_size: %llu\n", (unsigned long long)
183 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size));
184 printk(" fs_dsize: %llu\n", (unsigned long long)
185 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize));
186 printk(" bsize: %u\n",
187 fs32_to_cpu(sb, usb1->fs_bsize));
188 printk(" fsize: %u\n",
189 fs32_to_cpu(sb, usb1->fs_fsize));
190 printk(" fs_volname: %s\n", usb2->fs_un.fs_u2.fs_volname);
191 printk(" fs_sblockloc: %llu\n", (unsigned long long)
192 fs64_to_cpu(sb, usb2->fs_un.fs_u2.fs_sblockloc));
193 printk(" cs_ndir(No of dirs): %llu\n", (unsigned long long)
194 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir));
195 printk(" cs_nbfree(No of free blocks): %llu\n",
196 (unsigned long long)
197 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree));
198 printk(KERN_INFO" cs_nifree(Num of free inodes): %llu\n",
199 (unsigned long long)
200 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree));
201 printk(KERN_INFO" cs_nffree(Num of free frags): %llu\n",
202 (unsigned long long)
203 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree));
204 printk(KERN_INFO" fs_maxsymlinklen: %u\n",
205 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen));
206 } else {
207 printk(" sblkno: %u\n", fs32_to_cpu(sb, usb1->fs_sblkno));
208 printk(" cblkno: %u\n", fs32_to_cpu(sb, usb1->fs_cblkno));
209 printk(" iblkno: %u\n", fs32_to_cpu(sb, usb1->fs_iblkno));
210 printk(" dblkno: %u\n", fs32_to_cpu(sb, usb1->fs_dblkno));
211 printk(" cgoffset: %u\n",
212 fs32_to_cpu(sb, usb1->fs_cgoffset));
213 printk(" ~cgmask: 0x%x\n",
214 ~fs32_to_cpu(sb, usb1->fs_cgmask));
215 printk(" size: %u\n", fs32_to_cpu(sb, usb1->fs_size));
216 printk(" dsize: %u\n", fs32_to_cpu(sb, usb1->fs_dsize));
217 printk(" ncg: %u\n", fs32_to_cpu(sb, usb1->fs_ncg));
218 printk(" bsize: %u\n", fs32_to_cpu(sb, usb1->fs_bsize));
219 printk(" fsize: %u\n", fs32_to_cpu(sb, usb1->fs_fsize));
220 printk(" frag: %u\n", fs32_to_cpu(sb, usb1->fs_frag));
221 printk(" fragshift: %u\n",
222 fs32_to_cpu(sb, usb1->fs_fragshift));
223 printk(" ~fmask: %u\n", ~fs32_to_cpu(sb, usb1->fs_fmask));
224 printk(" fshift: %u\n", fs32_to_cpu(sb, usb1->fs_fshift));
225 printk(" sbsize: %u\n", fs32_to_cpu(sb, usb1->fs_sbsize));
226 printk(" spc: %u\n", fs32_to_cpu(sb, usb1->fs_spc));
227 printk(" cpg: %u\n", fs32_to_cpu(sb, usb1->fs_cpg));
228 printk(" ipg: %u\n", fs32_to_cpu(sb, usb1->fs_ipg));
229 printk(" fpg: %u\n", fs32_to_cpu(sb, usb1->fs_fpg));
230 printk(" csaddr: %u\n", fs32_to_cpu(sb, usb1->fs_csaddr));
231 printk(" cssize: %u\n", fs32_to_cpu(sb, usb1->fs_cssize));
232 printk(" cgsize: %u\n", fs32_to_cpu(sb, usb1->fs_cgsize));
233 printk(" fstodb: %u\n",
234 fs32_to_cpu(sb, usb1->fs_fsbtodb));
235 printk(" nrpos: %u\n", fs32_to_cpu(sb, usb3->fs_nrpos));
236 printk(" ndir %u\n",
237 fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir));
238 printk(" nifree %u\n",
239 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree));
240 printk(" nbfree %u\n",
241 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree));
242 printk(" nffree %u\n",
243 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree));
245 printk("\n");
249 * Print contents of ufs_cylinder_group, useful for debugging
251 static void ufs_print_cylinder_stuff(struct super_block *sb,
252 struct ufs_cylinder_group *cg)
254 printk("\nufs_print_cylinder_stuff\n");
255 printk("size of ucg: %zu\n", sizeof(struct ufs_cylinder_group));
256 printk(" magic: %x\n", fs32_to_cpu(sb, cg->cg_magic));
257 printk(" time: %u\n", fs32_to_cpu(sb, cg->cg_time));
258 printk(" cgx: %u\n", fs32_to_cpu(sb, cg->cg_cgx));
259 printk(" ncyl: %u\n", fs16_to_cpu(sb, cg->cg_ncyl));
260 printk(" niblk: %u\n", fs16_to_cpu(sb, cg->cg_niblk));
261 printk(" ndblk: %u\n", fs32_to_cpu(sb, cg->cg_ndblk));
262 printk(" cs_ndir: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_ndir));
263 printk(" cs_nbfree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nbfree));
264 printk(" cs_nifree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nifree));
265 printk(" cs_nffree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nffree));
266 printk(" rotor: %u\n", fs32_to_cpu(sb, cg->cg_rotor));
267 printk(" frotor: %u\n", fs32_to_cpu(sb, cg->cg_frotor));
268 printk(" irotor: %u\n", fs32_to_cpu(sb, cg->cg_irotor));
269 printk(" frsum: %u, %u, %u, %u, %u, %u, %u, %u\n",
270 fs32_to_cpu(sb, cg->cg_frsum[0]), fs32_to_cpu(sb, cg->cg_frsum[1]),
271 fs32_to_cpu(sb, cg->cg_frsum[2]), fs32_to_cpu(sb, cg->cg_frsum[3]),
272 fs32_to_cpu(sb, cg->cg_frsum[4]), fs32_to_cpu(sb, cg->cg_frsum[5]),
273 fs32_to_cpu(sb, cg->cg_frsum[6]), fs32_to_cpu(sb, cg->cg_frsum[7]));
274 printk(" btotoff: %u\n", fs32_to_cpu(sb, cg->cg_btotoff));
275 printk(" boff: %u\n", fs32_to_cpu(sb, cg->cg_boff));
276 printk(" iuseoff: %u\n", fs32_to_cpu(sb, cg->cg_iusedoff));
277 printk(" freeoff: %u\n", fs32_to_cpu(sb, cg->cg_freeoff));
278 printk(" nextfreeoff: %u\n", fs32_to_cpu(sb, cg->cg_nextfreeoff));
279 printk(" clustersumoff %u\n",
280 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clustersumoff));
281 printk(" clusteroff %u\n",
282 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clusteroff));
283 printk(" nclusterblks %u\n",
284 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_nclusterblks));
285 printk("\n");
287 #else
288 # define ufs_print_super_stuff(sb, usb1, usb2, usb3) /**/
289 # define ufs_print_cylinder_stuff(sb, cg) /**/
290 #endif /* CONFIG_UFS_DEBUG */
292 static const struct super_operations ufs_super_ops;
294 static char error_buf[1024];
296 void ufs_error (struct super_block * sb, const char * function,
297 const char * fmt, ...)
299 struct ufs_sb_private_info * uspi;
300 struct ufs_super_block_first * usb1;
301 va_list args;
303 uspi = UFS_SB(sb)->s_uspi;
304 usb1 = ubh_get_usb_first(uspi);
306 if (!(sb->s_flags & MS_RDONLY)) {
307 usb1->fs_clean = UFS_FSBAD;
308 ubh_mark_buffer_dirty(USPI_UBH(uspi));
309 sb->s_dirt = 1;
310 sb->s_flags |= MS_RDONLY;
312 va_start (args, fmt);
313 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
314 va_end (args);
315 switch (UFS_SB(sb)->s_mount_opt & UFS_MOUNT_ONERROR) {
316 case UFS_MOUNT_ONERROR_PANIC:
317 panic ("UFS-fs panic (device %s): %s: %s\n",
318 sb->s_id, function, error_buf);
320 case UFS_MOUNT_ONERROR_LOCK:
321 case UFS_MOUNT_ONERROR_UMOUNT:
322 case UFS_MOUNT_ONERROR_REPAIR:
323 printk (KERN_CRIT "UFS-fs error (device %s): %s: %s\n",
324 sb->s_id, function, error_buf);
328 void ufs_panic (struct super_block * sb, const char * function,
329 const char * fmt, ...)
331 struct ufs_sb_private_info * uspi;
332 struct ufs_super_block_first * usb1;
333 va_list args;
335 uspi = UFS_SB(sb)->s_uspi;
336 usb1 = ubh_get_usb_first(uspi);
338 if (!(sb->s_flags & MS_RDONLY)) {
339 usb1->fs_clean = UFS_FSBAD;
340 ubh_mark_buffer_dirty(USPI_UBH(uspi));
341 sb->s_dirt = 1;
343 va_start (args, fmt);
344 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
345 va_end (args);
346 sb->s_flags |= MS_RDONLY;
347 printk (KERN_CRIT "UFS-fs panic (device %s): %s: %s\n",
348 sb->s_id, function, error_buf);
351 void ufs_warning (struct super_block * sb, const char * function,
352 const char * fmt, ...)
354 va_list args;
356 va_start (args, fmt);
357 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
358 va_end (args);
359 printk (KERN_WARNING "UFS-fs warning (device %s): %s: %s\n",
360 sb->s_id, function, error_buf);
363 enum {
364 Opt_type_old = UFS_MOUNT_UFSTYPE_OLD,
365 Opt_type_sunx86 = UFS_MOUNT_UFSTYPE_SUNx86,
366 Opt_type_sun = UFS_MOUNT_UFSTYPE_SUN,
367 Opt_type_sunos = UFS_MOUNT_UFSTYPE_SUNOS,
368 Opt_type_44bsd = UFS_MOUNT_UFSTYPE_44BSD,
369 Opt_type_ufs2 = UFS_MOUNT_UFSTYPE_UFS2,
370 Opt_type_hp = UFS_MOUNT_UFSTYPE_HP,
371 Opt_type_nextstepcd = UFS_MOUNT_UFSTYPE_NEXTSTEP_CD,
372 Opt_type_nextstep = UFS_MOUNT_UFSTYPE_NEXTSTEP,
373 Opt_type_openstep = UFS_MOUNT_UFSTYPE_OPENSTEP,
374 Opt_onerror_panic = UFS_MOUNT_ONERROR_PANIC,
375 Opt_onerror_lock = UFS_MOUNT_ONERROR_LOCK,
376 Opt_onerror_umount = UFS_MOUNT_ONERROR_UMOUNT,
377 Opt_onerror_repair = UFS_MOUNT_ONERROR_REPAIR,
378 Opt_err
381 static const match_table_t tokens = {
382 {Opt_type_old, "ufstype=old"},
383 {Opt_type_sunx86, "ufstype=sunx86"},
384 {Opt_type_sun, "ufstype=sun"},
385 {Opt_type_sunos, "ufstype=sunos"},
386 {Opt_type_44bsd, "ufstype=44bsd"},
387 {Opt_type_ufs2, "ufstype=ufs2"},
388 {Opt_type_ufs2, "ufstype=5xbsd"},
389 {Opt_type_hp, "ufstype=hp"},
390 {Opt_type_nextstepcd, "ufstype=nextstep-cd"},
391 {Opt_type_nextstep, "ufstype=nextstep"},
392 {Opt_type_openstep, "ufstype=openstep"},
393 /*end of possible ufs types */
394 {Opt_onerror_panic, "onerror=panic"},
395 {Opt_onerror_lock, "onerror=lock"},
396 {Opt_onerror_umount, "onerror=umount"},
397 {Opt_onerror_repair, "onerror=repair"},
398 {Opt_err, NULL}
401 static int ufs_parse_options (char * options, unsigned * mount_options)
403 char * p;
405 UFSD("ENTER\n");
407 if (!options)
408 return 1;
410 while ((p = strsep(&options, ",")) != NULL) {
411 substring_t args[MAX_OPT_ARGS];
412 int token;
413 if (!*p)
414 continue;
416 token = match_token(p, tokens, args);
417 switch (token) {
418 case Opt_type_old:
419 ufs_clear_opt (*mount_options, UFSTYPE);
420 ufs_set_opt (*mount_options, UFSTYPE_OLD);
421 break;
422 case Opt_type_sunx86:
423 ufs_clear_opt (*mount_options, UFSTYPE);
424 ufs_set_opt (*mount_options, UFSTYPE_SUNx86);
425 break;
426 case Opt_type_sun:
427 ufs_clear_opt (*mount_options, UFSTYPE);
428 ufs_set_opt (*mount_options, UFSTYPE_SUN);
429 break;
430 case Opt_type_sunos:
431 ufs_clear_opt(*mount_options, UFSTYPE);
432 ufs_set_opt(*mount_options, UFSTYPE_SUNOS);
433 break;
434 case Opt_type_44bsd:
435 ufs_clear_opt (*mount_options, UFSTYPE);
436 ufs_set_opt (*mount_options, UFSTYPE_44BSD);
437 break;
438 case Opt_type_ufs2:
439 ufs_clear_opt(*mount_options, UFSTYPE);
440 ufs_set_opt(*mount_options, UFSTYPE_UFS2);
441 break;
442 case Opt_type_hp:
443 ufs_clear_opt (*mount_options, UFSTYPE);
444 ufs_set_opt (*mount_options, UFSTYPE_HP);
445 break;
446 case Opt_type_nextstepcd:
447 ufs_clear_opt (*mount_options, UFSTYPE);
448 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP_CD);
449 break;
450 case Opt_type_nextstep:
451 ufs_clear_opt (*mount_options, UFSTYPE);
452 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP);
453 break;
454 case Opt_type_openstep:
455 ufs_clear_opt (*mount_options, UFSTYPE);
456 ufs_set_opt (*mount_options, UFSTYPE_OPENSTEP);
457 break;
458 case Opt_onerror_panic:
459 ufs_clear_opt (*mount_options, ONERROR);
460 ufs_set_opt (*mount_options, ONERROR_PANIC);
461 break;
462 case Opt_onerror_lock:
463 ufs_clear_opt (*mount_options, ONERROR);
464 ufs_set_opt (*mount_options, ONERROR_LOCK);
465 break;
466 case Opt_onerror_umount:
467 ufs_clear_opt (*mount_options, ONERROR);
468 ufs_set_opt (*mount_options, ONERROR_UMOUNT);
469 break;
470 case Opt_onerror_repair:
471 printk("UFS-fs: Unable to do repair on error, "
472 "will lock lock instead\n");
473 ufs_clear_opt (*mount_options, ONERROR);
474 ufs_set_opt (*mount_options, ONERROR_REPAIR);
475 break;
476 default:
477 printk("UFS-fs: Invalid option: \"%s\" "
478 "or missing value\n", p);
479 return 0;
482 return 1;
486 * Different types of UFS hold fs_cstotal in different
487 * places, and use different data structure for it.
488 * To make things simpler we just copy fs_cstotal to ufs_sb_private_info
490 static void ufs_setup_cstotal(struct super_block *sb)
492 struct ufs_sb_info *sbi = UFS_SB(sb);
493 struct ufs_sb_private_info *uspi = sbi->s_uspi;
494 struct ufs_super_block_first *usb1;
495 struct ufs_super_block_second *usb2;
496 struct ufs_super_block_third *usb3;
497 unsigned mtype = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
499 UFSD("ENTER, mtype=%u\n", mtype);
500 usb1 = ubh_get_usb_first(uspi);
501 usb2 = ubh_get_usb_second(uspi);
502 usb3 = ubh_get_usb_third(uspi);
504 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
505 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
506 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
507 /*we have statistic in different place, then usual*/
508 uspi->cs_total.cs_ndir = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir);
509 uspi->cs_total.cs_nbfree = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree);
510 uspi->cs_total.cs_nifree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree);
511 uspi->cs_total.cs_nffree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree);
512 } else {
513 uspi->cs_total.cs_ndir = fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir);
514 uspi->cs_total.cs_nbfree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree);
515 uspi->cs_total.cs_nifree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree);
516 uspi->cs_total.cs_nffree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree);
518 UFSD("EXIT\n");
522 * Read on-disk structures associated with cylinder groups
524 static int ufs_read_cylinder_structures(struct super_block *sb)
526 struct ufs_sb_info *sbi = UFS_SB(sb);
527 struct ufs_sb_private_info *uspi = sbi->s_uspi;
528 struct ufs_buffer_head * ubh;
529 unsigned char * base, * space;
530 unsigned size, blks, i;
531 struct ufs_super_block_third *usb3;
533 UFSD("ENTER\n");
535 usb3 = ubh_get_usb_third(uspi);
537 * Read cs structures from (usually) first data block
538 * on the device.
540 size = uspi->s_cssize;
541 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
542 base = space = kmalloc(size, GFP_NOFS);
543 if (!base)
544 goto failed;
545 sbi->s_csp = (struct ufs_csum *)space;
546 for (i = 0; i < blks; i += uspi->s_fpb) {
547 size = uspi->s_bsize;
548 if (i + uspi->s_fpb > blks)
549 size = (blks - i) * uspi->s_fsize;
551 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
553 if (!ubh)
554 goto failed;
556 ubh_ubhcpymem (space, ubh, size);
558 space += size;
559 ubh_brelse (ubh);
560 ubh = NULL;
564 * Read cylinder group (we read only first fragment from block
565 * at this time) and prepare internal data structures for cg caching.
567 if (!(sbi->s_ucg = kmalloc (sizeof(struct buffer_head *) * uspi->s_ncg, GFP_NOFS)))
568 goto failed;
569 for (i = 0; i < uspi->s_ncg; i++)
570 sbi->s_ucg[i] = NULL;
571 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
572 sbi->s_ucpi[i] = NULL;
573 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
575 for (i = 0; i < uspi->s_ncg; i++) {
576 UFSD("read cg %u\n", i);
577 if (!(sbi->s_ucg[i] = sb_bread(sb, ufs_cgcmin(i))))
578 goto failed;
579 if (!ufs_cg_chkmagic (sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data))
580 goto failed;
582 ufs_print_cylinder_stuff(sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data);
584 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
585 if (!(sbi->s_ucpi[i] = kmalloc (sizeof(struct ufs_cg_private_info), GFP_NOFS)))
586 goto failed;
587 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
589 sbi->s_cg_loaded = 0;
590 UFSD("EXIT\n");
591 return 1;
593 failed:
594 kfree (base);
595 if (sbi->s_ucg) {
596 for (i = 0; i < uspi->s_ncg; i++)
597 if (sbi->s_ucg[i])
598 brelse (sbi->s_ucg[i]);
599 kfree (sbi->s_ucg);
600 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++)
601 kfree (sbi->s_ucpi[i]);
603 UFSD("EXIT (FAILED)\n");
604 return 0;
608 * Sync our internal copy of fs_cstotal with disk
610 static void ufs_put_cstotal(struct super_block *sb)
612 unsigned mtype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
613 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
614 struct ufs_super_block_first *usb1;
615 struct ufs_super_block_second *usb2;
616 struct ufs_super_block_third *usb3;
618 UFSD("ENTER\n");
619 usb1 = ubh_get_usb_first(uspi);
620 usb2 = ubh_get_usb_second(uspi);
621 usb3 = ubh_get_usb_third(uspi);
623 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
624 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
625 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
626 /*we have statistic in different place, then usual*/
627 usb2->fs_un.fs_u2.cs_ndir =
628 cpu_to_fs64(sb, uspi->cs_total.cs_ndir);
629 usb2->fs_un.fs_u2.cs_nbfree =
630 cpu_to_fs64(sb, uspi->cs_total.cs_nbfree);
631 usb3->fs_un1.fs_u2.cs_nifree =
632 cpu_to_fs64(sb, uspi->cs_total.cs_nifree);
633 usb3->fs_un1.fs_u2.cs_nffree =
634 cpu_to_fs64(sb, uspi->cs_total.cs_nffree);
635 } else {
636 usb1->fs_cstotal.cs_ndir =
637 cpu_to_fs32(sb, uspi->cs_total.cs_ndir);
638 usb1->fs_cstotal.cs_nbfree =
639 cpu_to_fs32(sb, uspi->cs_total.cs_nbfree);
640 usb1->fs_cstotal.cs_nifree =
641 cpu_to_fs32(sb, uspi->cs_total.cs_nifree);
642 usb1->fs_cstotal.cs_nffree =
643 cpu_to_fs32(sb, uspi->cs_total.cs_nffree);
645 ubh_mark_buffer_dirty(USPI_UBH(uspi));
646 ufs_print_super_stuff(sb, usb1, usb2, usb3);
647 UFSD("EXIT\n");
651 * ufs_put_super_internal() - put on-disk intrenal structures
652 * @sb: pointer to super_block structure
653 * Put on-disk structures associated with cylinder groups
654 * and write them back to disk, also update cs_total on disk
656 static void ufs_put_super_internal(struct super_block *sb)
658 struct ufs_sb_info *sbi = UFS_SB(sb);
659 struct ufs_sb_private_info *uspi = sbi->s_uspi;
660 struct ufs_buffer_head * ubh;
661 unsigned char * base, * space;
662 unsigned blks, size, i;
665 UFSD("ENTER\n");
667 ufs_put_cstotal(sb);
668 size = uspi->s_cssize;
669 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
670 base = space = (char*) sbi->s_csp;
671 for (i = 0; i < blks; i += uspi->s_fpb) {
672 size = uspi->s_bsize;
673 if (i + uspi->s_fpb > blks)
674 size = (blks - i) * uspi->s_fsize;
676 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
678 ubh_memcpyubh (ubh, space, size);
679 space += size;
680 ubh_mark_buffer_uptodate (ubh, 1);
681 ubh_mark_buffer_dirty (ubh);
682 ubh_brelse (ubh);
684 for (i = 0; i < sbi->s_cg_loaded; i++) {
685 ufs_put_cylinder (sb, i);
686 kfree (sbi->s_ucpi[i]);
688 for (; i < UFS_MAX_GROUP_LOADED; i++)
689 kfree (sbi->s_ucpi[i]);
690 for (i = 0; i < uspi->s_ncg; i++)
691 brelse (sbi->s_ucg[i]);
692 kfree (sbi->s_ucg);
693 kfree (base);
695 UFSD("EXIT\n");
698 static int ufs_fill_super(struct super_block *sb, void *data, int silent)
700 struct ufs_sb_info * sbi;
701 struct ufs_sb_private_info * uspi;
702 struct ufs_super_block_first * usb1;
703 struct ufs_super_block_second * usb2;
704 struct ufs_super_block_third * usb3;
705 struct ufs_buffer_head * ubh;
706 struct inode *inode;
707 unsigned block_size, super_block_size;
708 unsigned flags;
709 unsigned super_block_offset;
710 unsigned maxsymlen;
711 int ret = -EINVAL;
713 uspi = NULL;
714 ubh = NULL;
715 flags = 0;
717 UFSD("ENTER\n");
719 sbi = kzalloc(sizeof(struct ufs_sb_info), GFP_KERNEL);
720 if (!sbi)
721 goto failed_nomem;
722 sb->s_fs_info = sbi;
724 UFSD("flag %u\n", (int)(sb->s_flags & MS_RDONLY));
726 #ifndef CONFIG_UFS_FS_WRITE
727 if (!(sb->s_flags & MS_RDONLY)) {
728 printk("ufs was compiled with read-only support, "
729 "can't be mounted as read-write\n");
730 goto failed;
732 #endif
733 mutex_init(&sbi->mutex);
735 * Set default mount options
736 * Parse mount options
738 sbi->s_mount_opt = 0;
739 ufs_set_opt (sbi->s_mount_opt, ONERROR_LOCK);
740 if (!ufs_parse_options ((char *) data, &sbi->s_mount_opt)) {
741 printk("wrong mount options\n");
742 goto failed;
744 if (!(sbi->s_mount_opt & UFS_MOUNT_UFSTYPE)) {
745 if (!silent)
746 printk("You didn't specify the type of your ufs filesystem\n\n"
747 "mount -t ufs -o ufstype="
748 "sun|sunx86|44bsd|ufs2|5xbsd|old|hp|nextstep|nextstep-cd|openstep ...\n\n"
749 ">>>WARNING<<< Wrong ufstype may corrupt your filesystem, "
750 "default is ufstype=old\n");
751 ufs_set_opt (sbi->s_mount_opt, UFSTYPE_OLD);
754 uspi = kzalloc(sizeof(struct ufs_sb_private_info), GFP_KERNEL);
755 sbi->s_uspi = uspi;
756 if (!uspi)
757 goto failed;
758 uspi->s_dirblksize = UFS_SECTOR_SIZE;
759 super_block_offset=UFS_SBLOCK;
761 /* Keep 2Gig file limit. Some UFS variants need to override
762 this but as I don't know which I'll let those in the know loosen
763 the rules */
764 switch (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) {
765 case UFS_MOUNT_UFSTYPE_44BSD:
766 UFSD("ufstype=44bsd\n");
767 uspi->s_fsize = block_size = 512;
768 uspi->s_fmask = ~(512 - 1);
769 uspi->s_fshift = 9;
770 uspi->s_sbsize = super_block_size = 1536;
771 uspi->s_sbbase = 0;
772 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
773 break;
774 case UFS_MOUNT_UFSTYPE_UFS2:
775 UFSD("ufstype=ufs2\n");
776 super_block_offset=SBLOCK_UFS2;
777 uspi->s_fsize = block_size = 512;
778 uspi->s_fmask = ~(512 - 1);
779 uspi->s_fshift = 9;
780 uspi->s_sbsize = super_block_size = 1536;
781 uspi->s_sbbase = 0;
782 flags |= UFS_TYPE_UFS2 | UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
783 break;
785 case UFS_MOUNT_UFSTYPE_SUN:
786 UFSD("ufstype=sun\n");
787 uspi->s_fsize = block_size = 1024;
788 uspi->s_fmask = ~(1024 - 1);
789 uspi->s_fshift = 10;
790 uspi->s_sbsize = super_block_size = 2048;
791 uspi->s_sbbase = 0;
792 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
793 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUN | UFS_CG_SUN;
794 break;
796 case UFS_MOUNT_UFSTYPE_SUNOS:
797 UFSD(("ufstype=sunos\n"))
798 uspi->s_fsize = block_size = 1024;
799 uspi->s_fmask = ~(1024 - 1);
800 uspi->s_fshift = 10;
801 uspi->s_sbsize = 2048;
802 super_block_size = 2048;
803 uspi->s_sbbase = 0;
804 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
805 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_SUNOS | UFS_CG_SUN;
806 break;
808 case UFS_MOUNT_UFSTYPE_SUNx86:
809 UFSD("ufstype=sunx86\n");
810 uspi->s_fsize = block_size = 1024;
811 uspi->s_fmask = ~(1024 - 1);
812 uspi->s_fshift = 10;
813 uspi->s_sbsize = super_block_size = 2048;
814 uspi->s_sbbase = 0;
815 uspi->s_maxsymlinklen = 0; /* Not supported on disk */
816 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUNx86 | UFS_CG_SUN;
817 break;
819 case UFS_MOUNT_UFSTYPE_OLD:
820 UFSD("ufstype=old\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 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
827 if (!(sb->s_flags & MS_RDONLY)) {
828 if (!silent)
829 printk(KERN_INFO "ufstype=old is supported read-only\n");
830 sb->s_flags |= MS_RDONLY;
832 break;
834 case UFS_MOUNT_UFSTYPE_NEXTSTEP:
835 UFSD("ufstype=nextstep\n");
836 uspi->s_fsize = block_size = 1024;
837 uspi->s_fmask = ~(1024 - 1);
838 uspi->s_fshift = 10;
839 uspi->s_sbsize = super_block_size = 2048;
840 uspi->s_sbbase = 0;
841 uspi->s_dirblksize = 1024;
842 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
843 if (!(sb->s_flags & MS_RDONLY)) {
844 if (!silent)
845 printk(KERN_INFO "ufstype=nextstep is supported read-only\n");
846 sb->s_flags |= MS_RDONLY;
848 break;
850 case UFS_MOUNT_UFSTYPE_NEXTSTEP_CD:
851 UFSD("ufstype=nextstep-cd\n");
852 uspi->s_fsize = block_size = 2048;
853 uspi->s_fmask = ~(2048 - 1);
854 uspi->s_fshift = 11;
855 uspi->s_sbsize = super_block_size = 2048;
856 uspi->s_sbbase = 0;
857 uspi->s_dirblksize = 1024;
858 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
859 if (!(sb->s_flags & MS_RDONLY)) {
860 if (!silent)
861 printk(KERN_INFO "ufstype=nextstep-cd is supported read-only\n");
862 sb->s_flags |= MS_RDONLY;
864 break;
866 case UFS_MOUNT_UFSTYPE_OPENSTEP:
867 UFSD("ufstype=openstep\n");
868 uspi->s_fsize = block_size = 1024;
869 uspi->s_fmask = ~(1024 - 1);
870 uspi->s_fshift = 10;
871 uspi->s_sbsize = super_block_size = 2048;
872 uspi->s_sbbase = 0;
873 uspi->s_dirblksize = 1024;
874 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
875 if (!(sb->s_flags & MS_RDONLY)) {
876 if (!silent)
877 printk(KERN_INFO "ufstype=openstep is supported read-only\n");
878 sb->s_flags |= MS_RDONLY;
880 break;
882 case UFS_MOUNT_UFSTYPE_HP:
883 UFSD("ufstype=hp\n");
884 uspi->s_fsize = block_size = 1024;
885 uspi->s_fmask = ~(1024 - 1);
886 uspi->s_fshift = 10;
887 uspi->s_sbsize = super_block_size = 2048;
888 uspi->s_sbbase = 0;
889 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
890 if (!(sb->s_flags & MS_RDONLY)) {
891 if (!silent)
892 printk(KERN_INFO "ufstype=hp is supported read-only\n");
893 sb->s_flags |= MS_RDONLY;
895 break;
896 default:
897 if (!silent)
898 printk("unknown ufstype\n");
899 goto failed;
902 again:
903 if (!sb_set_blocksize(sb, block_size)) {
904 printk(KERN_ERR "UFS: failed to set blocksize\n");
905 goto failed;
909 * read ufs super block from device
912 ubh = ubh_bread_uspi(uspi, sb, uspi->s_sbbase + super_block_offset/block_size, super_block_size);
914 if (!ubh)
915 goto failed;
917 usb1 = ubh_get_usb_first(uspi);
918 usb2 = ubh_get_usb_second(uspi);
919 usb3 = ubh_get_usb_third(uspi);
921 /* Sort out mod used on SunOS 4.1.3 for fs_state */
922 uspi->s_postblformat = fs32_to_cpu(sb, usb3->fs_postblformat);
923 if (((flags & UFS_ST_MASK) == UFS_ST_SUNOS) &&
924 (uspi->s_postblformat != UFS_42POSTBLFMT)) {
925 flags &= ~UFS_ST_MASK;
926 flags |= UFS_ST_SUN;
930 * Check ufs magic number
932 sbi->s_bytesex = BYTESEX_LE;
933 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
934 case UFS_MAGIC:
935 case UFS_MAGIC_BW:
936 case UFS2_MAGIC:
937 case UFS_MAGIC_LFN:
938 case UFS_MAGIC_FEA:
939 case UFS_MAGIC_4GB:
940 goto magic_found;
942 sbi->s_bytesex = BYTESEX_BE;
943 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
944 case UFS_MAGIC:
945 case UFS_MAGIC_BW:
946 case UFS2_MAGIC:
947 case UFS_MAGIC_LFN:
948 case UFS_MAGIC_FEA:
949 case UFS_MAGIC_4GB:
950 goto magic_found;
953 if ((((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP)
954 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP_CD)
955 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_OPENSTEP))
956 && uspi->s_sbbase < 256) {
957 ubh_brelse_uspi(uspi);
958 ubh = NULL;
959 uspi->s_sbbase += 8;
960 goto again;
962 if (!silent)
963 printk("ufs_read_super: bad magic number\n");
964 goto failed;
966 magic_found:
968 * Check block and fragment sizes
970 uspi->s_bsize = fs32_to_cpu(sb, usb1->fs_bsize);
971 uspi->s_fsize = fs32_to_cpu(sb, usb1->fs_fsize);
972 uspi->s_sbsize = fs32_to_cpu(sb, usb1->fs_sbsize);
973 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
974 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
976 if (!is_power_of_2(uspi->s_fsize)) {
977 printk(KERN_ERR "ufs_read_super: fragment size %u is not a power of 2\n",
978 uspi->s_fsize);
979 goto failed;
981 if (uspi->s_fsize < 512) {
982 printk(KERN_ERR "ufs_read_super: fragment size %u is too small\n",
983 uspi->s_fsize);
984 goto failed;
986 if (uspi->s_fsize > 4096) {
987 printk(KERN_ERR "ufs_read_super: fragment size %u is too large\n",
988 uspi->s_fsize);
989 goto failed;
991 if (!is_power_of_2(uspi->s_bsize)) {
992 printk(KERN_ERR "ufs_read_super: block size %u is not a power of 2\n",
993 uspi->s_bsize);
994 goto failed;
996 if (uspi->s_bsize < 4096) {
997 printk(KERN_ERR "ufs_read_super: block size %u is too small\n",
998 uspi->s_bsize);
999 goto failed;
1001 if (uspi->s_bsize / uspi->s_fsize > 8) {
1002 printk(KERN_ERR "ufs_read_super: too many fragments per block (%u)\n",
1003 uspi->s_bsize / uspi->s_fsize);
1004 goto failed;
1006 if (uspi->s_fsize != block_size || uspi->s_sbsize != super_block_size) {
1007 ubh_brelse_uspi(uspi);
1008 ubh = NULL;
1009 block_size = uspi->s_fsize;
1010 super_block_size = uspi->s_sbsize;
1011 UFSD("another value of block_size or super_block_size %u, %u\n", block_size, super_block_size);
1012 goto again;
1015 sbi->s_flags = flags;/*after that line some functions use s_flags*/
1016 ufs_print_super_stuff(sb, usb1, usb2, usb3);
1019 * Check, if file system was correctly unmounted.
1020 * If not, make it read only.
1022 if (((flags & UFS_ST_MASK) == UFS_ST_44BSD) ||
1023 ((flags & UFS_ST_MASK) == UFS_ST_OLD) ||
1024 (((flags & UFS_ST_MASK) == UFS_ST_SUN ||
1025 (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
1026 (flags & UFS_ST_MASK) == UFS_ST_SUNx86) &&
1027 (ufs_get_fs_state(sb, usb1, usb3) == (UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time))))) {
1028 switch(usb1->fs_clean) {
1029 case UFS_FSCLEAN:
1030 UFSD("fs is clean\n");
1031 break;
1032 case UFS_FSSTABLE:
1033 UFSD("fs is stable\n");
1034 break;
1035 case UFS_FSLOG:
1036 UFSD("fs is logging fs\n");
1037 break;
1038 case UFS_FSOSF1:
1039 UFSD("fs is DEC OSF/1\n");
1040 break;
1041 case UFS_FSACTIVE:
1042 printk("ufs_read_super: fs is active\n");
1043 sb->s_flags |= MS_RDONLY;
1044 break;
1045 case UFS_FSBAD:
1046 printk("ufs_read_super: fs is bad\n");
1047 sb->s_flags |= MS_RDONLY;
1048 break;
1049 default:
1050 printk("ufs_read_super: can't grok fs_clean 0x%x\n", usb1->fs_clean);
1051 sb->s_flags |= MS_RDONLY;
1052 break;
1054 } else {
1055 printk("ufs_read_super: fs needs fsck\n");
1056 sb->s_flags |= MS_RDONLY;
1060 * Read ufs_super_block into internal data structures
1062 sb->s_op = &ufs_super_ops;
1063 sb->s_export_op = &ufs_export_ops;
1065 sb->s_magic = fs32_to_cpu(sb, usb3->fs_magic);
1067 uspi->s_sblkno = fs32_to_cpu(sb, usb1->fs_sblkno);
1068 uspi->s_cblkno = fs32_to_cpu(sb, usb1->fs_cblkno);
1069 uspi->s_iblkno = fs32_to_cpu(sb, usb1->fs_iblkno);
1070 uspi->s_dblkno = fs32_to_cpu(sb, usb1->fs_dblkno);
1071 uspi->s_cgoffset = fs32_to_cpu(sb, usb1->fs_cgoffset);
1072 uspi->s_cgmask = fs32_to_cpu(sb, usb1->fs_cgmask);
1074 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
1075 uspi->s_u2_size = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size);
1076 uspi->s_u2_dsize = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
1077 } else {
1078 uspi->s_size = fs32_to_cpu(sb, usb1->fs_size);
1079 uspi->s_dsize = fs32_to_cpu(sb, usb1->fs_dsize);
1082 uspi->s_ncg = fs32_to_cpu(sb, usb1->fs_ncg);
1083 /* s_bsize already set */
1084 /* s_fsize already set */
1085 uspi->s_fpb = fs32_to_cpu(sb, usb1->fs_frag);
1086 uspi->s_minfree = fs32_to_cpu(sb, usb1->fs_minfree);
1087 uspi->s_bmask = fs32_to_cpu(sb, usb1->fs_bmask);
1088 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
1089 uspi->s_bshift = fs32_to_cpu(sb, usb1->fs_bshift);
1090 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
1091 UFSD("uspi->s_bshift = %d,uspi->s_fshift = %d", uspi->s_bshift,
1092 uspi->s_fshift);
1093 uspi->s_fpbshift = fs32_to_cpu(sb, usb1->fs_fragshift);
1094 uspi->s_fsbtodb = fs32_to_cpu(sb, usb1->fs_fsbtodb);
1095 /* s_sbsize already set */
1096 uspi->s_csmask = fs32_to_cpu(sb, usb1->fs_csmask);
1097 uspi->s_csshift = fs32_to_cpu(sb, usb1->fs_csshift);
1098 uspi->s_nindir = fs32_to_cpu(sb, usb1->fs_nindir);
1099 uspi->s_inopb = fs32_to_cpu(sb, usb1->fs_inopb);
1100 uspi->s_nspf = fs32_to_cpu(sb, usb1->fs_nspf);
1101 uspi->s_npsect = ufs_get_fs_npsect(sb, usb1, usb3);
1102 uspi->s_interleave = fs32_to_cpu(sb, usb1->fs_interleave);
1103 uspi->s_trackskew = fs32_to_cpu(sb, usb1->fs_trackskew);
1105 if (uspi->fs_magic == UFS2_MAGIC)
1106 uspi->s_csaddr = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_csaddr);
1107 else
1108 uspi->s_csaddr = fs32_to_cpu(sb, usb1->fs_csaddr);
1110 uspi->s_cssize = fs32_to_cpu(sb, usb1->fs_cssize);
1111 uspi->s_cgsize = fs32_to_cpu(sb, usb1->fs_cgsize);
1112 uspi->s_ntrak = fs32_to_cpu(sb, usb1->fs_ntrak);
1113 uspi->s_nsect = fs32_to_cpu(sb, usb1->fs_nsect);
1114 uspi->s_spc = fs32_to_cpu(sb, usb1->fs_spc);
1115 uspi->s_ipg = fs32_to_cpu(sb, usb1->fs_ipg);
1116 uspi->s_fpg = fs32_to_cpu(sb, usb1->fs_fpg);
1117 uspi->s_cpc = fs32_to_cpu(sb, usb2->fs_un.fs_u1.fs_cpc);
1118 uspi->s_contigsumsize = fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_contigsumsize);
1119 uspi->s_qbmask = ufs_get_fs_qbmask(sb, usb3);
1120 uspi->s_qfmask = ufs_get_fs_qfmask(sb, usb3);
1121 uspi->s_nrpos = fs32_to_cpu(sb, usb3->fs_nrpos);
1122 uspi->s_postbloff = fs32_to_cpu(sb, usb3->fs_postbloff);
1123 uspi->s_rotbloff = fs32_to_cpu(sb, usb3->fs_rotbloff);
1126 * Compute another frequently used values
1128 uspi->s_fpbmask = uspi->s_fpb - 1;
1129 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
1130 uspi->s_apbshift = uspi->s_bshift - 3;
1131 else
1132 uspi->s_apbshift = uspi->s_bshift - 2;
1134 uspi->s_2apbshift = uspi->s_apbshift * 2;
1135 uspi->s_3apbshift = uspi->s_apbshift * 3;
1136 uspi->s_apb = 1 << uspi->s_apbshift;
1137 uspi->s_2apb = 1 << uspi->s_2apbshift;
1138 uspi->s_3apb = 1 << uspi->s_3apbshift;
1139 uspi->s_apbmask = uspi->s_apb - 1;
1140 uspi->s_nspfshift = uspi->s_fshift - UFS_SECTOR_BITS;
1141 uspi->s_nspb = uspi->s_nspf << uspi->s_fpbshift;
1142 uspi->s_inopf = uspi->s_inopb >> uspi->s_fpbshift;
1143 uspi->s_bpf = uspi->s_fsize << 3;
1144 uspi->s_bpfshift = uspi->s_fshift + 3;
1145 uspi->s_bpfmask = uspi->s_bpf - 1;
1146 if ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_44BSD ||
1147 (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_UFS2)
1148 uspi->s_maxsymlinklen =
1149 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen);
1151 if (uspi->fs_magic == UFS2_MAGIC)
1152 maxsymlen = 2 * 4 * (UFS_NDADDR + UFS_NINDIR);
1153 else
1154 maxsymlen = 4 * (UFS_NDADDR + UFS_NINDIR);
1155 if (uspi->s_maxsymlinklen > maxsymlen) {
1156 ufs_warning(sb, __func__, "ufs_read_super: excessive maximum "
1157 "fast symlink size (%u)\n", uspi->s_maxsymlinklen);
1158 uspi->s_maxsymlinklen = maxsymlen;
1161 inode = ufs_iget(sb, UFS_ROOTINO);
1162 if (IS_ERR(inode)) {
1163 ret = PTR_ERR(inode);
1164 goto failed;
1166 sb->s_root = d_alloc_root(inode);
1167 if (!sb->s_root) {
1168 ret = -ENOMEM;
1169 goto dalloc_failed;
1172 ufs_setup_cstotal(sb);
1174 * Read cylinder group structures
1176 if (!(sb->s_flags & MS_RDONLY))
1177 if (!ufs_read_cylinder_structures(sb))
1178 goto failed;
1180 UFSD("EXIT\n");
1181 return 0;
1183 dalloc_failed:
1184 iput(inode);
1185 failed:
1186 if (ubh)
1187 ubh_brelse_uspi (uspi);
1188 kfree (uspi);
1189 kfree(sbi);
1190 sb->s_fs_info = NULL;
1191 UFSD("EXIT (FAILED)\n");
1192 return ret;
1194 failed_nomem:
1195 UFSD("EXIT (NOMEM)\n");
1196 return -ENOMEM;
1199 static int ufs_sync_fs(struct super_block *sb, int wait)
1201 struct ufs_sb_private_info * uspi;
1202 struct ufs_super_block_first * usb1;
1203 struct ufs_super_block_third * usb3;
1204 unsigned flags;
1206 lock_ufs(sb);
1207 lock_super(sb);
1209 UFSD("ENTER\n");
1211 flags = UFS_SB(sb)->s_flags;
1212 uspi = UFS_SB(sb)->s_uspi;
1213 usb1 = ubh_get_usb_first(uspi);
1214 usb3 = ubh_get_usb_third(uspi);
1216 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1217 if ((flags & UFS_ST_MASK) == UFS_ST_SUN ||
1218 (flags & UFS_ST_MASK) == UFS_ST_SUNOS ||
1219 (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1220 ufs_set_fs_state(sb, usb1, usb3,
1221 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
1222 ufs_put_cstotal(sb);
1223 sb->s_dirt = 0;
1225 UFSD("EXIT\n");
1226 unlock_super(sb);
1227 unlock_ufs(sb);
1229 return 0;
1232 static void ufs_write_super(struct super_block *sb)
1234 if (!(sb->s_flags & MS_RDONLY))
1235 ufs_sync_fs(sb, 1);
1236 else
1237 sb->s_dirt = 0;
1240 static void ufs_put_super(struct super_block *sb)
1242 struct ufs_sb_info * sbi = UFS_SB(sb);
1244 UFSD("ENTER\n");
1246 if (sb->s_dirt)
1247 ufs_write_super(sb);
1249 if (!(sb->s_flags & MS_RDONLY))
1250 ufs_put_super_internal(sb);
1252 ubh_brelse_uspi (sbi->s_uspi);
1253 kfree (sbi->s_uspi);
1254 kfree (sbi);
1255 sb->s_fs_info = NULL;
1256 UFSD("EXIT\n");
1257 return;
1261 static int ufs_remount (struct super_block *sb, int *mount_flags, char *data)
1263 struct ufs_sb_private_info * uspi;
1264 struct ufs_super_block_first * usb1;
1265 struct ufs_super_block_third * usb3;
1266 unsigned new_mount_opt, ufstype;
1267 unsigned flags;
1269 lock_ufs(sb);
1270 lock_super(sb);
1271 uspi = UFS_SB(sb)->s_uspi;
1272 flags = UFS_SB(sb)->s_flags;
1273 usb1 = ubh_get_usb_first(uspi);
1274 usb3 = ubh_get_usb_third(uspi);
1277 * Allow the "check" option to be passed as a remount option.
1278 * It is not possible to change ufstype option during remount
1280 ufstype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
1281 new_mount_opt = 0;
1282 ufs_set_opt (new_mount_opt, ONERROR_LOCK);
1283 if (!ufs_parse_options (data, &new_mount_opt)) {
1284 unlock_super(sb);
1285 unlock_ufs(sb);
1286 return -EINVAL;
1288 if (!(new_mount_opt & UFS_MOUNT_UFSTYPE)) {
1289 new_mount_opt |= ufstype;
1290 } else if ((new_mount_opt & UFS_MOUNT_UFSTYPE) != ufstype) {
1291 printk("ufstype can't be changed during remount\n");
1292 unlock_super(sb);
1293 unlock_ufs(sb);
1294 return -EINVAL;
1297 if ((*mount_flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
1298 UFS_SB(sb)->s_mount_opt = new_mount_opt;
1299 unlock_super(sb);
1300 unlock_ufs(sb);
1301 return 0;
1305 * fs was mouted as rw, remounting ro
1307 if (*mount_flags & MS_RDONLY) {
1308 ufs_put_super_internal(sb);
1309 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1310 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
1311 || (flags & UFS_ST_MASK) == UFS_ST_SUNOS
1312 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1313 ufs_set_fs_state(sb, usb1, usb3,
1314 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
1315 ubh_mark_buffer_dirty (USPI_UBH(uspi));
1316 sb->s_dirt = 0;
1317 sb->s_flags |= MS_RDONLY;
1318 } else {
1320 * fs was mounted as ro, remounting rw
1322 #ifndef CONFIG_UFS_FS_WRITE
1323 printk("ufs was compiled with read-only support, "
1324 "can't be mounted as read-write\n");
1325 unlock_super(sb);
1326 unlock_ufs(sb);
1327 return -EINVAL;
1328 #else
1329 if (ufstype != UFS_MOUNT_UFSTYPE_SUN &&
1330 ufstype != UFS_MOUNT_UFSTYPE_SUNOS &&
1331 ufstype != UFS_MOUNT_UFSTYPE_44BSD &&
1332 ufstype != UFS_MOUNT_UFSTYPE_SUNx86 &&
1333 ufstype != UFS_MOUNT_UFSTYPE_UFS2) {
1334 printk("this ufstype is read-only supported\n");
1335 unlock_super(sb);
1336 unlock_ufs(sb);
1337 return -EINVAL;
1339 if (!ufs_read_cylinder_structures(sb)) {
1340 printk("failed during remounting\n");
1341 unlock_super(sb);
1342 unlock_ufs(sb);
1343 return -EPERM;
1345 sb->s_flags &= ~MS_RDONLY;
1346 #endif
1348 UFS_SB(sb)->s_mount_opt = new_mount_opt;
1349 unlock_super(sb);
1350 unlock_ufs(sb);
1351 return 0;
1354 static int ufs_show_options(struct seq_file *seq, struct dentry *root)
1356 struct ufs_sb_info *sbi = UFS_SB(root->d_sb);
1357 unsigned mval = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
1358 const struct match_token *tp = tokens;
1360 while (tp->token != Opt_onerror_panic && tp->token != mval)
1361 ++tp;
1362 BUG_ON(tp->token == Opt_onerror_panic);
1363 seq_printf(seq, ",%s", tp->pattern);
1365 mval = sbi->s_mount_opt & UFS_MOUNT_ONERROR;
1366 while (tp->token != Opt_err && tp->token != mval)
1367 ++tp;
1368 BUG_ON(tp->token == Opt_err);
1369 seq_printf(seq, ",%s", tp->pattern);
1371 return 0;
1374 static int ufs_statfs(struct dentry *dentry, struct kstatfs *buf)
1376 struct super_block *sb = dentry->d_sb;
1377 struct ufs_sb_private_info *uspi= UFS_SB(sb)->s_uspi;
1378 unsigned flags = UFS_SB(sb)->s_flags;
1379 struct ufs_super_block_first *usb1;
1380 struct ufs_super_block_second *usb2;
1381 struct ufs_super_block_third *usb3;
1382 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1384 lock_ufs(sb);
1386 usb1 = ubh_get_usb_first(uspi);
1387 usb2 = ubh_get_usb_second(uspi);
1388 usb3 = ubh_get_usb_third(uspi);
1390 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
1391 buf->f_type = UFS2_MAGIC;
1392 buf->f_blocks = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
1393 } else {
1394 buf->f_type = UFS_MAGIC;
1395 buf->f_blocks = uspi->s_dsize;
1397 buf->f_bfree = ufs_blkstofrags(uspi->cs_total.cs_nbfree) +
1398 uspi->cs_total.cs_nffree;
1399 buf->f_ffree = uspi->cs_total.cs_nifree;
1400 buf->f_bsize = sb->s_blocksize;
1401 buf->f_bavail = (buf->f_bfree > (((long)buf->f_blocks / 100) * uspi->s_minfree))
1402 ? (buf->f_bfree - (((long)buf->f_blocks / 100) * uspi->s_minfree)) : 0;
1403 buf->f_files = uspi->s_ncg * uspi->s_ipg;
1404 buf->f_namelen = UFS_MAXNAMLEN;
1405 buf->f_fsid.val[0] = (u32)id;
1406 buf->f_fsid.val[1] = (u32)(id >> 32);
1408 unlock_ufs(sb);
1410 return 0;
1413 static struct kmem_cache * ufs_inode_cachep;
1415 static struct inode *ufs_alloc_inode(struct super_block *sb)
1417 struct ufs_inode_info *ei;
1418 ei = (struct ufs_inode_info *)kmem_cache_alloc(ufs_inode_cachep, GFP_NOFS);
1419 if (!ei)
1420 return NULL;
1421 ei->vfs_inode.i_version = 1;
1422 return &ei->vfs_inode;
1425 static void ufs_i_callback(struct rcu_head *head)
1427 struct inode *inode = container_of(head, struct inode, i_rcu);
1428 kmem_cache_free(ufs_inode_cachep, UFS_I(inode));
1431 static void ufs_destroy_inode(struct inode *inode)
1433 call_rcu(&inode->i_rcu, ufs_i_callback);
1436 static void init_once(void *foo)
1438 struct ufs_inode_info *ei = (struct ufs_inode_info *) foo;
1440 inode_init_once(&ei->vfs_inode);
1443 static int init_inodecache(void)
1445 ufs_inode_cachep = kmem_cache_create("ufs_inode_cache",
1446 sizeof(struct ufs_inode_info),
1447 0, (SLAB_RECLAIM_ACCOUNT|
1448 SLAB_MEM_SPREAD),
1449 init_once);
1450 if (ufs_inode_cachep == NULL)
1451 return -ENOMEM;
1452 return 0;
1455 static void destroy_inodecache(void)
1457 kmem_cache_destroy(ufs_inode_cachep);
1460 static const struct super_operations ufs_super_ops = {
1461 .alloc_inode = ufs_alloc_inode,
1462 .destroy_inode = ufs_destroy_inode,
1463 .write_inode = ufs_write_inode,
1464 .evict_inode = ufs_evict_inode,
1465 .put_super = ufs_put_super,
1466 .write_super = ufs_write_super,
1467 .sync_fs = ufs_sync_fs,
1468 .statfs = ufs_statfs,
1469 .remount_fs = ufs_remount,
1470 .show_options = ufs_show_options,
1473 static struct dentry *ufs_mount(struct file_system_type *fs_type,
1474 int flags, const char *dev_name, void *data)
1476 return mount_bdev(fs_type, flags, dev_name, data, ufs_fill_super);
1479 static struct file_system_type ufs_fs_type = {
1480 .owner = THIS_MODULE,
1481 .name = "ufs",
1482 .mount = ufs_mount,
1483 .kill_sb = kill_block_super,
1484 .fs_flags = FS_REQUIRES_DEV,
1487 static int __init init_ufs_fs(void)
1489 int err = init_inodecache();
1490 if (err)
1491 goto out1;
1492 err = register_filesystem(&ufs_fs_type);
1493 if (err)
1494 goto out;
1495 return 0;
1496 out:
1497 destroy_inodecache();
1498 out1:
1499 return err;
1502 static void __exit exit_ufs_fs(void)
1504 unregister_filesystem(&ufs_fs_type);
1505 destroy_inodecache();
1508 module_init(init_ufs_fs)
1509 module_exit(exit_ufs_fs)
1510 MODULE_LICENSE("GPL");