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