[PATCH] namei fixes (11/19)
[linux-2.6] / 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         struct udf_sb_info *sbi = UDF_SB(sb);
40
41         /*
42          * Note: we must free any quota before locking the superblock,
43          * as writing the quota to disk may need the lock as well.
44          */
45         DQUOT_FREE_INODE(inode);
46         DQUOT_DROP(inode);
47
48         clear_inode(inode);
49
50         down(&sbi->s_alloc_sem);
51         if (sbi->s_lvidbh) {
52                 if (S_ISDIR(inode->i_mode))
53                         UDF_SB_LVIDIU(sb)->numDirs =
54                                 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs) - 1);
55                 else
56                         UDF_SB_LVIDIU(sb)->numFiles =
57                                 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) - 1);
58                 
59                 mark_buffer_dirty(sbi->s_lvidbh);
60         }
61         up(&sbi->s_alloc_sem);
62
63         udf_free_blocks(sb, NULL, UDF_I_LOCATION(inode), 0, 1);
64 }
65
66 struct inode * udf_new_inode (struct inode *dir, int mode, int * err)
67 {
68         struct super_block *sb = dir->i_sb;
69         struct udf_sb_info *sbi = UDF_SB(sb);
70         struct inode * inode;
71         int block;
72         uint32_t start = UDF_I_LOCATION(dir).logicalBlockNum;
73
74         inode = new_inode(sb);
75
76         if (!inode)
77         {
78                 *err = -ENOMEM;
79                 return NULL;
80         }
81         *err = -ENOSPC;
82
83         block = udf_new_block(dir->i_sb, NULL, UDF_I_LOCATION(dir).partitionReferenceNum,
84                 start, err);
85         if (*err)
86         {
87                 iput(inode);
88                 return NULL;
89         }
90
91         down(&sbi->s_alloc_sem);
92         UDF_I_UNIQUE(inode) = 0;
93         UDF_I_LENEXTENTS(inode) = 0;
94         UDF_I_NEXT_ALLOC_BLOCK(inode) = 0;
95         UDF_I_NEXT_ALLOC_GOAL(inode) = 0;
96         UDF_I_STRAT4096(inode) = 0;
97         if (UDF_SB_LVIDBH(sb))
98         {
99                 struct logicalVolHeaderDesc *lvhd;
100                 uint64_t uniqueID;
101                 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(sb)->logicalVolContentsUse);
102                 if (S_ISDIR(mode))
103                         UDF_SB_LVIDIU(sb)->numDirs =
104                                 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs) + 1);
105                 else
106                         UDF_SB_LVIDIU(sb)->numFiles =
107                                 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) + 1);
108                 UDF_I_UNIQUE(inode) = uniqueID = le64_to_cpu(lvhd->uniqueID);
109                 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
110                         uniqueID += 16;
111                 lvhd->uniqueID = cpu_to_le64(uniqueID);
112                 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
113         }
114         inode->i_mode = mode;
115         inode->i_uid = current->fsuid;
116         if (dir->i_mode & S_ISGID)
117         {
118                 inode->i_gid = dir->i_gid;
119                 if (S_ISDIR(mode))
120                         mode |= S_ISGID;
121         }
122         else
123                 inode->i_gid = current->fsgid;
124
125         UDF_I_LOCATION(inode).logicalBlockNum = block;
126         UDF_I_LOCATION(inode).partitionReferenceNum = UDF_I_LOCATION(dir).partitionReferenceNum;
127         inode->i_ino = udf_get_lb_pblock(sb, UDF_I_LOCATION(inode), 0);
128         inode->i_blksize = PAGE_SIZE;
129         inode->i_blocks = 0;
130         UDF_I_LENEATTR(inode) = 0;
131         UDF_I_LENALLOC(inode) = 0;
132         UDF_I_USE(inode) = 0;
133         if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_EXTENDED_FE))
134         {
135                 UDF_I_EFE(inode) = 1;
136                 UDF_UPDATE_UDFREV(inode->i_sb, UDF_VERS_USE_EXTENDED_FE);
137                 UDF_I_DATA(inode) = kmalloc(inode->i_sb->s_blocksize - sizeof(struct extendedFileEntry), GFP_KERNEL);
138                 memset(UDF_I_DATA(inode), 0x00, inode->i_sb->s_blocksize - sizeof(struct extendedFileEntry));
139         }
140         else
141         {
142                 UDF_I_EFE(inode) = 0;
143                 UDF_I_DATA(inode) = kmalloc(inode->i_sb->s_blocksize - sizeof(struct fileEntry), GFP_KERNEL);
144                 memset(UDF_I_DATA(inode), 0x00, inode->i_sb->s_blocksize - sizeof(struct fileEntry));
145         }
146         if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB))
147                 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_IN_ICB;
148         else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
149                 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_SHORT;
150         else
151                 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_LONG;
152         inode->i_mtime = inode->i_atime = inode->i_ctime =
153                 UDF_I_CRTIME(inode) = current_fs_time(inode->i_sb);
154         insert_inode_hash(inode);
155         mark_inode_dirty(inode);
156         up(&sbi->s_alloc_sem);
157
158         if (DQUOT_ALLOC_INODE(inode))
159         {
160                 DQUOT_DROP(inode);
161                 inode->i_flags |= S_NOQUOTA;
162                 inode->i_nlink = 0;
163                 iput(inode);
164                 *err = -EDQUOT;
165                 return NULL;
166         }
167
168         *err = 0;
169         return inode;
170 }