udf: Push i_data_sem locking into udf_expand_file_adinicb()
[linux-block.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>
1da177e4
LT
36#include <linux/writeback.h>
37#include <linux/slab.h>
f845fced 38#include <linux/crc-itu-t.h>
bc112323 39#include <linux/mpage.h>
e2e40f2c 40#include <linux/uio.h>
2f8b5444 41#include <linux/bio.h>
1da177e4
LT
42
43#include "udf_i.h"
44#include "udf_sb.h"
45
1da177e4
LT
46#define EXTENT_MERGE_SIZE 5
47
c3367a1b
SM
48#define FE_MAPPED_PERMS (FE_PERM_U_READ | FE_PERM_U_WRITE | FE_PERM_U_EXEC | \
49 FE_PERM_G_READ | FE_PERM_G_WRITE | FE_PERM_G_EXEC | \
50 FE_PERM_O_READ | FE_PERM_O_WRITE | FE_PERM_O_EXEC)
51
52#define FE_DELETE_PERMS (FE_PERM_U_DELETE | FE_PERM_G_DELETE | \
53 FE_PERM_O_DELETE)
54
f3a30be7
JK
55struct udf_map_rq;
56
faa17292 57static umode_t udf_convert_permissions(struct fileEntry *);
1da177e4 58static int udf_update_inode(struct inode *, int);
49521de1 59static int udf_sync_inode(struct inode *inode);
647bd61a 60static int udf_alloc_i_data(struct inode *inode, size_t size);
b3c03fce 61static int inode_getblk(struct inode *inode, struct udf_map_rq *map);
19fd80de
JK
62static int udf_insert_aext(struct inode *, struct extent_position,
63 struct kernel_lb_addr, uint32_t);
b490bdd6 64static void udf_split_extents(struct inode *, int *, int, udf_pblk_t,
3cc6f844 65 struct kernel_long_ad *, int *);
1da177e4 66static void udf_prealloc_extents(struct inode *, int, int,
3cc6f844
FF
67 struct kernel_long_ad *, int *);
68static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *);
19fd80de
JK
69static int udf_update_extents(struct inode *, struct kernel_long_ad *, int,
70 int, struct extent_position *);
1da177e4
LT
71static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
72
99600051
NJ
73static void __udf_clear_extent_cache(struct inode *inode)
74{
75 struct udf_inode_info *iinfo = UDF_I(inode);
76
77 if (iinfo->cached_extent.lstart != -1) {
78 brelse(iinfo->cached_extent.epos.bh);
79 iinfo->cached_extent.lstart = -1;
80 }
81}
82
83/* Invalidate extent cache */
84static void udf_clear_extent_cache(struct inode *inode)
85{
86 struct udf_inode_info *iinfo = UDF_I(inode);
87
88 spin_lock(&iinfo->i_extent_cache_lock);
89 __udf_clear_extent_cache(inode);
90 spin_unlock(&iinfo->i_extent_cache_lock);
91}
92
93/* Return contents of extent cache */
94static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
95 loff_t *lbcount, struct extent_position *pos)
96{
97 struct udf_inode_info *iinfo = UDF_I(inode);
98 int ret = 0;
99
100 spin_lock(&iinfo->i_extent_cache_lock);
101 if ((iinfo->cached_extent.lstart <= bcount) &&
102 (iinfo->cached_extent.lstart != -1)) {
103 /* Cache hit */
104 *lbcount = iinfo->cached_extent.lstart;
105 memcpy(pos, &iinfo->cached_extent.epos,
106 sizeof(struct extent_position));
107 if (pos->bh)
108 get_bh(pos->bh);
109 ret = 1;
110 }
111 spin_unlock(&iinfo->i_extent_cache_lock);
112 return ret;
113}
114
115/* Add extent to extent cache */
116static void udf_update_extent_cache(struct inode *inode, loff_t estart,
b31c9ed9 117 struct extent_position *pos)
99600051
NJ
118{
119 struct udf_inode_info *iinfo = UDF_I(inode);
120
121 spin_lock(&iinfo->i_extent_cache_lock);
122 /* Invalidate previously cached extent */
123 __udf_clear_extent_cache(inode);
124 if (pos->bh)
125 get_bh(pos->bh);
033c9da0 126 memcpy(&iinfo->cached_extent.epos, pos, sizeof(*pos));
99600051 127 iinfo->cached_extent.lstart = estart;
b31c9ed9
FF
128 switch (iinfo->i_alloc_type) {
129 case ICBTAG_FLAG_AD_SHORT:
130 iinfo->cached_extent.epos.offset -= sizeof(struct short_ad);
131 break;
132 case ICBTAG_FLAG_AD_LONG:
133 iinfo->cached_extent.epos.offset -= sizeof(struct long_ad);
134 break;
135 }
99600051
NJ
136 spin_unlock(&iinfo->i_extent_cache_lock);
137}
b1e32126 138
3aac2b62 139void udf_evict_inode(struct inode *inode)
1da177e4 140{
2c948b3f 141 struct udf_inode_info *iinfo = UDF_I(inode);
3aac2b62
AV
142 int want_delete = 0;
143
044e2e26
JK
144 if (!is_bad_inode(inode)) {
145 if (!inode->i_nlink) {
146 want_delete = 1;
147 udf_setsize(inode, 0);
148 udf_update_inode(inode, IS_SYNC(inode));
149 }
150 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
151 inode->i_size != iinfo->i_lenExtents) {
152 udf_warn(inode->i_sb,
153 "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
154 inode->i_ino, inode->i_mode,
155 (unsigned long long)inode->i_size,
156 (unsigned long long)iinfo->i_lenExtents);
157 }
91b0abe3
JW
158 }
159 truncate_inode_pages_final(&inode->i_data);
3aac2b62 160 invalidate_inode_buffers(inode);
dbd5768f 161 clear_inode(inode);
382a2287
JK
162 kfree(iinfo->i_data);
163 iinfo->i_data = NULL;
99600051 164 udf_clear_extent_cache(inode);
3aac2b62 165 if (want_delete) {
3aac2b62 166 udf_free_inode(inode);
3aac2b62 167 }
1da177e4
LT
168}
169
5eec54fc
IA
170static void udf_write_failed(struct address_space *mapping, loff_t to)
171{
172 struct inode *inode = mapping->host;
173 struct udf_inode_info *iinfo = UDF_I(inode);
174 loff_t isize = inode->i_size;
175
176 if (to > isize) {
7caef267 177 truncate_pagecache(inode, isize);
5eec54fc
IA
178 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
179 down_write(&iinfo->i_data_sem);
99600051 180 udf_clear_extent_cache(inode);
5eec54fc
IA
181 udf_truncate_extents(inode);
182 up_write(&iinfo->i_data_sem);
183 }
184 }
185}
186
378b8e1a
NJ
187static int udf_writepages(struct address_space *mapping,
188 struct writeback_control *wbc)
189{
190 return mpage_writepages(mapping, wbc, udf_get_block);
191}
192
f132ab7d 193static int udf_read_folio(struct file *file, struct folio *folio)
1da177e4 194{
f132ab7d 195 return mpage_read_folio(folio, udf_get_block);
bc112323
NJ
196}
197
d4388340 198static void udf_readahead(struct readahead_control *rac)
bc112323 199{
d4388340 200 mpage_readahead(rac, udf_get_block);
1da177e4
LT
201}
202
be021ee4 203static int udf_write_begin(struct file *file, struct address_space *mapping,
9d6b0cd7 204 loff_t pos, unsigned len,
be021ee4 205 struct page **pagep, void **fsdata)
1da177e4 206{
155130a4
CH
207 int ret;
208
b3992d1e 209 ret = block_write_begin(mapping, pos, len, pagep, udf_get_block);
5eec54fc
IA
210 if (unlikely(ret))
211 udf_write_failed(mapping, pos + len);
212 return ret;
213}
155130a4 214
c8b8e32d 215static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
5eec54fc
IA
216{
217 struct file *file = iocb->ki_filp;
218 struct address_space *mapping = file->f_mapping;
219 struct inode *inode = mapping->host;
a6cbcd4a 220 size_t count = iov_iter_count(iter);
5eec54fc
IA
221 ssize_t ret;
222
c8b8e32d 223 ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
6f673763 224 if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
c8b8e32d 225 udf_write_failed(mapping, iocb->ki_pos + count);
155130a4 226 return ret;
1da177e4
LT
227}
228
229static sector_t udf_bmap(struct address_space *mapping, sector_t block)
230{
cb00ea35 231 return generic_block_bmap(mapping, block, udf_get_block);
1da177e4
LT
232}
233
f5e54d6e 234const struct address_space_operations udf_aops = {
e621900a 235 .dirty_folio = block_dirty_folio,
7ba13abb 236 .invalidate_folio = block_invalidate_folio,
f132ab7d 237 .read_folio = udf_read_folio,
d4388340 238 .readahead = udf_readahead,
378b8e1a 239 .writepages = udf_writepages,
5eec54fc
IA
240 .write_begin = udf_write_begin,
241 .write_end = generic_write_end,
242 .direct_IO = udf_direct_IO,
28de7948 243 .bmap = udf_bmap,
36273e5b 244 .migrate_folio = buffer_migrate_folio,
1da177e4
LT
245};
246
d2eb8c35
JK
247/*
248 * Expand file stored in ICB to a normal one-block-file
249 *
d2eb8c35
JK
250 * This function requires i_mutex held
251 */
7e49b6f2 252int udf_expand_file_adinicb(struct inode *inode)
1da177e4
LT
253{
254 struct page *page;
255 char *kaddr;
48d6d8ff 256 struct udf_inode_info *iinfo = UDF_I(inode);
7e49b6f2 257 int err;
1da177e4 258
5955102c 259 WARN_ON_ONCE(!inode_is_locked(inode));
48d6d8ff 260 if (!iinfo->i_lenAlloc) {
6a3b37e0 261 down_write(&iinfo->i_data_sem);
1da177e4 262 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
48d6d8ff 263 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
1da177e4 264 else
48d6d8ff 265 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
7e49b6f2
JK
266 /* from now on we have normal address_space methods */
267 inode->i_data.a_ops = &udf_aops;
d2eb8c35 268 up_write(&iinfo->i_data_sem);
1da177e4 269 mark_inode_dirty(inode);
7e49b6f2 270 return 0;
1da177e4
LT
271 }
272
7e49b6f2
JK
273 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
274 if (!page)
275 return -ENOMEM;
cd7619d6 276
cb00ea35 277 if (!PageUptodate(page)) {
5c26eac4 278 kaddr = kmap_atomic(page);
48d6d8ff 279 memset(kaddr + iinfo->i_lenAlloc, 0x00,
09cbfeaf 280 PAGE_SIZE - iinfo->i_lenAlloc);
382a2287 281 memcpy(kaddr, iinfo->i_data + iinfo->i_lenEAttr,
48d6d8ff 282 iinfo->i_lenAlloc);
1da177e4
LT
283 flush_dcache_page(page);
284 SetPageUptodate(page);
5c26eac4 285 kunmap_atomic(kaddr);
1da177e4 286 }
d2eb8c35 287 down_write(&iinfo->i_data_sem);
382a2287 288 memset(iinfo->i_data + iinfo->i_lenEAttr, 0x00,
48d6d8ff
MS
289 iinfo->i_lenAlloc);
290 iinfo->i_lenAlloc = 0;
1da177e4 291 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
48d6d8ff 292 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
1da177e4 293 else
48d6d8ff 294 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
7e49b6f2
JK
295 /* from now on we have normal address_space methods */
296 inode->i_data.a_ops = &udf_aops;
7fc3b7c2
JK
297 set_page_dirty(page);
298 unlock_page(page);
d2eb8c35 299 up_write(&iinfo->i_data_sem);
7fc3b7c2 300 err = filemap_fdatawrite(inode->i_mapping);
7e49b6f2
JK
301 if (err) {
302 /* Restore everything back so that we don't lose data... */
303 lock_page(page);
d2eb8c35 304 down_write(&iinfo->i_data_sem);
5c26eac4 305 kaddr = kmap_atomic(page);
382a2287 306 memcpy(iinfo->i_data + iinfo->i_lenEAttr, kaddr, inode->i_size);
5c26eac4 307 kunmap_atomic(kaddr);
7e49b6f2
JK
308 unlock_page(page);
309 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
310 inode->i_data.a_ops = &udf_adinicb_aops;
ea856919 311 iinfo->i_lenAlloc = inode->i_size;
d2eb8c35 312 up_write(&iinfo->i_data_sem);
7e49b6f2 313 }
09cbfeaf 314 put_page(page);
1da177e4 315 mark_inode_dirty(inode);
7e49b6f2
JK
316
317 return err;
1da177e4
LT
318}
319
80942521
JK
320#define UDF_MAP_CREATE 0x01 /* Mapping can allocate new blocks */
321#define UDF_MAP_NOPREALLOC 0x02 /* Do not preallocate blocks */
f3a30be7
JK
322
323#define UDF_BLK_MAPPED 0x01 /* Block was successfully mapped */
324#define UDF_BLK_NEW 0x02 /* Block was freshly allocated */
325
326struct udf_map_rq {
327 sector_t lblk;
328 udf_pblk_t pblk;
329 int iflags; /* UDF_MAP_ flags determining behavior */
330 int oflags; /* UDF_BLK_ flags reporting results */
331};
332
333static int udf_map_block(struct inode *inode, struct udf_map_rq *map)
1da177e4 334{
b3c03fce 335 int err;
f3a30be7 336 struct udf_inode_info *iinfo = UDF_I(inode);
1da177e4 337
f3a30be7
JK
338 map->oflags = 0;
339 if (!(map->iflags & UDF_MAP_CREATE)) {
364a6665
JK
340 struct kernel_lb_addr eloc;
341 uint32_t elen;
342 sector_t offset;
343 struct extent_position epos = {};
344
345 down_read(&iinfo->i_data_sem);
346 if (inode_bmap(inode, map->lblk, &epos, &eloc, &elen, &offset)
347 == (EXT_RECORDED_ALLOCATED >> 30)) {
348 map->pblk = udf_get_lb_pblock(inode->i_sb, &eloc,
349 offset);
f3a30be7 350 map->oflags |= UDF_BLK_MAPPED;
364a6665
JK
351 }
352 up_read(&iinfo->i_data_sem);
353 brelse(epos.bh);
354
1da177e4
LT
355 return 0;
356 }
357
4d0fb621 358 down_write(&iinfo->i_data_sem);
16d05565
JK
359 /*
360 * Block beyond EOF and prealloc extents? Just discard preallocation
361 * as it is not useful and complicates things.
362 */
f3a30be7 363 if (((loff_t)map->lblk) << inode->i_blkbits > iinfo->i_lenExtents)
16d05565 364 udf_discard_prealloc(inode);
99600051 365 udf_clear_extent_cache(inode);
b3c03fce 366 err = inode_getblk(inode, map);
f3a30be7 367 up_write(&iinfo->i_data_sem);
b3c03fce 368 return err;
f3a30be7 369}
28de7948 370
f3a30be7
JK
371static int udf_get_block(struct inode *inode, sector_t block,
372 struct buffer_head *bh_result, int create)
373{
374 int err;
375 struct udf_map_rq map = {
376 .lblk = block,
377 .iflags = create ? UDF_MAP_CREATE : 0,
378 };
379
80942521
JK
380 /*
381 * We preallocate blocks only for regular files. It also makes sense
382 * for directories but there's a problem when to drop the
383 * preallocation. We might use some delayed work for that but I feel
384 * it's overengineering for a filesystem like UDF.
385 */
386 if (!S_ISREG(inode->i_mode))
387 map.iflags |= UDF_MAP_NOPREALLOC;
f3a30be7
JK
388 err = udf_map_block(inode, &map);
389 if (err < 0)
390 return err;
391 if (map.oflags & UDF_BLK_MAPPED) {
392 map_bh(bh_result, inode->i_sb, map.pblk);
393 if (map.oflags & UDF_BLK_NEW)
394 set_buffer_new(bh_result);
395 }
396 return 0;
1da177e4
LT
397}
398
fa33cdbf
SM
399/* Extend the file with new blocks totaling 'new_block_bytes',
400 * return the number of extents added
401 */
7e49b6f2
JK
402static int udf_do_extend_file(struct inode *inode,
403 struct extent_position *last_pos,
404 struct kernel_long_ad *last_ext,
fa33cdbf 405 loff_t new_block_bytes)
31170b6a 406{
fa33cdbf 407 uint32_t add;
31170b6a
JK
408 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
409 struct super_block *sb = inode->i_sb;
48d6d8ff 410 struct udf_inode_info *iinfo;
7e49b6f2 411 int err;
31170b6a
JK
412
413 /* The previous extent is fake and we should not extend by anything
414 * - there's nothing to do... */
fa33cdbf 415 if (!new_block_bytes && fake)
31170b6a 416 return 0;
28de7948 417
48d6d8ff 418 iinfo = UDF_I(inode);
31170b6a
JK
419 /* Round the last extent up to a multiple of block size */
420 if (last_ext->extLength & (sb->s_blocksize - 1)) {
421 last_ext->extLength =
28de7948
CG
422 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
423 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
424 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
48d6d8ff
MS
425 iinfo->i_lenExtents =
426 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
28de7948 427 ~(sb->s_blocksize - 1);
31170b6a 428 }
28de7948 429
e57191a8 430 add = 0;
31170b6a 431 /* Can we merge with the previous extent? */
4b11111a
MS
432 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
433 EXT_NOT_RECORDED_NOT_ALLOCATED) {
fa33cdbf
SM
434 add = (1 << 30) - sb->s_blocksize -
435 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
436 if (add > new_block_bytes)
437 add = new_block_bytes;
438 new_block_bytes -= add;
439 last_ext->extLength += add;
31170b6a
JK
440 }
441
442 if (fake) {
70bfb3a8
JK
443 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
444 last_ext->extLength, 1);
445 if (err < 0)
446 goto out_err;
31170b6a 447 count++;
6c371578
JK
448 } else {
449 struct kernel_lb_addr tmploc;
450 uint32_t tmplen;
451
97e961fd 452 udf_write_aext(inode, last_pos, &last_ext->extLocation,
4b11111a 453 last_ext->extLength, 1);
63c9e47a 454
6c371578 455 /*
63c9e47a
SM
456 * We've rewritten the last extent. If we are going to add
457 * more extents, we may need to enter possible following
458 * empty indirect extent.
6c371578 459 */
16d05565 460 if (new_block_bytes)
63c9e47a 461 udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
6c371578 462 }
e57191a8 463 iinfo->i_lenExtents += add;
28de7948 464
31170b6a 465 /* Managed to do everything necessary? */
fa33cdbf 466 if (!new_block_bytes)
31170b6a
JK
467 goto out;
468
469 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
470 last_ext->extLocation.logicalBlockNum = 0;
4b11111a 471 last_ext->extLocation.partitionReferenceNum = 0;
fa33cdbf
SM
472 add = (1 << 30) - sb->s_blocksize;
473 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add;
28de7948 474
31170b6a 475 /* Create enough extents to cover the whole hole */
fa33cdbf
SM
476 while (new_block_bytes > add) {
477 new_block_bytes -= add;
7e49b6f2
JK
478 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
479 last_ext->extLength, 1);
480 if (err)
70bfb3a8 481 goto out_err;
e57191a8 482 iinfo->i_lenExtents += add;
31170b6a
JK
483 count++;
484 }
fa33cdbf 485 if (new_block_bytes) {
31170b6a 486 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
fa33cdbf 487 new_block_bytes;
7e49b6f2
JK
488 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
489 last_ext->extLength, 1);
490 if (err)
70bfb3a8 491 goto out_err;
e57191a8 492 iinfo->i_lenExtents += new_block_bytes;
31170b6a
JK
493 count++;
494 }
28de7948
CG
495
496out:
31170b6a 497 /* last_pos should point to the last written extent... */
48d6d8ff 498 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 499 last_pos->offset -= sizeof(struct short_ad);
48d6d8ff 500 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 501 last_pos->offset -= sizeof(struct long_ad);
31170b6a 502 else
7e49b6f2 503 return -EIO;
28de7948 504
31170b6a 505 return count;
70bfb3a8
JK
506out_err:
507 /* Remove extents we've created so far */
508 udf_clear_extent_cache(inode);
509 udf_truncate_extents(inode);
510 return err;
31170b6a
JK
511}
512
fa33cdbf
SM
513/* Extend the final block of the file to final_block_len bytes */
514static void udf_do_extend_final_block(struct inode *inode,
515 struct extent_position *last_pos,
516 struct kernel_long_ad *last_ext,
1f3868f0 517 uint32_t new_elen)
fa33cdbf 518{
fa33cdbf
SM
519 uint32_t added_bytes;
520
1f3868f0
JK
521 /*
522 * Extent already large enough? It may be already rounded up to block
523 * size...
524 */
525 if (new_elen <= (last_ext->extLength & UDF_EXTENT_LENGTH_MASK))
526 return;
83c7423d 527 added_bytes = new_elen - (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
fa33cdbf
SM
528 last_ext->extLength += added_bytes;
529 UDF_I(inode)->i_lenExtents += added_bytes;
530
531 udf_write_aext(inode, last_pos, &last_ext->extLocation,
532 last_ext->extLength, 1);
533}
534
7e49b6f2
JK
535static int udf_extend_file(struct inode *inode, loff_t newsize)
536{
537
538 struct extent_position epos;
539 struct kernel_lb_addr eloc;
540 uint32_t elen;
541 int8_t etype;
542 struct super_block *sb = inode->i_sb;
543 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
1f3868f0 544 loff_t new_elen;
7e49b6f2
JK
545 int adsize;
546 struct udf_inode_info *iinfo = UDF_I(inode);
547 struct kernel_long_ad extent;
fa33cdbf 548 int err = 0;
1f3868f0 549 bool within_last_ext;
7e49b6f2
JK
550
551 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
552 adsize = sizeof(struct short_ad);
553 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
554 adsize = sizeof(struct long_ad);
555 else
556 BUG();
557
16d05565
JK
558 /*
559 * When creating hole in file, just don't bother with preserving
560 * preallocation. It likely won't be very useful anyway.
561 */
562 udf_discard_prealloc(inode);
563
7e49b6f2 564 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
1f3868f0 565 within_last_ext = (etype != -1);
16d05565 566 /* We don't expect extents past EOF... */
1f3868f0 567 WARN_ON_ONCE(within_last_ext &&
16d05565 568 elen > ((loff_t)offset + 1) << inode->i_blkbits);
7e49b6f2 569
7e49b6f2
JK
570 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
571 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
572 /* File has no extents at all or has empty last
573 * indirect extent! Create a fake extent... */
574 extent.extLocation.logicalBlockNum = 0;
575 extent.extLocation.partitionReferenceNum = 0;
576 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
577 } else {
578 epos.offset -= adsize;
579 etype = udf_next_aext(inode, &epos, &extent.extLocation,
580 &extent.extLength, 0);
581 extent.extLength |= etype << 30;
582 }
fa33cdbf 583
1f3868f0
JK
584 new_elen = ((loff_t)offset << inode->i_blkbits) |
585 (newsize & (sb->s_blocksize - 1));
fa33cdbf
SM
586
587 /* File has extent covering the new size (could happen when extending
588 * inside a block)?
589 */
1f3868f0 590 if (within_last_ext) {
fa33cdbf 591 /* Extending file within the last file block */
1f3868f0 592 udf_do_extend_final_block(inode, &epos, &extent, new_elen);
fa33cdbf 593 } else {
1f3868f0 594 err = udf_do_extend_file(inode, &epos, &extent, new_elen);
fa33cdbf
SM
595 }
596
7e49b6f2
JK
597 if (err < 0)
598 goto out;
599 err = 0;
7e49b6f2
JK
600out:
601 brelse(epos.bh);
602 return err;
603}
604
b3c03fce 605static int inode_getblk(struct inode *inode, struct udf_map_rq *map)
1da177e4 606{
5ca4e4be 607 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
ff116fc8 608 struct extent_position prev_epos, cur_epos, next_epos;
1da177e4 609 int count = 0, startnum = 0, endnum = 0;
85d71244 610 uint32_t elen = 0, tmpelen;
5ca4e4be 611 struct kernel_lb_addr eloc, tmpeloc;
1da177e4 612 int c = 1;
60448b1d 613 loff_t lbcount = 0, b_off = 0;
b3c03fce 614 udf_pblk_t newblocknum;
60448b1d 615 sector_t offset = 0;
1da177e4 616 int8_t etype;
48d6d8ff 617 struct udf_inode_info *iinfo = UDF_I(inode);
b490bdd6 618 udf_pblk_t goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
31170b6a 619 int lastblock = 0;
fb719c59 620 bool isBeyondEOF;
b3c03fce 621 int ret = 0;
1da177e4 622
ff116fc8 623 prev_epos.offset = udf_file_entry_alloc_offset(inode);
48d6d8ff 624 prev_epos.block = iinfo->i_location;
ff116fc8
JK
625 prev_epos.bh = NULL;
626 cur_epos = next_epos = prev_epos;
b3c03fce 627 b_off = (loff_t)map->lblk << inode->i_sb->s_blocksize_bits;
1da177e4
LT
628
629 /* find the extent which contains the block we are looking for.
cb00ea35
CG
630 alternate between laarr[0] and laarr[1] for locations of the
631 current extent, and the previous extent */
632 do {
633 if (prev_epos.bh != cur_epos.bh) {
3bf25cb4
JK
634 brelse(prev_epos.bh);
635 get_bh(cur_epos.bh);
ff116fc8 636 prev_epos.bh = cur_epos.bh;
1da177e4 637 }
cb00ea35 638 if (cur_epos.bh != next_epos.bh) {
3bf25cb4
JK
639 brelse(cur_epos.bh);
640 get_bh(next_epos.bh);
ff116fc8 641 cur_epos.bh = next_epos.bh;
1da177e4
LT
642 }
643
644 lbcount += elen;
645
ff116fc8
JK
646 prev_epos.block = cur_epos.block;
647 cur_epos.block = next_epos.block;
1da177e4 648
ff116fc8
JK
649 prev_epos.offset = cur_epos.offset;
650 cur_epos.offset = next_epos.offset;
1da177e4 651
4b11111a
MS
652 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
653 if (etype == -1)
1da177e4
LT
654 break;
655
656 c = !c;
657
658 laarr[c].extLength = (etype << 30) | elen;
659 laarr[c].extLocation = eloc;
660
661 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
662 pgoal = eloc.logicalBlockNum +
28de7948
CG
663 ((elen + inode->i_sb->s_blocksize - 1) >>
664 inode->i_sb->s_blocksize_bits);
1da177e4 665
cb00ea35 666 count++;
1da177e4
LT
667 } while (lbcount + elen <= b_off);
668
669 b_off -= lbcount;
670 offset = b_off >> inode->i_sb->s_blocksize_bits;
85d71244
JK
671 /*
672 * Move prev_epos and cur_epos into indirect extent if we are at
673 * the pointer to it
674 */
675 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
676 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
1da177e4
LT
677
678 /* if the extent is allocated and recorded, return the block
cb00ea35 679 if the extent is not a multiple of the blocksize, round up */
1da177e4 680
cb00ea35
CG
681 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
682 if (elen & (inode->i_sb->s_blocksize - 1)) {
1da177e4 683 elen = EXT_RECORDED_ALLOCATED |
28de7948
CG
684 ((elen + inode->i_sb->s_blocksize - 1) &
685 ~(inode->i_sb->s_blocksize - 1));
b316c443
JK
686 iinfo->i_lenExtents =
687 ALIGN(iinfo->i_lenExtents,
688 inode->i_sb->s_blocksize);
7e49b6f2 689 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
1da177e4 690 }
b3c03fce
JK
691 map->oflags = UDF_BLK_MAPPED;
692 map->pblk = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
02d4ca49 693 goto out_free;
1da177e4
LT
694 }
695
16d05565 696 /* Are we beyond EOF and preallocated extent? */
cb00ea35 697 if (etype == -1) {
fa33cdbf 698 loff_t hole_len;
16d05565 699
6981498d 700 isBeyondEOF = true;
31170b6a
JK
701 if (count) {
702 if (c)
703 laarr[0] = laarr[1];
704 startnum = 1;
cb00ea35 705 } else {
31170b6a 706 /* Create a fake extent when there's not one */
4b11111a 707 memset(&laarr[0].extLocation, 0x00,
5ca4e4be 708 sizeof(struct kernel_lb_addr));
31170b6a 709 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
7e49b6f2 710 /* Will udf_do_extend_file() create real extent from
4b11111a 711 a fake one? */
31170b6a
JK
712 startnum = (offset > 0);
713 }
714 /* Create extents for the hole between EOF and offset */
fa33cdbf
SM
715 hole_len = (loff_t)offset << inode->i_blkbits;
716 ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
b3c03fce 717 if (ret < 0)
02d4ca49 718 goto out_free;
31170b6a
JK
719 c = 0;
720 offset = 0;
721 count += ret;
722 /* We are not covered by a preallocated extent? */
4b11111a
MS
723 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
724 EXT_NOT_RECORDED_ALLOCATED) {
31170b6a
JK
725 /* Is there any real extent? - otherwise we overwrite
726 * the fake one... */
727 if (count)
728 c = !c;
729 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
28de7948 730 inode->i_sb->s_blocksize;
4b11111a 731 memset(&laarr[c].extLocation, 0x00,
5ca4e4be 732 sizeof(struct kernel_lb_addr));
cb00ea35 733 count++;
31170b6a 734 }
cb00ea35 735 endnum = c + 1;
1da177e4 736 lastblock = 1;
cb00ea35 737 } else {
6981498d 738 isBeyondEOF = false;
1da177e4
LT
739 endnum = startnum = ((count > 2) ? 2 : count);
740
4b11111a
MS
741 /* if the current extent is in position 0,
742 swap it with the previous */
cb00ea35 743 if (!c && count != 1) {
31170b6a
JK
744 laarr[2] = laarr[0];
745 laarr[0] = laarr[1];
746 laarr[1] = laarr[2];
747 c = 1;
748 }
1da177e4 749
4b11111a
MS
750 /* if the current block is located in an extent,
751 read the next extent */
752 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
753 if (etype != -1) {
cb00ea35
CG
754 laarr[c + 1].extLength = (etype << 30) | elen;
755 laarr[c + 1].extLocation = eloc;
756 count++;
757 startnum++;
758 endnum++;
4b11111a 759 } else
1da177e4
LT
760 lastblock = 1;
761 }
1da177e4
LT
762
763 /* if the current extent is not recorded but allocated, get the
28de7948 764 * block in the extent corresponding to the requested block */
4b11111a 765 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
1da177e4 766 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
4b11111a 767 else { /* otherwise, allocate a new block */
b3c03fce 768 if (iinfo->i_next_alloc_block == map->lblk)
48d6d8ff 769 goal = iinfo->i_next_alloc_goal;
1da177e4 770
cb00ea35 771 if (!goal) {
4b11111a 772 if (!(goal = pgoal)) /* XXX: what was intended here? */
48d6d8ff 773 goal = iinfo->i_location.logicalBlockNum + 1;
1da177e4
LT
774 }
775
4b11111a 776 newblocknum = udf_new_block(inode->i_sb, inode,
48d6d8ff 777 iinfo->i_location.partitionReferenceNum,
b3c03fce
JK
778 goal, &ret);
779 if (!newblocknum)
02d4ca49 780 goto out_free;
fb719c59
NJ
781 if (isBeyondEOF)
782 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
1da177e4
LT
783 }
784
4b11111a
MS
785 /* if the extent the requsted block is located in contains multiple
786 * blocks, split the extent into at most three extents. blocks prior
787 * to requested block, requested block, and blocks after requested
788 * block */
1da177e4
LT
789 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
790
80942521 791 if (!(map->iflags & UDF_MAP_NOPREALLOC))
81056dd0 792 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
1da177e4
LT
793
794 /* merge any continuous blocks in laarr */
795 udf_merge_extents(inode, laarr, &endnum);
796
797 /* write back the new extents, inserting new extents if the new number
28de7948
CG
798 * of extents is greater than the old number, and deleting extents if
799 * the new number of extents is less than the old number */
b3c03fce
JK
800 ret = udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
801 if (ret < 0)
19fd80de 802 goto out_free;
1da177e4 803
b3c03fce 804 map->pblk = udf_get_pblock(inode->i_sb, newblocknum,
48d6d8ff 805 iinfo->i_location.partitionReferenceNum, 0);
b3c03fce
JK
806 if (!map->pblk) {
807 ret = -EFSCORRUPTED;
02d4ca49 808 goto out_free;
7b0b0933 809 }
b3c03fce
JK
810 map->oflags = UDF_BLK_NEW | UDF_BLK_MAPPED;
811 iinfo->i_next_alloc_block = map->lblk + 1;
de80dae0 812 iinfo->i_next_alloc_goal = newblocknum + 1;
c2050a45 813 inode->i_ctime = current_time(inode);
1da177e4
LT
814
815 if (IS_SYNC(inode))
816 udf_sync_inode(inode);
817 else
818 mark_inode_dirty(inode);
b3c03fce 819 ret = 0;
02d4ca49
FF
820out_free:
821 brelse(prev_epos.bh);
822 brelse(cur_epos.bh);
823 brelse(next_epos.bh);
b3c03fce 824 return ret;
1da177e4
LT
825}
826
cb00ea35 827static void udf_split_extents(struct inode *inode, int *c, int offset,
b490bdd6
SM
828 udf_pblk_t newblocknum,
829 struct kernel_long_ad *laarr, int *endnum)
1da177e4 830{
4b11111a
MS
831 unsigned long blocksize = inode->i_sb->s_blocksize;
832 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
833
1da177e4 834 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
4b11111a
MS
835 (laarr[*c].extLength >> 30) ==
836 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
1da177e4
LT
837 int curr = *c;
838 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
4b11111a 839 blocksize - 1) >> blocksize_bits;
1da177e4
LT
840 int8_t etype = (laarr[curr].extLength >> 30);
841
4b11111a 842 if (blen == 1)
28de7948 843 ;
4b11111a 844 else if (!offset || blen == offset + 1) {
cb00ea35
CG
845 laarr[curr + 2] = laarr[curr + 1];
846 laarr[curr + 1] = laarr[curr];
847 } else {
848 laarr[curr + 3] = laarr[curr + 1];
849 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
850 }
851
852 if (offset) {
853 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
4b11111a 854 udf_free_blocks(inode->i_sb, inode,
97e961fd 855 &laarr[curr].extLocation,
4b11111a
MS
856 0, offset);
857 laarr[curr].extLength =
858 EXT_NOT_RECORDED_NOT_ALLOCATED |
859 (offset << blocksize_bits);
1da177e4 860 laarr[curr].extLocation.logicalBlockNum = 0;
4b11111a
MS
861 laarr[curr].extLocation.
862 partitionReferenceNum = 0;
863 } else
1da177e4 864 laarr[curr].extLength = (etype << 30) |
4b11111a 865 (offset << blocksize_bits);
cb00ea35
CG
866 curr++;
867 (*c)++;
868 (*endnum)++;
1da177e4 869 }
647bd61a 870
1da177e4
LT
871 laarr[curr].extLocation.logicalBlockNum = newblocknum;
872 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
873 laarr[curr].extLocation.partitionReferenceNum =
c0b34438 874 UDF_I(inode)->i_location.partitionReferenceNum;
1da177e4 875 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
4b11111a 876 blocksize;
cb00ea35 877 curr++;
1da177e4 878
cb00ea35 879 if (blen != offset + 1) {
1da177e4 880 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
4b11111a
MS
881 laarr[curr].extLocation.logicalBlockNum +=
882 offset + 1;
28de7948 883 laarr[curr].extLength = (etype << 30) |
4b11111a 884 ((blen - (offset + 1)) << blocksize_bits);
cb00ea35
CG
885 curr++;
886 (*endnum)++;
1da177e4
LT
887 }
888 }
889}
890
891static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
3cc6f844 892 struct kernel_long_ad *laarr,
cb00ea35 893 int *endnum)
1da177e4
LT
894{
895 int start, length = 0, currlength = 0, i;
896
cb00ea35 897 if (*endnum >= (c + 1)) {
1da177e4
LT
898 if (!lastblock)
899 return;
900 else
901 start = c;
cb00ea35 902 } else {
4b11111a
MS
903 if ((laarr[c + 1].extLength >> 30) ==
904 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
cb00ea35 905 start = c + 1;
4b11111a
MS
906 length = currlength =
907 (((laarr[c + 1].extLength &
908 UDF_EXTENT_LENGTH_MASK) +
909 inode->i_sb->s_blocksize - 1) >>
910 inode->i_sb->s_blocksize_bits);
911 } else
1da177e4
LT
912 start = c;
913 }
914
cb00ea35
CG
915 for (i = start + 1; i <= *endnum; i++) {
916 if (i == *endnum) {
1da177e4
LT
917 if (lastblock)
918 length += UDF_DEFAULT_PREALLOC_BLOCKS;
4b11111a
MS
919 } else if ((laarr[i].extLength >> 30) ==
920 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
921 length += (((laarr[i].extLength &
922 UDF_EXTENT_LENGTH_MASK) +
923 inode->i_sb->s_blocksize - 1) >>
924 inode->i_sb->s_blocksize_bits);
925 } else
1da177e4
LT
926 break;
927 }
928
cb00ea35 929 if (length) {
1da177e4 930 int next = laarr[start].extLocation.logicalBlockNum +
28de7948 931 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
4b11111a
MS
932 inode->i_sb->s_blocksize - 1) >>
933 inode->i_sb->s_blocksize_bits);
1da177e4 934 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
4b11111a
MS
935 laarr[start].extLocation.partitionReferenceNum,
936 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
937 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
938 currlength);
28de7948 939 if (numalloc) {
4b11111a 940 if (start == (c + 1))
1da177e4 941 laarr[start].extLength +=
4b11111a
MS
942 (numalloc <<
943 inode->i_sb->s_blocksize_bits);
944 else {
cb00ea35 945 memmove(&laarr[c + 2], &laarr[c + 1],
5ca4e4be 946 sizeof(struct long_ad) * (*endnum - (c + 1)));
cb00ea35
CG
947 (*endnum)++;
948 laarr[c + 1].extLocation.logicalBlockNum = next;
949 laarr[c + 1].extLocation.partitionReferenceNum =
4b11111a
MS
950 laarr[c].extLocation.
951 partitionReferenceNum;
952 laarr[c + 1].extLength =
953 EXT_NOT_RECORDED_ALLOCATED |
954 (numalloc <<
955 inode->i_sb->s_blocksize_bits);
cb00ea35 956 start = c + 1;
1da177e4
LT
957 }
958
cb00ea35 959 for (i = start + 1; numalloc && i < *endnum; i++) {
4b11111a
MS
960 int elen = ((laarr[i].extLength &
961 UDF_EXTENT_LENGTH_MASK) +
962 inode->i_sb->s_blocksize - 1) >>
963 inode->i_sb->s_blocksize_bits;
1da177e4 964
cb00ea35 965 if (elen > numalloc) {
1da177e4 966 laarr[i].extLength -=
4b11111a
MS
967 (numalloc <<
968 inode->i_sb->s_blocksize_bits);
1da177e4 969 numalloc = 0;
cb00ea35 970 } else {
1da177e4 971 numalloc -= elen;
cb00ea35 972 if (*endnum > (i + 1))
4b11111a
MS
973 memmove(&laarr[i],
974 &laarr[i + 1],
5ca4e4be 975 sizeof(struct long_ad) *
4b11111a 976 (*endnum - (i + 1)));
cb00ea35
CG
977 i--;
978 (*endnum)--;
1da177e4
LT
979 }
980 }
c0b34438 981 UDF_I(inode)->i_lenExtents +=
4b11111a 982 numalloc << inode->i_sb->s_blocksize_bits;
1da177e4
LT
983 }
984 }
985}
986
3cc6f844 987static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
cb00ea35 988 int *endnum)
1da177e4
LT
989{
990 int i;
4b11111a
MS
991 unsigned long blocksize = inode->i_sb->s_blocksize;
992 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1da177e4 993
cb00ea35 994 for (i = 0; i < (*endnum - 1); i++) {
5ca4e4be
PE
995 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
996 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
4b11111a
MS
997
998 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
999 (((li->extLength >> 30) ==
1000 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1001 ((lip1->extLocation.logicalBlockNum -
1002 li->extLocation.logicalBlockNum) ==
1003 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1004 blocksize - 1) >> blocksize_bits)))) {
1005
1006 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
53cafe1d
JK
1007 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1008 blocksize - 1) <= UDF_EXTENT_LENGTH_MASK) {
4b11111a
MS
1009 li->extLength = lip1->extLength +
1010 (((li->extLength &
1011 UDF_EXTENT_LENGTH_MASK) +
1012 blocksize - 1) & ~(blocksize - 1));
1013 if (*endnum > (i + 2))
1014 memmove(&laarr[i + 1], &laarr[i + 2],
5ca4e4be 1015 sizeof(struct long_ad) *
4b11111a
MS
1016 (*endnum - (i + 2)));
1017 i--;
1018 (*endnum)--;
1da177e4 1019 }
4b11111a
MS
1020 } else if (((li->extLength >> 30) ==
1021 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1022 ((lip1->extLength >> 30) ==
1023 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
97e961fd 1024 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
4b11111a
MS
1025 ((li->extLength &
1026 UDF_EXTENT_LENGTH_MASK) +
1027 blocksize - 1) >> blocksize_bits);
1028 li->extLocation.logicalBlockNum = 0;
1029 li->extLocation.partitionReferenceNum = 0;
1030
1031 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1032 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1033 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1034 lip1->extLength = (lip1->extLength -
1035 (li->extLength &
1036 UDF_EXTENT_LENGTH_MASK) +
1037 UDF_EXTENT_LENGTH_MASK) &
1038 ~(blocksize - 1);
1039 li->extLength = (li->extLength &
1040 UDF_EXTENT_FLAG_MASK) +
1041 (UDF_EXTENT_LENGTH_MASK + 1) -
1042 blocksize;
cb00ea35 1043 } else {
4b11111a
MS
1044 li->extLength = lip1->extLength +
1045 (((li->extLength &
1046 UDF_EXTENT_LENGTH_MASK) +
1047 blocksize - 1) & ~(blocksize - 1));
cb00ea35
CG
1048 if (*endnum > (i + 2))
1049 memmove(&laarr[i + 1], &laarr[i + 2],
5ca4e4be 1050 sizeof(struct long_ad) *
4b11111a 1051 (*endnum - (i + 2)));
cb00ea35
CG
1052 i--;
1053 (*endnum)--;
1da177e4 1054 }
4b11111a
MS
1055 } else if ((li->extLength >> 30) ==
1056 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1057 udf_free_blocks(inode->i_sb, inode,
97e961fd 1058 &li->extLocation, 0,
4b11111a
MS
1059 ((li->extLength &
1060 UDF_EXTENT_LENGTH_MASK) +
1061 blocksize - 1) >> blocksize_bits);
1062 li->extLocation.logicalBlockNum = 0;
1063 li->extLocation.partitionReferenceNum = 0;
1064 li->extLength = (li->extLength &
1065 UDF_EXTENT_LENGTH_MASK) |
1066 EXT_NOT_RECORDED_NOT_ALLOCATED;
1da177e4
LT
1067 }
1068 }
1069}
1070
19fd80de
JK
1071static int udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
1072 int startnum, int endnum,
1073 struct extent_position *epos)
1da177e4
LT
1074{
1075 int start = 0, i;
5ca4e4be 1076 struct kernel_lb_addr tmploc;
1da177e4 1077 uint32_t tmplen;
19fd80de 1078 int err;
1da177e4 1079
cb00ea35
CG
1080 if (startnum > endnum) {
1081 for (i = 0; i < (startnum - endnum); i++)
6c1e4d06 1082 udf_delete_aext(inode, *epos);
cb00ea35
CG
1083 } else if (startnum < endnum) {
1084 for (i = 0; i < (endnum - startnum); i++) {
19fd80de
JK
1085 err = udf_insert_aext(inode, *epos,
1086 laarr[i].extLocation,
1087 laarr[i].extLength);
1088 /*
1089 * If we fail here, we are likely corrupting the extent
1090 * list and leaking blocks. At least stop early to
1091 * limit the damage.
1092 */
1093 if (err < 0)
1094 return err;
ff116fc8 1095 udf_next_aext(inode, epos, &laarr[i].extLocation,
cb00ea35
CG
1096 &laarr[i].extLength, 1);
1097 start++;
1da177e4
LT
1098 }
1099 }
1100
cb00ea35 1101 for (i = start; i < endnum; i++) {
ff116fc8 1102 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
97e961fd 1103 udf_write_aext(inode, epos, &laarr[i].extLocation,
cb00ea35 1104 laarr[i].extLength, 1);
1da177e4 1105 }
19fd80de 1106 return 0;
1da177e4
LT
1107}
1108
b490bdd6 1109struct buffer_head *udf_bread(struct inode *inode, udf_pblk_t block,
cb00ea35 1110 int create, int *err)
1da177e4 1111{
cb00ea35 1112 struct buffer_head *bh = NULL;
32f123a3
JK
1113 struct udf_map_rq map = {
1114 .lblk = block,
1115 .iflags = UDF_MAP_NOPREALLOC | (create ? UDF_MAP_CREATE : 0),
1116 };
1da177e4 1117
32f123a3
JK
1118 *err = udf_map_block(inode, &map);
1119 if (*err || !(map.oflags & UDF_BLK_MAPPED))
1120 return NULL;
1121
1122 bh = sb_getblk(inode->i_sb, map.pblk);
1123 if (!bh) {
1124 *err = -ENOMEM;
1da177e4 1125 return NULL;
32f123a3
JK
1126 }
1127 if (map.oflags & UDF_BLK_NEW) {
1128 lock_buffer(bh);
1129 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
1130 set_buffer_uptodate(bh);
1131 unlock_buffer(bh);
1132 mark_buffer_dirty_inode(bh, inode);
1133 return bh;
1134 }
1da177e4 1135
59a16786 1136 if (bh_read(bh, 0) >= 0)
1da177e4 1137 return bh;
28de7948 1138
1da177e4
LT
1139 brelse(bh);
1140 *err = -EIO;
1141 return NULL;
1142}
1143
7e49b6f2 1144int udf_setsize(struct inode *inode, loff_t newsize)
1da177e4 1145{
1da177e4 1146 int err;
48d6d8ff 1147 struct udf_inode_info *iinfo;
89a4d970 1148 unsigned int bsize = i_blocksize(inode);
1da177e4
LT
1149
1150 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
cb00ea35 1151 S_ISLNK(inode->i_mode)))
7e49b6f2 1152 return -EINVAL;
1da177e4 1153 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
7e49b6f2 1154 return -EPERM;
1da177e4 1155
48d6d8ff 1156 iinfo = UDF_I(inode);
7e49b6f2 1157 if (newsize > inode->i_size) {
7e49b6f2 1158 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
6a3b37e0 1159 if (bsize >=
7e49b6f2 1160 (udf_file_entry_alloc_offset(inode) + newsize)) {
d2eb8c35 1161 down_write(&iinfo->i_data_sem);
7e49b6f2 1162 iinfo->i_lenAlloc = newsize;
bb2b6d19
IA
1163 goto set_size;
1164 }
6a3b37e0
JK
1165 err = udf_expand_file_adinicb(inode);
1166 if (err)
1167 return err;
7e49b6f2 1168 }
6a3b37e0 1169 down_write(&iinfo->i_data_sem);
7e49b6f2
JK
1170 err = udf_extend_file(inode, newsize);
1171 if (err) {
1172 up_write(&iinfo->i_data_sem);
1173 return err;
1da177e4 1174 }
bb2b6d19 1175set_size:
4d0fb621 1176 up_write(&iinfo->i_data_sem);
f2e95355 1177 truncate_setsize(inode, newsize);
cb00ea35 1178 } else {
7e49b6f2
JK
1179 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1180 down_write(&iinfo->i_data_sem);
99600051 1181 udf_clear_extent_cache(inode);
382a2287 1182 memset(iinfo->i_data + iinfo->i_lenEAttr + newsize,
7e49b6f2
JK
1183 0x00, bsize - newsize -
1184 udf_file_entry_alloc_offset(inode));
1185 iinfo->i_lenAlloc = newsize;
1186 truncate_setsize(inode, newsize);
1187 up_write(&iinfo->i_data_sem);
1188 goto update_time;
1189 }
1190 err = block_truncate_page(inode->i_mapping, newsize,
1191 udf_get_block);
1192 if (err)
1193 return err;
f2e95355 1194 truncate_setsize(inode, newsize);
4d0fb621 1195 down_write(&iinfo->i_data_sem);
99600051 1196 udf_clear_extent_cache(inode);
2b42be5e 1197 err = udf_truncate_extents(inode);
4d0fb621 1198 up_write(&iinfo->i_data_sem);
2b42be5e
JK
1199 if (err)
1200 return err;
647bd61a 1201 }
7e49b6f2 1202update_time:
c2050a45 1203 inode->i_mtime = inode->i_ctime = current_time(inode);
1da177e4 1204 if (IS_SYNC(inode))
cb00ea35 1205 udf_sync_inode(inode);
1da177e4
LT
1206 else
1207 mark_inode_dirty(inode);
7e49b6f2 1208 return 0;
1da177e4
LT
1209}
1210
c03aa9f6
JK
1211/*
1212 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1213 * arbitrary - just that we hopefully don't limit any real use of rewritten
1214 * inode on write-once media but avoid looping for too long on corrupted media.
1215 */
1216#define UDF_MAX_ICB_NESTING 1024
1217
6174c2eb 1218static int udf_read_inode(struct inode *inode, bool hidden_inode)
1da177e4
LT
1219{
1220 struct buffer_head *bh = NULL;
1221 struct fileEntry *fe;
bb7720a0 1222 struct extendedFileEntry *efe;
1da177e4 1223 uint16_t ident;
48d6d8ff 1224 struct udf_inode_info *iinfo = UDF_I(inode);
bb7720a0 1225 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
6d3d5e86 1226 struct kernel_lb_addr *iloc = &iinfo->i_location;
bb7720a0 1227 unsigned int link_count;
c03aa9f6 1228 unsigned int indirections = 0;
79144954 1229 int bs = inode->i_sb->s_blocksize;
6d3d5e86 1230 int ret = -EIO;
0c9850f4 1231 uint32_t uid, gid;
1da177e4 1232
c03aa9f6 1233reread:
1d82a56b 1234 if (iloc->partitionReferenceNum >= sbi->s_partitions) {
fcbf7637 1235 udf_debug("partition reference: %u > logical volume partitions: %u\n",
1d82a56b
FF
1236 iloc->partitionReferenceNum, sbi->s_partitions);
1237 return -EIO;
1238 }
1239
6d3d5e86
JK
1240 if (iloc->logicalBlockNum >=
1241 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
fcbf7637 1242 udf_debug("block=%u, partition=%u out of range\n",
6d3d5e86
JK
1243 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1244 return -EIO;
1245 }
1246
1da177e4
LT
1247 /*
1248 * Set defaults, but the inode is still incomplete!
1249 * Note: get_new_inode() sets the following on a new inode:
1250 * i_sb = sb
1251 * i_no = ino
1252 * i_flags = sb->s_flags
1253 * i_state = 0
1254 * clean_inode(): zero fills and sets
1255 * i_count = 1
1256 * i_nlink = 1
1257 * i_op = NULL;
1258 */
6d3d5e86 1259 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
cb00ea35 1260 if (!bh) {
fcbf7637 1261 udf_err(inode->i_sb, "(ino %lu) failed !bh\n", inode->i_ino);
6d3d5e86 1262 return -EIO;
1da177e4
LT
1263 }
1264
1265 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
cb00ea35 1266 ident != TAG_IDENT_USE) {
fcbf7637 1267 udf_err(inode->i_sb, "(ino %lu) failed ident=%u\n",
78ace70c 1268 inode->i_ino, ident);
6d3d5e86 1269 goto out;
1da177e4
LT
1270 }
1271
1272 fe = (struct fileEntry *)bh->b_data;
bb7720a0 1273 efe = (struct extendedFileEntry *)bh->b_data;
1da177e4 1274
5e0f0017 1275 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1ab92785 1276 struct buffer_head *ibh;
1da177e4 1277
6d3d5e86 1278 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
1ab92785 1279 if (ident == TAG_IDENT_IE && ibh) {
5ca4e4be 1280 struct kernel_lb_addr loc;
1ab92785 1281 struct indirectEntry *ie;
1282
1283 ie = (struct indirectEntry *)ibh->b_data;
1284 loc = lelb_to_cpu(ie->indirectICB.extLocation);
1285
c03aa9f6 1286 if (ie->indirectICB.extLength) {
c03aa9f6
JK
1287 brelse(ibh);
1288 memcpy(&iinfo->i_location, &loc,
1289 sizeof(struct kernel_lb_addr));
1290 if (++indirections > UDF_MAX_ICB_NESTING) {
1291 udf_err(inode->i_sb,
1292 "too many ICBs in ICB hierarchy"
1293 " (max %d supported)\n",
1294 UDF_MAX_ICB_NESTING);
6d3d5e86 1295 goto out;
28de7948 1296 }
6d3d5e86 1297 brelse(bh);
c03aa9f6 1298 goto reread;
1da177e4 1299 }
28de7948 1300 }
1ab92785 1301 brelse(ibh);
5e0f0017 1302 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
fcbf7637 1303 udf_err(inode->i_sb, "unsupported strategy type: %u\n",
78ace70c 1304 le16_to_cpu(fe->icbTag.strategyType));
6d3d5e86 1305 goto out;
1da177e4 1306 }
5e0f0017 1307 if (fe->icbTag.strategyType == cpu_to_le16(4))
48d6d8ff 1308 iinfo->i_strat4096 = 0;
5e0f0017 1309 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
48d6d8ff 1310 iinfo->i_strat4096 = 1;
1da177e4 1311
48d6d8ff 1312 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
4b11111a 1313 ICBTAG_FLAG_AD_MASK;
d288d958
JK
1314 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
1315 iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
1316 iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1317 ret = -EIO;
1318 goto out;
1319 }
fc8033a3 1320 iinfo->i_hidden = hidden_inode;
48d6d8ff
MS
1321 iinfo->i_unique = 0;
1322 iinfo->i_lenEAttr = 0;
1323 iinfo->i_lenExtents = 0;
1324 iinfo->i_lenAlloc = 0;
1325 iinfo->i_next_alloc_block = 0;
1326 iinfo->i_next_alloc_goal = 0;
5e0f0017 1327 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
48d6d8ff
MS
1328 iinfo->i_efe = 1;
1329 iinfo->i_use = 0;
79144954 1330 ret = udf_alloc_i_data(inode, bs -
6d3d5e86
JK
1331 sizeof(struct extendedFileEntry));
1332 if (ret)
1333 goto out;
382a2287 1334 memcpy(iinfo->i_data,
4b11111a 1335 bh->b_data + sizeof(struct extendedFileEntry),
79144954 1336 bs - sizeof(struct extendedFileEntry));
5e0f0017 1337 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
48d6d8ff
MS
1338 iinfo->i_efe = 0;
1339 iinfo->i_use = 0;
79144954 1340 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
6d3d5e86
JK
1341 if (ret)
1342 goto out;
382a2287 1343 memcpy(iinfo->i_data,
c0b34438 1344 bh->b_data + sizeof(struct fileEntry),
79144954 1345 bs - sizeof(struct fileEntry));
5e0f0017 1346 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
48d6d8ff
MS
1347 iinfo->i_efe = 0;
1348 iinfo->i_use = 1;
1349 iinfo->i_lenAlloc = le32_to_cpu(
4b11111a
MS
1350 ((struct unallocSpaceEntry *)bh->b_data)->
1351 lengthAllocDescs);
79144954 1352 ret = udf_alloc_i_data(inode, bs -
6d3d5e86
JK
1353 sizeof(struct unallocSpaceEntry));
1354 if (ret)
1355 goto out;
382a2287 1356 memcpy(iinfo->i_data,
4b11111a 1357 bh->b_data + sizeof(struct unallocSpaceEntry),
79144954 1358 bs - sizeof(struct unallocSpaceEntry));
6d3d5e86 1359 return 0;
1da177e4
LT
1360 }
1361
6d3d5e86 1362 ret = -EIO;
c03cad24 1363 read_lock(&sbi->s_cred_lock);
0c9850f4
JK
1364 uid = le32_to_cpu(fe->uid);
1365 if (uid == UDF_INVALID_ID ||
ca76d2d8 1366 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
0c9850f4
JK
1367 inode->i_uid = sbi->s_uid;
1368 else
1369 i_uid_write(inode, uid);
1da177e4 1370
0c9850f4
JK
1371 gid = le32_to_cpu(fe->gid);
1372 if (gid == UDF_INVALID_ID ||
ca76d2d8 1373 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
0c9850f4
JK
1374 inode->i_gid = sbi->s_gid;
1375 else
1376 i_gid_write(inode, gid);
1da177e4 1377
7ac9bcd5 1378 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
87bc730c 1379 sbi->s_fmode != UDF_INVALID_MODE)
7ac9bcd5
MS
1380 inode->i_mode = sbi->s_fmode;
1381 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
87bc730c 1382 sbi->s_dmode != UDF_INVALID_MODE)
7ac9bcd5
MS
1383 inode->i_mode = sbi->s_dmode;
1384 else
1385 inode->i_mode = udf_convert_permissions(fe);
1386 inode->i_mode &= ~sbi->s_umask;
c3367a1b
SM
1387 iinfo->i_extraPerms = le32_to_cpu(fe->permissions) & ~FE_MAPPED_PERMS;
1388
c03cad24
JK
1389 read_unlock(&sbi->s_cred_lock);
1390
bfe86848 1391 link_count = le16_to_cpu(fe->fileLinkCount);
4071b913 1392 if (!link_count) {
6174c2eb
JK
1393 if (!hidden_inode) {
1394 ret = -ESTALE;
1395 goto out;
1396 }
1397 link_count = 1;
4071b913 1398 }
bfe86848 1399 set_nlink(inode, link_count);
c03cad24
JK
1400
1401 inode->i_size = le64_to_cpu(fe->informationLength);
1402 iinfo->i_lenExtents = inode->i_size;
1da177e4 1403
48d6d8ff 1404 if (iinfo->i_efe == 0) {
1da177e4 1405 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
28de7948 1406 (inode->i_sb->s_blocksize_bits - 9);
1da177e4 1407
c3b9cecd
AB
1408 udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime);
1409 udf_disk_stamp_to_time(&inode->i_mtime, fe->modificationTime);
1410 udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime);
1da177e4 1411
48d6d8ff
MS
1412 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1413 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1414 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
d5e2cf07 1415 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
ab9a3a73
SM
1416 iinfo->i_streamdir = 0;
1417 iinfo->i_lenStreams = 0;
cb00ea35 1418 } else {
647bd61a 1419 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
cb00ea35 1420 (inode->i_sb->s_blocksize_bits - 9);
1da177e4 1421
c3b9cecd
AB
1422 udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime);
1423 udf_disk_stamp_to_time(&inode->i_mtime, efe->modificationTime);
0220edda 1424 udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime);
c3b9cecd 1425 udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime);
1da177e4 1426
48d6d8ff
MS
1427 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1428 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1429 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
d5e2cf07 1430 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
ab9a3a73
SM
1431
1432 /* Named streams */
1433 iinfo->i_streamdir = (efe->streamDirectoryICB.extLength != 0);
1434 iinfo->i_locStreamdir =
1435 lelb_to_cpu(efe->streamDirectoryICB.extLocation);
1436 iinfo->i_lenStreams = le64_to_cpu(efe->objectSize);
1437 if (iinfo->i_lenStreams >= inode->i_size)
1438 iinfo->i_lenStreams -= inode->i_size;
1439 else
1440 iinfo->i_lenStreams = 0;
1da177e4 1441 }
470cca56 1442 inode->i_generation = iinfo->i_unique;
1da177e4 1443
23b133bd
JK
1444 /*
1445 * Sanity check length of allocation descriptors and extended attrs to
1446 * avoid integer overflows
1447 */
1448 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1449 goto out;
1450 /* Now do exact checks */
1451 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1452 goto out;
e159332b
JK
1453 /* Sanity checks for files in ICB so that we don't get confused later */
1454 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1455 /*
1456 * For file in ICB data is stored in allocation descriptor
1457 * so sizes should match
1458 */
1459 if (iinfo->i_lenAlloc != inode->i_size)
1460 goto out;
1461 /* File in ICB has to fit in there... */
79144954 1462 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
e159332b
JK
1463 goto out;
1464 }
1465
cb00ea35
CG
1466 switch (fe->icbTag.fileType) {
1467 case ICBTAG_FILE_TYPE_DIRECTORY:
28de7948
CG
1468 inode->i_op = &udf_dir_inode_operations;
1469 inode->i_fop = &udf_dir_operations;
1470 inode->i_mode |= S_IFDIR;
1471 inc_nlink(inode);
1472 break;
cb00ea35
CG
1473 case ICBTAG_FILE_TYPE_REALTIME:
1474 case ICBTAG_FILE_TYPE_REGULAR:
1475 case ICBTAG_FILE_TYPE_UNDEF:
742e1795 1476 case ICBTAG_FILE_TYPE_VAT20:
48d6d8ff 1477 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
28de7948
CG
1478 inode->i_data.a_ops = &udf_adinicb_aops;
1479 else
1480 inode->i_data.a_ops = &udf_aops;
1481 inode->i_op = &udf_file_inode_operations;
1482 inode->i_fop = &udf_file_operations;
1483 inode->i_mode |= S_IFREG;
1484 break;
cb00ea35 1485 case ICBTAG_FILE_TYPE_BLOCK:
28de7948
CG
1486 inode->i_mode |= S_IFBLK;
1487 break;
cb00ea35 1488 case ICBTAG_FILE_TYPE_CHAR:
28de7948
CG
1489 inode->i_mode |= S_IFCHR;
1490 break;
cb00ea35 1491 case ICBTAG_FILE_TYPE_FIFO:
28de7948
CG
1492 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1493 break;
cb00ea35 1494 case ICBTAG_FILE_TYPE_SOCKET:
28de7948
CG
1495 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1496 break;
cb00ea35 1497 case ICBTAG_FILE_TYPE_SYMLINK:
28de7948 1498 inode->i_data.a_ops = &udf_symlink_aops;
ad4d0532 1499 inode->i_op = &udf_symlink_inode_operations;
21fc61c7 1500 inode_nohighmem(inode);
6ff6b2b3 1501 inode->i_mode = S_IFLNK | 0777;
28de7948 1502 break;
bfb257a5
JK
1503 case ICBTAG_FILE_TYPE_MAIN:
1504 udf_debug("METADATA FILE-----\n");
1505 break;
1506 case ICBTAG_FILE_TYPE_MIRROR:
1507 udf_debug("METADATA MIRROR FILE-----\n");
1508 break;
1509 case ICBTAG_FILE_TYPE_BITMAP:
1510 udf_debug("METADATA BITMAP FILE-----\n");
1511 break;
cb00ea35 1512 default:
fcbf7637 1513 udf_err(inode->i_sb, "(ino %lu) failed unknown file type=%u\n",
78ace70c 1514 inode->i_ino, fe->icbTag.fileType);
6d3d5e86 1515 goto out;
1da177e4 1516 }
cb00ea35 1517 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4b11111a
MS
1518 struct deviceSpec *dsea =
1519 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
cb00ea35
CG
1520 if (dsea) {
1521 init_special_inode(inode, inode->i_mode,
4b11111a
MS
1522 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1523 le32_to_cpu(dsea->minorDeviceIdent)));
1da177e4 1524 /* Developer ID ??? */
4b11111a 1525 } else
6d3d5e86 1526 goto out;
1da177e4 1527 }
6d3d5e86
JK
1528 ret = 0;
1529out:
bb7720a0 1530 brelse(bh);
6d3d5e86 1531 return ret;
1da177e4
LT
1532}
1533
647bd61a
CG
1534static int udf_alloc_i_data(struct inode *inode, size_t size)
1535{
48d6d8ff 1536 struct udf_inode_info *iinfo = UDF_I(inode);
382a2287
JK
1537 iinfo->i_data = kmalloc(size, GFP_KERNEL);
1538 if (!iinfo->i_data)
647bd61a 1539 return -ENOMEM;
647bd61a
CG
1540 return 0;
1541}
1542
faa17292 1543static umode_t udf_convert_permissions(struct fileEntry *fe)
1da177e4 1544{
faa17292 1545 umode_t mode;
1da177e4
LT
1546 uint32_t permissions;
1547 uint32_t flags;
1548
1549 permissions = le32_to_cpu(fe->permissions);
1550 flags = le16_to_cpu(fe->icbTag.flags);
1551
6ff6b2b3
FF
1552 mode = ((permissions) & 0007) |
1553 ((permissions >> 2) & 0070) |
1554 ((permissions >> 4) & 0700) |
4b11111a
MS
1555 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1556 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1557 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1da177e4
LT
1558
1559 return mode;
1560}
1561
c3367a1b
SM
1562void udf_update_extra_perms(struct inode *inode, umode_t mode)
1563{
1564 struct udf_inode_info *iinfo = UDF_I(inode);
1565
1566 /*
1567 * UDF 2.01 sec. 3.3.3.3 Note 2:
1568 * In Unix, delete permission tracks write
1569 */
1570 iinfo->i_extraPerms &= ~FE_DELETE_PERMS;
1571 if (mode & 0200)
1572 iinfo->i_extraPerms |= FE_PERM_U_DELETE;
1573 if (mode & 0020)
1574 iinfo->i_extraPerms |= FE_PERM_G_DELETE;
1575 if (mode & 0002)
1576 iinfo->i_extraPerms |= FE_PERM_O_DELETE;
1577}
1578
a9185b41 1579int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1da177e4 1580{
49521de1 1581 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1da177e4
LT
1582}
1583
49521de1 1584static int udf_sync_inode(struct inode *inode)
1da177e4
LT
1585{
1586 return udf_update_inode(inode, 1);
1587}
1588
c3b9cecd 1589static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec64 time)
7ed0fbd7
FF
1590{
1591 if (iinfo->i_crtime.tv_sec > time.tv_sec ||
1592 (iinfo->i_crtime.tv_sec == time.tv_sec &&
1593 iinfo->i_crtime.tv_nsec > time.tv_nsec))
1594 iinfo->i_crtime = time;
1595}
1596
cb00ea35 1597static int udf_update_inode(struct inode *inode, int do_sync)
1da177e4
LT
1598{
1599 struct buffer_head *bh = NULL;
1600 struct fileEntry *fe;
1601 struct extendedFileEntry *efe;
b2527bfa 1602 uint64_t lb_recorded;
1da177e4
LT
1603 uint32_t udfperms;
1604 uint16_t icbflags;
1605 uint16_t crclen;
1da177e4 1606 int err = 0;
6c79e987 1607 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
4b11111a 1608 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
48d6d8ff 1609 struct udf_inode_info *iinfo = UDF_I(inode);
1da177e4 1610
101ee137 1611 bh = sb_getblk(inode->i_sb,
5833ded9 1612 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
cb00ea35 1613 if (!bh) {
aae917cd 1614 udf_debug("getblk failure\n");
0fd2ba36 1615 return -EIO;
1da177e4
LT
1616 }
1617
aae917cd
JK
1618 lock_buffer(bh);
1619 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1da177e4
LT
1620 fe = (struct fileEntry *)bh->b_data;
1621 efe = (struct extendedFileEntry *)bh->b_data;
1622
aae917cd 1623 if (iinfo->i_use) {
1da177e4 1624 struct unallocSpaceEntry *use =
28de7948 1625 (struct unallocSpaceEntry *)bh->b_data;
1da177e4 1626
48d6d8ff 1627 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
4b11111a 1628 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
382a2287 1629 iinfo->i_data, inode->i_sb->s_blocksize -
4b11111a 1630 sizeof(struct unallocSpaceEntry));
aae917cd 1631 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
70f19f58 1632 crclen = sizeof(struct unallocSpaceEntry);
1da177e4 1633
70f19f58 1634 goto finish;
1da177e4
LT
1635 }
1636
4d6660eb 1637 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
0c9850f4 1638 fe->uid = cpu_to_le32(UDF_INVALID_ID);
cb00ea35 1639 else
c2ba138a 1640 fe->uid = cpu_to_le32(i_uid_read(inode));
1da177e4 1641
4d6660eb 1642 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
0c9850f4 1643 fe->gid = cpu_to_le32(UDF_INVALID_ID);
cb00ea35 1644 else
c2ba138a 1645 fe->gid = cpu_to_le32(i_gid_read(inode));
1da177e4 1646
6ff6b2b3
FF
1647 udfperms = ((inode->i_mode & 0007)) |
1648 ((inode->i_mode & 0070) << 2) |
1649 ((inode->i_mode & 0700) << 4);
1da177e4 1650
c3367a1b 1651 udfperms |= iinfo->i_extraPerms;
1da177e4
LT
1652 fe->permissions = cpu_to_le32(udfperms);
1653
8a70ee33 1654 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
1da177e4 1655 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
fc8033a3
JK
1656 else {
1657 if (iinfo->i_hidden)
1658 fe->fileLinkCount = cpu_to_le16(0);
1659 else
1660 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1661 }
1da177e4
LT
1662
1663 fe->informationLength = cpu_to_le64(inode->i_size);
1664
cb00ea35 1665 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
5ca4e4be 1666 struct regid *eid;
28de7948
CG
1667 struct deviceSpec *dsea =
1668 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
cb00ea35 1669 if (!dsea) {
1da177e4 1670 dsea = (struct deviceSpec *)
28de7948
CG
1671 udf_add_extendedattr(inode,
1672 sizeof(struct deviceSpec) +
5ca4e4be 1673 sizeof(struct regid), 12, 0x3);
1da177e4
LT
1674 dsea->attrType = cpu_to_le32(12);
1675 dsea->attrSubtype = 1;
4b11111a
MS
1676 dsea->attrLength = cpu_to_le32(
1677 sizeof(struct deviceSpec) +
5ca4e4be
PE
1678 sizeof(struct regid));
1679 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1da177e4 1680 }
5ca4e4be 1681 eid = (struct regid *)dsea->impUse;
033c9da0 1682 memset(eid, 0, sizeof(*eid));
1da177e4
LT
1683 strcpy(eid->ident, UDF_ID_DEVELOPER);
1684 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1685 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1686 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1687 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1688 }
1689
b2527bfa
SN
1690 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1691 lb_recorded = 0; /* No extents => no blocks! */
1692 else
1693 lb_recorded =
1694 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1695 (blocksize_bits - 9);
1696
48d6d8ff 1697 if (iinfo->i_efe == 0) {
c0b34438 1698 memcpy(bh->b_data + sizeof(struct fileEntry),
382a2287 1699 iinfo->i_data,
cb00ea35 1700 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
b2527bfa 1701 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1da177e4 1702
c3b9cecd
AB
1703 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1704 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1705 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
5ca4e4be 1706 memset(&(fe->impIdent), 0, sizeof(struct regid));
1da177e4
LT
1707 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1708 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1709 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
48d6d8ff
MS
1710 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1711 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1712 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
d5e2cf07 1713 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1da177e4
LT
1714 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1715 crclen = sizeof(struct fileEntry);
cb00ea35 1716 } else {
4b11111a 1717 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
382a2287 1718 iinfo->i_data,
4b11111a
MS
1719 inode->i_sb->s_blocksize -
1720 sizeof(struct extendedFileEntry));
ab9a3a73
SM
1721 efe->objectSize =
1722 cpu_to_le64(inode->i_size + iinfo->i_lenStreams);
b2527bfa 1723 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1da177e4 1724
ab9a3a73
SM
1725 if (iinfo->i_streamdir) {
1726 struct long_ad *icb_lad = &efe->streamDirectoryICB;
1727
1728 icb_lad->extLocation =
1729 cpu_to_lelb(iinfo->i_locStreamdir);
1730 icb_lad->extLength =
1731 cpu_to_le32(inode->i_sb->s_blocksize);
1732 }
1733
c3b9cecd
AB
1734 udf_adjust_time(iinfo, inode->i_atime);
1735 udf_adjust_time(iinfo, inode->i_mtime);
1736 udf_adjust_time(iinfo, inode->i_ctime);
1da177e4 1737
c3b9cecd
AB
1738 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1739 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
56774805 1740 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
c3b9cecd 1741 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1da177e4 1742
033c9da0 1743 memset(&(efe->impIdent), 0, sizeof(efe->impIdent));
1da177e4
LT
1744 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1745 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1746 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
48d6d8ff
MS
1747 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1748 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1749 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
d5e2cf07 1750 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1da177e4
LT
1751 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1752 crclen = sizeof(struct extendedFileEntry);
1753 }
70f19f58
SM
1754
1755finish:
48d6d8ff 1756 if (iinfo->i_strat4096) {
1da177e4
LT
1757 fe->icbTag.strategyType = cpu_to_le16(4096);
1758 fe->icbTag.strategyParameter = cpu_to_le16(1);
1759 fe->icbTag.numEntries = cpu_to_le16(2);
cb00ea35 1760 } else {
1da177e4
LT
1761 fe->icbTag.strategyType = cpu_to_le16(4);
1762 fe->icbTag.numEntries = cpu_to_le16(1);
1763 }
1764
70f19f58
SM
1765 if (iinfo->i_use)
1766 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1767 else if (S_ISDIR(inode->i_mode))
1da177e4
LT
1768 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1769 else if (S_ISREG(inode->i_mode))
1770 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1771 else if (S_ISLNK(inode->i_mode))
1772 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1773 else if (S_ISBLK(inode->i_mode))
1774 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1775 else if (S_ISCHR(inode->i_mode))
1776 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1777 else if (S_ISFIFO(inode->i_mode))
1778 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1779 else if (S_ISSOCK(inode->i_mode))
1780 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1781
48d6d8ff 1782 icbflags = iinfo->i_alloc_type |
28de7948
CG
1783 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1784 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1785 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1786 (le16_to_cpu(fe->icbTag.flags) &
1787 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1788 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1da177e4
LT
1789
1790 fe->icbTag.flags = cpu_to_le16(icbflags);
6c79e987 1791 if (sbi->s_udfrev >= 0x0200)
1da177e4
LT
1792 fe->descTag.descVersion = cpu_to_le16(3);
1793 else
1794 fe->descTag.descVersion = cpu_to_le16(2);
6c79e987 1795 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
4b11111a 1796 fe->descTag.tagLocation = cpu_to_le32(
48d6d8ff 1797 iinfo->i_location.logicalBlockNum);
aae917cd 1798 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1da177e4 1799 fe->descTag.descCRCLength = cpu_to_le16(crclen);
5ca4e4be 1800 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
f845fced 1801 crclen));
3f2587bb 1802 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1da177e4 1803
5833ded9 1804 set_buffer_uptodate(bh);
aae917cd
JK
1805 unlock_buffer(bh);
1806
1da177e4
LT
1807 /* write the data blocks */
1808 mark_buffer_dirty(bh);
cb00ea35 1809 if (do_sync) {
1da177e4 1810 sync_dirty_buffer(bh);
aae917cd 1811 if (buffer_write_io_error(bh)) {
78ace70c
JP
1812 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1813 inode->i_ino);
1da177e4
LT
1814 err = -EIO;
1815 }
1816 }
3bf25cb4 1817 brelse(bh);
28de7948 1818
1da177e4
LT
1819 return err;
1820}
1821
6174c2eb
JK
1822struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1823 bool hidden_inode)
1da177e4
LT
1824{
1825 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1826 struct inode *inode = iget_locked(sb, block);
6d3d5e86 1827 int err;
1da177e4
LT
1828
1829 if (!inode)
6d3d5e86 1830 return ERR_PTR(-ENOMEM);
1da177e4 1831
85a37983
JK
1832 if (!(inode->i_state & I_NEW)) {
1833 if (UDF_I(inode)->i_hidden != hidden_inode) {
1834 iput(inode);
1835 return ERR_PTR(-EFSCORRUPTED);
1836 }
6d3d5e86 1837 return inode;
85a37983 1838 }
1da177e4 1839
6d3d5e86 1840 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
6174c2eb 1841 err = udf_read_inode(inode, hidden_inode);
6d3d5e86
JK
1842 if (err < 0) {
1843 iget_failed(inode);
1844 return ERR_PTR(err);
1da177e4 1845 }
6d3d5e86 1846 unlock_new_inode(inode);
1da177e4
LT
1847
1848 return inode;
1da177e4
LT
1849}
1850
b490bdd6 1851int udf_setup_indirect_aext(struct inode *inode, udf_pblk_t block,
fcea62ba 1852 struct extent_position *epos)
1da177e4 1853{
fcea62ba
JK
1854 struct super_block *sb = inode->i_sb;
1855 struct buffer_head *bh;
1da177e4 1856 struct allocExtDesc *aed;
fcea62ba
JK
1857 struct extent_position nepos;
1858 struct kernel_lb_addr neloc;
1859 int ver, adsize;
1da177e4 1860
fcea62ba
JK
1861 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1862 adsize = sizeof(struct short_ad);
1863 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1864 adsize = sizeof(struct long_ad);
1da177e4 1865 else
4f1b1519 1866 return -EIO;
fcea62ba
JK
1867
1868 neloc.logicalBlockNum = block;
1869 neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1870
101ee137 1871 bh = sb_getblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
fcea62ba
JK
1872 if (!bh)
1873 return -EIO;
1874 lock_buffer(bh);
1875 memset(bh->b_data, 0x00, sb->s_blocksize);
1876 set_buffer_uptodate(bh);
1877 unlock_buffer(bh);
1878 mark_buffer_dirty_inode(bh, inode);
1879
1880 aed = (struct allocExtDesc *)(bh->b_data);
1881 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1882 aed->previousAllocExtLocation =
1883 cpu_to_le32(epos->block.logicalBlockNum);
1884 }
1885 aed->lengthAllocDescs = cpu_to_le32(0);
1886 if (UDF_SB(sb)->s_udfrev >= 0x0200)
1887 ver = 3;
1da177e4 1888 else
fcea62ba
JK
1889 ver = 2;
1890 udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1891 sizeof(struct tag));
1892
1893 nepos.block = neloc;
1894 nepos.offset = sizeof(struct allocExtDesc);
1895 nepos.bh = bh;
1896
1897 /*
1898 * Do we have to copy current last extent to make space for indirect
1899 * one?
1900 */
1901 if (epos->offset + adsize > sb->s_blocksize) {
1902 struct kernel_lb_addr cp_loc;
1903 uint32_t cp_len;
1904 int cp_type;
1905
1906 epos->offset -= adsize;
1907 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1908 cp_len |= ((uint32_t)cp_type) << 30;
1909
1910 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1911 udf_write_aext(inode, epos, &nepos.block,
800552ce 1912 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
fcea62ba
JK
1913 } else {
1914 __udf_add_aext(inode, epos, &nepos.block,
800552ce 1915 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
fcea62ba
JK
1916 }
1917
1918 brelse(epos->bh);
1919 *epos = nepos;
1920
1921 return 0;
1922}
1923
1924/*
1925 * Append extent at the given position - should be the first free one in inode
1926 * / indirect extent. This function assumes there is enough space in the inode
1927 * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1928 */
1929int __udf_add_aext(struct inode *inode, struct extent_position *epos,
1930 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1931{
1932 struct udf_inode_info *iinfo = UDF_I(inode);
1933 struct allocExtDesc *aed;
1934 int adsize;
1da177e4 1935
48d6d8ff 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 1940 else
7e49b6f2 1941 return -EIO;
1da177e4 1942
fcea62ba
JK
1943 if (!epos->bh) {
1944 WARN_ON(iinfo->i_lenAlloc !=
1945 epos->offset - udf_file_entry_alloc_offset(inode));
1946 } else {
1947 aed = (struct allocExtDesc *)epos->bh->b_data;
1948 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
1949 epos->offset - sizeof(struct allocExtDesc));
1950 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
1da177e4
LT
1951 }
1952
7e49b6f2 1953 udf_write_aext(inode, epos, eloc, elen, inc);
1da177e4 1954
cb00ea35 1955 if (!epos->bh) {
48d6d8ff 1956 iinfo->i_lenAlloc += adsize;
1da177e4 1957 mark_inode_dirty(inode);
cb00ea35 1958 } else {
ff116fc8 1959 aed = (struct allocExtDesc *)epos->bh->b_data;
c2104fda 1960 le32_add_cpu(&aed->lengthAllocDescs, adsize);
4b11111a
MS
1961 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1962 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1963 udf_update_tag(epos->bh->b_data,
1964 epos->offset + (inc ? 0 : adsize));
1da177e4 1965 else
4b11111a
MS
1966 udf_update_tag(epos->bh->b_data,
1967 sizeof(struct allocExtDesc));
ff116fc8 1968 mark_buffer_dirty_inode(epos->bh, inode);
1da177e4
LT
1969 }
1970
7e49b6f2 1971 return 0;
1da177e4
LT
1972}
1973
fcea62ba
JK
1974/*
1975 * Append extent at given position - should be the first free one in inode
1976 * / indirect extent. Takes care of allocating and linking indirect blocks.
1977 */
1978int udf_add_aext(struct inode *inode, struct extent_position *epos,
1979 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1980{
1981 int adsize;
1982 struct super_block *sb = inode->i_sb;
1983
1984 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1985 adsize = sizeof(struct short_ad);
1986 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1987 adsize = sizeof(struct long_ad);
1988 else
1989 return -EIO;
1990
1991 if (epos->offset + (2 * adsize) > sb->s_blocksize) {
1992 int err;
b490bdd6 1993 udf_pblk_t new_block;
fcea62ba
JK
1994
1995 new_block = udf_new_block(sb, NULL,
1996 epos->block.partitionReferenceNum,
1997 epos->block.logicalBlockNum, &err);
1998 if (!new_block)
1999 return -ENOSPC;
2000
2001 err = udf_setup_indirect_aext(inode, new_block, epos);
2002 if (err)
2003 return err;
2004 }
2005
2006 return __udf_add_aext(inode, epos, eloc, elen, inc);
2007}
2008
7e49b6f2
JK
2009void udf_write_aext(struct inode *inode, struct extent_position *epos,
2010 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1da177e4
LT
2011{
2012 int adsize;
2013 uint8_t *ptr;
5ca4e4be
PE
2014 struct short_ad *sad;
2015 struct long_ad *lad;
48d6d8ff 2016 struct udf_inode_info *iinfo = UDF_I(inode);
1da177e4 2017
ff116fc8 2018 if (!epos->bh)
382a2287 2019 ptr = iinfo->i_data + epos->offset -
4b11111a 2020 udf_file_entry_alloc_offset(inode) +
48d6d8ff 2021 iinfo->i_lenEAttr;
1da177e4 2022 else
ff116fc8 2023 ptr = epos->bh->b_data + epos->offset;
1da177e4 2024
48d6d8ff 2025 switch (iinfo->i_alloc_type) {
cb00ea35 2026 case ICBTAG_FLAG_AD_SHORT:
5ca4e4be 2027 sad = (struct short_ad *)ptr;
28de7948 2028 sad->extLength = cpu_to_le32(elen);
97e961fd 2029 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
5ca4e4be 2030 adsize = sizeof(struct short_ad);
28de7948 2031 break;
cb00ea35 2032 case ICBTAG_FLAG_AD_LONG:
5ca4e4be 2033 lad = (struct long_ad *)ptr;
28de7948 2034 lad->extLength = cpu_to_le32(elen);
97e961fd 2035 lad->extLocation = cpu_to_lelb(*eloc);
28de7948 2036 memset(lad->impUse, 0x00, sizeof(lad->impUse));
5ca4e4be 2037 adsize = sizeof(struct long_ad);
28de7948 2038 break;
cb00ea35 2039 default:
7e49b6f2 2040 return;
1da177e4
LT
2041 }
2042
cb00ea35 2043 if (epos->bh) {
28de7948 2044 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
6c79e987 2045 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
4b11111a
MS
2046 struct allocExtDesc *aed =
2047 (struct allocExtDesc *)epos->bh->b_data;
ff116fc8 2048 udf_update_tag(epos->bh->b_data,
4b11111a
MS
2049 le32_to_cpu(aed->lengthAllocDescs) +
2050 sizeof(struct allocExtDesc));
1da177e4 2051 }
ff116fc8 2052 mark_buffer_dirty_inode(epos->bh, inode);
28de7948 2053 } else {
1da177e4 2054 mark_inode_dirty(inode);
28de7948 2055 }
1da177e4
LT
2056
2057 if (inc)
ff116fc8 2058 epos->offset += adsize;
1da177e4
LT
2059}
2060
b0918d9f
VN
2061/*
2062 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2063 * someone does some weird stuff.
2064 */
2065#define UDF_MAX_INDIR_EXTS 16
2066
4b11111a 2067int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
5ca4e4be 2068 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
1da177e4
LT
2069{
2070 int8_t etype;
b0918d9f 2071 unsigned int indirections = 0;
1da177e4 2072
ff116fc8 2073 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
800552ce 2074 (EXT_NEXT_EXTENT_ALLOCDESCS >> 30)) {
b490bdd6 2075 udf_pblk_t block;
b0918d9f
VN
2076
2077 if (++indirections > UDF_MAX_INDIR_EXTS) {
2078 udf_err(inode->i_sb,
2079 "too many indirect extents in inode %lu\n",
2080 inode->i_ino);
2081 return -1;
2082 }
2083
ff116fc8
JK
2084 epos->block = *eloc;
2085 epos->offset = sizeof(struct allocExtDesc);
3bf25cb4 2086 brelse(epos->bh);
97e961fd 2087 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
101ee137 2088 epos->bh = sb_bread(inode->i_sb, block);
4b11111a 2089 if (!epos->bh) {
fcbf7637 2090 udf_debug("reading block %u failed!\n", block);
1da177e4
LT
2091 return -1;
2092 }
2093 }
2094
2095 return etype;
2096}
2097
4b11111a 2098int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
5ca4e4be 2099 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
1da177e4
LT
2100{
2101 int alen;
2102 int8_t etype;
2103 uint8_t *ptr;
5ca4e4be
PE
2104 struct short_ad *sad;
2105 struct long_ad *lad;
48d6d8ff 2106 struct udf_inode_info *iinfo = UDF_I(inode);
28de7948 2107
cb00ea35 2108 if (!epos->bh) {
ff116fc8
JK
2109 if (!epos->offset)
2110 epos->offset = udf_file_entry_alloc_offset(inode);
382a2287 2111 ptr = iinfo->i_data + epos->offset -
4b11111a 2112 udf_file_entry_alloc_offset(inode) +
48d6d8ff 2113 iinfo->i_lenEAttr;
4b11111a 2114 alen = udf_file_entry_alloc_offset(inode) +
48d6d8ff 2115 iinfo->i_lenAlloc;
cb00ea35 2116 } else {
ff116fc8
JK
2117 if (!epos->offset)
2118 epos->offset = sizeof(struct allocExtDesc);
2119 ptr = epos->bh->b_data + epos->offset;
28de7948 2120 alen = sizeof(struct allocExtDesc) +
4b11111a
MS
2121 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2122 lengthAllocDescs);
1da177e4
LT
2123 }
2124
48d6d8ff 2125 switch (iinfo->i_alloc_type) {
cb00ea35 2126 case ICBTAG_FLAG_AD_SHORT:
4b11111a
MS
2127 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2128 if (!sad)
28de7948
CG
2129 return -1;
2130 etype = le32_to_cpu(sad->extLength) >> 30;
2131 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
4b11111a 2132 eloc->partitionReferenceNum =
48d6d8ff 2133 iinfo->i_location.partitionReferenceNum;
28de7948
CG
2134 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2135 break;
cb00ea35 2136 case ICBTAG_FLAG_AD_LONG:
4b11111a
MS
2137 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2138 if (!lad)
1da177e4 2139 return -1;
28de7948
CG
2140 etype = le32_to_cpu(lad->extLength) >> 30;
2141 *eloc = lelb_to_cpu(lad->extLocation);
2142 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2143 break;
2144 default:
fcbf7637 2145 udf_debug("alloc_type = %u unsupported\n", iinfo->i_alloc_type);
28de7948 2146 return -1;
1da177e4
LT
2147 }
2148
2149 return etype;
2150}
2151
19fd80de
JK
2152static int udf_insert_aext(struct inode *inode, struct extent_position epos,
2153 struct kernel_lb_addr neloc, uint32_t nelen)
1da177e4 2154{
5ca4e4be 2155 struct kernel_lb_addr oeloc;
1da177e4
LT
2156 uint32_t oelen;
2157 int8_t etype;
19fd80de 2158 int err;
1da177e4 2159
ff116fc8 2160 if (epos.bh)
3bf25cb4 2161 get_bh(epos.bh);
1da177e4 2162
cb00ea35 2163 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
97e961fd 2164 udf_write_aext(inode, &epos, &neloc, nelen, 1);
1da177e4
LT
2165 neloc = oeloc;
2166 nelen = (etype << 30) | oelen;
2167 }
19fd80de 2168 err = udf_add_aext(inode, &epos, &neloc, nelen, 1);
3bf25cb4 2169 brelse(epos.bh);
28de7948 2170
19fd80de 2171 return err;
1da177e4
LT
2172}
2173
6c1e4d06 2174int8_t udf_delete_aext(struct inode *inode, struct extent_position epos)
1da177e4 2175{
ff116fc8
JK
2176 struct extent_position oepos;
2177 int adsize;
1da177e4
LT
2178 int8_t etype;
2179 struct allocExtDesc *aed;
48d6d8ff 2180 struct udf_inode_info *iinfo;
6c1e4d06
JK
2181 struct kernel_lb_addr eloc;
2182 uint32_t elen;
1da177e4 2183
cb00ea35 2184 if (epos.bh) {
3bf25cb4
JK
2185 get_bh(epos.bh);
2186 get_bh(epos.bh);
1da177e4
LT
2187 }
2188
48d6d8ff
MS
2189 iinfo = UDF_I(inode);
2190 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be 2191 adsize = sizeof(struct short_ad);
48d6d8ff 2192 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5ca4e4be 2193 adsize = sizeof(struct long_ad);
1da177e4
LT
2194 else
2195 adsize = 0;
2196
ff116fc8
JK
2197 oepos = epos;
2198 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
1da177e4
LT
2199 return -1;
2200
cb00ea35 2201 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
97e961fd 2202 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
cb00ea35 2203 if (oepos.bh != epos.bh) {
ff116fc8 2204 oepos.block = epos.block;
3bf25cb4
JK
2205 brelse(oepos.bh);
2206 get_bh(epos.bh);
ff116fc8
JK
2207 oepos.bh = epos.bh;
2208 oepos.offset = epos.offset - adsize;
1da177e4
LT
2209 }
2210 }
5ca4e4be 2211 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
1da177e4
LT
2212 elen = 0;
2213
cb00ea35 2214 if (epos.bh != oepos.bh) {
97e961fd
PE
2215 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2216 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2217 udf_write_aext(inode, &oepos, &eloc, elen, 1);
cb00ea35 2218 if (!oepos.bh) {
48d6d8ff 2219 iinfo->i_lenAlloc -= (adsize * 2);
1da177e4 2220 mark_inode_dirty(inode);
cb00ea35 2221 } else {
ff116fc8 2222 aed = (struct allocExtDesc *)oepos.bh->b_data;
c2104fda 2223 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
28de7948 2224 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
6c79e987 2225 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
4b11111a
MS
2226 udf_update_tag(oepos.bh->b_data,
2227 oepos.offset - (2 * adsize));
1da177e4 2228 else
4b11111a
MS
2229 udf_update_tag(oepos.bh->b_data,
2230 sizeof(struct allocExtDesc));
ff116fc8 2231 mark_buffer_dirty_inode(oepos.bh, inode);
1da177e4 2232 }
cb00ea35 2233 } else {
97e961fd 2234 udf_write_aext(inode, &oepos, &eloc, elen, 1);
cb00ea35 2235 if (!oepos.bh) {
48d6d8ff 2236 iinfo->i_lenAlloc -= adsize;
1da177e4 2237 mark_inode_dirty(inode);
cb00ea35 2238 } else {
ff116fc8 2239 aed = (struct allocExtDesc *)oepos.bh->b_data;
c2104fda 2240 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
28de7948 2241 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
6c79e987 2242 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
4b11111a
MS
2243 udf_update_tag(oepos.bh->b_data,
2244 epos.offset - adsize);
1da177e4 2245 else
4b11111a
MS
2246 udf_update_tag(oepos.bh->b_data,
2247 sizeof(struct allocExtDesc));
ff116fc8 2248 mark_buffer_dirty_inode(oepos.bh, inode);
1da177e4
LT
2249 }
2250 }
647bd61a 2251
3bf25cb4
JK
2252 brelse(epos.bh);
2253 brelse(oepos.bh);
28de7948 2254
1da177e4
LT
2255 return (elen >> 30);
2256}
2257
4b11111a 2258int8_t inode_bmap(struct inode *inode, sector_t block,
5ca4e4be 2259 struct extent_position *pos, struct kernel_lb_addr *eloc,
4b11111a 2260 uint32_t *elen, sector_t *offset)
1da177e4 2261{
4b11111a 2262 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
bbc9abd2 2263 loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
1da177e4 2264 int8_t etype;
48d6d8ff 2265 struct udf_inode_info *iinfo;
1da177e4 2266
48d6d8ff 2267 iinfo = UDF_I(inode);
99600051
NJ
2268 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2269 pos->offset = 0;
2270 pos->block = iinfo->i_location;
2271 pos->bh = NULL;
2272 }
1da177e4 2273 *elen = 0;
cb00ea35 2274 do {
4b11111a
MS
2275 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2276 if (etype == -1) {
2277 *offset = (bcount - lbcount) >> blocksize_bits;
48d6d8ff 2278 iinfo->i_lenExtents = lbcount;
1da177e4
LT
2279 return -1;
2280 }
2281 lbcount += *elen;
2282 } while (lbcount <= bcount);
99600051 2283 /* update extent cache */
b31c9ed9 2284 udf_update_extent_cache(inode, lbcount - *elen, pos);
4b11111a 2285 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
1da177e4
LT
2286
2287 return etype;
2288}