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