]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/udf/inode.c
udf: potential integer overflow
[mirror_ubuntu-hirsute-kernel.git] / fs / udf / inode.c
CommitLineData
1da177e4
LT
1/*
2 * inode.c
3 *
4 * PURPOSE
5 * Inode handling routines for the OSTA-UDF(tm) filesystem.
6 *
1da177e4
LT
7 * 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
4b11111a
MS
22 * 12/06/98 blf partition support in udf_iget, udf_block_map
23 * and udf_read_inode
1da177e4
LT
24 * 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>
907f4554 39#include <linux/quotaops.h>
1da177e4 40#include <linux/slab.h>
f845fced 41#include <linux/crc-itu-t.h>
1da177e4
LT
42
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 *);
647bd61a 55static int udf_alloc_i_data(struct inode *inode, size_t size);
60448b1d 56static struct buffer_head *inode_getblk(struct inode *, sector_t, int *,
1ed16171 57 sector_t *, int *);
ff116fc8 58static int8_t udf_insert_aext(struct inode *, struct extent_position,
5ca4e4be 59 struct kernel_lb_addr, uint32_t);
1da177e4 60static void udf_split_extents(struct inode *, int *, int, int,
5ca4e4be 61 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
1da177e4 62static void udf_prealloc_extents(struct inode *, int, int,
5ca4e4be 63 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
1da177e4 64static void udf_merge_extents(struct inode *,
5ca4e4be 65 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
1da177e4 66static void udf_update_extents(struct inode *,
5ca4e4be 67 struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
cb00ea35 68 struct extent_position *);
1da177e4
LT
69static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
70
b1e32126 71
cb00ea35 72void udf_delete_inode(struct inode *inode)
1da177e4 73{
907f4554 74 if (!is_bad_inode(inode))
871a2931 75 dquot_initialize(inode);
907f4554 76
fef26658
MF
77 truncate_inode_pages(&inode->i_data, 0);
78
1da177e4
LT
79 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;
28de7948
CG
91
92no_delete:
1da177e4
LT
93 clear_inode(inode);
94}
95
74584ae5 96/*
81056dd0
JK
97 * 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.
74584ae5 101 */
1da177e4
LT
102void udf_clear_inode(struct inode *inode)
103{
2c948b3f
JK
104 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 "
1537a363 109 "inode size %llu different from extent length %llu. "
2c948b3f
JK
110 "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);
1da177e4 114 }
257ba15c 115
9f754758 116 dquot_drop(inode);
48d6d8ff
MS
117 kfree(iinfo->i_ext.i_data);
118 iinfo->i_ext.i_data = NULL;
1da177e4
LT
119}
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
be021ee4
NP
131static 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)
1da177e4 134{
be021ee4
NP
135 *pagep = NULL;
136 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
137 udf_get_block);
1da177e4
LT
138}
139
140static sector_t udf_bmap(struct address_space *mapping, sector_t block)
141{
cb00ea35 142 return generic_block_bmap(mapping, block, udf_get_block);
1da177e4
LT
143}
144
f5e54d6e 145const struct address_space_operations udf_aops = {
28de7948
CG
146 .readpage = udf_readpage,
147 .writepage = udf_writepage,
148 .sync_page = block_sync_page,
be021ee4
NP
149 .write_begin = udf_write_begin,
150 .write_end = generic_write_end,
28de7948 151 .bmap = udf_bmap,
1da177e4
LT
152};
153
cb00ea35 154void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
1da177e4
LT
155{
156 struct page *page;
157 char *kaddr;
48d6d8ff 158 struct udf_inode_info *iinfo = UDF_I(inode);
1da177e4
LT
159 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
48d6d8ff 167 if (!iinfo->i_lenAlloc) {
1da177e4 168 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
48d6d8ff 169 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
1da177e4 170 else
48d6d8ff 171 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
1da177e4
LT
172 mark_inode_dirty(inode);
173 return;
174 }
175
176 page = grab_cache_page(inode->i_mapping, 0);
cd7619d6
MM
177 BUG_ON(!PageLocked(page));
178
cb00ea35 179 if (!PageUptodate(page)) {
1da177e4 180 kaddr = kmap(page);
48d6d8ff
MS
181 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);
1da177e4
LT
185 flush_dcache_page(page);
186 SetPageUptodate(page);
187 kunmap(page);
188 }
48d6d8ff
MS
189 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
190 iinfo->i_lenAlloc);
191 iinfo->i_lenAlloc = 0;
1da177e4 192 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
48d6d8ff 193 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
1da177e4 194 else
48d6d8ff 195 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
1da177e4
LT
196
197 inode->i_data.a_ops->writepage(page, &udf_wbc);
198 page_cache_release(page);
199
200 mark_inode_dirty(inode);
201}
202
cb00ea35
CG
203struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
204 int *err)
1da177e4
LT
205{
206 int newblock;
ff116fc8 207 struct buffer_head *dbh = NULL;
5ca4e4be 208 struct kernel_lb_addr eloc;
1da177e4 209 uint8_t alloctype;
ff116fc8 210 struct extent_position epos;
1da177e4
LT
211
212 struct udf_fileident_bh sfibh, dfibh;
af793295
JK
213 loff_t f_pos = udf_ext0_offset(inode);
214 int size = udf_ext0_offset(inode) + inode->i_size;
1da177e4 215 struct fileIdentDesc cfi, *sfi, *dfi;
48d6d8ff 216 struct udf_inode_info *iinfo = UDF_I(inode);
1da177e4
LT
217
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
cb00ea35 223 if (!inode->i_size) {
48d6d8ff 224 iinfo->i_alloc_type = alloctype;
1da177e4
LT
225 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,
48d6d8ff
MS
231 iinfo->i_location.partitionReferenceNum,
232 iinfo->i_location.logicalBlockNum, err);
1da177e4
LT
233 if (!(*block))
234 return NULL;
235 newblock = udf_get_pblock(inode->i_sb, *block,
48d6d8ff 236 iinfo->i_location.partitionReferenceNum,
c0b34438 237 0);
1da177e4
LT
238 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
4b11111a 249 sfibh.soffset = sfibh.eoffset =
af793295 250 f_pos & (inode->i_sb->s_blocksize - 1);
ff116fc8 251 sfibh.sbh = sfibh.ebh = NULL;
1da177e4
LT
252 dfibh.soffset = dfibh.eoffset = 0;
253 dfibh.sbh = dfibh.ebh = dbh;
af793295 254 while (f_pos < size) {
48d6d8ff 255 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
4b11111a
MS
256 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
257 NULL, NULL, NULL);
cb00ea35 258 if (!sfi) {
3bf25cb4 259 brelse(dbh);
1da177e4
LT
260 return NULL;
261 }
48d6d8ff 262 iinfo->i_alloc_type = alloctype;
1da177e4
LT
263 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,
4b11111a
MS
268 sfi->fileIdent +
269 le16_to_cpu(sfi->lengthOfImpUse))) {
48d6d8ff 270 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
3bf25cb4 271 brelse(dbh);
1da177e4
LT
272 return NULL;
273 }
274 }
275 mark_buffer_dirty_inode(dbh, inode);
276
48d6d8ff
MS
277 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
278 iinfo->i_lenAlloc);
279 iinfo->i_lenAlloc = 0;
1da177e4 280 eloc.logicalBlockNum = *block;
4b11111a 281 eloc.partitionReferenceNum =
48d6d8ff 282 iinfo->i_location.partitionReferenceNum;
2c948b3f 283 iinfo->i_lenExtents = inode->i_size;
ff116fc8 284 epos.bh = NULL;
48d6d8ff 285 epos.block = iinfo->i_location;
ff116fc8 286 epos.offset = udf_file_entry_alloc_offset(inode);
2c948b3f 287 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
1da177e4
LT
288 /* UniqueID stuff */
289
3bf25cb4 290 brelse(epos.bh);
1da177e4
LT
291 mark_inode_dirty(inode);
292 return dbh;
293}
294
cb00ea35
CG
295static int udf_get_block(struct inode *inode, sector_t block,
296 struct buffer_head *bh_result, int create)
1da177e4
LT
297{
298 int err, new;
299 struct buffer_head *bh;
1ed16171 300 sector_t phys = 0;
48d6d8ff 301 struct udf_inode_info *iinfo;
1da177e4 302
cb00ea35 303 if (!create) {
1da177e4
LT
304 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
48d6d8ff
MS
316 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++;
1da177e4
LT
320 }
321
322 err = 0;
323
324 bh = inode_getblk(inode, block, &err, &phys, &new);
2c2111c2 325 BUG_ON(bh);
1da177e4
LT
326 if (err)
327 goto abort;
2c2111c2 328 BUG_ON(!phys);
1da177e4
LT
329
330 if (new)
331 set_buffer_new(bh_result);
332 map_bh(bh_result, inode->i_sb, phys);
28de7948
CG
333
334abort:
1da177e4
LT
335 unlock_kernel();
336 return err;
1da177e4
LT
337}
338
cb00ea35
CG
339static struct buffer_head *udf_getblk(struct inode *inode, long block,
340 int create, int *err)
1da177e4 341{
28de7948 342 struct buffer_head *bh;
1da177e4
LT
343 struct buffer_head dummy;
344
345 dummy.b_state = 0;
346 dummy.b_blocknr = -1000;
347 *err = udf_get_block(inode, block, &dummy, create);
cb00ea35 348 if (!*err && buffer_mapped(&dummy)) {
1da177e4 349 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
cb00ea35 350 if (buffer_new(&dummy)) {
1da177e4
LT
351 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 }
28de7948 359
1da177e4
LT
360 return NULL;
361}
362
31170b6a
JK
363/* Extend the file by 'blocks' blocks, return the number of extents added */
364int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
5ca4e4be 365 struct kernel_long_ad *last_ext, sector_t blocks)
31170b6a
JK
366{
367 sector_t add;
368 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
369 struct super_block *sb = inode->i_sb;
5ca4e4be 370 struct kernel_lb_addr prealloc_loc = {};
31170b6a 371 int prealloc_len = 0;
48d6d8ff 372 struct udf_inode_info *iinfo;
31170b6a
JK
373
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;
28de7948 378
48d6d8ff 379 iinfo = UDF_I(inode);
31170b6a
JK
380 /* Round the last extent up to a multiple of block size */
381 if (last_ext->extLength & (sb->s_blocksize - 1)) {
382 last_ext->extLength =
28de7948
CG
383 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
384 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
385 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
48d6d8ff
MS
386 iinfo->i_lenExtents =
387 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
28de7948 388 ~(sb->s_blocksize - 1);
31170b6a 389 }
28de7948 390
31170b6a 391 /* Last extent are just preallocated blocks? */
4b11111a
MS
392 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
393 EXT_NOT_RECORDED_ALLOCATED) {
31170b6a
JK
394 /* 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 |
28de7948 399 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
31170b6a 400 last_ext->extLocation.logicalBlockNum = 0;
4b11111a 401 last_ext->extLocation.partitionReferenceNum = 0;
31170b6a 402 }
28de7948 403
31170b6a 404 /* Can we merge with the previous extent? */
4b11111a
MS
405 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;
31170b6a
JK
410 if (add > blocks)
411 add = blocks;
412 blocks -= add;
413 last_ext->extLength += add << sb->s_blocksize_bits;
414 }
415
416 if (fake) {
97e961fd 417 udf_add_aext(inode, last_pos, &last_ext->extLocation,
cb00ea35 418 last_ext->extLength, 1);
31170b6a 419 count++;
4b11111a 420 } else
97e961fd 421 udf_write_aext(inode, last_pos, &last_ext->extLocation,
4b11111a 422 last_ext->extLength, 1);
28de7948 423
31170b6a
JK
424 /* 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;
4b11111a 430 last_ext->extLocation.partitionReferenceNum = 0;
28de7948 431 add = (1 << (30-sb->s_blocksize_bits)) - 1;
4b11111a
MS
432 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
433 (add << sb->s_blocksize_bits);
28de7948 434
31170b6a
JK
435 /* Create enough extents to cover the whole hole */
436 while (blocks > add) {
437 blocks -= add;
97e961fd 438 if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
cb00ea35 439 last_ext->extLength, 1) == -1)
31170b6a
JK
440 return -1;
441 count++;
442 }
443 if (blocks) {
444 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
28de7948 445 (blocks << sb->s_blocksize_bits);
97e961fd 446 if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
cb00ea35 447 last_ext->extLength, 1) == -1)
31170b6a
JK
448 return -1;
449 count++;
450 }
28de7948
CG
451
452out:
31170b6a
JK
453 /* Do we have some preallocated blocks saved? */
454 if (prealloc_len) {
97e961fd 455 if (udf_add_aext(inode, last_pos, &prealloc_loc,
4b11111a 456 prealloc_len, 1) == -1)
31170b6a
JK
457 return -1;
458 last_ext->extLocation = prealloc_loc;
459 last_ext->extLength = prealloc_len;
460 count++;
461 }
28de7948 462
31170b6a 463 /* last_pos should point to the last written extent... */
48d6d8ff 464 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 465 last_pos->offset -= sizeof(struct short_ad);
48d6d8ff 466 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 467 last_pos->offset -= sizeof(struct long_ad);
31170b6a
JK
468 else
469 return -1;
28de7948 470
31170b6a
JK
471 return count;
472}
473
cb00ea35 474static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
1ed16171 475 int *err, sector_t *phys, int *new)
1da177e4 476{
31170b6a 477 static sector_t last_block;
ff116fc8 478 struct buffer_head *result = NULL;
5ca4e4be 479 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
ff116fc8 480 struct extent_position prev_epos, cur_epos, next_epos;
1da177e4 481 int count = 0, startnum = 0, endnum = 0;
85d71244 482 uint32_t elen = 0, tmpelen;
5ca4e4be 483 struct kernel_lb_addr eloc, tmpeloc;
1da177e4 484 int c = 1;
60448b1d
JK
485 loff_t lbcount = 0, b_off = 0;
486 uint32_t newblocknum, newblock;
487 sector_t offset = 0;
1da177e4 488 int8_t etype;
48d6d8ff
MS
489 struct udf_inode_info *iinfo = UDF_I(inode);
490 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
31170b6a 491 int lastblock = 0;
1da177e4 492
ff116fc8 493 prev_epos.offset = udf_file_entry_alloc_offset(inode);
48d6d8ff 494 prev_epos.block = iinfo->i_location;
ff116fc8
JK
495 prev_epos.bh = NULL;
496 cur_epos = next_epos = prev_epos;
28de7948 497 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
1da177e4
LT
498
499 /* find the extent which contains the block we are looking for.
cb00ea35
CG
500 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) {
3bf25cb4
JK
504 brelse(prev_epos.bh);
505 get_bh(cur_epos.bh);
ff116fc8 506 prev_epos.bh = cur_epos.bh;
1da177e4 507 }
cb00ea35 508 if (cur_epos.bh != next_epos.bh) {
3bf25cb4
JK
509 brelse(cur_epos.bh);
510 get_bh(next_epos.bh);
ff116fc8 511 cur_epos.bh = next_epos.bh;
1da177e4
LT
512 }
513
514 lbcount += elen;
515
ff116fc8
JK
516 prev_epos.block = cur_epos.block;
517 cur_epos.block = next_epos.block;
1da177e4 518
ff116fc8
JK
519 prev_epos.offset = cur_epos.offset;
520 cur_epos.offset = next_epos.offset;
1da177e4 521
4b11111a
MS
522 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
523 if (etype == -1)
1da177e4
LT
524 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 +
28de7948
CG
533 ((elen + inode->i_sb->s_blocksize - 1) >>
534 inode->i_sb->s_blocksize_bits);
1da177e4 535
cb00ea35 536 count++;
1da177e4
LT
537 } while (lbcount + elen <= b_off);
538
539 b_off -= lbcount;
540 offset = b_off >> inode->i_sb->s_blocksize_bits;
85d71244
JK
541 /*
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);
1da177e4
LT
547
548 /* if the extent is allocated and recorded, return the block
cb00ea35 549 if the extent is not a multiple of the blocksize, round up */
1da177e4 550
cb00ea35
CG
551 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
552 if (elen & (inode->i_sb->s_blocksize - 1)) {
1da177e4 553 elen = EXT_RECORDED_ALLOCATED |
28de7948
CG
554 ((elen + inode->i_sb->s_blocksize - 1) &
555 ~(inode->i_sb->s_blocksize - 1));
97e961fd 556 etype = udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
1da177e4 557 }
3bf25cb4
JK
558 brelse(prev_epos.bh);
559 brelse(cur_epos.bh);
560 brelse(next_epos.bh);
97e961fd 561 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
1da177e4
LT
562 *phys = newblock;
563 return NULL;
564 }
565
31170b6a
JK
566 last_block = block;
567 /* Are we beyond EOF? */
cb00ea35 568 if (etype == -1) {
31170b6a
JK
569 int ret;
570
571 if (count) {
572 if (c)
573 laarr[0] = laarr[1];
574 startnum = 1;
cb00ea35 575 } else {
31170b6a 576 /* Create a fake extent when there's not one */
4b11111a 577 memset(&laarr[0].extLocation, 0x00,
5ca4e4be 578 sizeof(struct kernel_lb_addr));
31170b6a 579 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
4b11111a
MS
580 /* Will udf_extend_file() create real extent from
581 a fake one? */
31170b6a
JK
582 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? */
4b11111a
MS
599 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
600 EXT_NOT_RECORDED_ALLOCATED) {
31170b6a
JK
601 /* 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 |
28de7948 606 inode->i_sb->s_blocksize;
4b11111a 607 memset(&laarr[c].extLocation, 0x00,
5ca4e4be 608 sizeof(struct kernel_lb_addr));
cb00ea35
CG
609 count++;
610 endnum++;
31170b6a 611 }
cb00ea35 612 endnum = c + 1;
1da177e4 613 lastblock = 1;
cb00ea35 614 } else {
1da177e4
LT
615 endnum = startnum = ((count > 2) ? 2 : count);
616
4b11111a
MS
617 /* if the current extent is in position 0,
618 swap it with the previous */
cb00ea35 619 if (!c && count != 1) {
31170b6a
JK
620 laarr[2] = laarr[0];
621 laarr[0] = laarr[1];
622 laarr[1] = laarr[2];
623 c = 1;
624 }
1da177e4 625
4b11111a
MS
626 /* 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) {
cb00ea35
CG
630 laarr[c + 1].extLength = (etype << 30) | elen;
631 laarr[c + 1].extLocation = eloc;
632 count++;
633 startnum++;
634 endnum++;
4b11111a 635 } else
1da177e4
LT
636 lastblock = 1;
637 }
1da177e4
LT
638
639 /* if the current extent is not recorded but allocated, get the
28de7948 640 * block in the extent corresponding to the requested block */
4b11111a 641 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
1da177e4 642 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
4b11111a 643 else { /* otherwise, allocate a new block */
48d6d8ff
MS
644 if (iinfo->i_next_alloc_block == block)
645 goal = iinfo->i_next_alloc_goal;
1da177e4 646
cb00ea35 647 if (!goal) {
4b11111a 648 if (!(goal = pgoal)) /* XXX: what was intended here? */
48d6d8ff 649 goal = iinfo->i_location.logicalBlockNum + 1;
1da177e4
LT
650 }
651
4b11111a 652 newblocknum = udf_new_block(inode->i_sb, inode,
48d6d8ff 653 iinfo->i_location.partitionReferenceNum,
4b11111a
MS
654 goal, err);
655 if (!newblocknum) {
3bf25cb4 656 brelse(prev_epos.bh);
1da177e4
LT
657 *err = -ENOSPC;
658 return NULL;
659 }
48d6d8ff 660 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
1da177e4
LT
661 }
662
4b11111a
MS
663 /* 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 */
1da177e4
LT
667 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
668
669#ifdef UDF_PREALLOCATE
81056dd0
JK
670 /* 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);
1da177e4
LT
676#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
28de7948
CG
682 * of extents is greater than the old number, and deleting extents if
683 * the new number of extents is less than the old number */
ff116fc8 684 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
1da177e4 685
3bf25cb4 686 brelse(prev_epos.bh);
1da177e4 687
4b11111a 688 newblock = udf_get_pblock(inode->i_sb, newblocknum,
48d6d8ff 689 iinfo->i_location.partitionReferenceNum, 0);
4b11111a 690 if (!newblock)
1da177e4 691 return NULL;
1da177e4
LT
692 *phys = newblock;
693 *err = 0;
694 *new = 1;
48d6d8ff
MS
695 iinfo->i_next_alloc_block = block;
696 iinfo->i_next_alloc_goal = newblocknum;
1da177e4
LT
697 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);
28de7948 703
1da177e4
LT
704 return result;
705}
706
cb00ea35
CG
707static void udf_split_extents(struct inode *inode, int *c, int offset,
708 int newblocknum,
5ca4e4be 709 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
cb00ea35 710 int *endnum)
1da177e4 711{
4b11111a
MS
712 unsigned long blocksize = inode->i_sb->s_blocksize;
713 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
714
1da177e4 715 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
4b11111a
MS
716 (laarr[*c].extLength >> 30) ==
717 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
1da177e4
LT
718 int curr = *c;
719 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
4b11111a 720 blocksize - 1) >> blocksize_bits;
1da177e4
LT
721 int8_t etype = (laarr[curr].extLength >> 30);
722
4b11111a 723 if (blen == 1)
28de7948 724 ;
4b11111a 725 else if (!offset || blen == offset + 1) {
cb00ea35
CG
726 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];
731 }
732
733 if (offset) {
734 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
4b11111a 735 udf_free_blocks(inode->i_sb, inode,
97e961fd 736 &laarr[curr].extLocation,
4b11111a
MS
737 0, offset);
738 laarr[curr].extLength =
739 EXT_NOT_RECORDED_NOT_ALLOCATED |
740 (offset << blocksize_bits);
1da177e4 741 laarr[curr].extLocation.logicalBlockNum = 0;
4b11111a
MS
742 laarr[curr].extLocation.
743 partitionReferenceNum = 0;
744 } else
1da177e4 745 laarr[curr].extLength = (etype << 30) |
4b11111a 746 (offset << blocksize_bits);
cb00ea35
CG
747 curr++;
748 (*c)++;
749 (*endnum)++;
1da177e4 750 }
647bd61a 751
1da177e4
LT
752 laarr[curr].extLocation.logicalBlockNum = newblocknum;
753 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
754 laarr[curr].extLocation.partitionReferenceNum =
c0b34438 755 UDF_I(inode)->i_location.partitionReferenceNum;
1da177e4 756 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
4b11111a 757 blocksize;
cb00ea35 758 curr++;
1da177e4 759
cb00ea35 760 if (blen != offset + 1) {
1da177e4 761 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
4b11111a
MS
762 laarr[curr].extLocation.logicalBlockNum +=
763 offset + 1;
28de7948 764 laarr[curr].extLength = (etype << 30) |
4b11111a 765 ((blen - (offset + 1)) << blocksize_bits);
cb00ea35
CG
766 curr++;
767 (*endnum)++;
1da177e4
LT
768 }
769 }
770}
771
772static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
5ca4e4be 773 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
cb00ea35 774 int *endnum)
1da177e4
LT
775{
776 int start, length = 0, currlength = 0, i;
777
cb00ea35 778 if (*endnum >= (c + 1)) {
1da177e4
LT
779 if (!lastblock)
780 return;
781 else
782 start = c;
cb00ea35 783 } else {
4b11111a
MS
784 if ((laarr[c + 1].extLength >> 30) ==
785 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
cb00ea35 786 start = c + 1;
4b11111a
MS
787 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
1da177e4
LT
793 start = c;
794 }
795
cb00ea35
CG
796 for (i = start + 1; i <= *endnum; i++) {
797 if (i == *endnum) {
1da177e4
LT
798 if (lastblock)
799 length += UDF_DEFAULT_PREALLOC_BLOCKS;
4b11111a
MS
800 } 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
1da177e4
LT
807 break;
808 }
809
cb00ea35 810 if (length) {
1da177e4 811 int next = laarr[start].extLocation.logicalBlockNum +
28de7948 812 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
4b11111a
MS
813 inode->i_sb->s_blocksize - 1) >>
814 inode->i_sb->s_blocksize_bits);
1da177e4 815 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
4b11111a
MS
816 laarr[start].extLocation.partitionReferenceNum,
817 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
818 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
819 currlength);
28de7948 820 if (numalloc) {
4b11111a 821 if (start == (c + 1))
1da177e4 822 laarr[start].extLength +=
4b11111a
MS
823 (numalloc <<
824 inode->i_sb->s_blocksize_bits);
825 else {
cb00ea35 826 memmove(&laarr[c + 2], &laarr[c + 1],
5ca4e4be 827 sizeof(struct long_ad) * (*endnum - (c + 1)));
cb00ea35
CG
828 (*endnum)++;
829 laarr[c + 1].extLocation.logicalBlockNum = next;
830 laarr[c + 1].extLocation.partitionReferenceNum =
4b11111a
MS
831 laarr[c].extLocation.
832 partitionReferenceNum;
833 laarr[c + 1].extLength =
834 EXT_NOT_RECORDED_ALLOCATED |
835 (numalloc <<
836 inode->i_sb->s_blocksize_bits);
cb00ea35 837 start = c + 1;
1da177e4
LT
838 }
839
cb00ea35 840 for (i = start + 1; numalloc && i < *endnum; i++) {
4b11111a
MS
841 int elen = ((laarr[i].extLength &
842 UDF_EXTENT_LENGTH_MASK) +
843 inode->i_sb->s_blocksize - 1) >>
844 inode->i_sb->s_blocksize_bits;
1da177e4 845
cb00ea35 846 if (elen > numalloc) {
1da177e4 847 laarr[i].extLength -=
4b11111a
MS
848 (numalloc <<
849 inode->i_sb->s_blocksize_bits);
1da177e4 850 numalloc = 0;
cb00ea35 851 } else {
1da177e4 852 numalloc -= elen;
cb00ea35 853 if (*endnum > (i + 1))
4b11111a
MS
854 memmove(&laarr[i],
855 &laarr[i + 1],
5ca4e4be 856 sizeof(struct long_ad) *
4b11111a 857 (*endnum - (i + 1)));
cb00ea35
CG
858 i--;
859 (*endnum)--;
1da177e4
LT
860 }
861 }
c0b34438 862 UDF_I(inode)->i_lenExtents +=
4b11111a 863 numalloc << inode->i_sb->s_blocksize_bits;
1da177e4
LT
864 }
865 }
866}
867
868static void udf_merge_extents(struct inode *inode,
5ca4e4be 869 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
cb00ea35 870 int *endnum)
1da177e4
LT
871{
872 int i;
4b11111a
MS
873 unsigned long blocksize = inode->i_sb->s_blocksize;
874 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1da177e4 875
cb00ea35 876 for (i = 0; i < (*endnum - 1); i++) {
5ca4e4be
PE
877 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
878 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
4b11111a
MS
879
880 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);
905 } else {
906 li->extLength = lip1->extLength +
907 (((li->extLength &
908 UDF_EXTENT_LENGTH_MASK) +
909 blocksize - 1) & ~(blocksize - 1));
910 if (*endnum > (i + 2))
911 memmove(&laarr[i + 1], &laarr[i + 2],
5ca4e4be 912 sizeof(struct long_ad) *
4b11111a
MS
913 (*endnum - (i + 2)));
914 i--;
915 (*endnum)--;
1da177e4 916 }
4b11111a
MS
917 } else if (((li->extLength >> 30) ==
918 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
919 ((lip1->extLength >> 30) ==
920 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
97e961fd 921 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
4b11111a
MS
922 ((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;
cb00ea35 940 } else {
4b11111a
MS
941 li->extLength = lip1->extLength +
942 (((li->extLength &
943 UDF_EXTENT_LENGTH_MASK) +
944 blocksize - 1) & ~(blocksize - 1));
cb00ea35
CG
945 if (*endnum > (i + 2))
946 memmove(&laarr[i + 1], &laarr[i + 2],
5ca4e4be 947 sizeof(struct long_ad) *
4b11111a 948 (*endnum - (i + 2)));
cb00ea35
CG
949 i--;
950 (*endnum)--;
1da177e4 951 }
4b11111a
MS
952 } else if ((li->extLength >> 30) ==
953 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
954 udf_free_blocks(inode->i_sb, inode,
97e961fd 955 &li->extLocation, 0,
4b11111a
MS
956 ((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;
1da177e4
LT
964 }
965 }
966}
967
968static void udf_update_extents(struct inode *inode,
5ca4e4be 969 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
cb00ea35
CG
970 int startnum, int endnum,
971 struct extent_position *epos)
1da177e4
LT
972{
973 int start = 0, i;
5ca4e4be 974 struct kernel_lb_addr tmploc;
1da177e4
LT
975 uint32_t tmplen;
976
cb00ea35
CG
977 if (startnum > endnum) {
978 for (i = 0; i < (startnum - endnum); i++)
ff116fc8 979 udf_delete_aext(inode, *epos, laarr[i].extLocation,
cb00ea35
CG
980 laarr[i].extLength);
981 } else if (startnum < endnum) {
982 for (i = 0; i < (endnum - startnum); i++) {
ff116fc8 983 udf_insert_aext(inode, *epos, laarr[i].extLocation,
cb00ea35 984 laarr[i].extLength);
ff116fc8 985 udf_next_aext(inode, epos, &laarr[i].extLocation,
cb00ea35
CG
986 &laarr[i].extLength, 1);
987 start++;
1da177e4
LT
988 }
989 }
990
cb00ea35 991 for (i = start; i < endnum; i++) {
ff116fc8 992 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
97e961fd 993 udf_write_aext(inode, epos, &laarr[i].extLocation,
cb00ea35 994 laarr[i].extLength, 1);
1da177e4
LT
995 }
996}
997
cb00ea35
CG
998struct buffer_head *udf_bread(struct inode *inode, int block,
999 int create, int *err)
1da177e4 1000{
cb00ea35 1001 struct buffer_head *bh = NULL;
1da177e4
LT
1002
1003 bh = udf_getblk(inode, block, create, err);
1004 if (!bh)
1005 return NULL;
1006
1007 if (buffer_uptodate(bh))
1008 return bh;
28de7948 1009
1da177e4 1010 ll_rw_block(READ, 1, &bh);
28de7948 1011
1da177e4
LT
1012 wait_on_buffer(bh);
1013 if (buffer_uptodate(bh))
1014 return bh;
28de7948 1015
1da177e4
LT
1016 brelse(bh);
1017 *err = -EIO;
1018 return NULL;
1019}
1020
cb00ea35 1021void udf_truncate(struct inode *inode)
1da177e4
LT
1022{
1023 int offset;
1024 int err;
48d6d8ff 1025 struct udf_inode_info *iinfo;
1da177e4
LT
1026
1027 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
cb00ea35 1028 S_ISLNK(inode->i_mode)))
1da177e4
LT
1029 return;
1030 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1031 return;
1032
1033 lock_kernel();
48d6d8ff
MS
1034 iinfo = UDF_I(inode);
1035 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
4b11111a
MS
1036 if (inode->i_sb->s_blocksize <
1037 (udf_file_entry_alloc_offset(inode) +
1038 inode->i_size)) {
1da177e4 1039 udf_expand_file_adinicb(inode, inode->i_size, &err);
48d6d8ff
MS
1040 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1041 inode->i_size = iinfo->i_lenAlloc;
1da177e4
LT
1042 unlock_kernel();
1043 return;
4b11111a 1044 } else
1da177e4 1045 udf_truncate_extents(inode);
cb00ea35 1046 } else {
1da177e4 1047 offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
48d6d8ff 1048 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + offset,
c0b34438 1049 0x00, inode->i_sb->s_blocksize -
4b11111a 1050 offset - udf_file_entry_alloc_offset(inode));
48d6d8ff 1051 iinfo->i_lenAlloc = inode->i_size;
1da177e4 1052 }
cb00ea35 1053 } else {
4b11111a
MS
1054 block_truncate_page(inode->i_mapping, inode->i_size,
1055 udf_get_block);
1da177e4 1056 udf_truncate_extents(inode);
647bd61a 1057 }
1da177e4
LT
1058
1059 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1060 if (IS_SYNC(inode))
cb00ea35 1061 udf_sync_inode(inode);
1da177e4
LT
1062 else
1063 mark_inode_dirty(inode);
1064 unlock_kernel();
1065}
1066
cb00ea35 1067static void __udf_read_inode(struct inode *inode)
1da177e4
LT
1068{
1069 struct buffer_head *bh = NULL;
1070 struct fileEntry *fe;
1071 uint16_t ident;
48d6d8ff 1072 struct udf_inode_info *iinfo = UDF_I(inode);
1da177e4
LT
1073
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 */
97e961fd 1086 bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
cb00ea35 1087 if (!bh) {
1da177e4 1088 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
cb00ea35 1089 inode->i_ino);
1da177e4
LT
1090 make_bad_inode(inode);
1091 return;
1092 }
1093
1094 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
cb00ea35 1095 ident != TAG_IDENT_USE) {
4b11111a
MS
1096 printk(KERN_ERR "udf: udf_read_inode(ino %ld) "
1097 "failed ident=%d\n", inode->i_ino, ident);
3bf25cb4 1098 brelse(bh);
1da177e4
LT
1099 make_bad_inode(inode);
1100 return;
1101 }
1102
1103 fe = (struct fileEntry *)bh->b_data;
1104
5e0f0017 1105 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1ab92785 1106 struct buffer_head *ibh;
1da177e4 1107
97e961fd 1108 ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
4b11111a 1109 &ident);
1ab92785 1110 if (ident == TAG_IDENT_IE && ibh) {
1111 struct buffer_head *nbh = NULL;
5ca4e4be 1112 struct kernel_lb_addr loc;
1ab92785 1113 struct indirectEntry *ie;
1114
1115 ie = (struct indirectEntry *)ibh->b_data;
1116 loc = lelb_to_cpu(ie->indirectICB.extLocation);
1117
1118 if (ie->indirectICB.extLength &&
97e961fd 1119 (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
1ab92785 1120 &ident))) {
1121 if (ident == TAG_IDENT_FE ||
1122 ident == TAG_IDENT_EFE) {
1123 memcpy(&iinfo->i_location,
1124 &loc,
5ca4e4be 1125 sizeof(struct kernel_lb_addr));
1ab92785 1126 brelse(bh);
3bf25cb4 1127 brelse(ibh);
1ab92785 1128 brelse(nbh);
1129 __udf_read_inode(inode);
1130 return;
28de7948 1131 }
1ab92785 1132 brelse(nbh);
1da177e4 1133 }
28de7948 1134 }
1ab92785 1135 brelse(ibh);
5e0f0017 1136 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1da177e4 1137 printk(KERN_ERR "udf: unsupported strategy type: %d\n",
cb00ea35 1138 le16_to_cpu(fe->icbTag.strategyType));
3bf25cb4 1139 brelse(bh);
1da177e4
LT
1140 make_bad_inode(inode);
1141 return;
1142 }
1143 udf_fill_inode(inode, bh);
31170b6a 1144
3bf25cb4 1145 brelse(bh);
1da177e4
LT
1146}
1147
1148static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1149{
1150 struct fileEntry *fe;
1151 struct extendedFileEntry *efe;
1da177e4 1152 int offset;
6c79e987 1153 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
48d6d8ff 1154 struct udf_inode_info *iinfo = UDF_I(inode);
1da177e4
LT
1155
1156 fe = (struct fileEntry *)bh->b_data;
1157 efe = (struct extendedFileEntry *)bh->b_data;
1158
5e0f0017 1159 if (fe->icbTag.strategyType == cpu_to_le16(4))
48d6d8ff 1160 iinfo->i_strat4096 = 0;
5e0f0017 1161 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
48d6d8ff 1162 iinfo->i_strat4096 = 1;
1da177e4 1163
48d6d8ff 1164 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
4b11111a 1165 ICBTAG_FLAG_AD_MASK;
48d6d8ff
MS
1166 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;
5e0f0017 1172 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
48d6d8ff
MS
1173 iinfo->i_efe = 1;
1174 iinfo->i_use = 0;
4b11111a
MS
1175 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1176 sizeof(struct extendedFileEntry))) {
647bd61a
CG
1177 make_bad_inode(inode);
1178 return;
1179 }
48d6d8ff 1180 memcpy(iinfo->i_ext.i_data,
4b11111a
MS
1181 bh->b_data + sizeof(struct extendedFileEntry),
1182 inode->i_sb->s_blocksize -
1183 sizeof(struct extendedFileEntry));
5e0f0017 1184 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
48d6d8ff
MS
1185 iinfo->i_efe = 0;
1186 iinfo->i_use = 0;
4b11111a
MS
1187 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1188 sizeof(struct fileEntry))) {
647bd61a
CG
1189 make_bad_inode(inode);
1190 return;
1191 }
48d6d8ff 1192 memcpy(iinfo->i_ext.i_data,
c0b34438 1193 bh->b_data + sizeof(struct fileEntry),
cb00ea35 1194 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
5e0f0017 1195 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
48d6d8ff
MS
1196 iinfo->i_efe = 0;
1197 iinfo->i_use = 1;
1198 iinfo->i_lenAlloc = le32_to_cpu(
4b11111a
MS
1199 ((struct unallocSpaceEntry *)bh->b_data)->
1200 lengthAllocDescs);
1201 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1202 sizeof(struct unallocSpaceEntry))) {
647bd61a
CG
1203 make_bad_inode(inode);
1204 return;
1205 }
48d6d8ff 1206 memcpy(iinfo->i_ext.i_data,
4b11111a
MS
1207 bh->b_data + sizeof(struct unallocSpaceEntry),
1208 inode->i_sb->s_blocksize -
1209 sizeof(struct unallocSpaceEntry));
1da177e4
LT
1210 return;
1211 }
1212
1213 inode->i_uid = le32_to_cpu(fe->uid);
ca76d2d8
CG
1214 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))
4d6660eb 1217 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1da177e4
LT
1218
1219 inode->i_gid = le32_to_cpu(fe->gid);
ca76d2d8
CG
1220 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))
4d6660eb 1223 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1da177e4
LT
1224
1225 inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
1226 if (!inode->i_nlink)
1227 inode->i_nlink = 1;
647bd61a 1228
1da177e4 1229 inode->i_size = le64_to_cpu(fe->informationLength);
48d6d8ff 1230 iinfo->i_lenExtents = inode->i_size;
1da177e4 1231
7ac9bcd5 1232 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
87bc730c 1233 sbi->s_fmode != UDF_INVALID_MODE)
7ac9bcd5
MS
1234 inode->i_mode = sbi->s_fmode;
1235 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
87bc730c 1236 sbi->s_dmode != UDF_INVALID_MODE)
7ac9bcd5
MS
1237 inode->i_mode = sbi->s_dmode;
1238 else
1239 inode->i_mode = udf_convert_permissions(fe);
1240 inode->i_mode &= ~sbi->s_umask;
1da177e4 1241
48d6d8ff 1242 if (iinfo->i_efe == 0) {
1da177e4 1243 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
28de7948 1244 (inode->i_sb->s_blocksize_bits - 9);
1da177e4 1245
56774805 1246 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
cbf5676a 1247 inode->i_atime = sbi->s_record_time;
1248
56774805
MS
1249 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1250 fe->modificationTime))
cbf5676a 1251 inode->i_mtime = sbi->s_record_time;
1252
56774805 1253 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
cbf5676a 1254 inode->i_ctime = sbi->s_record_time;
1da177e4 1255
48d6d8ff
MS
1256 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;
cb00ea35 1260 } else {
647bd61a 1261 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
cb00ea35 1262 (inode->i_sb->s_blocksize_bits - 9);
1da177e4 1263
56774805 1264 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
cbf5676a 1265 inode->i_atime = sbi->s_record_time;
1266
56774805
MS
1267 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1268 efe->modificationTime))
cbf5676a 1269 inode->i_mtime = sbi->s_record_time;
1270
56774805 1271 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
cbf5676a 1272 iinfo->i_crtime = sbi->s_record_time;
1273
56774805 1274 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
cbf5676a 1275 inode->i_ctime = sbi->s_record_time;
1da177e4 1276
48d6d8ff
MS
1277 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);
4b11111a 1280 offset = sizeof(struct extendedFileEntry) +
48d6d8ff 1281 iinfo->i_lenEAttr;
1da177e4
LT
1282 }
1283
cb00ea35
CG
1284 switch (fe->icbTag.fileType) {
1285 case ICBTAG_FILE_TYPE_DIRECTORY:
28de7948
CG
1286 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;
cb00ea35
CG
1291 case ICBTAG_FILE_TYPE_REALTIME:
1292 case ICBTAG_FILE_TYPE_REGULAR:
1293 case ICBTAG_FILE_TYPE_UNDEF:
742e1795 1294 case ICBTAG_FILE_TYPE_VAT20:
48d6d8ff 1295 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
28de7948
CG
1296 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;
cb00ea35 1303 case ICBTAG_FILE_TYPE_BLOCK:
28de7948
CG
1304 inode->i_mode |= S_IFBLK;
1305 break;
cb00ea35 1306 case ICBTAG_FILE_TYPE_CHAR:
28de7948
CG
1307 inode->i_mode |= S_IFCHR;
1308 break;
cb00ea35 1309 case ICBTAG_FILE_TYPE_FIFO:
28de7948
CG
1310 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1311 break;
cb00ea35 1312 case ICBTAG_FILE_TYPE_SOCKET:
28de7948
CG
1313 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1314 break;
cb00ea35 1315 case ICBTAG_FILE_TYPE_SYMLINK:
28de7948
CG
1316 inode->i_data.a_ops = &udf_symlink_aops;
1317 inode->i_op = &page_symlink_inode_operations;
1318 inode->i_mode = S_IFLNK | S_IRWXUGO;
1319 break;
bfb257a5
JK
1320 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;
cb00ea35 1329 default:
4b11111a
MS
1330 printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
1331 "file type=%d\n", inode->i_ino,
1332 fe->icbTag.fileType);
28de7948
CG
1333 make_bad_inode(inode);
1334 return;
1da177e4 1335 }
cb00ea35 1336 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4b11111a
MS
1337 struct deviceSpec *dsea =
1338 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
cb00ea35
CG
1339 if (dsea) {
1340 init_special_inode(inode, inode->i_mode,
4b11111a
MS
1341 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1342 le32_to_cpu(dsea->minorDeviceIdent)));
1da177e4 1343 /* Developer ID ??? */
4b11111a 1344 } else
1da177e4 1345 make_bad_inode(inode);
1da177e4
LT
1346 }
1347}
1348
647bd61a
CG
1349static int udf_alloc_i_data(struct inode *inode, size_t size)
1350{
48d6d8ff
MS
1351 struct udf_inode_info *iinfo = UDF_I(inode);
1352 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
647bd61a 1353
48d6d8ff 1354 if (!iinfo->i_ext.i_data) {
4b11111a
MS
1355 printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
1356 "no free memory\n", inode->i_ino);
647bd61a
CG
1357 return -ENOMEM;
1358 }
1359
1360 return 0;
1361}
1362
cb00ea35 1363static mode_t udf_convert_permissions(struct fileEntry *fe)
1da177e4
LT
1364{
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
4b11111a
MS
1372 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);
1da177e4
LT
1378
1379 return mode;
1380}
1381
a9185b41 1382int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1da177e4
LT
1383{
1384 int ret;
28de7948 1385
1da177e4 1386 lock_kernel();
a9185b41 1387 ret = udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1da177e4 1388 unlock_kernel();
28de7948 1389
1da177e4
LT
1390 return ret;
1391}
1392
cb00ea35 1393int udf_sync_inode(struct inode *inode)
1da177e4
LT
1394{
1395 return udf_update_inode(inode, 1);
1396}
1397
cb00ea35 1398static int udf_update_inode(struct inode *inode, int do_sync)
1da177e4
LT
1399{
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;
1da177e4 1406 int err = 0;
6c79e987 1407 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
4b11111a 1408 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
48d6d8ff 1409 struct udf_inode_info *iinfo = UDF_I(inode);
1da177e4 1410
5833ded9
JK
1411 bh = udf_tgetblk(inode->i_sb,
1412 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
cb00ea35 1413 if (!bh) {
aae917cd
JK
1414 udf_debug("getblk failure\n");
1415 return -ENOMEM;
1da177e4
LT
1416 }
1417
aae917cd
JK
1418 lock_buffer(bh);
1419 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1da177e4
LT
1420 fe = (struct fileEntry *)bh->b_data;
1421 efe = (struct extendedFileEntry *)bh->b_data;
1422
aae917cd 1423 if (iinfo->i_use) {
1da177e4 1424 struct unallocSpaceEntry *use =
28de7948 1425 (struct unallocSpaceEntry *)bh->b_data;
1da177e4 1426
48d6d8ff 1427 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
4b11111a 1428 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
48d6d8ff 1429 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
4b11111a 1430 sizeof(struct unallocSpaceEntry));
aae917cd
JK
1431 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1432 use->descTag.tagLocation =
1433 cpu_to_le32(iinfo->i_location.logicalBlockNum);
4b11111a 1434 crclen = sizeof(struct unallocSpaceEntry) +
5ca4e4be 1435 iinfo->i_lenAlloc - sizeof(struct tag);
1da177e4 1436 use->descTag.descCRCLength = cpu_to_le16(crclen);
f845fced 1437 use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
5ca4e4be 1438 sizeof(struct tag),
f845fced 1439 crclen));
3f2587bb 1440 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
1da177e4 1441
aae917cd 1442 goto out;
1da177e4
LT
1443 }
1444
4d6660eb
PS
1445 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1446 fe->uid = cpu_to_le32(-1);
cb00ea35
CG
1447 else
1448 fe->uid = cpu_to_le32(inode->i_uid);
1da177e4 1449
4d6660eb
PS
1450 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1451 fe->gid = cpu_to_le32(-1);
cb00ea35
CG
1452 else
1453 fe->gid = cpu_to_le32(inode->i_gid);
1da177e4 1454
4b11111a
MS
1455 udfperms = ((inode->i_mode & S_IRWXO)) |
1456 ((inode->i_mode & S_IRWXG) << 2) |
1457 ((inode->i_mode & S_IRWXU) << 4);
1da177e4 1458
4b11111a
MS
1459 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));
1da177e4
LT
1463 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
cb00ea35 1472 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
5ca4e4be 1473 struct regid *eid;
28de7948
CG
1474 struct deviceSpec *dsea =
1475 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
cb00ea35 1476 if (!dsea) {
1da177e4 1477 dsea = (struct deviceSpec *)
28de7948
CG
1478 udf_add_extendedattr(inode,
1479 sizeof(struct deviceSpec) +
5ca4e4be 1480 sizeof(struct regid), 12, 0x3);
1da177e4
LT
1481 dsea->attrType = cpu_to_le32(12);
1482 dsea->attrSubtype = 1;
4b11111a
MS
1483 dsea->attrLength = cpu_to_le32(
1484 sizeof(struct deviceSpec) +
5ca4e4be
PE
1485 sizeof(struct regid));
1486 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1da177e4 1487 }
5ca4e4be
PE
1488 eid = (struct regid *)dsea->impUse;
1489 memset(eid, 0, sizeof(struct regid));
1da177e4
LT
1490 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
48d6d8ff 1497 if (iinfo->i_efe == 0) {
c0b34438 1498 memcpy(bh->b_data + sizeof(struct fileEntry),
48d6d8ff 1499 iinfo->i_ext.i_data,
cb00ea35 1500 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
28de7948 1501 fe->logicalBlocksRecorded = cpu_to_le64(
4b11111a
MS
1502 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1503 (blocksize_bits - 9));
1da177e4 1504
56774805
MS
1505 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);
5ca4e4be 1508 memset(&(fe->impIdent), 0, sizeof(struct regid));
1da177e4
LT
1509 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;
48d6d8ff
MS
1512 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);
1da177e4
LT
1515 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1516 crclen = sizeof(struct fileEntry);
cb00ea35 1517 } else {
4b11111a 1518 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
48d6d8ff 1519 iinfo->i_ext.i_data,
4b11111a
MS
1520 inode->i_sb->s_blocksize -
1521 sizeof(struct extendedFileEntry));
1da177e4 1522 efe->objectSize = cpu_to_le64(inode->i_size);
28de7948 1523 efe->logicalBlocksRecorded = cpu_to_le64(
4b11111a
MS
1524 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1525 (blocksize_bits - 9));
1da177e4 1526
48d6d8ff
MS
1527 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;
4b11111a 1531
48d6d8ff
MS
1532 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;
4b11111a 1536
48d6d8ff
MS
1537 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;
1da177e4 1541
56774805
MS
1542 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);
1da177e4 1546
5ca4e4be 1547 memset(&(efe->impIdent), 0, sizeof(struct regid));
1da177e4
LT
1548 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;
48d6d8ff
MS
1551 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);
1da177e4
LT
1554 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1555 crclen = sizeof(struct extendedFileEntry);
1556 }
48d6d8ff 1557 if (iinfo->i_strat4096) {
1da177e4
LT
1558 fe->icbTag.strategyType = cpu_to_le16(4096);
1559 fe->icbTag.strategyParameter = cpu_to_le16(1);
1560 fe->icbTag.numEntries = cpu_to_le16(2);
cb00ea35 1561 } else {
1da177e4
LT
1562 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
48d6d8ff 1581 icbflags = iinfo->i_alloc_type |
28de7948
CG
1582 ((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));
1da177e4
LT
1588
1589 fe->icbTag.flags = cpu_to_le16(icbflags);
6c79e987 1590 if (sbi->s_udfrev >= 0x0200)
1da177e4
LT
1591 fe->descTag.descVersion = cpu_to_le16(3);
1592 else
1593 fe->descTag.descVersion = cpu_to_le16(2);
6c79e987 1594 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
4b11111a 1595 fe->descTag.tagLocation = cpu_to_le32(
48d6d8ff 1596 iinfo->i_location.logicalBlockNum);
aae917cd 1597 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1da177e4 1598 fe->descTag.descCRCLength = cpu_to_le16(crclen);
5ca4e4be 1599 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
f845fced 1600 crclen));
3f2587bb 1601 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1da177e4 1602
aae917cd 1603out:
5833ded9 1604 set_buffer_uptodate(bh);
aae917cd
JK
1605 unlock_buffer(bh);
1606
1da177e4
LT
1607 /* write the data blocks */
1608 mark_buffer_dirty(bh);
cb00ea35 1609 if (do_sync) {
1da177e4 1610 sync_dirty_buffer(bh);
aae917cd 1611 if (buffer_write_io_error(bh)) {
4b11111a
MS
1612 printk(KERN_WARNING "IO error syncing udf inode "
1613 "[%s:%08lx]\n", inode->i_sb->s_id,
1614 inode->i_ino);
1da177e4
LT
1615 err = -EIO;
1616 }
1617 }
3bf25cb4 1618 brelse(bh);
28de7948 1619
1da177e4
LT
1620 return err;
1621}
1622
97e961fd 1623struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
1da177e4
LT
1624{
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) {
97e961fd 1632 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1da177e4
LT
1633 __udf_read_inode(inode);
1634 unlock_new_inode(inode);
1635 }
1636
1637 if (is_bad_inode(inode))
1638 goto out_iput;
1639
97e961fd
PE
1640 if (ino->logicalBlockNum >= UDF_SB(sb)->
1641 s_partmaps[ino->partitionReferenceNum].s_partition_len) {
1da177e4 1642 udf_debug("block=%d, partition=%d out of range\n",
97e961fd 1643 ino->logicalBlockNum, ino->partitionReferenceNum);
1da177e4
LT
1644 make_bad_inode(inode);
1645 goto out_iput;
1646 }
1647
1648 return inode;
1649
28de7948 1650 out_iput:
1da177e4
LT
1651 iput(inode);
1652 return NULL;
1653}
1654
4b11111a 1655int8_t udf_add_aext(struct inode *inode, struct extent_position *epos,
97e961fd 1656 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1da177e4
LT
1657{
1658 int adsize;
5ca4e4be
PE
1659 struct short_ad *sad = NULL;
1660 struct long_ad *lad = NULL;
1da177e4
LT
1661 struct allocExtDesc *aed;
1662 int8_t etype;
1663 uint8_t *ptr;
48d6d8ff 1664 struct udf_inode_info *iinfo = UDF_I(inode);
1da177e4 1665
ff116fc8 1666 if (!epos->bh)
48d6d8ff 1667 ptr = iinfo->i_ext.i_data + epos->offset -
4b11111a 1668 udf_file_entry_alloc_offset(inode) +
48d6d8ff 1669 iinfo->i_lenEAttr;
1da177e4 1670 else
ff116fc8 1671 ptr = epos->bh->b_data + epos->offset;
1da177e4 1672
48d6d8ff 1673 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 1674 adsize = sizeof(struct short_ad);
48d6d8ff 1675 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 1676 adsize = sizeof(struct long_ad);
1da177e4
LT
1677 else
1678 return -1;
1679
cb00ea35 1680 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
391e8bbd 1681 unsigned char *sptr, *dptr;
1da177e4
LT
1682 struct buffer_head *nbh;
1683 int err, loffset;
5ca4e4be 1684 struct kernel_lb_addr obloc = epos->block;
1da177e4 1685
4b11111a
MS
1686 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1687 obloc.partitionReferenceNum,
1688 obloc.logicalBlockNum, &err);
1689 if (!epos->block.logicalBlockNum)
1da177e4 1690 return -1;
4b11111a 1691 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
97e961fd 1692 &epos->block,
4b11111a
MS
1693 0));
1694 if (!nbh)
1da177e4 1695 return -1;
1da177e4
LT
1696 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))
4b11111a
MS
1704 aed->previousAllocExtLocation =
1705 cpu_to_le32(obloc.logicalBlockNum);
cb00ea35 1706 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
ff116fc8 1707 loffset = epos->offset;
1da177e4
LT
1708 aed->lengthAllocDescs = cpu_to_le32(adsize);
1709 sptr = ptr - adsize;
1710 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1711 memcpy(dptr, sptr, adsize);
ff116fc8 1712 epos->offset = sizeof(struct allocExtDesc) + adsize;
cb00ea35 1713 } else {
ff116fc8 1714 loffset = epos->offset + adsize;
1da177e4
LT
1715 aed->lengthAllocDescs = cpu_to_le32(0);
1716 sptr = ptr;
ff116fc8 1717 epos->offset = sizeof(struct allocExtDesc);
1da177e4 1718
cb00ea35 1719 if (epos->bh) {
ff116fc8 1720 aed = (struct allocExtDesc *)epos->bh->b_data;
c2104fda 1721 le32_add_cpu(&aed->lengthAllocDescs, adsize);
cb00ea35 1722 } else {
48d6d8ff 1723 iinfo->i_lenAlloc += adsize;
1da177e4
LT
1724 mark_inode_dirty(inode);
1725 }
1726 }
6c79e987 1727 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
1da177e4 1728 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
5ca4e4be 1729 epos->block.logicalBlockNum, sizeof(struct tag));
1da177e4
LT
1730 else
1731 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
5ca4e4be 1732 epos->block.logicalBlockNum, sizeof(struct tag));
48d6d8ff 1733 switch (iinfo->i_alloc_type) {
cb00ea35 1734 case ICBTAG_FLAG_AD_SHORT:
5ca4e4be 1735 sad = (struct short_ad *)sptr;
28de7948
CG
1736 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1737 inode->i_sb->s_blocksize);
4b11111a
MS
1738 sad->extPosition =
1739 cpu_to_le32(epos->block.logicalBlockNum);
28de7948 1740 break;
cb00ea35 1741 case ICBTAG_FLAG_AD_LONG:
5ca4e4be 1742 lad = (struct long_ad *)sptr;
28de7948
CG
1743 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;
1da177e4 1748 }
cb00ea35 1749 if (epos->bh) {
28de7948 1750 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
6c79e987 1751 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
ff116fc8 1752 udf_update_tag(epos->bh->b_data, loffset);
1da177e4 1753 else
4b11111a
MS
1754 udf_update_tag(epos->bh->b_data,
1755 sizeof(struct allocExtDesc));
ff116fc8 1756 mark_buffer_dirty_inode(epos->bh, inode);
3bf25cb4 1757 brelse(epos->bh);
28de7948 1758 } else {
1da177e4 1759 mark_inode_dirty(inode);
28de7948 1760 }
ff116fc8 1761 epos->bh = nbh;
1da177e4
LT
1762 }
1763
ff116fc8 1764 etype = udf_write_aext(inode, epos, eloc, elen, inc);
1da177e4 1765
cb00ea35 1766 if (!epos->bh) {
48d6d8ff 1767 iinfo->i_lenAlloc += adsize;
1da177e4 1768 mark_inode_dirty(inode);
cb00ea35 1769 } else {
ff116fc8 1770 aed = (struct allocExtDesc *)epos->bh->b_data;
c2104fda 1771 le32_add_cpu(&aed->lengthAllocDescs, adsize);
4b11111a
MS
1772 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));
1da177e4 1776 else
4b11111a
MS
1777 udf_update_tag(epos->bh->b_data,
1778 sizeof(struct allocExtDesc));
ff116fc8 1779 mark_buffer_dirty_inode(epos->bh, inode);
1da177e4
LT
1780 }
1781
1782 return etype;
1783}
1784
4b11111a 1785int8_t udf_write_aext(struct inode *inode, struct extent_position *epos,
97e961fd 1786 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1da177e4
LT
1787{
1788 int adsize;
1789 uint8_t *ptr;
5ca4e4be
PE
1790 struct short_ad *sad;
1791 struct long_ad *lad;
48d6d8ff 1792 struct udf_inode_info *iinfo = UDF_I(inode);
1da177e4 1793
ff116fc8 1794 if (!epos->bh)
48d6d8ff 1795 ptr = iinfo->i_ext.i_data + epos->offset -
4b11111a 1796 udf_file_entry_alloc_offset(inode) +
48d6d8ff 1797 iinfo->i_lenEAttr;
1da177e4 1798 else
ff116fc8 1799 ptr = epos->bh->b_data + epos->offset;
1da177e4 1800
48d6d8ff 1801 switch (iinfo->i_alloc_type) {
cb00ea35 1802 case ICBTAG_FLAG_AD_SHORT:
5ca4e4be 1803 sad = (struct short_ad *)ptr;
28de7948 1804 sad->extLength = cpu_to_le32(elen);
97e961fd 1805 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
5ca4e4be 1806 adsize = sizeof(struct short_ad);
28de7948 1807 break;
cb00ea35 1808 case ICBTAG_FLAG_AD_LONG:
5ca4e4be 1809 lad = (struct long_ad *)ptr;
28de7948 1810 lad->extLength = cpu_to_le32(elen);
97e961fd 1811 lad->extLocation = cpu_to_lelb(*eloc);
28de7948 1812 memset(lad->impUse, 0x00, sizeof(lad->impUse));
5ca4e4be 1813 adsize = sizeof(struct long_ad);
28de7948 1814 break;
cb00ea35
CG
1815 default:
1816 return -1;
1da177e4
LT
1817 }
1818
cb00ea35 1819 if (epos->bh) {
28de7948 1820 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
6c79e987 1821 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
4b11111a
MS
1822 struct allocExtDesc *aed =
1823 (struct allocExtDesc *)epos->bh->b_data;
ff116fc8 1824 udf_update_tag(epos->bh->b_data,
4b11111a
MS
1825 le32_to_cpu(aed->lengthAllocDescs) +
1826 sizeof(struct allocExtDesc));
1da177e4 1827 }
ff116fc8 1828 mark_buffer_dirty_inode(epos->bh, inode);
28de7948 1829 } else {
1da177e4 1830 mark_inode_dirty(inode);
28de7948 1831 }
1da177e4
LT
1832
1833 if (inc)
ff116fc8 1834 epos->offset += adsize;
28de7948 1835
1da177e4
LT
1836 return (elen >> 30);
1837}
1838
4b11111a 1839int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
5ca4e4be 1840 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
1da177e4
LT
1841{
1842 int8_t etype;
1843
ff116fc8 1844 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
cb00ea35 1845 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
4b11111a 1846 int block;
ff116fc8
JK
1847 epos->block = *eloc;
1848 epos->offset = sizeof(struct allocExtDesc);
3bf25cb4 1849 brelse(epos->bh);
97e961fd 1850 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
4b11111a
MS
1851 epos->bh = udf_tread(inode->i_sb, block);
1852 if (!epos->bh) {
1853 udf_debug("reading block %d failed!\n", block);
1da177e4
LT
1854 return -1;
1855 }
1856 }
1857
1858 return etype;
1859}
1860
4b11111a 1861int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
5ca4e4be 1862 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
1da177e4
LT
1863{
1864 int alen;
1865 int8_t etype;
1866 uint8_t *ptr;
5ca4e4be
PE
1867 struct short_ad *sad;
1868 struct long_ad *lad;
48d6d8ff 1869 struct udf_inode_info *iinfo = UDF_I(inode);
28de7948 1870
cb00ea35 1871 if (!epos->bh) {
ff116fc8
JK
1872 if (!epos->offset)
1873 epos->offset = udf_file_entry_alloc_offset(inode);
48d6d8ff 1874 ptr = iinfo->i_ext.i_data + epos->offset -
4b11111a 1875 udf_file_entry_alloc_offset(inode) +
48d6d8ff 1876 iinfo->i_lenEAttr;
4b11111a 1877 alen = udf_file_entry_alloc_offset(inode) +
48d6d8ff 1878 iinfo->i_lenAlloc;
cb00ea35 1879 } else {
ff116fc8
JK
1880 if (!epos->offset)
1881 epos->offset = sizeof(struct allocExtDesc);
1882 ptr = epos->bh->b_data + epos->offset;
28de7948 1883 alen = sizeof(struct allocExtDesc) +
4b11111a
MS
1884 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
1885 lengthAllocDescs);
1da177e4
LT
1886 }
1887
48d6d8ff 1888 switch (iinfo->i_alloc_type) {
cb00ea35 1889 case ICBTAG_FLAG_AD_SHORT:
4b11111a
MS
1890 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
1891 if (!sad)
28de7948
CG
1892 return -1;
1893 etype = le32_to_cpu(sad->extLength) >> 30;
1894 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
4b11111a 1895 eloc->partitionReferenceNum =
48d6d8ff 1896 iinfo->i_location.partitionReferenceNum;
28de7948
CG
1897 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
1898 break;
cb00ea35 1899 case ICBTAG_FLAG_AD_LONG:
4b11111a
MS
1900 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
1901 if (!lad)
1da177e4 1902 return -1;
28de7948
CG
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:
4b11111a 1908 udf_debug("alloc_type = %d unsupported\n",
48d6d8ff 1909 iinfo->i_alloc_type);
28de7948 1910 return -1;
1da177e4
LT
1911 }
1912
1913 return etype;
1914}
1915
28de7948 1916static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
5ca4e4be 1917 struct kernel_lb_addr neloc, uint32_t nelen)
1da177e4 1918{
5ca4e4be 1919 struct kernel_lb_addr oeloc;
1da177e4
LT
1920 uint32_t oelen;
1921 int8_t etype;
1922
ff116fc8 1923 if (epos.bh)
3bf25cb4 1924 get_bh(epos.bh);
1da177e4 1925
cb00ea35 1926 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
97e961fd 1927 udf_write_aext(inode, &epos, &neloc, nelen, 1);
1da177e4
LT
1928 neloc = oeloc;
1929 nelen = (etype << 30) | oelen;
1930 }
97e961fd 1931 udf_add_aext(inode, &epos, &neloc, nelen, 1);
3bf25cb4 1932 brelse(epos.bh);
28de7948 1933
1da177e4
LT
1934 return (nelen >> 30);
1935}
1936
4b11111a 1937int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
5ca4e4be 1938 struct kernel_lb_addr eloc, uint32_t elen)
1da177e4 1939{
ff116fc8
JK
1940 struct extent_position oepos;
1941 int adsize;
1da177e4
LT
1942 int8_t etype;
1943 struct allocExtDesc *aed;
48d6d8ff 1944 struct udf_inode_info *iinfo;
1da177e4 1945
cb00ea35 1946 if (epos.bh) {
3bf25cb4
JK
1947 get_bh(epos.bh);
1948 get_bh(epos.bh);
1da177e4
LT
1949 }
1950
48d6d8ff
MS
1951 iinfo = UDF_I(inode);
1952 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 1953 adsize = sizeof(struct short_ad);
48d6d8ff 1954 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 1955 adsize = sizeof(struct long_ad);
1da177e4
LT
1956 else
1957 adsize = 0;
1958
ff116fc8
JK
1959 oepos = epos;
1960 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
1da177e4
LT
1961 return -1;
1962
cb00ea35 1963 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
97e961fd 1964 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
cb00ea35 1965 if (oepos.bh != epos.bh) {
ff116fc8 1966 oepos.block = epos.block;
3bf25cb4
JK
1967 brelse(oepos.bh);
1968 get_bh(epos.bh);
ff116fc8
JK
1969 oepos.bh = epos.bh;
1970 oepos.offset = epos.offset - adsize;
1da177e4
LT
1971 }
1972 }
5ca4e4be 1973 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
1da177e4
LT
1974 elen = 0;
1975
cb00ea35 1976 if (epos.bh != oepos.bh) {
97e961fd
PE
1977 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);
cb00ea35 1980 if (!oepos.bh) {
48d6d8ff 1981 iinfo->i_lenAlloc -= (adsize * 2);
1da177e4 1982 mark_inode_dirty(inode);
cb00ea35 1983 } else {
ff116fc8 1984 aed = (struct allocExtDesc *)oepos.bh->b_data;
c2104fda 1985 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
28de7948 1986 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
6c79e987 1987 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
4b11111a
MS
1988 udf_update_tag(oepos.bh->b_data,
1989 oepos.offset - (2 * adsize));
1da177e4 1990 else
4b11111a
MS
1991 udf_update_tag(oepos.bh->b_data,
1992 sizeof(struct allocExtDesc));
ff116fc8 1993 mark_buffer_dirty_inode(oepos.bh, inode);
1da177e4 1994 }
cb00ea35 1995 } else {
97e961fd 1996 udf_write_aext(inode, &oepos, &eloc, elen, 1);
cb00ea35 1997 if (!oepos.bh) {
48d6d8ff 1998 iinfo->i_lenAlloc -= adsize;
1da177e4 1999 mark_inode_dirty(inode);
cb00ea35 2000 } else {
ff116fc8 2001 aed = (struct allocExtDesc *)oepos.bh->b_data;
c2104fda 2002 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
28de7948 2003 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
6c79e987 2004 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
4b11111a
MS
2005 udf_update_tag(oepos.bh->b_data,
2006 epos.offset - adsize);
1da177e4 2007 else
4b11111a
MS
2008 udf_update_tag(oepos.bh->b_data,
2009 sizeof(struct allocExtDesc));
ff116fc8 2010 mark_buffer_dirty_inode(oepos.bh, inode);
1da177e4
LT
2011 }
2012 }
647bd61a 2013
3bf25cb4
JK
2014 brelse(epos.bh);
2015 brelse(oepos.bh);
28de7948 2016
1da177e4
LT
2017 return (elen >> 30);
2018}
2019
4b11111a 2020int8_t inode_bmap(struct inode *inode, sector_t block,
5ca4e4be 2021 struct extent_position *pos, struct kernel_lb_addr *eloc,
4b11111a 2022 uint32_t *elen, sector_t *offset)
1da177e4 2023{
4b11111a 2024 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
cb00ea35 2025 loff_t lbcount = 0, bcount =
4b11111a 2026 (loff_t) block << blocksize_bits;
1da177e4 2027 int8_t etype;
48d6d8ff 2028 struct udf_inode_info *iinfo;
1da177e4 2029
48d6d8ff 2030 iinfo = UDF_I(inode);
ff116fc8 2031 pos->offset = 0;
48d6d8ff 2032 pos->block = iinfo->i_location;
ff116fc8 2033 pos->bh = NULL;
1da177e4 2034 *elen = 0;
1da177e4 2035
cb00ea35 2036 do {
4b11111a
MS
2037 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2038 if (etype == -1) {
2039 *offset = (bcount - lbcount) >> blocksize_bits;
48d6d8ff 2040 iinfo->i_lenExtents = lbcount;
1da177e4
LT
2041 return -1;
2042 }
2043 lbcount += *elen;
2044 } while (lbcount <= bcount);
2045
4b11111a 2046 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
1da177e4
LT
2047
2048 return etype;
2049}
2050
60448b1d 2051long udf_block_map(struct inode *inode, sector_t block)
1da177e4 2052{
5ca4e4be 2053 struct kernel_lb_addr eloc;
ff116fc8 2054 uint32_t elen;
60448b1d 2055 sector_t offset;
28de7948 2056 struct extent_position epos = {};
1da177e4
LT
2057 int ret;
2058
2059 lock_kernel();
2060
4b11111a
MS
2061 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2062 (EXT_RECORDED_ALLOCATED >> 30))
97e961fd 2063 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
1da177e4
LT
2064 else
2065 ret = 0;
2066
2067 unlock_kernel();
3bf25cb4 2068 brelse(epos.bh);
1da177e4
LT
2069
2070 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2071 return udf_fixed_to_variable(ret);
2072 else
2073 return ret;
2074}