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