15ce34554SBagas Sanjaya // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * inode.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * PURPOSE
61da177e4SLinus Torvalds * Inode handling routines for the OSTA-UDF(tm) filesystem.
71da177e4SLinus Torvalds *
81da177e4SLinus Torvalds * COPYRIGHT
91da177e4SLinus Torvalds * (C) 1998 Dave Boynton
101da177e4SLinus Torvalds * (C) 1998-2004 Ben Fennema
111da177e4SLinus Torvalds * (C) 1999-2000 Stelias Computing Inc
121da177e4SLinus Torvalds *
131da177e4SLinus Torvalds * HISTORY
141da177e4SLinus Torvalds *
151da177e4SLinus Torvalds * 10/04/98 dgb Added rudimentary directory functions
161da177e4SLinus Torvalds * 10/07/98 Fully working udf_block_map! It works!
171da177e4SLinus Torvalds * 11/25/98 bmap altered to better support extents
184b11111aSMarcin Slusarz * 12/06/98 blf partition support in udf_iget, udf_block_map
194b11111aSMarcin Slusarz * and udf_read_inode
201da177e4SLinus Torvalds * 12/12/98 rewrote udf_block_map to handle next extents and descs across
211da177e4SLinus Torvalds * block boundaries (which is not actually allowed)
221da177e4SLinus Torvalds * 12/20/98 added support for strategy 4096
231da177e4SLinus Torvalds * 03/07/99 rewrote udf_block_map (again)
241da177e4SLinus Torvalds * New funcs, inode_bmap, udf_next_aext
251da177e4SLinus Torvalds * 04/19/99 Support for writing device EA's for major/minor #
261da177e4SLinus Torvalds */
271da177e4SLinus Torvalds
281da177e4SLinus Torvalds #include "udfdecl.h"
291da177e4SLinus Torvalds #include <linux/mm.h>
301da177e4SLinus Torvalds #include <linux/module.h>
311da177e4SLinus Torvalds #include <linux/pagemap.h>
321da177e4SLinus Torvalds #include <linux/writeback.h>
331da177e4SLinus Torvalds #include <linux/slab.h>
34f845fcedSBob Copeland #include <linux/crc-itu-t.h>
35bc112323SNamjae Jeon #include <linux/mpage.h>
36e2e40f2cSChristoph Hellwig #include <linux/uio.h>
372f8b5444SChristoph Hellwig #include <linux/bio.h>
381da177e4SLinus Torvalds
391da177e4SLinus Torvalds #include "udf_i.h"
401da177e4SLinus Torvalds #include "udf_sb.h"
411da177e4SLinus Torvalds
421da177e4SLinus Torvalds #define EXTENT_MERGE_SIZE 5
431da177e4SLinus Torvalds
44c3367a1bSSteven J. Magnani #define FE_MAPPED_PERMS (FE_PERM_U_READ | FE_PERM_U_WRITE | FE_PERM_U_EXEC | \
45c3367a1bSSteven J. Magnani FE_PERM_G_READ | FE_PERM_G_WRITE | FE_PERM_G_EXEC | \
46c3367a1bSSteven J. Magnani FE_PERM_O_READ | FE_PERM_O_WRITE | FE_PERM_O_EXEC)
47c3367a1bSSteven J. Magnani
48c3367a1bSSteven J. Magnani #define FE_DELETE_PERMS (FE_PERM_U_DELETE | FE_PERM_G_DELETE | \
49c3367a1bSSteven J. Magnani FE_PERM_O_DELETE)
50c3367a1bSSteven J. Magnani
51f3a30be7SJan Kara struct udf_map_rq;
52f3a30be7SJan Kara
53faa17292SAl Viro static umode_t udf_convert_permissions(struct fileEntry *);
541da177e4SLinus Torvalds static int udf_update_inode(struct inode *, int);
5549521de1SJan Kara static int udf_sync_inode(struct inode *inode);
56647bd61aSCyrill Gorcunov static int udf_alloc_i_data(struct inode *inode, size_t size);
57b3c03fceSJan Kara static int inode_getblk(struct inode *inode, struct udf_map_rq *map);
5819fd80deSJan Kara static int udf_insert_aext(struct inode *, struct extent_position,
595ca4e4beSPekka Enberg struct kernel_lb_addr, uint32_t);
60b490bdd6SSteve Magnani static void udf_split_extents(struct inode *, int *, int, udf_pblk_t,
613cc6f844SFabian Frederick struct kernel_long_ad *, int *);
621da177e4SLinus Torvalds static void udf_prealloc_extents(struct inode *, int, int,
633cc6f844SFabian Frederick struct kernel_long_ad *, int *);
643cc6f844SFabian Frederick static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *);
6519fd80deSJan Kara static int udf_update_extents(struct inode *, struct kernel_long_ad *, int,
663cc6f844SFabian Frederick int, struct extent_position *);
6736580ed0SJan Kara static int udf_get_block_wb(struct inode *inode, sector_t block,
6836580ed0SJan Kara struct buffer_head *bh_result, int create);
691da177e4SLinus Torvalds
__udf_clear_extent_cache(struct inode * inode)7099600051SNamjae Jeon static void __udf_clear_extent_cache(struct inode *inode)
7199600051SNamjae Jeon {
7299600051SNamjae Jeon struct udf_inode_info *iinfo = UDF_I(inode);
7399600051SNamjae Jeon
7499600051SNamjae Jeon if (iinfo->cached_extent.lstart != -1) {
7599600051SNamjae Jeon brelse(iinfo->cached_extent.epos.bh);
7699600051SNamjae Jeon iinfo->cached_extent.lstart = -1;
7799600051SNamjae Jeon }
7899600051SNamjae Jeon }
7999600051SNamjae Jeon
8099600051SNamjae Jeon /* Invalidate extent cache */
udf_clear_extent_cache(struct inode * inode)8199600051SNamjae Jeon static void udf_clear_extent_cache(struct inode *inode)
8299600051SNamjae Jeon {
8399600051SNamjae Jeon struct udf_inode_info *iinfo = UDF_I(inode);
8499600051SNamjae Jeon
8599600051SNamjae Jeon spin_lock(&iinfo->i_extent_cache_lock);
8699600051SNamjae Jeon __udf_clear_extent_cache(inode);
8799600051SNamjae Jeon spin_unlock(&iinfo->i_extent_cache_lock);
8899600051SNamjae Jeon }
8999600051SNamjae Jeon
9099600051SNamjae Jeon /* Return contents of extent cache */
udf_read_extent_cache(struct inode * inode,loff_t bcount,loff_t * lbcount,struct extent_position * pos)9199600051SNamjae Jeon static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
9299600051SNamjae Jeon loff_t *lbcount, struct extent_position *pos)
9399600051SNamjae Jeon {
9499600051SNamjae Jeon struct udf_inode_info *iinfo = UDF_I(inode);
9599600051SNamjae Jeon int ret = 0;
9699600051SNamjae Jeon
9799600051SNamjae Jeon spin_lock(&iinfo->i_extent_cache_lock);
9899600051SNamjae Jeon if ((iinfo->cached_extent.lstart <= bcount) &&
9999600051SNamjae Jeon (iinfo->cached_extent.lstart != -1)) {
10099600051SNamjae Jeon /* Cache hit */
10199600051SNamjae Jeon *lbcount = iinfo->cached_extent.lstart;
10299600051SNamjae Jeon memcpy(pos, &iinfo->cached_extent.epos,
10399600051SNamjae Jeon sizeof(struct extent_position));
10499600051SNamjae Jeon if (pos->bh)
10599600051SNamjae Jeon get_bh(pos->bh);
10699600051SNamjae Jeon ret = 1;
10799600051SNamjae Jeon }
10899600051SNamjae Jeon spin_unlock(&iinfo->i_extent_cache_lock);
10999600051SNamjae Jeon return ret;
11099600051SNamjae Jeon }
11199600051SNamjae Jeon
11299600051SNamjae Jeon /* Add extent to extent cache */
udf_update_extent_cache(struct inode * inode,loff_t estart,struct extent_position * pos)11399600051SNamjae Jeon static void udf_update_extent_cache(struct inode *inode, loff_t estart,
114b31c9ed9SFabian Frederick struct extent_position *pos)
11599600051SNamjae Jeon {
11699600051SNamjae Jeon struct udf_inode_info *iinfo = UDF_I(inode);
11799600051SNamjae Jeon
11899600051SNamjae Jeon spin_lock(&iinfo->i_extent_cache_lock);
11999600051SNamjae Jeon /* Invalidate previously cached extent */
12099600051SNamjae Jeon __udf_clear_extent_cache(inode);
12199600051SNamjae Jeon if (pos->bh)
12299600051SNamjae Jeon get_bh(pos->bh);
123033c9da0SMarkus Elfring memcpy(&iinfo->cached_extent.epos, pos, sizeof(*pos));
12499600051SNamjae Jeon iinfo->cached_extent.lstart = estart;
12599600051SNamjae Jeon switch (iinfo->i_alloc_type) {
12699600051SNamjae Jeon case ICBTAG_FLAG_AD_SHORT:
127b31c9ed9SFabian Frederick iinfo->cached_extent.epos.offset -= sizeof(struct short_ad);
12899600051SNamjae Jeon break;
12999600051SNamjae Jeon case ICBTAG_FLAG_AD_LONG:
130b31c9ed9SFabian Frederick iinfo->cached_extent.epos.offset -= sizeof(struct long_ad);
131b31c9ed9SFabian Frederick break;
13299600051SNamjae Jeon }
13399600051SNamjae Jeon spin_unlock(&iinfo->i_extent_cache_lock);
13499600051SNamjae Jeon }
135b1e32126SChristoph Hellwig
udf_evict_inode(struct inode * inode)1363aac2b62SAl Viro void udf_evict_inode(struct inode *inode)
1371da177e4SLinus Torvalds {
1383aac2b62SAl Viro struct udf_inode_info *iinfo = UDF_I(inode);
1393aac2b62SAl Viro int want_delete = 0;
1403aac2b62SAl Viro
141044e2e26SJan Kara if (!is_bad_inode(inode)) {
142044e2e26SJan Kara if (!inode->i_nlink) {
1433aac2b62SAl Viro want_delete = 1;
1447e49b6f2SJan Kara udf_setsize(inode, 0);
1451da177e4SLinus Torvalds udf_update_inode(inode, IS_SYNC(inode));
14691b0abe3SJohannes Weiner }
1472c948b3fSJan Kara if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
1482c948b3fSJan Kara inode->i_size != iinfo->i_lenExtents) {
149044e2e26SJan Kara udf_warn(inode->i_sb,
150044e2e26SJan Kara "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
15178ace70cSJoe Perches inode->i_ino, inode->i_mode,
1522c948b3fSJan Kara (unsigned long long)inode->i_size,
1532c948b3fSJan Kara (unsigned long long)iinfo->i_lenExtents);
1541da177e4SLinus Torvalds }
155044e2e26SJan Kara }
156044e2e26SJan Kara truncate_inode_pages_final(&inode->i_data);
157044e2e26SJan Kara invalidate_inode_buffers(inode);
158044e2e26SJan Kara clear_inode(inode);
159382a2287SJan Kara kfree(iinfo->i_data);
160382a2287SJan Kara iinfo->i_data = NULL;
16199600051SNamjae Jeon udf_clear_extent_cache(inode);
1623aac2b62SAl Viro if (want_delete) {
1633aac2b62SAl Viro udf_free_inode(inode);
1643aac2b62SAl Viro }
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds
udf_write_failed(struct address_space * mapping,loff_t to)1675eec54fcSIan Abbott static void udf_write_failed(struct address_space *mapping, loff_t to)
1685eec54fcSIan Abbott {
1695eec54fcSIan Abbott struct inode *inode = mapping->host;
1705eec54fcSIan Abbott struct udf_inode_info *iinfo = UDF_I(inode);
1715eec54fcSIan Abbott loff_t isize = inode->i_size;
1725eec54fcSIan Abbott
1735eec54fcSIan Abbott if (to > isize) {
1747caef267SKirill A. Shutemov truncate_pagecache(inode, isize);
1755eec54fcSIan Abbott if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1765eec54fcSIan Abbott down_write(&iinfo->i_data_sem);
17799600051SNamjae Jeon udf_clear_extent_cache(inode);
1785eec54fcSIan Abbott udf_truncate_extents(inode);
1795eec54fcSIan Abbott up_write(&iinfo->i_data_sem);
1805eec54fcSIan Abbott }
1815eec54fcSIan Abbott }
1825eec54fcSIan Abbott }
1835eec54fcSIan Abbott
udf_adinicb_writepage(struct folio * folio,struct writeback_control * wbc,void * data)1843822a7c4SLinus Torvalds static int udf_adinicb_writepage(struct folio *folio,
18579d3c6dbSJan Kara struct writeback_control *wbc, void *data)
186378b8e1aSNamjae Jeon {
18796acbef6SMatthew Wilcox (Oracle) struct inode *inode = folio->mapping->host;
18879d3c6dbSJan Kara struct udf_inode_info *iinfo = UDF_I(inode);
18979d3c6dbSJan Kara
19096acbef6SMatthew Wilcox (Oracle) BUG_ON(!folio_test_locked(folio));
19196acbef6SMatthew Wilcox (Oracle) BUG_ON(folio->index != 0);
19296acbef6SMatthew Wilcox (Oracle) memcpy_from_file_folio(iinfo->i_data + iinfo->i_lenEAttr, folio, 0,
1935cfc4532SJan Kara i_size_read(inode));
19496acbef6SMatthew Wilcox (Oracle) folio_unlock(folio);
19579d3c6dbSJan Kara mark_inode_dirty(inode);
19679d3c6dbSJan Kara
19779d3c6dbSJan Kara return 0;
19879d3c6dbSJan Kara }
19979d3c6dbSJan Kara
udf_writepages(struct address_space * mapping,struct writeback_control * wbc)200759e4d74SJan Kara static int udf_writepages(struct address_space *mapping,
201759e4d74SJan Kara struct writeback_control *wbc)
20279d3c6dbSJan Kara {
20379d3c6dbSJan Kara struct inode *inode = mapping->host;
20479d3c6dbSJan Kara struct udf_inode_info *iinfo = UDF_I(inode);
20579d3c6dbSJan Kara
20679d3c6dbSJan Kara if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB)
20736580ed0SJan Kara return mpage_writepages(mapping, wbc, udf_get_block_wb);
20879d3c6dbSJan Kara return write_cache_pages(mapping, wbc, udf_adinicb_writepage, NULL);
209378b8e1aSNamjae Jeon }
210378b8e1aSNamjae Jeon
udf_adinicb_readpage(struct page * page)211174cb748SJan Kara static void udf_adinicb_readpage(struct page *page)
212174cb748SJan Kara {
213174cb748SJan Kara struct inode *inode = page->mapping->host;
214174cb748SJan Kara char *kaddr;
215174cb748SJan Kara struct udf_inode_info *iinfo = UDF_I(inode);
216174cb748SJan Kara loff_t isize = i_size_read(inode);
217174cb748SJan Kara
2187b7f6865SJan Kara kaddr = kmap_local_page(page);
219174cb748SJan Kara memcpy(kaddr, iinfo->i_data + iinfo->i_lenEAttr, isize);
220174cb748SJan Kara memset(kaddr + isize, 0, PAGE_SIZE - isize);
221174cb748SJan Kara flush_dcache_page(page);
222174cb748SJan Kara SetPageUptodate(page);
2237b7f6865SJan Kara kunmap_local(kaddr);
224174cb748SJan Kara }
225174cb748SJan Kara
udf_read_folio(struct file * file,struct folio * folio)226759e4d74SJan Kara static int udf_read_folio(struct file *file, struct folio *folio)
2271da177e4SLinus Torvalds {
228b7c31e6fSJan Kara struct udf_inode_info *iinfo = UDF_I(file_inode(file));
229b7c31e6fSJan Kara
230b7c31e6fSJan Kara if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
231b7c31e6fSJan Kara udf_adinicb_readpage(&folio->page);
232b7c31e6fSJan Kara folio_unlock(folio);
233b7c31e6fSJan Kara return 0;
234b7c31e6fSJan Kara }
235f132ab7dSMatthew Wilcox (Oracle) return mpage_read_folio(folio, udf_get_block);
236bc112323SNamjae Jeon }
237bc112323SNamjae Jeon
udf_readahead(struct readahead_control * rac)238d4388340SMatthew Wilcox (Oracle) static void udf_readahead(struct readahead_control *rac)
239bc112323SNamjae Jeon {
240cecb1f06SJan Kara struct udf_inode_info *iinfo = UDF_I(rac->mapping->host);
241cecb1f06SJan Kara
242cecb1f06SJan Kara /*
243cecb1f06SJan Kara * No readahead needed for in-ICB files and udf_get_block() would get
244cecb1f06SJan Kara * confused for such file anyway.
245cecb1f06SJan Kara */
246cecb1f06SJan Kara if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
247cecb1f06SJan Kara return;
248cecb1f06SJan Kara
249d4388340SMatthew Wilcox (Oracle) mpage_readahead(rac, udf_get_block);
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds
udf_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,struct page ** pagep,void ** fsdata)252759e4d74SJan Kara static int udf_write_begin(struct file *file, struct address_space *mapping,
2539d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len,
254be021ee4SNick Piggin struct page **pagep, void **fsdata)
2551da177e4SLinus Torvalds {
25660b99a1bSJan Kara struct udf_inode_info *iinfo = UDF_I(file_inode(file));
25760b99a1bSJan Kara struct page *page;
258155130a4SChristoph Hellwig int ret;
259155130a4SChristoph Hellwig
26060b99a1bSJan Kara if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
26160b99a1bSJan Kara ret = block_write_begin(mapping, pos, len, pagep,
26260b99a1bSJan Kara udf_get_block);
2635eec54fcSIan Abbott if (unlikely(ret))
2645eec54fcSIan Abbott udf_write_failed(mapping, pos + len);
2655eec54fcSIan Abbott return ret;
2665eec54fcSIan Abbott }
26760b99a1bSJan Kara if (WARN_ON_ONCE(pos >= PAGE_SIZE))
26860b99a1bSJan Kara return -EIO;
26960b99a1bSJan Kara page = grab_cache_page_write_begin(mapping, 0);
27060b99a1bSJan Kara if (!page)
27160b99a1bSJan Kara return -ENOMEM;
27260b99a1bSJan Kara *pagep = page;
27360b99a1bSJan Kara if (!PageUptodate(page))
27460b99a1bSJan Kara udf_adinicb_readpage(page);
27560b99a1bSJan Kara return 0;
27660b99a1bSJan Kara }
2775eec54fcSIan Abbott
udf_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)278759e4d74SJan Kara static int udf_write_end(struct file *file, struct address_space *mapping,
279c694e40bSJan Kara loff_t pos, unsigned len, unsigned copied,
280c694e40bSJan Kara struct page *page, void *fsdata)
281c694e40bSJan Kara {
282c694e40bSJan Kara struct inode *inode = file_inode(file);
283c694e40bSJan Kara loff_t last_pos;
284c694e40bSJan Kara
285c694e40bSJan Kara if (UDF_I(inode)->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB)
286c694e40bSJan Kara return generic_write_end(file, mapping, pos, len, copied, page,
287c694e40bSJan Kara fsdata);
288c694e40bSJan Kara last_pos = pos + copied;
289c694e40bSJan Kara if (last_pos > inode->i_size)
290c694e40bSJan Kara i_size_write(inode, last_pos);
291c694e40bSJan Kara set_page_dirty(page);
292c694e40bSJan Kara unlock_page(page);
293c694e40bSJan Kara put_page(page);
294c694e40bSJan Kara
295c694e40bSJan Kara return copied;
296c694e40bSJan Kara }
297c694e40bSJan Kara
udf_direct_IO(struct kiocb * iocb,struct iov_iter * iter)298759e4d74SJan Kara static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2995eec54fcSIan Abbott {
3005eec54fcSIan Abbott struct file *file = iocb->ki_filp;
3015eec54fcSIan Abbott struct address_space *mapping = file->f_mapping;
3027e49b6f2SJan Kara struct inode *inode = mapping->host;
303a6cbcd4aSAl Viro size_t count = iov_iter_count(iter);
3045eec54fcSIan Abbott ssize_t ret;
3057e49b6f2SJan Kara
306d5abfb1bSJan Kara /* Fallback to buffered IO for in-ICB files */
307d5abfb1bSJan Kara if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
308d5abfb1bSJan Kara return 0;
309c8b8e32dSChristoph Hellwig ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
3106f673763SOmar Sandoval if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
311c8b8e32dSChristoph Hellwig udf_write_failed(mapping, iocb->ki_pos + count);
312155130a4SChristoph Hellwig return ret;
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds
udf_bmap(struct address_space * mapping,sector_t block)3151da177e4SLinus Torvalds static sector_t udf_bmap(struct address_space *mapping, sector_t block)
3161da177e4SLinus Torvalds {
317907c6c2fSJan Kara struct udf_inode_info *iinfo = UDF_I(mapping->host);
318907c6c2fSJan Kara
319907c6c2fSJan Kara if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
320907c6c2fSJan Kara return -EINVAL;
3211da177e4SLinus Torvalds return generic_block_bmap(mapping, block, udf_get_block);
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds
324f5e54d6eSChristoph Hellwig const struct address_space_operations udf_aops = {
325e621900aSMatthew Wilcox (Oracle) .dirty_folio = block_dirty_folio,
3267ba13abbSMatthew Wilcox (Oracle) .invalidate_folio = block_invalidate_folio,
327f132ab7dSMatthew Wilcox (Oracle) .read_folio = udf_read_folio,
328d4388340SMatthew Wilcox (Oracle) .readahead = udf_readahead,
329378b8e1aSNamjae Jeon .writepages = udf_writepages,
330be021ee4SNick Piggin .write_begin = udf_write_begin,
331c694e40bSJan Kara .write_end = udf_write_end,
3325eec54fcSIan Abbott .direct_IO = udf_direct_IO,
3331da177e4SLinus Torvalds .bmap = udf_bmap,
33436273e5bSChristoph Hellwig .migrate_folio = buffer_migrate_folio,
3351da177e4SLinus Torvalds };
3361da177e4SLinus Torvalds
337d2eb8c35SJan Kara /*
338d2eb8c35SJan Kara * Expand file stored in ICB to a normal one-block-file
339d2eb8c35SJan Kara *
340d2eb8c35SJan Kara * This function requires i_mutex held
341d2eb8c35SJan Kara */
udf_expand_file_adinicb(struct inode * inode)3427e49b6f2SJan Kara int udf_expand_file_adinicb(struct inode *inode)
3431da177e4SLinus Torvalds {
344a758e99cSMatthew Wilcox (Oracle) struct folio *folio;
34548d6d8ffSMarcin Slusarz struct udf_inode_info *iinfo = UDF_I(inode);
3467e49b6f2SJan Kara int err;
3471da177e4SLinus Torvalds
3485955102cSAl Viro WARN_ON_ONCE(!inode_is_locked(inode));
34948d6d8ffSMarcin Slusarz if (!iinfo->i_lenAlloc) {
3506a3b37e0SJan Kara down_write(&iinfo->i_data_sem);
3511da177e4SLinus Torvalds if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
35248d6d8ffSMarcin Slusarz iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
3531da177e4SLinus Torvalds else
35448d6d8ffSMarcin Slusarz iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
355d2eb8c35SJan Kara up_write(&iinfo->i_data_sem);
3561da177e4SLinus Torvalds mark_inode_dirty(inode);
3577e49b6f2SJan Kara return 0;
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds
360a758e99cSMatthew Wilcox (Oracle) folio = __filemap_get_folio(inode->i_mapping, 0,
361a758e99cSMatthew Wilcox (Oracle) FGP_LOCK | FGP_ACCESSED | FGP_CREAT, GFP_KERNEL);
362a758e99cSMatthew Wilcox (Oracle) if (IS_ERR(folio))
363a758e99cSMatthew Wilcox (Oracle) return PTR_ERR(folio);
364cd7619d6SMatt Mackall
365a758e99cSMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio))
366a758e99cSMatthew Wilcox (Oracle) udf_adinicb_readpage(&folio->page);
367d2eb8c35SJan Kara down_write(&iinfo->i_data_sem);
368382a2287SJan Kara memset(iinfo->i_data + iinfo->i_lenEAttr, 0x00,
36948d6d8ffSMarcin Slusarz iinfo->i_lenAlloc);
37048d6d8ffSMarcin Slusarz iinfo->i_lenAlloc = 0;
3711da177e4SLinus Torvalds if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
37248d6d8ffSMarcin Slusarz iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
3731da177e4SLinus Torvalds else
37448d6d8ffSMarcin Slusarz iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
375a758e99cSMatthew Wilcox (Oracle) folio_mark_dirty(folio);
376a758e99cSMatthew Wilcox (Oracle) folio_unlock(folio);
377d2eb8c35SJan Kara up_write(&iinfo->i_data_sem);
3787fc3b7c2SJan Kara err = filemap_fdatawrite(inode->i_mapping);
3797e49b6f2SJan Kara if (err) {
3807e49b6f2SJan Kara /* Restore everything back so that we don't lose data... */
381a758e99cSMatthew Wilcox (Oracle) folio_lock(folio);
382d2eb8c35SJan Kara down_write(&iinfo->i_data_sem);
383a758e99cSMatthew Wilcox (Oracle) memcpy_from_folio(iinfo->i_data + iinfo->i_lenEAttr,
384a758e99cSMatthew Wilcox (Oracle) folio, 0, inode->i_size);
385a758e99cSMatthew Wilcox (Oracle) folio_unlock(folio);
3867e49b6f2SJan Kara iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
387ea856919SJan Kara iinfo->i_lenAlloc = inode->i_size;
388d2eb8c35SJan Kara up_write(&iinfo->i_data_sem);
3897e49b6f2SJan Kara }
390a758e99cSMatthew Wilcox (Oracle) folio_put(folio);
3911da177e4SLinus Torvalds mark_inode_dirty(inode);
3927e49b6f2SJan Kara
3937e49b6f2SJan Kara return err;
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds
396f3a30be7SJan Kara #define UDF_MAP_CREATE 0x01 /* Mapping can allocate new blocks */
39780942521SJan Kara #define UDF_MAP_NOPREALLOC 0x02 /* Do not preallocate blocks */
398f3a30be7SJan Kara
399f3a30be7SJan Kara #define UDF_BLK_MAPPED 0x01 /* Block was successfully mapped */
400f3a30be7SJan Kara #define UDF_BLK_NEW 0x02 /* Block was freshly allocated */
401f3a30be7SJan Kara
402f3a30be7SJan Kara struct udf_map_rq {
403f3a30be7SJan Kara sector_t lblk;
404f3a30be7SJan Kara udf_pblk_t pblk;
405f3a30be7SJan Kara int iflags; /* UDF_MAP_ flags determining behavior */
406f3a30be7SJan Kara int oflags; /* UDF_BLK_ flags reporting results */
407f3a30be7SJan Kara };
408f3a30be7SJan Kara
udf_map_block(struct inode * inode,struct udf_map_rq * map)409f3a30be7SJan Kara static int udf_map_block(struct inode *inode, struct udf_map_rq *map)
4101da177e4SLinus Torvalds {
411493447ddSZhao Mengmeng int ret;
412f3a30be7SJan Kara struct udf_inode_info *iinfo = UDF_I(inode);
4131da177e4SLinus Torvalds
41463bceed8SJan Kara if (WARN_ON_ONCE(iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB))
41563bceed8SJan Kara return -EFSCORRUPTED;
41663bceed8SJan Kara
417f3a30be7SJan Kara map->oflags = 0;
418f3a30be7SJan Kara if (!(map->iflags & UDF_MAP_CREATE)) {
419364a6665SJan Kara struct kernel_lb_addr eloc;
420364a6665SJan Kara uint32_t elen;
421364a6665SJan Kara sector_t offset;
422364a6665SJan Kara struct extent_position epos = {};
423493447ddSZhao Mengmeng int8_t etype;
424364a6665SJan Kara
425364a6665SJan Kara down_read(&iinfo->i_data_sem);
426493447ddSZhao Mengmeng ret = inode_bmap(inode, map->lblk, &epos, &eloc, &elen, &offset,
427493447ddSZhao Mengmeng &etype);
428493447ddSZhao Mengmeng if (ret < 0)
429493447ddSZhao Mengmeng goto out_read;
430493447ddSZhao Mengmeng if (ret > 0 && etype == (EXT_RECORDED_ALLOCATED >> 30)) {
431364a6665SJan Kara map->pblk = udf_get_lb_pblock(inode->i_sb, &eloc,
432364a6665SJan Kara offset);
433f3a30be7SJan Kara map->oflags |= UDF_BLK_MAPPED;
434493447ddSZhao Mengmeng ret = 0;
435364a6665SJan Kara }
436493447ddSZhao Mengmeng out_read:
437364a6665SJan Kara up_read(&iinfo->i_data_sem);
438364a6665SJan Kara brelse(epos.bh);
439364a6665SJan Kara
440493447ddSZhao Mengmeng return ret;
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds
4434d0fb621SAlessio Igor Bogani down_write(&iinfo->i_data_sem);
44416d05565SJan Kara /*
44516d05565SJan Kara * Block beyond EOF and prealloc extents? Just discard preallocation
44616d05565SJan Kara * as it is not useful and complicates things.
44716d05565SJan Kara */
448f54aa97fSJan Kara if (((loff_t)map->lblk) << inode->i_blkbits >= iinfo->i_lenExtents)
44916d05565SJan Kara udf_discard_prealloc(inode);
45099600051SNamjae Jeon udf_clear_extent_cache(inode);
451493447ddSZhao Mengmeng ret = inode_getblk(inode, map);
4524d0fb621SAlessio Igor Bogani up_write(&iinfo->i_data_sem);
453493447ddSZhao Mengmeng return ret;
454f3a30be7SJan Kara }
455f3a30be7SJan Kara
__udf_get_block(struct inode * inode,sector_t block,struct buffer_head * bh_result,int flags)45636580ed0SJan Kara static int __udf_get_block(struct inode *inode, sector_t block,
45736580ed0SJan Kara struct buffer_head *bh_result, int flags)
458f3a30be7SJan Kara {
459f3a30be7SJan Kara int err;
460f3a30be7SJan Kara struct udf_map_rq map = {
461f3a30be7SJan Kara .lblk = block,
46236580ed0SJan Kara .iflags = flags,
463f3a30be7SJan Kara };
464f3a30be7SJan Kara
465f3a30be7SJan Kara err = udf_map_block(inode, &map);
466f3a30be7SJan Kara if (err < 0)
467f3a30be7SJan Kara return err;
468f3a30be7SJan Kara if (map.oflags & UDF_BLK_MAPPED) {
469f3a30be7SJan Kara map_bh(bh_result, inode->i_sb, map.pblk);
470f3a30be7SJan Kara if (map.oflags & UDF_BLK_NEW)
471f3a30be7SJan Kara set_buffer_new(bh_result);
472f3a30be7SJan Kara }
473f3a30be7SJan Kara return 0;
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds
udf_get_block(struct inode * inode,sector_t block,struct buffer_head * bh_result,int create)47636580ed0SJan Kara int udf_get_block(struct inode *inode, sector_t block,
47736580ed0SJan Kara struct buffer_head *bh_result, int create)
47836580ed0SJan Kara {
47936580ed0SJan Kara int flags = create ? UDF_MAP_CREATE : 0;
48036580ed0SJan Kara
48136580ed0SJan Kara /*
48236580ed0SJan Kara * We preallocate blocks only for regular files. It also makes sense
48336580ed0SJan Kara * for directories but there's a problem when to drop the
48436580ed0SJan Kara * preallocation. We might use some delayed work for that but I feel
48536580ed0SJan Kara * it's overengineering for a filesystem like UDF.
48636580ed0SJan Kara */
48736580ed0SJan Kara if (!S_ISREG(inode->i_mode))
48836580ed0SJan Kara flags |= UDF_MAP_NOPREALLOC;
48936580ed0SJan Kara return __udf_get_block(inode, block, bh_result, flags);
49036580ed0SJan Kara }
49136580ed0SJan Kara
49236580ed0SJan Kara /*
49336580ed0SJan Kara * We shouldn't be allocating blocks on page writeback since we allocate them
49436580ed0SJan Kara * on page fault. We can spot dirty buffers without allocated blocks though
49536580ed0SJan Kara * when truncate expands file. These however don't have valid data so we can
49636580ed0SJan Kara * safely ignore them. So never allocate blocks from page writeback.
49736580ed0SJan Kara */
udf_get_block_wb(struct inode * inode,sector_t block,struct buffer_head * bh_result,int create)49836580ed0SJan Kara static int udf_get_block_wb(struct inode *inode, sector_t block,
49936580ed0SJan Kara struct buffer_head *bh_result, int create)
50036580ed0SJan Kara {
50136580ed0SJan Kara return __udf_get_block(inode, block, bh_result, 0);
50236580ed0SJan Kara }
50336580ed0SJan Kara
504fa33cdbfSSteven J. Magnani /* Extend the file with new blocks totaling 'new_block_bytes',
505fa33cdbfSSteven J. Magnani * return the number of extents added
506fa33cdbfSSteven J. Magnani */
udf_do_extend_file(struct inode * inode,struct extent_position * last_pos,struct kernel_long_ad * last_ext,loff_t new_block_bytes)5077e49b6f2SJan Kara static int udf_do_extend_file(struct inode *inode,
5087e49b6f2SJan Kara struct extent_position *last_pos,
5097e49b6f2SJan Kara struct kernel_long_ad *last_ext,
510fa33cdbfSSteven J. Magnani loff_t new_block_bytes)
51131170b6aSJan Kara {
512fa33cdbfSSteven J. Magnani uint32_t add;
51331170b6aSJan Kara int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
51431170b6aSJan Kara struct super_block *sb = inode->i_sb;
51548d6d8ffSMarcin Slusarz struct udf_inode_info *iinfo;
5167e49b6f2SJan Kara int err;
51731170b6aSJan Kara
51831170b6aSJan Kara /* The previous extent is fake and we should not extend by anything
51931170b6aSJan Kara * - there's nothing to do... */
520fa33cdbfSSteven J. Magnani if (!new_block_bytes && fake)
52131170b6aSJan Kara return 0;
52228de7948SCyrill Gorcunov
52348d6d8ffSMarcin Slusarz iinfo = UDF_I(inode);
52431170b6aSJan Kara /* Round the last extent up to a multiple of block size */
52531170b6aSJan Kara if (last_ext->extLength & (sb->s_blocksize - 1)) {
52631170b6aSJan Kara last_ext->extLength =
52731170b6aSJan Kara (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
52831170b6aSJan Kara (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
52931170b6aSJan Kara sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
53048d6d8ffSMarcin Slusarz iinfo->i_lenExtents =
53148d6d8ffSMarcin Slusarz (iinfo->i_lenExtents + sb->s_blocksize - 1) &
53231170b6aSJan Kara ~(sb->s_blocksize - 1);
53331170b6aSJan Kara }
53428de7948SCyrill Gorcunov
535e57191a8SJan Kara add = 0;
53631170b6aSJan Kara /* Can we merge with the previous extent? */
5374b11111aSMarcin Slusarz if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
5384b11111aSMarcin Slusarz EXT_NOT_RECORDED_NOT_ALLOCATED) {
539fa33cdbfSSteven J. Magnani add = (1 << 30) - sb->s_blocksize -
540fa33cdbfSSteven J. Magnani (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
541fa33cdbfSSteven J. Magnani if (add > new_block_bytes)
542fa33cdbfSSteven J. Magnani add = new_block_bytes;
543fa33cdbfSSteven J. Magnani new_block_bytes -= add;
544fa33cdbfSSteven J. Magnani last_ext->extLength += add;
54531170b6aSJan Kara }
54631170b6aSJan Kara
54731170b6aSJan Kara if (fake) {
54870bfb3a8SJan Kara err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
54931170b6aSJan Kara last_ext->extLength, 1);
55070bfb3a8SJan Kara if (err < 0)
55170bfb3a8SJan Kara goto out_err;
55231170b6aSJan Kara count++;
5536c371578SJan Kara } else {
5546c371578SJan Kara struct kernel_lb_addr tmploc;
5556c371578SJan Kara uint32_t tmplen;
5565fc8da4dSZhao Mengmeng int8_t tmptype;
5576c371578SJan Kara
55897e961fdSPekka Enberg udf_write_aext(inode, last_pos, &last_ext->extLocation,
5594b11111aSMarcin Slusarz last_ext->extLength, 1);
56063c9e47aSSteven J. Magnani
5616c371578SJan Kara /*
56263c9e47aSSteven J. Magnani * We've rewritten the last extent. If we are going to add
56363c9e47aSSteven J. Magnani * more extents, we may need to enter possible following
56463c9e47aSSteven J. Magnani * empty indirect extent.
5656c371578SJan Kara */
5665fc8da4dSZhao Mengmeng if (new_block_bytes) {
5675fc8da4dSZhao Mengmeng err = udf_next_aext(inode, last_pos, &tmploc, &tmplen,
5685fc8da4dSZhao Mengmeng &tmptype, 0);
5695fc8da4dSZhao Mengmeng if (err < 0)
5705fc8da4dSZhao Mengmeng goto out_err;
5715fc8da4dSZhao Mengmeng }
5726c371578SJan Kara }
573e57191a8SJan Kara iinfo->i_lenExtents += add;
57428de7948SCyrill Gorcunov
57531170b6aSJan Kara /* Managed to do everything necessary? */
576fa33cdbfSSteven J. Magnani if (!new_block_bytes)
57731170b6aSJan Kara goto out;
57831170b6aSJan Kara
57931170b6aSJan Kara /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
58031170b6aSJan Kara last_ext->extLocation.logicalBlockNum = 0;
58131170b6aSJan Kara last_ext->extLocation.partitionReferenceNum = 0;
582fa33cdbfSSteven J. Magnani add = (1 << 30) - sb->s_blocksize;
583fa33cdbfSSteven J. Magnani last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add;
58428de7948SCyrill Gorcunov
58531170b6aSJan Kara /* Create enough extents to cover the whole hole */
586fa33cdbfSSteven J. Magnani while (new_block_bytes > add) {
587fa33cdbfSSteven J. Magnani new_block_bytes -= add;
5887e49b6f2SJan Kara err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
5897e49b6f2SJan Kara last_ext->extLength, 1);
5907e49b6f2SJan Kara if (err)
59170bfb3a8SJan Kara goto out_err;
592e57191a8SJan Kara iinfo->i_lenExtents += add;
59331170b6aSJan Kara count++;
59431170b6aSJan Kara }
595fa33cdbfSSteven J. Magnani if (new_block_bytes) {
59631170b6aSJan Kara last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
597fa33cdbfSSteven J. Magnani new_block_bytes;
5987e49b6f2SJan Kara err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
5997e49b6f2SJan Kara last_ext->extLength, 1);
6007e49b6f2SJan Kara if (err)
60170bfb3a8SJan Kara goto out_err;
602e57191a8SJan Kara iinfo->i_lenExtents += new_block_bytes;
60331170b6aSJan Kara count++;
60431170b6aSJan Kara }
60528de7948SCyrill Gorcunov
60631170b6aSJan Kara out:
60731170b6aSJan Kara /* last_pos should point to the last written extent... */
60848d6d8ffSMarcin Slusarz if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
6095ca4e4beSPekka Enberg last_pos->offset -= sizeof(struct short_ad);
61048d6d8ffSMarcin Slusarz else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
6115ca4e4beSPekka Enberg last_pos->offset -= sizeof(struct long_ad);
61231170b6aSJan Kara else
6137e49b6f2SJan Kara return -EIO;
61428de7948SCyrill Gorcunov
61531170b6aSJan Kara return count;
61670bfb3a8SJan Kara out_err:
61770bfb3a8SJan Kara /* Remove extents we've created so far */
61870bfb3a8SJan Kara udf_clear_extent_cache(inode);
61970bfb3a8SJan Kara udf_truncate_extents(inode);
62070bfb3a8SJan Kara return err;
62131170b6aSJan Kara }
62231170b6aSJan Kara
623fa33cdbfSSteven J. Magnani /* Extend the final block of the file to final_block_len bytes */
udf_do_extend_final_block(struct inode * inode,struct extent_position * last_pos,struct kernel_long_ad * last_ext,uint32_t new_elen)624fa33cdbfSSteven J. Magnani static void udf_do_extend_final_block(struct inode *inode,
625fa33cdbfSSteven J. Magnani struct extent_position *last_pos,
626fa33cdbfSSteven J. Magnani struct kernel_long_ad *last_ext,
6271f3868f0SJan Kara uint32_t new_elen)
628fa33cdbfSSteven J. Magnani {
629fa33cdbfSSteven J. Magnani uint32_t added_bytes;
630fa33cdbfSSteven J. Magnani
6311f3868f0SJan Kara /*
6321f3868f0SJan Kara * Extent already large enough? It may be already rounded up to block
6331f3868f0SJan Kara * size...
6341f3868f0SJan Kara */
6351f3868f0SJan Kara if (new_elen <= (last_ext->extLength & UDF_EXTENT_LENGTH_MASK))
6361f3868f0SJan Kara return;
63783c7423dSJan Kara added_bytes = new_elen - (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
638fa33cdbfSSteven J. Magnani last_ext->extLength += added_bytes;
639fa33cdbfSSteven J. Magnani UDF_I(inode)->i_lenExtents += added_bytes;
640fa33cdbfSSteven J. Magnani
641fa33cdbfSSteven J. Magnani udf_write_aext(inode, last_pos, &last_ext->extLocation,
642fa33cdbfSSteven J. Magnani last_ext->extLength, 1);
643fa33cdbfSSteven J. Magnani }
644fa33cdbfSSteven J. Magnani
udf_extend_file(struct inode * inode,loff_t newsize)6457e49b6f2SJan Kara static int udf_extend_file(struct inode *inode, loff_t newsize)
6467e49b6f2SJan Kara {
6477e49b6f2SJan Kara
6487e49b6f2SJan Kara struct extent_position epos;
6497e49b6f2SJan Kara struct kernel_lb_addr eloc;
6507e49b6f2SJan Kara uint32_t elen;
6517e49b6f2SJan Kara int8_t etype;
6527e49b6f2SJan Kara struct super_block *sb = inode->i_sb;
6537e49b6f2SJan Kara sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
6541f3868f0SJan Kara loff_t new_elen;
6557e49b6f2SJan Kara int adsize;
6567e49b6f2SJan Kara struct udf_inode_info *iinfo = UDF_I(inode);
6577e49b6f2SJan Kara struct kernel_long_ad extent;
658fa33cdbfSSteven J. Magnani int err = 0;
6591f3868f0SJan Kara bool within_last_ext;
6607e49b6f2SJan Kara
6617e49b6f2SJan Kara if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
6627e49b6f2SJan Kara adsize = sizeof(struct short_ad);
6637e49b6f2SJan Kara else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
6647e49b6f2SJan Kara adsize = sizeof(struct long_ad);
6657e49b6f2SJan Kara else
6667e49b6f2SJan Kara BUG();
6677e49b6f2SJan Kara
6682d532616SJan Kara down_write(&iinfo->i_data_sem);
66916d05565SJan Kara /*
67016d05565SJan Kara * When creating hole in file, just don't bother with preserving
67116d05565SJan Kara * preallocation. It likely won't be very useful anyway.
67216d05565SJan Kara */
67316d05565SJan Kara udf_discard_prealloc(inode);
67416d05565SJan Kara
675493447ddSZhao Mengmeng err = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset, &etype);
676493447ddSZhao Mengmeng if (err < 0)
677493447ddSZhao Mengmeng goto out;
678493447ddSZhao Mengmeng within_last_ext = (err == 1);
67916d05565SJan Kara /* We don't expect extents past EOF... */
6801f3868f0SJan Kara WARN_ON_ONCE(within_last_ext &&
68116d05565SJan Kara elen > ((loff_t)offset + 1) << inode->i_blkbits);
6827e49b6f2SJan Kara
6837e49b6f2SJan Kara if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
6847e49b6f2SJan Kara (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
6857e49b6f2SJan Kara /* File has no extents at all or has empty last
6867e49b6f2SJan Kara * indirect extent! Create a fake extent... */
6877e49b6f2SJan Kara extent.extLocation.logicalBlockNum = 0;
6887e49b6f2SJan Kara extent.extLocation.partitionReferenceNum = 0;
6897e49b6f2SJan Kara extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
6907e49b6f2SJan Kara } else {
6917e49b6f2SJan Kara epos.offset -= adsize;
6925fc8da4dSZhao Mengmeng err = udf_next_aext(inode, &epos, &extent.extLocation,
6935fc8da4dSZhao Mengmeng &extent.extLength, &etype, 0);
6945fc8da4dSZhao Mengmeng if (err <= 0)
6955fc8da4dSZhao Mengmeng goto out;
6967e49b6f2SJan Kara extent.extLength |= etype << 30;
6977e49b6f2SJan Kara }
698fa33cdbfSSteven J. Magnani
6991f3868f0SJan Kara new_elen = ((loff_t)offset << inode->i_blkbits) |
7001f3868f0SJan Kara (newsize & (sb->s_blocksize - 1));
701fa33cdbfSSteven J. Magnani
702fa33cdbfSSteven J. Magnani /* File has extent covering the new size (could happen when extending
703fa33cdbfSSteven J. Magnani * inside a block)?
704fa33cdbfSSteven J. Magnani */
7051f3868f0SJan Kara if (within_last_ext) {
706fa33cdbfSSteven J. Magnani /* Extending file within the last file block */
7071f3868f0SJan Kara udf_do_extend_final_block(inode, &epos, &extent, new_elen);
708fa33cdbfSSteven J. Magnani } else {
7091f3868f0SJan Kara err = udf_do_extend_file(inode, &epos, &extent, new_elen);
710fa33cdbfSSteven J. Magnani }
711fa33cdbfSSteven J. Magnani
7127e49b6f2SJan Kara if (err < 0)
7137e49b6f2SJan Kara goto out;
7147e49b6f2SJan Kara err = 0;
7157e49b6f2SJan Kara out:
7167e49b6f2SJan Kara brelse(epos.bh);
7172d532616SJan Kara up_write(&iinfo->i_data_sem);
7187e49b6f2SJan Kara return err;
7197e49b6f2SJan Kara }
7207e49b6f2SJan Kara
inode_getblk(struct inode * inode,struct udf_map_rq * map)721b3c03fceSJan Kara static int inode_getblk(struct inode *inode, struct udf_map_rq *map)
7221da177e4SLinus Torvalds {
7235ca4e4beSPekka Enberg struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
724ff116fc8SJan Kara struct extent_position prev_epos, cur_epos, next_epos;
7251da177e4SLinus Torvalds int count = 0, startnum = 0, endnum = 0;
72685d71244SJan Kara uint32_t elen = 0, tmpelen;
7275ca4e4beSPekka Enberg struct kernel_lb_addr eloc, tmpeloc;
7281da177e4SLinus Torvalds int c = 1;
72960448b1dSJan Kara loff_t lbcount = 0, b_off = 0;
730b3c03fceSJan Kara udf_pblk_t newblocknum;
73160448b1dSJan Kara sector_t offset = 0;
7325fc8da4dSZhao Mengmeng int8_t etype, tmpetype;
73348d6d8ffSMarcin Slusarz struct udf_inode_info *iinfo = UDF_I(inode);
734b490bdd6SSteve Magnani udf_pblk_t goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
73531170b6aSJan Kara int lastblock = 0;
7365fc8da4dSZhao Mengmeng bool isBeyondEOF = false;
737b3c03fceSJan Kara int ret = 0;
7381da177e4SLinus Torvalds
739ff116fc8SJan Kara prev_epos.offset = udf_file_entry_alloc_offset(inode);
74048d6d8ffSMarcin Slusarz prev_epos.block = iinfo->i_location;
741ff116fc8SJan Kara prev_epos.bh = NULL;
742ff116fc8SJan Kara cur_epos = next_epos = prev_epos;
743b3c03fceSJan Kara b_off = (loff_t)map->lblk << inode->i_sb->s_blocksize_bits;
7441da177e4SLinus Torvalds
7451da177e4SLinus Torvalds /* find the extent which contains the block we are looking for.
7461da177e4SLinus Torvalds alternate between laarr[0] and laarr[1] for locations of the
7471da177e4SLinus Torvalds current extent, and the previous extent */
748cb00ea35SCyrill Gorcunov do {
749cb00ea35SCyrill Gorcunov if (prev_epos.bh != cur_epos.bh) {
7503bf25cb4SJan Kara brelse(prev_epos.bh);
7513bf25cb4SJan Kara get_bh(cur_epos.bh);
752ff116fc8SJan Kara prev_epos.bh = cur_epos.bh;
7531da177e4SLinus Torvalds }
754cb00ea35SCyrill Gorcunov if (cur_epos.bh != next_epos.bh) {
7553bf25cb4SJan Kara brelse(cur_epos.bh);
7563bf25cb4SJan Kara get_bh(next_epos.bh);
757ff116fc8SJan Kara cur_epos.bh = next_epos.bh;
7581da177e4SLinus Torvalds }
7591da177e4SLinus Torvalds
7601da177e4SLinus Torvalds lbcount += elen;
7611da177e4SLinus Torvalds
762ff116fc8SJan Kara prev_epos.block = cur_epos.block;
763ff116fc8SJan Kara cur_epos.block = next_epos.block;
7641da177e4SLinus Torvalds
765ff116fc8SJan Kara prev_epos.offset = cur_epos.offset;
766ff116fc8SJan Kara cur_epos.offset = next_epos.offset;
7671da177e4SLinus Torvalds
7685fc8da4dSZhao Mengmeng ret = udf_next_aext(inode, &next_epos, &eloc, &elen, &etype, 1);
7695fc8da4dSZhao Mengmeng if (ret < 0) {
7705fc8da4dSZhao Mengmeng goto out_free;
7715fc8da4dSZhao Mengmeng } else if (ret == 0) {
7725fc8da4dSZhao Mengmeng isBeyondEOF = true;
7731da177e4SLinus Torvalds break;
7745fc8da4dSZhao Mengmeng }
7751da177e4SLinus Torvalds
7761da177e4SLinus Torvalds c = !c;
7771da177e4SLinus Torvalds
7781da177e4SLinus Torvalds laarr[c].extLength = (etype << 30) | elen;
7791da177e4SLinus Torvalds laarr[c].extLocation = eloc;
7801da177e4SLinus Torvalds
7811da177e4SLinus Torvalds if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
7821da177e4SLinus Torvalds pgoal = eloc.logicalBlockNum +
7831da177e4SLinus Torvalds ((elen + inode->i_sb->s_blocksize - 1) >>
7841da177e4SLinus Torvalds inode->i_sb->s_blocksize_bits);
7851da177e4SLinus Torvalds
7861da177e4SLinus Torvalds count++;
7871da177e4SLinus Torvalds } while (lbcount + elen <= b_off);
7881da177e4SLinus Torvalds
7891da177e4SLinus Torvalds b_off -= lbcount;
7901da177e4SLinus Torvalds offset = b_off >> inode->i_sb->s_blocksize_bits;
79185d71244SJan Kara /*
79285d71244SJan Kara * Move prev_epos and cur_epos into indirect extent if we are at
79385d71244SJan Kara * the pointer to it
79485d71244SJan Kara */
7955fc8da4dSZhao Mengmeng ret = udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, &tmpetype, 0);
7965fc8da4dSZhao Mengmeng if (ret < 0)
7975fc8da4dSZhao Mengmeng goto out_free;
7985fc8da4dSZhao Mengmeng ret = udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, &tmpetype, 0);
7995fc8da4dSZhao Mengmeng if (ret < 0)
8005fc8da4dSZhao Mengmeng goto out_free;
8011da177e4SLinus Torvalds
8021da177e4SLinus Torvalds /* if the extent is allocated and recorded, return the block
8031da177e4SLinus Torvalds if the extent is not a multiple of the blocksize, round up */
8041da177e4SLinus Torvalds
8055fc8da4dSZhao Mengmeng if (!isBeyondEOF && etype == (EXT_RECORDED_ALLOCATED >> 30)) {
806cb00ea35SCyrill Gorcunov if (elen & (inode->i_sb->s_blocksize - 1)) {
8071da177e4SLinus Torvalds elen = EXT_RECORDED_ALLOCATED |
8081da177e4SLinus Torvalds ((elen + inode->i_sb->s_blocksize - 1) &
8091da177e4SLinus Torvalds ~(inode->i_sb->s_blocksize - 1));
810b316c443SJan Kara iinfo->i_lenExtents =
811b316c443SJan Kara ALIGN(iinfo->i_lenExtents,
812b316c443SJan Kara inode->i_sb->s_blocksize);
8137e49b6f2SJan Kara udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
8141da177e4SLinus Torvalds }
815b3c03fceSJan Kara map->oflags = UDF_BLK_MAPPED;
816b3c03fceSJan Kara map->pblk = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
81702d4ca49SFabian Frederick goto out_free;
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds
82016d05565SJan Kara /* Are we beyond EOF and preallocated extent? */
8215fc8da4dSZhao Mengmeng if (isBeyondEOF) {
822fa33cdbfSSteven J. Magnani loff_t hole_len;
82316d05565SJan Kara
82431170b6aSJan Kara if (count) {
82531170b6aSJan Kara if (c)
82631170b6aSJan Kara laarr[0] = laarr[1];
82731170b6aSJan Kara startnum = 1;
828cb00ea35SCyrill Gorcunov } else {
82931170b6aSJan Kara /* Create a fake extent when there's not one */
8304b11111aSMarcin Slusarz memset(&laarr[0].extLocation, 0x00,
8315ca4e4beSPekka Enberg sizeof(struct kernel_lb_addr));
83231170b6aSJan Kara laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
8337e49b6f2SJan Kara /* Will udf_do_extend_file() create real extent from
8344b11111aSMarcin Slusarz a fake one? */
83531170b6aSJan Kara startnum = (offset > 0);
83631170b6aSJan Kara }
83731170b6aSJan Kara /* Create extents for the hole between EOF and offset */
838fa33cdbfSSteven J. Magnani hole_len = (loff_t)offset << inode->i_blkbits;
839fa33cdbfSSteven J. Magnani ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
840b3c03fceSJan Kara if (ret < 0)
84102d4ca49SFabian Frederick goto out_free;
84231170b6aSJan Kara c = 0;
84331170b6aSJan Kara offset = 0;
84431170b6aSJan Kara count += ret;
84536ec52eaSJan Kara /*
84636ec52eaSJan Kara * Is there any real extent? - otherwise we overwrite the fake
84736ec52eaSJan Kara * one...
84836ec52eaSJan Kara */
84931170b6aSJan Kara if (count)
8501da177e4SLinus Torvalds c = !c;
8511da177e4SLinus Torvalds laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
85231170b6aSJan Kara inode->i_sb->s_blocksize;
8534b11111aSMarcin Slusarz memset(&laarr[c].extLocation, 0x00,
8545ca4e4beSPekka Enberg sizeof(struct kernel_lb_addr));
8551da177e4SLinus Torvalds count++;
85631170b6aSJan Kara endnum = c + 1;
8571da177e4SLinus Torvalds lastblock = 1;
858cb00ea35SCyrill Gorcunov } else {
8591da177e4SLinus Torvalds endnum = startnum = ((count > 2) ? 2 : count);
8601da177e4SLinus Torvalds
8614b11111aSMarcin Slusarz /* if the current extent is in position 0,
8624b11111aSMarcin Slusarz swap it with the previous */
863cb00ea35SCyrill Gorcunov if (!c && count != 1) {
8641da177e4SLinus Torvalds laarr[2] = laarr[0];
8651da177e4SLinus Torvalds laarr[0] = laarr[1];
8661da177e4SLinus Torvalds laarr[1] = laarr[2];
8671da177e4SLinus Torvalds c = 1;
8681da177e4SLinus Torvalds }
8691da177e4SLinus Torvalds
8704b11111aSMarcin Slusarz /* if the current block is located in an extent,
8714b11111aSMarcin Slusarz read the next extent */
8725fc8da4dSZhao Mengmeng ret = udf_next_aext(inode, &next_epos, &eloc, &elen, &etype, 0);
8735fc8da4dSZhao Mengmeng if (ret > 0) {
8741da177e4SLinus Torvalds laarr[c + 1].extLength = (etype << 30) | elen;
8751da177e4SLinus Torvalds laarr[c + 1].extLocation = eloc;
8761da177e4SLinus Torvalds count++;
8771da177e4SLinus Torvalds startnum++;
8781da177e4SLinus Torvalds endnum++;
8795fc8da4dSZhao Mengmeng } else if (ret == 0)
8801da177e4SLinus Torvalds lastblock = 1;
8815fc8da4dSZhao Mengmeng else
8825fc8da4dSZhao Mengmeng goto out_free;
8831da177e4SLinus Torvalds }
8841da177e4SLinus Torvalds
8851da177e4SLinus Torvalds /* if the current extent is not recorded but allocated, get the
88628de7948SCyrill Gorcunov * block in the extent corresponding to the requested block */
8874b11111aSMarcin Slusarz if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
8881da177e4SLinus Torvalds newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
8894b11111aSMarcin Slusarz else { /* otherwise, allocate a new block */
890b3c03fceSJan Kara if (iinfo->i_next_alloc_block == map->lblk)
89148d6d8ffSMarcin Slusarz goal = iinfo->i_next_alloc_goal;
8921da177e4SLinus Torvalds
893cb00ea35SCyrill Gorcunov if (!goal) {
8944b11111aSMarcin Slusarz if (!(goal = pgoal)) /* XXX: what was intended here? */
89548d6d8ffSMarcin Slusarz goal = iinfo->i_location.logicalBlockNum + 1;
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds
8984b11111aSMarcin Slusarz newblocknum = udf_new_block(inode->i_sb, inode,
89948d6d8ffSMarcin Slusarz iinfo->i_location.partitionReferenceNum,
900b3c03fceSJan Kara goal, &ret);
901b3c03fceSJan Kara if (!newblocknum)
90202d4ca49SFabian Frederick goto out_free;
903fb719c59SNamjae Jeon if (isBeyondEOF)
90448d6d8ffSMarcin Slusarz iinfo->i_lenExtents += inode->i_sb->s_blocksize;
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds
9074b11111aSMarcin Slusarz /* if the extent the requsted block is located in contains multiple
9084b11111aSMarcin Slusarz * blocks, split the extent into at most three extents. blocks prior
9094b11111aSMarcin Slusarz * to requested block, requested block, and blocks after requested
9104b11111aSMarcin Slusarz * block */
9111da177e4SLinus Torvalds udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
9121da177e4SLinus Torvalds
91380942521SJan Kara if (!(map->iflags & UDF_MAP_NOPREALLOC))
9141da177e4SLinus Torvalds udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
9151da177e4SLinus Torvalds
9161da177e4SLinus Torvalds /* merge any continuous blocks in laarr */
9171da177e4SLinus Torvalds udf_merge_extents(inode, laarr, &endnum);
9181da177e4SLinus Torvalds
9191da177e4SLinus Torvalds /* write back the new extents, inserting new extents if the new number
92028de7948SCyrill Gorcunov * of extents is greater than the old number, and deleting extents if
92128de7948SCyrill Gorcunov * the new number of extents is less than the old number */
922b3c03fceSJan Kara ret = udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
923b3c03fceSJan Kara if (ret < 0)
92419fd80deSJan Kara goto out_free;
9251da177e4SLinus Torvalds
926b3c03fceSJan Kara map->pblk = udf_get_pblock(inode->i_sb, newblocknum,
92748d6d8ffSMarcin Slusarz iinfo->i_location.partitionReferenceNum, 0);
928b3c03fceSJan Kara if (!map->pblk) {
929b3c03fceSJan Kara ret = -EFSCORRUPTED;
93002d4ca49SFabian Frederick goto out_free;
9317b0b0933SJan Kara }
932b3c03fceSJan Kara map->oflags = UDF_BLK_NEW | UDF_BLK_MAPPED;
933b3c03fceSJan Kara iinfo->i_next_alloc_block = map->lblk + 1;
934de80dae0SJan Kara iinfo->i_next_alloc_goal = newblocknum + 1;
93559ad88f2SJeff Layton inode_set_ctime_current(inode);
9361da177e4SLinus Torvalds
9371da177e4SLinus Torvalds if (IS_SYNC(inode))
9381da177e4SLinus Torvalds udf_sync_inode(inode);
9391da177e4SLinus Torvalds else
9401da177e4SLinus Torvalds mark_inode_dirty(inode);
941b3c03fceSJan Kara ret = 0;
94202d4ca49SFabian Frederick out_free:
94302d4ca49SFabian Frederick brelse(prev_epos.bh);
94402d4ca49SFabian Frederick brelse(cur_epos.bh);
94502d4ca49SFabian Frederick brelse(next_epos.bh);
946b3c03fceSJan Kara return ret;
9471da177e4SLinus Torvalds }
9481da177e4SLinus Torvalds
udf_split_extents(struct inode * inode,int * c,int offset,udf_pblk_t newblocknum,struct kernel_long_ad * laarr,int * endnum)949cb00ea35SCyrill Gorcunov static void udf_split_extents(struct inode *inode, int *c, int offset,
950b490bdd6SSteve Magnani udf_pblk_t newblocknum,
951b490bdd6SSteve Magnani struct kernel_long_ad *laarr, int *endnum)
9521da177e4SLinus Torvalds {
9534b11111aSMarcin Slusarz unsigned long blocksize = inode->i_sb->s_blocksize;
9544b11111aSMarcin Slusarz unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
9554b11111aSMarcin Slusarz
9561da177e4SLinus Torvalds if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
9574b11111aSMarcin Slusarz (laarr[*c].extLength >> 30) ==
9584b11111aSMarcin Slusarz (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
9591da177e4SLinus Torvalds int curr = *c;
9601da177e4SLinus Torvalds int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
9614b11111aSMarcin Slusarz blocksize - 1) >> blocksize_bits;
9621da177e4SLinus Torvalds int8_t etype = (laarr[curr].extLength >> 30);
9631da177e4SLinus Torvalds
9644b11111aSMarcin Slusarz if (blen == 1)
96528de7948SCyrill Gorcunov ;
9664b11111aSMarcin Slusarz else if (!offset || blen == offset + 1) {
9671da177e4SLinus Torvalds laarr[curr + 2] = laarr[curr + 1];
9681da177e4SLinus Torvalds laarr[curr + 1] = laarr[curr];
969cb00ea35SCyrill Gorcunov } else {
9701da177e4SLinus Torvalds laarr[curr + 3] = laarr[curr + 1];
9711da177e4SLinus Torvalds laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds
974cb00ea35SCyrill Gorcunov if (offset) {
975cb00ea35SCyrill Gorcunov if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
9764b11111aSMarcin Slusarz udf_free_blocks(inode->i_sb, inode,
97797e961fdSPekka Enberg &laarr[curr].extLocation,
9784b11111aSMarcin Slusarz 0, offset);
9794b11111aSMarcin Slusarz laarr[curr].extLength =
9804b11111aSMarcin Slusarz EXT_NOT_RECORDED_NOT_ALLOCATED |
9814b11111aSMarcin Slusarz (offset << blocksize_bits);
9821da177e4SLinus Torvalds laarr[curr].extLocation.logicalBlockNum = 0;
9834b11111aSMarcin Slusarz laarr[curr].extLocation.
9844b11111aSMarcin Slusarz partitionReferenceNum = 0;
9854b11111aSMarcin Slusarz } else
9861da177e4SLinus Torvalds laarr[curr].extLength = (etype << 30) |
9874b11111aSMarcin Slusarz (offset << blocksize_bits);
9881da177e4SLinus Torvalds curr++;
9891da177e4SLinus Torvalds (*c)++;
9901da177e4SLinus Torvalds (*endnum)++;
9911da177e4SLinus Torvalds }
9921da177e4SLinus Torvalds
9931da177e4SLinus Torvalds laarr[curr].extLocation.logicalBlockNum = newblocknum;
9941da177e4SLinus Torvalds if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
9951da177e4SLinus Torvalds laarr[curr].extLocation.partitionReferenceNum =
996c0b34438SMarcin Slusarz UDF_I(inode)->i_location.partitionReferenceNum;
9971da177e4SLinus Torvalds laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
9984b11111aSMarcin Slusarz blocksize;
9991da177e4SLinus Torvalds curr++;
10001da177e4SLinus Torvalds
1001cb00ea35SCyrill Gorcunov if (blen != offset + 1) {
10021da177e4SLinus Torvalds if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
10034b11111aSMarcin Slusarz laarr[curr].extLocation.logicalBlockNum +=
10044b11111aSMarcin Slusarz offset + 1;
100528de7948SCyrill Gorcunov laarr[curr].extLength = (etype << 30) |
10064b11111aSMarcin Slusarz ((blen - (offset + 1)) << blocksize_bits);
10071da177e4SLinus Torvalds curr++;
10081da177e4SLinus Torvalds (*endnum)++;
10091da177e4SLinus Torvalds }
10101da177e4SLinus Torvalds }
10111da177e4SLinus Torvalds }
10121da177e4SLinus Torvalds
udf_prealloc_extents(struct inode * inode,int c,int lastblock,struct kernel_long_ad * laarr,int * endnum)10131da177e4SLinus Torvalds static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
10143cc6f844SFabian Frederick struct kernel_long_ad *laarr,
1015cb00ea35SCyrill Gorcunov int *endnum)
10161da177e4SLinus Torvalds {
10171da177e4SLinus Torvalds int start, length = 0, currlength = 0, i;
10181da177e4SLinus Torvalds
1019cb00ea35SCyrill Gorcunov if (*endnum >= (c + 1)) {
10201da177e4SLinus Torvalds if (!lastblock)
10211da177e4SLinus Torvalds return;
10221da177e4SLinus Torvalds else
10231da177e4SLinus Torvalds start = c;
1024cb00ea35SCyrill Gorcunov } else {
10254b11111aSMarcin Slusarz if ((laarr[c + 1].extLength >> 30) ==
10264b11111aSMarcin Slusarz (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
10271da177e4SLinus Torvalds start = c + 1;
10284b11111aSMarcin Slusarz length = currlength =
10294b11111aSMarcin Slusarz (((laarr[c + 1].extLength &
10304b11111aSMarcin Slusarz UDF_EXTENT_LENGTH_MASK) +
10314b11111aSMarcin Slusarz inode->i_sb->s_blocksize - 1) >>
10324b11111aSMarcin Slusarz inode->i_sb->s_blocksize_bits);
10334b11111aSMarcin Slusarz } else
10341da177e4SLinus Torvalds start = c;
10351da177e4SLinus Torvalds }
10361da177e4SLinus Torvalds
1037cb00ea35SCyrill Gorcunov for (i = start + 1; i <= *endnum; i++) {
1038cb00ea35SCyrill Gorcunov if (i == *endnum) {
10391da177e4SLinus Torvalds if (lastblock)
10401da177e4SLinus Torvalds length += UDF_DEFAULT_PREALLOC_BLOCKS;
10414b11111aSMarcin Slusarz } else if ((laarr[i].extLength >> 30) ==
10424b11111aSMarcin Slusarz (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
10434b11111aSMarcin Slusarz length += (((laarr[i].extLength &
10444b11111aSMarcin Slusarz UDF_EXTENT_LENGTH_MASK) +
10454b11111aSMarcin Slusarz inode->i_sb->s_blocksize - 1) >>
10464b11111aSMarcin Slusarz inode->i_sb->s_blocksize_bits);
10474b11111aSMarcin Slusarz } else
10481da177e4SLinus Torvalds break;
10491da177e4SLinus Torvalds }
10501da177e4SLinus Torvalds
1051cb00ea35SCyrill Gorcunov if (length) {
10521da177e4SLinus Torvalds int next = laarr[start].extLocation.logicalBlockNum +
10531da177e4SLinus Torvalds (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
10544b11111aSMarcin Slusarz inode->i_sb->s_blocksize - 1) >>
10554b11111aSMarcin Slusarz inode->i_sb->s_blocksize_bits);
10561da177e4SLinus Torvalds int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
105728de7948SCyrill Gorcunov laarr[start].extLocation.partitionReferenceNum,
10584b11111aSMarcin Slusarz next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
10594b11111aSMarcin Slusarz length : UDF_DEFAULT_PREALLOC_BLOCKS) -
10604b11111aSMarcin Slusarz currlength);
1061cb00ea35SCyrill Gorcunov if (numalloc) {
10624b11111aSMarcin Slusarz if (start == (c + 1))
10631da177e4SLinus Torvalds laarr[start].extLength +=
10644b11111aSMarcin Slusarz (numalloc <<
10654b11111aSMarcin Slusarz inode->i_sb->s_blocksize_bits);
10664b11111aSMarcin Slusarz else {
10671da177e4SLinus Torvalds memmove(&laarr[c + 2], &laarr[c + 1],
10685ca4e4beSPekka Enberg sizeof(struct long_ad) * (*endnum - (c + 1)));
10691da177e4SLinus Torvalds (*endnum)++;
10701da177e4SLinus Torvalds laarr[c + 1].extLocation.logicalBlockNum = next;
10711da177e4SLinus Torvalds laarr[c + 1].extLocation.partitionReferenceNum =
10724b11111aSMarcin Slusarz laarr[c].extLocation.
10734b11111aSMarcin Slusarz partitionReferenceNum;
10744b11111aSMarcin Slusarz laarr[c + 1].extLength =
10754b11111aSMarcin Slusarz EXT_NOT_RECORDED_ALLOCATED |
10764b11111aSMarcin Slusarz (numalloc <<
10774b11111aSMarcin Slusarz inode->i_sb->s_blocksize_bits);
10781da177e4SLinus Torvalds start = c + 1;
10791da177e4SLinus Torvalds }
10801da177e4SLinus Torvalds
1081cb00ea35SCyrill Gorcunov for (i = start + 1; numalloc && i < *endnum; i++) {
10824b11111aSMarcin Slusarz int elen = ((laarr[i].extLength &
10834b11111aSMarcin Slusarz UDF_EXTENT_LENGTH_MASK) +
10844b11111aSMarcin Slusarz inode->i_sb->s_blocksize - 1) >>
10854b11111aSMarcin Slusarz inode->i_sb->s_blocksize_bits;
10861da177e4SLinus Torvalds
1087cb00ea35SCyrill Gorcunov if (elen > numalloc) {
10881da177e4SLinus Torvalds laarr[i].extLength -=
10894b11111aSMarcin Slusarz (numalloc <<
10904b11111aSMarcin Slusarz inode->i_sb->s_blocksize_bits);
10911da177e4SLinus Torvalds numalloc = 0;
1092cb00ea35SCyrill Gorcunov } else {
10931da177e4SLinus Torvalds numalloc -= elen;
10941da177e4SLinus Torvalds if (*endnum > (i + 1))
10954b11111aSMarcin Slusarz memmove(&laarr[i],
10964b11111aSMarcin Slusarz &laarr[i + 1],
10975ca4e4beSPekka Enberg sizeof(struct long_ad) *
10984b11111aSMarcin Slusarz (*endnum - (i + 1)));
10991da177e4SLinus Torvalds i--;
11001da177e4SLinus Torvalds (*endnum)--;
11011da177e4SLinus Torvalds }
11021da177e4SLinus Torvalds }
1103c0b34438SMarcin Slusarz UDF_I(inode)->i_lenExtents +=
11044b11111aSMarcin Slusarz numalloc << inode->i_sb->s_blocksize_bits;
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds }
11071da177e4SLinus Torvalds }
11081da177e4SLinus Torvalds
udf_merge_extents(struct inode * inode,struct kernel_long_ad * laarr,int * endnum)11093cc6f844SFabian Frederick static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
1110cb00ea35SCyrill Gorcunov int *endnum)
11111da177e4SLinus Torvalds {
11121da177e4SLinus Torvalds int i;
11134b11111aSMarcin Slusarz unsigned long blocksize = inode->i_sb->s_blocksize;
11144b11111aSMarcin Slusarz unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
11151da177e4SLinus Torvalds
1116cb00ea35SCyrill Gorcunov for (i = 0; i < (*endnum - 1); i++) {
11175ca4e4beSPekka Enberg struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
11185ca4e4beSPekka Enberg struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
11191da177e4SLinus Torvalds
11204b11111aSMarcin Slusarz if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
11214b11111aSMarcin Slusarz (((li->extLength >> 30) ==
11224b11111aSMarcin Slusarz (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
11234b11111aSMarcin Slusarz ((lip1->extLocation.logicalBlockNum -
11244b11111aSMarcin Slusarz li->extLocation.logicalBlockNum) ==
11254b11111aSMarcin Slusarz (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
11264b11111aSMarcin Slusarz blocksize - 1) >> blocksize_bits)))) {
11274b11111aSMarcin Slusarz
11284b11111aSMarcin Slusarz if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
11294b11111aSMarcin Slusarz (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
113053cafe1dSJan Kara blocksize - 1) <= UDF_EXTENT_LENGTH_MASK) {
11314b11111aSMarcin Slusarz li->extLength = lip1->extLength +
11324b11111aSMarcin Slusarz (((li->extLength &
11334b11111aSMarcin Slusarz UDF_EXTENT_LENGTH_MASK) +
11344b11111aSMarcin Slusarz blocksize - 1) & ~(blocksize - 1));
11351da177e4SLinus Torvalds if (*endnum > (i + 2))
11361da177e4SLinus Torvalds memmove(&laarr[i + 1], &laarr[i + 2],
11375ca4e4beSPekka Enberg sizeof(struct long_ad) *
11384b11111aSMarcin Slusarz (*endnum - (i + 2)));
11391da177e4SLinus Torvalds i--;
11401da177e4SLinus Torvalds (*endnum)--;
11411da177e4SLinus Torvalds }
11424b11111aSMarcin Slusarz } else if (((li->extLength >> 30) ==
11434b11111aSMarcin Slusarz (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
11444b11111aSMarcin Slusarz ((lip1->extLength >> 30) ==
11454b11111aSMarcin Slusarz (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
114697e961fdSPekka Enberg udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
11474b11111aSMarcin Slusarz ((li->extLength &
11484b11111aSMarcin Slusarz UDF_EXTENT_LENGTH_MASK) +
11494b11111aSMarcin Slusarz blocksize - 1) >> blocksize_bits);
11504b11111aSMarcin Slusarz li->extLocation.logicalBlockNum = 0;
11514b11111aSMarcin Slusarz li->extLocation.partitionReferenceNum = 0;
11524b11111aSMarcin Slusarz
11534b11111aSMarcin Slusarz if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
11544b11111aSMarcin Slusarz (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
11554b11111aSMarcin Slusarz blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
11564b11111aSMarcin Slusarz lip1->extLength = (lip1->extLength -
11574b11111aSMarcin Slusarz (li->extLength &
11584b11111aSMarcin Slusarz UDF_EXTENT_LENGTH_MASK) +
11594b11111aSMarcin Slusarz UDF_EXTENT_LENGTH_MASK) &
11604b11111aSMarcin Slusarz ~(blocksize - 1);
11614b11111aSMarcin Slusarz li->extLength = (li->extLength &
11624b11111aSMarcin Slusarz UDF_EXTENT_FLAG_MASK) +
11634b11111aSMarcin Slusarz (UDF_EXTENT_LENGTH_MASK + 1) -
11644b11111aSMarcin Slusarz blocksize;
11654b11111aSMarcin Slusarz } else {
11664b11111aSMarcin Slusarz li->extLength = lip1->extLength +
11674b11111aSMarcin Slusarz (((li->extLength &
11684b11111aSMarcin Slusarz UDF_EXTENT_LENGTH_MASK) +
11694b11111aSMarcin Slusarz blocksize - 1) & ~(blocksize - 1));
11704b11111aSMarcin Slusarz if (*endnum > (i + 2))
11714b11111aSMarcin Slusarz memmove(&laarr[i + 1], &laarr[i + 2],
11725ca4e4beSPekka Enberg sizeof(struct long_ad) *
11734b11111aSMarcin Slusarz (*endnum - (i + 2)));
11744b11111aSMarcin Slusarz i--;
11754b11111aSMarcin Slusarz (*endnum)--;
11764b11111aSMarcin Slusarz }
11774b11111aSMarcin Slusarz } else if ((li->extLength >> 30) ==
11784b11111aSMarcin Slusarz (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
11794b11111aSMarcin Slusarz udf_free_blocks(inode->i_sb, inode,
118097e961fdSPekka Enberg &li->extLocation, 0,
11814b11111aSMarcin Slusarz ((li->extLength &
11824b11111aSMarcin Slusarz UDF_EXTENT_LENGTH_MASK) +
11834b11111aSMarcin Slusarz blocksize - 1) >> blocksize_bits);
11844b11111aSMarcin Slusarz li->extLocation.logicalBlockNum = 0;
11854b11111aSMarcin Slusarz li->extLocation.partitionReferenceNum = 0;
11864b11111aSMarcin Slusarz li->extLength = (li->extLength &
11874b11111aSMarcin Slusarz UDF_EXTENT_LENGTH_MASK) |
11881da177e4SLinus Torvalds EXT_NOT_RECORDED_NOT_ALLOCATED;
11891da177e4SLinus Torvalds }
11901da177e4SLinus Torvalds }
11911da177e4SLinus Torvalds }
11921da177e4SLinus Torvalds
udf_update_extents(struct inode * inode,struct kernel_long_ad * laarr,int startnum,int endnum,struct extent_position * epos)119319fd80deSJan Kara static int udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
1194cb00ea35SCyrill Gorcunov int startnum, int endnum,
1195ff116fc8SJan Kara struct extent_position *epos)
11961da177e4SLinus Torvalds {
11971da177e4SLinus Torvalds int start = 0, i;
11985ca4e4beSPekka Enberg struct kernel_lb_addr tmploc;
11991da177e4SLinus Torvalds uint32_t tmplen;
12005fc8da4dSZhao Mengmeng int8_t tmpetype;
120119fd80deSJan Kara int err;
12021da177e4SLinus Torvalds
1203cb00ea35SCyrill Gorcunov if (startnum > endnum) {
12041da177e4SLinus Torvalds for (i = 0; i < (startnum - endnum); i++)
12056c1e4d06SJan Kara udf_delete_aext(inode, *epos);
1206cb00ea35SCyrill Gorcunov } else if (startnum < endnum) {
1207cb00ea35SCyrill Gorcunov for (i = 0; i < (endnum - startnum); i++) {
120819fd80deSJan Kara err = udf_insert_aext(inode, *epos,
120919fd80deSJan Kara laarr[i].extLocation,
1210ff116fc8SJan Kara laarr[i].extLength);
121119fd80deSJan Kara /*
121219fd80deSJan Kara * If we fail here, we are likely corrupting the extent
121319fd80deSJan Kara * list and leaking blocks. At least stop early to
121419fd80deSJan Kara * limit the damage.
121519fd80deSJan Kara */
121619fd80deSJan Kara if (err < 0)
121719fd80deSJan Kara return err;
12185fc8da4dSZhao Mengmeng err = udf_next_aext(inode, epos, &laarr[i].extLocation,
12195fc8da4dSZhao Mengmeng &laarr[i].extLength, &tmpetype, 1);
12205fc8da4dSZhao Mengmeng if (err < 0)
12215fc8da4dSZhao Mengmeng return err;
12221da177e4SLinus Torvalds start++;
12231da177e4SLinus Torvalds }
12241da177e4SLinus Torvalds }
12251da177e4SLinus Torvalds
1226cb00ea35SCyrill Gorcunov for (i = start; i < endnum; i++) {
12275fc8da4dSZhao Mengmeng err = udf_next_aext(inode, epos, &tmploc, &tmplen, &tmpetype, 0);
12285fc8da4dSZhao Mengmeng if (err < 0)
12295fc8da4dSZhao Mengmeng return err;
12305fc8da4dSZhao Mengmeng
123197e961fdSPekka Enberg udf_write_aext(inode, epos, &laarr[i].extLocation,
1232ff116fc8SJan Kara laarr[i].extLength, 1);
12331da177e4SLinus Torvalds }
123419fd80deSJan Kara return 0;
12351da177e4SLinus Torvalds }
12361da177e4SLinus Torvalds
udf_bread(struct inode * inode,udf_pblk_t block,int create,int * err)1237b490bdd6SSteve Magnani struct buffer_head *udf_bread(struct inode *inode, udf_pblk_t block,
12381da177e4SLinus Torvalds int create, int *err)
12391da177e4SLinus Torvalds {
12401da177e4SLinus Torvalds struct buffer_head *bh = NULL;
124132f123a3SJan Kara struct udf_map_rq map = {
124232f123a3SJan Kara .lblk = block,
124332f123a3SJan Kara .iflags = UDF_MAP_NOPREALLOC | (create ? UDF_MAP_CREATE : 0),
124432f123a3SJan Kara };
12451da177e4SLinus Torvalds
124632f123a3SJan Kara *err = udf_map_block(inode, &map);
124732f123a3SJan Kara if (*err || !(map.oflags & UDF_BLK_MAPPED))
12481da177e4SLinus Torvalds return NULL;
12491da177e4SLinus Torvalds
125032f123a3SJan Kara bh = sb_getblk(inode->i_sb, map.pblk);
125132f123a3SJan Kara if (!bh) {
125232f123a3SJan Kara *err = -ENOMEM;
125332f123a3SJan Kara return NULL;
125432f123a3SJan Kara }
125532f123a3SJan Kara if (map.oflags & UDF_BLK_NEW) {
125632f123a3SJan Kara lock_buffer(bh);
125732f123a3SJan Kara memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
125832f123a3SJan Kara set_buffer_uptodate(bh);
125932f123a3SJan Kara unlock_buffer(bh);
126032f123a3SJan Kara mark_buffer_dirty_inode(bh, inode);
126132f123a3SJan Kara return bh;
126232f123a3SJan Kara }
126332f123a3SJan Kara
126459a16786SZhang Yi if (bh_read(bh, 0) >= 0)
12651da177e4SLinus Torvalds return bh;
126628de7948SCyrill Gorcunov
12671da177e4SLinus Torvalds brelse(bh);
12681da177e4SLinus Torvalds *err = -EIO;
12691da177e4SLinus Torvalds return NULL;
12701da177e4SLinus Torvalds }
12711da177e4SLinus Torvalds
udf_setsize(struct inode * inode,loff_t newsize)12727e49b6f2SJan Kara int udf_setsize(struct inode *inode, loff_t newsize)
12731da177e4SLinus Torvalds {
1274b9a861fdSJan Kara int err = 0;
127548d6d8ffSMarcin Slusarz struct udf_inode_info *iinfo;
127689a4d970SSteve Magnani unsigned int bsize = i_blocksize(inode);
12771da177e4SLinus Torvalds
12781da177e4SLinus Torvalds if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
12791da177e4SLinus Torvalds S_ISLNK(inode->i_mode)))
12807e49b6f2SJan Kara return -EINVAL;
12811da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
12827e49b6f2SJan Kara return -EPERM;
12831da177e4SLinus Torvalds
128448d6d8ffSMarcin Slusarz iinfo = UDF_I(inode);
12857e49b6f2SJan Kara if (newsize > inode->i_size) {
128648d6d8ffSMarcin Slusarz if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
12876a3b37e0SJan Kara if (bsize >=
12887e49b6f2SJan Kara (udf_file_entry_alloc_offset(inode) + newsize)) {
1289d2eb8c35SJan Kara down_write(&iinfo->i_data_sem);
12907e49b6f2SJan Kara iinfo->i_lenAlloc = newsize;
12912d532616SJan Kara up_write(&iinfo->i_data_sem);
1292bb2b6d19SIan Abbott goto set_size;
1293bb2b6d19SIan Abbott }
12946a3b37e0SJan Kara err = udf_expand_file_adinicb(inode);
12956a3b37e0SJan Kara if (err)
1296e382588bSJan Kara return err;
12971da177e4SLinus Torvalds }
12987e49b6f2SJan Kara err = udf_extend_file(inode, newsize);
12992d532616SJan Kara if (err)
1300e382588bSJan Kara return err;
1301bb2b6d19SIan Abbott set_size:
1302f2e95355SJan Kara truncate_setsize(inode, newsize);
1303cb00ea35SCyrill Gorcunov } else {
13047e49b6f2SJan Kara if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
13054d0fb621SAlessio Igor Bogani down_write(&iinfo->i_data_sem);
130699600051SNamjae Jeon udf_clear_extent_cache(inode);
1307382a2287SJan Kara memset(iinfo->i_data + iinfo->i_lenEAttr + newsize,
13087e49b6f2SJan Kara 0x00, bsize - newsize -
13097e49b6f2SJan Kara udf_file_entry_alloc_offset(inode));
13107e49b6f2SJan Kara iinfo->i_lenAlloc = newsize;
13117e49b6f2SJan Kara truncate_setsize(inode, newsize);
13127e49b6f2SJan Kara up_write(&iinfo->i_data_sem);
13137e49b6f2SJan Kara goto update_time;
13147e49b6f2SJan Kara }
13157e49b6f2SJan Kara err = block_truncate_page(inode->i_mapping, newsize,
13167e49b6f2SJan Kara udf_get_block);
13177e49b6f2SJan Kara if (err)
1318e382588bSJan Kara return err;
1319f2e95355SJan Kara truncate_setsize(inode, newsize);
13207e49b6f2SJan Kara down_write(&iinfo->i_data_sem);
132199600051SNamjae Jeon udf_clear_extent_cache(inode);
13222b42be5eSJan Kara err = udf_truncate_extents(inode);
13234d0fb621SAlessio Igor Bogani up_write(&iinfo->i_data_sem);
13242b42be5eSJan Kara if (err)
1325e382588bSJan Kara return err;
13261da177e4SLinus Torvalds }
13277e49b6f2SJan Kara update_time:
132859ad88f2SJeff Layton inode->i_mtime = inode_set_ctime_current(inode);
13291da177e4SLinus Torvalds if (IS_SYNC(inode))
13301da177e4SLinus Torvalds udf_sync_inode(inode);
13311da177e4SLinus Torvalds else
13321da177e4SLinus Torvalds mark_inode_dirty(inode);
1333b9a861fdSJan Kara return err;
13341da177e4SLinus Torvalds }
13351da177e4SLinus Torvalds
1336c03aa9f6SJan Kara /*
1337c03aa9f6SJan Kara * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1338c03aa9f6SJan Kara * arbitrary - just that we hopefully don't limit any real use of rewritten
1339c03aa9f6SJan Kara * inode on write-once media but avoid looping for too long on corrupted media.
1340c03aa9f6SJan Kara */
1341c03aa9f6SJan Kara #define UDF_MAX_ICB_NESTING 1024
1342c03aa9f6SJan Kara
udf_read_inode(struct inode * inode,bool hidden_inode)13436174c2ebSJan Kara static int udf_read_inode(struct inode *inode, bool hidden_inode)
13441da177e4SLinus Torvalds {
13451da177e4SLinus Torvalds struct buffer_head *bh = NULL;
13461da177e4SLinus Torvalds struct fileEntry *fe;
1347bb7720a0SJan Kara struct extendedFileEntry *efe;
13481da177e4SLinus Torvalds uint16_t ident;
134948d6d8ffSMarcin Slusarz struct udf_inode_info *iinfo = UDF_I(inode);
1350bb7720a0SJan Kara struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
13516d3d5e86SJan Kara struct kernel_lb_addr *iloc = &iinfo->i_location;
1352bb7720a0SJan Kara unsigned int link_count;
1353c03aa9f6SJan Kara unsigned int indirections = 0;
135479144954SJan Kara int bs = inode->i_sb->s_blocksize;
13556d3d5e86SJan Kara int ret = -EIO;
13560c9850f4SJan Kara uint32_t uid, gid;
135759ad88f2SJeff Layton struct timespec64 ctime;
13581da177e4SLinus Torvalds
1359c03aa9f6SJan Kara reread:
13601d82a56bSFabian Frederick if (iloc->partitionReferenceNum >= sbi->s_partitions) {
1361fcbf7637SSteve Magnani udf_debug("partition reference: %u > logical volume partitions: %u\n",
13621d82a56bSFabian Frederick iloc->partitionReferenceNum, sbi->s_partitions);
13631d82a56bSFabian Frederick return -EIO;
13641d82a56bSFabian Frederick }
13651d82a56bSFabian Frederick
13666d3d5e86SJan Kara if (iloc->logicalBlockNum >=
13676d3d5e86SJan Kara sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1368fcbf7637SSteve Magnani udf_debug("block=%u, partition=%u out of range\n",
13696d3d5e86SJan Kara iloc->logicalBlockNum, iloc->partitionReferenceNum);
13706d3d5e86SJan Kara return -EIO;
13716d3d5e86SJan Kara }
13726d3d5e86SJan Kara
13731da177e4SLinus Torvalds /*
13741da177e4SLinus Torvalds * Set defaults, but the inode is still incomplete!
13751da177e4SLinus Torvalds * Note: get_new_inode() sets the following on a new inode:
13761da177e4SLinus Torvalds * i_sb = sb
13771da177e4SLinus Torvalds * i_no = ino
13781da177e4SLinus Torvalds * i_flags = sb->s_flags
13791da177e4SLinus Torvalds * i_state = 0
13801da177e4SLinus Torvalds * clean_inode(): zero fills and sets
13811da177e4SLinus Torvalds * i_count = 1
13821da177e4SLinus Torvalds * i_nlink = 1
13831da177e4SLinus Torvalds * i_op = NULL;
13841da177e4SLinus Torvalds */
13856d3d5e86SJan Kara bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
1386cb00ea35SCyrill Gorcunov if (!bh) {
1387fcbf7637SSteve Magnani udf_err(inode->i_sb, "(ino %lu) failed !bh\n", inode->i_ino);
13886d3d5e86SJan Kara return -EIO;
13891da177e4SLinus Torvalds }
13901da177e4SLinus Torvalds
13911da177e4SLinus Torvalds if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1392cb00ea35SCyrill Gorcunov ident != TAG_IDENT_USE) {
1393fcbf7637SSteve Magnani udf_err(inode->i_sb, "(ino %lu) failed ident=%u\n",
139478ace70cSJoe Perches inode->i_ino, ident);
13956d3d5e86SJan Kara goto out;
13961da177e4SLinus Torvalds }
13971da177e4SLinus Torvalds
13981da177e4SLinus Torvalds fe = (struct fileEntry *)bh->b_data;
1399bb7720a0SJan Kara efe = (struct extendedFileEntry *)bh->b_data;
14001da177e4SLinus Torvalds
14015e0f0017SMarcin Slusarz if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
14021ab92785Smarcin.slusarz@gmail.com struct buffer_head *ibh;
14031da177e4SLinus Torvalds
14046d3d5e86SJan Kara ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
14051ab92785Smarcin.slusarz@gmail.com if (ident == TAG_IDENT_IE && ibh) {
14065ca4e4beSPekka Enberg struct kernel_lb_addr loc;
14071ab92785Smarcin.slusarz@gmail.com struct indirectEntry *ie;
14081da177e4SLinus Torvalds
14091ab92785Smarcin.slusarz@gmail.com ie = (struct indirectEntry *)ibh->b_data;
14101da177e4SLinus Torvalds loc = lelb_to_cpu(ie->indirectICB.extLocation);
14111da177e4SLinus Torvalds
1412c03aa9f6SJan Kara if (ie->indirectICB.extLength) {
14133bf25cb4SJan Kara brelse(ibh);
1414c03aa9f6SJan Kara memcpy(&iinfo->i_location, &loc,
1415c03aa9f6SJan Kara sizeof(struct kernel_lb_addr));
1416c03aa9f6SJan Kara if (++indirections > UDF_MAX_ICB_NESTING) {
1417c03aa9f6SJan Kara udf_err(inode->i_sb,
1418c03aa9f6SJan Kara "too many ICBs in ICB hierarchy"
1419c03aa9f6SJan Kara " (max %d supported)\n",
1420c03aa9f6SJan Kara UDF_MAX_ICB_NESTING);
14216d3d5e86SJan Kara goto out;
14221ab92785Smarcin.slusarz@gmail.com }
14236d3d5e86SJan Kara brelse(bh);
1424c03aa9f6SJan Kara goto reread;
14251ab92785Smarcin.slusarz@gmail.com }
14261ab92785Smarcin.slusarz@gmail.com }
14273bf25cb4SJan Kara brelse(ibh);
14285e0f0017SMarcin Slusarz } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1429fcbf7637SSteve Magnani udf_err(inode->i_sb, "unsupported strategy type: %u\n",
14301da177e4SLinus Torvalds le16_to_cpu(fe->icbTag.strategyType));
14316d3d5e86SJan Kara goto out;
14321da177e4SLinus Torvalds }
14335e0f0017SMarcin Slusarz if (fe->icbTag.strategyType == cpu_to_le16(4))
143448d6d8ffSMarcin Slusarz iinfo->i_strat4096 = 0;
14355e0f0017SMarcin Slusarz else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
143648d6d8ffSMarcin Slusarz iinfo->i_strat4096 = 1;
14371da177e4SLinus Torvalds
143848d6d8ffSMarcin Slusarz iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
14394b11111aSMarcin Slusarz ICBTAG_FLAG_AD_MASK;
1440d288d958SJan Kara if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
1441d288d958SJan Kara iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
1442d288d958SJan Kara iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1443d288d958SJan Kara ret = -EIO;
1444d288d958SJan Kara goto out;
1445d288d958SJan Kara }
1446fc8033a3SJan Kara iinfo->i_hidden = hidden_inode;
144748d6d8ffSMarcin Slusarz iinfo->i_unique = 0;
144848d6d8ffSMarcin Slusarz iinfo->i_lenEAttr = 0;
144948d6d8ffSMarcin Slusarz iinfo->i_lenExtents = 0;
145048d6d8ffSMarcin Slusarz iinfo->i_lenAlloc = 0;
145148d6d8ffSMarcin Slusarz iinfo->i_next_alloc_block = 0;
145248d6d8ffSMarcin Slusarz iinfo->i_next_alloc_goal = 0;
14535e0f0017SMarcin Slusarz if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
145448d6d8ffSMarcin Slusarz iinfo->i_efe = 1;
145548d6d8ffSMarcin Slusarz iinfo->i_use = 0;
145679144954SJan Kara ret = udf_alloc_i_data(inode, bs -
14576d3d5e86SJan Kara sizeof(struct extendedFileEntry));
14586d3d5e86SJan Kara if (ret)
14596d3d5e86SJan Kara goto out;
1460382a2287SJan Kara memcpy(iinfo->i_data,
14614b11111aSMarcin Slusarz bh->b_data + sizeof(struct extendedFileEntry),
146279144954SJan Kara bs - sizeof(struct extendedFileEntry));
14635e0f0017SMarcin Slusarz } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
146448d6d8ffSMarcin Slusarz iinfo->i_efe = 0;
146548d6d8ffSMarcin Slusarz iinfo->i_use = 0;
146679144954SJan Kara ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
14676d3d5e86SJan Kara if (ret)
14686d3d5e86SJan Kara goto out;
1469382a2287SJan Kara memcpy(iinfo->i_data,
1470c0b34438SMarcin Slusarz bh->b_data + sizeof(struct fileEntry),
147179144954SJan Kara bs - sizeof(struct fileEntry));
14725e0f0017SMarcin Slusarz } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
147348d6d8ffSMarcin Slusarz iinfo->i_efe = 0;
147448d6d8ffSMarcin Slusarz iinfo->i_use = 1;
147548d6d8ffSMarcin Slusarz iinfo->i_lenAlloc = le32_to_cpu(
14764b11111aSMarcin Slusarz ((struct unallocSpaceEntry *)bh->b_data)->
14774b11111aSMarcin Slusarz lengthAllocDescs);
147879144954SJan Kara ret = udf_alloc_i_data(inode, bs -
14796d3d5e86SJan Kara sizeof(struct unallocSpaceEntry));
14806d3d5e86SJan Kara if (ret)
14816d3d5e86SJan Kara goto out;
1482382a2287SJan Kara memcpy(iinfo->i_data,
14834b11111aSMarcin Slusarz bh->b_data + sizeof(struct unallocSpaceEntry),
148479144954SJan Kara bs - sizeof(struct unallocSpaceEntry));
14856d3d5e86SJan Kara return 0;
14861da177e4SLinus Torvalds }
14871da177e4SLinus Torvalds
14886d3d5e86SJan Kara ret = -EIO;
1489c03cad24SJan Kara read_lock(&sbi->s_cred_lock);
14900c9850f4SJan Kara uid = le32_to_cpu(fe->uid);
14910c9850f4SJan Kara if (uid == UDF_INVALID_ID ||
1492ca76d2d8SCyrill Gorcunov UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
14930c9850f4SJan Kara inode->i_uid = sbi->s_uid;
14940c9850f4SJan Kara else
14950c9850f4SJan Kara i_uid_write(inode, uid);
14961da177e4SLinus Torvalds
14970c9850f4SJan Kara gid = le32_to_cpu(fe->gid);
14980c9850f4SJan Kara if (gid == UDF_INVALID_ID ||
1499ca76d2d8SCyrill Gorcunov UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
15000c9850f4SJan Kara inode->i_gid = sbi->s_gid;
15010c9850f4SJan Kara else
15020c9850f4SJan Kara i_gid_write(inode, gid);
15031da177e4SLinus Torvalds
15047ac9bcd5SMarcin Slusarz if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
150587bc730cSMarcin Slusarz sbi->s_fmode != UDF_INVALID_MODE)
15067ac9bcd5SMarcin Slusarz inode->i_mode = sbi->s_fmode;
15077ac9bcd5SMarcin Slusarz else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
150887bc730cSMarcin Slusarz sbi->s_dmode != UDF_INVALID_MODE)
15097ac9bcd5SMarcin Slusarz inode->i_mode = sbi->s_dmode;
15107ac9bcd5SMarcin Slusarz else
15111da177e4SLinus Torvalds inode->i_mode = udf_convert_permissions(fe);
15127ac9bcd5SMarcin Slusarz inode->i_mode &= ~sbi->s_umask;
1513c3367a1bSSteven J. Magnani iinfo->i_extraPerms = le32_to_cpu(fe->permissions) & ~FE_MAPPED_PERMS;
1514c3367a1bSSteven J. Magnani
1515c03cad24SJan Kara read_unlock(&sbi->s_cred_lock);
1516c03cad24SJan Kara
1517bfe86848SMiklos Szeredi link_count = le16_to_cpu(fe->fileLinkCount);
15184071b913SJan Kara if (!link_count) {
15196174c2ebSJan Kara if (!hidden_inode) {
15204071b913SJan Kara ret = -ESTALE;
15214071b913SJan Kara goto out;
15224071b913SJan Kara }
15236174c2ebSJan Kara link_count = 1;
15246174c2ebSJan Kara }
1525bfe86848SMiklos Szeredi set_nlink(inode, link_count);
1526c03cad24SJan Kara
1527c03cad24SJan Kara inode->i_size = le64_to_cpu(fe->informationLength);
1528c03cad24SJan Kara iinfo->i_lenExtents = inode->i_size;
15291da177e4SLinus Torvalds
153048d6d8ffSMarcin Slusarz if (iinfo->i_efe == 0) {
15311da177e4SLinus Torvalds inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
15321da177e4SLinus Torvalds (inode->i_sb->s_blocksize_bits - 9);
15331da177e4SLinus Torvalds
1534c3b9cecdSArnd Bergmann udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime);
1535c3b9cecdSArnd Bergmann udf_disk_stamp_to_time(&inode->i_mtime, fe->modificationTime);
153659ad88f2SJeff Layton udf_disk_stamp_to_time(&ctime, fe->attrTime);
153759ad88f2SJeff Layton inode_set_ctime_to_ts(inode, ctime);
15381da177e4SLinus Torvalds
153948d6d8ffSMarcin Slusarz iinfo->i_unique = le64_to_cpu(fe->uniqueID);
154048d6d8ffSMarcin Slusarz iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
154148d6d8ffSMarcin Slusarz iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1542d5e2cf07SSteve Nickel iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1543ab9a3a73SSteven J. Magnani iinfo->i_streamdir = 0;
1544ab9a3a73SSteven J. Magnani iinfo->i_lenStreams = 0;
1545cb00ea35SCyrill Gorcunov } else {
15461da177e4SLinus Torvalds inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
15471da177e4SLinus Torvalds (inode->i_sb->s_blocksize_bits - 9);
15481da177e4SLinus Torvalds
1549c3b9cecdSArnd Bergmann udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime);
1550c3b9cecdSArnd Bergmann udf_disk_stamp_to_time(&inode->i_mtime, efe->modificationTime);
15510220eddaSDeepa Dinamani udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime);
155259ad88f2SJeff Layton udf_disk_stamp_to_time(&ctime, efe->attrTime);
155359ad88f2SJeff Layton inode_set_ctime_to_ts(inode, ctime);
15541da177e4SLinus Torvalds
155548d6d8ffSMarcin Slusarz iinfo->i_unique = le64_to_cpu(efe->uniqueID);
155648d6d8ffSMarcin Slusarz iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
155748d6d8ffSMarcin Slusarz iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1558d5e2cf07SSteve Nickel iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
1559ab9a3a73SSteven J. Magnani
1560ab9a3a73SSteven J. Magnani /* Named streams */
1561ab9a3a73SSteven J. Magnani iinfo->i_streamdir = (efe->streamDirectoryICB.extLength != 0);
1562ab9a3a73SSteven J. Magnani iinfo->i_locStreamdir =
1563ab9a3a73SSteven J. Magnani lelb_to_cpu(efe->streamDirectoryICB.extLocation);
1564ab9a3a73SSteven J. Magnani iinfo->i_lenStreams = le64_to_cpu(efe->objectSize);
1565ab9a3a73SSteven J. Magnani if (iinfo->i_lenStreams >= inode->i_size)
1566ab9a3a73SSteven J. Magnani iinfo->i_lenStreams -= inode->i_size;
1567ab9a3a73SSteven J. Magnani else
1568ab9a3a73SSteven J. Magnani iinfo->i_lenStreams = 0;
15691da177e4SLinus Torvalds }
1570470cca56SJan Kara inode->i_generation = iinfo->i_unique;
15711da177e4SLinus Torvalds
157223b133bdSJan Kara /*
157323b133bdSJan Kara * Sanity check length of allocation descriptors and extended attrs to
157423b133bdSJan Kara * avoid integer overflows
157523b133bdSJan Kara */
157623b133bdSJan Kara if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
157723b133bdSJan Kara goto out;
157823b133bdSJan Kara /* Now do exact checks */
157923b133bdSJan Kara if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
158023b133bdSJan Kara goto out;
1581e159332bSJan Kara /* Sanity checks for files in ICB so that we don't get confused later */
1582e159332bSJan Kara if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1583e159332bSJan Kara /*
1584e159332bSJan Kara * For file in ICB data is stored in allocation descriptor
1585e159332bSJan Kara * so sizes should match
1586e159332bSJan Kara */
1587e159332bSJan Kara if (iinfo->i_lenAlloc != inode->i_size)
1588e159332bSJan Kara goto out;
1589e159332bSJan Kara /* File in ICB has to fit in there... */
159079144954SJan Kara if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
1591e159332bSJan Kara goto out;
1592e159332bSJan Kara }
1593e159332bSJan Kara
1594cb00ea35SCyrill Gorcunov switch (fe->icbTag.fileType) {
15951da177e4SLinus Torvalds case ICBTAG_FILE_TYPE_DIRECTORY:
15961da177e4SLinus Torvalds inode->i_op = &udf_dir_inode_operations;
15971da177e4SLinus Torvalds inode->i_fop = &udf_dir_operations;
15981da177e4SLinus Torvalds inode->i_mode |= S_IFDIR;
1599d8c76e6fSDave Hansen inc_nlink(inode);
16001da177e4SLinus Torvalds break;
16011da177e4SLinus Torvalds case ICBTAG_FILE_TYPE_REALTIME:
16021da177e4SLinus Torvalds case ICBTAG_FILE_TYPE_REGULAR:
16031da177e4SLinus Torvalds case ICBTAG_FILE_TYPE_UNDEF:
1604742e1795SJan Kara case ICBTAG_FILE_TYPE_VAT20:
16051da177e4SLinus Torvalds inode->i_data.a_ops = &udf_aops;
16061da177e4SLinus Torvalds inode->i_op = &udf_file_inode_operations;
16071da177e4SLinus Torvalds inode->i_fop = &udf_file_operations;
16081da177e4SLinus Torvalds inode->i_mode |= S_IFREG;
16091da177e4SLinus Torvalds break;
16101da177e4SLinus Torvalds case ICBTAG_FILE_TYPE_BLOCK:
16111da177e4SLinus Torvalds inode->i_mode |= S_IFBLK;
16121da177e4SLinus Torvalds break;
16131da177e4SLinus Torvalds case ICBTAG_FILE_TYPE_CHAR:
16141da177e4SLinus Torvalds inode->i_mode |= S_IFCHR;
16151da177e4SLinus Torvalds break;
16161da177e4SLinus Torvalds case ICBTAG_FILE_TYPE_FIFO:
16171da177e4SLinus Torvalds init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
16181da177e4SLinus Torvalds break;
16191da177e4SLinus Torvalds case ICBTAG_FILE_TYPE_SOCKET:
16201da177e4SLinus Torvalds init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
16211da177e4SLinus Torvalds break;
16221da177e4SLinus Torvalds case ICBTAG_FILE_TYPE_SYMLINK:
16231da177e4SLinus Torvalds inode->i_data.a_ops = &udf_symlink_aops;
1624ad4d0532SJan Kara inode->i_op = &udf_symlink_inode_operations;
162521fc61c7SAl Viro inode_nohighmem(inode);
16266ff6b2b3SFabian Frederick inode->i_mode = S_IFLNK | 0777;
16271da177e4SLinus Torvalds break;
1628bfb257a5SJan Kara case ICBTAG_FILE_TYPE_MAIN:
1629bfb257a5SJan Kara udf_debug("METADATA FILE-----\n");
1630bfb257a5SJan Kara break;
1631bfb257a5SJan Kara case ICBTAG_FILE_TYPE_MIRROR:
1632bfb257a5SJan Kara udf_debug("METADATA MIRROR FILE-----\n");
1633bfb257a5SJan Kara break;
1634bfb257a5SJan Kara case ICBTAG_FILE_TYPE_BITMAP:
1635bfb257a5SJan Kara udf_debug("METADATA BITMAP FILE-----\n");
1636bfb257a5SJan Kara break;
16371da177e4SLinus Torvalds default:
1638fcbf7637SSteve Magnani udf_err(inode->i_sb, "(ino %lu) failed unknown file type=%u\n",
163978ace70cSJoe Perches inode->i_ino, fe->icbTag.fileType);
16406d3d5e86SJan Kara goto out;
16411da177e4SLinus Torvalds }
1642cb00ea35SCyrill Gorcunov if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
16434b11111aSMarcin Slusarz struct deviceSpec *dsea =
16444b11111aSMarcin Slusarz (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1645cb00ea35SCyrill Gorcunov if (dsea) {
1646cb00ea35SCyrill Gorcunov init_special_inode(inode, inode->i_mode,
164728de7948SCyrill Gorcunov MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
164828de7948SCyrill Gorcunov le32_to_cpu(dsea->minorDeviceIdent)));
16491da177e4SLinus Torvalds /* Developer ID ??? */
16504b11111aSMarcin Slusarz } else
16516d3d5e86SJan Kara goto out;
16521da177e4SLinus Torvalds }
16536d3d5e86SJan Kara ret = 0;
16546d3d5e86SJan Kara out:
1655bb7720a0SJan Kara brelse(bh);
16566d3d5e86SJan Kara return ret;
16571da177e4SLinus Torvalds }
16581da177e4SLinus Torvalds
udf_alloc_i_data(struct inode * inode,size_t size)1659647bd61aSCyrill Gorcunov static int udf_alloc_i_data(struct inode *inode, size_t size)
1660647bd61aSCyrill Gorcunov {
166148d6d8ffSMarcin Slusarz struct udf_inode_info *iinfo = UDF_I(inode);
1662382a2287SJan Kara iinfo->i_data = kmalloc(size, GFP_KERNEL);
1663382a2287SJan Kara if (!iinfo->i_data)
1664647bd61aSCyrill Gorcunov return -ENOMEM;
1665647bd61aSCyrill Gorcunov return 0;
1666647bd61aSCyrill Gorcunov }
1667647bd61aSCyrill Gorcunov
udf_convert_permissions(struct fileEntry * fe)1668faa17292SAl Viro static umode_t udf_convert_permissions(struct fileEntry *fe)
16691da177e4SLinus Torvalds {
1670faa17292SAl Viro umode_t mode;
16711da177e4SLinus Torvalds uint32_t permissions;
16721da177e4SLinus Torvalds uint32_t flags;
16731da177e4SLinus Torvalds
16741da177e4SLinus Torvalds permissions = le32_to_cpu(fe->permissions);
16751da177e4SLinus Torvalds flags = le16_to_cpu(fe->icbTag.flags);
16761da177e4SLinus Torvalds
16776ff6b2b3SFabian Frederick mode = ((permissions) & 0007) |
16786ff6b2b3SFabian Frederick ((permissions >> 2) & 0070) |
16796ff6b2b3SFabian Frederick ((permissions >> 4) & 0700) |
16801da177e4SLinus Torvalds ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
16811da177e4SLinus Torvalds ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
16821da177e4SLinus Torvalds ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
16831da177e4SLinus Torvalds
16841da177e4SLinus Torvalds return mode;
16851da177e4SLinus Torvalds }
16861da177e4SLinus Torvalds
udf_update_extra_perms(struct inode * inode,umode_t mode)1687c3367a1bSSteven J. Magnani void udf_update_extra_perms(struct inode *inode, umode_t mode)
1688c3367a1bSSteven J. Magnani {
1689c3367a1bSSteven J. Magnani struct udf_inode_info *iinfo = UDF_I(inode);
1690c3367a1bSSteven J. Magnani
1691c3367a1bSSteven J. Magnani /*
1692c3367a1bSSteven J. Magnani * UDF 2.01 sec. 3.3.3.3 Note 2:
1693c3367a1bSSteven J. Magnani * In Unix, delete permission tracks write
1694c3367a1bSSteven J. Magnani */
1695c3367a1bSSteven J. Magnani iinfo->i_extraPerms &= ~FE_DELETE_PERMS;
1696c3367a1bSSteven J. Magnani if (mode & 0200)
1697c3367a1bSSteven J. Magnani iinfo->i_extraPerms |= FE_PERM_U_DELETE;
1698c3367a1bSSteven J. Magnani if (mode & 0020)
1699c3367a1bSSteven J. Magnani iinfo->i_extraPerms |= FE_PERM_G_DELETE;
1700c3367a1bSSteven J. Magnani if (mode & 0002)
1701c3367a1bSSteven J. Magnani iinfo->i_extraPerms |= FE_PERM_O_DELETE;
1702c3367a1bSSteven J. Magnani }
1703c3367a1bSSteven J. Magnani
udf_write_inode(struct inode * inode,struct writeback_control * wbc)1704a9185b41SChristoph Hellwig int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
17051da177e4SLinus Torvalds {
170649521de1SJan Kara return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
17071da177e4SLinus Torvalds }
17081da177e4SLinus Torvalds
udf_sync_inode(struct inode * inode)170949521de1SJan Kara static int udf_sync_inode(struct inode *inode)
17101da177e4SLinus Torvalds {
17111da177e4SLinus Torvalds return udf_update_inode(inode, 1);
17121da177e4SLinus Torvalds }
17131da177e4SLinus Torvalds
udf_adjust_time(struct udf_inode_info * iinfo,struct timespec64 time)1714c3b9cecdSArnd Bergmann static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec64 time)
17157ed0fbd7SFabian Frederick {
17167ed0fbd7SFabian Frederick if (iinfo->i_crtime.tv_sec > time.tv_sec ||
17177ed0fbd7SFabian Frederick (iinfo->i_crtime.tv_sec == time.tv_sec &&
17187ed0fbd7SFabian Frederick iinfo->i_crtime.tv_nsec > time.tv_nsec))
17197ed0fbd7SFabian Frederick iinfo->i_crtime = time;
17207ed0fbd7SFabian Frederick }
17217ed0fbd7SFabian Frederick
udf_update_inode(struct inode * inode,int do_sync)1722cb00ea35SCyrill Gorcunov static int udf_update_inode(struct inode *inode, int do_sync)
17231da177e4SLinus Torvalds {
17241da177e4SLinus Torvalds struct buffer_head *bh = NULL;
17251da177e4SLinus Torvalds struct fileEntry *fe;
17261da177e4SLinus Torvalds struct extendedFileEntry *efe;
1727b2527bfaSSteve Nickel uint64_t lb_recorded;
17281da177e4SLinus Torvalds uint32_t udfperms;
17291da177e4SLinus Torvalds uint16_t icbflags;
17301da177e4SLinus Torvalds uint16_t crclen;
17311da177e4SLinus Torvalds int err = 0;
17326c79e987SMarcin Slusarz struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
17334b11111aSMarcin Slusarz unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
173448d6d8ffSMarcin Slusarz struct udf_inode_info *iinfo = UDF_I(inode);
17351da177e4SLinus Torvalds
1736101ee137SJan Kara bh = sb_getblk(inode->i_sb,
17375833ded9SJan Kara udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1738cb00ea35SCyrill Gorcunov if (!bh) {
1739aae917cdSJan Kara udf_debug("getblk failure\n");
17400fd2ba36SChangwoo Min return -EIO;
17411da177e4SLinus Torvalds }
17421da177e4SLinus Torvalds
1743aae917cdSJan Kara lock_buffer(bh);
1744aae917cdSJan Kara memset(bh->b_data, 0, inode->i_sb->s_blocksize);
17451da177e4SLinus Torvalds fe = (struct fileEntry *)bh->b_data;
17461da177e4SLinus Torvalds efe = (struct extendedFileEntry *)bh->b_data;
17471da177e4SLinus Torvalds
1748aae917cdSJan Kara if (iinfo->i_use) {
17491da177e4SLinus Torvalds struct unallocSpaceEntry *use =
17501da177e4SLinus Torvalds (struct unallocSpaceEntry *)bh->b_data;
17511da177e4SLinus Torvalds
175248d6d8ffSMarcin Slusarz use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
17534b11111aSMarcin Slusarz memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1754382a2287SJan Kara iinfo->i_data, inode->i_sb->s_blocksize -
17554b11111aSMarcin Slusarz sizeof(struct unallocSpaceEntry));
1756aae917cdSJan Kara use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
175770f19f58SSteven J. Magnani crclen = sizeof(struct unallocSpaceEntry);
17581da177e4SLinus Torvalds
175970f19f58SSteven J. Magnani goto finish;
17601da177e4SLinus Torvalds }
17611da177e4SLinus Torvalds
17624d6660ebSPhillip Susi if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
17630c9850f4SJan Kara fe->uid = cpu_to_le32(UDF_INVALID_ID);
1764cb00ea35SCyrill Gorcunov else
1765c2ba138aSEric W. Biederman fe->uid = cpu_to_le32(i_uid_read(inode));
17661da177e4SLinus Torvalds
17674d6660ebSPhillip Susi if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
17680c9850f4SJan Kara fe->gid = cpu_to_le32(UDF_INVALID_ID);
1769cb00ea35SCyrill Gorcunov else
1770c2ba138aSEric W. Biederman fe->gid = cpu_to_le32(i_gid_read(inode));
17711da177e4SLinus Torvalds
17726ff6b2b3SFabian Frederick udfperms = ((inode->i_mode & 0007)) |
17736ff6b2b3SFabian Frederick ((inode->i_mode & 0070) << 2) |
17746ff6b2b3SFabian Frederick ((inode->i_mode & 0700) << 4);
17751da177e4SLinus Torvalds
1776c3367a1bSSteven J. Magnani udfperms |= iinfo->i_extraPerms;
17771da177e4SLinus Torvalds fe->permissions = cpu_to_le32(udfperms);
17781da177e4SLinus Torvalds
17798a70ee33SJan Kara if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
17801da177e4SLinus Torvalds fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1781fc8033a3SJan Kara else {
1782fc8033a3SJan Kara if (iinfo->i_hidden)
1783fc8033a3SJan Kara fe->fileLinkCount = cpu_to_le16(0);
17841da177e4SLinus Torvalds else
17851da177e4SLinus Torvalds fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1786fc8033a3SJan Kara }
17871da177e4SLinus Torvalds
17881da177e4SLinus Torvalds fe->informationLength = cpu_to_le64(inode->i_size);
17891da177e4SLinus Torvalds
1790cb00ea35SCyrill Gorcunov if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
17915ca4e4beSPekka Enberg struct regid *eid;
179228de7948SCyrill Gorcunov struct deviceSpec *dsea =
179328de7948SCyrill Gorcunov (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1794cb00ea35SCyrill Gorcunov if (!dsea) {
17951da177e4SLinus Torvalds dsea = (struct deviceSpec *)
17961da177e4SLinus Torvalds udf_add_extendedattr(inode,
17971da177e4SLinus Torvalds sizeof(struct deviceSpec) +
17985ca4e4beSPekka Enberg sizeof(struct regid), 12, 0x3);
17991da177e4SLinus Torvalds dsea->attrType = cpu_to_le32(12);
18001da177e4SLinus Torvalds dsea->attrSubtype = 1;
18014b11111aSMarcin Slusarz dsea->attrLength = cpu_to_le32(
18024b11111aSMarcin Slusarz sizeof(struct deviceSpec) +
18035ca4e4beSPekka Enberg sizeof(struct regid));
18045ca4e4beSPekka Enberg dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
18051da177e4SLinus Torvalds }
18065ca4e4beSPekka Enberg eid = (struct regid *)dsea->impUse;
1807033c9da0SMarkus Elfring memset(eid, 0, sizeof(*eid));
18081da177e4SLinus Torvalds strcpy(eid->ident, UDF_ID_DEVELOPER);
18091da177e4SLinus Torvalds eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
18101da177e4SLinus Torvalds eid->identSuffix[1] = UDF_OS_ID_LINUX;
18111da177e4SLinus Torvalds dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
18121da177e4SLinus Torvalds dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
18131da177e4SLinus Torvalds }
18141da177e4SLinus Torvalds
1815b2527bfaSSteve Nickel if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1816b2527bfaSSteve Nickel lb_recorded = 0; /* No extents => no blocks! */
1817b2527bfaSSteve Nickel else
1818b2527bfaSSteve Nickel lb_recorded =
1819b2527bfaSSteve Nickel (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1820b2527bfaSSteve Nickel (blocksize_bits - 9);
1821b2527bfaSSteve Nickel
182248d6d8ffSMarcin Slusarz if (iinfo->i_efe == 0) {
1823c0b34438SMarcin Slusarz memcpy(bh->b_data + sizeof(struct fileEntry),
1824382a2287SJan Kara iinfo->i_data,
1825cb00ea35SCyrill Gorcunov inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1826b2527bfaSSteve Nickel fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
18271da177e4SLinus Torvalds
1828c3b9cecdSArnd Bergmann udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1829c3b9cecdSArnd Bergmann udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
183059ad88f2SJeff Layton udf_time_to_disk_stamp(&fe->attrTime, inode_get_ctime(inode));
18315ca4e4beSPekka Enberg memset(&(fe->impIdent), 0, sizeof(struct regid));
18321da177e4SLinus Torvalds strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
18331da177e4SLinus Torvalds fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
18341da177e4SLinus Torvalds fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
183548d6d8ffSMarcin Slusarz fe->uniqueID = cpu_to_le64(iinfo->i_unique);
183648d6d8ffSMarcin Slusarz fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
183748d6d8ffSMarcin Slusarz fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1838d5e2cf07SSteve Nickel fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
18391da177e4SLinus Torvalds fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
18401da177e4SLinus Torvalds crclen = sizeof(struct fileEntry);
1841cb00ea35SCyrill Gorcunov } else {
18424b11111aSMarcin Slusarz memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1843382a2287SJan Kara iinfo->i_data,
18444b11111aSMarcin Slusarz inode->i_sb->s_blocksize -
18454b11111aSMarcin Slusarz sizeof(struct extendedFileEntry));
1846ab9a3a73SSteven J. Magnani efe->objectSize =
1847ab9a3a73SSteven J. Magnani cpu_to_le64(inode->i_size + iinfo->i_lenStreams);
1848b2527bfaSSteve Nickel efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
18491da177e4SLinus Torvalds
1850ab9a3a73SSteven J. Magnani if (iinfo->i_streamdir) {
1851ab9a3a73SSteven J. Magnani struct long_ad *icb_lad = &efe->streamDirectoryICB;
1852ab9a3a73SSteven J. Magnani
1853ab9a3a73SSteven J. Magnani icb_lad->extLocation =
1854ab9a3a73SSteven J. Magnani cpu_to_lelb(iinfo->i_locStreamdir);
1855ab9a3a73SSteven J. Magnani icb_lad->extLength =
1856ab9a3a73SSteven J. Magnani cpu_to_le32(inode->i_sb->s_blocksize);
1857ab9a3a73SSteven J. Magnani }
1858ab9a3a73SSteven J. Magnani
1859c3b9cecdSArnd Bergmann udf_adjust_time(iinfo, inode->i_atime);
1860c3b9cecdSArnd Bergmann udf_adjust_time(iinfo, inode->i_mtime);
186159ad88f2SJeff Layton udf_adjust_time(iinfo, inode_get_ctime(inode));
18621da177e4SLinus Torvalds
1863c3b9cecdSArnd Bergmann udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1864c3b9cecdSArnd Bergmann udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
186556774805SMarcin Slusarz udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
186659ad88f2SJeff Layton udf_time_to_disk_stamp(&efe->attrTime, inode_get_ctime(inode));
18671da177e4SLinus Torvalds
1868033c9da0SMarkus Elfring memset(&(efe->impIdent), 0, sizeof(efe->impIdent));
18691da177e4SLinus Torvalds strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
18701da177e4SLinus Torvalds efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
18711da177e4SLinus Torvalds efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
187248d6d8ffSMarcin Slusarz efe->uniqueID = cpu_to_le64(iinfo->i_unique);
187348d6d8ffSMarcin Slusarz efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
187448d6d8ffSMarcin Slusarz efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1875d5e2cf07SSteve Nickel efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
18761da177e4SLinus Torvalds efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
18771da177e4SLinus Torvalds crclen = sizeof(struct extendedFileEntry);
18781da177e4SLinus Torvalds }
187970f19f58SSteven J. Magnani
188070f19f58SSteven J. Magnani finish:
188148d6d8ffSMarcin Slusarz if (iinfo->i_strat4096) {
18821da177e4SLinus Torvalds fe->icbTag.strategyType = cpu_to_le16(4096);
18831da177e4SLinus Torvalds fe->icbTag.strategyParameter = cpu_to_le16(1);
18841da177e4SLinus Torvalds fe->icbTag.numEntries = cpu_to_le16(2);
1885cb00ea35SCyrill Gorcunov } else {
18861da177e4SLinus Torvalds fe->icbTag.strategyType = cpu_to_le16(4);
18871da177e4SLinus Torvalds fe->icbTag.numEntries = cpu_to_le16(1);
18881da177e4SLinus Torvalds }
18891da177e4SLinus Torvalds
189070f19f58SSteven J. Magnani if (iinfo->i_use)
189170f19f58SSteven J. Magnani fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
189270f19f58SSteven J. Magnani else if (S_ISDIR(inode->i_mode))
18931da177e4SLinus Torvalds fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
18941da177e4SLinus Torvalds else if (S_ISREG(inode->i_mode))
18951da177e4SLinus Torvalds fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
18961da177e4SLinus Torvalds else if (S_ISLNK(inode->i_mode))
18971da177e4SLinus Torvalds fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
18981da177e4SLinus Torvalds else if (S_ISBLK(inode->i_mode))
18991da177e4SLinus Torvalds fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
19001da177e4SLinus Torvalds else if (S_ISCHR(inode->i_mode))
19011da177e4SLinus Torvalds fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
19021da177e4SLinus Torvalds else if (S_ISFIFO(inode->i_mode))
19031da177e4SLinus Torvalds fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
19041da177e4SLinus Torvalds else if (S_ISSOCK(inode->i_mode))
19051da177e4SLinus Torvalds fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
19061da177e4SLinus Torvalds
190748d6d8ffSMarcin Slusarz icbflags = iinfo->i_alloc_type |
19081da177e4SLinus Torvalds ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
19091da177e4SLinus Torvalds ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
19101da177e4SLinus Torvalds ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
19111da177e4SLinus Torvalds (le16_to_cpu(fe->icbTag.flags) &
19121da177e4SLinus Torvalds ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
19131da177e4SLinus Torvalds ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
19141da177e4SLinus Torvalds
19151da177e4SLinus Torvalds fe->icbTag.flags = cpu_to_le16(icbflags);
19166c79e987SMarcin Slusarz if (sbi->s_udfrev >= 0x0200)
19171da177e4SLinus Torvalds fe->descTag.descVersion = cpu_to_le16(3);
19181da177e4SLinus Torvalds else
19191da177e4SLinus Torvalds fe->descTag.descVersion = cpu_to_le16(2);
19206c79e987SMarcin Slusarz fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
19214b11111aSMarcin Slusarz fe->descTag.tagLocation = cpu_to_le32(
192248d6d8ffSMarcin Slusarz iinfo->i_location.logicalBlockNum);
1923aae917cdSJan Kara crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
19241da177e4SLinus Torvalds fe->descTag.descCRCLength = cpu_to_le16(crclen);
19255ca4e4beSPekka Enberg fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1926f845fcedSBob Copeland crclen));
19273f2587bbSMarcin Slusarz fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
19281da177e4SLinus Torvalds
19295833ded9SJan Kara set_buffer_uptodate(bh);
1930aae917cdSJan Kara unlock_buffer(bh);
1931aae917cdSJan Kara
19321da177e4SLinus Torvalds /* write the data blocks */
19331da177e4SLinus Torvalds mark_buffer_dirty(bh);
1934cb00ea35SCyrill Gorcunov if (do_sync) {
19351da177e4SLinus Torvalds sync_dirty_buffer(bh);
1936aae917cdSJan Kara if (buffer_write_io_error(bh)) {
193778ace70cSJoe Perches udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
19384b11111aSMarcin Slusarz inode->i_ino);
19391da177e4SLinus Torvalds err = -EIO;
19401da177e4SLinus Torvalds }
19411da177e4SLinus Torvalds }
19423bf25cb4SJan Kara brelse(bh);
194328de7948SCyrill Gorcunov
19441da177e4SLinus Torvalds return err;
19451da177e4SLinus Torvalds }
19461da177e4SLinus Torvalds
__udf_iget(struct super_block * sb,struct kernel_lb_addr * ino,bool hidden_inode)19476174c2ebSJan Kara struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
19486174c2ebSJan Kara bool hidden_inode)
19491da177e4SLinus Torvalds {
19501da177e4SLinus Torvalds unsigned long block = udf_get_lb_pblock(sb, ino, 0);
19511da177e4SLinus Torvalds struct inode *inode = iget_locked(sb, block);
19526d3d5e86SJan Kara int err;
19531da177e4SLinus Torvalds
19541da177e4SLinus Torvalds if (!inode)
19556d3d5e86SJan Kara return ERR_PTR(-ENOMEM);
19561da177e4SLinus Torvalds
195785a37983SJan Kara if (!(inode->i_state & I_NEW)) {
195885a37983SJan Kara if (UDF_I(inode)->i_hidden != hidden_inode) {
195985a37983SJan Kara iput(inode);
196085a37983SJan Kara return ERR_PTR(-EFSCORRUPTED);
196185a37983SJan Kara }
19621da177e4SLinus Torvalds return inode;
196385a37983SJan Kara }
19641da177e4SLinus Torvalds
19656d3d5e86SJan Kara memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
19666174c2ebSJan Kara err = udf_read_inode(inode, hidden_inode);
19676d3d5e86SJan Kara if (err < 0) {
19686d3d5e86SJan Kara iget_failed(inode);
19696d3d5e86SJan Kara return ERR_PTR(err);
19706d3d5e86SJan Kara }
19716d3d5e86SJan Kara unlock_new_inode(inode);
19726d3d5e86SJan Kara
19736d3d5e86SJan Kara return inode;
19741da177e4SLinus Torvalds }
19751da177e4SLinus Torvalds
udf_setup_indirect_aext(struct inode * inode,udf_pblk_t block,struct extent_position * epos)1976b490bdd6SSteve Magnani int udf_setup_indirect_aext(struct inode *inode, udf_pblk_t block,
1977fcea62baSJan Kara struct extent_position *epos)
1978fcea62baSJan Kara {
1979fcea62baSJan Kara struct super_block *sb = inode->i_sb;
1980fcea62baSJan Kara struct buffer_head *bh;
1981fcea62baSJan Kara struct allocExtDesc *aed;
1982fcea62baSJan Kara struct extent_position nepos;
1983fcea62baSJan Kara struct kernel_lb_addr neloc;
1984fcea62baSJan Kara int ver, adsize;
1985402c8862SZhao Mengmeng int err = 0;
1986fcea62baSJan Kara
1987fcea62baSJan Kara if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1988fcea62baSJan Kara adsize = sizeof(struct short_ad);
1989fcea62baSJan Kara else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1990fcea62baSJan Kara adsize = sizeof(struct long_ad);
19914f1b1519SArnd Bergmann else
19924f1b1519SArnd Bergmann return -EIO;
1993fcea62baSJan Kara
1994fcea62baSJan Kara neloc.logicalBlockNum = block;
1995fcea62baSJan Kara neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1996fcea62baSJan Kara
1997101ee137SJan Kara bh = sb_getblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1998fcea62baSJan Kara if (!bh)
1999fcea62baSJan Kara return -EIO;
2000fcea62baSJan Kara lock_buffer(bh);
2001fcea62baSJan Kara memset(bh->b_data, 0x00, sb->s_blocksize);
2002fcea62baSJan Kara set_buffer_uptodate(bh);
2003fcea62baSJan Kara unlock_buffer(bh);
2004fcea62baSJan Kara mark_buffer_dirty_inode(bh, inode);
2005fcea62baSJan Kara
2006fcea62baSJan Kara aed = (struct allocExtDesc *)(bh->b_data);
2007fcea62baSJan Kara if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
2008fcea62baSJan Kara aed->previousAllocExtLocation =
2009fcea62baSJan Kara cpu_to_le32(epos->block.logicalBlockNum);
2010fcea62baSJan Kara }
2011fcea62baSJan Kara aed->lengthAllocDescs = cpu_to_le32(0);
2012fcea62baSJan Kara if (UDF_SB(sb)->s_udfrev >= 0x0200)
2013fcea62baSJan Kara ver = 3;
2014fcea62baSJan Kara else
2015fcea62baSJan Kara ver = 2;
2016fcea62baSJan Kara udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
2017fcea62baSJan Kara sizeof(struct tag));
2018fcea62baSJan Kara
2019fcea62baSJan Kara nepos.block = neloc;
2020fcea62baSJan Kara nepos.offset = sizeof(struct allocExtDesc);
2021fcea62baSJan Kara nepos.bh = bh;
2022fcea62baSJan Kara
2023fcea62baSJan Kara /*
2024fcea62baSJan Kara * Do we have to copy current last extent to make space for indirect
2025fcea62baSJan Kara * one?
2026fcea62baSJan Kara */
2027fcea62baSJan Kara if (epos->offset + adsize > sb->s_blocksize) {
2028fcea62baSJan Kara struct kernel_lb_addr cp_loc;
2029fcea62baSJan Kara uint32_t cp_len;
2030402c8862SZhao Mengmeng int8_t cp_type;
2031fcea62baSJan Kara
2032fcea62baSJan Kara epos->offset -= adsize;
2033402c8862SZhao Mengmeng err = udf_current_aext(inode, epos, &cp_loc, &cp_len, &cp_type, 0);
2034402c8862SZhao Mengmeng if (err <= 0)
2035402c8862SZhao Mengmeng goto err_out;
2036fcea62baSJan Kara cp_len |= ((uint32_t)cp_type) << 30;
2037fcea62baSJan Kara
2038fcea62baSJan Kara __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
2039fcea62baSJan Kara udf_write_aext(inode, epos, &nepos.block,
2040800552ceSPali Rohár sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
2041fcea62baSJan Kara } else {
2042fcea62baSJan Kara __udf_add_aext(inode, epos, &nepos.block,
2043800552ceSPali Rohár sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
2044fcea62baSJan Kara }
2045fcea62baSJan Kara
2046fcea62baSJan Kara brelse(epos->bh);
2047fcea62baSJan Kara *epos = nepos;
2048fcea62baSJan Kara
2049fcea62baSJan Kara return 0;
2050402c8862SZhao Mengmeng err_out:
2051402c8862SZhao Mengmeng brelse(bh);
2052402c8862SZhao Mengmeng return err;
2053fcea62baSJan Kara }
2054fcea62baSJan Kara
2055fcea62baSJan Kara /*
2056fcea62baSJan Kara * Append extent at the given position - should be the first free one in inode
2057fcea62baSJan Kara * / indirect extent. This function assumes there is enough space in the inode
2058fcea62baSJan Kara * or indirect extent. Use udf_add_aext() if you didn't check for this before.
2059fcea62baSJan Kara */
__udf_add_aext(struct inode * inode,struct extent_position * epos,struct kernel_lb_addr * eloc,uint32_t elen,int inc)2060fcea62baSJan Kara int __udf_add_aext(struct inode *inode, struct extent_position *epos,
206197e961fdSPekka Enberg struct kernel_lb_addr *eloc, uint32_t elen, int inc)
20621da177e4SLinus Torvalds {
206348d6d8ffSMarcin Slusarz struct udf_inode_info *iinfo = UDF_I(inode);
2064fcea62baSJan Kara struct allocExtDesc *aed;
2065fcea62baSJan Kara int adsize;
20661da177e4SLinus Torvalds
206748d6d8ffSMarcin Slusarz if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
20685ca4e4beSPekka Enberg adsize = sizeof(struct short_ad);
206948d6d8ffSMarcin Slusarz else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
20705ca4e4beSPekka Enberg adsize = sizeof(struct long_ad);
20711da177e4SLinus Torvalds else
20727e49b6f2SJan Kara return -EIO;
20731da177e4SLinus Torvalds
2074fcea62baSJan Kara if (!epos->bh) {
2075fcea62baSJan Kara WARN_ON(iinfo->i_lenAlloc !=
2076fcea62baSJan Kara epos->offset - udf_file_entry_alloc_offset(inode));
2077cb00ea35SCyrill Gorcunov } else {
2078ff116fc8SJan Kara aed = (struct allocExtDesc *)epos->bh->b_data;
2079fcea62baSJan Kara WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
2080fcea62baSJan Kara epos->offset - sizeof(struct allocExtDesc));
2081fcea62baSJan Kara WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
20821da177e4SLinus Torvalds }
20831da177e4SLinus Torvalds
20847e49b6f2SJan Kara udf_write_aext(inode, epos, eloc, elen, inc);
20851da177e4SLinus Torvalds
2086cb00ea35SCyrill Gorcunov if (!epos->bh) {
208748d6d8ffSMarcin Slusarz iinfo->i_lenAlloc += adsize;
20881da177e4SLinus Torvalds mark_inode_dirty(inode);
2089cb00ea35SCyrill Gorcunov } else {
2090ff116fc8SJan Kara aed = (struct allocExtDesc *)epos->bh->b_data;
2091c2104fdaSmarcin.slusarz@gmail.com le32_add_cpu(&aed->lengthAllocDescs, adsize);
20924b11111aSMarcin Slusarz if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
20934b11111aSMarcin Slusarz UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
20944b11111aSMarcin Slusarz udf_update_tag(epos->bh->b_data,
20954b11111aSMarcin Slusarz epos->offset + (inc ? 0 : adsize));
20961da177e4SLinus Torvalds else
20974b11111aSMarcin Slusarz udf_update_tag(epos->bh->b_data,
20984b11111aSMarcin Slusarz sizeof(struct allocExtDesc));
2099ff116fc8SJan Kara mark_buffer_dirty_inode(epos->bh, inode);
21001da177e4SLinus Torvalds }
21011da177e4SLinus Torvalds
21027e49b6f2SJan Kara return 0;
21031da177e4SLinus Torvalds }
21041da177e4SLinus Torvalds
2105fcea62baSJan Kara /*
2106fcea62baSJan Kara * Append extent at given position - should be the first free one in inode
2107fcea62baSJan Kara * / indirect extent. Takes care of allocating and linking indirect blocks.
2108fcea62baSJan Kara */
udf_add_aext(struct inode * inode,struct extent_position * epos,struct kernel_lb_addr * eloc,uint32_t elen,int inc)2109fcea62baSJan Kara int udf_add_aext(struct inode *inode, struct extent_position *epos,
2110fcea62baSJan Kara struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2111fcea62baSJan Kara {
2112fcea62baSJan Kara int adsize;
2113fcea62baSJan Kara struct super_block *sb = inode->i_sb;
2114fcea62baSJan Kara
2115fcea62baSJan Kara if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2116fcea62baSJan Kara adsize = sizeof(struct short_ad);
2117fcea62baSJan Kara else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2118fcea62baSJan Kara adsize = sizeof(struct long_ad);
2119fcea62baSJan Kara else
2120fcea62baSJan Kara return -EIO;
2121fcea62baSJan Kara
2122fcea62baSJan Kara if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2123fcea62baSJan Kara int err;
2124b490bdd6SSteve Magnani udf_pblk_t new_block;
2125fcea62baSJan Kara
2126fcea62baSJan Kara new_block = udf_new_block(sb, NULL,
2127fcea62baSJan Kara epos->block.partitionReferenceNum,
2128fcea62baSJan Kara epos->block.logicalBlockNum, &err);
2129fcea62baSJan Kara if (!new_block)
2130fcea62baSJan Kara return -ENOSPC;
2131fcea62baSJan Kara
2132fcea62baSJan Kara err = udf_setup_indirect_aext(inode, new_block, epos);
2133fcea62baSJan Kara if (err)
2134fcea62baSJan Kara return err;
2135fcea62baSJan Kara }
2136fcea62baSJan Kara
2137fcea62baSJan Kara return __udf_add_aext(inode, epos, eloc, elen, inc);
2138fcea62baSJan Kara }
2139fcea62baSJan Kara
udf_write_aext(struct inode * inode,struct extent_position * epos,struct kernel_lb_addr * eloc,uint32_t elen,int inc)21407e49b6f2SJan Kara void udf_write_aext(struct inode *inode, struct extent_position *epos,
214197e961fdSPekka Enberg struct kernel_lb_addr *eloc, uint32_t elen, int inc)
21421da177e4SLinus Torvalds {
21431da177e4SLinus Torvalds int adsize;
21441da177e4SLinus Torvalds uint8_t *ptr;
21455ca4e4beSPekka Enberg struct short_ad *sad;
21465ca4e4beSPekka Enberg struct long_ad *lad;
214748d6d8ffSMarcin Slusarz struct udf_inode_info *iinfo = UDF_I(inode);
21481da177e4SLinus Torvalds
2149ff116fc8SJan Kara if (!epos->bh)
2150382a2287SJan Kara ptr = iinfo->i_data + epos->offset -
21514b11111aSMarcin Slusarz udf_file_entry_alloc_offset(inode) +
215248d6d8ffSMarcin Slusarz iinfo->i_lenEAttr;
21531da177e4SLinus Torvalds else
2154ff116fc8SJan Kara ptr = epos->bh->b_data + epos->offset;
21551da177e4SLinus Torvalds
215648d6d8ffSMarcin Slusarz switch (iinfo->i_alloc_type) {
21571da177e4SLinus Torvalds case ICBTAG_FLAG_AD_SHORT:
21585ca4e4beSPekka Enberg sad = (struct short_ad *)ptr;
21591da177e4SLinus Torvalds sad->extLength = cpu_to_le32(elen);
216097e961fdSPekka Enberg sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
21615ca4e4beSPekka Enberg adsize = sizeof(struct short_ad);
21621da177e4SLinus Torvalds break;
21631da177e4SLinus Torvalds case ICBTAG_FLAG_AD_LONG:
21645ca4e4beSPekka Enberg lad = (struct long_ad *)ptr;
21651da177e4SLinus Torvalds lad->extLength = cpu_to_le32(elen);
216697e961fdSPekka Enberg lad->extLocation = cpu_to_lelb(*eloc);
21671da177e4SLinus Torvalds memset(lad->impUse, 0x00, sizeof(lad->impUse));
21685ca4e4beSPekka Enberg adsize = sizeof(struct long_ad);
21691da177e4SLinus Torvalds break;
21701da177e4SLinus Torvalds default:
21717e49b6f2SJan Kara return;
21721da177e4SLinus Torvalds }
21731da177e4SLinus Torvalds
2174cb00ea35SCyrill Gorcunov if (epos->bh) {
217528de7948SCyrill Gorcunov if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
21766c79e987SMarcin Slusarz UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
21774b11111aSMarcin Slusarz struct allocExtDesc *aed =
21784b11111aSMarcin Slusarz (struct allocExtDesc *)epos->bh->b_data;
2179ff116fc8SJan Kara udf_update_tag(epos->bh->b_data,
21804b11111aSMarcin Slusarz le32_to_cpu(aed->lengthAllocDescs) +
21814b11111aSMarcin Slusarz sizeof(struct allocExtDesc));
21821da177e4SLinus Torvalds }
2183ff116fc8SJan Kara mark_buffer_dirty_inode(epos->bh, inode);
218428de7948SCyrill Gorcunov } else {
21851da177e4SLinus Torvalds mark_inode_dirty(inode);
218628de7948SCyrill Gorcunov }
21871da177e4SLinus Torvalds
21881da177e4SLinus Torvalds if (inc)
2189ff116fc8SJan Kara epos->offset += adsize;
21901da177e4SLinus Torvalds }
21911da177e4SLinus Torvalds
2192b0918d9fSVegard Nossum /*
2193b0918d9fSVegard Nossum * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2194b0918d9fSVegard Nossum * someone does some weird stuff.
2195b0918d9fSVegard Nossum */
2196b0918d9fSVegard Nossum #define UDF_MAX_INDIR_EXTS 16
2197b0918d9fSVegard Nossum
21985fc8da4dSZhao Mengmeng /*
21995fc8da4dSZhao Mengmeng * Returns 1 on success, -errno on error, 0 on hit EOF.
22005fc8da4dSZhao Mengmeng */
udf_next_aext(struct inode * inode,struct extent_position * epos,struct kernel_lb_addr * eloc,uint32_t * elen,int8_t * etype,int inc)22015fc8da4dSZhao Mengmeng int udf_next_aext(struct inode *inode, struct extent_position *epos,
22025fc8da4dSZhao Mengmeng struct kernel_lb_addr *eloc, uint32_t *elen, int8_t *etype,
22035fc8da4dSZhao Mengmeng int inc)
22041da177e4SLinus Torvalds {
2205b0918d9fSVegard Nossum unsigned int indirections = 0;
2206402c8862SZhao Mengmeng int ret = 0;
2207b490bdd6SSteve Magnani udf_pblk_t block;
2208b0918d9fSVegard Nossum
22095fc8da4dSZhao Mengmeng while (1) {
22105fc8da4dSZhao Mengmeng ret = udf_current_aext(inode, epos, eloc, elen,
22115fc8da4dSZhao Mengmeng etype, inc);
22125fc8da4dSZhao Mengmeng if (ret <= 0)
22135fc8da4dSZhao Mengmeng return ret;
22145fc8da4dSZhao Mengmeng if (*etype != (EXT_NEXT_EXTENT_ALLOCDESCS >> 30))
22155fc8da4dSZhao Mengmeng return ret;
22165fc8da4dSZhao Mengmeng
2217b0918d9fSVegard Nossum if (++indirections > UDF_MAX_INDIR_EXTS) {
2218b0918d9fSVegard Nossum udf_err(inode->i_sb,
2219b0918d9fSVegard Nossum "too many indirect extents in inode %lu\n",
2220b0918d9fSVegard Nossum inode->i_ino);
22215fc8da4dSZhao Mengmeng return -EFSCORRUPTED;
2222b0918d9fSVegard Nossum }
2223b0918d9fSVegard Nossum
2224ff116fc8SJan Kara epos->block = *eloc;
2225ff116fc8SJan Kara epos->offset = sizeof(struct allocExtDesc);
22263bf25cb4SJan Kara brelse(epos->bh);
222797e961fdSPekka Enberg block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
2228101ee137SJan Kara epos->bh = sb_bread(inode->i_sb, block);
22294b11111aSMarcin Slusarz if (!epos->bh) {
2230fcbf7637SSteve Magnani udf_debug("reading block %u failed!\n", block);
22315fc8da4dSZhao Mengmeng return -EIO;
22321da177e4SLinus Torvalds }
22331da177e4SLinus Torvalds }
22341da177e4SLinus Torvalds }
22351da177e4SLinus Torvalds
2236402c8862SZhao Mengmeng /*
2237402c8862SZhao Mengmeng * Returns 1 on success, -errno on error, 0 on hit EOF.
2238402c8862SZhao Mengmeng */
udf_current_aext(struct inode * inode,struct extent_position * epos,struct kernel_lb_addr * eloc,uint32_t * elen,int8_t * etype,int inc)2239402c8862SZhao Mengmeng int udf_current_aext(struct inode *inode, struct extent_position *epos,
2240402c8862SZhao Mengmeng struct kernel_lb_addr *eloc, uint32_t *elen, int8_t *etype,
2241402c8862SZhao Mengmeng int inc)
22421da177e4SLinus Torvalds {
22431da177e4SLinus Torvalds int alen;
22441da177e4SLinus Torvalds uint8_t *ptr;
22455ca4e4beSPekka Enberg struct short_ad *sad;
22465ca4e4beSPekka Enberg struct long_ad *lad;
224748d6d8ffSMarcin Slusarz struct udf_inode_info *iinfo = UDF_I(inode);
224828de7948SCyrill Gorcunov
2249cb00ea35SCyrill Gorcunov if (!epos->bh) {
2250ff116fc8SJan Kara if (!epos->offset)
2251ff116fc8SJan Kara epos->offset = udf_file_entry_alloc_offset(inode);
2252382a2287SJan Kara ptr = iinfo->i_data + epos->offset -
22534b11111aSMarcin Slusarz udf_file_entry_alloc_offset(inode) +
225448d6d8ffSMarcin Slusarz iinfo->i_lenEAttr;
22554b11111aSMarcin Slusarz alen = udf_file_entry_alloc_offset(inode) +
225648d6d8ffSMarcin Slusarz iinfo->i_lenAlloc;
2257cb00ea35SCyrill Gorcunov } else {
2258*1ac49babSGianfranco Trad struct allocExtDesc *header =
2259*1ac49babSGianfranco Trad (struct allocExtDesc *)epos->bh->b_data;
2260*1ac49babSGianfranco Trad
2261ff116fc8SJan Kara if (!epos->offset)
2262ff116fc8SJan Kara epos->offset = sizeof(struct allocExtDesc);
2263ff116fc8SJan Kara ptr = epos->bh->b_data + epos->offset;
2264*1ac49babSGianfranco Trad if (check_add_overflow(sizeof(struct allocExtDesc),
2265*1ac49babSGianfranco Trad le32_to_cpu(header->lengthAllocDescs), &alen))
2266*1ac49babSGianfranco Trad return -1;
22671da177e4SLinus Torvalds }
22681da177e4SLinus Torvalds
226948d6d8ffSMarcin Slusarz switch (iinfo->i_alloc_type) {
22701da177e4SLinus Torvalds case ICBTAG_FLAG_AD_SHORT:
22714b11111aSMarcin Slusarz sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
22724b11111aSMarcin Slusarz if (!sad)
2273402c8862SZhao Mengmeng return 0;
2274402c8862SZhao Mengmeng *etype = le32_to_cpu(sad->extLength) >> 30;
22751da177e4SLinus Torvalds eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
22764b11111aSMarcin Slusarz eloc->partitionReferenceNum =
227748d6d8ffSMarcin Slusarz iinfo->i_location.partitionReferenceNum;
227828de7948SCyrill Gorcunov *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
22791da177e4SLinus Torvalds break;
22801da177e4SLinus Torvalds case ICBTAG_FLAG_AD_LONG:
22814b11111aSMarcin Slusarz lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
22824b11111aSMarcin Slusarz if (!lad)
2283402c8862SZhao Mengmeng return 0;
2284402c8862SZhao Mengmeng *etype = le32_to_cpu(lad->extLength) >> 30;
22851da177e4SLinus Torvalds *eloc = lelb_to_cpu(lad->extLocation);
228628de7948SCyrill Gorcunov *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
22871da177e4SLinus Torvalds break;
22881da177e4SLinus Torvalds default:
2289fcbf7637SSteve Magnani udf_debug("alloc_type = %u unsupported\n", iinfo->i_alloc_type);
2290402c8862SZhao Mengmeng return -EINVAL;
22911da177e4SLinus Torvalds }
22921da177e4SLinus Torvalds
2293402c8862SZhao Mengmeng return 1;
22941da177e4SLinus Torvalds }
22951da177e4SLinus Torvalds
udf_insert_aext(struct inode * inode,struct extent_position epos,struct kernel_lb_addr neloc,uint32_t nelen)229619fd80deSJan Kara static int udf_insert_aext(struct inode *inode, struct extent_position epos,
22975ca4e4beSPekka Enberg struct kernel_lb_addr neloc, uint32_t nelen)
22981da177e4SLinus Torvalds {
22995ca4e4beSPekka Enberg struct kernel_lb_addr oeloc;
23001da177e4SLinus Torvalds uint32_t oelen;
23011da177e4SLinus Torvalds int8_t etype;
23025fc8da4dSZhao Mengmeng int ret;
23031da177e4SLinus Torvalds
2304ff116fc8SJan Kara if (epos.bh)
23053bf25cb4SJan Kara get_bh(epos.bh);
23061da177e4SLinus Torvalds
23075fc8da4dSZhao Mengmeng while (1) {
23085fc8da4dSZhao Mengmeng ret = udf_next_aext(inode, &epos, &oeloc, &oelen, &etype, 0);
23095fc8da4dSZhao Mengmeng if (ret <= 0)
23105fc8da4dSZhao Mengmeng break;
231197e961fdSPekka Enberg udf_write_aext(inode, &epos, &neloc, nelen, 1);
23121da177e4SLinus Torvalds neloc = oeloc;
23131da177e4SLinus Torvalds nelen = (etype << 30) | oelen;
23141da177e4SLinus Torvalds }
23155fc8da4dSZhao Mengmeng if (ret == 0)
23165fc8da4dSZhao Mengmeng ret = udf_add_aext(inode, &epos, &neloc, nelen, 1);
23173bf25cb4SJan Kara brelse(epos.bh);
231828de7948SCyrill Gorcunov
23195fc8da4dSZhao Mengmeng return ret;
23201da177e4SLinus Torvalds }
23211da177e4SLinus Torvalds
udf_delete_aext(struct inode * inode,struct extent_position epos)23226c1e4d06SJan Kara int8_t udf_delete_aext(struct inode *inode, struct extent_position epos)
23231da177e4SLinus Torvalds {
2324ff116fc8SJan Kara struct extent_position oepos;
2325ff116fc8SJan Kara int adsize;
23261da177e4SLinus Torvalds int8_t etype;
23271da177e4SLinus Torvalds struct allocExtDesc *aed;
232848d6d8ffSMarcin Slusarz struct udf_inode_info *iinfo;
23296c1e4d06SJan Kara struct kernel_lb_addr eloc;
23306c1e4d06SJan Kara uint32_t elen;
23315fc8da4dSZhao Mengmeng int ret;
23321da177e4SLinus Torvalds
2333cb00ea35SCyrill Gorcunov if (epos.bh) {
23343bf25cb4SJan Kara get_bh(epos.bh);
23353bf25cb4SJan Kara get_bh(epos.bh);
23361da177e4SLinus Torvalds }
23371da177e4SLinus Torvalds
233848d6d8ffSMarcin Slusarz iinfo = UDF_I(inode);
233948d6d8ffSMarcin Slusarz if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
23405ca4e4beSPekka Enberg adsize = sizeof(struct short_ad);
234148d6d8ffSMarcin Slusarz else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
23425ca4e4beSPekka Enberg adsize = sizeof(struct long_ad);
23431da177e4SLinus Torvalds else
23441da177e4SLinus Torvalds adsize = 0;
23451da177e4SLinus Torvalds
2346ff116fc8SJan Kara oepos = epos;
23475fc8da4dSZhao Mengmeng if (udf_next_aext(inode, &epos, &eloc, &elen, &etype, 1) <= 0)
23481da177e4SLinus Torvalds return -1;
23491da177e4SLinus Torvalds
23505fc8da4dSZhao Mengmeng while (1) {
23515fc8da4dSZhao Mengmeng ret = udf_next_aext(inode, &epos, &eloc, &elen, &etype, 1);
23525fc8da4dSZhao Mengmeng if (ret < 0) {
23535fc8da4dSZhao Mengmeng brelse(epos.bh);
23545fc8da4dSZhao Mengmeng brelse(oepos.bh);
23555fc8da4dSZhao Mengmeng return -1;
23565fc8da4dSZhao Mengmeng }
23575fc8da4dSZhao Mengmeng if (ret == 0)
23585fc8da4dSZhao Mengmeng break;
235997e961fdSPekka Enberg udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2360cb00ea35SCyrill Gorcunov if (oepos.bh != epos.bh) {
2361ff116fc8SJan Kara oepos.block = epos.block;
23623bf25cb4SJan Kara brelse(oepos.bh);
23633bf25cb4SJan Kara get_bh(epos.bh);
2364ff116fc8SJan Kara oepos.bh = epos.bh;
2365ff116fc8SJan Kara oepos.offset = epos.offset - adsize;
23661da177e4SLinus Torvalds }
23671da177e4SLinus Torvalds }
23685ca4e4beSPekka Enberg memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
23691da177e4SLinus Torvalds elen = 0;
23701da177e4SLinus Torvalds
2371cb00ea35SCyrill Gorcunov if (epos.bh != oepos.bh) {
237297e961fdSPekka Enberg udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
237397e961fdSPekka Enberg udf_write_aext(inode, &oepos, &eloc, elen, 1);
237497e961fdSPekka Enberg udf_write_aext(inode, &oepos, &eloc, elen, 1);
2375cb00ea35SCyrill Gorcunov if (!oepos.bh) {
237648d6d8ffSMarcin Slusarz iinfo->i_lenAlloc -= (adsize * 2);
23771da177e4SLinus Torvalds mark_inode_dirty(inode);
2378cb00ea35SCyrill Gorcunov } else {
2379ff116fc8SJan Kara aed = (struct allocExtDesc *)oepos.bh->b_data;
2380c2104fdaSmarcin.slusarz@gmail.com le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
238128de7948SCyrill Gorcunov if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
23826c79e987SMarcin Slusarz UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
23834b11111aSMarcin Slusarz udf_update_tag(oepos.bh->b_data,
23844b11111aSMarcin Slusarz oepos.offset - (2 * adsize));
23851da177e4SLinus Torvalds else
23864b11111aSMarcin Slusarz udf_update_tag(oepos.bh->b_data,
23874b11111aSMarcin Slusarz sizeof(struct allocExtDesc));
2388ff116fc8SJan Kara mark_buffer_dirty_inode(oepos.bh, inode);
23891da177e4SLinus Torvalds }
2390cb00ea35SCyrill Gorcunov } else {
239197e961fdSPekka Enberg udf_write_aext(inode, &oepos, &eloc, elen, 1);
2392cb00ea35SCyrill Gorcunov if (!oepos.bh) {
239348d6d8ffSMarcin Slusarz iinfo->i_lenAlloc -= adsize;
23941da177e4SLinus Torvalds mark_inode_dirty(inode);
2395cb00ea35SCyrill Gorcunov } else {
2396ff116fc8SJan Kara aed = (struct allocExtDesc *)oepos.bh->b_data;
2397c2104fdaSmarcin.slusarz@gmail.com le32_add_cpu(&aed->lengthAllocDescs, -adsize);
239828de7948SCyrill Gorcunov if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
23996c79e987SMarcin Slusarz UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
24004b11111aSMarcin Slusarz udf_update_tag(oepos.bh->b_data,
24014b11111aSMarcin Slusarz epos.offset - adsize);
24021da177e4SLinus Torvalds else
24034b11111aSMarcin Slusarz udf_update_tag(oepos.bh->b_data,
24044b11111aSMarcin Slusarz sizeof(struct allocExtDesc));
2405ff116fc8SJan Kara mark_buffer_dirty_inode(oepos.bh, inode);
24061da177e4SLinus Torvalds }
24071da177e4SLinus Torvalds }
24081da177e4SLinus Torvalds
24093bf25cb4SJan Kara brelse(epos.bh);
24103bf25cb4SJan Kara brelse(oepos.bh);
241128de7948SCyrill Gorcunov
24121da177e4SLinus Torvalds return (elen >> 30);
24131da177e4SLinus Torvalds }
24141da177e4SLinus Torvalds
2415493447ddSZhao Mengmeng /*
2416493447ddSZhao Mengmeng * Returns 1 on success, -errno on error, 0 on hit EOF.
2417493447ddSZhao Mengmeng */
inode_bmap(struct inode * inode,sector_t block,struct extent_position * pos,struct kernel_lb_addr * eloc,uint32_t * elen,sector_t * offset,int8_t * etype)2418493447ddSZhao Mengmeng int inode_bmap(struct inode *inode, sector_t block, struct extent_position *pos,
2419493447ddSZhao Mengmeng struct kernel_lb_addr *eloc, uint32_t *elen, sector_t *offset,
2420493447ddSZhao Mengmeng int8_t *etype)
24211da177e4SLinus Torvalds {
24224b11111aSMarcin Slusarz unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2423bbc9abd2SFabian Frederick loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
242448d6d8ffSMarcin Slusarz struct udf_inode_info *iinfo;
24255fc8da4dSZhao Mengmeng int err = 0;
24261da177e4SLinus Torvalds
242748d6d8ffSMarcin Slusarz iinfo = UDF_I(inode);
242899600051SNamjae Jeon if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2429ff116fc8SJan Kara pos->offset = 0;
243048d6d8ffSMarcin Slusarz pos->block = iinfo->i_location;
2431ff116fc8SJan Kara pos->bh = NULL;
243299600051SNamjae Jeon }
24331da177e4SLinus Torvalds *elen = 0;
2434cb00ea35SCyrill Gorcunov do {
2435493447ddSZhao Mengmeng err = udf_next_aext(inode, pos, eloc, elen, etype, 1);
24365fc8da4dSZhao Mengmeng if (err <= 0) {
24375fc8da4dSZhao Mengmeng if (err == 0) {
24384b11111aSMarcin Slusarz *offset = (bcount - lbcount) >> blocksize_bits;
243948d6d8ffSMarcin Slusarz iinfo->i_lenExtents = lbcount;
24405fc8da4dSZhao Mengmeng }
2441493447ddSZhao Mengmeng return err;
24421da177e4SLinus Torvalds }
24431da177e4SLinus Torvalds lbcount += *elen;
24441da177e4SLinus Torvalds } while (lbcount <= bcount);
244599600051SNamjae Jeon /* update extent cache */
2446b31c9ed9SFabian Frederick udf_update_extent_cache(inode, lbcount - *elen, pos);
24474b11111aSMarcin Slusarz *offset = (bcount + *elen - lbcount) >> blocksize_bits;
24481da177e4SLinus Torvalds
2449493447ddSZhao Mengmeng return 1;
24501da177e4SLinus Torvalds }
2451