Import 2.3.26pre2
[davej-history.git] / fs / udf / ialloc.c
blob32f60fef5028c4f61a4e4c7ca62a6e3d9b614ee8
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-1999 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/udf_fs.h>
31 #include "udf_i.h"
32 #include "udf_sb.h"
34 void udf_free_inode(struct inode * inode)
36 struct super_block * sb = inode->i_sb;
37 int is_directory;
38 unsigned long ino;
40 if (!inode->i_dev)
42 udf_debug("inode has no device\n");
43 return;
45 if (inode->i_count > 1)
47 udf_debug("inode has count=%d\n", inode->i_count);
48 return;
50 if (inode->i_nlink)
52 udf_debug("inode has nlink=%d\n", inode->i_nlink);
53 return;
55 if (!sb)
57 udf_debug("inode on nonexistent device\n");
58 return;
61 ino = inode->i_ino;
63 lock_super(sb);
65 is_directory = S_ISDIR(inode->i_mode);
67 clear_inode(inode);
69 if (UDF_SB_LVIDBH(sb))
71 if (is_directory)
72 UDF_SB_LVIDIU(sb)->numDirs =
73 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs) - 1);
74 else
75 UDF_SB_LVIDIU(sb)->numFiles =
76 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) - 1);
78 mark_buffer_dirty(UDF_SB_LVIDBH(sb), 1);
81 unlock_super(sb);
83 udf_free_blocks(inode, UDF_I_LOCATION(inode), 0, 1);
86 struct inode * udf_new_inode (const struct inode *dir, int mode, int * err)
88 struct super_block *sb;
89 struct inode * inode;
90 int block;
91 Uint32 start = UDF_I_LOCATION(dir).logicalBlockNum;
93 inode = get_empty_inode();
94 if (!inode)
96 *err = -ENOMEM;
97 return NULL;
99 sb = dir->i_sb;
100 inode->i_sb = sb;
101 inode->i_flags = 0;
102 *err = -ENOSPC;
104 block = udf_new_block(dir, UDF_I_LOCATION(dir).partitionReferenceNum,
105 start, err);
106 if (*err)
108 iput(inode);
109 return NULL;
111 lock_super(sb);
113 if (UDF_SB_LVIDBH(sb))
115 struct LogicalVolHeaderDesc *lvhd;
116 Uint64 uniqueID;
117 lvhd = (struct LogicalVolHeaderDesc *)(UDF_SB_LVID(sb)->logicalVolContentsUse);
118 if (S_ISDIR(mode))
119 UDF_SB_LVIDIU(sb)->numDirs =
120 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs) + 1);
121 else
122 UDF_SB_LVIDIU(sb)->numFiles =
123 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) + 1);
124 UDF_I_UNIQUE(inode) = uniqueID = le64_to_cpu(lvhd->uniqueID);
125 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
126 uniqueID += 16;
127 lvhd->uniqueID = cpu_to_le64(uniqueID);
128 mark_buffer_dirty(UDF_SB_LVIDBH(sb), 1);
130 inode->i_mode = mode;
131 inode->i_sb = sb;
132 inode->i_nlink = 1;
133 inode->i_dev = sb->s_dev;
134 inode->i_uid = current->fsuid;
135 if (dir->i_mode & S_ISGID)
137 inode->i_gid = dir->i_gid;
138 if (S_ISDIR(mode))
139 mode |= S_ISGID;
141 else
142 inode->i_gid = current->fsgid;
143 UDF_I_LOCATION(inode).logicalBlockNum = block;
144 UDF_I_LOCATION(inode).partitionReferenceNum = UDF_I_LOCATION(dir).partitionReferenceNum;
145 inode->i_ino = udf_get_lb_pblock(sb, UDF_I_LOCATION(inode), 0);
146 inode->i_blksize = PAGE_SIZE;
147 inode->i_blocks = 0;
148 inode->i_size = 0;
149 UDF_I_LENEATTR(inode) = 0;
150 UDF_I_LENALLOC(inode) = 0;
151 UDF_I_EXT0LOC(inode) = UDF_I_LOCATION(inode);
152 UDF_I_EXT0LEN(inode) = 0;
153 #if 1
154 UDF_I_EXT0OFFS(inode) = sizeof(struct FileEntry);
155 UDF_I_ALLOCTYPE(inode) = ICB_FLAG_AD_IN_ICB;
156 #else
157 UDF_I_EXT0OFFS(inode) = 0;
158 UDF_I_ALLOCTYPE(inode) = ICB_FLAG_AD_LONG;
159 #endif
161 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
162 UDF_I_UMTIME(inode) = UDF_I_UATIME(inode) = UDF_I_UCTIME(inode) = CURRENT_UTIME;
163 inode->i_op = NULL;
164 insert_inode_hash(inode);
165 mark_inode_dirty(inode);
166 unlock_super(sb);
167 *err = 0;
168 return inode;