Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[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  * 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>
23 #include <linux/quotaops.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
26
27 #include "udf_i.h"
28 #include "udf_sb.h"
29
30 void udf_free_inode(struct inode *inode)
31 {
32         struct super_block *sb = inode->i_sb;
33         struct udf_sb_info *sbi = UDF_SB(sb);
34
35         /*
36          * Note: we must free any quota before locking the superblock,
37          * as writing the quota to disk may need the lock as well.
38          */
39         DQUOT_FREE_INODE(inode);
40         DQUOT_DROP(inode);
41
42         clear_inode(inode);
43
44         mutex_lock(&sbi->s_alloc_mutex);
45         if (sbi->s_lvid_bh) {
46                 struct logicalVolIntegrityDescImpUse *lvidiu =
47                                                         udf_sb_lvidiu(sbi);
48                 if (S_ISDIR(inode->i_mode))
49                         le32_add_cpu(&lvidiu->numDirs, -1);
50                 else
51                         le32_add_cpu(&lvidiu->numFiles, -1);
52
53                 mark_buffer_dirty(sbi->s_lvid_bh);
54         }
55         mutex_unlock(&sbi->s_alloc_mutex);
56
57         udf_free_blocks(sb, NULL, UDF_I(inode)->i_location, 0, 1);
58 }
59
60 struct inode *udf_new_inode(struct inode *dir, int mode, int *err)
61 {
62         struct super_block *sb = dir->i_sb;
63         struct udf_sb_info *sbi = UDF_SB(sb);
64         struct inode *inode;
65         int block;
66         uint32_t start = UDF_I(dir)->i_location.logicalBlockNum;
67         struct udf_inode_info *iinfo;
68         struct udf_inode_info *dinfo = UDF_I(dir);
69
70         inode = new_inode(sb);
71
72         if (!inode) {
73                 *err = -ENOMEM;
74                 return NULL;
75         }
76         *err = -ENOSPC;
77
78         iinfo = UDF_I(inode);
79         iinfo->i_unique = 0;
80         iinfo->i_lenExtents = 0;
81         iinfo->i_next_alloc_block = 0;
82         iinfo->i_next_alloc_goal = 0;
83         iinfo->i_strat4096 = 0;
84
85         block = udf_new_block(dir->i_sb, NULL,
86                               dinfo->i_location.partitionReferenceNum,
87                               start, err);
88         if (*err) {
89                 iput(inode);
90                 return NULL;
91         }
92
93         mutex_lock(&sbi->s_alloc_mutex);
94         if (sbi->s_lvid_bh) {
95                 struct logicalVolIntegrityDesc *lvid =
96                         (struct logicalVolIntegrityDesc *)
97                         sbi->s_lvid_bh->b_data;
98                 struct logicalVolIntegrityDescImpUse *lvidiu =
99                                                         udf_sb_lvidiu(sbi);
100                 struct logicalVolHeaderDesc *lvhd;
101                 uint64_t uniqueID;
102                 lvhd = (struct logicalVolHeaderDesc *)
103                                 (lvid->logicalVolContentsUse);
104                 if (S_ISDIR(mode))
105                         le32_add_cpu(&lvidiu->numDirs, 1);
106                 else
107                         le32_add_cpu(&lvidiu->numFiles, 1);
108                 iinfo->i_unique = uniqueID = le64_to_cpu(lvhd->uniqueID);
109                 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
110                         uniqueID += 16;
111                 lvhd->uniqueID = cpu_to_le64(uniqueID);
112                 mark_buffer_dirty(sbi->s_lvid_bh);
113         }
114         inode->i_mode = mode;
115         inode->i_uid = current->fsuid;
116         if (dir->i_mode & S_ISGID) {
117                 inode->i_gid = dir->i_gid;
118                 if (S_ISDIR(mode))
119                         mode |= S_ISGID;
120         } else {
121                 inode->i_gid = current->fsgid;
122         }
123
124         iinfo->i_location.logicalBlockNum = block;
125         iinfo->i_location.partitionReferenceNum =
126                                 dinfo->i_location.partitionReferenceNum;
127         inode->i_ino = udf_get_lb_pblock(sb, iinfo->i_location, 0);
128         inode->i_blocks = 0;
129         iinfo->i_lenEAttr = 0;
130         iinfo->i_lenAlloc = 0;
131         iinfo->i_use = 0;
132         if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_EXTENDED_FE)) {
133                 iinfo->i_efe = 1;
134                 if (UDF_VERS_USE_EXTENDED_FE > sbi->s_udfrev)
135                         sbi->s_udfrev = UDF_VERS_USE_EXTENDED_FE;
136                 iinfo->i_ext.i_data = kzalloc(inode->i_sb->s_blocksize -
137                                             sizeof(struct extendedFileEntry),
138                                             GFP_KERNEL);
139         } else {
140                 iinfo->i_efe = 0;
141                 iinfo->i_ext.i_data = kzalloc(inode->i_sb->s_blocksize -
142                                             sizeof(struct fileEntry),
143                                             GFP_KERNEL);
144         }
145         if (!iinfo->i_ext.i_data) {
146                 iput(inode);
147                 *err = -ENOMEM;
148                 mutex_unlock(&sbi->s_alloc_mutex);
149                 return NULL;
150         }
151         if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB))
152                 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
153         else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
154                 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
155         else
156                 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
157         inode->i_mtime = inode->i_atime = inode->i_ctime =
158                 iinfo->i_crtime = current_fs_time(inode->i_sb);
159         insert_inode_hash(inode);
160         mark_inode_dirty(inode);
161         mutex_unlock(&sbi->s_alloc_mutex);
162
163         if (DQUOT_ALLOC_INODE(inode)) {
164                 DQUOT_DROP(inode);
165                 inode->i_flags |= S_NOQUOTA;
166                 inode->i_nlink = 0;
167                 iput(inode);
168                 *err = -EDQUOT;
169                 return NULL;
170         }
171
172         *err = 0;
173         return inode;
174 }