]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/udf/inode.c
UDF: coding style conversion - lindent
[mirror_ubuntu-artful-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
22 * 12/06/98 blf partition support in udf_iget, udf_block_map and udf_read_inode
23 * 12/12/98 rewrote udf_block_map to handle next extents and descs across
24 * block boundaries (which is not actually allowed)
25 * 12/20/98 added support for strategy 4096
26 * 03/07/99 rewrote udf_block_map (again)
27 * New funcs, inode_bmap, udf_next_aext
28 * 04/19/99 Support for writing device EA's for major/minor #
29 */
30
31#include "udfdecl.h"
32#include <linux/mm.h>
33#include <linux/smp_lock.h>
34#include <linux/module.h>
35#include <linux/pagemap.h>
36#include <linux/buffer_head.h>
37#include <linux/writeback.h>
38#include <linux/slab.h>
39
40#include "udf_i.h"
41#include "udf_sb.h"
42
43MODULE_AUTHOR("Ben Fennema");
44MODULE_DESCRIPTION("Universal Disk Format Filesystem");
45MODULE_LICENSE("GPL");
46
47#define EXTENT_MERGE_SIZE 5
48
49static mode_t udf_convert_permissions(struct fileEntry *);
50static int udf_update_inode(struct inode *, int);
51static void udf_fill_inode(struct inode *, struct buffer_head *);
647bd61a 52static int udf_alloc_i_data(struct inode *inode, size_t size);
60448b1d 53static struct buffer_head *inode_getblk(struct inode *, sector_t, int *,
cb00ea35 54 long *, int *);
ff116fc8 55static int8_t udf_insert_aext(struct inode *, struct extent_position,
cb00ea35 56 kernel_lb_addr, uint32_t);
1da177e4 57static void udf_split_extents(struct inode *, int *, int, int,
cb00ea35 58 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
1da177e4 59static void udf_prealloc_extents(struct inode *, int, int,
cb00ea35 60 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
1da177e4 61static void udf_merge_extents(struct inode *,
cb00ea35 62 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
1da177e4 63static void udf_update_extents(struct inode *,
cb00ea35
CG
64 kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
65 struct extent_position *);
1da177e4
LT
66static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
67
68/*
69 * udf_delete_inode
70 *
71 * PURPOSE
72 * Clean-up before the specified inode is destroyed.
73 *
74 * DESCRIPTION
75 * This routine is called when the kernel destroys an inode structure
76 * ie. when iput() finds i_count == 0.
77 *
78 * HISTORY
79 * July 1, 1997 - Andrew E. Mileski
80 * Written, tested, and released.
81 *
82 * Called at the last iput() if i_nlink is zero.
83 */
cb00ea35 84void udf_delete_inode(struct inode *inode)
1da177e4 85{
fef26658
MF
86 truncate_inode_pages(&inode->i_data, 0);
87
1da177e4
LT
88 if (is_bad_inode(inode))
89 goto no_delete;
90
91 inode->i_size = 0;
92 udf_truncate(inode);
93 lock_kernel();
94
95 udf_update_inode(inode, IS_SYNC(inode));
96 udf_free_inode(inode);
97
98 unlock_kernel();
99 return;
cb00ea35 100 no_delete:
1da177e4
LT
101 clear_inode(inode);
102}
103
74584ae5
JK
104/*
105 * If we are going to release inode from memory, we discard preallocation and
106 * truncate last inode extent to proper length. We could use drop_inode() but
107 * it's called under inode_lock and thus we cannot mark inode dirty there. We
108 * use clear_inode() but we have to make sure to write inode as it's not written
109 * automatically.
110 */
1da177e4
LT
111void udf_clear_inode(struct inode *inode)
112{
113 if (!(inode->i_sb->s_flags & MS_RDONLY)) {
114 lock_kernel();
74584ae5 115 /* Discard preallocation for directories, symlinks, etc. */
1da177e4 116 udf_discard_prealloc(inode);
74584ae5 117 udf_truncate_tail_extent(inode);
1da177e4 118 unlock_kernel();
74584ae5 119 write_inode_now(inode, 1);
1da177e4 120 }
1da177e4
LT
121 kfree(UDF_I_DATA(inode));
122 UDF_I_DATA(inode) = NULL;
123}
124
125static int udf_writepage(struct page *page, struct writeback_control *wbc)
126{
127 return block_write_full_page(page, udf_get_block, wbc);
128}
129
130static int udf_readpage(struct file *file, struct page *page)
131{
132 return block_read_full_page(page, udf_get_block);
133}
134
cb00ea35
CG
135static int udf_prepare_write(struct file *file, struct page *page,
136 unsigned from, unsigned to)
1da177e4
LT
137{
138 return block_prepare_write(page, from, to, udf_get_block);
139}
140
141static sector_t udf_bmap(struct address_space *mapping, sector_t block)
142{
cb00ea35 143 return generic_block_bmap(mapping, block, udf_get_block);
1da177e4
LT
144}
145
f5e54d6e 146const struct address_space_operations udf_aops = {
cb00ea35
CG
147 .readpage = udf_readpage,
148 .writepage = udf_writepage,
149 .sync_page = block_sync_page,
150 .prepare_write = udf_prepare_write,
151 .commit_write = generic_commit_write,
152 .bmap = udf_bmap,
1da177e4
LT
153};
154
cb00ea35 155void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
1da177e4
LT
156{
157 struct page *page;
158 char *kaddr;
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
cb00ea35 167 if (!UDF_I_LENALLOC(inode)) {
1da177e4
LT
168 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
169 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_SHORT;
170 else
171 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_LONG;
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
LT
180 kaddr = kmap(page);
181 memset(kaddr + UDF_I_LENALLOC(inode), 0x00,
cb00ea35 182 PAGE_CACHE_SIZE - UDF_I_LENALLOC(inode));
1da177e4 183 memcpy(kaddr, UDF_I_DATA(inode) + UDF_I_LENEATTR(inode),
cb00ea35 184 UDF_I_LENALLOC(inode));
1da177e4
LT
185 flush_dcache_page(page);
186 SetPageUptodate(page);
187 kunmap(page);
188 }
189 memset(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode), 0x00,
cb00ea35 190 UDF_I_LENALLOC(inode));
1da177e4
LT
191 UDF_I_LENALLOC(inode) = 0;
192 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
193 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_SHORT;
194 else
195 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_LONG;
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
JK
207 struct buffer_head *dbh = NULL;
208 kernel_lb_addr eloc;
209 uint32_t elen;
1da177e4 210 uint8_t alloctype;
ff116fc8 211 struct extent_position epos;
1da177e4
LT
212
213 struct udf_fileident_bh sfibh, dfibh;
214 loff_t f_pos = udf_ext0_offset(inode) >> 2;
215 int size = (udf_ext0_offset(inode) + inode->i_size) >> 2;
216 struct fileIdentDesc cfi, *sfi, *dfi;
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) {
1da177e4
LT
224 UDF_I_ALLOCTYPE(inode) = alloctype;
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,
cb00ea35
CG
231 UDF_I_LOCATION(inode).partitionReferenceNum,
232 UDF_I_LOCATION(inode).logicalBlockNum, err);
1da177e4
LT
233
234 if (!(*block))
235 return NULL;
236 newblock = udf_get_pblock(inode->i_sb, *block,
cb00ea35
CG
237 UDF_I_LOCATION(inode).partitionReferenceNum,
238 0);
1da177e4
LT
239 if (!newblock)
240 return NULL;
241 dbh = udf_tgetblk(inode->i_sb, newblock);
242 if (!dbh)
243 return NULL;
244 lock_buffer(dbh);
245 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
246 set_buffer_uptodate(dbh);
247 unlock_buffer(dbh);
248 mark_buffer_dirty_inode(dbh, inode);
249
cb00ea35
CG
250 sfibh.soffset = sfibh.eoffset =
251 (f_pos & ((inode->i_sb->s_blocksize - 1) >> 2)) << 2;
ff116fc8 252 sfibh.sbh = sfibh.ebh = NULL;
1da177e4
LT
253 dfibh.soffset = dfibh.eoffset = 0;
254 dfibh.sbh = dfibh.ebh = dbh;
cb00ea35 255 while ((f_pos < size)) {
1da177e4 256 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_IN_ICB;
cb00ea35
CG
257 sfi =
258 udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL, NULL,
259 NULL, NULL);
260 if (!sfi) {
3bf25cb4 261 brelse(dbh);
1da177e4
LT
262 return NULL;
263 }
264 UDF_I_ALLOCTYPE(inode) = alloctype;
265 sfi->descTag.tagLocation = cpu_to_le32(*block);
266 dfibh.soffset = dfibh.eoffset;
267 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
268 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
269 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
cb00ea35
CG
270 sfi->fileIdent +
271 le16_to_cpu(sfi->lengthOfImpUse))) {
1da177e4 272 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_IN_ICB;
3bf25cb4 273 brelse(dbh);
1da177e4
LT
274 return NULL;
275 }
276 }
277 mark_buffer_dirty_inode(dbh, inode);
278
cb00ea35
CG
279 memset(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode), 0,
280 UDF_I_LENALLOC(inode));
1da177e4 281 UDF_I_LENALLOC(inode) = 0;
1da177e4 282 eloc.logicalBlockNum = *block;
cb00ea35
CG
283 eloc.partitionReferenceNum =
284 UDF_I_LOCATION(inode).partitionReferenceNum;
1da177e4
LT
285 elen = inode->i_size;
286 UDF_I_LENEXTENTS(inode) = elen;
ff116fc8
JK
287 epos.bh = NULL;
288 epos.block = UDF_I_LOCATION(inode);
289 epos.offset = udf_file_entry_alloc_offset(inode);
290 udf_add_aext(inode, &epos, eloc, elen, 0);
1da177e4
LT
291 /* UniqueID stuff */
292
3bf25cb4 293 brelse(epos.bh);
1da177e4
LT
294 mark_inode_dirty(inode);
295 return dbh;
296}
297
cb00ea35
CG
298static int udf_get_block(struct inode *inode, sector_t block,
299 struct buffer_head *bh_result, int create)
1da177e4
LT
300{
301 int err, new;
302 struct buffer_head *bh;
303 unsigned long phys;
304
cb00ea35 305 if (!create) {
1da177e4
LT
306 phys = udf_block_map(inode, block);
307 if (phys)
308 map_bh(bh_result, inode->i_sb, phys);
309 return 0;
310 }
311
312 err = -EIO;
313 new = 0;
314 bh = NULL;
315
316 lock_kernel();
317
318 if (block < 0)
319 goto abort_negative;
320
cb00ea35
CG
321 if (block == UDF_I_NEXT_ALLOC_BLOCK(inode) + 1) {
322 UDF_I_NEXT_ALLOC_BLOCK(inode)++;
323 UDF_I_NEXT_ALLOC_GOAL(inode)++;
1da177e4
LT
324 }
325
326 err = 0;
327
328 bh = inode_getblk(inode, block, &err, &phys, &new);
2c2111c2 329 BUG_ON(bh);
1da177e4
LT
330 if (err)
331 goto abort;
2c2111c2 332 BUG_ON(!phys);
1da177e4
LT
333
334 if (new)
335 set_buffer_new(bh_result);
336 map_bh(bh_result, inode->i_sb, phys);
cb00ea35 337 abort:
1da177e4
LT
338 unlock_kernel();
339 return err;
340
cb00ea35 341 abort_negative:
1da177e4
LT
342 udf_warning(inode->i_sb, "udf_get_block", "block < 0");
343 goto abort;
344}
345
cb00ea35
CG
346static struct buffer_head *udf_getblk(struct inode *inode, long block,
347 int create, int *err)
1da177e4
LT
348{
349 struct buffer_head dummy;
350
351 dummy.b_state = 0;
352 dummy.b_blocknr = -1000;
353 *err = udf_get_block(inode, block, &dummy, create);
cb00ea35 354 if (!*err && buffer_mapped(&dummy)) {
1da177e4
LT
355 struct buffer_head *bh;
356 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
cb00ea35 357 if (buffer_new(&dummy)) {
1da177e4
LT
358 lock_buffer(bh);
359 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
360 set_buffer_uptodate(bh);
361 unlock_buffer(bh);
362 mark_buffer_dirty_inode(bh, inode);
363 }
364 return bh;
365 }
366 return NULL;
367}
368
31170b6a
JK
369/* Extend the file by 'blocks' blocks, return the number of extents added */
370int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
cb00ea35 371 kernel_long_ad * last_ext, sector_t blocks)
31170b6a
JK
372{
373 sector_t add;
374 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
375 struct super_block *sb = inode->i_sb;
cb00ea35 376 kernel_lb_addr prealloc_loc = { 0, 0 };
31170b6a
JK
377 int prealloc_len = 0;
378
379 /* The previous extent is fake and we should not extend by anything
380 * - there's nothing to do... */
381 if (!blocks && fake)
382 return 0;
383 /* Round the last extent up to a multiple of block size */
384 if (last_ext->extLength & (sb->s_blocksize - 1)) {
385 last_ext->extLength =
cb00ea35
CG
386 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
387 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
388 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
31170b6a 389 UDF_I_LENEXTENTS(inode) =
cb00ea35
CG
390 (UDF_I_LENEXTENTS(inode) + sb->s_blocksize - 1) &
391 ~(sb->s_blocksize - 1);
31170b6a
JK
392 }
393 /* Last extent are just preallocated blocks? */
cb00ea35
CG
394 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
395 EXT_NOT_RECORDED_ALLOCATED) {
31170b6a
JK
396 /* Save the extent so that we can reattach it to the end */
397 prealloc_loc = last_ext->extLocation;
398 prealloc_len = last_ext->extLength;
399 /* Mark the extent as a hole */
400 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
cb00ea35 401 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
31170b6a 402 last_ext->extLocation.logicalBlockNum = 0;
cb00ea35 403 last_ext->extLocation.partitionReferenceNum = 0;
31170b6a
JK
404 }
405 /* Can we merge with the previous extent? */
cb00ea35
CG
406 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
407 EXT_NOT_RECORDED_NOT_ALLOCATED) {
408 add =
409 ((1 << 30) - sb->s_blocksize -
410 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >> sb->
411 s_blocksize_bits;
31170b6a
JK
412 if (add > blocks)
413 add = blocks;
414 blocks -= add;
415 last_ext->extLength += add << sb->s_blocksize_bits;
416 }
417
418 if (fake) {
419 udf_add_aext(inode, last_pos, last_ext->extLocation,
cb00ea35 420 last_ext->extLength, 1);
31170b6a 421 count++;
cb00ea35
CG
422 } else
423 udf_write_aext(inode, last_pos, last_ext->extLocation,
424 last_ext->extLength, 1);
31170b6a
JK
425 /* Managed to do everything necessary? */
426 if (!blocks)
427 goto out;
428
429 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
430 last_ext->extLocation.logicalBlockNum = 0;
cb00ea35
CG
431 last_ext->extLocation.partitionReferenceNum = 0;
432 add = (1 << (30 - sb->s_blocksize_bits)) - 1;
433 last_ext->extLength =
434 EXT_NOT_RECORDED_NOT_ALLOCATED | (add << sb->s_blocksize_bits);
31170b6a
JK
435 /* Create enough extents to cover the whole hole */
436 while (blocks > add) {
437 blocks -= add;
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 |
cb00ea35 445 (blocks << sb->s_blocksize_bits);
31170b6a 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 }
cb00ea35 451 out:
31170b6a
JK
452 /* Do we have some preallocated blocks saved? */
453 if (prealloc_len) {
cb00ea35
CG
454 if (udf_add_aext(inode, last_pos, prealloc_loc, prealloc_len, 1)
455 == -1)
31170b6a
JK
456 return -1;
457 last_ext->extLocation = prealloc_loc;
458 last_ext->extLength = prealloc_len;
459 count++;
460 }
461 /* last_pos should point to the last written extent... */
462 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
463 last_pos->offset -= sizeof(short_ad);
464 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
465 last_pos->offset -= sizeof(long_ad);
466 else
467 return -1;
468 return count;
469}
470
cb00ea35
CG
471static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
472 int *err, long *phys, int *new)
1da177e4 473{
31170b6a 474 static sector_t last_block;
ff116fc8 475 struct buffer_head *result = NULL;
1da177e4 476 kernel_long_ad laarr[EXTENT_MERGE_SIZE];
ff116fc8 477 struct extent_position prev_epos, cur_epos, next_epos;
1da177e4 478 int count = 0, startnum = 0, endnum = 0;
85d71244
JK
479 uint32_t elen = 0, tmpelen;
480 kernel_lb_addr eloc, tmpeloc;
1da177e4 481 int c = 1;
60448b1d
JK
482 loff_t lbcount = 0, b_off = 0;
483 uint32_t newblocknum, newblock;
484 sector_t offset = 0;
1da177e4
LT
485 int8_t etype;
486 int goal = 0, pgoal = UDF_I_LOCATION(inode).logicalBlockNum;
31170b6a 487 int lastblock = 0;
1da177e4 488
ff116fc8
JK
489 prev_epos.offset = udf_file_entry_alloc_offset(inode);
490 prev_epos.block = UDF_I_LOCATION(inode);
491 prev_epos.bh = NULL;
492 cur_epos = next_epos = prev_epos;
cb00ea35 493 b_off = (loff_t) block << inode->i_sb->s_blocksize_bits;
1da177e4
LT
494
495 /* find the extent which contains the block we are looking for.
cb00ea35
CG
496 alternate between laarr[0] and laarr[1] for locations of the
497 current extent, and the previous extent */
498 do {
499 if (prev_epos.bh != cur_epos.bh) {
3bf25cb4
JK
500 brelse(prev_epos.bh);
501 get_bh(cur_epos.bh);
ff116fc8 502 prev_epos.bh = cur_epos.bh;
1da177e4 503 }
cb00ea35 504 if (cur_epos.bh != next_epos.bh) {
3bf25cb4
JK
505 brelse(cur_epos.bh);
506 get_bh(next_epos.bh);
ff116fc8 507 cur_epos.bh = next_epos.bh;
1da177e4
LT
508 }
509
510 lbcount += elen;
511
ff116fc8
JK
512 prev_epos.block = cur_epos.block;
513 cur_epos.block = next_epos.block;
1da177e4 514
ff116fc8
JK
515 prev_epos.offset = cur_epos.offset;
516 cur_epos.offset = next_epos.offset;
1da177e4 517
cb00ea35
CG
518 if ((etype =
519 udf_next_aext(inode, &next_epos, &eloc, &elen, 1)) == -1)
1da177e4
LT
520 break;
521
522 c = !c;
523
524 laarr[c].extLength = (etype << 30) | elen;
525 laarr[c].extLocation = eloc;
526
527 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
528 pgoal = eloc.logicalBlockNum +
cb00ea35
CG
529 ((elen + inode->i_sb->s_blocksize - 1) >>
530 inode->i_sb->s_blocksize_bits);
1da177e4 531
cb00ea35 532 count++;
1da177e4
LT
533 } while (lbcount + elen <= b_off);
534
535 b_off -= lbcount;
536 offset = b_off >> inode->i_sb->s_blocksize_bits;
85d71244
JK
537 /*
538 * Move prev_epos and cur_epos into indirect extent if we are at
539 * the pointer to it
540 */
541 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
542 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
1da177e4
LT
543
544 /* if the extent is allocated and recorded, return the block
cb00ea35 545 if the extent is not a multiple of the blocksize, round up */
1da177e4 546
cb00ea35
CG
547 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
548 if (elen & (inode->i_sb->s_blocksize - 1)) {
1da177e4 549 elen = EXT_RECORDED_ALLOCATED |
cb00ea35
CG
550 ((elen + inode->i_sb->s_blocksize - 1) &
551 ~(inode->i_sb->s_blocksize - 1));
ff116fc8 552 etype = udf_write_aext(inode, &cur_epos, eloc, elen, 1);
1da177e4 553 }
3bf25cb4
JK
554 brelse(prev_epos.bh);
555 brelse(cur_epos.bh);
556 brelse(next_epos.bh);
1da177e4
LT
557 newblock = udf_get_lb_pblock(inode->i_sb, eloc, offset);
558 *phys = newblock;
559 return NULL;
560 }
561
31170b6a
JK
562 last_block = block;
563 /* Are we beyond EOF? */
cb00ea35 564 if (etype == -1) {
31170b6a
JK
565 int ret;
566
567 if (count) {
568 if (c)
569 laarr[0] = laarr[1];
570 startnum = 1;
cb00ea35 571 } else {
31170b6a 572 /* Create a fake extent when there's not one */
cb00ea35
CG
573 memset(&laarr[0].extLocation, 0x00,
574 sizeof(kernel_lb_addr));
31170b6a
JK
575 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
576 /* Will udf_extend_file() create real extent from a fake one? */
577 startnum = (offset > 0);
578 }
579 /* Create extents for the hole between EOF and offset */
580 ret = udf_extend_file(inode, &prev_epos, laarr, offset);
581 if (ret == -1) {
582 brelse(prev_epos.bh);
583 brelse(cur_epos.bh);
584 brelse(next_epos.bh);
585 /* We don't really know the error here so we just make
586 * something up */
587 *err = -ENOSPC;
588 return NULL;
589 }
590 c = 0;
591 offset = 0;
592 count += ret;
593 /* We are not covered by a preallocated extent? */
cb00ea35
CG
594 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
595 EXT_NOT_RECORDED_ALLOCATED) {
31170b6a
JK
596 /* Is there any real extent? - otherwise we overwrite
597 * the fake one... */
598 if (count)
599 c = !c;
600 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
cb00ea35
CG
601 inode->i_sb->s_blocksize;
602 memset(&laarr[c].extLocation, 0x00,
603 sizeof(kernel_lb_addr));
604 count++;
605 endnum++;
31170b6a 606 }
cb00ea35 607 endnum = c + 1;
1da177e4 608 lastblock = 1;
cb00ea35 609 } else {
1da177e4
LT
610 endnum = startnum = ((count > 2) ? 2 : count);
611
31170b6a 612 /* if the current extent is in position 0, swap it with the previous */
cb00ea35 613 if (!c && count != 1) {
31170b6a
JK
614 laarr[2] = laarr[0];
615 laarr[0] = laarr[1];
616 laarr[1] = laarr[2];
617 c = 1;
618 }
1da177e4 619
31170b6a 620 /* if the current block is located in an extent, read the next extent */
cb00ea35
CG
621 if ((etype =
622 udf_next_aext(inode, &next_epos, &eloc, &elen, 0)) != -1) {
623 laarr[c + 1].extLength = (etype << 30) | elen;
624 laarr[c + 1].extLocation = eloc;
625 count++;
626 startnum++;
627 endnum++;
628 } else {
1da177e4 629 lastblock = 1;
31170b6a 630 }
1da177e4 631 }
1da177e4
LT
632
633 /* if the current extent is not recorded but allocated, get the
cb00ea35 634 block in the extent corresponding to the requested block */
1da177e4
LT
635 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
636 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
cb00ea35
CG
637 else { /* otherwise, allocate a new block */
638
1da177e4
LT
639 if (UDF_I_NEXT_ALLOC_BLOCK(inode) == block)
640 goal = UDF_I_NEXT_ALLOC_GOAL(inode);
641
cb00ea35 642 if (!goal) {
1da177e4 643 if (!(goal = pgoal))
cb00ea35
CG
644 goal =
645 UDF_I_LOCATION(inode).logicalBlockNum + 1;
1da177e4
LT
646 }
647
648 if (!(newblocknum = udf_new_block(inode->i_sb, inode,
cb00ea35
CG
649 UDF_I_LOCATION(inode).
650 partitionReferenceNum, goal,
651 err))) {
3bf25cb4 652 brelse(prev_epos.bh);
1da177e4
LT
653 *err = -ENOSPC;
654 return NULL;
655 }
656 UDF_I_LENEXTENTS(inode) += inode->i_sb->s_blocksize;
657 }
658
659 /* if the extent the requsted block is located in contains multiple blocks,
cb00ea35
CG
660 split the extent into at most three extents. blocks prior to requested
661 block, requested block, and blocks after requested block */
1da177e4
LT
662 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
663
664#ifdef UDF_PREALLOCATE
665 /* preallocate blocks */
666 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
667#endif
668
669 /* merge any continuous blocks in laarr */
670 udf_merge_extents(inode, laarr, &endnum);
671
672 /* write back the new extents, inserting new extents if the new number
cb00ea35
CG
673 of extents is greater than the old number, and deleting extents if
674 the new number of extents is less than the old number */
ff116fc8 675 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
1da177e4 676
3bf25cb4 677 brelse(prev_epos.bh);
1da177e4
LT
678
679 if (!(newblock = udf_get_pblock(inode->i_sb, newblocknum,
cb00ea35
CG
680 UDF_I_LOCATION(inode).
681 partitionReferenceNum, 0))) {
1da177e4
LT
682 return NULL;
683 }
684 *phys = newblock;
685 *err = 0;
686 *new = 1;
687 UDF_I_NEXT_ALLOC_BLOCK(inode) = block;
688 UDF_I_NEXT_ALLOC_GOAL(inode) = newblocknum;
689 inode->i_ctime = current_fs_time(inode->i_sb);
690
691 if (IS_SYNC(inode))
692 udf_sync_inode(inode);
693 else
694 mark_inode_dirty(inode);
695 return result;
696}
697
cb00ea35
CG
698static void udf_split_extents(struct inode *inode, int *c, int offset,
699 int newblocknum,
700 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
701 int *endnum)
1da177e4
LT
702{
703 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
cb00ea35
CG
704 (laarr[*c].extLength >> 30) ==
705 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
1da177e4
LT
706 int curr = *c;
707 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
cb00ea35
CG
708 inode->i_sb->s_blocksize -
709 1) >> inode->i_sb->s_blocksize_bits;
1da177e4
LT
710 int8_t etype = (laarr[curr].extLength >> 30);
711
cb00ea35
CG
712 if (blen == 1) ;
713 else if (!offset || blen == offset + 1) {
714 laarr[curr + 2] = laarr[curr + 1];
715 laarr[curr + 1] = laarr[curr];
716 } else {
717 laarr[curr + 3] = laarr[curr + 1];
718 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
719 }
720
721 if (offset) {
722 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
723 udf_free_blocks(inode->i_sb, inode,
724 laarr[curr].extLocation, 0,
725 offset);
726 laarr[curr].extLength =
727 EXT_NOT_RECORDED_NOT_ALLOCATED | (offset <<
728 inode->
729 i_sb->
730 s_blocksize_bits);
1da177e4 731 laarr[curr].extLocation.logicalBlockNum = 0;
cb00ea35
CG
732 laarr[curr].extLocation.partitionReferenceNum =
733 0;
734 } else
1da177e4 735 laarr[curr].extLength = (etype << 30) |
cb00ea35
CG
736 (offset << inode->i_sb->s_blocksize_bits);
737 curr++;
738 (*c)++;
739 (*endnum)++;
1da177e4 740 }
647bd61a 741
1da177e4
LT
742 laarr[curr].extLocation.logicalBlockNum = newblocknum;
743 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
744 laarr[curr].extLocation.partitionReferenceNum =
cb00ea35 745 UDF_I_LOCATION(inode).partitionReferenceNum;
1da177e4 746 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
cb00ea35
CG
747 inode->i_sb->s_blocksize;
748 curr++;
1da177e4 749
cb00ea35 750 if (blen != offset + 1) {
1da177e4 751 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
cb00ea35
CG
752 laarr[curr].extLocation.logicalBlockNum +=
753 (offset + 1);
754 laarr[curr].extLength =
755 (etype << 30) | ((blen - (offset + 1)) << inode->
756 i_sb->s_blocksize_bits);
757 curr++;
758 (*endnum)++;
1da177e4
LT
759 }
760 }
761}
762
763static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
cb00ea35
CG
764 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
765 int *endnum)
1da177e4
LT
766{
767 int start, length = 0, currlength = 0, i;
768
cb00ea35 769 if (*endnum >= (c + 1)) {
1da177e4
LT
770 if (!lastblock)
771 return;
772 else
773 start = c;
cb00ea35
CG
774 } else {
775 if ((laarr[c + 1].extLength >> 30) ==
776 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
777 start = c + 1;
778 length = currlength =
779 (((laarr[c + 1].
780 extLength & UDF_EXTENT_LENGTH_MASK) +
781 inode->i_sb->s_blocksize -
782 1) >> inode->i_sb->s_blocksize_bits);
783 } else
1da177e4
LT
784 start = c;
785 }
786
cb00ea35
CG
787 for (i = start + 1; i <= *endnum; i++) {
788 if (i == *endnum) {
1da177e4
LT
789 if (lastblock)
790 length += UDF_DEFAULT_PREALLOC_BLOCKS;
cb00ea35
CG
791 } else if ((laarr[i].extLength >> 30) ==
792 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
793 length +=
794 (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
795 inode->i_sb->s_blocksize -
796 1) >> inode->i_sb->s_blocksize_bits);
1da177e4
LT
797 else
798 break;
799 }
800
cb00ea35 801 if (length) {
1da177e4 802 int next = laarr[start].extLocation.logicalBlockNum +
cb00ea35
CG
803 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
804 inode->i_sb->s_blocksize -
805 1) >> inode->i_sb->s_blocksize_bits);
1da177e4 806 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
cb00ea35
CG
807 laarr[start].extLocation.
808 partitionReferenceNum,
809 next,
810 (UDF_DEFAULT_PREALLOC_BLOCKS
811 >
812 length ? length :
813 UDF_DEFAULT_PREALLOC_BLOCKS)
814 - currlength);
815
816 if (numalloc) {
817 if (start == (c + 1))
1da177e4 818 laarr[start].extLength +=
cb00ea35
CG
819 (numalloc << inode->i_sb->s_blocksize_bits);
820 else {
821 memmove(&laarr[c + 2], &laarr[c + 1],
822 sizeof(long_ad) * (*endnum - (c + 1)));
823 (*endnum)++;
824 laarr[c + 1].extLocation.logicalBlockNum = next;
825 laarr[c + 1].extLocation.partitionReferenceNum =
826 laarr[c].extLocation.partitionReferenceNum;
827 laarr[c + 1].extLength =
828 EXT_NOT_RECORDED_ALLOCATED | (numalloc <<
829 inode->i_sb->
830 s_blocksize_bits);
831 start = c + 1;
1da177e4
LT
832 }
833
cb00ea35
CG
834 for (i = start + 1; numalloc && i < *endnum; i++) {
835 int elen =
836 ((laarr[i].
837 extLength & UDF_EXTENT_LENGTH_MASK) +
838 inode->i_sb->s_blocksize -
839 1) >> inode->i_sb->s_blocksize_bits;
1da177e4 840
cb00ea35 841 if (elen > numalloc) {
1da177e4 842 laarr[i].extLength -=
cb00ea35
CG
843 (numalloc << inode->i_sb->
844 s_blocksize_bits);
1da177e4 845 numalloc = 0;
cb00ea35 846 } else {
1da177e4 847 numalloc -= elen;
cb00ea35
CG
848 if (*endnum > (i + 1))
849 memmove(&laarr[i],
850 &laarr[i + 1],
851 sizeof(long_ad) *
852 (*endnum - (i + 1)));
853 i--;
854 (*endnum)--;
1da177e4
LT
855 }
856 }
cb00ea35
CG
857 UDF_I_LENEXTENTS(inode) +=
858 numalloc << inode->i_sb->s_blocksize_bits;
1da177e4
LT
859 }
860 }
861}
862
863static void udf_merge_extents(struct inode *inode,
cb00ea35
CG
864 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
865 int *endnum)
1da177e4
LT
866{
867 int i;
868
cb00ea35
CG
869 for (i = 0; i < (*endnum - 1); i++) {
870 if ((laarr[i].extLength >> 30) ==
871 (laarr[i + 1].extLength >> 30)) {
872 if (((laarr[i].extLength >> 30) ==
873 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
874 ||
875 ((laarr[i + 1].extLocation.logicalBlockNum -
876 laarr[i].extLocation.logicalBlockNum) ==
877 (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
878 inode->i_sb->s_blocksize -
879 1) >> inode->i_sb->s_blocksize_bits))) {
880 if (((laarr[i].
881 extLength & UDF_EXTENT_LENGTH_MASK) +
882 (laarr[i + 1].
883 extLength & UDF_EXTENT_LENGTH_MASK) +
884 inode->i_sb->s_blocksize -
885 1) & ~UDF_EXTENT_LENGTH_MASK) {
886 laarr[i + 1].extLength =
887 (laarr[i + 1].extLength -
888 (laarr[i].
889 extLength &
890 UDF_EXTENT_LENGTH_MASK) +
891 UDF_EXTENT_LENGTH_MASK) & ~(inode->
892 i_sb->
893 s_blocksize
894 - 1);
895 laarr[i].extLength =
896 (laarr[i].
897 extLength & UDF_EXTENT_FLAG_MASK) +
898 (UDF_EXTENT_LENGTH_MASK + 1) -
899 inode->i_sb->s_blocksize;
900 laarr[i +
901 1].extLocation.logicalBlockNum =
902 laarr[i].extLocation.
903 logicalBlockNum +
904 ((laarr[i].
905 extLength &
906 UDF_EXTENT_LENGTH_MASK) >> inode->
907 i_sb->s_blocksize_bits);
908 } else {
909 laarr[i].extLength =
910 laarr[i + 1].extLength +
911 (((laarr[i].
912 extLength &
913 UDF_EXTENT_LENGTH_MASK) +
914 inode->i_sb->s_blocksize -
915 1) & ~(inode->i_sb->s_blocksize -
916 1));
917 if (*endnum > (i + 2))
918 memmove(&laarr[i + 1],
919 &laarr[i + 2],
920 sizeof(long_ad) *
921 (*endnum - (i + 2)));
922 i--;
923 (*endnum)--;
1da177e4
LT
924 }
925 }
cb00ea35
CG
926 } else
927 if (((laarr[i].extLength >> 30) ==
928 (EXT_NOT_RECORDED_ALLOCATED >> 30))
929 && ((laarr[i + 1].extLength >> 30) ==
930 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
931 udf_free_blocks(inode->i_sb, inode,
932 laarr[i].extLocation, 0,
933 ((laarr[i].
934 extLength & UDF_EXTENT_LENGTH_MASK) +
935 inode->i_sb->s_blocksize -
936 1) >> inode->i_sb->s_blocksize_bits);
1da177e4
LT
937 laarr[i].extLocation.logicalBlockNum = 0;
938 laarr[i].extLocation.partitionReferenceNum = 0;
939
940 if (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
cb00ea35
CG
941 (laarr[i + 1].extLength & UDF_EXTENT_LENGTH_MASK) +
942 inode->i_sb->s_blocksize -
943 1) & ~UDF_EXTENT_LENGTH_MASK) {
944 laarr[i + 1].extLength =
945 (laarr[i + 1].extLength -
946 (laarr[i].
947 extLength & UDF_EXTENT_LENGTH_MASK) +
948 UDF_EXTENT_LENGTH_MASK) & ~(inode->i_sb->
949 s_blocksize -
950 1);
951 laarr[i].extLength =
952 (laarr[i].
953 extLength & UDF_EXTENT_FLAG_MASK) +
954 (UDF_EXTENT_LENGTH_MASK + 1) -
955 inode->i_sb->s_blocksize;
956 } else {
957 laarr[i].extLength = laarr[i + 1].extLength +
958 (((laarr[i].
959 extLength & UDF_EXTENT_LENGTH_MASK) +
960 inode->i_sb->s_blocksize -
961 1) & ~(inode->i_sb->s_blocksize - 1));
962 if (*endnum > (i + 2))
963 memmove(&laarr[i + 1], &laarr[i + 2],
964 sizeof(long_ad) * (*endnum -
965 (i + 2)));
966 i--;
967 (*endnum)--;
1da177e4 968 }
cb00ea35
CG
969 } else if ((laarr[i].extLength >> 30) ==
970 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
971 udf_free_blocks(inode->i_sb, inode,
972 laarr[i].extLocation, 0,
973 ((laarr[i].
974 extLength & UDF_EXTENT_LENGTH_MASK) +
975 inode->i_sb->s_blocksize -
976 1) >> inode->i_sb->s_blocksize_bits);
1da177e4
LT
977 laarr[i].extLocation.logicalBlockNum = 0;
978 laarr[i].extLocation.partitionReferenceNum = 0;
cb00ea35
CG
979 laarr[i].extLength =
980 (laarr[i].
981 extLength & UDF_EXTENT_LENGTH_MASK) |
982 EXT_NOT_RECORDED_NOT_ALLOCATED;
1da177e4
LT
983 }
984 }
985}
986
987static void udf_update_extents(struct inode *inode,
cb00ea35
CG
988 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
989 int startnum, int endnum,
990 struct extent_position *epos)
1da177e4
LT
991{
992 int start = 0, i;
993 kernel_lb_addr tmploc;
994 uint32_t tmplen;
995
cb00ea35
CG
996 if (startnum > endnum) {
997 for (i = 0; i < (startnum - endnum); i++)
ff116fc8 998 udf_delete_aext(inode, *epos, laarr[i].extLocation,
cb00ea35
CG
999 laarr[i].extLength);
1000 } else if (startnum < endnum) {
1001 for (i = 0; i < (endnum - startnum); i++) {
ff116fc8 1002 udf_insert_aext(inode, *epos, laarr[i].extLocation,
cb00ea35 1003 laarr[i].extLength);
ff116fc8 1004 udf_next_aext(inode, epos, &laarr[i].extLocation,
cb00ea35
CG
1005 &laarr[i].extLength, 1);
1006 start++;
1da177e4
LT
1007 }
1008 }
1009
cb00ea35 1010 for (i = start; i < endnum; i++) {
ff116fc8
JK
1011 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1012 udf_write_aext(inode, epos, laarr[i].extLocation,
cb00ea35 1013 laarr[i].extLength, 1);
1da177e4
LT
1014 }
1015}
1016
cb00ea35
CG
1017struct buffer_head *udf_bread(struct inode *inode, int block,
1018 int create, int *err)
1da177e4 1019{
cb00ea35 1020 struct buffer_head *bh = NULL;
1da177e4
LT
1021
1022 bh = udf_getblk(inode, block, create, err);
1023 if (!bh)
1024 return NULL;
1025
1026 if (buffer_uptodate(bh))
1027 return bh;
1028 ll_rw_block(READ, 1, &bh);
1029 wait_on_buffer(bh);
1030 if (buffer_uptodate(bh))
1031 return bh;
1032 brelse(bh);
1033 *err = -EIO;
1034 return NULL;
1035}
1036
cb00ea35 1037void udf_truncate(struct inode *inode)
1da177e4
LT
1038{
1039 int offset;
1040 int err;
1041
1042 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
cb00ea35 1043 S_ISLNK(inode->i_mode)))
1da177e4
LT
1044 return;
1045 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1046 return;
1047
1048 lock_kernel();
cb00ea35
CG
1049 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB) {
1050 if (inode->i_sb->s_blocksize <
1051 (udf_file_entry_alloc_offset(inode) + inode->i_size)) {
1da177e4 1052 udf_expand_file_adinicb(inode, inode->i_size, &err);
cb00ea35 1053 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB) {
1da177e4
LT
1054 inode->i_size = UDF_I_LENALLOC(inode);
1055 unlock_kernel();
1056 return;
cb00ea35 1057 } else
1da177e4 1058 udf_truncate_extents(inode);
cb00ea35 1059 } else {
1da177e4 1060 offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
cb00ea35
CG
1061 memset(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode) +
1062 offset, 0x00,
1063 inode->i_sb->s_blocksize - offset -
1064 udf_file_entry_alloc_offset(inode));
1da177e4
LT
1065 UDF_I_LENALLOC(inode) = inode->i_size;
1066 }
cb00ea35
CG
1067 } else {
1068 block_truncate_page(inode->i_mapping, inode->i_size,
1069 udf_get_block);
1da177e4 1070 udf_truncate_extents(inode);
647bd61a 1071 }
1da177e4
LT
1072
1073 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1074 if (IS_SYNC(inode))
cb00ea35 1075 udf_sync_inode(inode);
1da177e4
LT
1076 else
1077 mark_inode_dirty(inode);
1078 unlock_kernel();
1079}
1080
cb00ea35 1081static void __udf_read_inode(struct inode *inode)
1da177e4
LT
1082{
1083 struct buffer_head *bh = NULL;
1084 struct fileEntry *fe;
1085 uint16_t ident;
1086
1087 /*
1088 * Set defaults, but the inode is still incomplete!
1089 * Note: get_new_inode() sets the following on a new inode:
1090 * i_sb = sb
1091 * i_no = ino
1092 * i_flags = sb->s_flags
1093 * i_state = 0
1094 * clean_inode(): zero fills and sets
1095 * i_count = 1
1096 * i_nlink = 1
1097 * i_op = NULL;
1098 */
1da177e4
LT
1099 bh = udf_read_ptagged(inode->i_sb, UDF_I_LOCATION(inode), 0, &ident);
1100
cb00ea35 1101 if (!bh) {
1da177e4 1102 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
cb00ea35 1103 inode->i_ino);
1da177e4
LT
1104 make_bad_inode(inode);
1105 return;
1106 }
1107
1108 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
cb00ea35
CG
1109 ident != TAG_IDENT_USE) {
1110 printk(KERN_ERR
1111 "udf: udf_read_inode(ino %ld) failed ident=%d\n",
1112 inode->i_ino, ident);
3bf25cb4 1113 brelse(bh);
1da177e4
LT
1114 make_bad_inode(inode);
1115 return;
1116 }
1117
1118 fe = (struct fileEntry *)bh->b_data;
1119
cb00ea35 1120 if (le16_to_cpu(fe->icbTag.strategyType) == 4096) {
1da177e4
LT
1121 struct buffer_head *ibh = NULL, *nbh = NULL;
1122 struct indirectEntry *ie;
1123
cb00ea35
CG
1124 ibh =
1125 udf_read_ptagged(inode->i_sb, UDF_I_LOCATION(inode), 1,
1126 &ident);
1127 if (ident == TAG_IDENT_IE) {
1128 if (ibh) {
1da177e4
LT
1129 kernel_lb_addr loc;
1130 ie = (struct indirectEntry *)ibh->b_data;
647bd61a 1131
1da177e4 1132 loc = lelb_to_cpu(ie->indirectICB.extLocation);
647bd61a
CG
1133
1134 if (ie->indirectICB.extLength &&
cb00ea35
CG
1135 (nbh =
1136 udf_read_ptagged(inode->i_sb, loc, 0,
1137 &ident))) {
1138 if (ident == TAG_IDENT_FE
1139 || ident == TAG_IDENT_EFE) {
1140 memcpy(&UDF_I_LOCATION(inode),
1141 &loc,
1142 sizeof(kernel_lb_addr));
3bf25cb4
JK
1143 brelse(bh);
1144 brelse(ibh);
1145 brelse(nbh);
1da177e4
LT
1146 __udf_read_inode(inode);
1147 return;
cb00ea35 1148 } else {
3bf25cb4
JK
1149 brelse(nbh);
1150 brelse(ibh);
1da177e4 1151 }
cb00ea35 1152 } else
3bf25cb4 1153 brelse(ibh);
1da177e4 1154 }
cb00ea35 1155 } else
3bf25cb4 1156 brelse(ibh);
cb00ea35 1157 } else if (le16_to_cpu(fe->icbTag.strategyType) != 4) {
1da177e4 1158 printk(KERN_ERR "udf: unsupported strategy type: %d\n",
cb00ea35 1159 le16_to_cpu(fe->icbTag.strategyType));
3bf25cb4 1160 brelse(bh);
1da177e4
LT
1161 make_bad_inode(inode);
1162 return;
1163 }
1164 udf_fill_inode(inode, bh);
31170b6a 1165
3bf25cb4 1166 brelse(bh);
1da177e4
LT
1167}
1168
1169static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1170{
1171 struct fileEntry *fe;
1172 struct extendedFileEntry *efe;
1173 time_t convtime;
1174 long convtime_usec;
1175 int offset;
1176
1177 fe = (struct fileEntry *)bh->b_data;
1178 efe = (struct extendedFileEntry *)bh->b_data;
1179
1180 if (le16_to_cpu(fe->icbTag.strategyType) == 4)
1181 UDF_I_STRAT4096(inode) = 0;
cb00ea35 1182 else /* if (le16_to_cpu(fe->icbTag.strategyType) == 4096) */
1da177e4
LT
1183 UDF_I_STRAT4096(inode) = 1;
1184
cb00ea35
CG
1185 UDF_I_ALLOCTYPE(inode) =
1186 le16_to_cpu(fe->icbTag.flags) & ICBTAG_FLAG_AD_MASK;
1da177e4
LT
1187 UDF_I_UNIQUE(inode) = 0;
1188 UDF_I_LENEATTR(inode) = 0;
1189 UDF_I_LENEXTENTS(inode) = 0;
1190 UDF_I_LENALLOC(inode) = 0;
1191 UDF_I_NEXT_ALLOC_BLOCK(inode) = 0;
1192 UDF_I_NEXT_ALLOC_GOAL(inode) = 0;
cb00ea35 1193 if (le16_to_cpu(fe->descTag.tagIdent) == TAG_IDENT_EFE) {
1da177e4
LT
1194 UDF_I_EFE(inode) = 1;
1195 UDF_I_USE(inode) = 0;
cb00ea35
CG
1196 if (udf_alloc_i_data
1197 (inode,
1198 inode->i_sb->s_blocksize -
1199 sizeof(struct extendedFileEntry))) {
647bd61a
CG
1200 make_bad_inode(inode);
1201 return;
1202 }
cb00ea35
CG
1203 memcpy(UDF_I_DATA(inode),
1204 bh->b_data + sizeof(struct extendedFileEntry),
1205 inode->i_sb->s_blocksize -
1206 sizeof(struct extendedFileEntry));
1207 } else if (le16_to_cpu(fe->descTag.tagIdent) == TAG_IDENT_FE) {
1da177e4
LT
1208 UDF_I_EFE(inode) = 0;
1209 UDF_I_USE(inode) = 0;
cb00ea35
CG
1210 if (udf_alloc_i_data
1211 (inode,
1212 inode->i_sb->s_blocksize - sizeof(struct fileEntry))) {
647bd61a
CG
1213 make_bad_inode(inode);
1214 return;
1215 }
cb00ea35
CG
1216 memcpy(UDF_I_DATA(inode), bh->b_data + sizeof(struct fileEntry),
1217 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1218 } else if (le16_to_cpu(fe->descTag.tagIdent) == TAG_IDENT_USE) {
1da177e4
LT
1219 UDF_I_EFE(inode) = 0;
1220 UDF_I_USE(inode) = 1;
1221 UDF_I_LENALLOC(inode) =
cb00ea35
CG
1222 le32_to_cpu(((struct unallocSpaceEntry *)bh->b_data)->
1223 lengthAllocDescs);
1224 if (udf_alloc_i_data
1225 (inode,
1226 inode->i_sb->s_blocksize -
1227 sizeof(struct unallocSpaceEntry))) {
647bd61a
CG
1228 make_bad_inode(inode);
1229 return;
1230 }
cb00ea35
CG
1231 memcpy(UDF_I_DATA(inode),
1232 bh->b_data + sizeof(struct unallocSpaceEntry),
1233 inode->i_sb->s_blocksize -
1234 sizeof(struct unallocSpaceEntry));
1da177e4
LT
1235 return;
1236 }
1237
1238 inode->i_uid = le32_to_cpu(fe->uid);
4d6660eb 1239 if (inode->i_uid == -1 || UDF_QUERY_FLAG(inode->i_sb,
cb00ea35 1240 UDF_FLAG_UID_IGNORE))
4d6660eb 1241 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1da177e4
LT
1242
1243 inode->i_gid = le32_to_cpu(fe->gid);
4d6660eb 1244 if (inode->i_gid == -1 || UDF_QUERY_FLAG(inode->i_sb,
cb00ea35 1245 UDF_FLAG_GID_IGNORE))
4d6660eb 1246 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1da177e4
LT
1247
1248 inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
1249 if (!inode->i_nlink)
1250 inode->i_nlink = 1;
647bd61a 1251
1da177e4
LT
1252 inode->i_size = le64_to_cpu(fe->informationLength);
1253 UDF_I_LENEXTENTS(inode) = inode->i_size;
1254
1255 inode->i_mode = udf_convert_permissions(fe);
1256 inode->i_mode &= ~UDF_SB(inode->i_sb)->s_umask;
1257
cb00ea35 1258 if (UDF_I_EFE(inode) == 0) {
1da177e4 1259 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
cb00ea35 1260 (inode->i_sb->s_blocksize_bits - 9);
1da177e4 1261
cb00ea35
CG
1262 if (udf_stamp_to_time(&convtime, &convtime_usec,
1263 lets_to_cpu(fe->accessTime))) {
1da177e4
LT
1264 inode->i_atime.tv_sec = convtime;
1265 inode->i_atime.tv_nsec = convtime_usec * 1000;
cb00ea35 1266 } else {
1da177e4
LT
1267 inode->i_atime = UDF_SB_RECORDTIME(inode->i_sb);
1268 }
1269
cb00ea35
CG
1270 if (udf_stamp_to_time(&convtime, &convtime_usec,
1271 lets_to_cpu(fe->modificationTime))) {
1da177e4
LT
1272 inode->i_mtime.tv_sec = convtime;
1273 inode->i_mtime.tv_nsec = convtime_usec * 1000;
cb00ea35 1274 } else {
1da177e4
LT
1275 inode->i_mtime = UDF_SB_RECORDTIME(inode->i_sb);
1276 }
1277
cb00ea35
CG
1278 if (udf_stamp_to_time(&convtime, &convtime_usec,
1279 lets_to_cpu(fe->attrTime))) {
1da177e4
LT
1280 inode->i_ctime.tv_sec = convtime;
1281 inode->i_ctime.tv_nsec = convtime_usec * 1000;
cb00ea35 1282 } else {
1da177e4
LT
1283 inode->i_ctime = UDF_SB_RECORDTIME(inode->i_sb);
1284 }
1285
1286 UDF_I_UNIQUE(inode) = le64_to_cpu(fe->uniqueID);
1287 UDF_I_LENEATTR(inode) = le32_to_cpu(fe->lengthExtendedAttr);
1288 UDF_I_LENALLOC(inode) = le32_to_cpu(fe->lengthAllocDescs);
1289 offset = sizeof(struct fileEntry) + UDF_I_LENEATTR(inode);
cb00ea35 1290 } else {
647bd61a 1291 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
cb00ea35 1292 (inode->i_sb->s_blocksize_bits - 9);
1da177e4 1293
cb00ea35
CG
1294 if (udf_stamp_to_time(&convtime, &convtime_usec,
1295 lets_to_cpu(efe->accessTime))) {
1da177e4
LT
1296 inode->i_atime.tv_sec = convtime;
1297 inode->i_atime.tv_nsec = convtime_usec * 1000;
cb00ea35 1298 } else {
1da177e4
LT
1299 inode->i_atime = UDF_SB_RECORDTIME(inode->i_sb);
1300 }
1301
cb00ea35
CG
1302 if (udf_stamp_to_time(&convtime, &convtime_usec,
1303 lets_to_cpu(efe->modificationTime))) {
1da177e4
LT
1304 inode->i_mtime.tv_sec = convtime;
1305 inode->i_mtime.tv_nsec = convtime_usec * 1000;
cb00ea35 1306 } else {
1da177e4
LT
1307 inode->i_mtime = UDF_SB_RECORDTIME(inode->i_sb);
1308 }
1309
cb00ea35
CG
1310 if (udf_stamp_to_time(&convtime, &convtime_usec,
1311 lets_to_cpu(efe->createTime))) {
1da177e4
LT
1312 UDF_I_CRTIME(inode).tv_sec = convtime;
1313 UDF_I_CRTIME(inode).tv_nsec = convtime_usec * 1000;
cb00ea35 1314 } else {
1da177e4
LT
1315 UDF_I_CRTIME(inode) = UDF_SB_RECORDTIME(inode->i_sb);
1316 }
1317
cb00ea35
CG
1318 if (udf_stamp_to_time(&convtime, &convtime_usec,
1319 lets_to_cpu(efe->attrTime))) {
1da177e4
LT
1320 inode->i_ctime.tv_sec = convtime;
1321 inode->i_ctime.tv_nsec = convtime_usec * 1000;
cb00ea35 1322 } else {
1da177e4
LT
1323 inode->i_ctime = UDF_SB_RECORDTIME(inode->i_sb);
1324 }
1325
1326 UDF_I_UNIQUE(inode) = le64_to_cpu(efe->uniqueID);
1327 UDF_I_LENEATTR(inode) = le32_to_cpu(efe->lengthExtendedAttr);
1328 UDF_I_LENALLOC(inode) = le32_to_cpu(efe->lengthAllocDescs);
cb00ea35
CG
1329 offset =
1330 sizeof(struct extendedFileEntry) + UDF_I_LENEATTR(inode);
1da177e4
LT
1331 }
1332
cb00ea35
CG
1333 switch (fe->icbTag.fileType) {
1334 case ICBTAG_FILE_TYPE_DIRECTORY:
1da177e4
LT
1335 {
1336 inode->i_op = &udf_dir_inode_operations;
1337 inode->i_fop = &udf_dir_operations;
1338 inode->i_mode |= S_IFDIR;
d8c76e6f 1339 inc_nlink(inode);
1da177e4
LT
1340 break;
1341 }
cb00ea35
CG
1342 case ICBTAG_FILE_TYPE_REALTIME:
1343 case ICBTAG_FILE_TYPE_REGULAR:
1344 case ICBTAG_FILE_TYPE_UNDEF:
1da177e4
LT
1345 {
1346 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
1347 inode->i_data.a_ops = &udf_adinicb_aops;
1348 else
1349 inode->i_data.a_ops = &udf_aops;
1350 inode->i_op = &udf_file_inode_operations;
1351 inode->i_fop = &udf_file_operations;
1352 inode->i_mode |= S_IFREG;
1353 break;
1354 }
cb00ea35 1355 case ICBTAG_FILE_TYPE_BLOCK:
1da177e4
LT
1356 {
1357 inode->i_mode |= S_IFBLK;
1358 break;
1359 }
cb00ea35 1360 case ICBTAG_FILE_TYPE_CHAR:
1da177e4
LT
1361 {
1362 inode->i_mode |= S_IFCHR;
1363 break;
1364 }
cb00ea35 1365 case ICBTAG_FILE_TYPE_FIFO:
1da177e4
LT
1366 {
1367 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1368 break;
1369 }
cb00ea35 1370 case ICBTAG_FILE_TYPE_SOCKET:
1da177e4
LT
1371 {
1372 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1373 break;
1374 }
cb00ea35 1375 case ICBTAG_FILE_TYPE_SYMLINK:
1da177e4
LT
1376 {
1377 inode->i_data.a_ops = &udf_symlink_aops;
1378 inode->i_op = &page_symlink_inode_operations;
cb00ea35 1379 inode->i_mode = S_IFLNK | S_IRWXUGO;
1da177e4
LT
1380 break;
1381 }
cb00ea35 1382 default:
1da177e4 1383 {
cb00ea35
CG
1384 printk(KERN_ERR
1385 "udf: udf_fill_inode(ino %ld) failed unknown file type=%d\n",
1386 inode->i_ino, fe->icbTag.fileType);
1da177e4
LT
1387 make_bad_inode(inode);
1388 return;
1389 }
1390 }
cb00ea35
CG
1391 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1392 struct deviceSpec *dsea = (struct deviceSpec *)
1393 udf_get_extendedattr(inode, 12, 1);
1da177e4 1394
cb00ea35
CG
1395 if (dsea) {
1396 init_special_inode(inode, inode->i_mode,
1397 MKDEV(le32_to_cpu
1398 (dsea->majorDeviceIdent),
1399 le32_to_cpu(dsea->
1400 minorDeviceIdent)));
1da177e4 1401 /* Developer ID ??? */
cb00ea35 1402 } else {
1da177e4
LT
1403 make_bad_inode(inode);
1404 }
1405 }
1406}
1407
647bd61a
CG
1408static int udf_alloc_i_data(struct inode *inode, size_t size)
1409{
1410 UDF_I_DATA(inode) = kmalloc(size, GFP_KERNEL);
1411
cb00ea35
CG
1412 if (!UDF_I_DATA(inode)) {
1413 printk(KERN_ERR
1414 "udf:udf_alloc_i_data (ino %ld) no free memory\n",
647bd61a
CG
1415 inode->i_ino);
1416 return -ENOMEM;
1417 }
1418
1419 return 0;
1420}
1421
cb00ea35 1422static mode_t udf_convert_permissions(struct fileEntry *fe)
1da177e4
LT
1423{
1424 mode_t mode;
1425 uint32_t permissions;
1426 uint32_t flags;
1427
1428 permissions = le32_to_cpu(fe->permissions);
1429 flags = le16_to_cpu(fe->icbTag.flags);
1430
cb00ea35
CG
1431 mode = ((permissions) & S_IRWXO) |
1432 ((permissions >> 2) & S_IRWXG) |
1433 ((permissions >> 4) & S_IRWXU) |
1434 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1435 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1436 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1da177e4
LT
1437
1438 return mode;
1439}
1440
1441/*
1442 * udf_write_inode
1443 *
1444 * PURPOSE
1445 * Write out the specified inode.
1446 *
1447 * DESCRIPTION
1448 * This routine is called whenever an inode is synced.
1449 * Currently this routine is just a placeholder.
1450 *
1451 * HISTORY
1452 * July 1, 1997 - Andrew E. Mileski
1453 * Written, tested, and released.
1454 */
1455
cb00ea35 1456int udf_write_inode(struct inode *inode, int sync)
1da177e4
LT
1457{
1458 int ret;
1459 lock_kernel();
1460 ret = udf_update_inode(inode, sync);
1461 unlock_kernel();
1462 return ret;
1463}
1464
cb00ea35 1465int udf_sync_inode(struct inode *inode)
1da177e4
LT
1466{
1467 return udf_update_inode(inode, 1);
1468}
1469
cb00ea35 1470static int udf_update_inode(struct inode *inode, int do_sync)
1da177e4
LT
1471{
1472 struct buffer_head *bh = NULL;
1473 struct fileEntry *fe;
1474 struct extendedFileEntry *efe;
1475 uint32_t udfperms;
1476 uint16_t icbflags;
1477 uint16_t crclen;
1478 int i;
1479 kernel_timestamp cpu_time;
1480 int err = 0;
1481
1482 bh = udf_tread(inode->i_sb,
cb00ea35
CG
1483 udf_get_lb_pblock(inode->i_sb, UDF_I_LOCATION(inode),
1484 0));
1da177e4 1485
cb00ea35 1486 if (!bh) {
1da177e4
LT
1487 udf_debug("bread failure\n");
1488 return -EIO;
1489 }
1490
1491 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
1492
1493 fe = (struct fileEntry *)bh->b_data;
1494 efe = (struct extendedFileEntry *)bh->b_data;
1495
cb00ea35 1496 if (le16_to_cpu(fe->descTag.tagIdent) == TAG_IDENT_USE) {
1da177e4 1497 struct unallocSpaceEntry *use =
cb00ea35 1498 (struct unallocSpaceEntry *)bh->b_data;
1da177e4
LT
1499
1500 use->lengthAllocDescs = cpu_to_le32(UDF_I_LENALLOC(inode));
cb00ea35
CG
1501 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1502 UDF_I_DATA(inode),
1503 inode->i_sb->s_blocksize -
1504 sizeof(struct unallocSpaceEntry));
1505 crclen =
1506 sizeof(struct unallocSpaceEntry) + UDF_I_LENALLOC(inode) -
1507 sizeof(tag);
1508 use->descTag.tagLocation =
1509 cpu_to_le32(UDF_I_LOCATION(inode).logicalBlockNum);
1da177e4 1510 use->descTag.descCRCLength = cpu_to_le16(crclen);
cb00ea35
CG
1511 use->descTag.descCRC =
1512 cpu_to_le16(udf_crc((char *)use + sizeof(tag), crclen, 0));
1da177e4
LT
1513
1514 use->descTag.tagChecksum = 0;
cb00ea35 1515 for (i = 0; i < 16; i++)
1da177e4 1516 if (i != 4)
cb00ea35
CG
1517 use->descTag.tagChecksum +=
1518 ((uint8_t *) & (use->descTag))[i];
1da177e4
LT
1519
1520 mark_buffer_dirty(bh);
3bf25cb4 1521 brelse(bh);
1da177e4
LT
1522 return err;
1523 }
1524
4d6660eb
PS
1525 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1526 fe->uid = cpu_to_le32(-1);
cb00ea35
CG
1527 else
1528 fe->uid = cpu_to_le32(inode->i_uid);
1da177e4 1529
4d6660eb
PS
1530 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1531 fe->gid = cpu_to_le32(-1);
cb00ea35
CG
1532 else
1533 fe->gid = cpu_to_le32(inode->i_gid);
1da177e4 1534
cb00ea35
CG
1535 udfperms = ((inode->i_mode & S_IRWXO)) |
1536 ((inode->i_mode & S_IRWXG) << 2) | ((inode->i_mode & S_IRWXU) << 4);
1da177e4 1537
cb00ea35
CG
1538 udfperms |= (le32_to_cpu(fe->permissions) &
1539 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1540 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1541 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1da177e4
LT
1542 fe->permissions = cpu_to_le32(udfperms);
1543
1544 if (S_ISDIR(inode->i_mode))
1545 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1546 else
1547 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1548
1549 fe->informationLength = cpu_to_le64(inode->i_size);
1550
cb00ea35 1551 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1da177e4 1552 regid *eid;
cb00ea35
CG
1553 struct deviceSpec *dsea = (struct deviceSpec *)
1554 udf_get_extendedattr(inode, 12, 1);
1da177e4 1555
cb00ea35 1556 if (!dsea) {
1da177e4 1557 dsea = (struct deviceSpec *)
cb00ea35
CG
1558 udf_add_extendedattr(inode,
1559 sizeof(struct deviceSpec) +
1560 sizeof(regid), 12, 0x3);
1da177e4
LT
1561 dsea->attrType = cpu_to_le32(12);
1562 dsea->attrSubtype = 1;
cb00ea35
CG
1563 dsea->attrLength =
1564 cpu_to_le32(sizeof(struct deviceSpec) +
1565 sizeof(regid));
1da177e4
LT
1566 dsea->impUseLength = cpu_to_le32(sizeof(regid));
1567 }
cb00ea35 1568 eid = (regid *) dsea->impUse;
1da177e4
LT
1569 memset(eid, 0, sizeof(regid));
1570 strcpy(eid->ident, UDF_ID_DEVELOPER);
1571 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1572 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1573 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1574 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1575 }
1576
cb00ea35
CG
1577 if (UDF_I_EFE(inode) == 0) {
1578 memcpy(bh->b_data + sizeof(struct fileEntry), UDF_I_DATA(inode),
1579 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1580 fe->logicalBlocksRecorded =
1581 cpu_to_le64((inode->i_blocks +
1582 (1 << (inode->i_sb->s_blocksize_bits - 9)) -
1583 1) >> (inode->i_sb->s_blocksize_bits - 9));
1da177e4
LT
1584
1585 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1586 fe->accessTime = cpu_to_lets(cpu_time);
1587 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1588 fe->modificationTime = cpu_to_lets(cpu_time);
1589 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1590 fe->attrTime = cpu_to_lets(cpu_time);
1591 memset(&(fe->impIdent), 0, sizeof(regid));
1592 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1593 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1594 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1595 fe->uniqueID = cpu_to_le64(UDF_I_UNIQUE(inode));
1596 fe->lengthExtendedAttr = cpu_to_le32(UDF_I_LENEATTR(inode));
1597 fe->lengthAllocDescs = cpu_to_le32(UDF_I_LENALLOC(inode));
1598 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1599 crclen = sizeof(struct fileEntry);
cb00ea35
CG
1600 } else {
1601 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1602 UDF_I_DATA(inode),
1603 inode->i_sb->s_blocksize -
1604 sizeof(struct extendedFileEntry));
1da177e4 1605 efe->objectSize = cpu_to_le64(inode->i_size);
cb00ea35
CG
1606 efe->logicalBlocksRecorded = cpu_to_le64((inode->i_blocks +
1607 (1 <<
1608 (inode->i_sb->
1609 s_blocksize_bits -
1610 9)) -
1611 1) >> (inode->i_sb->
1612 s_blocksize_bits
1613 - 9));
1da177e4
LT
1614
1615 if (UDF_I_CRTIME(inode).tv_sec > inode->i_atime.tv_sec ||
cb00ea35
CG
1616 (UDF_I_CRTIME(inode).tv_sec == inode->i_atime.tv_sec &&
1617 UDF_I_CRTIME(inode).tv_nsec > inode->i_atime.tv_nsec)) {
1da177e4
LT
1618 UDF_I_CRTIME(inode) = inode->i_atime;
1619 }
1620 if (UDF_I_CRTIME(inode).tv_sec > inode->i_mtime.tv_sec ||
cb00ea35
CG
1621 (UDF_I_CRTIME(inode).tv_sec == inode->i_mtime.tv_sec &&
1622 UDF_I_CRTIME(inode).tv_nsec > inode->i_mtime.tv_nsec)) {
1da177e4
LT
1623 UDF_I_CRTIME(inode) = inode->i_mtime;
1624 }
1625 if (UDF_I_CRTIME(inode).tv_sec > inode->i_ctime.tv_sec ||
cb00ea35
CG
1626 (UDF_I_CRTIME(inode).tv_sec == inode->i_ctime.tv_sec &&
1627 UDF_I_CRTIME(inode).tv_nsec > inode->i_ctime.tv_nsec)) {
1da177e4
LT
1628 UDF_I_CRTIME(inode) = inode->i_ctime;
1629 }
1630
1631 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1632 efe->accessTime = cpu_to_lets(cpu_time);
1633 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1634 efe->modificationTime = cpu_to_lets(cpu_time);
1635 if (udf_time_to_stamp(&cpu_time, UDF_I_CRTIME(inode)))
1636 efe->createTime = cpu_to_lets(cpu_time);
1637 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1638 efe->attrTime = cpu_to_lets(cpu_time);
1639
1640 memset(&(efe->impIdent), 0, sizeof(regid));
1641 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1642 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1643 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1644 efe->uniqueID = cpu_to_le64(UDF_I_UNIQUE(inode));
1645 efe->lengthExtendedAttr = cpu_to_le32(UDF_I_LENEATTR(inode));
1646 efe->lengthAllocDescs = cpu_to_le32(UDF_I_LENALLOC(inode));
1647 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1648 crclen = sizeof(struct extendedFileEntry);
1649 }
cb00ea35 1650 if (UDF_I_STRAT4096(inode)) {
1da177e4
LT
1651 fe->icbTag.strategyType = cpu_to_le16(4096);
1652 fe->icbTag.strategyParameter = cpu_to_le16(1);
1653 fe->icbTag.numEntries = cpu_to_le16(2);
cb00ea35 1654 } else {
1da177e4
LT
1655 fe->icbTag.strategyType = cpu_to_le16(4);
1656 fe->icbTag.numEntries = cpu_to_le16(1);
1657 }
1658
1659 if (S_ISDIR(inode->i_mode))
1660 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1661 else if (S_ISREG(inode->i_mode))
1662 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1663 else if (S_ISLNK(inode->i_mode))
1664 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1665 else if (S_ISBLK(inode->i_mode))
1666 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1667 else if (S_ISCHR(inode->i_mode))
1668 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1669 else if (S_ISFIFO(inode->i_mode))
1670 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1671 else if (S_ISSOCK(inode->i_mode))
1672 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1673
cb00ea35
CG
1674 icbflags = UDF_I_ALLOCTYPE(inode) |
1675 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1676 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1677 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1678 (le16_to_cpu(fe->icbTag.flags) &
1679 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1680 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1da177e4
LT
1681
1682 fe->icbTag.flags = cpu_to_le16(icbflags);
1683 if (UDF_SB_UDFREV(inode->i_sb) >= 0x0200)
1684 fe->descTag.descVersion = cpu_to_le16(3);
1685 else
1686 fe->descTag.descVersion = cpu_to_le16(2);
1687 fe->descTag.tagSerialNum = cpu_to_le16(UDF_SB_SERIALNUM(inode->i_sb));
cb00ea35
CG
1688 fe->descTag.tagLocation =
1689 cpu_to_le32(UDF_I_LOCATION(inode).logicalBlockNum);
1da177e4
LT
1690 crclen += UDF_I_LENEATTR(inode) + UDF_I_LENALLOC(inode) - sizeof(tag);
1691 fe->descTag.descCRCLength = cpu_to_le16(crclen);
cb00ea35
CG
1692 fe->descTag.descCRC =
1693 cpu_to_le16(udf_crc((char *)fe + sizeof(tag), crclen, 0));
1da177e4
LT
1694
1695 fe->descTag.tagChecksum = 0;
cb00ea35 1696 for (i = 0; i < 16; i++)
1da177e4 1697 if (i != 4)
cb00ea35
CG
1698 fe->descTag.tagChecksum +=
1699 ((uint8_t *) & (fe->descTag))[i];
1da177e4
LT
1700
1701 /* write the data blocks */
1702 mark_buffer_dirty(bh);
cb00ea35 1703 if (do_sync) {
1da177e4 1704 sync_dirty_buffer(bh);
cb00ea35 1705 if (buffer_req(bh) && !buffer_uptodate(bh)) {
1da177e4 1706 printk("IO error syncing udf inode [%s:%08lx]\n",
cb00ea35 1707 inode->i_sb->s_id, inode->i_ino);
1da177e4
LT
1708 err = -EIO;
1709 }
1710 }
3bf25cb4 1711 brelse(bh);
1da177e4
LT
1712 return err;
1713}
1714
cb00ea35 1715struct inode *udf_iget(struct super_block *sb, kernel_lb_addr ino)
1da177e4
LT
1716{
1717 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1718 struct inode *inode = iget_locked(sb, block);
1719
1720 if (!inode)
1721 return NULL;
1722
1723 if (inode->i_state & I_NEW) {
1724 memcpy(&UDF_I_LOCATION(inode), &ino, sizeof(kernel_lb_addr));
1725 __udf_read_inode(inode);
1726 unlock_new_inode(inode);
1727 }
1728
1729 if (is_bad_inode(inode))
1730 goto out_iput;
1731
cb00ea35
CG
1732 if (ino.logicalBlockNum >=
1733 UDF_SB_PARTLEN(sb, ino.partitionReferenceNum)) {
1da177e4 1734 udf_debug("block=%d, partition=%d out of range\n",
cb00ea35 1735 ino.logicalBlockNum, ino.partitionReferenceNum);
1da177e4
LT
1736 make_bad_inode(inode);
1737 goto out_iput;
1738 }
1739
1740 return inode;
1741
cb00ea35 1742 out_iput:
1da177e4
LT
1743 iput(inode);
1744 return NULL;
1745}
1746
cb00ea35
CG
1747int8_t udf_add_aext(struct inode * inode, struct extent_position * epos,
1748 kernel_lb_addr eloc, uint32_t elen, int inc)
1da177e4
LT
1749{
1750 int adsize;
1751 short_ad *sad = NULL;
1752 long_ad *lad = NULL;
1753 struct allocExtDesc *aed;
1754 int8_t etype;
1755 uint8_t *ptr;
1756
ff116fc8 1757 if (!epos->bh)
cb00ea35
CG
1758 ptr =
1759 UDF_I_DATA(inode) + epos->offset -
1760 udf_file_entry_alloc_offset(inode) + UDF_I_LENEATTR(inode);
1da177e4 1761 else
ff116fc8 1762 ptr = epos->bh->b_data + epos->offset;
1da177e4
LT
1763
1764 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
1765 adsize = sizeof(short_ad);
1766 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
1767 adsize = sizeof(long_ad);
1768 else
1769 return -1;
1770
cb00ea35 1771 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
1da177e4
LT
1772 char *sptr, *dptr;
1773 struct buffer_head *nbh;
1774 int err, loffset;
ff116fc8 1775 kernel_lb_addr obloc = epos->block;
1da177e4 1776
cb00ea35
CG
1777 if (!
1778 (epos->block.logicalBlockNum =
1779 udf_new_block(inode->i_sb, NULL,
1780 obloc.partitionReferenceNum,
1781 obloc.logicalBlockNum, &err))) {
1da177e4
LT
1782 return -1;
1783 }
cb00ea35
CG
1784 if (!
1785 (nbh =
1786 udf_tgetblk(inode->i_sb,
1787 udf_get_lb_pblock(inode->i_sb, epos->block,
1788 0)))) {
1da177e4
LT
1789 return -1;
1790 }
1791 lock_buffer(nbh);
1792 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1793 set_buffer_uptodate(nbh);
1794 unlock_buffer(nbh);
1795 mark_buffer_dirty_inode(nbh, inode);
1796
1797 aed = (struct allocExtDesc *)(nbh->b_data);
1798 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
cb00ea35
CG
1799 aed->previousAllocExtLocation =
1800 cpu_to_le32(obloc.logicalBlockNum);
1801 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
ff116fc8 1802 loffset = epos->offset;
1da177e4
LT
1803 aed->lengthAllocDescs = cpu_to_le32(adsize);
1804 sptr = ptr - adsize;
1805 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1806 memcpy(dptr, sptr, adsize);
ff116fc8 1807 epos->offset = sizeof(struct allocExtDesc) + adsize;
cb00ea35 1808 } else {
ff116fc8 1809 loffset = epos->offset + adsize;
1da177e4
LT
1810 aed->lengthAllocDescs = cpu_to_le32(0);
1811 sptr = ptr;
ff116fc8 1812 epos->offset = sizeof(struct allocExtDesc);
1da177e4 1813
cb00ea35 1814 if (epos->bh) {
ff116fc8 1815 aed = (struct allocExtDesc *)epos->bh->b_data;
1da177e4 1816 aed->lengthAllocDescs =
cb00ea35
CG
1817 cpu_to_le32(le32_to_cpu
1818 (aed->lengthAllocDescs) +
1819 adsize);
1820 } else {
1da177e4
LT
1821 UDF_I_LENALLOC(inode) += adsize;
1822 mark_inode_dirty(inode);
1823 }
1824 }
1825 if (UDF_SB_UDFREV(inode->i_sb) >= 0x0200)
1826 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
cb00ea35 1827 epos->block.logicalBlockNum, sizeof(tag));
1da177e4
LT
1828 else
1829 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
cb00ea35
CG
1830 epos->block.logicalBlockNum, sizeof(tag));
1831 switch (UDF_I_ALLOCTYPE(inode)) {
1832 case ICBTAG_FLAG_AD_SHORT:
1da177e4 1833 {
cb00ea35
CG
1834 sad = (short_ad *) sptr;
1835 sad->extLength =
1836 cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1837 inode->i_sb->s_blocksize);
1838 sad->extPosition =
1839 cpu_to_le32(epos->block.logicalBlockNum);
1da177e4
LT
1840 break;
1841 }
cb00ea35 1842 case ICBTAG_FLAG_AD_LONG:
1da177e4 1843 {
cb00ea35
CG
1844 lad = (long_ad *) sptr;
1845 lad->extLength =
1846 cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1847 inode->i_sb->s_blocksize);
ff116fc8 1848 lad->extLocation = cpu_to_lelb(epos->block);
1da177e4
LT
1849 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1850 break;
1851 }
1852 }
cb00ea35
CG
1853 if (epos->bh) {
1854 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT)
1855 || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
ff116fc8 1856 udf_update_tag(epos->bh->b_data, loffset);
1da177e4 1857 else
cb00ea35
CG
1858 udf_update_tag(epos->bh->b_data,
1859 sizeof(struct allocExtDesc));
ff116fc8 1860 mark_buffer_dirty_inode(epos->bh, inode);
3bf25cb4 1861 brelse(epos->bh);
cb00ea35 1862 } else
1da177e4 1863 mark_inode_dirty(inode);
ff116fc8 1864 epos->bh = nbh;
1da177e4
LT
1865 }
1866
ff116fc8 1867 etype = udf_write_aext(inode, epos, eloc, elen, inc);
1da177e4 1868
cb00ea35 1869 if (!epos->bh) {
1da177e4
LT
1870 UDF_I_LENALLOC(inode) += adsize;
1871 mark_inode_dirty(inode);
cb00ea35 1872 } else {
ff116fc8 1873 aed = (struct allocExtDesc *)epos->bh->b_data;
1da177e4 1874 aed->lengthAllocDescs =
cb00ea35
CG
1875 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
1876 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT)
1877 || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
1878 udf_update_tag(epos->bh->b_data,
1879 epos->offset + (inc ? 0 : adsize));
1da177e4 1880 else
cb00ea35
CG
1881 udf_update_tag(epos->bh->b_data,
1882 sizeof(struct allocExtDesc));
ff116fc8 1883 mark_buffer_dirty_inode(epos->bh, inode);
1da177e4
LT
1884 }
1885
1886 return etype;
1887}
1888
cb00ea35
CG
1889int8_t udf_write_aext(struct inode * inode, struct extent_position * epos,
1890 kernel_lb_addr eloc, uint32_t elen, int inc)
1da177e4
LT
1891{
1892 int adsize;
1893 uint8_t *ptr;
1894
ff116fc8 1895 if (!epos->bh)
cb00ea35
CG
1896 ptr =
1897 UDF_I_DATA(inode) + epos->offset -
1898 udf_file_entry_alloc_offset(inode) + UDF_I_LENEATTR(inode);
1da177e4 1899 else
ff116fc8 1900 ptr = epos->bh->b_data + epos->offset;
1da177e4 1901
cb00ea35
CG
1902 switch (UDF_I_ALLOCTYPE(inode)) {
1903 case ICBTAG_FLAG_AD_SHORT:
1da177e4 1904 {
cb00ea35 1905 short_ad *sad = (short_ad *) ptr;
1da177e4
LT
1906 sad->extLength = cpu_to_le32(elen);
1907 sad->extPosition = cpu_to_le32(eloc.logicalBlockNum);
1908 adsize = sizeof(short_ad);
1909 break;
1910 }
cb00ea35 1911 case ICBTAG_FLAG_AD_LONG:
1da177e4 1912 {
cb00ea35 1913 long_ad *lad = (long_ad *) ptr;
1da177e4
LT
1914 lad->extLength = cpu_to_le32(elen);
1915 lad->extLocation = cpu_to_lelb(eloc);
1916 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1917 adsize = sizeof(long_ad);
1918 break;
1919 }
cb00ea35
CG
1920 default:
1921 return -1;
1da177e4
LT
1922 }
1923
cb00ea35
CG
1924 if (epos->bh) {
1925 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT)
1926 || UDF_SB_UDFREV(inode->i_sb) >= 0x0201) {
1927 struct allocExtDesc *aed =
1928 (struct allocExtDesc *)epos->bh->b_data;
ff116fc8 1929 udf_update_tag(epos->bh->b_data,
cb00ea35
CG
1930 le32_to_cpu(aed->lengthAllocDescs) +
1931 sizeof(struct allocExtDesc));
1da177e4 1932 }
ff116fc8 1933 mark_buffer_dirty_inode(epos->bh, inode);
cb00ea35 1934 } else
1da177e4
LT
1935 mark_inode_dirty(inode);
1936
1937 if (inc)
ff116fc8 1938 epos->offset += adsize;
1da177e4
LT
1939 return (elen >> 30);
1940}
1941
cb00ea35
CG
1942int8_t udf_next_aext(struct inode * inode, struct extent_position * epos,
1943 kernel_lb_addr * eloc, uint32_t * elen, int inc)
1da177e4
LT
1944{
1945 int8_t etype;
1946
ff116fc8 1947 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
cb00ea35 1948 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
ff116fc8
JK
1949 epos->block = *eloc;
1950 epos->offset = sizeof(struct allocExtDesc);
3bf25cb4 1951 brelse(epos->bh);
cb00ea35
CG
1952 if (!
1953 (epos->bh =
1954 udf_tread(inode->i_sb,
1955 udf_get_lb_pblock(inode->i_sb, epos->block,
1956 0)))) {
1da177e4 1957 udf_debug("reading block %d failed!\n",
cb00ea35
CG
1958 udf_get_lb_pblock(inode->i_sb, epos->block,
1959 0));
1da177e4
LT
1960 return -1;
1961 }
1962 }
1963
1964 return etype;
1965}
1966
cb00ea35
CG
1967int8_t udf_current_aext(struct inode * inode, struct extent_position * epos,
1968 kernel_lb_addr * eloc, uint32_t * elen, int inc)
1da177e4
LT
1969{
1970 int alen;
1971 int8_t etype;
1972 uint8_t *ptr;
1973
cb00ea35 1974 if (!epos->bh) {
ff116fc8
JK
1975 if (!epos->offset)
1976 epos->offset = udf_file_entry_alloc_offset(inode);
cb00ea35
CG
1977 ptr =
1978 UDF_I_DATA(inode) + epos->offset -
1979 udf_file_entry_alloc_offset(inode) + UDF_I_LENEATTR(inode);
1980 alen =
1981 udf_file_entry_alloc_offset(inode) + UDF_I_LENALLOC(inode);
1982 } else {
ff116fc8
JK
1983 if (!epos->offset)
1984 epos->offset = sizeof(struct allocExtDesc);
1985 ptr = epos->bh->b_data + epos->offset;
cb00ea35
CG
1986 alen =
1987 sizeof(struct allocExtDesc) +
1988 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
1989 lengthAllocDescs);
1da177e4
LT
1990 }
1991
cb00ea35
CG
1992 switch (UDF_I_ALLOCTYPE(inode)) {
1993 case ICBTAG_FLAG_AD_SHORT:
1da177e4
LT
1994 {
1995 short_ad *sad;
1996
cb00ea35
CG
1997 if (!
1998 (sad =
1999 udf_get_fileshortad(ptr, alen, &epos->offset,
2000 inc)))
1da177e4
LT
2001 return -1;
2002
2003 etype = le32_to_cpu(sad->extLength) >> 30;
2004 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
cb00ea35
CG
2005 eloc->partitionReferenceNum =
2006 UDF_I_LOCATION(inode).partitionReferenceNum;
2007 *elen =
2008 le32_to_cpu(sad->
2009 extLength) & UDF_EXTENT_LENGTH_MASK;
1da177e4
LT
2010 break;
2011 }
cb00ea35 2012 case ICBTAG_FLAG_AD_LONG:
1da177e4
LT
2013 {
2014 long_ad *lad;
2015
cb00ea35
CG
2016 if (!
2017 (lad =
2018 udf_get_filelongad(ptr, alen, &epos->offset, inc)))
1da177e4
LT
2019 return -1;
2020
2021 etype = le32_to_cpu(lad->extLength) >> 30;
2022 *eloc = lelb_to_cpu(lad->extLocation);
cb00ea35
CG
2023 *elen =
2024 le32_to_cpu(lad->
2025 extLength) & UDF_EXTENT_LENGTH_MASK;
1da177e4
LT
2026 break;
2027 }
cb00ea35 2028 default:
1da177e4 2029 {
cb00ea35
CG
2030 udf_debug("alloc_type = %d unsupported\n",
2031 UDF_I_ALLOCTYPE(inode));
1da177e4
LT
2032 return -1;
2033 }
2034 }
2035
2036 return etype;
2037}
2038
2039static int8_t
ff116fc8
JK
2040udf_insert_aext(struct inode *inode, struct extent_position epos,
2041 kernel_lb_addr neloc, uint32_t nelen)
1da177e4
LT
2042{
2043 kernel_lb_addr oeloc;
2044 uint32_t oelen;
2045 int8_t etype;
2046
ff116fc8 2047 if (epos.bh)
3bf25cb4 2048 get_bh(epos.bh);
1da177e4 2049
cb00ea35 2050 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
ff116fc8 2051 udf_write_aext(inode, &epos, neloc, nelen, 1);
1da177e4
LT
2052
2053 neloc = oeloc;
2054 nelen = (etype << 30) | oelen;
2055 }
ff116fc8 2056 udf_add_aext(inode, &epos, neloc, nelen, 1);
3bf25cb4 2057 brelse(epos.bh);
1da177e4
LT
2058 return (nelen >> 30);
2059}
2060
cb00ea35
CG
2061int8_t udf_delete_aext(struct inode * inode, struct extent_position epos,
2062 kernel_lb_addr eloc, uint32_t elen)
1da177e4 2063{
ff116fc8
JK
2064 struct extent_position oepos;
2065 int adsize;
1da177e4
LT
2066 int8_t etype;
2067 struct allocExtDesc *aed;
2068
cb00ea35 2069 if (epos.bh) {
3bf25cb4
JK
2070 get_bh(epos.bh);
2071 get_bh(epos.bh);
1da177e4
LT
2072 }
2073
2074 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
2075 adsize = sizeof(short_ad);
2076 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
2077 adsize = sizeof(long_ad);
2078 else
2079 adsize = 0;
2080
ff116fc8
JK
2081 oepos = epos;
2082 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
1da177e4
LT
2083 return -1;
2084
cb00ea35 2085 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
ff116fc8 2086 udf_write_aext(inode, &oepos, eloc, (etype << 30) | elen, 1);
cb00ea35 2087 if (oepos.bh != epos.bh) {
ff116fc8 2088 oepos.block = epos.block;
3bf25cb4
JK
2089 brelse(oepos.bh);
2090 get_bh(epos.bh);
ff116fc8
JK
2091 oepos.bh = epos.bh;
2092 oepos.offset = epos.offset - adsize;
1da177e4
LT
2093 }
2094 }
2095 memset(&eloc, 0x00, sizeof(kernel_lb_addr));
2096 elen = 0;
2097
cb00ea35 2098 if (epos.bh != oepos.bh) {
ff116fc8
JK
2099 udf_free_blocks(inode->i_sb, inode, epos.block, 0, 1);
2100 udf_write_aext(inode, &oepos, eloc, elen, 1);
2101 udf_write_aext(inode, &oepos, eloc, elen, 1);
cb00ea35 2102 if (!oepos.bh) {
1da177e4
LT
2103 UDF_I_LENALLOC(inode) -= (adsize * 2);
2104 mark_inode_dirty(inode);
cb00ea35 2105 } else {
ff116fc8 2106 aed = (struct allocExtDesc *)oepos.bh->b_data;
1da177e4 2107 aed->lengthAllocDescs =
cb00ea35
CG
2108 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) -
2109 (2 * adsize));
2110 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT)
2111 || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
2112 udf_update_tag(oepos.bh->b_data,
2113 oepos.offset - (2 * adsize));
1da177e4 2114 else
cb00ea35
CG
2115 udf_update_tag(oepos.bh->b_data,
2116 sizeof(struct allocExtDesc));
ff116fc8 2117 mark_buffer_dirty_inode(oepos.bh, inode);
1da177e4 2118 }
cb00ea35 2119 } else {
ff116fc8 2120 udf_write_aext(inode, &oepos, eloc, elen, 1);
cb00ea35 2121 if (!oepos.bh) {
1da177e4
LT
2122 UDF_I_LENALLOC(inode) -= adsize;
2123 mark_inode_dirty(inode);
cb00ea35 2124 } else {
ff116fc8 2125 aed = (struct allocExtDesc *)oepos.bh->b_data;
1da177e4 2126 aed->lengthAllocDescs =
cb00ea35
CG
2127 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) -
2128 adsize);
2129 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT)
2130 || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
2131 udf_update_tag(oepos.bh->b_data,
2132 epos.offset - adsize);
1da177e4 2133 else
cb00ea35
CG
2134 udf_update_tag(oepos.bh->b_data,
2135 sizeof(struct allocExtDesc));
ff116fc8 2136 mark_buffer_dirty_inode(oepos.bh, inode);
1da177e4
LT
2137 }
2138 }
647bd61a 2139
3bf25cb4
JK
2140 brelse(epos.bh);
2141 brelse(oepos.bh);
1da177e4
LT
2142 return (elen >> 30);
2143}
2144
cb00ea35
CG
2145int8_t inode_bmap(struct inode * inode, sector_t block,
2146 struct extent_position * pos, kernel_lb_addr * eloc,
2147 uint32_t * elen, sector_t * offset)
1da177e4 2148{
cb00ea35
CG
2149 loff_t lbcount = 0, bcount =
2150 (loff_t) block << inode->i_sb->s_blocksize_bits;
1da177e4
LT
2151 int8_t etype;
2152
cb00ea35 2153 if (block < 0) {
1da177e4
LT
2154 printk(KERN_ERR "udf: inode_bmap: block < 0\n");
2155 return -1;
2156 }
1da177e4 2157
ff116fc8
JK
2158 pos->offset = 0;
2159 pos->block = UDF_I_LOCATION(inode);
2160 pos->bh = NULL;
1da177e4 2161 *elen = 0;
1da177e4 2162
cb00ea35
CG
2163 do {
2164 if ((etype = udf_next_aext(inode, pos, eloc, elen, 1)) == -1) {
2165 *offset =
2166 (bcount - lbcount) >> inode->i_sb->s_blocksize_bits;
1da177e4
LT
2167 UDF_I_LENEXTENTS(inode) = lbcount;
2168 return -1;
2169 }
2170 lbcount += *elen;
2171 } while (lbcount <= bcount);
2172
60448b1d 2173 *offset = (bcount + *elen - lbcount) >> inode->i_sb->s_blocksize_bits;
1da177e4
LT
2174
2175 return etype;
2176}
2177
60448b1d 2178long udf_block_map(struct inode *inode, sector_t block)
1da177e4 2179{
ff116fc8
JK
2180 kernel_lb_addr eloc;
2181 uint32_t elen;
60448b1d 2182 sector_t offset;
cb00ea35 2183 struct extent_position epos = { NULL, 0, {0, 0} };
1da177e4
LT
2184 int ret;
2185
2186 lock_kernel();
2187
cb00ea35
CG
2188 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2189 (EXT_RECORDED_ALLOCATED >> 30))
60448b1d 2190 ret = udf_get_lb_pblock(inode->i_sb, eloc, offset);
1da177e4
LT
2191 else
2192 ret = 0;
2193
2194 unlock_kernel();
3bf25cb4 2195 brelse(epos.bh);
1da177e4
LT
2196
2197 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2198 return udf_fixed_to_variable(ret);
2199 else
2200 return ret;
2201}