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> |
| 23 | #include <linux/quotaops.h> |
| 24 | #include <linux/udf_fs.h> |
| 25 | #include <linux/sched.h> |
| 26 | #include <linux/slab.h> |
| 27 | |
| 28 | #include "udf_i.h" |
| 29 | #include "udf_sb.h" |
| 30 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 31 | void udf_free_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
| 33 | struct super_block *sb = inode->i_sb; |
| 34 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 35 | |
| 36 | /* |
| 37 | * Note: we must free any quota before locking the superblock, |
| 38 | * as writing the quota to disk may need the lock as well. |
| 39 | */ |
| 40 | DQUOT_FREE_INODE(inode); |
| 41 | DQUOT_DROP(inode); |
| 42 | |
| 43 | clear_inode(inode); |
| 44 | |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 45 | mutex_lock(&sbi->s_alloc_mutex); |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 46 | if (sbi->s_lvid_bh) { |
| 47 | struct logicalVolIntegrityDescImpUse *lvidiu = |
| 48 | udf_sb_lvidiu(sbi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | if (S_ISDIR(inode->i_mode)) |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 50 | lvidiu->numDirs = |
| 51 | cpu_to_le32(le32_to_cpu(lvidiu->numDirs) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | else |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 53 | lvidiu->numFiles = |
| 54 | cpu_to_le32(le32_to_cpu(lvidiu->numFiles) - 1); |
Cyrill Gorcunov | c9c6415 | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 55 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 56 | mark_buffer_dirty(sbi->s_lvid_bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 58 | mutex_unlock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame] | 60 | udf_free_blocks(sb, NULL, UDF_I(inode)->i_location, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 63 | struct inode *udf_new_inode(struct inode *dir, int mode, int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
| 65 | struct super_block *sb = dir->i_sb; |
| 66 | struct udf_sb_info *sbi = UDF_SB(sb); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 67 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | int block; |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame] | 69 | uint32_t start = UDF_I(dir)->i_location.logicalBlockNum; |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame^] | 70 | struct udf_inode_info *iinfo; |
| 71 | struct udf_inode_info *dinfo = UDF_I(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | inode = new_inode(sb); |
| 74 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 75 | if (!inode) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | *err = -ENOMEM; |
| 77 | return NULL; |
| 78 | } |
| 79 | *err = -ENOSPC; |
| 80 | |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame^] | 81 | iinfo = UDF_I(inode); |
| 82 | iinfo->i_unique = 0; |
| 83 | iinfo->i_lenExtents = 0; |
| 84 | iinfo->i_next_alloc_block = 0; |
| 85 | iinfo->i_next_alloc_goal = 0; |
| 86 | iinfo->i_strat4096 = 0; |
Eric Sandeen | 225add6 | 2006-08-05 12:15:17 -0700 | [diff] [blame] | 87 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 88 | block = udf_new_block(dir->i_sb, NULL, |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame^] | 89 | dinfo->i_location.partitionReferenceNum, |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 90 | start, err); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 91 | if (*err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | iput(inode); |
| 93 | return NULL; |
| 94 | } |
| 95 | |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 96 | mutex_lock(&sbi->s_alloc_mutex); |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 97 | if (sbi->s_lvid_bh) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 98 | struct logicalVolIntegrityDesc *lvid = |
| 99 | (struct logicalVolIntegrityDesc *) |
| 100 | sbi->s_lvid_bh->b_data; |
| 101 | struct logicalVolIntegrityDescImpUse *lvidiu = |
| 102 | udf_sb_lvidiu(sbi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | struct logicalVolHeaderDesc *lvhd; |
| 104 | uint64_t uniqueID; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 105 | lvhd = (struct logicalVolHeaderDesc *) |
| 106 | (lvid->logicalVolContentsUse); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | if (S_ISDIR(mode)) |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 108 | lvidiu->numDirs = |
| 109 | cpu_to_le32(le32_to_cpu(lvidiu->numDirs) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | else |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 111 | lvidiu->numFiles = |
| 112 | cpu_to_le32(le32_to_cpu(lvidiu->numFiles) + 1); |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame^] | 113 | iinfo->i_unique = uniqueID = le64_to_cpu(lvhd->uniqueID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | if (!(++uniqueID & 0x00000000FFFFFFFFUL)) |
| 115 | uniqueID += 16; |
| 116 | lvhd->uniqueID = cpu_to_le64(uniqueID); |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 117 | mark_buffer_dirty(sbi->s_lvid_bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | inode->i_mode = mode; |
| 120 | inode->i_uid = current->fsuid; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 121 | if (dir->i_mode & S_ISGID) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | inode->i_gid = dir->i_gid; |
| 123 | if (S_ISDIR(mode)) |
| 124 | mode |= S_ISGID; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 125 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | inode->i_gid = current->fsgid; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame^] | 129 | iinfo->i_location.logicalBlockNum = block; |
| 130 | iinfo->i_location.partitionReferenceNum = |
| 131 | dinfo->i_location.partitionReferenceNum; |
| 132 | inode->i_ino = udf_get_lb_pblock(sb, iinfo->i_location, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | inode->i_blocks = 0; |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame^] | 134 | iinfo->i_lenEAttr = 0; |
| 135 | iinfo->i_lenAlloc = 0; |
| 136 | iinfo->i_use = 0; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 137 | if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_EXTENDED_FE)) { |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame^] | 138 | iinfo->i_efe = 1; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 139 | if (UDF_VERS_USE_EXTENDED_FE > sbi->s_udfrev) |
| 140 | sbi->s_udfrev = UDF_VERS_USE_EXTENDED_FE; |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame^] | 141 | iinfo->i_ext.i_data = kzalloc(inode->i_sb->s_blocksize - |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 142 | sizeof(struct extendedFileEntry), |
| 143 | GFP_KERNEL); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 144 | } else { |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame^] | 145 | iinfo->i_efe = 0; |
| 146 | iinfo->i_ext.i_data = kzalloc(inode->i_sb->s_blocksize - |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 147 | sizeof(struct fileEntry), |
| 148 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame^] | 150 | if (!iinfo->i_ext.i_data) { |
Cyrill Gorcunov | c9c6415 | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 151 | iput(inode); |
| 152 | *err = -ENOMEM; |
| 153 | mutex_unlock(&sbi->s_alloc_mutex); |
| 154 | return NULL; |
| 155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | 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^] | 157 | iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | 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^] | 159 | iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | else |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame^] | 161 | iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | inode->i_mtime = inode->i_atime = inode->i_ctime = |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame^] | 163 | iinfo->i_crtime = current_fs_time(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | insert_inode_hash(inode); |
| 165 | mark_inode_dirty(inode); |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 166 | mutex_unlock(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 168 | if (DQUOT_ALLOC_INODE(inode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | DQUOT_DROP(inode); |
| 170 | inode->i_flags |= S_NOQUOTA; |
| 171 | inode->i_nlink = 0; |
| 172 | iput(inode); |
| 173 | *err = -EDQUOT; |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | *err = 0; |
| 178 | return inode; |
| 179 | } |