projects/linux·blob master · refs

2535 lines · 70.5 KiB

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