blob: b30426b1fc978edf5ce15b204e4ab087a9967edb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/hpfs/super.c
3 *
4 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
5 *
6 * mounting, unmounting, error handling
7 */
8
9#include "hpfs_fn.h"
10#include <linux/module.h>
11#include <linux/parser.h>
12#include <linux/init.h>
13#include <linux/statfs.h>
Jeff Garzike18fa702006-09-24 11:13:19 -040014#include <linux/magic.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040015#include <linux/sched.h>
Alessio Igor Bogani337eb002009-05-12 15:10:54 +020016#include <linux/smp_lock.h>
Akinobu Mitaf4c54fc2009-12-15 16:46:56 -080017#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20/* Mark the filesystem dirty, so that chkdsk checks it when os/2 booted */
21
22static void mark_dirty(struct super_block *s)
23{
24 if (hpfs_sb(s)->sb_chkdsk && !(s->s_flags & MS_RDONLY)) {
25 struct buffer_head *bh;
26 struct hpfs_spare_block *sb;
27 if ((sb = hpfs_map_sector(s, 17, &bh, 0))) {
28 sb->dirty = 1;
29 sb->old_wrote = 0;
30 mark_buffer_dirty(bh);
31 brelse(bh);
32 }
33 }
34}
35
36/* Mark the filesystem clean (mark it dirty for chkdsk if chkdsk==2 or if there
37 were errors) */
38
39static void unmark_dirty(struct super_block *s)
40{
41 struct buffer_head *bh;
42 struct hpfs_spare_block *sb;
43 if (s->s_flags & MS_RDONLY) return;
44 if ((sb = hpfs_map_sector(s, 17, &bh, 0))) {
45 sb->dirty = hpfs_sb(s)->sb_chkdsk > 1 - hpfs_sb(s)->sb_was_error;
46 sb->old_wrote = hpfs_sb(s)->sb_chkdsk >= 2 && !hpfs_sb(s)->sb_was_error;
47 mark_buffer_dirty(bh);
48 brelse(bh);
49 }
50}
51
52/* Filesystem error... */
Alexey Dobriyan352d94d2006-12-06 20:37:04 -080053static char err_buf[1024];
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Alexey Dobriyan352d94d2006-12-06 20:37:04 -080055void hpfs_error(struct super_block *s, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Alexey Dobriyan352d94d2006-12-06 20:37:04 -080057 va_list args;
58
59 va_start(args, fmt);
60 vsnprintf(err_buf, sizeof(err_buf), fmt, args);
61 va_end(args);
62
63 printk("HPFS: filesystem error: %s", err_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (!hpfs_sb(s)->sb_was_error) {
65 if (hpfs_sb(s)->sb_err == 2) {
66 printk("; crashing the system because you wanted it\n");
67 mark_dirty(s);
68 panic("HPFS panic");
69 } else if (hpfs_sb(s)->sb_err == 1) {
70 if (s->s_flags & MS_RDONLY) printk("; already mounted read-only\n");
71 else {
72 printk("; remounting read-only\n");
73 mark_dirty(s);
74 s->s_flags |= MS_RDONLY;
75 }
76 } else if (s->s_flags & MS_RDONLY) printk("; going on - but anything won't be destroyed because it's read-only\n");
77 else printk("; corrupted filesystem mounted read/write - your computer will explode within 20 seconds ... but you wanted it so!\n");
78 } else printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 hpfs_sb(s)->sb_was_error = 1;
80}
81
82/*
83 * A little trick to detect cycles in many hpfs structures and don't let the
84 * kernel crash on corrupted filesystem. When first called, set c2 to 0.
85 *
86 * BTW. chkdsk doesn't detect cycles correctly. When I had 2 lost directories
87 * nested each in other, chkdsk locked up happilly.
88 */
89
90int hpfs_stop_cycles(struct super_block *s, int key, int *c1, int *c2,
91 char *msg)
92{
93 if (*c2 && *c1 == key) {
94 hpfs_error(s, "cycle detected on key %08x in %s", key, msg);
95 return 1;
96 }
97 (*c2)++;
98 if (!((*c2 - 1) & *c2)) *c1 = key;
99 return 0;
100}
101
102static void hpfs_put_super(struct super_block *s)
103{
104 struct hpfs_sb_info *sbi = hpfs_sb(s);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200105
106 lock_kernel();
107
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800108 kfree(sbi->sb_cp_table);
109 kfree(sbi->sb_bmp_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 unmark_dirty(s);
111 s->s_fs_info = NULL;
112 kfree(sbi);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200113
114 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117unsigned hpfs_count_one_bitmap(struct super_block *s, secno secno)
118{
119 struct quad_buffer_head qbh;
Akinobu Mitaf4c54fc2009-12-15 16:46:56 -0800120 unsigned long *bits;
121 unsigned count;
122
123 bits = hpfs_map_4sectors(s, secno, &qbh, 4);
124 if (!bits)
125 return 0;
126 count = bitmap_weight(bits, 2048 * BITS_PER_BYTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 hpfs_brelse4(&qbh);
128 return count;
129}
130
131static unsigned count_bitmaps(struct super_block *s)
132{
133 unsigned n, count, n_bands;
134 n_bands = (hpfs_sb(s)->sb_fs_size + 0x3fff) >> 14;
135 count = 0;
136 for (n = 0; n < n_bands; n++)
137 count += hpfs_count_one_bitmap(s, hpfs_sb(s)->sb_bmp_dir[n]);
138 return count;
139}
140
David Howells726c3342006-06-23 02:02:58 -0700141static int hpfs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
David Howells726c3342006-06-23 02:02:58 -0700143 struct super_block *s = dentry->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 struct hpfs_sb_info *sbi = hpfs_sb(s);
Coly Li604d2952009-04-02 16:59:37 -0700145 u64 id = huge_encode_dev(s->s_bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 lock_kernel();
147
148 /*if (sbi->sb_n_free == -1) {*/
149 sbi->sb_n_free = count_bitmaps(s);
150 sbi->sb_n_free_dnodes = hpfs_count_one_bitmap(s, sbi->sb_dmap);
151 /*}*/
152 buf->f_type = s->s_magic;
153 buf->f_bsize = 512;
154 buf->f_blocks = sbi->sb_fs_size;
155 buf->f_bfree = sbi->sb_n_free;
156 buf->f_bavail = sbi->sb_n_free;
157 buf->f_files = sbi->sb_dirband_size / 4;
158 buf->f_ffree = sbi->sb_n_free_dnodes;
Coly Li604d2952009-04-02 16:59:37 -0700159 buf->f_fsid.val[0] = (u32)id;
160 buf->f_fsid.val[1] = (u32)(id >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 buf->f_namelen = 254;
162
163 unlock_kernel();
164
165 return 0;
166}
167
Christoph Lametere18b8902006-12-06 20:33:20 -0800168static struct kmem_cache * hpfs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170static struct inode *hpfs_alloc_inode(struct super_block *sb)
171{
172 struct hpfs_inode_info *ei;
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800173 ei = (struct hpfs_inode_info *)kmem_cache_alloc(hpfs_inode_cachep, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (!ei)
175 return NULL;
176 ei->vfs_inode.i_version = 1;
177 return &ei->vfs_inode;
178}
179
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100180static void hpfs_i_callback(struct rcu_head *head)
181{
182 struct inode *inode = container_of(head, struct inode, i_rcu);
183 INIT_LIST_HEAD(&inode->i_dentry);
184 kmem_cache_free(hpfs_inode_cachep, hpfs_i(inode));
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static void hpfs_destroy_inode(struct inode *inode)
188{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100189 call_rcu(&inode->i_rcu, hpfs_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700192static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 struct hpfs_inode_info *ei = (struct hpfs_inode_info *) foo;
195
Christoph Lametera35afb82007-05-16 22:10:57 -0700196 mutex_init(&ei->i_mutex);
197 mutex_init(&ei->i_parent_mutex);
198 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
Paul Mundt20c2df82007-07-20 10:11:58 +0900200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int init_inodecache(void)
202{
203 hpfs_inode_cachep = kmem_cache_create("hpfs_inode_cache",
204 sizeof(struct hpfs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800205 0, (SLAB_RECLAIM_ACCOUNT|
206 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900207 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (hpfs_inode_cachep == NULL)
209 return -ENOMEM;
210 return 0;
211}
212
213static void destroy_inodecache(void)
214{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700215 kmem_cache_destroy(hpfs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218/*
219 * A tiny parser for option strings, stolen from dosfs.
220 * Stolen again from read-only hpfs.
221 * And updated for table-driven option parsing.
222 */
223
224enum {
225 Opt_help, Opt_uid, Opt_gid, Opt_umask, Opt_case_lower, Opt_case_asis,
226 Opt_conv_binary, Opt_conv_text, Opt_conv_auto,
227 Opt_check_none, Opt_check_normal, Opt_check_strict,
228 Opt_err_cont, Opt_err_ro, Opt_err_panic,
229 Opt_eas_no, Opt_eas_ro, Opt_eas_rw,
230 Opt_chkdsk_no, Opt_chkdsk_errors, Opt_chkdsk_always,
231 Opt_timeshift, Opt_err,
232};
233
Steven Whitehousea447c092008-10-13 10:46:57 +0100234static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 {Opt_help, "help"},
236 {Opt_uid, "uid=%u"},
237 {Opt_gid, "gid=%u"},
238 {Opt_umask, "umask=%o"},
239 {Opt_case_lower, "case=lower"},
240 {Opt_case_asis, "case=asis"},
241 {Opt_conv_binary, "conv=binary"},
242 {Opt_conv_text, "conv=text"},
243 {Opt_conv_auto, "conv=auto"},
244 {Opt_check_none, "check=none"},
245 {Opt_check_normal, "check=normal"},
246 {Opt_check_strict, "check=strict"},
247 {Opt_err_cont, "errors=continue"},
248 {Opt_err_ro, "errors=remount-ro"},
249 {Opt_err_panic, "errors=panic"},
250 {Opt_eas_no, "eas=no"},
251 {Opt_eas_ro, "eas=ro"},
252 {Opt_eas_rw, "eas=rw"},
253 {Opt_chkdsk_no, "chkdsk=no"},
254 {Opt_chkdsk_errors, "chkdsk=errors"},
255 {Opt_chkdsk_always, "chkdsk=always"},
256 {Opt_timeshift, "timeshift=%d"},
257 {Opt_err, NULL},
258};
259
260static int parse_opts(char *opts, uid_t *uid, gid_t *gid, umode_t *umask,
261 int *lowercase, int *conv, int *eas, int *chk, int *errs,
262 int *chkdsk, int *timeshift)
263{
264 char *p;
265 int option;
266
267 if (!opts)
268 return 1;
269
270 /*printk("Parsing opts: '%s'\n",opts);*/
271
272 while ((p = strsep(&opts, ",")) != NULL) {
273 substring_t args[MAX_OPT_ARGS];
274 int token;
275 if (!*p)
276 continue;
277
278 token = match_token(p, tokens, args);
279 switch (token) {
280 case Opt_help:
281 return 2;
282 case Opt_uid:
283 if (match_int(args, &option))
284 return 0;
285 *uid = option;
286 break;
287 case Opt_gid:
288 if (match_int(args, &option))
289 return 0;
290 *gid = option;
291 break;
292 case Opt_umask:
293 if (match_octal(args, &option))
294 return 0;
295 *umask = option;
296 break;
297 case Opt_case_lower:
298 *lowercase = 1;
299 break;
300 case Opt_case_asis:
301 *lowercase = 0;
302 break;
303 case Opt_conv_binary:
304 *conv = CONV_BINARY;
305 break;
306 case Opt_conv_text:
307 *conv = CONV_TEXT;
308 break;
309 case Opt_conv_auto:
310 *conv = CONV_AUTO;
311 break;
312 case Opt_check_none:
313 *chk = 0;
314 break;
315 case Opt_check_normal:
316 *chk = 1;
317 break;
318 case Opt_check_strict:
319 *chk = 2;
320 break;
321 case Opt_err_cont:
322 *errs = 0;
323 break;
324 case Opt_err_ro:
325 *errs = 1;
326 break;
327 case Opt_err_panic:
328 *errs = 2;
329 break;
330 case Opt_eas_no:
331 *eas = 0;
332 break;
333 case Opt_eas_ro:
334 *eas = 1;
335 break;
336 case Opt_eas_rw:
337 *eas = 2;
338 break;
339 case Opt_chkdsk_no:
340 *chkdsk = 0;
341 break;
342 case Opt_chkdsk_errors:
343 *chkdsk = 1;
344 break;
345 case Opt_chkdsk_always:
346 *chkdsk = 2;
347 break;
348 case Opt_timeshift:
349 {
350 int m = 1;
351 char *rhs = args[0].from;
352 if (!rhs || !*rhs)
353 return 0;
354 if (*rhs == '-') m = -1;
355 if (*rhs == '+' || *rhs == '-') rhs++;
356 *timeshift = simple_strtoul(rhs, &rhs, 0) * m;
357 if (*rhs)
358 return 0;
359 break;
360 }
361 default:
362 return 0;
363 }
364 }
365 return 1;
366}
367
368static inline void hpfs_help(void)
369{
370 printk("\n\
371HPFS filesystem options:\n\
372 help do not mount and display this text\n\
373 uid=xxx set uid of files that don't have uid specified in eas\n\
374 gid=xxx set gid of files that don't have gid specified in eas\n\
375 umask=xxx set mode of files that don't have mode specified in eas\n\
376 case=lower lowercase all files\n\
377 case=asis do not lowercase files (default)\n\
378 conv=binary do not convert CR/LF -> LF (default)\n\
379 conv=auto convert only files with known text extensions\n\
380 conv=text convert all files\n\
381 check=none no fs checks - kernel may crash on corrupted filesystem\n\
382 check=normal do some checks - it should not crash (default)\n\
383 check=strict do extra time-consuming checks, used for debugging\n\
384 errors=continue continue on errors\n\
385 errors=remount-ro remount read-only if errors found (default)\n\
386 errors=panic panic on errors\n\
387 chkdsk=no do not mark fs for chkdsking even if there were errors\n\
388 chkdsk=errors mark fs dirty if errors found (default)\n\
389 chkdsk=always always mark fs dirty - used for debugging\n\
390 eas=no ignore extended attributes\n\
391 eas=ro read but do not write extended attributes\n\
392 eas=rw r/w eas => enables chmod, chown, mknod, ln -s (default)\n\
393 timeshift=nnn add nnn seconds to file times\n\
394\n");
395}
396
397static int hpfs_remount_fs(struct super_block *s, int *flags, char *data)
398{
399 uid_t uid;
400 gid_t gid;
401 umode_t umask;
402 int lowercase, conv, eas, chk, errs, chkdsk, timeshift;
403 int o;
404 struct hpfs_sb_info *sbi = hpfs_sb(s);
Miklos Szeredi6d9c1fd2008-02-08 04:21:44 -0800405 char *new_opts = kstrdup(data, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 *flags |= MS_NOATIME;
408
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200409 lock_kernel();
Al Virobbd68512009-05-06 10:43:07 -0400410 lock_super(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 uid = sbi->sb_uid; gid = sbi->sb_gid;
412 umask = 0777 & ~sbi->sb_mode;
413 lowercase = sbi->sb_lowercase; conv = sbi->sb_conv;
414 eas = sbi->sb_eas; chk = sbi->sb_chk; chkdsk = sbi->sb_chkdsk;
415 errs = sbi->sb_err; timeshift = sbi->sb_timeshift;
416
417 if (!(o = parse_opts(data, &uid, &gid, &umask, &lowercase, &conv,
418 &eas, &chk, &errs, &chkdsk, &timeshift))) {
419 printk("HPFS: bad mount options.\n");
Miklos Szeredi6d9c1fd2008-02-08 04:21:44 -0800420 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422 if (o == 2) {
423 hpfs_help();
Miklos Szeredi6d9c1fd2008-02-08 04:21:44 -0800424 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 if (timeshift != sbi->sb_timeshift) {
427 printk("HPFS: timeshift can't be changed using remount.\n");
Miklos Szeredi6d9c1fd2008-02-08 04:21:44 -0800428 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
431 unmark_dirty(s);
432
433 sbi->sb_uid = uid; sbi->sb_gid = gid;
434 sbi->sb_mode = 0777 & ~umask;
435 sbi->sb_lowercase = lowercase; sbi->sb_conv = conv;
436 sbi->sb_eas = eas; sbi->sb_chk = chk; sbi->sb_chkdsk = chkdsk;
437 sbi->sb_err = errs; sbi->sb_timeshift = timeshift;
438
439 if (!(*flags & MS_RDONLY)) mark_dirty(s);
440
Al Viro2a32ceb2009-05-08 16:05:57 -0400441 replace_mount_options(s, new_opts);
Miklos Szeredi6d9c1fd2008-02-08 04:21:44 -0800442
Al Virobbd68512009-05-06 10:43:07 -0400443 unlock_super(s);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200444 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return 0;
Miklos Szeredi6d9c1fd2008-02-08 04:21:44 -0800446
447out_err:
Al Virobbd68512009-05-06 10:43:07 -0400448 unlock_super(s);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200449 unlock_kernel();
Miklos Szeredi6d9c1fd2008-02-08 04:21:44 -0800450 kfree(new_opts);
451 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454/* Super operations */
455
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800456static const struct super_operations hpfs_sops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 .alloc_inode = hpfs_alloc_inode,
459 .destroy_inode = hpfs_destroy_inode,
Al Viroea544002010-06-07 00:18:40 -0400460 .evict_inode = hpfs_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 .put_super = hpfs_put_super,
462 .statfs = hpfs_statfs,
463 .remount_fs = hpfs_remount_fs,
Miklos Szeredi6d9c1fd2008-02-08 04:21:44 -0800464 .show_options = generic_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465};
466
467static int hpfs_fill_super(struct super_block *s, void *options, int silent)
468{
469 struct buffer_head *bh0, *bh1, *bh2;
470 struct hpfs_boot_block *bootblock;
471 struct hpfs_super_block *superblock;
472 struct hpfs_spare_block *spareblock;
473 struct hpfs_sb_info *sbi;
474 struct inode *root;
475
476 uid_t uid;
477 gid_t gid;
478 umode_t umask;
479 int lowercase, conv, eas, chk, errs, chkdsk, timeshift;
480
481 dnode_secno root_dno;
482 struct hpfs_dirent *de = NULL;
483 struct quad_buffer_head qbh;
484
485 int o;
486
Jan Blunckdb719222010-08-15 22:51:10 +0200487 lock_kernel();
488
Miklos Szeredi6d9c1fd2008-02-08 04:21:44 -0800489 save_mount_options(s, options);
490
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700491 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Jan Blunckdb719222010-08-15 22:51:10 +0200492 if (!sbi) {
493 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return -ENOMEM;
Jan Blunckdb719222010-08-15 22:51:10 +0200495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 s->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 sbi->sb_bmp_dir = NULL;
499 sbi->sb_cp_table = NULL;
500
Thomas Gleixner117bf5f2010-09-07 14:32:56 +0000501 mutex_init(&sbi->hpfs_creation_de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
David Howellsde395b82008-11-14 10:38:55 +1100503 uid = current_uid();
504 gid = current_gid();
Al Viroce3b0f82009-03-29 19:08:22 -0400505 umask = current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 lowercase = 0;
507 conv = CONV_BINARY;
508 eas = 2;
509 chk = 1;
510 errs = 1;
511 chkdsk = 1;
512 timeshift = 0;
513
514 if (!(o = parse_opts(options, &uid, &gid, &umask, &lowercase, &conv,
515 &eas, &chk, &errs, &chkdsk, &timeshift))) {
516 printk("HPFS: bad mount options.\n");
517 goto bail0;
518 }
519 if (o==2) {
520 hpfs_help();
521 goto bail0;
522 }
523
524 /*sbi->sb_mounting = 1;*/
525 sb_set_blocksize(s, 512);
526 sbi->sb_fs_size = -1;
527 if (!(bootblock = hpfs_map_sector(s, 0, &bh0, 0))) goto bail1;
528 if (!(superblock = hpfs_map_sector(s, 16, &bh1, 1))) goto bail2;
529 if (!(spareblock = hpfs_map_sector(s, 17, &bh2, 0))) goto bail3;
530
531 /* Check magics */
532 if (/*bootblock->magic != BB_MAGIC
533 ||*/ superblock->magic != SB_MAGIC
534 || spareblock->magic != SP_MAGIC) {
535 if (!silent) printk("HPFS: Bad magic ... probably not HPFS\n");
536 goto bail4;
537 }
538
539 /* Check version */
540 if (!(s->s_flags & MS_RDONLY) &&
541 superblock->funcversion != 2 && superblock->funcversion != 3) {
542 printk("HPFS: Bad version %d,%d. Mount readonly to go around\n",
543 (int)superblock->version, (int)superblock->funcversion);
544 printk("HPFS: please try recent version of HPFS driver at http://artax.karlin.mff.cuni.cz/~mikulas/vyplody/hpfs/index-e.cgi and if it still can't understand this format, contact author - mikulas@artax.karlin.mff.cuni.cz\n");
545 goto bail4;
546 }
547
548 s->s_flags |= MS_NOATIME;
549
550 /* Fill superblock stuff */
551 s->s_magic = HPFS_SUPER_MAGIC;
552 s->s_op = &hpfs_sops;
Al Viro43d344d2011-01-12 16:12:05 -0500553 s->s_d_op = &hpfs_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 sbi->sb_root = superblock->root;
556 sbi->sb_fs_size = superblock->n_sectors;
557 sbi->sb_bitmaps = superblock->bitmaps;
558 sbi->sb_dirband_start = superblock->dir_band_start;
559 sbi->sb_dirband_size = superblock->n_dir_band;
560 sbi->sb_dmap = superblock->dir_band_bitmap;
561 sbi->sb_uid = uid;
562 sbi->sb_gid = gid;
563 sbi->sb_mode = 0777 & ~umask;
564 sbi->sb_n_free = -1;
565 sbi->sb_n_free_dnodes = -1;
566 sbi->sb_lowercase = lowercase;
567 sbi->sb_conv = conv;
568 sbi->sb_eas = eas;
569 sbi->sb_chk = chk;
570 sbi->sb_chkdsk = chkdsk;
571 sbi->sb_err = errs;
572 sbi->sb_timeshift = timeshift;
573 sbi->sb_was_error = 0;
574 sbi->sb_cp_table = NULL;
575 sbi->sb_c_bitmap = -1;
576 sbi->sb_max_fwd_alloc = 0xffffff;
577
578 /* Load bitmap directory */
579 if (!(sbi->sb_bmp_dir = hpfs_load_bitmap_directory(s, superblock->bitmaps)))
580 goto bail4;
581
582 /* Check for general fs errors*/
583 if (spareblock->dirty && !spareblock->old_wrote) {
584 if (errs == 2) {
585 printk("HPFS: Improperly stopped, not mounted\n");
586 goto bail4;
587 }
588 hpfs_error(s, "improperly stopped");
589 }
590
591 if (!(s->s_flags & MS_RDONLY)) {
592 spareblock->dirty = 1;
593 spareblock->old_wrote = 0;
594 mark_buffer_dirty(bh2);
595 }
596
597 if (spareblock->hotfixes_used || spareblock->n_spares_used) {
598 if (errs >= 2) {
599 printk("HPFS: Hotfixes not supported here, try chkdsk\n");
600 mark_dirty(s);
601 goto bail4;
602 }
603 hpfs_error(s, "hotfixes not supported here, try chkdsk");
604 if (errs == 0) printk("HPFS: Proceeding, but your filesystem will be probably corrupted by this driver...\n");
605 else printk("HPFS: This driver may read bad files or crash when operating on disk with hotfixes.\n");
606 }
607 if (spareblock->n_dnode_spares != spareblock->n_dnode_spares_free) {
608 if (errs >= 2) {
609 printk("HPFS: Spare dnodes used, try chkdsk\n");
610 mark_dirty(s);
611 goto bail4;
612 }
613 hpfs_error(s, "warning: spare dnodes used, try chkdsk");
614 if (errs == 0) printk("HPFS: Proceeding, but your filesystem could be corrupted if you delete files or directories\n");
615 }
616 if (chk) {
617 unsigned a;
618 if (superblock->dir_band_end - superblock->dir_band_start + 1 != superblock->n_dir_band ||
619 superblock->dir_band_end < superblock->dir_band_start || superblock->n_dir_band > 0x4000) {
620 hpfs_error(s, "dir band size mismatch: dir_band_start==%08x, dir_band_end==%08x, n_dir_band==%08x",
621 superblock->dir_band_start, superblock->dir_band_end, superblock->n_dir_band);
622 goto bail4;
623 }
624 a = sbi->sb_dirband_size;
625 sbi->sb_dirband_size = 0;
626 if (hpfs_chk_sectors(s, superblock->dir_band_start, superblock->n_dir_band, "dir_band") ||
627 hpfs_chk_sectors(s, superblock->dir_band_bitmap, 4, "dir_band_bitmap") ||
628 hpfs_chk_sectors(s, superblock->bitmaps, 4, "bitmaps")) {
629 mark_dirty(s);
630 goto bail4;
631 }
632 sbi->sb_dirband_size = a;
633 } else printk("HPFS: You really don't want any checks? You are crazy...\n");
634
635 /* Load code page table */
636 if (spareblock->n_code_pages)
637 if (!(sbi->sb_cp_table = hpfs_load_code_page(s, spareblock->code_page_dir)))
638 printk("HPFS: Warning: code page support is disabled\n");
639
640 brelse(bh2);
641 brelse(bh1);
642 brelse(bh0);
643
644 root = iget_locked(s, sbi->sb_root);
645 if (!root)
646 goto bail0;
647 hpfs_init_inode(root);
648 hpfs_read_inode(root);
649 unlock_new_inode(root);
650 s->s_root = d_alloc_root(root);
651 if (!s->s_root) {
652 iput(root);
653 goto bail0;
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /*
657 * find the root directory's . pointer & finish filling in the inode
658 */
659
660 root_dno = hpfs_fnode_dno(s, sbi->sb_root);
661 if (root_dno)
662 de = map_dirent(root, root_dno, "\001\001", 2, NULL, &qbh);
663 if (!de)
664 hpfs_error(s, "unable to find root dir");
665 else {
666 root->i_atime.tv_sec = local_to_gmt(s, de->read_date);
667 root->i_atime.tv_nsec = 0;
668 root->i_mtime.tv_sec = local_to_gmt(s, de->write_date);
669 root->i_mtime.tv_nsec = 0;
670 root->i_ctime.tv_sec = local_to_gmt(s, de->creation_date);
671 root->i_ctime.tv_nsec = 0;
672 hpfs_i(root)->i_ea_size = de->ea_size;
673 hpfs_i(root)->i_parent_dir = root->i_ino;
674 if (root->i_size == -1)
675 root->i_size = 2048;
676 if (root->i_blocks == -1)
677 root->i_blocks = 5;
678 hpfs_brelse4(&qbh);
679 }
Jan Blunckdb719222010-08-15 22:51:10 +0200680 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return 0;
682
683bail4: brelse(bh2);
684bail3: brelse(bh1);
685bail2: brelse(bh0);
686bail1:
687bail0:
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800688 kfree(sbi->sb_bmp_dir);
689 kfree(sbi->sb_cp_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 s->s_fs_info = NULL;
691 kfree(sbi);
Jan Blunckdb719222010-08-15 22:51:10 +0200692 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -EINVAL;
694}
695
Al Viro152a0832010-07-25 00:46:55 +0400696static struct dentry *hpfs_mount(struct file_system_type *fs_type,
697 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Al Viro152a0832010-07-25 00:46:55 +0400699 return mount_bdev(fs_type, flags, dev_name, data, hpfs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702static struct file_system_type hpfs_fs_type = {
703 .owner = THIS_MODULE,
704 .name = "hpfs",
Al Viro152a0832010-07-25 00:46:55 +0400705 .mount = hpfs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 .kill_sb = kill_block_super,
707 .fs_flags = FS_REQUIRES_DEV,
708};
709
710static int __init init_hpfs_fs(void)
711{
712 int err = init_inodecache();
713 if (err)
714 goto out1;
715 err = register_filesystem(&hpfs_fs_type);
716 if (err)
717 goto out;
718 return 0;
719out:
720 destroy_inodecache();
721out1:
722 return err;
723}
724
725static void __exit exit_hpfs_fs(void)
726{
727 unregister_filesystem(&hpfs_fs_type);
728 destroy_inodecache();
729}
730
731module_init(init_hpfs_fs)
732module_exit(exit_hpfs_fs)
733MODULE_LICENSE("GPL");