xref: /openbmc/linux/fs/udf/ialloc.c (revision dd1431e5)
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 	struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sb);
34 
35 	if (lvidiu) {
36 		mutex_lock(&sbi->s_alloc_mutex);
37 		if (S_ISDIR(inode->i_mode))
38 			le32_add_cpu(&lvidiu->numDirs, -1);
39 		else
40 			le32_add_cpu(&lvidiu->numFiles, -1);
41 		udf_updated_lvid(sb);
42 		mutex_unlock(&sbi->s_alloc_mutex);
43 	}
44 
45 	udf_free_blocks(sb, NULL, &UDF_I(inode)->i_location, 0, 1);
46 }
47 
48 struct inode *udf_new_inode(struct inode *dir, umode_t mode)
49 {
50 	struct super_block *sb = dir->i_sb;
51 	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 	struct udf_inode_info *iinfo;
56 	struct udf_inode_info *dinfo = UDF_I(dir);
57 	struct logicalVolIntegrityDescImpUse *lvidiu;
58 	int err;
59 
60 	inode = new_inode(sb);
61 
62 	if (!inode)
63 		return ERR_PTR(-ENOMEM);
64 
65 	iinfo = UDF_I(inode);
66 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_EXTENDED_FE)) {
67 		iinfo->i_efe = 1;
68 		if (UDF_VERS_USE_EXTENDED_FE > sbi->s_udfrev)
69 			sbi->s_udfrev = UDF_VERS_USE_EXTENDED_FE;
70 		iinfo->i_data = kzalloc(inode->i_sb->s_blocksize -
71 					sizeof(struct extendedFileEntry),
72 					GFP_KERNEL);
73 	} else {
74 		iinfo->i_efe = 0;
75 		iinfo->i_data = kzalloc(inode->i_sb->s_blocksize -
76 					sizeof(struct fileEntry),
77 					GFP_KERNEL);
78 	}
79 	if (!iinfo->i_data) {
80 		iput(inode);
81 		return ERR_PTR(-ENOMEM);
82 	}
83 
84 	err = -ENOSPC;
85 	block = udf_new_block(dir->i_sb, NULL,
86 			      dinfo->i_location.partitionReferenceNum,
87 			      start, &err);
88 	if (err) {
89 		iput(inode);
90 		return ERR_PTR(err);
91 	}
92 
93 	lvidiu = udf_sb_lvidiu(sb);
94 	if (lvidiu) {
95 		iinfo->i_unique = lvid_get_unique_id(sb);
96 		inode->i_generation = iinfo->i_unique;
97 		mutex_lock(&sbi->s_alloc_mutex);
98 		if (S_ISDIR(mode))
99 			le32_add_cpu(&lvidiu->numDirs, 1);
100 		else
101 			le32_add_cpu(&lvidiu->numFiles, 1);
102 		udf_updated_lvid(sb);
103 		mutex_unlock(&sbi->s_alloc_mutex);
104 	}
105 
106 	inode_init_owner(&init_user_ns, inode, dir, mode);
107 	if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
108 		inode->i_uid = sbi->s_uid;
109 	if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
110 		inode->i_gid = sbi->s_gid;
111 
112 	iinfo->i_location.logicalBlockNum = block;
113 	iinfo->i_location.partitionReferenceNum =
114 				dinfo->i_location.partitionReferenceNum;
115 	inode->i_ino = udf_get_lb_pblock(sb, &iinfo->i_location, 0);
116 	inode->i_blocks = 0;
117 	iinfo->i_lenEAttr = 0;
118 	iinfo->i_lenAlloc = 0;
119 	iinfo->i_use = 0;
120 	iinfo->i_checkpoint = 1;
121 	iinfo->i_extraPerms = FE_PERM_U_CHATTR;
122 	udf_update_extra_perms(inode, mode);
123 
124 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB))
125 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
126 	else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
127 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
128 	else
129 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
130 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
131 	iinfo->i_crtime = inode->i_mtime;
132 	if (unlikely(insert_inode_locked(inode) < 0)) {
133 		make_bad_inode(inode);
134 		iput(inode);
135 		return ERR_PTR(-EIO);
136 	}
137 	mark_inode_dirty(inode);
138 
139 	return inode;
140 }
141