super.c 49.5 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2
/*
 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
Linus Torvalds's avatar
Linus Torvalds committed
3 4 5 6 7 8 9 10 11
 *
 * Trivial changes by Alan Cox to add the LFS fixes
 *
 * Trivial Changes:
 * Rights granted to Hans Reiser to redistribute under other terms providing
 * he accepts all liability including but not limited to patent, fitness
 * for purpose, and direct or indirect claims arising from failure to perform.
 *
 * NO WARRANTY
Linus Torvalds's avatar
Linus Torvalds committed
12 13
 */

Linus Torvalds's avatar
Linus Torvalds committed
14
#include <linux/config.h>
Linus Torvalds's avatar
Linus Torvalds committed
15
#include <linux/module.h>
16
#include <linux/vmalloc.h>
17
#include <linux/time.h>
Linus Torvalds's avatar
Linus Torvalds committed
18 19
#include <asm/uaccess.h>
#include <linux/reiserfs_fs.h>
20
#include <linux/reiserfs_xattr.h>
Linus Torvalds's avatar
Linus Torvalds committed
21 22
#include <linux/smp_lock.h>
#include <linux/init.h>
Linus Torvalds's avatar
Linus Torvalds committed
23
#include <linux/blkdev.h>
24
#include <linux/buffer_head.h>
25
#include <linux/vfs.h>
Linus Torvalds's avatar
Linus Torvalds committed
26

27
struct file_system_type reiserfs_fs_type;
28

Linus Torvalds's avatar
Linus Torvalds committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
const char reiserfs_3_5_magic_string[] = REISERFS_SUPER_MAGIC_STRING;
const char reiserfs_3_6_magic_string[] = REISER2FS_SUPER_MAGIC_STRING;
const char reiserfs_jr_magic_string[] = REISER2FS_JR_SUPER_MAGIC_STRING;

int is_reiserfs_3_5 (struct reiserfs_super_block * rs)
{
  return !strncmp (rs->s_v1.s_magic, reiserfs_3_5_magic_string,
		   strlen (reiserfs_3_5_magic_string));
}


int is_reiserfs_3_6 (struct reiserfs_super_block * rs)
{
  return !strncmp (rs->s_v1.s_magic, reiserfs_3_6_magic_string,
 		   strlen (reiserfs_3_6_magic_string));
}


int is_reiserfs_jr (struct reiserfs_super_block * rs)
{
  return !strncmp (rs->s_v1.s_magic, reiserfs_jr_magic_string,
 		   strlen (reiserfs_jr_magic_string));
}


static int is_any_reiserfs_magic_string (struct reiserfs_super_block * rs)
{
  return (is_reiserfs_3_5 (rs) || is_reiserfs_3_6 (rs) ||
	  is_reiserfs_jr (rs));
}

static int reiserfs_remount (struct super_block * s, int * flags, char * data);
61
static int reiserfs_statfs (struct super_block * s, struct kstatfs * buf);
Linus Torvalds's avatar
Linus Torvalds committed
62

63
static void reiserfs_sync_fs (struct super_block * s)
Linus Torvalds's avatar
Linus Torvalds committed
64
{
65 66 67 68 69 70 71 72 73 74
    if (!(s->s_flags & MS_RDONLY)) {
        struct reiserfs_transaction_handle th;
	reiserfs_write_lock(s);
	journal_begin(&th, s, 1);
	journal_end_sync(&th, s, 1);
	reiserfs_flush_old_commits(s);
	s->s_dirt = 0;
	reiserfs_write_unlock(s);
    }
}
Linus Torvalds's avatar
Linus Torvalds committed
75

76 77 78
static void reiserfs_write_super(struct super_block *s)
{
    reiserfs_sync_fs(s);
Linus Torvalds's avatar
Linus Torvalds committed
79 80
}

Linus Torvalds's avatar
Linus Torvalds committed
81
static void reiserfs_write_super_lockfs (struct super_block * s)
Linus Torvalds's avatar
Linus Torvalds committed
82 83
{
  struct reiserfs_transaction_handle th ;
84
  reiserfs_write_lock(s);
Linus Torvalds's avatar
Linus Torvalds committed
85 86
  if (!(s->s_flags & MS_RDONLY)) {
    journal_begin(&th, s, 1) ;
Linus Torvalds's avatar
Linus Torvalds committed
87
    reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
Linus Torvalds's avatar
Linus Torvalds committed
88 89
    journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
    reiserfs_block_writes(&th) ;
Andrew Morton's avatar
Andrew Morton committed
90
    journal_end_sync(&th, s, 1) ;
Linus Torvalds's avatar
Linus Torvalds committed
91
  }
92
  s->s_dirt = 0;
93
  reiserfs_write_unlock(s);
Linus Torvalds's avatar
Linus Torvalds committed
94 95 96 97 98 99
}

void reiserfs_unlockfs(struct super_block *s) {
  reiserfs_allow_writes(s) ;
}

Linus Torvalds's avatar
Linus Torvalds committed
100 101 102 103 104 105 106 107 108 109
extern const struct key  MAX_KEY;


/* this is used to delete "save link" when there are no items of a
   file it points to. It can either happen if unlink is completed but
   "save unlink" removal, or if file has both unlink and truncate
   pending and as unlink completes first (because key of "save link"
   protecting unlink is bigger that a key lf "save link" which
   protects truncate), so there left no items to make truncate
   completion on */
110
static void remove_save_link_only (struct super_block * s, struct key * key, int oid_free)
Linus Torvalds's avatar
Linus Torvalds committed
111 112 113 114 115 116 117
{
    struct reiserfs_transaction_handle th;

     /* we are going to do one balancing */
     journal_begin (&th, s, JOURNAL_PER_BALANCE_CNT);
 
     reiserfs_delete_solid_item (&th, key);
118
     if (oid_free)
Linus Torvalds's avatar
Linus Torvalds committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        /* removals are protected by direct items */
        reiserfs_release_objectid (&th, le32_to_cpu (key->k_objectid));

     journal_end (&th, s, JOURNAL_PER_BALANCE_CNT);
}
 
 
/* look for uncompleted unlinks and truncates and complete them */
static void finish_unfinished (struct super_block * s)
{
    INITIALIZE_PATH (path);
    struct cpu_key max_cpu_key, obj_key;
    struct key save_link_key;
    int retval;
    struct item_head * ih;
    struct buffer_head * bh;
    int item_pos;
    char * item;
    int done;
    struct inode * inode;
    int truncate;
 
 
    /* compose key to look for "save" links */
    max_cpu_key.version = KEY_FORMAT_3_5;
    max_cpu_key.on_disk_key = MAX_KEY;
    max_cpu_key.key_length = 3;
 
    done = 0;
148
    REISERFS_SB(s)->s_is_unlinked_ok = 1;
Linus Torvalds's avatar
Linus Torvalds committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    while (1) {
        retval = search_item (s, &max_cpu_key, &path);
        if (retval != ITEM_NOT_FOUND) {
            reiserfs_warning ("vs-2140: finish_unfinished: search_by_key returned %d\n",
                              retval);
            break;
        }
        
        bh = get_last_bh (&path);
        item_pos = get_item_pos (&path);
        if (item_pos != B_NR_ITEMS (bh)) {
            reiserfs_warning ("vs-2060: finish_unfinished: wrong position found\n");
            break;
        }
        item_pos --;
        ih = B_N_PITEM_HEAD (bh, item_pos);
 
        if (le32_to_cpu (ih->ih_key.k_dir_id) != MAX_KEY_OBJECTID)
            /* there are no "save" links anymore */
            break;
 
        save_link_key = ih->ih_key;
        if (is_indirect_le_ih (ih))
            truncate = 1;
        else
            truncate = 0;
 
        /* reiserfs_iget needs k_dirid and k_objectid only */
        item = B_I_PITEM (bh, ih);
        obj_key.on_disk_key.k_dir_id = le32_to_cpu (*(__u32 *)item);
        obj_key.on_disk_key.k_objectid = le32_to_cpu (ih->ih_key.k_objectid);
	obj_key.on_disk_key.u.k_offset_v1.k_offset = 0;
	obj_key.on_disk_key.u.k_offset_v1.k_uniqueness = 0;
	
        pathrelse (&path);
 
        inode = reiserfs_iget (s, &obj_key);
        if (!inode) {
            /* the unlink almost completed, it just did not manage to remove
	       "save" link and release objectid */
            reiserfs_warning ("vs-2180: finish_unfinished: iget failed for %K\n",
                              &obj_key);
191
            remove_save_link_only (s, &save_link_key, 1);
Linus Torvalds's avatar
Linus Torvalds committed
192 193 194 195 196 197 198
            continue;
        }

	if (!truncate && inode->i_nlink) {
	    /* file is not unlinked */
            reiserfs_warning ("vs-2185: finish_unfinished: file %K is not unlinked\n",
                              &obj_key);
199
            remove_save_link_only (s, &save_link_key, 0);
Linus Torvalds's avatar
Linus Torvalds committed
200 201
            continue;
	}
202 203 204 205 206 207 208 209 210 211 212 213

	if (truncate && S_ISDIR (inode->i_mode) ) {
	    /* We got a truncate request for a dir which is impossible.
	       The only imaginable way is to execute unfinished truncate request
	       then boot into old kernel, remove the file and create dir with
	       the same key. */
	    reiserfs_warning("green-2101: impossible truncate on a directory %k. Please report\n", INODE_PKEY (inode));
	    remove_save_link_only (s, &save_link_key, 0);
	    truncate = 0;
	    iput (inode); 
	    continue;
	}
Linus Torvalds's avatar
Linus Torvalds committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
 
        if (truncate) {
            REISERFS_I(inode) -> i_flags |= i_link_saved_truncate_mask;
            /* not completed truncate found. New size was committed together
	       with "save" link */
            reiserfs_warning ("Truncating %k to %Ld ..",
                              INODE_PKEY (inode), inode->i_size);
            reiserfs_truncate_file (inode, 0/*don't update modification time*/);
            remove_save_link (inode, truncate);
        } else {
            REISERFS_I(inode) -> i_flags |= i_link_saved_unlink_mask;
            /* not completed unlink (rmdir) found */
            reiserfs_warning ("Removing %k..", INODE_PKEY (inode));
            /* removal gets completed in iput */
        }
 
        iput (inode);
        printk ("done\n");
        done ++;
    }
234
    REISERFS_SB(s)->s_is_unlinked_ok = 0;
Linus Torvalds's avatar
Linus Torvalds committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
     
    pathrelse (&path);
    if (done)
        reiserfs_warning ("There were %d uncompleted unlinks/truncates. "
                          "Completed\n", done);
}
 
