blob: 38eb0b7a1f3d3111c0e4a1b604d7ac40e110cc46 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ufs/super.c
3 *
4 * Copyright (C) 1998
5 * Daniel Pirkl <daniel.pirkl@email.cz>
6 * Charles University, Faculty of Mathematics and Physics
7 */
8
9/* Derived from
10 *
11 * linux/fs/ext2/super.c
12 *
13 * Copyright (C) 1992, 1993, 1994, 1995
14 * Remy Card (card@masi.ibp.fr)
15 * Laboratoire MASI - Institut Blaise Pascal
16 * Universite Pierre et Marie Curie (Paris VI)
17 *
18 * from
19 *
20 * linux/fs/minix/inode.c
21 *
22 * Copyright (C) 1991, 1992 Linus Torvalds
23 *
24 * Big-endian to little-endian byte-swapping/bitmaps by
25 * David S. Miller (davem@caip.rutgers.edu), 1995
26 */
27
28/*
29 * Inspired by
30 *
31 * linux/fs/ufs/super.c
32 *
33 * Copyright (C) 1996
34 * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu)
35 * Laboratory for Computer Science Research Computing Facility
36 * Rutgers, The State University of New Jersey
37 *
38 * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
39 *
40 * Kernel module support added on 96/04/26 by
41 * Stefan Reinauer <stepan@home.culture.mipt.ru>
42 *
43 * Module usage counts added on 96/04/29 by
44 * Gertjan van Wingerde <gertjan@cs.vu.nl>
45 *
46 * Clean swab support on 19970406 by
47 * Francois-Rene Rideau <fare@tunes.org>
48 *
49 * 4.4BSD (FreeBSD) support added on February 1st 1998 by
50 * Niels Kristian Bech Jensen <nkbj@image.dk> partially based
51 * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>.
52 *
53 * NeXTstep support added on February 5th 1998 by
54 * Niels Kristian Bech Jensen <nkbj@image.dk>.
55 *
56 * write support Daniel Pirkl <daniel.pirkl@email.cz> 1998
57 *
58 * HP/UX hfs filesystem support added by
59 * Martin K. Petersen <mkp@mkp.net>, August 1999
60 *
61 * UFS2 (of FreeBSD 5.x) support added by
62 * Niraj Kumar <niraj17@iitbombay.org>, Jan 2004
63 *
Evgeniy Dushistovcbcae392007-02-12 00:54:30 -080064 * UFS2 write support added by
65 * Evgeniy Dushistov <dushistov@mail.ru>, 2007
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 */
67
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/module.h>
70#include <linux/bitops.h>
71
72#include <stdarg.h>
73
74#include <asm/uaccess.h>
75#include <asm/system.h>
76
77#include <linux/errno.h>
78#include <linux/fs.h>
79#include <linux/ufs_fs.h>
80#include <linux/slab.h>
81#include <linux/time.h>
82#include <linux/stat.h>
83#include <linux/string.h>
84#include <linux/blkdev.h>
85#include <linux/init.h>
86#include <linux/parser.h>
87#include <linux/smp_lock.h>
88#include <linux/buffer_head.h>
89#include <linux/vfs.h>
vignesh babu9e8c4272007-07-15 23:41:36 -070090#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#include "swab.h"
93#include "util.h"
94
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070095#ifdef CONFIG_UFS_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/*
97 * Print contents of ufs_super_block, useful for debugging
98 */
Evgeniy Dushistov3313e292007-02-12 00:54:31 -080099static void ufs_print_super_stuff(struct super_block *sb,
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700100 struct ufs_super_block_first *usb1,
101 struct ufs_super_block_second *usb2,
102 struct ufs_super_block_third *usb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800104 u32 magic = fs32_to_cpu(sb, usb3->fs_magic);
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 printk("ufs_print_super_stuff\n");
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800107 printk(" magic: 0x%x\n", magic);
108 if (fs32_to_cpu(sb, usb3->fs_magic) == UFS2_MAGIC) {
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700109 printk(" fs_size: %llu\n", (unsigned long long)
110 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size));
111 printk(" fs_dsize: %llu\n", (unsigned long long)
112 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize));
113 printk(" bsize: %u\n",
114 fs32_to_cpu(sb, usb1->fs_bsize));
115 printk(" fsize: %u\n",
116 fs32_to_cpu(sb, usb1->fs_fsize));
117 printk(" fs_volname: %s\n", usb2->fs_un.fs_u2.fs_volname);
118 printk(" fs_sblockloc: %llu\n", (unsigned long long)
119 fs64_to_cpu(sb, usb2->fs_un.fs_u2.fs_sblockloc));
120 printk(" cs_ndir(No of dirs): %llu\n", (unsigned long long)
121 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir));
122 printk(" cs_nbfree(No of free blocks): %llu\n",
123 (unsigned long long)
124 fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800125 printk(KERN_INFO" cs_nifree(Num of free inodes): %llu\n",
126 (unsigned long long)
127 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree));
128 printk(KERN_INFO" cs_nffree(Num of free frags): %llu\n",
129 (unsigned long long)
130 fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree));
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700131 } else {
132 printk(" sblkno: %u\n", fs32_to_cpu(sb, usb1->fs_sblkno));
133 printk(" cblkno: %u\n", fs32_to_cpu(sb, usb1->fs_cblkno));
134 printk(" iblkno: %u\n", fs32_to_cpu(sb, usb1->fs_iblkno));
135 printk(" dblkno: %u\n", fs32_to_cpu(sb, usb1->fs_dblkno));
136 printk(" cgoffset: %u\n",
137 fs32_to_cpu(sb, usb1->fs_cgoffset));
138 printk(" ~cgmask: 0x%x\n",
139 ~fs32_to_cpu(sb, usb1->fs_cgmask));
140 printk(" size: %u\n", fs32_to_cpu(sb, usb1->fs_size));
141 printk(" dsize: %u\n", fs32_to_cpu(sb, usb1->fs_dsize));
142 printk(" ncg: %u\n", fs32_to_cpu(sb, usb1->fs_ncg));
143 printk(" bsize: %u\n", fs32_to_cpu(sb, usb1->fs_bsize));
144 printk(" fsize: %u\n", fs32_to_cpu(sb, usb1->fs_fsize));
145 printk(" frag: %u\n", fs32_to_cpu(sb, usb1->fs_frag));
146 printk(" fragshift: %u\n",
147 fs32_to_cpu(sb, usb1->fs_fragshift));
148 printk(" ~fmask: %u\n", ~fs32_to_cpu(sb, usb1->fs_fmask));
149 printk(" fshift: %u\n", fs32_to_cpu(sb, usb1->fs_fshift));
150 printk(" sbsize: %u\n", fs32_to_cpu(sb, usb1->fs_sbsize));
151 printk(" spc: %u\n", fs32_to_cpu(sb, usb1->fs_spc));
152 printk(" cpg: %u\n", fs32_to_cpu(sb, usb1->fs_cpg));
153 printk(" ipg: %u\n", fs32_to_cpu(sb, usb1->fs_ipg));
154 printk(" fpg: %u\n", fs32_to_cpu(sb, usb1->fs_fpg));
155 printk(" csaddr: %u\n", fs32_to_cpu(sb, usb1->fs_csaddr));
156 printk(" cssize: %u\n", fs32_to_cpu(sb, usb1->fs_cssize));
157 printk(" cgsize: %u\n", fs32_to_cpu(sb, usb1->fs_cgsize));
158 printk(" fstodb: %u\n",
159 fs32_to_cpu(sb, usb1->fs_fsbtodb));
160 printk(" nrpos: %u\n", fs32_to_cpu(sb, usb3->fs_nrpos));
161 printk(" ndir %u\n",
162 fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir));
163 printk(" nifree %u\n",
164 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree));
165 printk(" nbfree %u\n",
166 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree));
167 printk(" nffree %u\n",
168 fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree));
169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 printk("\n");
171}
172
173/*
174 * Print contents of ufs_cylinder_group, useful for debugging
175 */
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700176static void ufs_print_cylinder_stuff(struct super_block *sb,
177 struct ufs_cylinder_group *cg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 printk("\nufs_print_cylinder_stuff\n");
Andrew Morton96710b22006-06-25 05:47:28 -0700180 printk("size of ucg: %zu\n", sizeof(struct ufs_cylinder_group));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 printk(" magic: %x\n", fs32_to_cpu(sb, cg->cg_magic));
182 printk(" time: %u\n", fs32_to_cpu(sb, cg->cg_time));
183 printk(" cgx: %u\n", fs32_to_cpu(sb, cg->cg_cgx));
184 printk(" ncyl: %u\n", fs16_to_cpu(sb, cg->cg_ncyl));
185 printk(" niblk: %u\n", fs16_to_cpu(sb, cg->cg_niblk));
186 printk(" ndblk: %u\n", fs32_to_cpu(sb, cg->cg_ndblk));
187 printk(" cs_ndir: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_ndir));
188 printk(" cs_nbfree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nbfree));
189 printk(" cs_nifree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nifree));
190 printk(" cs_nffree: %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nffree));
191 printk(" rotor: %u\n", fs32_to_cpu(sb, cg->cg_rotor));
192 printk(" frotor: %u\n", fs32_to_cpu(sb, cg->cg_frotor));
193 printk(" irotor: %u\n", fs32_to_cpu(sb, cg->cg_irotor));
194 printk(" frsum: %u, %u, %u, %u, %u, %u, %u, %u\n",
195 fs32_to_cpu(sb, cg->cg_frsum[0]), fs32_to_cpu(sb, cg->cg_frsum[1]),
196 fs32_to_cpu(sb, cg->cg_frsum[2]), fs32_to_cpu(sb, cg->cg_frsum[3]),
197 fs32_to_cpu(sb, cg->cg_frsum[4]), fs32_to_cpu(sb, cg->cg_frsum[5]),
198 fs32_to_cpu(sb, cg->cg_frsum[6]), fs32_to_cpu(sb, cg->cg_frsum[7]));
199 printk(" btotoff: %u\n", fs32_to_cpu(sb, cg->cg_btotoff));
200 printk(" boff: %u\n", fs32_to_cpu(sb, cg->cg_boff));
201 printk(" iuseoff: %u\n", fs32_to_cpu(sb, cg->cg_iusedoff));
202 printk(" freeoff: %u\n", fs32_to_cpu(sb, cg->cg_freeoff));
203 printk(" nextfreeoff: %u\n", fs32_to_cpu(sb, cg->cg_nextfreeoff));
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700204 printk(" clustersumoff %u\n",
205 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clustersumoff));
206 printk(" clusteroff %u\n",
207 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clusteroff));
208 printk(" nclusterblks %u\n",
209 fs32_to_cpu(sb, cg->cg_u.cg_44.cg_nclusterblks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 printk("\n");
211}
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700212#else
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800213# define ufs_print_super_stuff(sb, usb1, usb2, usb3) /**/
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700214# define ufs_print_cylinder_stuff(sb, cg) /**/
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700215#endif /* CONFIG_UFS_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800217static const struct super_operations ufs_super_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219static char error_buf[1024];
220
221void ufs_error (struct super_block * sb, const char * function,
222 const char * fmt, ...)
223{
224 struct ufs_sb_private_info * uspi;
225 struct ufs_super_block_first * usb1;
226 va_list args;
227
228 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300229 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 if (!(sb->s_flags & MS_RDONLY)) {
232 usb1->fs_clean = UFS_FSBAD;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700233 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 sb->s_dirt = 1;
235 sb->s_flags |= MS_RDONLY;
236 }
237 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800238 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 va_end (args);
240 switch (UFS_SB(sb)->s_mount_opt & UFS_MOUNT_ONERROR) {
241 case UFS_MOUNT_ONERROR_PANIC:
242 panic ("UFS-fs panic (device %s): %s: %s\n",
243 sb->s_id, function, error_buf);
244
245 case UFS_MOUNT_ONERROR_LOCK:
246 case UFS_MOUNT_ONERROR_UMOUNT:
247 case UFS_MOUNT_ONERROR_REPAIR:
248 printk (KERN_CRIT "UFS-fs error (device %s): %s: %s\n",
249 sb->s_id, function, error_buf);
250 }
251}
252
253void ufs_panic (struct super_block * sb, const char * function,
254 const char * fmt, ...)
255{
256 struct ufs_sb_private_info * uspi;
257 struct ufs_super_block_first * usb1;
258 va_list args;
259
260 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +0300261 usb1 = ubh_get_usb_first(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 if (!(sb->s_flags & MS_RDONLY)) {
264 usb1->fs_clean = UFS_FSBAD;
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700265 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 sb->s_dirt = 1;
267 }
268 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800269 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 va_end (args);
271 sb->s_flags |= MS_RDONLY;
272 printk (KERN_CRIT "UFS-fs panic (device %s): %s: %s\n",
273 sb->s_id, function, error_buf);
274}
275
276void ufs_warning (struct super_block * sb, const char * function,
277 const char * fmt, ...)
278{
279 va_list args;
280
281 va_start (args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -0800282 vsnprintf (error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 va_end (args);
284 printk (KERN_WARNING "UFS-fs warning (device %s): %s: %s\n",
285 sb->s_id, function, error_buf);
286}
287
288enum {
289 Opt_type_old, Opt_type_sunx86, Opt_type_sun, Opt_type_44bsd,
290 Opt_type_ufs2, Opt_type_hp, Opt_type_nextstepcd, Opt_type_nextstep,
291 Opt_type_openstep, Opt_onerror_panic, Opt_onerror_lock,
292 Opt_onerror_umount, Opt_onerror_repair, Opt_err
293};
294
295static match_table_t tokens = {
296 {Opt_type_old, "ufstype=old"},
297 {Opt_type_sunx86, "ufstype=sunx86"},
298 {Opt_type_sun, "ufstype=sun"},
299 {Opt_type_44bsd, "ufstype=44bsd"},
300 {Opt_type_ufs2, "ufstype=ufs2"},
301 {Opt_type_ufs2, "ufstype=5xbsd"},
302 {Opt_type_hp, "ufstype=hp"},
303 {Opt_type_nextstepcd, "ufstype=nextstep-cd"},
304 {Opt_type_nextstep, "ufstype=nextstep"},
305 {Opt_type_openstep, "ufstype=openstep"},
306 {Opt_onerror_panic, "onerror=panic"},
307 {Opt_onerror_lock, "onerror=lock"},
308 {Opt_onerror_umount, "onerror=umount"},
309 {Opt_onerror_repair, "onerror=repair"},
310 {Opt_err, NULL}
311};
312
313static int ufs_parse_options (char * options, unsigned * mount_options)
314{
315 char * p;
316
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700317 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 if (!options)
320 return 1;
321
322 while ((p = strsep(&options, ",")) != NULL) {
323 substring_t args[MAX_OPT_ARGS];
324 int token;
325 if (!*p)
326 continue;
327
328 token = match_token(p, tokens, args);
329 switch (token) {
330 case Opt_type_old:
331 ufs_clear_opt (*mount_options, UFSTYPE);
332 ufs_set_opt (*mount_options, UFSTYPE_OLD);
333 break;
334 case Opt_type_sunx86:
335 ufs_clear_opt (*mount_options, UFSTYPE);
336 ufs_set_opt (*mount_options, UFSTYPE_SUNx86);
337 break;
338 case Opt_type_sun:
339 ufs_clear_opt (*mount_options, UFSTYPE);
340 ufs_set_opt (*mount_options, UFSTYPE_SUN);
341 break;
342 case Opt_type_44bsd:
343 ufs_clear_opt (*mount_options, UFSTYPE);
344 ufs_set_opt (*mount_options, UFSTYPE_44BSD);
345 break;
346 case Opt_type_ufs2:
347 ufs_clear_opt(*mount_options, UFSTYPE);
348 ufs_set_opt(*mount_options, UFSTYPE_UFS2);
349 break;
350 case Opt_type_hp:
351 ufs_clear_opt (*mount_options, UFSTYPE);
352 ufs_set_opt (*mount_options, UFSTYPE_HP);
353 break;
354 case Opt_type_nextstepcd:
355 ufs_clear_opt (*mount_options, UFSTYPE);
356 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP_CD);
357 break;
358 case Opt_type_nextstep:
359 ufs_clear_opt (*mount_options, UFSTYPE);
360 ufs_set_opt (*mount_options, UFSTYPE_NEXTSTEP);
361 break;
362 case Opt_type_openstep:
363 ufs_clear_opt (*mount_options, UFSTYPE);
364 ufs_set_opt (*mount_options, UFSTYPE_OPENSTEP);
365 break;
366 case Opt_onerror_panic:
367 ufs_clear_opt (*mount_options, ONERROR);
368 ufs_set_opt (*mount_options, ONERROR_PANIC);
369 break;
370 case Opt_onerror_lock:
371 ufs_clear_opt (*mount_options, ONERROR);
372 ufs_set_opt (*mount_options, ONERROR_LOCK);
373 break;
374 case Opt_onerror_umount:
375 ufs_clear_opt (*mount_options, ONERROR);
376 ufs_set_opt (*mount_options, ONERROR_UMOUNT);
377 break;
378 case Opt_onerror_repair:
379 printk("UFS-fs: Unable to do repair on error, "
380 "will lock lock instead\n");
381 ufs_clear_opt (*mount_options, ONERROR);
382 ufs_set_opt (*mount_options, ONERROR_REPAIR);
383 break;
384 default:
385 printk("UFS-fs: Invalid option: \"%s\" "
386 "or missing value\n", p);
387 return 0;
388 }
389 }
390 return 1;
391}
392
393/*
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700394 * Diffrent types of UFS hold fs_cstotal in different
395 * places, and use diffrent data structure for it.
396 * To make things simplier we just copy fs_cstotal to ufs_sb_private_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700398static void ufs_setup_cstotal(struct super_block *sb)
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800399{
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700400 struct ufs_sb_info *sbi = UFS_SB(sb);
401 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700402 struct ufs_super_block_first *usb1;
403 struct ufs_super_block_second *usb2;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700404 struct ufs_super_block_third *usb3;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700405 unsigned mtype = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
406
407 UFSD("ENTER, mtype=%u\n", mtype);
408 usb1 = ubh_get_usb_first(uspi);
409 usb2 = ubh_get_usb_second(uspi);
410 usb3 = ubh_get_usb_third(uspi);
411
412 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
413 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
414 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
415 /*we have statistic in different place, then usual*/
416 uspi->cs_total.cs_ndir = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir);
417 uspi->cs_total.cs_nbfree = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree);
418 uspi->cs_total.cs_nifree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree);
419 uspi->cs_total.cs_nffree = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree);
420 } else {
421 uspi->cs_total.cs_ndir = fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir);
422 uspi->cs_total.cs_nbfree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree);
423 uspi->cs_total.cs_nifree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree);
424 uspi->cs_total.cs_nffree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree);
425 }
426 UFSD("EXIT\n");
427}
428
429/*
430 * Read on-disk structures associated with cylinder groups
431 */
432static int ufs_read_cylinder_structures(struct super_block *sb)
433{
434 struct ufs_sb_info *sbi = UFS_SB(sb);
435 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 struct ufs_buffer_head * ubh;
437 unsigned char * base, * space;
438 unsigned size, blks, i;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700439 struct ufs_super_block_third *usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700441 UFSD("ENTER\n");
442
443 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /*
445 * Read cs structures from (usually) first data block
446 * on the device.
447 */
448 size = uspi->s_cssize;
449 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
450 base = space = kmalloc(size, GFP_KERNEL);
451 if (!base)
452 goto failed;
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800453 sbi->s_csp = (struct ufs_csum *)space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 for (i = 0; i < blks; i += uspi->s_fpb) {
455 size = uspi->s_bsize;
456 if (i + uspi->s_fpb > blks)
457 size = (blks - i) * uspi->s_fsize;
458
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800459 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
Evgeniy7b4ee732006-01-14 11:42:06 +0300460
461 if (!ubh)
462 goto failed;
463
464 ubh_ubhcpymem (space, ubh, size);
Evgeniy7b4ee732006-01-14 11:42:06 +0300465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 space += size;
467 ubh_brelse (ubh);
468 ubh = NULL;
469 }
470
471 /*
472 * Read cylinder group (we read only first fragment from block
473 * at this time) and prepare internal data structures for cg caching.
474 */
475 if (!(sbi->s_ucg = kmalloc (sizeof(struct buffer_head *) * uspi->s_ncg, GFP_KERNEL)))
476 goto failed;
477 for (i = 0; i < uspi->s_ncg; i++)
478 sbi->s_ucg[i] = NULL;
479 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
480 sbi->s_ucpi[i] = NULL;
481 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
482 }
483 for (i = 0; i < uspi->s_ncg; i++) {
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700484 UFSD("read cg %u\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (!(sbi->s_ucg[i] = sb_bread(sb, ufs_cgcmin(i))))
486 goto failed;
487 if (!ufs_cg_chkmagic (sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data))
488 goto failed;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 ufs_print_cylinder_stuff(sb, (struct ufs_cylinder_group *) sbi->s_ucg[i]->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
493 if (!(sbi->s_ucpi[i] = kmalloc (sizeof(struct ufs_cg_private_info), GFP_KERNEL)))
494 goto failed;
495 sbi->s_cgno[i] = UFS_CGNO_EMPTY;
496 }
497 sbi->s_cg_loaded = 0;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700498 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return 1;
500
501failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800502 kfree (base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (sbi->s_ucg) {
504 for (i = 0; i < uspi->s_ncg; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800505 if (sbi->s_ucg[i])
506 brelse (sbi->s_ucg[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 kfree (sbi->s_ucg);
508 for (i = 0; i < UFS_MAX_GROUP_LOADED; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800509 kfree (sbi->s_ucpi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700511 UFSD("EXIT (FAILED)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return 0;
513}
514
515/*
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700516 * Sync our internal copy of fs_cstotal with disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700518static void ufs_put_cstotal(struct super_block *sb)
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800519{
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700520 unsigned mtype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
521 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
522 struct ufs_super_block_first *usb1;
523 struct ufs_super_block_second *usb2;
524 struct ufs_super_block_third *usb3;
525
526 UFSD("ENTER\n");
527 usb1 = ubh_get_usb_first(uspi);
528 usb2 = ubh_get_usb_second(uspi);
529 usb3 = ubh_get_usb_third(uspi);
530
531 if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
532 (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
533 mtype == UFS_MOUNT_UFSTYPE_UFS2) {
534 /*we have statistic in different place, then usual*/
535 usb2->fs_un.fs_u2.cs_ndir =
536 cpu_to_fs64(sb, uspi->cs_total.cs_ndir);
537 usb2->fs_un.fs_u2.cs_nbfree =
538 cpu_to_fs64(sb, uspi->cs_total.cs_nbfree);
539 usb3->fs_un1.fs_u2.cs_nifree =
540 cpu_to_fs64(sb, uspi->cs_total.cs_nifree);
541 usb3->fs_un1.fs_u2.cs_nffree =
542 cpu_to_fs64(sb, uspi->cs_total.cs_nffree);
543 } else {
544 usb1->fs_cstotal.cs_ndir =
545 cpu_to_fs32(sb, uspi->cs_total.cs_ndir);
546 usb1->fs_cstotal.cs_nbfree =
547 cpu_to_fs32(sb, uspi->cs_total.cs_nbfree);
548 usb1->fs_cstotal.cs_nifree =
549 cpu_to_fs32(sb, uspi->cs_total.cs_nifree);
550 usb1->fs_cstotal.cs_nffree =
551 cpu_to_fs32(sb, uspi->cs_total.cs_nffree);
552 }
553 ubh_mark_buffer_dirty(USPI_UBH(uspi));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800554 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700555 UFSD("EXIT\n");
556}
557
558/**
559 * ufs_put_super_internal() - put on-disk intrenal structures
560 * @sb: pointer to super_block structure
561 * Put on-disk structures associated with cylinder groups
562 * and write them back to disk, also update cs_total on disk
563 */
564static void ufs_put_super_internal(struct super_block *sb)
565{
566 struct ufs_sb_info *sbi = UFS_SB(sb);
567 struct ufs_sb_private_info *uspi = sbi->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 struct ufs_buffer_head * ubh;
569 unsigned char * base, * space;
570 unsigned blks, size, i;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700573 UFSD("ENTER\n");
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700574 ufs_put_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 size = uspi->s_cssize;
576 blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
Evgeniy Dushistove295cfc2006-02-03 03:04:04 -0800577 base = space = (char*) sbi->s_csp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 for (i = 0; i < blks; i += uspi->s_fpb) {
579 size = uspi->s_bsize;
580 if (i + uspi->s_fpb > blks)
581 size = (blks - i) * uspi->s_fsize;
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 ubh_memcpyubh (ubh, space, size);
586 space += size;
587 ubh_mark_buffer_uptodate (ubh, 1);
588 ubh_mark_buffer_dirty (ubh);
589 ubh_brelse (ubh);
590 }
591 for (i = 0; i < sbi->s_cg_loaded; i++) {
592 ufs_put_cylinder (sb, i);
593 kfree (sbi->s_ucpi[i]);
594 }
595 for (; i < UFS_MAX_GROUP_LOADED; i++)
596 kfree (sbi->s_ucpi[i]);
597 for (i = 0; i < uspi->s_ncg; i++)
598 brelse (sbi->s_ucg[i]);
599 kfree (sbi->s_ucg);
600 kfree (base);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700601 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604static int ufs_fill_super(struct super_block *sb, void *data, int silent)
605{
606 struct ufs_sb_info * sbi;
607 struct ufs_sb_private_info * uspi;
608 struct ufs_super_block_first * usb1;
609 struct ufs_super_block_second * usb2;
610 struct ufs_super_block_third * usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 struct ufs_buffer_head * ubh;
612 struct inode *inode;
613 unsigned block_size, super_block_size;
614 unsigned flags;
Evgeniy7b4ee732006-01-14 11:42:06 +0300615 unsigned super_block_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 uspi = NULL;
618 ubh = NULL;
619 flags = 0;
620
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700621 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700623 sbi = kzalloc(sizeof(struct ufs_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (!sbi)
625 goto failed_nomem;
626 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700628 UFSD("flag %u\n", (int)(sb->s_flags & MS_RDONLY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630#ifndef CONFIG_UFS_FS_WRITE
631 if (!(sb->s_flags & MS_RDONLY)) {
632 printk("ufs was compiled with read-only support, "
633 "can't be mounted as read-write\n");
634 goto failed;
635 }
636#endif
637 /*
638 * Set default mount options
639 * Parse mount options
640 */
641 sbi->s_mount_opt = 0;
642 ufs_set_opt (sbi->s_mount_opt, ONERROR_LOCK);
643 if (!ufs_parse_options ((char *) data, &sbi->s_mount_opt)) {
644 printk("wrong mount options\n");
645 goto failed;
646 }
647 if (!(sbi->s_mount_opt & UFS_MOUNT_UFSTYPE)) {
648 if (!silent)
649 printk("You didn't specify the type of your ufs filesystem\n\n"
650 "mount -t ufs -o ufstype="
Michael Owenc690a722006-03-24 18:21:44 +0100651 "sun|sunx86|44bsd|ufs2|5xbsd|old|hp|nextstep|nextstep-cd|openstep ...\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 ">>>WARNING<<< Wrong ufstype may corrupt your filesystem, "
653 "default is ufstype=old\n");
654 ufs_set_opt (sbi->s_mount_opt, UFSTYPE_OLD);
655 }
656
657 sbi->s_uspi = uspi =
658 kmalloc (sizeof(struct ufs_sb_private_info), GFP_KERNEL);
659 if (!uspi)
660 goto failed;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800661 uspi->s_dirblksize = UFS_SECTOR_SIZE;
Evgeniy7b4ee732006-01-14 11:42:06 +0300662 super_block_offset=UFS_SBLOCK;
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /* Keep 2Gig file limit. Some UFS variants need to override
665 this but as I don't know which I'll let those in the know loosen
666 the rules */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 switch (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) {
668 case UFS_MOUNT_UFSTYPE_44BSD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700669 UFSD("ufstype=44bsd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 uspi->s_fsize = block_size = 512;
671 uspi->s_fmask = ~(512 - 1);
672 uspi->s_fshift = 9;
673 uspi->s_sbsize = super_block_size = 1536;
674 uspi->s_sbbase = 0;
675 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
676 break;
677 case UFS_MOUNT_UFSTYPE_UFS2:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700678 UFSD("ufstype=ufs2\n");
Evgeniy7b4ee732006-01-14 11:42:06 +0300679 super_block_offset=SBLOCK_UFS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 uspi->s_fsize = block_size = 512;
681 uspi->s_fmask = ~(512 - 1);
682 uspi->s_fshift = 9;
683 uspi->s_sbsize = super_block_size = 1536;
684 uspi->s_sbbase = 0;
685 flags |= UFS_TYPE_UFS2 | UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 break;
687
688 case UFS_MOUNT_UFSTYPE_SUN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700689 UFSD("ufstype=sun\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 uspi->s_fsize = block_size = 1024;
691 uspi->s_fmask = ~(1024 - 1);
692 uspi->s_fshift = 10;
693 uspi->s_sbsize = super_block_size = 2048;
694 uspi->s_sbbase = 0;
695 uspi->s_maxsymlinklen = 56;
696 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUN | UFS_CG_SUN;
697 break;
698
699 case UFS_MOUNT_UFSTYPE_SUNx86:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700700 UFSD("ufstype=sunx86\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 uspi->s_fsize = block_size = 1024;
702 uspi->s_fmask = ~(1024 - 1);
703 uspi->s_fshift = 10;
704 uspi->s_sbsize = super_block_size = 2048;
705 uspi->s_sbbase = 0;
706 uspi->s_maxsymlinklen = 56;
707 flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUNx86 | UFS_CG_SUN;
708 break;
709
710 case UFS_MOUNT_UFSTYPE_OLD:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700711 UFSD("ufstype=old\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 uspi->s_fsize = block_size = 1024;
713 uspi->s_fmask = ~(1024 - 1);
714 uspi->s_fshift = 10;
715 uspi->s_sbsize = super_block_size = 2048;
716 uspi->s_sbbase = 0;
717 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
718 if (!(sb->s_flags & MS_RDONLY)) {
719 if (!silent)
720 printk(KERN_INFO "ufstype=old is supported read-only\n");
721 sb->s_flags |= MS_RDONLY;
722 }
723 break;
724
725 case UFS_MOUNT_UFSTYPE_NEXTSTEP:
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800726 /*TODO: check may be we need set special dir block size?*/
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700727 UFSD("ufstype=nextstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 uspi->s_fsize = block_size = 1024;
729 uspi->s_fmask = ~(1024 - 1);
730 uspi->s_fshift = 10;
731 uspi->s_sbsize = super_block_size = 2048;
732 uspi->s_sbbase = 0;
733 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
734 if (!(sb->s_flags & MS_RDONLY)) {
735 if (!silent)
736 printk(KERN_INFO "ufstype=nextstep is supported read-only\n");
737 sb->s_flags |= MS_RDONLY;
738 }
739 break;
740
741 case UFS_MOUNT_UFSTYPE_NEXTSTEP_CD:
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800742 /*TODO: check may be we need set special dir block size?*/
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700743 UFSD("ufstype=nextstep-cd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 uspi->s_fsize = block_size = 2048;
745 uspi->s_fmask = ~(2048 - 1);
746 uspi->s_fshift = 11;
747 uspi->s_sbsize = super_block_size = 2048;
748 uspi->s_sbbase = 0;
749 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
750 if (!(sb->s_flags & MS_RDONLY)) {
751 if (!silent)
752 printk(KERN_INFO "ufstype=nextstep-cd is supported read-only\n");
753 sb->s_flags |= MS_RDONLY;
754 }
755 break;
756
757 case UFS_MOUNT_UFSTYPE_OPENSTEP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700758 UFSD("ufstype=openstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 uspi->s_fsize = block_size = 1024;
760 uspi->s_fmask = ~(1024 - 1);
761 uspi->s_fshift = 10;
762 uspi->s_sbsize = super_block_size = 2048;
763 uspi->s_sbbase = 0;
Evgeniy Dushistovf3369532007-02-08 14:20:25 -0800764 uspi->s_dirblksize = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
766 if (!(sb->s_flags & MS_RDONLY)) {
767 if (!silent)
768 printk(KERN_INFO "ufstype=openstep is supported read-only\n");
769 sb->s_flags |= MS_RDONLY;
770 }
771 break;
772
773 case UFS_MOUNT_UFSTYPE_HP:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700774 UFSD("ufstype=hp\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 uspi->s_fsize = block_size = 1024;
776 uspi->s_fmask = ~(1024 - 1);
777 uspi->s_fshift = 10;
778 uspi->s_sbsize = super_block_size = 2048;
779 uspi->s_sbbase = 0;
780 flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
781 if (!(sb->s_flags & MS_RDONLY)) {
782 if (!silent)
783 printk(KERN_INFO "ufstype=hp is supported read-only\n");
784 sb->s_flags |= MS_RDONLY;
785 }
786 break;
787 default:
788 if (!silent)
789 printk("unknown ufstype\n");
790 goto failed;
791 }
792
793again:
794 if (!sb_set_blocksize(sb, block_size)) {
795 printk(KERN_ERR "UFS: failed to set blocksize\n");
796 goto failed;
797 }
798
799 /*
800 * read ufs super block from device
801 */
Evgeniy7b4ee732006-01-14 11:42:06 +0300802
803 ubh = ubh_bread_uspi(uspi, sb, uspi->s_sbbase + super_block_offset/block_size, super_block_size);
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (!ubh)
806 goto failed;
807
808
Evgeniy7b4ee732006-01-14 11:42:06 +0300809 usb1 = ubh_get_usb_first(uspi);
810 usb2 = ubh_get_usb_second(uspi);
811 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 /*
814 * Check ufs magic number
815 */
816 sbi->s_bytesex = BYTESEX_LE;
817 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
818 case UFS_MAGIC:
819 case UFS2_MAGIC:
820 case UFS_MAGIC_LFN:
821 case UFS_MAGIC_FEA:
822 case UFS_MAGIC_4GB:
823 goto magic_found;
824 }
825 sbi->s_bytesex = BYTESEX_BE;
826 switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
827 case UFS_MAGIC:
828 case UFS2_MAGIC:
829 case UFS_MAGIC_LFN:
830 case UFS_MAGIC_FEA:
831 case UFS_MAGIC_4GB:
832 goto magic_found;
833 }
834
835 if ((((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP)
836 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_NEXTSTEP_CD)
837 || ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_OPENSTEP))
838 && uspi->s_sbbase < 256) {
839 ubh_brelse_uspi(uspi);
840 ubh = NULL;
841 uspi->s_sbbase += 8;
842 goto again;
843 }
844 if (!silent)
845 printk("ufs_read_super: bad magic number\n");
846 goto failed;
847
848magic_found:
849 /*
850 * Check block and fragment sizes
851 */
852 uspi->s_bsize = fs32_to_cpu(sb, usb1->fs_bsize);
853 uspi->s_fsize = fs32_to_cpu(sb, usb1->fs_fsize);
854 uspi->s_sbsize = fs32_to_cpu(sb, usb1->fs_sbsize);
855 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
856 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
857
vignesh babu9e8c4272007-07-15 23:41:36 -0700858 if (!is_power_of_2(uspi->s_fsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 printk(KERN_ERR "ufs_read_super: fragment size %u is not a power of 2\n",
860 uspi->s_fsize);
861 goto failed;
862 }
863 if (uspi->s_fsize < 512) {
864 printk(KERN_ERR "ufs_read_super: fragment size %u is too small\n",
865 uspi->s_fsize);
866 goto failed;
867 }
868 if (uspi->s_fsize > 4096) {
869 printk(KERN_ERR "ufs_read_super: fragment size %u is too large\n",
870 uspi->s_fsize);
871 goto failed;
872 }
vignesh babu9e8c4272007-07-15 23:41:36 -0700873 if (!is_power_of_2(uspi->s_bsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 printk(KERN_ERR "ufs_read_super: block size %u is not a power of 2\n",
875 uspi->s_bsize);
876 goto failed;
877 }
878 if (uspi->s_bsize < 4096) {
879 printk(KERN_ERR "ufs_read_super: block size %u is too small\n",
880 uspi->s_bsize);
881 goto failed;
882 }
883 if (uspi->s_bsize / uspi->s_fsize > 8) {
884 printk(KERN_ERR "ufs_read_super: too many fragments per block (%u)\n",
885 uspi->s_bsize / uspi->s_fsize);
886 goto failed;
887 }
888 if (uspi->s_fsize != block_size || uspi->s_sbsize != super_block_size) {
889 ubh_brelse_uspi(uspi);
890 ubh = NULL;
891 block_size = uspi->s_fsize;
892 super_block_size = uspi->s_sbsize;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700893 UFSD("another value of block_size or super_block_size %u, %u\n", block_size, super_block_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 goto again;
895 }
896
Evgeniy Dushistovf9b7cba2007-09-24 21:24:39 -0700897 sbi->s_flags = flags;/*after that line some functions use s_flags*/
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800898 ufs_print_super_stuff(sb, usb1, usb2, usb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 /*
901 * Check, if file system was correctly unmounted.
902 * If not, make it read only.
903 */
904 if (((flags & UFS_ST_MASK) == UFS_ST_44BSD) ||
905 ((flags & UFS_ST_MASK) == UFS_ST_OLD) ||
906 (((flags & UFS_ST_MASK) == UFS_ST_SUN ||
907 (flags & UFS_ST_MASK) == UFS_ST_SUNx86) &&
908 (ufs_get_fs_state(sb, usb1, usb3) == (UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time))))) {
909 switch(usb1->fs_clean) {
910 case UFS_FSCLEAN:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700911 UFSD("fs is clean\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 break;
913 case UFS_FSSTABLE:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700914 UFSD("fs is stable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 break;
916 case UFS_FSOSF1:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700917 UFSD("fs is DEC OSF/1\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 break;
919 case UFS_FSACTIVE:
920 printk("ufs_read_super: fs is active\n");
921 sb->s_flags |= MS_RDONLY;
922 break;
923 case UFS_FSBAD:
924 printk("ufs_read_super: fs is bad\n");
925 sb->s_flags |= MS_RDONLY;
926 break;
927 default:
928 printk("ufs_read_super: can't grok fs_clean 0x%x\n", usb1->fs_clean);
929 sb->s_flags |= MS_RDONLY;
930 break;
931 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -0700932 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 printk("ufs_read_super: fs needs fsck\n");
934 sb->s_flags |= MS_RDONLY;
935 }
936
937 /*
938 * Read ufs_super_block into internal data structures
939 */
940 sb->s_op = &ufs_super_ops;
941 sb->dq_op = NULL; /***/
942 sb->s_magic = fs32_to_cpu(sb, usb3->fs_magic);
943
944 uspi->s_sblkno = fs32_to_cpu(sb, usb1->fs_sblkno);
945 uspi->s_cblkno = fs32_to_cpu(sb, usb1->fs_cblkno);
946 uspi->s_iblkno = fs32_to_cpu(sb, usb1->fs_iblkno);
947 uspi->s_dblkno = fs32_to_cpu(sb, usb1->fs_dblkno);
948 uspi->s_cgoffset = fs32_to_cpu(sb, usb1->fs_cgoffset);
949 uspi->s_cgmask = fs32_to_cpu(sb, usb1->fs_cgmask);
950
951 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700952 uspi->s_u2_size = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size);
953 uspi->s_u2_dsize = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
954 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 uspi->s_size = fs32_to_cpu(sb, usb1->fs_size);
956 uspi->s_dsize = fs32_to_cpu(sb, usb1->fs_dsize);
957 }
958
959 uspi->s_ncg = fs32_to_cpu(sb, usb1->fs_ncg);
960 /* s_bsize already set */
961 /* s_fsize already set */
962 uspi->s_fpb = fs32_to_cpu(sb, usb1->fs_frag);
963 uspi->s_minfree = fs32_to_cpu(sb, usb1->fs_minfree);
964 uspi->s_bmask = fs32_to_cpu(sb, usb1->fs_bmask);
965 uspi->s_fmask = fs32_to_cpu(sb, usb1->fs_fmask);
966 uspi->s_bshift = fs32_to_cpu(sb, usb1->fs_bshift);
967 uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700968 UFSD("uspi->s_bshift = %d,uspi->s_fshift = %d", uspi->s_bshift,
969 uspi->s_fshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 uspi->s_fpbshift = fs32_to_cpu(sb, usb1->fs_fragshift);
971 uspi->s_fsbtodb = fs32_to_cpu(sb, usb1->fs_fsbtodb);
972 /* s_sbsize already set */
973 uspi->s_csmask = fs32_to_cpu(sb, usb1->fs_csmask);
974 uspi->s_csshift = fs32_to_cpu(sb, usb1->fs_csshift);
975 uspi->s_nindir = fs32_to_cpu(sb, usb1->fs_nindir);
976 uspi->s_inopb = fs32_to_cpu(sb, usb1->fs_inopb);
977 uspi->s_nspf = fs32_to_cpu(sb, usb1->fs_nspf);
978 uspi->s_npsect = ufs_get_fs_npsect(sb, usb1, usb3);
979 uspi->s_interleave = fs32_to_cpu(sb, usb1->fs_interleave);
980 uspi->s_trackskew = fs32_to_cpu(sb, usb1->fs_trackskew);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800981
982 if (uspi->fs_magic == UFS2_MAGIC)
983 uspi->s_csaddr = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_csaddr);
984 else
985 uspi->s_csaddr = fs32_to_cpu(sb, usb1->fs_csaddr);
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 uspi->s_cssize = fs32_to_cpu(sb, usb1->fs_cssize);
988 uspi->s_cgsize = fs32_to_cpu(sb, usb1->fs_cgsize);
989 uspi->s_ntrak = fs32_to_cpu(sb, usb1->fs_ntrak);
990 uspi->s_nsect = fs32_to_cpu(sb, usb1->fs_nsect);
991 uspi->s_spc = fs32_to_cpu(sb, usb1->fs_spc);
992 uspi->s_ipg = fs32_to_cpu(sb, usb1->fs_ipg);
993 uspi->s_fpg = fs32_to_cpu(sb, usb1->fs_fpg);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -0700994 uspi->s_cpc = fs32_to_cpu(sb, usb2->fs_un.fs_u1.fs_cpc);
995 uspi->s_contigsumsize = fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_contigsumsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 uspi->s_qbmask = ufs_get_fs_qbmask(sb, usb3);
997 uspi->s_qfmask = ufs_get_fs_qfmask(sb, usb3);
998 uspi->s_postblformat = fs32_to_cpu(sb, usb3->fs_postblformat);
999 uspi->s_nrpos = fs32_to_cpu(sb, usb3->fs_nrpos);
1000 uspi->s_postbloff = fs32_to_cpu(sb, usb3->fs_postbloff);
1001 uspi->s_rotbloff = fs32_to_cpu(sb, usb3->fs_rotbloff);
1002
1003 /*
1004 * Compute another frequently used values
1005 */
1006 uspi->s_fpbmask = uspi->s_fpb - 1;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001007 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 uspi->s_apbshift = uspi->s_bshift - 3;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001009 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 uspi->s_apbshift = uspi->s_bshift - 2;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 uspi->s_2apbshift = uspi->s_apbshift * 2;
1013 uspi->s_3apbshift = uspi->s_apbshift * 3;
1014 uspi->s_apb = 1 << uspi->s_apbshift;
1015 uspi->s_2apb = 1 << uspi->s_2apbshift;
1016 uspi->s_3apb = 1 << uspi->s_3apbshift;
1017 uspi->s_apbmask = uspi->s_apb - 1;
1018 uspi->s_nspfshift = uspi->s_fshift - UFS_SECTOR_BITS;
1019 uspi->s_nspb = uspi->s_nspf << uspi->s_fpbshift;
1020 uspi->s_inopf = uspi->s_inopb >> uspi->s_fpbshift;
1021 uspi->s_bpf = uspi->s_fsize << 3;
1022 uspi->s_bpfshift = uspi->s_fshift + 3;
1023 uspi->s_bpfmask = uspi->s_bpf - 1;
1024 if ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) ==
1025 UFS_MOUNT_UFSTYPE_44BSD)
1026 uspi->s_maxsymlinklen =
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001027 fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 inode = iget(sb, UFS_ROOTINO);
1030 if (!inode || is_bad_inode(inode))
1031 goto failed;
1032 sb->s_root = d_alloc_root(inode);
1033 if (!sb->s_root)
1034 goto dalloc_failed;
1035
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001036 ufs_setup_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /*
1038 * Read cylinder group structures
1039 */
1040 if (!(sb->s_flags & MS_RDONLY))
1041 if (!ufs_read_cylinder_structures(sb))
1042 goto failed;
1043
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001044 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return 0;
1046
1047dalloc_failed:
1048 iput(inode);
1049failed:
Jesper Juhlf99d49a2005-11-07 01:01:34 -08001050 if (ubh)
1051 ubh_brelse_uspi (uspi);
1052 kfree (uspi);
1053 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 sb->s_fs_info = NULL;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001055 UFSD("EXIT (FAILED)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return -EINVAL;
1057
1058failed_nomem:
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001059 UFSD("EXIT (NOMEM)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return -ENOMEM;
1061}
1062
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001063static void ufs_write_super(struct super_block *sb)
1064{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct ufs_sb_private_info * uspi;
1066 struct ufs_super_block_first * usb1;
1067 struct ufs_super_block_third * usb3;
1068 unsigned flags;
1069
1070 lock_kernel();
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001071 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 flags = UFS_SB(sb)->s_flags;
1073 uspi = UFS_SB(sb)->s_uspi;
Evgeniy7b4ee732006-01-14 11:42:06 +03001074 usb1 = ubh_get_usb_first(uspi);
1075 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 if (!(sb->s_flags & MS_RDONLY)) {
1078 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1079 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
1080 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1081 ufs_set_fs_state(sb, usb1, usb3,
1082 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001083 ufs_put_cstotal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085 sb->s_dirt = 0;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001086 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 unlock_kernel();
1088}
1089
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001090static void ufs_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
1092 struct ufs_sb_info * sbi = UFS_SB(sb);
1093
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -07001094 UFSD("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 if (!(sb->s_flags & MS_RDONLY))
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001097 ufs_put_super_internal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 ubh_brelse_uspi (sbi->s_uspi);
1100 kfree (sbi->s_uspi);
1101 kfree (sbi);
1102 sb->s_fs_info = NULL;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001103 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return;
1105}
1106
1107
1108static int ufs_remount (struct super_block *sb, int *mount_flags, char *data)
1109{
1110 struct ufs_sb_private_info * uspi;
1111 struct ufs_super_block_first * usb1;
1112 struct ufs_super_block_third * usb3;
1113 unsigned new_mount_opt, ufstype;
1114 unsigned flags;
1115
1116 uspi = UFS_SB(sb)->s_uspi;
1117 flags = UFS_SB(sb)->s_flags;
Evgeniy7b4ee732006-01-14 11:42:06 +03001118 usb1 = ubh_get_usb_first(uspi);
1119 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 /*
1122 * Allow the "check" option to be passed as a remount option.
1123 * It is not possible to change ufstype option during remount
1124 */
1125 ufstype = UFS_SB(sb)->s_mount_opt & UFS_MOUNT_UFSTYPE;
1126 new_mount_opt = 0;
1127 ufs_set_opt (new_mount_opt, ONERROR_LOCK);
1128 if (!ufs_parse_options (data, &new_mount_opt))
1129 return -EINVAL;
1130 if (!(new_mount_opt & UFS_MOUNT_UFSTYPE)) {
1131 new_mount_opt |= ufstype;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001132 } else if ((new_mount_opt & UFS_MOUNT_UFSTYPE) != ufstype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 printk("ufstype can't be changed during remount\n");
1134 return -EINVAL;
1135 }
1136
1137 if ((*mount_flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
1138 UFS_SB(sb)->s_mount_opt = new_mount_opt;
1139 return 0;
1140 }
1141
1142 /*
1143 * fs was mouted as rw, remounting ro
1144 */
1145 if (*mount_flags & MS_RDONLY) {
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001146 ufs_put_super_internal(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 usb1->fs_time = cpu_to_fs32(sb, get_seconds());
1148 if ((flags & UFS_ST_MASK) == UFS_ST_SUN
1149 || (flags & UFS_ST_MASK) == UFS_ST_SUNx86)
1150 ufs_set_fs_state(sb, usb1, usb3,
1151 UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -07001152 ubh_mark_buffer_dirty (USPI_UBH(uspi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 sb->s_dirt = 0;
1154 sb->s_flags |= MS_RDONLY;
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001155 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /*
1157 * fs was mounted as ro, remounting rw
1158 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159#ifndef CONFIG_UFS_FS_WRITE
1160 printk("ufs was compiled with read-only support, "
1161 "can't be mounted as read-write\n");
1162 return -EINVAL;
1163#else
1164 if (ufstype != UFS_MOUNT_UFSTYPE_SUN &&
1165 ufstype != UFS_MOUNT_UFSTYPE_44BSD &&
Evgeniy Dushistovcbcae392007-02-12 00:54:30 -08001166 ufstype != UFS_MOUNT_UFSTYPE_SUNx86 &&
1167 ufstype != UFS_MOUNT_UFSTYPE_UFS2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 printk("this ufstype is read-only supported\n");
1169 return -EINVAL;
1170 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001171 if (!ufs_read_cylinder_structures(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 printk("failed during remounting\n");
1173 return -EPERM;
1174 }
1175 sb->s_flags &= ~MS_RDONLY;
1176#endif
1177 }
1178 UFS_SB(sb)->s_mount_opt = new_mount_opt;
1179 return 0;
1180}
1181
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001182static int ufs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
David Howells726c3342006-06-23 02:02:58 -07001184 struct super_block *sb = dentry->d_sb;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001185 struct ufs_sb_private_info *uspi= UFS_SB(sb)->s_uspi;
1186 unsigned flags = UFS_SB(sb)->s_flags;
1187 struct ufs_super_block_first *usb1;
1188 struct ufs_super_block_second *usb2;
1189 struct ufs_super_block_third *usb3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 lock_kernel();
1192
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001193 usb1 = ubh_get_usb_first(uspi);
1194 usb2 = ubh_get_usb_second(uspi);
1195 usb3 = ubh_get_usb_third(uspi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
1198 buf->f_type = UFS2_MAGIC;
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001199 buf->f_blocks = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
Evgeniy Dushistov647b7e82006-06-25 05:47:29 -07001200 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 buf->f_type = UFS_MAGIC;
1202 buf->f_blocks = uspi->s_dsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
Evgeniy Dushistovee3ffd62006-06-25 05:47:30 -07001204 buf->f_bfree = ufs_blkstofrags(uspi->cs_total.cs_nbfree) +
1205 uspi->cs_total.cs_nffree;
1206 buf->f_ffree = uspi->cs_total.cs_nifree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 buf->f_bsize = sb->s_blocksize;
1208 buf->f_bavail = (buf->f_bfree > (((long)buf->f_blocks / 100) * uspi->s_minfree))
1209 ? (buf->f_bfree - (((long)buf->f_blocks / 100) * uspi->s_minfree)) : 0;
1210 buf->f_files = uspi->s_ncg * uspi->s_ipg;
1211 buf->f_namelen = UFS_MAXNAMLEN;
1212
1213 unlock_kernel();
1214
1215 return 0;
1216}
1217
Christoph Lametere18b8902006-12-06 20:33:20 -08001218static struct kmem_cache * ufs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220static struct inode *ufs_alloc_inode(struct super_block *sb)
1221{
1222 struct ufs_inode_info *ei;
Christoph Lametere94b1762006-12-06 20:33:17 -08001223 ei = (struct ufs_inode_info *)kmem_cache_alloc(ufs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (!ei)
1225 return NULL;
1226 ei->vfs_inode.i_version = 1;
1227 return &ei->vfs_inode;
1228}
1229
1230static void ufs_destroy_inode(struct inode *inode)
1231{
1232 kmem_cache_free(ufs_inode_cachep, UFS_I(inode));
1233}
1234
Christoph Lametere18b8902006-12-06 20:33:20 -08001235static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 struct ufs_inode_info *ei = (struct ufs_inode_info *) foo;
1238
Christoph Lametera35afb82007-05-16 22:10:57 -07001239 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
Paul Mundt20c2df82007-07-20 10:11:58 +09001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242static int init_inodecache(void)
1243{
1244 ufs_inode_cachep = kmem_cache_create("ufs_inode_cache",
1245 sizeof(struct ufs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001246 0, (SLAB_RECLAIM_ACCOUNT|
1247 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001248 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (ufs_inode_cachep == NULL)
1250 return -ENOMEM;
1251 return 0;
1252}
1253
1254static void destroy_inodecache(void)
1255{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001256 kmem_cache_destroy(ufs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
1259#ifdef CONFIG_QUOTA
1260static ssize_t ufs_quota_read(struct super_block *, int, char *,size_t, loff_t);
1261static ssize_t ufs_quota_write(struct super_block *, int, const char *, size_t, loff_t);
1262#endif
1263
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001264static const struct super_operations ufs_super_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 .alloc_inode = ufs_alloc_inode,
1266 .destroy_inode = ufs_destroy_inode,
1267 .read_inode = ufs_read_inode,
1268 .write_inode = ufs_write_inode,
1269 .delete_inode = ufs_delete_inode,
1270 .put_super = ufs_put_super,
1271 .write_super = ufs_write_super,
1272 .statfs = ufs_statfs,
1273 .remount_fs = ufs_remount,
1274#ifdef CONFIG_QUOTA
1275 .quota_read = ufs_quota_read,
1276 .quota_write = ufs_quota_write,
1277#endif
1278};
1279
1280#ifdef CONFIG_QUOTA
1281
1282/* Read data from quotafile - avoid pagecache and such because we cannot afford
1283 * acquiring the locks... As quota files are never truncated and quota code
1284 * itself serializes the operations (and noone else should touch the files)
1285 * we don't have to be afraid of races */
1286static ssize_t ufs_quota_read(struct super_block *sb, int type, char *data,
1287 size_t len, loff_t off)
1288{
1289 struct inode *inode = sb_dqopt(sb)->files[type];
1290 sector_t blk = off >> sb->s_blocksize_bits;
1291 int err = 0;
1292 int offset = off & (sb->s_blocksize - 1);
1293 int tocopy;
1294 size_t toread;
1295 struct buffer_head *bh;
1296 loff_t i_size = i_size_read(inode);
1297
1298 if (off > i_size)
1299 return 0;
1300 if (off+len > i_size)
1301 len = i_size-off;
1302 toread = len;
1303 while (toread > 0) {
1304 tocopy = sb->s_blocksize - offset < toread ?
1305 sb->s_blocksize - offset : toread;
1306
1307 bh = ufs_bread(inode, blk, 0, &err);
1308 if (err)
1309 return err;
1310 if (!bh) /* A hole? */
1311 memset(data, 0, tocopy);
1312 else {
1313 memcpy(data, bh->b_data+offset, tocopy);
1314 brelse(bh);
1315 }
1316 offset = 0;
1317 toread -= tocopy;
1318 data += tocopy;
1319 blk++;
1320 }
1321 return len;
1322}
1323
1324/* Write to quotafile */
1325static ssize_t ufs_quota_write(struct super_block *sb, int type,
1326 const char *data, size_t len, loff_t off)
1327{
1328 struct inode *inode = sb_dqopt(sb)->files[type];
1329 sector_t blk = off >> sb->s_blocksize_bits;
1330 int err = 0;
1331 int offset = off & (sb->s_blocksize - 1);
1332 int tocopy;
1333 size_t towrite = len;
1334 struct buffer_head *bh;
1335
Arjan van de Ven5c81a412006-07-03 00:25:20 -07001336 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 while (towrite > 0) {
1338 tocopy = sb->s_blocksize - offset < towrite ?
1339 sb->s_blocksize - offset : towrite;
1340
1341 bh = ufs_bread(inode, blk, 1, &err);
1342 if (!bh)
1343 goto out;
1344 lock_buffer(bh);
1345 memcpy(bh->b_data+offset, data, tocopy);
1346 flush_dcache_page(bh->b_page);
1347 set_buffer_uptodate(bh);
1348 mark_buffer_dirty(bh);
1349 unlock_buffer(bh);
1350 brelse(bh);
1351 offset = 0;
1352 towrite -= tocopy;
1353 data += tocopy;
1354 blk++;
1355 }
1356out:
Evgeniy Polyakov15b2fe32006-01-08 01:04:51 -08001357 if (len == towrite) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001358 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return err;
Evgeniy Polyakov15b2fe32006-01-08 01:04:51 -08001360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (inode->i_size < off+len-towrite)
1362 i_size_write(inode, off+len-towrite);
1363 inode->i_version++;
1364 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
1365 mark_inode_dirty(inode);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001366 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return len - towrite;
1368}
1369
1370#endif
1371
David Howells454e2392006-06-23 02:02:57 -07001372static int ufs_get_sb(struct file_system_type *fs_type,
1373 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
David Howells454e2392006-06-23 02:02:57 -07001375 return get_sb_bdev(fs_type, flags, dev_name, data, ufs_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
1378static struct file_system_type ufs_fs_type = {
1379 .owner = THIS_MODULE,
1380 .name = "ufs",
1381 .get_sb = ufs_get_sb,
1382 .kill_sb = kill_block_super,
1383 .fs_flags = FS_REQUIRES_DEV,
1384};
1385
1386static int __init init_ufs_fs(void)
1387{
1388 int err = init_inodecache();
1389 if (err)
1390 goto out1;
1391 err = register_filesystem(&ufs_fs_type);
1392 if (err)
1393 goto out;
1394 return 0;
1395out:
1396 destroy_inodecache();
1397out1:
1398 return err;
1399}
1400
1401static void __exit exit_ufs_fs(void)
1402{
1403 unregister_filesystem(&ufs_fs_type);
1404 destroy_inodecache();
1405}
1406
1407module_init(init_ufs_fs)
1408module_exit(exit_ufs_fs)
1409MODULE_LICENSE("GPL");