blob: 33323473e3c43edfbd53a16014f3bf313d580ac4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ialloc.c
3 *
4 * PURPOSE
5 * Inode allocation handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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/udf_fs.h>
25#include <linux/sched.h>
26#include <linux/slab.h>
27
28#include "udf_i.h"
29#include "udf_sb.h"
30
31void udf_free_inode(struct inode * inode)
32{
33 struct super_block *sb = inode->i_sb;
34 struct udf_sb_info *sbi = UDF_SB(sb);
35
36 /*
37 * Note: we must free any quota before locking the superblock,
38 * as writing the quota to disk may need the lock as well.
39 */
40 DQUOT_FREE_INODE(inode);
41 DQUOT_DROP(inode);
42
43 clear_inode(inode);
44
Ingo Molnar1e7933d2006-03-23 03:00:44 -080045 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 if (sbi->s_lvidbh) {
47 if (S_ISDIR(inode->i_mode))
48 UDF_SB_LVIDIU(sb)->numDirs =
49 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs) - 1);
50 else
51 UDF_SB_LVIDIU(sb)->numFiles =
52 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) - 1);
53
54 mark_buffer_dirty(sbi->s_lvidbh);
55 }
Ingo Molnar1e7933d2006-03-23 03:00:44 -080056 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 udf_free_blocks(sb, NULL, UDF_I_LOCATION(inode), 0, 1);
59}
60
61struct inode * udf_new_inode (struct inode *dir, int mode, int * err)
62{
63 struct super_block *sb = dir->i_sb;
64 struct udf_sb_info *sbi = UDF_SB(sb);
65 struct inode * inode;
66 int block;
67 uint32_t start = UDF_I_LOCATION(dir).logicalBlockNum;
68
69 inode = new_inode(sb);
70
71 if (!inode)
72 {
73 *err = -ENOMEM;
74 return NULL;
75 }
76 *err = -ENOSPC;
77
Eric Sandeen225add62006-08-05 12:15:17 -070078 UDF_I_UNIQUE(inode) = 0;
79 UDF_I_LENEXTENTS(inode) = 0;
80 UDF_I_NEXT_ALLOC_BLOCK(inode) = 0;
81 UDF_I_NEXT_ALLOC_GOAL(inode) = 0;
82 UDF_I_STRAT4096(inode) = 0;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 block = udf_new_block(dir->i_sb, NULL, UDF_I_LOCATION(dir).partitionReferenceNum,
85 start, err);
86 if (*err)
87 {
88 iput(inode);
89 return NULL;
90 }
91
Ingo Molnar1e7933d2006-03-23 03:00:44 -080092 mutex_lock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (UDF_SB_LVIDBH(sb))
94 {
95 struct logicalVolHeaderDesc *lvhd;
96 uint64_t uniqueID;
97 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(sb)->logicalVolContentsUse);
98 if (S_ISDIR(mode))
99 UDF_SB_LVIDIU(sb)->numDirs =
100 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs) + 1);
101 else
102 UDF_SB_LVIDIU(sb)->numFiles =
103 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) + 1);
104 UDF_I_UNIQUE(inode) = uniqueID = le64_to_cpu(lvhd->uniqueID);
105 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
106 uniqueID += 16;
107 lvhd->uniqueID = cpu_to_le64(uniqueID);
108 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
109 }
110 inode->i_mode = mode;
111 inode->i_uid = current->fsuid;
112 if (dir->i_mode & S_ISGID)
113 {
114 inode->i_gid = dir->i_gid;
115 if (S_ISDIR(mode))
116 mode |= S_ISGID;
117 }
118 else
119 inode->i_gid = current->fsgid;
120
121 UDF_I_LOCATION(inode).logicalBlockNum = block;
122 UDF_I_LOCATION(inode).partitionReferenceNum = UDF_I_LOCATION(dir).partitionReferenceNum;
123 inode->i_ino = udf_get_lb_pblock(sb, UDF_I_LOCATION(inode), 0);
124 inode->i_blksize = PAGE_SIZE;
125 inode->i_blocks = 0;
126 UDF_I_LENEATTR(inode) = 0;
127 UDF_I_LENALLOC(inode) = 0;
128 UDF_I_USE(inode) = 0;
129 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_EXTENDED_FE))
130 {
131 UDF_I_EFE(inode) = 1;
132 UDF_UPDATE_UDFREV(inode->i_sb, UDF_VERS_USE_EXTENDED_FE);
133 UDF_I_DATA(inode) = kmalloc(inode->i_sb->s_blocksize - sizeof(struct extendedFileEntry), GFP_KERNEL);
134 memset(UDF_I_DATA(inode), 0x00, inode->i_sb->s_blocksize - sizeof(struct extendedFileEntry));
135 }
136 else
137 {
138 UDF_I_EFE(inode) = 0;
139 UDF_I_DATA(inode) = kmalloc(inode->i_sb->s_blocksize - sizeof(struct fileEntry), GFP_KERNEL);
140 memset(UDF_I_DATA(inode), 0x00, inode->i_sb->s_blocksize - sizeof(struct fileEntry));
141 }
142 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB))
143 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_IN_ICB;
144 else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
145 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_SHORT;
146 else
147 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_LONG;
148 inode->i_mtime = inode->i_atime = inode->i_ctime =
149 UDF_I_CRTIME(inode) = current_fs_time(inode->i_sb);
150 insert_inode_hash(inode);
151 mark_inode_dirty(inode);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800152 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 if (DQUOT_ALLOC_INODE(inode))
155 {
156 DQUOT_DROP(inode);
157 inode->i_flags |= S_NOQUOTA;
158 inode->i_nlink = 0;
159 iput(inode);
160 *err = -EDQUOT;
161 return NULL;
162 }
163
164 *err = 0;
165 return inode;
166}