blob: 1d6b7a50736bac681f03dc8af2ca7f8bb9f26681 [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 struct writeback_control udf_wbc = {
262 .sync_mode = WB_SYNC_NONE,
263 .nr_to_write = 1,
264 };
265
Al Viro59551022016-01-22 15:40:57 -0500266 WARN_ON_ONCE(!inode_is_locked(inode));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800267 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800269 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800271 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200272 /* from now on we have normal address_space methods */
273 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100274 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100278 /*
279 * Release i_data_sem so that we can lock a page - page lock ranks
280 * above i_data_sem. i_mutex still protects us against file changes.
281 */
282 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Jan Kara7e49b6f2010-10-22 00:30:26 +0200284 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
285 if (!page)
286 return -ENOMEM;
Matt Mackallcd7619d2005-05-01 08:59:01 -0700287
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700288 if (!PageUptodate(page)) {
Fabian Frederick5c26eac2017-04-23 20:58:34 +0200289 kaddr = kmap_atomic(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800290 memset(kaddr + iinfo->i_lenAlloc, 0x00,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300291 PAGE_SIZE - iinfo->i_lenAlloc);
Jan Kara382a2282020-09-25 12:29:54 +0200292 memcpy(kaddr, iinfo->i_data + iinfo->i_lenEAttr,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800293 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 flush_dcache_page(page);
295 SetPageUptodate(page);
Fabian Frederick5c26eac2017-04-23 20:58:34 +0200296 kunmap_atomic(kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100298 down_write(&iinfo->i_data_sem);
Jan Kara382a2282020-09-25 12:29:54 +0200299 memset(iinfo->i_data + iinfo->i_lenEAttr, 0x00,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800300 iinfo->i_lenAlloc);
301 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800303 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800305 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200306 /* from now on we have normal address_space methods */
307 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100308 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200309 err = inode->i_data.a_ops->writepage(page, &udf_wbc);
310 if (err) {
311 /* Restore everything back so that we don't lose data... */
312 lock_page(page);
Jan Karad2eb8c32011-12-10 02:30:48 +0100313 down_write(&iinfo->i_data_sem);
Fabian Frederick5c26eac2017-04-23 20:58:34 +0200314 kaddr = kmap_atomic(page);
Jan Kara382a2282020-09-25 12:29:54 +0200315 memcpy(iinfo->i_data + iinfo->i_lenEAttr, kaddr, inode->i_size);
Fabian Frederick5c26eac2017-04-23 20:58:34 +0200316 kunmap_atomic(kaddr);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200317 unlock_page(page);
318 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
319 inode->i_data.a_ops = &udf_adinicb_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100320 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200321 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300322 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200324
325 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Steve Magnanib490bdd2017-10-12 08:48:40 -0500328struct buffer_head *udf_expand_dir_adinicb(struct inode *inode,
329 udf_pblk_t *block, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Steve Magnanib490bdd2017-10-12 08:48:40 -0500331 udf_pblk_t newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700332 struct buffer_head *dbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200333 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700335 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 struct udf_fileident_bh sfibh, dfibh;
Jan Karaaf793292008-02-08 04:20:50 -0800338 loff_t f_pos = udf_ext0_offset(inode);
339 int size = udf_ext0_offset(inode) + inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800341 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
344 alloctype = ICBTAG_FLAG_AD_SHORT;
345 else
346 alloctype = ICBTAG_FLAG_AD_LONG;
347
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700348 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800349 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 mark_inode_dirty(inode);
351 return NULL;
352 }
353
354 /* alloc block, and copy data to it */
355 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800356 iinfo->i_location.partitionReferenceNum,
357 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (!(*block))
359 return NULL;
360 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800361 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800362 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (!newblock)
364 return NULL;
365 dbh = udf_tgetblk(inode->i_sb, newblock);
366 if (!dbh)
367 return NULL;
368 lock_buffer(dbh);
369 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
370 set_buffer_uptodate(dbh);
371 unlock_buffer(dbh);
372 mark_buffer_dirty_inode(dbh, inode);
373
Marcin Slusarz4b111112008-02-08 04:20:36 -0800374 sfibh.soffset = sfibh.eoffset =
Jan Karaaf793292008-02-08 04:20:50 -0800375 f_pos & (inode->i_sb->s_blocksize - 1);
Jan Karaff116fc2007-05-08 00:35:14 -0700376 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 dfibh.soffset = dfibh.eoffset = 0;
378 dfibh.sbh = dfibh.ebh = dbh;
Jan Karaaf793292008-02-08 04:20:50 -0800379 while (f_pos < size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800380 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800381 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
382 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700383 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700384 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return NULL;
386 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800387 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 sfi->descTag.tagLocation = cpu_to_le32(*block);
389 dfibh.soffset = dfibh.eoffset;
390 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
391 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
392 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Jan Kara979a6e22021-05-03 11:54:24 +0200393 udf_get_fi_ident(sfi))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800394 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700395 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return NULL;
397 }
398 }
399 mark_buffer_dirty_inode(dbh, inode);
400
Jan Kara382a2282020-09-25 12:29:54 +0200401 memset(iinfo->i_data + iinfo->i_lenEAttr, 0, iinfo->i_lenAlloc);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800402 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800404 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800405 iinfo->i_location.partitionReferenceNum;
Jan Kara2c948b32009-12-03 13:39:28 +0100406 iinfo->i_lenExtents = inode->i_size;
Jan Karaff116fc2007-05-08 00:35:14 -0700407 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800408 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700409 epos.offset = udf_file_entry_alloc_offset(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100410 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* UniqueID stuff */
412
Jan Kara3bf25cb2007-05-08 00:35:16 -0700413 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 mark_inode_dirty(inode);
415 return dbh;
416}
417
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700418static int udf_get_block(struct inode *inode, sector_t block,
419 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 int err, new;
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800422 sector_t phys = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800423 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700425 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 phys = udf_block_map(inode, block);
427 if (phys)
428 map_bh(bh_result, inode->i_sb, phys);
429 return 0;
430 }
431
432 err = -EIO;
433 new = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800434 iinfo = UDF_I(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100435
436 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800437 if (block == iinfo->i_next_alloc_block + 1) {
438 iinfo->i_next_alloc_block++;
439 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
Namjae Jeon99600052013-01-19 11:17:14 +0900442 udf_clear_extent_cache(inode);
Jan Kara7b0b0932011-12-10 01:43:33 +0100443 phys = inode_getblk(inode, block, &err, &new);
444 if (!phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 if (new)
448 set_buffer_new(bh_result);
449 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700450
451abort:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100452 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Steve Magnanib490bdd2017-10-12 08:48:40 -0500456static struct buffer_head *udf_getblk(struct inode *inode, udf_pblk_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700457 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700459 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct buffer_head dummy;
461
462 dummy.b_state = 0;
463 dummy.b_blocknr = -1000;
464 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700465 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700467 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 lock_buffer(bh);
469 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
470 set_buffer_uptodate(bh);
471 unlock_buffer(bh);
472 mark_buffer_dirty_inode(bh, inode);
473 }
474 return bh;
475 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return NULL;
478}
479
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500480/* Extend the file with new blocks totaling 'new_block_bytes',
481 * return the number of extents added
482 */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200483static int udf_do_extend_file(struct inode *inode,
484 struct extent_position *last_pos,
485 struct kernel_long_ad *last_ext,
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500486 loff_t new_block_bytes)
Jan Kara31170b62007-05-08 00:35:21 -0700487{
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500488 uint32_t add;
Jan Kara31170b62007-05-08 00:35:21 -0700489 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
490 struct super_block *sb = inode->i_sb;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200491 struct kernel_lb_addr prealloc_loc = {};
Steve Magnani89a4d972017-10-12 08:48:42 -0500492 uint32_t prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800493 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200494 int err;
Jan Kara31170b62007-05-08 00:35:21 -0700495
496 /* The previous extent is fake and we should not extend by anything
497 * - there's nothing to do... */
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500498 if (!new_block_bytes && fake)
Jan Kara31170b62007-05-08 00:35:21 -0700499 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700500
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800501 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700502 /* Round the last extent up to a multiple of block size */
503 if (last_ext->extLength & (sb->s_blocksize - 1)) {
504 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700505 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
506 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
507 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800508 iinfo->i_lenExtents =
509 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700510 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700511 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700512
Jan Kara31170b62007-05-08 00:35:21 -0700513 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800514 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
515 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700516 /* Save the extent so that we can reattach it to the end */
517 prealloc_loc = last_ext->extLocation;
518 prealloc_len = last_ext->extLength;
519 /* Mark the extent as a hole */
520 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700521 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700522 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800523 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700524 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700525
Jan Kara31170b62007-05-08 00:35:21 -0700526 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800527 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
528 EXT_NOT_RECORDED_NOT_ALLOCATED) {
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500529 add = (1 << 30) - sb->s_blocksize -
530 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
531 if (add > new_block_bytes)
532 add = new_block_bytes;
533 new_block_bytes -= add;
534 last_ext->extLength += add;
Jan Kara31170b62007-05-08 00:35:21 -0700535 }
536
537 if (fake) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200538 udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700539 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700540 count++;
Jan Kara6c371572015-12-23 18:05:03 +0100541 } else {
542 struct kernel_lb_addr tmploc;
543 uint32_t tmplen;
544
Pekka Enberg97e961f2008-10-15 12:29:03 +0200545 udf_write_aext(inode, last_pos, &last_ext->extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800546 last_ext->extLength, 1);
Steven J. Magnani63c9e472021-01-07 17:41:16 -0600547
Jan Kara6c371572015-12-23 18:05:03 +0100548 /*
Steven J. Magnani63c9e472021-01-07 17:41:16 -0600549 * We've rewritten the last extent. If we are going to add
550 * more extents, we may need to enter possible following
551 * empty indirect extent.
Jan Kara6c371572015-12-23 18:05:03 +0100552 */
Steven J. Magnani63c9e472021-01-07 17:41:16 -0600553 if (new_block_bytes || prealloc_len)
554 udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
Jan Kara6c371572015-12-23 18:05:03 +0100555 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700556
Jan Kara31170b62007-05-08 00:35:21 -0700557 /* Managed to do everything necessary? */
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500558 if (!new_block_bytes)
Jan Kara31170b62007-05-08 00:35:21 -0700559 goto out;
560
561 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
562 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800563 last_ext->extLocation.partitionReferenceNum = 0;
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500564 add = (1 << 30) - sb->s_blocksize;
565 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700566
Jan Kara31170b62007-05-08 00:35:21 -0700567 /* Create enough extents to cover the whole hole */
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500568 while (new_block_bytes > add) {
569 new_block_bytes -= add;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200570 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
571 last_ext->extLength, 1);
572 if (err)
573 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700574 count++;
575 }
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500576 if (new_block_bytes) {
Jan Kara31170b62007-05-08 00:35:21 -0700577 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500578 new_block_bytes;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200579 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
580 last_ext->extLength, 1);
581 if (err)
582 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700583 count++;
584 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700585
586out:
Jan Kara31170b62007-05-08 00:35:21 -0700587 /* Do we have some preallocated blocks saved? */
588 if (prealloc_len) {
Jan Kara7e49b6f2010-10-22 00:30:26 +0200589 err = udf_add_aext(inode, last_pos, &prealloc_loc,
590 prealloc_len, 1);
591 if (err)
592 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700593 last_ext->extLocation = prealloc_loc;
594 last_ext->extLength = prealloc_len;
595 count++;
596 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700597
Jan Kara31170b62007-05-08 00:35:21 -0700598 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800599 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200600 last_pos->offset -= sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800601 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200602 last_pos->offset -= sizeof(struct long_ad);
Jan Kara31170b62007-05-08 00:35:21 -0700603 else
Jan Kara7e49b6f2010-10-22 00:30:26 +0200604 return -EIO;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700605
Jan Kara31170b62007-05-08 00:35:21 -0700606 return count;
607}
608
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500609/* Extend the final block of the file to final_block_len bytes */
610static void udf_do_extend_final_block(struct inode *inode,
611 struct extent_position *last_pos,
612 struct kernel_long_ad *last_ext,
613 uint32_t final_block_len)
614{
615 struct super_block *sb = inode->i_sb;
616 uint32_t added_bytes;
617
618 added_bytes = final_block_len -
619 (last_ext->extLength & (sb->s_blocksize - 1));
620 last_ext->extLength += added_bytes;
621 UDF_I(inode)->i_lenExtents += added_bytes;
622
623 udf_write_aext(inode, last_pos, &last_ext->extLocation,
624 last_ext->extLength, 1);
625}
626
Jan Kara7e49b6f2010-10-22 00:30:26 +0200627static int udf_extend_file(struct inode *inode, loff_t newsize)
628{
629
630 struct extent_position epos;
631 struct kernel_lb_addr eloc;
632 uint32_t elen;
633 int8_t etype;
634 struct super_block *sb = inode->i_sb;
635 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500636 unsigned long partial_final_block;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200637 int adsize;
638 struct udf_inode_info *iinfo = UDF_I(inode);
639 struct kernel_long_ad extent;
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500640 int err = 0;
641 int within_final_block;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200642
643 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
644 adsize = sizeof(struct short_ad);
645 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
646 adsize = sizeof(struct long_ad);
647 else
648 BUG();
649
650 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500651 within_final_block = (etype != -1);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200652
Jan Kara7e49b6f2010-10-22 00:30:26 +0200653 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
654 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
655 /* File has no extents at all or has empty last
656 * indirect extent! Create a fake extent... */
657 extent.extLocation.logicalBlockNum = 0;
658 extent.extLocation.partitionReferenceNum = 0;
659 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
660 } else {
661 epos.offset -= adsize;
662 etype = udf_next_aext(inode, &epos, &extent.extLocation,
663 &extent.extLength, 0);
664 extent.extLength |= etype << 30;
665 }
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500666
667 partial_final_block = newsize & (sb->s_blocksize - 1);
668
669 /* File has extent covering the new size (could happen when extending
670 * inside a block)?
671 */
672 if (within_final_block) {
673 /* Extending file within the last file block */
674 udf_do_extend_final_block(inode, &epos, &extent,
675 partial_final_block);
676 } else {
677 loff_t add = ((loff_t)offset << sb->s_blocksize_bits) |
678 partial_final_block;
679 err = udf_do_extend_file(inode, &epos, &extent, add);
680 }
681
Jan Kara7e49b6f2010-10-22 00:30:26 +0200682 if (err < 0)
683 goto out;
684 err = 0;
685 iinfo->i_lenExtents = newsize;
686out:
687 brelse(epos.bh);
688 return err;
689}
690
Jan Kara7b0b0932011-12-10 01:43:33 +0100691static sector_t inode_getblk(struct inode *inode, sector_t block,
692 int *err, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200694 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700695 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700697 uint32_t elen = 0, tmpelen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200698 struct kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700700 loff_t lbcount = 0, b_off = 0;
Steve Magnanib490bdd2017-10-12 08:48:40 -0500701 udf_pblk_t newblocknum, newblock;
Jan Kara60448b12007-05-08 00:35:13 -0700702 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800704 struct udf_inode_info *iinfo = UDF_I(inode);
Steve Magnanib490bdd2017-10-12 08:48:40 -0500705 udf_pblk_t goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700706 int lastblock = 0;
Namjae Jeonfb719c52012-10-10 00:09:12 +0900707 bool isBeyondEOF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Jan Kara7b0b0932011-12-10 01:43:33 +0100709 *err = 0;
710 *new = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700711 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800712 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700713 prev_epos.bh = NULL;
714 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700715 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700718 alternate between laarr[0] and laarr[1] for locations of the
719 current extent, and the previous extent */
720 do {
721 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700722 brelse(prev_epos.bh);
723 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700724 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700726 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700727 brelse(cur_epos.bh);
728 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700729 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731
732 lbcount += elen;
733
Jan Karaff116fc2007-05-08 00:35:14 -0700734 prev_epos.block = cur_epos.block;
735 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Jan Karaff116fc2007-05-08 00:35:14 -0700737 prev_epos.offset = cur_epos.offset;
738 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Marcin Slusarz4b111112008-02-08 04:20:36 -0800740 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
741 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 break;
743
744 c = !c;
745
746 laarr[c].extLength = (etype << 30) | elen;
747 laarr[c].extLocation = eloc;
748
749 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
750 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700751 ((elen + inode->i_sb->s_blocksize - 1) >>
752 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700754 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 } while (lbcount + elen <= b_off);
756
757 b_off -= lbcount;
758 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700759 /*
760 * Move prev_epos and cur_epos into indirect extent if we are at
761 * the pointer to it
762 */
763 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
764 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700767 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700769 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
770 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700772 ((elen + inode->i_sb->s_blocksize - 1) &
773 ~(inode->i_sb->s_blocksize - 1));
Jan Kara7e49b6f2010-10-22 00:30:26 +0200774 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200776 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Fabian Frederick02d4ca42017-01-06 21:53:51 +0100777 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779
Jan Kara31170b62007-05-08 00:35:21 -0700780 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700781 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700782 int ret;
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500783 loff_t hole_len;
Fabian Frederick69814982015-02-04 18:26:27 +0100784 isBeyondEOF = true;
Jan Kara31170b62007-05-08 00:35:21 -0700785 if (count) {
786 if (c)
787 laarr[0] = laarr[1];
788 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700789 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700790 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800791 memset(&laarr[0].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200792 sizeof(struct kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700793 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200794 /* Will udf_do_extend_file() create real extent from
Marcin Slusarz4b111112008-02-08 04:20:36 -0800795 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700796 startnum = (offset > 0);
797 }
798 /* Create extents for the hole between EOF and offset */
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500799 hole_len = (loff_t)offset << inode->i_blkbits;
800 ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200801 if (ret < 0) {
Jan Kara7e49b6f2010-10-22 00:30:26 +0200802 *err = ret;
Fabian Frederick02d4ca42017-01-06 21:53:51 +0100803 newblock = 0;
804 goto out_free;
Jan Kara31170b62007-05-08 00:35:21 -0700805 }
806 c = 0;
807 offset = 0;
808 count += ret;
809 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800810 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
811 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700812 /* Is there any real extent? - otherwise we overwrite
813 * the fake one... */
814 if (count)
815 c = !c;
816 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700817 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800818 memset(&laarr[c].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200819 sizeof(struct kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700820 count++;
Jan Kara31170b62007-05-08 00:35:21 -0700821 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700822 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700824 } else {
Fabian Frederick69814982015-02-04 18:26:27 +0100825 isBeyondEOF = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 endnum = startnum = ((count > 2) ? 2 : count);
827
Marcin Slusarz4b111112008-02-08 04:20:36 -0800828 /* if the current extent is in position 0,
829 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700830 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700831 laarr[2] = laarr[0];
832 laarr[0] = laarr[1];
833 laarr[1] = laarr[2];
834 c = 1;
835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Marcin Slusarz4b111112008-02-08 04:20:36 -0800837 /* if the current block is located in an extent,
838 read the next extent */
839 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
840 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700841 laarr[c + 1].extLength = (etype << 30) | elen;
842 laarr[c + 1].extLocation = eloc;
843 count++;
844 startnum++;
845 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800846 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 lastblock = 1;
848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700851 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800852 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800854 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800855 if (iinfo->i_next_alloc_block == block)
856 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700858 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800859 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800860 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862
Marcin Slusarz4b111112008-02-08 04:20:36 -0800863 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800864 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800865 goal, err);
866 if (!newblocknum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 *err = -ENOSPC;
Fabian Frederick02d4ca42017-01-06 21:53:51 +0100868 newblock = 0;
869 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
Namjae Jeonfb719c52012-10-10 00:09:12 +0900871 if (isBeyondEOF)
872 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874
Marcin Slusarz4b111112008-02-08 04:20:36 -0800875 /* if the extent the requsted block is located in contains multiple
876 * blocks, split the extent into at most three extents. blocks prior
877 * to requested block, requested block, and blocks after requested
878 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
880
Jan Kara81056dd2009-07-16 18:02:25 +0200881 /* We preallocate blocks only for regular files. It also makes sense
882 * for directories but there's a problem when to drop the
883 * preallocation. We might use some delayed work for that but I feel
884 * it's overengineering for a filesystem like UDF. */
885 if (S_ISREG(inode->i_mode))
886 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 /* merge any continuous blocks in laarr */
889 udf_merge_extents(inode, laarr, &endnum);
890
891 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700892 * of extents is greater than the old number, and deleting extents if
893 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700894 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Marcin Slusarz4b111112008-02-08 04:20:36 -0800896 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800897 iinfo->i_location.partitionReferenceNum, 0);
Jan Kara7b0b0932011-12-10 01:43:33 +0100898 if (!newblock) {
899 *err = -EIO;
Fabian Frederick02d4ca42017-01-06 21:53:51 +0100900 goto out_free;
Jan Kara7b0b0932011-12-10 01:43:33 +0100901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800903 iinfo->i_next_alloc_block = block;
904 iinfo->i_next_alloc_goal = newblocknum;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700905 inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 if (IS_SYNC(inode))
908 udf_sync_inode(inode);
909 else
910 mark_inode_dirty(inode);
Fabian Frederick02d4ca42017-01-06 21:53:51 +0100911out_free:
912 brelse(prev_epos.bh);
913 brelse(cur_epos.bh);
914 brelse(next_epos.bh);
Jan Kara7b0b0932011-12-10 01:43:33 +0100915 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700918static void udf_split_extents(struct inode *inode, int *c, int offset,
Steve Magnanib490bdd2017-10-12 08:48:40 -0500919 udf_pblk_t newblocknum,
920 struct kernel_long_ad *laarr, int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800922 unsigned long blocksize = inode->i_sb->s_blocksize;
923 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800926 (laarr[*c].extLength >> 30) ==
927 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 int curr = *c;
929 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800930 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 int8_t etype = (laarr[curr].extLength >> 30);
932
Marcin Slusarz4b111112008-02-08 04:20:36 -0800933 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700934 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800935 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700936 laarr[curr + 2] = laarr[curr + 1];
937 laarr[curr + 1] = laarr[curr];
938 } else {
939 laarr[curr + 3] = laarr[curr + 1];
940 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700943 if (offset) {
944 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800945 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200946 &laarr[curr].extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800947 0, offset);
948 laarr[curr].extLength =
949 EXT_NOT_RECORDED_NOT_ALLOCATED |
950 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800952 laarr[curr].extLocation.
953 partitionReferenceNum = 0;
954 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800956 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700957 curr++;
958 (*c)++;
959 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 laarr[curr].extLocation.logicalBlockNum = newblocknum;
963 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
964 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800965 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800967 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700968 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700970 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800972 laarr[curr].extLocation.logicalBlockNum +=
973 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700974 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800975 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700976 curr++;
977 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979 }
980}
981
982static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Fabian Frederick3cc6f842017-01-06 21:53:50 +0100983 struct kernel_long_ad *laarr,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700984 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 int start, length = 0, currlength = 0, i;
987
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700988 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (!lastblock)
990 return;
991 else
992 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700993 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800994 if ((laarr[c + 1].extLength >> 30) ==
995 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700996 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800997 length = currlength =
998 (((laarr[c + 1].extLength &
999 UDF_EXTENT_LENGTH_MASK) +
1000 inode->i_sb->s_blocksize - 1) >>
1001 inode->i_sb->s_blocksize_bits);
1002 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 start = c;
1004 }
1005
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001006 for (i = start + 1; i <= *endnum; i++) {
1007 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (lastblock)
1009 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001010 } else if ((laarr[i].extLength >> 30) ==
1011 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
1012 length += (((laarr[i].extLength &
1013 UDF_EXTENT_LENGTH_MASK) +
1014 inode->i_sb->s_blocksize - 1) >>
1015 inode->i_sb->s_blocksize_bits);
1016 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 break;
1018 }
1019
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001020 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001022 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08001023 inode->i_sb->s_blocksize - 1) >>
1024 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001026 laarr[start].extLocation.partitionReferenceNum,
1027 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1028 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1029 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001030 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001031 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001033 (numalloc <<
1034 inode->i_sb->s_blocksize_bits);
1035 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001036 memmove(&laarr[c + 2], &laarr[c + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001037 sizeof(struct long_ad) * (*endnum - (c + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001038 (*endnum)++;
1039 laarr[c + 1].extLocation.logicalBlockNum = next;
1040 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -08001041 laarr[c].extLocation.
1042 partitionReferenceNum;
1043 laarr[c + 1].extLength =
1044 EXT_NOT_RECORDED_ALLOCATED |
1045 (numalloc <<
1046 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001047 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001050 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001051 int elen = ((laarr[i].extLength &
1052 UDF_EXTENT_LENGTH_MASK) +
1053 inode->i_sb->s_blocksize - 1) >>
1054 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001056 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001058 (numalloc <<
1059 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001061 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001063 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001064 memmove(&laarr[i],
1065 &laarr[i + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001066 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001067 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001068 i--;
1069 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001072 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001073 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075 }
1076}
1077
Fabian Frederick3cc6f842017-01-06 21:53:50 +01001078static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001079 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001082 unsigned long blocksize = inode->i_sb->s_blocksize;
1083 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001085 for (i = 0; i < (*endnum - 1); i++) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001086 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1087 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Marcin Slusarz4b111112008-02-08 04:20:36 -08001089 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1090 (((li->extLength >> 30) ==
1091 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1092 ((lip1->extLocation.logicalBlockNum -
1093 li->extLocation.logicalBlockNum) ==
1094 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1095 blocksize - 1) >> blocksize_bits)))) {
1096
1097 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1098 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1099 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1100 lip1->extLength = (lip1->extLength -
1101 (li->extLength &
1102 UDF_EXTENT_LENGTH_MASK) +
1103 UDF_EXTENT_LENGTH_MASK) &
1104 ~(blocksize - 1);
1105 li->extLength = (li->extLength &
1106 UDF_EXTENT_FLAG_MASK) +
1107 (UDF_EXTENT_LENGTH_MASK + 1) -
1108 blocksize;
1109 lip1->extLocation.logicalBlockNum =
1110 li->extLocation.logicalBlockNum +
1111 ((li->extLength &
1112 UDF_EXTENT_LENGTH_MASK) >>
1113 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001114 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001115 li->extLength = lip1->extLength +
1116 (((li->extLength &
1117 UDF_EXTENT_LENGTH_MASK) +
1118 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001119 if (*endnum > (i + 2))
1120 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001121 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001122 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001123 i--;
1124 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001126 } else if (((li->extLength >> 30) ==
1127 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1128 ((lip1->extLength >> 30) ==
1129 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001130 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001131 ((li->extLength &
1132 UDF_EXTENT_LENGTH_MASK) +
1133 blocksize - 1) >> blocksize_bits);
1134 li->extLocation.logicalBlockNum = 0;
1135 li->extLocation.partitionReferenceNum = 0;
1136
1137 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1138 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1139 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1140 lip1->extLength = (lip1->extLength -
1141 (li->extLength &
1142 UDF_EXTENT_LENGTH_MASK) +
1143 UDF_EXTENT_LENGTH_MASK) &
1144 ~(blocksize - 1);
1145 li->extLength = (li->extLength &
1146 UDF_EXTENT_FLAG_MASK) +
1147 (UDF_EXTENT_LENGTH_MASK + 1) -
1148 blocksize;
1149 } else {
1150 li->extLength = lip1->extLength +
1151 (((li->extLength &
1152 UDF_EXTENT_LENGTH_MASK) +
1153 blocksize - 1) & ~(blocksize - 1));
1154 if (*endnum > (i + 2))
1155 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001156 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001157 (*endnum - (i + 2)));
1158 i--;
1159 (*endnum)--;
1160 }
1161 } else if ((li->extLength >> 30) ==
1162 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1163 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001164 &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001165 ((li->extLength &
1166 UDF_EXTENT_LENGTH_MASK) +
1167 blocksize - 1) >> blocksize_bits);
1168 li->extLocation.logicalBlockNum = 0;
1169 li->extLocation.partitionReferenceNum = 0;
1170 li->extLength = (li->extLength &
1171 UDF_EXTENT_LENGTH_MASK) |
1172 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174 }
1175}
1176
Fabian Frederick3cc6f842017-01-06 21:53:50 +01001177static void udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001178 int startnum, int endnum,
1179 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 int start = 0, i;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001182 struct kernel_lb_addr tmploc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 uint32_t tmplen;
1184
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001185 if (startnum > endnum) {
1186 for (i = 0; i < (startnum - endnum); i++)
Jan Kara6c1e4d02018-06-13 18:04:24 +02001187 udf_delete_aext(inode, *epos);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001188 } else if (startnum < endnum) {
1189 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001190 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001191 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -07001192 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001193 &laarr[i].extLength, 1);
1194 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196 }
1197
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001198 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001199 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001200 udf_write_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001201 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203}
1204
Steve Magnanib490bdd2017-10-12 08:48:40 -05001205struct buffer_head *udf_bread(struct inode *inode, udf_pblk_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001206 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001208 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 bh = udf_getblk(inode, block, create, err);
1211 if (!bh)
1212 return NULL;
1213
1214 if (buffer_uptodate(bh))
1215 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001216
Mike Christiedfec8a12016-06-05 14:31:44 -05001217 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 wait_on_buffer(bh);
1220 if (buffer_uptodate(bh))
1221 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 brelse(bh);
1224 *err = -EIO;
1225 return NULL;
1226}
1227
Jan Kara7e49b6f2010-10-22 00:30:26 +02001228int udf_setsize(struct inode *inode, loff_t newsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001231 struct udf_inode_info *iinfo;
Steve Magnani89a4d972017-10-12 08:48:42 -05001232 unsigned int bsize = i_blocksize(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001235 S_ISLNK(inode->i_mode)))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001236 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001238 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001240 iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001241 if (newsize > inode->i_size) {
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001242 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001243 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1244 if (bsize <
1245 (udf_file_entry_alloc_offset(inode) + newsize)) {
1246 err = udf_expand_file_adinicb(inode);
Jan Karad2eb8c32011-12-10 02:30:48 +01001247 if (err)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001248 return err;
Jan Karad2eb8c32011-12-10 02:30:48 +01001249 down_write(&iinfo->i_data_sem);
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001250 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001251 iinfo->i_lenAlloc = newsize;
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001252 goto set_size;
1253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001255 err = udf_extend_file(inode, newsize);
1256 if (err) {
1257 up_write(&iinfo->i_data_sem);
1258 return err;
1259 }
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001260set_size:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001261 up_write(&iinfo->i_data_sem);
Jan Karaf2e95352017-06-13 16:20:25 +02001262 truncate_setsize(inode, newsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001263 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001264 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1265 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001266 udf_clear_extent_cache(inode);
Jan Kara382a2282020-09-25 12:29:54 +02001267 memset(iinfo->i_data + iinfo->i_lenEAttr + newsize,
Jan Kara7e49b6f2010-10-22 00:30:26 +02001268 0x00, bsize - newsize -
1269 udf_file_entry_alloc_offset(inode));
1270 iinfo->i_lenAlloc = newsize;
1271 truncate_setsize(inode, newsize);
1272 up_write(&iinfo->i_data_sem);
1273 goto update_time;
1274 }
1275 err = block_truncate_page(inode->i_mapping, newsize,
1276 udf_get_block);
1277 if (err)
1278 return err;
Jan Karaf2e95352017-06-13 16:20:25 +02001279 truncate_setsize(inode, newsize);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001280 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001281 udf_clear_extent_cache(inode);
Jan Kara2b42be52019-03-11 15:27:02 +01001282 err = udf_truncate_extents(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001283 up_write(&iinfo->i_data_sem);
Jan Kara2b42be52019-03-11 15:27:02 +01001284 if (err)
1285 return err;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001286 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001287update_time:
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001288 inode->i_mtime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001290 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 else
1292 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001293 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
Jan Karac03aa9f2014-09-04 14:06:55 +02001296/*
1297 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1298 * arbitrary - just that we hopefully don't limit any real use of rewritten
1299 * inode on write-once media but avoid looping for too long on corrupted media.
1300 */
1301#define UDF_MAX_ICB_NESTING 1024
1302
Jan Kara6174c2e2014-10-09 12:52:16 +02001303static int udf_read_inode(struct inode *inode, bool hidden_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 struct buffer_head *bh = NULL;
1306 struct fileEntry *fe;
Jan Karabb7720a02014-09-04 13:32:50 +02001307 struct extendedFileEntry *efe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001309 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Karabb7720a02014-09-04 13:32:50 +02001310 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001311 struct kernel_lb_addr *iloc = &iinfo->i_location;
Jan Karabb7720a02014-09-04 13:32:50 +02001312 unsigned int link_count;
Jan Karac03aa9f2014-09-04 14:06:55 +02001313 unsigned int indirections = 0;
Jan Kara79144952015-01-07 13:46:16 +01001314 int bs = inode->i_sb->s_blocksize;
Jan Kara6d3d5e82014-09-04 16:15:51 +02001315 int ret = -EIO;
Jan Kara0c9850f2018-02-22 10:28:52 +01001316 uint32_t uid, gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Jan Karac03aa9f2014-09-04 14:06:55 +02001318reread:
Fabian Frederick1d82a562017-01-06 21:54:43 +01001319 if (iloc->partitionReferenceNum >= sbi->s_partitions) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05001320 udf_debug("partition reference: %u > logical volume partitions: %u\n",
Fabian Frederick1d82a562017-01-06 21:54:43 +01001321 iloc->partitionReferenceNum, sbi->s_partitions);
1322 return -EIO;
1323 }
1324
Jan Kara6d3d5e82014-09-04 16:15:51 +02001325 if (iloc->logicalBlockNum >=
1326 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05001327 udf_debug("block=%u, partition=%u out of range\n",
Jan Kara6d3d5e82014-09-04 16:15:51 +02001328 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1329 return -EIO;
1330 }
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 /*
1333 * Set defaults, but the inode is still incomplete!
1334 * Note: get_new_inode() sets the following on a new inode:
1335 * i_sb = sb
1336 * i_no = ino
1337 * i_flags = sb->s_flags
1338 * i_state = 0
1339 * clean_inode(): zero fills and sets
1340 * i_count = 1
1341 * i_nlink = 1
1342 * i_op = NULL;
1343 */
Jan Kara6d3d5e82014-09-04 16:15:51 +02001344 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001345 if (!bh) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05001346 udf_err(inode->i_sb, "(ino %lu) failed !bh\n", inode->i_ino);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001347 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349
1350 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001351 ident != TAG_IDENT_USE) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05001352 udf_err(inode->i_sb, "(ino %lu) failed ident=%u\n",
Joe Perches78ace702011-10-10 01:08:05 -07001353 inode->i_ino, ident);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001354 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
1356
1357 fe = (struct fileEntry *)bh->b_data;
Jan Karabb7720a02014-09-04 13:32:50 +02001358 efe = (struct extendedFileEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001360 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001361 struct buffer_head *ibh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Jan Kara6d3d5e82014-09-04 16:15:51 +02001363 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001364 if (ident == TAG_IDENT_IE && ibh) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001365 struct kernel_lb_addr loc;
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001366 struct indirectEntry *ie;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001367
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001368 ie = (struct indirectEntry *)ibh->b_data;
1369 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001370
Jan Karac03aa9f2014-09-04 14:06:55 +02001371 if (ie->indirectICB.extLength) {
Jan Karac03aa9f2014-09-04 14:06:55 +02001372 brelse(ibh);
1373 memcpy(&iinfo->i_location, &loc,
1374 sizeof(struct kernel_lb_addr));
1375 if (++indirections > UDF_MAX_ICB_NESTING) {
1376 udf_err(inode->i_sb,
1377 "too many ICBs in ICB hierarchy"
1378 " (max %d supported)\n",
1379 UDF_MAX_ICB_NESTING);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001380 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001381 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001382 brelse(bh);
Jan Karac03aa9f2014-09-04 14:06:55 +02001383 goto reread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001385 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001386 brelse(ibh);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001387 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05001388 udf_err(inode->i_sb, "unsupported strategy type: %u\n",
Joe Perches78ace702011-10-10 01:08:05 -07001389 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001390 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001392 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001393 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001394 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001395 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001397 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001398 ICBTAG_FLAG_AD_MASK;
Jan Karad288d952018-12-12 14:29:20 +01001399 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
1400 iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
1401 iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1402 ret = -EIO;
1403 goto out;
1404 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001405 iinfo->i_unique = 0;
1406 iinfo->i_lenEAttr = 0;
1407 iinfo->i_lenExtents = 0;
1408 iinfo->i_lenAlloc = 0;
1409 iinfo->i_next_alloc_block = 0;
1410 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001411 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001412 iinfo->i_efe = 1;
1413 iinfo->i_use = 0;
Jan Kara79144952015-01-07 13:46:16 +01001414 ret = udf_alloc_i_data(inode, bs -
Jan Kara6d3d5e82014-09-04 16:15:51 +02001415 sizeof(struct extendedFileEntry));
1416 if (ret)
1417 goto out;
Jan Kara382a2282020-09-25 12:29:54 +02001418 memcpy(iinfo->i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001419 bh->b_data + sizeof(struct extendedFileEntry),
Jan Kara79144952015-01-07 13:46:16 +01001420 bs - sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001421 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001422 iinfo->i_efe = 0;
1423 iinfo->i_use = 0;
Jan Kara79144952015-01-07 13:46:16 +01001424 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001425 if (ret)
1426 goto out;
Jan Kara382a2282020-09-25 12:29:54 +02001427 memcpy(iinfo->i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001428 bh->b_data + sizeof(struct fileEntry),
Jan Kara79144952015-01-07 13:46:16 +01001429 bs - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001430 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001431 iinfo->i_efe = 0;
1432 iinfo->i_use = 1;
1433 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001434 ((struct unallocSpaceEntry *)bh->b_data)->
1435 lengthAllocDescs);
Jan Kara79144952015-01-07 13:46:16 +01001436 ret = udf_alloc_i_data(inode, bs -
Jan Kara6d3d5e82014-09-04 16:15:51 +02001437 sizeof(struct unallocSpaceEntry));
1438 if (ret)
1439 goto out;
Jan Kara382a2282020-09-25 12:29:54 +02001440 memcpy(iinfo->i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001441 bh->b_data + sizeof(struct unallocSpaceEntry),
Jan Kara79144952015-01-07 13:46:16 +01001442 bs - sizeof(struct unallocSpaceEntry));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445
Jan Kara6d3d5e82014-09-04 16:15:51 +02001446 ret = -EIO;
Jan Karac03cad22010-10-20 22:17:28 +02001447 read_lock(&sbi->s_cred_lock);
Jan Kara0c9850f2018-02-22 10:28:52 +01001448 uid = le32_to_cpu(fe->uid);
1449 if (uid == UDF_INVALID_ID ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001450 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Jan Kara0c9850f2018-02-22 10:28:52 +01001451 inode->i_uid = sbi->s_uid;
1452 else
1453 i_uid_write(inode, uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Jan Kara0c9850f2018-02-22 10:28:52 +01001455 gid = le32_to_cpu(fe->gid);
1456 if (gid == UDF_INVALID_ID ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001457 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Jan Kara0c9850f2018-02-22 10:28:52 +01001458 inode->i_gid = sbi->s_gid;
1459 else
1460 i_gid_write(inode, gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001462 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001463 sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001464 inode->i_mode = sbi->s_fmode;
1465 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001466 sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001467 inode->i_mode = sbi->s_dmode;
1468 else
1469 inode->i_mode = udf_convert_permissions(fe);
1470 inode->i_mode &= ~sbi->s_umask;
Steven J. Magnanic3367a12019-08-27 07:13:59 -05001471 iinfo->i_extraPerms = le32_to_cpu(fe->permissions) & ~FE_MAPPED_PERMS;
1472
Jan Karac03cad22010-10-20 22:17:28 +02001473 read_unlock(&sbi->s_cred_lock);
1474
Miklos Szeredibfe86842011-10-28 14:13:29 +02001475 link_count = le16_to_cpu(fe->fileLinkCount);
Jan Kara4071b912014-09-04 16:19:47 +02001476 if (!link_count) {
Jan Kara6174c2e2014-10-09 12:52:16 +02001477 if (!hidden_inode) {
1478 ret = -ESTALE;
1479 goto out;
1480 }
1481 link_count = 1;
Jan Kara4071b912014-09-04 16:19:47 +02001482 }
Miklos Szeredibfe86842011-10-28 14:13:29 +02001483 set_nlink(inode, link_count);
Jan Karac03cad22010-10-20 22:17:28 +02001484
1485 inode->i_size = le64_to_cpu(fe->informationLength);
1486 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001488 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001490 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001492 udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime);
1493 udf_disk_stamp_to_time(&inode->i_mtime, fe->modificationTime);
1494 udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001496 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1497 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1498 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001499 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
Steven J. Magnaniab9a3a72019-08-14 07:50:02 -05001500 iinfo->i_streamdir = 0;
1501 iinfo->i_lenStreams = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001502 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001503 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001504 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001506 udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime);
1507 udf_disk_stamp_to_time(&inode->i_mtime, efe->modificationTime);
Deepa Dinamani0220edd2018-05-10 08:26:17 -07001508 udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime);
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001509 udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001511 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1512 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1513 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001514 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
Steven J. Magnaniab9a3a72019-08-14 07:50:02 -05001515
1516 /* Named streams */
1517 iinfo->i_streamdir = (efe->streamDirectoryICB.extLength != 0);
1518 iinfo->i_locStreamdir =
1519 lelb_to_cpu(efe->streamDirectoryICB.extLocation);
1520 iinfo->i_lenStreams = le64_to_cpu(efe->objectSize);
1521 if (iinfo->i_lenStreams >= inode->i_size)
1522 iinfo->i_lenStreams -= inode->i_size;
1523 else
1524 iinfo->i_lenStreams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
Jan Kara470cca52014-09-04 16:26:19 +02001526 inode->i_generation = iinfo->i_unique;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Jan Kara23b133bd2015-01-07 13:49:08 +01001528 /*
1529 * Sanity check length of allocation descriptors and extended attrs to
1530 * avoid integer overflows
1531 */
1532 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1533 goto out;
1534 /* Now do exact checks */
1535 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1536 goto out;
Jan Karae1593322014-12-19 12:03:53 +01001537 /* Sanity checks for files in ICB so that we don't get confused later */
1538 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1539 /*
1540 * For file in ICB data is stored in allocation descriptor
1541 * so sizes should match
1542 */
1543 if (iinfo->i_lenAlloc != inode->i_size)
1544 goto out;
1545 /* File in ICB has to fit in there... */
Jan Kara79144952015-01-07 13:46:16 +01001546 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
Jan Karae1593322014-12-19 12:03:53 +01001547 goto out;
1548 }
1549
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001550 switch (fe->icbTag.fileType) {
1551 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001552 inode->i_op = &udf_dir_inode_operations;
1553 inode->i_fop = &udf_dir_operations;
1554 inode->i_mode |= S_IFDIR;
1555 inc_nlink(inode);
1556 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001557 case ICBTAG_FILE_TYPE_REALTIME:
1558 case ICBTAG_FILE_TYPE_REGULAR:
1559 case ICBTAG_FILE_TYPE_UNDEF:
Jan Kara742e1792008-04-08 01:17:52 +02001560 case ICBTAG_FILE_TYPE_VAT20:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001561 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001562 inode->i_data.a_ops = &udf_adinicb_aops;
1563 else
1564 inode->i_data.a_ops = &udf_aops;
1565 inode->i_op = &udf_file_inode_operations;
1566 inode->i_fop = &udf_file_operations;
1567 inode->i_mode |= S_IFREG;
1568 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001569 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001570 inode->i_mode |= S_IFBLK;
1571 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001572 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001573 inode->i_mode |= S_IFCHR;
1574 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001575 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001576 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1577 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001578 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001579 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1580 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001581 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001582 inode->i_data.a_ops = &udf_symlink_aops;
Jan Karaad4d0532017-01-02 14:30:31 +01001583 inode->i_op = &udf_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05001584 inode_nohighmem(inode);
Fabian Frederick6ff6b2b2017-04-23 20:58:15 +02001585 inode->i_mode = S_IFLNK | 0777;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001586 break;
Jan Karabfb257a2008-04-08 20:37:21 +02001587 case ICBTAG_FILE_TYPE_MAIN:
1588 udf_debug("METADATA FILE-----\n");
1589 break;
1590 case ICBTAG_FILE_TYPE_MIRROR:
1591 udf_debug("METADATA MIRROR FILE-----\n");
1592 break;
1593 case ICBTAG_FILE_TYPE_BITMAP:
1594 udf_debug("METADATA BITMAP FILE-----\n");
1595 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001596 default:
Steve Magnanifcbf7632017-10-12 08:48:41 -05001597 udf_err(inode->i_sb, "(ino %lu) failed unknown file type=%u\n",
Joe Perches78ace702011-10-10 01:08:05 -07001598 inode->i_ino, fe->icbTag.fileType);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001599 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001601 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001602 struct deviceSpec *dsea =
1603 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001604 if (dsea) {
1605 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001606 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1607 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001609 } else
Jan Kara6d3d5e82014-09-04 16:15:51 +02001610 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001612 ret = 0;
1613out:
Jan Karabb7720a02014-09-04 13:32:50 +02001614 brelse(bh);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001615 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001618static int udf_alloc_i_data(struct inode *inode, size_t size)
1619{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001620 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Kara382a2282020-09-25 12:29:54 +02001621 iinfo->i_data = kmalloc(size, GFP_KERNEL);
1622 if (!iinfo->i_data)
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001623 return -ENOMEM;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001624 return 0;
1625}
1626
Al Virofaa17292011-07-26 03:18:29 -04001627static umode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Al Virofaa17292011-07-26 03:18:29 -04001629 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 uint32_t permissions;
1631 uint32_t flags;
1632
1633 permissions = le32_to_cpu(fe->permissions);
1634 flags = le16_to_cpu(fe->icbTag.flags);
1635
Fabian Frederick6ff6b2b2017-04-23 20:58:15 +02001636 mode = ((permissions) & 0007) |
1637 ((permissions >> 2) & 0070) |
1638 ((permissions >> 4) & 0700) |
Marcin Slusarz4b111112008-02-08 04:20:36 -08001639 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1640 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1641 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 return mode;
1644}
1645
Steven J. Magnanic3367a12019-08-27 07:13:59 -05001646void udf_update_extra_perms(struct inode *inode, umode_t mode)
1647{
1648 struct udf_inode_info *iinfo = UDF_I(inode);
1649
1650 /*
1651 * UDF 2.01 sec. 3.3.3.3 Note 2:
1652 * In Unix, delete permission tracks write
1653 */
1654 iinfo->i_extraPerms &= ~FE_DELETE_PERMS;
1655 if (mode & 0200)
1656 iinfo->i_extraPerms |= FE_PERM_U_DELETE;
1657 if (mode & 0020)
1658 iinfo->i_extraPerms |= FE_PERM_G_DELETE;
1659 if (mode & 0002)
1660 iinfo->i_extraPerms |= FE_PERM_O_DELETE;
1661}
1662
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001663int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
Jan Kara49521de2010-10-20 17:42:44 +02001665 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
Jan Kara49521de2010-10-20 17:42:44 +02001668static int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
1670 return udf_update_inode(inode, 1);
1671}
1672
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001673static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec64 time)
Fabian Frederick7ed0fbd2017-01-06 21:53:54 +01001674{
1675 if (iinfo->i_crtime.tv_sec > time.tv_sec ||
1676 (iinfo->i_crtime.tv_sec == time.tv_sec &&
1677 iinfo->i_crtime.tv_nsec > time.tv_nsec))
1678 iinfo->i_crtime = time;
1679}
1680
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001681static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
1683 struct buffer_head *bh = NULL;
1684 struct fileEntry *fe;
1685 struct extendedFileEntry *efe;
Steve Nickelb2527bf2012-02-16 12:53:53 -05001686 uint64_t lb_recorded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 uint32_t udfperms;
1688 uint16_t icbflags;
1689 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001691 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001692 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001693 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Jan Kara5833ded2010-01-08 16:52:59 +01001695 bh = udf_tgetblk(inode->i_sb,
1696 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001697 if (!bh) {
Jan Karaaae917c2010-01-08 16:46:29 +01001698 udf_debug("getblk failure\n");
Changwoo Min0fd2ba32015-03-22 19:17:49 -04001699 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701
Jan Karaaae917c2010-01-08 16:46:29 +01001702 lock_buffer(bh);
1703 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 fe = (struct fileEntry *)bh->b_data;
1705 efe = (struct extendedFileEntry *)bh->b_data;
1706
Jan Karaaae917c2010-01-08 16:46:29 +01001707 if (iinfo->i_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001709 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001711 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001712 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Jan Kara382a2282020-09-25 12:29:54 +02001713 iinfo->i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001714 sizeof(struct unallocSpaceEntry));
Jan Karaaae917c2010-01-08 16:46:29 +01001715 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001716 crclen = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001718 goto finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
1720
Phillip Susi4d6660e2006-03-07 21:55:24 -08001721 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
Jan Kara0c9850f2018-02-22 10:28:52 +01001722 fe->uid = cpu_to_le32(UDF_INVALID_ID);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001723 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001724 fe->uid = cpu_to_le32(i_uid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Phillip Susi4d6660e2006-03-07 21:55:24 -08001726 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
Jan Kara0c9850f2018-02-22 10:28:52 +01001727 fe->gid = cpu_to_le32(UDF_INVALID_ID);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001728 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001729 fe->gid = cpu_to_le32(i_gid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Fabian Frederick6ff6b2b2017-04-23 20:58:15 +02001731 udfperms = ((inode->i_mode & 0007)) |
1732 ((inode->i_mode & 0070) << 2) |
1733 ((inode->i_mode & 0700) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Steven J. Magnanic3367a12019-08-27 07:13:59 -05001735 udfperms |= iinfo->i_extraPerms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 fe->permissions = cpu_to_le32(udfperms);
1737
Jan Kara8a70ee32014-09-04 11:47:51 +02001738 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1740 else
1741 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1742
1743 fe->informationLength = cpu_to_le64(inode->i_size);
1744
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001745 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001746 struct regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001747 struct deviceSpec *dsea =
1748 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001749 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001751 udf_add_extendedattr(inode,
1752 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001753 sizeof(struct regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 dsea->attrType = cpu_to_le32(12);
1755 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001756 dsea->attrLength = cpu_to_le32(
1757 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001758 sizeof(struct regid));
1759 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001761 eid = (struct regid *)dsea->impUse;
Markus Elfring033c9da2017-08-15 16:45:44 +02001762 memset(eid, 0, sizeof(*eid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 strcpy(eid->ident, UDF_ID_DEVELOPER);
1764 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1765 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1766 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1767 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1768 }
1769
Steve Nickelb2527bf2012-02-16 12:53:53 -05001770 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1771 lb_recorded = 0; /* No extents => no blocks! */
1772 else
1773 lb_recorded =
1774 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1775 (blocksize_bits - 9);
1776
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001777 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001778 memcpy(bh->b_data + sizeof(struct fileEntry),
Jan Kara382a2282020-09-25 12:29:54 +02001779 iinfo->i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001780 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Steve Nickelb2527bf2012-02-16 12:53:53 -05001781 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001783 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1784 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1785 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001786 memset(&(fe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1788 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1789 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001790 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1791 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1792 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001793 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1795 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001796 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001797 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Jan Kara382a2282020-09-25 12:29:54 +02001798 iinfo->i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001799 inode->i_sb->s_blocksize -
1800 sizeof(struct extendedFileEntry));
Steven J. Magnaniab9a3a72019-08-14 07:50:02 -05001801 efe->objectSize =
1802 cpu_to_le64(inode->i_size + iinfo->i_lenStreams);
Steve Nickelb2527bf2012-02-16 12:53:53 -05001803 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Steven J. Magnaniab9a3a72019-08-14 07:50:02 -05001805 if (iinfo->i_streamdir) {
1806 struct long_ad *icb_lad = &efe->streamDirectoryICB;
1807
1808 icb_lad->extLocation =
1809 cpu_to_lelb(iinfo->i_locStreamdir);
1810 icb_lad->extLength =
1811 cpu_to_le32(inode->i_sb->s_blocksize);
1812 }
1813
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001814 udf_adjust_time(iinfo, inode->i_atime);
1815 udf_adjust_time(iinfo, inode->i_mtime);
1816 udf_adjust_time(iinfo, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001818 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1819 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
Marcin Slusarz56774802008-02-10 11:25:31 +01001820 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001821 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Markus Elfring033c9da2017-08-15 16:45:44 +02001823 memset(&(efe->impIdent), 0, sizeof(efe->impIdent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1825 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1826 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001827 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1828 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1829 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001830 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1832 crclen = sizeof(struct extendedFileEntry);
1833 }
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001834
1835finish:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001836 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 fe->icbTag.strategyType = cpu_to_le16(4096);
1838 fe->icbTag.strategyParameter = cpu_to_le16(1);
1839 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001840 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 fe->icbTag.strategyType = cpu_to_le16(4);
1842 fe->icbTag.numEntries = cpu_to_le16(1);
1843 }
1844
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001845 if (iinfo->i_use)
1846 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1847 else if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1849 else if (S_ISREG(inode->i_mode))
1850 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1851 else if (S_ISLNK(inode->i_mode))
1852 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1853 else if (S_ISBLK(inode->i_mode))
1854 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1855 else if (S_ISCHR(inode->i_mode))
1856 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1857 else if (S_ISFIFO(inode->i_mode))
1858 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1859 else if (S_ISSOCK(inode->i_mode))
1860 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1861
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001862 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001863 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1864 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1865 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1866 (le16_to_cpu(fe->icbTag.flags) &
1867 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1868 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001871 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 fe->descTag.descVersion = cpu_to_le16(3);
1873 else
1874 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001875 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001876 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001877 iinfo->i_location.logicalBlockNum);
Jan Karaaae917c2010-01-08 16:46:29 +01001878 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001880 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001881 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001882 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Jan Kara5833ded2010-01-08 16:52:59 +01001884 set_buffer_uptodate(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001885 unlock_buffer(bh);
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 /* write the data blocks */
1888 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001889 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 sync_dirty_buffer(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001891 if (buffer_write_io_error(bh)) {
Joe Perches78ace702011-10-10 01:08:05 -07001892 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1893 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 err = -EIO;
1895 }
1896 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001897 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return err;
1900}
1901
Jan Kara6174c2e2014-10-09 12:52:16 +02001902struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1903 bool hidden_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
1905 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1906 struct inode *inode = iget_locked(sb, block);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001907 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 if (!inode)
Jan Kara6d3d5e82014-09-04 16:15:51 +02001910 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Jan Kara6d3d5e82014-09-04 16:15:51 +02001912 if (!(inode->i_state & I_NEW))
1913 return inode;
1914
1915 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
Jan Kara6174c2e2014-10-09 12:52:16 +02001916 err = udf_read_inode(inode, hidden_inode);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001917 if (err < 0) {
1918 iget_failed(inode);
1919 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001921 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
Steve Magnanib490bdd2017-10-12 08:48:40 -05001926int udf_setup_indirect_aext(struct inode *inode, udf_pblk_t block,
Jan Karafcea62b2015-12-23 14:21:13 +01001927 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Jan Karafcea62b2015-12-23 14:21:13 +01001929 struct super_block *sb = inode->i_sb;
1930 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 struct allocExtDesc *aed;
Jan Karafcea62b2015-12-23 14:21:13 +01001932 struct extent_position nepos;
1933 struct kernel_lb_addr neloc;
1934 int ver, adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Jan Karafcea62b2015-12-23 14:21:13 +01001936 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1937 adsize = sizeof(struct short_ad);
1938 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1939 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 else
Arnd Bergmann4f1b1512016-01-01 15:21:54 +01001941 return -EIO;
Jan Karafcea62b2015-12-23 14:21:13 +01001942
1943 neloc.logicalBlockNum = block;
1944 neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1945
1946 bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1947 if (!bh)
1948 return -EIO;
1949 lock_buffer(bh);
1950 memset(bh->b_data, 0x00, sb->s_blocksize);
1951 set_buffer_uptodate(bh);
1952 unlock_buffer(bh);
1953 mark_buffer_dirty_inode(bh, inode);
1954
1955 aed = (struct allocExtDesc *)(bh->b_data);
1956 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1957 aed->previousAllocExtLocation =
1958 cpu_to_le32(epos->block.logicalBlockNum);
1959 }
1960 aed->lengthAllocDescs = cpu_to_le32(0);
1961 if (UDF_SB(sb)->s_udfrev >= 0x0200)
1962 ver = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 else
Jan Karafcea62b2015-12-23 14:21:13 +01001964 ver = 2;
1965 udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1966 sizeof(struct tag));
1967
1968 nepos.block = neloc;
1969 nepos.offset = sizeof(struct allocExtDesc);
1970 nepos.bh = bh;
1971
1972 /*
1973 * Do we have to copy current last extent to make space for indirect
1974 * one?
1975 */
1976 if (epos->offset + adsize > sb->s_blocksize) {
1977 struct kernel_lb_addr cp_loc;
1978 uint32_t cp_len;
1979 int cp_type;
1980
1981 epos->offset -= adsize;
1982 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1983 cp_len |= ((uint32_t)cp_type) << 30;
1984
1985 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1986 udf_write_aext(inode, epos, &nepos.block,
Pali Rohár800552c2020-01-07 22:29:02 +01001987 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
Jan Karafcea62b2015-12-23 14:21:13 +01001988 } else {
1989 __udf_add_aext(inode, epos, &nepos.block,
Pali Rohár800552c2020-01-07 22:29:02 +01001990 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
Jan Karafcea62b2015-12-23 14:21:13 +01001991 }
1992
1993 brelse(epos->bh);
1994 *epos = nepos;
1995
1996 return 0;
1997}
1998
1999/*
2000 * Append extent at the given position - should be the first free one in inode
2001 * / indirect extent. This function assumes there is enough space in the inode
2002 * or indirect extent. Use udf_add_aext() if you didn't check for this before.
2003 */
2004int __udf_add_aext(struct inode *inode, struct extent_position *epos,
2005 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2006{
2007 struct udf_inode_info *iinfo = UDF_I(inode);
2008 struct allocExtDesc *aed;
2009 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002011 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002012 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002013 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002014 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 else
Jan Kara7e49b6f2010-10-22 00:30:26 +02002016 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Jan Karafcea62b2015-12-23 14:21:13 +01002018 if (!epos->bh) {
2019 WARN_ON(iinfo->i_lenAlloc !=
2020 epos->offset - udf_file_entry_alloc_offset(inode));
2021 } else {
2022 aed = (struct allocExtDesc *)epos->bh->b_data;
2023 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
2024 epos->offset - sizeof(struct allocExtDesc));
2025 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027
Jan Kara7e49b6f2010-10-22 00:30:26 +02002028 udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002030 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002031 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002033 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002034 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002035 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002036 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2037 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2038 udf_update_tag(epos->bh->b_data,
2039 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002041 udf_update_tag(epos->bh->b_data,
2042 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002043 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
2045
Jan Kara7e49b6f2010-10-22 00:30:26 +02002046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
Jan Karafcea62b2015-12-23 14:21:13 +01002049/*
2050 * Append extent at given position - should be the first free one in inode
2051 * / indirect extent. Takes care of allocating and linking indirect blocks.
2052 */
2053int udf_add_aext(struct inode *inode, struct extent_position *epos,
2054 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2055{
2056 int adsize;
2057 struct super_block *sb = inode->i_sb;
2058
2059 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2060 adsize = sizeof(struct short_ad);
2061 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2062 adsize = sizeof(struct long_ad);
2063 else
2064 return -EIO;
2065
2066 if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2067 int err;
Steve Magnanib490bdd2017-10-12 08:48:40 -05002068 udf_pblk_t new_block;
Jan Karafcea62b2015-12-23 14:21:13 +01002069
2070 new_block = udf_new_block(sb, NULL,
2071 epos->block.partitionReferenceNum,
2072 epos->block.logicalBlockNum, &err);
2073 if (!new_block)
2074 return -ENOSPC;
2075
2076 err = udf_setup_indirect_aext(inode, new_block, epos);
2077 if (err)
2078 return err;
2079 }
2080
2081 return __udf_add_aext(inode, epos, eloc, elen, inc);
2082}
2083
Jan Kara7e49b6f2010-10-22 00:30:26 +02002084void udf_write_aext(struct inode *inode, struct extent_position *epos,
2085 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
2087 int adsize;
2088 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002089 struct short_ad *sad;
2090 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002091 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Jan Karaff116fc2007-05-08 00:35:14 -07002093 if (!epos->bh)
Jan Kara382a2282020-09-25 12:29:54 +02002094 ptr = iinfo->i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002095 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002096 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 else
Jan Karaff116fc2007-05-08 00:35:14 -07002098 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002100 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002101 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002102 sad = (struct short_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002103 sad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002104 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002105 adsize = sizeof(struct short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002106 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002107 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002108 lad = (struct long_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002109 lad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002110 lad->extLocation = cpu_to_lelb(*eloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002111 memset(lad->impUse, 0x00, sizeof(lad->impUse));
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002112 adsize = sizeof(struct long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002113 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002114 default:
Jan Kara7e49b6f2010-10-22 00:30:26 +02002115 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
2117
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002118 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002119 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002120 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002121 struct allocExtDesc *aed =
2122 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07002123 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002124 le32_to_cpu(aed->lengthAllocDescs) +
2125 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 }
Jan Karaff116fc2007-05-08 00:35:14 -07002127 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002128 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07002133 epos->offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Vegard Nossumb0918d92015-12-11 15:54:16 +01002136/*
2137 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2138 * someone does some weird stuff.
2139 */
2140#define UDF_MAX_INDIR_EXTS 16
2141
Marcin Slusarz4b111112008-02-08 04:20:36 -08002142int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002143 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
2145 int8_t etype;
Vegard Nossumb0918d92015-12-11 15:54:16 +01002146 unsigned int indirections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Jan Karaff116fc2007-05-08 00:35:14 -07002148 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Pali Rohár800552c2020-01-07 22:29:02 +01002149 (EXT_NEXT_EXTENT_ALLOCDESCS >> 30)) {
Steve Magnanib490bdd2017-10-12 08:48:40 -05002150 udf_pblk_t block;
Vegard Nossumb0918d92015-12-11 15:54:16 +01002151
2152 if (++indirections > UDF_MAX_INDIR_EXTS) {
2153 udf_err(inode->i_sb,
2154 "too many indirect extents in inode %lu\n",
2155 inode->i_ino);
2156 return -1;
2157 }
2158
Jan Karaff116fc2007-05-08 00:35:14 -07002159 epos->block = *eloc;
2160 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002161 brelse(epos->bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002162 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002163 epos->bh = udf_tread(inode->i_sb, block);
2164 if (!epos->bh) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05002165 udf_debug("reading block %u failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return -1;
2167 }
2168 }
2169
2170 return etype;
2171}
2172
Marcin Slusarz4b111112008-02-08 04:20:36 -08002173int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002174 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
2176 int alen;
2177 int8_t etype;
2178 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002179 struct short_ad *sad;
2180 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002181 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002182
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002183 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002184 if (!epos->offset)
2185 epos->offset = udf_file_entry_alloc_offset(inode);
Jan Kara382a2282020-09-25 12:29:54 +02002186 ptr = iinfo->i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002187 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002188 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08002189 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002190 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002191 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002192 if (!epos->offset)
2193 epos->offset = sizeof(struct allocExtDesc);
2194 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002195 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08002196 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2197 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
2199
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002200 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002201 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002202 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2203 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002205 etype = le32_to_cpu(sad->extLength) >> 30;
2206 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002207 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002208 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002209 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2210 break;
2211 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002212 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2213 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002214 return -1;
2215 etype = le32_to_cpu(lad->extLength) >> 30;
2216 *eloc = lelb_to_cpu(lad->extLocation);
2217 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2218 break;
2219 default:
Steve Magnanifcbf7632017-10-12 08:48:41 -05002220 udf_debug("alloc_type = %u unsupported\n", iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002221 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
2223
2224 return etype;
2225}
2226
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002227static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002228 struct kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002230 struct kernel_lb_addr oeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 uint32_t oelen;
2232 int8_t etype;
2233
Jan Karaff116fc2007-05-08 00:35:14 -07002234 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07002235 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002237 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002238 udf_write_aext(inode, &epos, &neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 neloc = oeloc;
2240 nelen = (etype << 30) | oelen;
2241 }
Pekka Enberg97e961f2008-10-15 12:29:03 +02002242 udf_add_aext(inode, &epos, &neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002243 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 return (nelen >> 30);
2246}
2247
Jan Kara6c1e4d02018-06-13 18:04:24 +02002248int8_t udf_delete_aext(struct inode *inode, struct extent_position epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Jan Karaff116fc2007-05-08 00:35:14 -07002250 struct extent_position oepos;
2251 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 int8_t etype;
2253 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002254 struct udf_inode_info *iinfo;
Jan Kara6c1e4d02018-06-13 18:04:24 +02002255 struct kernel_lb_addr eloc;
2256 uint32_t elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002258 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002259 get_bh(epos.bh);
2260 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
2262
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002263 iinfo = UDF_I(inode);
2264 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002265 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002266 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002267 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 else
2269 adsize = 0;
2270
Jan Karaff116fc2007-05-08 00:35:14 -07002271 oepos = epos;
2272 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 return -1;
2274
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002275 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002276 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002277 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002278 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07002279 brelse(oepos.bh);
2280 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07002281 oepos.bh = epos.bh;
2282 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
2284 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002285 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 elen = 0;
2287
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002288 if (epos.bh != oepos.bh) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002289 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2290 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2291 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002292 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002293 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002295 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002296 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002297 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002298 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002299 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002300 udf_update_tag(oepos.bh->b_data,
2301 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002303 udf_update_tag(oepos.bh->b_data,
2304 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002305 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002307 } else {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002308 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002309 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002310 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002312 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002313 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002314 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002315 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002316 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002317 udf_update_tag(oepos.bh->b_data,
2318 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002320 udf_update_tag(oepos.bh->b_data,
2321 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002322 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
2324 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002325
Jan Kara3bf25cb2007-05-08 00:35:16 -07002326 brelse(epos.bh);
2327 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return (elen >> 30);
2330}
2331
Marcin Slusarz4b111112008-02-08 04:20:36 -08002332int8_t inode_bmap(struct inode *inode, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002333 struct extent_position *pos, struct kernel_lb_addr *eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002334 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002336 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Fabian Frederickbbc9abd2017-01-06 21:53:52 +01002337 loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002339 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002341 iinfo = UDF_I(inode);
Namjae Jeon99600052013-01-19 11:17:14 +09002342 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2343 pos->offset = 0;
2344 pos->block = iinfo->i_location;
2345 pos->bh = NULL;
2346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 *elen = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002348 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002349 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2350 if (etype == -1) {
2351 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002352 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return -1;
2354 }
2355 lbcount += *elen;
2356 } while (lbcount <= bcount);
Namjae Jeon99600052013-01-19 11:17:14 +09002357 /* update extent cache */
Fabian Frederickb31c9ed2017-01-06 21:53:56 +01002358 udf_update_extent_cache(inode, lbcount - *elen, pos);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002359 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 return etype;
2362}
2363
Steve Magnanib490bdd2017-10-12 08:48:40 -05002364udf_pblk_t udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002366 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -07002367 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002368 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002369 struct extent_position epos = {};
Steve Magnanib490bdd2017-10-12 08:48:40 -05002370 udf_pblk_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002372 down_read(&UDF_I(inode)->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Marcin Slusarz4b111112008-02-08 04:20:36 -08002374 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2375 (EXT_RECORDED_ALLOCATED >> 30))
Pekka Enberg97e961f2008-10-15 12:29:03 +02002376 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 else
2378 ret = 0;
2379
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002380 up_read(&UDF_I(inode)->i_data_sem);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002381 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2384 return udf_fixed_to_variable(ret);
2385 else
2386 return ret;
2387}