/* to protect file being unlinked from getting lost we "safe" link files
   being unlinked. This link will be deleted in the same transaction with last
   item of file. mounting the filesytem we scan all these links and remove
   files which almost got lost */
void add_save_link (struct reiserfs_transaction_handle * th,
		    struct inode * inode, int truncate)
{
    INITIALIZE_PATH (path);
    int retval;
    struct cpu_key key;
    struct item_head ih;
    __u32 link;

    /* file can only get one "save link" of each kind */
    RFALSE( truncate && 
	    ( REISERFS_I(inode) -> i_flags & i_link_saved_truncate_mask ),
	    "saved link already exists for truncated inode %lx",
	    ( long ) inode -> i_ino );
    RFALSE( !truncate && 
	    ( REISERFS_I(inode) -> i_flags & i_link_saved_unlink_mask ),
	    "saved link already exists for unlinked inode %lx",
	    ( long ) inode -> i_ino );

    /* setup key of "save" link */
    key.version = KEY_FORMAT_3_5;
    key.on_disk_key.k_dir_id = MAX_KEY_OBJECTID;
    key.on_disk_key.k_objectid = inode->i_ino;
    if (!truncate) {
	/* unlink, rmdir, rename */
	set_cpu_key_k_offset (&key, 1 + inode->i_sb->s_blocksize);
	set_cpu_key_k_type (&key, TYPE_DIRECT);

	/* item head of "safe" link */
	make_le_item_head (&ih, &key, key.version, 1 + inode->i_sb->s_blocksize, TYPE_DIRECT,
			   4/*length*/, 0xffff/*free space*/);
    } else {
	/* truncate */
279 280
	if (S_ISDIR (inode->i_mode))
	    reiserfs_warning("green-2102: Adding a truncate savelink for a directory %k! Please report\n", INODE_PKEY(inode));
Linus Torvalds's avatar
Linus Torvalds committed
281 282 283 284 285 286 287 288 289 290 291 292
	set_cpu_key_k_offset (&key, 1);
	set_cpu_key_k_type (&key, TYPE_INDIRECT);

	/* item head of "safe" link */
	make_le_item_head (&ih, &key, key.version, 1, TYPE_INDIRECT,
			   4/*length*/, 0/*free space*/);
    }
    key.key_length = 3;

    /* look for its place in the tree */
    retval = search_item (inode->i_sb, &key, &path);
    if (retval != ITEM_NOT_FOUND) {
293 294
	if ( retval != -ENOSPC )
	    reiserfs_warning ("vs-2100: add_save_link:"
Linus Torvalds's avatar
Linus Torvalds committed
295 296 297 298 299 300
			  "search_by_key (%K) returned %d\n", &key, retval);
	pathrelse (&path);
	return;
    }

    /* body of "save" link */
301
    link = INODE_PKEY (inode)->k_dir_id;
Linus Torvalds's avatar
Linus Torvalds committed
302 303 304

    /* put "save" link inot tree */
    retval = reiserfs_insert_item (th, &path, &key, &ih, (char *)&link);
305 306 307
    if (retval) {
	if (retval != -ENOSPC)
	    reiserfs_warning ("vs-2120: add_save_link: insert_item returned %d\n",
Linus Torvalds's avatar
Linus Torvalds committed
308
			  retval);
309
    } else {
Linus Torvalds's avatar
Linus Torvalds committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	if( truncate )
	    REISERFS_I(inode) -> i_flags |= i_link_saved_truncate_mask;
	else
	    REISERFS_I(inode) -> i_flags |= i_link_saved_unlink_mask;
    }
}


