| 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); | 
|  | 33 |  | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 34 | mutex_lock(&sbi->s_alloc_mutex); | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 35 | if (sbi->s_lvid_bh) { | 
|  | 36 | struct logicalVolIntegrityDescImpUse *lvidiu = | 
|  | 37 | udf_sb_lvidiu(sbi); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | if (S_ISDIR(inode->i_mode)) | 
| marcin.slusarz@gmail.com | c2104fd | 2008-01-30 22:03:57 +0100 | [diff] [blame] | 39 | le32_add_cpu(&lvidiu->numDirs, -1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | else | 
| marcin.slusarz@gmail.com | c2104fd | 2008-01-30 22:03:57 +0100 | [diff] [blame] | 41 | le32_add_cpu(&lvidiu->numFiles, -1); | 
| Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 42 | udf_updated_lvid(sb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | } | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 44 | mutex_unlock(&sbi->s_alloc_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 |  | 
| Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 46 | udf_free_blocks(sb, NULL, &UDF_I(inode)->i_location, 0, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } | 
|  | 48 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 49 | struct inode *udf_new_inode(struct inode *dir, int mode, int *err) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { | 
|  | 51 | struct super_block *sb = dir->i_sb; | 
|  | 52 | struct udf_sb_info *sbi = UDF_SB(sb); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 53 | struct inode *inode; | 
| Jan Kara | 3635046 | 2010-05-19 16:28:56 +0200 | [diff] [blame] | 54 | int block; | 
| Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame] | 55 | uint32_t start = UDF_I(dir)->i_location.logicalBlockNum; | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 56 | struct udf_inode_info *iinfo; | 
|  | 57 | struct udf_inode_info *dinfo = UDF_I(dir); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 |  | 
|  | 59 | inode = new_inode(sb); | 
|  | 60 |  | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 61 | if (!inode) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | *err = -ENOMEM; | 
|  | 63 | return NULL; | 
|  | 64 | } | 
|  | 65 | *err = -ENOSPC; | 
|  | 66 |  | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 67 | iinfo = UDF_I(inode); | 
| Jan Kara | 97e1cfb | 2008-08-18 13:44:48 +0200 | [diff] [blame] | 68 | if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_EXTENDED_FE)) { | 
|  | 69 | iinfo->i_efe = 1; | 
|  | 70 | if (UDF_VERS_USE_EXTENDED_FE > sbi->s_udfrev) | 
|  | 71 | sbi->s_udfrev = UDF_VERS_USE_EXTENDED_FE; | 
|  | 72 | iinfo->i_ext.i_data = kzalloc(inode->i_sb->s_blocksize - | 
|  | 73 | sizeof(struct extendedFileEntry), | 
|  | 74 | GFP_KERNEL); | 
|  | 75 | } else { | 
|  | 76 | iinfo->i_efe = 0; | 
|  | 77 | iinfo->i_ext.i_data = kzalloc(inode->i_sb->s_blocksize - | 
|  | 78 | sizeof(struct fileEntry), | 
|  | 79 | GFP_KERNEL); | 
|  | 80 | } | 
|  | 81 | if (!iinfo->i_ext.i_data) { | 
|  | 82 | iput(inode); | 
|  | 83 | *err = -ENOMEM; | 
|  | 84 | return NULL; | 
|  | 85 | } | 
| Eric Sandeen | 225add6 | 2006-08-05 12:15:17 -0700 | [diff] [blame] | 86 |  | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 87 | block = udf_new_block(dir->i_sb, NULL, | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 88 | dinfo->i_location.partitionReferenceNum, | 
| Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 89 | start, err); | 
| Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 90 | if (*err) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | iput(inode); | 
|  | 92 | return NULL; | 
|  | 93 | } | 
|  | 94 |  | 
| Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 95 | mutex_lock(&sbi->s_alloc_mutex); | 
| Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 96 | if (sbi->s_lvid_bh) { | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 97 | struct logicalVolIntegrityDesc *lvid = | 
|  | 98 | (struct logicalVolIntegrityDesc *) | 
|  | 99 | sbi->s_lvid_bh->b_data; | 
|  | 100 | struct logicalVolIntegrityDescImpUse *lvidiu = | 
|  | 101 | udf_sb_lvidiu(sbi); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | struct logicalVolHeaderDesc *lvhd; | 
|  | 103 | uint64_t uniqueID; | 
| Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 104 | lvhd = (struct logicalVolHeaderDesc *) | 
|  | 105 | (lvid->logicalVolContentsUse); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | if (S_ISDIR(mode)) | 
| marcin.slusarz@gmail.com | c2104fd | 2008-01-30 22:03:57 +0100 | [diff] [blame] | 107 | le32_add_cpu(&lvidiu->numDirs, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | else | 
| marcin.slusarz@gmail.com | c2104fd | 2008-01-30 22:03:57 +0100 | [diff] [blame] | 109 | le32_add_cpu(&lvidiu->numFiles, 1); | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 110 | iinfo->i_unique = uniqueID = le64_to_cpu(lvhd->uniqueID); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | if (!(++uniqueID & 0x00000000FFFFFFFFUL)) | 
|  | 112 | uniqueID += 16; | 
|  | 113 | lvhd->uniqueID = cpu_to_le64(uniqueID); | 
| Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 114 | udf_updated_lvid(sb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } | 
| Jan Kara | db0badc | 2008-08-18 13:40:18 +0200 | [diff] [blame] | 116 | mutex_unlock(&sbi->s_alloc_mutex); | 
| Dmitry Monakhov | a6c5a03 | 2010-03-04 17:32:22 +0300 | [diff] [blame] | 117 |  | 
|  | 118 | inode_init_owner(inode, dir, mode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 |  | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 120 | iinfo->i_location.logicalBlockNum = block; | 
|  | 121 | iinfo->i_location.partitionReferenceNum = | 
|  | 122 | dinfo->i_location.partitionReferenceNum; | 
| Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 123 | inode->i_ino = udf_get_lb_pblock(sb, &iinfo->i_location, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | inode->i_blocks = 0; | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 125 | iinfo->i_lenEAttr = 0; | 
|  | 126 | iinfo->i_lenAlloc = 0; | 
|  | 127 | iinfo->i_use = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | 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] | 129 | iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | 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] | 131 | iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | else | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 133 | iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | inode->i_mtime = inode->i_atime = inode->i_ctime = | 
| Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame] | 135 | iinfo->i_crtime = current_fs_time(inode->i_sb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | insert_inode_hash(inode); | 
|  | 137 | mark_inode_dirty(inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | *err = 0; | 
|  | 140 | return inode; | 
|  | 141 | } |