blob: 9e66d85021fcb38e5e28249138d91ff47a847d9e [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "udf_i.h"
43#include "udf_sb.h"
44
45MODULE_AUTHOR("Ben Fennema");
46MODULE_DESCRIPTION("Universal Disk Format Filesystem");
47MODULE_LICENSE("GPL");
48
49#define EXTENT_MERGE_SIZE 5
50
Al Virofaa17292011-07-26 03:18:29 -040051static umode_t udf_convert_permissions(struct fileEntry *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static int udf_update_inode(struct inode *, int);
Jan Kara49521de2010-10-20 17:42:44 +020053static int udf_sync_inode(struct inode *inode);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -070054static int udf_alloc_i_data(struct inode *inode, size_t size);
Jan Kara7b0b0932011-12-10 01:43:33 +010055static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
Jan Karaff116fc2007-05-08 00:35:14 -070056static int8_t udf_insert_aext(struct inode *, struct extent_position,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020057 struct kernel_lb_addr, uint32_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static void udf_split_extents(struct inode *, int *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020059 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static void udf_prealloc_extents(struct inode *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020061 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void udf_merge_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020063 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static void udf_update_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020065 struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070066 struct extent_position *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
68
Namjae Jeon99600052013-01-19 11:17:14 +090069static void __udf_clear_extent_cache(struct inode *inode)
70{
71 struct udf_inode_info *iinfo = UDF_I(inode);
72
73 if (iinfo->cached_extent.lstart != -1) {
74 brelse(iinfo->cached_extent.epos.bh);
75 iinfo->cached_extent.lstart = -1;
76 }
77}
78
79/* Invalidate extent cache */
80static void udf_clear_extent_cache(struct inode *inode)
81{
82 struct udf_inode_info *iinfo = UDF_I(inode);
83
84 spin_lock(&iinfo->i_extent_cache_lock);
85 __udf_clear_extent_cache(inode);
86 spin_unlock(&iinfo->i_extent_cache_lock);
87}
88
89/* Return contents of extent cache */
90static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
91 loff_t *lbcount, struct extent_position *pos)
92{
93 struct udf_inode_info *iinfo = UDF_I(inode);
94 int ret = 0;
95
96 spin_lock(&iinfo->i_extent_cache_lock);
97 if ((iinfo->cached_extent.lstart <= bcount) &&
98 (iinfo->cached_extent.lstart != -1)) {
99 /* Cache hit */
100 *lbcount = iinfo->cached_extent.lstart;
101 memcpy(pos, &iinfo->cached_extent.epos,
102 sizeof(struct extent_position));
103 if (pos->bh)
104 get_bh(pos->bh);
105 ret = 1;
106 }
107 spin_unlock(&iinfo->i_extent_cache_lock);
108 return ret;
109}
110
111/* Add extent to extent cache */
112static void udf_update_extent_cache(struct inode *inode, loff_t estart,
113 struct extent_position *pos, int next_epos)
114{
115 struct udf_inode_info *iinfo = UDF_I(inode);
116
117 spin_lock(&iinfo->i_extent_cache_lock);
118 /* Invalidate previously cached extent */
119 __udf_clear_extent_cache(inode);
120 if (pos->bh)
121 get_bh(pos->bh);
122 memcpy(&iinfo->cached_extent.epos, pos,
123 sizeof(struct extent_position));
124 iinfo->cached_extent.lstart = estart;
125 if (next_epos)
126 switch (iinfo->i_alloc_type) {
127 case ICBTAG_FLAG_AD_SHORT:
128 iinfo->cached_extent.epos.offset -=
129 sizeof(struct short_ad);
130 break;
131 case ICBTAG_FLAG_AD_LONG:
132 iinfo->cached_extent.epos.offset -=
133 sizeof(struct long_ad);
134 }
135 spin_unlock(&iinfo->i_extent_cache_lock);
136}
Christoph Hellwigb1e32122008-02-22 12:38:48 +0100137
Al Viro3aac2b62010-06-07 00:43:39 -0400138void udf_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Jan Kara2c948b32009-12-03 13:39:28 +0100140 struct udf_inode_info *iinfo = UDF_I(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400141 int want_delete = 0;
Jan Kara2c948b32009-12-03 13:39:28 +0100142
Al Viro3aac2b62010-06-07 00:43:39 -0400143 if (!inode->i_nlink && !is_bad_inode(inode)) {
144 want_delete = 1;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200145 udf_setsize(inode, 0);
Al Viro3aac2b62010-06-07 00:43:39 -0400146 udf_update_inode(inode, IS_SYNC(inode));
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700147 }
148 truncate_inode_pages_final(&inode->i_data);
Al Viro3aac2b62010-06-07 00:43:39 -0400149 invalidate_inode_buffers(inode);
Jan Karadbd57682012-05-03 14:48:02 +0200150 clear_inode(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100151 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
152 inode->i_size != iinfo->i_lenExtents) {
Joe Perches78ace702011-10-10 01:08:05 -0700153 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",
154 inode->i_ino, inode->i_mode,
155 (unsigned long long)inode->i_size,
156 (unsigned long long)iinfo->i_lenExtents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800158 kfree(iinfo->i_ext.i_data);
159 iinfo->i_ext.i_data = NULL;
Namjae Jeon99600052013-01-19 11:17:14 +0900160 udf_clear_extent_cache(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400161 if (want_delete) {
Al Viro3aac2b62010-06-07 00:43:39 -0400162 udf_free_inode(inode);
Al Viro3aac2b62010-06-07 00:43:39 -0400163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Ian Abbott5eec54f2012-09-05 17:44:31 +0100166static void udf_write_failed(struct address_space *mapping, loff_t to)
167{
168 struct inode *inode = mapping->host;
169 struct udf_inode_info *iinfo = UDF_I(inode);
170 loff_t isize = inode->i_size;
171
172 if (to > isize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700173 truncate_pagecache(inode, isize);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100174 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
175 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +0900176 udf_clear_extent_cache(inode);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100177 udf_truncate_extents(inode);
178 up_write(&iinfo->i_data_sem);
179 }
180 }
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static int udf_writepage(struct page *page, struct writeback_control *wbc)
184{
185 return block_write_full_page(page, udf_get_block, wbc);
186}
187
Namjae Jeon378b8e12012-08-31 12:49:07 -0400188static int udf_writepages(struct address_space *mapping,
189 struct writeback_control *wbc)
190{
191 return mpage_writepages(mapping, wbc, udf_get_block);
192}
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static int udf_readpage(struct file *file, struct page *page)
195{
Namjae Jeonbc112322011-10-03 14:02:59 +0900196 return mpage_readpage(page, udf_get_block);
197}
198
199static int udf_readpages(struct file *file, struct address_space *mapping,
200 struct list_head *pages, unsigned nr_pages)
201{
202 return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Nick Pigginbe021ee2007-10-16 01:25:20 -0700205static int udf_write_begin(struct file *file, struct address_space *mapping,
206 loff_t pos, unsigned len, unsigned flags,
207 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200209 int ret;
210
211 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100212 if (unlikely(ret))
213 udf_write_failed(mapping, pos + len);
214 return ret;
215}
Jan Kara7e49b6f2010-10-22 00:30:26 +0200216
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700217static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Ian Abbott5eec54f2012-09-05 17:44:31 +0100218{
219 struct file *file = iocb->ki_filp;
220 struct address_space *mapping = file->f_mapping;
221 struct inode *inode = mapping->host;
Al Viroa6cbcd42014-03-04 22:38:00 -0500222 size_t count = iov_iter_count(iter);
Ian Abbott5eec54f2012-09-05 17:44:31 +0100223 ssize_t ret;
Christoph Hellwig155130a2010-06-04 11:29:58 +0200224
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700225 ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
Omar Sandoval6f673762015-03-16 04:33:52 -0700226 if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700227 udf_write_failed(mapping, iocb->ki_pos + count);
Christoph Hellwig155130a2010-06-04 11:29:58 +0200228 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231static sector_t udf_bmap(struct address_space *mapping, sector_t block)
232{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700233 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700236const struct address_space_operations udf_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700237 .readpage = udf_readpage,
Namjae Jeonbc112322011-10-03 14:02:59 +0900238 .readpages = udf_readpages,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700239 .writepage = udf_writepage,
Namjae Jeon378b8e12012-08-31 12:49:07 -0400240 .writepages = udf_writepages,
Ian Abbott5eec54f2012-09-05 17:44:31 +0100241 .write_begin = udf_write_begin,
242 .write_end = generic_write_end,
243 .direct_IO = udf_direct_IO,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700244 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245};
246
Jan Karad2eb8c32011-12-10 02:30:48 +0100247/*
248 * Expand file stored in ICB to a normal one-block-file
249 *
250 * This function requires i_data_sem for writing and releases it.
251 * This function requires i_mutex held
252 */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200253int udf_expand_file_adinicb(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct page *page;
256 char *kaddr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800257 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200258 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 struct writeback_control udf_wbc = {
260 .sync_mode = WB_SYNC_NONE,
261 .nr_to_write = 1,
262 };
263
Al Viro59551022016-01-22 15:40:57 -0500264 WARN_ON_ONCE(!inode_is_locked(inode));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800265 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800267 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800269 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200270 /* from now on we have normal address_space methods */
271 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100272 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100276 /*
277 * Release i_data_sem so that we can lock a page - page lock ranks
278 * above i_data_sem. i_mutex still protects us against file changes.
279 */
280 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Jan Kara7e49b6f2010-10-22 00:30:26 +0200282 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
283 if (!page)
284 return -ENOMEM;
Matt Mackallcd7619d2005-05-01 08:59:01 -0700285
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700286 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 kaddr = kmap(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800288 memset(kaddr + iinfo->i_lenAlloc, 0x00,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300289 PAGE_SIZE - iinfo->i_lenAlloc);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800290 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
291 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 flush_dcache_page(page);
293 SetPageUptodate(page);
294 kunmap(page);
295 }
Jan Karad2eb8c32011-12-10 02:30:48 +0100296 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800297 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
298 iinfo->i_lenAlloc);
299 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800301 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800303 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200304 /* from now on we have normal address_space methods */
305 inode->i_data.a_ops = &udf_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100306 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200307 err = inode->i_data.a_ops->writepage(page, &udf_wbc);
308 if (err) {
309 /* Restore everything back so that we don't lose data... */
310 lock_page(page);
311 kaddr = kmap(page);
Jan Karad2eb8c32011-12-10 02:30:48 +0100312 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200313 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
314 inode->i_size);
315 kunmap(page);
316 unlock_page(page);
317 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
318 inode->i_data.a_ops = &udf_adinicb_aops;
Jan Karad2eb8c32011-12-10 02:30:48 +0100319 up_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200320 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300321 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200323
324 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700327struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
328 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700331 struct buffer_head *dbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200332 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700334 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 struct udf_fileident_bh sfibh, dfibh;
Jan Karaaf793292008-02-08 04:20:50 -0800337 loff_t f_pos = udf_ext0_offset(inode);
338 int size = udf_ext0_offset(inode) + inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800340 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
343 alloctype = ICBTAG_FLAG_AD_SHORT;
344 else
345 alloctype = ICBTAG_FLAG_AD_LONG;
346
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700347 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800348 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 mark_inode_dirty(inode);
350 return NULL;
351 }
352
353 /* alloc block, and copy data to it */
354 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800355 iinfo->i_location.partitionReferenceNum,
356 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (!(*block))
358 return NULL;
359 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800360 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800361 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (!newblock)
363 return NULL;
364 dbh = udf_tgetblk(inode->i_sb, newblock);
365 if (!dbh)
366 return NULL;
367 lock_buffer(dbh);
368 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
369 set_buffer_uptodate(dbh);
370 unlock_buffer(dbh);
371 mark_buffer_dirty_inode(dbh, inode);
372
Marcin Slusarz4b111112008-02-08 04:20:36 -0800373 sfibh.soffset = sfibh.eoffset =
Jan Karaaf793292008-02-08 04:20:50 -0800374 f_pos & (inode->i_sb->s_blocksize - 1);
Jan Karaff116fc2007-05-08 00:35:14 -0700375 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 dfibh.soffset = dfibh.eoffset = 0;
377 dfibh.sbh = dfibh.ebh = dbh;
Jan Karaaf793292008-02-08 04:20:50 -0800378 while (f_pos < size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800379 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800380 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
381 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700382 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700383 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return NULL;
385 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800386 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 sfi->descTag.tagLocation = cpu_to_le32(*block);
388 dfibh.soffset = dfibh.eoffset;
389 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
390 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
391 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800392 sfi->fileIdent +
393 le16_to_cpu(sfi->lengthOfImpUse))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800394 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700395 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return NULL;
397 }
398 }
399 mark_buffer_dirty_inode(dbh, inode);
400
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800401 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
402 iinfo->i_lenAlloc);
403 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800405 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800406 iinfo->i_location.partitionReferenceNum;
Jan Kara2c948b32009-12-03 13:39:28 +0100407 iinfo->i_lenExtents = inode->i_size;
Jan Karaff116fc2007-05-08 00:35:14 -0700408 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800409 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700410 epos.offset = udf_file_entry_alloc_offset(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100411 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* UniqueID stuff */
413
Jan Kara3bf25cb2007-05-08 00:35:16 -0700414 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 mark_inode_dirty(inode);
416 return dbh;
417}
418
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700419static int udf_get_block(struct inode *inode, sector_t block,
420 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 int err, new;
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800423 sector_t phys = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800424 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700426 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 phys = udf_block_map(inode, block);
428 if (phys)
429 map_bh(bh_result, inode->i_sb, phys);
430 return 0;
431 }
432
433 err = -EIO;
434 new = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800435 iinfo = UDF_I(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100436
437 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800438 if (block == iinfo->i_next_alloc_block + 1) {
439 iinfo->i_next_alloc_block++;
440 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
Namjae Jeon99600052013-01-19 11:17:14 +0900443 udf_clear_extent_cache(inode);
Jan Kara7b0b0932011-12-10 01:43:33 +0100444 phys = inode_getblk(inode, block, &err, &new);
445 if (!phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (new)
449 set_buffer_new(bh_result);
450 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700451
452abort:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100453 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700457static struct buffer_head *udf_getblk(struct inode *inode, long block,
458 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700460 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct buffer_head dummy;
462
463 dummy.b_state = 0;
464 dummy.b_blocknr = -1000;
465 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700466 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700468 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 lock_buffer(bh);
470 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
471 set_buffer_uptodate(bh);
472 unlock_buffer(bh);
473 mark_buffer_dirty_inode(bh, inode);
474 }
475 return bh;
476 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return NULL;
479}
480
Steven J. Magnani9be0a1a2019-06-30 21:39:35 -0500481/* Extend the file with new blocks totaling 'new_block_bytes',
482 * return the number of extents added
483 */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200484static int udf_do_extend_file(struct inode *inode,
485 struct extent_position *last_pos,
486 struct kernel_long_ad *last_ext,
Steven J. Magnani9be0a1a2019-06-30 21:39:35 -0500487 loff_t new_block_bytes)
Jan Kara31170b62007-05-08 00:35:21 -0700488{
Steven J. Magnani9be0a1a2019-06-30 21:39:35 -0500489 uint32_t add;
Jan Kara31170b62007-05-08 00:35:21 -0700490 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
491 struct super_block *sb = inode->i_sb;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200492 struct kernel_lb_addr prealloc_loc = {};
Jan Kara31170b62007-05-08 00:35:21 -0700493 int prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800494 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200495 int err;
Jan Kara31170b62007-05-08 00:35:21 -0700496
497 /* The previous extent is fake and we should not extend by anything
498 * - there's nothing to do... */
Steven J. Magnani9be0a1a2019-06-30 21:39:35 -0500499 if (!new_block_bytes && fake)
Jan Kara31170b62007-05-08 00:35:21 -0700500 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700501
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800502 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700503 /* Round the last extent up to a multiple of block size */
504 if (last_ext->extLength & (sb->s_blocksize - 1)) {
505 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700506 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
507 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
508 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800509 iinfo->i_lenExtents =
510 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700511 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700512 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700513
Jan Kara31170b62007-05-08 00:35:21 -0700514 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800515 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
516 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700517 /* Save the extent so that we can reattach it to the end */
518 prealloc_loc = last_ext->extLocation;
519 prealloc_len = last_ext->extLength;
520 /* Mark the extent as a hole */
521 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700522 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700523 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800524 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700525 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700526
Jan Kara31170b62007-05-08 00:35:21 -0700527 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800528 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
529 EXT_NOT_RECORDED_NOT_ALLOCATED) {
Steven J. Magnani9be0a1a2019-06-30 21:39:35 -0500530 add = (1 << 30) - sb->s_blocksize -
531 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
532 if (add > new_block_bytes)
533 add = new_block_bytes;
534 new_block_bytes -= add;
535 last_ext->extLength += add;
Jan Kara31170b62007-05-08 00:35:21 -0700536 }
537
538 if (fake) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200539 udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700540 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700541 count++;
Jan Kara6c371572015-12-23 18:05:03 +0100542 } else {
543 struct kernel_lb_addr tmploc;
544 uint32_t tmplen;
545
Pekka Enberg97e961f2008-10-15 12:29:03 +0200546 udf_write_aext(inode, last_pos, &last_ext->extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800547 last_ext->extLength, 1);
Jan Kara6c371572015-12-23 18:05:03 +0100548 /*
549 * We've rewritten the last extent but there may be empty
550 * indirect extent after it - enter it.
551 */
552 udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
553 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700554
Jan Kara31170b62007-05-08 00:35:21 -0700555 /* Managed to do everything necessary? */
Steven J. Magnani9be0a1a2019-06-30 21:39:35 -0500556 if (!new_block_bytes)
Jan Kara31170b62007-05-08 00:35:21 -0700557 goto out;
558
559 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
560 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800561 last_ext->extLocation.partitionReferenceNum = 0;
Steven J. Magnani9be0a1a2019-06-30 21:39:35 -0500562 add = (1 << 30) - sb->s_blocksize;
563 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700564
Jan Kara31170b62007-05-08 00:35:21 -0700565 /* Create enough extents to cover the whole hole */
Steven J. Magnani9be0a1a2019-06-30 21:39:35 -0500566 while (new_block_bytes > add) {
567 new_block_bytes -= add;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200568 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
569 last_ext->extLength, 1);
570 if (err)
571 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700572 count++;
573 }
Steven J. Magnani9be0a1a2019-06-30 21:39:35 -0500574 if (new_block_bytes) {
Jan Kara31170b62007-05-08 00:35:21 -0700575 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Steven J. Magnani9be0a1a2019-06-30 21:39:35 -0500576 new_block_bytes;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200577 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
578 last_ext->extLength, 1);
579 if (err)
580 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700581 count++;
582 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700583
584out:
Jan Kara31170b62007-05-08 00:35:21 -0700585 /* Do we have some preallocated blocks saved? */
586 if (prealloc_len) {
Jan Kara7e49b6f2010-10-22 00:30:26 +0200587 err = udf_add_aext(inode, last_pos, &prealloc_loc,
588 prealloc_len, 1);
589 if (err)
590 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700591 last_ext->extLocation = prealloc_loc;
592 last_ext->extLength = prealloc_len;
593 count++;
594 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700595
Jan Kara31170b62007-05-08 00:35:21 -0700596 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800597 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200598 last_pos->offset -= sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800599 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200600 last_pos->offset -= sizeof(struct long_ad);
Jan Kara31170b62007-05-08 00:35:21 -0700601 else
Jan Kara7e49b6f2010-10-22 00:30:26 +0200602 return -EIO;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700603
Jan Kara31170b62007-05-08 00:35:21 -0700604 return count;
605}
606
Steven J. Magnani9be0a1a2019-06-30 21:39:35 -0500607/* Extend the final block of the file to final_block_len bytes */
608static void udf_do_extend_final_block(struct inode *inode,
609 struct extent_position *last_pos,
610 struct kernel_long_ad *last_ext,
611 uint32_t final_block_len)
612{
613 struct super_block *sb = inode->i_sb;
614 uint32_t added_bytes;
615
616 added_bytes = final_block_len -
617 (last_ext->extLength & (sb->s_blocksize - 1));
618 last_ext->extLength += added_bytes;
619 UDF_I(inode)->i_lenExtents += added_bytes;
620
621 udf_write_aext(inode, last_pos, &last_ext->extLocation,
622 last_ext->extLength, 1);
623}
624
Jan Kara7e49b6f2010-10-22 00:30:26 +0200625static int udf_extend_file(struct inode *inode, loff_t newsize)
626{
627
628 struct extent_position epos;
629 struct kernel_lb_addr eloc;
630 uint32_t elen;
631 int8_t etype;
632 struct super_block *sb = inode->i_sb;
633 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
Steven J. Magnani9be0a1a2019-06-30 21:39:35 -0500634 unsigned long partial_final_block;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200635 int adsize;
636 struct udf_inode_info *iinfo = UDF_I(inode);
637 struct kernel_long_ad extent;
Steven J. Magnani9be0a1a2019-06-30 21:39:35 -0500638 int err = 0;
639 int within_final_block;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200640
641 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
642 adsize = sizeof(struct short_ad);
643 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
644 adsize = sizeof(struct long_ad);
645 else
646 BUG();
647
648 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
Steven J. Magnani9be0a1a2019-06-30 21:39:35 -0500649 within_final_block = (etype != -1);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200650
Jan Kara7e49b6f2010-10-22 00:30:26 +0200651 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
652 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
653 /* File has no extents at all or has empty last
654 * indirect extent! Create a fake extent... */
655 extent.extLocation.logicalBlockNum = 0;
656 extent.extLocation.partitionReferenceNum = 0;
657 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
658 } else {
659 epos.offset -= adsize;
660 etype = udf_next_aext(inode, &epos, &extent.extLocation,
661 &extent.extLength, 0);
662 extent.extLength |= etype << 30;
663 }
Steven J. Magnani9be0a1a2019-06-30 21:39:35 -0500664
665 partial_final_block = newsize & (sb->s_blocksize - 1);
666
667 /* File has extent covering the new size (could happen when extending
668 * inside a block)?
669 */
670 if (within_final_block) {
671 /* Extending file within the last file block */
672 udf_do_extend_final_block(inode, &epos, &extent,
673 partial_final_block);
674 } else {
675 loff_t add = ((loff_t)offset << sb->s_blocksize_bits) |
676 partial_final_block;
677 err = udf_do_extend_file(inode, &epos, &extent, add);
678 }
679
Jan Kara7e49b6f2010-10-22 00:30:26 +0200680 if (err < 0)
681 goto out;
682 err = 0;
683 iinfo->i_lenExtents = newsize;
684out:
685 brelse(epos.bh);
686 return err;
687}
688
Jan Kara7b0b0932011-12-10 01:43:33 +0100689static sector_t inode_getblk(struct inode *inode, sector_t block,
690 int *err, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200692 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700693 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700695 uint32_t elen = 0, tmpelen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200696 struct kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700698 loff_t lbcount = 0, b_off = 0;
699 uint32_t newblocknum, newblock;
700 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800702 struct udf_inode_info *iinfo = UDF_I(inode);
703 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700704 int lastblock = 0;
Namjae Jeonfb719c52012-10-10 00:09:12 +0900705 bool isBeyondEOF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Jan Kara7b0b0932011-12-10 01:43:33 +0100707 *err = 0;
708 *new = 0;
Jan Karaff116fc2007-05-08 00:35:14 -0700709 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800710 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700711 prev_epos.bh = NULL;
712 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700713 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700716 alternate between laarr[0] and laarr[1] for locations of the
717 current extent, and the previous extent */
718 do {
719 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700720 brelse(prev_epos.bh);
721 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700722 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700724 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700725 brelse(cur_epos.bh);
726 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700727 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729
730 lbcount += elen;
731
Jan Karaff116fc2007-05-08 00:35:14 -0700732 prev_epos.block = cur_epos.block;
733 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Jan Karaff116fc2007-05-08 00:35:14 -0700735 prev_epos.offset = cur_epos.offset;
736 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Marcin Slusarz4b111112008-02-08 04:20:36 -0800738 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
739 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 break;
741
742 c = !c;
743
744 laarr[c].extLength = (etype << 30) | elen;
745 laarr[c].extLocation = eloc;
746
747 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
748 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700749 ((elen + inode->i_sb->s_blocksize - 1) >>
750 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700752 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 } while (lbcount + elen <= b_off);
754
755 b_off -= lbcount;
756 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700757 /*
758 * Move prev_epos and cur_epos into indirect extent if we are at
759 * the pointer to it
760 */
761 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
762 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700765 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700767 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
768 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700770 ((elen + inode->i_sb->s_blocksize - 1) &
771 ~(inode->i_sb->s_blocksize - 1));
Jan Kara7e49b6f2010-10-22 00:30:26 +0200772 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700774 brelse(prev_epos.bh);
775 brelse(cur_epos.bh);
776 brelse(next_epos.bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200777 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Jan Kara7b0b0932011-12-10 01:43:33 +0100778 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
Jan Kara31170b62007-05-08 00:35:21 -0700781 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700782 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700783 int ret;
Steven J. Magnani9be0a1a2019-06-30 21:39:35 -0500784 loff_t hole_len;
Fabian Frederick69814982015-02-04 18:26:27 +0100785 isBeyondEOF = true;
Jan Kara31170b62007-05-08 00:35:21 -0700786 if (count) {
787 if (c)
788 laarr[0] = laarr[1];
789 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700790 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700791 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800792 memset(&laarr[0].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200793 sizeof(struct kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700794 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200795 /* Will udf_do_extend_file() create real extent from
Marcin Slusarz4b111112008-02-08 04:20:36 -0800796 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700797 startnum = (offset > 0);
798 }
799 /* Create extents for the hole between EOF and offset */
Steven J. Magnani9be0a1a2019-06-30 21:39:35 -0500800 hole_len = (loff_t)offset << inode->i_blkbits;
801 ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200802 if (ret < 0) {
Jan Kara31170b62007-05-08 00:35:21 -0700803 brelse(prev_epos.bh);
804 brelse(cur_epos.bh);
805 brelse(next_epos.bh);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200806 *err = ret;
Jan Kara7b0b0932011-12-10 01:43:33 +0100807 return 0;
Jan Kara31170b62007-05-08 00:35:21 -0700808 }
809 c = 0;
810 offset = 0;
811 count += ret;
812 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800813 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
814 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700815 /* Is there any real extent? - otherwise we overwrite
816 * the fake one... */
817 if (count)
818 c = !c;
819 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700820 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800821 memset(&laarr[c].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200822 sizeof(struct kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700823 count++;
Jan Kara31170b62007-05-08 00:35:21 -0700824 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700825 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700827 } else {
Fabian Frederick69814982015-02-04 18:26:27 +0100828 isBeyondEOF = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 endnum = startnum = ((count > 2) ? 2 : count);
830
Marcin Slusarz4b111112008-02-08 04:20:36 -0800831 /* if the current extent is in position 0,
832 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700833 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700834 laarr[2] = laarr[0];
835 laarr[0] = laarr[1];
836 laarr[1] = laarr[2];
837 c = 1;
838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Marcin Slusarz4b111112008-02-08 04:20:36 -0800840 /* if the current block is located in an extent,
841 read the next extent */
842 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
843 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700844 laarr[c + 1].extLength = (etype << 30) | elen;
845 laarr[c + 1].extLocation = eloc;
846 count++;
847 startnum++;
848 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800849 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 lastblock = 1;
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700854 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800855 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800857 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800858 if (iinfo->i_next_alloc_block == block)
859 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700861 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800862 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800863 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865
Marcin Slusarz4b111112008-02-08 04:20:36 -0800866 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800867 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800868 goal, err);
869 if (!newblocknum) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700870 brelse(prev_epos.bh);
Namjae Jeon2fb7d992012-10-10 00:08:56 +0900871 brelse(cur_epos.bh);
872 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 *err = -ENOSPC;
Jan Kara7b0b0932011-12-10 01:43:33 +0100874 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
Namjae Jeonfb719c52012-10-10 00:09:12 +0900876 if (isBeyondEOF)
877 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879
Marcin Slusarz4b111112008-02-08 04:20:36 -0800880 /* if the extent the requsted block is located in contains multiple
881 * blocks, split the extent into at most three extents. blocks prior
882 * to requested block, requested block, and blocks after requested
883 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
885
886#ifdef UDF_PREALLOCATE
Jan Kara81056dd2009-07-16 18:02:25 +0200887 /* We preallocate blocks only for regular files. It also makes sense
888 * for directories but there's a problem when to drop the
889 * preallocation. We might use some delayed work for that but I feel
890 * it's overengineering for a filesystem like UDF. */
891 if (S_ISREG(inode->i_mode))
892 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893#endif
894
895 /* merge any continuous blocks in laarr */
896 udf_merge_extents(inode, laarr, &endnum);
897
898 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700899 * of extents is greater than the old number, and deleting extents if
900 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700901 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Jan Kara3bf25cb2007-05-08 00:35:16 -0700903 brelse(prev_epos.bh);
Namjae Jeon2fb7d992012-10-10 00:08:56 +0900904 brelse(cur_epos.bh);
905 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Marcin Slusarz4b111112008-02-08 04:20:36 -0800907 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800908 iinfo->i_location.partitionReferenceNum, 0);
Jan Kara7b0b0932011-12-10 01:43:33 +0100909 if (!newblock) {
910 *err = -EIO;
911 return 0;
912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800914 iinfo->i_next_alloc_block = block;
915 iinfo->i_next_alloc_goal = newblocknum;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700916 inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 if (IS_SYNC(inode))
919 udf_sync_inode(inode);
920 else
921 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700922
Jan Kara7b0b0932011-12-10 01:43:33 +0100923 return newblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700926static void udf_split_extents(struct inode *inode, int *c, int offset,
927 int newblocknum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200928 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700929 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800931 unsigned long blocksize = inode->i_sb->s_blocksize;
932 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800935 (laarr[*c].extLength >> 30) ==
936 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 int curr = *c;
938 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800939 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 int8_t etype = (laarr[curr].extLength >> 30);
941
Marcin Slusarz4b111112008-02-08 04:20:36 -0800942 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700943 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800944 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700945 laarr[curr + 2] = laarr[curr + 1];
946 laarr[curr + 1] = laarr[curr];
947 } else {
948 laarr[curr + 3] = laarr[curr + 1];
949 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700952 if (offset) {
953 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800954 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200955 &laarr[curr].extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800956 0, offset);
957 laarr[curr].extLength =
958 EXT_NOT_RECORDED_NOT_ALLOCATED |
959 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800961 laarr[curr].extLocation.
962 partitionReferenceNum = 0;
963 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800965 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700966 curr++;
967 (*c)++;
968 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 laarr[curr].extLocation.logicalBlockNum = newblocknum;
972 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
973 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800974 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800976 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700977 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700979 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800981 laarr[curr].extLocation.logicalBlockNum +=
982 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700983 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800984 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700985 curr++;
986 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988 }
989}
990
991static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200992 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700993 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 int start, length = 0, currlength = 0, i;
996
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700997 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (!lastblock)
999 return;
1000 else
1001 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001002 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001003 if ((laarr[c + 1].extLength >> 30) ==
1004 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001005 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001006 length = currlength =
1007 (((laarr[c + 1].extLength &
1008 UDF_EXTENT_LENGTH_MASK) +
1009 inode->i_sb->s_blocksize - 1) >>
1010 inode->i_sb->s_blocksize_bits);
1011 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 start = c;
1013 }
1014
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001015 for (i = start + 1; i <= *endnum; i++) {
1016 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (lastblock)
1018 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001019 } else if ((laarr[i].extLength >> 30) ==
1020 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
1021 length += (((laarr[i].extLength &
1022 UDF_EXTENT_LENGTH_MASK) +
1023 inode->i_sb->s_blocksize - 1) >>
1024 inode->i_sb->s_blocksize_bits);
1025 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 break;
1027 }
1028
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001029 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001031 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08001032 inode->i_sb->s_blocksize - 1) >>
1033 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001035 laarr[start].extLocation.partitionReferenceNum,
1036 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1037 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1038 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001039 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001040 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001042 (numalloc <<
1043 inode->i_sb->s_blocksize_bits);
1044 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001045 memmove(&laarr[c + 2], &laarr[c + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001046 sizeof(struct long_ad) * (*endnum - (c + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001047 (*endnum)++;
1048 laarr[c + 1].extLocation.logicalBlockNum = next;
1049 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -08001050 laarr[c].extLocation.
1051 partitionReferenceNum;
1052 laarr[c + 1].extLength =
1053 EXT_NOT_RECORDED_ALLOCATED |
1054 (numalloc <<
1055 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001056 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001059 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001060 int elen = ((laarr[i].extLength &
1061 UDF_EXTENT_LENGTH_MASK) +
1062 inode->i_sb->s_blocksize - 1) >>
1063 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001065 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001067 (numalloc <<
1068 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001070 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001072 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001073 memmove(&laarr[i],
1074 &laarr[i + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001075 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001076 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001077 i--;
1078 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001081 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -08001082 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084 }
1085}
1086
1087static void udf_merge_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001088 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001089 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001092 unsigned long blocksize = inode->i_sb->s_blocksize;
1093 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001095 for (i = 0; i < (*endnum - 1); i++) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001096 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1097 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Marcin Slusarz4b111112008-02-08 04:20:36 -08001099 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1100 (((li->extLength >> 30) ==
1101 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1102 ((lip1->extLocation.logicalBlockNum -
1103 li->extLocation.logicalBlockNum) ==
1104 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1105 blocksize - 1) >> blocksize_bits)))) {
1106
1107 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1108 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1109 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1110 lip1->extLength = (lip1->extLength -
1111 (li->extLength &
1112 UDF_EXTENT_LENGTH_MASK) +
1113 UDF_EXTENT_LENGTH_MASK) &
1114 ~(blocksize - 1);
1115 li->extLength = (li->extLength &
1116 UDF_EXTENT_FLAG_MASK) +
1117 (UDF_EXTENT_LENGTH_MASK + 1) -
1118 blocksize;
1119 lip1->extLocation.logicalBlockNum =
1120 li->extLocation.logicalBlockNum +
1121 ((li->extLength &
1122 UDF_EXTENT_LENGTH_MASK) >>
1123 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001124 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001125 li->extLength = lip1->extLength +
1126 (((li->extLength &
1127 UDF_EXTENT_LENGTH_MASK) +
1128 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001129 if (*endnum > (i + 2))
1130 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001131 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001132 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001133 i--;
1134 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001136 } else if (((li->extLength >> 30) ==
1137 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1138 ((lip1->extLength >> 30) ==
1139 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001140 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001141 ((li->extLength &
1142 UDF_EXTENT_LENGTH_MASK) +
1143 blocksize - 1) >> blocksize_bits);
1144 li->extLocation.logicalBlockNum = 0;
1145 li->extLocation.partitionReferenceNum = 0;
1146
1147 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1148 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1149 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1150 lip1->extLength = (lip1->extLength -
1151 (li->extLength &
1152 UDF_EXTENT_LENGTH_MASK) +
1153 UDF_EXTENT_LENGTH_MASK) &
1154 ~(blocksize - 1);
1155 li->extLength = (li->extLength &
1156 UDF_EXTENT_FLAG_MASK) +
1157 (UDF_EXTENT_LENGTH_MASK + 1) -
1158 blocksize;
1159 } else {
1160 li->extLength = lip1->extLength +
1161 (((li->extLength &
1162 UDF_EXTENT_LENGTH_MASK) +
1163 blocksize - 1) & ~(blocksize - 1));
1164 if (*endnum > (i + 2))
1165 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001166 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001167 (*endnum - (i + 2)));
1168 i--;
1169 (*endnum)--;
1170 }
1171 } else if ((li->extLength >> 30) ==
1172 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1173 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001174 &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001175 ((li->extLength &
1176 UDF_EXTENT_LENGTH_MASK) +
1177 blocksize - 1) >> blocksize_bits);
1178 li->extLocation.logicalBlockNum = 0;
1179 li->extLocation.partitionReferenceNum = 0;
1180 li->extLength = (li->extLength &
1181 UDF_EXTENT_LENGTH_MASK) |
1182 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184 }
1185}
1186
1187static void udf_update_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001188 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001189 int startnum, int endnum,
1190 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 int start = 0, i;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001193 struct kernel_lb_addr tmploc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 uint32_t tmplen;
1195
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001196 if (startnum > endnum) {
1197 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -07001198 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001199 laarr[i].extLength);
1200 } else if (startnum < endnum) {
1201 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001202 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001203 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -07001204 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001205 &laarr[i].extLength, 1);
1206 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208 }
1209
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001210 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001211 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001212 udf_write_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001213 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215}
1216
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001217struct buffer_head *udf_bread(struct inode *inode, int block,
1218 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001220 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 bh = udf_getblk(inode, block, create, err);
1223 if (!bh)
1224 return NULL;
1225
1226 if (buffer_uptodate(bh))
1227 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001228
Mike Christiedfec8a12016-06-05 14:31:44 -05001229 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 wait_on_buffer(bh);
1232 if (buffer_uptodate(bh))
1233 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 brelse(bh);
1236 *err = -EIO;
1237 return NULL;
1238}
1239
Jan Kara7e49b6f2010-10-22 00:30:26 +02001240int udf_setsize(struct inode *inode, loff_t newsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001243 struct udf_inode_info *iinfo;
Fabian Frederick61604a22017-02-27 14:28:32 -08001244 int bsize = i_blocksize(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001247 S_ISLNK(inode->i_mode)))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001248 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001250 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001252 iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001253 if (newsize > inode->i_size) {
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001254 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001255 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1256 if (bsize <
1257 (udf_file_entry_alloc_offset(inode) + newsize)) {
1258 err = udf_expand_file_adinicb(inode);
Jan Karad2eb8c32011-12-10 02:30:48 +01001259 if (err)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001260 return err;
Jan Karad2eb8c32011-12-10 02:30:48 +01001261 down_write(&iinfo->i_data_sem);
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001262 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001263 iinfo->i_lenAlloc = newsize;
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001264 goto set_size;
1265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001267 err = udf_extend_file(inode, newsize);
1268 if (err) {
1269 up_write(&iinfo->i_data_sem);
1270 return err;
1271 }
Ian Abbottbb2b6d12012-07-23 16:39:29 +00001272set_size:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001273 up_write(&iinfo->i_data_sem);
Jan Karafa67ac12017-06-13 16:20:25 +02001274 truncate_setsize(inode, newsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001275 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001276 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1277 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001278 udf_clear_extent_cache(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001279 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1280 0x00, bsize - newsize -
1281 udf_file_entry_alloc_offset(inode));
1282 iinfo->i_lenAlloc = newsize;
1283 truncate_setsize(inode, newsize);
1284 up_write(&iinfo->i_data_sem);
1285 goto update_time;
1286 }
1287 err = block_truncate_page(inode->i_mapping, newsize,
1288 udf_get_block);
1289 if (err)
1290 return err;
Jan Karafa67ac12017-06-13 16:20:25 +02001291 truncate_setsize(inode, newsize);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001292 down_write(&iinfo->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +09001293 udf_clear_extent_cache(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 udf_truncate_extents(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001295 up_write(&iinfo->i_data_sem);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001296 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001297update_time:
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001298 inode->i_mtime = inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001300 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 else
1302 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
Jan Karac03aa9f2014-09-04 14:06:55 +02001306/*
1307 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1308 * arbitrary - just that we hopefully don't limit any real use of rewritten
1309 * inode on write-once media but avoid looping for too long on corrupted media.
1310 */
1311#define UDF_MAX_ICB_NESTING 1024
1312
Jan Kara6174c2e2014-10-09 12:52:16 +02001313static int udf_read_inode(struct inode *inode, bool hidden_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
1315 struct buffer_head *bh = NULL;
1316 struct fileEntry *fe;
Jan Karabb7720a02014-09-04 13:32:50 +02001317 struct extendedFileEntry *efe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001319 struct udf_inode_info *iinfo = UDF_I(inode);
Jan Karabb7720a02014-09-04 13:32:50 +02001320 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001321 struct kernel_lb_addr *iloc = &iinfo->i_location;
Jan Karabb7720a02014-09-04 13:32:50 +02001322 unsigned int link_count;
Jan Karac03aa9f2014-09-04 14:06:55 +02001323 unsigned int indirections = 0;
Jan Kara79144952015-01-07 13:46:16 +01001324 int bs = inode->i_sb->s_blocksize;
Jan Kara6d3d5e82014-09-04 16:15:51 +02001325 int ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Jan Karac03aa9f2014-09-04 14:06:55 +02001327reread:
Jan Kara6d3d5e82014-09-04 16:15:51 +02001328 if (iloc->logicalBlockNum >=
1329 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1330 udf_debug("block=%d, partition=%d out of range\n",
1331 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1332 return -EIO;
1333 }
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 /*
1336 * Set defaults, but the inode is still incomplete!
1337 * Note: get_new_inode() sets the following on a new inode:
1338 * i_sb = sb
1339 * i_no = ino
1340 * i_flags = sb->s_flags
1341 * i_state = 0
1342 * clean_inode(): zero fills and sets
1343 * i_count = 1
1344 * i_nlink = 1
1345 * i_op = NULL;
1346 */
Jan Kara6d3d5e82014-09-04 16:15:51 +02001347 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001348 if (!bh) {
Joe Perches78ace702011-10-10 01:08:05 -07001349 udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001350 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352
1353 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001354 ident != TAG_IDENT_USE) {
Joe Perches78ace702011-10-10 01:08:05 -07001355 udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1356 inode->i_ino, ident);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001357 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359
1360 fe = (struct fileEntry *)bh->b_data;
Jan Karabb7720a02014-09-04 13:32:50 +02001361 efe = (struct extendedFileEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001363 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001364 struct buffer_head *ibh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Jan Kara6d3d5e82014-09-04 16:15:51 +02001366 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001367 if (ident == TAG_IDENT_IE && ibh) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001368 struct kernel_lb_addr loc;
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001369 struct indirectEntry *ie;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001370
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001371 ie = (struct indirectEntry *)ibh->b_data;
1372 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001373
Jan Karac03aa9f2014-09-04 14:06:55 +02001374 if (ie->indirectICB.extLength) {
Jan Karac03aa9f2014-09-04 14:06:55 +02001375 brelse(ibh);
1376 memcpy(&iinfo->i_location, &loc,
1377 sizeof(struct kernel_lb_addr));
1378 if (++indirections > UDF_MAX_ICB_NESTING) {
1379 udf_err(inode->i_sb,
1380 "too many ICBs in ICB hierarchy"
1381 " (max %d supported)\n",
1382 UDF_MAX_ICB_NESTING);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001383 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001384 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001385 brelse(bh);
Jan Karac03aa9f2014-09-04 14:06:55 +02001386 goto reread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001388 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001389 brelse(ibh);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001390 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Joe Perches78ace702011-10-10 01:08:05 -07001391 udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1392 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001393 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001395 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001396 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001397 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001398 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001400 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001401 ICBTAG_FLAG_AD_MASK;
Jan Kara46171c32018-12-12 14:29:20 +01001402 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
1403 iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
1404 iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1405 ret = -EIO;
1406 goto out;
1407 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001408 iinfo->i_unique = 0;
1409 iinfo->i_lenEAttr = 0;
1410 iinfo->i_lenExtents = 0;
1411 iinfo->i_lenAlloc = 0;
1412 iinfo->i_next_alloc_block = 0;
1413 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001414 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001415 iinfo->i_efe = 1;
1416 iinfo->i_use = 0;
Jan Kara79144952015-01-07 13:46:16 +01001417 ret = udf_alloc_i_data(inode, bs -
Jan Kara6d3d5e82014-09-04 16:15:51 +02001418 sizeof(struct extendedFileEntry));
1419 if (ret)
1420 goto out;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001421 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001422 bh->b_data + sizeof(struct extendedFileEntry),
Jan Kara79144952015-01-07 13:46:16 +01001423 bs - sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001424 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001425 iinfo->i_efe = 0;
1426 iinfo->i_use = 0;
Jan Kara79144952015-01-07 13:46:16 +01001427 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001428 if (ret)
1429 goto out;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001430 memcpy(iinfo->i_ext.i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001431 bh->b_data + sizeof(struct fileEntry),
Jan Kara79144952015-01-07 13:46:16 +01001432 bs - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001433 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001434 iinfo->i_efe = 0;
1435 iinfo->i_use = 1;
1436 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001437 ((struct unallocSpaceEntry *)bh->b_data)->
1438 lengthAllocDescs);
Jan Kara79144952015-01-07 13:46:16 +01001439 ret = udf_alloc_i_data(inode, bs -
Jan Kara6d3d5e82014-09-04 16:15:51 +02001440 sizeof(struct unallocSpaceEntry));
1441 if (ret)
1442 goto out;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001443 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001444 bh->b_data + sizeof(struct unallocSpaceEntry),
Jan Kara79144952015-01-07 13:46:16 +01001445 bs - sizeof(struct unallocSpaceEntry));
Jan Kara6d3d5e82014-09-04 16:15:51 +02001446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
1448
Jan Kara6d3d5e82014-09-04 16:15:51 +02001449 ret = -EIO;
Jan Karac03cad22010-10-20 22:17:28 +02001450 read_lock(&sbi->s_cred_lock);
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001451 i_uid_write(inode, le32_to_cpu(fe->uid));
1452 if (!uid_valid(inode->i_uid) ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001453 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1454 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001455 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001457 i_gid_write(inode, le32_to_cpu(fe->gid));
1458 if (!gid_valid(inode->i_gid) ||
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001459 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1460 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001461 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001463 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001464 sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001465 inode->i_mode = sbi->s_fmode;
1466 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001467 sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001468 inode->i_mode = sbi->s_dmode;
1469 else
1470 inode->i_mode = udf_convert_permissions(fe);
1471 inode->i_mode &= ~sbi->s_umask;
Jan Karac03cad22010-10-20 22:17:28 +02001472 read_unlock(&sbi->s_cred_lock);
1473
Miklos Szeredibfe86842011-10-28 14:13:29 +02001474 link_count = le16_to_cpu(fe->fileLinkCount);
Jan Kara4071b912014-09-04 16:19:47 +02001475 if (!link_count) {
Jan Kara6174c2e2014-10-09 12:52:16 +02001476 if (!hidden_inode) {
1477 ret = -ESTALE;
1478 goto out;
1479 }
1480 link_count = 1;
Jan Kara4071b912014-09-04 16:19:47 +02001481 }
Miklos Szeredibfe86842011-10-28 14:13:29 +02001482 set_nlink(inode, link_count);
Jan Karac03cad22010-10-20 22:17:28 +02001483
1484 inode->i_size = le64_to_cpu(fe->informationLength);
1485 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001487 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001489 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Marcin Slusarz56774802008-02-10 11:25:31 +01001491 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001492 inode->i_atime = sbi->s_record_time;
1493
Marcin Slusarz56774802008-02-10 11:25:31 +01001494 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1495 fe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001496 inode->i_mtime = sbi->s_record_time;
1497
Marcin Slusarz56774802008-02-10 11:25:31 +01001498 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001499 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001501 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1502 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1503 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001504 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001505 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001506 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001507 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Marcin Slusarz56774802008-02-10 11:25:31 +01001509 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001510 inode->i_atime = sbi->s_record_time;
1511
Marcin Slusarz56774802008-02-10 11:25:31 +01001512 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1513 efe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001514 inode->i_mtime = sbi->s_record_time;
1515
Marcin Slusarz56774802008-02-10 11:25:31 +01001516 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001517 iinfo->i_crtime = sbi->s_record_time;
1518
Marcin Slusarz56774802008-02-10 11:25:31 +01001519 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001520 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001522 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1523 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1524 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001525 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
Jan Kara470cca52014-09-04 16:26:19 +02001527 inode->i_generation = iinfo->i_unique;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Jan Kara23b133bd2015-01-07 13:49:08 +01001529 /*
1530 * Sanity check length of allocation descriptors and extended attrs to
1531 * avoid integer overflows
1532 */
1533 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1534 goto out;
1535 /* Now do exact checks */
1536 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1537 goto out;
Jan Karae1593322014-12-19 12:03:53 +01001538 /* Sanity checks for files in ICB so that we don't get confused later */
1539 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1540 /*
1541 * For file in ICB data is stored in allocation descriptor
1542 * so sizes should match
1543 */
1544 if (iinfo->i_lenAlloc != inode->i_size)
1545 goto out;
1546 /* File in ICB has to fit in there... */
Jan Kara79144952015-01-07 13:46:16 +01001547 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
Jan Karae1593322014-12-19 12:03:53 +01001548 goto out;
1549 }
1550
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001551 switch (fe->icbTag.fileType) {
1552 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001553 inode->i_op = &udf_dir_inode_operations;
1554 inode->i_fop = &udf_dir_operations;
1555 inode->i_mode |= S_IFDIR;
1556 inc_nlink(inode);
1557 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001558 case ICBTAG_FILE_TYPE_REALTIME:
1559 case ICBTAG_FILE_TYPE_REGULAR:
1560 case ICBTAG_FILE_TYPE_UNDEF:
Jan Kara742e1792008-04-08 01:17:52 +02001561 case ICBTAG_FILE_TYPE_VAT20:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001562 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001563 inode->i_data.a_ops = &udf_adinicb_aops;
1564 else
1565 inode->i_data.a_ops = &udf_aops;
1566 inode->i_op = &udf_file_inode_operations;
1567 inode->i_fop = &udf_file_operations;
1568 inode->i_mode |= S_IFREG;
1569 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001570 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001571 inode->i_mode |= S_IFBLK;
1572 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001573 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001574 inode->i_mode |= S_IFCHR;
1575 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001576 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001577 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1578 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001579 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001580 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1581 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001582 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001583 inode->i_data.a_ops = &udf_symlink_aops;
Al Viroc73119c2015-11-13 20:33:18 -05001584 inode->i_op = &page_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05001585 inode_nohighmem(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001586 inode->i_mode = S_IFLNK | S_IRWXUGO;
1587 break;
Jan Karabfb257a2008-04-08 20:37:21 +02001588 case ICBTAG_FILE_TYPE_MAIN:
1589 udf_debug("METADATA FILE-----\n");
1590 break;
1591 case ICBTAG_FILE_TYPE_MIRROR:
1592 udf_debug("METADATA MIRROR FILE-----\n");
1593 break;
1594 case ICBTAG_FILE_TYPE_BITMAP:
1595 udf_debug("METADATA BITMAP FILE-----\n");
1596 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001597 default:
Joe Perches78ace702011-10-10 01:08:05 -07001598 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1599 inode->i_ino, fe->icbTag.fileType);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001600 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001602 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001603 struct deviceSpec *dsea =
1604 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001605 if (dsea) {
1606 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001607 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1608 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001610 } else
Jan Kara6d3d5e82014-09-04 16:15:51 +02001611 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001613 ret = 0;
1614out:
Jan Karabb7720a02014-09-04 13:32:50 +02001615 brelse(bh);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001616 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001619static int udf_alloc_i_data(struct inode *inode, size_t size)
1620{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001621 struct udf_inode_info *iinfo = UDF_I(inode);
1622 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001623
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001624 if (!iinfo->i_ext.i_data) {
Joe Perches78ace702011-10-10 01:08:05 -07001625 udf_err(inode->i_sb, "(ino %ld) no free memory\n",
1626 inode->i_ino);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001627 return -ENOMEM;
1628 }
1629
1630 return 0;
1631}
1632
Al Virofaa17292011-07-26 03:18:29 -04001633static umode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Al Virofaa17292011-07-26 03:18:29 -04001635 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 uint32_t permissions;
1637 uint32_t flags;
1638
1639 permissions = le32_to_cpu(fe->permissions);
1640 flags = le16_to_cpu(fe->icbTag.flags);
1641
Marcin Slusarz4b111112008-02-08 04:20:36 -08001642 mode = ((permissions) & S_IRWXO) |
1643 ((permissions >> 2) & S_IRWXG) |
1644 ((permissions >> 4) & S_IRWXU) |
1645 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1646 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1647 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 return mode;
1650}
1651
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001652int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Jan Kara49521de2010-10-20 17:42:44 +02001654 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
Jan Kara49521de2010-10-20 17:42:44 +02001657static int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 return udf_update_inode(inode, 1);
1660}
1661
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001662static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
1664 struct buffer_head *bh = NULL;
1665 struct fileEntry *fe;
1666 struct extendedFileEntry *efe;
Steve Nickelb2527bf2012-02-16 12:53:53 -05001667 uint64_t lb_recorded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 uint32_t udfperms;
1669 uint16_t icbflags;
1670 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001672 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001673 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001674 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Jan Kara5833ded2010-01-08 16:52:59 +01001676 bh = udf_tgetblk(inode->i_sb,
1677 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001678 if (!bh) {
Jan Karaaae917c2010-01-08 16:46:29 +01001679 udf_debug("getblk failure\n");
Changwoo Min0fd2ba32015-03-22 19:17:49 -04001680 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682
Jan Karaaae917c2010-01-08 16:46:29 +01001683 lock_buffer(bh);
1684 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 fe = (struct fileEntry *)bh->b_data;
1686 efe = (struct extendedFileEntry *)bh->b_data;
1687
Jan Karaaae917c2010-01-08 16:46:29 +01001688 if (iinfo->i_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001690 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001692 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001693 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001694 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001695 sizeof(struct unallocSpaceEntry));
Jan Karaaae917c2010-01-08 16:46:29 +01001696 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001697 crclen = sizeof(struct unallocSpaceEntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001699 goto finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701
Phillip Susi4d6660e2006-03-07 21:55:24 -08001702 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1703 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001704 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001705 fe->uid = cpu_to_le32(i_uid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Phillip Susi4d6660e2006-03-07 21:55:24 -08001707 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1708 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001709 else
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08001710 fe->gid = cpu_to_le32(i_gid_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Marcin Slusarz4b111112008-02-08 04:20:36 -08001712 udfperms = ((inode->i_mode & S_IRWXO)) |
1713 ((inode->i_mode & S_IRWXG) << 2) |
1714 ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Marcin Slusarz4b111112008-02-08 04:20:36 -08001716 udfperms |= (le32_to_cpu(fe->permissions) &
1717 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1718 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1719 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 fe->permissions = cpu_to_le32(udfperms);
1721
Jan Kara8a70ee32014-09-04 11:47:51 +02001722 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1724 else
1725 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1726
1727 fe->informationLength = cpu_to_le64(inode->i_size);
1728
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001729 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001730 struct regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001731 struct deviceSpec *dsea =
1732 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001733 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001735 udf_add_extendedattr(inode,
1736 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001737 sizeof(struct regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 dsea->attrType = cpu_to_le32(12);
1739 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001740 dsea->attrLength = cpu_to_le32(
1741 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001742 sizeof(struct regid));
1743 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001745 eid = (struct regid *)dsea->impUse;
1746 memset(eid, 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 strcpy(eid->ident, UDF_ID_DEVELOPER);
1748 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1749 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1750 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1751 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1752 }
1753
Steve Nickelb2527bf2012-02-16 12:53:53 -05001754 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1755 lb_recorded = 0; /* No extents => no blocks! */
1756 else
1757 lb_recorded =
1758 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1759 (blocksize_bits - 9);
1760
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001761 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001762 memcpy(bh->b_data + sizeof(struct fileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001763 iinfo->i_ext.i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001764 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Steve Nickelb2527bf2012-02-16 12:53:53 -05001765 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Marcin Slusarz56774802008-02-10 11:25:31 +01001767 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1768 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1769 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001770 memset(&(fe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1772 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1773 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001774 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1775 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1776 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001777 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1779 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001780 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001781 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001782 iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001783 inode->i_sb->s_blocksize -
1784 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 efe->objectSize = cpu_to_le64(inode->i_size);
Steve Nickelb2527bf2012-02-16 12:53:53 -05001786 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001788 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1789 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1790 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1791 iinfo->i_crtime = inode->i_atime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001792
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001793 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1794 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1795 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1796 iinfo->i_crtime = inode->i_mtime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001797
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001798 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1799 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1800 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1801 iinfo->i_crtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Marcin Slusarz56774802008-02-10 11:25:31 +01001803 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1804 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1805 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1806 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001808 memset(&(efe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1810 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1811 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001812 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1813 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1814 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Steve Nickeld5e2cf02012-02-14 00:28:42 -05001815 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1817 crclen = sizeof(struct extendedFileEntry);
1818 }
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001819
1820finish:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001821 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 fe->icbTag.strategyType = cpu_to_le16(4096);
1823 fe->icbTag.strategyParameter = cpu_to_le16(1);
1824 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001825 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 fe->icbTag.strategyType = cpu_to_le16(4);
1827 fe->icbTag.numEntries = cpu_to_le16(1);
1828 }
1829
Steven J. Magnani70f19f52015-07-07 13:06:05 -05001830 if (iinfo->i_use)
1831 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1832 else if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1834 else if (S_ISREG(inode->i_mode))
1835 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1836 else if (S_ISLNK(inode->i_mode))
1837 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1838 else if (S_ISBLK(inode->i_mode))
1839 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1840 else if (S_ISCHR(inode->i_mode))
1841 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1842 else if (S_ISFIFO(inode->i_mode))
1843 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1844 else if (S_ISSOCK(inode->i_mode))
1845 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1846
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001847 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001848 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1849 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1850 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1851 (le16_to_cpu(fe->icbTag.flags) &
1852 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1853 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001856 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 fe->descTag.descVersion = cpu_to_le16(3);
1858 else
1859 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001860 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001861 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001862 iinfo->i_location.logicalBlockNum);
Jan Karaaae917c2010-01-08 16:46:29 +01001863 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001865 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001866 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001867 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Jan Kara5833ded2010-01-08 16:52:59 +01001869 set_buffer_uptodate(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001870 unlock_buffer(bh);
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /* write the data blocks */
1873 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001874 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 sync_dirty_buffer(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001876 if (buffer_write_io_error(bh)) {
Joe Perches78ace702011-10-10 01:08:05 -07001877 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1878 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 err = -EIO;
1880 }
1881 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001882 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return err;
1885}
1886
Jan Kara6174c2e2014-10-09 12:52:16 +02001887struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1888 bool hidden_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
1890 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1891 struct inode *inode = iget_locked(sb, block);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001892 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 if (!inode)
Jan Kara6d3d5e82014-09-04 16:15:51 +02001895 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Jan Kara6d3d5e82014-09-04 16:15:51 +02001897 if (!(inode->i_state & I_NEW))
1898 return inode;
1899
1900 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
Jan Kara6174c2e2014-10-09 12:52:16 +02001901 err = udf_read_inode(inode, hidden_inode);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001902 if (err < 0) {
1903 iget_failed(inode);
1904 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001906 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
Jan Karafcea62b2015-12-23 14:21:13 +01001911int udf_setup_indirect_aext(struct inode *inode, int block,
1912 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Jan Karafcea62b2015-12-23 14:21:13 +01001914 struct super_block *sb = inode->i_sb;
1915 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 struct allocExtDesc *aed;
Jan Karafcea62b2015-12-23 14:21:13 +01001917 struct extent_position nepos;
1918 struct kernel_lb_addr neloc;
1919 int ver, adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Jan Karafcea62b2015-12-23 14:21:13 +01001921 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1922 adsize = sizeof(struct short_ad);
1923 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1924 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 else
Arnd Bergmann4f1b1512016-01-01 15:21:54 +01001926 return -EIO;
Jan Karafcea62b2015-12-23 14:21:13 +01001927
1928 neloc.logicalBlockNum = block;
1929 neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1930
1931 bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1932 if (!bh)
1933 return -EIO;
1934 lock_buffer(bh);
1935 memset(bh->b_data, 0x00, sb->s_blocksize);
1936 set_buffer_uptodate(bh);
1937 unlock_buffer(bh);
1938 mark_buffer_dirty_inode(bh, inode);
1939
1940 aed = (struct allocExtDesc *)(bh->b_data);
1941 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1942 aed->previousAllocExtLocation =
1943 cpu_to_le32(epos->block.logicalBlockNum);
1944 }
1945 aed->lengthAllocDescs = cpu_to_le32(0);
1946 if (UDF_SB(sb)->s_udfrev >= 0x0200)
1947 ver = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 else
Jan Karafcea62b2015-12-23 14:21:13 +01001949 ver = 2;
1950 udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1951 sizeof(struct tag));
1952
1953 nepos.block = neloc;
1954 nepos.offset = sizeof(struct allocExtDesc);
1955 nepos.bh = bh;
1956
1957 /*
1958 * Do we have to copy current last extent to make space for indirect
1959 * one?
1960 */
1961 if (epos->offset + adsize > sb->s_blocksize) {
1962 struct kernel_lb_addr cp_loc;
1963 uint32_t cp_len;
1964 int cp_type;
1965
1966 epos->offset -= adsize;
1967 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1968 cp_len |= ((uint32_t)cp_type) << 30;
1969
1970 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1971 udf_write_aext(inode, epos, &nepos.block,
1972 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1973 } else {
1974 __udf_add_aext(inode, epos, &nepos.block,
1975 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1976 }
1977
1978 brelse(epos->bh);
1979 *epos = nepos;
1980
1981 return 0;
1982}
1983
1984/*
1985 * Append extent at the given position - should be the first free one in inode
1986 * / indirect extent. This function assumes there is enough space in the inode
1987 * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1988 */
1989int __udf_add_aext(struct inode *inode, struct extent_position *epos,
1990 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1991{
1992 struct udf_inode_info *iinfo = UDF_I(inode);
1993 struct allocExtDesc *aed;
1994 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001996 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001997 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001998 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001999 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 else
Jan Kara7e49b6f2010-10-22 00:30:26 +02002001 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Jan Karafcea62b2015-12-23 14:21:13 +01002003 if (!epos->bh) {
2004 WARN_ON(iinfo->i_lenAlloc !=
2005 epos->offset - udf_file_entry_alloc_offset(inode));
2006 } else {
2007 aed = (struct allocExtDesc *)epos->bh->b_data;
2008 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
2009 epos->offset - sizeof(struct allocExtDesc));
2010 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
2012
Jan Kara7e49b6f2010-10-22 00:30:26 +02002013 udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002015 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002016 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002018 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002019 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002020 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002021 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2022 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2023 udf_update_tag(epos->bh->b_data,
2024 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002026 udf_update_tag(epos->bh->b_data,
2027 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002028 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
2030
Jan Kara7e49b6f2010-10-22 00:30:26 +02002031 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
Jan Karafcea62b2015-12-23 14:21:13 +01002034/*
2035 * Append extent at given position - should be the first free one in inode
2036 * / indirect extent. Takes care of allocating and linking indirect blocks.
2037 */
2038int udf_add_aext(struct inode *inode, struct extent_position *epos,
2039 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2040{
2041 int adsize;
2042 struct super_block *sb = inode->i_sb;
2043
2044 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2045 adsize = sizeof(struct short_ad);
2046 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2047 adsize = sizeof(struct long_ad);
2048 else
2049 return -EIO;
2050
2051 if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2052 int err;
2053 int new_block;
2054
2055 new_block = udf_new_block(sb, NULL,
2056 epos->block.partitionReferenceNum,
2057 epos->block.logicalBlockNum, &err);
2058 if (!new_block)
2059 return -ENOSPC;
2060
2061 err = udf_setup_indirect_aext(inode, new_block, epos);
2062 if (err)
2063 return err;
2064 }
2065
2066 return __udf_add_aext(inode, epos, eloc, elen, inc);
2067}
2068
Jan Kara7e49b6f2010-10-22 00:30:26 +02002069void udf_write_aext(struct inode *inode, struct extent_position *epos,
2070 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
2072 int adsize;
2073 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002074 struct short_ad *sad;
2075 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002076 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Jan Karaff116fc2007-05-08 00:35:14 -07002078 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002079 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002080 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002081 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 else
Jan Karaff116fc2007-05-08 00:35:14 -07002083 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002085 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002086 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002087 sad = (struct short_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002088 sad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002089 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002090 adsize = sizeof(struct short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002091 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002092 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002093 lad = (struct long_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002094 lad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002095 lad->extLocation = cpu_to_lelb(*eloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002096 memset(lad->impUse, 0x00, sizeof(lad->impUse));
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002097 adsize = sizeof(struct long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002098 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002099 default:
Jan Kara7e49b6f2010-10-22 00:30:26 +02002100 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
2102
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002103 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002104 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002105 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002106 struct allocExtDesc *aed =
2107 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07002108 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002109 le32_to_cpu(aed->lengthAllocDescs) +
2110 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
Jan Karaff116fc2007-05-08 00:35:14 -07002112 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002113 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07002118 epos->offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
Vegard Nossumb0918d92015-12-11 15:54:16 +01002121/*
2122 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2123 * someone does some weird stuff.
2124 */
2125#define UDF_MAX_INDIR_EXTS 16
2126
Marcin Slusarz4b111112008-02-08 04:20:36 -08002127int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002128 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 int8_t etype;
Vegard Nossumb0918d92015-12-11 15:54:16 +01002131 unsigned int indirections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Jan Karaff116fc2007-05-08 00:35:14 -07002133 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002134 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002135 int block;
Vegard Nossumb0918d92015-12-11 15:54:16 +01002136
2137 if (++indirections > UDF_MAX_INDIR_EXTS) {
2138 udf_err(inode->i_sb,
2139 "too many indirect extents in inode %lu\n",
2140 inode->i_ino);
2141 return -1;
2142 }
2143
Jan Karaff116fc2007-05-08 00:35:14 -07002144 epos->block = *eloc;
2145 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002146 brelse(epos->bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002147 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002148 epos->bh = udf_tread(inode->i_sb, block);
2149 if (!epos->bh) {
2150 udf_debug("reading block %d failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return -1;
2152 }
2153 }
2154
2155 return etype;
2156}
2157
Marcin Slusarz4b111112008-02-08 04:20:36 -08002158int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002159 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 int alen;
2162 int8_t etype;
2163 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002164 struct short_ad *sad;
2165 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002166 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002167
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002168 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002169 if (!epos->offset)
2170 epos->offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002171 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08002172 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002173 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08002174 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002175 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002176 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002177 if (!epos->offset)
2178 epos->offset = sizeof(struct allocExtDesc);
2179 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002180 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08002181 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2182 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 }
2184
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002185 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002186 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002187 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2188 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002190 etype = le32_to_cpu(sad->extLength) >> 30;
2191 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002192 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002193 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002194 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2195 break;
2196 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08002197 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2198 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002199 return -1;
2200 etype = le32_to_cpu(lad->extLength) >> 30;
2201 *eloc = lelb_to_cpu(lad->extLocation);
2202 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2203 break;
2204 default:
Joe Perchesa983f362011-10-10 01:08:07 -07002205 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002206 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
2208
2209 return etype;
2210}
2211
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002212static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002213 struct kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002215 struct kernel_lb_addr oeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 uint32_t oelen;
2217 int8_t etype;
2218
Jan Karaff116fc2007-05-08 00:35:14 -07002219 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07002220 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002222 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002223 udf_write_aext(inode, &epos, &neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 neloc = oeloc;
2225 nelen = (etype << 30) | oelen;
2226 }
Pekka Enberg97e961f2008-10-15 12:29:03 +02002227 udf_add_aext(inode, &epos, &neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002228 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return (nelen >> 30);
2231}
2232
Marcin Slusarz4b111112008-02-08 04:20:36 -08002233int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002234 struct kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
Jan Karaff116fc2007-05-08 00:35:14 -07002236 struct extent_position oepos;
2237 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 int8_t etype;
2239 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002240 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002242 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002243 get_bh(epos.bh);
2244 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
2246
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002247 iinfo = UDF_I(inode);
2248 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002249 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002250 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002251 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 else
2253 adsize = 0;
2254
Jan Karaff116fc2007-05-08 00:35:14 -07002255 oepos = epos;
2256 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return -1;
2258
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002259 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002260 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002261 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002262 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07002263 brelse(oepos.bh);
2264 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07002265 oepos.bh = epos.bh;
2266 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
2268 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002269 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 elen = 0;
2271
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002272 if (epos.bh != oepos.bh) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002273 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2274 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2275 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002276 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002277 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002279 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002280 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002281 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002282 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002283 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002284 udf_update_tag(oepos.bh->b_data,
2285 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002287 udf_update_tag(oepos.bh->b_data,
2288 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002289 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002291 } else {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002292 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002293 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002294 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002296 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002297 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002298 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002299 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002300 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002301 udf_update_tag(oepos.bh->b_data,
2302 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002304 udf_update_tag(oepos.bh->b_data,
2305 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002306 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
2308 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002309
Jan Kara3bf25cb2007-05-08 00:35:16 -07002310 brelse(epos.bh);
2311 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return (elen >> 30);
2314}
2315
Marcin Slusarz4b111112008-02-08 04:20:36 -08002316int8_t inode_bmap(struct inode *inode, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002317 struct extent_position *pos, struct kernel_lb_addr *eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002318 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002320 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002321 loff_t lbcount = 0, bcount =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002322 (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002324 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002326 iinfo = UDF_I(inode);
Namjae Jeon99600052013-01-19 11:17:14 +09002327 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2328 pos->offset = 0;
2329 pos->block = iinfo->i_location;
2330 pos->bh = NULL;
2331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 *elen = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002333 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002334 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2335 if (etype == -1) {
2336 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002337 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 return -1;
2339 }
2340 lbcount += *elen;
2341 } while (lbcount <= bcount);
Namjae Jeon99600052013-01-19 11:17:14 +09002342 /* update extent cache */
2343 udf_update_extent_cache(inode, lbcount - *elen, pos, 1);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002344 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346 return etype;
2347}
2348
Jan Kara60448b12007-05-08 00:35:13 -07002349long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002351 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -07002352 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002353 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002354 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 int ret;
2356
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002357 down_read(&UDF_I(inode)->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Marcin Slusarz4b111112008-02-08 04:20:36 -08002359 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2360 (EXT_RECORDED_ALLOCATED >> 30))
Pekka Enberg97e961f2008-10-15 12:29:03 +02002361 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 else
2363 ret = 0;
2364
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002365 up_read(&UDF_I(inode)->i_data_sem);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002366 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2369 return udf_fixed_to_variable(ret);
2370 else
2371 return ret;
2372}