ialloc.c 4.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 29
/*
 * 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/quotaops.h>
#include <linux/sched.h>
#include <linux/slab.h>

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

30
void udf_free_inode(struct inode *inode)
L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43
{
	struct super_block *sb = inode->i_sb;
	struct udf_sb_info *sbi = UDF_SB(sb);

	/*
	 * Note: we must free any quota before locking the superblock,
	 * as writing the quota to disk may need the lock as well.
	 */
	DQUOT_FREE_INODE(inode);
	DQUOT_DROP(inode);

	clear_inode(inode);

I
Ingo Molnar 已提交
44
	mutex_lock(&sbi->s_alloc_mutex);
M
Marcin Slusarz 已提交
45 46 47
	if (sbi->s_lvid_bh) {
		struct logicalVolIntegrityDescImpUse *lvidiu =
							udf_sb_lvidiu(sbi);
L
Linus Torvalds 已提交
48
		if (S_ISDIR(inode->i_mode))
49
			le32_add_cpu(&lvidiu->numDirs, -1);
L
Linus Torvalds 已提交
50
		else
51
			le32_add_cpu(&lvidiu->numFiles, -1);
52

M
Marcin Slusarz 已提交
53
		mark_buffer_dirty(sbi->s_lvid_bh);
L
Linus Torvalds 已提交
54
	}
I
Ingo Molnar 已提交
55
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
56

57
	udf_free_blocks(sb, NULL, UDF_I(inode)->i_location, 0, 1);
L
Linus Torvalds 已提交
58 59
}

60
struct inode *udf_new_inode(struct inode *dir, int mode, int *err)
L
Linus Torvalds 已提交
61 62 63
{
	struct super_block *sb = dir->i_sb;
	struct udf_sb_info *sbi = UDF_SB(sb);
64
	struct inode *inode;
L
Linus Torvalds 已提交
65
	int block;
66
	uint32_t start = UDF_I(dir)->i_location.logicalBlockNum;
67 68
	struct udf_inode_info *iinfo;
	struct udf_inode_info *dinfo = UDF_I(dir);
L
Linus Torvalds 已提交
69 70 71

	inode = new_inode(sb);

72
	if (!inode) {
L
Linus Torvalds 已提交
73 74 75 76 77
		*err = -ENOMEM;
		return NULL;
	}
	*err = -ENOSPC;

78 79 80 81 82 83
	iinfo = UDF_I(inode);
	iinfo->i_unique = 0;
	iinfo->i_lenExtents = 0;
	iinfo->i_next_alloc_block = 0;
	iinfo->i_next_alloc_goal = 0;
	iinfo->i_strat4096 = 0;
84

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

I
Ingo Molnar 已提交
93
	mutex_lock(&sbi->s_alloc_mutex);
M
Marcin Slusarz 已提交
94
	if (sbi->s_lvid_bh) {
M
Marcin Slusarz 已提交
95 96 97 98 99
		struct logicalVolIntegrityDesc *lvid =
			(struct logicalVolIntegrityDesc *)
			sbi->s_lvid_bh->b_data;
		struct logicalVolIntegrityDescImpUse *lvidiu =
							udf_sb_lvidiu(sbi);
L
Linus Torvalds 已提交
100 101
		struct logicalVolHeaderDesc *lvhd;
		uint64_t uniqueID;
M
Marcin Slusarz 已提交
102 103
		lvhd = (struct logicalVolHeaderDesc *)
				(lvid->logicalVolContentsUse);
L
Linus Torvalds 已提交
104
		if (S_ISDIR(mode))
105
			le32_add_cpu(&lvidiu->numDirs, 1);
L
Linus Torvalds 已提交
106
		else
107
			le32_add_cpu(&lvidiu->numFiles, 1);
108
		iinfo->i_unique = uniqueID = le64_to_cpu(lvhd->uniqueID);
L
Linus Torvalds 已提交
109 110 111
		if (!(++uniqueID & 0x00000000FFFFFFFFUL))
			uniqueID += 16;
		lvhd->uniqueID = cpu_to_le64(uniqueID);
M
Marcin Slusarz 已提交
112
		mark_buffer_dirty(sbi->s_lvid_bh);
L
Linus Torvalds 已提交
113 114 115
	}
	inode->i_mode = mode;
	inode->i_uid = current->fsuid;
116
	if (dir->i_mode & S_ISGID) {
L
Linus Torvalds 已提交
117 118 119
		inode->i_gid = dir->i_gid;
		if (S_ISDIR(mode))
			mode |= S_ISGID;
120
	} else {
L
Linus Torvalds 已提交
121
		inode->i_gid = current->fsgid;
122
	}
L
Linus Torvalds 已提交
123

124 125 126 127
	iinfo->i_location.logicalBlockNum = block;
	iinfo->i_location.partitionReferenceNum =
				dinfo->i_location.partitionReferenceNum;
	inode->i_ino = udf_get_lb_pblock(sb, iinfo->i_location, 0);
L
Linus Torvalds 已提交
128
	inode->i_blocks = 0;
129 130 131
	iinfo->i_lenEAttr = 0;
	iinfo->i_lenAlloc = 0;
	iinfo->i_use = 0;
132
	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_EXTENDED_FE)) {
133
		iinfo->i_efe = 1;
M
Marcin Slusarz 已提交
134 135
		if (UDF_VERS_USE_EXTENDED_FE > sbi->s_udfrev)
			sbi->s_udfrev = UDF_VERS_USE_EXTENDED_FE;
136
		iinfo->i_ext.i_data = kzalloc(inode->i_sb->s_blocksize -
M
Marcin Slusarz 已提交
137 138
					    sizeof(struct extendedFileEntry),
					    GFP_KERNEL);
139
	} else {
140 141
		iinfo->i_efe = 0;
		iinfo->i_ext.i_data = kzalloc(inode->i_sb->s_blocksize -
M
Marcin Slusarz 已提交
142 143
					    sizeof(struct fileEntry),
					    GFP_KERNEL);
L
Linus Torvalds 已提交
144
	}
145
	if (!iinfo->i_ext.i_data) {
146 147 148 149 150
		iput(inode);
		*err = -ENOMEM;
		mutex_unlock(&sbi->s_alloc_mutex);
		return NULL;
	}
L
Linus Torvalds 已提交
151
	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB))
152
		iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
L
Linus Torvalds 已提交
153
	else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
154
		iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
L
Linus Torvalds 已提交
155
	else
156
		iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
L
Linus Torvalds 已提交
157
	inode->i_mtime = inode->i_atime = inode->i_ctime =
158
		iinfo->i_crtime = current_fs_time(inode->i_sb);
L
Linus Torvalds 已提交
159 160
	insert_inode_hash(inode);
	mark_inode_dirty(inode);
I
Ingo Molnar 已提交
161
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
162

163
	if (DQUOT_ALLOC_INODE(inode)) {
L
Linus Torvalds 已提交
164 165 166 167 168 169 170 171 172 173 174
		DQUOT_DROP(inode);
		inode->i_flags |= S_NOQUOTA;
		inode->i_nlink = 0;
		iput(inode);
		*err = -EDQUOT;
		return NULL;
	}

	*err = 0;
	return inode;
}