xref: /openbmc/linux/fs/udf/ialloc.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
15ce34554SBagas Sanjaya // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * ialloc.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * PURPOSE
61da177e4SLinus Torvalds  *	Inode allocation handling routines for the OSTA-UDF(tm) filesystem.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * COPYRIGHT
91da177e4SLinus Torvalds  *  (C) 1998-2001 Ben Fennema
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * HISTORY
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  *  02/24/99 blf  Created.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include "udfdecl.h"
181da177e4SLinus Torvalds #include <linux/fs.h>
191da177e4SLinus Torvalds #include <linux/sched.h>
201da177e4SLinus Torvalds #include <linux/slab.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include "udf_i.h"
231da177e4SLinus Torvalds #include "udf_sb.h"
241da177e4SLinus Torvalds 
udf_free_inode(struct inode * inode)251da177e4SLinus Torvalds void udf_free_inode(struct inode *inode)
261da177e4SLinus Torvalds {
27085cf7b7SJan Kara 	udf_free_blocks(inode->i_sb, NULL, &UDF_I(inode)->i_location, 0, 1);
281da177e4SLinus Torvalds }
291da177e4SLinus Torvalds 
udf_new_inode(struct inode * dir,umode_t mode)300b93a92bSAl Viro struct inode *udf_new_inode(struct inode *dir, umode_t mode)
311da177e4SLinus Torvalds {
321da177e4SLinus Torvalds 	struct super_block *sb = dir->i_sb;
331da177e4SLinus Torvalds 	struct udf_sb_info *sbi = UDF_SB(sb);
341da177e4SLinus Torvalds 	struct inode *inode;
35b490bdd6SSteve Magnani 	udf_pblk_t block;
36c0b34438SMarcin Slusarz 	uint32_t start = UDF_I(dir)->i_location.logicalBlockNum;
3748d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo;
3848d6d8ffSMarcin Slusarz 	struct udf_inode_info *dinfo = UDF_I(dir);
390b93a92bSAl Viro 	int err;
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds 	inode = new_inode(sb);
421da177e4SLinus Torvalds 
430b93a92bSAl Viro 	if (!inode)
440b93a92bSAl Viro 		return ERR_PTR(-ENOMEM);
451da177e4SLinus Torvalds 
4648d6d8ffSMarcin Slusarz 	iinfo = UDF_I(inode);
4797e1cfb0SJan Kara 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_EXTENDED_FE)) {
4897e1cfb0SJan Kara 		iinfo->i_efe = 1;
4997e1cfb0SJan Kara 		if (UDF_VERS_USE_EXTENDED_FE > sbi->s_udfrev)
5097e1cfb0SJan Kara 			sbi->s_udfrev = UDF_VERS_USE_EXTENDED_FE;
51382a2287SJan Kara 		iinfo->i_data = kzalloc(inode->i_sb->s_blocksize -
5297e1cfb0SJan Kara 					sizeof(struct extendedFileEntry),
5397e1cfb0SJan Kara 					GFP_KERNEL);
5497e1cfb0SJan Kara 	} else {
5597e1cfb0SJan Kara 		iinfo->i_efe = 0;
56382a2287SJan Kara 		iinfo->i_data = kzalloc(inode->i_sb->s_blocksize -
5797e1cfb0SJan Kara 					sizeof(struct fileEntry),
5897e1cfb0SJan Kara 					GFP_KERNEL);
5997e1cfb0SJan Kara 	}
60382a2287SJan Kara 	if (!iinfo->i_data) {
61f05f2429SJan Kara 		make_bad_inode(inode);
6297e1cfb0SJan Kara 		iput(inode);
630b93a92bSAl Viro 		return ERR_PTR(-ENOMEM);
6497e1cfb0SJan Kara 	}
65225add61SEric Sandeen 
660b93a92bSAl Viro 	err = -ENOSPC;
674b11111aSMarcin Slusarz 	block = udf_new_block(dir->i_sb, NULL,
6848d6d8ffSMarcin Slusarz 			      dinfo->i_location.partitionReferenceNum,
690b93a92bSAl Viro 			      start, &err);
700b93a92bSAl Viro 	if (err) {
71f05f2429SJan Kara 		make_bad_inode(inode);
721da177e4SLinus Torvalds 		iput(inode);
730b93a92bSAl Viro 		return ERR_PTR(err);
741da177e4SLinus Torvalds 	}
751da177e4SLinus Torvalds 
76d664b6afSJan Kara 	iinfo->i_unique = lvid_get_unique_id(sb);
77470cca56SJan Kara 	inode->i_generation = iinfo->i_unique;
78a6c5a034SDmitry Monakhov 
79f2d40141SChristian Brauner 	inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
80ecd10aa4SJan Kara 	if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
81ecd10aa4SJan Kara 		inode->i_uid = sbi->s_uid;
82ecd10aa4SJan Kara 	if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
83ecd10aa4SJan Kara 		inode->i_gid = sbi->s_gid;
841da177e4SLinus Torvalds 
8548d6d8ffSMarcin Slusarz 	iinfo->i_location.logicalBlockNum = block;
8648d6d8ffSMarcin Slusarz 	iinfo->i_location.partitionReferenceNum =
8748d6d8ffSMarcin Slusarz 				dinfo->i_location.partitionReferenceNum;
8897e961fdSPekka Enberg 	inode->i_ino = udf_get_lb_pblock(sb, &iinfo->i_location, 0);
891da177e4SLinus Torvalds 	inode->i_blocks = 0;
9048d6d8ffSMarcin Slusarz 	iinfo->i_lenEAttr = 0;
9148d6d8ffSMarcin Slusarz 	iinfo->i_lenAlloc = 0;
9248d6d8ffSMarcin Slusarz 	iinfo->i_use = 0;
93d5e2cf07SSteve Nickel 	iinfo->i_checkpoint = 1;
94c3367a1bSSteven J. Magnani 	iinfo->i_extraPerms = FE_PERM_U_CHATTR;
95c3367a1bSSteven J. Magnani 	udf_update_extra_perms(inode, mode);
96c3367a1bSSteven J. Magnani 
971da177e4SLinus Torvalds 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB))
9848d6d8ffSMarcin Slusarz 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
991da177e4SLinus Torvalds 	else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
10048d6d8ffSMarcin Slusarz 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
1011da177e4SLinus Torvalds 	else
10248d6d8ffSMarcin Slusarz 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
103*59ad88f2SJeff Layton 	inode->i_mtime = inode->i_atime = inode_set_ctime_current(inode);
104c3b9cecdSArnd Bergmann 	iinfo->i_crtime = inode->i_mtime;
105b2315096SAl Viro 	if (unlikely(insert_inode_locked(inode) < 0)) {
106b2315096SAl Viro 		make_bad_inode(inode);
107b2315096SAl Viro 		iput(inode);
1080b93a92bSAl Viro 		return ERR_PTR(-EIO);
109b2315096SAl Viro 	}
1101da177e4SLinus Torvalds 	mark_inode_dirty(inode);
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	return inode;
1131da177e4SLinus Torvalds }
114