blob: fb68c9cd0c3e59feb556b02593aafd332dbb814b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/sched.h>
25#include <linux/slab.h>
26
27#include "udf_i.h"
28#include "udf_sb.h"
29
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070030void udf_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
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 */
Christoph Hellwig63936dd2010-03-03 09:05:01 -050039 dquot_free_inode(inode);
Christoph Hellwig9f754752010-03-03 09:05:05 -050040 dquot_drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 clear_inode(inode);
43
Ingo Molnar1e7933d2006-03-23 03:00:44 -080044 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -080045 if (sbi->s_lvid_bh) {
46 struct logicalVolIntegrityDescImpUse *lvidiu =
47 udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 if (S_ISDIR(inode->i_mode))
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +010049 le32_add_cpu(&lvidiu->numDirs, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 else
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +010051 le32_add_cpu(&lvidiu->numFiles, -1);
Jan Kara146bca72009-03-16 18:27:37 +010052 udf_updated_lvid(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 }
Ingo Molnar1e7933d2006-03-23 03:00:44 -080054 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Pekka Enberg97e961f2008-10-15 12:29:03 +020056 udf_free_blocks(sb, NULL, &UDF_I(inode)->i_location, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070059struct inode *udf_new_inode(struct inode *dir, int mode, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 struct super_block *sb = dir->i_sb;
62 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070063 struct inode *inode;
Christoph Hellwig63936dd2010-03-03 09:05:01 -050064 int block, ret;
Marcin Slusarzc0b34432008-02-08 04:20:42 -080065 uint32_t start = UDF_I(dir)->i_location.logicalBlockNum;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080066 struct udf_inode_info *iinfo;
67 struct udf_inode_info *dinfo = UDF_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
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
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080077 iinfo = UDF_I(inode);
Jan Kara97e1cfb2008-08-18 13:44:48 +020078 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_EXTENDED_FE)) {
79 iinfo->i_efe = 1;
80 if (UDF_VERS_USE_EXTENDED_FE > sbi->s_udfrev)
81 sbi->s_udfrev = UDF_VERS_USE_EXTENDED_FE;
82 iinfo->i_ext.i_data = kzalloc(inode->i_sb->s_blocksize -
83 sizeof(struct extendedFileEntry),
84 GFP_KERNEL);
85 } else {
86 iinfo->i_efe = 0;
87 iinfo->i_ext.i_data = kzalloc(inode->i_sb->s_blocksize -
88 sizeof(struct fileEntry),
89 GFP_KERNEL);
90 }
91 if (!iinfo->i_ext.i_data) {
92 iput(inode);
93 *err = -ENOMEM;
94 return NULL;
95 }
Eric Sandeen225add62006-08-05 12:15:17 -070096
Marcin Slusarz4b111112008-02-08 04:20:36 -080097 block = udf_new_block(dir->i_sb, NULL,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080098 dinfo->i_location.partitionReferenceNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070099 start, err);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700100 if (*err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 iput(inode);
102 return NULL;
103 }
104
Ingo Molnar1e7933d2006-03-23 03:00:44 -0800105 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800106 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800107 struct logicalVolIntegrityDesc *lvid =
108 (struct logicalVolIntegrityDesc *)
109 sbi->s_lvid_bh->b_data;
110 struct logicalVolIntegrityDescImpUse *lvidiu =
111 udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 struct logicalVolHeaderDesc *lvhd;
113 uint64_t uniqueID;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800114 lvhd = (struct logicalVolHeaderDesc *)
115 (lvid->logicalVolContentsUse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (S_ISDIR(mode))
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100117 le32_add_cpu(&lvidiu->numDirs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 else
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100119 le32_add_cpu(&lvidiu->numFiles, 1);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800120 iinfo->i_unique = uniqueID = le64_to_cpu(lvhd->uniqueID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
122 uniqueID += 16;
123 lvhd->uniqueID = cpu_to_le64(uniqueID);
Jan Kara146bca72009-03-16 18:27:37 +0100124 udf_updated_lvid(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
Jan Karadb0badc2008-08-18 13:40:18 +0200126 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 inode->i_mode = mode;
David Howells7706bb32008-11-14 10:39:03 +1100128 inode->i_uid = current_fsuid();
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700129 if (dir->i_mode & S_ISGID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 inode->i_gid = dir->i_gid;
131 if (S_ISDIR(mode))
132 mode |= S_ISGID;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700133 } else {
David Howells7706bb32008-11-14 10:39:03 +1100134 inode->i_gid = current_fsgid();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800137 iinfo->i_location.logicalBlockNum = block;
138 iinfo->i_location.partitionReferenceNum =
139 dinfo->i_location.partitionReferenceNum;
Pekka Enberg97e961f2008-10-15 12:29:03 +0200140 inode->i_ino = udf_get_lb_pblock(sb, &iinfo->i_location, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 inode->i_blocks = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800142 iinfo->i_lenEAttr = 0;
143 iinfo->i_lenAlloc = 0;
144 iinfo->i_use = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800146 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800148 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800150 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 inode->i_mtime = inode->i_atime = inode->i_ctime =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800152 iinfo->i_crtime = current_fs_time(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 insert_inode_hash(inode);
154 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Christoph Hellwig871a2932010-03-03 09:05:07 -0500156 dquot_initialize(inode);
Christoph Hellwig63936dd2010-03-03 09:05:01 -0500157 ret = dquot_alloc_inode(inode);
158 if (ret) {
Christoph Hellwig9f754752010-03-03 09:05:05 -0500159 dquot_drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 inode->i_flags |= S_NOQUOTA;
161 inode->i_nlink = 0;
162 iput(inode);
Christoph Hellwig63936dd2010-03-03 09:05:01 -0500163 *err = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return NULL;
165 }
166
167 *err = 0;
168 return inode;
169}