/* this opens transaction unlike add_save_link */
void remove_save_link (struct inode * inode, int truncate)
{
    struct reiserfs_transaction_handle th;
    struct key key;
 
 
    /* we are going to do one balancing only */
    journal_begin (&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
 
    /* setup key of "save" link */
    key.k_dir_id = cpu_to_le32 (MAX_KEY_OBJECTID);
    key.k_objectid = INODE_PKEY (inode)->k_objectid;
    if (!truncate) {
        /* unlink, rmdir, rename */
        set_le_key_k_offset (KEY_FORMAT_3_5, &key,
			     1 + inode->i_sb->s_blocksize);
        set_le_key_k_type (KEY_FORMAT_3_5, &key, TYPE_DIRECT);
    } else {
        /* truncate */
        set_le_key_k_offset (KEY_FORMAT_3_5, &key, 1);
        set_le_key_k_type (KEY_FORMAT_3_5, &key, TYPE_INDIRECT);
    }
 
    if( ( truncate && 
          ( REISERFS_I(inode) -> i_flags & i_link_saved_truncate_mask ) ) ||
        ( !truncate && 
          ( REISERFS_I(inode) -> i_flags & i_link_saved_unlink_mask ) ) )
	reiserfs_delete_solid_item (&th, &key);
    if (!truncate) {
	reiserfs_release_objectid (&th, inode->i_ino);
	REISERFS_I(inode) -> i_flags &= ~i_link_saved_unlink_mask;
    } else
	REISERFS_I(inode) -> i_flags &= ~i_link_saved_truncate_mask;
 
    journal_end (&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
}


static void reiserfs_put_super (struct super_block * s)
Linus Torvalds's avatar
Linus Torvalds committed
358 359 360
{
  int i;
  struct reiserfs_transaction_handle th ;
361 362 363 364 365

  if (REISERFS_SB(s)->xattr_root) {
    d_invalidate (REISERFS_SB(s)->xattr_root);
    dput (REISERFS_SB(s)->xattr_root);
  }
Linus Torvalds's avatar
Linus Torvalds committed
366
  
367 368 369 370 371
  if (REISERFS_SB(s)->priv_root) {
    d_invalidate (REISERFS_SB(s)->priv_root);
    dput (REISERFS_SB(s)->priv_root);
  }

Linus Torvalds's avatar
Linus Torvalds committed
372 373 374 375
  /* change file system state to current state if it was mounted with read-write permissions */
  if (!(s->s_flags & MS_RDONLY)) {
    journal_begin(&th, s, 10) ;
    reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
376
    set_sb_umount_state( SB_DISK_SUPER_BLOCK(s), REISERFS_SB(s)->s_mount_state );
Linus Torvalds's avatar
Linus Torvalds committed
377 378 379 380 381 382 383 384 385
    journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
  }

  /* note, journal_release checks for readonly mount, and can decide not
  ** to do a journal_end
  */
  journal_release(&th, s) ;

  for (i = 0; i < SB_BMAP_NR (s); i ++)
386
    brelse (SB_AP_BITMAP (s)[i].bh);
Linus Torvalds's avatar
Linus Torvalds committed
387

388
  vfree (SB_AP_BITMAP (s));
Linus Torvalds's avatar
Linus Torvalds committed
389 390 391 392 393

  brelse (SB_BUFFER_WITH_SB (s));

  print_statistics (s);

394
  if (REISERFS_SB(s)->s_kmallocs != 0) {
Linus Torvalds's avatar
Linus Torvalds committed
395
    reiserfs_warning ("vs-2004: reiserfs_put_super: allocated memory left %d\n",
396
		      REISERFS_SB(s)->s_kmallocs);
Linus Torvalds's avatar
Linus Torvalds committed
397 398
  }

399 400 401 402 403
  if (REISERFS_SB(s)->reserved_blocks != 0) {
    reiserfs_warning ("green-2005: reiserfs_put_super: reserved blocks left %d\n",
		      REISERFS_SB(s)->reserved_blocks);
  }

Linus Torvalds's avatar
Linus Torvalds committed
404
  reiserfs_proc_info_done( s );
405

406 407
  kfree(s->s_fs_info);
  s->s_fs_info = NULL;
408

Linus Torvalds's avatar
Linus Torvalds committed
409 410 411
  return;
}

Linus Torvalds's avatar
Linus Torvalds committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
static kmem_cache_t * reiserfs_inode_cachep;

static struct inode *reiserfs_alloc_inode(struct super_block *sb)
{
	struct reiserfs_inode_info *ei;
	ei = (struct reiserfs_inode_info *)kmem_cache_alloc(reiserfs_inode_cachep, SLAB_KERNEL);
	if (!ei)
		return NULL;
	return &ei->vfs_inode;
}

static void reiserfs_destroy_inode(struct inode *inode)
{
	kmem_cache_free(reiserfs_inode_cachep, REISERFS_I(inode));
}

static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
{
	struct reiserfs_inode_info *ei = (struct reiserfs_inode_info *) foo;

	if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
	    SLAB_CTOR_CONSTRUCTOR) {
		INIT_LIST_HEAD(&ei->i_prealloc_list) ;
		inode_init_once(&ei->vfs_inode);
	}
}
 
static int init_inodecache(void)
{
Linus Torvalds's avatar
Linus Torvalds committed
441
	reiserfs_inode_cachep = kmem_cache_create("reiser_inode_cache",
Linus Torvalds's avatar
Linus Torvalds committed
442
					     sizeof(struct reiserfs_inode_info),
443
					     0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
Linus Torvalds's avatar
Linus Torvalds committed
444 445 446 447 448 449 450 451 452 453 454 455
					     init_once, NULL);
	if (reiserfs_inode_cachep == NULL)
		return -ENOMEM;
	return 0;
}

static void destroy_inodecache(void)
{
	if (kmem_cache_destroy(reiserfs_inode_cachep))
		printk(KERN_INFO "reiserfs_inode_cache: not all structures were freed\n");
}

Linus Torvalds's avatar
Linus Torvalds committed
456 457 458 459 460 461 462 463 464
/* we don't mark inodes dirty, we just log them */
static void reiserfs_dirty_inode (struct inode * inode) {
    struct reiserfs_transaction_handle th ;

    if (inode->i_sb->s_flags & MS_RDONLY) {
        reiserfs_warning("clm-6006: writing inode %lu on readonly FS\n", 
	                  inode->i_ino) ;
        return ;
    }
465
    reiserfs_write_lock(inode->i_sb);
Linus Torvalds's avatar
Linus Torvalds committed
466 467 468 469 470 471 472

    /* this is really only used for atime updates, so they don't have
    ** to be included in O_SYNC or fsync
    */
    journal_begin(&th, inode->i_sb, 1) ;
    reiserfs_update_sd (&th, inode);
    journal_end(&th, inode->i_sb, 1) ;
473
    reiserfs_write_unlock(inode->i_sb);
Linus Torvalds's avatar
Linus Torvalds committed
474 475
}

Linus Torvalds's avatar
Linus Torvalds committed
476 477
struct super_operations reiserfs_sops = 
{
478 479 480 481 482 483 484 485 486 487 488
  .alloc_inode = reiserfs_alloc_inode,
  .destroy_inode = reiserfs_destroy_inode,
  .write_inode = reiserfs_write_inode,
  .dirty_inode = reiserfs_dirty_inode,
  .delete_inode = reiserfs_delete_inode,
  .put_super = reiserfs_put_super,
  .write_super = reiserfs_write_super,
  .write_super_lockfs = reiserfs_write_super_lockfs,
  .unlockfs = reiserfs_unlockfs,
  .statfs = reiserfs_statfs,
  .remount_fs = reiserfs_remount,
Linus Torvalds's avatar
Linus Torvalds committed
489 490 491

};

492
static struct export_operations reiserfs_export_ops = {
493 494 495 496
  .encode_fh = reiserfs_encode_fh,
  .decode_fh = reiserfs_decode_fh,
  .get_parent = reiserfs_get_parent,
  .get_dentry = reiserfs_get_dentry,
497 498
} ;

499 500 501
/* this struct is used in reiserfs_getopt () for containing the value for those
   mount options that have values rather than being toggles. */
typedef struct {
Linus Torvalds's avatar
Linus Torvalds committed
502
    char * value;
503 504 505 506 507
    int setmask; /* bitmask which is to set on mount_options bitmask when this
                    value is found, 0 is no bits are to be changed. */
    int clrmask; /* bitmask which is to clear on mount_options bitmask when  this
		    value is found, 0 is no bits are to be changed. This is
		    applied BEFORE setmask */
508 509 510 511 512 513 514 515 516
} arg_desc_t;


/* this struct is used in reiserfs_getopt() for describing the set of reiserfs
   mount options */
typedef struct {
    char * option_name;
    int arg_required; /* 0 if argument is not required, not 0 otherwise */
    const arg_desc_t * values; /* list of values accepted by an option */
517 518 519 520 521
    int setmask; /* bitmask which is to set on mount_options bitmask when this
                    value is found, 0 is no bits are to be changed. */
    int clrmask; /* bitmask which is to clear on mount_options bitmask when  this
		    value is found, 0 is no bits are to be changed. This is
		    applied BEFORE setmask */
522 523
} opt_desc_t;

524 525 526 527 528 529 530 531
/* possible values for -o data= */
static const arg_desc_t logging_mode[] = {
    {"ordered", 1<<REISERFS_DATA_ORDERED, (1<<REISERFS_DATA_LOG|1<<REISERFS_DATA_WRITEBACK)},
    {"journal", 1<<REISERFS_DATA_LOG, (1<<REISERFS_DATA_ORDERED|1<<REISERFS_DATA_WRITEBACK)},
    {"writeback", 1<<REISERFS_DATA_WRITEBACK, (1<<REISERFS_DATA_ORDERED|1<<REISERFS_DATA_LOG)},
    {NULL, 0}
};

532 533
/* possible values for "-o block-allocator=" and bits which are to be set in
   s_mount_opt of reiserfs specific part of in-core super block */
534
static const arg_desc_t balloc[] = {
535 536 537 538 539 540 541
    {"noborder", 1<<REISERFS_NO_BORDER, 0},
    {"border", 0, 1<<REISERFS_NO_BORDER},
    {"no_unhashed_relocation", 1<<REISERFS_NO_UNHASHED_RELOCATION, 0},
    {"hashed_relocation", 1<<REISERFS_HASHED_RELOCATION, 0},
    {"test4", 1<<REISERFS_TEST4, 0},
    {"notest4", 0, 1<<REISERFS_TEST4},
    {NULL, 0, 0}
542 543
};

544
static const arg_desc_t tails[] = {
545 546 547 548
    {"on", 1<<REISERFS_LARGETAIL, 1<<REISERFS_SMALLTAIL},
    {"off", 0, (1<<REISERFS_LARGETAIL)|(1<<REISERFS_SMALLTAIL)},
    {"small", 1<<REISERFS_SMALLTAIL, 1<<REISERFS_LARGETAIL},
    {NULL, 0, 0}
549 550
};

551 552 553 554 555
int reiserfs_default_io_size = 128 * 1024; /* Default recommended I/O size is 128k.
					      There might be broken applications that are
					      confused by this. Use nolargeio mount option
					      to get usual i/o size = PAGE_SIZE.
					    */
556 557 558 559 560 561 562

/* proceed only one option from a list *cur - string containing of mount options
   opts - array of options which are accepted
   opt_arg - if option is found and requires an argument and if it is specifed
   in the input - pointer to the argument is stored here
   bit_flags - if option requires to set a certain bit - it is set here
   return -1 if unknown option is found, opt->arg_required otherwise */
563
static int reiserfs_getopt ( struct super_block * s, char ** cur, opt_desc_t * opts, char ** opt_arg,
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
			    unsigned long * bit_flags)
{
    char * p;
    /* foo=bar, 
       ^   ^  ^
       |   |  +-- option_end
       |   +-- arg_start
       +-- option_start
    */
    const opt_desc_t * opt;
    const arg_desc_t * arg;
    
    
    p = *cur;
    
    /* assume argument cannot contain commas */
    *cur = strchr (p, ',');
    if (*cur) {
	*(*cur) = '\0';
	(*cur) ++;
    }
585 586 587 588 589

    if ( !strncmp (p, "alloc=", 6) ) {
	/* Ugly special case, probably we should redo options parser so that
	   it can understand several arguments for some options, also so that
	   it can fill several bitfields with option values. */
590 591 592 593 594
	if ( reiserfs_parse_alloc_options( s, p + 6) ) {
	    return -1;
	} else {
	    return 0;
	}
595 596 597
    }

 
598 599 600
    /* for every option in the list */
    for (opt = opts; opt->option_name; opt ++) {
	if (!strncmp (p, opt->option_name, strlen (opt->option_name))) {
601 602 603 604
	    if (bit_flags) {
		*bit_flags &= ~opt->clrmask;
		*bit_flags |= opt->setmask;
	    }
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
	    break;
	}
    }
    if (!opt->option_name) {
	printk ("reiserfs_getopt: unknown option \"%s\"\n", p);
	return -1;
    }
    
    p += strlen (opt->option_name);
    switch (*p) {
    case '=':
	if (!opt->arg_required) {
	    printk ("reiserfs_getopt: the option \"%s\" does not require an argument\n",
		    opt->option_name);
	    return -1;
	}
	break;
	
    case 0:
	if (opt->arg_required) {
	    printk ("reiserfs_getopt: the option \"%s\" requires an argument\n", opt->option_name);
	    return -1;
	}
	break;
    default:
	printk ("reiserfs_getopt: head of option \"%s\" is only correct\n", opt->option_name);
	return -1;
    }
	
    /* move to the argument, or to next option if argument is not required */
    p ++;
    
    if ( opt->arg_required && !strlen (p) ) {
	/* this catches "option=," */
	printk ("reiserfs_getopt: empty argument for \"%s\"\n", opt->option_name);
	return -1;
    }
    
    if (!opt->values) {
644
	/* *=NULLopt_arg contains pointer to argument */
645 646 647 648 649 650 651
	*opt_arg = p;
	return opt->arg_required;
    }
    
    /* values possible for this option are listed in opt->values */
    for (arg = opt->values; arg->value; arg ++) {
	if (!strcmp (p, arg->value)) {
652 653 654 655
	    if (bit_flags) {
		*bit_flags &= ~arg->clrmask;
		*bit_flags |= arg->setmask;
	    }
656 657 658 659 660 661 662 663 664
	    return opt->arg_required;
	}
    }
    
    printk ("reiserfs_getopt: bad value \"%s\" for option \"%s\"\n", p, opt->option_name);
    return -1;
}

/* returns 0 if something is wrong in option string, 1 - otherwise */
665
static int reiserfs_parse_options (struct super_block * s, char * options, /* string given via mount's -o */
666 667 668 669 670
				   unsigned long * mount_options,
				   /* after the parsing phase, contains the
				      collection of bitflags defining what
				      mount options were selected. */
				   unsigned long * blocks, /* strtol-ed from NNN of resize=NNN */
671 672
				   char ** jdev_name,
				   unsigned int * commit_max_age)
673 674 675 676 677
{
    int c;
    char * arg = NULL;
    char * pos;
    opt_desc_t opts[] = {
678 679 680 681 682
	{"tails", 't', tails, 0, 0}, /* Compatibility stuff, so that -o notail for old setups still work */
	{"notail", 0, 0, 0,  (1<<REISERFS_LARGETAIL)|(1<<REISERFS_SMALLTAIL)},
	{"conv", 0, 0, 1<<REISERFS_CONVERT, 0},
	{"attrs", 0, 0, 1<<REISERFS_ATTRS, 0},
	{"noattrs", 0, 0, 0, 1<<REISERFS_ATTRS},
683 684
	{"user_xattr", 0, 0, 1<<REISERFS_XATTRS_USER, 0},
	{"nouser_xattr", 0, 0, 0, 1<<REISERFS_XATTRS_USER},
685 686 687
	{"nolog", 0, 0, 0, 0}, /* This is unsupported */
	{"replayonly", 0, 0, 1<<REPLAYONLY, 0},
	{"block-allocator", 'a', balloc, 0, 0},
688
	{"data", 'd', logging_mode, 0, 0},
689 690 691
	{"resize", 'r', 0, 0, 0},
	{"jdev", 'j', 0, 0, 0},
	{"nolargeio", 'w', 0, 0, 0},
692
	{"commit", 'c', 0, 0, 0},
693
	{NULL, 0, 0, 0, 0}
694 695
    };
	
Linus Torvalds's avatar
Linus Torvalds committed
696
    *blocks = 0;
697
    if (!options || !*options)
Linus Torvalds's avatar
Linus Torvalds committed
698
	/* use default configuration: create tails, journaling on, no
699
	   conversion to newest format */
Linus Torvalds's avatar
Linus Torvalds committed
700
	return 1;
701 702
    
    for (pos = options; pos; ) {
703
	c = reiserfs_getopt (s, &pos, opts, &arg, mount_options);
704 705 706 707 708 709 710 711 712 713 714 715 716
	if (c == -1)
	    /* wrong option is given */
	    return 0;
	
	if (c == 'r') {
	    char * p;
	    
	    p = 0;
	    /* "resize=NNN" */
	    *blocks = simple_strtoul (arg, &p, 0);
	    if (*p != '\0') {
		/* NNN does not look like a number */
		printk ("reiserfs_parse_options: bad value %s\n", arg);
Linus Torvalds's avatar
Linus Torvalds committed
717 718
		return 0;
	    }
719 720
	}

721 722
	if ( c == 'c' ) {
		char *p = 0;
723
		unsigned long val = simple_strtoul (arg, &p, 0);
724
		/* commit=NNN (time in seconds) */
725
		if ( *p != '\0' || val >= (unsigned int)-1) {
726 727 728
			printk ("reiserfs_parse_options: bad value %s\n", arg);
			return 0;
		}
729
		*commit_max_age = (unsigned int)val;
730 731
	}

732
	if ( c == 'w' ) {
733 734 735 736 737 738 739 740 741 742 743
		char *p=0;
		int val = simple_strtoul (arg, &p, 0);

		if ( *p != '\0') {
		    printk ("reiserfs_parse_options: non-numeric value %s for nolargeio option\n", arg);
		    return 0;
		}
		if ( val ) 
		    reiserfs_default_io_size = PAGE_SIZE;
		else
		    reiserfs_default_io_size = 128 * 1024;
744 745
	}

746 747
	if (c == 'j') {
	    if (arg && *arg && jdev_name) {
748 749 750 751
		if ( *jdev_name ) { //Hm, already assigned?
		    printk("reiserfs_parse_options: journal device was already  specified to be %s\n", *jdev_name);
		    return 0;
		}
752
		*jdev_name = arg;
Linus Torvalds's avatar
Linus Torvalds committed
753
	    }
Linus Torvalds's avatar
Linus Torvalds committed
754 755
	}
    }
756
    
Linus Torvalds's avatar
Linus Torvalds committed
757 758 759
    return 1;
}

760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
static void switch_data_mode(struct super_block *s, unsigned long mode) {
    REISERFS_SB(s)->s_mount_opt &= ~((1 << REISERFS_DATA_LOG) |
                                       (1 << REISERFS_DATA_ORDERED) |
				       (1 << REISERFS_DATA_WRITEBACK));
    REISERFS_SB(s)->s_mount_opt |= (1 << mode);
}

static void handle_data_mode(struct super_block *s, unsigned long mount_options)
{
    if (mount_options & (1 << REISERFS_DATA_LOG)) {
        if (!reiserfs_data_log(s)) {
	    switch_data_mode(s, REISERFS_DATA_LOG);
	    printk("reiserfs: switching to journaled data mode\n");
	}
    } else if (mount_options & (1 << REISERFS_DATA_ORDERED)) {
        if (!reiserfs_data_ordered(s)) {
	    switch_data_mode(s, REISERFS_DATA_ORDERED);
	    printk("reiserfs: switching to ordered data mode\n");
	}
    } else if (mount_options & (1 << REISERFS_DATA_WRITEBACK)) {
        if (!reiserfs_data_writeback(s)) {
	    switch_data_mode(s, REISERFS_DATA_WRITEBACK);
	    printk("reiserfs: switching to writeback data mode\n");
	}
    }
}

787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
static void handle_attrs( struct super_block *s )
{
	struct reiserfs_super_block * rs;

	if( reiserfs_attrs( s ) ) {
		rs = SB_DISK_SUPER_BLOCK (s);
		if( old_format_only(s) ) {
			reiserfs_warning( "reiserfs: cannot support attributes on 3.5.x disk format\n" );
			REISERFS_SB(s) -> s_mount_opt &= ~ ( 1 << REISERFS_ATTRS );
			return;
		}
		if( !( le32_to_cpu( rs -> s_flags ) & reiserfs_attrs_cleared ) ) {
				reiserfs_warning( "reiserfs: cannot support attributes until flag is set in super-block\n" );
				REISERFS_SB(s) -> s_mount_opt &= ~ ( 1 << REISERFS_ATTRS );
		}
	}
}

805
static int reiserfs_remount (struct super_block * s, int * mount_flags, char * arg)
Linus Torvalds's avatar
Linus Torvalds committed
806 807 808 809
{
  struct reiserfs_super_block * rs;
  struct reiserfs_transaction_handle th ;
  unsigned long blocks;
810 811
  unsigned long mount_options = REISERFS_SB(s)->s_mount_opt;
  unsigned long safe_mask = 0;
812
  unsigned int commit_max_age = (unsigned int)-1;
Linus Torvalds's avatar
Linus Torvalds committed
813 814 815

  rs = SB_DISK_SUPER_BLOCK (s);

816
  if (!reiserfs_parse_options(s, arg, &mount_options, &blocks, NULL, &commit_max_age))
817 818
    return -EINVAL;
  
819 820 821 822 823 824 825 826 827 828
  handle_attrs(s);

  /* Add options that are safe here */
  safe_mask |= 1 << REISERFS_SMALLTAIL;
  safe_mask |= 1 << REISERFS_LARGETAIL;
  safe_mask |= 1 << REISERFS_NO_BORDER;
  safe_mask |= 1 << REISERFS_NO_UNHASHED_RELOCATION;
  safe_mask |= 1 << REISERFS_HASHED_RELOCATION;
  safe_mask |= 1 << REISERFS_TEST4;
  safe_mask |= 1 << REISERFS_ATTRS;
829
  safe_mask |= 1 << REISERFS_XATTRS_USER;
830 831 832 833

  /* Update the bitmask, taking care to keep
   * the bits we're not allowed to change here */
  REISERFS_SB(s)->s_mount_opt = (REISERFS_SB(s)->s_mount_opt & ~safe_mask) |  (mount_options & safe_mask);
834

835
  if(commit_max_age != 0 && commit_max_age != (unsigned int)-1) {
836 837 838
    SB_JOURNAL_MAX_COMMIT_AGE(s) = commit_max_age;
    SB_JOURNAL_MAX_TRANS_AGE(s) = commit_max_age;
  }
839
  else if(commit_max_age == 0)
840 841 842 843
  {
    /* 0 means restore defaults. */
    SB_JOURNAL_MAX_COMMIT_AGE(s) = SB_JOURNAL_DEFAULT_MAX_COMMIT_AGE(s);
    SB_JOURNAL_MAX_TRANS_AGE(s) = JOURNAL_MAX_TRANS_AGE;
844 845
  }

Linus Torvalds's avatar
Linus Torvalds committed
846
  if(blocks) {
847 848 849
    int rc = reiserfs_resize(s, blocks);
    if (rc != 0)
      return rc;
Linus Torvalds's avatar
Linus Torvalds committed
850 851
  }

852
  if (*mount_flags & MS_RDONLY) {
853
    reiserfs_xattr_init (s, *mount_flags);
854
    /* remount read-only */
855 856 857
    if (s->s_flags & MS_RDONLY)
      /* it is read-only already */
      return 0;
Linus Torvalds's avatar
Linus Torvalds committed
858
    /* try to remount file system with read-only permissions */
859
    if (sb_umount_state(rs) == REISERFS_VALID_FS || REISERFS_SB(s)->s_mount_state != REISERFS_VALID_FS) {
Linus Torvalds's avatar
Linus Torvalds committed
860 861 862 863 864 865
      return 0;
    }

    journal_begin(&th, s, 10) ;
    /* Mounting a rw partition read-only. */
    reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
866
    set_sb_umount_state( rs, REISERFS_SB(s)->s_mount_state );
Linus Torvalds's avatar
Linus Torvalds committed
867 868
    journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
  } else {
869
    /* remount read-write */
870 871
    if (!(s->s_flags & MS_RDONLY)) {
	reiserfs_xattr_init (s, *mount_flags);
872
	return 0; /* We are read-write already */
873
    }
874

875
    handle_data_mode(s, mount_options);
876
    REISERFS_SB(s)->s_mount_state = sb_umount_state(rs) ;
Linus Torvalds's avatar
Linus Torvalds committed
877 878
    s->s_flags &= ~MS_RDONLY ; /* now it is safe to call journal_begin */
    journal_begin(&th, s, 10) ;
879
    
Linus Torvalds's avatar
Linus Torvalds committed
880 881
    /* Mount a partition which is read-only, read-write */
    reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
882
    REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
Linus Torvalds's avatar
Linus Torvalds committed
883
    s->s_flags &= ~MS_RDONLY;
Linus Torvalds's avatar
Linus Torvalds committed
884
    set_sb_umount_state( rs, REISERFS_ERROR_FS );
Linus Torvalds's avatar
Linus Torvalds committed
885 886
    /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
    journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
887
    REISERFS_SB(s)->s_mount_state = REISERFS_VALID_FS ;
Linus Torvalds's avatar
Linus Torvalds committed
888 889 890 891
  }
  /* this will force a full flush of all journal lists */
  SB_JOURNAL(s)->j_must_wait = 1 ;
  journal_end(&th, s, 10) ;
892
  s->s_dirt = 0;
Linus Torvalds's avatar
Linus Torvalds committed
893

894
  if (!( *mount_flags & MS_RDONLY ) ) {
Linus Torvalds's avatar
Linus Torvalds committed
895
    finish_unfinished( s );
896 897
    reiserfs_xattr_init (s, *mount_flags);
  }
Linus Torvalds's avatar
Linus Torvalds committed
898

Linus Torvalds's avatar
Linus Torvalds committed
899 900 901
  return 0;
}

902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
/* load_bitmap_info_data - Sets up the reiserfs_bitmap_info structure from disk.
 * @sb - superblock for this filesystem
 * @bi - the bitmap info to be loaded. Requires that bi->bh is valid.
 *
 * This routine counts how many free bits there are, finding the first zero
 * as a side effect. Could also be implemented as a loop of test_bit() calls, or
 * a loop of find_first_zero_bit() calls. This implementation is similar to
 * find_first_zero_bit(), but doesn't return after it finds the first bit.
 * Should only be called on fs mount, but should be fairly efficient anyways.
 *
 * bi->first_zero_hint is considered unset if it == 0, since the bitmap itself
 * will * invariably occupt block 0 represented in the bitmap. The only
 * exception to this is when free_count also == 0, since there will be no
 * free blocks at all.
 */

static void load_bitmap_info_data (struct super_block *sb,
                                   struct reiserfs_bitmap_info *bi)
{
    unsigned long *cur = (unsigned long *)bi->bh->b_data;

    while ((char *)cur < (bi->bh->b_data + sb->s_blocksize)) {

	/* No need to scan if all 0's or all 1's.
	 * Since we're only counting 0's, we can simply ignore all 1's */
	if (*cur == 0) {
	    if (bi->first_zero_hint == 0) {
		bi->first_zero_hint = ((char *)cur - bi->bh->b_data) << 3;
	    }
	    bi->free_count += sizeof(unsigned long)*8;
	} else if (*cur != ~0L) {
	    int b;
	    for (b = 0; b < sizeof(unsigned long)*8; b++) {
		if (!reiserfs_test_le_bit (b, cur)) {
		    bi->free_count ++;
		    if (bi->first_zero_hint == 0)
			bi->first_zero_hint =
					(((char *)cur - bi->bh->b_data) << 3) + b;
		    }
		}
	    }
	cur ++;
    }
Linus Torvalds's avatar
Linus Torvalds committed
945

946 947 948 949 950 951 952
#ifdef CONFIG_REISERFS_CHECK
// This outputs a lot of unneded info on big FSes
//    reiserfs_warning ("bitmap loaded from block %d: %d free blocks\n",
//		      bi->bh->b_blocknr, bi->free_count);
#endif
}
  
Linus Torvalds's avatar
Linus Torvalds committed
953 954
static int read_bitmaps (struct super_block * s)
{
Oleg Drokin's avatar
Oleg Drokin committed
955
    int i, bmap_nr;
Linus Torvalds's avatar
Linus Torvalds committed
956

957
    SB_AP_BITMAP (s) = vmalloc (sizeof (struct reiserfs_bitmap_info) * SB_BMAP_NR(s));
Linus Torvalds's avatar
Linus Torvalds committed
958 959
    if (SB_AP_BITMAP (s) == 0)
	return 1;
960
    memset (SB_AP_BITMAP (s), 0, sizeof (struct reiserfs_bitmap_info) * SB_BMAP_NR(s));
Oleg Drokin's avatar
Oleg Drokin committed
961 962
    for (i = 0, bmap_nr = REISERFS_DISK_OFFSET_IN_BYTES / s->s_blocksize + 1;
	 i < SB_BMAP_NR(s); i++, bmap_nr = s->s_blocksize * 8 * i) {
963 964 965
	SB_AP_BITMAP (s)[i].bh = sb_getblk(s, bmap_nr);
	if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh))
	    ll_rw_block(READ, 1, &SB_AP_BITMAP(s)[i].bh);
966 967
    }
    for (i = 0; i < SB_BMAP_NR(s); i++) {
968 969
	wait_on_buffer(SB_AP_BITMAP (s)[i].bh);
	if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) {
970 971
	    reiserfs_warning("sh-2029: reiserfs read_bitmaps: "
			 "bitmap block (#%lu) reading failed\n",
972
			 SB_AP_BITMAP(s)[i].bh->b_blocknr);
973
	    for (i = 0; i < SB_BMAP_NR(s); i++)
974 975
		brelse(SB_AP_BITMAP(s)[i].bh);
	    vfree(SB_AP_BITMAP(s));
976
	    SB_AP_BITMAP(s) = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
977
	    return 1;
978
	}
