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