[NETFILTER]: ip_tables: fix module refcount leaks in compat error paths
[hh.org.git] / fs / ufs / super.c
blob992ee0b87cc3f7835fa376c75733822d9ddfbdc6
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 <gertjan@cs.vu.nl>
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
67 #include <linux/module.h>
68 #include <linux/bitops.h>
70 #include <stdarg.h>
72 #include <asm/uaccess.h>
73 #include <asm/system.h>
75 #include <linux/errno.h>
76 #include <linux/fs.h>
77 #include <linux/ufs_fs.h>
78 #include <linux/slab.h>
79 #include <linux/time.h>
80 #include <linux/stat.h>
81 #include <linux/string.h>
82 #include <linux/blkdev.h>
83 #include <linux/init.h>
84 #include <linux/parser.h>
85 #include <linux/smp_lock.h>
86 #include <linux/buffer_head.h>
87 #include <linux/vfs.h>
89 #include "swab.h"
90 #include "util.h"
92 #ifdef CONFIG_UFS_DEBUG
94 * Print contents of ufs_super_block, useful for debugging
96 static void ufs_print_super_stuff(struct super_block *sb, unsigned flags,
97 struct ufs_super_block_first *usb1,
98 struct ufs_super_block_second *usb2,
99 struct ufs_super_block_third *usb3)
101 printk("ufs_print_super_stuff\n");
102 printk(" magic: 0x%x\n", fs32_to_cpu(sb, usb3->fs_magic));
103 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
104 printk(" fs_size: %llu\n", (unsigned long long)
105 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size));
106 printk(" fs_dsize: %llu\n", (unsigned long long)
107 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize));
108 printk(" bsize: %u\n",
109 fs32_to_cpu(sb, usb1->fs_bsize));
110 printk(" fsize: %u\n",
111 fs32_to_cpu(sb, usb1->fs_fsize));
112 printk(" fs_volname: %s\n", usb2->fs_un.fs_u2.fs_volname);
113 printk(" fs_sblockloc: %llu\n", (unsigned long long)
114 fs64_to_cpu(sb, usb2->fs_un.fs_u2.fs_sblockloc));
115 printk(" cs_ndir(No of dirs): %llu\n", (unsigned long long)
116 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir));
117 printk(" cs_nbfree(No of free blocks): %llu\n",
118 (unsigned long long)
119 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree));
120 } else {
121 printk(" sblkno: %u\n", fs32_to_cpu(sb, usb1->fs_sblkno));
122 printk(" cblkno: %u\n", fs32_to_cpu(sb, usb1->fs_cblkno));
123 printk(" iblkno: %u\n", fs32_to_cpu(sb, usb1->fs_iblkno));
124 printk(" dblkno: %u\n", fs32_to_cpu(sb, usb1->fs_dblkno));
125 printk(" cgoffset: %u\n",
126 fs32_to_cpu(sb, usb1->fs_cgoffset));
127 printk(" ~cgmask: 0x%x\n",
128 ~fs32_to_cpu(sb, usb1->fs_cgmask));
129 printk(" size: %u\n", fs32_to_cpu(sb, usb1->fs_size));
130 printk(" dsize: %u\n", fs32_to_cpu(sb, usb1->fs_dsize));
131 printk(" ncg: %u\n", fs32_to_cpu(sb, usb1->fs_ncg));
132 printk(" bsize: %u\n", fs32_to_cpu(sb, usb1->fs_bsize));
133 printk(" fsize: %u\n", fs32_to_cpu(sb, usb1->fs_fsize));
134 printk(" frag: %u\n", fs32_to_cpu(sb, usb1->fs_frag));
135 printk(" fragshift: %u\n",
136 fs32_to_cpu(sb, usb1->fs_fragshift));
137 printk(" ~fmask: %u\n", ~fs32_to_cpu(sb, usb1->fs_fmask));
138 printk(" fshift: %u\n", fs32_to_cpu(sb, usb1->fs_fshift));
139 printk(" sbsize: %u\n", fs32_to_cpu(sb, usb1->fs_sbsize));
140 printk(" spc: %u\n", fs32_to_cpu(sb, usb1->fs_spc));
141 printk(" cpg: %u\n", fs32_to_cpu(sb, usb1->fs_cpg));
142 printk(" ipg: %u\n", fs32_to_cpu(sb, usb1->fs_ipg));
143 printk(" fpg: %u\n", fs32_to_cpu(sb, usb1->fs_fpg));
144 printk(" csaddr: %u\n", fs32_to_cpu(sb, usb1->fs_csaddr));
145 printk(" cssize: %u\n", fs32_to_cpu(sb, usb1->fs_cssize));
146 printk(" cgsize: %u\n", fs32_to_cpu(sb, usb1->fs_cgsize));
147 printk(" fstodb: %u\n",
148 fs32_to_cpu(sb, usb1->fs_fsbtodb));
149 printk(" nrpos: %u\n", fs32_to_cpu(sb, usb3->fs_nrpos));
150 printk(" ndir %u\n",
151 fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir));
152 printk(" nifree %u\n",
153 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree));
154 printk(" nbfree %u\n",
155 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree));
156 printk(" nffree %u\n",
157 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree));
159 printk("\n");
163 * Print contents of ufs_cylinder_group, useful for debugging
165 static void ufs_print_cylinder_stuff(struct super_block *sb,
166 struct ufs_cylinder_group *cg)
168 printk("\nufs_print_cylinder_stuff\n");
169 printk("size of ucg: %zu\n", sizeof(struct ufs_cylinder_group));
170 printk(" magic: %x\n", fs32_to_cpu(sb, cg->cg_magic));
171 printk(" time: %u\n", fs32_to_cpu(sb, cg->cg_time));
172 printk(" cgx: %u\n", fs32_to_cpu(sb, cg->cg_cgx));
173 printk(" ncyl: %u\n", fs16_to_cpu(sb, cg->cg_ncyl));
174 printk(" niblk: %u\n", fs16_to_cpu(sb, cg->cg_niblk));
175 printk(" ndblk: %u\n", fs32_to_cpu(sb, cg->cg_ndblk));
176 printk(" cs_ndir: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_ndir));
177 printk(" cs_nbfree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nbfree));
178 printk(" cs_nifree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nifree));
179 printk(" cs_nffree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nffree));
180 printk(" rotor: %u\n", fs32_to_cpu(sb, cg->cg_rotor));
181 printk(" frotor: %u\n", fs32_to_cpu(sb, cg->cg_frotor));
182 printk(" irotor: %u\n", fs32_to_cpu(sb, cg->cg_irotor));
183 printk(" frsum: %u, %u, %u, %u, %u, %u, %u, %u\n",
184 fs32_to_cpu(sb, cg->cg_frsum[0]), fs32_to_cpu(sb, cg->cg_frsum[1]),
185 fs32_to_cpu(sb, cg->cg_frsum[2]), fs32_to_cpu(sb, cg->cg_frsum[3]),
186 fs32_to_cpu(sb, cg->cg_frsum[4]), fs32_to_cpu(sb, cg->cg_frsum[5]),
187 fs32_to_cpu(sb, cg->cg_frsum[6]), fs32_to_cpu(sb, cg->cg_frsum[7]));
188 printk(" btotoff: %u\n", fs32_to_cpu(sb, cg->cg_btotoff));
189 printk(" boff: %u\n", fs32_to_cpu(sb, cg->cg_boff));
190 printk(" iuseoff: %u\n", fs32_to_cpu(sb, cg->cg_iusedoff));
191 printk(" freeoff: %u\n", fs32_to_cpu(sb, cg->cg_freeoff));
192 printk(" nextfreeoff: %u\n", fs32_to_cpu(sb, cg->cg_nextfreeoff));
193 printk(" clustersumoff %u\n",
194 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clustersumoff));
195 printk(" clusteroff %u\n",
196 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clusteroff));
197 printk(" nclusterblks %u\n",
198 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_nclusterblks));
199 printk("\n");
201 #else
202 # define ufs_print_super_stuff(sb, flags, usb1, usb2, usb3) /**/
203 # define ufs_print_cylinder_stuff(sb, cg) /**/
204 #endif /* CONFIG_UFS_DEBUG */
206 static struct super_operations ufs_super_ops;
208 static char error_buf[1024];
210 void ufs_error (struct super_block * sb, const char * function,
211 const char * fmt, ...)
213 struct ufs_sb_private_info * uspi;
214 struct ufs_super_block_first * usb1;
215 va_list args;
217 uspi = UFS_SB(sb)->s_uspi;
218 usb1 = ubh_get_usb_first(uspi);
220 if (!(sb->s_flags & MS_RDONLY)) {
221 usb1->fs_clean = UFS_FSBAD;
222 ubh_mark_buffer_dirty(USPI_UBH(uspi));
223 sb->s_dirt = 1;
224 sb->s_flags |= MS_RDONLY;
226 va_start (args, fmt);
227 vsprintf (error_buf, fmt, args);
228 va_end (args);
229 switch (UFS_SB(sb)->s_mount_opt & UFS_MOUNT_ONERROR) {
230 case UFS_MOUNT_ONERROR_PANIC:
231 panic ("UFS-fs panic (device %s): %s: %s\n",
232 sb->s_id, function, error_buf);
234 case UFS_MOUNT_ONERROR_LOCK:
235 case UFS_MOUNT_ONERROR_UMOUNT:
236 case UFS_MOUNT_ONERROR_REPAIR:
237 printk (KERN_CRIT "UFS-fs error (device %s): %s: %s\n",
238 sb->s_id, function, error_buf);
242 void ufs_panic (struct super_block * sb, const char * function,
243 const char * fmt, ...)
245 struct ufs_sb_private_info * uspi;
246 struct ufs_super_block_first * usb1;
247 va_list args;
249 uspi = UFS_SB(sb)->s_uspi;
250 usb1 = ubh_get_usb_first(uspi);
252 if (!(sb->s_flags & MS_RDONLY)) {
253 usb1->fs_clean = UFS_FSBAD;
254 ubh_mark_buffer_dirty(USPI_UBH(uspi));
255 sb->s_dirt = 1;
257 va_start (args, fmt);
258 vsprintf (error_buf, fmt, args);
259 va_end (args);
260 sb->s_flags |= MS_RDONLY;
261 printk (KERN_CRIT "UFS-fs panic (device %s): %s: %s\n",
262 sb->s_id, function, error_buf);
265 void ufs_warning (struct super_block * sb, const char * function,
266 const char * fmt, ...)
268 va_list args;
270 va_start (args, fmt);
271 vsprintf (error_buf, fmt, args);
272 va_end (args);
273 printk (KERN_WARNING "UFS-fs warning (device %s): %s: %s\n",
274 sb->s_id, function, error_buf);
277 enum {
278 Opt_type_old, Opt_type_sunx86, Opt_type_sun, Opt_type_44bsd,
279 Opt_type_ufs2, Opt_type_hp, Opt_type_nextstepcd, Opt_type_nextstep,
280 Opt_type_openstep, Opt_onerror_panic, Opt_onerror_lock,
281 Opt_onerror_umount, Opt_onerror_repair, Opt_err
284 static match_table_t tokens = {
285 {Opt_type_old, "ufstype=old"},
286 {Opt_type_sunx86, "ufstype=sunx86"},
287 {Opt_type_sun, "ufstype=sun"},
288 {Opt_type_44bsd, "ufstype=44bsd"},
289 {Opt_type_ufs2, "ufstype=ufs2"},
290 {Opt_type_ufs2, "ufstype=5xbsd"},
291 {Opt_type_hp, "ufstype=hp"},
292 {Opt_type_nextstepcd, "ufstype=nextstep-cd"},
293 {Opt_type_nextstep, "ufstype=nextstep"},
294 {Opt_type_openstep, "ufstype=openstep"},
295 {Opt_onerror_panic, "onerror=panic"},
296 {Opt_onerror_lock, "onerror=lock"},
297 {Opt_onerror_umount, "onerror=umount"},
298 {Opt_onerror_repair, "onerror=repair"},
299 {Opt_err, NULL}
302 static int ufs_parse_options (char * options, unsigned * mount_options)
304 char * p;
306 UFSD("ENTER\n");
308 if (!options)
309 return 1;
311 while ((p = strsep(&options, ",")) != NULL) {
312 substring_t args[MAX_OPT_ARGS];
313 int token;
314 if (!*p)
315 continue;
317 token = match_token(p, tokens, args);
318 switch (token) {
319 case Opt_type_old:
320 ufs_clear_opt (*mount_options, UFSTYPE);
321 ufs_set_opt (*mount_options, UFSTYPE_OLD);
322 break;
323 case Opt_type_sunx86:
324 ufs_clear_opt (*mount_options, UFSTYPE);
325 ufs_set_opt (*mount_options, UFSTYPE_SUNx86);
326 break;
327 case Opt_type_sun:
328 ufs_clear_opt (*mount_options, UFSTYPE);
329 ufs_set_opt (*mount_options, UFSTYPE_SUN);
330 break;
331 case Opt_type_44bsd:
332 ufs_clear_opt (*mount_options, UFSTYPE);
333 ufs_set_opt (*mount_options, UFSTYPE_44BSD);
334 break;
335 case Opt_type_ufs2:
336 ufs_clear_opt(*mount_options, UFSTYPE);
337 ufs_set_opt(*mount_options, UFSTYPE_UFS2);
338 break;
339 case Opt_type_hp:
340 ufs_clear_opt (*mount_options, UFSTYPE);
341 ufs_set_opt (*mount_options, UFSTYPE_HP);
342 break;
343 case Opt_type_nextstepcd:
344 ufs_clear_opt (*mount_options, UFSTYPE);
345 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP_CD);
346 break;
347 case Opt_type_nextstep:
348 ufs_clear_opt (*mount_options, UFSTYPE);
349 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP);
350 break;
351 case Opt_type_openstep:
352 ufs_clear_opt (*mount_options, UFSTYPE);
353 ufs_set_opt (*mount_options, UFSTYPE_OPENSTEP);
354 break;
355 case Opt_onerror_panic:
356 ufs_clear_opt (*mount_options, ONERROR);
357 ufs_set_opt (*mount_options, ONERROR_PANIC);
358 break;
359 case Opt_onerror_lock:
360 ufs_clear_opt (*mount_options, ONERROR);
361 ufs_set_opt (*mount_options, ONERROR_LOCK);
362 break;
363 case Opt_onerror_umount:
364 ufs_clear_opt (*mount_options, ONERROR);
365 ufs_set_opt (*mount_options, ONERROR_UMOUNT);
366 break;
367 case Opt_onerror_repair:
368 printk("UFS-fs: Unable to do repair on error, "
369 "will lock lock instead\n");
370 ufs_clear_opt (*mount_options, ONERROR);
371 ufs_set_opt (*mount_options, ONERROR_REPAIR);
372 break;
373 default:
374 printk("UFS-fs: Invalid option: \"%s\" "
375 "or missing value\n", p);
376 return 0;
379 return 1;
383 * Diffrent types of UFS hold fs_cstotal in different
384 * places, and use diffrent data structure for it.
385 * To make things simplier we just copy fs_cstotal to ufs_sb_private_info
387 static void ufs_setup_cstotal(struct super_block *sb)
389 struct ufs_sb_info *sbi = UFS_SB(sb);
390 struct ufs_sb_private_info *uspi = sbi->s_uspi;
391 struct ufs_super_block_first *usb1;
392 struct ufs_super_block_second *usb2;
393 struct ufs_super_block_third *usb3;
394 unsigned mtype = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
396 UFSD("ENTER, mtype=%u\n", mtype);
397 usb1 = ubh_get_usb_first(uspi);
398 usb2 = ubh_get_usb_second(uspi);
399 usb3 = ubh_get_usb_third(uspi);
401 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
402 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
403 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
404 /*we have statistic in different place, then usual*/
405 uspi->cs_total.cs_ndir = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir);
406 uspi->cs_total.cs_nbfree = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree);
407 uspi->cs_total.cs_nifree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree);
408 uspi->cs_total.cs_nffree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree);
409 } else {
410 uspi->cs_total.cs_ndir = fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir);
411 uspi->cs_total.cs_nbfree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree);
412 uspi->cs_total.cs_nifree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree);
413 uspi->cs_total.cs_nffree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree);
415 UFSD("EXIT\n");
419 * Read on-disk structures associated with cylinder groups
421 static int ufs_read_cylinder_structures(struct super_block *sb)
423 struct ufs_sb_info *sbi = UFS_SB(sb);
424 struct ufs_sb_private_info *uspi = sbi->s_uspi;
425 unsigned flags = sbi->s_flags;
426 struct ufs_buffer_head * ubh;
427 unsigned char * base, * space;
428 unsigned size, blks, i;
429 struct ufs_super_block_third *usb3;
431 UFSD("ENTER\n");
433 usb3 = ubh_get_usb_third(uspi);
435 * Read cs structures from (usually) first data block
436 * on the device.
438 size = uspi->s_cssize;
439 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
440 base = space = kmalloc(size, GFP_KERNEL);
441 if (!base)
442 goto failed;
443 sbi->s_csp = (struct ufs_csum *)space;
444 for (i = 0; i < blks; i += uspi->s_fpb) {
445 size = uspi->s_bsize;
446 if (i + uspi->s_fpb > blks)
447 size = (blks - i) * uspi->s_fsize;
449 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
450 ubh = ubh_bread(sb,
451 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_csaddr) + i, size);
452 else
453 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
455 if (!ubh)
456 goto failed;
458 ubh_ubhcpymem (space, ubh, size);
460 space += size;
461 ubh_brelse (ubh);
462 ubh = NULL;
466 * Read cylinder group (we read only first fragment from block
467 * at this time) and prepare internal data structures for cg caching.
469 if (!(sbi->s_ucg = kmalloc (sizeof(struct buffer_head *) * uspi->s_ncg, GFP_KERNEL)))
470 goto failed;
471 for (i = 0; i < uspi->s_ncg; i++)
472 sbi->s_ucg[i] = NULL;
473 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
474 sbi->s_ucpi[i] = NULL;
475 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
477 for (i = 0; i < uspi->s_ncg; i++) {
478 UFSD("read cg %u\n", i);
479 if (!(sbi->s_ucg[i] = sb_bread(sb, ufs_cgcmin(i))))
480 goto failed;
481 if (!ufs_cg_chkmagic (sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data))
482 goto failed;
484 ufs_print_cylinder_stuff(sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data);
486 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
487 if (!(sbi->s_ucpi[i] = kmalloc (sizeof(struct ufs_cg_private_info), GFP_KERNEL)))
488 goto failed;
489 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
491 sbi->s_cg_loaded = 0;
492 UFSD("EXIT\n");
493 return 1;
495 failed:
496 kfree (base);
497 if (sbi->s_ucg) {
498 for (i = 0; i < uspi->s_ncg; i++)
499 if (sbi->s_ucg[i])
500 brelse (sbi->s_ucg[i]);
501 kfree (sbi->s_ucg);
502 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++)
503 kfree (sbi->s_ucpi[i]);
505 UFSD("EXIT (FAILED)\n");
506 return 0;
510 * Sync our internal copy of fs_cstotal with disk
512 static void ufs_put_cstotal(struct super_block *sb)
514 unsigned mtype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
515 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
516 struct ufs_super_block_first *usb1;
517 struct ufs_super_block_second *usb2;
518 struct ufs_super_block_third *usb3;
520 UFSD("ENTER\n");
521 usb1 = ubh_get_usb_first(uspi);
522 usb2 = ubh_get_usb_second(uspi);
523 usb3 = ubh_get_usb_third(uspi);
525 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
526 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
527 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
528 /*we have statistic in different place, then usual*/
529 usb2->fs_un.fs_u2.cs_ndir =
530 cpu_to_fs64(sb, uspi->cs_total.cs_ndir);
531 usb2->fs_un.fs_u2.cs_nbfree =
532 cpu_to_fs64(sb, uspi->cs_total.cs_nbfree);
533 usb3->fs_un1.fs_u2.cs_nifree =
534 cpu_to_fs64(sb, uspi->cs_total.cs_nifree);
535 usb3->fs_un1.fs_u2.cs_nffree =
536 cpu_to_fs64(sb, uspi->cs_total.cs_nffree);
537 } else {
538 usb1->fs_cstotal.cs_ndir =
539 cpu_to_fs32(sb, uspi->cs_total.cs_ndir);
540 usb1->fs_cstotal.cs_nbfree =
541 cpu_to_fs32(sb, uspi->cs_total.cs_nbfree);
542 usb1->fs_cstotal.cs_nifree =
543 cpu_to_fs32(sb, uspi->cs_total.cs_nifree);
544 usb1->fs_cstotal.cs_nffree =
545 cpu_to_fs32(sb, uspi->cs_total.cs_nffree);
547 ubh_mark_buffer_dirty(USPI_UBH(uspi));
548 UFSD("EXIT\n");
552 * ufs_put_super_internal() - put on-disk intrenal structures
553 * @sb: pointer to super_block structure
554 * Put on-disk structures associated with cylinder groups
555 * and write them back to disk, also update cs_total on disk
557 static void ufs_put_super_internal(struct super_block *sb)
559 struct ufs_sb_info *sbi = UFS_SB(sb);
560 struct ufs_sb_private_info *uspi = sbi->s_uspi;
561 struct ufs_buffer_head * ubh;
562 unsigned char * base, * space;
563 unsigned blks, size, i;
566 UFSD("ENTER\n");
567 ufs_put_cstotal(sb);
568 size = uspi->s_cssize;
569 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
570 base = space = (char*) sbi->s_csp;
571 for (i = 0; i < blks; i += uspi->s_fpb) {
572 size = uspi->s_bsize;
573 if (i + uspi->s_fpb > blks)
574 size = (blks - i) * uspi->s_fsize;
575 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
576 ubh_memcpyubh (ubh, space, size);
577 space += size;
578 ubh_mark_buffer_uptodate (ubh, 1);
579 ubh_mark_buffer_dirty (ubh);
580 ubh_brelse (ubh);
582 for (i = 0; i < sbi->s_cg_loaded; i++) {
583 ufs_put_cylinder (sb, i);
584 kfree (sbi->s_ucpi[i]);
586 for (; i < UFS_MAX_GROUP_LOADED; i++)
587 kfree (sbi->s_ucpi[i]);
588 for (i = 0; i < uspi->s_ncg; i++)
589 brelse (sbi->s_ucg[i]);
590 kfree (sbi->s_ucg);
591 kfree (base);
592 UFSD("EXIT\n");
595 static int ufs_fill_super(struct super_block *sb, void *data, int silent)
597 struct ufs_sb_info * sbi;
598 struct ufs_sb_private_info * uspi;
599 struct ufs_super_block_first * usb1;
600 struct ufs_super_block_second * usb2;
601 struct ufs_super_block_third * usb3;
602 struct ufs_buffer_head * ubh;
603 struct inode *inode;
604 unsigned block_size, super_block_size;
605 unsigned flags;
606 unsigned super_block_offset;
608 uspi = NULL;
609 ubh = NULL;
610 flags = 0;
612 UFSD("ENTER\n");
614 sbi = kmalloc(sizeof(struct ufs_sb_info), GFP_KERNEL);
615 if (!sbi)
616 goto failed_nomem;
617 sb->s_fs_info = sbi;
618 memset(sbi, 0, sizeof(struct ufs_sb_info));
620 UFSD("flag %u\n", (int)(sb->s_flags & MS_RDONLY));
622 #ifndef CONFIG_UFS_FS_WRITE
623 if (!(sb->s_flags & MS_RDONLY)) {
624 printk("ufs was compiled with read-only support, "
625 "can't be mounted as read-write\n");
626 goto failed;
628 #endif
630 * Set default mount options
631 * Parse mount options
633 sbi->s_mount_opt = 0;
634 ufs_set_opt (sbi->s_mount_opt, ONERROR_LOCK);
635 if (!ufs_parse_options ((char *) data, &sbi->s_mount_opt)) {
636 printk("wrong mount options\n");
637 goto failed;
639 if (!(sbi->s_mount_opt & UFS_MOUNT_UFSTYPE)) {
640 if (!silent)
641 printk("You didn't specify the type of your ufs filesystem\n\n"
642 "mount -t ufs -o ufstype="
643 "sun|sunx86|44bsd|ufs2|5xbsd|old|hp|nextstep|nextstep-cd|openstep ...\n\n"
644 ">>>WARNING<<< Wrong ufstype may corrupt your filesystem, "
645 "default is ufstype=old\n");
646 ufs_set_opt (sbi->s_mount_opt, UFSTYPE_OLD);
649 sbi->s_uspi = uspi =
650 kmalloc (sizeof(struct ufs_sb_private_info), GFP_KERNEL);
651 if (!uspi)
652 goto failed;
654 super_block_offset=UFS_SBLOCK;
656 /* Keep 2Gig file limit. Some UFS variants need to override
657 this but as I don't know which I'll let those in the know loosen
658 the rules */
659 switch (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) {
660 case UFS_MOUNT_UFSTYPE_44BSD:
661 UFSD("ufstype=44bsd\n");
662 uspi->s_fsize = block_size = 512;
663 uspi->s_fmask = ~(512 - 1);
664 uspi->s_fshift = 9;
665 uspi->s_sbsize = super_block_size = 1536;
666 uspi->s_sbbase = 0;
667 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
668 break;
669 case UFS_MOUNT_UFSTYPE_UFS2:
670 UFSD("ufstype=ufs2\n");
671 super_block_offset=SBLOCK_UFS2;
672 uspi->s_fsize = block_size = 512;
673 uspi->s_fmask = ~(512 - 1);
674 uspi->s_fshift = 9;
675 uspi->s_sbsize = super_block_size = 1536;
676 uspi->s_sbbase = 0;
677 flags |= UFS_TYPE_UFS2 | UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
678 if (!(sb->s_flags & MS_RDONLY)) {
679 printk(KERN_INFO "ufstype=ufs2 is supported read-only\n");
680 sb->s_flags |= MS_RDONLY;
682 break;
684 case UFS_MOUNT_UFSTYPE_SUN:
685 UFSD("ufstype=sun\n");
686 uspi->s_fsize = block_size = 1024;
687 uspi->s_fmask = ~(1024 - 1);
688 uspi->s_fshift = 10;
689 uspi->s_sbsize = super_block_size = 2048;
690 uspi->s_sbbase = 0;
691 uspi->s_maxsymlinklen = 56;
692 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUN | UFS_CG_SUN;
693 break;
695 case UFS_MOUNT_UFSTYPE_SUNx86:
696 UFSD("ufstype=sunx86\n");
697 uspi->s_fsize = block_size = 1024;
698 uspi->s_fmask = ~(1024 - 1);
699 uspi->s_fshift = 10;
700 uspi->s_sbsize = super_block_size = 2048;
701 uspi->s_sbbase = 0;
702 uspi->s_maxsymlinklen = 56;
703 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUNx86 | UFS_CG_SUN;
704 break;
706 case UFS_MOUNT_UFSTYPE_OLD:
707 UFSD("ufstype=old\n");
708 uspi->s_fsize = block_size = 1024;
709 uspi->s_fmask = ~(1024 - 1);
710 uspi->s_fshift = 10;
711 uspi->s_sbsize = super_block_size = 2048;
712 uspi->s_sbbase = 0;
713 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
714 if (!(sb->s_flags & MS_RDONLY)) {
715 if (!silent)
716 printk(KERN_INFO "ufstype=old is supported read-only\n");
717 sb->s_flags |= MS_RDONLY;
719 break;
721 case UFS_MOUNT_UFSTYPE_NEXTSTEP:
722 UFSD("ufstype=nextstep\n");
723 uspi->s_fsize = block_size = 1024;
724 uspi->s_fmask = ~(1024 - 1);
725 uspi->s_fshift = 10;
726 uspi->s_sbsize = super_block_size = 2048;
727 uspi->s_sbbase = 0;
728 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
729 if (!(sb->s_flags & MS_RDONLY)) {
730 if (!silent)
731 printk(KERN_INFO "ufstype=nextstep is supported read-only\n");
732 sb->s_flags |= MS_RDONLY;
734 break;
736 case UFS_MOUNT_UFSTYPE_NEXTSTEP_CD:
737 UFSD("ufstype=nextstep-cd\n");
738 uspi->s_fsize = block_size = 2048;
739 uspi->s_fmask = ~(2048 - 1);
740 uspi->s_fshift = 11;
741 uspi->s_sbsize = super_block_size = 2048;
742 uspi->s_sbbase = 0;
743 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
744 if (!(sb->s_flags & MS_RDONLY)) {
745 if (!silent)
746 printk(KERN_INFO "ufstype=nextstep-cd is supported read-only\n");
747 sb->s_flags |= MS_RDONLY;
749 break;
751 case UFS_MOUNT_UFSTYPE_OPENSTEP:
752 UFSD("ufstype=openstep\n");
753 uspi->s_fsize = block_size = 1024;
754 uspi->s_fmask = ~(1024 - 1);
755 uspi->s_fshift = 10;
756 uspi->s_sbsize = super_block_size = 2048;
757 uspi->s_sbbase = 0;
758 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
759 if (!(sb->s_flags & MS_RDONLY)) {
760 if (!silent)
761 printk(KERN_INFO "ufstype=openstep is supported read-only\n");
762 sb->s_flags |= MS_RDONLY;
764 break;
766 case UFS_MOUNT_UFSTYPE_HP:
767 UFSD("ufstype=hp\n");
768 uspi->s_fsize = block_size = 1024;
769 uspi->s_fmask = ~(1024 - 1);
770 uspi->s_fshift = 10;
771 uspi->s_sbsize = super_block_size = 2048;
772 uspi->s_sbbase = 0;
773 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
774 if (!(sb->s_flags & MS_RDONLY)) {
775 if (!silent)
776 printk(KERN_INFO "ufstype=hp is supported read-only\n");
777 sb->s_flags |= MS_RDONLY;
779 break;
780 default:
781 if (!silent)
782 printk("unknown ufstype\n");
783 goto failed;
786 again:
787 if (!sb_set_blocksize(sb, block_size)) {
788 printk(KERN_ERR "UFS: failed to set blocksize\n");
789 goto failed;
793 * read ufs super block from device
796 ubh = ubh_bread_uspi(uspi, sb, uspi->s_sbbase + super_block_offset/block_size, super_block_size);
798 if (!ubh)
799 goto failed;
802 usb1 = ubh_get_usb_first(uspi);
803 usb2 = ubh_get_usb_second(uspi);
804 usb3 = ubh_get_usb_third(uspi);
807 * Check ufs magic number
809 sbi->s_bytesex = BYTESEX_LE;
810 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
811 case UFS_MAGIC:
812 case UFS2_MAGIC:
813 case UFS_MAGIC_LFN:
814 case UFS_MAGIC_FEA:
815 case UFS_MAGIC_4GB:
816 goto magic_found;
818 sbi->s_bytesex = BYTESEX_BE;
819 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
820 case UFS_MAGIC:
821 case UFS2_MAGIC:
822 case UFS_MAGIC_LFN:
823 case UFS_MAGIC_FEA:
824 case UFS_MAGIC_4GB:
825 goto magic_found;
828 if ((((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP)
829 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP_CD)
830 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_OPENSTEP))
831 && uspi->s_sbbase < 256) {
832 ubh_brelse_uspi(uspi);
833 ubh = NULL;
834 uspi->s_sbbase += 8;
835 goto again;
837 if (!silent)
838 printk("ufs_read_super: bad magic number\n");
839 goto failed;
841 magic_found:
843 * Check block and fragment sizes
845 uspi->s_bsize = fs32_to_cpu(sb, usb1->fs_bsize);
846 uspi->s_fsize = fs32_to_cpu(sb, usb1->fs_fsize);
847 uspi->s_sbsize = fs32_to_cpu(sb, usb1->fs_sbsize);
848 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
849 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
851 if (uspi->s_fsize & (uspi->s_fsize - 1)) {
852 printk(KERN_ERR "ufs_read_super: fragment size %u is not a power of 2\n",
853 uspi->s_fsize);
854 goto failed;
856 if (uspi->s_fsize < 512) {
857 printk(KERN_ERR "ufs_read_super: fragment size %u is too small\n",
858 uspi->s_fsize);
859 goto failed;
861 if (uspi->s_fsize > 4096) {
862 printk(KERN_ERR "ufs_read_super: fragment size %u is too large\n",
863 uspi->s_fsize);
864 goto failed;
866 if (uspi->s_bsize & (uspi->s_bsize - 1)) {
867 printk(KERN_ERR "ufs_read_super: block size %u is not a power of 2\n",
868 uspi->s_bsize);
869 goto failed;
871 if (uspi->s_bsize < 4096) {
872 printk(KERN_ERR "ufs_read_super: block size %u is too small\n",
873 uspi->s_bsize);
874 goto failed;
876 if (uspi->s_bsize / uspi->s_fsize > 8) {
877 printk(KERN_ERR "ufs_read_super: too many fragments per block (%u)\n",
878 uspi->s_bsize / uspi->s_fsize);
879 goto failed;
881 if (uspi->s_fsize != block_size || uspi->s_sbsize != super_block_size) {
882 ubh_brelse_uspi(uspi);
883 ubh = NULL;
884 block_size = uspi->s_fsize;
885 super_block_size = uspi->s_sbsize;
886 UFSD("another value of block_size or super_block_size %u, %u\n", block_size, super_block_size);
887 goto again;
891 ufs_print_super_stuff(sb, flags, usb1, usb2, usb3);
894 * Check, if file system was correctly unmounted.
895 * If not, make it read only.
897 if (((flags & UFS_ST_MASK) == UFS_ST_44BSD) ||
898 ((flags & UFS_ST_MASK) == UFS_ST_OLD) ||
899 (((flags & UFS_ST_MASK) == UFS_ST_SUN ||
900 (flags & UFS_ST_MASK) == UFS_ST_SUNx86) &&
901 (ufs_get_fs_state(sb, usb1, usb3) == (UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time))))) {
902 switch(usb1->fs_clean) {
903 case UFS_FSCLEAN:
904 UFSD("fs is clean\n");
905 break;
906 case UFS_FSSTABLE:
907 UFSD("fs is stable\n");
908 break;
909 case UFS_FSOSF1:
910 UFSD("fs is DEC OSF/1\n");
911 break;
912 case UFS_FSACTIVE:
913 printk("ufs_read_super: fs is active\n");
914 sb->s_flags |= MS_RDONLY;
915 break;
916 case UFS_FSBAD:
917 printk("ufs_read_super: fs is bad\n");
918 sb->s_flags |= MS_RDONLY;
919 break;
920 default:
921 printk("ufs_read_super: can't grok fs_clean 0x%x\n", usb1->fs_clean);
922 sb->s_flags |= MS_RDONLY;
923 break;
925 } else {
926 printk("ufs_read_super: fs needs fsck\n");
927 sb->s_flags |= MS_RDONLY;
931 * Read ufs_super_block into internal data structures
933 sb->s_op = &ufs_super_ops;
934 sb->dq_op = NULL; /***/
935 sb->s_magic = fs32_to_cpu(sb, usb3->fs_magic);
937 uspi->s_sblkno = fs32_to_cpu(sb, usb1->fs_sblkno);
938 uspi->s_cblkno = fs32_to_cpu(sb, usb1->fs_cblkno);
939 uspi->s_iblkno = fs32_to_cpu(sb, usb1->fs_iblkno);
940 uspi->s_dblkno = fs32_to_cpu(sb, usb1->fs_dblkno);
941 uspi->s_cgoffset = fs32_to_cpu(sb, usb1->fs_cgoffset);
942 uspi->s_cgmask = fs32_to_cpu(sb, usb1->fs_cgmask);
944 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
945 uspi->s_u2_size = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size);
946 uspi->s_u2_dsize = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
947 } else {
948 uspi->s_size = fs32_to_cpu(sb, usb1->fs_size);
949 uspi->s_dsize = fs32_to_cpu(sb, usb1->fs_dsize);
952 uspi->s_ncg = fs32_to_cpu(sb, usb1->fs_ncg);
953 /* s_bsize already set */
954 /* s_fsize already set */
955 uspi->s_fpb = fs32_to_cpu(sb, usb1->fs_frag);
956 uspi->s_minfree = fs32_to_cpu(sb, usb1->fs_minfree);
957 uspi->s_bmask = fs32_to_cpu(sb, usb1->fs_bmask);
958 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
959 uspi->s_bshift = fs32_to_cpu(sb, usb1->fs_bshift);
960 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
961 UFSD("uspi->s_bshift = %d,uspi->s_fshift = %d", uspi->s_bshift,
962 uspi->s_fshift);
963 uspi->s_fpbshift = fs32_to_cpu(sb, usb1->fs_fragshift);
964 uspi->s_fsbtodb = fs32_to_cpu(sb, usb1->fs_fsbtodb);
965 /* s_sbsize already set */
966 uspi->s_csmask = fs32_to_cpu(sb, usb1->fs_csmask);
967 uspi->s_csshift = fs32_to_cpu(sb, usb1->fs_csshift);
968 uspi->s_nindir = fs32_to_cpu(sb, usb1->fs_nindir);
969 uspi->s_inopb = fs32_to_cpu(sb, usb1->fs_inopb);
970 uspi->s_nspf = fs32_to_cpu(sb, usb1->fs_nspf);
971 uspi->s_npsect = ufs_get_fs_npsect(sb, usb1, usb3);
972 uspi->s_interleave = fs32_to_cpu(sb, usb1->fs_interleave);
973 uspi->s_trackskew = fs32_to_cpu(sb, usb1->fs_trackskew);
974 uspi->s_csaddr = fs32_to_cpu(sb, usb1->fs_csaddr);
975 uspi->s_cssize = fs32_to_cpu(sb, usb1->fs_cssize);
976 uspi->s_cgsize = fs32_to_cpu(sb, usb1->fs_cgsize);
977 uspi->s_ntrak = fs32_to_cpu(sb, usb1->fs_ntrak);
978 uspi->s_nsect = fs32_to_cpu(sb, usb1->fs_nsect);
979 uspi->s_spc = fs32_to_cpu(sb, usb1->fs_spc);
980 uspi->s_ipg = fs32_to_cpu(sb, usb1->fs_ipg);
981 uspi->s_fpg = fs32_to_cpu(sb, usb1->fs_fpg);
982 uspi->s_cpc = fs32_to_cpu(sb, usb2->fs_un.fs_u1.fs_cpc);
983 uspi->s_contigsumsize = fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_contigsumsize);
984 uspi->s_qbmask = ufs_get_fs_qbmask(sb, usb3);
985 uspi->s_qfmask = ufs_get_fs_qfmask(sb, usb3);
986 uspi->s_postblformat = fs32_to_cpu(sb, usb3->fs_postblformat);
987 uspi->s_nrpos = fs32_to_cpu(sb, usb3->fs_nrpos);
988 uspi->s_postbloff = fs32_to_cpu(sb, usb3->fs_postbloff);
989 uspi->s_rotbloff = fs32_to_cpu(sb, usb3->fs_rotbloff);
992 * Compute another frequently used values
994 uspi->s_fpbmask = uspi->s_fpb - 1;
995 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
996 uspi->s_apbshift = uspi->s_bshift - 3;
997 else
998 uspi->s_apbshift = uspi->s_bshift - 2;
1000 uspi->s_2apbshift = uspi->s_apbshift * 2;
1001 uspi->s_3apbshift = uspi->s_apbshift * 3;
1002 uspi->s_apb = 1 << uspi->s_apbshift;
1003 uspi->s_2apb = 1 << uspi->s_2apbshift;
1004 uspi->s_3apb = 1 << uspi->s_3apbshift;
1005 uspi->s_apbmask = uspi->s_apb - 1;
1006 uspi->s_nspfshift = uspi->s_fshift - UFS_SECTOR_BITS;
1007 uspi->s_nspb = uspi->s_nspf << uspi->s_fpbshift;
1008 uspi->s_inopf = uspi->s_inopb >> uspi->s_fpbshift;
1009 uspi->s_bpf = uspi->s_fsize << 3;
1010 uspi->s_bpfshift = uspi->s_fshift + 3;
1011 uspi->s_bpfmask = uspi->s_bpf - 1;
1012 if ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) ==
1013 UFS_MOUNT_UFSTYPE_44BSD)
1014 uspi->s_maxsymlinklen =
1015 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen);
1017 sbi->s_flags = flags;
1019 inode = iget(sb, UFS_ROOTINO);
1020 if (!inode || is_bad_inode(inode))
1021 goto failed;
1022 sb->s_root = d_alloc_root(inode);
1023 if (!sb->s_root)
1024 goto dalloc_failed;
1026 ufs_setup_cstotal(sb);
1028 * Read cylinder group structures
1030 if (!(sb->s_flags & MS_RDONLY))
1031 if (!ufs_read_cylinder_structures(sb))
1032 goto failed;
1034 UFSD("EXIT\n");
1035 return 0;
1037 dalloc_failed:
1038 iput(inode);
1039 failed:
1040 if (ubh)
1041 ubh_brelse_uspi (uspi);
1042 kfree (uspi);
1043 kfree(sbi);
1044 sb->s_fs_info = NULL;
1045 UFSD("EXIT (FAILED)\n");
1046 return -EINVAL;
1048 failed_nomem:
1049 UFSD("EXIT (NOMEM)\n");
1050 return -ENOMEM;
1053 static void ufs_write_super(struct super_block *sb)
1055 struct ufs_sb_private_info * uspi;
1056 struct ufs_super_block_first * usb1;
1057 struct ufs_super_block_third * usb3;
1058 unsigned flags;
1060 lock_kernel();
1062 UFSD("ENTER\n");
1063 flags = UFS_SB(sb)->s_flags;
1064 uspi = UFS_SB(sb)->s_uspi;
1065 usb1 = ubh_get_usb_first(uspi);
1066 usb3 = ubh_get_usb_third(uspi);
1068 if (!(sb->s_flags & MS_RDONLY)) {
1069 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1070 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
1071 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1072 ufs_set_fs_state(sb, usb1, usb3,
1073 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
1074 ufs_put_cstotal(sb);
1076 sb->s_dirt = 0;
1077 UFSD("EXIT\n");
1078 unlock_kernel();
1081 static void ufs_put_super(struct super_block *sb)
1083 struct ufs_sb_info * sbi = UFS_SB(sb);
1085 UFSD("ENTER\n");
1087 if (!(sb->s_flags & MS_RDONLY))
1088 ufs_put_super_internal(sb);
1090 ubh_brelse_uspi (sbi->s_uspi);
1091 kfree (sbi->s_uspi);
1092 kfree (sbi);
1093 sb->s_fs_info = NULL;
1094 UFSD("EXIT\n");
1095 return;
1099 static int ufs_remount (struct super_block *sb, int *mount_flags, char *data)
1101 struct ufs_sb_private_info * uspi;
1102 struct ufs_super_block_first * usb1;
1103 struct ufs_super_block_third * usb3;
1104 unsigned new_mount_opt, ufstype;
1105 unsigned flags;
1107 uspi = UFS_SB(sb)->s_uspi;
1108 flags = UFS_SB(sb)->s_flags;
1109 usb1 = ubh_get_usb_first(uspi);
1110 usb3 = ubh_get_usb_third(uspi);
1113 * Allow the "check" option to be passed as a remount option.
1114 * It is not possible to change ufstype option during remount
1116 ufstype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
1117 new_mount_opt = 0;
1118 ufs_set_opt (new_mount_opt, ONERROR_LOCK);
1119 if (!ufs_parse_options (data, &new_mount_opt))
1120 return -EINVAL;
1121 if (!(new_mount_opt & UFS_MOUNT_UFSTYPE)) {
1122 new_mount_opt |= ufstype;
1123 } else if ((new_mount_opt & UFS_MOUNT_UFSTYPE) != ufstype) {
1124 printk("ufstype can't be changed during remount\n");
1125 return -EINVAL;
1128 if ((*mount_flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
1129 UFS_SB(sb)->s_mount_opt = new_mount_opt;
1130 return 0;
1134 * fs was mouted as rw, remounting ro
1136 if (*mount_flags & MS_RDONLY) {
1137 ufs_put_super_internal(sb);
1138 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1139 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
1140 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1141 ufs_set_fs_state(sb, usb1, usb3,
1142 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
1143 ubh_mark_buffer_dirty (USPI_UBH(uspi));
1144 sb->s_dirt = 0;
1145 sb->s_flags |= MS_RDONLY;
1146 } else {
1148 * fs was mounted as ro, remounting rw
1150 #ifndef CONFIG_UFS_FS_WRITE
1151 printk("ufs was compiled with read-only support, "
1152 "can't be mounted as read-write\n");
1153 return -EINVAL;
1154 #else
1155 if (ufstype != UFS_MOUNT_UFSTYPE_SUN &&
1156 ufstype != UFS_MOUNT_UFSTYPE_44BSD &&
1157 ufstype != UFS_MOUNT_UFSTYPE_SUNx86) {
1158 printk("this ufstype is read-only supported\n");
1159 return -EINVAL;
1161 if (!ufs_read_cylinder_structures(sb)) {
1162 printk("failed during remounting\n");
1163 return -EPERM;
1165 sb->s_flags &= ~MS_RDONLY;
1166 #endif
1168 UFS_SB(sb)->s_mount_opt = new_mount_opt;
1169 return 0;
1172 static int ufs_statfs(struct dentry *dentry, struct kstatfs *buf)
1174 struct super_block *sb = dentry->d_sb;
1175 struct ufs_sb_private_info *uspi= UFS_SB(sb)->s_uspi;
1176 unsigned flags = UFS_SB(sb)->s_flags;
1177 struct ufs_super_block_first *usb1;
1178 struct ufs_super_block_second *usb2;
1179 struct ufs_super_block_third *usb3;
1181 lock_kernel();
1183 usb1 = ubh_get_usb_first(uspi);
1184 usb2 = ubh_get_usb_second(uspi);
1185 usb3 = ubh_get_usb_third(uspi);
1187 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
1188 buf->f_type = UFS2_MAGIC;
1189 buf->f_blocks = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
1190 } else {
1191 buf->f_type = UFS_MAGIC;
1192 buf->f_blocks = uspi->s_dsize;
1194 buf->f_bfree = ufs_blkstofrags(uspi->cs_total.cs_nbfree) +
1195 uspi->cs_total.cs_nffree;
1196 buf->f_ffree = uspi->cs_total.cs_nifree;
1197 buf->f_bsize = sb->s_blocksize;
1198 buf->f_bavail = (buf->f_bfree > (((long)buf->f_blocks / 100) * uspi->s_minfree))
1199 ? (buf->f_bfree - (((long)buf->f_blocks / 100) * uspi->s_minfree)) : 0;
1200 buf->f_files = uspi->s_ncg * uspi->s_ipg;
1201 buf->f_namelen = UFS_MAXNAMLEN;
1203 unlock_kernel();
1205 return 0;
1208 static kmem_cache_t * ufs_inode_cachep;
1210 static struct inode *ufs_alloc_inode(struct super_block *sb)
1212 struct ufs_inode_info *ei;
1213 ei = (struct ufs_inode_info *)kmem_cache_alloc(ufs_inode_cachep, SLAB_KERNEL);
1214 if (!ei)
1215 return NULL;
1216 ei->vfs_inode.i_version = 1;
1217 return &ei->vfs_inode;
1220 static void ufs_destroy_inode(struct inode *inode)
1222 kmem_cache_free(ufs_inode_cachep, UFS_I(inode));
1225 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
1227 struct ufs_inode_info *ei = (struct ufs_inode_info *) foo;
1229 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1230 SLAB_CTOR_CONSTRUCTOR)
1231 inode_init_once(&ei->vfs_inode);
1234 static int init_inodecache(void)
1236 ufs_inode_cachep = kmem_cache_create("ufs_inode_cache",
1237 sizeof(struct ufs_inode_info),
1238 0, (SLAB_RECLAIM_ACCOUNT|
1239 SLAB_MEM_SPREAD),
1240 init_once, NULL);
1241 if (ufs_inode_cachep == NULL)
1242 return -ENOMEM;
1243 return 0;
1246 static void destroy_inodecache(void)
1248 if (kmem_cache_destroy(ufs_inode_cachep))
1249 printk(KERN_INFO "ufs_inode_cache: not all structures were freed\n");
1252 #ifdef CONFIG_QUOTA
1253 static ssize_t ufs_quota_read(struct super_block *, int, char *,size_t, loff_t);
1254 static ssize_t ufs_quota_write(struct super_block *, int, const char *, size_t, loff_t);
1255 #endif
1257 static struct super_operations ufs_super_ops = {
1258 .alloc_inode = ufs_alloc_inode,
1259 .destroy_inode = ufs_destroy_inode,
1260 .read_inode = ufs_read_inode,
1261 .write_inode = ufs_write_inode,
1262 .delete_inode = ufs_delete_inode,
1263 .put_super = ufs_put_super,
1264 .write_super = ufs_write_super,
1265 .statfs = ufs_statfs,
1266 .remount_fs = ufs_remount,
1267 #ifdef CONFIG_QUOTA
1268 .quota_read = ufs_quota_read,
1269 .quota_write = ufs_quota_write,
1270 #endif
1273 #ifdef CONFIG_QUOTA
1275 /* Read data from quotafile - avoid pagecache and such because we cannot afford
1276 * acquiring the locks... As quota files are never truncated and quota code
1277 * itself serializes the operations (and noone else should touch the files)
1278 * we don't have to be afraid of races */
1279 static ssize_t ufs_quota_read(struct super_block *sb, int type, char *data,
1280 size_t len, loff_t off)
1282 struct inode *inode = sb_dqopt(sb)->files[type];
1283 sector_t blk = off >> sb->s_blocksize_bits;
1284 int err = 0;
1285 int offset = off & (sb->s_blocksize - 1);
1286 int tocopy;
1287 size_t toread;
1288 struct buffer_head *bh;
1289 loff_t i_size = i_size_read(inode);
1291 if (off > i_size)
1292 return 0;
1293 if (off+len > i_size)
1294 len = i_size-off;
1295 toread = len;
1296 while (toread > 0) {
1297 tocopy = sb->s_blocksize - offset < toread ?
1298 sb->s_blocksize - offset : toread;
1300 bh = ufs_bread(inode, blk, 0, &err);
1301 if (err)
1302 return err;
1303 if (!bh) /* A hole? */
1304 memset(data, 0, tocopy);
1305 else {
1306 memcpy(data, bh->b_data+offset, tocopy);
1307 brelse(bh);
1309 offset = 0;
1310 toread -= tocopy;
1311 data += tocopy;
1312 blk++;
1314 return len;
1317 /* Write to quotafile */
1318 static ssize_t ufs_quota_write(struct super_block *sb, int type,
1319 const char *data, size_t len, loff_t off)
1321 struct inode *inode = sb_dqopt(sb)->files[type];
1322 sector_t blk = off >> sb->s_blocksize_bits;
1323 int err = 0;
1324 int offset = off & (sb->s_blocksize - 1);
1325 int tocopy;
1326 size_t towrite = len;
1327 struct buffer_head *bh;
1329 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
1330 while (towrite > 0) {
1331 tocopy = sb->s_blocksize - offset < towrite ?
1332 sb->s_blocksize - offset : towrite;
1334 bh = ufs_bread(inode, blk, 1, &err);
1335 if (!bh)
1336 goto out;
1337 lock_buffer(bh);
1338 memcpy(bh->b_data+offset, data, tocopy);
1339 flush_dcache_page(bh->b_page);
1340 set_buffer_uptodate(bh);
1341 mark_buffer_dirty(bh);
1342 unlock_buffer(bh);
1343 brelse(bh);
1344 offset = 0;
1345 towrite -= tocopy;
1346 data += tocopy;
1347 blk++;
1349 out:
1350 if (len == towrite) {
1351 mutex_unlock(&inode->i_mutex);
1352 return err;
1354 if (inode->i_size < off+len-towrite)
1355 i_size_write(inode, off+len-towrite);
1356 inode->i_version++;
1357 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
1358 mark_inode_dirty(inode);
1359 mutex_unlock(&inode->i_mutex);
1360 return len - towrite;
1363 #endif
1365 static int ufs_get_sb(struct file_system_type *fs_type,
1366 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1368 return get_sb_bdev(fs_type, flags, dev_name, data, ufs_fill_super, mnt);
1371 static struct file_system_type ufs_fs_type = {
1372 .owner = THIS_MODULE,
1373 .name = "ufs",
1374 .get_sb = ufs_get_sb,
1375 .kill_sb = kill_block_super,
1376 .fs_flags = FS_REQUIRES_DEV,
1379 static int __init init_ufs_fs(void)
1381 int err = init_inodecache();
1382 if (err)
1383 goto out1;
1384 err = register_filesystem(&ufs_fs_type);
1385 if (err)
1386 goto out;
1387 return 0;
1388 out:
1389 destroy_inodecache();
1390 out1:
1391 return err;
1394 static void __exit exit_ufs_fs(void)
1396 unregister_filesystem(&ufs_fs_type);
1397 destroy_inodecache();
1400 module_init(init_ufs_fs)
1401 module_exit(exit_ufs_fs)
1402 MODULE_LICENSE("GPL");