979
	load_bitmap_info_data (s, SB_AP_BITMAP (s) + i);
Linus Torvalds's avatar
Linus Torvalds committed
980 981 982 983 984 985 986 987 988 989 990
    }
    return 0;
}

static int read_old_bitmaps (struct super_block * s)
{
  int i ;
  struct reiserfs_super_block * rs = SB_DISK_SUPER_BLOCK(s);
  int bmp1 = (REISERFS_OLD_DISK_OFFSET_IN_BYTES / s->s_blocksize) + 1;  /* first of bitmap blocks */

  /* read true bitmap */
991
  SB_AP_BITMAP (s) = vmalloc (sizeof (struct reiserfs_buffer_info *) * sb_bmap_nr(rs));
Linus Torvalds's avatar
Linus Torvalds committed
992 993 994
  if (SB_AP_BITMAP (s) == 0)
    return 1;

995
  memset (SB_AP_BITMAP (s), 0, sizeof (struct reiserfs_buffer_info *) * sb_bmap_nr(rs));
Linus Torvalds's avatar
Linus Torvalds committed
996

Linus Torvalds's avatar
Linus Torvalds committed
997
  for (i = 0; i < sb_bmap_nr(rs); i ++) {
998 999
    SB_AP_BITMAP (s)[i].bh = sb_bread (s, bmp1 + i);
    if (!SB_AP_BITMAP (s)[i].bh)
Linus Torvalds's avatar
Linus Torvalds committed
1000
      return 1;
1001
    load_bitmap_info_data (s, SB_AP_BITMAP (s) + i);
Linus Torvalds's avatar
Linus Torvalds committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
  }

  return 0;
}

