xref: /linux/fs/udf/ialloc.c (revision e190bfe56841551b1ad5abb42ebd0c4798cc8c01)
1 /*
2  * ialloc.c
3  *
4  * PURPOSE
5  *	Inode allocation handling routines for the OSTA-UDF(tm) filesystem.
6  *
7  * COPYRIGHT
8  *	This file is distributed under the terms of the GNU General Public
9  *	License (GPL). Copies of the GPL can be obtained from:
10  *		ftp://prep.ai.mit.edu/pub/gnu/GPL
11  *	Each contributing author retains all rights to their own work.
12  *
13  *  (C) 1998-2001 Ben Fennema
14  *
15  * HISTORY
16  *
17  *  02/24/99 blf  Created.
18  *
19  */
20 
21 #include "udfdecl.h"
22 #include <linux/fs.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 
26 #include "udf_i.h"
27 #include "udf_sb.h"
28 
29 void udf_free_inode(struct inode *inode)
30 {
31 	struct super_block *sb = inode->i_sb;
32 	struct udf_sb_info *sbi = UDF_SB(sb);
33 
34 	clear_inode(inode);
35 
36 	mutex_lock(&sbi->s_alloc_mutex);
37 	if (sbi->s_lvid_bh) {
38 		struct logicalVolIntegrityDescImpUse *lvidiu =
39 							udf_sb_lvidiu(sbi);
40 		if (S_ISDIR(inode->i_mode))
41 			le32_add_cpu(&lvidiu->numDirs, -1);
42 		else
43 			le32_add_cpu(&lvidiu->numFiles, -1);
44 		udf_updated_lvid(sb);
45 	}
46 	mutex_unlock(&sbi->s_alloc_mutex);
47 
48 	udf_free_blocks(sb, NULL, &UDF_I(inode)->i_location, 0, 1);
49 }
50 
51 struct inode *udf_new_inode(struct inode *dir, int mode, int *err)
52 {
53 	struct super_block *sb = dir->i_sb;
54 	struct udf_sb_info *sbi = UDF_SB(sb);
55 	struct inode *inode;
56 	int block;
57 	uint32_t start = UDF_I(dir)->i_location.logicalBlockNum;
58 	struct udf_inode_info *iinfo;
59 	struct udf_inode_info *dinfo = UDF_I(dir);
60 
61 	inode = new_inode(sb);
62 
63 	if (!inode) {
64 		*err = -ENOMEM;
65 		return NULL;
66 	}
67 	*err = -ENOSPC;
68 
69 	iinfo = UDF_I(inode);
70 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_EXTENDED_FE)) {
71 		iinfo->i_efe = 1;
72 		if (UDF_VERS_USE_EXTENDED_FE > sbi->s_udfrev)
73 			sbi->s_udfrev = UDF_VERS_USE_EXTENDED_FE;
74 		iinfo->i_ext.i_data = kzalloc(inode->i_sb->s_blocksize -
75 					    sizeof(struct extendedFileEntry),
76 					    GFP_KERNEL);
77 	} else {
78 		iinfo->i_efe = 0;
79 		iinfo->i_ext.i_data = kzalloc(inode->i_sb->s_blocksize -
80 					    sizeof(struct fileEntry),
81 					    GFP_KERNEL);
82 	}
83 	if (!iinfo->i_ext.i_data) {
84 		iput(inode);
85 		*err = -ENOMEM;
86 		return NULL;
87 	}
88 
89 	block = udf_new_block(dir->i_sb, NULL,
90 			      dinfo->i_location.partitionReferenceNum,
91 			      start, err);
92 	if (*err) {
93 		iput(inode);
94 		return NULL;
95 	}
96 
97 	mutex_lock(&sbi->s_alloc_mutex);
98 	if (sbi->s_lvid_bh) {
99 		struct logicalVolIntegrityDesc *lvid =
100 			(struct logicalVolIntegrityDesc *)
101 			sbi->s_lvid_bh->b_data;
102 		struct logicalVolIntegrityDescImpUse *lvidiu =
103 							udf_sb_lvidiu(sbi);
104 		struct logicalVolHeaderDesc *lvhd;
105 		uint64_t uniqueID;
106 		lvhd = (struct logicalVolHeaderDesc *)
107 				(lvid->logicalVolContentsUse);
108 		if (S_ISDIR(mode))
109 			le32_add_cpu(&lvidiu->numDirs, 1);
110 		else
111 			le32_add_cpu(&lvidiu->numFiles, 1);
112 		iinfo->i_unique = uniqueID = le64_to_cpu(lvhd->uniqueID);
113 		if (!(++uniqueID & 0x00000000FFFFFFFFUL))
114 			uniqueID += 16;
115 		lvhd->uniqueID = cpu_to_le64(uniqueID);
116 		udf_updated_lvid(sb);
117 	}
118 	mutex_unlock(&sbi->s_alloc_mutex);
119 
120 	inode_init_owner(inode, dir, mode);
121 
122 	iinfo->i_location.logicalBlockNum = block;
123 	iinfo->i_location.partitionReferenceNum =
124 				dinfo->i_location.partitionReferenceNum;
125 	inode->i_ino = udf_get_lb_pblock(sb, &iinfo->i_location, 0);
126 	inode->i_blocks = 0;
127 	iinfo->i_lenEAttr = 0;
128 	iinfo->i_lenAlloc = 0;
129 	iinfo->i_use = 0;
130 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB))
131 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
132 	else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
133 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
134 	else
135 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
136 	inode->i_mtime = inode->i_atime = inode->i_ctime =
137 		iinfo->i_crtime = current_fs_time(inode->i_sb);
138 	insert_inode_hash(inode);
139 	mark_inode_dirty(inode);
140 
141 	*err = 0;
142 	return inode;
143 }
144