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