blob: 9bb18311a22fc1a60bb642c8b68891c2e5a05949 [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
Al Virofaa17292011-07-26 03:18:29 -040048static umode_t udf_convert_permissions(struct fileEntry *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static int udf_update_inode(struct inode *, int);
Jan Kara49521de2010-10-20 17:42:44 +020050static int udf_sync_inode(struct inode *inode);
Cyrill Gorcunov647bd61a2007-07-15 23:39:47 -070051static int udf_alloc_i_data(struct inode *inode, size_t size);
Jan Kara7b0b0932011-12-10 01:43:33 +010052static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
Jan Karaff116fc2007-05-08 00:35:14 -070053static int8_t udf_insert_aext(struct inode *, struct extent_position,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020054 struct kernel_lb_addr, uint32_t);
Steve Magnanib490bdd2017-10-12 08:48:40 -050055static void udf_split_extents(struct inode *, int *, int, udf_pblk_t,
Fabian Frederick3cc6f842017-01-06 21:53:50 +010056 struct kernel_long_ad *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static void udf_prealloc_extents(struct inode *, int, int,
Fabian Frederick3cc6f842017-01-06 21:53:50 +010058 struct kernel_long_ad *, int *);
59static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *);
60static void udf_update_extents(struct inode *, struct kernel_long_ad *, int,
61 int, struct extent_position *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
63
Namjae Jeon99600052013-01-19 11:17:14 +090064static void __udf_clear_extent_cache(struct inode *inode)
65{
66 struct udf_inode_info *iinfo = UDF_I(inode);
67
68 if (iinfo->cached_extent.lstart != -1) {
69 brelse(iinfo->cached_extent.epos.bh);
70 iinfo->cached_extent.lstart = -1;
71 }
72}
73
74/* Invalidate extent cache */
75static void udf_clear_extent_cache(struct inode *inode)
76{
77 struct udf_inode_info *iinfo = UDF_I(inode);
78
79 spin_lock(&iinfo->i_extent_cache_lock);
80 __udf_clear_extent_cache(inode);
81 spin_unlock(&iinfo->i_extent_cache_lock);
82}
83
84/* Return contents of extent cache */
85static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
86 loff_t *lbcount, struct extent_position *pos)
87{
88 struct udf_inode_info *iinfo = UDF_I(inode);
89 int ret = 0;
90
91 spin_lock(&iinfo->i_extent_cache_lock);
92 if ((iinfo->cached_extent.lstart <= bcount) &&
93 (iinfo->cached_extent.lstart != -1)) {
94 /* Cache hit */
95 *lbcount = iinfo->cached_extent.lstart;
96 memcpy(pos, &iinfo->cached_extent.epos,
97 sizeof(struct extent_position));
98 if (pos->bh)
99 get_bh(pos->bh);
100 ret = 1;
101 }
102 spin_unlock(&iinfo->i_extent_cache_lock);
103 return ret;
104}
105
106/* Add extent to extent cache */
107static void udf_update_extent_cache(struct inode *inode, loff_t estart,
Fabian Frederickb31c9ed2017-01-06 21:53:56 +0100108 struct extent_position *pos)
Namjae Jeon99600052013-01-19 11:17:14 +0900109{
110 struct udf_inode_info *iinfo = UDF_I(inode);
111
112 spin_lock(&iinfo->i_extent_cache_lock);
113 /* Invalidate previously cached extent */
114 __udf_clear_extent_cache(inode);
115 if (pos->bh)
116 get_bh(pos->bh);
Markus Elfring033c9da2017-08-15 16:45:44 +0200117 memcpy(&iinfo->cached_extent.epos, pos, sizeof(*pos));
Namjae Jeon99600052013-01-19 11:17:14 +0900118 iinfo->cached_extent.lstart = estart;
Fabian Frederickb31c9ed2017-01-06 21:53:56 +0100119 switch (iinfo->i_alloc_type) {
120 case ICBTAG_FLAG_AD_SHORT:
121 iinfo->cached_extent.epos.offset -= sizeof(struct short_ad);
122 break;
123 case ICBTAG_FLAG_AD_LONG:
124 iinfo->cached_extent.epos.offset -= sizeof(struct long_ad);
125 break;
126 }
Namjae Jeon99600052013-01-19 11:17:14 +0900127 spin_unlock(&iinfo->i_extent_cache_lock);
128}
Christoph Hellwigb1e32122008-02-22 12:38:48 +0100129
Al Viro3aac2b62010-06-07 00:43:39 -0400130void udf_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Jan Kara2c948b32009-12-03 13:39:28 +0100132 struct udf_inode_info *iinfo = UDF_I(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400133 int want_delete = 0;
Jan Kara2c948b32009-12-03 13:39:28 +0100134
Al Viro3aac2b62010-06-07 00:43:39 -0400135 if (!inode->i_nlink && !is_bad_inode(inode)) {
136 want_delete = 1;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200137 udf_setsize(inode, 0);
Al Viro3aac2b62010-06-07 00:43:39 -0400138 udf_update_inode(inode, IS_SYNC(inode));
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700139 }
140 truncate_inode_pages_final(&inode->i_data);
Al Viro3aac2b62010-06-07 00:43:39 -0400141 invalidate_inode_buffers(inode);
Jan Karadbd57682012-05-03 14:48:02 +0200142 clear_inode(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100143 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
144 inode->i_size != iinfo->i_lenExtents) {
Joe Perches78ace702011-10-10 01:08:05 -0700145 udf_warn(inode->i_sb, "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
146 inode->i_ino, inode->i_mode,
147 (unsigned long long)inode->i_size,
148 (unsigned long long)iinfo->i_lenExtents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800150 kfree(iinfo->i_ext.i_data);
151 iinfo->i_ext.i_data = NULL;
Namjae Jeon99600052013-01-19 11:17:14 +0900152 udf_clear_extent_cache(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400153 if (want_delete) {
Al Viro3aac2b62010-06-07 00:43:39 -0400154 udf_free_inode(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Ian Abbott5eec54f2012-09-05 17:44:31 +0100158static void udf_write_failed(struct address_space *mapping, loff_t to)
159{
160 struct inode *inode = mapping->host;
161 struct udf_inode_info *iinfo = UDF_I(inode);
162 loff_t isize = inode->i_size;
163
164 if (to > isize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700165 truncate_pagecache(inode, isize);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100166 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
167 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +0900168 udf_clear_extent_cache(inode);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100169 udf_truncate_extents(inode);
170 up_write(&iinfo->i_data_sem);
171 }
172 }
173}
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static int udf_writepage(struct page *page, struct writeback_control *wbc)
176{
177 return block_write_full_page(page, udf_get_block, wbc);
178}
179
Namjae Jeon378b8e12012-08-31 12:49:07 -0400180static int udf_writepages(struct address_space *mapping,
181 struct writeback_control *wbc)
182{
183 return mpage_writepages(mapping, wbc, udf_get_block);
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static int udf_readpage(struct file *file, struct page *page)
187{
Namjae Jeonbc112322011-10-03 14:02:59 +0900188 return mpage_readpage(page, udf_get_block);
189}
190
191static int udf_readpages(struct file *file, struct address_space *mapping,
192 struct list_head *pages, unsigned nr_pages)
193{
194 return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Nick Pigginbe021ee2007-10-16 01:25:20 -0700197static int udf_write_begin(struct file *file, struct address_space *mapping,
198 loff_t pos, unsigned len, unsigned flags,
199 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200201 int ret;
202
203 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100204 if (unlikely(ret))
205 udf_write_failed(mapping, pos + len);
206 return ret;
207}
Jan Kara7e49b6f2010-10-22 00:30:26 +0200208
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700209static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Ian Abbott5eec54f2012-09-05 17:44:31 +0100210{
211 struct file *file = iocb->ki_filp;
212 struct address_space *mapping = file->f_mapping;
213 struct inode *inode = mapping->host;
Al Viroa6cbcd42014-03-04 22:38:00 -0500214 size_t count = iov_iter_count(iter);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100215 ssize_t ret;
Christoph Hellwig155130a2010-06-04 11:29:58 +0200216
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700217 ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
Omar Sandoval6f673762015-03-16 04:33:52 -0700218 if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700219 udf_write_failed(mapping, iocb->ki_pos + count);
Christoph Hellwig155130a2010-06-04 11:29:58 +0200220 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223static sector_t udf_bmap(struct address_space *mapping, sector_t block)
224{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700225 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700228const struct address_space_operations udf_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700229 .readpage = udf_readpage,
Namjae Jeonbc112322011-10-03 14:02:59 +0900230 .readpages = udf_readpages,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700231 .writepage = udf_writepage,
Namjae Jeon378b8e12012-08-31 12:49:07 -0400232 .writepages = udf_writepages,
Ian Abbott5eec54f2012-09-05 17:44:31 +0100233 .write_begin = udf_write_begin,
234 .write_end = generic_write_end,
235 .direct_IO = udf_direct_IO,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700236 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237};
238
Jan Karad2eb8c32011-12-10 02:30:48 +0100239/*
240 * Expand file stored in ICB to a normal one-block-file
241 *
242 * This function requires i_data_sem for writing and releases it.
243 * This function requires i_mutex held
244 */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200245int udf_expand_file_adinicb(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 struct page *page;
248 char *kaddr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800249 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200250 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 struct writeback_control udf_wbc = {
252 .sync_mode = WB_SYNC_NONE,
253 .nr_to_write = 1,
254 };
255
Al Viro59551022016-01-22 15:40:57 -0500256 WARN_ON_ONCE(!inode_is_locked(inode));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800257 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800259 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800261 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200262 /* from now on we have normal address_space methods */
263 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100264 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100268 /*
269 * Release i_data_sem so that we can lock a page - page lock ranks
270 * above i_data_sem. i_mutex still protects us against file changes.
271 */
272 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Jan Kara7e49b6f2010-10-22 00:30:26 +0200274 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
275 if (!page)
276 return -ENOMEM;
Matt Mackallcd7619d2005-05-01 08:59:01 -0700277
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700278 if (!PageUptodate(page)) {
Fabian Frederick5c26eac2017-04-23 20:58:34 +0200279 kaddr = kmap_atomic(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800280 memset(kaddr + iinfo->i_lenAlloc, 0x00,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300281 PAGE_SIZE - iinfo->i_lenAlloc);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800282 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
283 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 flush_dcache_page(page);
285 SetPageUptodate(page);
Fabian Frederick5c26eac2017-04-23 20:58:34 +0200286 kunmap_atomic(kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100288 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800289 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
290 iinfo->i_lenAlloc);
291 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800293 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800295 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200296 /* from now on we have normal address_space methods */
297 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100298 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200299 err = inode->i_data.a_ops->writepage(page, &udf_wbc);
300 if (err) {
301 /* Restore everything back so that we don't lose data... */
302 lock_page(page);
Jan Karad2eb8c32011-12-10 02:30:48 +0100303 down_write(&iinfo->i_data_sem);
Fabian Frederick5c26eac2017-04-23 20:58:34 +0200304 kaddr = kmap_atomic(page);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200305 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
306 inode->i_size);
Fabian Frederick5c26eac2017-04-23 20:58:34 +0200307 kunmap_atomic(kaddr);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200308 unlock_page(page);
309 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
310 inode->i_data.a_ops = &udf_adinicb_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100311 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200312 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300313 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200315
316 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Steve Magnanib490bdd2017-10-12 08:48:40 -0500319struct buffer_head *udf_expand_dir_adinicb(struct inode *inode,
320 udf_pblk_t *block, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Steve Magnanib490bdd2017-10-12 08:48:40 -0500322 udf_pblk_t newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700323 struct buffer_head *dbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200324 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700326 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 struct udf_fileident_bh sfibh, dfibh;
Jan Karaaf793292008-02-08 04:20:50 -0800329 loff_t f_pos = udf_ext0_offset(inode);
330 int size = udf_ext0_offset(inode) + inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800332 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
335 alloctype = ICBTAG_FLAG_AD_SHORT;
336 else
337 alloctype = ICBTAG_FLAG_AD_LONG;
338
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700339 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800340 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 mark_inode_dirty(inode);
342 return NULL;
343 }
344
345 /* alloc block, and copy data to it */
346 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800347 iinfo->i_location.partitionReferenceNum,
348 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (!(*block))
350 return NULL;
351 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800352 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800353 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!newblock)
355 return NULL;
356 dbh = udf_tgetblk(inode->i_sb, newblock);
357 if (!dbh)
358 return NULL;
359 lock_buffer(dbh);
360 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
361 set_buffer_uptodate(dbh);
362 unlock_buffer(dbh);
363 mark_buffer_dirty_inode(dbh, inode);
364
Marcin Slusarz4b111112008-02-08 04:20:36 -0800365 sfibh.soffset = sfibh.eoffset =
Jan Karaaf793292008-02-08 04:20:50 -0800366 f_pos & (inode->i_sb->s_blocksize - 1);
Jan Karaff116fc2007-05-08 00:35:14 -0700367 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 dfibh.soffset = dfibh.eoffset = 0;
369 dfibh.sbh = dfibh.ebh = dbh;
Jan Karaaf793292008-02-08 04:20:50 -0800370 while (f_pos < size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800371 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800372 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
373 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700374 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700375 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return NULL;
377 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800378 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 sfi->descTag.tagLocation = cpu_to_le32(*block);
380 dfibh.soffset = dfibh.eoffset;
381 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
382 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
383 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800384 sfi->fileIdent +
385 le16_to_cpu(sfi->lengthOfImpUse))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800386 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700387 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return NULL;
389 }
390 }
391 mark_buffer_dirty_inode(dbh, inode);
392
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800393 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
394 iinfo->i_lenAlloc);
395 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800397 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800398 iinfo->i_location.partitionReferenceNum;
Jan Kara2c948b32009-12-03 13:39:28 +0100399 iinfo->i_lenExtents = inode->i_size;
Jan Karaff116fc2007-05-08 00:35:14 -0700400 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800401 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700402 epos.offset = udf_file_entry_alloc_offset(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100403 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* UniqueID stuff */
405
Jan Kara3bf25cb2007-05-08 00:35:16 -0700406 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 mark_inode_dirty(inode);
408 return dbh;
409}
410
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700411static int udf_get_block(struct inode *inode, sector_t block,
412 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 int err, new;
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800415 sector_t phys = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800416 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700418 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 phys = udf_block_map(inode, block);
420 if (phys)
421 map_bh(bh_result, inode->i_sb, phys);
422 return 0;
423 }
424
425 err = -EIO;
426 new = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800427 iinfo = UDF_I(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100428
429 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800430 if (block == iinfo->i_next_alloc_block + 1) {
431 iinfo->i_next_alloc_block++;
432 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
Namjae Jeon99600052013-01-19 11:17:14 +0900435 udf_clear_extent_cache(inode);
Jan Kara7b0b0932011-12-10 01:43:33 +0100436 phys = inode_getblk(inode, block, &err, &new);
437 if (!phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 if (new)
441 set_buffer_new(bh_result);
442 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700443
444abort:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100445 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Steve Magnanib490bdd2017-10-12 08:48:40 -0500449static struct buffer_head *udf_getblk(struct inode *inode, udf_pblk_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700450 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700452 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct buffer_head dummy;
454
455 dummy.b_state = 0;
456 dummy.b_blocknr = -1000;
457 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700458 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700460 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 lock_buffer(bh);
462 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
463 set_buffer_uptodate(bh);
464 unlock_buffer(bh);
465 mark_buffer_dirty_inode(bh, inode);
466 }
467 return bh;
468 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return NULL;
471}
472
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500473/* Extend the file with new blocks totaling 'new_block_bytes',
474 * return the number of extents added
475 */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200476static int udf_do_extend_file(struct inode *inode,
477 struct extent_position *last_pos,
478 struct kernel_long_ad *last_ext,
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500479 loff_t new_block_bytes)
Jan Kara31170b62007-05-08 00:35:21 -0700480{
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500481 uint32_t add;
Jan Kara31170b62007-05-08 00:35:21 -0700482 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
483 struct super_block *sb = inode->i_sb;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200484 struct kernel_lb_addr prealloc_loc = {};
Steve Magnani89a4d972017-10-12 08:48:42 -0500485 uint32_t prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800486 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200487 int err;
Jan Kara31170b62007-05-08 00:35:21 -0700488
489 /* The previous extent is fake and we should not extend by anything
490 * - there's nothing to do... */
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500491 if (!new_block_bytes && fake)
Jan Kara31170b62007-05-08 00:35:21 -0700492 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700493
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800494 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700495 /* Round the last extent up to a multiple of block size */
496 if (last_ext->extLength & (sb->s_blocksize - 1)) {
497 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700498 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
499 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
500 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800501 iinfo->i_lenExtents =
502 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700503 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700504 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700505
Jan Kara31170b62007-05-08 00:35:21 -0700506 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800507 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
508 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700509 /* Save the extent so that we can reattach it to the end */
510 prealloc_loc = last_ext->extLocation;
511 prealloc_len = last_ext->extLength;
512 /* Mark the extent as a hole */
513 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700514 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700515 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800516 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700517 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700518
Jan Kara31170b62007-05-08 00:35:21 -0700519 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800520 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
521 EXT_NOT_RECORDED_NOT_ALLOCATED) {
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500522 add = (1 << 30) - sb->s_blocksize -
523 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
524 if (add > new_block_bytes)
525 add = new_block_bytes;
526 new_block_bytes -= add;
527 last_ext->extLength += add;
Jan Kara31170b62007-05-08 00:35:21 -0700528 }
529
530 if (fake) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200531 udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700532 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700533 count++;
Jan Kara6c371572015-12-23 18:05:03 +0100534 } else {
535 struct kernel_lb_addr tmploc;
536 uint32_t tmplen;
537
Pekka Enberg97e961f2008-10-15 12:29:03 +0200538 udf_write_aext(inode, last_pos, &last_ext->extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800539 last_ext->extLength, 1);
Jan Kara6c371572015-12-23 18:05:03 +0100540 /*
541 * We've rewritten the last extent but there may be empty
542 * indirect extent after it - enter it.
543 */
544 udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
545 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700546
Jan Kara31170b62007-05-08 00:35:21 -0700547 /* Managed to do everything necessary? */
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500548 if (!new_block_bytes)
Jan Kara31170b62007-05-08 00:35:21 -0700549 goto out;
550
551 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
552 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800553 last_ext->extLocation.partitionReferenceNum = 0;
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500554 add = (1 << 30) - sb->s_blocksize;
555 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700556
Jan Kara31170b62007-05-08 00:35:21 -0700557 /* Create enough extents to cover the whole hole */
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500558 while (new_block_bytes > add) {
559 new_block_bytes -= add;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200560 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
561 last_ext->extLength, 1);
562 if (err)
563 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700564 count++;
565 }
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500566 if (new_block_bytes) {
Jan Kara31170b62007-05-08 00:35:21 -0700567 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500568 new_block_bytes;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200569 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
570 last_ext->extLength, 1);
571 if (err)
572 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700573 count++;
574 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700575
576out:
Jan Kara31170b62007-05-08 00:35:21 -0700577 /* Do we have some preallocated blocks saved? */
578 if (prealloc_len) {
Jan Kara7e49b6f2010-10-22 00:30:26 +0200579 err = udf_add_aext(inode, last_pos, &prealloc_loc,
580 prealloc_len, 1);
581 if (err)
582 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700583 last_ext->extLocation = prealloc_loc;
584 last_ext->extLength = prealloc_len;
585 count++;
586 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700587
Jan Kara31170b62007-05-08 00:35:21 -0700588 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800589 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200590 last_pos->offset -= sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800591 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200592 last_pos->offset -= sizeof(struct long_ad);
Jan Kara31170b62007-05-08 00:35:21 -0700593 else
Jan Kara7e49b6f2010-10-22 00:30:26 +0200594 return -EIO;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700595
Jan Kara31170b62007-05-08 00:35:21 -0700596 return count;
597}
598
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500599/* Extend the final block of the file to final_block_len bytes */
600static void udf_do_extend_final_block(struct inode *inode,
601 struct extent_position *last_pos,
602 struct kernel_long_ad *last_ext,
603 uint32_t final_block_len)
604{
605 struct super_block *sb = inode->i_sb;
606 uint32_t added_bytes;
607
608 added_bytes = final_block_len -
609 (last_ext->extLength & (sb->s_blocksize - 1));
610 last_ext->extLength += added_bytes;
611 UDF_I(inode)->i_lenExtents += added_bytes;
612
613 udf_write_aext(inode, last_pos, &last_ext->extLocation,
614 last_ext->extLength, 1);
615}
616
Jan Kara7e49b6f2010-10-22 00:30:26 +0200617static int udf_extend_file(struct inode *inode, loff_t newsize)
618{
619
620 struct extent_position epos;
621 struct kernel_lb_addr eloc;
622 uint32_t elen;
623 int8_t etype;
624 struct super_block *sb = inode->i_sb;
625 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500626 unsigned long partial_final_block;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200627 int adsize;
628 struct udf_inode_info *iinfo = UDF_I(inode);
629 struct kernel_long_ad extent;
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500630 int err = 0;
631 int within_final_block;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200632
633 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
634 adsize = sizeof(struct short_ad);
635 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
636 adsize = sizeof(struct long_ad);
637 else
638 BUG();
639
640 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500641 within_final_block = (etype != -1);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200642
Jan Kara7e49b6f2010-10-22 00:30:26 +0200643 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
644 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
645 /* File has no extents at all or has empty last
646 * indirect extent! Create a fake extent... */
647 extent.extLocation.logicalBlockNum = 0;
648 extent.extLocation.partitionReferenceNum = 0;
649 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
650 } else {
651 epos.offset -= adsize;
652 etype = udf_next_aext(inode, &epos, &extent.extLocation,
653 &extent.extLength, 0);
654 extent.extLength |= etype << 30;
655 }
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500656
657 partial_final_block = newsize & (sb->s_blocksize - 1);
658
659 /* File has extent covering the new size (could happen when extending
660 * inside a block)?
661 */
662 if (within_final_block) {
663 /* Extending file within the last file block */
664 udf_do_extend_final_block(inode, &epos, &extent,
665 partial_final_block);
666 } else {
667 loff_t add = ((loff_t)offset << sb->s_blocksize_bits) |
668 partial_final_block;
669 err = udf_do_extend_file(inode, &epos, &extent, add);
670 }
671
Jan Kara7e49b6f2010-10-22 00:30:26 +0200672 if (err < 0)
673 goto out;
674 err = 0;
675 iinfo->i_lenExtents = newsize;
676out:
677 brelse(epos.bh);
678 return err;
679}
680
Jan Kara7b0b0932011-12-10 01:43:33 +0100681static sector_t inode_getblk(struct inode *inode, sector_t block,
682 int *err, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200684 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700685 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700687 uint32_t elen = 0, tmpelen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200688 struct kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700690 loff_t lbcount = 0, b_off = 0;
Steve Magnanib490bdd2017-10-12 08:48:40 -0500691 udf_pblk_t newblocknum, newblock;
Jan Kara60448b12007-05-08 00:35:13 -0700692 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800694 struct udf_inode_info *iinfo = UDF_I(inode);
Steve Magnanib490bdd2017-10-12 08:48:40 -0500695 udf_pblk_t goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700696 int lastblock = 0;
Namjae Jeonfb719c52012-10-10 00:09:12 +0900697 bool isBeyondEOF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Jan Kara7b0b0932011-12-10 01:43:33 +0100699 *err = 0;
700 *new = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700701 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800702 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700703 prev_epos.bh = NULL;
704 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700705 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700708 alternate between laarr[0] and laarr[1] for locations of the
709 current extent, and the previous extent */
710 do {
711 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700712 brelse(prev_epos.bh);
713 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700714 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700716 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700717 brelse(cur_epos.bh);
718 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700719 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721
722 lbcount += elen;
723
Jan Karaff116fc2007-05-08 00:35:14 -0700724 prev_epos.block = cur_epos.block;
725 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Jan Karaff116fc2007-05-08 00:35:14 -0700727 prev_epos.offset = cur_epos.offset;
728 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Marcin Slusarz4b111112008-02-08 04:20:36 -0800730 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
731 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
733
734 c = !c;
735
736 laarr[c].extLength = (etype << 30) | elen;
737 laarr[c].extLocation = eloc;
738
739 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
740 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700741 ((elen + inode->i_sb->s_blocksize - 1) >>
742 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700744 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 } while (lbcount + elen <= b_off);
746
747 b_off -= lbcount;
748 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700749 /*
750 * Move prev_epos and cur_epos into indirect extent if we are at
751 * the pointer to it
752 */
753 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
754 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700757 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700759 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
760 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700762 ((elen + inode->i_sb->s_blocksize - 1) &
763 ~(inode->i_sb->s_blocksize - 1));
Jan Kara7e49b6f2010-10-22 00:30:26 +0200764 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
Pekka Enberg97e961f2008-10-15 12:29:03 +0200766 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Fabian Frederick02d4ca42017-01-06 21:53:51 +0100767 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769
Jan Kara31170b62007-05-08 00:35:21 -0700770 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700771 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700772 int ret;
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500773 loff_t hole_len;
Fabian Frederick69814982015-02-04 18:26:27 +0100774 isBeyondEOF = true;
Jan Kara31170b62007-05-08 00:35:21 -0700775 if (count) {
776 if (c)
777 laarr[0] = laarr[1];
778 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700779 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700780 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800781 memset(&laarr[0].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200782 sizeof(struct kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700783 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200784 /* Will udf_do_extend_file() create real extent from
Marcin Slusarz4b111112008-02-08 04:20:36 -0800785 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700786 startnum = (offset > 0);
787 }
788 /* Create extents for the hole between EOF and offset */
Steven J. Magnanifa33cdb2019-06-30 21:39:35 -0500789 hole_len = (loff_t)offset << inode->i_blkbits;
790 ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200791 if (ret < 0) {
Jan Kara7e49b6f2010-10-22 00:30:26 +0200792 *err = ret;
Fabian Frederick02d4ca42017-01-06 21:53:51 +0100793 newblock = 0;
794 goto out_free;
Jan Kara31170b62007-05-08 00:35:21 -0700795 }
796 c = 0;
797 offset = 0;
798 count += ret;
799 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800800 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
801 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700802 /* Is there any real extent? - otherwise we overwrite
803 * the fake one... */
804 if (count)
805 c = !c;
806 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700807 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800808 memset(&laarr[c].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200809 sizeof(struct kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700810 count++;
Jan Kara31170b62007-05-08 00:35:21 -0700811 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700812 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700814 } else {
Fabian Frederick69814982015-02-04 18:26:27 +0100815 isBeyondEOF = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 endnum = startnum = ((count > 2) ? 2 : count);
817
Marcin Slusarz4b111112008-02-08 04:20:36 -0800818 /* if the current extent is in position 0,
819 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700820 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700821 laarr[2] = laarr[0];
822 laarr[0] = laarr[1];
823 laarr[1] = laarr[2];
824 c = 1;
825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Marcin Slusarz4b111112008-02-08 04:20:36 -0800827 /* if the current block is located in an extent,
828 read the next extent */
829 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
830 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700831 laarr[c + 1].extLength = (etype << 30) | elen;
832 laarr[c + 1].extLocation = eloc;
833 count++;
834 startnum++;
835 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800836 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 lastblock = 1;
838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700841 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800842 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800844 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800845 if (iinfo->i_next_alloc_block == block)
846 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700848 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800849 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800850 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
Marcin Slusarz4b111112008-02-08 04:20:36 -0800853 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800854 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800855 goal, err);
856 if (!newblocknum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 *err = -ENOSPC;
Fabian Frederick02d4ca42017-01-06 21:53:51 +0100858 newblock = 0;
859 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Namjae Jeonfb719c52012-10-10 00:09:12 +0900861 if (isBeyondEOF)
862 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
Marcin Slusarz4b111112008-02-08 04:20:36 -0800865 /* if the extent the requsted block is located in contains multiple
866 * blocks, split the extent into at most three extents. blocks prior
867 * to requested block, requested block, and blocks after requested
868 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
870
Jan Kara81056dd2009-07-16 18:02:25 +0200871 /* We preallocate blocks only for regular files. It also makes sense
872 * for directories but there's a problem when to drop the
873 * preallocation. We might use some delayed work for that but I feel
874 * it's overengineering for a filesystem like UDF. */
875 if (S_ISREG(inode->i_mode))
876 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 /* merge any continuous blocks in laarr */
879 udf_merge_extents(inode, laarr, &endnum);
880
881 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700882 * of extents is greater than the old number, and deleting extents if
883 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700884 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Marcin Slusarz4b111112008-02-08 04:20:36 -0800886 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800887 iinfo->i_location.partitionReferenceNum, 0);
Jan Kara7b0b0932011-12-10 01:43:33 +0100888 if (!newblock) {
889 *err = -EIO;
Fabian Frederick02d4ca42017-01-06 21:53:51 +0100890 goto out_free;
Jan Kara7b0b0932011-12-10 01:43:33 +0100891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800893 iinfo->i_next_alloc_block = block;
894 iinfo->i_next_alloc_goal = newblocknum;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700895 inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 if (IS_SYNC(inode))
898 udf_sync_inode(inode);
899 else
900 mark_inode_dirty(inode);
Fabian Frederick02d4ca42017-01-06 21:53:51 +0100901out_free:
902 brelse(prev_epos.bh);
903 brelse(cur_epos.bh);
904 brelse(next_epos.bh);
Jan Kara7b0b0932011-12-10 01:43:33 +0100905 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700908static void udf_split_extents(struct inode *inode, int *c, int offset,
Steve Magnanib490bdd2017-10-12 08:48:40 -0500909 udf_pblk_t newblocknum,
910 struct kernel_long_ad *laarr, int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800912 unsigned long blocksize = inode->i_sb->s_blocksize;
913 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800916 (laarr[*c].extLength >> 30) ==
917 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 int curr = *c;
919 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800920 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int8_t etype = (laarr[curr].extLength >> 30);
922
Marcin Slusarz4b111112008-02-08 04:20:36 -0800923 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700924 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800925 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700926 laarr[curr + 2] = laarr[curr + 1];
927 laarr[curr + 1] = laarr[curr];
928 } else {
929 laarr[curr + 3] = laarr[curr + 1];
930 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700933 if (offset) {
934 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800935 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200936 &laarr[curr].extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800937 0, offset);
938 laarr[curr].extLength =
939 EXT_NOT_RECORDED_NOT_ALLOCATED |
940 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800942 laarr[curr].extLocation.
943 partitionReferenceNum = 0;
944 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800946 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700947 curr++;
948 (*c)++;
949 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
Cyrill Gorcunov647bd61a2007-07-15 23:39:47 -0700951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 laarr[curr].extLocation.logicalBlockNum = newblocknum;
953 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
954 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800955 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800957 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700958 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700960 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800962 laarr[curr].extLocation.logicalBlockNum +=
963 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700964 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800965 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700966 curr++;
967 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969 }
970}
971
972static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Fabian Frederick3cc6f842017-01-06 21:53:50 +0100973 struct kernel_long_ad *laarr,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700974 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 int start, length = 0, currlength = 0, i;
977
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700978 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (!lastblock)
980 return;
981 else
982 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700983 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800984 if ((laarr[c + 1].extLength >> 30) ==
985 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700986 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800987 length = currlength =
988 (((laarr[c + 1].extLength &
989 UDF_EXTENT_LENGTH_MASK) +
990 inode->i_sb->s_blocksize - 1) >>
991 inode->i_sb->s_blocksize_bits);
992 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 start = c;
994 }
995
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700996 for (i = start + 1; i <= *endnum; i++) {
997 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (lastblock)
999 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001000 } else if ((laarr[i].extLength >> 30) ==
1001 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
1002 length += (((laarr[i].extLength &
1003 UDF_EXTENT_LENGTH_MASK) +
1004 inode->i_sb->s_blocksize - 1) >>
1005 inode->i_sb->s_blocksize_bits);
1006 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 break;
1008 }
1009
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001010 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001012 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08001013 inode->i_sb->s_blocksize - 1) >>
1014 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001016 laarr[start].extLocation.partitionReferenceNum,
1017 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1018 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1019 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001020 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001021 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001023 (numalloc <<
1024 inode->i_sb->s_blocksize_bits);
1025 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001026 memmove(&laarr[c + 2], &laarr[c + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001027 sizeof(struct long_ad) * (*endnum - (c + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001028 (*endnum)++;
1029 laarr[c + 1].extLocation.logicalBlockNum = next;
1030 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -08001031 laarr[c].extLocation.
1032 partitionReferenceNum;
1033 laarr[c + 1].extLength =
1034 EXT_NOT_RECORDED_ALLOCATED |
1035 (numalloc <<
1036 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001037 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001040 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001041 int elen = ((laarr[i].extLength &
1042 UDF_EXTENT_LENGTH_MASK) +
1043 inode->i_sb->s_blocksize - 1) >>
1044 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001046 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001048 (numalloc <<
1049 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001051 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001053 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001054 memmove(&laarr[i],
1055 &laarr[i + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001056 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001057 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001058 i--;
1059 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001062 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001063 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065 }
1066}
1067
Fabian Frederick3cc6f842017-01-06 21:53:50 +01001068static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001069 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001072 unsigned long blocksize = inode->i_sb->s_blocksize;
1073 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001075 for (i = 0; i < (*endnum - 1); i++) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001076 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1077 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Marcin Slusarz4b111112008-02-08 04:20:36 -08001079 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1080 (((li->extLength >> 30) ==
1081 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1082 ((lip1->extLocation.logicalBlockNum -
1083 li->extLocation.logicalBlockNum) ==
1084 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1085 blocksize - 1) >> blocksize_bits)))) {
1086
1087 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1088 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1089 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1090 lip1->extLength = (lip1->extLength -
1091 (li->extLength &
1092 UDF_EXTENT_LENGTH_MASK) +
1093 UDF_EXTENT_LENGTH_MASK) &
1094 ~(blocksize - 1);
1095 li->extLength = (li->extLength &
1096 UDF_EXTENT_FLAG_MASK) +
1097 (UDF_EXTENT_LENGTH_MASK + 1) -
1098 blocksize;
1099 lip1->extLocation.logicalBlockNum =
1100 li->extLocation.logicalBlockNum +
1101 ((li->extLength &
1102 UDF_EXTENT_LENGTH_MASK) >>
1103 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001104 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001105 li->extLength = lip1->extLength +
1106 (((li->extLength &
1107 UDF_EXTENT_LENGTH_MASK) +
1108 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001109 if (*endnum > (i + 2))
1110 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001111 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001112 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001113 i--;
1114 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001116 } else if (((li->extLength >> 30) ==
1117 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1118 ((lip1->extLength >> 30) ==
1119 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001120 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001121 ((li->extLength &
1122 UDF_EXTENT_LENGTH_MASK) +
1123 blocksize - 1) >> blocksize_bits);
1124 li->extLocation.logicalBlockNum = 0;
1125 li->extLocation.partitionReferenceNum = 0;
1126
1127 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1128 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1129 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1130 lip1->extLength = (lip1->extLength -
1131 (li->extLength &
1132 UDF_EXTENT_LENGTH_MASK) +
1133 UDF_EXTENT_LENGTH_MASK) &
1134 ~(blocksize - 1);
1135 li->extLength = (li->extLength &
1136 UDF_EXTENT_FLAG_MASK) +
1137 (UDF_EXTENT_LENGTH_MASK + 1) -
1138 blocksize;
1139 } else {
1140 li->extLength = lip1->extLength +
1141 (((li->extLength &
1142 UDF_EXTENT_LENGTH_MASK) +
1143 blocksize - 1) & ~(blocksize - 1));
1144 if (*endnum > (i + 2))
1145 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001146 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001147 (*endnum - (i + 2)));
1148 i--;
1149 (*endnum)--;
1150 }
1151 } else if ((li->extLength >> 30) ==
1152 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1153 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001154 &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001155 ((li->extLength &
1156 UDF_EXTENT_LENGTH_MASK) +
1157 blocksize - 1) >> blocksize_bits);
1158 li->extLocation.logicalBlockNum = 0;
1159 li->extLocation.partitionReferenceNum = 0;
1160 li->extLength = (li->extLength &
1161 UDF_EXTENT_LENGTH_MASK) |
1162 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
1164 }
1165}
1166
Fabian Frederick3cc6f842017-01-06 21:53:50 +01001167static void udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001168 int startnum, int endnum,
1169 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 int start = 0, i;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001172 struct kernel_lb_addr tmploc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 uint32_t tmplen;
1174
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001175 if (startnum > endnum) {
1176 for (i = 0; i < (startnum - endnum); i++)
Jan Kara6c1e4d02018-06-13 18:04:24 +02001177 udf_delete_aext(inode, *epos);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001178 } else if (startnum < endnum) {
1179 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001180 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001181 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -07001182 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001183 &laarr[i].extLength, 1);
1184 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186 }
1187
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001188 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001189 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001190 udf_write_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001191 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193}
1194
Steve Magnanib490bdd2017-10-12 08:48:40 -05001195struct buffer_head *udf_bread(struct inode *inode, udf_pblk_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001196 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001198 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 bh = udf_getblk(inode, block, create, err);
1201 if (!bh)
1202 return NULL;
1203
1204 if (buffer_uptodate(bh))
1205 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001206
Mike Christiedfec8a12016-06-05 14:31:44 -05001207 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 wait_on_buffer(bh);
1210 if (buffer_uptodate(bh))
1211 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 brelse(bh);
1214 *err = -EIO;
1215 return NULL;
1216}
1217
Jan Kara7e49b6f2010-10-22 00:30:26 +02001218int udf_setsize(struct inode *inode, loff_t newsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001221 struct udf_inode_info *iinfo;
Steve Magnani89a4d972017-10-12 08:48:42 -05001222 unsigned int bsize = i_blocksize(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001225 S_ISLNK(inode->i_mode)))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001226 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001228 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001230 iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001231 if (newsize > inode->i_size) {
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001232 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001233 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1234 if (bsize <
1235 (udf_file_entry_alloc_offset(inode) + newsize)) {
1236 err = udf_expand_file_adinicb(inode);
Jan Karad2eb8c32011-12-10 02:30:48 +01001237 if (err)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001238 return err;
Jan Karad2eb8c32011-12-10 02:30:48 +01001239 down_write(&iinfo->i_data_sem);
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001240 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001241 iinfo->i_lenAlloc = newsize;
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001242 goto set_size;
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001245 err = udf_extend_file(inode, newsize);
1246 if (err) {
1247 up_write(&iinfo->i_data_sem);
1248 return err;
1249 }
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001250set_size:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001251 up_write(&iinfo->i_data_sem);
Jan Karaf2e95352017-06-13 16:20:25 +02001252 truncate_setsize(inode, newsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001253 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001254 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1255 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001256 udf_clear_extent_cache(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001257 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1258 0x00, bsize - newsize -
1259 udf_file_entry_alloc_offset(inode));
1260 iinfo->i_lenAlloc = newsize;
1261 truncate_setsize(inode, newsize);
1262 up_write(&iinfo->i_data_sem);
1263 goto update_time;
1264 }
1265 err = block_truncate_page(inode->i_mapping, newsize,
1266 udf_get_block);
1267 if (err)
1268 return err;
Jan Karaf2e95352017-06-13 16:20:25 +02001269 truncate_setsize(inode, newsize);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001270 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001271 udf_clear_extent_cache(inode);
Jan Kara2b42be52019-03-11 15:27:02 +01001272 err = udf_truncate_extents(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001273 up_write(&iinfo->i_data_sem);
Jan Kara2b42be52019-03-11 15:27:02 +01001274 if (err)
1275 return err;
Cyrill Gorcunov647bd61a2007-07-15 23:39:47 -07001276 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001277update_time:
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001278 inode->i_mtime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001280 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 else
1282 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Jan Karac03aa9f2014-09-04 14:06:55 +02001286/*
1287 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1288 * arbitrary - just that we hopefully don't limit any real use of rewritten
1289 * inode on write-once media but avoid looping for too long on corrupted media.
1290 */
1291#define UDF_MAX_ICB_NESTING 1024
1292
Jan Kara6174c2e2014-10-09 12:52:16 +02001293static int udf_read_inode(struct inode *inode, bool hidden_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
1295 struct buffer_head *bh = NULL;
1296 struct fileEntry *fe;
Jan Karabb7720a02014-09-04 13:32:50 +02001297 struct extendedFileEntry *efe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001299 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Karabb7720a02014-09-04 13:32:50 +02001300 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001301 struct kernel_lb_addr *iloc = &iinfo->i_location;
Jan Karabb7720a02014-09-04 13:32:50 +02001302 unsigned int link_count;
Jan Karac03aa9f2014-09-04 14:06:55 +02001303 unsigned int indirections = 0;
Jan Kara79144952015-01-07 13:46:16 +01001304 int bs = inode->i_sb->s_blocksize;
Jan Kara6d3d5e82014-09-04 16:15:51 +02001305 int ret = -EIO;
Jan Kara0c9850f2018-02-22 10:28:52 +01001306 uint32_t uid, gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Jan Karac03aa9f2014-09-04 14:06:55 +02001308reread:
Fabian Frederick1d82a562017-01-06 21:54:43 +01001309 if (iloc->partitionReferenceNum >= sbi->s_partitions) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05001310 udf_debug("partition reference: %u > logical volume partitions: %u\n",
Fabian Frederick1d82a562017-01-06 21:54:43 +01001311 iloc->partitionReferenceNum, sbi->s_partitions);
1312 return -EIO;
1313 }
1314
Jan Kara6d3d5e82014-09-04 16:15:51 +02001315 if (iloc->logicalBlockNum >=
1316 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05001317 udf_debug("block=%u, partition=%u out of range\n",
Jan Kara6d3d5e82014-09-04 16:15:51 +02001318 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1319 return -EIO;
1320 }
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 /*
1323 * Set defaults, but the inode is still incomplete!
1324 * Note: get_new_inode() sets the following on a new inode:
1325 * i_sb = sb
1326 * i_no = ino
1327 * i_flags = sb->s_flags
1328 * i_state = 0
1329 * clean_inode(): zero fills and sets
1330 * i_count = 1
1331 * i_nlink = 1
1332 * i_op = NULL;
1333 */
Jan Kara6d3d5e82014-09-04 16:15:51 +02001334 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001335 if (!bh) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05001336 udf_err(inode->i_sb, "(ino %lu) failed !bh\n", inode->i_ino);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001337 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339
1340 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001341 ident != TAG_IDENT_USE) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05001342 udf_err(inode->i_sb, "(ino %lu) failed ident=%u\n",
Joe Perches78ace702011-10-10 01:08:05 -07001343 inode->i_ino, ident);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001344 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346
1347 fe = (struct fileEntry *)bh->b_data;
Jan Karabb7720a02014-09-04 13:32:50 +02001348 efe = (struct extendedFileEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001350 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001351 struct buffer_head *ibh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Jan Kara6d3d5e82014-09-04 16:15:51 +02001353 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001354 if (ident == TAG_IDENT_IE && ibh) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001355 struct kernel_lb_addr loc;
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001356 struct indirectEntry *ie;
Cyrill Gorcunov647bd61a2007-07-15 23:39:47 -07001357
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001358 ie = (struct indirectEntry *)ibh->b_data;
1359 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd61a2007-07-15 23:39:47 -07001360
Jan Karac03aa9f2014-09-04 14:06:55 +02001361 if (ie->indirectICB.extLength) {
Jan Karac03aa9f2014-09-04 14:06:55 +02001362 brelse(ibh);
1363 memcpy(&iinfo->i_location, &loc,
1364 sizeof(struct kernel_lb_addr));
1365 if (++indirections > UDF_MAX_ICB_NESTING) {
1366 udf_err(inode->i_sb,
1367 "too many ICBs in ICB hierarchy"
1368 " (max %d supported)\n",
1369 UDF_MAX_ICB_NESTING);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001370 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001371 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001372 brelse(bh);
Jan Karac03aa9f2014-09-04 14:06:55 +02001373 goto reread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001375 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001376 brelse(ibh);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001377 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05001378 udf_err(inode->i_sb, "unsupported strategy type: %u\n",
Joe Perches78ace702011-10-10 01:08:05 -07001379 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001380 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001382 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001383 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001384 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001385 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001387 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001388 ICBTAG_FLAG_AD_MASK;
Jan Karad288d952018-12-12 14:29:20 +01001389 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
1390 iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
1391 iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1392 ret = -EIO;
1393 goto out;
1394 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001395 iinfo->i_unique = 0;
1396 iinfo->i_lenEAttr = 0;
1397 iinfo->i_lenExtents = 0;
1398 iinfo->i_lenAlloc = 0;
1399 iinfo->i_next_alloc_block = 0;
1400 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001401 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001402 iinfo->i_efe = 1;
1403 iinfo->i_use = 0;
Jan Kara79144952015-01-07 13:46:16 +01001404 ret = udf_alloc_i_data(inode, bs -
Jan Kara6d3d5e82014-09-04 16:15:51 +02001405 sizeof(struct extendedFileEntry));
1406 if (ret)
1407 goto out;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001408 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001409 bh->b_data + sizeof(struct extendedFileEntry),
Jan Kara79144952015-01-07 13:46:16 +01001410 bs - sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001411 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001412 iinfo->i_efe = 0;
1413 iinfo->i_use = 0;
Jan Kara79144952015-01-07 13:46:16 +01001414 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001415 if (ret)
1416 goto out;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001417 memcpy(iinfo->i_ext.i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001418 bh->b_data + sizeof(struct fileEntry),
Jan Kara79144952015-01-07 13:46:16 +01001419 bs - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001420 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001421 iinfo->i_efe = 0;
1422 iinfo->i_use = 1;
1423 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001424 ((struct unallocSpaceEntry *)bh->b_data)->
1425 lengthAllocDescs);
Jan Kara79144952015-01-07 13:46:16 +01001426 ret = udf_alloc_i_data(inode, bs -
Jan Kara6d3d5e82014-09-04 16:15:51 +02001427 sizeof(struct unallocSpaceEntry));
1428 if (ret)
1429 goto out;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001430 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001431 bh->b_data + sizeof(struct unallocSpaceEntry),
Jan Kara79144952015-01-07 13:46:16 +01001432 bs - sizeof(struct unallocSpaceEntry));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435
Jan Kara6d3d5e82014-09-04 16:15:51 +02001436 ret = -EIO;
Jan Karac03cad22010-10-20 22:17:28 +02001437 read_lock(&sbi->s_cred_lock);
Jan Kara0c9850f2018-02-22 10:28:52 +01001438 uid = le32_to_cpu(fe->uid);
1439 if (uid == UDF_INVALID_ID ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001440 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Jan Kara0c9850f2018-02-22 10:28:52 +01001441 inode->i_uid = sbi->s_uid;
1442 else
1443 i_uid_write(inode, uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Jan Kara0c9850f2018-02-22 10:28:52 +01001445 gid = le32_to_cpu(fe->gid);
1446 if (gid == UDF_INVALID_ID ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001447 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Jan Kara0c9850f2018-02-22 10:28:52 +01001448 inode->i_gid = sbi->s_gid;
1449 else
1450 i_gid_write(inode, gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001452 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001453 sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001454 inode->i_mode = sbi->s_fmode;
1455 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001456 sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001457 inode->i_mode = sbi->s_dmode;
1458 else
1459 inode->i_mode = udf_convert_permissions(fe);
1460 inode->i_mode &= ~sbi->s_umask;
Jan Karac03cad22010-10-20 22:17:28 +02001461 read_unlock(&sbi->s_cred_lock);
1462
Miklos Szeredibfe86842011-10-28 14:13:29 +02001463 link_count = le16_to_cpu(fe->fileLinkCount);
Jan Kara4071b912014-09-04 16:19:47 +02001464 if (!link_count) {
Jan Kara6174c2e2014-10-09 12:52:16 +02001465 if (!hidden_inode) {
1466 ret = -ESTALE;
1467 goto out;
1468 }
1469 link_count = 1;
Jan Kara4071b912014-09-04 16:19:47 +02001470 }
Miklos Szeredibfe86842011-10-28 14:13:29 +02001471 set_nlink(inode, link_count);
Jan Karac03cad22010-10-20 22:17:28 +02001472
1473 inode->i_size = le64_to_cpu(fe->informationLength);
1474 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001476 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001478 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001480 udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime);
1481 udf_disk_stamp_to_time(&inode->i_mtime, fe->modificationTime);
1482 udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001484 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1485 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1486 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001487 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001488 } else {
Cyrill Gorcunov647bd61a2007-07-15 23:39:47 -07001489 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -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, efe->accessTime);
1493 udf_disk_stamp_to_time(&inode->i_mtime, efe->modificationTime);
Deepa Dinamani0220edd2018-05-10 08:26:17 -07001494 udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime);
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001495 udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001497 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1498 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1499 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001500 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
Jan Kara470cca52014-09-04 16:26:19 +02001502 inode->i_generation = iinfo->i_unique;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Jan Kara23b133bd2015-01-07 13:49:08 +01001504 /*
1505 * Sanity check length of allocation descriptors and extended attrs to
1506 * avoid integer overflows
1507 */
1508 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1509 goto out;
1510 /* Now do exact checks */
1511 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1512 goto out;
Jan Karae1593322014-12-19 12:03:53 +01001513 /* Sanity checks for files in ICB so that we don't get confused later */
1514 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1515 /*
1516 * For file in ICB data is stored in allocation descriptor
1517 * so sizes should match
1518 */
1519 if (iinfo->i_lenAlloc != inode->i_size)
1520 goto out;
1521 /* File in ICB has to fit in there... */
Jan Kara79144952015-01-07 13:46:16 +01001522 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
Jan Karae1593322014-12-19 12:03:53 +01001523 goto out;
1524 }
1525
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001526 switch (fe->icbTag.fileType) {
1527 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001528 inode->i_op = &udf_dir_inode_operations;
1529 inode->i_fop = &udf_dir_operations;
1530 inode->i_mode |= S_IFDIR;
1531 inc_nlink(inode);
1532 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001533 case ICBTAG_FILE_TYPE_REALTIME:
1534 case ICBTAG_FILE_TYPE_REGULAR:
1535 case ICBTAG_FILE_TYPE_UNDEF:
Jan Kara742e1792008-04-08 01:17:52 +02001536 case ICBTAG_FILE_TYPE_VAT20:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001537 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001538 inode->i_data.a_ops = &udf_adinicb_aops;
1539 else
1540 inode->i_data.a_ops = &udf_aops;
1541 inode->i_op = &udf_file_inode_operations;
1542 inode->i_fop = &udf_file_operations;
1543 inode->i_mode |= S_IFREG;
1544 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001545 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001546 inode->i_mode |= S_IFBLK;
1547 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001548 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001549 inode->i_mode |= S_IFCHR;
1550 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001551 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001552 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1553 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001554 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001555 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1556 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001557 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001558 inode->i_data.a_ops = &udf_symlink_aops;
Jan Karaad4d0532017-01-02 14:30:31 +01001559 inode->i_op = &udf_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05001560 inode_nohighmem(inode);
Fabian Frederick6ff6b2b2017-04-23 20:58:15 +02001561 inode->i_mode = S_IFLNK | 0777;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001562 break;
Jan Karabfb257a2008-04-08 20:37:21 +02001563 case ICBTAG_FILE_TYPE_MAIN:
1564 udf_debug("METADATA FILE-----\n");
1565 break;
1566 case ICBTAG_FILE_TYPE_MIRROR:
1567 udf_debug("METADATA MIRROR FILE-----\n");
1568 break;
1569 case ICBTAG_FILE_TYPE_BITMAP:
1570 udf_debug("METADATA BITMAP FILE-----\n");
1571 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001572 default:
Steve Magnanifcbf7632017-10-12 08:48:41 -05001573 udf_err(inode->i_sb, "(ino %lu) failed unknown file type=%u\n",
Joe Perches78ace702011-10-10 01:08:05 -07001574 inode->i_ino, fe->icbTag.fileType);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001575 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001577 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001578 struct deviceSpec *dsea =
1579 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001580 if (dsea) {
1581 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001582 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1583 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001585 } else
Jan Kara6d3d5e82014-09-04 16:15:51 +02001586 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001588 ret = 0;
1589out:
Jan Karabb7720a02014-09-04 13:32:50 +02001590 brelse(bh);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001591 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
Cyrill Gorcunov647bd61a2007-07-15 23:39:47 -07001594static int udf_alloc_i_data(struct inode *inode, size_t size)
1595{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001596 struct udf_inode_info *iinfo = UDF_I(inode);
1597 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
Markus Elfringb5f52452017-08-15 16:14:19 +02001598 if (!iinfo->i_ext.i_data)
Cyrill Gorcunov647bd61a2007-07-15 23:39:47 -07001599 return -ENOMEM;
Cyrill Gorcunov647bd61a2007-07-15 23:39:47 -07001600 return 0;
1601}
1602
Al Virofaa17292011-07-26 03:18:29 -04001603static umode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
Al Virofaa17292011-07-26 03:18:29 -04001605 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 uint32_t permissions;
1607 uint32_t flags;
1608
1609 permissions = le32_to_cpu(fe->permissions);
1610 flags = le16_to_cpu(fe->icbTag.flags);
1611
Fabian Frederick6ff6b2b2017-04-23 20:58:15 +02001612 mode = ((permissions) & 0007) |
1613 ((permissions >> 2) & 0070) |
1614 ((permissions >> 4) & 0700) |
Marcin Slusarz4b111112008-02-08 04:20:36 -08001615 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1616 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1617 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 return mode;
1620}
1621
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001622int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Jan Kara49521de2010-10-20 17:42:44 +02001624 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
Jan Kara49521de2010-10-20 17:42:44 +02001627static int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629 return udf_update_inode(inode, 1);
1630}
1631
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001632static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec64 time)
Fabian Frederick7ed0fbd2017-01-06 21:53:54 +01001633{
1634 if (iinfo->i_crtime.tv_sec > time.tv_sec ||
1635 (iinfo->i_crtime.tv_sec == time.tv_sec &&
1636 iinfo->i_crtime.tv_nsec > time.tv_nsec))
1637 iinfo->i_crtime = time;
1638}
1639
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001640static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
1642 struct buffer_head *bh = NULL;
1643 struct fileEntry *fe;
1644 struct extendedFileEntry *efe;
Steve Nickelb2527bf2012-02-16 12:53:53 -05001645 uint64_t lb_recorded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 uint32_t udfperms;
1647 uint16_t icbflags;
1648 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001650 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001651 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001652 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Jan Kara5833ded2010-01-08 16:52:59 +01001654 bh = udf_tgetblk(inode->i_sb,
1655 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001656 if (!bh) {
Jan Karaaae917c2010-01-08 16:46:29 +01001657 udf_debug("getblk failure\n");
Changwoo Min0fd2ba32015-03-22 19:17:49 -04001658 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660
Jan Karaaae917c2010-01-08 16:46:29 +01001661 lock_buffer(bh);
1662 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 fe = (struct fileEntry *)bh->b_data;
1664 efe = (struct extendedFileEntry *)bh->b_data;
1665
Jan Karaaae917c2010-01-08 16:46:29 +01001666 if (iinfo->i_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001668 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001670 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001671 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001672 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001673 sizeof(struct unallocSpaceEntry));
Jan Karaaae917c2010-01-08 16:46:29 +01001674 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001675 crclen = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001677 goto finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
1679
Phillip Susi4d6660e2006-03-07 21:55:24 -08001680 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
Jan Kara0c9850f2018-02-22 10:28:52 +01001681 fe->uid = cpu_to_le32(UDF_INVALID_ID);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001682 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001683 fe->uid = cpu_to_le32(i_uid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Phillip Susi4d6660e2006-03-07 21:55:24 -08001685 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
Jan Kara0c9850f2018-02-22 10:28:52 +01001686 fe->gid = cpu_to_le32(UDF_INVALID_ID);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001687 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001688 fe->gid = cpu_to_le32(i_gid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Fabian Frederick6ff6b2b2017-04-23 20:58:15 +02001690 udfperms = ((inode->i_mode & 0007)) |
1691 ((inode->i_mode & 0070) << 2) |
1692 ((inode->i_mode & 0700) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Marcin Slusarz4b111112008-02-08 04:20:36 -08001694 udfperms |= (le32_to_cpu(fe->permissions) &
1695 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1696 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1697 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 fe->permissions = cpu_to_le32(udfperms);
1699
Jan Kara8a70ee32014-09-04 11:47:51 +02001700 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1702 else
1703 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1704
1705 fe->informationLength = cpu_to_le64(inode->i_size);
1706
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001707 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001708 struct regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001709 struct deviceSpec *dsea =
1710 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001711 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001713 udf_add_extendedattr(inode,
1714 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001715 sizeof(struct regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 dsea->attrType = cpu_to_le32(12);
1717 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001718 dsea->attrLength = cpu_to_le32(
1719 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001720 sizeof(struct regid));
1721 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001723 eid = (struct regid *)dsea->impUse;
Markus Elfring033c9da2017-08-15 16:45:44 +02001724 memset(eid, 0, sizeof(*eid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 strcpy(eid->ident, UDF_ID_DEVELOPER);
1726 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1727 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1728 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1729 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1730 }
1731
Steve Nickelb2527bf2012-02-16 12:53:53 -05001732 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1733 lb_recorded = 0; /* No extents => no blocks! */
1734 else
1735 lb_recorded =
1736 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1737 (blocksize_bits - 9);
1738
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001739 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001740 memcpy(bh->b_data + sizeof(struct fileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001741 iinfo->i_ext.i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001742 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Steve Nickelb2527bf2012-02-16 12:53:53 -05001743 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001745 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1746 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1747 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001748 memset(&(fe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1750 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1751 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001752 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1753 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1754 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001755 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1757 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001758 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001759 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001760 iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001761 inode->i_sb->s_blocksize -
1762 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 efe->objectSize = cpu_to_le64(inode->i_size);
Steve Nickelb2527bf2012-02-16 12:53:53 -05001764 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001766 udf_adjust_time(iinfo, inode->i_atime);
1767 udf_adjust_time(iinfo, inode->i_mtime);
1768 udf_adjust_time(iinfo, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001770 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1771 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
Marcin Slusarz56774802008-02-10 11:25:31 +01001772 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001773 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Markus Elfring033c9da2017-08-15 16:45:44 +02001775 memset(&(efe->impIdent), 0, sizeof(efe->impIdent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1777 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1778 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001779 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1780 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1781 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001782 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1784 crclen = sizeof(struct extendedFileEntry);
1785 }
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001786
1787finish:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001788 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 fe->icbTag.strategyType = cpu_to_le16(4096);
1790 fe->icbTag.strategyParameter = cpu_to_le16(1);
1791 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001792 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 fe->icbTag.strategyType = cpu_to_le16(4);
1794 fe->icbTag.numEntries = cpu_to_le16(1);
1795 }
1796
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001797 if (iinfo->i_use)
1798 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1799 else if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1801 else if (S_ISREG(inode->i_mode))
1802 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1803 else if (S_ISLNK(inode->i_mode))
1804 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1805 else if (S_ISBLK(inode->i_mode))
1806 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1807 else if (S_ISCHR(inode->i_mode))
1808 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1809 else if (S_ISFIFO(inode->i_mode))
1810 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1811 else if (S_ISSOCK(inode->i_mode))
1812 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1813
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001814 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001815 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1816 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1817 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1818 (le16_to_cpu(fe->icbTag.flags) &
1819 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1820 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001823 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 fe->descTag.descVersion = cpu_to_le16(3);
1825 else
1826 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001827 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001828 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001829 iinfo->i_location.logicalBlockNum);
Jan Karaaae917c2010-01-08 16:46:29 +01001830 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001832 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001833 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001834 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Jan Kara5833ded2010-01-08 16:52:59 +01001836 set_buffer_uptodate(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001837 unlock_buffer(bh);
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 /* write the data blocks */
1840 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001841 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 sync_dirty_buffer(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001843 if (buffer_write_io_error(bh)) {
Joe Perches78ace702011-10-10 01:08:05 -07001844 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1845 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 err = -EIO;
1847 }
1848 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001849 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return err;
1852}
1853
Jan Kara6174c2e2014-10-09 12:52:16 +02001854struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1855 bool hidden_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1858 struct inode *inode = iget_locked(sb, block);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001859 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 if (!inode)
Jan Kara6d3d5e82014-09-04 16:15:51 +02001862 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Jan Kara6d3d5e82014-09-04 16:15:51 +02001864 if (!(inode->i_state & I_NEW))
1865 return inode;
1866
1867 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
Jan Kara6174c2e2014-10-09 12:52:16 +02001868 err = udf_read_inode(inode, hidden_inode);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001869 if (err < 0) {
1870 iget_failed(inode);
1871 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001873 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
Steve Magnanib490bdd2017-10-12 08:48:40 -05001878int udf_setup_indirect_aext(struct inode *inode, udf_pblk_t block,
Jan Karafcea62b2015-12-23 14:21:13 +01001879 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Jan Karafcea62b2015-12-23 14:21:13 +01001881 struct super_block *sb = inode->i_sb;
1882 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 struct allocExtDesc *aed;
Jan Karafcea62b2015-12-23 14:21:13 +01001884 struct extent_position nepos;
1885 struct kernel_lb_addr neloc;
1886 int ver, adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Jan Karafcea62b2015-12-23 14:21:13 +01001888 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1889 adsize = sizeof(struct short_ad);
1890 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1891 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 else
Arnd Bergmann4f1b1512016-01-01 15:21:54 +01001893 return -EIO;
Jan Karafcea62b2015-12-23 14:21:13 +01001894
1895 neloc.logicalBlockNum = block;
1896 neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1897
1898 bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1899 if (!bh)
1900 return -EIO;
1901 lock_buffer(bh);
1902 memset(bh->b_data, 0x00, sb->s_blocksize);
1903 set_buffer_uptodate(bh);
1904 unlock_buffer(bh);
1905 mark_buffer_dirty_inode(bh, inode);
1906
1907 aed = (struct allocExtDesc *)(bh->b_data);
1908 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1909 aed->previousAllocExtLocation =
1910 cpu_to_le32(epos->block.logicalBlockNum);
1911 }
1912 aed->lengthAllocDescs = cpu_to_le32(0);
1913 if (UDF_SB(sb)->s_udfrev >= 0x0200)
1914 ver = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 else
Jan Karafcea62b2015-12-23 14:21:13 +01001916 ver = 2;
1917 udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1918 sizeof(struct tag));
1919
1920 nepos.block = neloc;
1921 nepos.offset = sizeof(struct allocExtDesc);
1922 nepos.bh = bh;
1923
1924 /*
1925 * Do we have to copy current last extent to make space for indirect
1926 * one?
1927 */
1928 if (epos->offset + adsize > sb->s_blocksize) {
1929 struct kernel_lb_addr cp_loc;
1930 uint32_t cp_len;
1931 int cp_type;
1932
1933 epos->offset -= adsize;
1934 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1935 cp_len |= ((uint32_t)cp_type) << 30;
1936
1937 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1938 udf_write_aext(inode, epos, &nepos.block,
1939 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1940 } else {
1941 __udf_add_aext(inode, epos, &nepos.block,
1942 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1943 }
1944
1945 brelse(epos->bh);
1946 *epos = nepos;
1947
1948 return 0;
1949}
1950
1951/*
1952 * Append extent at the given position - should be the first free one in inode
1953 * / indirect extent. This function assumes there is enough space in the inode
1954 * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1955 */
1956int __udf_add_aext(struct inode *inode, struct extent_position *epos,
1957 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1958{
1959 struct udf_inode_info *iinfo = UDF_I(inode);
1960 struct allocExtDesc *aed;
1961 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001963 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001964 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001965 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001966 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 else
Jan Kara7e49b6f2010-10-22 00:30:26 +02001968 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Jan Karafcea62b2015-12-23 14:21:13 +01001970 if (!epos->bh) {
1971 WARN_ON(iinfo->i_lenAlloc !=
1972 epos->offset - udf_file_entry_alloc_offset(inode));
1973 } else {
1974 aed = (struct allocExtDesc *)epos->bh->b_data;
1975 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
1976 epos->offset - sizeof(struct allocExtDesc));
1977 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
1979
Jan Kara7e49b6f2010-10-22 00:30:26 +02001980 udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001982 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001983 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001985 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001986 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001987 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001988 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1989 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1990 udf_update_tag(epos->bh->b_data,
1991 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001993 udf_update_tag(epos->bh->b_data,
1994 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001995 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997
Jan Kara7e49b6f2010-10-22 00:30:26 +02001998 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
Jan Karafcea62b2015-12-23 14:21:13 +01002001/*
2002 * Append extent at given position - should be the first free one in inode
2003 * / indirect extent. Takes care of allocating and linking indirect blocks.
2004 */
2005int udf_add_aext(struct inode *inode, struct extent_position *epos,
2006 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2007{
2008 int adsize;
2009 struct super_block *sb = inode->i_sb;
2010
2011 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2012 adsize = sizeof(struct short_ad);
2013 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2014 adsize = sizeof(struct long_ad);
2015 else
2016 return -EIO;
2017
2018 if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2019 int err;
Steve Magnanib490bdd2017-10-12 08:48:40 -05002020 udf_pblk_t new_block;
Jan Karafcea62b2015-12-23 14:21:13 +01002021
2022 new_block = udf_new_block(sb, NULL,
2023 epos->block.partitionReferenceNum,
2024 epos->block.logicalBlockNum, &err);
2025 if (!new_block)
2026 return -ENOSPC;
2027
2028 err = udf_setup_indirect_aext(inode, new_block, epos);
2029 if (err)
2030 return err;
2031 }
2032
2033 return __udf_add_aext(inode, epos, eloc, elen, inc);
2034}
2035
Jan Kara7e49b6f2010-10-22 00:30:26 +02002036void udf_write_aext(struct inode *inode, struct extent_position *epos,
2037 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
2039 int adsize;
2040 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002041 struct short_ad *sad;
2042 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002043 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Jan Karaff116fc2007-05-08 00:35:14 -07002045 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002046 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002047 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002048 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 else
Jan Karaff116fc2007-05-08 00:35:14 -07002050 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002052 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002053 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002054 sad = (struct short_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002055 sad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002056 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002057 adsize = sizeof(struct short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002058 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002059 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002060 lad = (struct long_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002061 lad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002062 lad->extLocation = cpu_to_lelb(*eloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002063 memset(lad->impUse, 0x00, sizeof(lad->impUse));
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002064 adsize = sizeof(struct long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002065 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002066 default:
Jan Kara7e49b6f2010-10-22 00:30:26 +02002067 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
2069
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002070 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002071 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002072 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002073 struct allocExtDesc *aed =
2074 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07002075 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002076 le32_to_cpu(aed->lengthAllocDescs) +
2077 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
Jan Karaff116fc2007-05-08 00:35:14 -07002079 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002080 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07002085 epos->offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
Vegard Nossumb0918d92015-12-11 15:54:16 +01002088/*
2089 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2090 * someone does some weird stuff.
2091 */
2092#define UDF_MAX_INDIR_EXTS 16
2093
Marcin Slusarz4b111112008-02-08 04:20:36 -08002094int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002095 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
2097 int8_t etype;
Vegard Nossumb0918d92015-12-11 15:54:16 +01002098 unsigned int indirections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Jan Karaff116fc2007-05-08 00:35:14 -07002100 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002101 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Steve Magnanib490bdd2017-10-12 08:48:40 -05002102 udf_pblk_t block;
Vegard Nossumb0918d92015-12-11 15:54:16 +01002103
2104 if (++indirections > UDF_MAX_INDIR_EXTS) {
2105 udf_err(inode->i_sb,
2106 "too many indirect extents in inode %lu\n",
2107 inode->i_ino);
2108 return -1;
2109 }
2110
Jan Karaff116fc2007-05-08 00:35:14 -07002111 epos->block = *eloc;
2112 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002113 brelse(epos->bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002114 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002115 epos->bh = udf_tread(inode->i_sb, block);
2116 if (!epos->bh) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05002117 udf_debug("reading block %u failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 return -1;
2119 }
2120 }
2121
2122 return etype;
2123}
2124
Marcin Slusarz4b111112008-02-08 04:20:36 -08002125int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002126 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 int alen;
2129 int8_t etype;
2130 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002131 struct short_ad *sad;
2132 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002133 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002134
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002135 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002136 if (!epos->offset)
2137 epos->offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002138 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002139 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002140 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08002141 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002142 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002143 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002144 if (!epos->offset)
2145 epos->offset = sizeof(struct allocExtDesc);
2146 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002147 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08002148 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2149 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 }
2151
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002152 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002153 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002154 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2155 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002157 etype = le32_to_cpu(sad->extLength) >> 30;
2158 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002159 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002160 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002161 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2162 break;
2163 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002164 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2165 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002166 return -1;
2167 etype = le32_to_cpu(lad->extLength) >> 30;
2168 *eloc = lelb_to_cpu(lad->extLocation);
2169 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2170 break;
2171 default:
Steve Magnanifcbf7632017-10-12 08:48:41 -05002172 udf_debug("alloc_type = %u unsupported\n", iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002173 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
2175
2176 return etype;
2177}
2178
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002179static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002180 struct kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002182 struct kernel_lb_addr oeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 uint32_t oelen;
2184 int8_t etype;
2185
Jan Karaff116fc2007-05-08 00:35:14 -07002186 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07002187 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002189 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002190 udf_write_aext(inode, &epos, &neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 neloc = oeloc;
2192 nelen = (etype << 30) | oelen;
2193 }
Pekka Enberg97e961f2008-10-15 12:29:03 +02002194 udf_add_aext(inode, &epos, &neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002195 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return (nelen >> 30);
2198}
2199
Jan Kara6c1e4d02018-06-13 18:04:24 +02002200int8_t udf_delete_aext(struct inode *inode, struct extent_position epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Jan Karaff116fc2007-05-08 00:35:14 -07002202 struct extent_position oepos;
2203 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 int8_t etype;
2205 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002206 struct udf_inode_info *iinfo;
Jan Kara6c1e4d02018-06-13 18:04:24 +02002207 struct kernel_lb_addr eloc;
2208 uint32_t elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002210 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002211 get_bh(epos.bh);
2212 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 }
2214
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002215 iinfo = UDF_I(inode);
2216 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002217 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002218 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002219 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 else
2221 adsize = 0;
2222
Jan Karaff116fc2007-05-08 00:35:14 -07002223 oepos = epos;
2224 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return -1;
2226
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002227 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002228 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002229 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002230 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07002231 brelse(oepos.bh);
2232 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07002233 oepos.bh = epos.bh;
2234 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002237 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 elen = 0;
2239
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002240 if (epos.bh != oepos.bh) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002241 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2242 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2243 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002244 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002245 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002247 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002248 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002249 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002250 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002251 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002252 udf_update_tag(oepos.bh->b_data,
2253 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002255 udf_update_tag(oepos.bh->b_data,
2256 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002257 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002259 } else {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002260 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002261 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002262 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002264 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002265 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002266 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002267 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002268 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002269 udf_update_tag(oepos.bh->b_data,
2270 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002272 udf_update_tag(oepos.bh->b_data,
2273 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002274 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
2276 }
Cyrill Gorcunov647bd61a2007-07-15 23:39:47 -07002277
Jan Kara3bf25cb2007-05-08 00:35:16 -07002278 brelse(epos.bh);
2279 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 return (elen >> 30);
2282}
2283
Marcin Slusarz4b111112008-02-08 04:20:36 -08002284int8_t inode_bmap(struct inode *inode, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002285 struct extent_position *pos, struct kernel_lb_addr *eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002286 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002288 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Fabian Frederickbbc9abd2017-01-06 21:53:52 +01002289 loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002291 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002293 iinfo = UDF_I(inode);
Namjae Jeon99600052013-01-19 11:17:14 +09002294 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2295 pos->offset = 0;
2296 pos->block = iinfo->i_location;
2297 pos->bh = NULL;
2298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 *elen = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002300 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002301 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2302 if (etype == -1) {
2303 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002304 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return -1;
2306 }
2307 lbcount += *elen;
2308 } while (lbcount <= bcount);
Namjae Jeon99600052013-01-19 11:17:14 +09002309 /* update extent cache */
Fabian Frederickb31c9ed2017-01-06 21:53:56 +01002310 udf_update_extent_cache(inode, lbcount - *elen, pos);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002311 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 return etype;
2314}
2315
Steve Magnanib490bdd2017-10-12 08:48:40 -05002316udf_pblk_t udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002318 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -07002319 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002320 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002321 struct extent_position epos = {};
Steve Magnanib490bdd2017-10-12 08:48:40 -05002322 udf_pblk_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002324 down_read(&UDF_I(inode)->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Marcin Slusarz4b111112008-02-08 04:20:36 -08002326 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2327 (EXT_RECORDED_ALLOCATED >> 30))
Pekka Enberg97e961f2008-10-15 12:29:03 +02002328 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 else
2330 ret = 0;
2331
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002332 up_read(&UDF_I(inode)->i_data_sem);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002333 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2336 return udf_fixed_to_variable(ret);
2337 else
2338 return ret;
2339}