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