blob: 05ab48195be96f0f38587493ec57423d1febc083 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/sched.h>
24#include <linux/slab.h>
25
26#include "udf_i.h"
27#include "udf_sb.h"
28
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070029void udf_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 struct super_block *sb = inode->i_sb;
32 struct udf_sb_info *sbi = UDF_SB(sb);
33
Ingo Molnar1e7933d2006-03-23 03:00:44 -080034 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -080035 if (sbi->s_lvid_bh) {
36 struct logicalVolIntegrityDescImpUse *lvidiu =
37 udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 if (S_ISDIR(inode->i_mode))
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +010039 le32_add_cpu(&lvidiu->numDirs, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 else
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +010041 le32_add_cpu(&lvidiu->numFiles, -1);
Jan Kara146bca72009-03-16 18:27:37 +010042 udf_updated_lvid(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 }
Ingo Molnar1e7933d2006-03-23 03:00:44 -080044 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Pekka Enberg97e961f2008-10-15 12:29:03 +020046 udf_free_blocks(sb, NULL, &UDF_I(inode)->i_location, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Al Virofaa17292011-07-26 03:18:29 -040049struct inode *udf_new_inode(struct inode *dir, umode_t mode, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 struct super_block *sb = dir->i_sb;
52 struct udf_sb_info *sbi = UDF_SB(sb);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070053 struct inode *inode;
Jan Kara36350462010-05-19 16:28:56 +020054 int block;
Marcin Slusarzc0b34432008-02-08 04:20:42 -080055 uint32_t start = UDF_I(dir)->i_location.logicalBlockNum;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080056 struct udf_inode_info *iinfo;
57 struct udf_inode_info *dinfo = UDF_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 inode = new_inode(sb);
60
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070061 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 *err = -ENOMEM;
63 return NULL;
64 }
65 *err = -ENOSPC;
66
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080067 iinfo = UDF_I(inode);
Jan Kara97e1cfb2008-08-18 13:44:48 +020068 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_EXTENDED_FE)) {
69 iinfo->i_efe = 1;
70 if (UDF_VERS_USE_EXTENDED_FE > sbi->s_udfrev)
71 sbi->s_udfrev = UDF_VERS_USE_EXTENDED_FE;
72 iinfo->i_ext.i_data = kzalloc(inode->i_sb->s_blocksize -
73 sizeof(struct extendedFileEntry),
74 GFP_KERNEL);
75 } else {
76 iinfo->i_efe = 0;
77 iinfo->i_ext.i_data = kzalloc(inode->i_sb->s_blocksize -
78 sizeof(struct fileEntry),
79 GFP_KERNEL);
80 }
81 if (!iinfo->i_ext.i_data) {
82 iput(inode);
83 *err = -ENOMEM;
84 return NULL;
85 }
Eric Sandeen225add62006-08-05 12:15:17 -070086
Marcin Slusarz4b111112008-02-08 04:20:36 -080087 block = udf_new_block(dir->i_sb, NULL,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080088 dinfo->i_location.partitionReferenceNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070089 start, err);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070090 if (*err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 iput(inode);
92 return NULL;
93 }
94
Marcin Slusarz6c79e982008-02-08 04:20:30 -080095 if (sbi->s_lvid_bh) {
Jan Karad664b6a2010-10-20 18:28:46 +020096 struct logicalVolIntegrityDescImpUse *lvidiu;
97
98 iinfo->i_unique = lvid_get_unique_id(sb);
99 mutex_lock(&sbi->s_alloc_mutex);
100 lvidiu = udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (S_ISDIR(mode))
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100102 le32_add_cpu(&lvidiu->numDirs, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 else
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +0100104 le32_add_cpu(&lvidiu->numFiles, 1);
Jan Kara146bca72009-03-16 18:27:37 +0100105 udf_updated_lvid(sb);
Jan Karad664b6a2010-10-20 18:28:46 +0200106 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
Dmitry Monakhova6c5a032010-03-04 17:32:22 +0300108
109 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800111 iinfo->i_location.logicalBlockNum = block;
112 iinfo->i_location.partitionReferenceNum =
113 dinfo->i_location.partitionReferenceNum;
Pekka Enberg97e961f2008-10-15 12:29:03 +0200114 inode->i_ino = udf_get_lb_pblock(sb, &iinfo->i_location, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 inode->i_blocks = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800116 iinfo->i_lenEAttr = 0;
117 iinfo->i_lenAlloc = 0;
118 iinfo->i_use = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_AD_IN_ICB))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800120 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 else if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800122 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800124 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 inode->i_mtime = inode->i_atime = inode->i_ctime =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800126 iinfo->i_crtime = current_fs_time(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 insert_inode_hash(inode);
128 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 *err = 0;
131 return inode;
132}