void check_bitmap (struct super_block * s)
{
  int i = 0;
  int free = 0;
  char * buf;

  while (i < SB_BLOCK_COUNT (s)) {
1014
    buf = SB_AP_BITMAP (s)[i / (s->s_blocksize * 8)].bh->b_data;
Linus Torvalds's avatar
Linus Torvalds committed
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
    if (!reiserfs_test_le_bit (i % (s->s_blocksize * 8), buf))
      free ++;
    i ++;
  }

  if (free != SB_FREE_BLOCKS (s))
    reiserfs_warning ("vs-4000: check_bitmap: %d free blocks, must be %d\n",
		      free, SB_FREE_BLOCKS (s));
}

Linus Torvalds's avatar
Linus Torvalds committed
1025
static int read_super_block (struct super_block * s, int offset)
Linus Torvalds's avatar
Linus Torvalds committed
1026 1027 1028
{
    struct buffer_head * bh;
    struct reiserfs_super_block * rs;
1029
    int fs_blocksize;
Linus Torvalds's avatar
Linus Torvalds committed
1030
 
Linus Torvalds's avatar
Linus Torvalds committed
1031

Linus Torvalds's avatar
Linus Torvalds committed
1032
    bh = sb_bread (s, offset / s->s_blocksize);
Linus Torvalds's avatar
Linus Torvalds committed
1033
    if (!bh) {
Linus Torvalds's avatar
Linus Torvalds committed
1034 1035
      printk ("sh-2006: read_super_block: "
              "bread failed (dev %s, block %lu, size %lu)\n",
Oleg Drokin's avatar
Oleg Drokin committed
1036
              reiserfs_bdevname (s), offset / s->s_blocksize, s->s_blocksize);
Linus Torvalds's avatar
Linus Torvalds committed
1037
      return 1;
Linus Torvalds's avatar
Linus Torvalds committed
1038
    }
Linus Torvalds's avatar
Linus Torvalds committed
1039
 
Linus Torvalds's avatar
Linus Torvalds committed
1040
    rs = (struct reiserfs_super_block *)bh->b_data;
Linus Torvalds's avatar
Linus Torvalds committed
1041
    if (!is_any_reiserfs_magic_string (rs)) {
Linus Torvalds's avatar
Linus Torvalds committed
1042 1043
      brelse (bh);
      return 1;
Linus Torvalds's avatar
Linus Torvalds committed
1044
    }
Linus Torvalds's avatar
Linus Torvalds committed
1045
 
Linus Torvalds's avatar
Linus Torvalds committed
1046
    //
Linus Torvalds's avatar
Linus Torvalds committed
1047
    // ok, reiserfs signature (old or new) found in at the given offset
Linus Torvalds's avatar
Linus Torvalds committed
1048
    //    
1049
    fs_blocksize = sb_blocksize(rs);
Hans Reiser's avatar
Hans Reiser committed
1050
    brelse (bh);
1051
    sb_set_blocksize (s, fs_blocksize);
Linus Torvalds's avatar
Linus Torvalds committed
1052
    
1053
    bh = sb_bread (s, offset / s->s_blocksize);
Linus Torvalds's avatar
Linus Torvalds committed
1054
    if (!bh) {
Linus Torvalds's avatar
Linus Torvalds committed
1055 1056
	printk("sh-2007: read_super_block: "
                "bread failed (dev %s, block %lu, size %lu)\n",
Oleg Drokin's avatar
Oleg Drokin committed
1057
                reiserfs_bdevname (s), offset / s->s_blocksize, s->s_blocksize);
Linus Torvalds's avatar
Linus Torvalds committed
1058 1059 1060 1061
	return 1;
    }
    
    rs = (struct reiserfs_super_block *)bh->b_data;
Linus Torvalds's avatar
Linus Torvalds committed
1062 1063
    if (sb_blocksize(rs) != s->s_blocksize) {
	printk ("sh-2011: read_super_block: "
1064 1065
		"can't find a reiserfs filesystem on (dev %s, block %Lu, size %lu)\n",
		reiserfs_bdevname (s), (unsigned long long)bh->b_blocknr, s->s_blocksize);
Linus Torvalds's avatar
Linus Torvalds committed
1066 1067 1068
	brelse (bh);
	return 1;
    }
Linus Torvalds's avatar
Linus Torvalds committed
1069

1070 1071 1072
    if ( rs->s_v1.s_root_block == -1 ) {
       brelse(bh) ;
       printk("dev %s: Unfinished reiserfsck --rebuild-tree run detected. Please run\n"
Hans Reiser's avatar
Hans Reiser committed
1073
              "reiserfsck --rebuild-tree and wait for a completion. If that fails\n"
Oleg Drokin's avatar
Oleg Drokin committed
1074
              "get newer reiserfsprogs package\n", reiserfs_bdevname (s));
1075 1076 1077
       return 1;
    }

Linus Torvalds's avatar
Linus Torvalds committed
1078 1079
    SB_BUFFER_WITH_SB (s) = bh;
    SB_DISK_SUPER_BLOCK (s) = rs;
Linus Torvalds's avatar
Linus Torvalds committed
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101

    if (is_reiserfs_jr (rs)) {
	/* magic is of non-standard journal filesystem, look at s_version to
	   find which format is in use */
	if (sb_version(rs) == REISERFS_VERSION_2)
	  printk ("read_super_block: found reiserfs format \"3.6\" "
		  "with non-standard journal\n");
	else if (sb_version(rs) == REISERFS_VERSION_1)
	  printk ("read_super_block: found reiserfs format \"3.5\" "
		  "with non-standard journal\n");
	else {
	  printk ("sh-2012: read_super_block: found unknown format \"%u\" "
	            "of reiserfs with non-standard magic\n", sb_version(rs));
	return 1;
	}
    }
    else
      /* s_version of standard format may contain incorrect information,
	 so we just look at the magic string */
      printk ("found reiserfs format \"%s\" with standard journal\n",
	      is_reiserfs_3_5 (rs) ? "3.5" : "3.6");

Linus Torvalds's avatar
Linus Torvalds committed
1102
    s->s_op = &reiserfs_sops;
1103
    s->s_export_op = &reiserfs_export_ops;
Linus Torvalds's avatar
Linus Torvalds committed
1104 1105 1106 1107 1108

    /* new format is limited by the 32 bit wide i_blocks field, want to
    ** be one full block below that.
    */
    s->s_maxbytes = (512LL << 32) - s->s_blocksize ;
Linus Torvalds's avatar
Linus Torvalds committed
1109 1110 1111
    return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1112 1113


Linus Torvalds's avatar
Linus Torvalds committed
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
/* after journal replay, reread all bitmap and super blocks */
static int reread_meta_blocks(struct super_block *s) {
  int i ;
  ll_rw_block(READ, 1, &(SB_BUFFER_WITH_SB(s))) ;
  wait_on_buffer(SB_BUFFER_WITH_SB(s)) ;
  if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
    printk("reread_meta_blocks, error reading the super\n") ;
    return 1 ;
  }

  for (i = 0; i < SB_BMAP_NR(s) ; i++) {
1125 1126 1127
    ll_rw_block(READ, 1, &(SB_AP_BITMAP(s)[i].bh)) ;
    wait_on_buffer(SB_AP_BITMAP(s)[i].bh) ;
    if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) {
1128 1129
      printk("reread_meta_blocks, error reading bitmap block number %d at %llu\n", 
        i, (unsigned long long)SB_AP_BITMAP(s)[i].bh->b_blocknr) ;
Linus Torvalds's avatar
Linus Torvalds committed
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
      return 1 ;
    }
  }
  return 0 ;

}


