ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / udf / ialloc.c
1 /*
2  * ialloc.c
3  *
4  * PURPOSE
5  *      Inode allocation handling routines for the OSTA-UDF(tm) filesystem.
6  *
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@hpesjro.fc.hp.com
11  *
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.
17  *
18  *  (C) 1998-2001 Ben Fennema
19  *
20  * HISTORY
21  *
22  *  02/24/99 blf  Created.
23  *
24  */
25
26 #include "udfdecl.h"
27 #include <linux/fs.h>
28 #include <linux/quotaops.h>
29 #include <linux/udf_fs.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
32
33 #include "udf_i.h"
34 #include "udf_sb.h"
35
36 void udf_free_inode(struct inode * inode)
37 {
38         struct super_block * sb = inode->i_sb;
39         int is_directory;
40         unsigned long ino;
41
42         ino = inode->i_ino;
43
44         /*
45          * Note: we must free any quota before locking the superblock,
46          * as writing the quota to disk may need the lock as well.
47          */
48         DQUOT_FREE_INODE(inode);
49         DQUOT_DROP(inode);
50
51         lock_super(sb);
52
53         is_directory = S_ISDIR(inode->i_mode);
54
55         clear_inode(inode);
56
57         if (UDF_SB_LVIDBH(sb))
58         {
59                 if (is_directory)
60                         UDF_SB_LVIDIU(sb)->numDirs =
61                                 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs) - 1);
62                 else
63                         UDF_SB_LVIDIU(sb)->numFiles =
64                                 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) - 1);
65                 
66                 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
67         }
68         unlock_super(sb);
69
70         udf_free_blocks(sb, NULL, UDF_I_LOCATION(inode), 0, 1);
71 }
72
73 struct inode * udf_new_inode (struct inode *dir, int mode, int * err)
74 {
75         struct super_block *sb;
76         struct inode * inode;
77         int block;
78         uint32_t start = UDF_I_LOCATION(dir).logicalBlockNum;
79
80         sb = dir->i_sb;
81         inode = new_inode(sb);
82
83         if (!inode)
84         {
85                 *err = -ENOMEM;
86                 return NULL;
87         }
88         *err = -ENOSPC;
89
90         block = udf_new_block(dir->i_sb, NULL, UDF_I_LOCATION(dir).partitionReferenceNum,
91                 start, err);
92         if (*err)
93         {
94                 iput(inode);
95                 return NULL;
96         }
97         lock_super(sb);
98
99         UDF_I_UNIQUE(inode) = 0;
100         UDF_I_LENEXTENTS(inode) = 0;
101         UDF_I_NEXT_ALLOC_BLOCK(inode) = 0;
102         UDF_I_NEXT_ALLOC_GOAL(inode) = 0;
103         UDF_I_STRAT4096(inode) = 0;
104         if (UDF_SB_LVIDBH(sb))
105         {
106                 struct logicalVolHeaderDesc *lvhd;
107                 uint64_t uniqueID;
108                 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(sb)->logicalVolContentsUse);
109                 if (S_ISDIR(mode))
110                         UDF_SB_LVIDIU(sb)->numDirs =
111                                 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs) + 1);
112                 else
113                         UDF_SB_LVIDIU(sb)->numFiles =
114                                 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) + 1);
115                 UDF_I_UNIQUE(inode) = uniqueID = le64_to_cpu(lvhd->uniqueID);
116                 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
117                         uniqueID += 16;
118                 lvhd->uniqueID = cpu_to_le64(uniqueID);
119                 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
120         }
121         inode->i_mode = mode;
122         inode->i_uid = current->fsuid;
123         if (dir->i_mode & S_ISGID)
124         {
125                 inode->i_gid = dir->i_gid;
126                 if (S_ISDIR(mode))
127                         mode |= S_ISGID;
128         }
129         else
130                 inode->i_gid = current->fsgid;
131
132         UDF_I_LOCATION(inode).logicalBlockNum = block;
133         UDF_I_LOCATION(inode).partitionReferenceNum = UDF_I_LOCATION(dir).partitionReferenceNum;
134         inode->i_ino = udf_get_lb_pblock(sb, UDF_I_LOCATION(inode), 0);
135         inode->i_blksize = PAGE_SIZE;
136         inode->i_blocks = 0;
137         UDF_I_LENEATTR(inode) = 0;
138         UDF_I_LENALLOC(inode) = 0;
139         UDF_I_USE(inode) = 0;
140         if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_EXTENDED_FE))
141         {
142                 UDF_I_EFE(inode) = 1;
143                 UDF_UPDATE_UDFREV(inode->i_sb, UDF_VERS_USE_EXTENDED_FE);
144                 UDF_I_DATA(inode) = kmalloc(inode->i_sb->s_blocksize - sizeof(struct extendedFileEntry), GFP_KERNEL);
145                 memset(UDF_I_DATA(inode), 0x00, inode->i_sb->s_blocksize - sizeof(struct extendedFileEntry));
146         }
147         else
148         {
149                 UDF_I_EFE(inode) = 0;
150                 UDF_I_DATA(inode) = kmalloc(inode->i_sb->s_blocksize - sizeof(struct fileEntry), GFP_KERNEL);
151                 memset(UDF_I_DATA(inode), 0x00, inode->i_sb->s_blocksize - sizeof(struct fileEntry));
152         }
153         if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB))
154                 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_IN_ICB;
155         else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
156                 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_SHORT;
157         else
158                 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_LONG;
159         inode->i_mtime = inode->i_atime = inode->i_ctime =
160                 UDF_I_CRTIME(inode) = CURRENT_TIME;
161         insert_inode_hash(inode);
162         mark_inode_dirty(inode);
163
164         unlock_super(sb);
165         if (DQUOT_ALLOC_INODE(inode))
166         {
167                 DQUOT_DROP(inode);
168                 inode->i_flags |= S_NOQUOTA;
169                 inode->i_nlink = 0;
170                 iput(inode);
171                 *err = -EDQUOT;
172                 return NULL;
173         }
174
175         *err = 0;
176         return inode;
177 }