xref: /openbmc/linux/fs/udf/inode.c (revision 3cc6f8444a9d9e4a167c575e4da7b6c6d626501a)
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 MODULE_AUTHOR("Ben Fennema");
471da177e4SLinus Torvalds MODULE_DESCRIPTION("Universal Disk Format Filesystem");
481da177e4SLinus Torvalds MODULE_LICENSE("GPL");
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #define EXTENT_MERGE_SIZE 5
511da177e4SLinus Torvalds 
52faa17292SAl Viro static umode_t udf_convert_permissions(struct fileEntry *);
531da177e4SLinus Torvalds static int udf_update_inode(struct inode *, int);
5449521de1SJan Kara static int udf_sync_inode(struct inode *inode);
55647bd61aSCyrill Gorcunov static int udf_alloc_i_data(struct inode *inode, size_t size);
567b0b0933SJan Kara static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
57ff116fc8SJan Kara static int8_t udf_insert_aext(struct inode *, struct extent_position,
585ca4e4beSPekka Enberg 			      struct kernel_lb_addr, uint32_t);
591da177e4SLinus Torvalds static void udf_split_extents(struct inode *, int *, int, int,
60*3cc6f844SFabian Frederick 			      struct kernel_long_ad *, int *);
611da177e4SLinus Torvalds static void udf_prealloc_extents(struct inode *, int, int,
62*3cc6f844SFabian Frederick 				 struct kernel_long_ad *, int *);
63*3cc6f844SFabian Frederick static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *);
64*3cc6f844SFabian Frederick static void udf_update_extents(struct inode *, struct kernel_long_ad *, int,
65*3cc6f844SFabian Frederick 			       int, struct extent_position *);
661da177e4SLinus Torvalds static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
671da177e4SLinus Torvalds 
6899600051SNamjae Jeon static void __udf_clear_extent_cache(struct inode *inode)
6999600051SNamjae Jeon {
7099600051SNamjae Jeon 	struct udf_inode_info *iinfo = UDF_I(inode);
7199600051SNamjae Jeon 
7299600051SNamjae Jeon 	if (iinfo->cached_extent.lstart != -1) {
7399600051SNamjae Jeon 		brelse(iinfo->cached_extent.epos.bh);
7499600051SNamjae Jeon 		iinfo->cached_extent.lstart = -1;
7599600051SNamjae Jeon 	}
7699600051SNamjae Jeon }
7799600051SNamjae Jeon 
7899600051SNamjae Jeon /* Invalidate extent cache */
7999600051SNamjae Jeon static void udf_clear_extent_cache(struct inode *inode)
8099600051SNamjae Jeon {
8199600051SNamjae Jeon 	struct udf_inode_info *iinfo = UDF_I(inode);
8299600051SNamjae Jeon 
8399600051SNamjae Jeon 	spin_lock(&iinfo->i_extent_cache_lock);
8499600051SNamjae Jeon 	__udf_clear_extent_cache(inode);
8599600051SNamjae Jeon 	spin_unlock(&iinfo->i_extent_cache_lock);
8699600051SNamjae Jeon }
8799600051SNamjae Jeon 
8899600051SNamjae Jeon /* Return contents of extent cache */
8999600051SNamjae Jeon static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
9099600051SNamjae Jeon 				 loff_t *lbcount, struct extent_position *pos)
9199600051SNamjae Jeon {
9299600051SNamjae Jeon 	struct udf_inode_info *iinfo = UDF_I(inode);
9399600051SNamjae Jeon 	int ret = 0;
9499600051SNamjae Jeon 
9599600051SNamjae Jeon 	spin_lock(&iinfo->i_extent_cache_lock);
9699600051SNamjae Jeon 	if ((iinfo->cached_extent.lstart <= bcount) &&
9799600051SNamjae Jeon 	    (iinfo->cached_extent.lstart != -1)) {
9899600051SNamjae Jeon 		/* Cache hit */
9999600051SNamjae Jeon 		*lbcount = iinfo->cached_extent.lstart;
10099600051SNamjae Jeon 		memcpy(pos, &iinfo->cached_extent.epos,
10199600051SNamjae Jeon 		       sizeof(struct extent_position));
10299600051SNamjae Jeon 		if (pos->bh)
10399600051SNamjae Jeon 			get_bh(pos->bh);
10499600051SNamjae Jeon 		ret = 1;
10599600051SNamjae Jeon 	}
10699600051SNamjae Jeon 	spin_unlock(&iinfo->i_extent_cache_lock);
10799600051SNamjae Jeon 	return ret;
10899600051SNamjae Jeon }
10999600051SNamjae Jeon 
11099600051SNamjae Jeon /* Add extent to extent cache */
11199600051SNamjae Jeon static void udf_update_extent_cache(struct inode *inode, loff_t estart,
11299600051SNamjae Jeon 				    struct extent_position *pos, int next_epos)
11399600051SNamjae Jeon {
11499600051SNamjae Jeon 	struct udf_inode_info *iinfo = UDF_I(inode);
11599600051SNamjae Jeon 
11699600051SNamjae Jeon 	spin_lock(&iinfo->i_extent_cache_lock);
11799600051SNamjae Jeon 	/* Invalidate previously cached extent */
11899600051SNamjae Jeon 	__udf_clear_extent_cache(inode);
11999600051SNamjae Jeon 	if (pos->bh)
12099600051SNamjae Jeon 		get_bh(pos->bh);
12199600051SNamjae Jeon 	memcpy(&iinfo->cached_extent.epos, pos,
12299600051SNamjae Jeon 	       sizeof(struct extent_position));
12399600051SNamjae Jeon 	iinfo->cached_extent.lstart = estart;
12499600051SNamjae Jeon 	if (next_epos)
12599600051SNamjae Jeon 		switch (iinfo->i_alloc_type) {
12699600051SNamjae Jeon 		case ICBTAG_FLAG_AD_SHORT:
12799600051SNamjae Jeon 			iinfo->cached_extent.epos.offset -=
12899600051SNamjae Jeon 			sizeof(struct short_ad);
12999600051SNamjae Jeon 			break;
13099600051SNamjae Jeon 		case ICBTAG_FLAG_AD_LONG:
13199600051SNamjae Jeon 			iinfo->cached_extent.epos.offset -=
13299600051SNamjae Jeon 			sizeof(struct long_ad);
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 
1423aac2b62SAl Viro 	if (!inode->i_nlink && !is_bad_inode(inode)) {
1433aac2b62SAl Viro 		want_delete = 1;
1447e49b6f2SJan Kara 		udf_setsize(inode, 0);
1451da177e4SLinus Torvalds 		udf_update_inode(inode, IS_SYNC(inode));
14691b0abe3SJohannes Weiner 	}
14791b0abe3SJohannes Weiner 	truncate_inode_pages_final(&inode->i_data);
1483aac2b62SAl Viro 	invalidate_inode_buffers(inode);
149dbd5768fSJan Kara 	clear_inode(inode);
1502c948b3fSJan Kara 	if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
1512c948b3fSJan Kara 	    inode->i_size != iinfo->i_lenExtents) {
15278ace70cSJoe Perches 		udf_warn(inode->i_sb, "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
15378ace70cSJoe Perches 			 inode->i_ino, inode->i_mode,
1542c948b3fSJan Kara 			 (unsigned long long)inode->i_size,
1552c948b3fSJan Kara 			 (unsigned long long)iinfo->i_lenExtents);
1561da177e4SLinus Torvalds 	}
15748d6d8ffSMarcin Slusarz 	kfree(iinfo->i_ext.i_data);
15848d6d8ffSMarcin Slusarz 	iinfo->i_ext.i_data = NULL;
15999600051SNamjae Jeon 	udf_clear_extent_cache(inode);
1603aac2b62SAl Viro 	if (want_delete) {
1613aac2b62SAl Viro 		udf_free_inode(inode);
1623aac2b62SAl Viro 	}
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
1655eec54fcSIan Abbott static void udf_write_failed(struct address_space *mapping, loff_t to)
1665eec54fcSIan Abbott {
1675eec54fcSIan Abbott 	struct inode *inode = mapping->host;
1685eec54fcSIan Abbott 	struct udf_inode_info *iinfo = UDF_I(inode);
1695eec54fcSIan Abbott 	loff_t isize = inode->i_size;
1705eec54fcSIan Abbott 
1715eec54fcSIan Abbott 	if (to > isize) {
1727caef267SKirill A. Shutemov 		truncate_pagecache(inode, isize);
1735eec54fcSIan Abbott 		if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1745eec54fcSIan Abbott 			down_write(&iinfo->i_data_sem);
17599600051SNamjae Jeon 			udf_clear_extent_cache(inode);
1765eec54fcSIan Abbott 			udf_truncate_extents(inode);
1775eec54fcSIan Abbott 			up_write(&iinfo->i_data_sem);
1785eec54fcSIan Abbott 		}
1795eec54fcSIan Abbott 	}
1805eec54fcSIan Abbott }
1815eec54fcSIan Abbott 
1821da177e4SLinus Torvalds static int udf_writepage(struct page *page, struct writeback_control *wbc)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	return block_write_full_page(page, udf_get_block, wbc);
1851da177e4SLinus Torvalds }
1861da177e4SLinus Torvalds 
187378b8e1aSNamjae Jeon static int udf_writepages(struct address_space *mapping,
188378b8e1aSNamjae Jeon 			struct writeback_control *wbc)
189378b8e1aSNamjae Jeon {
190378b8e1aSNamjae Jeon 	return mpage_writepages(mapping, wbc, udf_get_block);
191378b8e1aSNamjae Jeon }
192378b8e1aSNamjae Jeon 
1931da177e4SLinus Torvalds static int udf_readpage(struct file *file, struct page *page)
1941da177e4SLinus Torvalds {
195bc112323SNamjae Jeon 	return mpage_readpage(page, udf_get_block);
196bc112323SNamjae Jeon }
197bc112323SNamjae Jeon 
198bc112323SNamjae Jeon static int udf_readpages(struct file *file, struct address_space *mapping,
199bc112323SNamjae Jeon 			struct list_head *pages, unsigned nr_pages)
200bc112323SNamjae Jeon {
201bc112323SNamjae Jeon 	return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
204be021ee4SNick Piggin static int udf_write_begin(struct file *file, struct address_space *mapping,
205be021ee4SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
206be021ee4SNick Piggin 			struct page **pagep, void **fsdata)
2071da177e4SLinus Torvalds {
208155130a4SChristoph Hellwig 	int ret;
209155130a4SChristoph Hellwig 
210155130a4SChristoph Hellwig 	ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
2115eec54fcSIan Abbott 	if (unlikely(ret))
2125eec54fcSIan Abbott 		udf_write_failed(mapping, pos + len);
2135eec54fcSIan Abbott 	return ret;
2145eec54fcSIan Abbott }
2155eec54fcSIan Abbott 
216c8b8e32dSChristoph Hellwig static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2175eec54fcSIan Abbott {
2185eec54fcSIan Abbott 	struct file *file = iocb->ki_filp;
2195eec54fcSIan Abbott 	struct address_space *mapping = file->f_mapping;
2207e49b6f2SJan Kara 	struct inode *inode = mapping->host;
221a6cbcd4aSAl Viro 	size_t count = iov_iter_count(iter);
2225eec54fcSIan Abbott 	ssize_t ret;
2237e49b6f2SJan Kara 
224c8b8e32dSChristoph Hellwig 	ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
2256f673763SOmar Sandoval 	if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
226c8b8e32dSChristoph Hellwig 		udf_write_failed(mapping, iocb->ki_pos + count);
227155130a4SChristoph Hellwig 	return ret;
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds static sector_t udf_bmap(struct address_space *mapping, sector_t block)
2311da177e4SLinus Torvalds {
2321da177e4SLinus Torvalds 	return generic_block_bmap(mapping, block, udf_get_block);
2331da177e4SLinus Torvalds }
2341da177e4SLinus Torvalds 
235f5e54d6eSChristoph Hellwig const struct address_space_operations udf_aops = {
2361da177e4SLinus Torvalds 	.readpage	= udf_readpage,
237bc112323SNamjae Jeon 	.readpages	= udf_readpages,
2381da177e4SLinus Torvalds 	.writepage	= udf_writepage,
239378b8e1aSNamjae Jeon 	.writepages	= udf_writepages,
240be021ee4SNick Piggin 	.write_begin	= udf_write_begin,
241be021ee4SNick Piggin 	.write_end	= generic_write_end,
2425eec54fcSIan Abbott 	.direct_IO	= udf_direct_IO,
2431da177e4SLinus Torvalds 	.bmap		= udf_bmap,
2441da177e4SLinus Torvalds };
2451da177e4SLinus Torvalds 
246d2eb8c35SJan Kara /*
247d2eb8c35SJan Kara  * Expand file stored in ICB to a normal one-block-file
248d2eb8c35SJan Kara  *
249d2eb8c35SJan Kara  * This function requires i_data_sem for writing and releases it.
250d2eb8c35SJan Kara  * This function requires i_mutex held
251d2eb8c35SJan Kara  */
2527e49b6f2SJan Kara int udf_expand_file_adinicb(struct inode *inode)
2531da177e4SLinus Torvalds {
2541da177e4SLinus Torvalds 	struct page *page;
2551da177e4SLinus Torvalds 	char *kaddr;
25648d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
2577e49b6f2SJan Kara 	int err;
2581da177e4SLinus Torvalds 	struct writeback_control udf_wbc = {
2591da177e4SLinus Torvalds 		.sync_mode = WB_SYNC_NONE,
2601da177e4SLinus Torvalds 		.nr_to_write = 1,
2611da177e4SLinus Torvalds 	};
2621da177e4SLinus Torvalds 
2635955102cSAl Viro 	WARN_ON_ONCE(!inode_is_locked(inode));
26448d6d8ffSMarcin Slusarz 	if (!iinfo->i_lenAlloc) {
2651da177e4SLinus Torvalds 		if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
26648d6d8ffSMarcin Slusarz 			iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
2671da177e4SLinus Torvalds 		else
26848d6d8ffSMarcin Slusarz 			iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
2697e49b6f2SJan Kara 		/* from now on we have normal address_space methods */
2707e49b6f2SJan Kara 		inode->i_data.a_ops = &udf_aops;
271d2eb8c35SJan Kara 		up_write(&iinfo->i_data_sem);
2721da177e4SLinus Torvalds 		mark_inode_dirty(inode);
2737e49b6f2SJan Kara 		return 0;
2741da177e4SLinus Torvalds 	}
275d2eb8c35SJan Kara 	/*
276d2eb8c35SJan Kara 	 * Release i_data_sem so that we can lock a page - page lock ranks
277d2eb8c35SJan Kara 	 * above i_data_sem. i_mutex still protects us against file changes.
278d2eb8c35SJan Kara 	 */
279d2eb8c35SJan Kara 	up_write(&iinfo->i_data_sem);
2801da177e4SLinus Torvalds 
2817e49b6f2SJan Kara 	page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
2827e49b6f2SJan Kara 	if (!page)
2837e49b6f2SJan Kara 		return -ENOMEM;
284cd7619d6SMatt Mackall 
285cb00ea35SCyrill Gorcunov 	if (!PageUptodate(page)) {
2861da177e4SLinus Torvalds 		kaddr = kmap(page);
28748d6d8ffSMarcin Slusarz 		memset(kaddr + iinfo->i_lenAlloc, 0x00,
28809cbfeafSKirill A. Shutemov 		       PAGE_SIZE - iinfo->i_lenAlloc);
28948d6d8ffSMarcin Slusarz 		memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
29048d6d8ffSMarcin Slusarz 			iinfo->i_lenAlloc);
2911da177e4SLinus Torvalds 		flush_dcache_page(page);
2921da177e4SLinus Torvalds 		SetPageUptodate(page);
2931da177e4SLinus Torvalds 		kunmap(page);
2941da177e4SLinus Torvalds 	}
295d2eb8c35SJan Kara 	down_write(&iinfo->i_data_sem);
29648d6d8ffSMarcin Slusarz 	memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
29748d6d8ffSMarcin Slusarz 	       iinfo->i_lenAlloc);
29848d6d8ffSMarcin Slusarz 	iinfo->i_lenAlloc = 0;
2991da177e4SLinus Torvalds 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
30048d6d8ffSMarcin Slusarz 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
3011da177e4SLinus Torvalds 	else
30248d6d8ffSMarcin Slusarz 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
3037e49b6f2SJan Kara 	/* from now on we have normal address_space methods */
3047e49b6f2SJan Kara 	inode->i_data.a_ops = &udf_aops;
305d2eb8c35SJan Kara 	up_write(&iinfo->i_data_sem);
3067e49b6f2SJan Kara 	err = inode->i_data.a_ops->writepage(page, &udf_wbc);
3077e49b6f2SJan Kara 	if (err) {
3087e49b6f2SJan Kara 		/* Restore everything back so that we don't lose data... */
3097e49b6f2SJan Kara 		lock_page(page);
3107e49b6f2SJan Kara 		kaddr = kmap(page);
311d2eb8c35SJan Kara 		down_write(&iinfo->i_data_sem);
3127e49b6f2SJan Kara 		memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
3137e49b6f2SJan Kara 		       inode->i_size);
3147e49b6f2SJan Kara 		kunmap(page);
3157e49b6f2SJan Kara 		unlock_page(page);
3167e49b6f2SJan Kara 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
3177e49b6f2SJan Kara 		inode->i_data.a_ops = &udf_adinicb_aops;
318d2eb8c35SJan Kara 		up_write(&iinfo->i_data_sem);
3197e49b6f2SJan Kara 	}
32009cbfeafSKirill A. Shutemov 	put_page(page);
3211da177e4SLinus Torvalds 	mark_inode_dirty(inode);
3227e49b6f2SJan Kara 
3237e49b6f2SJan Kara 	return err;
3241da177e4SLinus Torvalds }
3251da177e4SLinus Torvalds 
326cb00ea35SCyrill Gorcunov struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
327cb00ea35SCyrill Gorcunov 					   int *err)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	int newblock;
330ff116fc8SJan Kara 	struct buffer_head *dbh = NULL;
3315ca4e4beSPekka Enberg 	struct kernel_lb_addr eloc;
3321da177e4SLinus Torvalds 	uint8_t alloctype;
333ff116fc8SJan Kara 	struct extent_position epos;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	struct udf_fileident_bh sfibh, dfibh;
336af793295SJan Kara 	loff_t f_pos = udf_ext0_offset(inode);
337af793295SJan Kara 	int size = udf_ext0_offset(inode) + inode->i_size;
3381da177e4SLinus Torvalds 	struct fileIdentDesc cfi, *sfi, *dfi;
33948d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
3421da177e4SLinus Torvalds 		alloctype = ICBTAG_FLAG_AD_SHORT;
3431da177e4SLinus Torvalds 	else
3441da177e4SLinus Torvalds 		alloctype = ICBTAG_FLAG_AD_LONG;
3451da177e4SLinus Torvalds 
346cb00ea35SCyrill Gorcunov 	if (!inode->i_size) {
34748d6d8ffSMarcin Slusarz 		iinfo->i_alloc_type = alloctype;
3481da177e4SLinus Torvalds 		mark_inode_dirty(inode);
3491da177e4SLinus Torvalds 		return NULL;
3501da177e4SLinus Torvalds 	}
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	/* alloc block, and copy data to it */
3531da177e4SLinus Torvalds 	*block = udf_new_block(inode->i_sb, inode,
35448d6d8ffSMarcin Slusarz 			       iinfo->i_location.partitionReferenceNum,
35548d6d8ffSMarcin Slusarz 			       iinfo->i_location.logicalBlockNum, err);
3561da177e4SLinus Torvalds 	if (!(*block))
3571da177e4SLinus Torvalds 		return NULL;
3581da177e4SLinus Torvalds 	newblock = udf_get_pblock(inode->i_sb, *block,
35948d6d8ffSMarcin Slusarz 				  iinfo->i_location.partitionReferenceNum,
3604b11111aSMarcin Slusarz 				0);
3611da177e4SLinus Torvalds 	if (!newblock)
3621da177e4SLinus Torvalds 		return NULL;
3631da177e4SLinus Torvalds 	dbh = udf_tgetblk(inode->i_sb, newblock);
3641da177e4SLinus Torvalds 	if (!dbh)
3651da177e4SLinus Torvalds 		return NULL;
3661da177e4SLinus Torvalds 	lock_buffer(dbh);
3671da177e4SLinus Torvalds 	memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
3681da177e4SLinus Torvalds 	set_buffer_uptodate(dbh);
3691da177e4SLinus Torvalds 	unlock_buffer(dbh);
3701da177e4SLinus Torvalds 	mark_buffer_dirty_inode(dbh, inode);
3711da177e4SLinus Torvalds 
3724b11111aSMarcin Slusarz 	sfibh.soffset = sfibh.eoffset =
373af793295SJan Kara 			f_pos & (inode->i_sb->s_blocksize - 1);
374ff116fc8SJan Kara 	sfibh.sbh = sfibh.ebh = NULL;
3751da177e4SLinus Torvalds 	dfibh.soffset = dfibh.eoffset = 0;
3761da177e4SLinus Torvalds 	dfibh.sbh = dfibh.ebh = dbh;
377af793295SJan Kara 	while (f_pos < size) {
37848d6d8ffSMarcin Slusarz 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
3794b11111aSMarcin Slusarz 		sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
3804b11111aSMarcin Slusarz 					 NULL, NULL, NULL);
381cb00ea35SCyrill Gorcunov 		if (!sfi) {
3823bf25cb4SJan Kara 			brelse(dbh);
3831da177e4SLinus Torvalds 			return NULL;
3841da177e4SLinus Torvalds 		}
38548d6d8ffSMarcin Slusarz 		iinfo->i_alloc_type = alloctype;
3861da177e4SLinus Torvalds 		sfi->descTag.tagLocation = cpu_to_le32(*block);
3871da177e4SLinus Torvalds 		dfibh.soffset = dfibh.eoffset;
3881da177e4SLinus Torvalds 		dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
3891da177e4SLinus Torvalds 		dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
3901da177e4SLinus Torvalds 		if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
3914b11111aSMarcin Slusarz 				 sfi->fileIdent +
3924b11111aSMarcin Slusarz 					le16_to_cpu(sfi->lengthOfImpUse))) {
39348d6d8ffSMarcin Slusarz 			iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
3943bf25cb4SJan Kara 			brelse(dbh);
3951da177e4SLinus Torvalds 			return NULL;
3961da177e4SLinus Torvalds 		}
3971da177e4SLinus Torvalds 	}
3981da177e4SLinus Torvalds 	mark_buffer_dirty_inode(dbh, inode);
3991da177e4SLinus Torvalds 
40048d6d8ffSMarcin Slusarz 	memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
40148d6d8ffSMarcin Slusarz 		iinfo->i_lenAlloc);
40248d6d8ffSMarcin Slusarz 	iinfo->i_lenAlloc = 0;
4031da177e4SLinus Torvalds 	eloc.logicalBlockNum = *block;
4044b11111aSMarcin Slusarz 	eloc.partitionReferenceNum =
40548d6d8ffSMarcin Slusarz 				iinfo->i_location.partitionReferenceNum;
4062c948b3fSJan Kara 	iinfo->i_lenExtents = inode->i_size;
407ff116fc8SJan Kara 	epos.bh = NULL;
40848d6d8ffSMarcin Slusarz 	epos.block = iinfo->i_location;
409ff116fc8SJan Kara 	epos.offset = udf_file_entry_alloc_offset(inode);
4102c948b3fSJan Kara 	udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
4111da177e4SLinus Torvalds 	/* UniqueID stuff */
4121da177e4SLinus Torvalds 
4133bf25cb4SJan Kara 	brelse(epos.bh);
4141da177e4SLinus Torvalds 	mark_inode_dirty(inode);
4151da177e4SLinus Torvalds 	return dbh;
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
418cb00ea35SCyrill Gorcunov static int udf_get_block(struct inode *inode, sector_t block,
419cb00ea35SCyrill Gorcunov 			 struct buffer_head *bh_result, int create)
4201da177e4SLinus Torvalds {
4211da177e4SLinus Torvalds 	int err, new;
4221ed16171SMarcin Slusarz 	sector_t phys = 0;
42348d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo;
4241da177e4SLinus Torvalds 
425cb00ea35SCyrill Gorcunov 	if (!create) {
4261da177e4SLinus Torvalds 		phys = udf_block_map(inode, block);
4271da177e4SLinus Torvalds 		if (phys)
4281da177e4SLinus Torvalds 			map_bh(bh_result, inode->i_sb, phys);
4291da177e4SLinus Torvalds 		return 0;
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	err = -EIO;
4331da177e4SLinus Torvalds 	new = 0;
43448d6d8ffSMarcin Slusarz 	iinfo = UDF_I(inode);
4354d0fb621SAlessio Igor Bogani 
4364d0fb621SAlessio Igor Bogani 	down_write(&iinfo->i_data_sem);
43748d6d8ffSMarcin Slusarz 	if (block == iinfo->i_next_alloc_block + 1) {
43848d6d8ffSMarcin Slusarz 		iinfo->i_next_alloc_block++;
43948d6d8ffSMarcin Slusarz 		iinfo->i_next_alloc_goal++;
4401da177e4SLinus Torvalds 	}
4411da177e4SLinus Torvalds 
44299600051SNamjae Jeon 	udf_clear_extent_cache(inode);
4437b0b0933SJan Kara 	phys = inode_getblk(inode, block, &err, &new);
4447b0b0933SJan Kara 	if (!phys)
4451da177e4SLinus Torvalds 		goto abort;
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	if (new)
4481da177e4SLinus Torvalds 		set_buffer_new(bh_result);
4491da177e4SLinus Torvalds 	map_bh(bh_result, inode->i_sb, phys);
45028de7948SCyrill Gorcunov 
4511da177e4SLinus Torvalds abort:
4524d0fb621SAlessio Igor Bogani 	up_write(&iinfo->i_data_sem);
4531da177e4SLinus Torvalds 	return err;
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
456cb00ea35SCyrill Gorcunov static struct buffer_head *udf_getblk(struct inode *inode, long block,
457cb00ea35SCyrill Gorcunov 				      int create, int *err)
4581da177e4SLinus Torvalds {
45928de7948SCyrill Gorcunov 	struct buffer_head *bh;
4601da177e4SLinus Torvalds 	struct buffer_head dummy;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	dummy.b_state = 0;
4631da177e4SLinus Torvalds 	dummy.b_blocknr = -1000;
4641da177e4SLinus Torvalds 	*err = udf_get_block(inode, block, &dummy, create);
465cb00ea35SCyrill Gorcunov 	if (!*err && buffer_mapped(&dummy)) {
4661da177e4SLinus Torvalds 		bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
467cb00ea35SCyrill Gorcunov 		if (buffer_new(&dummy)) {
4681da177e4SLinus Torvalds 			lock_buffer(bh);
4691da177e4SLinus Torvalds 			memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
4701da177e4SLinus Torvalds 			set_buffer_uptodate(bh);
4711da177e4SLinus Torvalds 			unlock_buffer(bh);
4721da177e4SLinus Torvalds 			mark_buffer_dirty_inode(bh, inode);
4731da177e4SLinus Torvalds 		}
4741da177e4SLinus Torvalds 		return bh;
4751da177e4SLinus Torvalds 	}
47628de7948SCyrill Gorcunov 
4771da177e4SLinus Torvalds 	return NULL;
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds 
48031170b6aSJan Kara /* Extend the file by 'blocks' blocks, return the number of extents added */
4817e49b6f2SJan Kara static int udf_do_extend_file(struct inode *inode,
4827e49b6f2SJan Kara 			      struct extent_position *last_pos,
4837e49b6f2SJan Kara 			      struct kernel_long_ad *last_ext,
4847e49b6f2SJan Kara 			      sector_t blocks)
48531170b6aSJan Kara {
48631170b6aSJan Kara 	sector_t add;
48731170b6aSJan Kara 	int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
48831170b6aSJan Kara 	struct super_block *sb = inode->i_sb;
4895ca4e4beSPekka Enberg 	struct kernel_lb_addr prealloc_loc = {};
49031170b6aSJan Kara 	int prealloc_len = 0;
49148d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo;
4927e49b6f2SJan Kara 	int err;
49331170b6aSJan Kara 
49431170b6aSJan Kara 	/* The previous extent is fake and we should not extend by anything
49531170b6aSJan Kara 	 * - there's nothing to do... */
49631170b6aSJan Kara 	if (!blocks && fake)
49731170b6aSJan Kara 		return 0;
49828de7948SCyrill Gorcunov 
49948d6d8ffSMarcin Slusarz 	iinfo = UDF_I(inode);
50031170b6aSJan Kara 	/* Round the last extent up to a multiple of block size */
50131170b6aSJan Kara 	if (last_ext->extLength & (sb->s_blocksize - 1)) {
50231170b6aSJan Kara 		last_ext->extLength =
50331170b6aSJan Kara 			(last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
50431170b6aSJan Kara 			(((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
50531170b6aSJan Kara 			  sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
50648d6d8ffSMarcin Slusarz 		iinfo->i_lenExtents =
50748d6d8ffSMarcin Slusarz 			(iinfo->i_lenExtents + sb->s_blocksize - 1) &
50831170b6aSJan Kara 			~(sb->s_blocksize - 1);
50931170b6aSJan Kara 	}
51028de7948SCyrill Gorcunov 
51131170b6aSJan Kara 	/* Last extent are just preallocated blocks? */
5124b11111aSMarcin Slusarz 	if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
5134b11111aSMarcin Slusarz 						EXT_NOT_RECORDED_ALLOCATED) {
51431170b6aSJan Kara 		/* Save the extent so that we can reattach it to the end */
51531170b6aSJan Kara 		prealloc_loc = last_ext->extLocation;
51631170b6aSJan Kara 		prealloc_len = last_ext->extLength;
51731170b6aSJan Kara 		/* Mark the extent as a hole */
51831170b6aSJan Kara 		last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
51931170b6aSJan Kara 			(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
52031170b6aSJan Kara 		last_ext->extLocation.logicalBlockNum = 0;
52131170b6aSJan Kara 		last_ext->extLocation.partitionReferenceNum = 0;
52231170b6aSJan Kara 	}
52328de7948SCyrill Gorcunov 
52431170b6aSJan Kara 	/* Can we merge with the previous extent? */
5254b11111aSMarcin Slusarz 	if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
5264b11111aSMarcin Slusarz 					EXT_NOT_RECORDED_NOT_ALLOCATED) {
5274b11111aSMarcin Slusarz 		add = ((1 << 30) - sb->s_blocksize -
5284b11111aSMarcin Slusarz 			(last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
5294b11111aSMarcin Slusarz 			sb->s_blocksize_bits;
53031170b6aSJan Kara 		if (add > blocks)
53131170b6aSJan Kara 			add = blocks;
53231170b6aSJan Kara 		blocks -= add;
53331170b6aSJan Kara 		last_ext->extLength += add << sb->s_blocksize_bits;
53431170b6aSJan Kara 	}
53531170b6aSJan Kara 
53631170b6aSJan Kara 	if (fake) {
53797e961fdSPekka Enberg 		udf_add_aext(inode, last_pos, &last_ext->extLocation,
53831170b6aSJan Kara 			     last_ext->extLength, 1);
53931170b6aSJan Kara 		count++;
5406c371578SJan Kara 	} else {
5416c371578SJan Kara 		struct kernel_lb_addr tmploc;
5426c371578SJan Kara 		uint32_t tmplen;
5436c371578SJan Kara 
54497e961fdSPekka Enberg 		udf_write_aext(inode, last_pos, &last_ext->extLocation,
5454b11111aSMarcin Slusarz 				last_ext->extLength, 1);
5466c371578SJan Kara 		/*
5476c371578SJan Kara 		 * We've rewritten the last extent but there may be empty
5486c371578SJan Kara 		 * indirect extent after it - enter it.
5496c371578SJan Kara 		 */
5506c371578SJan Kara 		udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
5516c371578SJan Kara 	}
55228de7948SCyrill Gorcunov 
55331170b6aSJan Kara 	/* Managed to do everything necessary? */
55431170b6aSJan Kara 	if (!blocks)
55531170b6aSJan Kara 		goto out;
55631170b6aSJan Kara 
55731170b6aSJan Kara 	/* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
55831170b6aSJan Kara 	last_ext->extLocation.logicalBlockNum = 0;
55931170b6aSJan Kara 	last_ext->extLocation.partitionReferenceNum = 0;
56031170b6aSJan Kara 	add = (1 << (30-sb->s_blocksize_bits)) - 1;
5614b11111aSMarcin Slusarz 	last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
5624b11111aSMarcin Slusarz 				(add << sb->s_blocksize_bits);
56328de7948SCyrill Gorcunov 
56431170b6aSJan Kara 	/* Create enough extents to cover the whole hole */
56531170b6aSJan Kara 	while (blocks > add) {
56631170b6aSJan Kara 		blocks -= add;
5677e49b6f2SJan Kara 		err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
5687e49b6f2SJan Kara 				   last_ext->extLength, 1);
5697e49b6f2SJan Kara 		if (err)
5707e49b6f2SJan Kara 			return err;
57131170b6aSJan Kara 		count++;
57231170b6aSJan Kara 	}
57331170b6aSJan Kara 	if (blocks) {
57431170b6aSJan Kara 		last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
57531170b6aSJan Kara 			(blocks << sb->s_blocksize_bits);
5767e49b6f2SJan Kara 		err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
5777e49b6f2SJan Kara 				   last_ext->extLength, 1);
5787e49b6f2SJan Kara 		if (err)
5797e49b6f2SJan Kara 			return err;
58031170b6aSJan Kara 		count++;
58131170b6aSJan Kara 	}
58228de7948SCyrill Gorcunov 
58331170b6aSJan Kara out:
58431170b6aSJan Kara 	/* Do we have some preallocated blocks saved? */
58531170b6aSJan Kara 	if (prealloc_len) {
5867e49b6f2SJan Kara 		err = udf_add_aext(inode, last_pos, &prealloc_loc,
5877e49b6f2SJan Kara 				   prealloc_len, 1);
5887e49b6f2SJan Kara 		if (err)
5897e49b6f2SJan Kara 			return err;
59031170b6aSJan Kara 		last_ext->extLocation = prealloc_loc;
59131170b6aSJan Kara 		last_ext->extLength = prealloc_len;
59231170b6aSJan Kara 		count++;
59331170b6aSJan Kara 	}
59428de7948SCyrill Gorcunov 
59531170b6aSJan Kara 	/* last_pos should point to the last written extent... */
59648d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5975ca4e4beSPekka Enberg 		last_pos->offset -= sizeof(struct short_ad);
59848d6d8ffSMarcin Slusarz 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
5995ca4e4beSPekka Enberg 		last_pos->offset -= sizeof(struct long_ad);
60031170b6aSJan Kara 	else
6017e49b6f2SJan Kara 		return -EIO;
60228de7948SCyrill Gorcunov 
60331170b6aSJan Kara 	return count;
60431170b6aSJan Kara }
60531170b6aSJan Kara 
6067e49b6f2SJan Kara static int udf_extend_file(struct inode *inode, loff_t newsize)
6077e49b6f2SJan Kara {
6087e49b6f2SJan Kara 
6097e49b6f2SJan Kara 	struct extent_position epos;
6107e49b6f2SJan Kara 	struct kernel_lb_addr eloc;
6117e49b6f2SJan Kara 	uint32_t elen;
6127e49b6f2SJan Kara 	int8_t etype;
6137e49b6f2SJan Kara 	struct super_block *sb = inode->i_sb;
6147e49b6f2SJan Kara 	sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
6157e49b6f2SJan Kara 	int adsize;
6167e49b6f2SJan Kara 	struct udf_inode_info *iinfo = UDF_I(inode);
6177e49b6f2SJan Kara 	struct kernel_long_ad extent;
6187e49b6f2SJan Kara 	int err;
6197e49b6f2SJan Kara 
6207e49b6f2SJan Kara 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
6217e49b6f2SJan Kara 		adsize = sizeof(struct short_ad);
6227e49b6f2SJan Kara 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
6237e49b6f2SJan Kara 		adsize = sizeof(struct long_ad);
6247e49b6f2SJan Kara 	else
6257e49b6f2SJan Kara 		BUG();
6267e49b6f2SJan Kara 
6277e49b6f2SJan Kara 	etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
6287e49b6f2SJan Kara 
6297e49b6f2SJan Kara 	/* File has extent covering the new size (could happen when extending
6307e49b6f2SJan Kara 	 * inside a block)? */
6317e49b6f2SJan Kara 	if (etype != -1)
6327e49b6f2SJan Kara 		return 0;
6337e49b6f2SJan Kara 	if (newsize & (sb->s_blocksize - 1))
6347e49b6f2SJan Kara 		offset++;
6357e49b6f2SJan Kara 	/* Extended file just to the boundary of the last file block? */
6367e49b6f2SJan Kara 	if (offset == 0)
6377e49b6f2SJan Kara 		return 0;
6387e49b6f2SJan Kara 
6397e49b6f2SJan Kara 	/* Truncate is extending the file by 'offset' blocks */
6407e49b6f2SJan Kara 	if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
6417e49b6f2SJan Kara 	    (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
6427e49b6f2SJan Kara 		/* File has no extents at all or has empty last
6437e49b6f2SJan Kara 		 * indirect extent! Create a fake extent... */
6447e49b6f2SJan Kara 		extent.extLocation.logicalBlockNum = 0;
6457e49b6f2SJan Kara 		extent.extLocation.partitionReferenceNum = 0;
6467e49b6f2SJan Kara 		extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
6477e49b6f2SJan Kara 	} else {
6487e49b6f2SJan Kara 		epos.offset -= adsize;
6497e49b6f2SJan Kara 		etype = udf_next_aext(inode, &epos, &extent.extLocation,
6507e49b6f2SJan Kara 				      &extent.extLength, 0);
6517e49b6f2SJan Kara 		extent.extLength |= etype << 30;
6527e49b6f2SJan Kara 	}
6537e49b6f2SJan Kara 	err = udf_do_extend_file(inode, &epos, &extent, offset);
6547e49b6f2SJan Kara 	if (err < 0)
6557e49b6f2SJan Kara 		goto out;
6567e49b6f2SJan Kara 	err = 0;
6577e49b6f2SJan Kara 	iinfo->i_lenExtents = newsize;
6587e49b6f2SJan Kara out:
6597e49b6f2SJan Kara 	brelse(epos.bh);
6607e49b6f2SJan Kara 	return err;
6617e49b6f2SJan Kara }
6627e49b6f2SJan Kara 
6637b0b0933SJan Kara static sector_t inode_getblk(struct inode *inode, sector_t block,
6647b0b0933SJan Kara 			     int *err, int *new)
6651da177e4SLinus Torvalds {
6665ca4e4beSPekka Enberg 	struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
667ff116fc8SJan Kara 	struct extent_position prev_epos, cur_epos, next_epos;
6681da177e4SLinus Torvalds 	int count = 0, startnum = 0, endnum = 0;
66985d71244SJan Kara 	uint32_t elen = 0, tmpelen;
6705ca4e4beSPekka Enberg 	struct kernel_lb_addr eloc, tmpeloc;
6711da177e4SLinus Torvalds 	int c = 1;
67260448b1dSJan Kara 	loff_t lbcount = 0, b_off = 0;
67360448b1dSJan Kara 	uint32_t newblocknum, newblock;
67460448b1dSJan Kara 	sector_t offset = 0;
6751da177e4SLinus Torvalds 	int8_t etype;
67648d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
67748d6d8ffSMarcin Slusarz 	int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
67831170b6aSJan Kara 	int lastblock = 0;
679fb719c59SNamjae Jeon 	bool isBeyondEOF;
6801da177e4SLinus Torvalds 
6817b0b0933SJan Kara 	*err = 0;
6827b0b0933SJan Kara 	*new = 0;
683ff116fc8SJan Kara 	prev_epos.offset = udf_file_entry_alloc_offset(inode);
68448d6d8ffSMarcin Slusarz 	prev_epos.block = iinfo->i_location;
685ff116fc8SJan Kara 	prev_epos.bh = NULL;
686ff116fc8SJan Kara 	cur_epos = next_epos = prev_epos;
68760448b1dSJan Kara 	b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	/* find the extent which contains the block we are looking for.
6901da177e4SLinus Torvalds 	   alternate between laarr[0] and laarr[1] for locations of the
6911da177e4SLinus Torvalds 	   current extent, and the previous extent */
692cb00ea35SCyrill Gorcunov 	do {
693cb00ea35SCyrill Gorcunov 		if (prev_epos.bh != cur_epos.bh) {
6943bf25cb4SJan Kara 			brelse(prev_epos.bh);
6953bf25cb4SJan Kara 			get_bh(cur_epos.bh);
696ff116fc8SJan Kara 			prev_epos.bh = cur_epos.bh;
6971da177e4SLinus Torvalds 		}
698cb00ea35SCyrill Gorcunov 		if (cur_epos.bh != next_epos.bh) {
6993bf25cb4SJan Kara 			brelse(cur_epos.bh);
7003bf25cb4SJan Kara 			get_bh(next_epos.bh);
701ff116fc8SJan Kara 			cur_epos.bh = next_epos.bh;
7021da177e4SLinus Torvalds 		}
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 		lbcount += elen;
7051da177e4SLinus Torvalds 
706ff116fc8SJan Kara 		prev_epos.block = cur_epos.block;
707ff116fc8SJan Kara 		cur_epos.block = next_epos.block;
7081da177e4SLinus Torvalds 
709ff116fc8SJan Kara 		prev_epos.offset = cur_epos.offset;
710ff116fc8SJan Kara 		cur_epos.offset = next_epos.offset;
7111da177e4SLinus Torvalds 
7124b11111aSMarcin Slusarz 		etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
7134b11111aSMarcin Slusarz 		if (etype == -1)
7141da177e4SLinus Torvalds 			break;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 		c = !c;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 		laarr[c].extLength = (etype << 30) | elen;
7191da177e4SLinus Torvalds 		laarr[c].extLocation = eloc;
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 		if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
7221da177e4SLinus Torvalds 			pgoal = eloc.logicalBlockNum +
7231da177e4SLinus Torvalds 				((elen + inode->i_sb->s_blocksize - 1) >>
7241da177e4SLinus Torvalds 				 inode->i_sb->s_blocksize_bits);
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 		count++;
7271da177e4SLinus Torvalds 	} while (lbcount + elen <= b_off);
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	b_off -= lbcount;
7301da177e4SLinus Torvalds 	offset = b_off >> inode->i_sb->s_blocksize_bits;
73185d71244SJan Kara 	/*
73285d71244SJan Kara 	 * Move prev_epos and cur_epos into indirect extent if we are at
73385d71244SJan Kara 	 * the pointer to it
73485d71244SJan Kara 	 */
73585d71244SJan Kara 	udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
73685d71244SJan Kara 	udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 	/* if the extent is allocated and recorded, return the block
7391da177e4SLinus Torvalds 	   if the extent is not a multiple of the blocksize, round up */
7401da177e4SLinus Torvalds 
741cb00ea35SCyrill Gorcunov 	if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
742cb00ea35SCyrill Gorcunov 		if (elen & (inode->i_sb->s_blocksize - 1)) {
7431da177e4SLinus Torvalds 			elen = EXT_RECORDED_ALLOCATED |
7441da177e4SLinus Torvalds 				((elen + inode->i_sb->s_blocksize - 1) &
7451da177e4SLinus Torvalds 				 ~(inode->i_sb->s_blocksize - 1));
7467e49b6f2SJan Kara 			udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
7471da177e4SLinus Torvalds 		}
7483bf25cb4SJan Kara 		brelse(prev_epos.bh);
7493bf25cb4SJan Kara 		brelse(cur_epos.bh);
7503bf25cb4SJan Kara 		brelse(next_epos.bh);
75197e961fdSPekka Enberg 		newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
7527b0b0933SJan Kara 		return newblock;
7531da177e4SLinus Torvalds 	}
7541da177e4SLinus Torvalds 
75531170b6aSJan Kara 	/* Are we beyond EOF? */
756cb00ea35SCyrill Gorcunov 	if (etype == -1) {
75731170b6aSJan Kara 		int ret;
7586981498dSFabian Frederick 		isBeyondEOF = true;
75931170b6aSJan Kara 		if (count) {
76031170b6aSJan Kara 			if (c)
76131170b6aSJan Kara 				laarr[0] = laarr[1];
76231170b6aSJan Kara 			startnum = 1;
763cb00ea35SCyrill Gorcunov 		} else {
76431170b6aSJan Kara 			/* Create a fake extent when there's not one */
7654b11111aSMarcin Slusarz 			memset(&laarr[0].extLocation, 0x00,
7665ca4e4beSPekka Enberg 				sizeof(struct kernel_lb_addr));
76731170b6aSJan Kara 			laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
7687e49b6f2SJan Kara 			/* Will udf_do_extend_file() create real extent from
7694b11111aSMarcin Slusarz 			   a fake one? */
77031170b6aSJan Kara 			startnum = (offset > 0);
77131170b6aSJan Kara 		}
77231170b6aSJan Kara 		/* Create extents for the hole between EOF and offset */
7737e49b6f2SJan Kara 		ret = udf_do_extend_file(inode, &prev_epos, laarr, offset);
7747e49b6f2SJan Kara 		if (ret < 0) {
77531170b6aSJan Kara 			brelse(prev_epos.bh);
77631170b6aSJan Kara 			brelse(cur_epos.bh);
77731170b6aSJan Kara 			brelse(next_epos.bh);
7787e49b6f2SJan Kara 			*err = ret;
7797b0b0933SJan Kara 			return 0;
78031170b6aSJan Kara 		}
78131170b6aSJan Kara 		c = 0;
78231170b6aSJan Kara 		offset = 0;
78331170b6aSJan Kara 		count += ret;
78431170b6aSJan Kara 		/* We are not covered by a preallocated extent? */
7854b11111aSMarcin Slusarz 		if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
7864b11111aSMarcin Slusarz 						EXT_NOT_RECORDED_ALLOCATED) {
78731170b6aSJan Kara 			/* Is there any real extent? - otherwise we overwrite
78831170b6aSJan Kara 			 * the fake one... */
78931170b6aSJan Kara 			if (count)
7901da177e4SLinus Torvalds 				c = !c;
7911da177e4SLinus Torvalds 			laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
79231170b6aSJan Kara 				inode->i_sb->s_blocksize;
7934b11111aSMarcin Slusarz 			memset(&laarr[c].extLocation, 0x00,
7945ca4e4beSPekka Enberg 				sizeof(struct kernel_lb_addr));
7951da177e4SLinus Torvalds 			count++;
79631170b6aSJan Kara 		}
79731170b6aSJan Kara 		endnum = c + 1;
7981da177e4SLinus Torvalds 		lastblock = 1;
799cb00ea35SCyrill Gorcunov 	} else {
8006981498dSFabian Frederick 		isBeyondEOF = false;
8011da177e4SLinus Torvalds 		endnum = startnum = ((count > 2) ? 2 : count);
8021da177e4SLinus Torvalds 
8034b11111aSMarcin Slusarz 		/* if the current extent is in position 0,
8044b11111aSMarcin Slusarz 		   swap it with the previous */
805cb00ea35SCyrill Gorcunov 		if (!c && count != 1) {
8061da177e4SLinus Torvalds 			laarr[2] = laarr[0];
8071da177e4SLinus Torvalds 			laarr[0] = laarr[1];
8081da177e4SLinus Torvalds 			laarr[1] = laarr[2];
8091da177e4SLinus Torvalds 			c = 1;
8101da177e4SLinus Torvalds 		}
8111da177e4SLinus Torvalds 
8124b11111aSMarcin Slusarz 		/* if the current block is located in an extent,
8134b11111aSMarcin Slusarz 		   read the next extent */
8144b11111aSMarcin Slusarz 		etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
8154b11111aSMarcin Slusarz 		if (etype != -1) {
8161da177e4SLinus Torvalds 			laarr[c + 1].extLength = (etype << 30) | elen;
8171da177e4SLinus Torvalds 			laarr[c + 1].extLocation = eloc;
8181da177e4SLinus Torvalds 			count++;
8191da177e4SLinus Torvalds 			startnum++;
8201da177e4SLinus Torvalds 			endnum++;
8214b11111aSMarcin Slusarz 		} else
8221da177e4SLinus Torvalds 			lastblock = 1;
8231da177e4SLinus Torvalds 	}
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	/* if the current extent is not recorded but allocated, get the
82628de7948SCyrill Gorcunov 	 * block in the extent corresponding to the requested block */
8274b11111aSMarcin Slusarz 	if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
8281da177e4SLinus Torvalds 		newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
8294b11111aSMarcin Slusarz 	else { /* otherwise, allocate a new block */
83048d6d8ffSMarcin Slusarz 		if (iinfo->i_next_alloc_block == block)
83148d6d8ffSMarcin Slusarz 			goal = iinfo->i_next_alloc_goal;
8321da177e4SLinus Torvalds 
833cb00ea35SCyrill Gorcunov 		if (!goal) {
8344b11111aSMarcin Slusarz 			if (!(goal = pgoal)) /* XXX: what was intended here? */
83548d6d8ffSMarcin Slusarz 				goal = iinfo->i_location.logicalBlockNum + 1;
8361da177e4SLinus Torvalds 		}
8371da177e4SLinus Torvalds 
8384b11111aSMarcin Slusarz 		newblocknum = udf_new_block(inode->i_sb, inode,
83948d6d8ffSMarcin Slusarz 				iinfo->i_location.partitionReferenceNum,
8404b11111aSMarcin Slusarz 				goal, err);
8414b11111aSMarcin Slusarz 		if (!newblocknum) {
8423bf25cb4SJan Kara 			brelse(prev_epos.bh);
8432fb7d99dSNamjae Jeon 			brelse(cur_epos.bh);
8442fb7d99dSNamjae Jeon 			brelse(next_epos.bh);
8451da177e4SLinus Torvalds 			*err = -ENOSPC;
8467b0b0933SJan Kara 			return 0;
8471da177e4SLinus Torvalds 		}
848fb719c59SNamjae Jeon 		if (isBeyondEOF)
84948d6d8ffSMarcin Slusarz 			iinfo->i_lenExtents += inode->i_sb->s_blocksize;
8501da177e4SLinus Torvalds 	}
8511da177e4SLinus Torvalds 
8524b11111aSMarcin Slusarz 	/* if the extent the requsted block is located in contains multiple
8534b11111aSMarcin Slusarz 	 * blocks, split the extent into at most three extents. blocks prior
8544b11111aSMarcin Slusarz 	 * to requested block, requested block, and blocks after requested
8554b11111aSMarcin Slusarz 	 * block */
8561da177e4SLinus Torvalds 	udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
8571da177e4SLinus Torvalds 
85881056dd0SJan Kara 	/* We preallocate blocks only for regular files. It also makes sense
85981056dd0SJan Kara 	 * for directories but there's a problem when to drop the
86081056dd0SJan Kara 	 * preallocation. We might use some delayed work for that but I feel
86181056dd0SJan Kara 	 * it's overengineering for a filesystem like UDF. */
86281056dd0SJan Kara 	if (S_ISREG(inode->i_mode))
8631da177e4SLinus Torvalds 		udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	/* merge any continuous blocks in laarr */
8661da177e4SLinus Torvalds 	udf_merge_extents(inode, laarr, &endnum);
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	/* write back the new extents, inserting new extents if the new number
86928de7948SCyrill Gorcunov 	 * of extents is greater than the old number, and deleting extents if
87028de7948SCyrill Gorcunov 	 * the new number of extents is less than the old number */
871ff116fc8SJan Kara 	udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
8721da177e4SLinus Torvalds 
8733bf25cb4SJan Kara 	brelse(prev_epos.bh);
8742fb7d99dSNamjae Jeon 	brelse(cur_epos.bh);
8752fb7d99dSNamjae Jeon 	brelse(next_epos.bh);
8761da177e4SLinus Torvalds 
8774b11111aSMarcin Slusarz 	newblock = udf_get_pblock(inode->i_sb, newblocknum,
87848d6d8ffSMarcin Slusarz 				iinfo->i_location.partitionReferenceNum, 0);
8797b0b0933SJan Kara 	if (!newblock) {
8807b0b0933SJan Kara 		*err = -EIO;
8817b0b0933SJan Kara 		return 0;
8827b0b0933SJan Kara 	}
8831da177e4SLinus Torvalds 	*new = 1;
88448d6d8ffSMarcin Slusarz 	iinfo->i_next_alloc_block = block;
88548d6d8ffSMarcin Slusarz 	iinfo->i_next_alloc_goal = newblocknum;
886c2050a45SDeepa Dinamani 	inode->i_ctime = current_time(inode);
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	if (IS_SYNC(inode))
8891da177e4SLinus Torvalds 		udf_sync_inode(inode);
8901da177e4SLinus Torvalds 	else
8911da177e4SLinus Torvalds 		mark_inode_dirty(inode);
89228de7948SCyrill Gorcunov 
8937b0b0933SJan Kara 	return newblock;
8941da177e4SLinus Torvalds }
8951da177e4SLinus Torvalds 
896cb00ea35SCyrill Gorcunov static void udf_split_extents(struct inode *inode, int *c, int offset,
897*3cc6f844SFabian Frederick 			      int newblocknum, struct kernel_long_ad *laarr,
898cb00ea35SCyrill Gorcunov 			      int *endnum)
8991da177e4SLinus Torvalds {
9004b11111aSMarcin Slusarz 	unsigned long blocksize = inode->i_sb->s_blocksize;
9014b11111aSMarcin Slusarz 	unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
9024b11111aSMarcin Slusarz 
9031da177e4SLinus Torvalds 	if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
9044b11111aSMarcin Slusarz 	    (laarr[*c].extLength >> 30) ==
9054b11111aSMarcin Slusarz 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
9061da177e4SLinus Torvalds 		int curr = *c;
9071da177e4SLinus Torvalds 		int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
9084b11111aSMarcin Slusarz 			    blocksize - 1) >> blocksize_bits;
9091da177e4SLinus Torvalds 		int8_t etype = (laarr[curr].extLength >> 30);
9101da177e4SLinus Torvalds 
9114b11111aSMarcin Slusarz 		if (blen == 1)
91228de7948SCyrill Gorcunov 			;
9134b11111aSMarcin Slusarz 		else if (!offset || blen == offset + 1) {
9141da177e4SLinus Torvalds 			laarr[curr + 2] = laarr[curr + 1];
9151da177e4SLinus Torvalds 			laarr[curr + 1] = laarr[curr];
916cb00ea35SCyrill Gorcunov 		} else {
9171da177e4SLinus Torvalds 			laarr[curr + 3] = laarr[curr + 1];
9181da177e4SLinus Torvalds 			laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
9191da177e4SLinus Torvalds 		}
9201da177e4SLinus Torvalds 
921cb00ea35SCyrill Gorcunov 		if (offset) {
922cb00ea35SCyrill Gorcunov 			if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
9234b11111aSMarcin Slusarz 				udf_free_blocks(inode->i_sb, inode,
92497e961fdSPekka Enberg 						&laarr[curr].extLocation,
9254b11111aSMarcin Slusarz 						0, offset);
9264b11111aSMarcin Slusarz 				laarr[curr].extLength =
9274b11111aSMarcin Slusarz 					EXT_NOT_RECORDED_NOT_ALLOCATED |
9284b11111aSMarcin Slusarz 					(offset << blocksize_bits);
9291da177e4SLinus Torvalds 				laarr[curr].extLocation.logicalBlockNum = 0;
9304b11111aSMarcin Slusarz 				laarr[curr].extLocation.
9314b11111aSMarcin Slusarz 						partitionReferenceNum = 0;
9324b11111aSMarcin Slusarz 			} else
9331da177e4SLinus Torvalds 				laarr[curr].extLength = (etype << 30) |
9344b11111aSMarcin Slusarz 					(offset << blocksize_bits);
9351da177e4SLinus Torvalds 			curr++;
9361da177e4SLinus Torvalds 			(*c)++;
9371da177e4SLinus Torvalds 			(*endnum)++;
9381da177e4SLinus Torvalds 		}
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 		laarr[curr].extLocation.logicalBlockNum = newblocknum;
9411da177e4SLinus Torvalds 		if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
9421da177e4SLinus Torvalds 			laarr[curr].extLocation.partitionReferenceNum =
943c0b34438SMarcin Slusarz 				UDF_I(inode)->i_location.partitionReferenceNum;
9441da177e4SLinus Torvalds 		laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
9454b11111aSMarcin Slusarz 			blocksize;
9461da177e4SLinus Torvalds 		curr++;
9471da177e4SLinus Torvalds 
948cb00ea35SCyrill Gorcunov 		if (blen != offset + 1) {
9491da177e4SLinus Torvalds 			if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
9504b11111aSMarcin Slusarz 				laarr[curr].extLocation.logicalBlockNum +=
9514b11111aSMarcin Slusarz 								offset + 1;
95228de7948SCyrill Gorcunov 			laarr[curr].extLength = (etype << 30) |
9534b11111aSMarcin Slusarz 				((blen - (offset + 1)) << blocksize_bits);
9541da177e4SLinus Torvalds 			curr++;
9551da177e4SLinus Torvalds 			(*endnum)++;
9561da177e4SLinus Torvalds 		}
9571da177e4SLinus Torvalds 	}
9581da177e4SLinus Torvalds }
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
961*3cc6f844SFabian Frederick 				 struct kernel_long_ad *laarr,
962cb00ea35SCyrill Gorcunov 				 int *endnum)
9631da177e4SLinus Torvalds {
9641da177e4SLinus Torvalds 	int start, length = 0, currlength = 0, i;
9651da177e4SLinus Torvalds 
966cb00ea35SCyrill Gorcunov 	if (*endnum >= (c + 1)) {
9671da177e4SLinus Torvalds 		if (!lastblock)
9681da177e4SLinus Torvalds 			return;
9691da177e4SLinus Torvalds 		else
9701da177e4SLinus Torvalds 			start = c;
971cb00ea35SCyrill Gorcunov 	} else {
9724b11111aSMarcin Slusarz 		if ((laarr[c + 1].extLength >> 30) ==
9734b11111aSMarcin Slusarz 					(EXT_NOT_RECORDED_ALLOCATED >> 30)) {
9741da177e4SLinus Torvalds 			start = c + 1;
9754b11111aSMarcin Slusarz 			length = currlength =
9764b11111aSMarcin Slusarz 				(((laarr[c + 1].extLength &
9774b11111aSMarcin Slusarz 					UDF_EXTENT_LENGTH_MASK) +
9784b11111aSMarcin Slusarz 				inode->i_sb->s_blocksize - 1) >>
9794b11111aSMarcin Slusarz 				inode->i_sb->s_blocksize_bits);
9804b11111aSMarcin Slusarz 		} else
9811da177e4SLinus Torvalds 			start = c;
9821da177e4SLinus Torvalds 	}
9831da177e4SLinus Torvalds 
984cb00ea35SCyrill Gorcunov 	for (i = start + 1; i <= *endnum; i++) {
985cb00ea35SCyrill Gorcunov 		if (i == *endnum) {
9861da177e4SLinus Torvalds 			if (lastblock)
9871da177e4SLinus Torvalds 				length += UDF_DEFAULT_PREALLOC_BLOCKS;
9884b11111aSMarcin Slusarz 		} else if ((laarr[i].extLength >> 30) ==
9894b11111aSMarcin Slusarz 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
9904b11111aSMarcin Slusarz 			length += (((laarr[i].extLength &
9914b11111aSMarcin Slusarz 						UDF_EXTENT_LENGTH_MASK) +
9924b11111aSMarcin Slusarz 				    inode->i_sb->s_blocksize - 1) >>
9934b11111aSMarcin Slusarz 				    inode->i_sb->s_blocksize_bits);
9944b11111aSMarcin Slusarz 		} else
9951da177e4SLinus Torvalds 			break;
9961da177e4SLinus Torvalds 	}
9971da177e4SLinus Torvalds 
998cb00ea35SCyrill Gorcunov 	if (length) {
9991da177e4SLinus Torvalds 		int next = laarr[start].extLocation.logicalBlockNum +
10001da177e4SLinus Torvalds 			(((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
10014b11111aSMarcin Slusarz 			  inode->i_sb->s_blocksize - 1) >>
10024b11111aSMarcin Slusarz 			  inode->i_sb->s_blocksize_bits);
10031da177e4SLinus Torvalds 		int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
100428de7948SCyrill Gorcunov 				laarr[start].extLocation.partitionReferenceNum,
10054b11111aSMarcin Slusarz 				next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
10064b11111aSMarcin Slusarz 				length : UDF_DEFAULT_PREALLOC_BLOCKS) -
10074b11111aSMarcin Slusarz 				currlength);
1008cb00ea35SCyrill Gorcunov 		if (numalloc) 	{
10094b11111aSMarcin Slusarz 			if (start == (c + 1))
10101da177e4SLinus Torvalds 				laarr[start].extLength +=
10114b11111aSMarcin Slusarz 					(numalloc <<
10124b11111aSMarcin Slusarz 					 inode->i_sb->s_blocksize_bits);
10134b11111aSMarcin Slusarz 			else {
10141da177e4SLinus Torvalds 				memmove(&laarr[c + 2], &laarr[c + 1],
10155ca4e4beSPekka Enberg 					sizeof(struct long_ad) * (*endnum - (c + 1)));
10161da177e4SLinus Torvalds 				(*endnum)++;
10171da177e4SLinus Torvalds 				laarr[c + 1].extLocation.logicalBlockNum = next;
10181da177e4SLinus Torvalds 				laarr[c + 1].extLocation.partitionReferenceNum =
10194b11111aSMarcin Slusarz 					laarr[c].extLocation.
10204b11111aSMarcin Slusarz 							partitionReferenceNum;
10214b11111aSMarcin Slusarz 				laarr[c + 1].extLength =
10224b11111aSMarcin Slusarz 					EXT_NOT_RECORDED_ALLOCATED |
10234b11111aSMarcin Slusarz 					(numalloc <<
10244b11111aSMarcin Slusarz 					 inode->i_sb->s_blocksize_bits);
10251da177e4SLinus Torvalds 				start = c + 1;
10261da177e4SLinus Torvalds 			}
10271da177e4SLinus Torvalds 
1028cb00ea35SCyrill Gorcunov 			for (i = start + 1; numalloc && i < *endnum; i++) {
10294b11111aSMarcin Slusarz 				int elen = ((laarr[i].extLength &
10304b11111aSMarcin Slusarz 						UDF_EXTENT_LENGTH_MASK) +
10314b11111aSMarcin Slusarz 					    inode->i_sb->s_blocksize - 1) >>
10324b11111aSMarcin Slusarz 					    inode->i_sb->s_blocksize_bits;
10331da177e4SLinus Torvalds 
1034cb00ea35SCyrill Gorcunov 				if (elen > numalloc) {
10351da177e4SLinus Torvalds 					laarr[i].extLength -=
10364b11111aSMarcin Slusarz 						(numalloc <<
10374b11111aSMarcin Slusarz 						 inode->i_sb->s_blocksize_bits);
10381da177e4SLinus Torvalds 					numalloc = 0;
1039cb00ea35SCyrill Gorcunov 				} else {
10401da177e4SLinus Torvalds 					numalloc -= elen;
10411da177e4SLinus Torvalds 					if (*endnum > (i + 1))
10424b11111aSMarcin Slusarz 						memmove(&laarr[i],
10434b11111aSMarcin Slusarz 							&laarr[i + 1],
10445ca4e4beSPekka Enberg 							sizeof(struct long_ad) *
10454b11111aSMarcin Slusarz 							(*endnum - (i + 1)));
10461da177e4SLinus Torvalds 					i--;
10471da177e4SLinus Torvalds 					(*endnum)--;
10481da177e4SLinus Torvalds 				}
10491da177e4SLinus Torvalds 			}
1050c0b34438SMarcin Slusarz 			UDF_I(inode)->i_lenExtents +=
10514b11111aSMarcin Slusarz 				numalloc << inode->i_sb->s_blocksize_bits;
10521da177e4SLinus Torvalds 		}
10531da177e4SLinus Torvalds 	}
10541da177e4SLinus Torvalds }
10551da177e4SLinus Torvalds 
1056*3cc6f844SFabian Frederick static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
1057cb00ea35SCyrill Gorcunov 			      int *endnum)
10581da177e4SLinus Torvalds {
10591da177e4SLinus Torvalds 	int i;
10604b11111aSMarcin Slusarz 	unsigned long blocksize = inode->i_sb->s_blocksize;
10614b11111aSMarcin Slusarz 	unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
10621da177e4SLinus Torvalds 
1063cb00ea35SCyrill Gorcunov 	for (i = 0; i < (*endnum - 1); i++) {
10645ca4e4beSPekka Enberg 		struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
10655ca4e4beSPekka Enberg 		struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
10661da177e4SLinus Torvalds 
10674b11111aSMarcin Slusarz 		if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
10684b11111aSMarcin Slusarz 			(((li->extLength >> 30) ==
10694b11111aSMarcin Slusarz 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
10704b11111aSMarcin Slusarz 			((lip1->extLocation.logicalBlockNum -
10714b11111aSMarcin Slusarz 			  li->extLocation.logicalBlockNum) ==
10724b11111aSMarcin Slusarz 			(((li->extLength & UDF_EXTENT_LENGTH_MASK) +
10734b11111aSMarcin Slusarz 			blocksize - 1) >> blocksize_bits)))) {
10744b11111aSMarcin Slusarz 
10754b11111aSMarcin Slusarz 			if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
10764b11111aSMarcin Slusarz 				(lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
10774b11111aSMarcin Slusarz 				blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
10784b11111aSMarcin Slusarz 				lip1->extLength = (lip1->extLength -
10794b11111aSMarcin Slusarz 						  (li->extLength &
10804b11111aSMarcin Slusarz 						   UDF_EXTENT_LENGTH_MASK) +
10814b11111aSMarcin Slusarz 						   UDF_EXTENT_LENGTH_MASK) &
10824b11111aSMarcin Slusarz 							~(blocksize - 1);
10834b11111aSMarcin Slusarz 				li->extLength = (li->extLength &
10844b11111aSMarcin Slusarz 						 UDF_EXTENT_FLAG_MASK) +
10854b11111aSMarcin Slusarz 						(UDF_EXTENT_LENGTH_MASK + 1) -
10864b11111aSMarcin Slusarz 						blocksize;
10874b11111aSMarcin Slusarz 				lip1->extLocation.logicalBlockNum =
10884b11111aSMarcin Slusarz 					li->extLocation.logicalBlockNum +
10894b11111aSMarcin Slusarz 					((li->extLength &
10904b11111aSMarcin Slusarz 						UDF_EXTENT_LENGTH_MASK) >>
10914b11111aSMarcin Slusarz 						blocksize_bits);
1092cb00ea35SCyrill Gorcunov 			} else {
10934b11111aSMarcin Slusarz 				li->extLength = lip1->extLength +
10944b11111aSMarcin Slusarz 					(((li->extLength &
10954b11111aSMarcin Slusarz 						UDF_EXTENT_LENGTH_MASK) +
10964b11111aSMarcin Slusarz 					 blocksize - 1) & ~(blocksize - 1));
10971da177e4SLinus Torvalds 				if (*endnum > (i + 2))
10981da177e4SLinus Torvalds 					memmove(&laarr[i + 1], &laarr[i + 2],
10995ca4e4beSPekka Enberg 						sizeof(struct long_ad) *
11004b11111aSMarcin Slusarz 						(*endnum - (i + 2)));
11011da177e4SLinus Torvalds 				i--;
11021da177e4SLinus Torvalds 				(*endnum)--;
11031da177e4SLinus Torvalds 			}
11044b11111aSMarcin Slusarz 		} else if (((li->extLength >> 30) ==
11054b11111aSMarcin Slusarz 				(EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
11064b11111aSMarcin Slusarz 			   ((lip1->extLength >> 30) ==
11074b11111aSMarcin Slusarz 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
110897e961fdSPekka Enberg 			udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
11094b11111aSMarcin Slusarz 					((li->extLength &
11104b11111aSMarcin Slusarz 					  UDF_EXTENT_LENGTH_MASK) +
11114b11111aSMarcin Slusarz 					 blocksize - 1) >> blocksize_bits);
11124b11111aSMarcin Slusarz 			li->extLocation.logicalBlockNum = 0;
11134b11111aSMarcin Slusarz 			li->extLocation.partitionReferenceNum = 0;
11144b11111aSMarcin Slusarz 
11154b11111aSMarcin Slusarz 			if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
11164b11111aSMarcin Slusarz 			     (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
11174b11111aSMarcin Slusarz 			     blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
11184b11111aSMarcin Slusarz 				lip1->extLength = (lip1->extLength -
11194b11111aSMarcin Slusarz 						   (li->extLength &
11204b11111aSMarcin Slusarz 						   UDF_EXTENT_LENGTH_MASK) +
11214b11111aSMarcin Slusarz 						   UDF_EXTENT_LENGTH_MASK) &
11224b11111aSMarcin Slusarz 						   ~(blocksize - 1);
11234b11111aSMarcin Slusarz 				li->extLength = (li->extLength &
11244b11111aSMarcin Slusarz 						 UDF_EXTENT_FLAG_MASK) +
11254b11111aSMarcin Slusarz 						(UDF_EXTENT_LENGTH_MASK + 1) -
11264b11111aSMarcin Slusarz 						blocksize;
11274b11111aSMarcin Slusarz 			} else {
11284b11111aSMarcin Slusarz 				li->extLength = lip1->extLength +
11294b11111aSMarcin Slusarz 					(((li->extLength &
11304b11111aSMarcin Slusarz 						UDF_EXTENT_LENGTH_MASK) +
11314b11111aSMarcin Slusarz 					  blocksize - 1) & ~(blocksize - 1));
11324b11111aSMarcin Slusarz 				if (*endnum > (i + 2))
11334b11111aSMarcin Slusarz 					memmove(&laarr[i + 1], &laarr[i + 2],
11345ca4e4beSPekka Enberg 						sizeof(struct long_ad) *
11354b11111aSMarcin Slusarz 						(*endnum - (i + 2)));
11364b11111aSMarcin Slusarz 				i--;
11374b11111aSMarcin Slusarz 				(*endnum)--;
11384b11111aSMarcin Slusarz 			}
11394b11111aSMarcin Slusarz 		} else if ((li->extLength >> 30) ==
11404b11111aSMarcin Slusarz 					(EXT_NOT_RECORDED_ALLOCATED >> 30)) {
11414b11111aSMarcin Slusarz 			udf_free_blocks(inode->i_sb, inode,
114297e961fdSPekka Enberg 					&li->extLocation, 0,
11434b11111aSMarcin Slusarz 					((li->extLength &
11444b11111aSMarcin Slusarz 						UDF_EXTENT_LENGTH_MASK) +
11454b11111aSMarcin Slusarz 					 blocksize - 1) >> blocksize_bits);
11464b11111aSMarcin Slusarz 			li->extLocation.logicalBlockNum = 0;
11474b11111aSMarcin Slusarz 			li->extLocation.partitionReferenceNum = 0;
11484b11111aSMarcin Slusarz 			li->extLength = (li->extLength &
11494b11111aSMarcin Slusarz 						UDF_EXTENT_LENGTH_MASK) |
11501da177e4SLinus Torvalds 						EXT_NOT_RECORDED_NOT_ALLOCATED;
11511da177e4SLinus Torvalds 		}
11521da177e4SLinus Torvalds 	}
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds 
1155*3cc6f844SFabian Frederick static void udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
1156cb00ea35SCyrill Gorcunov 			       int startnum, int endnum,
1157ff116fc8SJan Kara 			       struct extent_position *epos)
11581da177e4SLinus Torvalds {
11591da177e4SLinus Torvalds 	int start = 0, i;
11605ca4e4beSPekka Enberg 	struct kernel_lb_addr tmploc;
11611da177e4SLinus Torvalds 	uint32_t tmplen;
11621da177e4SLinus Torvalds 
1163cb00ea35SCyrill Gorcunov 	if (startnum > endnum) {
11641da177e4SLinus Torvalds 		for (i = 0; i < (startnum - endnum); i++)
1165ff116fc8SJan Kara 			udf_delete_aext(inode, *epos, laarr[i].extLocation,
1166ff116fc8SJan Kara 					laarr[i].extLength);
1167cb00ea35SCyrill Gorcunov 	} else if (startnum < endnum) {
1168cb00ea35SCyrill Gorcunov 		for (i = 0; i < (endnum - startnum); i++) {
1169ff116fc8SJan Kara 			udf_insert_aext(inode, *epos, laarr[i].extLocation,
1170ff116fc8SJan Kara 					laarr[i].extLength);
1171ff116fc8SJan Kara 			udf_next_aext(inode, epos, &laarr[i].extLocation,
1172ff116fc8SJan Kara 				      &laarr[i].extLength, 1);
11731da177e4SLinus Torvalds 			start++;
11741da177e4SLinus Torvalds 		}
11751da177e4SLinus Torvalds 	}
11761da177e4SLinus Torvalds 
1177cb00ea35SCyrill Gorcunov 	for (i = start; i < endnum; i++) {
1178ff116fc8SJan Kara 		udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
117997e961fdSPekka Enberg 		udf_write_aext(inode, epos, &laarr[i].extLocation,
1180ff116fc8SJan Kara 			       laarr[i].extLength, 1);
11811da177e4SLinus Torvalds 	}
11821da177e4SLinus Torvalds }
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds struct buffer_head *udf_bread(struct inode *inode, int block,
11851da177e4SLinus Torvalds 			      int create, int *err)
11861da177e4SLinus Torvalds {
11871da177e4SLinus Torvalds 	struct buffer_head *bh = NULL;
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	bh = udf_getblk(inode, block, create, err);
11901da177e4SLinus Torvalds 	if (!bh)
11911da177e4SLinus Torvalds 		return NULL;
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 	if (buffer_uptodate(bh))
11941da177e4SLinus Torvalds 		return bh;
119528de7948SCyrill Gorcunov 
1196dfec8a14SMike Christie 	ll_rw_block(REQ_OP_READ, 0, 1, &bh);
119728de7948SCyrill Gorcunov 
11981da177e4SLinus Torvalds 	wait_on_buffer(bh);
11991da177e4SLinus Torvalds 	if (buffer_uptodate(bh))
12001da177e4SLinus Torvalds 		return bh;
120128de7948SCyrill Gorcunov 
12021da177e4SLinus Torvalds 	brelse(bh);
12031da177e4SLinus Torvalds 	*err = -EIO;
12041da177e4SLinus Torvalds 	return NULL;
12051da177e4SLinus Torvalds }
12061da177e4SLinus Torvalds 
12077e49b6f2SJan Kara int udf_setsize(struct inode *inode, loff_t newsize)
12081da177e4SLinus Torvalds {
12091da177e4SLinus Torvalds 	int err;
121048d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo;
12117e49b6f2SJan Kara 	int bsize = 1 << inode->i_blkbits;
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
12141da177e4SLinus Torvalds 	      S_ISLNK(inode->i_mode)))
12157e49b6f2SJan Kara 		return -EINVAL;
12161da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
12177e49b6f2SJan Kara 		return -EPERM;
12181da177e4SLinus Torvalds 
121948d6d8ffSMarcin Slusarz 	iinfo = UDF_I(inode);
12207e49b6f2SJan Kara 	if (newsize > inode->i_size) {
12214d0fb621SAlessio Igor Bogani 		down_write(&iinfo->i_data_sem);
122248d6d8ffSMarcin Slusarz 		if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
12237e49b6f2SJan Kara 			if (bsize <
12247e49b6f2SJan Kara 			    (udf_file_entry_alloc_offset(inode) + newsize)) {
12257e49b6f2SJan Kara 				err = udf_expand_file_adinicb(inode);
1226d2eb8c35SJan Kara 				if (err)
12277e49b6f2SJan Kara 					return err;
1228d2eb8c35SJan Kara 				down_write(&iinfo->i_data_sem);
1229bb2b6d19SIan Abbott 			} else {
12307e49b6f2SJan Kara 				iinfo->i_lenAlloc = newsize;
1231bb2b6d19SIan Abbott 				goto set_size;
1232bb2b6d19SIan Abbott 			}
12331da177e4SLinus Torvalds 		}
12347e49b6f2SJan Kara 		err = udf_extend_file(inode, newsize);
12357e49b6f2SJan Kara 		if (err) {
12367e49b6f2SJan Kara 			up_write(&iinfo->i_data_sem);
12377e49b6f2SJan Kara 			return err;
12387e49b6f2SJan Kara 		}
1239bb2b6d19SIan Abbott set_size:
12407e49b6f2SJan Kara 		truncate_setsize(inode, newsize);
12414d0fb621SAlessio Igor Bogani 		up_write(&iinfo->i_data_sem);
1242cb00ea35SCyrill Gorcunov 	} else {
12437e49b6f2SJan Kara 		if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
12444d0fb621SAlessio Igor Bogani 			down_write(&iinfo->i_data_sem);
124599600051SNamjae Jeon 			udf_clear_extent_cache(inode);
12467e49b6f2SJan Kara 			memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
12477e49b6f2SJan Kara 			       0x00, bsize - newsize -
12487e49b6f2SJan Kara 			       udf_file_entry_alloc_offset(inode));
12497e49b6f2SJan Kara 			iinfo->i_lenAlloc = newsize;
12507e49b6f2SJan Kara 			truncate_setsize(inode, newsize);
12517e49b6f2SJan Kara 			up_write(&iinfo->i_data_sem);
12527e49b6f2SJan Kara 			goto update_time;
12537e49b6f2SJan Kara 		}
12547e49b6f2SJan Kara 		err = block_truncate_page(inode->i_mapping, newsize,
12557e49b6f2SJan Kara 					  udf_get_block);
12567e49b6f2SJan Kara 		if (err)
12577e49b6f2SJan Kara 			return err;
12587e49b6f2SJan Kara 		down_write(&iinfo->i_data_sem);
125999600051SNamjae Jeon 		udf_clear_extent_cache(inode);
12607e49b6f2SJan Kara 		truncate_setsize(inode, newsize);
12611da177e4SLinus Torvalds 		udf_truncate_extents(inode);
12624d0fb621SAlessio Igor Bogani 		up_write(&iinfo->i_data_sem);
12631da177e4SLinus Torvalds 	}
12647e49b6f2SJan Kara update_time:
1265c2050a45SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
12661da177e4SLinus Torvalds 	if (IS_SYNC(inode))
12671da177e4SLinus Torvalds 		udf_sync_inode(inode);
12681da177e4SLinus Torvalds 	else
12691da177e4SLinus Torvalds 		mark_inode_dirty(inode);
12707e49b6f2SJan Kara 	return 0;
12711da177e4SLinus Torvalds }
12721da177e4SLinus Torvalds 
1273c03aa9f6SJan Kara /*
1274c03aa9f6SJan Kara  * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1275c03aa9f6SJan Kara  * arbitrary - just that we hopefully don't limit any real use of rewritten
1276c03aa9f6SJan Kara  * inode on write-once media but avoid looping for too long on corrupted media.
1277c03aa9f6SJan Kara  */
1278c03aa9f6SJan Kara #define UDF_MAX_ICB_NESTING 1024
1279c03aa9f6SJan Kara 
12806174c2ebSJan Kara static int udf_read_inode(struct inode *inode, bool hidden_inode)
12811da177e4SLinus Torvalds {
12821da177e4SLinus Torvalds 	struct buffer_head *bh = NULL;
12831da177e4SLinus Torvalds 	struct fileEntry *fe;
1284bb7720a0SJan Kara 	struct extendedFileEntry *efe;
12851da177e4SLinus Torvalds 	uint16_t ident;
128648d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
1287bb7720a0SJan Kara 	struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
12886d3d5e86SJan Kara 	struct kernel_lb_addr *iloc = &iinfo->i_location;
1289bb7720a0SJan Kara 	unsigned int link_count;
1290c03aa9f6SJan Kara 	unsigned int indirections = 0;
129179144954SJan Kara 	int bs = inode->i_sb->s_blocksize;
12926d3d5e86SJan Kara 	int ret = -EIO;
12931da177e4SLinus Torvalds 
1294c03aa9f6SJan Kara reread:
12956d3d5e86SJan Kara 	if (iloc->logicalBlockNum >=
12966d3d5e86SJan Kara 	    sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
12976d3d5e86SJan Kara 		udf_debug("block=%d, partition=%d out of range\n",
12986d3d5e86SJan Kara 			  iloc->logicalBlockNum, iloc->partitionReferenceNum);
12996d3d5e86SJan Kara 		return -EIO;
13006d3d5e86SJan Kara 	}
13016d3d5e86SJan Kara 
13021da177e4SLinus Torvalds 	/*
13031da177e4SLinus Torvalds 	 * Set defaults, but the inode is still incomplete!
13041da177e4SLinus Torvalds 	 * Note: get_new_inode() sets the following on a new inode:
13051da177e4SLinus Torvalds 	 *      i_sb = sb
13061da177e4SLinus Torvalds 	 *      i_no = ino
13071da177e4SLinus Torvalds 	 *      i_flags = sb->s_flags
13081da177e4SLinus Torvalds 	 *      i_state = 0
13091da177e4SLinus Torvalds 	 * clean_inode(): zero fills and sets
13101da177e4SLinus Torvalds 	 *      i_count = 1
13111da177e4SLinus Torvalds 	 *      i_nlink = 1
13121da177e4SLinus Torvalds 	 *      i_op = NULL;
13131da177e4SLinus Torvalds 	 */
13146d3d5e86SJan Kara 	bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
1315cb00ea35SCyrill Gorcunov 	if (!bh) {
131678ace70cSJoe Perches 		udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
13176d3d5e86SJan Kara 		return -EIO;
13181da177e4SLinus Torvalds 	}
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 	if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1321cb00ea35SCyrill Gorcunov 	    ident != TAG_IDENT_USE) {
132278ace70cSJoe Perches 		udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
132378ace70cSJoe Perches 			inode->i_ino, ident);
13246d3d5e86SJan Kara 		goto out;
13251da177e4SLinus Torvalds 	}
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 	fe = (struct fileEntry *)bh->b_data;
1328bb7720a0SJan Kara 	efe = (struct extendedFileEntry *)bh->b_data;
13291da177e4SLinus Torvalds 
13305e0f0017SMarcin Slusarz 	if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
13311ab92785Smarcin.slusarz@gmail.com 		struct buffer_head *ibh;
13321da177e4SLinus Torvalds 
13336d3d5e86SJan Kara 		ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
13341ab92785Smarcin.slusarz@gmail.com 		if (ident == TAG_IDENT_IE && ibh) {
13355ca4e4beSPekka Enberg 			struct kernel_lb_addr loc;
13361ab92785Smarcin.slusarz@gmail.com 			struct indirectEntry *ie;
13371da177e4SLinus Torvalds 
13381ab92785Smarcin.slusarz@gmail.com 			ie = (struct indirectEntry *)ibh->b_data;
13391da177e4SLinus Torvalds 			loc = lelb_to_cpu(ie->indirectICB.extLocation);
13401da177e4SLinus Torvalds 
1341c03aa9f6SJan Kara 			if (ie->indirectICB.extLength) {
13423bf25cb4SJan Kara 				brelse(ibh);
1343c03aa9f6SJan Kara 				memcpy(&iinfo->i_location, &loc,
1344c03aa9f6SJan Kara 				       sizeof(struct kernel_lb_addr));
1345c03aa9f6SJan Kara 				if (++indirections > UDF_MAX_ICB_NESTING) {
1346c03aa9f6SJan Kara 					udf_err(inode->i_sb,
1347c03aa9f6SJan Kara 						"too many ICBs in ICB hierarchy"
1348c03aa9f6SJan Kara 						" (max %d supported)\n",
1349c03aa9f6SJan Kara 						UDF_MAX_ICB_NESTING);
13506d3d5e86SJan Kara 					goto out;
13511ab92785Smarcin.slusarz@gmail.com 				}
13526d3d5e86SJan Kara 				brelse(bh);
1353c03aa9f6SJan Kara 				goto reread;
13541ab92785Smarcin.slusarz@gmail.com 			}
13551ab92785Smarcin.slusarz@gmail.com 		}
13563bf25cb4SJan Kara 		brelse(ibh);
13575e0f0017SMarcin Slusarz 	} else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
135878ace70cSJoe Perches 		udf_err(inode->i_sb, "unsupported strategy type: %d\n",
13591da177e4SLinus Torvalds 			le16_to_cpu(fe->icbTag.strategyType));
13606d3d5e86SJan Kara 		goto out;
13611da177e4SLinus Torvalds 	}
13625e0f0017SMarcin Slusarz 	if (fe->icbTag.strategyType == cpu_to_le16(4))
136348d6d8ffSMarcin Slusarz 		iinfo->i_strat4096 = 0;
13645e0f0017SMarcin Slusarz 	else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
136548d6d8ffSMarcin Slusarz 		iinfo->i_strat4096 = 1;
13661da177e4SLinus Torvalds 
136748d6d8ffSMarcin Slusarz 	iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
13684b11111aSMarcin Slusarz 							ICBTAG_FLAG_AD_MASK;
136948d6d8ffSMarcin Slusarz 	iinfo->i_unique = 0;
137048d6d8ffSMarcin Slusarz 	iinfo->i_lenEAttr = 0;
137148d6d8ffSMarcin Slusarz 	iinfo->i_lenExtents = 0;
137248d6d8ffSMarcin Slusarz 	iinfo->i_lenAlloc = 0;
137348d6d8ffSMarcin Slusarz 	iinfo->i_next_alloc_block = 0;
137448d6d8ffSMarcin Slusarz 	iinfo->i_next_alloc_goal = 0;
13755e0f0017SMarcin Slusarz 	if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
137648d6d8ffSMarcin Slusarz 		iinfo->i_efe = 1;
137748d6d8ffSMarcin Slusarz 		iinfo->i_use = 0;
137879144954SJan Kara 		ret = udf_alloc_i_data(inode, bs -
13796d3d5e86SJan Kara 					sizeof(struct extendedFileEntry));
13806d3d5e86SJan Kara 		if (ret)
13816d3d5e86SJan Kara 			goto out;
138248d6d8ffSMarcin Slusarz 		memcpy(iinfo->i_ext.i_data,
13834b11111aSMarcin Slusarz 		       bh->b_data + sizeof(struct extendedFileEntry),
138479144954SJan Kara 		       bs - sizeof(struct extendedFileEntry));
13855e0f0017SMarcin Slusarz 	} else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
138648d6d8ffSMarcin Slusarz 		iinfo->i_efe = 0;
138748d6d8ffSMarcin Slusarz 		iinfo->i_use = 0;
138879144954SJan Kara 		ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
13896d3d5e86SJan Kara 		if (ret)
13906d3d5e86SJan Kara 			goto out;
139148d6d8ffSMarcin Slusarz 		memcpy(iinfo->i_ext.i_data,
1392c0b34438SMarcin Slusarz 		       bh->b_data + sizeof(struct fileEntry),
139379144954SJan Kara 		       bs - sizeof(struct fileEntry));
13945e0f0017SMarcin Slusarz 	} else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
139548d6d8ffSMarcin Slusarz 		iinfo->i_efe = 0;
139648d6d8ffSMarcin Slusarz 		iinfo->i_use = 1;
139748d6d8ffSMarcin Slusarz 		iinfo->i_lenAlloc = le32_to_cpu(
13984b11111aSMarcin Slusarz 				((struct unallocSpaceEntry *)bh->b_data)->
13994b11111aSMarcin Slusarz 				 lengthAllocDescs);
140079144954SJan Kara 		ret = udf_alloc_i_data(inode, bs -
14016d3d5e86SJan Kara 					sizeof(struct unallocSpaceEntry));
14026d3d5e86SJan Kara 		if (ret)
14036d3d5e86SJan Kara 			goto out;
140448d6d8ffSMarcin Slusarz 		memcpy(iinfo->i_ext.i_data,
14054b11111aSMarcin Slusarz 		       bh->b_data + sizeof(struct unallocSpaceEntry),
140679144954SJan Kara 		       bs - sizeof(struct unallocSpaceEntry));
14076d3d5e86SJan Kara 		return 0;
14081da177e4SLinus Torvalds 	}
14091da177e4SLinus Torvalds 
14106d3d5e86SJan Kara 	ret = -EIO;
1411c03cad24SJan Kara 	read_lock(&sbi->s_cred_lock);
1412c2ba138aSEric W. Biederman 	i_uid_write(inode, le32_to_cpu(fe->uid));
1413c2ba138aSEric W. Biederman 	if (!uid_valid(inode->i_uid) ||
1414ca76d2d8SCyrill Gorcunov 	    UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1415ca76d2d8SCyrill Gorcunov 	    UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
14164d6660ebSPhillip Susi 		inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
14171da177e4SLinus Torvalds 
1418c2ba138aSEric W. Biederman 	i_gid_write(inode, le32_to_cpu(fe->gid));
1419c2ba138aSEric W. Biederman 	if (!gid_valid(inode->i_gid) ||
1420ca76d2d8SCyrill Gorcunov 	    UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1421ca76d2d8SCyrill Gorcunov 	    UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
14224d6660ebSPhillip Susi 		inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
14231da177e4SLinus Torvalds 
14247ac9bcd5SMarcin Slusarz 	if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
142587bc730cSMarcin Slusarz 			sbi->s_fmode != UDF_INVALID_MODE)
14267ac9bcd5SMarcin Slusarz 		inode->i_mode = sbi->s_fmode;
14277ac9bcd5SMarcin Slusarz 	else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
142887bc730cSMarcin Slusarz 			sbi->s_dmode != UDF_INVALID_MODE)
14297ac9bcd5SMarcin Slusarz 		inode->i_mode = sbi->s_dmode;
14307ac9bcd5SMarcin Slusarz 	else
14311da177e4SLinus Torvalds 		inode->i_mode = udf_convert_permissions(fe);
14327ac9bcd5SMarcin Slusarz 	inode->i_mode &= ~sbi->s_umask;
1433c03cad24SJan Kara 	read_unlock(&sbi->s_cred_lock);
1434c03cad24SJan Kara 
1435bfe86848SMiklos Szeredi 	link_count = le16_to_cpu(fe->fileLinkCount);
14364071b913SJan Kara 	if (!link_count) {
14376174c2ebSJan Kara 		if (!hidden_inode) {
14384071b913SJan Kara 			ret = -ESTALE;
14394071b913SJan Kara 			goto out;
14404071b913SJan Kara 		}
14416174c2ebSJan Kara 		link_count = 1;
14426174c2ebSJan Kara 	}
1443bfe86848SMiklos Szeredi 	set_nlink(inode, link_count);
1444c03cad24SJan Kara 
1445c03cad24SJan Kara 	inode->i_size = le64_to_cpu(fe->informationLength);
1446c03cad24SJan Kara 	iinfo->i_lenExtents = inode->i_size;
14471da177e4SLinus Torvalds 
144848d6d8ffSMarcin Slusarz 	if (iinfo->i_efe == 0) {
14491da177e4SLinus Torvalds 		inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
14501da177e4SLinus Torvalds 			(inode->i_sb->s_blocksize_bits - 9);
14511da177e4SLinus Torvalds 
145256774805SMarcin Slusarz 		if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
1453cbf5676aSmarcin.slusarz@gmail.com 			inode->i_atime = sbi->s_record_time;
1454cbf5676aSmarcin.slusarz@gmail.com 
145556774805SMarcin Slusarz 		if (!udf_disk_stamp_to_time(&inode->i_mtime,
145656774805SMarcin Slusarz 					    fe->modificationTime))
1457cbf5676aSmarcin.slusarz@gmail.com 			inode->i_mtime = sbi->s_record_time;
1458cbf5676aSmarcin.slusarz@gmail.com 
145956774805SMarcin Slusarz 		if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
1460cbf5676aSmarcin.slusarz@gmail.com 			inode->i_ctime = sbi->s_record_time;
14611da177e4SLinus Torvalds 
146248d6d8ffSMarcin Slusarz 		iinfo->i_unique = le64_to_cpu(fe->uniqueID);
146348d6d8ffSMarcin Slusarz 		iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
146448d6d8ffSMarcin Slusarz 		iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1465d5e2cf07SSteve Nickel 		iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1466cb00ea35SCyrill Gorcunov 	} else {
14671da177e4SLinus Torvalds 		inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
14681da177e4SLinus Torvalds 		    (inode->i_sb->s_blocksize_bits - 9);
14691da177e4SLinus Torvalds 
147056774805SMarcin Slusarz 		if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
1471cbf5676aSmarcin.slusarz@gmail.com 			inode->i_atime = sbi->s_record_time;
1472cbf5676aSmarcin.slusarz@gmail.com 
147356774805SMarcin Slusarz 		if (!udf_disk_stamp_to_time(&inode->i_mtime,
147456774805SMarcin Slusarz 					    efe->modificationTime))
1475cbf5676aSmarcin.slusarz@gmail.com 			inode->i_mtime = sbi->s_record_time;
1476cbf5676aSmarcin.slusarz@gmail.com 
147756774805SMarcin Slusarz 		if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
1478cbf5676aSmarcin.slusarz@gmail.com 			iinfo->i_crtime = sbi->s_record_time;
1479cbf5676aSmarcin.slusarz@gmail.com 
148056774805SMarcin Slusarz 		if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
1481cbf5676aSmarcin.slusarz@gmail.com 			inode->i_ctime = sbi->s_record_time;
14821da177e4SLinus Torvalds 
148348d6d8ffSMarcin Slusarz 		iinfo->i_unique = le64_to_cpu(efe->uniqueID);
148448d6d8ffSMarcin Slusarz 		iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
148548d6d8ffSMarcin Slusarz 		iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1486d5e2cf07SSteve Nickel 		iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
14871da177e4SLinus Torvalds 	}
1488470cca56SJan Kara 	inode->i_generation = iinfo->i_unique;
14891da177e4SLinus Torvalds 
149023b133bdSJan Kara 	/*
149123b133bdSJan Kara 	 * Sanity check length of allocation descriptors and extended attrs to
149223b133bdSJan Kara 	 * avoid integer overflows
149323b133bdSJan Kara 	 */
149423b133bdSJan Kara 	if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
149523b133bdSJan Kara 		goto out;
149623b133bdSJan Kara 	/* Now do exact checks */
149723b133bdSJan Kara 	if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
149823b133bdSJan Kara 		goto out;
1499e159332bSJan Kara 	/* Sanity checks for files in ICB so that we don't get confused later */
1500e159332bSJan Kara 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1501e159332bSJan Kara 		/*
1502e159332bSJan Kara 		 * For file in ICB data is stored in allocation descriptor
1503e159332bSJan Kara 		 * so sizes should match
1504e159332bSJan Kara 		 */
1505e159332bSJan Kara 		if (iinfo->i_lenAlloc != inode->i_size)
1506e159332bSJan Kara 			goto out;
1507e159332bSJan Kara 		/* File in ICB has to fit in there... */
150879144954SJan Kara 		if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
1509e159332bSJan Kara 			goto out;
1510e159332bSJan Kara 	}
1511e159332bSJan Kara 
1512cb00ea35SCyrill Gorcunov 	switch (fe->icbTag.fileType) {
15131da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_DIRECTORY:
15141da177e4SLinus Torvalds 		inode->i_op = &udf_dir_inode_operations;
15151da177e4SLinus Torvalds 		inode->i_fop = &udf_dir_operations;
15161da177e4SLinus Torvalds 		inode->i_mode |= S_IFDIR;
1517d8c76e6fSDave Hansen 		inc_nlink(inode);
15181da177e4SLinus Torvalds 		break;
15191da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_REALTIME:
15201da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_REGULAR:
15211da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_UNDEF:
1522742e1795SJan Kara 	case ICBTAG_FILE_TYPE_VAT20:
152348d6d8ffSMarcin Slusarz 		if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
15241da177e4SLinus Torvalds 			inode->i_data.a_ops = &udf_adinicb_aops;
15251da177e4SLinus Torvalds 		else
15261da177e4SLinus Torvalds 			inode->i_data.a_ops = &udf_aops;
15271da177e4SLinus Torvalds 		inode->i_op = &udf_file_inode_operations;
15281da177e4SLinus Torvalds 		inode->i_fop = &udf_file_operations;
15291da177e4SLinus Torvalds 		inode->i_mode |= S_IFREG;
15301da177e4SLinus Torvalds 		break;
15311da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_BLOCK:
15321da177e4SLinus Torvalds 		inode->i_mode |= S_IFBLK;
15331da177e4SLinus Torvalds 		break;
15341da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_CHAR:
15351da177e4SLinus Torvalds 		inode->i_mode |= S_IFCHR;
15361da177e4SLinus Torvalds 		break;
15371da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_FIFO:
15381da177e4SLinus Torvalds 		init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
15391da177e4SLinus Torvalds 		break;
15401da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_SOCKET:
15411da177e4SLinus Torvalds 		init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
15421da177e4SLinus Torvalds 		break;
15431da177e4SLinus Torvalds 	case ICBTAG_FILE_TYPE_SYMLINK:
15441da177e4SLinus Torvalds 		inode->i_data.a_ops = &udf_symlink_aops;
1545ad4d0532SJan Kara 		inode->i_op = &udf_symlink_inode_operations;
154621fc61c7SAl Viro 		inode_nohighmem(inode);
15471da177e4SLinus Torvalds 		inode->i_mode = S_IFLNK | S_IRWXUGO;
15481da177e4SLinus Torvalds 		break;
1549bfb257a5SJan Kara 	case ICBTAG_FILE_TYPE_MAIN:
1550bfb257a5SJan Kara 		udf_debug("METADATA FILE-----\n");
1551bfb257a5SJan Kara 		break;
1552bfb257a5SJan Kara 	case ICBTAG_FILE_TYPE_MIRROR:
1553bfb257a5SJan Kara 		udf_debug("METADATA MIRROR FILE-----\n");
1554bfb257a5SJan Kara 		break;
1555bfb257a5SJan Kara 	case ICBTAG_FILE_TYPE_BITMAP:
1556bfb257a5SJan Kara 		udf_debug("METADATA BITMAP FILE-----\n");
1557bfb257a5SJan Kara 		break;
15581da177e4SLinus Torvalds 	default:
155978ace70cSJoe Perches 		udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
156078ace70cSJoe Perches 			inode->i_ino, fe->icbTag.fileType);
15616d3d5e86SJan Kara 		goto out;
15621da177e4SLinus Torvalds 	}
1563cb00ea35SCyrill Gorcunov 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
15644b11111aSMarcin Slusarz 		struct deviceSpec *dsea =
15654b11111aSMarcin Slusarz 			(struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1566cb00ea35SCyrill Gorcunov 		if (dsea) {
1567cb00ea35SCyrill Gorcunov 			init_special_inode(inode, inode->i_mode,
156828de7948SCyrill Gorcunov 				MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
156928de7948SCyrill Gorcunov 				      le32_to_cpu(dsea->minorDeviceIdent)));
15701da177e4SLinus Torvalds 			/* Developer ID ??? */
15714b11111aSMarcin Slusarz 		} else
15726d3d5e86SJan Kara 			goto out;
15731da177e4SLinus Torvalds 	}
15746d3d5e86SJan Kara 	ret = 0;
15756d3d5e86SJan Kara out:
1576bb7720a0SJan Kara 	brelse(bh);
15776d3d5e86SJan Kara 	return ret;
15781da177e4SLinus Torvalds }
15791da177e4SLinus Torvalds 
1580647bd61aSCyrill Gorcunov static int udf_alloc_i_data(struct inode *inode, size_t size)
1581647bd61aSCyrill Gorcunov {
158248d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
158348d6d8ffSMarcin Slusarz 	iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1584647bd61aSCyrill Gorcunov 
158548d6d8ffSMarcin Slusarz 	if (!iinfo->i_ext.i_data) {
158678ace70cSJoe Perches 		udf_err(inode->i_sb, "(ino %ld) no free memory\n",
158778ace70cSJoe Perches 			inode->i_ino);
1588647bd61aSCyrill Gorcunov 		return -ENOMEM;
1589647bd61aSCyrill Gorcunov 	}
1590647bd61aSCyrill Gorcunov 
1591647bd61aSCyrill Gorcunov 	return 0;
1592647bd61aSCyrill Gorcunov }
1593647bd61aSCyrill Gorcunov 
1594faa17292SAl Viro static umode_t udf_convert_permissions(struct fileEntry *fe)
15951da177e4SLinus Torvalds {
1596faa17292SAl Viro 	umode_t mode;
15971da177e4SLinus Torvalds 	uint32_t permissions;
15981da177e4SLinus Torvalds 	uint32_t flags;
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 	permissions = le32_to_cpu(fe->permissions);
16011da177e4SLinus Torvalds 	flags = le16_to_cpu(fe->icbTag.flags);
16021da177e4SLinus Torvalds 
16031da177e4SLinus Torvalds 	mode =	((permissions) & S_IRWXO) |
16041da177e4SLinus Torvalds 		((permissions >> 2) & S_IRWXG) |
16051da177e4SLinus Torvalds 		((permissions >> 4) & S_IRWXU) |
16061da177e4SLinus Torvalds 		((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
16071da177e4SLinus Torvalds 		((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
16081da177e4SLinus Torvalds 		((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 	return mode;
16111da177e4SLinus Torvalds }
16121da177e4SLinus Torvalds 
1613a9185b41SChristoph Hellwig int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
16141da177e4SLinus Torvalds {
161549521de1SJan Kara 	return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
16161da177e4SLinus Torvalds }
16171da177e4SLinus Torvalds 
161849521de1SJan Kara static int udf_sync_inode(struct inode *inode)
16191da177e4SLinus Torvalds {
16201da177e4SLinus Torvalds 	return udf_update_inode(inode, 1);
16211da177e4SLinus Torvalds }
16221da177e4SLinus Torvalds 
1623cb00ea35SCyrill Gorcunov static int udf_update_inode(struct inode *inode, int do_sync)
16241da177e4SLinus Torvalds {
16251da177e4SLinus Torvalds 	struct buffer_head *bh = NULL;
16261da177e4SLinus Torvalds 	struct fileEntry *fe;
16271da177e4SLinus Torvalds 	struct extendedFileEntry *efe;
1628b2527bfaSSteve Nickel 	uint64_t lb_recorded;
16291da177e4SLinus Torvalds 	uint32_t udfperms;
16301da177e4SLinus Torvalds 	uint16_t icbflags;
16311da177e4SLinus Torvalds 	uint16_t crclen;
16321da177e4SLinus Torvalds 	int err = 0;
16336c79e987SMarcin Slusarz 	struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
16344b11111aSMarcin Slusarz 	unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
163548d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
16361da177e4SLinus Torvalds 
16375833ded9SJan Kara 	bh = udf_tgetblk(inode->i_sb,
16385833ded9SJan Kara 			udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1639cb00ea35SCyrill Gorcunov 	if (!bh) {
1640aae917cdSJan Kara 		udf_debug("getblk failure\n");
16410fd2ba36SChangwoo Min 		return -EIO;
16421da177e4SLinus Torvalds 	}
16431da177e4SLinus Torvalds 
1644aae917cdSJan Kara 	lock_buffer(bh);
1645aae917cdSJan Kara 	memset(bh->b_data, 0, inode->i_sb->s_blocksize);
16461da177e4SLinus Torvalds 	fe = (struct fileEntry *)bh->b_data;
16471da177e4SLinus Torvalds 	efe = (struct extendedFileEntry *)bh->b_data;
16481da177e4SLinus Torvalds 
1649aae917cdSJan Kara 	if (iinfo->i_use) {
16501da177e4SLinus Torvalds 		struct unallocSpaceEntry *use =
16511da177e4SLinus Torvalds 			(struct unallocSpaceEntry *)bh->b_data;
16521da177e4SLinus Torvalds 
165348d6d8ffSMarcin Slusarz 		use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
16544b11111aSMarcin Slusarz 		memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
165548d6d8ffSMarcin Slusarz 		       iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
16564b11111aSMarcin Slusarz 					sizeof(struct unallocSpaceEntry));
1657aae917cdSJan Kara 		use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
165870f19f58SSteven J. Magnani 		crclen = sizeof(struct unallocSpaceEntry);
16591da177e4SLinus Torvalds 
166070f19f58SSteven J. Magnani 		goto finish;
16611da177e4SLinus Torvalds 	}
16621da177e4SLinus Torvalds 
16634d6660ebSPhillip Susi 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
16644d6660ebSPhillip Susi 		fe->uid = cpu_to_le32(-1);
1665cb00ea35SCyrill Gorcunov 	else
1666c2ba138aSEric W. Biederman 		fe->uid = cpu_to_le32(i_uid_read(inode));
16671da177e4SLinus Torvalds 
16684d6660ebSPhillip Susi 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
16694d6660ebSPhillip Susi 		fe->gid = cpu_to_le32(-1);
1670cb00ea35SCyrill Gorcunov 	else
1671c2ba138aSEric W. Biederman 		fe->gid = cpu_to_le32(i_gid_read(inode));
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 	udfperms = ((inode->i_mode & S_IRWXO)) |
167428de7948SCyrill Gorcunov 		   ((inode->i_mode & S_IRWXG) << 2) |
167528de7948SCyrill Gorcunov 		   ((inode->i_mode & S_IRWXU) << 4);
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds 	udfperms |= (le32_to_cpu(fe->permissions) &
16781da177e4SLinus Torvalds 		    (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
16791da177e4SLinus Torvalds 		     FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
16801da177e4SLinus Torvalds 		     FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
16811da177e4SLinus Torvalds 	fe->permissions = cpu_to_le32(udfperms);
16821da177e4SLinus Torvalds 
16838a70ee33SJan Kara 	if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
16841da177e4SLinus Torvalds 		fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
16851da177e4SLinus Torvalds 	else
16861da177e4SLinus Torvalds 		fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds 	fe->informationLength = cpu_to_le64(inode->i_size);
16891da177e4SLinus Torvalds 
1690cb00ea35SCyrill Gorcunov 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
16915ca4e4beSPekka Enberg 		struct regid *eid;
169228de7948SCyrill Gorcunov 		struct deviceSpec *dsea =
169328de7948SCyrill Gorcunov 			(struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1694cb00ea35SCyrill Gorcunov 		if (!dsea) {
16951da177e4SLinus Torvalds 			dsea = (struct deviceSpec *)
16961da177e4SLinus Torvalds 				udf_add_extendedattr(inode,
16971da177e4SLinus Torvalds 						     sizeof(struct deviceSpec) +
16985ca4e4beSPekka Enberg 						     sizeof(struct regid), 12, 0x3);
16991da177e4SLinus Torvalds 			dsea->attrType = cpu_to_le32(12);
17001da177e4SLinus Torvalds 			dsea->attrSubtype = 1;
17014b11111aSMarcin Slusarz 			dsea->attrLength = cpu_to_le32(
17024b11111aSMarcin Slusarz 						sizeof(struct deviceSpec) +
17035ca4e4beSPekka Enberg 						sizeof(struct regid));
17045ca4e4beSPekka Enberg 			dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
17051da177e4SLinus Torvalds 		}
17065ca4e4beSPekka Enberg 		eid = (struct regid *)dsea->impUse;
17075ca4e4beSPekka Enberg 		memset(eid, 0, sizeof(struct regid));
17081da177e4SLinus Torvalds 		strcpy(eid->ident, UDF_ID_DEVELOPER);
17091da177e4SLinus Torvalds 		eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
17101da177e4SLinus Torvalds 		eid->identSuffix[1] = UDF_OS_ID_LINUX;
17111da177e4SLinus Torvalds 		dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
17121da177e4SLinus Torvalds 		dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
17131da177e4SLinus Torvalds 	}
17141da177e4SLinus Torvalds 
1715b2527bfaSSteve Nickel 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1716b2527bfaSSteve Nickel 		lb_recorded = 0; /* No extents => no blocks! */
1717b2527bfaSSteve Nickel 	else
1718b2527bfaSSteve Nickel 		lb_recorded =
1719b2527bfaSSteve Nickel 			(inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1720b2527bfaSSteve Nickel 			(blocksize_bits - 9);
1721b2527bfaSSteve Nickel 
172248d6d8ffSMarcin Slusarz 	if (iinfo->i_efe == 0) {
1723c0b34438SMarcin Slusarz 		memcpy(bh->b_data + sizeof(struct fileEntry),
172448d6d8ffSMarcin Slusarz 		       iinfo->i_ext.i_data,
1725cb00ea35SCyrill Gorcunov 		       inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1726b2527bfaSSteve Nickel 		fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
17271da177e4SLinus Torvalds 
172856774805SMarcin Slusarz 		udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
172956774805SMarcin Slusarz 		udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
173056774805SMarcin Slusarz 		udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
17315ca4e4beSPekka Enberg 		memset(&(fe->impIdent), 0, sizeof(struct regid));
17321da177e4SLinus Torvalds 		strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
17331da177e4SLinus Torvalds 		fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
17341da177e4SLinus Torvalds 		fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
173548d6d8ffSMarcin Slusarz 		fe->uniqueID = cpu_to_le64(iinfo->i_unique);
173648d6d8ffSMarcin Slusarz 		fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
173748d6d8ffSMarcin Slusarz 		fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1738d5e2cf07SSteve Nickel 		fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
17391da177e4SLinus Torvalds 		fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
17401da177e4SLinus Torvalds 		crclen = sizeof(struct fileEntry);
1741cb00ea35SCyrill Gorcunov 	} else {
17424b11111aSMarcin Slusarz 		memcpy(bh->b_data + sizeof(struct extendedFileEntry),
174348d6d8ffSMarcin Slusarz 		       iinfo->i_ext.i_data,
17444b11111aSMarcin Slusarz 		       inode->i_sb->s_blocksize -
17454b11111aSMarcin Slusarz 					sizeof(struct extendedFileEntry));
17461da177e4SLinus Torvalds 		efe->objectSize = cpu_to_le64(inode->i_size);
1747b2527bfaSSteve Nickel 		efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
17481da177e4SLinus Torvalds 
174948d6d8ffSMarcin Slusarz 		if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
175048d6d8ffSMarcin Slusarz 		    (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
175148d6d8ffSMarcin Slusarz 		     iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
175248d6d8ffSMarcin Slusarz 			iinfo->i_crtime = inode->i_atime;
17534b11111aSMarcin Slusarz 
175448d6d8ffSMarcin Slusarz 		if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
175548d6d8ffSMarcin Slusarz 		    (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
175648d6d8ffSMarcin Slusarz 		     iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
175748d6d8ffSMarcin Slusarz 			iinfo->i_crtime = inode->i_mtime;
17584b11111aSMarcin Slusarz 
175948d6d8ffSMarcin Slusarz 		if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
176048d6d8ffSMarcin Slusarz 		    (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
176148d6d8ffSMarcin Slusarz 		     iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
176248d6d8ffSMarcin Slusarz 			iinfo->i_crtime = inode->i_ctime;
17631da177e4SLinus Torvalds 
176456774805SMarcin Slusarz 		udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
176556774805SMarcin Slusarz 		udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
176656774805SMarcin Slusarz 		udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
176756774805SMarcin Slusarz 		udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
17681da177e4SLinus Torvalds 
17695ca4e4beSPekka Enberg 		memset(&(efe->impIdent), 0, sizeof(struct regid));
17701da177e4SLinus Torvalds 		strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
17711da177e4SLinus Torvalds 		efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
17721da177e4SLinus Torvalds 		efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
177348d6d8ffSMarcin Slusarz 		efe->uniqueID = cpu_to_le64(iinfo->i_unique);
177448d6d8ffSMarcin Slusarz 		efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
177548d6d8ffSMarcin Slusarz 		efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1776d5e2cf07SSteve Nickel 		efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
17771da177e4SLinus Torvalds 		efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
17781da177e4SLinus Torvalds 		crclen = sizeof(struct extendedFileEntry);
17791da177e4SLinus Torvalds 	}
178070f19f58SSteven J. Magnani 
178170f19f58SSteven J. Magnani finish:
178248d6d8ffSMarcin Slusarz 	if (iinfo->i_strat4096) {
17831da177e4SLinus Torvalds 		fe->icbTag.strategyType = cpu_to_le16(4096);
17841da177e4SLinus Torvalds 		fe->icbTag.strategyParameter = cpu_to_le16(1);
17851da177e4SLinus Torvalds 		fe->icbTag.numEntries = cpu_to_le16(2);
1786cb00ea35SCyrill Gorcunov 	} else {
17871da177e4SLinus Torvalds 		fe->icbTag.strategyType = cpu_to_le16(4);
17881da177e4SLinus Torvalds 		fe->icbTag.numEntries = cpu_to_le16(1);
17891da177e4SLinus Torvalds 	}
17901da177e4SLinus Torvalds 
179170f19f58SSteven J. Magnani 	if (iinfo->i_use)
179270f19f58SSteven J. Magnani 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
179370f19f58SSteven J. Magnani 	else if (S_ISDIR(inode->i_mode))
17941da177e4SLinus Torvalds 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
17951da177e4SLinus Torvalds 	else if (S_ISREG(inode->i_mode))
17961da177e4SLinus Torvalds 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
17971da177e4SLinus Torvalds 	else if (S_ISLNK(inode->i_mode))
17981da177e4SLinus Torvalds 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
17991da177e4SLinus Torvalds 	else if (S_ISBLK(inode->i_mode))
18001da177e4SLinus Torvalds 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
18011da177e4SLinus Torvalds 	else if (S_ISCHR(inode->i_mode))
18021da177e4SLinus Torvalds 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
18031da177e4SLinus Torvalds 	else if (S_ISFIFO(inode->i_mode))
18041da177e4SLinus Torvalds 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
18051da177e4SLinus Torvalds 	else if (S_ISSOCK(inode->i_mode))
18061da177e4SLinus Torvalds 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
18071da177e4SLinus Torvalds 
180848d6d8ffSMarcin Slusarz 	icbflags =	iinfo->i_alloc_type |
18091da177e4SLinus Torvalds 			((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
18101da177e4SLinus Torvalds 			((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
18111da177e4SLinus Torvalds 			((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
18121da177e4SLinus Torvalds 			(le16_to_cpu(fe->icbTag.flags) &
18131da177e4SLinus Torvalds 				~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
18141da177e4SLinus Torvalds 				ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds 	fe->icbTag.flags = cpu_to_le16(icbflags);
18176c79e987SMarcin Slusarz 	if (sbi->s_udfrev >= 0x0200)
18181da177e4SLinus Torvalds 		fe->descTag.descVersion = cpu_to_le16(3);
18191da177e4SLinus Torvalds 	else
18201da177e4SLinus Torvalds 		fe->descTag.descVersion = cpu_to_le16(2);
18216c79e987SMarcin Slusarz 	fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
18224b11111aSMarcin Slusarz 	fe->descTag.tagLocation = cpu_to_le32(
182348d6d8ffSMarcin Slusarz 					iinfo->i_location.logicalBlockNum);
1824aae917cdSJan Kara 	crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
18251da177e4SLinus Torvalds 	fe->descTag.descCRCLength = cpu_to_le16(crclen);
18265ca4e4beSPekka Enberg 	fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1827f845fcedSBob Copeland 						  crclen));
18283f2587bbSMarcin Slusarz 	fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
18291da177e4SLinus Torvalds 
18305833ded9SJan Kara 	set_buffer_uptodate(bh);
1831aae917cdSJan Kara 	unlock_buffer(bh);
1832aae917cdSJan Kara 
18331da177e4SLinus Torvalds 	/* write the data blocks */
18341da177e4SLinus Torvalds 	mark_buffer_dirty(bh);
1835cb00ea35SCyrill Gorcunov 	if (do_sync) {
18361da177e4SLinus Torvalds 		sync_dirty_buffer(bh);
1837aae917cdSJan Kara 		if (buffer_write_io_error(bh)) {
183878ace70cSJoe Perches 			udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
18394b11111aSMarcin Slusarz 				 inode->i_ino);
18401da177e4SLinus Torvalds 			err = -EIO;
18411da177e4SLinus Torvalds 		}
18421da177e4SLinus Torvalds 	}
18433bf25cb4SJan Kara 	brelse(bh);
184428de7948SCyrill Gorcunov 
18451da177e4SLinus Torvalds 	return err;
18461da177e4SLinus Torvalds }
18471da177e4SLinus Torvalds 
18486174c2ebSJan Kara struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
18496174c2ebSJan Kara 			 bool hidden_inode)
18501da177e4SLinus Torvalds {
18511da177e4SLinus Torvalds 	unsigned long block = udf_get_lb_pblock(sb, ino, 0);
18521da177e4SLinus Torvalds 	struct inode *inode = iget_locked(sb, block);
18536d3d5e86SJan Kara 	int err;
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds 	if (!inode)
18566d3d5e86SJan Kara 		return ERR_PTR(-ENOMEM);
18571da177e4SLinus Torvalds 
18586d3d5e86SJan Kara 	if (!(inode->i_state & I_NEW))
18591da177e4SLinus Torvalds 		return inode;
18601da177e4SLinus Torvalds 
18616d3d5e86SJan Kara 	memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
18626174c2ebSJan Kara 	err = udf_read_inode(inode, hidden_inode);
18636d3d5e86SJan Kara 	if (err < 0) {
18646d3d5e86SJan Kara 		iget_failed(inode);
18656d3d5e86SJan Kara 		return ERR_PTR(err);
18666d3d5e86SJan Kara 	}
18676d3d5e86SJan Kara 	unlock_new_inode(inode);
18686d3d5e86SJan Kara 
18696d3d5e86SJan Kara 	return inode;
18701da177e4SLinus Torvalds }
18711da177e4SLinus Torvalds 
1872fcea62baSJan Kara int udf_setup_indirect_aext(struct inode *inode, int block,
1873fcea62baSJan Kara 			    struct extent_position *epos)
1874fcea62baSJan Kara {
1875fcea62baSJan Kara 	struct super_block *sb = inode->i_sb;
1876fcea62baSJan Kara 	struct buffer_head *bh;
1877fcea62baSJan Kara 	struct allocExtDesc *aed;
1878fcea62baSJan Kara 	struct extent_position nepos;
1879fcea62baSJan Kara 	struct kernel_lb_addr neloc;
1880fcea62baSJan Kara 	int ver, adsize;
1881fcea62baSJan Kara 
1882fcea62baSJan Kara 	if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1883fcea62baSJan Kara 		adsize = sizeof(struct short_ad);
1884fcea62baSJan Kara 	else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1885fcea62baSJan Kara 		adsize = sizeof(struct long_ad);
18864f1b1519SArnd Bergmann 	else
18874f1b1519SArnd Bergmann 		return -EIO;
1888fcea62baSJan Kara 
1889fcea62baSJan Kara 	neloc.logicalBlockNum = block;
1890fcea62baSJan Kara 	neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1891fcea62baSJan Kara 
1892fcea62baSJan Kara 	bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1893fcea62baSJan Kara 	if (!bh)
1894fcea62baSJan Kara 		return -EIO;
1895fcea62baSJan Kara 	lock_buffer(bh);
1896fcea62baSJan Kara 	memset(bh->b_data, 0x00, sb->s_blocksize);
1897fcea62baSJan Kara 	set_buffer_uptodate(bh);
1898fcea62baSJan Kara 	unlock_buffer(bh);
1899fcea62baSJan Kara 	mark_buffer_dirty_inode(bh, inode);
1900fcea62baSJan Kara 
1901fcea62baSJan Kara 	aed = (struct allocExtDesc *)(bh->b_data);
1902fcea62baSJan Kara 	if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1903fcea62baSJan Kara 		aed->previousAllocExtLocation =
1904fcea62baSJan Kara 				cpu_to_le32(epos->block.logicalBlockNum);
1905fcea62baSJan Kara 	}
1906fcea62baSJan Kara 	aed->lengthAllocDescs = cpu_to_le32(0);
1907fcea62baSJan Kara 	if (UDF_SB(sb)->s_udfrev >= 0x0200)
1908fcea62baSJan Kara 		ver = 3;
1909fcea62baSJan Kara 	else
1910fcea62baSJan Kara 		ver = 2;
1911fcea62baSJan Kara 	udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1912fcea62baSJan Kara 		    sizeof(struct tag));
1913fcea62baSJan Kara 
1914fcea62baSJan Kara 	nepos.block = neloc;
1915fcea62baSJan Kara 	nepos.offset = sizeof(struct allocExtDesc);
1916fcea62baSJan Kara 	nepos.bh = bh;
1917fcea62baSJan Kara 
1918fcea62baSJan Kara 	/*
1919fcea62baSJan Kara 	 * Do we have to copy current last extent to make space for indirect
1920fcea62baSJan Kara 	 * one?
1921fcea62baSJan Kara 	 */
1922fcea62baSJan Kara 	if (epos->offset + adsize > sb->s_blocksize) {
1923fcea62baSJan Kara 		struct kernel_lb_addr cp_loc;
1924fcea62baSJan Kara 		uint32_t cp_len;
1925fcea62baSJan Kara 		int cp_type;
1926fcea62baSJan Kara 
1927fcea62baSJan Kara 		epos->offset -= adsize;
1928fcea62baSJan Kara 		cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1929fcea62baSJan Kara 		cp_len |= ((uint32_t)cp_type) << 30;
1930fcea62baSJan Kara 
1931fcea62baSJan Kara 		__udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1932fcea62baSJan Kara 		udf_write_aext(inode, epos, &nepos.block,
1933fcea62baSJan Kara 			       sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1934fcea62baSJan Kara 	} else {
1935fcea62baSJan Kara 		__udf_add_aext(inode, epos, &nepos.block,
1936fcea62baSJan Kara 			       sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1937fcea62baSJan Kara 	}
1938fcea62baSJan Kara 
1939fcea62baSJan Kara 	brelse(epos->bh);
1940fcea62baSJan Kara 	*epos = nepos;
1941fcea62baSJan Kara 
1942fcea62baSJan Kara 	return 0;
1943fcea62baSJan Kara }
1944fcea62baSJan Kara 
1945fcea62baSJan Kara /*
1946fcea62baSJan Kara  * Append extent at the given position - should be the first free one in inode
1947fcea62baSJan Kara  * / indirect extent. This function assumes there is enough space in the inode
1948fcea62baSJan Kara  * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1949fcea62baSJan Kara  */
1950fcea62baSJan Kara int __udf_add_aext(struct inode *inode, struct extent_position *epos,
195197e961fdSPekka Enberg 		   struct kernel_lb_addr *eloc, uint32_t elen, int inc)
19521da177e4SLinus Torvalds {
195348d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
1954fcea62baSJan Kara 	struct allocExtDesc *aed;
1955fcea62baSJan Kara 	int adsize;
19561da177e4SLinus Torvalds 
195748d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
19585ca4e4beSPekka Enberg 		adsize = sizeof(struct short_ad);
195948d6d8ffSMarcin Slusarz 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
19605ca4e4beSPekka Enberg 		adsize = sizeof(struct long_ad);
19611da177e4SLinus Torvalds 	else
19627e49b6f2SJan Kara 		return -EIO;
19631da177e4SLinus Torvalds 
1964fcea62baSJan Kara 	if (!epos->bh) {
1965fcea62baSJan Kara 		WARN_ON(iinfo->i_lenAlloc !=
1966fcea62baSJan Kara 			epos->offset - udf_file_entry_alloc_offset(inode));
1967cb00ea35SCyrill Gorcunov 	} else {
1968ff116fc8SJan Kara 		aed = (struct allocExtDesc *)epos->bh->b_data;
1969fcea62baSJan Kara 		WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
1970fcea62baSJan Kara 			epos->offset - sizeof(struct allocExtDesc));
1971fcea62baSJan Kara 		WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
19721da177e4SLinus Torvalds 	}
19731da177e4SLinus Torvalds 
19747e49b6f2SJan Kara 	udf_write_aext(inode, epos, eloc, elen, inc);
19751da177e4SLinus Torvalds 
1976cb00ea35SCyrill Gorcunov 	if (!epos->bh) {
197748d6d8ffSMarcin Slusarz 		iinfo->i_lenAlloc += adsize;
19781da177e4SLinus Torvalds 		mark_inode_dirty(inode);
1979cb00ea35SCyrill Gorcunov 	} else {
1980ff116fc8SJan Kara 		aed = (struct allocExtDesc *)epos->bh->b_data;
1981c2104fdaSmarcin.slusarz@gmail.com 		le32_add_cpu(&aed->lengthAllocDescs, adsize);
19824b11111aSMarcin Slusarz 		if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
19834b11111aSMarcin Slusarz 				UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
19844b11111aSMarcin Slusarz 			udf_update_tag(epos->bh->b_data,
19854b11111aSMarcin Slusarz 					epos->offset + (inc ? 0 : adsize));
19861da177e4SLinus Torvalds 		else
19874b11111aSMarcin Slusarz 			udf_update_tag(epos->bh->b_data,
19884b11111aSMarcin Slusarz 					sizeof(struct allocExtDesc));
1989ff116fc8SJan Kara 		mark_buffer_dirty_inode(epos->bh, inode);
19901da177e4SLinus Torvalds 	}
19911da177e4SLinus Torvalds 
19927e49b6f2SJan Kara 	return 0;
19931da177e4SLinus Torvalds }
19941da177e4SLinus Torvalds 
1995fcea62baSJan Kara /*
1996fcea62baSJan Kara  * Append extent at given position - should be the first free one in inode
1997fcea62baSJan Kara  * / indirect extent. Takes care of allocating and linking indirect blocks.
1998fcea62baSJan Kara  */
1999fcea62baSJan Kara int udf_add_aext(struct inode *inode, struct extent_position *epos,
2000fcea62baSJan Kara 		 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2001fcea62baSJan Kara {
2002fcea62baSJan Kara 	int adsize;
2003fcea62baSJan Kara 	struct super_block *sb = inode->i_sb;
2004fcea62baSJan Kara 
2005fcea62baSJan Kara 	if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2006fcea62baSJan Kara 		adsize = sizeof(struct short_ad);
2007fcea62baSJan Kara 	else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2008fcea62baSJan Kara 		adsize = sizeof(struct long_ad);
2009fcea62baSJan Kara 	else
2010fcea62baSJan Kara 		return -EIO;
2011fcea62baSJan Kara 
2012fcea62baSJan Kara 	if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2013fcea62baSJan Kara 		int err;
2014fcea62baSJan Kara 		int new_block;
2015fcea62baSJan Kara 
2016fcea62baSJan Kara 		new_block = udf_new_block(sb, NULL,
2017fcea62baSJan Kara 					  epos->block.partitionReferenceNum,
2018fcea62baSJan Kara 					  epos->block.logicalBlockNum, &err);
2019fcea62baSJan Kara 		if (!new_block)
2020fcea62baSJan Kara 			return -ENOSPC;
2021fcea62baSJan Kara 
2022fcea62baSJan Kara 		err = udf_setup_indirect_aext(inode, new_block, epos);
2023fcea62baSJan Kara 		if (err)
2024fcea62baSJan Kara 			return err;
2025fcea62baSJan Kara 	}
2026fcea62baSJan Kara 
2027fcea62baSJan Kara 	return __udf_add_aext(inode, epos, eloc, elen, inc);
2028fcea62baSJan Kara }
2029fcea62baSJan Kara 
20307e49b6f2SJan Kara void udf_write_aext(struct inode *inode, struct extent_position *epos,
203197e961fdSPekka Enberg 		    struct kernel_lb_addr *eloc, uint32_t elen, int inc)
20321da177e4SLinus Torvalds {
20331da177e4SLinus Torvalds 	int adsize;
20341da177e4SLinus Torvalds 	uint8_t *ptr;
20355ca4e4beSPekka Enberg 	struct short_ad *sad;
20365ca4e4beSPekka Enberg 	struct long_ad *lad;
203748d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
20381da177e4SLinus Torvalds 
2039ff116fc8SJan Kara 	if (!epos->bh)
204048d6d8ffSMarcin Slusarz 		ptr = iinfo->i_ext.i_data + epos->offset -
20414b11111aSMarcin Slusarz 			udf_file_entry_alloc_offset(inode) +
204248d6d8ffSMarcin Slusarz 			iinfo->i_lenEAttr;
20431da177e4SLinus Torvalds 	else
2044ff116fc8SJan Kara 		ptr = epos->bh->b_data + epos->offset;
20451da177e4SLinus Torvalds 
204648d6d8ffSMarcin Slusarz 	switch (iinfo->i_alloc_type) {
20471da177e4SLinus Torvalds 	case ICBTAG_FLAG_AD_SHORT:
20485ca4e4beSPekka Enberg 		sad = (struct short_ad *)ptr;
20491da177e4SLinus Torvalds 		sad->extLength = cpu_to_le32(elen);
205097e961fdSPekka Enberg 		sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
20515ca4e4beSPekka Enberg 		adsize = sizeof(struct short_ad);
20521da177e4SLinus Torvalds 		break;
20531da177e4SLinus Torvalds 	case ICBTAG_FLAG_AD_LONG:
20545ca4e4beSPekka Enberg 		lad = (struct long_ad *)ptr;
20551da177e4SLinus Torvalds 		lad->extLength = cpu_to_le32(elen);
205697e961fdSPekka Enberg 		lad->extLocation = cpu_to_lelb(*eloc);
20571da177e4SLinus Torvalds 		memset(lad->impUse, 0x00, sizeof(lad->impUse));
20585ca4e4beSPekka Enberg 		adsize = sizeof(struct long_ad);
20591da177e4SLinus Torvalds 		break;
20601da177e4SLinus Torvalds 	default:
20617e49b6f2SJan Kara 		return;
20621da177e4SLinus Torvalds 	}
20631da177e4SLinus Torvalds 
2064cb00ea35SCyrill Gorcunov 	if (epos->bh) {
206528de7948SCyrill Gorcunov 		if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
20666c79e987SMarcin Slusarz 		    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
20674b11111aSMarcin Slusarz 			struct allocExtDesc *aed =
20684b11111aSMarcin Slusarz 				(struct allocExtDesc *)epos->bh->b_data;
2069ff116fc8SJan Kara 			udf_update_tag(epos->bh->b_data,
20704b11111aSMarcin Slusarz 				       le32_to_cpu(aed->lengthAllocDescs) +
20714b11111aSMarcin Slusarz 				       sizeof(struct allocExtDesc));
20721da177e4SLinus Torvalds 		}
2073ff116fc8SJan Kara 		mark_buffer_dirty_inode(epos->bh, inode);
207428de7948SCyrill Gorcunov 	} else {
20751da177e4SLinus Torvalds 		mark_inode_dirty(inode);
207628de7948SCyrill Gorcunov 	}
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 	if (inc)
2079ff116fc8SJan Kara 		epos->offset += adsize;
20801da177e4SLinus Torvalds }
20811da177e4SLinus Torvalds 
2082b0918d9fSVegard Nossum /*
2083b0918d9fSVegard Nossum  * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2084b0918d9fSVegard Nossum  * someone does some weird stuff.
2085b0918d9fSVegard Nossum  */
2086b0918d9fSVegard Nossum #define UDF_MAX_INDIR_EXTS 16
2087b0918d9fSVegard Nossum 
2088ff116fc8SJan Kara int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
20895ca4e4beSPekka Enberg 		     struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
20901da177e4SLinus Torvalds {
20911da177e4SLinus Torvalds 	int8_t etype;
2092b0918d9fSVegard Nossum 	unsigned int indirections = 0;
20931da177e4SLinus Torvalds 
2094ff116fc8SJan Kara 	while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
2095cb00ea35SCyrill Gorcunov 	       (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
20964b11111aSMarcin Slusarz 		int block;
2097b0918d9fSVegard Nossum 
2098b0918d9fSVegard Nossum 		if (++indirections > UDF_MAX_INDIR_EXTS) {
2099b0918d9fSVegard Nossum 			udf_err(inode->i_sb,
2100b0918d9fSVegard Nossum 				"too many indirect extents in inode %lu\n",
2101b0918d9fSVegard Nossum 				inode->i_ino);
2102b0918d9fSVegard Nossum 			return -1;
2103b0918d9fSVegard Nossum 		}
2104b0918d9fSVegard Nossum 
2105ff116fc8SJan Kara 		epos->block = *eloc;
2106ff116fc8SJan Kara 		epos->offset = sizeof(struct allocExtDesc);
21073bf25cb4SJan Kara 		brelse(epos->bh);
210897e961fdSPekka Enberg 		block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
21094b11111aSMarcin Slusarz 		epos->bh = udf_tread(inode->i_sb, block);
21104b11111aSMarcin Slusarz 		if (!epos->bh) {
21114b11111aSMarcin Slusarz 			udf_debug("reading block %d failed!\n", block);
21121da177e4SLinus Torvalds 			return -1;
21131da177e4SLinus Torvalds 		}
21141da177e4SLinus Torvalds 	}
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 	return etype;
21171da177e4SLinus Torvalds }
21181da177e4SLinus Torvalds 
2119ff116fc8SJan Kara int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
21205ca4e4beSPekka Enberg 			struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
21211da177e4SLinus Torvalds {
21221da177e4SLinus Torvalds 	int alen;
21231da177e4SLinus Torvalds 	int8_t etype;
21241da177e4SLinus Torvalds 	uint8_t *ptr;
21255ca4e4beSPekka Enberg 	struct short_ad *sad;
21265ca4e4beSPekka Enberg 	struct long_ad *lad;
212748d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
212828de7948SCyrill Gorcunov 
2129cb00ea35SCyrill Gorcunov 	if (!epos->bh) {
2130ff116fc8SJan Kara 		if (!epos->offset)
2131ff116fc8SJan Kara 			epos->offset = udf_file_entry_alloc_offset(inode);
213248d6d8ffSMarcin Slusarz 		ptr = iinfo->i_ext.i_data + epos->offset -
21334b11111aSMarcin Slusarz 			udf_file_entry_alloc_offset(inode) +
213448d6d8ffSMarcin Slusarz 			iinfo->i_lenEAttr;
21354b11111aSMarcin Slusarz 		alen = udf_file_entry_alloc_offset(inode) +
213648d6d8ffSMarcin Slusarz 							iinfo->i_lenAlloc;
2137cb00ea35SCyrill Gorcunov 	} else {
2138ff116fc8SJan Kara 		if (!epos->offset)
2139ff116fc8SJan Kara 			epos->offset = sizeof(struct allocExtDesc);
2140ff116fc8SJan Kara 		ptr = epos->bh->b_data + epos->offset;
214128de7948SCyrill Gorcunov 		alen = sizeof(struct allocExtDesc) +
21424b11111aSMarcin Slusarz 			le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
21434b11111aSMarcin Slusarz 							lengthAllocDescs);
21441da177e4SLinus Torvalds 	}
21451da177e4SLinus Torvalds 
214648d6d8ffSMarcin Slusarz 	switch (iinfo->i_alloc_type) {
21471da177e4SLinus Torvalds 	case ICBTAG_FLAG_AD_SHORT:
21484b11111aSMarcin Slusarz 		sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
21494b11111aSMarcin Slusarz 		if (!sad)
21501da177e4SLinus Torvalds 			return -1;
21511da177e4SLinus Torvalds 		etype = le32_to_cpu(sad->extLength) >> 30;
21521da177e4SLinus Torvalds 		eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
21534b11111aSMarcin Slusarz 		eloc->partitionReferenceNum =
215448d6d8ffSMarcin Slusarz 				iinfo->i_location.partitionReferenceNum;
215528de7948SCyrill Gorcunov 		*elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
21561da177e4SLinus Torvalds 		break;
21571da177e4SLinus Torvalds 	case ICBTAG_FLAG_AD_LONG:
21584b11111aSMarcin Slusarz 		lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
21594b11111aSMarcin Slusarz 		if (!lad)
21601da177e4SLinus Torvalds 			return -1;
21611da177e4SLinus Torvalds 		etype = le32_to_cpu(lad->extLength) >> 30;
21621da177e4SLinus Torvalds 		*eloc = lelb_to_cpu(lad->extLocation);
216328de7948SCyrill Gorcunov 		*elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
21641da177e4SLinus Torvalds 		break;
21651da177e4SLinus Torvalds 	default:
2166a983f368SJoe Perches 		udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
21671da177e4SLinus Torvalds 		return -1;
21681da177e4SLinus Torvalds 	}
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds 	return etype;
21711da177e4SLinus Torvalds }
21721da177e4SLinus Torvalds 
217328de7948SCyrill Gorcunov static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
21745ca4e4beSPekka Enberg 			      struct kernel_lb_addr neloc, uint32_t nelen)
21751da177e4SLinus Torvalds {
21765ca4e4beSPekka Enberg 	struct kernel_lb_addr oeloc;
21771da177e4SLinus Torvalds 	uint32_t oelen;
21781da177e4SLinus Torvalds 	int8_t etype;
21791da177e4SLinus Torvalds 
2180ff116fc8SJan Kara 	if (epos.bh)
21813bf25cb4SJan Kara 		get_bh(epos.bh);
21821da177e4SLinus Torvalds 
2183cb00ea35SCyrill Gorcunov 	while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
218497e961fdSPekka Enberg 		udf_write_aext(inode, &epos, &neloc, nelen, 1);
21851da177e4SLinus Torvalds 		neloc = oeloc;
21861da177e4SLinus Torvalds 		nelen = (etype << 30) | oelen;
21871da177e4SLinus Torvalds 	}
218897e961fdSPekka Enberg 	udf_add_aext(inode, &epos, &neloc, nelen, 1);
21893bf25cb4SJan Kara 	brelse(epos.bh);
219028de7948SCyrill Gorcunov 
21911da177e4SLinus Torvalds 	return (nelen >> 30);
21921da177e4SLinus Torvalds }
21931da177e4SLinus Torvalds 
2194ff116fc8SJan Kara int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
21955ca4e4beSPekka Enberg 		       struct kernel_lb_addr eloc, uint32_t elen)
21961da177e4SLinus Torvalds {
2197ff116fc8SJan Kara 	struct extent_position oepos;
2198ff116fc8SJan Kara 	int adsize;
21991da177e4SLinus Torvalds 	int8_t etype;
22001da177e4SLinus Torvalds 	struct allocExtDesc *aed;
220148d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo;
22021da177e4SLinus Torvalds 
2203cb00ea35SCyrill Gorcunov 	if (epos.bh) {
22043bf25cb4SJan Kara 		get_bh(epos.bh);
22053bf25cb4SJan Kara 		get_bh(epos.bh);
22061da177e4SLinus Torvalds 	}
22071da177e4SLinus Torvalds 
220848d6d8ffSMarcin Slusarz 	iinfo = UDF_I(inode);
220948d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
22105ca4e4beSPekka Enberg 		adsize = sizeof(struct short_ad);
221148d6d8ffSMarcin Slusarz 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
22125ca4e4beSPekka Enberg 		adsize = sizeof(struct long_ad);
22131da177e4SLinus Torvalds 	else
22141da177e4SLinus Torvalds 		adsize = 0;
22151da177e4SLinus Torvalds 
2216ff116fc8SJan Kara 	oepos = epos;
2217ff116fc8SJan Kara 	if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
22181da177e4SLinus Torvalds 		return -1;
22191da177e4SLinus Torvalds 
2220cb00ea35SCyrill Gorcunov 	while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
222197e961fdSPekka Enberg 		udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2222cb00ea35SCyrill Gorcunov 		if (oepos.bh != epos.bh) {
2223ff116fc8SJan Kara 			oepos.block = epos.block;
22243bf25cb4SJan Kara 			brelse(oepos.bh);
22253bf25cb4SJan Kara 			get_bh(epos.bh);
2226ff116fc8SJan Kara 			oepos.bh = epos.bh;
2227ff116fc8SJan Kara 			oepos.offset = epos.offset - adsize;
22281da177e4SLinus Torvalds 		}
22291da177e4SLinus Torvalds 	}
22305ca4e4beSPekka Enberg 	memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
22311da177e4SLinus Torvalds 	elen = 0;
22321da177e4SLinus Torvalds 
2233cb00ea35SCyrill Gorcunov 	if (epos.bh != oepos.bh) {
223497e961fdSPekka Enberg 		udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
223597e961fdSPekka Enberg 		udf_write_aext(inode, &oepos, &eloc, elen, 1);
223697e961fdSPekka Enberg 		udf_write_aext(inode, &oepos, &eloc, elen, 1);
2237cb00ea35SCyrill Gorcunov 		if (!oepos.bh) {
223848d6d8ffSMarcin Slusarz 			iinfo->i_lenAlloc -= (adsize * 2);
22391da177e4SLinus Torvalds 			mark_inode_dirty(inode);
2240cb00ea35SCyrill Gorcunov 		} else {
2241ff116fc8SJan Kara 			aed = (struct allocExtDesc *)oepos.bh->b_data;
2242c2104fdaSmarcin.slusarz@gmail.com 			le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
224328de7948SCyrill Gorcunov 			if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
22446c79e987SMarcin Slusarz 			    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
22454b11111aSMarcin Slusarz 				udf_update_tag(oepos.bh->b_data,
22464b11111aSMarcin Slusarz 						oepos.offset - (2 * adsize));
22471da177e4SLinus Torvalds 			else
22484b11111aSMarcin Slusarz 				udf_update_tag(oepos.bh->b_data,
22494b11111aSMarcin Slusarz 						sizeof(struct allocExtDesc));
2250ff116fc8SJan Kara 			mark_buffer_dirty_inode(oepos.bh, inode);
22511da177e4SLinus Torvalds 		}
2252cb00ea35SCyrill Gorcunov 	} else {
225397e961fdSPekka Enberg 		udf_write_aext(inode, &oepos, &eloc, elen, 1);
2254cb00ea35SCyrill Gorcunov 		if (!oepos.bh) {
225548d6d8ffSMarcin Slusarz 			iinfo->i_lenAlloc -= adsize;
22561da177e4SLinus Torvalds 			mark_inode_dirty(inode);
2257cb00ea35SCyrill Gorcunov 		} else {
2258ff116fc8SJan Kara 			aed = (struct allocExtDesc *)oepos.bh->b_data;
2259c2104fdaSmarcin.slusarz@gmail.com 			le32_add_cpu(&aed->lengthAllocDescs, -adsize);
226028de7948SCyrill Gorcunov 			if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
22616c79e987SMarcin Slusarz 			    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
22624b11111aSMarcin Slusarz 				udf_update_tag(oepos.bh->b_data,
22634b11111aSMarcin Slusarz 						epos.offset - adsize);
22641da177e4SLinus Torvalds 			else
22654b11111aSMarcin Slusarz 				udf_update_tag(oepos.bh->b_data,
22664b11111aSMarcin Slusarz 						sizeof(struct allocExtDesc));
2267ff116fc8SJan Kara 			mark_buffer_dirty_inode(oepos.bh, inode);
22681da177e4SLinus Torvalds 		}
22691da177e4SLinus Torvalds 	}
22701da177e4SLinus Torvalds 
22713bf25cb4SJan Kara 	brelse(epos.bh);
22723bf25cb4SJan Kara 	brelse(oepos.bh);
227328de7948SCyrill Gorcunov 
22741da177e4SLinus Torvalds 	return (elen >> 30);
22751da177e4SLinus Torvalds }
22761da177e4SLinus Torvalds 
2277cb00ea35SCyrill Gorcunov int8_t inode_bmap(struct inode *inode, sector_t block,
22785ca4e4beSPekka Enberg 		  struct extent_position *pos, struct kernel_lb_addr *eloc,
2279cb00ea35SCyrill Gorcunov 		  uint32_t *elen, sector_t *offset)
22801da177e4SLinus Torvalds {
22814b11111aSMarcin Slusarz 	unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2282cb00ea35SCyrill Gorcunov 	loff_t lbcount = 0, bcount =
22834b11111aSMarcin Slusarz 	    (loff_t) block << blocksize_bits;
22841da177e4SLinus Torvalds 	int8_t etype;
228548d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo;
22861da177e4SLinus Torvalds 
228748d6d8ffSMarcin Slusarz 	iinfo = UDF_I(inode);
228899600051SNamjae Jeon 	if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2289ff116fc8SJan Kara 		pos->offset = 0;
229048d6d8ffSMarcin Slusarz 		pos->block = iinfo->i_location;
2291ff116fc8SJan Kara 		pos->bh = NULL;
229299600051SNamjae Jeon 	}
22931da177e4SLinus Torvalds 	*elen = 0;
2294cb00ea35SCyrill Gorcunov 	do {
22954b11111aSMarcin Slusarz 		etype = udf_next_aext(inode, pos, eloc, elen, 1);
22964b11111aSMarcin Slusarz 		if (etype == -1) {
22974b11111aSMarcin Slusarz 			*offset = (bcount - lbcount) >> blocksize_bits;
229848d6d8ffSMarcin Slusarz 			iinfo->i_lenExtents = lbcount;
22991da177e4SLinus Torvalds 			return -1;
23001da177e4SLinus Torvalds 		}
23011da177e4SLinus Torvalds 		lbcount += *elen;
23021da177e4SLinus Torvalds 	} while (lbcount <= bcount);
230399600051SNamjae Jeon 	/* update extent cache */
230499600051SNamjae Jeon 	udf_update_extent_cache(inode, lbcount - *elen, pos, 1);
23054b11111aSMarcin Slusarz 	*offset = (bcount + *elen - lbcount) >> blocksize_bits;
23061da177e4SLinus Torvalds 
23071da177e4SLinus Torvalds 	return etype;
23081da177e4SLinus Torvalds }
23091da177e4SLinus Torvalds 
231060448b1dSJan Kara long udf_block_map(struct inode *inode, sector_t block)
23111da177e4SLinus Torvalds {
23125ca4e4beSPekka Enberg 	struct kernel_lb_addr eloc;
2313ff116fc8SJan Kara 	uint32_t elen;
231460448b1dSJan Kara 	sector_t offset;
231528de7948SCyrill Gorcunov 	struct extent_position epos = {};
23161da177e4SLinus Torvalds 	int ret;
23171da177e4SLinus Torvalds 
23184d0fb621SAlessio Igor Bogani 	down_read(&UDF_I(inode)->i_data_sem);
23191da177e4SLinus Torvalds 
23204b11111aSMarcin Slusarz 	if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
23214b11111aSMarcin Slusarz 						(EXT_RECORDED_ALLOCATED >> 30))
232297e961fdSPekka Enberg 		ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
23231da177e4SLinus Torvalds 	else
23241da177e4SLinus Torvalds 		ret = 0;
23251da177e4SLinus Torvalds 
23264d0fb621SAlessio Igor Bogani 	up_read(&UDF_I(inode)->i_data_sem);
23273bf25cb4SJan Kara 	brelse(epos.bh);
23281da177e4SLinus Torvalds 
23291da177e4SLinus Torvalds 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
23301da177e4SLinus Torvalds 		return udf_fixed_to_variable(ret);
23311da177e4SLinus Torvalds 	else
23321da177e4SLinus Torvalds 		return ret;
23331da177e4SLinus Torvalds }
2334