Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ialloc.c |
| 3 | * |
| 4 | * PURPOSE |
| 5 | * Inode allocation handling routines for the OSTA-UDF(tm) filesystem. |
| 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/sched.h> |
| 24 | #include <linux/slab.h> |
| 25 | |
| 26 | #include "udf_i.h" |
| 27 | #include "udf_sb.h" |
| 28 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 29 | void udf_free_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
| 31 | struct super_block *sb = inode->i_sb; |
| 32 | struct udf_sb_info *sbi = UDF_SB(sb); |
Jan Kara | 69d7567 | 2013-09-12 22:00:15 +0200 | [diff] [blame] | 33 | struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Jan Kara | 69d7567 | 2013-09-12 22:00:15 +0200 | [diff] [blame] | 35 | if (lvidiu) { |
| 36 | mutex_lock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | if (S_ISDIR(inode->i_mode)) |
marcin.slusarz@gmail.com | c2104fd | 2008-01-30 22:03:57 +0100 | [diff] [blame] | 38 | le32_add_cpu(&lvidiu->numDirs, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | else |
marcin.slusarz@gmail.com | c2104fd | 2008-01-30 22:03:57 +0100 | [diff] [blame] | 40 | le32_add_cpu(&lvidiu->numFiles, -1); |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 41 | udf_updated_lvid(sb); |
Jan Kara | 69d7567 | 2013-09-12 22:00:15 +0200 | [diff] [blame] | 42 | mutex_unlock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 45 | udf_free_blocks(sb, NULL, &UDF_I(inode)->i_location, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Al Viro | 0b93a92 | 2014-09-04 09:47:41 -0400 | [diff] [blame] | 48 | struct inode *udf_new_inode(struct inode *dir, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
| 50 | struct super_block *sb = dir->i_sb; |
| 51 | struct udf_sb_info *sbi = UDF_SB(sb); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 52 | struct inode *inode; |
Steve Magnani | b490bdd | 2017-10-12 08:48:40 -0500 | [diff] [blame] | 53 | udf_pblk_t block; |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame] | 54 | uint32_t start = UDF_I(dir)->i_location.logicalBlockNum; |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 55 | struct udf_inode_info *iinfo; |
| 56 | struct udf_inode_info *dinfo = UDF_I(dir); |
Jan Kara | 69d7567 | 2013-09-12 22:00:15 +0200 | [diff] [blame] | 57 | struct logicalVolIntegrityDescImpUse *lvidiu; |
Al Viro | 0b93a92 | 2014-09-04 09:47:41 -0400 | [diff] [blame] | 58 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | inode = new_inode(sb); |
| 61 | |
Al Viro | 0b93a92 | 2014-09-04 09:47:41 -0400 | [diff] [blame] | 62 | if (!inode) |
| 63 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 65 | iinfo = UDF_I(inode); |
Jan Kara | 97e1cfb | 2008-08-18 13:44:48 +0200 | [diff] [blame] | 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; |
Jan Kara | 382a228 | 2020-09-25 12:29:54 +0200 | [diff] [blame] | 70 | iinfo->i_data = kzalloc(inode->i_sb->s_blocksize - |
| 71 | sizeof(struct extendedFileEntry), |
| 72 | GFP_KERNEL); |
Jan Kara | 97e1cfb | 2008-08-18 13:44:48 +0200 | [diff] [blame] | 73 | } else { |
| 74 | iinfo->i_efe = 0; |
Jan Kara | 382a228 | 2020-09-25 12:29:54 +0200 | [diff] [blame] | 75 | iinfo->i_data = kzalloc(inode->i_sb->s_blocksize - |
| 76 | sizeof(struct fileEntry), |
| 77 | GFP_KERNEL); |
Jan Kara | 97e1cfb | 2008-08-18 13:44:48 +0200 | [diff] [blame] | 78 | } |
Jan Kara | 382a228 | 2020-09-25 12:29:54 +0200 | [diff] [blame] | 79 | if (!iinfo->i_data) { |
Jan Kara | f05f242 | 2021-12-14 11:04:29 +0100 | [diff] [blame] | 80 | make_bad_inode(inode); |
Jan Kara | 97e1cfb | 2008-08-18 13:44:48 +0200 | [diff] [blame] | 81 | iput(inode); |
Al Viro | 0b93a92 | 2014-09-04 09:47:41 -0400 | [diff] [blame] | 82 | return ERR_PTR(-ENOMEM); |
Jan Kara | 97e1cfb | 2008-08-18 13:44:48 +0200 | [diff] [blame] | 83 | } |
Eric Sandeen | 225add6 | 2006-08-05 12:15:17 -0700 | [diff] [blame] | 84 | |
Al Viro | 0b93a92 | 2014-09-04 09:47:41 -0400 | [diff] [blame] | 85 | err = -ENOSPC; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 86 | block = udf_new_block(dir->i_sb, NULL, |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 87 | dinfo->i_location.partitionReferenceNum, |
Al Viro | 0b93a92 | 2014-09-04 09:47:41 -0400 | [diff] [blame] | 88 | start, &err); |
| 89 | if (err) { |
Jan Kara | f05f242 | 2021-12-14 11:04:29 +0100 | [diff] [blame] | 90 | make_bad_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | iput(inode); |
Al Viro | 0b93a92 | 2014-09-04 09:47:41 -0400 | [diff] [blame] | 92 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Jan Kara | 69d7567 | 2013-09-12 22:00:15 +0200 | [diff] [blame] | 95 | lvidiu = udf_sb_lvidiu(sb); |
| 96 | if (lvidiu) { |
Jan Kara | d664b6a | 2010-10-20 18:28:46 +0200 | [diff] [blame] | 97 | iinfo->i_unique = lvid_get_unique_id(sb); |
Jan Kara | 470cca5 | 2014-09-04 16:26:19 +0200 | [diff] [blame] | 98 | inode->i_generation = iinfo->i_unique; |
Jan Kara | d664b6a | 2010-10-20 18:28:46 +0200 | [diff] [blame] | 99 | mutex_lock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | if (S_ISDIR(mode)) |
marcin.slusarz@gmail.com | c2104fd | 2008-01-30 22:03:57 +0100 | [diff] [blame] | 101 | le32_add_cpu(&lvidiu->numDirs, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | else |
marcin.slusarz@gmail.com | c2104fd | 2008-01-30 22:03:57 +0100 | [diff] [blame] | 103 | le32_add_cpu(&lvidiu->numFiles, 1); |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 104 | udf_updated_lvid(sb); |
Jan Kara | d664b6a | 2010-10-20 18:28:46 +0200 | [diff] [blame] | 105 | mutex_unlock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
Dmitry Monakhov | a6c5a03 | 2010-03-04 17:32:22 +0300 | [diff] [blame] | 107 | |
Christian Brauner | 21cb47b | 2021-01-21 14:19:25 +0100 | [diff] [blame] | 108 | inode_init_owner(&init_user_ns, inode, dir, mode); |
Jan Kara | ecd10aa | 2018-02-21 17:59:31 +0100 | [diff] [blame] | 109 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 114 | iinfo->i_location.logicalBlockNum = block; |
| 115 | iinfo->i_location.partitionReferenceNum = |
| 116 | dinfo->i_location.partitionReferenceNum; |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 117 | inode->i_ino = udf_get_lb_pblock(sb, &iinfo->i_location, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | inode->i_blocks = 0; |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 119 | iinfo->i_lenEAttr = 0; |
| 120 | iinfo->i_lenAlloc = 0; |
| 121 | iinfo->i_use = 0; |
Steve Nickel | d5e2cf0 | 2012-02-14 00:28:42 -0500 | [diff] [blame] | 122 | iinfo->i_checkpoint = 1; |
Steven J. Magnani | c3367a1 | 2019-08-27 07:13:59 -0500 | [diff] [blame] | 123 | iinfo->i_extraPerms = FE_PERM_U_CHATTR; |
| 124 | udf_update_extra_perms(inode, mode); |
| 125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB)) |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 127 | iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD)) |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 129 | iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | else |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 131 | iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 132 | inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); |
Arnd Bergmann | c3b9cec | 2018-06-20 10:15:13 +0200 | [diff] [blame] | 133 | iinfo->i_crtime = inode->i_mtime; |
Al Viro | b231509 | 2014-09-04 09:38:11 -0400 | [diff] [blame] | 134 | if (unlikely(insert_inode_locked(inode) < 0)) { |
| 135 | make_bad_inode(inode); |
| 136 | iput(inode); |
Al Viro | 0b93a92 | 2014-09-04 09:47:41 -0400 | [diff] [blame] | 137 | return ERR_PTR(-EIO); |
Al Viro | b231509 | 2014-09-04 09:38:11 -0400 | [diff] [blame] | 138 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | mark_inode_dirty(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | return inode; |
| 142 | } |