blob: ea8f6cd01f501696cd276e58b8229af677283796 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * inode.c
3 *
4 * PURPOSE
5 * Inode handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * COPYRIGHT
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
12 *
13 * (C) 1998 Dave Boynton
14 * (C) 1998-2004 Ben Fennema
15 * (C) 1999-2000 Stelias Computing Inc
16 *
17 * HISTORY
18 *
19 * 10/04/98 dgb Added rudimentary directory functions
20 * 10/07/98 Fully working udf_block_map! It works!
21 * 11/25/98 bmap altered to better support extents
Marcin Slusarz4b111112008-02-08 04:20:36 -080022 * 12/06/98 blf partition support in udf_iget, udf_block_map
23 * and udf_read_inode
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 12/12/98 rewrote udf_block_map to handle next extents and descs across
25 * block boundaries (which is not actually allowed)
26 * 12/20/98 added support for strategy 4096
27 * 03/07/99 rewrote udf_block_map (again)
28 * New funcs, inode_bmap, udf_next_aext
29 * 04/19/99 Support for writing device EA's for major/minor #
30 */
31
32#include "udfdecl.h"
33#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/writeback.h>
37#include <linux/slab.h>
Bob Copelandf845fce2008-04-17 09:47:48 +020038#include <linux/crc-itu-t.h>
Namjae Jeonbc112322011-10-03 14:02:59 +090039#include <linux/mpage.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080040#include <linux/uio.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060041#include <linux/bio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "udf_i.h"
44#include "udf_sb.h"
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define EXTENT_MERGE_SIZE 5
47
Steven J. Magnanic3367a12019-08-27 07:13:59 -050048#define FE_MAPPED_PERMS (FE_PERM_U_READ | FE_PERM_U_WRITE | FE_PERM_U_EXEC | \
49 FE_PERM_G_READ | FE_PERM_G_WRITE | FE_PERM_G_EXEC | \
50 FE_PERM_O_READ | FE_PERM_O_WRITE | FE_PERM_O_EXEC)
51
52#define FE_DELETE_PERMS (FE_PERM_U_DELETE | FE_PERM_G_DELETE | \
53 FE_PERM_O_DELETE)
54
Al Virofaa17292011-07-26 03:18:29 -040055static umode_t udf_convert_permissions(struct fileEntry *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static int udf_update_inode(struct inode *, int);
Jan Kara49521de2010-10-20 17:42:44 +020057static int udf_sync_inode(struct inode *inode);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -070058static int udf_alloc_i_data(struct inode *inode, size_t size);
Jan Kara7b0b0932011-12-10 01:43:33 +010059static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
Jan Karaff116fc2007-05-08 00:35:14 -070060static int8_t udf_insert_aext(struct inode *, struct extent_position,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020061 struct kernel_lb_addr, uint32_t);
Steve Magnanib490bdd2017-10-12 08:48:40 -050062static void udf_split_extents(struct inode *, int *, int, udf_pblk_t,
Fabian Frederick3cc6f842017-01-06 21:53:50 +010063 struct kernel_long_ad *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static void udf_prealloc_extents(struct inode *, int, int,
Fabian Frederick3cc6f842017-01-06 21:53:50 +010065 struct kernel_long_ad *, int *);
66static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *);
67static void udf_update_extents(struct inode *, struct kernel_long_ad *, int,
68 int, struct extent_position *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
70
Namjae Jeon99600052013-01-19 11:17:14 +090071static void __udf_clear_extent_cache(struct inode *inode)
72{
73 struct udf_inode_info *iinfo = UDF_I(inode);
74
75 if (iinfo->cached_extent.lstart != -1) {
76 brelse(iinfo->cached_extent.epos.bh);
77 iinfo->cached_extent.lstart = -1;
78 }
79}
80
81/* Invalidate extent cache */
82static void udf_clear_extent_cache(struct inode *inode)
83{
84 struct udf_inode_info *iinfo = UDF_I(inode);
85
86 spin_lock(&iinfo->i_extent_cache_lock);
87 __udf_clear_extent_cache(inode);
88 spin_unlock(&iinfo->i_extent_cache_lock);
89}
90
91/* Return contents of extent cache */
92static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
93 loff_t *lbcount, struct extent_position *pos)
94{
95 struct udf_inode_info *iinfo = UDF_I(inode);
96 int ret = 0;
97
98 spin_lock(&iinfo->i_extent_cache_lock);
99 if ((iinfo->cached_extent.lstart <= bcount) &&
100 (iinfo->cached_extent.lstart != -1)) {
101 /* Cache hit */
102 *lbcount = iinfo->cached_extent.lstart;
103 memcpy(pos, &iinfo->cached_extent.epos,
104 sizeof(struct extent_position));
105 if (pos->bh)
106 get_bh(pos->bh);
107 ret = 1;
108 }
109 spin_unlock(&iinfo->i_extent_cache_lock);
110 return ret;
111}
112
113/* Add extent to extent cache */
114static void udf_update_extent_cache(struct inode *inode, loff_t estart,
Fabian Frederickb31c9ed2017-01-06 21:53:56 +0100115 struct extent_position *pos)
Namjae Jeon99600052013-01-19 11:17:14 +0900116{
117 struct udf_inode_info *iinfo = UDF_I(inode);
118
119 spin_lock(&iinfo->i_extent_cache_lock);
120 /* Invalidate previously cached extent */
121 __udf_clear_extent_cache(inode);
122 if (pos->bh)
123 get_bh(pos->bh);
Markus Elfring033c9da2017-08-15 16:45:44 +0200124 memcpy(&iinfo->cached_extent.epos, pos, sizeof(*pos));
Namjae Jeon99600052013-01-19 11:17:14 +0900125 iinfo->cached_extent.lstart = estart;
Fabian Frederickb31c9ed2017-01-06 21:53:56 +0100126 switch (iinfo->i_alloc_type) {
127 case ICBTAG_FLAG_AD_SHORT:
128 iinfo->cached_extent.epos.offset -= sizeof(struct short_ad);
129 break;
130 case ICBTAG_FLAG_AD_LONG:
131 iinfo->cached_extent.epos.offset -= sizeof(struct long_ad);
132 break;
133 }
Namjae Jeon99600052013-01-19 11:17:14 +0900134 spin_unlock(&iinfo->i_extent_cache_lock);
135}
Christoph Hellwigb1e32122008-02-22 12:38:48 +0100136
Al Viro3aac2b62010-06-07 00:43:39 -0400137void udf_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Jan Kara2c948b32009-12-03 13:39:28 +0100139 struct udf_inode_info *iinfo = UDF_I(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400140 int want_delete = 0;
Jan Kara2c948b32009-12-03 13:39:28 +0100141
Jan Kara044e2e22020-09-25 12:14:03 +0200142 if (!is_bad_inode(inode)) {
143 if (!inode->i_nlink) {
144 want_delete = 1;
145 udf_setsize(inode, 0);
146 udf_update_inode(inode, IS_SYNC(inode));
147 }
148 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
149 inode->i_size != iinfo->i_lenExtents) {
150 udf_warn(inode->i_sb,
151 "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
152 inode->i_ino, inode->i_mode,
153 (unsigned long long)inode->i_size,
154 (unsigned long long)iinfo->i_lenExtents);
155 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700156 }
157 truncate_inode_pages_final(&inode->i_data);
Al Viro3aac2b62010-06-07 00:43:39 -0400158 invalidate_inode_buffers(inode);
Jan Karadbd57682012-05-03 14:48:02 +0200159 clear_inode(inode);
Jan Kara382a2282020-09-25 12:29:54 +0200160 kfree(iinfo->i_data);
161 iinfo->i_data = NULL;
Namjae Jeon99600052013-01-19 11:17:14 +0900162 udf_clear_extent_cache(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400163 if (want_delete) {
Al Viro3aac2b62010-06-07 00:43:39 -0400164 udf_free_inode(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Ian Abbott5eec54f2012-09-05 17:44:31 +0100168static void udf_write_failed(struct address_space *mapping, loff_t to)
169{
170 struct inode *inode = mapping->host;
171 struct udf_inode_info *iinfo = UDF_I(inode);
172 loff_t isize = inode->i_size;
173
174 if (to > isize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700175 truncate_pagecache(inode, isize);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100176 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
177 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +0900178 udf_clear_extent_cache(inode);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100179 udf_truncate_extents(inode);
180 up_write(&iinfo->i_data_sem);
181 }
182 }
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static int udf_writepage(struct page *page, struct writeback_control *wbc)
186{
187 return block_write_full_page(page, udf_get_block, wbc);
188}
189
Namjae Jeon378b8e12012-08-31 12:49:07 -0400190static int udf_writepages(struct address_space *mapping,
191 struct writeback_control *wbc)
192{
193 return mpage_writepages(mapping, wbc, udf_get_block);
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static int udf_readpage(struct file *file, struct page *page)
197{
Namjae Jeonbc112322011-10-03 14:02:59 +0900198 return mpage_readpage(page, udf_get_block);
199}
200
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700201static void udf_readahead(struct readahead_control *rac)
Namjae Jeonbc112322011-10-03 14:02:59 +0900202{
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700203 mpage_readahead(rac, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Nick Pigginbe021ee2007-10-16 01:25:20 -0700206static int udf_write_begin(struct file *file, struct address_space *mapping,
207 loff_t pos, unsigned len, unsigned flags,
208 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200210 int ret;
211
212 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100213 if (unlikely(ret))
214 udf_write_failed(mapping, pos + len);
215 return ret;
216}
Jan Kara7e49b6f2010-10-22 00:30:26 +0200217
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700218static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Ian Abbott5eec54f2012-09-05 17:44:31 +0100219{
220 struct file *file = iocb->ki_filp;
221 struct address_space *mapping = file->f_mapping;
222 struct inode *inode = mapping->host;
Al Viroa6cbcd42014-03-04 22:38:00 -0500223 size_t count = iov_iter_count(iter);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100224 ssize_t ret;
Christoph Hellwig155130a2010-06-04 11:29:58 +0200225
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700226 ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
Omar Sandoval6f673762015-03-16 04:33:52 -0700227 if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700228 udf_write_failed(mapping, iocb->ki_pos + count);
Christoph Hellwig155130a2010-06-04 11:29:58 +0200229 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232static sector_t udf_bmap(struct address_space *mapping, sector_t block)
233{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700234 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700237const struct address_space_operations udf_aops = {
Christoph Hellwig0af57372021-06-28 19:36:12 -0700238 .set_page_dirty = __set_page_dirty_buffers,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700239 .readpage = udf_readpage,
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700240 .readahead = udf_readahead,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700241 .writepage = udf_writepage,
Namjae Jeon378b8e12012-08-31 12:49:07 -0400242 .writepages = udf_writepages,
Ian Abbott5eec54f2012-09-05 17:44:31 +0100243 .write_begin = udf_write_begin,
244 .write_end = generic_write_end,
245 .direct_IO = udf_direct_IO,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700246 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247};
248
Jan Karad2eb8c32011-12-10 02:30:48 +0100249/*
250 * Expand file stored in ICB to a normal one-block-file
251 *
252 * This function requires i_data_sem for writing and releases it.
253 * This function requires i_mutex held
254 */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200255int udf_expand_file_adinicb(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct page *page;
258 char *kaddr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800259 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200260 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Al Viro59551022016-01-22 15:40:57 -0500262 WARN_ON_ONCE(!inode_is_locked(inode));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800263 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800265 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800267 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200268 /* from now on we have normal address_space methods */
269 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100270 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100274 /*
275 * Release i_data_sem so that we can lock a page - page lock ranks
276 * above i_data_sem. i_mutex still protects us against file changes.
277 */
278 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Jan Kara7e49b6f2010-10-22 00:30:26 +0200280 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
281 if (!page)
282 return -ENOMEM;
Matt Mackallcd7619d2005-05-01 08:59:01 -0700283
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700284 if (!PageUptodate(page)) {
Fabian Frederick5c26eac2017-04-23 20:58:34 +0200285 kaddr = kmap_atomic(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800286 memset(kaddr + iinfo->i_lenAlloc, 0x00,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300287 PAGE_SIZE - iinfo->i_lenAlloc);
Jan Kara382a2282020-09-25 12:29:54 +0200288 memcpy(kaddr, iinfo->i_data + iinfo->i_lenEAttr,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800289 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 flush_dcache_page(page);
291 SetPageUptodate(page);
Fabian Frederick5c26eac2017-04-23 20:58:34 +0200292 kunmap_atomic(kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100294 down_write(&iinfo->i_data_sem);
Jan Kara382a2282020-09-25 12:29:54 +0200295 memset(iinfo->i_data + iinfo->i_lenEAttr, 0x00,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800296 iinfo->i_lenAlloc);
297 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800299 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800301 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200302 /* from now on we have normal address_space methods */
303 inode->i_data.a_ops = &udf_aops;
Jan Kara7fc3b7c2022-01-17 18:22:13 +0100304 set_page_dirty(page);
305 unlock_page(page);
Jan Karad2eb8c32011-12-10 02:30:48 +0100306 up_write(&iinfo->i_data_sem);
Jan Kara7fc3b7c2022-01-17 18:22:13 +0100307 err = filemap_fdatawrite(inode->i_mapping);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200308 if (err) {
309 /* Restore everything back so that we don't lose data... */
310 lock_page(page);
Jan Karad2eb8c32011-12-10 02:30:48 +0100311 down_write(&iinfo->i_data_sem);
Fabian Frederick5c26eac2017-04-23 20:58:34 +0200312 kaddr = kmap_atomic(page);
Jan Kara382a2282020-09-25 12:29:54 +0200313 memcpy(iinfo->i_data + iinfo->i_lenEAttr, kaddr, inode->i_size);
Fabian Frederick5c26eac2017-04-23 20:58:34 +0200314 kunmap_atomic(kaddr);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200315 unlock_page(page);
316 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
317 inode->i_data.a_ops = &udf_adinicb_aops;
Jan Karaea856912022-01-18 09:57:25 +0100318 iinfo->i_lenAlloc = inode->i_size;
Jan Karad2eb8c32011-12-10 02:30:48 +0100319 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200320 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300321 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200323
324 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Steve Magnanib490bdd2017-10-12 08:48:40 -0500327struct buffer_head *udf_expand_dir_adinicb(struct inode *inode,
328 udf_pblk_t *block, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Steve Magnanib490bdd2017-10-12 08:48:40 -0500330 udf_pblk_t newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700331 struct buffer_head *dbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200332 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700334 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 struct udf_fileident_bh sfibh, dfibh;
Jan Karaaf793292008-02-08 04:20:50 -0800337 loff_t f_pos = udf_ext0_offset(inode);
338 int size = udf_ext0_offset(inode) + inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800340 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
343 alloctype = ICBTAG_FLAG_AD_SHORT;
344 else
345 alloctype = ICBTAG_FLAG_AD_LONG;
346
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700347 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800348 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 mark_inode_dirty(inode);
350 return NULL;
351 }
352
353 /* alloc block, and copy data to it */
354 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800355 iinfo->i_location.partitionReferenceNum,
356 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (!(*block))
358 return NULL;
359 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800360 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800361 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (!newblock)
363 return NULL;
364 dbh = udf_tgetblk(inode->i_sb, newblock);
365 if (!dbh)
366 return NULL;
367 lock_buffer(dbh);
368 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
369 set_buffer_uptodate(dbh);
370 unlock_buffer(dbh);
371 mark_buffer_dirty_inode(dbh, inode);
372
Marcin Slusarz4b111112008-02-08 04:20:36 -0800373 sfibh.soffset = sfibh.eoffset =
Jan Karaaf793292008-02-08 04:20:50 -0800374 f_pos & (inode->i_sb->s_blocksize - 1);
Jan Karaff116fc2007-05-08 00:35:14 -0700375 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 dfibh.soffset = dfibh.eoffset = 0;
377 dfibh.sbh = dfibh.ebh = dbh;
Jan Karaaf793292008-02-08 04:20:50 -0800378 while (f_pos < size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800379 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800380 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
381 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700382 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700383 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return NULL;
385 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800386 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 sfi->descTag.tagLocation = cpu_to_le32(*block);
388 dfibh.soffset = dfibh.eoffset;
389 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
390 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
391 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Jan Kara979a6e22021-05-03 11:54:24 +0200392 udf_get_fi_ident(sfi))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800393 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700394 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return NULL;
396 }
397 }
398 mark_buffer_dirty_inode(dbh, inode);
399
Jan Kara382a2282020-09-25 12:29:54 +0200400 memset(iinfo->i_data + iinfo->i_lenEAttr, 0, iinfo->i_lenAlloc);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800401 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800403 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800404 iinfo->i_location.partitionReferenceNum;
Jan Kara2c948b32009-12-03 13:39:28 +0100405 iinfo->i_lenExtents = inode->i_size;
Jan Karaff116fc2007-05-08 00:35:14 -0700406 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800407 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700408 epos.offset = udf_file_entry_alloc_offset(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100409 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* UniqueID stuff */
411
Jan Kara3bf25cb2007-05-08 00:35:16 -0700412 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 mark_inode_dirty(inode);
414 return dbh;
415}
416
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700417static int udf_get_block(struct inode *inode, sector_t block,
418 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 int err, new;
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800421 sector_t phys = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800422 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700424 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 phys = udf_block_map(inode, block);
426 if (phys)
427 map_bh(bh_result, inode->i_sb, phys);
428 return 0;
429 }
430
431 err = -EIO;
432 new = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800433 iinfo = UDF_I(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100434
435 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800436 if (block == iinfo->i_next_alloc_block + 1) {
437 iinfo->i_next_alloc_block++;
438 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
Namjae Jeon99600052013-01-19 11:17:14 +0900441 udf_clear_extent_cache(inode);
Jan Kara7b0b0932011-12-10 01:43:33 +0100442 phys = inode_getblk(inode, block, &err, &new);
443 if (!phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 if (new)
447 set_buffer_new(bh_result);
448 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700449
450abort:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100451 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Steve Magnanib490bdd2017-10-12 08:48:40 -0500455static struct buffer_head *udf_getblk(struct inode *inode, udf_pblk_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700456 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700458 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 struct buffer_head dummy;
460
461 dummy.b_state = 0;
462 dummy.b_blocknr = -1000;
463 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700464 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700466 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 lock_buffer(bh);
468 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
469 set_buffer_uptodate(bh);
470 unlock_buffer(bh);
471 mark_buffer_dirty_inode(bh, inode);
472 }
473 return bh;
474 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return NULL;
477}
478
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500479/* Extend the file with new blocks totaling 'new_block_bytes',
480 * return the number of extents added
481 */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200482static int udf_do_extend_file(struct inode *inode,
483 struct extent_position *last_pos,
484 struct kernel_long_ad *last_ext,
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500485 loff_t new_block_bytes)
Jan Kara31170b62007-05-08 00:35:21 -0700486{
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500487 uint32_t add;
Jan Kara31170b62007-05-08 00:35:21 -0700488 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
489 struct super_block *sb = inode->i_sb;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200490 struct kernel_lb_addr prealloc_loc = {};
Steve Magnani89a4d972017-10-12 08:48:42 -0500491 uint32_t prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800492 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200493 int err;
Jan Kara31170b62007-05-08 00:35:21 -0700494
495 /* The previous extent is fake and we should not extend by anything
496 * - there's nothing to do... */
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500497 if (!new_block_bytes && fake)
Jan Kara31170b62007-05-08 00:35:21 -0700498 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700499
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800500 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700501 /* Round the last extent up to a multiple of block size */
502 if (last_ext->extLength & (sb->s_blocksize - 1)) {
503 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700504 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
505 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
506 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800507 iinfo->i_lenExtents =
508 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700509 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700510 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700511
Jan Kara31170b62007-05-08 00:35:21 -0700512 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800513 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
514 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700515 /* Save the extent so that we can reattach it to the end */
516 prealloc_loc = last_ext->extLocation;
517 prealloc_len = last_ext->extLength;
518 /* Mark the extent as a hole */
519 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700520 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700521 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800522 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700523 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700524
Jan Kara31170b62007-05-08 00:35:21 -0700525 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800526 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
527 EXT_NOT_RECORDED_NOT_ALLOCATED) {
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500528 add = (1 << 30) - sb->s_blocksize -
529 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
530 if (add > new_block_bytes)
531 add = new_block_bytes;
532 new_block_bytes -= add;
533 last_ext->extLength += add;
Jan Kara31170b62007-05-08 00:35:21 -0700534 }
535
536 if (fake) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200537 udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700538 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700539 count++;
Jan Kara6c371572015-12-23 18:05:03 +0100540 } else {
541 struct kernel_lb_addr tmploc;
542 uint32_t tmplen;
543
Pekka Enberg97e961f2008-10-15 12:29:03 +0200544 udf_write_aext(inode, last_pos, &last_ext->extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800545 last_ext->extLength, 1);
Steven J. Magnani63c9e472021-01-07 17:41:16 -0600546
Jan Kara6c371572015-12-23 18:05:03 +0100547 /*
Steven J. Magnani63c9e472021-01-07 17:41:16 -0600548 * We've rewritten the last extent. If we are going to add
549 * more extents, we may need to enter possible following
550 * empty indirect extent.
Jan Kara6c371572015-12-23 18:05:03 +0100551 */
Steven J. Magnani63c9e472021-01-07 17:41:16 -0600552 if (new_block_bytes || prealloc_len)
553 udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
Jan Kara6c371572015-12-23 18:05:03 +0100554 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700555
Jan Kara31170b62007-05-08 00:35:21 -0700556 /* Managed to do everything necessary? */
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500557 if (!new_block_bytes)
Jan Kara31170b62007-05-08 00:35:21 -0700558 goto out;
559
560 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
561 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800562 last_ext->extLocation.partitionReferenceNum = 0;
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500563 add = (1 << 30) - sb->s_blocksize;
564 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700565
Jan Kara31170b62007-05-08 00:35:21 -0700566 /* Create enough extents to cover the whole hole */
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500567 while (new_block_bytes > add) {
568 new_block_bytes -= add;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200569 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
570 last_ext->extLength, 1);
571 if (err)
572 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700573 count++;
574 }
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500575 if (new_block_bytes) {
Jan Kara31170b62007-05-08 00:35:21 -0700576 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500577 new_block_bytes;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200578 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
579 last_ext->extLength, 1);
580 if (err)
581 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700582 count++;
583 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700584
585out:
Jan Kara31170b62007-05-08 00:35:21 -0700586 /* Do we have some preallocated blocks saved? */
587 if (prealloc_len) {
Jan Kara7e49b6f2010-10-22 00:30:26 +0200588 err = udf_add_aext(inode, last_pos, &prealloc_loc,
589 prealloc_len, 1);
590 if (err)
591 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700592 last_ext->extLocation = prealloc_loc;
593 last_ext->extLength = prealloc_len;
594 count++;
595 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700596
Jan Kara31170b62007-05-08 00:35:21 -0700597 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800598 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200599 last_pos->offset -= sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800600 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200601 last_pos->offset -= sizeof(struct long_ad);
Jan Kara31170b62007-05-08 00:35:21 -0700602 else
Jan Kara7e49b6f2010-10-22 00:30:26 +0200603 return -EIO;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700604
Jan Kara31170b62007-05-08 00:35:21 -0700605 return count;
606}
607
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500608/* Extend the final block of the file to final_block_len bytes */
609static void udf_do_extend_final_block(struct inode *inode,
610 struct extent_position *last_pos,
611 struct kernel_long_ad *last_ext,
612 uint32_t final_block_len)
613{
614 struct super_block *sb = inode->i_sb;
615 uint32_t added_bytes;
616
617 added_bytes = final_block_len -
618 (last_ext->extLength & (sb->s_blocksize - 1));
619 last_ext->extLength += added_bytes;
620 UDF_I(inode)->i_lenExtents += added_bytes;
621
622 udf_write_aext(inode, last_pos, &last_ext->extLocation,
623 last_ext->extLength, 1);
624}
625
Jan Kara7e49b6f2010-10-22 00:30:26 +0200626static int udf_extend_file(struct inode *inode, loff_t newsize)
627{
628
629 struct extent_position epos;
630 struct kernel_lb_addr eloc;
631 uint32_t elen;
632 int8_t etype;
633 struct super_block *sb = inode->i_sb;
634 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500635 unsigned long partial_final_block;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200636 int adsize;
637 struct udf_inode_info *iinfo = UDF_I(inode);
638 struct kernel_long_ad extent;
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500639 int err = 0;
640 int within_final_block;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200641
642 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
643 adsize = sizeof(struct short_ad);
644 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
645 adsize = sizeof(struct long_ad);
646 else
647 BUG();
648
649 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500650 within_final_block = (etype != -1);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200651
Jan Kara7e49b6f2010-10-22 00:30:26 +0200652 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
653 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
654 /* File has no extents at all or has empty last
655 * indirect extent! Create a fake extent... */
656 extent.extLocation.logicalBlockNum = 0;
657 extent.extLocation.partitionReferenceNum = 0;
658 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
659 } else {
660 epos.offset -= adsize;
661 etype = udf_next_aext(inode, &epos, &extent.extLocation,
662 &extent.extLength, 0);
663 extent.extLength |= etype << 30;
664 }
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500665
666 partial_final_block = newsize & (sb->s_blocksize - 1);
667
668 /* File has extent covering the new size (could happen when extending
669 * inside a block)?
670 */
671 if (within_final_block) {
672 /* Extending file within the last file block */
673 udf_do_extend_final_block(inode, &epos, &extent,
674 partial_final_block);
675 } else {
676 loff_t add = ((loff_t)offset << sb->s_blocksize_bits) |
677 partial_final_block;
678 err = udf_do_extend_file(inode, &epos, &extent, add);
679 }
680
Jan Kara7e49b6f2010-10-22 00:30:26 +0200681 if (err < 0)
682 goto out;
683 err = 0;
684 iinfo->i_lenExtents = newsize;
685out:
686 brelse(epos.bh);
687 return err;
688}
689
Jan Kara7b0b0932011-12-10 01:43:33 +0100690static sector_t inode_getblk(struct inode *inode, sector_t block,
691 int *err, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200693 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700694 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700696 uint32_t elen = 0, tmpelen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200697 struct kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700699 loff_t lbcount = 0, b_off = 0;
Steve Magnanib490bdd2017-10-12 08:48:40 -0500700 udf_pblk_t newblocknum, newblock;
Jan Kara60448b12007-05-08 00:35:13 -0700701 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800703 struct udf_inode_info *iinfo = UDF_I(inode);
Steve Magnanib490bdd2017-10-12 08:48:40 -0500704 udf_pblk_t goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700705 int lastblock = 0;
Namjae Jeonfb719c52012-10-10 00:09:12 +0900706 bool isBeyondEOF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Jan Kara7b0b0932011-12-10 01:43:33 +0100708 *err = 0;
709 *new = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700710 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800711 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700712 prev_epos.bh = NULL;
713 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700714 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700717 alternate between laarr[0] and laarr[1] for locations of the
718 current extent, and the previous extent */
719 do {
720 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700721 brelse(prev_epos.bh);
722 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700723 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700725 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700726 brelse(cur_epos.bh);
727 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700728 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730
731 lbcount += elen;
732
Jan Karaff116fc2007-05-08 00:35:14 -0700733 prev_epos.block = cur_epos.block;
734 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Jan Karaff116fc2007-05-08 00:35:14 -0700736 prev_epos.offset = cur_epos.offset;
737 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Marcin Slusarz4b111112008-02-08 04:20:36 -0800739 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
740 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 break;
742
743 c = !c;
744
745 laarr[c].extLength = (etype << 30) | elen;
746 laarr[c].extLocation = eloc;
747
748 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
749 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700750 ((elen + inode->i_sb->s_blocksize - 1) >>
751 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700753 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 } while (lbcount + elen <= b_off);
755
756 b_off -= lbcount;
757 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700758 /*
759 * Move prev_epos and cur_epos into indirect extent if we are at
760 * the pointer to it
761 */
762 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
763 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700766 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700768 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
769 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700771 ((elen + inode->i_sb->s_blocksize - 1) &
772 ~(inode->i_sb->s_blocksize - 1));
Jan Kara7e49b6f2010-10-22 00:30:26 +0200773 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200775 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Fabian Frederick02d4ca42017-01-06 21:53:51 +0100776 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778
Jan Kara31170b62007-05-08 00:35:21 -0700779 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700780 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700781 int ret;
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500782 loff_t hole_len;
Fabian Frederick69814982015-02-04 18:26:27 +0100783 isBeyondEOF = true;
Jan Kara31170b62007-05-08 00:35:21 -0700784 if (count) {
785 if (c)
786 laarr[0] = laarr[1];
787 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700788 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700789 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800790 memset(&laarr[0].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200791 sizeof(struct kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700792 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200793 /* Will udf_do_extend_file() create real extent from
Marcin Slusarz4b111112008-02-08 04:20:36 -0800794 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700795 startnum = (offset > 0);
796 }
797 /* Create extents for the hole between EOF and offset */
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500798 hole_len = (loff_t)offset << inode->i_blkbits;
799 ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200800 if (ret < 0) {
Jan Kara7e49b6f2010-10-22 00:30:26 +0200801 *err = ret;
Fabian Frederick02d4ca42017-01-06 21:53:51 +0100802 newblock = 0;
803 goto out_free;
Jan Kara31170b62007-05-08 00:35:21 -0700804 }
805 c = 0;
806 offset = 0;
807 count += ret;
808 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800809 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
810 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700811 /* Is there any real extent? - otherwise we overwrite
812 * the fake one... */
813 if (count)
814 c = !c;
815 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700816 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800817 memset(&laarr[c].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200818 sizeof(struct kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700819 count++;
Jan Kara31170b62007-05-08 00:35:21 -0700820 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700821 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700823 } else {
Fabian Frederick69814982015-02-04 18:26:27 +0100824 isBeyondEOF = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 endnum = startnum = ((count > 2) ? 2 : count);
826
Marcin Slusarz4b111112008-02-08 04:20:36 -0800827 /* if the current extent is in position 0,
828 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700829 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700830 laarr[2] = laarr[0];
831 laarr[0] = laarr[1];
832 laarr[1] = laarr[2];
833 c = 1;
834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Marcin Slusarz4b111112008-02-08 04:20:36 -0800836 /* if the current block is located in an extent,
837 read the next extent */
838 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
839 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700840 laarr[c + 1].extLength = (etype << 30) | elen;
841 laarr[c + 1].extLocation = eloc;
842 count++;
843 startnum++;
844 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800845 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 lastblock = 1;
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700850 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800851 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800853 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800854 if (iinfo->i_next_alloc_block == block)
855 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700857 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800858 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800859 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861
Marcin Slusarz4b111112008-02-08 04:20:36 -0800862 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800863 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800864 goal, err);
865 if (!newblocknum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 *err = -ENOSPC;
Fabian Frederick02d4ca42017-01-06 21:53:51 +0100867 newblock = 0;
868 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Namjae Jeonfb719c52012-10-10 00:09:12 +0900870 if (isBeyondEOF)
871 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873
Marcin Slusarz4b111112008-02-08 04:20:36 -0800874 /* if the extent the requsted block is located in contains multiple
875 * blocks, split the extent into at most three extents. blocks prior
876 * to requested block, requested block, and blocks after requested
877 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
879
Jan Kara81056dd2009-07-16 18:02:25 +0200880 /* We preallocate blocks only for regular files. It also makes sense
881 * for directories but there's a problem when to drop the
882 * preallocation. We might use some delayed work for that but I feel
883 * it's overengineering for a filesystem like UDF. */
884 if (S_ISREG(inode->i_mode))
885 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 /* merge any continuous blocks in laarr */
888 udf_merge_extents(inode, laarr, &endnum);
889
890 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700891 * of extents is greater than the old number, and deleting extents if
892 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700893 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Marcin Slusarz4b111112008-02-08 04:20:36 -0800895 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800896 iinfo->i_location.partitionReferenceNum, 0);
Jan Kara7b0b0932011-12-10 01:43:33 +0100897 if (!newblock) {
898 *err = -EIO;
Fabian Frederick02d4ca42017-01-06 21:53:51 +0100899 goto out_free;
Jan Kara7b0b0932011-12-10 01:43:33 +0100900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800902 iinfo->i_next_alloc_block = block;
903 iinfo->i_next_alloc_goal = newblocknum;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700904 inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 if (IS_SYNC(inode))
907 udf_sync_inode(inode);
908 else
909 mark_inode_dirty(inode);
Fabian Frederick02d4ca42017-01-06 21:53:51 +0100910out_free:
911 brelse(prev_epos.bh);
912 brelse(cur_epos.bh);
913 brelse(next_epos.bh);
Jan Kara7b0b0932011-12-10 01:43:33 +0100914 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700917static void udf_split_extents(struct inode *inode, int *c, int offset,
Steve Magnanib490bdd2017-10-12 08:48:40 -0500918 udf_pblk_t newblocknum,
919 struct kernel_long_ad *laarr, int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800921 unsigned long blocksize = inode->i_sb->s_blocksize;
922 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800925 (laarr[*c].extLength >> 30) ==
926 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int curr = *c;
928 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800929 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 int8_t etype = (laarr[curr].extLength >> 30);
931
Marcin Slusarz4b111112008-02-08 04:20:36 -0800932 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700933 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800934 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700935 laarr[curr + 2] = laarr[curr + 1];
936 laarr[curr + 1] = laarr[curr];
937 } else {
938 laarr[curr + 3] = laarr[curr + 1];
939 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700942 if (offset) {
943 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800944 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200945 &laarr[curr].extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800946 0, offset);
947 laarr[curr].extLength =
948 EXT_NOT_RECORDED_NOT_ALLOCATED |
949 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800951 laarr[curr].extLocation.
952 partitionReferenceNum = 0;
953 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800955 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700956 curr++;
957 (*c)++;
958 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 laarr[curr].extLocation.logicalBlockNum = newblocknum;
962 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
963 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800964 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800966 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700967 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700969 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800971 laarr[curr].extLocation.logicalBlockNum +=
972 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700973 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800974 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700975 curr++;
976 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978 }
979}
980
981static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Fabian Frederick3cc6f842017-01-06 21:53:50 +0100982 struct kernel_long_ad *laarr,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700983 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 int start, length = 0, currlength = 0, i;
986
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700987 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (!lastblock)
989 return;
990 else
991 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700992 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800993 if ((laarr[c + 1].extLength >> 30) ==
994 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700995 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800996 length = currlength =
997 (((laarr[c + 1].extLength &
998 UDF_EXTENT_LENGTH_MASK) +
999 inode->i_sb->s_blocksize - 1) >>
1000 inode->i_sb->s_blocksize_bits);
1001 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 start = c;
1003 }
1004
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001005 for (i = start + 1; i <= *endnum; i++) {
1006 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (lastblock)
1008 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001009 } else if ((laarr[i].extLength >> 30) ==
1010 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
1011 length += (((laarr[i].extLength &
1012 UDF_EXTENT_LENGTH_MASK) +
1013 inode->i_sb->s_blocksize - 1) >>
1014 inode->i_sb->s_blocksize_bits);
1015 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 break;
1017 }
1018
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001019 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001021 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08001022 inode->i_sb->s_blocksize - 1) >>
1023 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001025 laarr[start].extLocation.partitionReferenceNum,
1026 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1027 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1028 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001029 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001030 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001032 (numalloc <<
1033 inode->i_sb->s_blocksize_bits);
1034 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001035 memmove(&laarr[c + 2], &laarr[c + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001036 sizeof(struct long_ad) * (*endnum - (c + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001037 (*endnum)++;
1038 laarr[c + 1].extLocation.logicalBlockNum = next;
1039 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -08001040 laarr[c].extLocation.
1041 partitionReferenceNum;
1042 laarr[c + 1].extLength =
1043 EXT_NOT_RECORDED_ALLOCATED |
1044 (numalloc <<
1045 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001046 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001049 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001050 int elen = ((laarr[i].extLength &
1051 UDF_EXTENT_LENGTH_MASK) +
1052 inode->i_sb->s_blocksize - 1) >>
1053 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001055 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001057 (numalloc <<
1058 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001060 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001062 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001063 memmove(&laarr[i],
1064 &laarr[i + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001065 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001066 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001067 i--;
1068 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001071 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001072 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
1074 }
1075}
1076
Fabian Frederick3cc6f842017-01-06 21:53:50 +01001077static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001078 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001081 unsigned long blocksize = inode->i_sb->s_blocksize;
1082 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001084 for (i = 0; i < (*endnum - 1); i++) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001085 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1086 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Marcin Slusarz4b111112008-02-08 04:20:36 -08001088 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1089 (((li->extLength >> 30) ==
1090 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1091 ((lip1->extLocation.logicalBlockNum -
1092 li->extLocation.logicalBlockNum) ==
1093 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1094 blocksize - 1) >> blocksize_bits)))) {
1095
1096 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1097 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1098 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1099 lip1->extLength = (lip1->extLength -
1100 (li->extLength &
1101 UDF_EXTENT_LENGTH_MASK) +
1102 UDF_EXTENT_LENGTH_MASK) &
1103 ~(blocksize - 1);
1104 li->extLength = (li->extLength &
1105 UDF_EXTENT_FLAG_MASK) +
1106 (UDF_EXTENT_LENGTH_MASK + 1) -
1107 blocksize;
1108 lip1->extLocation.logicalBlockNum =
1109 li->extLocation.logicalBlockNum +
1110 ((li->extLength &
1111 UDF_EXTENT_LENGTH_MASK) >>
1112 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001113 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001114 li->extLength = lip1->extLength +
1115 (((li->extLength &
1116 UDF_EXTENT_LENGTH_MASK) +
1117 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001118 if (*endnum > (i + 2))
1119 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001120 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001121 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001122 i--;
1123 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001125 } else if (((li->extLength >> 30) ==
1126 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1127 ((lip1->extLength >> 30) ==
1128 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001129 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001130 ((li->extLength &
1131 UDF_EXTENT_LENGTH_MASK) +
1132 blocksize - 1) >> blocksize_bits);
1133 li->extLocation.logicalBlockNum = 0;
1134 li->extLocation.partitionReferenceNum = 0;
1135
1136 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1137 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1138 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1139 lip1->extLength = (lip1->extLength -
1140 (li->extLength &
1141 UDF_EXTENT_LENGTH_MASK) +
1142 UDF_EXTENT_LENGTH_MASK) &
1143 ~(blocksize - 1);
1144 li->extLength = (li->extLength &
1145 UDF_EXTENT_FLAG_MASK) +
1146 (UDF_EXTENT_LENGTH_MASK + 1) -
1147 blocksize;
1148 } else {
1149 li->extLength = lip1->extLength +
1150 (((li->extLength &
1151 UDF_EXTENT_LENGTH_MASK) +
1152 blocksize - 1) & ~(blocksize - 1));
1153 if (*endnum > (i + 2))
1154 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001155 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001156 (*endnum - (i + 2)));
1157 i--;
1158 (*endnum)--;
1159 }
1160 } else if ((li->extLength >> 30) ==
1161 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1162 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001163 &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001164 ((li->extLength &
1165 UDF_EXTENT_LENGTH_MASK) +
1166 blocksize - 1) >> blocksize_bits);
1167 li->extLocation.logicalBlockNum = 0;
1168 li->extLocation.partitionReferenceNum = 0;
1169 li->extLength = (li->extLength &
1170 UDF_EXTENT_LENGTH_MASK) |
1171 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173 }
1174}
1175
Fabian Frederick3cc6f842017-01-06 21:53:50 +01001176static void udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001177 int startnum, int endnum,
1178 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 int start = 0, i;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001181 struct kernel_lb_addr tmploc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 uint32_t tmplen;
1183
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001184 if (startnum > endnum) {
1185 for (i = 0; i < (startnum - endnum); i++)
Jan Kara6c1e4d02018-06-13 18:04:24 +02001186 udf_delete_aext(inode, *epos);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001187 } else if (startnum < endnum) {
1188 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001189 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001190 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -07001191 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001192 &laarr[i].extLength, 1);
1193 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195 }
1196
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001197 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001198 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001199 udf_write_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001200 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202}
1203
Steve Magnanib490bdd2017-10-12 08:48:40 -05001204struct buffer_head *udf_bread(struct inode *inode, udf_pblk_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001205 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001207 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 bh = udf_getblk(inode, block, create, err);
1210 if (!bh)
1211 return NULL;
1212
1213 if (buffer_uptodate(bh))
1214 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001215
Mike Christiedfec8a12016-06-05 14:31:44 -05001216 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 wait_on_buffer(bh);
1219 if (buffer_uptodate(bh))
1220 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 brelse(bh);
1223 *err = -EIO;
1224 return NULL;
1225}
1226
Jan Kara7e49b6f2010-10-22 00:30:26 +02001227int udf_setsize(struct inode *inode, loff_t newsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001230 struct udf_inode_info *iinfo;
Steve Magnani89a4d972017-10-12 08:48:42 -05001231 unsigned int bsize = i_blocksize(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001234 S_ISLNK(inode->i_mode)))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001235 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001237 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001239 iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001240 if (newsize > inode->i_size) {
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001241 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001242 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1243 if (bsize <
1244 (udf_file_entry_alloc_offset(inode) + newsize)) {
1245 err = udf_expand_file_adinicb(inode);
Jan Karad2eb8c32011-12-10 02:30:48 +01001246 if (err)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001247 return err;
Jan Karad2eb8c32011-12-10 02:30:48 +01001248 down_write(&iinfo->i_data_sem);
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001249 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001250 iinfo->i_lenAlloc = newsize;
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001251 goto set_size;
1252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001254 err = udf_extend_file(inode, newsize);
1255 if (err) {
1256 up_write(&iinfo->i_data_sem);
1257 return err;
1258 }
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001259set_size:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001260 up_write(&iinfo->i_data_sem);
Jan Karaf2e95352017-06-13 16:20:25 +02001261 truncate_setsize(inode, newsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001262 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001263 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1264 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001265 udf_clear_extent_cache(inode);
Jan Kara382a2282020-09-25 12:29:54 +02001266 memset(iinfo->i_data + iinfo->i_lenEAttr + newsize,
Jan Kara7e49b6f2010-10-22 00:30:26 +02001267 0x00, bsize - newsize -
1268 udf_file_entry_alloc_offset(inode));
1269 iinfo->i_lenAlloc = newsize;
1270 truncate_setsize(inode, newsize);
1271 up_write(&iinfo->i_data_sem);
1272 goto update_time;
1273 }
1274 err = block_truncate_page(inode->i_mapping, newsize,
1275 udf_get_block);
1276 if (err)
1277 return err;
Jan Karaf2e95352017-06-13 16:20:25 +02001278 truncate_setsize(inode, newsize);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001279 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001280 udf_clear_extent_cache(inode);
Jan Kara2b42be52019-03-11 15:27:02 +01001281 err = udf_truncate_extents(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001282 up_write(&iinfo->i_data_sem);
Jan Kara2b42be52019-03-11 15:27:02 +01001283 if (err)
1284 return err;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001285 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001286update_time:
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001287 inode->i_mtime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001289 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 else
1291 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
Jan Karac03aa9f2014-09-04 14:06:55 +02001295/*
1296 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1297 * arbitrary - just that we hopefully don't limit any real use of rewritten
1298 * inode on write-once media but avoid looping for too long on corrupted media.
1299 */
1300#define UDF_MAX_ICB_NESTING 1024
1301
Jan Kara6174c2e2014-10-09 12:52:16 +02001302static int udf_read_inode(struct inode *inode, bool hidden_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct buffer_head *bh = NULL;
1305 struct fileEntry *fe;
Jan Karabb7720a02014-09-04 13:32:50 +02001306 struct extendedFileEntry *efe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001308 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Karabb7720a02014-09-04 13:32:50 +02001309 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001310 struct kernel_lb_addr *iloc = &iinfo->i_location;
Jan Karabb7720a02014-09-04 13:32:50 +02001311 unsigned int link_count;
Jan Karac03aa9f2014-09-04 14:06:55 +02001312 unsigned int indirections = 0;
Jan Kara79144952015-01-07 13:46:16 +01001313 int bs = inode->i_sb->s_blocksize;
Jan Kara6d3d5e82014-09-04 16:15:51 +02001314 int ret = -EIO;
Jan Kara0c9850f2018-02-22 10:28:52 +01001315 uint32_t uid, gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Jan Karac03aa9f2014-09-04 14:06:55 +02001317reread:
Fabian Frederick1d82a562017-01-06 21:54:43 +01001318 if (iloc->partitionReferenceNum >= sbi->s_partitions) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05001319 udf_debug("partition reference: %u > logical volume partitions: %u\n",
Fabian Frederick1d82a562017-01-06 21:54:43 +01001320 iloc->partitionReferenceNum, sbi->s_partitions);
1321 return -EIO;
1322 }
1323
Jan Kara6d3d5e82014-09-04 16:15:51 +02001324 if (iloc->logicalBlockNum >=
1325 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05001326 udf_debug("block=%u, partition=%u out of range\n",
Jan Kara6d3d5e82014-09-04 16:15:51 +02001327 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1328 return -EIO;
1329 }
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 /*
1332 * Set defaults, but the inode is still incomplete!
1333 * Note: get_new_inode() sets the following on a new inode:
1334 * i_sb = sb
1335 * i_no = ino
1336 * i_flags = sb->s_flags
1337 * i_state = 0
1338 * clean_inode(): zero fills and sets
1339 * i_count = 1
1340 * i_nlink = 1
1341 * i_op = NULL;
1342 */
Jan Kara6d3d5e82014-09-04 16:15:51 +02001343 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001344 if (!bh) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05001345 udf_err(inode->i_sb, "(ino %lu) failed !bh\n", inode->i_ino);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001346 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348
1349 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001350 ident != TAG_IDENT_USE) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05001351 udf_err(inode->i_sb, "(ino %lu) failed ident=%u\n",
Joe Perches78ace702011-10-10 01:08:05 -07001352 inode->i_ino, ident);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001353 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355
1356 fe = (struct fileEntry *)bh->b_data;
Jan Karabb7720a02014-09-04 13:32:50 +02001357 efe = (struct extendedFileEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001359 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001360 struct buffer_head *ibh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Jan Kara6d3d5e82014-09-04 16:15:51 +02001362 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001363 if (ident == TAG_IDENT_IE && ibh) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001364 struct kernel_lb_addr loc;
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001365 struct indirectEntry *ie;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001366
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001367 ie = (struct indirectEntry *)ibh->b_data;
1368 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001369
Jan Karac03aa9f2014-09-04 14:06:55 +02001370 if (ie->indirectICB.extLength) {
Jan Karac03aa9f2014-09-04 14:06:55 +02001371 brelse(ibh);
1372 memcpy(&iinfo->i_location, &loc,
1373 sizeof(struct kernel_lb_addr));
1374 if (++indirections > UDF_MAX_ICB_NESTING) {
1375 udf_err(inode->i_sb,
1376 "too many ICBs in ICB hierarchy"
1377 " (max %d supported)\n",
1378 UDF_MAX_ICB_NESTING);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001379 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001380 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001381 brelse(bh);
Jan Karac03aa9f2014-09-04 14:06:55 +02001382 goto reread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001384 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001385 brelse(ibh);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001386 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05001387 udf_err(inode->i_sb, "unsupported strategy type: %u\n",
Joe Perches78ace702011-10-10 01:08:05 -07001388 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001389 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001391 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001392 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001393 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001394 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001396 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001397 ICBTAG_FLAG_AD_MASK;
Jan Karad288d952018-12-12 14:29:20 +01001398 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
1399 iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
1400 iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1401 ret = -EIO;
1402 goto out;
1403 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001404 iinfo->i_unique = 0;
1405 iinfo->i_lenEAttr = 0;
1406 iinfo->i_lenExtents = 0;
1407 iinfo->i_lenAlloc = 0;
1408 iinfo->i_next_alloc_block = 0;
1409 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001410 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001411 iinfo->i_efe = 1;
1412 iinfo->i_use = 0;
Jan Kara79144952015-01-07 13:46:16 +01001413 ret = udf_alloc_i_data(inode, bs -
Jan Kara6d3d5e82014-09-04 16:15:51 +02001414 sizeof(struct extendedFileEntry));
1415 if (ret)
1416 goto out;
Jan Kara382a2282020-09-25 12:29:54 +02001417 memcpy(iinfo->i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001418 bh->b_data + sizeof(struct extendedFileEntry),
Jan Kara79144952015-01-07 13:46:16 +01001419 bs - sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001420 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001421 iinfo->i_efe = 0;
1422 iinfo->i_use = 0;
Jan Kara79144952015-01-07 13:46:16 +01001423 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001424 if (ret)
1425 goto out;
Jan Kara382a2282020-09-25 12:29:54 +02001426 memcpy(iinfo->i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001427 bh->b_data + sizeof(struct fileEntry),
Jan Kara79144952015-01-07 13:46:16 +01001428 bs - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001429 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001430 iinfo->i_efe = 0;
1431 iinfo->i_use = 1;
1432 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001433 ((struct unallocSpaceEntry *)bh->b_data)->
1434 lengthAllocDescs);
Jan Kara79144952015-01-07 13:46:16 +01001435 ret = udf_alloc_i_data(inode, bs -
Jan Kara6d3d5e82014-09-04 16:15:51 +02001436 sizeof(struct unallocSpaceEntry));
1437 if (ret)
1438 goto out;
Jan Kara382a2282020-09-25 12:29:54 +02001439 memcpy(iinfo->i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001440 bh->b_data + sizeof(struct unallocSpaceEntry),
Jan Kara79144952015-01-07 13:46:16 +01001441 bs - sizeof(struct unallocSpaceEntry));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444
Jan Kara6d3d5e82014-09-04 16:15:51 +02001445 ret = -EIO;
Jan Karac03cad22010-10-20 22:17:28 +02001446 read_lock(&sbi->s_cred_lock);
Jan Kara0c9850f2018-02-22 10:28:52 +01001447 uid = le32_to_cpu(fe->uid);
1448 if (uid == UDF_INVALID_ID ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001449 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Jan Kara0c9850f2018-02-22 10:28:52 +01001450 inode->i_uid = sbi->s_uid;
1451 else
1452 i_uid_write(inode, uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Jan Kara0c9850f2018-02-22 10:28:52 +01001454 gid = le32_to_cpu(fe->gid);
1455 if (gid == UDF_INVALID_ID ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001456 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Jan Kara0c9850f2018-02-22 10:28:52 +01001457 inode->i_gid = sbi->s_gid;
1458 else
1459 i_gid_write(inode, gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001461 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001462 sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001463 inode->i_mode = sbi->s_fmode;
1464 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001465 sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001466 inode->i_mode = sbi->s_dmode;
1467 else
1468 inode->i_mode = udf_convert_permissions(fe);
1469 inode->i_mode &= ~sbi->s_umask;
Steven J. Magnanic3367a12019-08-27 07:13:59 -05001470 iinfo->i_extraPerms = le32_to_cpu(fe->permissions) & ~FE_MAPPED_PERMS;
1471
Jan Karac03cad22010-10-20 22:17:28 +02001472 read_unlock(&sbi->s_cred_lock);
1473
Miklos Szeredibfe86842011-10-28 14:13:29 +02001474 link_count = le16_to_cpu(fe->fileLinkCount);
Jan Kara4071b912014-09-04 16:19:47 +02001475 if (!link_count) {
Jan Kara6174c2e2014-10-09 12:52:16 +02001476 if (!hidden_inode) {
1477 ret = -ESTALE;
1478 goto out;
1479 }
1480 link_count = 1;
Jan Kara4071b912014-09-04 16:19:47 +02001481 }
Miklos Szeredibfe86842011-10-28 14:13:29 +02001482 set_nlink(inode, link_count);
Jan Karac03cad22010-10-20 22:17:28 +02001483
1484 inode->i_size = le64_to_cpu(fe->informationLength);
1485 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001487 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001489 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001491 udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime);
1492 udf_disk_stamp_to_time(&inode->i_mtime, fe->modificationTime);
1493 udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001495 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1496 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1497 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001498 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
Steven J. Magnaniab9a3a72019-08-14 07:50:02 -05001499 iinfo->i_streamdir = 0;
1500 iinfo->i_lenStreams = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001501 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001502 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001503 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001505 udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime);
1506 udf_disk_stamp_to_time(&inode->i_mtime, efe->modificationTime);
Deepa Dinamani0220edd2018-05-10 08:26:17 -07001507 udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime);
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001508 udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001510 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1511 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1512 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001513 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
Steven J. Magnaniab9a3a72019-08-14 07:50:02 -05001514
1515 /* Named streams */
1516 iinfo->i_streamdir = (efe->streamDirectoryICB.extLength != 0);
1517 iinfo->i_locStreamdir =
1518 lelb_to_cpu(efe->streamDirectoryICB.extLocation);
1519 iinfo->i_lenStreams = le64_to_cpu(efe->objectSize);
1520 if (iinfo->i_lenStreams >= inode->i_size)
1521 iinfo->i_lenStreams -= inode->i_size;
1522 else
1523 iinfo->i_lenStreams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
Jan Kara470cca52014-09-04 16:26:19 +02001525 inode->i_generation = iinfo->i_unique;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Jan Kara23b133bd2015-01-07 13:49:08 +01001527 /*
1528 * Sanity check length of allocation descriptors and extended attrs to
1529 * avoid integer overflows
1530 */
1531 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1532 goto out;
1533 /* Now do exact checks */
1534 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1535 goto out;
Jan Karae1593322014-12-19 12:03:53 +01001536 /* Sanity checks for files in ICB so that we don't get confused later */
1537 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1538 /*
1539 * For file in ICB data is stored in allocation descriptor
1540 * so sizes should match
1541 */
1542 if (iinfo->i_lenAlloc != inode->i_size)
1543 goto out;
1544 /* File in ICB has to fit in there... */
Jan Kara79144952015-01-07 13:46:16 +01001545 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
Jan Karae1593322014-12-19 12:03:53 +01001546 goto out;
1547 }
1548
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001549 switch (fe->icbTag.fileType) {
1550 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001551 inode->i_op = &udf_dir_inode_operations;
1552 inode->i_fop = &udf_dir_operations;
1553 inode->i_mode |= S_IFDIR;
1554 inc_nlink(inode);
1555 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001556 case ICBTAG_FILE_TYPE_REALTIME:
1557 case ICBTAG_FILE_TYPE_REGULAR:
1558 case ICBTAG_FILE_TYPE_UNDEF:
Jan Kara742e1792008-04-08 01:17:52 +02001559 case ICBTAG_FILE_TYPE_VAT20:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001560 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001561 inode->i_data.a_ops = &udf_adinicb_aops;
1562 else
1563 inode->i_data.a_ops = &udf_aops;
1564 inode->i_op = &udf_file_inode_operations;
1565 inode->i_fop = &udf_file_operations;
1566 inode->i_mode |= S_IFREG;
1567 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001568 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001569 inode->i_mode |= S_IFBLK;
1570 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001571 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001572 inode->i_mode |= S_IFCHR;
1573 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001574 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001575 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1576 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001577 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001578 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1579 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001580 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001581 inode->i_data.a_ops = &udf_symlink_aops;
Jan Karaad4d0532017-01-02 14:30:31 +01001582 inode->i_op = &udf_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05001583 inode_nohighmem(inode);
Fabian Frederick6ff6b2b2017-04-23 20:58:15 +02001584 inode->i_mode = S_IFLNK | 0777;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001585 break;
Jan Karabfb257a2008-04-08 20:37:21 +02001586 case ICBTAG_FILE_TYPE_MAIN:
1587 udf_debug("METADATA FILE-----\n");
1588 break;
1589 case ICBTAG_FILE_TYPE_MIRROR:
1590 udf_debug("METADATA MIRROR FILE-----\n");
1591 break;
1592 case ICBTAG_FILE_TYPE_BITMAP:
1593 udf_debug("METADATA BITMAP FILE-----\n");
1594 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001595 default:
Steve Magnanifcbf7632017-10-12 08:48:41 -05001596 udf_err(inode->i_sb, "(ino %lu) failed unknown file type=%u\n",
Joe Perches78ace702011-10-10 01:08:05 -07001597 inode->i_ino, fe->icbTag.fileType);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001598 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001600 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001601 struct deviceSpec *dsea =
1602 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001603 if (dsea) {
1604 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001605 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1606 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001608 } else
Jan Kara6d3d5e82014-09-04 16:15:51 +02001609 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001611 ret = 0;
1612out:
Jan Karabb7720a02014-09-04 13:32:50 +02001613 brelse(bh);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001614 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001617static int udf_alloc_i_data(struct inode *inode, size_t size)
1618{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001619 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Kara382a2282020-09-25 12:29:54 +02001620 iinfo->i_data = kmalloc(size, GFP_KERNEL);
1621 if (!iinfo->i_data)
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001622 return -ENOMEM;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001623 return 0;
1624}
1625
Al Virofaa17292011-07-26 03:18:29 -04001626static umode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Al Virofaa17292011-07-26 03:18:29 -04001628 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 uint32_t permissions;
1630 uint32_t flags;
1631
1632 permissions = le32_to_cpu(fe->permissions);
1633 flags = le16_to_cpu(fe->icbTag.flags);
1634
Fabian Frederick6ff6b2b2017-04-23 20:58:15 +02001635 mode = ((permissions) & 0007) |
1636 ((permissions >> 2) & 0070) |
1637 ((permissions >> 4) & 0700) |
Marcin Slusarz4b111112008-02-08 04:20:36 -08001638 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1639 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1640 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 return mode;
1643}
1644
Steven J. Magnanic3367a12019-08-27 07:13:59 -05001645void udf_update_extra_perms(struct inode *inode, umode_t mode)
1646{
1647 struct udf_inode_info *iinfo = UDF_I(inode);
1648
1649 /*
1650 * UDF 2.01 sec. 3.3.3.3 Note 2:
1651 * In Unix, delete permission tracks write
1652 */
1653 iinfo->i_extraPerms &= ~FE_DELETE_PERMS;
1654 if (mode & 0200)
1655 iinfo->i_extraPerms |= FE_PERM_U_DELETE;
1656 if (mode & 0020)
1657 iinfo->i_extraPerms |= FE_PERM_G_DELETE;
1658 if (mode & 0002)
1659 iinfo->i_extraPerms |= FE_PERM_O_DELETE;
1660}
1661
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001662int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Jan Kara49521de2010-10-20 17:42:44 +02001664 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Jan Kara49521de2010-10-20 17:42:44 +02001667static int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
1669 return udf_update_inode(inode, 1);
1670}
1671
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001672static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec64 time)
Fabian Frederick7ed0fbd2017-01-06 21:53:54 +01001673{
1674 if (iinfo->i_crtime.tv_sec > time.tv_sec ||
1675 (iinfo->i_crtime.tv_sec == time.tv_sec &&
1676 iinfo->i_crtime.tv_nsec > time.tv_nsec))
1677 iinfo->i_crtime = time;
1678}
1679
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001680static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 struct buffer_head *bh = NULL;
1683 struct fileEntry *fe;
1684 struct extendedFileEntry *efe;
Steve Nickelb2527bf2012-02-16 12:53:53 -05001685 uint64_t lb_recorded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 uint32_t udfperms;
1687 uint16_t icbflags;
1688 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001690 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001691 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001692 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Jan Kara5833ded2010-01-08 16:52:59 +01001694 bh = udf_tgetblk(inode->i_sb,
1695 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001696 if (!bh) {
Jan Karaaae917c2010-01-08 16:46:29 +01001697 udf_debug("getblk failure\n");
Changwoo Min0fd2ba32015-03-22 19:17:49 -04001698 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700
Jan Karaaae917c2010-01-08 16:46:29 +01001701 lock_buffer(bh);
1702 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 fe = (struct fileEntry *)bh->b_data;
1704 efe = (struct extendedFileEntry *)bh->b_data;
1705
Jan Karaaae917c2010-01-08 16:46:29 +01001706 if (iinfo->i_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001708 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001710 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001711 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Jan Kara382a2282020-09-25 12:29:54 +02001712 iinfo->i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001713 sizeof(struct unallocSpaceEntry));
Jan Karaaae917c2010-01-08 16:46:29 +01001714 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001715 crclen = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001717 goto finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719
Phillip Susi4d6660e2006-03-07 21:55:24 -08001720 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
Jan Kara0c9850f2018-02-22 10:28:52 +01001721 fe->uid = cpu_to_le32(UDF_INVALID_ID);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001722 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001723 fe->uid = cpu_to_le32(i_uid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Phillip Susi4d6660e2006-03-07 21:55:24 -08001725 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
Jan Kara0c9850f2018-02-22 10:28:52 +01001726 fe->gid = cpu_to_le32(UDF_INVALID_ID);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001727 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001728 fe->gid = cpu_to_le32(i_gid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Fabian Frederick6ff6b2b2017-04-23 20:58:15 +02001730 udfperms = ((inode->i_mode & 0007)) |
1731 ((inode->i_mode & 0070) << 2) |
1732 ((inode->i_mode & 0700) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Steven J. Magnanic3367a12019-08-27 07:13:59 -05001734 udfperms |= iinfo->i_extraPerms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 fe->permissions = cpu_to_le32(udfperms);
1736
Jan Kara8a70ee32014-09-04 11:47:51 +02001737 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1739 else
1740 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1741
1742 fe->informationLength = cpu_to_le64(inode->i_size);
1743
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001744 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001745 struct regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001746 struct deviceSpec *dsea =
1747 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001748 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001750 udf_add_extendedattr(inode,
1751 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001752 sizeof(struct regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 dsea->attrType = cpu_to_le32(12);
1754 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001755 dsea->attrLength = cpu_to_le32(
1756 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001757 sizeof(struct regid));
1758 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001760 eid = (struct regid *)dsea->impUse;
Markus Elfring033c9da2017-08-15 16:45:44 +02001761 memset(eid, 0, sizeof(*eid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 strcpy(eid->ident, UDF_ID_DEVELOPER);
1763 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1764 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1765 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1766 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1767 }
1768
Steve Nickelb2527bf2012-02-16 12:53:53 -05001769 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1770 lb_recorded = 0; /* No extents => no blocks! */
1771 else
1772 lb_recorded =
1773 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1774 (blocksize_bits - 9);
1775
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001776 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001777 memcpy(bh->b_data + sizeof(struct fileEntry),
Jan Kara382a2282020-09-25 12:29:54 +02001778 iinfo->i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001779 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Steve Nickelb2527bf2012-02-16 12:53:53 -05001780 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001782 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1783 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1784 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001785 memset(&(fe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1787 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1788 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001789 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1790 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1791 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001792 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1794 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001795 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001796 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Jan Kara382a2282020-09-25 12:29:54 +02001797 iinfo->i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001798 inode->i_sb->s_blocksize -
1799 sizeof(struct extendedFileEntry));
Steven J. Magnaniab9a3a72019-08-14 07:50:02 -05001800 efe->objectSize =
1801 cpu_to_le64(inode->i_size + iinfo->i_lenStreams);
Steve Nickelb2527bf2012-02-16 12:53:53 -05001802 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Steven J. Magnaniab9a3a72019-08-14 07:50:02 -05001804 if (iinfo->i_streamdir) {
1805 struct long_ad *icb_lad = &efe->streamDirectoryICB;
1806
1807 icb_lad->extLocation =
1808 cpu_to_lelb(iinfo->i_locStreamdir);
1809 icb_lad->extLength =
1810 cpu_to_le32(inode->i_sb->s_blocksize);
1811 }
1812
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001813 udf_adjust_time(iinfo, inode->i_atime);
1814 udf_adjust_time(iinfo, inode->i_mtime);
1815 udf_adjust_time(iinfo, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001817 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1818 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
Marcin Slusarz56774802008-02-10 11:25:31 +01001819 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001820 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Markus Elfring033c9da2017-08-15 16:45:44 +02001822 memset(&(efe->impIdent), 0, sizeof(efe->impIdent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1824 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1825 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001826 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1827 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1828 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001829 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1831 crclen = sizeof(struct extendedFileEntry);
1832 }
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001833
1834finish:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001835 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 fe->icbTag.strategyType = cpu_to_le16(4096);
1837 fe->icbTag.strategyParameter = cpu_to_le16(1);
1838 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001839 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 fe->icbTag.strategyType = cpu_to_le16(4);
1841 fe->icbTag.numEntries = cpu_to_le16(1);
1842 }
1843
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001844 if (iinfo->i_use)
1845 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1846 else if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1848 else if (S_ISREG(inode->i_mode))
1849 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1850 else if (S_ISLNK(inode->i_mode))
1851 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1852 else if (S_ISBLK(inode->i_mode))
1853 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1854 else if (S_ISCHR(inode->i_mode))
1855 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1856 else if (S_ISFIFO(inode->i_mode))
1857 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1858 else if (S_ISSOCK(inode->i_mode))
1859 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1860
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001861 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001862 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1863 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1864 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1865 (le16_to_cpu(fe->icbTag.flags) &
1866 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1867 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001870 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 fe->descTag.descVersion = cpu_to_le16(3);
1872 else
1873 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001874 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001875 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001876 iinfo->i_location.logicalBlockNum);
Jan Karaaae917c2010-01-08 16:46:29 +01001877 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001879 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001880 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001881 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Jan Kara5833ded2010-01-08 16:52:59 +01001883 set_buffer_uptodate(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001884 unlock_buffer(bh);
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 /* write the data blocks */
1887 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001888 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 sync_dirty_buffer(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001890 if (buffer_write_io_error(bh)) {
Joe Perches78ace702011-10-10 01:08:05 -07001891 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1892 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 err = -EIO;
1894 }
1895 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001896 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return err;
1899}
1900
Jan Kara6174c2e2014-10-09 12:52:16 +02001901struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1902 bool hidden_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1905 struct inode *inode = iget_locked(sb, block);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001906 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 if (!inode)
Jan Kara6d3d5e82014-09-04 16:15:51 +02001909 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Jan Kara6d3d5e82014-09-04 16:15:51 +02001911 if (!(inode->i_state & I_NEW))
1912 return inode;
1913
1914 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
Jan Kara6174c2e2014-10-09 12:52:16 +02001915 err = udf_read_inode(inode, hidden_inode);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001916 if (err < 0) {
1917 iget_failed(inode);
1918 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001920 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
Steve Magnanib490bdd2017-10-12 08:48:40 -05001925int udf_setup_indirect_aext(struct inode *inode, udf_pblk_t block,
Jan Karafcea62b2015-12-23 14:21:13 +01001926 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
Jan Karafcea62b2015-12-23 14:21:13 +01001928 struct super_block *sb = inode->i_sb;
1929 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 struct allocExtDesc *aed;
Jan Karafcea62b2015-12-23 14:21:13 +01001931 struct extent_position nepos;
1932 struct kernel_lb_addr neloc;
1933 int ver, adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Jan Karafcea62b2015-12-23 14:21:13 +01001935 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1936 adsize = sizeof(struct short_ad);
1937 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1938 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 else
Arnd Bergmann4f1b1512016-01-01 15:21:54 +01001940 return -EIO;
Jan Karafcea62b2015-12-23 14:21:13 +01001941
1942 neloc.logicalBlockNum = block;
1943 neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1944
1945 bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1946 if (!bh)
1947 return -EIO;
1948 lock_buffer(bh);
1949 memset(bh->b_data, 0x00, sb->s_blocksize);
1950 set_buffer_uptodate(bh);
1951 unlock_buffer(bh);
1952 mark_buffer_dirty_inode(bh, inode);
1953
1954 aed = (struct allocExtDesc *)(bh->b_data);
1955 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1956 aed->previousAllocExtLocation =
1957 cpu_to_le32(epos->block.logicalBlockNum);
1958 }
1959 aed->lengthAllocDescs = cpu_to_le32(0);
1960 if (UDF_SB(sb)->s_udfrev >= 0x0200)
1961 ver = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 else
Jan Karafcea62b2015-12-23 14:21:13 +01001963 ver = 2;
1964 udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1965 sizeof(struct tag));
1966
1967 nepos.block = neloc;
1968 nepos.offset = sizeof(struct allocExtDesc);
1969 nepos.bh = bh;
1970
1971 /*
1972 * Do we have to copy current last extent to make space for indirect
1973 * one?
1974 */
1975 if (epos->offset + adsize > sb->s_blocksize) {
1976 struct kernel_lb_addr cp_loc;
1977 uint32_t cp_len;
1978 int cp_type;
1979
1980 epos->offset -= adsize;
1981 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1982 cp_len |= ((uint32_t)cp_type) << 30;
1983
1984 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1985 udf_write_aext(inode, epos, &nepos.block,
Pali Rohár800552c2020-01-07 22:29:02 +01001986 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
Jan Karafcea62b2015-12-23 14:21:13 +01001987 } else {
1988 __udf_add_aext(inode, epos, &nepos.block,
Pali Rohár800552c2020-01-07 22:29:02 +01001989 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
Jan Karafcea62b2015-12-23 14:21:13 +01001990 }
1991
1992 brelse(epos->bh);
1993 *epos = nepos;
1994
1995 return 0;
1996}
1997
1998/*
1999 * Append extent at the given position - should be the first free one in inode
2000 * / indirect extent. This function assumes there is enough space in the inode
2001 * or indirect extent. Use udf_add_aext() if you didn't check for this before.
2002 */
2003int __udf_add_aext(struct inode *inode, struct extent_position *epos,
2004 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2005{
2006 struct udf_inode_info *iinfo = UDF_I(inode);
2007 struct allocExtDesc *aed;
2008 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002010 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002011 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002012 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002013 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 else
Jan Kara7e49b6f2010-10-22 00:30:26 +02002015 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Jan Karafcea62b2015-12-23 14:21:13 +01002017 if (!epos->bh) {
2018 WARN_ON(iinfo->i_lenAlloc !=
2019 epos->offset - udf_file_entry_alloc_offset(inode));
2020 } else {
2021 aed = (struct allocExtDesc *)epos->bh->b_data;
2022 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
2023 epos->offset - sizeof(struct allocExtDesc));
2024 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
2026
Jan Kara7e49b6f2010-10-22 00:30:26 +02002027 udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002029 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002030 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002032 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002033 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002034 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002035 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2036 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2037 udf_update_tag(epos->bh->b_data,
2038 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002040 udf_update_tag(epos->bh->b_data,
2041 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002042 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
2044
Jan Kara7e49b6f2010-10-22 00:30:26 +02002045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
Jan Karafcea62b2015-12-23 14:21:13 +01002048/*
2049 * Append extent at given position - should be the first free one in inode
2050 * / indirect extent. Takes care of allocating and linking indirect blocks.
2051 */
2052int udf_add_aext(struct inode *inode, struct extent_position *epos,
2053 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2054{
2055 int adsize;
2056 struct super_block *sb = inode->i_sb;
2057
2058 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2059 adsize = sizeof(struct short_ad);
2060 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2061 adsize = sizeof(struct long_ad);
2062 else
2063 return -EIO;
2064
2065 if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2066 int err;
Steve Magnanib490bdd2017-10-12 08:48:40 -05002067 udf_pblk_t new_block;
Jan Karafcea62b2015-12-23 14:21:13 +01002068
2069 new_block = udf_new_block(sb, NULL,
2070 epos->block.partitionReferenceNum,
2071 epos->block.logicalBlockNum, &err);
2072 if (!new_block)
2073 return -ENOSPC;
2074
2075 err = udf_setup_indirect_aext(inode, new_block, epos);
2076 if (err)
2077 return err;
2078 }
2079
2080 return __udf_add_aext(inode, epos, eloc, elen, inc);
2081}
2082
Jan Kara7e49b6f2010-10-22 00:30:26 +02002083void udf_write_aext(struct inode *inode, struct extent_position *epos,
2084 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
2086 int adsize;
2087 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002088 struct short_ad *sad;
2089 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002090 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Jan Karaff116fc2007-05-08 00:35:14 -07002092 if (!epos->bh)
Jan Kara382a2282020-09-25 12:29:54 +02002093 ptr = iinfo->i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002094 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002095 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 else
Jan Karaff116fc2007-05-08 00:35:14 -07002097 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002099 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002100 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002101 sad = (struct short_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002102 sad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002103 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002104 adsize = sizeof(struct short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002105 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002106 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002107 lad = (struct long_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002108 lad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002109 lad->extLocation = cpu_to_lelb(*eloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002110 memset(lad->impUse, 0x00, sizeof(lad->impUse));
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002111 adsize = sizeof(struct long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002112 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002113 default:
Jan Kara7e49b6f2010-10-22 00:30:26 +02002114 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
2116
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002117 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002118 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002119 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002120 struct allocExtDesc *aed =
2121 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07002122 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002123 le32_to_cpu(aed->lengthAllocDescs) +
2124 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
Jan Karaff116fc2007-05-08 00:35:14 -07002126 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002127 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07002132 epos->offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
Vegard Nossumb0918d92015-12-11 15:54:16 +01002135/*
2136 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2137 * someone does some weird stuff.
2138 */
2139#define UDF_MAX_INDIR_EXTS 16
2140
Marcin Slusarz4b111112008-02-08 04:20:36 -08002141int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002142 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
2144 int8_t etype;
Vegard Nossumb0918d92015-12-11 15:54:16 +01002145 unsigned int indirections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Jan Karaff116fc2007-05-08 00:35:14 -07002147 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Pali Rohár800552c2020-01-07 22:29:02 +01002148 (EXT_NEXT_EXTENT_ALLOCDESCS >> 30)) {
Steve Magnanib490bdd2017-10-12 08:48:40 -05002149 udf_pblk_t block;
Vegard Nossumb0918d92015-12-11 15:54:16 +01002150
2151 if (++indirections > UDF_MAX_INDIR_EXTS) {
2152 udf_err(inode->i_sb,
2153 "too many indirect extents in inode %lu\n",
2154 inode->i_ino);
2155 return -1;
2156 }
2157
Jan Karaff116fc2007-05-08 00:35:14 -07002158 epos->block = *eloc;
2159 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002160 brelse(epos->bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002161 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002162 epos->bh = udf_tread(inode->i_sb, block);
2163 if (!epos->bh) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05002164 udf_debug("reading block %u failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 return -1;
2166 }
2167 }
2168
2169 return etype;
2170}
2171
Marcin Slusarz4b111112008-02-08 04:20:36 -08002172int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002173 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
2175 int alen;
2176 int8_t etype;
2177 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002178 struct short_ad *sad;
2179 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002180 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002181
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002182 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002183 if (!epos->offset)
2184 epos->offset = udf_file_entry_alloc_offset(inode);
Jan Kara382a2282020-09-25 12:29:54 +02002185 ptr = iinfo->i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002186 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002187 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08002188 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002189 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002190 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002191 if (!epos->offset)
2192 epos->offset = sizeof(struct allocExtDesc);
2193 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002194 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08002195 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2196 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
2198
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002199 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002200 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002201 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2202 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002204 etype = le32_to_cpu(sad->extLength) >> 30;
2205 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002206 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002207 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002208 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2209 break;
2210 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002211 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2212 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002213 return -1;
2214 etype = le32_to_cpu(lad->extLength) >> 30;
2215 *eloc = lelb_to_cpu(lad->extLocation);
2216 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2217 break;
2218 default:
Steve Magnanifcbf7632017-10-12 08:48:41 -05002219 udf_debug("alloc_type = %u unsupported\n", iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002220 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
2222
2223 return etype;
2224}
2225
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002226static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002227 struct kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002229 struct kernel_lb_addr oeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 uint32_t oelen;
2231 int8_t etype;
2232
Jan Karaff116fc2007-05-08 00:35:14 -07002233 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07002234 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002236 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002237 udf_write_aext(inode, &epos, &neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 neloc = oeloc;
2239 nelen = (etype << 30) | oelen;
2240 }
Pekka Enberg97e961f2008-10-15 12:29:03 +02002241 udf_add_aext(inode, &epos, &neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002242 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return (nelen >> 30);
2245}
2246
Jan Kara6c1e4d02018-06-13 18:04:24 +02002247int8_t udf_delete_aext(struct inode *inode, struct extent_position epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Jan Karaff116fc2007-05-08 00:35:14 -07002249 struct extent_position oepos;
2250 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 int8_t etype;
2252 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002253 struct udf_inode_info *iinfo;
Jan Kara6c1e4d02018-06-13 18:04:24 +02002254 struct kernel_lb_addr eloc;
2255 uint32_t elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002257 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002258 get_bh(epos.bh);
2259 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002262 iinfo = UDF_I(inode);
2263 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002264 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002265 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002266 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 else
2268 adsize = 0;
2269
Jan Karaff116fc2007-05-08 00:35:14 -07002270 oepos = epos;
2271 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 return -1;
2273
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002274 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002275 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002276 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002277 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07002278 brelse(oepos.bh);
2279 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07002280 oepos.bh = epos.bh;
2281 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
2283 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002284 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 elen = 0;
2286
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002287 if (epos.bh != oepos.bh) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002288 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2289 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2290 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002291 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002292 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002294 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002295 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002296 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002297 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002298 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002299 udf_update_tag(oepos.bh->b_data,
2300 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002302 udf_update_tag(oepos.bh->b_data,
2303 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002304 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002306 } else {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002307 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002308 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002309 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002311 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002312 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002313 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002314 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002315 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002316 udf_update_tag(oepos.bh->b_data,
2317 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002319 udf_update_tag(oepos.bh->b_data,
2320 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002321 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
2323 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002324
Jan Kara3bf25cb2007-05-08 00:35:16 -07002325 brelse(epos.bh);
2326 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return (elen >> 30);
2329}
2330
Marcin Slusarz4b111112008-02-08 04:20:36 -08002331int8_t inode_bmap(struct inode *inode, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002332 struct extent_position *pos, struct kernel_lb_addr *eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002333 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002335 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Fabian Frederickbbc9abd2017-01-06 21:53:52 +01002336 loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002338 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002340 iinfo = UDF_I(inode);
Namjae Jeon99600052013-01-19 11:17:14 +09002341 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2342 pos->offset = 0;
2343 pos->block = iinfo->i_location;
2344 pos->bh = NULL;
2345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 *elen = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002347 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002348 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2349 if (etype == -1) {
2350 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002351 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 return -1;
2353 }
2354 lbcount += *elen;
2355 } while (lbcount <= bcount);
Namjae Jeon99600052013-01-19 11:17:14 +09002356 /* update extent cache */
Fabian Frederickb31c9ed2017-01-06 21:53:56 +01002357 udf_update_extent_cache(inode, lbcount - *elen, pos);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002358 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 return etype;
2361}
2362
Steve Magnanib490bdd2017-10-12 08:48:40 -05002363udf_pblk_t udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002365 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -07002366 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002367 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002368 struct extent_position epos = {};
Steve Magnanib490bdd2017-10-12 08:48:40 -05002369 udf_pblk_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002371 down_read(&UDF_I(inode)->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Marcin Slusarz4b111112008-02-08 04:20:36 -08002373 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2374 (EXT_RECORDED_ALLOCATED >> 30))
Pekka Enberg97e961f2008-10-15 12:29:03 +02002375 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 else
2377 ret = 0;
2378
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002379 up_read(&UDF_I(inode)->i_data_sem);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002380 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2383 return udf_fixed_to_variable(ret);
2384 else
2385 return ret;
2386}