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