blob: 636d8f613929a01dd854ae8773e87abdb325ba1b [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 Gorcunov28de7942007-07-21 04:37:18 -070049 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 else
51 UDF_SB_LVIDIU(sb)->numFiles =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070052 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) - 1);
Cyrill Gorcunovc9c64152007-07-15 23:39:43 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 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
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070061struct inode *udf_new_inode(struct inode *dir, int mode, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 struct super_block *sb = dir->i_sb;
64 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070065 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 int block;
67 uint32_t start = UDF_I_LOCATION(dir).logicalBlockNum;
68
69 inode = new_inode(sb);
70
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070071 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 *err = -ENOMEM;
73 return NULL;
74 }
75 *err = -ENOSPC;
76
Eric Sandeen225add62006-08-05 12:15:17 -070077 UDF_I_UNIQUE(inode) = 0;
78 UDF_I_LENEXTENTS(inode) = 0;
79 UDF_I_NEXT_ALLOC_BLOCK(inode) = 0;
80 UDF_I_NEXT_ALLOC_GOAL(inode) = 0;
81 UDF_I_STRAT4096(inode) = 0;
82
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070083 block = udf_new_block(dir->i_sb, NULL, UDF_I_LOCATION(dir).partitionReferenceNum,
84 start, err);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070085 if (*err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 iput(inode);
87 return NULL;
88 }
89
Ingo Molnar1e7933d2006-03-23 03:00:44 -080090 mutex_lock(&sbi->s_alloc_mutex);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070091 if (UDF_SB_LVIDBH(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct logicalVolHeaderDesc *lvhd;
93 uint64_t uniqueID;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070094 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(sb)->logicalVolContentsUse);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (S_ISDIR(mode))
96 UDF_SB_LVIDIU(sb)->numDirs =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070097 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 else
99 UDF_SB_LVIDIU(sb)->numFiles =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700100 cpu_to_le32(le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 UDF_I_UNIQUE(inode) = uniqueID = le64_to_cpu(lvhd->uniqueID);
102 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
103 uniqueID += 16;
104 lvhd->uniqueID = cpu_to_le64(uniqueID);
105 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
106 }
107 inode->i_mode = mode;
108 inode->i_uid = current->fsuid;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700109 if (dir->i_mode & S_ISGID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 inode->i_gid = dir->i_gid;
111 if (S_ISDIR(mode))
112 mode |= S_ISGID;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700113 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 inode->i_gid = current->fsgid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 UDF_I_LOCATION(inode).logicalBlockNum = block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700118 UDF_I_LOCATION(inode).partitionReferenceNum = UDF_I_LOCATION(dir).partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 inode->i_ino = udf_get_lb_pblock(sb, UDF_I_LOCATION(inode), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 inode->i_blocks = 0;
121 UDF_I_LENEATTR(inode) = 0;
122 UDF_I_LENALLOC(inode) = 0;
123 UDF_I_USE(inode) = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700124 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_EXTENDED_FE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 UDF_I_EFE(inode) = 1;
126 UDF_UPDATE_UDFREV(inode->i_sb, UDF_VERS_USE_EXTENDED_FE);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700127 UDF_I_DATA(inode) = kzalloc(inode->i_sb->s_blocksize - sizeof(struct extendedFileEntry), GFP_KERNEL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700128 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 UDF_I_EFE(inode) = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700130 UDF_I_DATA(inode) = kzalloc(inode->i_sb->s_blocksize - sizeof(struct fileEntry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700132 if (!UDF_I_DATA(inode)) {
Cyrill Gorcunovc9c64152007-07-15 23:39:43 -0700133 iput(inode);
134 *err = -ENOMEM;
135 mutex_unlock(&sbi->s_alloc_mutex);
136 return NULL;
137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB))
139 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_IN_ICB;
140 else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
141 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_SHORT;
142 else
143 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_LONG;
144 inode->i_mtime = inode->i_atime = inode->i_ctime =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700145 UDF_I_CRTIME(inode) = current_fs_time(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 insert_inode_hash(inode);
147 mark_inode_dirty(inode);
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800148 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700150 if (DQUOT_ALLOC_INODE(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 DQUOT_DROP(inode);
152 inode->i_flags |= S_NOQUOTA;
153 inode->i_nlink = 0;
154 iput(inode);
155 *err = -EDQUOT;
156 return NULL;
157 }
158
159 *err = 0;
160 return inode;
161}