ialloc.c 4.1 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
{
	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.
	 */
39 40
	vfs_dq_free_inode(inode);
	vfs_dq_drop(inode);
L
Linus Torvalds 已提交
41 42 43

	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
	iinfo = UDF_I(inode);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	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);
		*err = -ENOMEM;
		return NULL;
	}
97

M
Marcin Slusarz 已提交
98
	block = udf_new_block(dir->i_sb, NULL,
99
			      dinfo->i_location.partitionReferenceNum,
100
			      start, err);
101
	if (*err) {
L
Linus Torvalds 已提交
102 103 104 105
		iput(inode);
		return NULL;
	}

I
Ingo Molnar 已提交
106
	mutex_lock(&sbi->s_alloc_mutex);
M
Marcin Slusarz 已提交
107
	if (sbi->s_lvid_bh) {
M
Marcin Slusarz 已提交
108 109 110 111 112
		struct logicalVolIntegrityDesc *lvid =
			(struct logicalVolIntegrityDesc *)
			sbi->s_lvid_bh->b_data;
		struct logicalVolIntegrityDescImpUse *lvidiu =
							udf_sb_lvidiu(sbi);
L
Linus Torvalds 已提交
113 114
		struct logicalVolHeaderDesc *lvhd;
		uint64_t uniqueID;
M
Marcin Slusarz 已提交
115 116
		lvhd = (struct logicalVolHeaderDesc *)
				(lvid->logicalVolContentsUse);
L
Linus Torvalds 已提交
117
		if (S_ISDIR(mode))
118
			le32_add_cpu(&lvidiu->numDirs, 1);
L
Linus Torvalds 已提交
119
		else
120
			le32_add_cpu(&lvidiu->numFiles, 1);
121
		iinfo->i_unique = uniqueID = le64_to_cpu(lvhd->uniqueID);
L
Linus Torvalds 已提交
122 123 124
		if (!(++uniqueID & 0x00000000FFFFFFFFUL))
			uniqueID += 16;
		lvhd->uniqueID = cpu_to_le64(uniqueID);
M
Marcin Slusarz 已提交
125
		mark_buffer_dirty(sbi->s_lvid_bh);
L
Linus Torvalds 已提交
126
	}
127
	mutex_unlock(&sbi->s_alloc_mutex);
L
Linus Torvalds 已提交
128
	inode->i_mode = mode;
129
	inode->i_uid = current_fsuid();
130
	if (dir->i_mode & S_ISGID) {
L
Linus Torvalds 已提交
131 132 133
		inode->i_gid = dir->i_gid;
		if (S_ISDIR(mode))
			mode |= S_ISGID;
134
	} else {
135
		inode->i_gid = current_fsgid();
136
	}
L
Linus Torvalds 已提交
137

138 139 140 141
	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 已提交
142
	inode->i_blocks = 0;
143 144 145
	iinfo->i_lenEAttr = 0;
	iinfo->i_lenAlloc = 0;
	iinfo->i_use = 0;
L
Linus Torvalds 已提交
146
	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB))
147
		iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
L
Linus Torvalds 已提交
148
	else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
149
		iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
L
Linus Torvalds 已提交
150
	else
151
		iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
L
Linus Torvalds 已提交
152
	inode->i_mtime = inode->i_atime = inode->i_ctime =
153
		iinfo->i_crtime = current_fs_time(inode->i_sb);
L
Linus Torvalds 已提交
154 155 156
	insert_inode_hash(inode);
	mark_inode_dirty(inode);

157 158
	if (vfs_dq_alloc_inode(inode)) {
		vfs_dq_drop(inode);
L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166 167 168
		inode->i_flags |= S_NOQUOTA;
		inode->i_nlink = 0;
		iput(inode);
		*err = -EDQUOT;
		return NULL;
	}

	*err = 0;
	return inode;
}