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