blob: b5d611cee749ca422b63dbf1db6fd15c629df3c8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ialloc.c
3 *
4 * PURPOSE
5 * Inode allocation handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/sched.h>
24#include <linux/slab.h>
25
26#include "udf_i.h"
27#include "udf_sb.h"
28
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070029void udf_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 struct super_block *sb = inode->i_sb;
32 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara69d75672013-09-12 22:00:15 +020033 struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Jan Kara69d75672013-09-12 22:00:15 +020035 if (lvidiu) {
36 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 if (S_ISDIR(inode->i_mode))
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +010038 le32_add_cpu(&lvidiu->numDirs, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 else
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +010040 le32_add_cpu(&lvidiu->numFiles, -1);
Jan Kara146bca72009-03-16 18:27:37 +010041 udf_updated_lvid(sb);
Jan Kara69d75672013-09-12 22:00:15 +020042 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Pekka Enberg97e961f2008-10-15 12:29:03 +020045 udf_free_blocks(sb, NULL, &UDF_I(inode)->i_location, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Al Viro0b93a922014-09-04 09:47:41 -040048struct inode *udf_new_inode(struct inode *dir, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 struct super_block *sb = dir->i_sb;
51 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070052 struct inode *inode;
Steve Magnanib490bdd2017-10-12 08:48:40 -050053 udf_pblk_t block;
Marcin Slusarzc0b34432008-02-08 04:20:42 -080054 uint32_t start = UDF_I(dir)->i_location.logicalBlockNum;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080055 struct udf_inode_info *iinfo;
56 struct udf_inode_info *dinfo = UDF_I(dir);
Jan Kara69d75672013-09-12 22:00:15 +020057 struct logicalVolIntegrityDescImpUse *lvidiu;
Al Viro0b93a922014-09-04 09:47:41 -040058 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 inode = new_inode(sb);
61
Al Viro0b93a922014-09-04 09:47:41 -040062 if (!inode)
63 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080065 iinfo = UDF_I(inode);
Jan Kara97e1cfb2008-08-18 13:44:48 +020066 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;
Jan Kara382a2282020-09-25 12:29:54 +020070 iinfo->i_data = kzalloc(inode->i_sb->s_blocksize -
71 sizeof(struct extendedFileEntry),
72 GFP_KERNEL);
Jan Kara97e1cfb2008-08-18 13:44:48 +020073 } else {
74 iinfo->i_efe = 0;
Jan Kara382a2282020-09-25 12:29:54 +020075 iinfo->i_data = kzalloc(inode->i_sb->s_blocksize -
76 sizeof(struct fileEntry),
77 GFP_KERNEL);
Jan Kara97e1cfb2008-08-18 13:44:48 +020078 }
Jan Kara382a2282020-09-25 12:29:54 +020079 if (!iinfo->i_data) {
Jan Karaf05f2422021-12-14 11:04:29 +010080 make_bad_inode(inode);
Jan Kara97e1cfb2008-08-18 13:44:48 +020081 iput(inode);
Al Viro0b93a922014-09-04 09:47:41 -040082 return ERR_PTR(-ENOMEM);
Jan Kara97e1cfb2008-08-18 13:44:48 +020083 }
Eric Sandeen225add62006-08-05 12:15:17 -070084
Al Viro0b93a922014-09-04 09:47:41 -040085 err = -ENOSPC;
Marcin Slusarz4b111112008-02-08 04:20:36 -080086 block = udf_new_block(dir->i_sb, NULL,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080087 dinfo->i_location.partitionReferenceNum,
Al Viro0b93a922014-09-04 09:47:41 -040088 start, &err);
89 if (err) {
Jan Karaf05f2422021-12-14 11:04:29 +010090 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 iput(inode);
Al Viro0b93a922014-09-04 09:47:41 -040092 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94
Jan Kara69d75672013-09-12 22:00:15 +020095 lvidiu = udf_sb_lvidiu(sb);
96 if (lvidiu) {
Jan Karad664b6a2010-10-20 18:28:46 +020097 iinfo->i_unique = lvid_get_unique_id(sb);
Jan Kara470cca52014-09-04 16:26:19 +020098 inode->i_generation = iinfo->i_unique;
Jan Karad664b6a2010-10-20 18:28:46 +020099 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (S_ISDIR(mode))
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100101 le32_add_cpu(&lvidiu->numDirs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 else
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100103 le32_add_cpu(&lvidiu->numFiles, 1);
Jan Kara146bca72009-03-16 18:27:37 +0100104 udf_updated_lvid(sb);
Jan Karad664b6a2010-10-20 18:28:46 +0200105 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Dmitry Monakhova6c5a032010-03-04 17:32:22 +0300107
Christian Brauner21cb47b2021-01-21 14:19:25 +0100108 inode_init_owner(&init_user_ns, inode, dir, mode);
Jan Karaecd10aa2018-02-21 17:59:31 +0100109 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
110 inode->i_uid = sbi->s_uid;
111 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
112 inode->i_gid = sbi->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800114 iinfo->i_location.logicalBlockNum = block;
115 iinfo->i_location.partitionReferenceNum =
116 dinfo->i_location.partitionReferenceNum;
Pekka Enberg97e961f2008-10-15 12:29:03 +0200117 inode->i_ino = udf_get_lb_pblock(sb, &iinfo->i_location, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 inode->i_blocks = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800119 iinfo->i_lenEAttr = 0;
120 iinfo->i_lenAlloc = 0;
121 iinfo->i_use = 0;
Steve Nickeld5e2cf02012-02-14 00:28:42 -0500122 iinfo->i_checkpoint = 1;
Steven J. Magnanic3367a12019-08-27 07:13:59 -0500123 iinfo->i_extraPerms = FE_PERM_U_CHATTR;
124 udf_update_extra_perms(inode, mode);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800127 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800129 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800131 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700132 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +0200133 iinfo->i_crtime = inode->i_mtime;
Al Virob2315092014-09-04 09:38:11 -0400134 if (unlikely(insert_inode_locked(inode) < 0)) {
135 make_bad_inode(inode);
136 iput(inode);
Al Viro0b93a922014-09-04 09:47:41 -0400137 return ERR_PTR(-EIO);
Al Virob2315092014-09-04 09:38:11 -0400138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return inode;
142}