/////////////////////////////////////////////////////
// hash detection stuff


// if root directory is empty - we set default - Yura's - hash and
// warn about it
// FIXME: we look for only one name in a directory. If tea and yura
// bith have the same value - we ask user to send report to the
// mailing list
__u32 find_hash_out (struct super_block * s)
{
    int retval;
    struct inode * inode;
    struct cpu_key key;
    INITIALIZE_PATH (path);
    struct reiserfs_dir_entry de;
    __u32 hash = DEFAULT_HASH;

    inode = s->s_root->d_inode;

1158 1159 1160
    do { // Some serious "goto"-hater was there ;)
	u32 teahash, r5hash, yurahash;

Linus Torvalds's avatar
Linus Torvalds committed
1161 1162 1163 1164 1165 1166 1167 1168 1169
	make_cpu_key (&key, inode, ~0, TYPE_DIRENTRY, 3);
	retval = search_by_entry_key (s, &key, &path, &de);
	if (retval == IO_ERROR) {
	    pathrelse (&path);
	    return UNSET_HASH ;
	}
	if (retval == NAME_NOT_FOUND)
	    de.de_entry_num --;
	set_de_name_and_namelen (&de);
Linus Torvalds's avatar
Linus Torvalds committed
1170
	if (deh_offset( &(de.de_deh[de.de_entry_num]) ) == DOT_DOT_OFFSET) {
Linus Torvalds's avatar
Linus Torvalds committed
1171 1172 1173 1174 1175 1176 1177 1178
	    /* allow override in this case */
	    if (reiserfs_rupasov_hash(s)) {
		hash = YURA_HASH ;
	    }
	    reiserfs_warning("reiserfs: FS seems to be empty, autodetect "
	                     "is using the default hash\n");
	    break;
	}
1179 1180 1181 1182 1183 1184 1185 1186
	r5hash=GET_HASH_VALUE (r5_hash (de.de_name, de.de_namelen));
	teahash=GET_HASH_VALUE (keyed_hash (de.de_name, de.de_namelen));
	yurahash=GET_HASH_VALUE (yura_hash (de.de_name, de.de_namelen));
	if ( ( (teahash == r5hash) && (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash) ) ||
	     ( (teahash == yurahash) && (yurahash == GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])))) ) ||
	     ( (r5hash == yurahash) && (yurahash == GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])))) ) ) {
	    reiserfs_warning("reiserfs: Unable to automatically detect hash"
		"function for device %s\n"
Oleg Drokin's avatar
Oleg Drokin committed
1187
		"please mount with -o hash={tea,rupasov,r5}\n", reiserfs_bdevname (s));
1188
	    hash = UNSET_HASH;
Linus Torvalds's avatar
Linus Torvalds committed
1189 1190
	    break;
	}
1191
	if (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])) ) == yurahash)
Linus Torvalds's avatar
Linus Torvalds committed
1192
	    hash = YURA_HASH;
1193
	else if (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])) ) == teahash)
Linus Torvalds's avatar
Linus Torvalds committed
1194
	    hash = TEA_HASH;
1195 1196 1197 1198
	else if (GET_HASH_VALUE( deh_offset(&(de.de_deh[de.de_entry_num])) ) == r5hash)
	    hash = R5_HASH;
	else {
	    reiserfs_warning("reiserfs: Unrecognised hash function for "
Oleg Drokin's avatar
Oleg Drokin committed
1199
			     "device %s\n", reiserfs_bdevname (s));
1200 1201 1202
	    hash = UNSET_HASH;
	}
    } while (0);
Linus Torvalds's avatar
Linus Torvalds committed
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212

    pathrelse (&path);
    return hash;
}

// finds out which hash names are sorted with
static int what_hash (struct super_block * s)
{
    __u32 code;

Linus Torvalds's avatar
Linus Torvalds committed
1213
    code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
Linus Torvalds's avatar
Linus Torvalds committed
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226

    /* reiserfs_hash_detect() == true if any of the hash mount options
    ** were used.  We must check them to make sure the user isn't
    ** using a bad hash value
    */
    if (code == UNSET_HASH || reiserfs_hash_detect(s))
	code = find_hash_out (s);

    if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
	/* detection has found the hash, and we must check against the 
	** mount options 
	*/
	if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
1227 1228
	    printk("REISERFS: Error, %s hash detected, "
		   "unable to force rupasov hash\n", reiserfs_hashname(code)) ;
Linus Torvalds's avatar
Linus Torvalds committed
1229 1230
	    code = UNSET_HASH ;
	} else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
