blob: f746b9f1c03ca7d6c1dfc1c338b8d5d96a31e955 [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>
34#include <linux/smp_lock.h>
35#include <linux/module.h>
36#include <linux/pagemap.h>
37#include <linux/buffer_head.h>
38#include <linux/writeback.h>
39#include <linux/slab.h>
40
41#include "udf_i.h"
42#include "udf_sb.h"
43
44MODULE_AUTHOR("Ben Fennema");
45MODULE_DESCRIPTION("Universal Disk Format Filesystem");
46MODULE_LICENSE("GPL");
47
48#define EXTENT_MERGE_SIZE 5
49
50static mode_t udf_convert_permissions(struct fileEntry *);
51static int udf_update_inode(struct inode *, int);
52static void udf_fill_inode(struct inode *, struct buffer_head *);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -070053static int udf_alloc_i_data(struct inode *inode, size_t size);
Jan Kara60448b12007-05-08 00:35:13 -070054static struct buffer_head *inode_getblk(struct inode *, sector_t, int *,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070055 long *, int *);
Jan Karaff116fc2007-05-08 00:35:14 -070056static int8_t udf_insert_aext(struct inode *, struct extent_position,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070057 kernel_lb_addr, uint32_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static void udf_split_extents(struct inode *, int *, int, int,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070059 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static void udf_prealloc_extents(struct inode *, int, int,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070061 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void udf_merge_extents(struct inode *,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070063 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static void udf_update_extents(struct inode *,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070065 kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
66 struct extent_position *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
68
69/*
70 * udf_delete_inode
71 *
72 * PURPOSE
73 * Clean-up before the specified inode is destroyed.
74 *
75 * DESCRIPTION
76 * This routine is called when the kernel destroys an inode structure
77 * ie. when iput() finds i_count == 0.
78 *
79 * HISTORY
80 * July 1, 1997 - Andrew E. Mileski
81 * Written, tested, and released.
82 *
83 * Called at the last iput() if i_nlink is zero.
84 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070085void udf_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Mark Fashehfef26652005-09-09 13:01:31 -070087 truncate_inode_pages(&inode->i_data, 0);
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (is_bad_inode(inode))
90 goto no_delete;
91
92 inode->i_size = 0;
93 udf_truncate(inode);
94 lock_kernel();
95
96 udf_update_inode(inode, IS_SYNC(inode));
97 udf_free_inode(inode);
98
99 unlock_kernel();
100 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700101
102no_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 clear_inode(inode);
104}
105
Jan Kara74584ae2007-06-16 10:16:14 -0700106/*
107 * If we are going to release inode from memory, we discard preallocation and
108 * truncate last inode extent to proper length. We could use drop_inode() but
109 * it's called under inode_lock and thus we cannot mark inode dirty there. We
110 * use clear_inode() but we have to make sure to write inode as it's not written
111 * automatically.
112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113void udf_clear_inode(struct inode *inode)
114{
115 if (!(inode->i_sb->s_flags & MS_RDONLY)) {
116 lock_kernel();
Jan Kara74584ae2007-06-16 10:16:14 -0700117 /* Discard preallocation for directories, symlinks, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 udf_discard_prealloc(inode);
Jan Kara74584ae2007-06-16 10:16:14 -0700119 udf_truncate_tail_extent(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unlock_kernel();
Jan Kara74584ae2007-06-16 10:16:14 -0700121 write_inode_now(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800123 kfree(UDF_I(inode)->i_ext.i_data);
124 UDF_I(inode)->i_ext.i_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127static int udf_writepage(struct page *page, struct writeback_control *wbc)
128{
129 return block_write_full_page(page, udf_get_block, wbc);
130}
131
132static int udf_readpage(struct file *file, struct page *page)
133{
134 return block_read_full_page(page, udf_get_block);
135}
136
Nick Pigginbe021ee2007-10-16 01:25:20 -0700137static int udf_write_begin(struct file *file, struct address_space *mapping,
138 loff_t pos, unsigned len, unsigned flags,
139 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Nick Pigginbe021ee2007-10-16 01:25:20 -0700141 *pagep = NULL;
142 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
143 udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146static sector_t udf_bmap(struct address_space *mapping, sector_t block)
147{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700148 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700151const struct address_space_operations udf_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700152 .readpage = udf_readpage,
153 .writepage = udf_writepage,
154 .sync_page = block_sync_page,
Nick Pigginbe021ee2007-10-16 01:25:20 -0700155 .write_begin = udf_write_begin,
156 .write_end = generic_write_end,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700157 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700160void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct page *page;
163 char *kaddr;
164 struct writeback_control udf_wbc = {
165 .sync_mode = WB_SYNC_NONE,
166 .nr_to_write = 1,
167 };
168
169 /* from now on we have normal address_space methods */
170 inode->i_data.a_ops = &udf_aops;
171
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800172 if (!UDF_I(inode)->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800174 UDF_I(inode)->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 else
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800176 UDF_I(inode)->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 mark_inode_dirty(inode);
178 return;
179 }
180
181 page = grab_cache_page(inode->i_mapping, 0);
Matt Mackallcd7619d2005-05-01 08:59:01 -0700182 BUG_ON(!PageLocked(page));
183
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700184 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 kaddr = kmap(page);
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800186 memset(kaddr + UDF_I(inode)->i_lenAlloc, 0x00,
187 PAGE_CACHE_SIZE - UDF_I(inode)->i_lenAlloc);
188 memcpy(kaddr, UDF_I(inode)->i_ext.i_data +
189 UDF_I(inode)->i_lenEAttr, UDF_I(inode)->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 flush_dcache_page(page);
191 SetPageUptodate(page);
192 kunmap(page);
193 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800194 memset(UDF_I(inode)->i_ext.i_data + UDF_I(inode)->i_lenEAttr, 0x00,
195 UDF_I(inode)->i_lenAlloc);
196 UDF_I(inode)->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800198 UDF_I(inode)->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 else
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800200 UDF_I(inode)->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 inode->i_data.a_ops->writepage(page, &udf_wbc);
203 page_cache_release(page);
204
205 mark_inode_dirty(inode);
206}
207
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700208struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
209 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700212 struct buffer_head *dbh = NULL;
213 kernel_lb_addr eloc;
214 uint32_t elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700216 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 struct udf_fileident_bh sfibh, dfibh;
219 loff_t f_pos = udf_ext0_offset(inode) >> 2;
220 int size = (udf_ext0_offset(inode) + inode->i_size) >> 2;
221 struct fileIdentDesc cfi, *sfi, *dfi;
222
223 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
224 alloctype = ICBTAG_FLAG_AD_SHORT;
225 else
226 alloctype = ICBTAG_FLAG_AD_LONG;
227
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700228 if (!inode->i_size) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800229 UDF_I(inode)->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 mark_inode_dirty(inode);
231 return NULL;
232 }
233
234 /* alloc block, and copy data to it */
235 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800236 UDF_I(inode)->i_location.partitionReferenceNum,
237 UDF_I(inode)->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (!(*block))
239 return NULL;
240 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800241 UDF_I(inode)->i_location.partitionReferenceNum,
242 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (!newblock)
244 return NULL;
245 dbh = udf_tgetblk(inode->i_sb, newblock);
246 if (!dbh)
247 return NULL;
248 lock_buffer(dbh);
249 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
250 set_buffer_uptodate(dbh);
251 unlock_buffer(dbh);
252 mark_buffer_dirty_inode(dbh, inode);
253
Marcin Slusarz4b111112008-02-08 04:20:36 -0800254 sfibh.soffset = sfibh.eoffset =
255 (f_pos & ((inode->i_sb->s_blocksize - 1) >> 2)) << 2;
Jan Karaff116fc2007-05-08 00:35:14 -0700256 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 dfibh.soffset = dfibh.eoffset = 0;
258 dfibh.sbh = dfibh.ebh = dbh;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700259 while ((f_pos < size)) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800260 UDF_I(inode)->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800261 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
262 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700263 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700264 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return NULL;
266 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800267 UDF_I(inode)->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 sfi->descTag.tagLocation = cpu_to_le32(*block);
269 dfibh.soffset = dfibh.eoffset;
270 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
271 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
272 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800273 sfi->fileIdent +
274 le16_to_cpu(sfi->lengthOfImpUse))) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800275 UDF_I(inode)->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700276 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return NULL;
278 }
279 }
280 mark_buffer_dirty_inode(dbh, inode);
281
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800282 memset(UDF_I(inode)->i_ext.i_data + UDF_I(inode)->i_lenEAttr, 0,
283 UDF_I(inode)->i_lenAlloc);
284 UDF_I(inode)->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800286 eloc.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800287 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 elen = inode->i_size;
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800289 UDF_I(inode)->i_lenExtents = elen;
Jan Karaff116fc2007-05-08 00:35:14 -0700290 epos.bh = NULL;
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800291 epos.block = UDF_I(inode)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700292 epos.offset = udf_file_entry_alloc_offset(inode);
293 udf_add_aext(inode, &epos, eloc, elen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /* UniqueID stuff */
295
Jan Kara3bf25cb2007-05-08 00:35:16 -0700296 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 mark_inode_dirty(inode);
298 return dbh;
299}
300
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700301static int udf_get_block(struct inode *inode, sector_t block,
302 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 int err, new;
305 struct buffer_head *bh;
306 unsigned long phys;
307
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700308 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 phys = udf_block_map(inode, block);
310 if (phys)
311 map_bh(bh_result, inode->i_sb, phys);
312 return 0;
313 }
314
315 err = -EIO;
316 new = 0;
317 bh = NULL;
318
319 lock_kernel();
320
321 if (block < 0)
322 goto abort_negative;
323
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800324 if (block == UDF_I(inode)->i_next_alloc_block + 1) {
325 UDF_I(inode)->i_next_alloc_block++;
326 UDF_I(inode)->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
329 err = 0;
330
331 bh = inode_getblk(inode, block, &err, &phys, &new);
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200332 BUG_ON(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (err)
334 goto abort;
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200335 BUG_ON(!phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 if (new)
338 set_buffer_new(bh_result);
339 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700340
341abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 unlock_kernel();
343 return err;
344
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700345abort_negative:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 udf_warning(inode->i_sb, "udf_get_block", "block < 0");
347 goto abort;
348}
349
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700350static struct buffer_head *udf_getblk(struct inode *inode, long block,
351 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700353 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 struct buffer_head dummy;
355
356 dummy.b_state = 0;
357 dummy.b_blocknr = -1000;
358 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700359 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700361 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 lock_buffer(bh);
363 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
364 set_buffer_uptodate(bh);
365 unlock_buffer(bh);
366 mark_buffer_dirty_inode(bh, inode);
367 }
368 return bh;
369 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return NULL;
372}
373
Jan Kara31170b62007-05-08 00:35:21 -0700374/* Extend the file by 'blocks' blocks, return the number of extents added */
375int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800376 kernel_long_ad *last_ext, sector_t blocks)
Jan Kara31170b62007-05-08 00:35:21 -0700377{
378 sector_t add;
379 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
380 struct super_block *sb = inode->i_sb;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700381 kernel_lb_addr prealloc_loc = {};
Jan Kara31170b62007-05-08 00:35:21 -0700382 int prealloc_len = 0;
383
384 /* The previous extent is fake and we should not extend by anything
385 * - there's nothing to do... */
386 if (!blocks && fake)
387 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700388
Jan Kara31170b62007-05-08 00:35:21 -0700389 /* Round the last extent up to a multiple of block size */
390 if (last_ext->extLength & (sb->s_blocksize - 1)) {
391 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700392 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
393 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
394 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800395 UDF_I(inode)->i_lenExtents =
396 (UDF_I(inode)->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700397 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700398 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700399
Jan Kara31170b62007-05-08 00:35:21 -0700400 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800401 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
402 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700403 /* Save the extent so that we can reattach it to the end */
404 prealloc_loc = last_ext->extLocation;
405 prealloc_len = last_ext->extLength;
406 /* Mark the extent as a hole */
407 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700408 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700409 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800410 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700411 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700412
Jan Kara31170b62007-05-08 00:35:21 -0700413 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800414 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
415 EXT_NOT_RECORDED_NOT_ALLOCATED) {
416 add = ((1 << 30) - sb->s_blocksize -
417 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
418 sb->s_blocksize_bits;
Jan Kara31170b62007-05-08 00:35:21 -0700419 if (add > blocks)
420 add = blocks;
421 blocks -= add;
422 last_ext->extLength += add << sb->s_blocksize_bits;
423 }
424
425 if (fake) {
426 udf_add_aext(inode, last_pos, last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700427 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700428 count++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800429 } else
430 udf_write_aext(inode, last_pos, last_ext->extLocation,
431 last_ext->extLength, 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700432
Jan Kara31170b62007-05-08 00:35:21 -0700433 /* Managed to do everything necessary? */
434 if (!blocks)
435 goto out;
436
437 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
438 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800439 last_ext->extLocation.partitionReferenceNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700440 add = (1 << (30-sb->s_blocksize_bits)) - 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800441 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
442 (add << sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700443
Jan Kara31170b62007-05-08 00:35:21 -0700444 /* Create enough extents to cover the whole hole */
445 while (blocks > add) {
446 blocks -= add;
447 if (udf_add_aext(inode, last_pos, last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700448 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700449 return -1;
450 count++;
451 }
452 if (blocks) {
453 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700454 (blocks << sb->s_blocksize_bits);
Jan Kara31170b62007-05-08 00:35:21 -0700455 if (udf_add_aext(inode, last_pos, last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700456 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700457 return -1;
458 count++;
459 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700460
461out:
Jan Kara31170b62007-05-08 00:35:21 -0700462 /* Do we have some preallocated blocks saved? */
463 if (prealloc_len) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800464 if (udf_add_aext(inode, last_pos, prealloc_loc,
465 prealloc_len, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700466 return -1;
467 last_ext->extLocation = prealloc_loc;
468 last_ext->extLength = prealloc_len;
469 count++;
470 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700471
Jan Kara31170b62007-05-08 00:35:21 -0700472 /* last_pos should point to the last written extent... */
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800473 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Jan Kara31170b62007-05-08 00:35:21 -0700474 last_pos->offset -= sizeof(short_ad);
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800475 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Jan Kara31170b62007-05-08 00:35:21 -0700476 last_pos->offset -= sizeof(long_ad);
477 else
478 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700479
Jan Kara31170b62007-05-08 00:35:21 -0700480 return count;
481}
482
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700483static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
484 int *err, long *phys, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Jan Kara31170b62007-05-08 00:35:21 -0700486 static sector_t last_block;
Jan Karaff116fc2007-05-08 00:35:14 -0700487 struct buffer_head *result = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700489 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700491 uint32_t elen = 0, tmpelen;
492 kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700494 loff_t lbcount = 0, b_off = 0;
495 uint32_t newblocknum, newblock;
496 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 int8_t etype;
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800498 int goal = 0, pgoal = UDF_I(inode)->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700499 int lastblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Jan Karaff116fc2007-05-08 00:35:14 -0700501 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800502 prev_epos.block = UDF_I(inode)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700503 prev_epos.bh = NULL;
504 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700505 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700508 alternate between laarr[0] and laarr[1] for locations of the
509 current extent, and the previous extent */
510 do {
511 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700512 brelse(prev_epos.bh);
513 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700514 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700516 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700517 brelse(cur_epos.bh);
518 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700519 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521
522 lbcount += elen;
523
Jan Karaff116fc2007-05-08 00:35:14 -0700524 prev_epos.block = cur_epos.block;
525 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Jan Karaff116fc2007-05-08 00:35:14 -0700527 prev_epos.offset = cur_epos.offset;
528 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Marcin Slusarz4b111112008-02-08 04:20:36 -0800530 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
531 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 break;
533
534 c = !c;
535
536 laarr[c].extLength = (etype << 30) | elen;
537 laarr[c].extLocation = eloc;
538
539 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
540 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700541 ((elen + inode->i_sb->s_blocksize - 1) >>
542 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700544 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 } while (lbcount + elen <= b_off);
546
547 b_off -= lbcount;
548 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700549 /*
550 * Move prev_epos and cur_epos into indirect extent if we are at
551 * the pointer to it
552 */
553 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
554 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700557 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700559 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
560 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700562 ((elen + inode->i_sb->s_blocksize - 1) &
563 ~(inode->i_sb->s_blocksize - 1));
Jan Karaff116fc2007-05-08 00:35:14 -0700564 etype = udf_write_aext(inode, &cur_epos, eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700566 brelse(prev_epos.bh);
567 brelse(cur_epos.bh);
568 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 newblock = udf_get_lb_pblock(inode->i_sb, eloc, offset);
570 *phys = newblock;
571 return NULL;
572 }
573
Jan Kara31170b62007-05-08 00:35:21 -0700574 last_block = block;
575 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700576 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700577 int ret;
578
579 if (count) {
580 if (c)
581 laarr[0] = laarr[1];
582 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700583 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700584 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800585 memset(&laarr[0].extLocation, 0x00,
586 sizeof(kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700587 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800588 /* Will udf_extend_file() create real extent from
589 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700590 startnum = (offset > 0);
591 }
592 /* Create extents for the hole between EOF and offset */
593 ret = udf_extend_file(inode, &prev_epos, laarr, offset);
594 if (ret == -1) {
595 brelse(prev_epos.bh);
596 brelse(cur_epos.bh);
597 brelse(next_epos.bh);
598 /* We don't really know the error here so we just make
599 * something up */
600 *err = -ENOSPC;
601 return NULL;
602 }
603 c = 0;
604 offset = 0;
605 count += ret;
606 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800607 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
608 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700609 /* Is there any real extent? - otherwise we overwrite
610 * the fake one... */
611 if (count)
612 c = !c;
613 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700614 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800615 memset(&laarr[c].extLocation, 0x00,
616 sizeof(kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700617 count++;
618 endnum++;
Jan Kara31170b62007-05-08 00:35:21 -0700619 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700620 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700622 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 endnum = startnum = ((count > 2) ? 2 : count);
624
Marcin Slusarz4b111112008-02-08 04:20:36 -0800625 /* if the current extent is in position 0,
626 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700627 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700628 laarr[2] = laarr[0];
629 laarr[0] = laarr[1];
630 laarr[1] = laarr[2];
631 c = 1;
632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Marcin Slusarz4b111112008-02-08 04:20:36 -0800634 /* if the current block is located in an extent,
635 read the next extent */
636 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
637 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700638 laarr[c + 1].extLength = (etype << 30) | elen;
639 laarr[c + 1].extLocation = eloc;
640 count++;
641 startnum++;
642 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800643 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 lastblock = 1;
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700648 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800649 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800651 else { /* otherwise, allocate a new block */
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800652 if (UDF_I(inode)->i_next_alloc_block == block)
653 goal = UDF_I(inode)->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700655 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800656 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800657 goal = UDF_I(inode)->
658 i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
Marcin Slusarz4b111112008-02-08 04:20:36 -0800661 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800662 UDF_I(inode)->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800663 goal, err);
664 if (!newblocknum) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700665 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 *err = -ENOSPC;
667 return NULL;
668 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800669 UDF_I(inode)->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671
Marcin Slusarz4b111112008-02-08 04:20:36 -0800672 /* if the extent the requsted block is located in contains multiple
673 * blocks, split the extent into at most three extents. blocks prior
674 * to requested block, requested block, and blocks after requested
675 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
677
678#ifdef UDF_PREALLOCATE
679 /* preallocate blocks */
680 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
681#endif
682
683 /* merge any continuous blocks in laarr */
684 udf_merge_extents(inode, laarr, &endnum);
685
686 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700687 * of extents is greater than the old number, and deleting extents if
688 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700689 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Jan Kara3bf25cb2007-05-08 00:35:16 -0700691 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Marcin Slusarz4b111112008-02-08 04:20:36 -0800693 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800694 UDF_I(inode)->i_location.partitionReferenceNum, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800695 if (!newblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 *phys = newblock;
698 *err = 0;
699 *new = 1;
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800700 UDF_I(inode)->i_next_alloc_block = block;
701 UDF_I(inode)->i_next_alloc_goal = newblocknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 inode->i_ctime = current_fs_time(inode->i_sb);
703
704 if (IS_SYNC(inode))
705 udf_sync_inode(inode);
706 else
707 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return result;
710}
711
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700712static void udf_split_extents(struct inode *inode, int *c, int offset,
713 int newblocknum,
714 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
715 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800717 unsigned long blocksize = inode->i_sb->s_blocksize;
718 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800721 (laarr[*c].extLength >> 30) ==
722 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 int curr = *c;
724 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800725 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 int8_t etype = (laarr[curr].extLength >> 30);
727
Marcin Slusarz4b111112008-02-08 04:20:36 -0800728 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700729 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800730 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700731 laarr[curr + 2] = laarr[curr + 1];
732 laarr[curr + 1] = laarr[curr];
733 } else {
734 laarr[curr + 3] = laarr[curr + 1];
735 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700738 if (offset) {
739 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800740 udf_free_blocks(inode->i_sb, inode,
741 laarr[curr].extLocation,
742 0, offset);
743 laarr[curr].extLength =
744 EXT_NOT_RECORDED_NOT_ALLOCATED |
745 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800747 laarr[curr].extLocation.
748 partitionReferenceNum = 0;
749 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800751 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700752 curr++;
753 (*c)++;
754 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 laarr[curr].extLocation.logicalBlockNum = newblocknum;
758 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
759 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800760 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800762 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700763 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700765 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800767 laarr[curr].extLocation.logicalBlockNum +=
768 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700769 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800770 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700771 curr++;
772 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774 }
775}
776
777static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700778 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
779 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 int start, length = 0, currlength = 0, i;
782
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700783 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (!lastblock)
785 return;
786 else
787 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700788 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800789 if ((laarr[c + 1].extLength >> 30) ==
790 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700791 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800792 length = currlength =
793 (((laarr[c + 1].extLength &
794 UDF_EXTENT_LENGTH_MASK) +
795 inode->i_sb->s_blocksize - 1) >>
796 inode->i_sb->s_blocksize_bits);
797 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 start = c;
799 }
800
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700801 for (i = start + 1; i <= *endnum; i++) {
802 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (lastblock)
804 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800805 } else if ((laarr[i].extLength >> 30) ==
806 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
807 length += (((laarr[i].extLength &
808 UDF_EXTENT_LENGTH_MASK) +
809 inode->i_sb->s_blocksize - 1) >>
810 inode->i_sb->s_blocksize_bits);
811 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 break;
813 }
814
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700815 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700817 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800818 inode->i_sb->s_blocksize - 1) >>
819 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800821 laarr[start].extLocation.partitionReferenceNum,
822 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
823 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
824 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700825 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800826 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800828 (numalloc <<
829 inode->i_sb->s_blocksize_bits);
830 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700831 memmove(&laarr[c + 2], &laarr[c + 1],
832 sizeof(long_ad) * (*endnum - (c + 1)));
833 (*endnum)++;
834 laarr[c + 1].extLocation.logicalBlockNum = next;
835 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800836 laarr[c].extLocation.
837 partitionReferenceNum;
838 laarr[c + 1].extLength =
839 EXT_NOT_RECORDED_ALLOCATED |
840 (numalloc <<
841 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700842 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700845 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800846 int elen = ((laarr[i].extLength &
847 UDF_EXTENT_LENGTH_MASK) +
848 inode->i_sb->s_blocksize - 1) >>
849 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700851 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800853 (numalloc <<
854 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700856 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700858 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800859 memmove(&laarr[i],
860 &laarr[i + 1],
861 sizeof(long_ad) *
862 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700863 i--;
864 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800867 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800868 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870 }
871}
872
873static void udf_merge_extents(struct inode *inode,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700874 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
875 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800878 unsigned long blocksize = inode->i_sb->s_blocksize;
879 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700881 for (i = 0; i < (*endnum - 1); i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800882 kernel_long_ad *li /*l[i]*/ = &laarr[i];
883 kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Marcin Slusarz4b111112008-02-08 04:20:36 -0800885 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
886 (((li->extLength >> 30) ==
887 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
888 ((lip1->extLocation.logicalBlockNum -
889 li->extLocation.logicalBlockNum) ==
890 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
891 blocksize - 1) >> blocksize_bits)))) {
892
893 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
894 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
895 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
896 lip1->extLength = (lip1->extLength -
897 (li->extLength &
898 UDF_EXTENT_LENGTH_MASK) +
899 UDF_EXTENT_LENGTH_MASK) &
900 ~(blocksize - 1);
901 li->extLength = (li->extLength &
902 UDF_EXTENT_FLAG_MASK) +
903 (UDF_EXTENT_LENGTH_MASK + 1) -
904 blocksize;
905 lip1->extLocation.logicalBlockNum =
906 li->extLocation.logicalBlockNum +
907 ((li->extLength &
908 UDF_EXTENT_LENGTH_MASK) >>
909 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700910 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800911 li->extLength = lip1->extLength +
912 (((li->extLength &
913 UDF_EXTENT_LENGTH_MASK) +
914 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700915 if (*endnum > (i + 2))
916 memmove(&laarr[i + 1], &laarr[i + 2],
Marcin Slusarz4b111112008-02-08 04:20:36 -0800917 sizeof(long_ad) *
918 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700919 i--;
920 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800922 } else if (((li->extLength >> 30) ==
923 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
924 ((lip1->extLength >> 30) ==
925 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
926 udf_free_blocks(inode->i_sb, inode, li->extLocation, 0,
927 ((li->extLength &
928 UDF_EXTENT_LENGTH_MASK) +
929 blocksize - 1) >> blocksize_bits);
930 li->extLocation.logicalBlockNum = 0;
931 li->extLocation.partitionReferenceNum = 0;
932
933 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
934 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
935 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
936 lip1->extLength = (lip1->extLength -
937 (li->extLength &
938 UDF_EXTENT_LENGTH_MASK) +
939 UDF_EXTENT_LENGTH_MASK) &
940 ~(blocksize - 1);
941 li->extLength = (li->extLength &
942 UDF_EXTENT_FLAG_MASK) +
943 (UDF_EXTENT_LENGTH_MASK + 1) -
944 blocksize;
945 } else {
946 li->extLength = lip1->extLength +
947 (((li->extLength &
948 UDF_EXTENT_LENGTH_MASK) +
949 blocksize - 1) & ~(blocksize - 1));
950 if (*endnum > (i + 2))
951 memmove(&laarr[i + 1], &laarr[i + 2],
952 sizeof(long_ad) *
953 (*endnum - (i + 2)));
954 i--;
955 (*endnum)--;
956 }
957 } else if ((li->extLength >> 30) ==
958 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
959 udf_free_blocks(inode->i_sb, inode,
960 li->extLocation, 0,
961 ((li->extLength &
962 UDF_EXTENT_LENGTH_MASK) +
963 blocksize - 1) >> blocksize_bits);
964 li->extLocation.logicalBlockNum = 0;
965 li->extLocation.partitionReferenceNum = 0;
966 li->extLength = (li->extLength &
967 UDF_EXTENT_LENGTH_MASK) |
968 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970 }
971}
972
973static void udf_update_extents(struct inode *inode,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700974 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
975 int startnum, int endnum,
976 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 int start = 0, i;
979 kernel_lb_addr tmploc;
980 uint32_t tmplen;
981
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700982 if (startnum > endnum) {
983 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -0700984 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700985 laarr[i].extLength);
986 } else if (startnum < endnum) {
987 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -0700988 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700989 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -0700990 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700991 &laarr[i].extLength, 1);
992 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994 }
995
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700996 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -0700997 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
998 udf_write_aext(inode, epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700999 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001}
1002
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001003struct buffer_head *udf_bread(struct inode *inode, int block,
1004 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001006 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 bh = udf_getblk(inode, block, create, err);
1009 if (!bh)
1010 return NULL;
1011
1012 if (buffer_uptodate(bh))
1013 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 ll_rw_block(READ, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 wait_on_buffer(bh);
1018 if (buffer_uptodate(bh))
1019 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 brelse(bh);
1022 *err = -EIO;
1023 return NULL;
1024}
1025
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001026void udf_truncate(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 int offset;
1029 int err;
1030
1031 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001032 S_ISLNK(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return;
1034 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1035 return;
1036
1037 lock_kernel();
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001038 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001039 if (inode->i_sb->s_blocksize <
1040 (udf_file_entry_alloc_offset(inode) +
1041 inode->i_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 udf_expand_file_adinicb(inode, inode->i_size, &err);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001043 if (UDF_I(inode)->i_alloc_type ==
1044 ICBTAG_FLAG_AD_IN_ICB) {
1045 inode->i_size = UDF_I(inode)->i_lenAlloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 unlock_kernel();
1047 return;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001048 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 udf_truncate_extents(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001050 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001052 memset(UDF_I(inode)->i_ext.i_data +
1053 UDF_I(inode)->i_lenEAttr + offset,
1054 0x00, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001055 offset - udf_file_entry_alloc_offset(inode));
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001056 UDF_I(inode)->i_lenAlloc = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001058 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001059 block_truncate_page(inode->i_mapping, inode->i_size,
1060 udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 udf_truncate_extents(inode);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1065 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001066 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 else
1068 mark_inode_dirty(inode);
1069 unlock_kernel();
1070}
1071
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001072static void __udf_read_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 struct buffer_head *bh = NULL;
1075 struct fileEntry *fe;
1076 uint16_t ident;
1077
1078 /*
1079 * Set defaults, but the inode is still incomplete!
1080 * Note: get_new_inode() sets the following on a new inode:
1081 * i_sb = sb
1082 * i_no = ino
1083 * i_flags = sb->s_flags
1084 * i_state = 0
1085 * clean_inode(): zero fills and sets
1086 * i_count = 1
1087 * i_nlink = 1
1088 * i_op = NULL;
1089 */
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001090 bh = udf_read_ptagged(inode->i_sb, UDF_I(inode)->i_location, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001091 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001093 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 make_bad_inode(inode);
1095 return;
1096 }
1097
1098 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001099 ident != TAG_IDENT_USE) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001100 printk(KERN_ERR "udf: udf_read_inode(ino %ld) "
1101 "failed ident=%d\n", inode->i_ino, ident);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001102 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 make_bad_inode(inode);
1104 return;
1105 }
1106
1107 fe = (struct fileEntry *)bh->b_data;
1108
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001109 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 struct buffer_head *ibh = NULL, *nbh = NULL;
1111 struct indirectEntry *ie;
1112
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001113 ibh = udf_read_ptagged(inode->i_sb, UDF_I(inode)->i_location, 1,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001114 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001115 if (ident == TAG_IDENT_IE) {
1116 if (ibh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 kernel_lb_addr loc;
1118 ie = (struct indirectEntry *)ibh->b_data;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001121
1122 if (ie->indirectICB.extLength &&
Marcin Slusarz4b111112008-02-08 04:20:36 -08001123 (nbh = udf_read_ptagged(inode->i_sb, loc, 0,
1124 &ident))) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001125 if (ident == TAG_IDENT_FE ||
1126 ident == TAG_IDENT_EFE) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001127 memcpy(&UDF_I(inode)->i_location,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001128 &loc,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001129 sizeof(kernel_lb_addr));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001130 brelse(bh);
1131 brelse(ibh);
1132 brelse(nbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 __udf_read_inode(inode);
1134 return;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001135 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001136 brelse(nbh);
1137 brelse(ibh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001139 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001140 brelse(ibh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001143 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001144 brelse(ibh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001145 }
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001146 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 printk(KERN_ERR "udf: unsupported strategy type: %d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001148 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001149 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 make_bad_inode(inode);
1151 return;
1152 }
1153 udf_fill_inode(inode, bh);
Jan Kara31170b62007-05-08 00:35:21 -07001154
Jan Kara3bf25cb2007-05-08 00:35:16 -07001155 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
1158static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1159{
1160 struct fileEntry *fe;
1161 struct extendedFileEntry *efe;
1162 time_t convtime;
1163 long convtime_usec;
1164 int offset;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001165 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 fe = (struct fileEntry *)bh->b_data;
1168 efe = (struct extendedFileEntry *)bh->b_data;
1169
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001170 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001171 UDF_I(inode)->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001172 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001173 UDF_I(inode)->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001175 UDF_I(inode)->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001176 ICBTAG_FLAG_AD_MASK;
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001177 UDF_I(inode)->i_unique = 0;
1178 UDF_I(inode)->i_lenEAttr = 0;
1179 UDF_I(inode)->i_lenExtents = 0;
1180 UDF_I(inode)->i_lenAlloc = 0;
1181 UDF_I(inode)->i_next_alloc_block = 0;
1182 UDF_I(inode)->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001183 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001184 UDF_I(inode)->i_efe = 1;
1185 UDF_I(inode)->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001186 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1187 sizeof(struct extendedFileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001188 make_bad_inode(inode);
1189 return;
1190 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001191 memcpy(UDF_I(inode)->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001192 bh->b_data + sizeof(struct extendedFileEntry),
1193 inode->i_sb->s_blocksize -
1194 sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001195 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001196 UDF_I(inode)->i_efe = 0;
1197 UDF_I(inode)->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001198 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1199 sizeof(struct fileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001200 make_bad_inode(inode);
1201 return;
1202 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001203 memcpy(UDF_I(inode)->i_ext.i_data,
1204 bh->b_data + sizeof(struct fileEntry),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001205 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001206 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001207 UDF_I(inode)->i_efe = 0;
1208 UDF_I(inode)->i_use = 1;
1209 UDF_I(inode)->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001210 ((struct unallocSpaceEntry *)bh->b_data)->
1211 lengthAllocDescs);
1212 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1213 sizeof(struct unallocSpaceEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001214 make_bad_inode(inode);
1215 return;
1216 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001217 memcpy(UDF_I(inode)->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001218 bh->b_data + sizeof(struct unallocSpaceEntry),
1219 inode->i_sb->s_blocksize -
1220 sizeof(struct unallocSpaceEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return;
1222 }
1223
1224 inode->i_uid = le32_to_cpu(fe->uid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001225 if (inode->i_uid == -1 ||
1226 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1227 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001228 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 inode->i_gid = le32_to_cpu(fe->gid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001231 if (inode->i_gid == -1 ||
1232 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1233 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001234 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
1237 if (!inode->i_nlink)
1238 inode->i_nlink = 1;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 inode->i_size = le64_to_cpu(fe->informationLength);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001241 UDF_I(inode)->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 inode->i_mode = udf_convert_permissions(fe);
1244 inode->i_mode &= ~UDF_SB(inode->i_sb)->s_umask;
1245
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001246 if (UDF_I(inode)->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001248 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001250 if (udf_stamp_to_time(&convtime, &convtime_usec,
1251 lets_to_cpu(fe->accessTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 inode->i_atime.tv_sec = convtime;
1253 inode->i_atime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001254 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001255 inode->i_atime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001258 if (udf_stamp_to_time(&convtime, &convtime_usec,
1259 lets_to_cpu(fe->modificationTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 inode->i_mtime.tv_sec = convtime;
1261 inode->i_mtime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001262 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001263 inode->i_mtime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001266 if (udf_stamp_to_time(&convtime, &convtime_usec,
1267 lets_to_cpu(fe->attrTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 inode->i_ctime.tv_sec = convtime;
1269 inode->i_ctime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001270 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001271 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001274 UDF_I(inode)->i_unique = le64_to_cpu(fe->uniqueID);
1275 UDF_I(inode)->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1276 UDF_I(inode)->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1277 offset = sizeof(struct fileEntry) + UDF_I(inode)->i_lenEAttr;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001278 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001279 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001280 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001282 if (udf_stamp_to_time(&convtime, &convtime_usec,
1283 lets_to_cpu(efe->accessTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 inode->i_atime.tv_sec = convtime;
1285 inode->i_atime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001286 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001287 inode->i_atime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001290 if (udf_stamp_to_time(&convtime, &convtime_usec,
1291 lets_to_cpu(efe->modificationTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 inode->i_mtime.tv_sec = convtime;
1293 inode->i_mtime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001294 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001295 inode->i_mtime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001298 if (udf_stamp_to_time(&convtime, &convtime_usec,
1299 lets_to_cpu(efe->createTime))) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001300 UDF_I(inode)->i_crtime.tv_sec = convtime;
1301 UDF_I(inode)->i_crtime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001302 } else {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001303 UDF_I(inode)->i_crtime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001306 if (udf_stamp_to_time(&convtime, &convtime_usec,
1307 lets_to_cpu(efe->attrTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 inode->i_ctime.tv_sec = convtime;
1309 inode->i_ctime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001310 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001311 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001314 UDF_I(inode)->i_unique = le64_to_cpu(efe->uniqueID);
1315 UDF_I(inode)->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1316 UDF_I(inode)->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001317 offset = sizeof(struct extendedFileEntry) +
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001318 UDF_I(inode)->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001321 switch (fe->icbTag.fileType) {
1322 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001323 inode->i_op = &udf_dir_inode_operations;
1324 inode->i_fop = &udf_dir_operations;
1325 inode->i_mode |= S_IFDIR;
1326 inc_nlink(inode);
1327 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001328 case ICBTAG_FILE_TYPE_REALTIME:
1329 case ICBTAG_FILE_TYPE_REGULAR:
1330 case ICBTAG_FILE_TYPE_UNDEF:
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001331 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001332 inode->i_data.a_ops = &udf_adinicb_aops;
1333 else
1334 inode->i_data.a_ops = &udf_aops;
1335 inode->i_op = &udf_file_inode_operations;
1336 inode->i_fop = &udf_file_operations;
1337 inode->i_mode |= S_IFREG;
1338 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001339 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001340 inode->i_mode |= S_IFBLK;
1341 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001342 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001343 inode->i_mode |= S_IFCHR;
1344 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001345 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001346 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1347 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001348 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001349 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1350 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001351 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001352 inode->i_data.a_ops = &udf_symlink_aops;
1353 inode->i_op = &page_symlink_inode_operations;
1354 inode->i_mode = S_IFLNK | S_IRWXUGO;
1355 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001356 default:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001357 printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
1358 "file type=%d\n", inode->i_ino,
1359 fe->icbTag.fileType);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001360 make_bad_inode(inode);
1361 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001363 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001364 struct deviceSpec *dsea =
1365 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001366 if (dsea) {
1367 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001368 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1369 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001371 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374}
1375
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001376static int udf_alloc_i_data(struct inode *inode, size_t size)
1377{
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001378 UDF_I(inode)->i_ext.i_data = kmalloc(size, GFP_KERNEL);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001379
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001380 if (!UDF_I(inode)->i_ext.i_data) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001381 printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
1382 "no free memory\n", inode->i_ino);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001383 return -ENOMEM;
1384 }
1385
1386 return 0;
1387}
1388
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001389static mode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 mode_t mode;
1392 uint32_t permissions;
1393 uint32_t flags;
1394
1395 permissions = le32_to_cpu(fe->permissions);
1396 flags = le16_to_cpu(fe->icbTag.flags);
1397
Marcin Slusarz4b111112008-02-08 04:20:36 -08001398 mode = ((permissions) & S_IRWXO) |
1399 ((permissions >> 2) & S_IRWXG) |
1400 ((permissions >> 4) & S_IRWXU) |
1401 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1402 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1403 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 return mode;
1406}
1407
1408/*
1409 * udf_write_inode
1410 *
1411 * PURPOSE
1412 * Write out the specified inode.
1413 *
1414 * DESCRIPTION
1415 * This routine is called whenever an inode is synced.
1416 * Currently this routine is just a placeholder.
1417 *
1418 * HISTORY
1419 * July 1, 1997 - Andrew E. Mileski
1420 * Written, tested, and released.
1421 */
1422
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001423int udf_write_inode(struct inode *inode, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 int ret;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 lock_kernel();
1428 ret = udf_update_inode(inode, sync);
1429 unlock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return ret;
1432}
1433
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001434int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 return udf_update_inode(inode, 1);
1437}
1438
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001439static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 struct buffer_head *bh = NULL;
1442 struct fileEntry *fe;
1443 struct extendedFileEntry *efe;
1444 uint32_t udfperms;
1445 uint16_t icbflags;
1446 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 kernel_timestamp cpu_time;
1448 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001449 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001450 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Marcin Slusarz4b111112008-02-08 04:20:36 -08001452 bh = udf_tread(inode->i_sb,
1453 udf_get_lb_pblock(inode->i_sb,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001454 UDF_I(inode)->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001455 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 udf_debug("bread failure\n");
1457 return -EIO;
1458 }
1459
1460 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
1461
1462 fe = (struct fileEntry *)bh->b_data;
1463 efe = (struct extendedFileEntry *)bh->b_data;
1464
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001465 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001467 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001469 use->lengthAllocDescs = cpu_to_le32(UDF_I(inode)->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001470 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001471 UDF_I(inode)->i_ext.i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001472 sizeof(struct unallocSpaceEntry));
1473 crclen = sizeof(struct unallocSpaceEntry) +
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001474 UDF_I(inode)->i_lenAlloc - sizeof(tag);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001475 use->descTag.tagLocation = cpu_to_le32(
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001476 UDF_I(inode)->i_location.
Marcin Slusarz4b111112008-02-08 04:20:36 -08001477 logicalBlockNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 use->descTag.descCRCLength = cpu_to_le16(crclen);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001479 use->descTag.descCRC = cpu_to_le16(udf_crc((char *)use +
1480 sizeof(tag), crclen,
1481 0));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001482 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 mark_buffer_dirty(bh);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001485 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return err;
1487 }
1488
Phillip Susi4d6660e2006-03-07 21:55:24 -08001489 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1490 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001491 else
1492 fe->uid = cpu_to_le32(inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Phillip Susi4d6660e2006-03-07 21:55:24 -08001494 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1495 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001496 else
1497 fe->gid = cpu_to_le32(inode->i_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Marcin Slusarz4b111112008-02-08 04:20:36 -08001499 udfperms = ((inode->i_mode & S_IRWXO)) |
1500 ((inode->i_mode & S_IRWXG) << 2) |
1501 ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Marcin Slusarz4b111112008-02-08 04:20:36 -08001503 udfperms |= (le32_to_cpu(fe->permissions) &
1504 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1505 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1506 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 fe->permissions = cpu_to_le32(udfperms);
1508
1509 if (S_ISDIR(inode->i_mode))
1510 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1511 else
1512 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1513
1514 fe->informationLength = cpu_to_le64(inode->i_size);
1515
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001516 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001518 struct deviceSpec *dsea =
1519 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001520 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001522 udf_add_extendedattr(inode,
1523 sizeof(struct deviceSpec) +
1524 sizeof(regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 dsea->attrType = cpu_to_le32(12);
1526 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001527 dsea->attrLength = cpu_to_le32(
1528 sizeof(struct deviceSpec) +
1529 sizeof(regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 dsea->impUseLength = cpu_to_le32(sizeof(regid));
1531 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001532 eid = (regid *)dsea->impUse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 memset(eid, 0, sizeof(regid));
1534 strcpy(eid->ident, UDF_ID_DEVELOPER);
1535 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1536 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1537 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1538 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1539 }
1540
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001541 if (UDF_I(inode)->i_efe == 0) {
1542 memcpy(bh->b_data + sizeof(struct fileEntry),
1543 UDF_I(inode)->i_ext.i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001544 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001545 fe->logicalBlocksRecorded = cpu_to_le64(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001546 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1547 (blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1550 fe->accessTime = cpu_to_lets(cpu_time);
1551 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1552 fe->modificationTime = cpu_to_lets(cpu_time);
1553 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1554 fe->attrTime = cpu_to_lets(cpu_time);
1555 memset(&(fe->impIdent), 0, sizeof(regid));
1556 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1557 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1558 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001559 fe->uniqueID = cpu_to_le64(UDF_I(inode)->i_unique);
1560 fe->lengthExtendedAttr = cpu_to_le32(UDF_I(inode)->i_lenEAttr);
1561 fe->lengthAllocDescs = cpu_to_le32(UDF_I(inode)->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1563 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001564 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001565 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001566 UDF_I(inode)->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001567 inode->i_sb->s_blocksize -
1568 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 efe->objectSize = cpu_to_le64(inode->i_size);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001570 efe->logicalBlocksRecorded = cpu_to_le64(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001571 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1572 (blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001574 if (UDF_I(inode)->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1575 (UDF_I(inode)->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1576 UDF_I(inode)->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1577 UDF_I(inode)->i_crtime = inode->i_atime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001578
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001579 if (UDF_I(inode)->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1580 (UDF_I(inode)->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1581 UDF_I(inode)->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1582 UDF_I(inode)->i_crtime = inode->i_mtime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001583
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001584 if (UDF_I(inode)->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1585 (UDF_I(inode)->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1586 UDF_I(inode)->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1587 UDF_I(inode)->i_crtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1590 efe->accessTime = cpu_to_lets(cpu_time);
1591 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1592 efe->modificationTime = cpu_to_lets(cpu_time);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001593 if (udf_time_to_stamp(&cpu_time, UDF_I(inode)->i_crtime))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 efe->createTime = cpu_to_lets(cpu_time);
1595 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1596 efe->attrTime = cpu_to_lets(cpu_time);
1597
1598 memset(&(efe->impIdent), 0, sizeof(regid));
1599 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1600 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1601 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001602 efe->uniqueID = cpu_to_le64(UDF_I(inode)->i_unique);
1603 efe->lengthExtendedAttr = cpu_to_le32(UDF_I(inode)->i_lenEAttr);
1604 efe->lengthAllocDescs = cpu_to_le32(UDF_I(inode)->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1606 crclen = sizeof(struct extendedFileEntry);
1607 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001608 if (UDF_I(inode)->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 fe->icbTag.strategyType = cpu_to_le16(4096);
1610 fe->icbTag.strategyParameter = cpu_to_le16(1);
1611 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001612 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 fe->icbTag.strategyType = cpu_to_le16(4);
1614 fe->icbTag.numEntries = cpu_to_le16(1);
1615 }
1616
1617 if (S_ISDIR(inode->i_mode))
1618 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1619 else if (S_ISREG(inode->i_mode))
1620 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1621 else if (S_ISLNK(inode->i_mode))
1622 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1623 else if (S_ISBLK(inode->i_mode))
1624 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1625 else if (S_ISCHR(inode->i_mode))
1626 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1627 else if (S_ISFIFO(inode->i_mode))
1628 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1629 else if (S_ISSOCK(inode->i_mode))
1630 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1631
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001632 icbflags = UDF_I(inode)->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001633 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1634 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1635 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1636 (le16_to_cpu(fe->icbTag.flags) &
1637 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1638 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001641 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 fe->descTag.descVersion = cpu_to_le16(3);
1643 else
1644 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001645 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001646 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001647 UDF_I(inode)->i_location.logicalBlockNum);
1648 crclen += UDF_I(inode)->i_lenEAttr + UDF_I(inode)->i_lenAlloc -
1649 sizeof(tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001651 fe->descTag.descCRC = cpu_to_le16(udf_crc((char *)fe + sizeof(tag),
1652 crclen, 0));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001653 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 /* write the data blocks */
1656 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001657 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 sync_dirty_buffer(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001659 if (buffer_req(bh) && !buffer_uptodate(bh)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001660 printk(KERN_WARNING "IO error syncing udf inode "
1661 "[%s:%08lx]\n", inode->i_sb->s_id,
1662 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 err = -EIO;
1664 }
1665 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001666 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return err;
1669}
1670
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001671struct inode *udf_iget(struct super_block *sb, kernel_lb_addr ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1674 struct inode *inode = iget_locked(sb, block);
1675
1676 if (!inode)
1677 return NULL;
1678
1679 if (inode->i_state & I_NEW) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001680 memcpy(&UDF_I(inode)->i_location, &ino, sizeof(kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 __udf_read_inode(inode);
1682 unlock_new_inode(inode);
1683 }
1684
1685 if (is_bad_inode(inode))
1686 goto out_iput;
1687
Marcin Slusarz4b111112008-02-08 04:20:36 -08001688 if (ino.logicalBlockNum >= UDF_SB(sb)->
1689 s_partmaps[ino.partitionReferenceNum].s_partition_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 udf_debug("block=%d, partition=%d out of range\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001691 ino.logicalBlockNum, ino.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 make_bad_inode(inode);
1693 goto out_iput;
1694 }
1695
1696 return inode;
1697
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001698 out_iput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 iput(inode);
1700 return NULL;
1701}
1702
Marcin Slusarz4b111112008-02-08 04:20:36 -08001703int8_t udf_add_aext(struct inode *inode, struct extent_position *epos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001704 kernel_lb_addr eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
1706 int adsize;
1707 short_ad *sad = NULL;
1708 long_ad *lad = NULL;
1709 struct allocExtDesc *aed;
1710 int8_t etype;
1711 uint8_t *ptr;
1712
Jan Karaff116fc2007-05-08 00:35:14 -07001713 if (!epos->bh)
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001714 ptr = UDF_I(inode)->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001715 udf_file_entry_alloc_offset(inode) +
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001716 UDF_I(inode)->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 else
Jan Karaff116fc2007-05-08 00:35:14 -07001718 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001720 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 adsize = sizeof(short_ad);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001722 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 adsize = sizeof(long_ad);
1724 else
1725 return -1;
1726
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001727 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 char *sptr, *dptr;
1729 struct buffer_head *nbh;
1730 int err, loffset;
Jan Karaff116fc2007-05-08 00:35:14 -07001731 kernel_lb_addr obloc = epos->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Marcin Slusarz4b111112008-02-08 04:20:36 -08001733 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1734 obloc.partitionReferenceNum,
1735 obloc.logicalBlockNum, &err);
1736 if (!epos->block.logicalBlockNum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return -1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001738 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
1739 epos->block,
1740 0));
1741 if (!nbh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 lock_buffer(nbh);
1744 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1745 set_buffer_uptodate(nbh);
1746 unlock_buffer(nbh);
1747 mark_buffer_dirty_inode(nbh, inode);
1748
1749 aed = (struct allocExtDesc *)(nbh->b_data);
1750 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001751 aed->previousAllocExtLocation =
1752 cpu_to_le32(obloc.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001753 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -07001754 loffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 aed->lengthAllocDescs = cpu_to_le32(adsize);
1756 sptr = ptr - adsize;
1757 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1758 memcpy(dptr, sptr, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -07001759 epos->offset = sizeof(struct allocExtDesc) + adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001760 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001761 loffset = epos->offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 aed->lengthAllocDescs = cpu_to_le32(0);
1763 sptr = ptr;
Jan Karaff116fc2007-05-08 00:35:14 -07001764 epos->offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001766 if (epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001767 aed = (struct allocExtDesc *)epos->bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 aed->lengthAllocDescs =
Marcin Slusarz4b111112008-02-08 04:20:36 -08001769 cpu_to_le32(le32_to_cpu(
1770 aed->lengthAllocDescs) + adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001771 } else {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001772 UDF_I(inode)->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 mark_inode_dirty(inode);
1774 }
1775 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001776 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001778 epos->block.logicalBlockNum, sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 else
1780 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001781 epos->block.logicalBlockNum, sizeof(tag));
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001782 switch (UDF_I(inode)->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001783 case ICBTAG_FLAG_AD_SHORT:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001784 sad = (short_ad *)sptr;
1785 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1786 inode->i_sb->s_blocksize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001787 sad->extPosition =
1788 cpu_to_le32(epos->block.logicalBlockNum);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001789 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001790 case ICBTAG_FLAG_AD_LONG:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001791 lad = (long_ad *)sptr;
1792 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1793 inode->i_sb->s_blocksize);
1794 lad->extLocation = cpu_to_lelb(epos->block);
1795 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1796 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001798 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001799 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001800 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -07001801 udf_update_tag(epos->bh->b_data, loffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001803 udf_update_tag(epos->bh->b_data,
1804 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001805 mark_buffer_dirty_inode(epos->bh, inode);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001806 brelse(epos->bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001807 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001809 }
Jan Karaff116fc2007-05-08 00:35:14 -07001810 epos->bh = nbh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812
Jan Karaff116fc2007-05-08 00:35:14 -07001813 etype = udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001815 if (!epos->bh) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001816 UDF_I(inode)->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001818 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001819 aed = (struct allocExtDesc *)epos->bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 aed->lengthAllocDescs =
Marcin Slusarz4b111112008-02-08 04:20:36 -08001821 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) +
1822 adsize);
1823 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1824 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1825 udf_update_tag(epos->bh->b_data,
1826 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001828 udf_update_tag(epos->bh->b_data,
1829 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001830 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
1832
1833 return etype;
1834}
1835
Marcin Slusarz4b111112008-02-08 04:20:36 -08001836int8_t udf_write_aext(struct inode *inode, struct extent_position *epos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001837 kernel_lb_addr eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
1839 int adsize;
1840 uint8_t *ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001841 short_ad *sad;
1842 long_ad *lad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Jan Karaff116fc2007-05-08 00:35:14 -07001844 if (!epos->bh)
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001845 ptr = UDF_I(inode)->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001846 udf_file_entry_alloc_offset(inode) +
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001847 UDF_I(inode)->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 else
Jan Karaff116fc2007-05-08 00:35:14 -07001849 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001851 switch (UDF_I(inode)->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001852 case ICBTAG_FLAG_AD_SHORT:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001853 sad = (short_ad *)ptr;
1854 sad->extLength = cpu_to_le32(elen);
1855 sad->extPosition = cpu_to_le32(eloc.logicalBlockNum);
1856 adsize = sizeof(short_ad);
1857 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001858 case ICBTAG_FLAG_AD_LONG:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001859 lad = (long_ad *)ptr;
1860 lad->extLength = cpu_to_le32(elen);
1861 lad->extLocation = cpu_to_lelb(eloc);
1862 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1863 adsize = sizeof(long_ad);
1864 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001865 default:
1866 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001869 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001870 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001871 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001872 struct allocExtDesc *aed =
1873 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07001874 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001875 le32_to_cpu(aed->lengthAllocDescs) +
1876 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Jan Karaff116fc2007-05-08 00:35:14 -07001878 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001879 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07001884 epos->offset += adsize;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return (elen >> 30);
1887}
1888
Marcin Slusarz4b111112008-02-08 04:20:36 -08001889int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
1890 kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
1892 int8_t etype;
1893
Jan Karaff116fc2007-05-08 00:35:14 -07001894 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001895 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001896 int block;
Jan Karaff116fc2007-05-08 00:35:14 -07001897 epos->block = *eloc;
1898 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001899 brelse(epos->bh);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001900 block = udf_get_lb_pblock(inode->i_sb, epos->block, 0);
1901 epos->bh = udf_tread(inode->i_sb, block);
1902 if (!epos->bh) {
1903 udf_debug("reading block %d failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return -1;
1905 }
1906 }
1907
1908 return etype;
1909}
1910
Marcin Slusarz4b111112008-02-08 04:20:36 -08001911int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
1912 kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 int alen;
1915 int8_t etype;
1916 uint8_t *ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001917 short_ad *sad;
1918 long_ad *lad;
1919
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001920 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001921 if (!epos->offset)
1922 epos->offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001923 ptr = UDF_I(inode)->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001924 udf_file_entry_alloc_offset(inode) +
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001925 UDF_I(inode)->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001926 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001927 UDF_I(inode)->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001928 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001929 if (!epos->offset)
1930 epos->offset = sizeof(struct allocExtDesc);
1931 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001932 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08001933 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
1934 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 }
1936
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001937 switch (UDF_I(inode)->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001938 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001939 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
1940 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001942 etype = le32_to_cpu(sad->extLength) >> 30;
1943 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001944 eloc->partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001945 UDF_I(inode)->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001946 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
1947 break;
1948 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001949 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
1950 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001951 return -1;
1952 etype = le32_to_cpu(lad->extLength) >> 30;
1953 *eloc = lelb_to_cpu(lad->extLocation);
1954 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
1955 break;
1956 default:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001957 udf_debug("alloc_type = %d unsupported\n",
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001958 UDF_I(inode)->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001959 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961
1962 return etype;
1963}
1964
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001965static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
1966 kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
1968 kernel_lb_addr oeloc;
1969 uint32_t oelen;
1970 int8_t etype;
1971
Jan Karaff116fc2007-05-08 00:35:14 -07001972 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001973 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001975 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Jan Karaff116fc2007-05-08 00:35:14 -07001976 udf_write_aext(inode, &epos, neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 neloc = oeloc;
1978 nelen = (etype << 30) | oelen;
1979 }
Jan Karaff116fc2007-05-08 00:35:14 -07001980 udf_add_aext(inode, &epos, neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001981 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return (nelen >> 30);
1984}
1985
Marcin Slusarz4b111112008-02-08 04:20:36 -08001986int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001987 kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Jan Karaff116fc2007-05-08 00:35:14 -07001989 struct extent_position oepos;
1990 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 int8_t etype;
1992 struct allocExtDesc *aed;
1993
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001994 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001995 get_bh(epos.bh);
1996 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
1998
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001999 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 adsize = sizeof(short_ad);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002001 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 adsize = sizeof(long_ad);
2003 else
2004 adsize = 0;
2005
Jan Karaff116fc2007-05-08 00:35:14 -07002006 oepos = epos;
2007 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return -1;
2009
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002010 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Jan Karaff116fc2007-05-08 00:35:14 -07002011 udf_write_aext(inode, &oepos, eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002012 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002013 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07002014 brelse(oepos.bh);
2015 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07002016 oepos.bh = epos.bh;
2017 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
2019 }
2020 memset(&eloc, 0x00, sizeof(kernel_lb_addr));
2021 elen = 0;
2022
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002023 if (epos.bh != oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002024 udf_free_blocks(inode->i_sb, inode, epos.block, 0, 1);
2025 udf_write_aext(inode, &oepos, eloc, elen, 1);
2026 udf_write_aext(inode, &oepos, eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002027 if (!oepos.bh) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002028 UDF_I(inode)->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002030 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002031 aed = (struct allocExtDesc *)oepos.bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 aed->lengthAllocDescs =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002033 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) -
2034 (2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002035 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002036 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002037 udf_update_tag(oepos.bh->b_data,
2038 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002040 udf_update_tag(oepos.bh->b_data,
2041 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002042 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002044 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002045 udf_write_aext(inode, &oepos, eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002046 if (!oepos.bh) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002047 UDF_I(inode)->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002049 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002050 aed = (struct allocExtDesc *)oepos.bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 aed->lengthAllocDescs =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002052 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) -
2053 adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002054 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002055 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002056 udf_update_tag(oepos.bh->b_data,
2057 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002059 udf_update_tag(oepos.bh->b_data,
2060 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002061 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
2063 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002064
Jan Kara3bf25cb2007-05-08 00:35:16 -07002065 brelse(epos.bh);
2066 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return (elen >> 30);
2069}
2070
Marcin Slusarz4b111112008-02-08 04:20:36 -08002071int8_t inode_bmap(struct inode *inode, sector_t block,
2072 struct extent_position *pos, kernel_lb_addr *eloc,
2073 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002075 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002076 loff_t lbcount = 0, bcount =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002077 (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 int8_t etype;
2079
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002080 if (block < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 printk(KERN_ERR "udf: inode_bmap: block < 0\n");
2082 return -1;
2083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Jan Karaff116fc2007-05-08 00:35:14 -07002085 pos->offset = 0;
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002086 pos->block = UDF_I(inode)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002087 pos->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 *elen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002090 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002091 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2092 if (etype == -1) {
2093 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002094 UDF_I(inode)->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 return -1;
2096 }
2097 lbcount += *elen;
2098 } while (lbcount <= bcount);
2099
Marcin Slusarz4b111112008-02-08 04:20:36 -08002100 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 return etype;
2103}
2104
Jan Kara60448b12007-05-08 00:35:13 -07002105long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
Jan Karaff116fc2007-05-08 00:35:14 -07002107 kernel_lb_addr eloc;
2108 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002109 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002110 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 int ret;
2112
2113 lock_kernel();
2114
Marcin Slusarz4b111112008-02-08 04:20:36 -08002115 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2116 (EXT_RECORDED_ALLOCATED >> 30))
Jan Kara60448b12007-05-08 00:35:13 -07002117 ret = udf_get_lb_pblock(inode->i_sb, eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 else
2119 ret = 0;
2120
2121 unlock_kernel();
Jan Kara3bf25cb2007-05-08 00:35:16 -07002122 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2125 return udf_fixed_to_variable(ret);
2126 else
2127 return ret;
2128}