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