1231 1232
	    printk("REISERFS: Error, %s hash detected, "
		   "unable to force tea hash\n", reiserfs_hashname(code)) ;
Linus Torvalds's avatar
Linus Torvalds committed
1233 1234
	    code = UNSET_HASH ;
	} else if (reiserfs_r5_hash(s) && code != R5_HASH) {
1235 1236
	    printk("REISERFS: Error, %s hash detected, "
		   "unable to force r5 hash\n", reiserfs_hashname(code)) ;
Linus Torvalds's avatar
Linus Torvalds committed
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
	    code = UNSET_HASH ;
	} 
    } else { 
        /* find_hash_out was not called or could not determine the hash */
	if (reiserfs_rupasov_hash(s)) {
	    code = YURA_HASH ;
	} else if (reiserfs_tea_hash(s)) {
	    code = TEA_HASH ;
	} else if (reiserfs_r5_hash(s)) {
	    code = R5_HASH ;
	} 
    }

    /* if we are mounted RW, and we have a new valid hash code, update 
    ** the super
    */
    if (code != UNSET_HASH && 
	!(s->s_flags & MS_RDONLY) && 
Linus Torvalds's avatar
Linus Torvalds committed
1255 1256
        code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
        set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
Linus Torvalds's avatar
Linus Torvalds committed
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
    }
    return code;
}

// return pointer to appropriate function
static hashf_t hash_function (struct super_block * s)
{
    switch (what_hash (s)) {
    case TEA_HASH:
	reiserfs_warning ("Using tea hash to sort names\n");
	return keyed_hash;
    case YURA_HASH:
	reiserfs_warning ("Using rupasov hash to sort names\n");
	return yura_hash;
    case R5_HASH:
	reiserfs_warning ("Using r5 hash to sort names\n");
	return r5_hash;
    }
    return NULL;
}

// this is used to set up correct value for old partitions
int function2code (hashf_t func)
{
    if (func == keyed_hash)
	return TEA_HASH;
    if (func == yura_hash)
	return YURA_HASH;
    if (func == r5_hash)
	return R5_HASH;

1288
    BUG() ; // should never happen
Linus Torvalds's avatar
Linus Torvalds committed
1289 1290 1291 1292

    return 0;
}

1293 1294 1295 1296
#define SPRINTK(silent, ...)			\
	if (!(silent))				\
		printk(__VA_ARGS__)

