blob: 8a3fbd177cab342000164e0a934cc41c5bdec30e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * inode.c
3 *
4 * PURPOSE
5 * Inode 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 Dave Boynton
14 * (C) 1998-2004 Ben Fennema
15 * (C) 1999-2000 Stelias Computing Inc
16 *
17 * HISTORY
18 *
19 * 10/04/98 dgb Added rudimentary directory functions
20 * 10/07/98 Fully working udf_block_map! It works!
21 * 11/25/98 bmap altered to better support extents
Marcin Slusarz4b111112008-02-08 04:20:36 -080022 * 12/06/98 blf partition support in udf_iget, udf_block_map
23 * and udf_read_inode
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 12/12/98 rewrote udf_block_map to handle next extents and descs across
25 * block boundaries (which is not actually allowed)
26 * 12/20/98 added support for strategy 4096
27 * 03/07/99 rewrote udf_block_map (again)
28 * New funcs, inode_bmap, udf_next_aext
29 * 04/19/99 Support for writing device EA's for major/minor #
30 */
31
32#include "udfdecl.h"
33#include <linux/mm.h>
34#include <linux/smp_lock.h>
35#include <linux/module.h>
36#include <linux/pagemap.h>
37#include <linux/buffer_head.h>
38#include <linux/writeback.h>
Christoph Hellwig907f4552010-03-03 09:05:06 -050039#include <linux/quotaops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/slab.h>
Bob Copelandf845fce2008-04-17 09:47:48 +020041#include <linux/crc-itu-t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "udf_i.h"
44#include "udf_sb.h"
45
46MODULE_AUTHOR("Ben Fennema");
47MODULE_DESCRIPTION("Universal Disk Format Filesystem");
48MODULE_LICENSE("GPL");
49
50#define EXTENT_MERGE_SIZE 5
51
52static mode_t udf_convert_permissions(struct fileEntry *);
53static int udf_update_inode(struct inode *, int);
54static void udf_fill_inode(struct inode *, struct buffer_head *);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -070055static int udf_alloc_i_data(struct inode *inode, size_t size);
Jan Kara60448b12007-05-08 00:35:13 -070056static struct buffer_head *inode_getblk(struct inode *, sector_t, int *,
Marcin Slusarz1ed16172008-02-08 04:20:48 -080057 sector_t *, int *);
Jan Karaff116fc2007-05-08 00:35:14 -070058static int8_t udf_insert_aext(struct inode *, struct extent_position,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020059 struct kernel_lb_addr, uint32_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static void udf_split_extents(struct inode *, int *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020061 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void udf_prealloc_extents(struct inode *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020063 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static void udf_merge_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020065 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void udf_update_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020067 struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070068 struct extent_position *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
70
Christoph Hellwigb1e32122008-02-22 12:38:48 +010071
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070072void udf_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Christoph Hellwig907f4552010-03-03 09:05:06 -050074 if (!is_bad_inode(inode))
Christoph Hellwig871a2932010-03-03 09:05:07 -050075 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -050076
Mark Fashehfef26652005-09-09 13:01:31 -070077 truncate_inode_pages(&inode->i_data, 0);
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (is_bad_inode(inode))
80 goto no_delete;
81
82 inode->i_size = 0;
83 udf_truncate(inode);
84 lock_kernel();
85
86 udf_update_inode(inode, IS_SYNC(inode));
87 udf_free_inode(inode);
88
89 unlock_kernel();
90 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070091
92no_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 clear_inode(inode);
94}
95
Jan Kara74584ae2007-06-16 10:16:14 -070096/*
Jan Kara81056dd2009-07-16 18:02:25 +020097 * If we are going to release inode from memory, we truncate last inode extent
98 * to proper length. We could use drop_inode() but it's called under inode_lock
99 * and thus we cannot mark inode dirty there. We use clear_inode() but we have
100 * to make sure to write inode as it's not written automatically.
Jan Kara74584ae2007-06-16 10:16:14 -0700101 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102void udf_clear_inode(struct inode *inode)
103{
Jan Kara2c948b32009-12-03 13:39:28 +0100104 struct udf_inode_info *iinfo = UDF_I(inode);
105
106 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
107 inode->i_size != iinfo->i_lenExtents) {
108 printk(KERN_WARNING "UDF-fs (%s): Inode %lu (mode %o) has "
Daniel Mack1537a362010-01-29 15:57:49 +0800109 "inode size %llu different from extent length %llu. "
Jan Kara2c948b32009-12-03 13:39:28 +0100110 "Filesystem need not be standards compliant.\n",
111 inode->i_sb->s_id, inode->i_ino, inode->i_mode,
112 (unsigned long long)inode->i_size,
113 (unsigned long long)iinfo->i_lenExtents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
Christoph Hellwig257ba152010-03-03 09:05:04 -0500115
Christoph Hellwig9f754752010-03-03 09:05:05 -0500116 dquot_drop(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800117 kfree(iinfo->i_ext.i_data);
118 iinfo->i_ext.i_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121static int udf_writepage(struct page *page, struct writeback_control *wbc)
122{
123 return block_write_full_page(page, udf_get_block, wbc);
124}
125
126static int udf_readpage(struct file *file, struct page *page)
127{
128 return block_read_full_page(page, udf_get_block);
129}
130
Nick Pigginbe021ee2007-10-16 01:25:20 -0700131static int udf_write_begin(struct file *file, struct address_space *mapping,
132 loff_t pos, unsigned len, unsigned flags,
133 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Nick Pigginbe021ee2007-10-16 01:25:20 -0700135 *pagep = NULL;
136 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
137 udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140static sector_t udf_bmap(struct address_space *mapping, sector_t block)
141{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700142 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700145const struct address_space_operations udf_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700146 .readpage = udf_readpage,
147 .writepage = udf_writepage,
148 .sync_page = block_sync_page,
Nick Pigginbe021ee2007-10-16 01:25:20 -0700149 .write_begin = udf_write_begin,
150 .write_end = generic_write_end,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700151 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};
153
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700154void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct page *page;
157 char *kaddr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800158 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 struct writeback_control udf_wbc = {
160 .sync_mode = WB_SYNC_NONE,
161 .nr_to_write = 1,
162 };
163
164 /* from now on we have normal address_space methods */
165 inode->i_data.a_ops = &udf_aops;
166
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800167 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800169 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800171 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 mark_inode_dirty(inode);
173 return;
174 }
175
176 page = grab_cache_page(inode->i_mapping, 0);
Matt Mackallcd7619d2005-05-01 08:59:01 -0700177 BUG_ON(!PageLocked(page));
178
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700179 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 kaddr = kmap(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800181 memset(kaddr + iinfo->i_lenAlloc, 0x00,
182 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
183 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
184 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 flush_dcache_page(page);
186 SetPageUptodate(page);
187 kunmap(page);
188 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800189 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
190 iinfo->i_lenAlloc);
191 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800193 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800195 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 inode->i_data.a_ops->writepage(page, &udf_wbc);
198 page_cache_release(page);
199
200 mark_inode_dirty(inode);
201}
202
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700203struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
204 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700207 struct buffer_head *dbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200208 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700210 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 struct udf_fileident_bh sfibh, dfibh;
Jan Karaaf793292008-02-08 04:20:50 -0800213 loff_t f_pos = udf_ext0_offset(inode);
214 int size = udf_ext0_offset(inode) + inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800216 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
219 alloctype = ICBTAG_FLAG_AD_SHORT;
220 else
221 alloctype = ICBTAG_FLAG_AD_LONG;
222
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700223 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800224 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 mark_inode_dirty(inode);
226 return NULL;
227 }
228
229 /* alloc block, and copy data to it */
230 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800231 iinfo->i_location.partitionReferenceNum,
232 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (!(*block))
234 return NULL;
235 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800236 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800237 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (!newblock)
239 return NULL;
240 dbh = udf_tgetblk(inode->i_sb, newblock);
241 if (!dbh)
242 return NULL;
243 lock_buffer(dbh);
244 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
245 set_buffer_uptodate(dbh);
246 unlock_buffer(dbh);
247 mark_buffer_dirty_inode(dbh, inode);
248
Marcin Slusarz4b111112008-02-08 04:20:36 -0800249 sfibh.soffset = sfibh.eoffset =
Jan Karaaf793292008-02-08 04:20:50 -0800250 f_pos & (inode->i_sb->s_blocksize - 1);
Jan Karaff116fc2007-05-08 00:35:14 -0700251 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 dfibh.soffset = dfibh.eoffset = 0;
253 dfibh.sbh = dfibh.ebh = dbh;
Jan Karaaf793292008-02-08 04:20:50 -0800254 while (f_pos < size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800255 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800256 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
257 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700258 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700259 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return NULL;
261 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800262 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 sfi->descTag.tagLocation = cpu_to_le32(*block);
264 dfibh.soffset = dfibh.eoffset;
265 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
266 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
267 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800268 sfi->fileIdent +
269 le16_to_cpu(sfi->lengthOfImpUse))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800270 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700271 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return NULL;
273 }
274 }
275 mark_buffer_dirty_inode(dbh, inode);
276
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800277 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
278 iinfo->i_lenAlloc);
279 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800281 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800282 iinfo->i_location.partitionReferenceNum;
Jan Kara2c948b32009-12-03 13:39:28 +0100283 iinfo->i_lenExtents = inode->i_size;
Jan Karaff116fc2007-05-08 00:35:14 -0700284 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800285 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700286 epos.offset = udf_file_entry_alloc_offset(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100287 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /* UniqueID stuff */
289
Jan Kara3bf25cb2007-05-08 00:35:16 -0700290 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 mark_inode_dirty(inode);
292 return dbh;
293}
294
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700295static int udf_get_block(struct inode *inode, sector_t block,
296 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 int err, new;
299 struct buffer_head *bh;
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800300 sector_t phys = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800301 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700303 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 phys = udf_block_map(inode, block);
305 if (phys)
306 map_bh(bh_result, inode->i_sb, phys);
307 return 0;
308 }
309
310 err = -EIO;
311 new = 0;
312 bh = NULL;
313
314 lock_kernel();
315
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800316 iinfo = UDF_I(inode);
317 if (block == iinfo->i_next_alloc_block + 1) {
318 iinfo->i_next_alloc_block++;
319 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321
322 err = 0;
323
324 bh = inode_getblk(inode, block, &err, &phys, &new);
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200325 BUG_ON(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (err)
327 goto abort;
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200328 BUG_ON(!phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 if (new)
331 set_buffer_new(bh_result);
332 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700333
334abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 unlock_kernel();
336 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700339static struct buffer_head *udf_getblk(struct inode *inode, long block,
340 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700342 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct buffer_head dummy;
344
345 dummy.b_state = 0;
346 dummy.b_blocknr = -1000;
347 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700348 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700350 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 lock_buffer(bh);
352 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
353 set_buffer_uptodate(bh);
354 unlock_buffer(bh);
355 mark_buffer_dirty_inode(bh, inode);
356 }
357 return bh;
358 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return NULL;
361}
362
Jan Kara31170b62007-05-08 00:35:21 -0700363/* Extend the file by 'blocks' blocks, return the number of extents added */
364int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200365 struct kernel_long_ad *last_ext, sector_t blocks)
Jan Kara31170b62007-05-08 00:35:21 -0700366{
367 sector_t add;
368 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
369 struct super_block *sb = inode->i_sb;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200370 struct kernel_lb_addr prealloc_loc = {};
Jan Kara31170b62007-05-08 00:35:21 -0700371 int prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800372 struct udf_inode_info *iinfo;
Jan Kara31170b62007-05-08 00:35:21 -0700373
374 /* The previous extent is fake and we should not extend by anything
375 * - there's nothing to do... */
376 if (!blocks && fake)
377 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700378
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800379 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700380 /* Round the last extent up to a multiple of block size */
381 if (last_ext->extLength & (sb->s_blocksize - 1)) {
382 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700383 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
384 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
385 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800386 iinfo->i_lenExtents =
387 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700388 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700389 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700390
Jan Kara31170b62007-05-08 00:35:21 -0700391 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800392 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
393 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700394 /* Save the extent so that we can reattach it to the end */
395 prealloc_loc = last_ext->extLocation;
396 prealloc_len = last_ext->extLength;
397 /* Mark the extent as a hole */
398 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700399 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700400 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800401 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700402 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700403
Jan Kara31170b62007-05-08 00:35:21 -0700404 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800405 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
406 EXT_NOT_RECORDED_NOT_ALLOCATED) {
407 add = ((1 << 30) - sb->s_blocksize -
408 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
409 sb->s_blocksize_bits;
Jan Kara31170b62007-05-08 00:35:21 -0700410 if (add > blocks)
411 add = blocks;
412 blocks -= add;
413 last_ext->extLength += add << sb->s_blocksize_bits;
414 }
415
416 if (fake) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200417 udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700418 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700419 count++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800420 } else
Pekka Enberg97e961f2008-10-15 12:29:03 +0200421 udf_write_aext(inode, last_pos, &last_ext->extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800422 last_ext->extLength, 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700423
Jan Kara31170b62007-05-08 00:35:21 -0700424 /* Managed to do everything necessary? */
425 if (!blocks)
426 goto out;
427
428 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
429 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800430 last_ext->extLocation.partitionReferenceNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700431 add = (1 << (30-sb->s_blocksize_bits)) - 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800432 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
433 (add << sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700434
Jan Kara31170b62007-05-08 00:35:21 -0700435 /* Create enough extents to cover the whole hole */
436 while (blocks > add) {
437 blocks -= add;
Pekka Enberg97e961f2008-10-15 12:29:03 +0200438 if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700439 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700440 return -1;
441 count++;
442 }
443 if (blocks) {
444 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700445 (blocks << sb->s_blocksize_bits);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200446 if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700447 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700448 return -1;
449 count++;
450 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700451
452out:
Jan Kara31170b62007-05-08 00:35:21 -0700453 /* Do we have some preallocated blocks saved? */
454 if (prealloc_len) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200455 if (udf_add_aext(inode, last_pos, &prealloc_loc,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800456 prealloc_len, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700457 return -1;
458 last_ext->extLocation = prealloc_loc;
459 last_ext->extLength = prealloc_len;
460 count++;
461 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700462
Jan Kara31170b62007-05-08 00:35:21 -0700463 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800464 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200465 last_pos->offset -= sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800466 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200467 last_pos->offset -= sizeof(struct long_ad);
Jan Kara31170b62007-05-08 00:35:21 -0700468 else
469 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700470
Jan Kara31170b62007-05-08 00:35:21 -0700471 return count;
472}
473
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700474static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800475 int *err, sector_t *phys, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Jan Kara31170b62007-05-08 00:35:21 -0700477 static sector_t last_block;
Jan Karaff116fc2007-05-08 00:35:14 -0700478 struct buffer_head *result = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200479 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700480 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700482 uint32_t elen = 0, tmpelen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200483 struct kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700485 loff_t lbcount = 0, b_off = 0;
486 uint32_t newblocknum, newblock;
487 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800489 struct udf_inode_info *iinfo = UDF_I(inode);
490 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700491 int lastblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Jan Karaff116fc2007-05-08 00:35:14 -0700493 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800494 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700495 prev_epos.bh = NULL;
496 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700497 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700500 alternate between laarr[0] and laarr[1] for locations of the
501 current extent, and the previous extent */
502 do {
503 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700504 brelse(prev_epos.bh);
505 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700506 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700508 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700509 brelse(cur_epos.bh);
510 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700511 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
514 lbcount += elen;
515
Jan Karaff116fc2007-05-08 00:35:14 -0700516 prev_epos.block = cur_epos.block;
517 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Jan Karaff116fc2007-05-08 00:35:14 -0700519 prev_epos.offset = cur_epos.offset;
520 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Marcin Slusarz4b111112008-02-08 04:20:36 -0800522 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
523 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 break;
525
526 c = !c;
527
528 laarr[c].extLength = (etype << 30) | elen;
529 laarr[c].extLocation = eloc;
530
531 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
532 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700533 ((elen + inode->i_sb->s_blocksize - 1) >>
534 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700536 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 } while (lbcount + elen <= b_off);
538
539 b_off -= lbcount;
540 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700541 /*
542 * Move prev_epos and cur_epos into indirect extent if we are at
543 * the pointer to it
544 */
545 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
546 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700549 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700551 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
552 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700554 ((elen + inode->i_sb->s_blocksize - 1) &
555 ~(inode->i_sb->s_blocksize - 1));
Pekka Enberg97e961f2008-10-15 12:29:03 +0200556 etype = udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700558 brelse(prev_epos.bh);
559 brelse(cur_epos.bh);
560 brelse(next_epos.bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200561 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 *phys = newblock;
563 return NULL;
564 }
565
Jan Kara31170b62007-05-08 00:35:21 -0700566 last_block = block;
567 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700568 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700569 int ret;
570
571 if (count) {
572 if (c)
573 laarr[0] = laarr[1];
574 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700575 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700576 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800577 memset(&laarr[0].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200578 sizeof(struct kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700579 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800580 /* Will udf_extend_file() create real extent from
581 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700582 startnum = (offset > 0);
583 }
584 /* Create extents for the hole between EOF and offset */
585 ret = udf_extend_file(inode, &prev_epos, laarr, offset);
586 if (ret == -1) {
587 brelse(prev_epos.bh);
588 brelse(cur_epos.bh);
589 brelse(next_epos.bh);
590 /* We don't really know the error here so we just make
591 * something up */
592 *err = -ENOSPC;
593 return NULL;
594 }
595 c = 0;
596 offset = 0;
597 count += ret;
598 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800599 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
600 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700601 /* Is there any real extent? - otherwise we overwrite
602 * the fake one... */
603 if (count)
604 c = !c;
605 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700606 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800607 memset(&laarr[c].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200608 sizeof(struct kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700609 count++;
610 endnum++;
Jan Kara31170b62007-05-08 00:35:21 -0700611 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700612 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700614 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 endnum = startnum = ((count > 2) ? 2 : count);
616
Marcin Slusarz4b111112008-02-08 04:20:36 -0800617 /* if the current extent is in position 0,
618 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700619 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700620 laarr[2] = laarr[0];
621 laarr[0] = laarr[1];
622 laarr[1] = laarr[2];
623 c = 1;
624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Marcin Slusarz4b111112008-02-08 04:20:36 -0800626 /* if the current block is located in an extent,
627 read the next extent */
628 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
629 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700630 laarr[c + 1].extLength = (etype << 30) | elen;
631 laarr[c + 1].extLocation = eloc;
632 count++;
633 startnum++;
634 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800635 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 lastblock = 1;
637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700640 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800641 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800643 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800644 if (iinfo->i_next_alloc_block == block)
645 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700647 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800648 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800649 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
Marcin Slusarz4b111112008-02-08 04:20:36 -0800652 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800653 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800654 goal, err);
655 if (!newblocknum) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700656 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 *err = -ENOSPC;
658 return NULL;
659 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800660 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
Marcin Slusarz4b111112008-02-08 04:20:36 -0800663 /* if the extent the requsted block is located in contains multiple
664 * blocks, split the extent into at most three extents. blocks prior
665 * to requested block, requested block, and blocks after requested
666 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
668
669#ifdef UDF_PREALLOCATE
Jan Kara81056dd2009-07-16 18:02:25 +0200670 /* We preallocate blocks only for regular files. It also makes sense
671 * for directories but there's a problem when to drop the
672 * preallocation. We might use some delayed work for that but I feel
673 * it's overengineering for a filesystem like UDF. */
674 if (S_ISREG(inode->i_mode))
675 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676#endif
677
678 /* merge any continuous blocks in laarr */
679 udf_merge_extents(inode, laarr, &endnum);
680
681 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700682 * of extents is greater than the old number, and deleting extents if
683 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700684 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Jan Kara3bf25cb2007-05-08 00:35:16 -0700686 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Marcin Slusarz4b111112008-02-08 04:20:36 -0800688 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800689 iinfo->i_location.partitionReferenceNum, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800690 if (!newblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 *phys = newblock;
693 *err = 0;
694 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800695 iinfo->i_next_alloc_block = block;
696 iinfo->i_next_alloc_goal = newblocknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 inode->i_ctime = current_fs_time(inode->i_sb);
698
699 if (IS_SYNC(inode))
700 udf_sync_inode(inode);
701 else
702 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return result;
705}
706
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700707static void udf_split_extents(struct inode *inode, int *c, int offset,
708 int newblocknum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200709 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700710 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800712 unsigned long blocksize = inode->i_sb->s_blocksize;
713 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800716 (laarr[*c].extLength >> 30) ==
717 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 int curr = *c;
719 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800720 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 int8_t etype = (laarr[curr].extLength >> 30);
722
Marcin Slusarz4b111112008-02-08 04:20:36 -0800723 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700724 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800725 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700726 laarr[curr + 2] = laarr[curr + 1];
727 laarr[curr + 1] = laarr[curr];
728 } else {
729 laarr[curr + 3] = laarr[curr + 1];
730 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700733 if (offset) {
734 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800735 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200736 &laarr[curr].extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800737 0, offset);
738 laarr[curr].extLength =
739 EXT_NOT_RECORDED_NOT_ALLOCATED |
740 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800742 laarr[curr].extLocation.
743 partitionReferenceNum = 0;
744 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800746 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700747 curr++;
748 (*c)++;
749 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 laarr[curr].extLocation.logicalBlockNum = newblocknum;
753 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
754 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800755 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800757 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700758 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700760 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800762 laarr[curr].extLocation.logicalBlockNum +=
763 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700764 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800765 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700766 curr++;
767 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 }
770}
771
772static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200773 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700774 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 int start, length = 0, currlength = 0, i;
777
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700778 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (!lastblock)
780 return;
781 else
782 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700783 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800784 if ((laarr[c + 1].extLength >> 30) ==
785 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700786 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800787 length = currlength =
788 (((laarr[c + 1].extLength &
789 UDF_EXTENT_LENGTH_MASK) +
790 inode->i_sb->s_blocksize - 1) >>
791 inode->i_sb->s_blocksize_bits);
792 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 start = c;
794 }
795
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700796 for (i = start + 1; i <= *endnum; i++) {
797 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (lastblock)
799 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800800 } else if ((laarr[i].extLength >> 30) ==
801 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
802 length += (((laarr[i].extLength &
803 UDF_EXTENT_LENGTH_MASK) +
804 inode->i_sb->s_blocksize - 1) >>
805 inode->i_sb->s_blocksize_bits);
806 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 break;
808 }
809
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700810 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700812 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800813 inode->i_sb->s_blocksize - 1) >>
814 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800816 laarr[start].extLocation.partitionReferenceNum,
817 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
818 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
819 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700820 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800821 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800823 (numalloc <<
824 inode->i_sb->s_blocksize_bits);
825 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700826 memmove(&laarr[c + 2], &laarr[c + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200827 sizeof(struct long_ad) * (*endnum - (c + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700828 (*endnum)++;
829 laarr[c + 1].extLocation.logicalBlockNum = next;
830 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800831 laarr[c].extLocation.
832 partitionReferenceNum;
833 laarr[c + 1].extLength =
834 EXT_NOT_RECORDED_ALLOCATED |
835 (numalloc <<
836 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700837 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700840 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800841 int elen = ((laarr[i].extLength &
842 UDF_EXTENT_LENGTH_MASK) +
843 inode->i_sb->s_blocksize - 1) >>
844 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700846 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800848 (numalloc <<
849 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700851 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700853 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800854 memmove(&laarr[i],
855 &laarr[i + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200856 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -0800857 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700858 i--;
859 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800862 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800863 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865 }
866}
867
868static void udf_merge_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200869 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700870 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800873 unsigned long blocksize = inode->i_sb->s_blocksize;
874 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700876 for (i = 0; i < (*endnum - 1); i++) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200877 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
878 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Marcin Slusarz4b111112008-02-08 04:20:36 -0800880 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
881 (((li->extLength >> 30) ==
882 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
883 ((lip1->extLocation.logicalBlockNum -
884 li->extLocation.logicalBlockNum) ==
885 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
886 blocksize - 1) >> blocksize_bits)))) {
887
888 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
889 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
890 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
891 lip1->extLength = (lip1->extLength -
892 (li->extLength &
893 UDF_EXTENT_LENGTH_MASK) +
894 UDF_EXTENT_LENGTH_MASK) &
895 ~(blocksize - 1);
896 li->extLength = (li->extLength &
897 UDF_EXTENT_FLAG_MASK) +
898 (UDF_EXTENT_LENGTH_MASK + 1) -
899 blocksize;
900 lip1->extLocation.logicalBlockNum =
901 li->extLocation.logicalBlockNum +
902 ((li->extLength &
903 UDF_EXTENT_LENGTH_MASK) >>
904 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700905 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800906 li->extLength = lip1->extLength +
907 (((li->extLength &
908 UDF_EXTENT_LENGTH_MASK) +
909 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700910 if (*endnum > (i + 2))
911 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200912 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -0800913 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700914 i--;
915 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800917 } else if (((li->extLength >> 30) ==
918 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
919 ((lip1->extLength >> 30) ==
920 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200921 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800922 ((li->extLength &
923 UDF_EXTENT_LENGTH_MASK) +
924 blocksize - 1) >> blocksize_bits);
925 li->extLocation.logicalBlockNum = 0;
926 li->extLocation.partitionReferenceNum = 0;
927
928 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
929 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
930 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
931 lip1->extLength = (lip1->extLength -
932 (li->extLength &
933 UDF_EXTENT_LENGTH_MASK) +
934 UDF_EXTENT_LENGTH_MASK) &
935 ~(blocksize - 1);
936 li->extLength = (li->extLength &
937 UDF_EXTENT_FLAG_MASK) +
938 (UDF_EXTENT_LENGTH_MASK + 1) -
939 blocksize;
940 } else {
941 li->extLength = lip1->extLength +
942 (((li->extLength &
943 UDF_EXTENT_LENGTH_MASK) +
944 blocksize - 1) & ~(blocksize - 1));
945 if (*endnum > (i + 2))
946 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200947 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -0800948 (*endnum - (i + 2)));
949 i--;
950 (*endnum)--;
951 }
952 } else if ((li->extLength >> 30) ==
953 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
954 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200955 &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800956 ((li->extLength &
957 UDF_EXTENT_LENGTH_MASK) +
958 blocksize - 1) >> blocksize_bits);
959 li->extLocation.logicalBlockNum = 0;
960 li->extLocation.partitionReferenceNum = 0;
961 li->extLength = (li->extLength &
962 UDF_EXTENT_LENGTH_MASK) |
963 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965 }
966}
967
968static void udf_update_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200969 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700970 int startnum, int endnum,
971 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 int start = 0, i;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200974 struct kernel_lb_addr tmploc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 uint32_t tmplen;
976
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700977 if (startnum > endnum) {
978 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -0700979 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700980 laarr[i].extLength);
981 } else if (startnum < endnum) {
982 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -0700983 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700984 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -0700985 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700986 &laarr[i].extLength, 1);
987 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989 }
990
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700991 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -0700992 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200993 udf_write_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700994 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996}
997
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700998struct buffer_head *udf_bread(struct inode *inode, int block,
999 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001001 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 bh = udf_getblk(inode, block, create, err);
1004 if (!bh)
1005 return NULL;
1006
1007 if (buffer_uptodate(bh))
1008 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 ll_rw_block(READ, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 wait_on_buffer(bh);
1013 if (buffer_uptodate(bh))
1014 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 brelse(bh);
1017 *err = -EIO;
1018 return NULL;
1019}
1020
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001021void udf_truncate(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 int offset;
1024 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001025 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001028 S_ISLNK(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return;
1030 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1031 return;
1032
1033 lock_kernel();
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001034 iinfo = UDF_I(inode);
1035 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001036 if (inode->i_sb->s_blocksize <
1037 (udf_file_entry_alloc_offset(inode) +
1038 inode->i_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 udf_expand_file_adinicb(inode, inode->i_size, &err);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001040 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1041 inode->i_size = iinfo->i_lenAlloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 unlock_kernel();
1043 return;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001044 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 udf_truncate_extents(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001046 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001048 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + offset,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001049 0x00, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001050 offset - udf_file_entry_alloc_offset(inode));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001051 iinfo->i_lenAlloc = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001053 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001054 block_truncate_page(inode->i_mapping, inode->i_size,
1055 udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 udf_truncate_extents(inode);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1060 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001061 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 else
1063 mark_inode_dirty(inode);
1064 unlock_kernel();
1065}
1066
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001067static void __udf_read_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 struct buffer_head *bh = NULL;
1070 struct fileEntry *fe;
1071 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001072 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 /*
1075 * Set defaults, but the inode is still incomplete!
1076 * Note: get_new_inode() sets the following on a new inode:
1077 * i_sb = sb
1078 * i_no = ino
1079 * i_flags = sb->s_flags
1080 * i_state = 0
1081 * clean_inode(): zero fills and sets
1082 * i_count = 1
1083 * i_nlink = 1
1084 * i_op = NULL;
1085 */
Pekka Enberg97e961f2008-10-15 12:29:03 +02001086 bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001087 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001089 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 make_bad_inode(inode);
1091 return;
1092 }
1093
1094 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001095 ident != TAG_IDENT_USE) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001096 printk(KERN_ERR "udf: udf_read_inode(ino %ld) "
1097 "failed ident=%d\n", inode->i_ino, ident);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001098 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 make_bad_inode(inode);
1100 return;
1101 }
1102
1103 fe = (struct fileEntry *)bh->b_data;
1104
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001105 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001106 struct buffer_head *ibh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Pekka Enberg97e961f2008-10-15 12:29:03 +02001108 ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001109 &ident);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001110 if (ident == TAG_IDENT_IE && ibh) {
1111 struct buffer_head *nbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001112 struct kernel_lb_addr loc;
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001113 struct indirectEntry *ie;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001114
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001115 ie = (struct indirectEntry *)ibh->b_data;
1116 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001117
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001118 if (ie->indirectICB.extLength &&
Pekka Enberg97e961f2008-10-15 12:29:03 +02001119 (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001120 &ident))) {
1121 if (ident == TAG_IDENT_FE ||
1122 ident == TAG_IDENT_EFE) {
1123 memcpy(&iinfo->i_location,
1124 &loc,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001125 sizeof(struct kernel_lb_addr));
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001126 brelse(bh);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001127 brelse(ibh);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001128 brelse(nbh);
1129 __udf_read_inode(inode);
1130 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001131 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001132 brelse(nbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001134 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001135 brelse(ibh);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001136 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 printk(KERN_ERR "udf: unsupported strategy type: %d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001138 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001139 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 make_bad_inode(inode);
1141 return;
1142 }
1143 udf_fill_inode(inode, bh);
Jan Kara31170b62007-05-08 00:35:21 -07001144
Jan Kara3bf25cb2007-05-08 00:35:16 -07001145 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
1148static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1149{
1150 struct fileEntry *fe;
1151 struct extendedFileEntry *efe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 int offset;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001153 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001154 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 fe = (struct fileEntry *)bh->b_data;
1157 efe = (struct extendedFileEntry *)bh->b_data;
1158
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001159 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001160 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001161 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001162 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001164 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001165 ICBTAG_FLAG_AD_MASK;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001166 iinfo->i_unique = 0;
1167 iinfo->i_lenEAttr = 0;
1168 iinfo->i_lenExtents = 0;
1169 iinfo->i_lenAlloc = 0;
1170 iinfo->i_next_alloc_block = 0;
1171 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001172 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001173 iinfo->i_efe = 1;
1174 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001175 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1176 sizeof(struct extendedFileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001177 make_bad_inode(inode);
1178 return;
1179 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001180 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001181 bh->b_data + sizeof(struct extendedFileEntry),
1182 inode->i_sb->s_blocksize -
1183 sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001184 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001185 iinfo->i_efe = 0;
1186 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001187 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1188 sizeof(struct fileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001189 make_bad_inode(inode);
1190 return;
1191 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001192 memcpy(iinfo->i_ext.i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001193 bh->b_data + sizeof(struct fileEntry),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001194 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001195 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001196 iinfo->i_efe = 0;
1197 iinfo->i_use = 1;
1198 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001199 ((struct unallocSpaceEntry *)bh->b_data)->
1200 lengthAllocDescs);
1201 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1202 sizeof(struct unallocSpaceEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001203 make_bad_inode(inode);
1204 return;
1205 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001206 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001207 bh->b_data + sizeof(struct unallocSpaceEntry),
1208 inode->i_sb->s_blocksize -
1209 sizeof(struct unallocSpaceEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return;
1211 }
1212
1213 inode->i_uid = le32_to_cpu(fe->uid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001214 if (inode->i_uid == -1 ||
1215 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1216 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001217 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 inode->i_gid = le32_to_cpu(fe->gid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001220 if (inode->i_gid == -1 ||
1221 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1222 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001223 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
1226 if (!inode->i_nlink)
1227 inode->i_nlink = 1;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 inode->i_size = le64_to_cpu(fe->informationLength);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001230 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +01001232 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001233 sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +01001234 inode->i_mode = sbi->s_fmode;
1235 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001236 sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +01001237 inode->i_mode = sbi->s_dmode;
1238 else
1239 inode->i_mode = udf_convert_permissions(fe);
1240 inode->i_mode &= ~sbi->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001242 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001244 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Marcin Slusarz56774802008-02-10 11:25:31 +01001246 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001247 inode->i_atime = sbi->s_record_time;
1248
Marcin Slusarz56774802008-02-10 11:25:31 +01001249 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1250 fe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001251 inode->i_mtime = sbi->s_record_time;
1252
Marcin Slusarz56774802008-02-10 11:25:31 +01001253 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001254 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001256 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1257 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1258 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1259 offset = sizeof(struct fileEntry) + iinfo->i_lenEAttr;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001260 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001261 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001262 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Marcin Slusarz56774802008-02-10 11:25:31 +01001264 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001265 inode->i_atime = sbi->s_record_time;
1266
Marcin Slusarz56774802008-02-10 11:25:31 +01001267 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1268 efe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001269 inode->i_mtime = sbi->s_record_time;
1270
Marcin Slusarz56774802008-02-10 11:25:31 +01001271 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001272 iinfo->i_crtime = sbi->s_record_time;
1273
Marcin Slusarz56774802008-02-10 11:25:31 +01001274 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001275 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001277 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1278 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1279 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001280 offset = sizeof(struct extendedFileEntry) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001281 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001284 switch (fe->icbTag.fileType) {
1285 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001286 inode->i_op = &udf_dir_inode_operations;
1287 inode->i_fop = &udf_dir_operations;
1288 inode->i_mode |= S_IFDIR;
1289 inc_nlink(inode);
1290 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001291 case ICBTAG_FILE_TYPE_REALTIME:
1292 case ICBTAG_FILE_TYPE_REGULAR:
1293 case ICBTAG_FILE_TYPE_UNDEF:
Jan Kara742e1792008-04-08 01:17:52 +02001294 case ICBTAG_FILE_TYPE_VAT20:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001295 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001296 inode->i_data.a_ops = &udf_adinicb_aops;
1297 else
1298 inode->i_data.a_ops = &udf_aops;
1299 inode->i_op = &udf_file_inode_operations;
1300 inode->i_fop = &udf_file_operations;
1301 inode->i_mode |= S_IFREG;
1302 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001303 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001304 inode->i_mode |= S_IFBLK;
1305 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001306 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001307 inode->i_mode |= S_IFCHR;
1308 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001309 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001310 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1311 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001312 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001313 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1314 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001315 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001316 inode->i_data.a_ops = &udf_symlink_aops;
Dmitry Monakhovc15d0fc2010-03-29 11:05:21 +04001317 inode->i_op = &udf_symlink_inode_operations;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001318 inode->i_mode = S_IFLNK | S_IRWXUGO;
1319 break;
Jan Karabfb257a2008-04-08 20:37:21 +02001320 case ICBTAG_FILE_TYPE_MAIN:
1321 udf_debug("METADATA FILE-----\n");
1322 break;
1323 case ICBTAG_FILE_TYPE_MIRROR:
1324 udf_debug("METADATA MIRROR FILE-----\n");
1325 break;
1326 case ICBTAG_FILE_TYPE_BITMAP:
1327 udf_debug("METADATA BITMAP FILE-----\n");
1328 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001329 default:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001330 printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
1331 "file type=%d\n", inode->i_ino,
1332 fe->icbTag.fileType);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001333 make_bad_inode(inode);
1334 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001336 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001337 struct deviceSpec *dsea =
1338 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001339 if (dsea) {
1340 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001341 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1342 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001344 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347}
1348
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001349static int udf_alloc_i_data(struct inode *inode, size_t size)
1350{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001351 struct udf_inode_info *iinfo = UDF_I(inode);
1352 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001353
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001354 if (!iinfo->i_ext.i_data) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001355 printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
1356 "no free memory\n", inode->i_ino);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001357 return -ENOMEM;
1358 }
1359
1360 return 0;
1361}
1362
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001363static mode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 mode_t mode;
1366 uint32_t permissions;
1367 uint32_t flags;
1368
1369 permissions = le32_to_cpu(fe->permissions);
1370 flags = le16_to_cpu(fe->icbTag.flags);
1371
Marcin Slusarz4b111112008-02-08 04:20:36 -08001372 mode = ((permissions) & S_IRWXO) |
1373 ((permissions >> 2) & S_IRWXG) |
1374 ((permissions >> 4) & S_IRWXU) |
1375 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1376 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1377 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 return mode;
1380}
1381
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001382int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 int ret;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 lock_kernel();
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001387 ret = udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 unlock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return ret;
1391}
1392
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001393int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 return udf_update_inode(inode, 1);
1396}
1397
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001398static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400 struct buffer_head *bh = NULL;
1401 struct fileEntry *fe;
1402 struct extendedFileEntry *efe;
1403 uint32_t udfperms;
1404 uint16_t icbflags;
1405 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001407 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001408 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001409 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Jan Kara5833ded2010-01-08 16:52:59 +01001411 bh = udf_tgetblk(inode->i_sb,
1412 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001413 if (!bh) {
Jan Karaaae917c2010-01-08 16:46:29 +01001414 udf_debug("getblk failure\n");
1415 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417
Jan Karaaae917c2010-01-08 16:46:29 +01001418 lock_buffer(bh);
1419 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 fe = (struct fileEntry *)bh->b_data;
1421 efe = (struct extendedFileEntry *)bh->b_data;
1422
Jan Karaaae917c2010-01-08 16:46:29 +01001423 if (iinfo->i_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001425 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001427 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001428 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001429 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001430 sizeof(struct unallocSpaceEntry));
Jan Karaaae917c2010-01-08 16:46:29 +01001431 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1432 use->descTag.tagLocation =
1433 cpu_to_le32(iinfo->i_location.logicalBlockNum);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001434 crclen = sizeof(struct unallocSpaceEntry) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001435 iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 use->descTag.descCRCLength = cpu_to_le16(crclen);
Bob Copelandf845fce2008-04-17 09:47:48 +02001437 use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001438 sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001439 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001440 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Jan Karaaae917c2010-01-08 16:46:29 +01001442 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444
Phillip Susi4d6660e2006-03-07 21:55:24 -08001445 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1446 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001447 else
1448 fe->uid = cpu_to_le32(inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Phillip Susi4d6660e2006-03-07 21:55:24 -08001450 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1451 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001452 else
1453 fe->gid = cpu_to_le32(inode->i_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Marcin Slusarz4b111112008-02-08 04:20:36 -08001455 udfperms = ((inode->i_mode & S_IRWXO)) |
1456 ((inode->i_mode & S_IRWXG) << 2) |
1457 ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Marcin Slusarz4b111112008-02-08 04:20:36 -08001459 udfperms |= (le32_to_cpu(fe->permissions) &
1460 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1461 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1462 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 fe->permissions = cpu_to_le32(udfperms);
1464
1465 if (S_ISDIR(inode->i_mode))
1466 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1467 else
1468 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1469
1470 fe->informationLength = cpu_to_le64(inode->i_size);
1471
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001472 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001473 struct regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001474 struct deviceSpec *dsea =
1475 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001476 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001478 udf_add_extendedattr(inode,
1479 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001480 sizeof(struct regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 dsea->attrType = cpu_to_le32(12);
1482 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001483 dsea->attrLength = cpu_to_le32(
1484 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001485 sizeof(struct regid));
1486 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001488 eid = (struct regid *)dsea->impUse;
1489 memset(eid, 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 strcpy(eid->ident, UDF_ID_DEVELOPER);
1491 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1492 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1493 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1494 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1495 }
1496
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001497 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001498 memcpy(bh->b_data + sizeof(struct fileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001499 iinfo->i_ext.i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001500 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001501 fe->logicalBlocksRecorded = cpu_to_le64(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001502 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1503 (blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Marcin Slusarz56774802008-02-10 11:25:31 +01001505 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1506 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1507 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001508 memset(&(fe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1510 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1511 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001512 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1513 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1514 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1516 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001517 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001518 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001519 iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001520 inode->i_sb->s_blocksize -
1521 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 efe->objectSize = cpu_to_le64(inode->i_size);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001523 efe->logicalBlocksRecorded = cpu_to_le64(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001524 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1525 (blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001527 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1528 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1529 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1530 iinfo->i_crtime = inode->i_atime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001531
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001532 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1533 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1534 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1535 iinfo->i_crtime = inode->i_mtime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001536
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001537 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1538 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1539 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1540 iinfo->i_crtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Marcin Slusarz56774802008-02-10 11:25:31 +01001542 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1543 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1544 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1545 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001547 memset(&(efe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1549 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1550 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001551 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1552 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1553 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1555 crclen = sizeof(struct extendedFileEntry);
1556 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001557 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 fe->icbTag.strategyType = cpu_to_le16(4096);
1559 fe->icbTag.strategyParameter = cpu_to_le16(1);
1560 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001561 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 fe->icbTag.strategyType = cpu_to_le16(4);
1563 fe->icbTag.numEntries = cpu_to_le16(1);
1564 }
1565
1566 if (S_ISDIR(inode->i_mode))
1567 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1568 else if (S_ISREG(inode->i_mode))
1569 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1570 else if (S_ISLNK(inode->i_mode))
1571 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1572 else if (S_ISBLK(inode->i_mode))
1573 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1574 else if (S_ISCHR(inode->i_mode))
1575 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1576 else if (S_ISFIFO(inode->i_mode))
1577 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1578 else if (S_ISSOCK(inode->i_mode))
1579 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1580
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001581 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001582 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1583 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1584 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1585 (le16_to_cpu(fe->icbTag.flags) &
1586 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1587 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001590 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 fe->descTag.descVersion = cpu_to_le16(3);
1592 else
1593 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001594 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001595 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001596 iinfo->i_location.logicalBlockNum);
Jan Karaaae917c2010-01-08 16:46:29 +01001597 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001599 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001600 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001601 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Jan Karaaae917c2010-01-08 16:46:29 +01001603out:
Jan Kara5833ded2010-01-08 16:52:59 +01001604 set_buffer_uptodate(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001605 unlock_buffer(bh);
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 /* write the data blocks */
1608 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001609 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 sync_dirty_buffer(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001611 if (buffer_write_io_error(bh)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001612 printk(KERN_WARNING "IO error syncing udf inode "
1613 "[%s:%08lx]\n", inode->i_sb->s_id,
1614 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 err = -EIO;
1616 }
1617 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001618 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return err;
1621}
1622
Pekka Enberg97e961f2008-10-15 12:29:03 +02001623struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
1625 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1626 struct inode *inode = iget_locked(sb, block);
1627
1628 if (!inode)
1629 return NULL;
1630
1631 if (inode->i_state & I_NEW) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001632 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 __udf_read_inode(inode);
1634 unlock_new_inode(inode);
1635 }
1636
1637 if (is_bad_inode(inode))
1638 goto out_iput;
1639
Pekka Enberg97e961f2008-10-15 12:29:03 +02001640 if (ino->logicalBlockNum >= UDF_SB(sb)->
1641 s_partmaps[ino->partitionReferenceNum].s_partition_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 udf_debug("block=%d, partition=%d out of range\n",
Pekka Enberg97e961f2008-10-15 12:29:03 +02001643 ino->logicalBlockNum, ino->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 make_bad_inode(inode);
1645 goto out_iput;
1646 }
1647
1648 return inode;
1649
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001650 out_iput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 iput(inode);
1652 return NULL;
1653}
1654
Marcin Slusarz4b111112008-02-08 04:20:36 -08001655int8_t udf_add_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001656 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001659 struct short_ad *sad = NULL;
1660 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 struct allocExtDesc *aed;
1662 int8_t etype;
1663 uint8_t *ptr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001664 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Jan Karaff116fc2007-05-08 00:35:14 -07001666 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001667 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001668 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001669 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 else
Jan Karaff116fc2007-05-08 00:35:14 -07001671 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001673 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001674 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001675 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001676 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 else
1678 return -1;
1679
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001680 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
Al Viro391e8bb2010-01-31 21:28:48 -05001681 unsigned char *sptr, *dptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 struct buffer_head *nbh;
1683 int err, loffset;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001684 struct kernel_lb_addr obloc = epos->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Marcin Slusarz4b111112008-02-08 04:20:36 -08001686 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1687 obloc.partitionReferenceNum,
1688 obloc.logicalBlockNum, &err);
1689 if (!epos->block.logicalBlockNum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return -1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001691 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001692 &epos->block,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001693 0));
1694 if (!nbh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 lock_buffer(nbh);
1697 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1698 set_buffer_uptodate(nbh);
1699 unlock_buffer(nbh);
1700 mark_buffer_dirty_inode(nbh, inode);
1701
1702 aed = (struct allocExtDesc *)(nbh->b_data);
1703 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001704 aed->previousAllocExtLocation =
1705 cpu_to_le32(obloc.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001706 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -07001707 loffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 aed->lengthAllocDescs = cpu_to_le32(adsize);
1709 sptr = ptr - adsize;
1710 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1711 memcpy(dptr, sptr, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -07001712 epos->offset = sizeof(struct allocExtDesc) + adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001713 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001714 loffset = epos->offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 aed->lengthAllocDescs = cpu_to_le32(0);
1716 sptr = ptr;
Jan Karaff116fc2007-05-08 00:35:14 -07001717 epos->offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001719 if (epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001720 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001721 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001722 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001723 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 mark_inode_dirty(inode);
1725 }
1726 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001727 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001729 epos->block.logicalBlockNum, sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 else
1731 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001732 epos->block.logicalBlockNum, sizeof(struct tag));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001733 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001734 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001735 sad = (struct short_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001736 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1737 inode->i_sb->s_blocksize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001738 sad->extPosition =
1739 cpu_to_le32(epos->block.logicalBlockNum);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001740 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001741 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001742 lad = (struct long_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001743 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1744 inode->i_sb->s_blocksize);
1745 lad->extLocation = cpu_to_lelb(epos->block);
1746 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1747 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001749 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001750 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001751 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -07001752 udf_update_tag(epos->bh->b_data, loffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001754 udf_update_tag(epos->bh->b_data,
1755 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001756 mark_buffer_dirty_inode(epos->bh, inode);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001757 brelse(epos->bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001758 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001760 }
Jan Karaff116fc2007-05-08 00:35:14 -07001761 epos->bh = nbh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763
Jan Karaff116fc2007-05-08 00:35:14 -07001764 etype = udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001766 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001767 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001769 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001770 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001771 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001772 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1773 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1774 udf_update_tag(epos->bh->b_data,
1775 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001777 udf_update_tag(epos->bh->b_data,
1778 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001779 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781
1782 return etype;
1783}
1784
Marcin Slusarz4b111112008-02-08 04:20:36 -08001785int8_t udf_write_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001786 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 int adsize;
1789 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001790 struct short_ad *sad;
1791 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001792 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Jan Karaff116fc2007-05-08 00:35:14 -07001794 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001795 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001796 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001797 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 else
Jan Karaff116fc2007-05-08 00:35:14 -07001799 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001801 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001802 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001803 sad = (struct short_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001804 sad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001805 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001806 adsize = sizeof(struct short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001807 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001808 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001809 lad = (struct long_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001810 lad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001811 lad->extLocation = cpu_to_lelb(*eloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001812 memset(lad->impUse, 0x00, sizeof(lad->impUse));
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001813 adsize = sizeof(struct long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001814 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001815 default:
1816 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
1818
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001819 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001820 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001821 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001822 struct allocExtDesc *aed =
1823 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07001824 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001825 le32_to_cpu(aed->lengthAllocDescs) +
1826 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
Jan Karaff116fc2007-05-08 00:35:14 -07001828 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001829 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07001834 epos->offset += adsize;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return (elen >> 30);
1837}
1838
Marcin Slusarz4b111112008-02-08 04:20:36 -08001839int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001840 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 int8_t etype;
1843
Jan Karaff116fc2007-05-08 00:35:14 -07001844 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001845 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001846 int block;
Jan Karaff116fc2007-05-08 00:35:14 -07001847 epos->block = *eloc;
1848 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001849 brelse(epos->bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001850 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001851 epos->bh = udf_tread(inode->i_sb, block);
1852 if (!epos->bh) {
1853 udf_debug("reading block %d failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return -1;
1855 }
1856 }
1857
1858 return etype;
1859}
1860
Marcin Slusarz4b111112008-02-08 04:20:36 -08001861int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001862 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 int alen;
1865 int8_t etype;
1866 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001867 struct short_ad *sad;
1868 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001869 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001870
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001871 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001872 if (!epos->offset)
1873 epos->offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001874 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001875 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001876 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001877 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001878 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001879 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001880 if (!epos->offset)
1881 epos->offset = sizeof(struct allocExtDesc);
1882 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001883 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08001884 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
1885 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001888 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001889 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001890 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
1891 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001893 etype = le32_to_cpu(sad->extLength) >> 30;
1894 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001895 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001896 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001897 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
1898 break;
1899 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001900 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
1901 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001902 return -1;
1903 etype = le32_to_cpu(lad->extLength) >> 30;
1904 *eloc = lelb_to_cpu(lad->extLocation);
1905 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
1906 break;
1907 default:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001908 udf_debug("alloc_type = %d unsupported\n",
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001909 iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001910 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912
1913 return etype;
1914}
1915
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001916static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001917 struct kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001919 struct kernel_lb_addr oeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 uint32_t oelen;
1921 int8_t etype;
1922
Jan Karaff116fc2007-05-08 00:35:14 -07001923 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001924 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001926 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001927 udf_write_aext(inode, &epos, &neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 neloc = oeloc;
1929 nelen = (etype << 30) | oelen;
1930 }
Pekka Enberg97e961f2008-10-15 12:29:03 +02001931 udf_add_aext(inode, &epos, &neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001932 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return (nelen >> 30);
1935}
1936
Marcin Slusarz4b111112008-02-08 04:20:36 -08001937int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001938 struct kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
Jan Karaff116fc2007-05-08 00:35:14 -07001940 struct extent_position oepos;
1941 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 int8_t etype;
1943 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001944 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001946 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001947 get_bh(epos.bh);
1948 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
1950
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001951 iinfo = UDF_I(inode);
1952 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001953 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001954 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001955 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 else
1957 adsize = 0;
1958
Jan Karaff116fc2007-05-08 00:35:14 -07001959 oepos = epos;
1960 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return -1;
1962
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001963 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001964 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001965 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001966 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07001967 brelse(oepos.bh);
1968 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07001969 oepos.bh = epos.bh;
1970 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001973 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 elen = 0;
1975
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001976 if (epos.bh != oepos.bh) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001977 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
1978 udf_write_aext(inode, &oepos, &eloc, elen, 1);
1979 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001980 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001981 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001983 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001984 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001985 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001986 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001987 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08001988 udf_update_tag(oepos.bh->b_data,
1989 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001991 udf_update_tag(oepos.bh->b_data,
1992 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001993 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001995 } else {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001996 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001997 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001998 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002000 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002001 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002002 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002003 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002004 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002005 udf_update_tag(oepos.bh->b_data,
2006 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002008 udf_update_tag(oepos.bh->b_data,
2009 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002010 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
2012 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002013
Jan Kara3bf25cb2007-05-08 00:35:16 -07002014 brelse(epos.bh);
2015 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 return (elen >> 30);
2018}
2019
Marcin Slusarz4b111112008-02-08 04:20:36 -08002020int8_t inode_bmap(struct inode *inode, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002021 struct extent_position *pos, struct kernel_lb_addr *eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002022 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002024 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002025 loff_t lbcount = 0, bcount =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002026 (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002028 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002030 iinfo = UDF_I(inode);
Jan Karaff116fc2007-05-08 00:35:14 -07002031 pos->offset = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002032 pos->block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002033 pos->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 *elen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002036 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002037 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2038 if (etype == -1) {
2039 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002040 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return -1;
2042 }
2043 lbcount += *elen;
2044 } while (lbcount <= bcount);
2045
Marcin Slusarz4b111112008-02-08 04:20:36 -08002046 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 return etype;
2049}
2050
Jan Kara60448b12007-05-08 00:35:13 -07002051long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002053 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -07002054 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002055 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002056 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 int ret;
2058
2059 lock_kernel();
2060
Marcin Slusarz4b111112008-02-08 04:20:36 -08002061 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2062 (EXT_RECORDED_ALLOCATED >> 30))
Pekka Enberg97e961f2008-10-15 12:29:03 +02002063 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 else
2065 ret = 0;
2066
2067 unlock_kernel();
Jan Kara3bf25cb2007-05-08 00:35:16 -07002068 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2071 return udf_fixed_to_variable(ret);
2072 else
2073 return ret;
2074}