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