1297
static int reiserfs_fill_super (struct super_block * s, void * data, int silent)
Linus Torvalds's avatar
Linus Torvalds committed
1298 1299 1300 1301 1302 1303
{
    struct inode *root_inode;
    int j;
    struct reiserfs_transaction_handle th ;
    int old_format = 0;
    unsigned long blocks;
1304
    unsigned int commit_max_age = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1305
    int jinit_done = 0 ;
Jan Harkes's avatar
Jan Harkes committed
1306
    struct reiserfs_iget_args args ;
Linus Torvalds's avatar
Linus Torvalds committed
1307 1308
    struct reiserfs_super_block * rs;
    char *jdev_name;
1309
    struct reiserfs_sb_info *sbi;
1310
    int errval = -EINVAL;
1311 1312

    sbi = kmalloc(sizeof(struct reiserfs_sb_info), GFP_KERNEL);
1313 1314 1315 1316
    if (!sbi) {
	errval = -ENOMEM;
	goto error;
    }
1317
    s->s_fs_info = sbi;
1318
    memset (sbi, 0, sizeof (struct reiserfs_sb_info));
1319 1320 1321 1322 1323 1324
    /* Set default values for options: non-aggressive tails */
    REISERFS_SB(s)->s_mount_opt = ( 1 << REISERFS_SMALLTAIL );
    /* default block allocator option: skip_busy */
    REISERFS_SB(s)->s_alloc_options.bits = ( 1 << 5);
    /* If file grew past 4 blocks, start preallocation blocks for it. */
    REISERFS_SB(s)->s_alloc_options.preallocmin = 4;
1325 1326
    /* Preallocate by 16 blocks (17-1) at once */
    REISERFS_SB(s)->s_alloc_options.preallocsize = 17;
1327 1328
    /* Initialize the rwsem for xattr dir */
    init_rwsem(&REISERFS_SB(s)->xattr_dir_sem);
Linus Torvalds's avatar
Linus Torvalds committed
1329

Linus Torvalds's avatar
Linus Torvalds committed
1330
    jdev_name = NULL;
1331
    if (reiserfs_parse_options (s, (char *) data, &(sbi->s_mount_opt), &blocks, &jdev_name, &commit_max_age) == 0) {
1332
	goto error;
Linus Torvalds's avatar
Linus Torvalds committed
1333 1334 1335
    }

    if (blocks) {
1336
	SPRINTK(silent, "jmacd-7: reiserfs_fill_super: resize option for remount only\n");
1337
	goto error;
Linus Torvalds's avatar
Linus Torvalds committed
1338 1339
    }	

Linus Torvalds's avatar
Linus Torvalds committed
1340
    /* try old format (undistributed bitmap, super block in 8-th 1k block of a device) */
1341
    if (!read_super_block (s, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
Linus Torvalds's avatar
Linus Torvalds committed
1342 1343 1344
      old_format = 1;
    /* try new format (64-th 1k block), which can contain reiserfs super block */
    else if (read_super_block (s, REISERFS_DISK_OFFSET_IN_BYTES)) {
1345 1346
      SPRINTK(silent, "sh-2021: reiserfs_fill_super: can not find reiserfs on %s\n", reiserfs_bdevname (s));
      goto error;
Linus Torvalds's avatar
Linus Torvalds committed
1347
    }
1348 1349 1350 1351 1352

    rs = SB_DISK_SUPER_BLOCK (s);
    /* Let's do basic sanity check to verify that underlying device is not
       smaller than the filesystem. If the check fails then abort and scream,
       because bad stuff will happen otherwise. */
1353 1354 1355 1356
    if ( s->s_bdev && s->s_bdev->bd_inode && i_size_read(s->s_bdev->bd_inode) < sb_block_count(rs)*sb_blocksize(rs)) {
	SPRINTK(silent, "Filesystem on %s cannot be mounted because it is bigger than the device\n", reiserfs_bdevname(s));
	SPRINTK(silent, "You may need to run fsck or increase size of your LVM partition\n");
	SPRINTK(silent, "Or may be you forgot to reboot after fdisk when it told you to\n");
1357 1358 1359
	goto error;
    }

1360 1361
    sbi->s_mount_state = SB_REISERFS_STATE(s);
    sbi->s_mount_state = REISERFS_VALID_FS ;
Linus Torvalds's avatar
Linus Torvalds committed
1362

1363 1364
    if (old_format ? read_old_bitmaps(s) : read_bitmaps(s)) {
	SPRINTK(silent, "jmacd-8: reiserfs_fill_super: unable to read bitmap\n");
Linus Torvalds's avatar
Linus Torvalds committed
1365 1366 1367
	goto error;
    }
#ifdef CONFIG_REISERFS_CHECK
1368 1369
    SPRINTK(silent, "reiserfs:warning: CONFIG_REISERFS_CHECK is set ON\n");
    SPRINTK(silent, "reiserfs:warning: - it is slow mode for debugging.\n");
Linus Torvalds's avatar
Linus Torvalds committed
1370 1371
#endif

1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
    /* make data=ordered the default */
    if (!reiserfs_data_log(s) && !reiserfs_data_ordered(s) &&
        !reiserfs_data_writeback(s))
    {
         REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
    }

    if (reiserfs_data_log(s)) {
        printk("reiserfs: using journaled data mode\n");
    } else if (reiserfs_data_ordered(s)) {
        printk("reiserfs: using ordered data mode\n");
    } else {
        printk("reiserfs: using writeback data mode\n");
    }

Linus Torvalds's avatar
Linus Torvalds committed
1387
    // set_device_ro(s->s_dev, 1) ;
1388
    if( journal_init(s, jdev_name, old_format, commit_max_age) ) {
1389
	SPRINTK(silent, "sh-2022: reiserfs_fill_super: unable to initialize journal space\n") ;
Linus Torvalds's avatar
Linus Torvalds committed
1390 1391 1392
	goto error ;
    } else {
	jinit_done = 1 ; /* once this is set, journal_release must be called
1393
			 ** if we error out of the mount
Linus Torvalds's avatar
Linus Torvalds committed
1394 1395 1396
			 */
    }
    if (reread_meta_blocks(s)) {
1397
	SPRINTK(silent, "jmacd-9: reiserfs_fill_super: unable to reread meta blocks after journal init\n") ;
Linus Torvalds's avatar
Linus Torvalds committed
1398 1399 1400 1401 1402 1403
	goto error ;
    }

    if (replay_only (s))
	goto error;

Alexander Viro's avatar
Alexander Viro committed
1404
    if (bdev_read_only(s->s_bdev) && !(s->s_flags & MS_RDONLY)) {
1405
        SPRINTK(silent, "clm-7000: Detected readonly device, marking FS readonly\n") ;
Linus Torvalds's avatar
Linus Torvalds committed
1406 1407
	s->s_flags |= MS_RDONLY ;
    }
Jan Harkes's avatar
Jan Harkes committed
1408 1409
    args.objectid = REISERFS_ROOT_OBJECTID ;
    args.dirid = REISERFS_ROOT_PARENT_OBJECTID ;
Jan Harkes's avatar
Jan Harkes committed
1410
    root_inode = iget5_locked (s, REISERFS_ROOT_OBJECTID, reiserfs_find_actor, reiserfs_init_locked_inode, (void *)(&args));
Linus Torvalds's avatar
Linus Torvalds committed
1411
    if (!root_inode) {
1412
	SPRINTK(silent, "jmacd-10: reiserfs_fill_super: get root inode failed\n");
Linus Torvalds's avatar
Linus Torvalds committed
1413 1414 1415
	goto error;
    }

Jan Harkes's avatar
Jan Harkes committed
1416 1417 1418 1419 1420
    if (root_inode->i_state & I_NEW) {
	reiserfs_read_locked_inode(root_inode, &args);
	unlock_new_inode(root_inode);
    }

Linus Torvalds's avatar
Linus Torvalds committed
1421 1422 1423 1424 1425 1426 1427
    s->s_root = d_alloc_root(root_inode);  
    if (!s->s_root) {
	iput(root_inode);
	goto error;
    }

    // define and initialize hash function
1428 1429
    sbi->s_hash_function = hash_function (s);
    if (sbi->s_hash_function == NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
1430 1431 1432 1433 1434
      dput(s->s_root) ;
      s->s_root = NULL ;
      goto error ;
    }

Linus Torvalds's avatar
Linus Torvalds committed
1435
    if (is_reiserfs_3_5 (rs) || (is_reiserfs_jr (rs) && SB_VERSION (s) == REISERFS_VERSION_1))
1436
	set_bit(REISERFS_3_5, &(sbi->s_properties));
Linus Torvalds's avatar
Linus Torvalds committed
1437
    else
1438
	set_bit(REISERFS_3_6, &(sbi->s_properties));
Linus Torvalds's avatar
Linus Torvalds committed
1439
    
Linus Torvalds's avatar
Linus Torvalds committed
1440 1441 1442 1443 1444
    if (!(s->s_flags & MS_RDONLY)) {

	journal_begin(&th, s, 1) ;
	reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;

Linus Torvalds's avatar
Linus Torvalds committed
1445 1446 1447 1448 1449
        set_sb_umount_state( rs, REISERFS_ERROR_FS );
	set_sb_fs_state (rs, 0);
	
	if (old_format_only(s)) {
	  /* filesystem of format 3.5 either with standard or non-standard
1450
	     journal */
Linus Torvalds's avatar
Linus Torvalds committed
1451
	  if (convert_reiserfs (s)) {
1452 1453 1454 1455
	    /* and -o conv is given */
	    if(!silent)
	      reiserfs_warning ("reiserfs: converting 3.5 filesystem to the 3.6 format\n") ;

Linus Torvalds's avatar
Linus Torvalds committed
1456 1457 1458
	    if (is_reiserfs_3_5 (rs))
	      /* put magic string of 3.6 format. 2.2 will not be able to
		 mount this filesystem anymore */
1459
	      memcpy (rs->s_v1.s_magic, reiserfs_3_6_magic_string,
Linus Torvalds's avatar
Linus Torvalds committed
1460
		      sizeof (reiserfs_3_6_magic_string));
1461

Linus Torvalds's avatar
Linus Torvalds committed
1462 1463
	    set_sb_version(rs,REISERFS_VERSION_2);
	    reiserfs_convert_objectid_map_v1(s) ;
1464 1465
	    set_bit(REISERFS_3_6, &(sbi->s_properties));
	    clear_bit(REISERFS_3_5, &(sbi->s_properties));
1466
	  } else if (!silent){
Linus Torvalds's avatar
Linus Torvalds committed
1467 1468
	    reiserfs_warning("reiserfs: using 3.5.x disk format\n") ;
	  }
Linus Torvalds's avatar
Linus Torvalds committed
1469 1470 1471 1472
	}

	journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB (s));
	journal_end(&th, s, 1) ;
1473 1474 1475 1476 1477 1478 1479

	if (reiserfs_xattr_init (s, s->s_flags)) {
	    dput (s->s_root);
	    s->s_root = NULL;
	    goto error;
	}

Linus Torvalds's avatar
Linus Torvalds committed
1480 1481
	/* look for files which were to be removed in previous session */
	finish_unfinished (s);
Linus Torvalds's avatar
Linus Torvalds committed
1482
    } else {
1483
	if ( old_format_only(s) && !silent) {
Linus Torvalds's avatar
Linus Torvalds committed
1484 1485
	    reiserfs_warning("reiserfs: using 3.5.x disk format\n") ;
	}
1486 1487 1488 1489 1490 1491

	if (reiserfs_xattr_init (s, s->s_flags)) {
	    dput (s->s_root);
	    s->s_root = NULL;
	    goto error;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1492
    }
Linus Torvalds's avatar
Linus Torvalds committed
1493
    // mark hash in super block: it could be unset. overwrite should be ok
1494
    set_sb_hash_function_code( rs, function2code(sbi->s_hash_function ) );
Linus Torvalds's avatar
Linus Torvalds committed
1495

1496 1497
    handle_attrs( s );

Linus Torvalds's avatar
Linus Torvalds committed
1498
    reiserfs_proc_info_init( s );
1499

1500
    init_waitqueue_head (&(sbi->s_wait));
1501
    sbi->bitmap_lock = SPIN_LOCK_UNLOCKED;
Linus Torvalds's avatar
Linus Torvalds committed
1502

1503
    return (0);
Linus Torvalds's avatar
Linus Torvalds committed
1504 1505 1506 1507 1508 1509 1510 1511

 error:
    if (jinit_done) { /* kill the commit thread, free journal ram */
	journal_release_error(NULL, s) ;
    }
    if (SB_DISK_SUPER_BLOCK (s)) {
	for (j = 0; j < SB_BMAP_NR (s); j ++) {
	    if (SB_AP_BITMAP (s))
1512
		brelse (SB_AP_BITMAP (s)[j].bh);
Linus Torvalds's avatar
Linus Torvalds committed
1513 1514
	}
	if (SB_AP_BITMAP (s))
1515
	    vfree (SB_AP_BITMAP (s));
Linus Torvalds's avatar
Linus Torvalds committed
1516 1517 1518 1519
    }
    if (SB_BUFFER_WITH_SB (s))
	brelse(SB_BUFFER_WITH_SB (s));

1520 1521 1522
    if (sbi != NULL) {
	kfree(sbi);
    }
1523

1524
    s->s_fs_info = NULL;
1525
    return errval;
Linus Torvalds's avatar
Linus Torvalds committed
1526 1527 1528
}


1529
static int reiserfs_statfs (struct super_block * s, struct kstatfs * buf)
Linus Torvalds's avatar
Linus Torvalds committed
1530 1531 1532
{
  struct reiserfs_super_block * rs = SB_DISK_SUPER_BLOCK (s);
  
1533
  buf->f_namelen = (REISERFS_MAX_NAME (s->s_blocksize));
Linus Torvalds's avatar
Linus Torvalds committed
1534 1535
  buf->f_bfree   = sb_free_blocks(rs);
  buf->f_bavail  = buf->f_bfree;
1536 1537
  buf->f_blocks  = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
  buf->f_bsize   = s->s_blocksize;
1538
  /* changed to accommodate gcc folks.*/
1539
  buf->f_type    =  REISERFS_SUPER_MAGIC;
Linus Torvalds's avatar
Linus Torvalds committed
1540 1541 1542
  return 0;
}

1543
static struct super_block*
1544 1545
get_super_block (struct file_system_type *fs_type, int flags,
		 const char *dev_name, void *data)
1546
{
1547
	return get_sb_bdev(fs_type, flags, dev_name, data, reiserfs_fill_super);
1548 1549
}

1550 1551
static int __init
init_reiserfs_fs ( void )
1552
{
1553
	int ret;
1554

1555 1556 1557
	if ((ret = init_inodecache ())) {
		return ret;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1558

1559 1560 1561
        if ((ret = reiserfs_xattr_register_handlers ()))
            goto failed_reiserfs_xattr_register_handlers;

1562 1563
	reiserfs_proc_info_global_init ();
	reiserfs_proc_register_global ("version", reiserfs_global_version_in_proc);
Linus Torvalds's avatar
Linus Torvalds committed
1564

1565
        ret = register_filesystem (& reiserfs_fs_type);
Linus Torvalds's avatar
Linus Torvalds committed
1566

1567 1568 1569 1570
	if (ret == 0) {
		return 0;
	}

1571 1572 1573
        reiserfs_xattr_unregister_handlers ();

failed_reiserfs_xattr_register_handlers:
1574 1575 1576 1577 1578
	reiserfs_proc_unregister_global ("version");
	reiserfs_proc_info_global_done ();
	destroy_inodecache ();

	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
1579 1580
}

1581 1582 1583
static void __exit
exit_reiserfs_fs ( void )
{
1584
        reiserfs_xattr_unregister_handlers ();
1585 1586 1587 1588 1589
	reiserfs_proc_unregister_global ("version");
	reiserfs_proc_info_global_done ();
        unregister_filesystem (& reiserfs_fs_type);
	destroy_inodecache ();
}
Linus Torvalds's avatar
Linus Torvalds committed
1590

1591
struct file_system_type reiserfs_fs_type = {
Oleg Drokin's avatar
Oleg Drokin committed
1592 1593 1594 1595 1596
	.owner		= THIS_MODULE,
	.name		= "reiserfs",
	.get_sb		= get_super_block,
	.kill_sb	= kill_block_super,
	.fs_flags	= FS_REQUIRES_DEV,
1597
};
Linus Torvalds's avatar
Linus Torvalds committed
1598

1599 1600 1601
MODULE_DESCRIPTION ("ReiserFS journaled filesystem");
MODULE_AUTHOR      ("Hans Reiser <reiser@namesys.com>");
MODULE_LICENSE     ("GPL");
Linus Torvalds's avatar
Linus Torvalds committed
1602

1603 1604
module_init (init_reiserfs_fs);
module_exit (exit_reiserfs_fs);