ialloc.c 3.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * ialloc.c
 *
 * PURPOSE
 *	Inode allocation handling routines for the OSTA-UDF(tm) filesystem.
 *
 * COPYRIGHT
 *	This file is distributed under the terms of the GNU General Public
 *	License (GPL). Copies of the GPL can be obtained from:
 *		ftp://prep.ai.mit.edu/pub/gnu/GPL
 *	Each contributing author retains all rights to their own work.
 *
 *  (C) 1998-2001 Ben Fennema
 *
 * HISTORY
 *
 *  02/24/99 blf  Created.
 *
 */

#include "udfdecl.h"
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/slab.h>

#include "udf_i.h"
#include "udf_sb.h"

29
void udf_free_inode(struct inode *inode)
L
Linus Torvalds 已提交
30 31 32
{
	struct super_block *sb = inode->i_sb;
	struct udf_sb_info *sbi = UDF_SB(sb);
J
Jan Kara 已提交
33
	struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sb);
L
Linus Torvalds 已提交
34

J
Jan Kara 已提交
35 36
	if (lvidiu) {
		mutex_lock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
37
		if (S_ISDIR(inode->i_mode))
38
			le32_add_cpu(&lvidiu->numDirs, -1);
L
Linus Torvalds 已提交
39
		else
40
			le32_add_cpu(&lvidiu->numFiles, -1);
41
		udf_updated_lvid(sb);
J
Jan Kara 已提交
42
		mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
43 44
	}

45
	udf_free_blocks(sb, NULL, &UDF_I(inode)->i_location, 0, 1);
L
Linus Torvalds 已提交
46 47
}

48
struct inode *udf_new_inode(struct inode *dir, umode_t mode)
L
Linus Torvalds 已提交
49 50 51
{
	struct super_block *sb = dir->i_sb;
	struct udf_sb_info *sbi = UDF_SB(sb);
52
	struct inode *inode;
53
	udf_pblk_t block;
54
	uint32_t start = UDF_I(dir)->i_location.logicalBlockNum;
55 56
	struct udf_inode_info *iinfo;
	struct udf_inode_info *dinfo = UDF_I(dir);
J
Jan Kara 已提交
57
	struct logicalVolIntegrityDescImpUse *lvidiu;
58
	int err;
L
Linus Torvalds 已提交
59 60 61

	inode = new_inode(sb);

62 63
	if (!inode)
		return ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
64

65
	iinfo = UDF_I(inode);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_EXTENDED_FE)) {
		iinfo->i_efe = 1;
		if (UDF_VERS_USE_EXTENDED_FE > sbi->s_udfrev)
			sbi->s_udfrev = UDF_VERS_USE_EXTENDED_FE;
		iinfo->i_ext.i_data = kzalloc(inode->i_sb->s_blocksize -
					    sizeof(struct extendedFileEntry),
					    GFP_KERNEL);
	} else {
		iinfo->i_efe = 0;
		iinfo->i_ext.i_data = kzalloc(inode->i_sb->s_blocksize -
					    sizeof(struct fileEntry),
					    GFP_KERNEL);
	}
	if (!iinfo->i_ext.i_data) {
		iput(inode);
81
		return ERR_PTR(-ENOMEM);
82
	}
83

84
	err = -ENOSPC;
M
Marcin Slusarz 已提交
85
	block = udf_new_block(dir->i_sb, NULL,
86
			      dinfo->i_location.partitionReferenceNum,
87 88
			      start, &err);
	if (err) {
L
Linus Torvalds 已提交
89
		iput(inode);
90
		return ERR_PTR(err);
L
Linus Torvalds 已提交
91 92
	}

J
Jan Kara 已提交
93 94
	lvidiu = udf_sb_lvidiu(sb);
	if (lvidiu) {
95
		iinfo->i_unique = lvid_get_unique_id(sb);
J
Jan Kara 已提交
96
		inode->i_generation = iinfo->i_unique;
97
		mutex_lock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
98
		if (S_ISDIR(mode))
99
			le32_add_cpu(&lvidiu->numDirs, 1);
L
Linus Torvalds 已提交
100
		else
101
			le32_add_cpu(&lvidiu->numFiles, 1);
102
		udf_updated_lvid(sb);
103
		mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
104
	}
105 106

	inode_init_owner(inode, dir, mode);
L
Linus Torvalds 已提交
107

108 109 110
	iinfo->i_location.logicalBlockNum = block;
	iinfo->i_location.partitionReferenceNum =
				dinfo->i_location.partitionReferenceNum;
111
	inode->i_ino = udf_get_lb_pblock(sb, &iinfo->i_location, 0);
L
Linus Torvalds 已提交
112
	inode->i_blocks = 0;
113 114 115
	iinfo->i_lenEAttr = 0;
	iinfo->i_lenAlloc = 0;
	iinfo->i_use = 0;
116
	iinfo->i_checkpoint = 1;
L
Linus Torvalds 已提交
117
	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB))
118
		iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
L
Linus Torvalds 已提交
119
	else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
120
		iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
L
Linus Torvalds 已提交
121
	else
122
		iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
L
Linus Torvalds 已提交
123
	inode->i_mtime = inode->i_atime = inode->i_ctime =
124
		iinfo->i_crtime = current_time(inode);
125 126 127
	if (unlikely(insert_inode_locked(inode) < 0)) {
		make_bad_inode(inode);
		iput(inode);
128
		return ERR_PTR(-EIO);
129
	}
L
Linus Torvalds 已提交
130 131 132 133
	mark_inode_dirty(inode);

	return inode;
}