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