xref: /openbmc/linux/fs/udf/inode.c (revision 85a37983ec69cc9fcd188bc37c4de15ee326355a)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * inode.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * PURPOSE
51da177e4SLinus Torvalds  *  Inode handling routines for the OSTA-UDF(tm) filesystem.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * COPYRIGHT
81da177e4SLinus Torvalds  *  This file is distributed under the terms of the GNU General Public
91da177e4SLinus Torvalds  *  License (GPL). Copies of the GPL can be obtained from:
101da177e4SLinus Torvalds  *    ftp://prep.ai.mit.edu/pub/gnu/GPL
111da177e4SLinus Torvalds  *  Each contributing author retains all rights to their own work.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  *  (C) 1998 Dave Boynton
141da177e4SLinus Torvalds  *  (C) 1998-2004 Ben Fennema
151da177e4SLinus Torvalds  *  (C) 1999-2000 Stelias Computing Inc
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * HISTORY
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  *  10/04/98 dgb  Added rudimentary directory functions
201da177e4SLinus Torvalds  *  10/07/98      Fully working udf_block_map! It works!
211da177e4SLinus Torvalds  *  11/25/98      bmap altered to better support extents
224b11111aSMarcin Slusarz  *  12/06/98 blf  partition support in udf_iget, udf_block_map
234b11111aSMarcin Slusarz  *                and udf_read_inode
241da177e4SLinus Torvalds  *  12/12/98      rewrote udf_block_map to handle next extents and descs across
251da177e4SLinus Torvalds  *                block boundaries (which is not actually allowed)
261da177e4SLinus Torvalds  *  12/20/98      added support for strategy 4096
271da177e4SLinus Torvalds  *  03/07/99      rewrote udf_block_map (again)
281da177e4SLinus Torvalds  *                New funcs, inode_bmap, udf_next_aext
291da177e4SLinus Torvalds  *  04/19/99      Support for writing device EA's for major/minor #
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include "udfdecl.h"
331da177e4SLinus Torvalds #include <linux/mm.h>
341da177e4SLinus Torvalds #include <linux/module.h>
351da177e4SLinus Torvalds #include <linux/pagemap.h>
361da177e4SLinus Torvalds #include <linux/writeback.h>
371da177e4SLinus Torvalds #include <linux/slab.h>
38f845fcedSBob Copeland #include <linux/crc-itu-t.h>
39bc112323SNamjae Jeon #include <linux/mpage.h>
40e2e40f2cSChristoph Hellwig #include <linux/uio.h>
412f8b5444SChristoph Hellwig #include <linux/bio.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include "udf_i.h"
441da177e4SLinus Torvalds #include "udf_sb.h"
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #define EXTENT_MERGE_SIZE 5
471da177e4SLinus Torvalds 
48c3367a1bSSteven J. Magnani #define FE_MAPPED_PERMS	(FE_PERM_U_READ | FE_PERM_U_WRITE | FE_PERM_U_EXEC | \
49c3367a1bSSteven J. Magnani 			 FE_PERM_G_READ | FE_PERM_G_WRITE | FE_PERM_G_EXEC | \
50c3367a1bSSteven J. Magnani 			 FE_PERM_O_READ | FE_PERM_O_WRITE | FE_PERM_O_EXEC)
51c3367a1bSSteven J. Magnani 
52c3367a1bSSteven J. Magnani #define FE_DELETE_PERMS	(FE_PERM_U_DELETE | FE_PERM_G_DELETE | \
53c3367a1bSSteven J. Magnani 			 FE_PERM_O_DELETE)
54c3367a1bSSteven J. Magnani 
55faa17292SAl Viro static umode_t udf_convert_permissions(struct fileEntry *);
561da177e4SLinus Torvalds static int udf_update_inode(struct inode *, int);
5749521de1SJan Kara static int udf_sync_inode(struct inode *inode);
58647bd61aSCyrill Gorcunov static int udf_alloc_i_data(struct inode *inode, size_t size);
597b0b0933SJan Kara static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
6019fd80deSJan Kara static int udf_insert_aext(struct inode *, struct extent_position,
615ca4e4beSPekka Enberg 			   struct kernel_lb_addr, uint32_t);
62b490bdd6SSteve Magnani static void udf_split_extents(struct inode *, int *, int, udf_pblk_t,
633cc6f844SFabian Frederick 			      struct kernel_long_ad *, int *);
641da177e4SLinus Torvalds static void udf_prealloc_extents(struct inode *, int, int,
653cc6f844SFabian Frederick 				 struct kernel_long_ad *, int *);
663cc6f844SFabian Frederick static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *);
6719fd80deSJan Kara static int udf_update_extents(struct inode *, struct kernel_long_ad *, int,
683cc6f844SFabian Frederick 			      int, struct extent_position *);
691da177e4SLinus Torvalds static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
701da177e4SLinus Torvalds 
7199600051SNamjae Jeon static void __udf_clear_extent_cache(struct inode *inode)
7299600051SNamjae Jeon {
7399600051SNamjae Jeon 	struct udf_inode_info *iinfo = UDF_I(inode);
7499600051SNamjae Jeon 
7599600051SNamjae Jeon 	if (iinfo->cached_extent.lstart != -1) {
7699600051SNamjae Jeon 		brelse(iinfo->cached_extent.epos.bh);
7799600051SNamjae Jeon 		iinfo->cached_extent.lstart = -1;
7899600051SNamjae Jeon 	}
7999600051SNamjae Jeon }
8099600051SNamjae Jeon 
8199600051SNamjae Jeon /* Invalidate extent cache */
8299600051SNamjae Jeon static void udf_clear_extent_cache(struct inode *inode)
8399600051SNamjae Jeon {
8499600051SNamjae Jeon 	struct udf_inode_info *iinfo = UDF_I(inode);
8599600051SNamjae Jeon 
8699600051SNamjae Jeon 	spin_lock(&iinfo->i_extent_cache_lock);
8799600051SNamjae Jeon 	__udf_clear_extent_cache(inode);
8899600051SNamjae Jeon 	spin_unlock(&iinfo->i_extent_cache_lock);
8999600051SNamjae Jeon }
9099600051SNamjae Jeon 
9199600051SNamjae Jeon /* Return contents of extent cache */
9299600051SNamjae Jeon static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
9399600051SNamjae Jeon 				 loff_t *lbcount, struct extent_position *pos)
9499600051SNamjae Jeon {
9599600051SNamjae Jeon 	struct udf_inode_info *iinfo = UDF_I(inode);
9699600051SNamjae Jeon 	int ret = 0;
9799600051SNamjae Jeon 
9899600051SNamjae Jeon 	spin_lock(&iinfo->i_extent_cache_lock);
9999600051SNamjae Jeon 	if ((iinfo->cached_extent.lstart <= bcount) &&
10099600051SNamjae Jeon 	    (iinfo->cached_extent.lstart != -1)) {
10199600051SNamjae Jeon 		/* Cache hit */
10299600051SNamjae Jeon 		*lbcount = iinfo->cached_extent.lstart;
10399600051SNamjae Jeon 		memcpy(pos, &iinfo->cached_extent.epos,
10499600051SNamjae Jeon 		       sizeof(struct extent_position));
10599600051SNamjae Jeon 		if (pos->bh)
10699600051SNamjae Jeon 			get_bh(pos->bh);
10799600051SNamjae Jeon 		ret = 1;
10899600051SNamjae Jeon 	}
10999600051SNamjae Jeon 	spin_unlock(&iinfo->i_extent_cache_lock);
11099600051SNamjae Jeon 	return ret;
11199600051SNamjae Jeon }
11299600051SNamjae Jeon 
11399600051SNamjae Jeon /* Add extent to extent cache */
11499600051SNamjae Jeon static void udf_update_extent_cache(struct inode *inode, loff_t estart,
115b31c9ed9SFabian Frederick 				    struct extent_position *pos)
11699600051SNamjae Jeon {
11799600051SNamjae Jeon 	struct udf_inode_info *iinfo = UDF_I(inode);
11899600051SNamjae Jeon 
11999600051SNamjae Jeon 	spin_lock(&iinfo->i_extent_cache_lock);
12099600051SNamjae Jeon 	/* Invalidate previously cached extent */
12199600051SNamjae Jeon 	__udf_clear_extent_cache(inode);
12299600051SNamjae Jeon 	if (pos->bh)
12399600051SNamjae Jeon 		get_bh(pos->bh);
124033c9da0SMarkus Elfring 	memcpy(&iinfo->cached_extent.epos, pos, sizeof(*pos));
12599600051SNamjae Jeon 	iinfo->cached_extent.lstart = estart;
12699600051SNamjae Jeon 	switch (iinfo->i_alloc_type) {
12799600051SNamjae Jeon 	case ICBTAG_FLAG_AD_SHORT:
128b31c9ed9SFabian Frederick 		iinfo->cached_extent.epos.offset -= sizeof(struct short_ad);
12999600051SNamjae Jeon 		break;
13099600051SNamjae Jeon 	case ICBTAG_FLAG_AD_LONG:
131b31c9ed9SFabian Frederick 		iinfo->cached_extent.epos.offset -= sizeof(struct long_ad);
132b31c9ed9SFabian Frederick 		break;
13399600051SNamjae Jeon 	}
13499600051SNamjae Jeon 	spin_unlock(&iinfo->i_extent_cache_lock);
13599600051SNamjae Jeon }
136b1e32126SChristoph Hellwig 
1373aac2b62SAl Viro void udf_evict_inode(struct inode *inode)
1381da177e4SLinus Torvalds {
1393aac2b62SAl Viro 	struct udf_inode_info *iinfo = UDF_I(inode);
1403aac2b62SAl Viro 	int want_delete = 0;
1413aac2b62SAl Viro 
142044e2e26SJan Kara 	if (!is_bad_inode(inode)) {
143044e2e26SJan Kara 		if (!inode->i_nlink) {
1443aac2b62SAl Viro 			want_delete = 1;
1457e49b6f2SJan Kara 			udf_setsize(inode, 0);
1461da177e4SLinus Torvalds 			udf_update_inode(inode, IS_SYNC(inode));
14791b0abe3SJohannes Weiner 		}
1482c948b3fSJan Kara 		if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
1492c948b3fSJan Kara 		    inode->i_size != iinfo->i_lenExtents) {
150044e2e26SJan Kara 			udf_warn(inode->i_sb,
151044e2e26SJan Kara 				 "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
15278ace70cSJoe Perches 				 inode->i_ino, inode->i_mode,
1532c948b3fSJan Kara 				 (unsigned long long)inode->i_size,
1542c948b3fSJan Kara 				 (unsigned long long)iinfo->i_lenExtents);
1551da177e4SLinus Torvalds 		}
156044e2e26SJan Kara 	}
157044e2e26SJan Kara 	truncate_inode_pages_final(&inode->i_data);
158044e2e26SJan Kara 	invalidate_inode_buffers(inode);
159044e2e26SJan Kara 	clear_inode(inode);
160382a2287SJan Kara 	kfree(iinfo->i_data);
161382a2287SJan Kara 	iinfo->i_data = NULL;
16299600051SNamjae Jeon 	udf_clear_extent_cache(inode);
1633aac2b62SAl Viro 	if (want_delete) {
1643aac2b62SAl Viro 		udf_free_inode(inode);
1653aac2b62SAl Viro 	}
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
1685eec54fcSIan Abbott static void udf_write_failed(struct address_space *mapping, loff_t to)
1695eec54fcSIan Abbott {
1705eec54fcSIan Abbott 	struct inode *inode = mapping->host;
1715eec54fcSIan Abbott 	struct udf_inode_info *iinfo = UDF_I(inode);
1725eec54fcSIan Abbott 	loff_t isize = inode->i_size;
1735eec54fcSIan Abbott 
1745eec54fcSIan Abbott 	if (to > isize) {
1757caef267SKirill A. Shutemov 		truncate_pagecache(inode, isize);
1765eec54fcSIan Abbott 		if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1775eec54fcSIan Abbott 			down_write(&iinfo->i_data_sem);
17899600051SNamjae Jeon 			udf_clear_extent_cache(inode);
1795eec54fcSIan Abbott 			udf_truncate_extents(inode);
1805eec54fcSIan Abbott 			up_write(&iinfo->i_data_sem);
1815eec54fcSIan Abbott 		}
1825eec54fcSIan Abbott 	}
1835eec54fcSIan Abbott }
1845eec54fcSIan Abbott 
185378b8e1aSNamjae Jeon static int udf_writepages(struct address_space *mapping,
186378b8e1aSNamjae Jeon 			struct writeback_control *wbc)
187378b8e1aSNamjae Jeon {
188378b8e1aSNamjae Jeon 	return mpage_writepages(mapping, wbc, udf_get_block);
189378b8e1aSNamjae Jeon }
190378b8e1aSNamjae Jeon 
191f132ab7dSMatthew Wilcox (Oracle) static int udf_read_folio(struct file *file, struct folio *folio)
1921da177e4SLinus Torvalds {
193f132ab7dSMatthew Wilcox (Oracle) 	return mpage_read_folio(folio, udf_get_block);
194bc112323SNamjae Jeon }
195bc112323SNamjae Jeon 
196d4388340SMatthew Wilcox (Oracle) static void udf_readahead(struct readahead_control *rac)
197bc112323SNamjae Jeon {
198d4388340SMatthew Wilcox (Oracle) 	mpage_readahead(rac, udf_get_block);
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds 
201be021ee4SNick Piggin static int udf_write_begin(struct file *file, struct address_space *mapping,
2029d6b0cd7SMatthew Wilcox (Oracle) 			loff_t pos, unsigned len,
203be021ee4SNick Piggin 			struct page **pagep, void **fsdata)
2041da177e4SLinus Torvalds {
205155130a4SChristoph Hellwig 	int ret;
206155130a4SChristoph Hellwig 
207b3992d1eSMatthew Wilcox (Oracle) 	ret = block_write_begin(mapping, pos, len, pagep, udf_get_block);
2085eec54fcSIan Abbott 	if (unlikely(ret))
2095eec54fcSIan Abbott 		udf_write_failed(mapping, pos + len);
2105eec54fcSIan Abbott 	return ret;
2115eec54fcSIan Abbott }
2125eec54fcSIan Abbott 
213c8b8e32dSChristoph Hellwig static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2145eec54fcSIan Abbott {
2155eec54fcSIan Abbott 	struct file *file = iocb->ki_filp;
2165eec54fcSIan Abbott 	struct address_space *mapping = file->f_mapping;
2177e49b6f2SJan Kara 	struct inode *inode = mapping->host;
218a6cbcd4aSAl Viro 	size_t count = iov_iter_count(iter);
2195eec54fcSIan Abbott 	ssize_t ret;
2207e49b6f2SJan Kara 
221c8b8e32dSChristoph Hellwig 	ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
2226f673763SOmar Sandoval 	if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
223c8b8e32dSChristoph Hellwig 		udf_write_failed(mapping, iocb->ki_pos + count);
224155130a4SChristoph Hellwig 	return ret;
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds static sector_t udf_bmap(struct address_space *mapping, sector_t block)
2281da177e4SLinus Torvalds {
2291da177e4SLinus Torvalds 	return generic_block_bmap(mapping, block, udf_get_block);
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
232f5e54d6eSChristoph Hellwig const struct address_space_operations udf_aops = {
233e621900aSMatthew Wilcox (Oracle) 	.dirty_folio	= block_dirty_folio,
2347ba13abbSMatthew Wilcox (Oracle) 	.invalidate_folio = block_invalidate_folio,
235f132ab7dSMatthew Wilcox (Oracle) 	.read_folio	= udf_read_folio,
236d4388340SMatthew Wilcox (Oracle) 	.readahead	= udf_readahead,
237378b8e1aSNamjae Jeon 	.writepages	= udf_writepages,
238be021ee4SNick Piggin 	.write_begin	= udf_write_begin,
239be021ee4SNick Piggin 	.write_end	= generic_write_end,
2405eec54fcSIan Abbott 	.direct_IO	= udf_direct_IO,
2411da177e4SLinus Torvalds 	.bmap		= udf_bmap,
24236273e5bSChristoph Hellwig 	.migrate_folio	= buffer_migrate_folio,
2431da177e4SLinus Torvalds };
2441da177e4SLinus Torvalds 
245d2eb8c35SJan Kara /*
246d2eb8c35SJan Kara  * Expand file stored in ICB to a normal one-block-file
247d2eb8c35SJan Kara  *
248d2eb8c35SJan Kara  * This function requires i_data_sem for writing and releases it.
249d2eb8c35SJan Kara  * This function requires i_mutex held
250d2eb8c35SJan Kara  */
2517e49b6f2SJan Kara int udf_expand_file_adinicb(struct inode *inode)
2521da177e4SLinus Torvalds {
2531da177e4SLinus Torvalds 	struct page *page;
2541da177e4SLinus Torvalds 	char *kaddr;
25548d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
2567e49b6f2SJan Kara 	int err;
2571da177e4SLinus Torvalds 
2585955102cSAl Viro 	WARN_ON_ONCE(!inode_is_locked(inode));
25948d6d8ffSMarcin Slusarz 	if (!iinfo->i_lenAlloc) {
2601da177e4SLinus Torvalds 		if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
26148d6d8ffSMarcin Slusarz 			iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
2621da177e4SLinus Torvalds 		else
26348d6d8ffSMarcin Slusarz 			iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
2647e49b6f2SJan Kara 		/* from now on we have normal address_space methods */
2657e49b6f2SJan Kara 		inode->i_data.a_ops = &udf_aops;
266d2eb8c35SJan Kara 		up_write(&iinfo->i_data_sem);
2671da177e4SLinus Torvalds 		mark_inode_dirty(inode);
2687e49b6f2SJan Kara 		return 0;
2691da177e4SLinus Torvalds 	}
270d2eb8c35SJan Kara 	/*
271d2eb8c35SJan Kara 	 * Release i_data_sem so that we can lock a page - page lock ranks
272d2eb8c35SJan Kara 	 * above i_data_sem. i_mutex still protects us against file changes.
273d2eb8c35SJan Kara 	 */
274d2eb8c35SJan Kara 	up_write(&iinfo->i_data_sem);
2751da177e4SLinus Torvalds 
2767e49b6f2SJan Kara 	page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
2777e49b6f2SJan Kara 	if (!page)
2787e49b6f2SJan Kara 		return -ENOMEM;
279cd7619d6SMatt Mackall 
280cb00ea35SCyrill Gorcunov 	if (!PageUptodate(page)) {
2815c26eac4SFabian Frederick 		kaddr = kmap_atomic(page);
28248d6d8ffSMarcin Slusarz 		memset(kaddr + iinfo->i_lenAlloc, 0x00,
28309cbfeafSKirill A. Shutemov 		       PAGE_SIZE - iinfo->i_lenAlloc);
284382a2287SJan Kara 		memcpy(kaddr, iinfo->i_data + iinfo->i_lenEAttr,
28548d6d8ffSMarcin Slusarz 			iinfo->i_lenAlloc);
2861da177e4SLinus Torvalds 		flush_dcache_page(page);
2871da177e4SLinus Torvalds 		SetPageUptodate(page);
2885c26eac4SFabian Frederick 		kunmap_atomic(kaddr);
2891da177e4SLinus Torvalds 	}
290d2eb8c35SJan Kara 	down_write(&iinfo->i_data_sem);
291382a2287SJan Kara 	memset(iinfo->i_data + iinfo->i_lenEAttr, 0x00,
29248d6d8ffSMarcin Slusarz 	       iinfo->i_lenAlloc);
29348d6d8ffSMarcin Slusarz 	iinfo->i_lenAlloc = 0;
2941da177e4SLinus Torvalds 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
29548d6d8ffSMarcin Slusarz 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
2961da177e4SLinus Torvalds 	else
29748d6d8ffSMarcin Slusarz 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
2987e49b6f2SJan Kara 	/* from now on we have normal address_space methods */
2997e49b6f2SJan Kara 	inode->i_data.a_ops = &udf_aops;
3007fc3b7c2SJan Kara 	set_page_dirty(page);
3017fc3b7c2SJan Kara 	unlock_page(page);
302d2eb8c35SJan Kara 	up_write(&iinfo->i_data_sem);
3037fc3b7c2SJan Kara 	err = filemap_fdatawrite(inode->i_mapping);
3047e49b6f2SJan Kara 	if (err) {
3057e49b6f2SJan Kara 		/* Restore everything back so that we don't lose data... */
3067e49b6f2SJan Kara 		lock_page(page);
307d2eb8c35SJan Kara 		down_write(&iinfo->i_data_sem);
3085c26eac4SFabian Frederick 		kaddr = kmap_atomic(page);
309382a2287SJan Kara 		memcpy(iinfo->i_data + iinfo->i_lenEAttr, kaddr, inode->i_size);
3105c26eac4SFabian Frederick 		kunmap_atomic(kaddr);
3117e49b6f2SJan Kara 		unlock_page(page);
3127e49b6f2SJan Kara 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
3137e49b6f2SJan Kara 		inode->i_data.a_ops = &udf_adinicb_aops;
314ea856919SJan Kara 		iinfo->i_lenAlloc = inode->i_size;
315d2eb8c35SJan Kara 		up_write(&iinfo->i_data_sem);
3167e49b6f2SJan Kara 	}
31709cbfeafSKirill A. Shutemov 	put_page(page);
3181da177e4SLinus Torvalds 	mark_inode_dirty(inode);
3197e49b6f2SJan Kara 
3207e49b6f2SJan Kara 	return err;
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
323cb00ea35SCyrill Gorcunov static int udf_get_block(struct inode *inode, sector_t block,
324cb00ea35SCyrill Gorcunov 			 struct buffer_head *bh_result, int create)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	int err, new;
3271ed16171SMarcin Slusarz 	sector_t phys = 0;
32848d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo;
3291da177e4SLinus Torvalds 
330cb00ea35SCyrill Gorcunov 	if (!create) {
3311da177e4SLinus Torvalds 		phys = udf_block_map(inode, block);
3321da177e4SLinus Torvalds 		if (phys)
3331da177e4SLinus Torvalds 			map_bh(bh_result, inode->i_sb, phys);
3341da177e4SLinus Torvalds 		return 0;
3351da177e4SLinus Torvalds 	}
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	err = -EIO;
3381da177e4SLinus Torvalds 	new = 0;
33948d6d8ffSMarcin Slusarz 	iinfo = UDF_I(inode);
3404d0fb621SAlessio Igor Bogani 
3414d0fb621SAlessio Igor Bogani 	down_write(&iinfo->i_data_sem);
34248d6d8ffSMarcin Slusarz 	if (block == iinfo->i_next_alloc_block + 1) {
34348d6d8ffSMarcin Slusarz 		iinfo->i_next_alloc_block++;
34448d6d8ffSMarcin Slusarz 		iinfo->i_next_alloc_goal++;
3451da177e4SLinus Torvalds 	}
3461da177e4SLinus Torvalds 
34716d05565SJan Kara 	/*
34816d05565SJan Kara 	 * Block beyond EOF and prealloc extents? Just discard preallocation
34916d05565SJan Kara 	 * as it is not useful and complicates things.
35016d05565SJan Kara 	 */
35116d05565SJan Kara 	if (((loff_t)block) << inode->i_blkbits > iinfo->i_lenExtents)
35216d05565SJan Kara 		udf_discard_prealloc(inode);
35399600051SNamjae Jeon 	udf_clear_extent_cache(inode);
3547b0b0933SJan Kara 	phys = inode_getblk(inode, block, &err, &new);
3557b0b0933SJan Kara 	if (!phys)
3561da177e4SLinus Torvalds 		goto abort;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	if (new)
3591da177e4SLinus Torvalds 		set_buffer_new(bh_result);
3601da177e4SLinus Torvalds 	map_bh(bh_result, inode->i_sb, phys);
36128de7948SCyrill Gorcunov 
3621da177e4SLinus Torvalds abort:
3634d0fb621SAlessio Igor Bogani 	up_write(&iinfo->i_data_sem);
3641da177e4SLinus Torvalds 	return err;
3651da177e4SLinus Torvalds }
3661da177e4SLinus Torvalds 
367b490bdd6SSteve Magnani static struct buffer_head *udf_getblk(struct inode *inode, udf_pblk_t block,
368cb00ea35SCyrill Gorcunov 				      int create, int *err)
3691da177e4SLinus Torvalds {
37028de7948SCyrill Gorcunov 	struct buffer_head *bh;
3711da177e4SLinus Torvalds 	struct buffer_head dummy;
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	dummy.b_state = 0;
3741da177e4SLinus Torvalds 	dummy.b_blocknr = -1000;
3751da177e4SLinus Torvalds 	*err = udf_get_block(inode, block, &dummy, create);
376cb00ea35SCyrill Gorcunov 	if (!*err && buffer_mapped(&dummy)) {
3771da177e4SLinus Torvalds 		bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
378cb00ea35SCyrill Gorcunov 		if (buffer_new(&dummy)) {
3791da177e4SLinus Torvalds 			lock_buffer(bh);
3801da177e4SLinus Torvalds 			memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
3811da177e4SLinus Torvalds 			set_buffer_uptodate(bh);
3821da177e4SLinus Torvalds 			unlock_buffer(bh);
3831da177e4SLinus Torvalds 			mark_buffer_dirty_inode(bh, inode);
3841da177e4SLinus Torvalds 		}
3851da177e4SLinus Torvalds 		return bh;
3861da177e4SLinus Torvalds 	}
38728de7948SCyrill Gorcunov 
3881da177e4SLinus Torvalds 	return NULL;
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
391fa33cdbfSSteven J. Magnani /* Extend the file with new blocks totaling 'new_block_bytes',
392fa33cdbfSSteven J. Magnani  * return the number of extents added
393fa33cdbfSSteven J. Magnani  */
3947e49b6f2SJan Kara static int udf_do_extend_file(struct inode *inode,
3957e49b6f2SJan Kara 			      struct extent_position *last_pos,
3967e49b6f2SJan Kara 			      struct kernel_long_ad *last_ext,
397fa33cdbfSSteven J. Magnani 			      loff_t new_block_bytes)
39831170b6aSJan Kara {
399fa33cdbfSSteven J. Magnani 	uint32_t add;
40031170b6aSJan Kara 	int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
40131170b6aSJan Kara 	struct super_block *sb = inode->i_sb;
40248d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo;
4037e49b6f2SJan Kara 	int err;
40431170b6aSJan Kara 
40531170b6aSJan Kara 	/* The previous extent is fake and we should not extend by anything
40631170b6aSJan Kara 	 * - there's nothing to do... */
407fa33cdbfSSteven J. Magnani 	if (!new_block_bytes && fake)
40831170b6aSJan Kara 		return 0;
40928de7948SCyrill Gorcunov 
41048d6d8ffSMarcin Slusarz 	iinfo = UDF_I(inode);
41131170b6aSJan Kara 	/* Round the last extent up to a multiple of block size */
41231170b6aSJan Kara 	if (last_ext->extLength & (sb->s_blocksize - 1)) {
41331170b6aSJan Kara 		last_ext->extLength =
41431170b6aSJan Kara 			(last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
41531170b6aSJan Kara 			(((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
41631170b6aSJan Kara 			  sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
41748d6d8ffSMarcin Slusarz 		iinfo->i_lenExtents =
41848d6d8ffSMarcin Slusarz 			(iinfo->i_lenExtents + sb->s_blocksize - 1) &
41931170b6aSJan Kara 			~(sb->s_blocksize - 1);
42031170b6aSJan Kara 	}
42128de7948SCyrill Gorcunov 
422e57191a8SJan Kara 	add = 0;
42331170b6aSJan Kara 	/* Can we merge with the previous extent? */
4244b11111aSMarcin Slusarz 	if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
4254b11111aSMarcin Slusarz 					EXT_NOT_RECORDED_NOT_ALLOCATED) {
426fa33cdbfSSteven J. Magnani 		add = (1 << 30) - sb->s_blocksize -
427fa33cdbfSSteven J. Magnani 			(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
428fa33cdbfSSteven J. Magnani 		if (add > new_block_bytes)
429fa33cdbfSSteven J. Magnani 			add = new_block_bytes;
430fa33cdbfSSteven J. Magnani 		new_block_bytes -= add;
431fa33cdbfSSteven J. Magnani 		last_ext->extLength += add;
43231170b6aSJan Kara 	}
43331170b6aSJan Kara 
43431170b6aSJan Kara 	if (fake) {
43570bfb3a8SJan Kara 		err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
43631170b6aSJan Kara 				   last_ext->extLength, 1);
43770bfb3a8SJan Kara 		if (err < 0)
43870bfb3a8SJan Kara 			goto out_err;
43931170b6aSJan Kara 		count++;
4406c371578SJan Kara 	} else {
4416c371578SJan Kara 		struct kernel_lb_addr tmploc;
4426c371578SJan Kara 		uint32_t tmplen;
4436c371578SJan Kara 
44497e961fdSPekka Enberg 		udf_write_aext(inode, last_pos, &last_ext->extLocation,
4454b11111aSMarcin Slusarz 				last_ext->extLength, 1);
44663c9e47aSSteven J. Magnani 
4476c371578SJan Kara 		/*
44863c9e47aSSteven J. Magnani 		 * We've rewritten the last extent. If we are going to add
44963c9e47aSSteven J. Magnani 		 * more extents, we may need to enter possible following
45063c9e47aSSteven J. Magnani 		 * empty indirect extent.
4516c371578SJan Kara 		 */
45216d05565SJan Kara 		if (new_block_bytes)
4536c371578SJan Kara 			udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
4546c371578SJan Kara 	}
455e57191a8SJan Kara 	iinfo->i_lenExtents += add;
45628de7948SCyrill Gorcunov 
45731170b6aSJan Kara 	/* Managed to do everything necessary? */
458fa33cdbfSSteven J. Magnani 	if (!new_block_bytes)
45931170b6aSJan Kara 		goto out;
46031170b6aSJan Kara 
46131170b6aSJan Kara 	/* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
46231170b6aSJan Kara 	last_ext->extLocation.logicalBlockNum = 0;
46331170b6aSJan Kara 	last_ext->extLocation.partitionReferenceNum = 0;
464fa33cdbfSSteven J. Magnani 	add = (1 << 30) - sb->s_blocksize;
465fa33cdbfSSteven J. Magnani 	last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add;
46628de7948SCyrill Gorcunov 
46731170b6aSJan Kara 	/* Create enough extents to cover the whole hole */
468fa33cdbfSSteven J. Magnani 	while (new_block_bytes > add) {
469fa33cdbfSSteven J. Magnani 		new_block_bytes -= add;
4707e49b6f2SJan Kara 		err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
4717e49b6f2SJan Kara 				   last_ext->extLength, 1);
4727e49b6f2SJan Kara 		if (err)
47370bfb3a8SJan Kara 			goto out_err;
474e57191a8SJan Kara 		iinfo->i_lenExtents += add;
47531170b6aSJan Kara 		count++;
47631170b6aSJan Kara 	}
477fa33cdbfSSteven J. Magnani 	if (new_block_bytes) {
47831170b6aSJan Kara 		last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
479fa33cdbfSSteven J. Magnani 			new_block_bytes;
4807e49b6f2SJan Kara 		err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
4817e49b6f2SJan Kara 				   last_ext->extLength, 1);
4827e49b6f2SJan Kara 		if (err)
48370bfb3a8SJan Kara 			goto out_err;
484e57191a8SJan Kara 		iinfo->i_lenExtents += new_block_bytes;
48531170b6aSJan Kara 		count++;
48631170b6aSJan Kara 	}
48728de7948SCyrill Gorcunov 
48831170b6aSJan Kara out:
48931170b6aSJan Kara 	/* last_pos should point to the last written extent... */
49048d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
4915ca4e4beSPekka Enberg 		last_pos->offset -= sizeof(struct short_ad);
49248d6d8ffSMarcin Slusarz 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
4935ca4e4beSPekka Enberg 		last_pos->offset -= sizeof(struct long_ad);
49431170b6aSJan Kara 	else
4957e49b6f2SJan Kara 		return -EIO;
49628de7948SCyrill Gorcunov 
49731170b6aSJan Kara 	return count;
49870bfb3a8SJan Kara out_err:
49970bfb3a8SJan Kara 	/* Remove extents we've created so far */
50070bfb3a8SJan Kara 	udf_clear_extent_cache(inode);
50170bfb3a8SJan Kara 	udf_truncate_extents(inode);
50270bfb3a8SJan Kara 	return err;
50331170b6aSJan Kara }
50431170b6aSJan Kara 
505fa33cdbfSSteven J. Magnani /* Extend the final block of the file to final_block_len bytes */
506fa33cdbfSSteven J. Magnani static void udf_do_extend_final_block(struct inode *inode,
507fa33cdbfSSteven J. Magnani 				      struct extent_position *last_pos,
508fa33cdbfSSteven J. Magnani 				      struct kernel_long_ad *last_ext,
5091f3868f0SJan Kara 				      uint32_t new_elen)
510fa33cdbfSSteven J. Magnani {
511fa33cdbfSSteven J. Magnani 	uint32_t added_bytes;
512fa33cdbfSSteven J. Magnani 
5131f3868f0SJan Kara 	/*
5141f3868f0SJan Kara 	 * Extent already large enough? It may be already rounded up to block
5151f3868f0SJan Kara 	 * size...
5161f3868f0SJan Kara 	 */
5171f3868f0SJan Kara 	if (new_elen <= (last_ext->extLength & UDF_EXTENT_LENGTH_MASK))
5181f3868f0SJan Kara 		return;
51983c7423dSJan Kara 	added_bytes = new_elen - (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
520fa33cdbfSSteven J. Magnani 	last_ext->extLength += added_bytes;
521fa33cdbfSSteven J. Magnani 	UDF_I(inode)->i_lenExtents += added_bytes;
522fa33cdbfSSteven J. Magnani 
523fa33cdbfSSteven J. Magnani 	udf_write_aext(inode, last_pos, &last_ext->extLocation,
524fa33cdbfSSteven J. Magnani 			last_ext->extLength, 1);
525fa33cdbfSSteven J. Magnani }
526fa33cdbfSSteven J. Magnani 
5277e49b6f2SJan Kara static int udf_extend_file(struct inode *inode, loff_t newsize)
5287e49b6f2SJan Kara {
5297e49b6f2SJan Kara 
5307e49b6f2SJan Kara 	struct extent_position epos;
5317e49b6f2SJan Kara 	struct kernel_lb_addr eloc;
5327e49b6f2SJan Kara 	uint32_t elen;
5337e49b6f2SJan Kara 	int8_t etype;
5347e49b6f2SJan Kara 	struct super_block *sb = inode->i_sb;
5357e49b6f2SJan Kara 	sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
5361f3868f0SJan Kara 	loff_t new_elen;
5377e49b6f2SJan Kara 	int adsize;
5387e49b6f2SJan Kara 	struct udf_inode_info *iinfo = UDF_I(inode);
5397e49b6f2SJan Kara 	struct kernel_long_ad extent;
540fa33cdbfSSteven J. Magnani 	int err = 0;
5411f3868f0SJan Kara 	bool within_last_ext;
5427e49b6f2SJan Kara 
5437e49b6f2SJan Kara 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5447e49b6f2SJan Kara 		adsize = sizeof(struct short_ad);
5457e49b6f2SJan Kara 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5467e49b6f2SJan Kara 		adsize = sizeof(struct long_ad);
5477e49b6f2SJan Kara 	else
5487e49b6f2SJan Kara 		BUG();
5497e49b6f2SJan Kara 
55016d05565SJan Kara 	/*
55116d05565SJan Kara 	 * When creating hole in file, just don't bother with preserving
55216d05565SJan Kara 	 * preallocation. It likely won't be very useful anyway.
55316d05565SJan Kara 	 */
55416d05565SJan Kara 	udf_discard_prealloc(inode);
55516d05565SJan Kara 
5567e49b6f2SJan Kara 	etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
5571f3868f0SJan Kara 	within_last_ext = (etype != -1);
55816d05565SJan Kara 	/* We don't expect extents past EOF... */
5591f3868f0SJan Kara 	WARN_ON_ONCE(within_last_ext &&
56016d05565SJan Kara 		     elen > ((loff_t)offset + 1) << inode->i_blkbits);
5617e49b6f2SJan Kara 
5627e49b6f2SJan Kara 	if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
5637e49b6f2SJan Kara 	    (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
5647e49b6f2SJan Kara 		/* File has no extents at all or has empty last
5657e49b6f2SJan Kara 		 * indirect extent! Create a fake extent... */
5667e49b6f2SJan Kara 		extent.extLocation.logicalBlockNum = 0;
5677e49b6f2SJan Kara 		extent.extLocation.partitionReferenceNum = 0;
5687e49b6f2SJan Kara 		extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
5697e49b6f2SJan Kara 	} else {
5707e49b6f2SJan Kara 		epos.offset -= adsize;
5717e49b6f2SJan Kara 		etype = udf_next_aext(inode, &epos, &extent.extLocation,
5727e49b6f2SJan Kara 				      &extent.extLength, 0);
5737e49b6f2SJan Kara 		extent.extLength |= etype << 30;
5747e49b6f2SJan Kara 	}
575fa33cdbfSSteven J. Magnani 
5761f3868f0SJan Kara 	new_elen = ((loff_t)offset << inode->i_blkbits) |
5771f3868f0SJan Kara 					(newsize & (sb->s_blocksize - 1));
578fa33cdbfSSteven J. Magnani 
579fa33cdbfSSteven J. Magnani 	/* File has extent covering the new size (could happen when extending
580fa33cdbfSSteven J. Magnani 	 * inside a block)?
581fa33cdbfSSteven J. Magnani 	 */
5821f3868f0SJan Kara 	if (within_last_ext) {
583fa33cdbfSSteven J. Magnani 		/* Extending file within the last file block */
5841f3868f0SJan Kara 		udf_do_extend_final_block(inode, &epos, &extent, new_elen);
585fa33cdbfSSteven J. Magnani 	} else {
5861f3868f0SJan Kara 		err = udf_do_extend_file(inode, &epos, &extent, new_elen);
587fa33cdbfSSteven J. Magnani 	}
588fa33cdbfSSteven J. Magnani 
5897e49b6f2SJan Kara 	if (err < 0)
5907e49b6f2SJan Kara 		goto out;
5917e49b6f2SJan Kara 	err = 0;
5927e49b6f2SJan Kara out:
5937e49b6f2SJan Kara 	brelse(epos.bh);
5947e49b6f2SJan Kara 	return err;
5957e49b6f2SJan Kara }
5967e49b6f2SJan Kara 
5977b0b0933SJan Kara static sector_t inode_getblk(struct inode *inode, sector_t block,
5987b0b0933SJan Kara 			     int *err, int *new)
5991da177e4SLinus Torvalds {
6005ca4e4beSPekka Enberg 	struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
601ff116fc8SJan Kara 	struct extent_position prev_epos, cur_epos, next_epos;
6021da177e4SLinus Torvalds 	int count = 0, startnum = 0, endnum = 0;
60385d71244SJan Kara 	uint32_t elen = 0, tmpelen;
6045ca4e4beSPekka Enberg 	struct kernel_lb_addr eloc, tmpeloc;
6051da177e4SLinus Torvalds 	int c = 1;
60660448b1dSJan Kara 	loff_t lbcount = 0, b_off = 0;
60723970a1cSTom Rix 	udf_pblk_t newblocknum, newblock = 0;
60860448b1dSJan Kara 	sector_t offset = 0;
6091da177e4SLinus Torvalds 	int8_t etype;
61048d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
611b490bdd6SSteve Magnani 	udf_pblk_t goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
61231170b6aSJan Kara 	int lastblock = 0;
613fb719c59SNamjae Jeon 	bool isBeyondEOF;
6141da177e4SLinus Torvalds 
6157b0b0933SJan Kara 	*err = 0;
6167b0b0933SJan Kara 	*new = 0;
617ff116fc8SJan Kara 	prev_epos.offset = udf_file_entry_alloc_offset(inode);
61848d6d8ffSMarcin Slusarz 	prev_epos.block = iinfo->i_location;
619ff116fc8SJan Kara 	prev_epos.bh = NULL;
620ff116fc8SJan Kara 	cur_epos = next_epos = prev_epos;
62160448b1dSJan Kara 	b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	/* find the extent which contains the block we are looking for.
6241da177e4SLinus Torvalds 	   alternate between laarr[0] and laarr[1] for locations of the
6251da177e4SLinus Torvalds 	   current extent, and the previous extent */
626cb00ea35SCyrill Gorcunov 	do {
627cb00ea35SCyrill Gorcunov 		if (prev_epos.bh != cur_epos.bh) {
6283bf25cb4SJan Kara 			brelse(prev_epos.bh);
6293bf25cb4SJan Kara 			get_bh(cur_epos.bh);
630ff116fc8SJan Kara 			prev_epos.bh = cur_epos.bh;
6311da177e4SLinus Torvalds 		}
632cb00ea35SCyrill Gorcunov 		if (cur_epos.bh != next_epos.bh) {
6333bf25cb4SJan Kara 			brelse(cur_epos.bh);
6343bf25cb4SJan Kara 			get_bh(next_epos.bh);
635ff116fc8SJan Kara 			cur_epos.bh = next_epos.bh;
6361da177e4SLinus Torvalds 		}
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 		lbcount += elen;
6391da177e4SLinus Torvalds 
640ff116fc8SJan Kara 		prev_epos.block = cur_epos.block;
641ff116fc8SJan Kara 		cur_epos.block = next_epos.block;
6421da177e4SLinus Torvalds 
643ff116fc8SJan Kara 		prev_epos.offset = cur_epos.offset;
644ff116fc8SJan Kara 		cur_epos.offset = next_epos.offset;
6451da177e4SLinus Torvalds 
6464b11111aSMarcin Slusarz 		etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
6474b11111aSMarcin Slusarz 		if (etype == -1)
6481da177e4SLinus Torvalds 			break;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 		c = !c;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 		laarr[c].extLength = (etype << 30) | elen;
6531da177e4SLinus Torvalds 		laarr[c].extLocation = eloc;
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 		if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
6561da177e4SLinus Torvalds 			pgoal = eloc.logicalBlockNum +
6571da177e4SLinus Torvalds 				((elen + inode->i_sb->s_blocksize - 1) >>
6581da177e4SLinus Torvalds 				 inode->i_sb->s_blocksize_bits);
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 		count++;
6611da177e4SLinus Torvalds 	} while (lbcount + elen <= b_off);
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	b_off -= lbcount;
6641da177e4SLinus Torvalds 	offset = b_off >> inode->i_sb->s_blocksize_bits;
66585d71244SJan Kara 	/*
66685d71244SJan Kara 	 * Move prev_epos and cur_epos into indirect extent if we are at
66785d71244SJan Kara 	 * the pointer to it
66885d71244SJan Kara 	 */
66985d71244SJan Kara 	udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
67085d71244SJan Kara 	udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	/* if the extent is allocated and recorded, return the block
6731da177e4SLinus Torvalds 	   if the extent is not a multiple of the blocksize, round up */
6741da177e4SLinus Torvalds 
675cb00ea35SCyrill Gorcunov 	if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
676cb00ea35SCyrill Gorcunov 		if (elen & (inode->i_sb->s_blocksize - 1)) {
6771da177e4SLinus Torvalds 			elen = EXT_RECORDED_ALLOCATED |
6781da177e4SLinus Torvalds 				((elen + inode->i_sb->s_blocksize - 1) &
6791da177e4SLinus Torvalds 				 ~(inode->i_sb->s_blocksize - 1));
680b316c443SJan Kara 			iinfo->i_lenExtents =
681b316c443SJan Kara 				ALIGN(iinfo->i_lenExtents,
682b316c443SJan Kara 				      inode->i_sb->s_blocksize);
6837e49b6f2SJan Kara 			udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
6841da177e4SLinus Torvalds 		}
68597e961fdSPekka Enberg 		newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
68602d4ca49SFabian Frederick 		goto out_free;
6871da177e4SLinus Torvalds 	}
6881da177e4SLinus Torvalds 
68916d05565SJan Kara 	/* Are we beyond EOF and preallocated extent? */
690cb00ea35SCyrill Gorcunov 	if (etype == -1) {
69131170b6aSJan Kara 		int ret;
692fa33cdbfSSteven J. Magnani 		loff_t hole_len;
69316d05565SJan Kara 
6946981498dSFabian Frederick 		isBeyondEOF = true;
69531170b6aSJan Kara 		if (count) {
69631170b6aSJan Kara 			if (c)
69731170b6aSJan Kara 				laarr[0] = laarr[1];
69831170b6aSJan Kara 			startnum = 1;
699cb00ea35SCyrill Gorcunov 		} else {
70031170b6aSJan Kara 			/* Create a fake extent when there's not one */
7014b11111aSMarcin Slusarz 			memset(&laarr[0].extLocation, 0x00,
7025ca4e4beSPekka Enberg 				sizeof(struct kernel_lb_addr));
70331170b6aSJan Kara 			laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
7047e49b6f2SJan Kara 			/* Will udf_do_extend_file() create real extent from
7054b11111aSMarcin Slusarz 			   a fake one? */
70631170b6aSJan Kara 			startnum = (offset > 0);
70731170b6aSJan Kara 		}
70831170b6aSJan Kara 		/* Create extents for the hole between EOF and offset */
709fa33cdbfSSteven J. Magnani 		hole_len = (loff_t)offset << inode->i_blkbits;
710fa33cdbfSSteven J. Magnani 		ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
7117e49b6f2SJan Kara 		if (ret < 0) {
7127e49b6f2SJan Kara 			*err = ret;
71302d4ca49SFabian Frederick 			goto out_free;
71431170b6aSJan Kara 		}
71531170b6aSJan Kara 		c = 0;
71631170b6aSJan Kara 		offset = 0;
71731170b6aSJan Kara 		count += ret;
71831170b6aSJan Kara 		/* We are not covered by a preallocated extent? */
7194b11111aSMarcin Slusarz 		if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
7204b11111aSMarcin Slusarz 						EXT_NOT_RECORDED_ALLOCATED) {
72131170b6aSJan Kara 			/* Is there any real extent? - otherwise we overwrite
72231170b6aSJan Kara 			 * the fake one... */
72331170b6aSJan Kara 			if (count)
7241da177e4SLinus Torvalds 				c = !c;
7251da177e4SLinus Torvalds 			laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
72631170b6aSJan Kara 				inode->i_sb->s_blocksize;
7274b11111aSMarcin Slusarz 			memset(&laarr[c].extLocation, 0x00,
7285ca4e4beSPekka Enberg 				sizeof(struct kernel_lb_addr));
7291da177e4SLinus Torvalds 			count++;
73031170b6aSJan Kara 		}
73131170b6aSJan Kara 		endnum = c + 1;
7321da177e4SLinus Torvalds 		lastblock = 1;
733cb00ea35SCyrill Gorcunov 	} else {
7346981498dSFabian Frederick 		isBeyondEOF = false;
7351da177e4SLinus Torvalds 		endnum = startnum = ((count > 2) ? 2 : count);
7361da177e4SLinus Torvalds 
7374b11111aSMarcin Slusarz 		/* if the current extent is in position 0,
7384b11111aSMarcin Slusarz 		   swap it with the previous */
739cb00ea35SCyrill Gorcunov 		if (!c && count != 1) {
7401da177e4SLinus Torvalds 			laarr[2] = laarr[0];
7411da177e4SLinus Torvalds 			laarr[0] = laarr[1];
7421da177e4SLinus Torvalds 			laarr[1] = laarr[2];
7431da177e4SLinus Torvalds 			c = 1;
7441da177e4SLinus Torvalds 		}
7451da177e4SLinus Torvalds 
7464b11111aSMarcin Slusarz 		/* if the current block is located in an extent,
7474b11111aSMarcin Slusarz 		   read the next extent */
7484b11111aSMarcin Slusarz 		etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
7494b11111aSMarcin Slusarz 		if (etype != -1) {
7501da177e4SLinus Torvalds 			laarr[c + 1].extLength = (etype << 30) | elen;
7511da177e4SLinus Torvalds 			laarr[c + 1].extLocation = eloc;
7521da177e4SLinus Torvalds 			count++;
7531da177e4SLinus Torvalds 			startnum++;
7541da177e4SLinus Torvalds 			endnum++;
7554b11111aSMarcin Slusarz 		} else
7561da177e4SLinus Torvalds 			lastblock = 1;
7571da177e4SLinus Torvalds 	}
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	/* if the current extent is not recorded but allocated, get the
76028de7948SCyrill Gorcunov 	 * block in the extent corresponding to the requested block */
7614b11111aSMarcin Slusarz 	if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
7621da177e4SLinus Torvalds 		newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
7634b11111aSMarcin Slusarz 	else { /* otherwise, allocate a new block */
76448d6d8ffSMarcin Slusarz 		if (iinfo->i_next_alloc_block == block)
76548d6d8ffSMarcin Slusarz 			goal = iinfo->i_next_alloc_goal;
7661da177e4SLinus Torvalds 
767cb00ea35SCyrill Gorcunov 		if (!goal) {
7684b11111aSMarcin Slusarz 			if (!(goal = pgoal)) /* XXX: what was intended here? */
76948d6d8ffSMarcin Slusarz 				goal = iinfo->i_location.logicalBlockNum + 1;
7701da177e4SLinus Torvalds 		}
7711da177e4SLinus Torvalds 
7724b11111aSMarcin Slusarz 		newblocknum = udf_new_block(inode->i_sb, inode,
77348d6d8ffSMarcin Slusarz 				iinfo->i_location.partitionReferenceNum,
7744b11111aSMarcin Slusarz 				goal, err);
7754b11111aSMarcin Slusarz 		if (!newblocknum) {
7761da177e4SLinus Torvalds 			*err = -ENOSPC;
77702d4ca49SFabian Frederick 			goto out_free;
7781da177e4SLinus Torvalds 		}
779fb719c59SNamjae Jeon 		if (isBeyondEOF)
78048d6d8ffSMarcin Slusarz 			iinfo->i_lenExtents += inode->i_sb->s_blocksize;
7811da177e4SLinus Torvalds 	}
7821da177e4SLinus Torvalds 
7834b11111aSMarcin Slusarz 	/* if the extent the requsted block is located in contains multiple
7844b11111aSMarcin Slusarz 	 * blocks, split the extent into at most three extents. blocks prior
7854b11111aSMarcin Slusarz 	 * to requested block, requested block, and blocks after requested
7864b11111aSMarcin Slusarz 	 * block */
7871da177e4SLinus Torvalds 	udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
7881da177e4SLinus Torvalds 
78981056dd0SJan Kara 	/* We preallocate blocks only for regular files. It also makes sense
79081056dd0SJan Kara 	 * for directories but there's a problem when to drop the
79181056dd0SJan Kara 	 * preallocation. We might use some delayed work for that but I feel
79281056dd0SJan Kara 	 * it's overengineering for a filesystem like UDF. */
79381056dd0SJan Kara 	if (S_ISREG(inode->i_mode))
7941da177e4SLinus Torvalds 		udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	/* merge any continuous blocks in laarr */
7971da177e4SLinus Torvalds 	udf_merge_extents(inode, laarr, &endnum);
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	/* write back the new extents, inserting new extents if the new number
80028de7948SCyrill Gorcunov 	 * of extents is greater than the old number, and deleting extents if
80128de7948SCyrill Gorcunov 	 * the new number of extents is less than the old number */
80219fd80deSJan Kara 	*err = udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
80319fd80deSJan Kara 	if (*err < 0)
80419fd80deSJan Kara 		goto out_free;
8051da177e4SLinus Torvalds 
8064b11111aSMarcin Slusarz 	newblock = udf_get_pblock(inode->i_sb, newblocknum,
80748d6d8ffSMarcin Slusarz 				iinfo->i_location.partitionReferenceNum, 0);
8087b0b0933SJan Kara 	if (!newblock) {
8097b0b0933SJan Kara 		*err = -EIO;
81002d4ca49SFabian Frederick 		goto out_free;
8117b0b0933SJan Kara 	}
8121da177e4SLinus Torvalds 	*new = 1;
81348d6d8ffSMarcin Slusarz 	iinfo->i_next_alloc_block = block;
81448d6d8ffSMarcin Slusarz 	iinfo->i_next_alloc_goal = newblocknum;
815c2050a45SDeepa Dinamani 	inode->i_ctime = current_time(inode);
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	if (IS_SYNC(inode))
8181da177e4SLinus Torvalds 		udf_sync_inode(inode);
8191da177e4SLinus Torvalds 	else
8201da177e4SLinus Torvalds 		mark_inode_dirty(inode);
82102d4ca49SFabian Frederick out_free:
82202d4ca49SFabian Frederick 	brelse(prev_epos.bh);
82302d4ca49SFabian Frederick 	brelse(cur_epos.bh);
82402d4ca49SFabian Frederick 	brelse(next_epos.bh);
8257b0b0933SJan Kara 	return newblock;
8261da177e4SLinus Torvalds }
8271da177e4SLinus Torvalds 
828cb00ea35SCyrill Gorcunov static void udf_split_extents(struct inode *inode, int *c, int offset,
829b490bdd6SSteve Magnani 			       udf_pblk_t newblocknum,
830b490bdd6SSteve Magnani 			       struct kernel_long_ad *laarr, int *endnum)
8311da177e4SLinus Torvalds {
8324b11111aSMarcin Slusarz 	unsigned long blocksize = inode->i_sb->s_blocksize;
8334b11111aSMarcin Slusarz 	unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
8344b11111aSMarcin Slusarz 
8351da177e4SLinus Torvalds 	if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
8364b11111aSMarcin Slusarz 	    (laarr[*c].extLength >> 30) ==
8374b11111aSMarcin Slusarz 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
8381da177e4SLinus Torvalds 		int curr = *c;
8391da177e4SLinus Torvalds 		int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
8404b11111aSMarcin Slusarz 			    blocksize - 1) >> blocksize_bits;
8411da177e4SLinus Torvalds 		int8_t etype = (laarr[curr].extLength >> 30);
8421da177e4SLinus Torvalds 
8434b11111aSMarcin Slusarz 		if (blen == 1)
84428de7948SCyrill Gorcunov 			;
8454b11111aSMarcin Slusarz 		else if (!offset || blen == offset + 1) {
8461da177e4SLinus Torvalds 			laarr[curr + 2] = laarr[curr + 1];
8471da177e4SLinus Torvalds 			laarr[curr + 1] = laarr[curr];
848cb00ea35SCyrill Gorcunov 		} else {
8491da177e4SLinus Torvalds 			laarr[curr + 3] = laarr[curr + 1];
8501da177e4SLinus Torvalds 			laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
8511da177e4SLinus Torvalds 		}
8521da177e4SLinus Torvalds 
853cb00ea35SCyrill Gorcunov 		if (offset) {
854cb00ea35SCyrill Gorcunov 			if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
8554b11111aSMarcin Slusarz 				udf_free_blocks(inode->i_sb, inode,
85697e961fdSPekka Enberg 						&laarr[curr].extLocation,
8574b11111aSMarcin Slusarz 						0, offset);
8584b11111aSMarcin Slusarz 				laarr[curr].extLength =
8594b11111aSMarcin Slusarz 					EXT_NOT_RECORDED_NOT_ALLOCATED |
8604b11111aSMarcin Slusarz 					(offset << blocksize_bits);
8611da177e4SLinus Torvalds 				laarr[curr].extLocation.logicalBlockNum = 0;
8624b11111aSMarcin Slusarz 				laarr[curr].extLocation.
8634b11111aSMarcin Slusarz 						partitionReferenceNum = 0;
8644b11111aSMarcin Slusarz 			} else
8651da177e4SLinus Torvalds 				laarr[curr].extLength = (etype << 30) |
8664b11111aSMarcin Slusarz 					(offset << blocksize_bits);
8671da177e4SLinus Torvalds 			curr++;
8681da177e4SLinus Torvalds 			(*c)++;
8691da177e4SLinus Torvalds 			(*endnum)++;
8701da177e4SLinus Torvalds 		}
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 		laarr[curr].extLocation.logicalBlockNum = newblocknum;
8731da177e4SLinus Torvalds 		if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
8741da177e4SLinus Torvalds 			laarr[curr].extLocation.partitionReferenceNum =
875c0b34438SMarcin Slusarz 				UDF_I(inode)->i_location.partitionReferenceNum;
8761da177e4SLinus Torvalds 		laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
8774b11111aSMarcin Slusarz 			blocksize;
8781da177e4SLinus Torvalds 		curr++;
8791da177e4SLinus Torvalds 
880cb00ea35SCyrill Gorcunov 		if (blen != offset + 1) {
8811da177e4SLinus Torvalds 			if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
8824b11111aSMarcin Slusarz 				laarr[curr].extLocation.logicalBlockNum +=
8834b11111aSMarcin Slusarz 								offset + 1;
88428de7948SCyrill Gorcunov 			laarr[curr].extLength = (etype << 30) |
8854b11111aSMarcin Slusarz 				((blen - (offset + 1)) << blocksize_bits);
8861da177e4SLinus Torvalds 			curr++;
8871da177e4SLinus Torvalds 			(*endnum)++;
8881da177e4SLinus Torvalds 		}
8891da177e4SLinus Torvalds 	}
8901da177e4SLinus Torvalds }
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
8933cc6f844SFabian Frederick 				 struct kernel_long_ad *laarr,
894cb00ea35SCyrill Gorcunov 				 int *endnum)
8951da177e4SLinus Torvalds {
8961da177e4SLinus Torvalds 	int start, length = 0, currlength = 0, i;
8971da177e4SLinus Torvalds 
898cb00ea35SCyrill Gorcunov 	if (*endnum >= (c + 1)) {
8991da177e4SLinus Torvalds 		if (!lastblock)
9001da177e4SLinus Torvalds 			return;
9011da177e4SLinus Torvalds 		else
9021da177e4SLinus Torvalds 			start = c;
903cb00ea35SCyrill Gorcunov 	} else {
9044b11111aSMarcin Slusarz 		if ((laarr[c + 1].extLength >> 30) ==
9054b11111aSMarcin Slusarz 					(EXT_NOT_RECORDED_ALLOCATED >> 30)) {
9061da177e4SLinus Torvalds 			start = c + 1;
9074b11111aSMarcin Slusarz 			length = currlength =
9084b11111aSMarcin Slusarz 				(((laarr[c + 1].extLength &
9094b11111aSMarcin Slusarz 					UDF_EXTENT_LENGTH_MASK) +
9104b11111aSMarcin Slusarz 				inode->i_sb->s_blocksize - 1) >>
9114b11111aSMarcin Slusarz 				inode->i_sb->s_blocksize_bits);
9124b11111aSMarcin Slusarz 		} else
9131da177e4SLinus Torvalds 			start = c;
9141da177e4SLinus Torvalds 	}
9151da177e4SLinus Torvalds 
916cb00ea35SCyrill Gorcunov 	for (i = start + 1; i <= *endnum; i++) {
917cb00ea35SCyrill Gorcunov 		if (i == *endnum) {
9181da177e4SLinus Torvalds 			if (lastblock)
9191da177e4SLinus Torvalds 				length += UDF_DEFAULT_PREALLOC_BLOCKS;
9204b11111aSMarcin Slusarz 		} else if ((laarr[i].extLength >> 30) ==
9214b11111aSMarcin Slusarz 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
9224b11111aSMarcin Slusarz 			length += (((laarr[i].extLength &
9234b11111aSMarcin Slusarz 						UDF_EXTENT_LENGTH_MASK) +
9244b11111aSMarcin Slusarz 				    inode->i_sb->s_blocksize - 1) >>
9254b11111aSMarcin Slusarz 				    inode->i_sb->s_blocksize_bits);
9264b11111aSMarcin Slusarz 		} else
9271da177e4SLinus Torvalds 			break;
9281da177e4SLinus Torvalds 	}
9291da177e4SLinus Torvalds 
930cb00ea35SCyrill Gorcunov 	if (length) {
9311da177e4SLinus Torvalds 		int next = laarr[start].extLocation.logicalBlockNum +
9321da177e4SLinus Torvalds 			(((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
9334b11111aSMarcin Slusarz 			  inode->i_sb->s_blocksize - 1) >>
9344b11111aSMarcin Slusarz 			  inode->i_sb->s_blocksize_bits);
9351da177e4SLinus Torvalds 		int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
93628de7948SCyrill Gorcunov 				laarr[start].extLocation.partitionReferenceNum,
9374b11111aSMarcin Slusarz 				next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
9384b11111aSMarcin Slusarz 				length : UDF_DEFAULT_PREALLOC_BLOCKS) -
9394b11111aSMarcin Slusarz 				currlength);
940cb00ea35SCyrill Gorcunov 		if (numalloc) 	{
9414b11111aSMarcin Slusarz 			if (start == (c + 1))
9421da177e4SLinus Torvalds 				laarr[start].extLength +=
9434b11111aSMarcin Slusarz 					(numalloc <<
9444b11111aSMarcin Slusarz 					 inode->i_sb->s_blocksize_bits);
9454b11111aSMarcin Slusarz 			else {
9461da177e4SLinus Torvalds 				memmove(&laarr[c + 2], &laarr[c + 1],
9475ca4e4beSPekka Enberg 					sizeof(struct long_ad) * (*endnum - (c + 1)));
9481da177e4SLinus Torvalds 				(*endnum)++;
9491da177e4SLinus Torvalds 				laarr[c + 1].extLocation.logicalBlockNum = next;
9501da177e4SLinus Torvalds 				laarr[c + 1].extLocation.partitionReferenceNum =
9514b11111aSMarcin Slusarz 					laarr[c].extLocation.
9524b11111aSMarcin Slusarz 							partitionReferenceNum;
9534b11111aSMarcin Slusarz 				laarr[c + 1].extLength =
9544b11111aSMarcin Slusarz 					EXT_NOT_RECORDED_ALLOCATED |
9554b11111aSMarcin Slusarz 					(numalloc <<
9564b11111aSMarcin Slusarz 					 inode->i_sb->s_blocksize_bits);
9571da177e4SLinus Torvalds 				start = c + 1;
9581da177e4SLinus Torvalds 			}
9591da177e4SLinus Torvalds 
960cb00ea35SCyrill Gorcunov 			for (i = start + 1; numalloc && i < *endnum; i++) {
9614b11111aSMarcin Slusarz 				int elen = ((laarr[i].extLength &
9624b11111aSMarcin Slusarz 						UDF_EXTENT_LENGTH_MASK) +
9634b11111aSMarcin Slusarz 					    inode->i_sb->s_blocksize - 1) >>
9644b11111aSMarcin Slusarz 					    inode->i_sb->s_blocksize_bits;
9651da177e4SLinus Torvalds 
966cb00ea35SCyrill Gorcunov 				if (elen > numalloc) {
9671da177e4SLinus Torvalds 					laarr[i].extLength -=
9684b11111aSMarcin Slusarz 						(numalloc <<
9694b11111aSMarcin Slusarz 						 inode->i_sb->s_blocksize_bits);
9701da177e4SLinus Torvalds 					numalloc = 0;
971cb00ea35SCyrill Gorcunov 				} else {
9721da177e4SLinus Torvalds 					numalloc -= elen;
9731da177e4SLinus Torvalds 					if (*endnum > (i + 1))
9744b11111aSMarcin Slusarz 						memmove(&laarr[i],
9754b11111aSMarcin Slusarz 							&laarr[i + 1],
9765ca4e4beSPekka Enberg 							sizeof(struct long_ad) *
9774b11111aSMarcin Slusarz 							(*endnum - (i + 1)));
9781da177e4SLinus Torvalds 					i--;
9791da177e4SLinus Torvalds 					(*endnum)--;
9801da177e4SLinus Torvalds 				}
9811da177e4SLinus Torvalds 			}
982c0b34438SMarcin Slusarz 			UDF_I(inode)->i_lenExtents +=
9834b11111aSMarcin Slusarz 				numalloc << inode->i_sb->s_blocksize_bits;
9841da177e4SLinus Torvalds 		}
9851da177e4SLinus Torvalds 	}
9861da177e4SLinus Torvalds }
9871da177e4SLinus Torvalds 
9883cc6f844SFabian Frederick static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
989cb00ea35SCyrill Gorcunov 			      int *endnum)
9901da177e4SLinus Torvalds {
9911da177e4SLinus Torvalds 	int i;
9924b11111aSMarcin Slusarz 	unsigned long blocksize = inode->i_sb->s_blocksize;
9934b11111aSMarcin Slusarz 	unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
9941da177e4SLinus Torvalds 
995cb00ea35SCyrill Gorcunov 	for (i = 0; i < (*endnum - 1); i++) {
9965ca4e4beSPekka Enberg 		struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
9975ca4e4beSPekka Enberg 		struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
9981da177e4SLinus Torvalds 
9994b11111aSMarcin Slusarz 		if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
10004b11111aSMarcin Slusarz 			(((li->extLength >> 30) ==
10014b11111aSMarcin Slusarz 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
10024b11111aSMarcin Slusarz 			((lip1->extLocation.logicalBlockNum -
10034b11111aSMarcin Slusarz 			  li->extLocation.logicalBlockNum) ==
10044b11111aSMarcin Slusarz 			(((li->extLength & UDF_EXTENT_LENGTH_MASK) +
10054b11111aSMarcin Slusarz 			blocksize - 1) >> blocksize_bits)))) {
10064b11111aSMarcin Slusarz 
10074b11111aSMarcin Slusarz 			if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
10084b11111aSMarcin Slusarz 			     (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
100953cafe1dSJan Kara 			     blocksize - 1) <= UDF_EXTENT_LENGTH_MASK) {
10104b11111aSMarcin Slusarz 				li->extLength = lip1->extLength +
10114b11111aSMarcin Slusarz 					(((li->extLength &
10124b11111aSMarcin Slusarz 						UDF_EXTENT_LENGTH_MASK) +
10134b11111aSMarcin Slusarz 					 blocksize - 1) & ~(blocksize - 1));
10141da177e4SLinus Torvalds 				if (*endnum > (i + 2))
10151da177e4SLinus Torvalds 					memmove(&laarr[i + 1], &laarr[i + 2],
10165ca4e4beSPekka Enberg 						sizeof(struct long_ad) *
10174b11111aSMarcin Slusarz 						(*endnum - (i + 2)));
10181da177e4SLinus Torvalds 				i--;
10191da177e4SLinus Torvalds 				(*endnum)--;
10201da177e4SLinus Torvalds 			}
10214b11111aSMarcin Slusarz 		} else if (((li->extLength >> 30) ==
10224b11111aSMarcin Slusarz 				(EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
10234b11111aSMarcin Slusarz 			   ((lip1->extLength >> 30) ==
10244b11111aSMarcin Slusarz 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
102597e961fdSPekka Enberg 			udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
10264b11111aSMarcin Slusarz 					((li->extLength &
10274b11111aSMarcin Slusarz 					  UDF_EXTENT_LENGTH_MASK) +
10284b11111aSMarcin Slusarz 					 blocksize - 1) >> blocksize_bits);
10294b11111aSMarcin Slusarz 			li->extLocation.logicalBlockNum = 0;
10304b11111aSMarcin Slusarz 			li->extLocation.partitionReferenceNum = 0;
10314b11111aSMarcin Slusarz 
10324b11111aSMarcin Slusarz 			if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
10334b11111aSMarcin Slusarz 			     (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
10344b11111aSMarcin Slusarz 			     blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
10354b11111aSMarcin Slusarz 				lip1->extLength = (lip1->extLength -
10364b11111aSMarcin Slusarz 						   (li->extLength &
10374b11111aSMarcin Slusarz 						   UDF_EXTENT_LENGTH_MASK) +
10384b11111aSMarcin Slusarz 						   UDF_EXTENT_LENGTH_MASK) &
10394b11111aSMarcin Slusarz 						   ~(blocksize - 1);
10404b11111aSMarcin Slusarz 				li->extLength = (li->extLength &
10414b11111aSMarcin Slusarz 						 UDF_EXTENT_FLAG_MASK) +
10424b11111aSMarcin Slusarz 						(UDF_EXTENT_LENGTH_MASK + 1) -
10434b11111aSMarcin Slusarz 						blocksize;
10444b11111aSMarcin Slusarz 			} else {
10454b11111aSMarcin Slusarz 				li->extLength = lip1->extLength +
10464b11111aSMarcin Slusarz 					(((li->extLength &
10474b11111aSMarcin Slusarz 						UDF_EXTENT_LENGTH_MASK) +
10484b11111aSMarcin Slusarz 					  blocksize - 1) & ~(blocksize - 1));
10494b11111aSMarcin Slusarz 				if (*endnum > (i + 2))
10504b11111aSMarcin Slusarz 					memmove(&laarr[i + 1], &laarr[i + 2],
10515ca4e4beSPekka Enberg 						sizeof(struct long_ad) *
10524b11111aSMarcin Slusarz 						(*endnum - (i + 2)));
10534b11111aSMarcin Slusarz 				i--;
10544b11111aSMarcin Slusarz 				(*endnum)--;
10554b11111aSMarcin Slusarz 			}
10564b11111aSMarcin Slusarz 		} else if ((li->extLength >> 30) ==
10574b11111aSMarcin Slusarz 					(EXT_NOT_RECORDED_ALLOCATED >> 30)) {
10584b11111aSMarcin Slusarz 			udf_free_blocks(inode->i_sb, inode,
105997e961fdSPekka Enberg 					&li->extLocation, 0,
10604b11111aSMarcin Slusarz 					((li->extLength &
10614b11111aSMarcin Slusarz 						UDF_EXTENT_LENGTH_MASK) +
10624b11111aSMarcin Slusarz 					 blocksize - 1) >> blocksize_bits);
10634b11111aSMarcin Slusarz 			li->extLocation.logicalBlockNum = 0;
10644b11111aSMarcin Slusarz 			li->extLocation.partitionReferenceNum = 0;
10654b11111aSMarcin Slusarz 			li->extLength = (li->extLength &
10664b11111aSMarcin Slusarz 						UDF_EXTENT_LENGTH_MASK) |
10671da177e4SLinus Torvalds 						EXT_NOT_RECORDED_NOT_ALLOCATED;
10681da177e4SLinus Torvalds 		}
10691da177e4SLinus Torvalds 	}
10701da177e4SLinus Torvalds }
10711da177e4SLinus Torvalds 
107219fd80deSJan Kara static int udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
1073cb00ea35SCyrill Gorcunov 			      int startnum, int endnum,
1074ff116fc8SJan Kara 			      struct extent_position *epos)
10751da177e4SLinus Torvalds {
10761da177e4SLinus Torvalds 	int start = 0, i;
10775ca4e4beSPekka Enberg 	struct kernel_lb_addr tmploc;
10781da177e4SLinus Torvalds 	uint32_t tmplen;
107919fd80deSJan Kara 	int err;
10801da177e4SLinus Torvalds 
1081cb00ea35SCyrill Gorcunov 	if (startnum > endnum) {
10821da177e4SLinus Torvalds 		for (i = 0; i < (startnum - endnum); i++)
10836c1e4d06SJan Kara 			udf_delete_aext(inode, *epos);
1084cb00ea35SCyrill Gorcunov 	} else if (startnum < endnum) {
1085cb00ea35SCyrill Gorcunov 		for (i = 0; i < (endnum - startnum); i++) {
108619fd80deSJan Kara 			err = udf_insert_aext(inode, *epos,
108719fd80deSJan Kara 					      laarr[i].extLocation,
1088ff116fc8SJan Kara 					      laarr[i].extLength);
108919fd80deSJan Kara 			/*
109019fd80deSJan Kara 			 * If we fail here, we are likely corrupting the extent
109119fd80deSJan Kara 			 * list and leaking blocks. At least stop early to
109219fd80deSJan Kara 			 * limit the damage.
109319fd80deSJan Kara 			 */
109419fd80deSJan Kara 			if (err < 0)
109519fd80deSJan Kara 				return err;
1096ff116fc8SJan Kara 			udf_next_aext(inode, epos, &laarr[i].extLocation,
1097ff116fc8SJan Kara 				      &laarr[i].extLength, 1);
10981da177e4SLinus Torvalds 			start++;
10991da177e4SLinus Torvalds 		}
11001da177e4SLinus Torvalds 	}
11011da177e4SLinus Torvalds 
1102cb00ea35SCyrill Gorcunov 	for (i = start; i < endnum; i++) {
1103ff116fc8SJan Kara 		udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
110497e961fdSPekka Enberg 		udf_write_aext(inode, epos, &laarr[i].extLocation,
1105ff116fc8SJan Kara 			       laarr[i].extLength, 1);
11061da177e4SLinus Torvalds 	}
110719fd80deSJan Kara 	return 0;
11081da177e4SLinus Torvalds }
11091da177e4SLinus Torvalds 
1110b490bdd6SSteve Magnani struct buffer_head *udf_bread(struct inode *inode, udf_pblk_t block,
11111da177e4SLinus Torvalds 			      int create, int *err)
11121da177e4SLinus Torvalds {
11131da177e4SLinus Torvalds 	struct buffer_head *bh = NULL;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	bh = udf_getblk(inode, block, create, err);
11161da177e4SLinus Torvalds 	if (!bh)
11171da177e4SLinus Torvalds 		return NULL;
11181da177e4SLinus Torvalds 
111959a16786SZhang Yi 	if (bh_read(bh, 0) >= 0)
11201da177e4SLinus Torvalds 		return bh;
112128de7948SCyrill Gorcunov 
11221da177e4SLinus Torvalds 	brelse(bh);
11231da177e4SLinus Torvalds 	*err = -EIO;
11241da177e4SLinus Torvalds 	return NULL;
11251da177e4SLinus Torvalds }
11261da177e4SLinus Torvalds 
11277e49b6f2SJan Kara int udf_setsize(struct inode *inode, loff_t newsize)
11281da177e4SLinus Torvalds {
11291da177e4SLinus Torvalds 	int err;
113048d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo;
113189a4d970SSteve Magnani 	unsigned int bsize = i_blocksize(inode);
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
11341da177e4SLinus Torvalds 	      S_ISLNK(inode->i_mode)))
11357e49b6f2SJan Kara 		return -EINVAL;
11361da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
11377e49b6f2SJan Kara 		return -EPERM;
11381da177e4SLinus Torvalds 
113948d6d8ffSMarcin Slusarz 	iinfo = UDF_I(inode);
11407e49b6f2SJan Kara 	if (newsize > inode->i_size) {
11414d0fb621SAlessio Igor Bogani 		down_write(&iinfo->i_data_sem);
114248d6d8ffSMarcin Slusarz 		if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
11437e49b6f2SJan Kara 			if (bsize <
11447e49b6f2SJan Kara 			    (udf_file_entry_alloc_offset(inode) + newsize)) {
11457e49b6f2SJan Kara 				err = udf_expand_file_adinicb(inode);
1146d2eb8c35SJan Kara 				if (err)
11477e49b6f2SJan Kara 					return err;
1148d2eb8c35SJan Kara 				down_write(&iinfo->i_data_sem);
1149bb2b6d19SIan Abbott 			} else {
11507e49b6f2SJan Kara 				iinfo->i_lenAlloc = newsize;
1151bb2b6d19SIan Abbott 				goto set_size;
1152bb2b6d19SIan Abbott 			}
11531da177e4SLinus Torvalds 		}
11547e49b6f2SJan Kara 		err = udf_extend_file(inode, newsize);
11557e49b6f2SJan Kara 		if (err) {
11567e49b6f2SJan Kara 			up_write(&iinfo->i_data_sem);
11577e49b6f2SJan Kara 			return err;
11587e49b6f2SJan Kara 		}
1159bb2b6d19SIan Abbott set_size:
11604d0fb621SAlessio Igor Bogani 		up_write(&iinfo->i_data_sem);
1161f2e95355SJan Kara 		truncate_setsize(inode, newsize);
1162cb00ea35SCyrill Gorcunov 	} else {
11637e49b6f2SJan Kara 		if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
11644d0fb621SAlessio Igor Bogani 			down_write(&iinfo->i_data_sem);
116599600051SNamjae Jeon 			udf_clear_extent_cache(inode);
1166382a2287SJan Kara 			memset(iinfo->i_data + iinfo->i_lenEAttr + newsize,
11677e49b6f2SJan Kara 			       0x00, bsize - newsize -
11687e49b6f2SJan Kara 			       udf_file_entry_alloc_offset(inode));
11697e49b6f2SJan Kara 			iinfo->i_lenAlloc = newsize;
11707e49b6f2SJan Kara 			truncate_setsize(inode, newsize);
11717e49b6f2SJan Kara 			up_write(&iinfo->i_data_sem);
11727e49b6f2SJan Kara 			goto update_time;
11737e49b6f2SJan Kara 		}
11747e49b6f2SJan Kara 		err = block_truncate_page(inode->i_mapping, newsize,
11757e49b6f2SJan Kara 					  udf_get_block);
11767e49b6f2SJan Kara 		if (err)
11777e49b6f2SJan Kara 			return err;
1178f2e95355SJan Kara 		truncate_setsize(inode, newsize);
11797e49b6f2SJan Kara 		down_write(&iinfo->i_data_sem);
118099600051SNamjae Jeon 		udf_clear_extent_cache(inode);
11812b42be5eSJan Kara 		err = udf_truncate_extents(inode);
11824d0fb621SAlessio Igor Bogani 		up_write(&iinfo->i_data_sem);
11832b42be5eSJan Kara 		if (err)
11842b42be5eSJan Kara 			return err;
11851da177e4SLinus Torvalds 	}
11867e49b6f2SJan Kara update_time:
1187c2050a45SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
11881da177e4SLinus Torvalds 	if (IS_SYNC(inode))
11891da177e4SLinus Torvalds 		udf_sync_inode(inode);
11901da177e4SLinus Torvalds 	else
11911da177e4SLinus Torvalds 		mark_inode_dirty(inode);
11927e49b6f2SJan Kara 	return 0;
11931da177e4SLinus Torvalds }
11941da177e4SLinus Torvalds 
1195c03aa9f6SJan Kara /*
1196c03aa9f6SJan Kara  * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1197c03aa9f6SJan Kara  * arbitrary - just that we hopefully don't limit any real use of rewritten
1198c03aa9f6SJan Kara  * inode on write-once media but avoid looping for too long on corrupted media.
1199c03aa9f6SJan Kara  */
1200c03aa9f6SJan Kara #define UDF_MAX_ICB_NESTING 1024
1201c03aa9f6SJan Kara 
12026174c2ebSJan Kara static int udf_read_inode(struct inode *inode, bool hidden_inode)
12031da177e4SLinus Torvalds {
12041da177e4SLinus Torvalds 	struct buffer_head *bh = NULL;
12051da177e4SLinus Torvalds 	struct fileEntry *fe;
1206bb7720a0SJan Kara 	struct extendedFileEntry *efe;
12071da177e4SLinus Torvalds 	uint16_t ident;
120848d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
1209bb7720a0SJan Kara 	struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
12106d3d5e86SJan Kara 	struct kernel_lb_addr *iloc = &iinfo->i_location;
1211bb7720a0SJan Kara 	unsigned int link_count;
1212c03aa9f6SJan Kara 	unsigned int indirections = 0;
121379144954SJan Kara 	int bs = inode->i_sb->s_blocksize;
12146d3d5e86SJan Kara 	int ret = -EIO;
12150c9850f4SJan Kara 	uint32_t uid, gid;
12161da177e4SLinus Torvalds 
1217c03aa9f6SJan Kara reread:
12181d82a56bSFabian Frederick 	if (iloc->partitionReferenceNum >= sbi->s_partitions) {
1219fcbf7637SSteve Magnani 		udf_debug("partition reference: %u > logical volume partitions: %u\n",
12201d82a56bSFabian Frederick 			  iloc->partitionReferenceNum, sbi->s_partitions);
12211d82a56bSFabian Frederick 		return -EIO;
12221d82a56bSFabian Frederick 	}
12231d82a56bSFabian Frederick 
12246d3d5e86SJan Kara 	if (iloc->logicalBlockNum >=
12256d3d5e86SJan Kara 	    sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1226fcbf7637SSteve Magnani 		udf_debug("block=%u, partition=%u out of range\n",
12276d3d5e86SJan Kara 			  iloc->logicalBlockNum, iloc->partitionReferenceNum);
12286d3d5e86SJan Kara 		return -EIO;
12296d3d5e86SJan Kara 	}
12306d3d5e86SJan Kara 
12311da177e4SLinus Torvalds 	/*
12321da177e4SLinus Torvalds 	 * Set defaults, but the inode is still incomplete!
12331da177e4SLinus Torvalds 	 * Note: get_new_inode() sets the following on a new inode:
12341da177e4SLinus Torvalds 	 *      i_sb = sb
12351da177e4SLinus Torvalds 	 *      i_no = ino
12361da177e4SLinus Torvalds 	 *      i_flags = sb->s_flags
12371da177e4SLinus Torvalds 	 *      i_state = 0
12381da177e4SLinus Torvalds 	 * clean_inode(): zero fills and sets
12391da177e4SLinus Torvalds 	 *      i_count = 1
12401da177e4SLinus Torvalds 	 *      i_nlink = 1
12411da177e4SLinus Torvalds 	 *      i_op = NULL;
12421da177e4SLinus Torvalds 	 */
12436d3d5e86SJan Kara 	bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
1244cb00ea35SCyrill Gorcunov 	if (!bh) {
1245fcbf7637SSteve Magnani 		udf_err(inode->i_sb, "(ino %lu) failed !bh\n", inode->i_ino);
12466d3d5e86SJan Kara 		return -EIO;
12471da177e4SLinus Torvalds 	}
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1250cb00ea35SCyrill Gorcunov 	    ident != TAG_IDENT_USE) {
1251fcbf7637SSteve Magnani 		udf_err(inode->i_sb, "(ino %lu) failed ident=%u\n",
125278ace70cSJoe Perches 			inode->i_ino, ident);
12536d3d5e86SJan Kara 		goto out;
12541da177e4SLinus Torvalds 	}
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	fe = (struct fileEntry *)bh->b_data;
1257bb7720a0SJan Kara 	efe = (struct extendedFileEntry *)bh->b_data;
12581da177e4SLinus Torvalds 
12595e0f0017SMarcin Slusarz 	if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
12601ab92785Smarcin.slusarz@gmail.com 		struct buffer_head *ibh;
12611da177e4SLinus Torvalds 
12626d3d5e86SJan Kara 		ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
12631ab92785Smarcin.slusarz@gmail.com 		if (ident == TAG_IDENT_IE && ibh) {
12645ca4e4beSPekka Enberg 			struct kernel_lb_addr loc;
12651ab92785Smarcin.slusarz@gmail.com 			struct indirectEntry *ie;
12661da177e4SLinus Torvalds 
12671ab92785Smarcin.slusarz@gmail.com 			ie = (struct indirectEntry *)ibh->b_data;
12681da177e4SLinus Torvalds 			loc = lelb_to_cpu(ie->indirectICB.extLocation);
12691da177e4SLinus Torvalds 
1270c03aa9f6SJan Kara 			if (ie->indirectICB.extLength) {
12713bf25cb4SJan Kara 				brelse(ibh);
1272c03aa9f6SJan Kara 				memcpy(&iinfo->i_location, &loc,
1273c03aa9f6SJan Kara 				       sizeof(struct kernel_lb_addr));
1274c03aa9f6SJan Kara 				if (++indirections > UDF_MAX_ICB_NESTING) {
1275c03aa9f6SJan Kara 					udf_err(inode->i_sb,
1276c03aa9f6SJan Kara 						"too many ICBs in ICB hierarchy"
1277c03aa9f6SJan Kara 						" (max %d supported)\n",
1278c03aa9f6SJan Kara 						UDF_MAX_ICB_NESTING);
12796d3d5e86SJan Kara 					goto out;
12801ab92785Smarcin.slusarz@gmail.com 				}
12816d3d5e86SJan Kara 				brelse(bh);
1282c03aa9f6SJan Kara 				goto reread;
12831ab92785Smarcin.slusarz@gmail.com 			}
12841ab92785Smarcin.slusarz@gmail.com 		}
12853bf25cb4SJan Kara 		brelse(ibh);
12865e0f0017SMarcin Slusarz 	} else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1287fcbf7637SSteve Magnani 		udf_err(inode->i_sb, "unsupported strategy type: %u\n",
12881da177e4SLinus Torvalds 			le16_to_cpu(fe->icbTag.strategyType));
12896d3d5e86SJan Kara 		goto out;
12901da177e4SLinus Torvalds 	}
12915e0f0017SMarcin Slusarz 	if (fe->icbTag.strategyType == cpu_to_le16(4))
129248d6d8ffSMarcin Slusarz 		iinfo->i_strat4096 = 0;
12935e0f0017SMarcin Slusarz 	else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
129448d6d8ffSMarcin Slusarz 		iinfo->i_strat4096 = 1;
12951da177e4SLinus Torvalds 
129648d6d8ffSMarcin Slusarz 	iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
12974b11111aSMarcin Slusarz 							ICBTAG_FLAG_AD_MASK;
1298d288d958SJan Kara 	if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
1299d288d958SJan Kara 	    iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
1300d288d958SJan Kara 	    iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1301d288d958SJan Kara 		ret = -EIO;
1302d288d958SJan Kara 		goto out;
1303d288d958SJan Kara 	}
1304fc8033a3SJan Kara 	iinfo->i_hidden = hidden_inode;
130548d6d8ffSMarcin Slusarz 	iinfo->i_unique = 0;
130648d6d8ffSMarcin Slusarz 	iinfo->i_lenEAttr = 0;
130748d6d8ffSMarcin Slusarz 	iinfo->i_lenExtents = 0;
130848d6d8ffSMarcin Slusarz 	iinfo->i_lenAlloc = 0;
130948d6d8ffSMarcin Slusarz 	iinfo->i_next_alloc_block = 0;
131048d6d8ffSMarcin Slusarz 	iinfo->i_next_alloc_goal = 0;
13115e0f0017SMarcin Slusarz 	if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
131248d6d8ffSMarcin Slusarz 		iinfo->i_efe = 1;
131348d6d8ffSMarcin Slusarz 		iinfo->i_use = 0;
131479144954SJan Kara 		ret = udf_alloc_i_data(inode, bs -
13156d3d5e86SJan Kara 					sizeof(struct extendedFileEntry));
13166d3d5e86SJan Kara 		if (ret)
13176d3d5e86SJan Kara 			goto out;
1318382a2287SJan Kara 		memcpy(iinfo->i_data,
13194b11111aSMarcin Slusarz 		       bh->b_data + sizeof(struct extendedFileEntry),
132079144954SJan Kara 		       bs - sizeof(struct extendedFileEntry));
13215e0f0017SMarcin Slusarz 	} else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
132248d6d8ffSMarcin Slusarz 		iinfo->i_efe = 0;
132348d6d8ffSMarcin Slusarz 		iinfo->i_use = 0;
132479144954SJan Kara 		ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
13256d3d5e86SJan Kara 		if (ret)
13266d3d5e86SJan Kara 			goto out;
1327382a2287SJan Kara 		memcpy(iinfo->i_data,
1328c0b34438SMarcin Slusarz 		       bh->b_data + sizeof(struct fileEntry),
132979144954SJan Kara 		       bs - sizeof(struct fileEntry));
13305e0f0017SMarcin Slusarz 	} else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
133148d6d8ffSMarcin Slusarz 		iinfo->i_efe = 0;
133248d6d8ffSMarcin Slusarz 		iinfo->i_use = 1;
133348d6d8ffSMarcin Slusarz 		iinfo->i_lenAlloc = le32_to_cpu(
13344b11111aSMarcin Slusarz 				((struct unallocSpaceEntry *)bh->b_data)->
13354b11111aSMarcin Slusarz 				 lengthAllocDescs);
133679144954SJan Kara 		ret = udf_alloc_i_data(inode, bs -
13376d3d5e86SJan Kara 					sizeof(struct unallocSpaceEntry));
13386d3d5e86SJan Kara 		if (ret)
13396d3d5e86SJan Kara 			goto out;
1340382a2287SJan Kara 		memcpy(iinfo->i_data,
13414b11111aSMarcin Slusarz 		       bh->b_data + sizeof(struct unallocSpaceEntry),
134279144954SJan Kara 		       bs - sizeof(struct unallocSpaceEntry));
13436d3d5e86SJan Kara 		return 0;
13441da177e4SLinus Torvalds 	}
13451da177e4SLinus Torvalds 
13466d3d5e86SJan Kara 	ret = -EIO;
1347c03cad24SJan Kara 	read_lock(&sbi->s_cred_lock);
13480c9850f4SJan Kara 	uid = le32_to_cpu(fe->uid);
13490c9850f4SJan Kara 	if (uid == UDF_INVALID_ID ||
1350ca76d2d8SCyrill Gorcunov 	    UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
13510c9850f4SJan Kara 		inode->i_uid = sbi->s_uid;
13520c9850f4SJan Kara 	else
13530c9850f4SJan Kara 		i_uid_write(inode, uid);
13541da177e4SLinus Torvalds 
13550c9850f4SJan Kara 	gid = le32_to_cpu(fe->gid);
13560c9850f4SJan Kara 	if (gid == UDF_INVALID_ID ||
1357ca76d2d8SCyrill Gorcunov 	    UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
13580c9850f4SJan Kara 		inode->i_gid = sbi->s_gid;
13590c9850f4SJan Kara 	else
13600c9850f4SJan Kara 		i_gid_write(inode, gid);
13611da177e4SLinus Torvalds 
13627ac9bcd5SMarcin Slusarz 	if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
136387bc730cSMarcin Slusarz 			sbi->s_fmode != UDF_INVALID_MODE)
13647ac9bcd5SMarcin Slusarz 		inode->i_mode = sbi->s_fmode;
13657ac9bcd5SMarcin Slusarz 	else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
136687bc730cSMarcin Slusarz 			sbi->s_dmode != UDF_INVALID_MODE)
13677ac9bcd5SMarcin Slusarz 		inode->i_mode = sbi->s_dmode;
13687ac9bcd5SMarcin Slusarz 	else
13691da177e4SLinus Torvalds 		inode->i_mode = udf_convert_permissions(fe);
13707ac9bcd5SMarcin Slusarz 	inode->i_mode &= ~sbi->s_umask;
1371c3367a1bSSteven J. Magnani 	iinfo->i_extraPerms = le32_to_cpu(fe->permissions) & ~FE_MAPPED_PERMS;
1372c3367a1bSSteven J. Magnani 
1373c03cad24SJan Kara 	read_unlock(&sbi->s_cred_lock);
1374c03cad24SJan Kara 
1375bfe86848SMiklos Szeredi 	link_count = le16_to_cpu(fe->fileLinkCount);
13764071b913SJan Kara 	if (!link_count) {
13776174c2ebSJan Kara 		if (!hidden_inode) {
13784071b913SJan Kara 			ret = -ESTALE;
13794071b913SJan Kara 			goto out;
13804071b913SJan Kara 		}
13816174c2ebSJan Kara 		link_count = 1;
13826174c2ebSJan Kara 	}
1383bfe86848SMiklos Szeredi 	set_nlink(inode, link_count);
1384c03cad24SJan Kara 
1385c03cad24SJan Kara 	inode->i_size = le64_to_cpu(fe->informationLength);
1386c03cad24SJan Kara 	iinfo->i_lenExtents = inode->i_size;
13871da177e4SLinus Torvalds 
138848d6d8ffSMarcin Slusarz 	if (iinfo->i_efe == 0) {
13891da177e4SLinus Torvalds 		inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
13901da177e4SLinus Torvalds 			(inode->i_sb->s_blocksize_bits - 9);
13911da177e4SLinus Torvalds 
1392c3b9cecdSArnd Bergmann 		udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime);
1393c3b9cecdSArnd Bergmann 		udf_disk_stamp_to_time(&inode->i_mtime, fe->modificationTime);
1394c3b9cecdSArnd Bergmann 		udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime);
13951da177e4SLinus Torvalds 
139648d6d8ffSMarcin Slusarz 		iinfo->i_unique = le64_to_cpu(fe->uniqueID);
139748d6d8ffSMarcin Slusarz 		iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
139848d6d8ffSMarcin Slusarz 		iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1399d5e2cf07SSteve Nickel 		iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1400ab9a3a73SSteven J. Magnani 		iinfo->i_streamdir = 0;
1401ab9a3a73SSteven J. Magnani 		iinfo->i_lenStreams = 0;
1402cb00ea35SCyrill Gorcunov 	} else {
14031da177e4SLinus Torvalds 		inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
14041da177e4SLinus Torvalds 		    (inode->i_sb->s_blocksize_bits - 9);
14051da177e4SLinus Torvalds 
1406c3b9cecdSArnd Bergmann 		udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime);
1407c3b9cecdSArnd Bergmann 		udf_disk_stamp_to_time(&inode->i_mtime, efe->modificationTime);
14080220eddaSDeepa Dinamani 		udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime);
1409c3b9cecdSArnd Bergmann 		udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime);
14101da177e4SLinus Torvalds 
141148d6d8ffSMarcin Slusarz 		iinfo->i_unique = le64_to_cpu(efe->uniqueID);
141248d6d8ffSMarcin Slusarz 		iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
141348d6d8ffSMarcin Slusarz 		iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1414d5e2cf07SSteve Nickel 		iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
1415ab9a3a73SSteven J. Magnani 
1416ab9a3a73SSteven J. Magnani 		/* Named streams */
1417ab9a3a73SSteven J. Magnani 		iinfo->i_streamdir = (efe->streamDirectoryICB.extLength != 0);
1418ab9a3a73SSteven J. Magnani 		iinfo->i_locStreamdir =
1419ab9a3a73SSteven J. Magnani 			lelb_to_cpu(efe->streamDirectoryICB.extLocation);
1420ab9a3a73SSteven J. Magnani 		iinfo->i_lenStreams = le64_to_cpu(efe->objectSize);
1421ab9a3a73SSteven J. Magnani 		if (iinfo->i_lenStreams >= inode->i_size)
1422ab9a3a73SSteven J. Magnani 			iinfo->i_lenStreams -= inode->i_size;
1423ab9a3a73SSteven J. Magnani 		else
1424ab9a3a73SSteven J. Magnani 			iinfo->i_lenStreams = 0;
14251da177e4SLinus Torvalds 	}
1426470cca56SJan Kara 	inode->i_generation = iinfo->i_unique;
14271da177e4SLinus Torvalds 
142823b133bdSJan Kara 	/*
142923b133bdSJan Kara 	 * Sanity check length of allocation descriptors and extended attrs to
143023b133bdSJan Kara 	 * avoid integer overflows
143123b133bdSJan Kara 	 */
143223b133bdSJan Kara 	if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
143323b133bdSJan Kara 		goto out;
143423b133bdSJan Kara 	/* Now do exact checks */
143523b133bdSJan Kara 	if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
143623b133bdSJan Kara 		goto out;
1437e159332bSJan Kara 	/* Sanity checks for files in ICB so that we don't get confused later */
1438e159332bSJan Kara 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1439e159332bSJan Kara 		/*
1440e159332bSJan Kara 		 * For file in ICB data is stored in allocation descriptor
1441e159332bSJan Kara 		 * so sizes should match
1442e159332bSJan Kara 		 */
1443e159332bSJan Kara 		if (iinfo->i_lenAlloc != inode->i_size)
1444e159332bSJan Kara 			goto out;
1445e159332bSJan Kara 		/* File in ICB has to fit in there... */
144679144954SJan Kara 		if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
1447e159332bSJan Kara 			goto out;
1448e159332bSJan Kara 	}
1449e159332bSJan Kara 
1450cb00ea35SCyrill Gorcunov 	switch (fe->icbTag.fileType) {
14511da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_DIRECTORY:
14521da177e4SLinus Torvalds 		inode->i_op = &udf_dir_inode_operations;
14531da177e4SLinus Torvalds 		inode->i_fop = &udf_dir_operations;
14541da177e4SLinus Torvalds 		inode->i_mode |= S_IFDIR;
1455d8c76e6fSDave Hansen 		inc_nlink(inode);
14561da177e4SLinus Torvalds 		break;
14571da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_REALTIME:
14581da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_REGULAR:
14591da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_UNDEF:
1460742e1795SJan Kara 	case ICBTAG_FILE_TYPE_VAT20:
146148d6d8ffSMarcin Slusarz 		if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
14621da177e4SLinus Torvalds 			inode->i_data.a_ops = &udf_adinicb_aops;
14631da177e4SLinus Torvalds 		else
14641da177e4SLinus Torvalds 			inode->i_data.a_ops = &udf_aops;
14651da177e4SLinus Torvalds 		inode->i_op = &udf_file_inode_operations;
14661da177e4SLinus Torvalds 		inode->i_fop = &udf_file_operations;
14671da177e4SLinus Torvalds 		inode->i_mode |= S_IFREG;
14681da177e4SLinus Torvalds 		break;
14691da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_BLOCK:
14701da177e4SLinus Torvalds 		inode->i_mode |= S_IFBLK;
14711da177e4SLinus Torvalds 		break;
14721da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_CHAR:
14731da177e4SLinus Torvalds 		inode->i_mode |= S_IFCHR;
14741da177e4SLinus Torvalds 		break;
14751da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_FIFO:
14761da177e4SLinus Torvalds 		init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
14771da177e4SLinus Torvalds 		break;
14781da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_SOCKET:
14791da177e4SLinus Torvalds 		init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
14801da177e4SLinus Torvalds 		break;
14811da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_SYMLINK:
14821da177e4SLinus Torvalds 		inode->i_data.a_ops = &udf_symlink_aops;
1483ad4d0532SJan Kara 		inode->i_op = &udf_symlink_inode_operations;
148421fc61c7SAl Viro 		inode_nohighmem(inode);
14856ff6b2b3SFabian Frederick 		inode->i_mode = S_IFLNK | 0777;
14861da177e4SLinus Torvalds 		break;
1487bfb257a5SJan Kara 	case ICBTAG_FILE_TYPE_MAIN:
1488bfb257a5SJan Kara 		udf_debug("METADATA FILE-----\n");
1489bfb257a5SJan Kara 		break;
1490bfb257a5SJan Kara 	case ICBTAG_FILE_TYPE_MIRROR:
1491bfb257a5SJan Kara 		udf_debug("METADATA MIRROR FILE-----\n");
1492bfb257a5SJan Kara 		break;
1493bfb257a5SJan Kara 	case ICBTAG_FILE_TYPE_BITMAP:
1494bfb257a5SJan Kara 		udf_debug("METADATA BITMAP FILE-----\n");
1495bfb257a5SJan Kara 		break;
14961da177e4SLinus Torvalds 	default:
1497fcbf7637SSteve Magnani 		udf_err(inode->i_sb, "(ino %lu) failed unknown file type=%u\n",
149878ace70cSJoe Perches 			inode->i_ino, fe->icbTag.fileType);
14996d3d5e86SJan Kara 		goto out;
15001da177e4SLinus Torvalds 	}
1501cb00ea35SCyrill Gorcunov 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
15024b11111aSMarcin Slusarz 		struct deviceSpec *dsea =
15034b11111aSMarcin Slusarz 			(struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1504cb00ea35SCyrill Gorcunov 		if (dsea) {
1505cb00ea35SCyrill Gorcunov 			init_special_inode(inode, inode->i_mode,
150628de7948SCyrill Gorcunov 				MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
150728de7948SCyrill Gorcunov 				      le32_to_cpu(dsea->minorDeviceIdent)));
15081da177e4SLinus Torvalds 			/* Developer ID ??? */
15094b11111aSMarcin Slusarz 		} else
15106d3d5e86SJan Kara 			goto out;
15111da177e4SLinus Torvalds 	}
15126d3d5e86SJan Kara 	ret = 0;
15136d3d5e86SJan Kara out:
1514bb7720a0SJan Kara 	brelse(bh);
15156d3d5e86SJan Kara 	return ret;
15161da177e4SLinus Torvalds }
15171da177e4SLinus Torvalds 
1518647bd61aSCyrill Gorcunov static int udf_alloc_i_data(struct inode *inode, size_t size)
1519647bd61aSCyrill Gorcunov {
152048d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
1521382a2287SJan Kara 	iinfo->i_data = kmalloc(size, GFP_KERNEL);
1522382a2287SJan Kara 	if (!iinfo->i_data)
1523647bd61aSCyrill Gorcunov 		return -ENOMEM;
1524647bd61aSCyrill Gorcunov 	return 0;
1525647bd61aSCyrill Gorcunov }
1526647bd61aSCyrill Gorcunov 
1527faa17292SAl Viro static umode_t udf_convert_permissions(struct fileEntry *fe)
15281da177e4SLinus Torvalds {
1529faa17292SAl Viro 	umode_t mode;
15301da177e4SLinus Torvalds 	uint32_t permissions;
15311da177e4SLinus Torvalds 	uint32_t flags;
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds 	permissions = le32_to_cpu(fe->permissions);
15341da177e4SLinus Torvalds 	flags = le16_to_cpu(fe->icbTag.flags);
15351da177e4SLinus Torvalds 
15366ff6b2b3SFabian Frederick 	mode =	((permissions) & 0007) |
15376ff6b2b3SFabian Frederick 		((permissions >> 2) & 0070) |
15386ff6b2b3SFabian Frederick 		((permissions >> 4) & 0700) |
15391da177e4SLinus Torvalds 		((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
15401da177e4SLinus Torvalds 		((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
15411da177e4SLinus Torvalds 		((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds 	return mode;
15441da177e4SLinus Torvalds }
15451da177e4SLinus Torvalds 
1546c3367a1bSSteven J. Magnani void udf_update_extra_perms(struct inode *inode, umode_t mode)
1547c3367a1bSSteven J. Magnani {
1548c3367a1bSSteven J. Magnani 	struct udf_inode_info *iinfo = UDF_I(inode);
1549c3367a1bSSteven J. Magnani 
1550c3367a1bSSteven J. Magnani 	/*
1551c3367a1bSSteven J. Magnani 	 * UDF 2.01 sec. 3.3.3.3 Note 2:
1552c3367a1bSSteven J. Magnani 	 * In Unix, delete permission tracks write
1553c3367a1bSSteven J. Magnani 	 */
1554c3367a1bSSteven J. Magnani 	iinfo->i_extraPerms &= ~FE_DELETE_PERMS;
1555c3367a1bSSteven J. Magnani 	if (mode & 0200)
1556c3367a1bSSteven J. Magnani 		iinfo->i_extraPerms |= FE_PERM_U_DELETE;
1557c3367a1bSSteven J. Magnani 	if (mode & 0020)
1558c3367a1bSSteven J. Magnani 		iinfo->i_extraPerms |= FE_PERM_G_DELETE;
1559c3367a1bSSteven J. Magnani 	if (mode & 0002)
1560c3367a1bSSteven J. Magnani 		iinfo->i_extraPerms |= FE_PERM_O_DELETE;
1561c3367a1bSSteven J. Magnani }
1562c3367a1bSSteven J. Magnani 
1563a9185b41SChristoph Hellwig int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
15641da177e4SLinus Torvalds {
156549521de1SJan Kara 	return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
15661da177e4SLinus Torvalds }
15671da177e4SLinus Torvalds 
156849521de1SJan Kara static int udf_sync_inode(struct inode *inode)
15691da177e4SLinus Torvalds {
15701da177e4SLinus Torvalds 	return udf_update_inode(inode, 1);
15711da177e4SLinus Torvalds }
15721da177e4SLinus Torvalds 
1573c3b9cecdSArnd Bergmann static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec64 time)
15747ed0fbd7SFabian Frederick {
15757ed0fbd7SFabian Frederick 	if (iinfo->i_crtime.tv_sec > time.tv_sec ||
15767ed0fbd7SFabian Frederick 	    (iinfo->i_crtime.tv_sec == time.tv_sec &&
15777ed0fbd7SFabian Frederick 	     iinfo->i_crtime.tv_nsec > time.tv_nsec))
15787ed0fbd7SFabian Frederick 		iinfo->i_crtime = time;
15797ed0fbd7SFabian Frederick }
15807ed0fbd7SFabian Frederick 
1581cb00ea35SCyrill Gorcunov static int udf_update_inode(struct inode *inode, int do_sync)
15821da177e4SLinus Torvalds {
15831da177e4SLinus Torvalds 	struct buffer_head *bh = NULL;
15841da177e4SLinus Torvalds 	struct fileEntry *fe;
15851da177e4SLinus Torvalds 	struct extendedFileEntry *efe;
1586b2527bfaSSteve Nickel 	uint64_t lb_recorded;
15871da177e4SLinus Torvalds 	uint32_t udfperms;
15881da177e4SLinus Torvalds 	uint16_t icbflags;
15891da177e4SLinus Torvalds 	uint16_t crclen;
15901da177e4SLinus Torvalds 	int err = 0;
15916c79e987SMarcin Slusarz 	struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
15924b11111aSMarcin Slusarz 	unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
159348d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
15941da177e4SLinus Torvalds 
15955833ded9SJan Kara 	bh = udf_tgetblk(inode->i_sb,
15965833ded9SJan Kara 			udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1597cb00ea35SCyrill Gorcunov 	if (!bh) {
1598aae917cdSJan Kara 		udf_debug("getblk failure\n");
15990fd2ba36SChangwoo Min 		return -EIO;
16001da177e4SLinus Torvalds 	}
16011da177e4SLinus Torvalds 
1602aae917cdSJan Kara 	lock_buffer(bh);
1603aae917cdSJan Kara 	memset(bh->b_data, 0, inode->i_sb->s_blocksize);
16041da177e4SLinus Torvalds 	fe = (struct fileEntry *)bh->b_data;
16051da177e4SLinus Torvalds 	efe = (struct extendedFileEntry *)bh->b_data;
16061da177e4SLinus Torvalds 
1607aae917cdSJan Kara 	if (iinfo->i_use) {
16081da177e4SLinus Torvalds 		struct unallocSpaceEntry *use =
16091da177e4SLinus Torvalds 			(struct unallocSpaceEntry *)bh->b_data;
16101da177e4SLinus Torvalds 
161148d6d8ffSMarcin Slusarz 		use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
16124b11111aSMarcin Slusarz 		memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1613382a2287SJan Kara 		       iinfo->i_data, inode->i_sb->s_blocksize -
16144b11111aSMarcin Slusarz 					sizeof(struct unallocSpaceEntry));
1615aae917cdSJan Kara 		use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
161670f19f58SSteven J. Magnani 		crclen = sizeof(struct unallocSpaceEntry);
16171da177e4SLinus Torvalds 
161870f19f58SSteven J. Magnani 		goto finish;
16191da177e4SLinus Torvalds 	}
16201da177e4SLinus Torvalds 
16214d6660ebSPhillip Susi 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
16220c9850f4SJan Kara 		fe->uid = cpu_to_le32(UDF_INVALID_ID);
1623cb00ea35SCyrill Gorcunov 	else
1624c2ba138aSEric W. Biederman 		fe->uid = cpu_to_le32(i_uid_read(inode));
16251da177e4SLinus Torvalds 
16264d6660ebSPhillip Susi 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
16270c9850f4SJan Kara 		fe->gid = cpu_to_le32(UDF_INVALID_ID);
1628cb00ea35SCyrill Gorcunov 	else
1629c2ba138aSEric W. Biederman 		fe->gid = cpu_to_le32(i_gid_read(inode));
16301da177e4SLinus Torvalds 
16316ff6b2b3SFabian Frederick 	udfperms = ((inode->i_mode & 0007)) |
16326ff6b2b3SFabian Frederick 		   ((inode->i_mode & 0070) << 2) |
16336ff6b2b3SFabian Frederick 		   ((inode->i_mode & 0700) << 4);
16341da177e4SLinus Torvalds 
1635c3367a1bSSteven J. Magnani 	udfperms |= iinfo->i_extraPerms;
16361da177e4SLinus Torvalds 	fe->permissions = cpu_to_le32(udfperms);
16371da177e4SLinus Torvalds 
16388a70ee33SJan Kara 	if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
16391da177e4SLinus Torvalds 		fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1640fc8033a3SJan Kara 	else {
1641fc8033a3SJan Kara 		if (iinfo->i_hidden)
1642fc8033a3SJan Kara 			fe->fileLinkCount = cpu_to_le16(0);
16431da177e4SLinus Torvalds 		else
16441da177e4SLinus Torvalds 			fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1645fc8033a3SJan Kara 	}
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 	fe->informationLength = cpu_to_le64(inode->i_size);
16481da177e4SLinus Torvalds 
1649cb00ea35SCyrill Gorcunov 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
16505ca4e4beSPekka Enberg 		struct regid *eid;
165128de7948SCyrill Gorcunov 		struct deviceSpec *dsea =
165228de7948SCyrill Gorcunov 			(struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1653cb00ea35SCyrill Gorcunov 		if (!dsea) {
16541da177e4SLinus Torvalds 			dsea = (struct deviceSpec *)
16551da177e4SLinus Torvalds 				udf_add_extendedattr(inode,
16561da177e4SLinus Torvalds 						     sizeof(struct deviceSpec) +
16575ca4e4beSPekka Enberg 						     sizeof(struct regid), 12, 0x3);
16581da177e4SLinus Torvalds 			dsea->attrType = cpu_to_le32(12);
16591da177e4SLinus Torvalds 			dsea->attrSubtype = 1;
16604b11111aSMarcin Slusarz 			dsea->attrLength = cpu_to_le32(
16614b11111aSMarcin Slusarz 						sizeof(struct deviceSpec) +
16625ca4e4beSPekka Enberg 						sizeof(struct regid));
16635ca4e4beSPekka Enberg 			dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
16641da177e4SLinus Torvalds 		}
16655ca4e4beSPekka Enberg 		eid = (struct regid *)dsea->impUse;
1666033c9da0SMarkus Elfring 		memset(eid, 0, sizeof(*eid));
16671da177e4SLinus Torvalds 		strcpy(eid->ident, UDF_ID_DEVELOPER);
16681da177e4SLinus Torvalds 		eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
16691da177e4SLinus Torvalds 		eid->identSuffix[1] = UDF_OS_ID_LINUX;
16701da177e4SLinus Torvalds 		dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
16711da177e4SLinus Torvalds 		dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
16721da177e4SLinus Torvalds 	}
16731da177e4SLinus Torvalds 
1674b2527bfaSSteve Nickel 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1675b2527bfaSSteve Nickel 		lb_recorded = 0; /* No extents => no blocks! */
1676b2527bfaSSteve Nickel 	else
1677b2527bfaSSteve Nickel 		lb_recorded =
1678b2527bfaSSteve Nickel 			(inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1679b2527bfaSSteve Nickel 			(blocksize_bits - 9);
1680b2527bfaSSteve Nickel 
168148d6d8ffSMarcin Slusarz 	if (iinfo->i_efe == 0) {
1682c0b34438SMarcin Slusarz 		memcpy(bh->b_data + sizeof(struct fileEntry),
1683382a2287SJan Kara 		       iinfo->i_data,
1684cb00ea35SCyrill Gorcunov 		       inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1685b2527bfaSSteve Nickel 		fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
16861da177e4SLinus Torvalds 
1687c3b9cecdSArnd Bergmann 		udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1688c3b9cecdSArnd Bergmann 		udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1689c3b9cecdSArnd Bergmann 		udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
16905ca4e4beSPekka Enberg 		memset(&(fe->impIdent), 0, sizeof(struct regid));
16911da177e4SLinus Torvalds 		strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
16921da177e4SLinus Torvalds 		fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
16931da177e4SLinus Torvalds 		fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
169448d6d8ffSMarcin Slusarz 		fe->uniqueID = cpu_to_le64(iinfo->i_unique);
169548d6d8ffSMarcin Slusarz 		fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
169648d6d8ffSMarcin Slusarz 		fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1697d5e2cf07SSteve Nickel 		fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
16981da177e4SLinus Torvalds 		fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
16991da177e4SLinus Torvalds 		crclen = sizeof(struct fileEntry);
1700cb00ea35SCyrill Gorcunov 	} else {
17014b11111aSMarcin Slusarz 		memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1702382a2287SJan Kara 		       iinfo->i_data,
17034b11111aSMarcin Slusarz 		       inode->i_sb->s_blocksize -
17044b11111aSMarcin Slusarz 					sizeof(struct extendedFileEntry));
1705ab9a3a73SSteven J. Magnani 		efe->objectSize =
1706ab9a3a73SSteven J. Magnani 			cpu_to_le64(inode->i_size + iinfo->i_lenStreams);
1707b2527bfaSSteve Nickel 		efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
17081da177e4SLinus Torvalds 
1709ab9a3a73SSteven J. Magnani 		if (iinfo->i_streamdir) {
1710ab9a3a73SSteven J. Magnani 			struct long_ad *icb_lad = &efe->streamDirectoryICB;
1711ab9a3a73SSteven J. Magnani 
1712ab9a3a73SSteven J. Magnani 			icb_lad->extLocation =
1713ab9a3a73SSteven J. Magnani 				cpu_to_lelb(iinfo->i_locStreamdir);
1714ab9a3a73SSteven J. Magnani 			icb_lad->extLength =
1715ab9a3a73SSteven J. Magnani 				cpu_to_le32(inode->i_sb->s_blocksize);
1716ab9a3a73SSteven J. Magnani 		}
1717ab9a3a73SSteven J. Magnani 
1718c3b9cecdSArnd Bergmann 		udf_adjust_time(iinfo, inode->i_atime);
1719c3b9cecdSArnd Bergmann 		udf_adjust_time(iinfo, inode->i_mtime);
1720c3b9cecdSArnd Bergmann 		udf_adjust_time(iinfo, inode->i_ctime);
17211da177e4SLinus Torvalds 
1722c3b9cecdSArnd Bergmann 		udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1723c3b9cecdSArnd Bergmann 		udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
172456774805SMarcin Slusarz 		udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1725c3b9cecdSArnd Bergmann 		udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
17261da177e4SLinus Torvalds 
1727033c9da0SMarkus Elfring 		memset(&(efe->impIdent), 0, sizeof(efe->impIdent));
17281da177e4SLinus Torvalds 		strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
17291da177e4SLinus Torvalds 		efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
17301da177e4SLinus Torvalds 		efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
173148d6d8ffSMarcin Slusarz 		efe->uniqueID = cpu_to_le64(iinfo->i_unique);
173248d6d8ffSMarcin Slusarz 		efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
173348d6d8ffSMarcin Slusarz 		efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1734d5e2cf07SSteve Nickel 		efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
17351da177e4SLinus Torvalds 		efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
17361da177e4SLinus Torvalds 		crclen = sizeof(struct extendedFileEntry);
17371da177e4SLinus Torvalds 	}
173870f19f58SSteven J. Magnani 
173970f19f58SSteven J. Magnani finish:
174048d6d8ffSMarcin Slusarz 	if (iinfo->i_strat4096) {
17411da177e4SLinus Torvalds 		fe->icbTag.strategyType = cpu_to_le16(4096);
17421da177e4SLinus Torvalds 		fe->icbTag.strategyParameter = cpu_to_le16(1);
17431da177e4SLinus Torvalds 		fe->icbTag.numEntries = cpu_to_le16(2);
1744cb00ea35SCyrill Gorcunov 	} else {
17451da177e4SLinus Torvalds 		fe->icbTag.strategyType = cpu_to_le16(4);
17461da177e4SLinus Torvalds 		fe->icbTag.numEntries = cpu_to_le16(1);
17471da177e4SLinus Torvalds 	}
17481da177e4SLinus Torvalds 
174970f19f58SSteven J. Magnani 	if (iinfo->i_use)
175070f19f58SSteven J. Magnani 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
175170f19f58SSteven J. Magnani 	else if (S_ISDIR(inode->i_mode))
17521da177e4SLinus Torvalds 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
17531da177e4SLinus Torvalds 	else if (S_ISREG(inode->i_mode))
17541da177e4SLinus Torvalds 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
17551da177e4SLinus Torvalds 	else if (S_ISLNK(inode->i_mode))
17561da177e4SLinus Torvalds 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
17571da177e4SLinus Torvalds 	else if (S_ISBLK(inode->i_mode))
17581da177e4SLinus Torvalds 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
17591da177e4SLinus Torvalds 	else if (S_ISCHR(inode->i_mode))
17601da177e4SLinus Torvalds 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
17611da177e4SLinus Torvalds 	else if (S_ISFIFO(inode->i_mode))
17621da177e4SLinus Torvalds 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
17631da177e4SLinus Torvalds 	else if (S_ISSOCK(inode->i_mode))
17641da177e4SLinus Torvalds 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
17651da177e4SLinus Torvalds 
176648d6d8ffSMarcin Slusarz 	icbflags =	iinfo->i_alloc_type |
17671da177e4SLinus Torvalds 			((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
17681da177e4SLinus Torvalds 			((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
17691da177e4SLinus Torvalds 			((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
17701da177e4SLinus Torvalds 			(le16_to_cpu(fe->icbTag.flags) &
17711da177e4SLinus Torvalds 				~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
17721da177e4SLinus Torvalds 				ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds 	fe->icbTag.flags = cpu_to_le16(icbflags);
17756c79e987SMarcin Slusarz 	if (sbi->s_udfrev >= 0x0200)
17761da177e4SLinus Torvalds 		fe->descTag.descVersion = cpu_to_le16(3);
17771da177e4SLinus Torvalds 	else
17781da177e4SLinus Torvalds 		fe->descTag.descVersion = cpu_to_le16(2);
17796c79e987SMarcin Slusarz 	fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
17804b11111aSMarcin Slusarz 	fe->descTag.tagLocation = cpu_to_le32(
178148d6d8ffSMarcin Slusarz 					iinfo->i_location.logicalBlockNum);
1782aae917cdSJan Kara 	crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
17831da177e4SLinus Torvalds 	fe->descTag.descCRCLength = cpu_to_le16(crclen);
17845ca4e4beSPekka Enberg 	fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1785f845fcedSBob Copeland 						  crclen));
17863f2587bbSMarcin Slusarz 	fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
17871da177e4SLinus Torvalds 
17885833ded9SJan Kara 	set_buffer_uptodate(bh);
1789aae917cdSJan Kara 	unlock_buffer(bh);
1790aae917cdSJan Kara 
17911da177e4SLinus Torvalds 	/* write the data blocks */
17921da177e4SLinus Torvalds 	mark_buffer_dirty(bh);
1793cb00ea35SCyrill Gorcunov 	if (do_sync) {
17941da177e4SLinus Torvalds 		sync_dirty_buffer(bh);
1795aae917cdSJan Kara 		if (buffer_write_io_error(bh)) {
179678ace70cSJoe Perches 			udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
17974b11111aSMarcin Slusarz 				 inode->i_ino);
17981da177e4SLinus Torvalds 			err = -EIO;
17991da177e4SLinus Torvalds 		}
18001da177e4SLinus Torvalds 	}
18013bf25cb4SJan Kara 	brelse(bh);
180228de7948SCyrill Gorcunov 
18031da177e4SLinus Torvalds 	return err;
18041da177e4SLinus Torvalds }
18051da177e4SLinus Torvalds 
18066174c2ebSJan Kara struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
18076174c2ebSJan Kara 			 bool hidden_inode)
18081da177e4SLinus Torvalds {
18091da177e4SLinus Torvalds 	unsigned long block = udf_get_lb_pblock(sb, ino, 0);
18101da177e4SLinus Torvalds 	struct inode *inode = iget_locked(sb, block);
18116d3d5e86SJan Kara 	int err;
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds 	if (!inode)
18146d3d5e86SJan Kara 		return ERR_PTR(-ENOMEM);
18151da177e4SLinus Torvalds 
1816*85a37983SJan Kara 	if (!(inode->i_state & I_NEW)) {
1817*85a37983SJan Kara 		if (UDF_I(inode)->i_hidden != hidden_inode) {
1818*85a37983SJan Kara 			iput(inode);
1819*85a37983SJan Kara 			return ERR_PTR(-EFSCORRUPTED);
1820*85a37983SJan Kara 		}
18211da177e4SLinus Torvalds 		return inode;
1822*85a37983SJan Kara 	}
18231da177e4SLinus Torvalds 
18246d3d5e86SJan Kara 	memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
18256174c2ebSJan Kara 	err = udf_read_inode(inode, hidden_inode);
18266d3d5e86SJan Kara 	if (err < 0) {
18276d3d5e86SJan Kara 		iget_failed(inode);
18286d3d5e86SJan Kara 		return ERR_PTR(err);
18296d3d5e86SJan Kara 	}
18306d3d5e86SJan Kara 	unlock_new_inode(inode);
18316d3d5e86SJan Kara 
18326d3d5e86SJan Kara 	return inode;
18331da177e4SLinus Torvalds }
18341da177e4SLinus Torvalds 
1835b490bdd6SSteve Magnani int udf_setup_indirect_aext(struct inode *inode, udf_pblk_t block,
1836fcea62baSJan Kara 			    struct extent_position *epos)
1837fcea62baSJan Kara {
1838fcea62baSJan Kara 	struct super_block *sb = inode->i_sb;
1839fcea62baSJan Kara 	struct buffer_head *bh;
1840fcea62baSJan Kara 	struct allocExtDesc *aed;
1841fcea62baSJan Kara 	struct extent_position nepos;
1842fcea62baSJan Kara 	struct kernel_lb_addr neloc;
1843fcea62baSJan Kara 	int ver, adsize;
1844fcea62baSJan Kara 
1845fcea62baSJan Kara 	if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1846fcea62baSJan Kara 		adsize = sizeof(struct short_ad);
1847fcea62baSJan Kara 	else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1848fcea62baSJan Kara 		adsize = sizeof(struct long_ad);
18494f1b1519SArnd Bergmann 	else
18504f1b1519SArnd Bergmann 		return -EIO;
1851fcea62baSJan Kara 
1852fcea62baSJan Kara 	neloc.logicalBlockNum = block;
1853fcea62baSJan Kara 	neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1854fcea62baSJan Kara 
1855fcea62baSJan Kara 	bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1856fcea62baSJan Kara 	if (!bh)
1857fcea62baSJan Kara 		return -EIO;
1858fcea62baSJan Kara 	lock_buffer(bh);
1859fcea62baSJan Kara 	memset(bh->b_data, 0x00, sb->s_blocksize);
1860fcea62baSJan Kara 	set_buffer_uptodate(bh);
1861fcea62baSJan Kara 	unlock_buffer(bh);
1862fcea62baSJan Kara 	mark_buffer_dirty_inode(bh, inode);
1863fcea62baSJan Kara 
1864fcea62baSJan Kara 	aed = (struct allocExtDesc *)(bh->b_data);
1865fcea62baSJan Kara 	if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1866fcea62baSJan Kara 		aed->previousAllocExtLocation =
1867fcea62baSJan Kara 				cpu_to_le32(epos->block.logicalBlockNum);
1868fcea62baSJan Kara 	}
1869fcea62baSJan Kara 	aed->lengthAllocDescs = cpu_to_le32(0);
1870fcea62baSJan Kara 	if (UDF_SB(sb)->s_udfrev >= 0x0200)
1871fcea62baSJan Kara 		ver = 3;
1872fcea62baSJan Kara 	else
1873fcea62baSJan Kara 		ver = 2;
1874fcea62baSJan Kara 	udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1875fcea62baSJan Kara 		    sizeof(struct tag));
1876fcea62baSJan Kara 
1877fcea62baSJan Kara 	nepos.block = neloc;
1878fcea62baSJan Kara 	nepos.offset = sizeof(struct allocExtDesc);
1879fcea62baSJan Kara 	nepos.bh = bh;
1880fcea62baSJan Kara 
1881fcea62baSJan Kara 	/*
1882fcea62baSJan Kara 	 * Do we have to copy current last extent to make space for indirect
1883fcea62baSJan Kara 	 * one?
1884fcea62baSJan Kara 	 */
1885fcea62baSJan Kara 	if (epos->offset + adsize > sb->s_blocksize) {
1886fcea62baSJan Kara 		struct kernel_lb_addr cp_loc;
1887fcea62baSJan Kara 		uint32_t cp_len;
1888fcea62baSJan Kara 		int cp_type;
1889fcea62baSJan Kara 
1890fcea62baSJan Kara 		epos->offset -= adsize;
1891fcea62baSJan Kara 		cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1892fcea62baSJan Kara 		cp_len |= ((uint32_t)cp_type) << 30;
1893fcea62baSJan Kara 
1894fcea62baSJan Kara 		__udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1895fcea62baSJan Kara 		udf_write_aext(inode, epos, &nepos.block,
1896800552ceSPali Rohár 			       sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
1897fcea62baSJan Kara 	} else {
1898fcea62baSJan Kara 		__udf_add_aext(inode, epos, &nepos.block,
1899800552ceSPali Rohár 			       sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
1900fcea62baSJan Kara 	}
1901fcea62baSJan Kara 
1902fcea62baSJan Kara 	brelse(epos->bh);
1903fcea62baSJan Kara 	*epos = nepos;
1904fcea62baSJan Kara 
1905fcea62baSJan Kara 	return 0;
1906fcea62baSJan Kara }
1907fcea62baSJan Kara 
1908fcea62baSJan Kara /*
1909fcea62baSJan Kara  * Append extent at the given position - should be the first free one in inode
1910fcea62baSJan Kara  * / indirect extent. This function assumes there is enough space in the inode
1911fcea62baSJan Kara  * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1912fcea62baSJan Kara  */
1913fcea62baSJan Kara int __udf_add_aext(struct inode *inode, struct extent_position *epos,
191497e961fdSPekka Enberg 		   struct kernel_lb_addr *eloc, uint32_t elen, int inc)
19151da177e4SLinus Torvalds {
191648d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
1917fcea62baSJan Kara 	struct allocExtDesc *aed;
1918fcea62baSJan Kara 	int adsize;
19191da177e4SLinus Torvalds 
192048d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
19215ca4e4beSPekka Enberg 		adsize = sizeof(struct short_ad);
192248d6d8ffSMarcin Slusarz 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
19235ca4e4beSPekka Enberg 		adsize = sizeof(struct long_ad);
19241da177e4SLinus Torvalds 	else
19257e49b6f2SJan Kara 		return -EIO;
19261da177e4SLinus Torvalds 
1927fcea62baSJan Kara 	if (!epos->bh) {
1928fcea62baSJan Kara 		WARN_ON(iinfo->i_lenAlloc !=
1929fcea62baSJan Kara 			epos->offset - udf_file_entry_alloc_offset(inode));
1930cb00ea35SCyrill Gorcunov 	} else {
1931ff116fc8SJan Kara 		aed = (struct allocExtDesc *)epos->bh->b_data;
1932fcea62baSJan Kara 		WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
1933fcea62baSJan Kara 			epos->offset - sizeof(struct allocExtDesc));
1934fcea62baSJan Kara 		WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
19351da177e4SLinus Torvalds 	}
19361da177e4SLinus Torvalds 
19377e49b6f2SJan Kara 	udf_write_aext(inode, epos, eloc, elen, inc);
19381da177e4SLinus Torvalds 
1939cb00ea35SCyrill Gorcunov 	if (!epos->bh) {
194048d6d8ffSMarcin Slusarz 		iinfo->i_lenAlloc += adsize;
19411da177e4SLinus Torvalds 		mark_inode_dirty(inode);
1942cb00ea35SCyrill Gorcunov 	} else {
1943ff116fc8SJan Kara 		aed = (struct allocExtDesc *)epos->bh->b_data;
1944c2104fdaSmarcin.slusarz@gmail.com 		le32_add_cpu(&aed->lengthAllocDescs, adsize);
19454b11111aSMarcin Slusarz 		if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
19464b11111aSMarcin Slusarz 				UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
19474b11111aSMarcin Slusarz 			udf_update_tag(epos->bh->b_data,
19484b11111aSMarcin Slusarz 					epos->offset + (inc ? 0 : adsize));
19491da177e4SLinus Torvalds 		else
19504b11111aSMarcin Slusarz 			udf_update_tag(epos->bh->b_data,
19514b11111aSMarcin Slusarz 					sizeof(struct allocExtDesc));
1952ff116fc8SJan Kara 		mark_buffer_dirty_inode(epos->bh, inode);
19531da177e4SLinus Torvalds 	}
19541da177e4SLinus Torvalds 
19557e49b6f2SJan Kara 	return 0;
19561da177e4SLinus Torvalds }
19571da177e4SLinus Torvalds 
1958fcea62baSJan Kara /*
1959fcea62baSJan Kara  * Append extent at given position - should be the first free one in inode
1960fcea62baSJan Kara  * / indirect extent. Takes care of allocating and linking indirect blocks.
1961fcea62baSJan Kara  */
1962fcea62baSJan Kara int udf_add_aext(struct inode *inode, struct extent_position *epos,
1963fcea62baSJan Kara 		 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1964fcea62baSJan Kara {
1965fcea62baSJan Kara 	int adsize;
1966fcea62baSJan Kara 	struct super_block *sb = inode->i_sb;
1967fcea62baSJan Kara 
1968fcea62baSJan Kara 	if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1969fcea62baSJan Kara 		adsize = sizeof(struct short_ad);
1970fcea62baSJan Kara 	else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1971fcea62baSJan Kara 		adsize = sizeof(struct long_ad);
1972fcea62baSJan Kara 	else
1973fcea62baSJan Kara 		return -EIO;
1974fcea62baSJan Kara 
1975fcea62baSJan Kara 	if (epos->offset + (2 * adsize) > sb->s_blocksize) {
1976fcea62baSJan Kara 		int err;
1977b490bdd6SSteve Magnani 		udf_pblk_t new_block;
1978fcea62baSJan Kara 
1979fcea62baSJan Kara 		new_block = udf_new_block(sb, NULL,
1980fcea62baSJan Kara 					  epos->block.partitionReferenceNum,
1981fcea62baSJan Kara 					  epos->block.logicalBlockNum, &err);
1982fcea62baSJan Kara 		if (!new_block)
1983fcea62baSJan Kara 			return -ENOSPC;
1984fcea62baSJan Kara 
1985fcea62baSJan Kara 		err = udf_setup_indirect_aext(inode, new_block, epos);
1986fcea62baSJan Kara 		if (err)
1987fcea62baSJan Kara 			return err;
1988fcea62baSJan Kara 	}
1989fcea62baSJan Kara 
1990fcea62baSJan Kara 	return __udf_add_aext(inode, epos, eloc, elen, inc);
1991fcea62baSJan Kara }
1992fcea62baSJan Kara 
19937e49b6f2SJan Kara void udf_write_aext(struct inode *inode, struct extent_position *epos,
199497e961fdSPekka Enberg 		    struct kernel_lb_addr *eloc, uint32_t elen, int inc)
19951da177e4SLinus Torvalds {
19961da177e4SLinus Torvalds 	int adsize;
19971da177e4SLinus Torvalds 	uint8_t *ptr;
19985ca4e4beSPekka Enberg 	struct short_ad *sad;
19995ca4e4beSPekka Enberg 	struct long_ad *lad;
200048d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
20011da177e4SLinus Torvalds 
2002ff116fc8SJan Kara 	if (!epos->bh)
2003382a2287SJan Kara 		ptr = iinfo->i_data + epos->offset -
20044b11111aSMarcin Slusarz 			udf_file_entry_alloc_offset(inode) +
200548d6d8ffSMarcin Slusarz 			iinfo->i_lenEAttr;
20061da177e4SLinus Torvalds 	else
2007ff116fc8SJan Kara 		ptr = epos->bh->b_data + epos->offset;
20081da177e4SLinus Torvalds 
200948d6d8ffSMarcin Slusarz 	switch (iinfo->i_alloc_type) {
20101da177e4SLinus Torvalds 	case ICBTAG_FLAG_AD_SHORT:
20115ca4e4beSPekka Enberg 		sad = (struct short_ad *)ptr;
20121da177e4SLinus Torvalds 		sad->extLength = cpu_to_le32(elen);
201397e961fdSPekka Enberg 		sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
20145ca4e4beSPekka Enberg 		adsize = sizeof(struct short_ad);
20151da177e4SLinus Torvalds 		break;
20161da177e4SLinus Torvalds 	case ICBTAG_FLAG_AD_LONG:
20175ca4e4beSPekka Enberg 		lad = (struct long_ad *)ptr;
20181da177e4SLinus Torvalds 		lad->extLength = cpu_to_le32(elen);
201997e961fdSPekka Enberg 		lad->extLocation = cpu_to_lelb(*eloc);
20201da177e4SLinus Torvalds 		memset(lad->impUse, 0x00, sizeof(lad->impUse));
20215ca4e4beSPekka Enberg 		adsize = sizeof(struct long_ad);
20221da177e4SLinus Torvalds 		break;
20231da177e4SLinus Torvalds 	default:
20247e49b6f2SJan Kara 		return;
20251da177e4SLinus Torvalds 	}
20261da177e4SLinus Torvalds 
2027cb00ea35SCyrill Gorcunov 	if (epos->bh) {
202828de7948SCyrill Gorcunov 		if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
20296c79e987SMarcin Slusarz 		    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
20304b11111aSMarcin Slusarz 			struct allocExtDesc *aed =
20314b11111aSMarcin Slusarz 				(struct allocExtDesc *)epos->bh->b_data;
2032ff116fc8SJan Kara 			udf_update_tag(epos->bh->b_data,
20334b11111aSMarcin Slusarz 				       le32_to_cpu(aed->lengthAllocDescs) +
20344b11111aSMarcin Slusarz 				       sizeof(struct allocExtDesc));
20351da177e4SLinus Torvalds 		}
2036ff116fc8SJan Kara 		mark_buffer_dirty_inode(epos->bh, inode);
203728de7948SCyrill Gorcunov 	} else {
20381da177e4SLinus Torvalds 		mark_inode_dirty(inode);
203928de7948SCyrill Gorcunov 	}
20401da177e4SLinus Torvalds 
20411da177e4SLinus Torvalds 	if (inc)
2042ff116fc8SJan Kara 		epos->offset += adsize;
20431da177e4SLinus Torvalds }
20441da177e4SLinus Torvalds 
2045b0918d9fSVegard Nossum /*
2046b0918d9fSVegard Nossum  * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2047b0918d9fSVegard Nossum  * someone does some weird stuff.
2048b0918d9fSVegard Nossum  */
2049b0918d9fSVegard Nossum #define UDF_MAX_INDIR_EXTS 16
2050b0918d9fSVegard Nossum 
2051ff116fc8SJan Kara int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
20525ca4e4beSPekka Enberg 		     struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
20531da177e4SLinus Torvalds {
20541da177e4SLinus Torvalds 	int8_t etype;
2055b0918d9fSVegard Nossum 	unsigned int indirections = 0;
20561da177e4SLinus Torvalds 
2057ff116fc8SJan Kara 	while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
2058800552ceSPali Rohár 	       (EXT_NEXT_EXTENT_ALLOCDESCS >> 30)) {
2059b490bdd6SSteve Magnani 		udf_pblk_t block;
2060b0918d9fSVegard Nossum 
2061b0918d9fSVegard Nossum 		if (++indirections > UDF_MAX_INDIR_EXTS) {
2062b0918d9fSVegard Nossum 			udf_err(inode->i_sb,
2063b0918d9fSVegard Nossum 				"too many indirect extents in inode %lu\n",
2064b0918d9fSVegard Nossum 				inode->i_ino);
2065b0918d9fSVegard Nossum 			return -1;
2066b0918d9fSVegard Nossum 		}
2067b0918d9fSVegard Nossum 
2068ff116fc8SJan Kara 		epos->block = *eloc;
2069ff116fc8SJan Kara 		epos->offset = sizeof(struct allocExtDesc);
20703bf25cb4SJan Kara 		brelse(epos->bh);
207197e961fdSPekka Enberg 		block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
20724b11111aSMarcin Slusarz 		epos->bh = udf_tread(inode->i_sb, block);
20734b11111aSMarcin Slusarz 		if (!epos->bh) {
2074fcbf7637SSteve Magnani 			udf_debug("reading block %u failed!\n", block);
20751da177e4SLinus Torvalds 			return -1;
20761da177e4SLinus Torvalds 		}
20771da177e4SLinus Torvalds 	}
20781da177e4SLinus Torvalds 
20791da177e4SLinus Torvalds 	return etype;
20801da177e4SLinus Torvalds }
20811da177e4SLinus Torvalds 
2082ff116fc8SJan Kara int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
20835ca4e4beSPekka Enberg 			struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
20841da177e4SLinus Torvalds {
20851da177e4SLinus Torvalds 	int alen;
20861da177e4SLinus Torvalds 	int8_t etype;
20871da177e4SLinus Torvalds 	uint8_t *ptr;
20885ca4e4beSPekka Enberg 	struct short_ad *sad;
20895ca4e4beSPekka Enberg 	struct long_ad *lad;
209048d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
209128de7948SCyrill Gorcunov 
2092cb00ea35SCyrill Gorcunov 	if (!epos->bh) {
2093ff116fc8SJan Kara 		if (!epos->offset)
2094ff116fc8SJan Kara 			epos->offset = udf_file_entry_alloc_offset(inode);
2095382a2287SJan Kara 		ptr = iinfo->i_data + epos->offset -
20964b11111aSMarcin Slusarz 			udf_file_entry_alloc_offset(inode) +
209748d6d8ffSMarcin Slusarz 			iinfo->i_lenEAttr;
20984b11111aSMarcin Slusarz 		alen = udf_file_entry_alloc_offset(inode) +
209948d6d8ffSMarcin Slusarz 							iinfo->i_lenAlloc;
2100cb00ea35SCyrill Gorcunov 	} else {
2101ff116fc8SJan Kara 		if (!epos->offset)
2102ff116fc8SJan Kara 			epos->offset = sizeof(struct allocExtDesc);
2103ff116fc8SJan Kara 		ptr = epos->bh->b_data + epos->offset;
210428de7948SCyrill Gorcunov 		alen = sizeof(struct allocExtDesc) +
21054b11111aSMarcin Slusarz 			le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
21064b11111aSMarcin Slusarz 							lengthAllocDescs);
21071da177e4SLinus Torvalds 	}
21081da177e4SLinus Torvalds 
210948d6d8ffSMarcin Slusarz 	switch (iinfo->i_alloc_type) {
21101da177e4SLinus Torvalds 	case ICBTAG_FLAG_AD_SHORT:
21114b11111aSMarcin Slusarz 		sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
21124b11111aSMarcin Slusarz 		if (!sad)
21131da177e4SLinus Torvalds 			return -1;
21141da177e4SLinus Torvalds 		etype = le32_to_cpu(sad->extLength) >> 30;
21151da177e4SLinus Torvalds 		eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
21164b11111aSMarcin Slusarz 		eloc->partitionReferenceNum =
211748d6d8ffSMarcin Slusarz 				iinfo->i_location.partitionReferenceNum;
211828de7948SCyrill Gorcunov 		*elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
21191da177e4SLinus Torvalds 		break;
21201da177e4SLinus Torvalds 	case ICBTAG_FLAG_AD_LONG:
21214b11111aSMarcin Slusarz 		lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
21224b11111aSMarcin Slusarz 		if (!lad)
21231da177e4SLinus Torvalds 			return -1;
21241da177e4SLinus Torvalds 		etype = le32_to_cpu(lad->extLength) >> 30;
21251da177e4SLinus Torvalds 		*eloc = lelb_to_cpu(lad->extLocation);
212628de7948SCyrill Gorcunov 		*elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
21271da177e4SLinus Torvalds 		break;
21281da177e4SLinus Torvalds 	default:
2129fcbf7637SSteve Magnani 		udf_debug("alloc_type = %u unsupported\n", iinfo->i_alloc_type);
21301da177e4SLinus Torvalds 		return -1;
21311da177e4SLinus Torvalds 	}
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds 	return etype;
21341da177e4SLinus Torvalds }
21351da177e4SLinus Torvalds 
213619fd80deSJan Kara static int udf_insert_aext(struct inode *inode, struct extent_position epos,
21375ca4e4beSPekka Enberg 			   struct kernel_lb_addr neloc, uint32_t nelen)
21381da177e4SLinus Torvalds {
21395ca4e4beSPekka Enberg 	struct kernel_lb_addr oeloc;
21401da177e4SLinus Torvalds 	uint32_t oelen;
21411da177e4SLinus Torvalds 	int8_t etype;
214219fd80deSJan Kara 	int err;
21431da177e4SLinus Torvalds 
2144ff116fc8SJan Kara 	if (epos.bh)
21453bf25cb4SJan Kara 		get_bh(epos.bh);
21461da177e4SLinus Torvalds 
2147cb00ea35SCyrill Gorcunov 	while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
214897e961fdSPekka Enberg 		udf_write_aext(inode, &epos, &neloc, nelen, 1);
21491da177e4SLinus Torvalds 		neloc = oeloc;
21501da177e4SLinus Torvalds 		nelen = (etype << 30) | oelen;
21511da177e4SLinus Torvalds 	}
215219fd80deSJan Kara 	err = udf_add_aext(inode, &epos, &neloc, nelen, 1);
21533bf25cb4SJan Kara 	brelse(epos.bh);
215428de7948SCyrill Gorcunov 
215519fd80deSJan Kara 	return err;
21561da177e4SLinus Torvalds }
21571da177e4SLinus Torvalds 
21586c1e4d06SJan Kara int8_t udf_delete_aext(struct inode *inode, struct extent_position epos)
21591da177e4SLinus Torvalds {
2160ff116fc8SJan Kara 	struct extent_position oepos;
2161ff116fc8SJan Kara 	int adsize;
21621da177e4SLinus Torvalds 	int8_t etype;
21631da177e4SLinus Torvalds 	struct allocExtDesc *aed;
216448d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo;
21656c1e4d06SJan Kara 	struct kernel_lb_addr eloc;
21666c1e4d06SJan Kara 	uint32_t elen;
21671da177e4SLinus Torvalds 
2168cb00ea35SCyrill Gorcunov 	if (epos.bh) {
21693bf25cb4SJan Kara 		get_bh(epos.bh);
21703bf25cb4SJan Kara 		get_bh(epos.bh);
21711da177e4SLinus Torvalds 	}
21721da177e4SLinus Torvalds 
217348d6d8ffSMarcin Slusarz 	iinfo = UDF_I(inode);
217448d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
21755ca4e4beSPekka Enberg 		adsize = sizeof(struct short_ad);
217648d6d8ffSMarcin Slusarz 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
21775ca4e4beSPekka Enberg 		adsize = sizeof(struct long_ad);
21781da177e4SLinus Torvalds 	else
21791da177e4SLinus Torvalds 		adsize = 0;
21801da177e4SLinus Torvalds 
2181ff116fc8SJan Kara 	oepos = epos;
2182ff116fc8SJan Kara 	if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
21831da177e4SLinus Torvalds 		return -1;
21841da177e4SLinus Torvalds 
2185cb00ea35SCyrill Gorcunov 	while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
218697e961fdSPekka Enberg 		udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2187cb00ea35SCyrill Gorcunov 		if (oepos.bh != epos.bh) {
2188ff116fc8SJan Kara 			oepos.block = epos.block;
21893bf25cb4SJan Kara 			brelse(oepos.bh);
21903bf25cb4SJan Kara 			get_bh(epos.bh);
2191ff116fc8SJan Kara 			oepos.bh = epos.bh;
2192ff116fc8SJan Kara 			oepos.offset = epos.offset - adsize;
21931da177e4SLinus Torvalds 		}
21941da177e4SLinus Torvalds 	}
21955ca4e4beSPekka Enberg 	memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
21961da177e4SLinus Torvalds 	elen = 0;
21971da177e4SLinus Torvalds 
2198cb00ea35SCyrill Gorcunov 	if (epos.bh != oepos.bh) {
219997e961fdSPekka Enberg 		udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
220097e961fdSPekka Enberg 		udf_write_aext(inode, &oepos, &eloc, elen, 1);
220197e961fdSPekka Enberg 		udf_write_aext(inode, &oepos, &eloc, elen, 1);
2202cb00ea35SCyrill Gorcunov 		if (!oepos.bh) {
220348d6d8ffSMarcin Slusarz 			iinfo->i_lenAlloc -= (adsize * 2);
22041da177e4SLinus Torvalds 			mark_inode_dirty(inode);
2205cb00ea35SCyrill Gorcunov 		} else {
2206ff116fc8SJan Kara 			aed = (struct allocExtDesc *)oepos.bh->b_data;
2207c2104fdaSmarcin.slusarz@gmail.com 			le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
220828de7948SCyrill Gorcunov 			if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
22096c79e987SMarcin Slusarz 			    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
22104b11111aSMarcin Slusarz 				udf_update_tag(oepos.bh->b_data,
22114b11111aSMarcin Slusarz 						oepos.offset - (2 * adsize));
22121da177e4SLinus Torvalds 			else
22134b11111aSMarcin Slusarz 				udf_update_tag(oepos.bh->b_data,
22144b11111aSMarcin Slusarz 						sizeof(struct allocExtDesc));
2215ff116fc8SJan Kara 			mark_buffer_dirty_inode(oepos.bh, inode);
22161da177e4SLinus Torvalds 		}
2217cb00ea35SCyrill Gorcunov 	} else {
221897e961fdSPekka Enberg 		udf_write_aext(inode, &oepos, &eloc, elen, 1);
2219cb00ea35SCyrill Gorcunov 		if (!oepos.bh) {
222048d6d8ffSMarcin Slusarz 			iinfo->i_lenAlloc -= adsize;
22211da177e4SLinus Torvalds 			mark_inode_dirty(inode);
2222cb00ea35SCyrill Gorcunov 		} else {
2223ff116fc8SJan Kara 			aed = (struct allocExtDesc *)oepos.bh->b_data;
2224c2104fdaSmarcin.slusarz@gmail.com 			le32_add_cpu(&aed->lengthAllocDescs, -adsize);
222528de7948SCyrill Gorcunov 			if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
22266c79e987SMarcin Slusarz 			    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
22274b11111aSMarcin Slusarz 				udf_update_tag(oepos.bh->b_data,
22284b11111aSMarcin Slusarz 						epos.offset - adsize);
22291da177e4SLinus Torvalds 			else
22304b11111aSMarcin Slusarz 				udf_update_tag(oepos.bh->b_data,
22314b11111aSMarcin Slusarz 						sizeof(struct allocExtDesc));
2232ff116fc8SJan Kara 			mark_buffer_dirty_inode(oepos.bh, inode);
22331da177e4SLinus Torvalds 		}
22341da177e4SLinus Torvalds 	}
22351da177e4SLinus Torvalds 
22363bf25cb4SJan Kara 	brelse(epos.bh);
22373bf25cb4SJan Kara 	brelse(oepos.bh);
223828de7948SCyrill Gorcunov 
22391da177e4SLinus Torvalds 	return (elen >> 30);
22401da177e4SLinus Torvalds }
22411da177e4SLinus Torvalds 
2242cb00ea35SCyrill Gorcunov int8_t inode_bmap(struct inode *inode, sector_t block,
22435ca4e4beSPekka Enberg 		  struct extent_position *pos, struct kernel_lb_addr *eloc,
2244cb00ea35SCyrill Gorcunov 		  uint32_t *elen, sector_t *offset)
22451da177e4SLinus Torvalds {
22464b11111aSMarcin Slusarz 	unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2247bbc9abd2SFabian Frederick 	loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
22481da177e4SLinus Torvalds 	int8_t etype;
224948d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo;
22501da177e4SLinus Torvalds 
225148d6d8ffSMarcin Slusarz 	iinfo = UDF_I(inode);
225299600051SNamjae Jeon 	if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2253ff116fc8SJan Kara 		pos->offset = 0;
225448d6d8ffSMarcin Slusarz 		pos->block = iinfo->i_location;
2255ff116fc8SJan Kara 		pos->bh = NULL;
225699600051SNamjae Jeon 	}
22571da177e4SLinus Torvalds 	*elen = 0;
2258cb00ea35SCyrill Gorcunov 	do {
22594b11111aSMarcin Slusarz 		etype = udf_next_aext(inode, pos, eloc, elen, 1);
22604b11111aSMarcin Slusarz 		if (etype == -1) {
22614b11111aSMarcin Slusarz 			*offset = (bcount - lbcount) >> blocksize_bits;
226248d6d8ffSMarcin Slusarz 			iinfo->i_lenExtents = lbcount;
22631da177e4SLinus Torvalds 			return -1;
22641da177e4SLinus Torvalds 		}
22651da177e4SLinus Torvalds 		lbcount += *elen;
22661da177e4SLinus Torvalds 	} while (lbcount <= bcount);
226799600051SNamjae Jeon 	/* update extent cache */
2268b31c9ed9SFabian Frederick 	udf_update_extent_cache(inode, lbcount - *elen, pos);
22694b11111aSMarcin Slusarz 	*offset = (bcount + *elen - lbcount) >> blocksize_bits;
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds 	return etype;
22721da177e4SLinus Torvalds }
22731da177e4SLinus Torvalds 
2274b490bdd6SSteve Magnani udf_pblk_t udf_block_map(struct inode *inode, sector_t block)
22751da177e4SLinus Torvalds {
22765ca4e4beSPekka Enberg 	struct kernel_lb_addr eloc;
2277ff116fc8SJan Kara 	uint32_t elen;
227860448b1dSJan Kara 	sector_t offset;
227928de7948SCyrill Gorcunov 	struct extent_position epos = {};
2280b490bdd6SSteve Magnani 	udf_pblk_t ret;
22811da177e4SLinus Torvalds 
22824d0fb621SAlessio Igor Bogani 	down_read(&UDF_I(inode)->i_data_sem);
22831da177e4SLinus Torvalds 
22844b11111aSMarcin Slusarz 	if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
22854b11111aSMarcin Slusarz 						(EXT_RECORDED_ALLOCATED >> 30))
228697e961fdSPekka Enberg 		ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
22871da177e4SLinus Torvalds 	else
22881da177e4SLinus Torvalds 		ret = 0;
22891da177e4SLinus Torvalds 
22904d0fb621SAlessio Igor Bogani 	up_read(&UDF_I(inode)->i_data_sem);
22913bf25cb4SJan Kara 	brelse(epos.bh);
22921da177e4SLinus Torvalds 
22931da177e4SLinus Torvalds 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
22941da177e4SLinus Torvalds 		return udf_fixed_to_variable(ret);
22951da177e4SLinus Torvalds 	else
22961da177e4SLinus Torvalds 		return ret;
22971da177e4SLinus Torvalds }
2298