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