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