// Path: src/jext2/exceptions/JExt2Exception.java
// public class JExt2Exception extends Exception {
// 	protected static final int ERRNO = -1;
// 
// 	private static final long serialVersionUID = -7429088074385678308L;
// 
// 	Logger logger = Filesystem.getLogger();
// 
// 	public JExt2Exception() {
// 		log("");
// 	}
// 
// 	public JExt2Exception(String msg) {
// 		log(msg);
// 	}
// 
// 	private void log(String msg) {
// 		if (logger.isLoggable(Level.FINE)) {
// 			StackTraceElement[] stack = getStackTrace();
// 
// 			StringBuilder log = new StringBuilder();
// 			log.append("JEXT2 exception was raised: ");
// 			log.append(this.getClass().getSimpleName());
// 			log.append(" source=");
// 			log.append(stack[0].getClassName());
// 			log.append("->");
// 			log.append(stack[0].getMethodName());
// 			log.append(" msg=");
// 			log.append(msg);
// 			log.append(" errno=");
// 			log.append(getErrno());
// 
// 			logger.fine(log.toString());
// 		}
// 	}
// 
// 	public int getErrno() {
// 		throw new RuntimeException("This method should not be executed. " +
// 				"- The errno value defined here is meaningless");
// 	}
// }
// 
// Path: src/jext2/exceptions/NoSpaceLeftOnDevice.java
// public class NoSpaceLeftOnDevice extends JExt2Exception {
// 	static final long serialVersionUID = 42;
// 	protected final static int ERRNO = Errno.ENOSPC;
// 
// 	public int getErrno() {
// 		return ERRNO;
// 	}
// }


import jext2.exceptions.JExt2Exception;
import jext2.exceptions.NoSpaceLeftOnDevice;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
 * Copyright (c) 2011 Marcel Lauhoff.
 * 
 * This file is part of jext2.
 * 
 * jext2 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * jext2 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with jext2.  If not, see <http://www.gnu.org/licenses/>.
 */

package jext2;



/**
 * Inode allocation and deallocation routines
 * Adapted from the linux kernel implementation. The long comments on
 * functionality are mostly copied from linux or e2fsprogs
 */
public class InodeAlloc {
	private static Superblock superblock = Superblock.getInstance();
	private static BlockGroupAccess blockGroups = BlockGroupAccess.getInstance();
	private static BitmapAccess bitmaps = BitmapAccess.getInstance();

	private static Logger logger = Filesystem.getLogger();

	public static long countFreeInodes() {
		long count = 0;
		for (BlockGroupDescriptor group : blockGroups.iterateBlockGroups()) {
			count += group.getFreeInodesCount();
		}
		return count;
	}

	/*
	 * From linux 2.6.36 source:
	 * There are two policies for allocating an inode.	If the new inode is
	 * a directory, then a forward search is made for a block group with both
	 * free space and a low directory-to-inode ratio; if that fails, then of
	 * the groups with above-average free space, that group with the fewest
	 * directories already is chosen.
	 *
	 * For other inodes, search forward from the parent directory\'s block
	 * group to find a free inode.
	 */
// Path: src/jext2/exceptions/JExt2Exception.java
// public class JExt2Exception extends Exception {
// 	protected static final int ERRNO = -1;
// 
// 	private static final long serialVersionUID = -7429088074385678308L;
// 
// 	Logger logger = Filesystem.getLogger();
// 
// 	public JExt2Exception() {
// 		log("");
// 	}
// 
// 	public JExt2Exception(String msg) {
// 		log(msg);
// 	}
// 
// 	private void log(String msg) {
// 		if (logger.isLoggable(Level.FINE)) {
// 			StackTraceElement[] stack = getStackTrace();
// 
// 			StringBuilder log = new StringBuilder();
// 			log.append("JEXT2 exception was raised: ");
// 			log.append(this.getClass().getSimpleName());
// 			log.append(" source=");
// 			log.append(stack[0].getClassName());
// 			log.append("->");
// 			log.append(stack[0].getMethodName());
// 			log.append(" msg=");
// 			log.append(msg);
// 			log.append(" errno=");
// 			log.append(getErrno());
// 
// 			logger.fine(log.toString());
// 		}
// 	}
// 
// 	public int getErrno() {
// 		throw new RuntimeException("This method should not be executed. " +
// 				"- The errno value defined here is meaningless");
// 	}
// }
// 
// Path: src/jext2/exceptions/NoSpaceLeftOnDevice.java
// public class NoSpaceLeftOnDevice extends JExt2Exception {
// 	static final long serialVersionUID = 42;
// 	protected final static int ERRNO = Errno.ENOSPC;
// 
// 	public int getErrno() {
// 		return ERRNO;
// 	}
// }


// Path: src/jext2/InodeAlloc.java
import jext2.exceptions.JExt2Exception;
import jext2.exceptions.NoSpaceLeftOnDevice;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * Copyright (c) 2011 Marcel Lauhoff.
 * 
 * This file is part of jext2.
 * 
 * jext2 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * jext2 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with jext2.  If not, see <http://www.gnu.org/licenses/>.
 */

package jext2;



/**
 * Inode allocation and deallocation routines
 * Adapted from the linux kernel implementation. The long comments on
 * functionality are mostly copied from linux or e2fsprogs
 */
public class InodeAlloc {
	private static Superblock superblock = Superblock.getInstance();
	private static BlockGroupAccess blockGroups = BlockGroupAccess.getInstance();
	private static BitmapAccess bitmaps = BitmapAccess.getInstance();

	private static Logger logger = Filesystem.getLogger();

	public static long countFreeInodes() {
		long count = 0;
		for (BlockGroupDescriptor group : blockGroups.iterateBlockGroups()) {
			count += group.getFreeInodesCount();
		}
		return count;
	}

	/*
	 * From linux 2.6.36 source:
	 * There are two policies for allocating an inode.	If the new inode is
	 * a directory, then a forward search is made for a block group with both
	 * free space and a low directory-to-inode ratio; if that fails, then of
	 * the groups with above-average free space, that group with the fewest
	 * directories already is chosen.
	 *
	 * For other inodes, search forward from the parent directory\'s block
	 * group to find a free inode.
	 */
	public static int findGroupDir(Inode parent) throws NoSpaceLeftOnDevice {