Import 2.4.0-test2pre6
[davej-history.git] / fs / udf / ialloc.c
blob11e5711f5e0bbadefe6f2658683754d034f80ed3
1 /*
2 * ialloc.c
4 * PURPOSE
5 * Inode allocation handling routines for the OSTA-UDF(tm) filesystem.
7 * CONTACTS
8 * E-mail regarding any portion of the Linux UDF file system should be
9 * directed to the development team mailing list (run by majordomo):
10 * linux_udf@hootie.lvld.hp.com
12 * COPYRIGHT
13 * This file is distributed under the terms of the GNU General Public
14 * License (GPL). Copies of the GPL can be obtained from:
15 * ftp://prep.ai.mit.edu/pub/gnu/GPL
16 * Each contributing author retains all rights to their own work.
18 * (C) 1998-2000 Ben Fennema
20 * HISTORY
22 * 02/24/99 blf Created.
26 #include "udfdecl.h"
27 #include <linux/fs.h>
28 #include <linux/locks.h>
29 #include <linux/quotaops.h>
30 #include <linux/udf_fs.h>
32 #include "udf_i.h"
33 #include "udf_sb.h"
35 void udf_free_inode(struct inode * inode)
37 struct super_block * sb = inode->i_sb;
38 int is_directory;
39 unsigned long ino;
41 ino = inode->i_ino;
44 * Note: we must free any quota before locking the superblock,
45 * as writing the quota to disk may need the lock as well.
47 DQUOT_FREE_INODE(sb, inode);
48 DQUOT_DROP(inode);
50 lock_super(sb);
52 is_directory = S_ISDIR(inode->i_mode);
54 clear_inode(inode);
56 if (UDF_SB_LVIDBH(sb))
58 if (is_directory)
59 UDF_SB_LVIDIU(sb)->numDirs =
60 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs) - 1);
61 else
62 UDF_SB_LVIDIU(sb)->numFiles =
63 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) - 1);
65 mark_buffer_dirty(UDF_SB_LVIDBH(sb), 1);
68 unlock_super(sb);
70 udf_free_blocks(inode, UDF_I_LOCATION(inode), 0, 1);
73 struct inode * udf_new_inode (const struct inode *dir, int mode, int * err)
75 struct super_block *sb;
76 struct inode * inode;
77 int block;
78 Uint32 start = UDF_I_LOCATION(dir).logicalBlockNum;
80 inode = get_empty_inode();
81 if (!inode)
83 *err = -ENOMEM;
84 return NULL;
86 sb = dir->i_sb;
87 inode->i_sb = sb;
88 inode->i_flags = 0;
89 *err = -ENOSPC;
91 block = udf_new_block(dir, UDF_I_LOCATION(dir).partitionReferenceNum,
92 start, err);
93 if (*err)
95 iput(inode);
96 return NULL;
98 lock_super(sb);
100 if (UDF_SB_LVIDBH(sb))
102 struct LogicalVolHeaderDesc *lvhd;
103 Uint64 uniqueID;
104 lvhd = (struct LogicalVolHeaderDesc *)(UDF_SB_LVID(sb)->logicalVolContentsUse);
105 if (S_ISDIR(mode))
106 UDF_SB_LVIDIU(sb)->numDirs =
107 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs) + 1);
108 else
109 UDF_SB_LVIDIU(sb)->numFiles =
110 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) + 1);
111 UDF_I_UNIQUE(inode) = uniqueID = le64_to_cpu(lvhd->uniqueID);
112 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
113 uniqueID += 16;
114 lvhd->uniqueID = cpu_to_le64(uniqueID);
115 mark_buffer_dirty(UDF_SB_LVIDBH(sb), 1);
117 inode->i_mode = mode;
118 inode->i_sb = sb;
119 inode->i_nlink = 1;
120 inode->i_dev = sb->s_dev;
121 inode->i_uid = current->fsuid;
122 if (test_opt (sb, GRPID))
123 inode->i_gid = dir->i_gid;
124 else if (dir->i_mode & S_ISGID)
126 inode->i_gid = dir->i_gid;
127 if (S_ISDIR(mode))
128 mode |= S_ISGID;
130 else
131 inode->i_gid = current->fsgid;
133 UDF_I_LOCATION(inode).logicalBlockNum = block;
134 UDF_I_LOCATION(inode).partitionReferenceNum = UDF_I_LOCATION(dir).partitionReferenceNum;
135 inode->i_ino = udf_get_lb_pblock(sb, UDF_I_LOCATION(inode), 0);
136 inode->i_blksize = PAGE_SIZE;
137 inode->i_blocks = 0;
138 inode->i_size = 0;
139 UDF_I_LENEATTR(inode) = 0;
140 UDF_I_LENALLOC(inode) = 0;
141 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_EXTENDED_FE))
143 UDF_I_EXTENDED_FE(inode) = 1;
144 UDF_UPDATE_UDFREV(inode->i_sb, UDF_VERS_USE_EXTENDED_FE);
146 else
147 UDF_I_EXTENDED_FE(inode) = 0;
148 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB))
149 UDF_I_ALLOCTYPE(inode) = ICB_FLAG_AD_IN_ICB;
150 else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
151 UDF_I_ALLOCTYPE(inode) = ICB_FLAG_AD_SHORT;
152 else
153 UDF_I_ALLOCTYPE(inode) = ICB_FLAG_AD_LONG;
154 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
155 UDF_I_UMTIME(inode) = UDF_I_UATIME(inode) = UDF_I_UCTIME(inode) = CURRENT_UTIME;
156 UDF_I_NEW_INODE(inode) = 1;
157 insert_inode_hash(inode);
158 mark_inode_dirty(inode);
160 unlock_super(sb);
161 if (DQUOT_ALLOC_INODE(sb, inode))
163 sb->dq_op->drop(inode);
164 inode->i_nlink = 0;
165 iput(inode);
166 *err = -EDQUOT;
167 return NULL;
170 *err = 0;
171 return inode;