blob: 2eb503806bce12528a517277ce4fa4020bb0967d [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
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070031void udf_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
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 =
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070049 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs)
50 - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 else
52 UDF_SB_LVIDIU(sb)->numFiles =
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070053 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles)
54 - 1);
Cyrill Gorcunovc9c64152007-07-15 23:39:43 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 mark_buffer_dirty(sbi->s_lvidbh);
57 }
Ingo Molnar1e7933d2006-03-23 03:00:44 -080058 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 udf_free_blocks(sb, NULL, UDF_I_LOCATION(inode), 0, 1);
61}
62
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070063struct inode *udf_new_inode(struct inode *dir, int mode, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 struct super_block *sb = dir->i_sb;
66 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070067 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 int block;
69 uint32_t start = UDF_I_LOCATION(dir).logicalBlockNum;
70
71 inode = new_inode(sb);
72
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070073 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *err = -ENOMEM;
75 return NULL;
76 }
77 *err = -ENOSPC;
78
Eric Sandeen225add62006-08-05 12:15:17 -070079 UDF_I_UNIQUE(inode) = 0;
80 UDF_I_LENEXTENTS(inode) = 0;
81 UDF_I_NEXT_ALLOC_BLOCK(inode) = 0;
82 UDF_I_NEXT_ALLOC_GOAL(inode) = 0;
83 UDF_I_STRAT4096(inode) = 0;
84
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070085 block =
86 udf_new_block(dir->i_sb, NULL,
87 UDF_I_LOCATION(dir).partitionReferenceNum, start,
88 err);
89 if (*err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 iput(inode);
91 return NULL;
92 }
93
Ingo Molnar1e7933d2006-03-23 03:00:44 -080094 mutex_lock(&sbi->s_alloc_mutex);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070095 if (UDF_SB_LVIDBH(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 struct logicalVolHeaderDesc *lvhd;
97 uint64_t uniqueID;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070098 lvhd =
99 (struct logicalVolHeaderDesc *)(UDF_SB_LVID(sb)->
100 logicalVolContentsUse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (S_ISDIR(mode))
102 UDF_SB_LVIDIU(sb)->numDirs =
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700103 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs)
104 + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 else
106 UDF_SB_LVIDIU(sb)->numFiles =
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700107 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles)
108 + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 UDF_I_UNIQUE(inode) = uniqueID = le64_to_cpu(lvhd->uniqueID);
110 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
111 uniqueID += 16;
112 lvhd->uniqueID = cpu_to_le64(uniqueID);
113 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
114 }
115 inode->i_mode = mode;
116 inode->i_uid = current->fsuid;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700117 if (dir->i_mode & S_ISGID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 inode->i_gid = dir->i_gid;
119 if (S_ISDIR(mode))
120 mode |= S_ISGID;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700121 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 inode->i_gid = current->fsgid;
123
124 UDF_I_LOCATION(inode).logicalBlockNum = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700125 UDF_I_LOCATION(inode).partitionReferenceNum =
126 UDF_I_LOCATION(dir).partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 inode->i_ino = udf_get_lb_pblock(sb, UDF_I_LOCATION(inode), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 inode->i_blocks = 0;
129 UDF_I_LENEATTR(inode) = 0;
130 UDF_I_LENALLOC(inode) = 0;
131 UDF_I_USE(inode) = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700132 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_EXTENDED_FE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 UDF_I_EFE(inode) = 1;
134 UDF_UPDATE_UDFREV(inode->i_sb, UDF_VERS_USE_EXTENDED_FE);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700135 UDF_I_DATA(inode) =
136 kzalloc(inode->i_sb->s_blocksize -
137 sizeof(struct extendedFileEntry), GFP_KERNEL);
138 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 UDF_I_EFE(inode) = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700140 UDF_I_DATA(inode) =
141 kzalloc(inode->i_sb->s_blocksize - sizeof(struct fileEntry),
142 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700144 if (!UDF_I_DATA(inode)) {
Cyrill Gorcunovc9c64152007-07-15 23:39:43 -0700145 iput(inode);
146 *err = -ENOMEM;
147 mutex_unlock(&sbi->s_alloc_mutex);
148 return NULL;
149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB))
151 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_IN_ICB;
152 else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
153 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_SHORT;
154 else
155 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_LONG;
156 inode->i_mtime = inode->i_atime = inode->i_ctime =
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700157 UDF_I_CRTIME(inode) = current_fs_time(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 insert_inode_hash(inode);
159 mark_inode_dirty(inode);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800160 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700162 if (DQUOT_ALLOC_INODE(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 DQUOT_DROP(inode);
164 inode->i_flags |= S_NOQUOTA;
165 inode->i_nlink = 0;
166 iput(inode);
167 *err = -EDQUOT;
168 return NULL;
169 }
170
171 *err = 0;
172 return inode;
173}