blob: 859389a3832bdbd2602373758152c209662e6cbb [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>
Bob Copelandf845fce2008-04-17 09:47:48 +020040#include <linux/crc-itu-t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "udf_i.h"
43#include "udf_sb.h"
44
45MODULE_AUTHOR("Ben Fennema");
46MODULE_DESCRIPTION("Universal Disk Format Filesystem");
47MODULE_LICENSE("GPL");
48
49#define EXTENT_MERGE_SIZE 5
50
51static mode_t udf_convert_permissions(struct fileEntry *);
52static int udf_update_inode(struct inode *, int);
53static void udf_fill_inode(struct inode *, struct buffer_head *);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -070054static int udf_alloc_i_data(struct inode *inode, size_t size);
Jan Kara60448b12007-05-08 00:35:13 -070055static struct buffer_head *inode_getblk(struct inode *, sector_t, int *,
Marcin Slusarz1ed16172008-02-08 04:20:48 -080056 sector_t *, int *);
Jan Karaff116fc2007-05-08 00:35:14 -070057static int8_t udf_insert_aext(struct inode *, struct extent_position,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020058 struct kernel_lb_addr, uint32_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static void udf_split_extents(struct inode *, int *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020060 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static void udf_prealloc_extents(struct inode *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020062 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static void udf_merge_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020064 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void udf_update_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020066 struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070067 struct extent_position *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
69
Christoph Hellwigb1e32122008-02-22 12:38:48 +010070
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070071void udf_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Mark Fashehfef26652005-09-09 13:01:31 -070073 truncate_inode_pages(&inode->i_data, 0);
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (is_bad_inode(inode))
76 goto no_delete;
77
78 inode->i_size = 0;
79 udf_truncate(inode);
80 lock_kernel();
81
82 udf_update_inode(inode, IS_SYNC(inode));
83 udf_free_inode(inode);
84
85 unlock_kernel();
86 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070087
88no_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 clear_inode(inode);
90}
91
Jan Kara74584ae2007-06-16 10:16:14 -070092/*
Jan Kara81056dd2009-07-16 18:02:25 +020093 * If we are going to release inode from memory, we truncate last inode extent
94 * to proper length. We could use drop_inode() but it's called under inode_lock
95 * and thus we cannot mark inode dirty there. We use clear_inode() but we have
96 * to make sure to write inode as it's not written automatically.
Jan Kara74584ae2007-06-16 10:16:14 -070097 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098void udf_clear_inode(struct inode *inode)
99{
Jan Kara2c948b32009-12-03 13:39:28 +0100100 struct udf_inode_info *iinfo = UDF_I(inode);
101
102 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
103 inode->i_size != iinfo->i_lenExtents) {
104 printk(KERN_WARNING "UDF-fs (%s): Inode %lu (mode %o) has "
105 "inode size %llu different from extent lenght %llu. "
106 "Filesystem need not be standards compliant.\n",
107 inode->i_sb->s_id, inode->i_ino, inode->i_mode,
108 (unsigned long long)inode->i_size,
109 (unsigned long long)iinfo->i_lenExtents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Christoph Hellwig257ba152010-03-03 09:05:04 -0500111
112 vfs_dq_drop(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800113 kfree(iinfo->i_ext.i_data);
114 iinfo->i_ext.i_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117static int udf_writepage(struct page *page, struct writeback_control *wbc)
118{
119 return block_write_full_page(page, udf_get_block, wbc);
120}
121
122static int udf_readpage(struct file *file, struct page *page)
123{
124 return block_read_full_page(page, udf_get_block);
125}
126
Nick Pigginbe021ee2007-10-16 01:25:20 -0700127static int udf_write_begin(struct file *file, struct address_space *mapping,
128 loff_t pos, unsigned len, unsigned flags,
129 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Nick Pigginbe021ee2007-10-16 01:25:20 -0700131 *pagep = NULL;
132 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
133 udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136static sector_t udf_bmap(struct address_space *mapping, sector_t block)
137{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700138 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700141const struct address_space_operations udf_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700142 .readpage = udf_readpage,
143 .writepage = udf_writepage,
144 .sync_page = block_sync_page,
Nick Pigginbe021ee2007-10-16 01:25:20 -0700145 .write_begin = udf_write_begin,
146 .write_end = generic_write_end,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700147 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700150void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 struct page *page;
153 char *kaddr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800154 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct writeback_control udf_wbc = {
156 .sync_mode = WB_SYNC_NONE,
157 .nr_to_write = 1,
158 };
159
160 /* from now on we have normal address_space methods */
161 inode->i_data.a_ops = &udf_aops;
162
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800163 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800165 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800167 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 mark_inode_dirty(inode);
169 return;
170 }
171
172 page = grab_cache_page(inode->i_mapping, 0);
Matt Mackallcd7619d2005-05-01 08:59:01 -0700173 BUG_ON(!PageLocked(page));
174
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700175 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 kaddr = kmap(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800177 memset(kaddr + iinfo->i_lenAlloc, 0x00,
178 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
179 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
180 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 flush_dcache_page(page);
182 SetPageUptodate(page);
183 kunmap(page);
184 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800185 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
186 iinfo->i_lenAlloc);
187 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800189 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800191 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 inode->i_data.a_ops->writepage(page, &udf_wbc);
194 page_cache_release(page);
195
196 mark_inode_dirty(inode);
197}
198
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700199struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
200 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700203 struct buffer_head *dbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200204 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700206 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 struct udf_fileident_bh sfibh, dfibh;
Jan Karaaf793292008-02-08 04:20:50 -0800209 loff_t f_pos = udf_ext0_offset(inode);
210 int size = udf_ext0_offset(inode) + inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800212 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
215 alloctype = ICBTAG_FLAG_AD_SHORT;
216 else
217 alloctype = ICBTAG_FLAG_AD_LONG;
218
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700219 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800220 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 mark_inode_dirty(inode);
222 return NULL;
223 }
224
225 /* alloc block, and copy data to it */
226 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800227 iinfo->i_location.partitionReferenceNum,
228 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (!(*block))
230 return NULL;
231 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800232 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800233 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (!newblock)
235 return NULL;
236 dbh = udf_tgetblk(inode->i_sb, newblock);
237 if (!dbh)
238 return NULL;
239 lock_buffer(dbh);
240 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
241 set_buffer_uptodate(dbh);
242 unlock_buffer(dbh);
243 mark_buffer_dirty_inode(dbh, inode);
244
Marcin Slusarz4b111112008-02-08 04:20:36 -0800245 sfibh.soffset = sfibh.eoffset =
Jan Karaaf793292008-02-08 04:20:50 -0800246 f_pos & (inode->i_sb->s_blocksize - 1);
Jan Karaff116fc2007-05-08 00:35:14 -0700247 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 dfibh.soffset = dfibh.eoffset = 0;
249 dfibh.sbh = dfibh.ebh = dbh;
Jan Karaaf793292008-02-08 04:20:50 -0800250 while (f_pos < size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800251 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800252 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
253 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700254 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700255 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return NULL;
257 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800258 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 sfi->descTag.tagLocation = cpu_to_le32(*block);
260 dfibh.soffset = dfibh.eoffset;
261 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
262 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
263 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800264 sfi->fileIdent +
265 le16_to_cpu(sfi->lengthOfImpUse))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800266 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700267 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return NULL;
269 }
270 }
271 mark_buffer_dirty_inode(dbh, inode);
272
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800273 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
274 iinfo->i_lenAlloc);
275 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800277 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800278 iinfo->i_location.partitionReferenceNum;
Jan Kara2c948b32009-12-03 13:39:28 +0100279 iinfo->i_lenExtents = inode->i_size;
Jan Karaff116fc2007-05-08 00:35:14 -0700280 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800281 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700282 epos.offset = udf_file_entry_alloc_offset(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100283 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* UniqueID stuff */
285
Jan Kara3bf25cb2007-05-08 00:35:16 -0700286 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 mark_inode_dirty(inode);
288 return dbh;
289}
290
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700291static int udf_get_block(struct inode *inode, sector_t block,
292 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 int err, new;
295 struct buffer_head *bh;
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800296 sector_t phys = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800297 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700299 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 phys = udf_block_map(inode, block);
301 if (phys)
302 map_bh(bh_result, inode->i_sb, phys);
303 return 0;
304 }
305
306 err = -EIO;
307 new = 0;
308 bh = NULL;
309
310 lock_kernel();
311
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800312 iinfo = UDF_I(inode);
313 if (block == iinfo->i_next_alloc_block + 1) {
314 iinfo->i_next_alloc_block++;
315 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317
318 err = 0;
319
320 bh = inode_getblk(inode, block, &err, &phys, &new);
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200321 BUG_ON(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (err)
323 goto abort;
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200324 BUG_ON(!phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (new)
327 set_buffer_new(bh_result);
328 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700329
330abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 unlock_kernel();
332 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700335static struct buffer_head *udf_getblk(struct inode *inode, long block,
336 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700338 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct buffer_head dummy;
340
341 dummy.b_state = 0;
342 dummy.b_blocknr = -1000;
343 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700344 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700346 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 lock_buffer(bh);
348 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
349 set_buffer_uptodate(bh);
350 unlock_buffer(bh);
351 mark_buffer_dirty_inode(bh, inode);
352 }
353 return bh;
354 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return NULL;
357}
358
Jan Kara31170b62007-05-08 00:35:21 -0700359/* Extend the file by 'blocks' blocks, return the number of extents added */
360int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200361 struct kernel_long_ad *last_ext, sector_t blocks)
Jan Kara31170b62007-05-08 00:35:21 -0700362{
363 sector_t add;
364 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
365 struct super_block *sb = inode->i_sb;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200366 struct kernel_lb_addr prealloc_loc = {};
Jan Kara31170b62007-05-08 00:35:21 -0700367 int prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800368 struct udf_inode_info *iinfo;
Jan Kara31170b62007-05-08 00:35:21 -0700369
370 /* The previous extent is fake and we should not extend by anything
371 * - there's nothing to do... */
372 if (!blocks && fake)
373 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700374
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800375 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700376 /* Round the last extent up to a multiple of block size */
377 if (last_ext->extLength & (sb->s_blocksize - 1)) {
378 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700379 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
380 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
381 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800382 iinfo->i_lenExtents =
383 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700384 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700385 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700386
Jan Kara31170b62007-05-08 00:35:21 -0700387 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800388 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
389 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700390 /* Save the extent so that we can reattach it to the end */
391 prealloc_loc = last_ext->extLocation;
392 prealloc_len = last_ext->extLength;
393 /* Mark the extent as a hole */
394 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700395 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700396 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800397 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700398 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700399
Jan Kara31170b62007-05-08 00:35:21 -0700400 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800401 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
402 EXT_NOT_RECORDED_NOT_ALLOCATED) {
403 add = ((1 << 30) - sb->s_blocksize -
404 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
405 sb->s_blocksize_bits;
Jan Kara31170b62007-05-08 00:35:21 -0700406 if (add > blocks)
407 add = blocks;
408 blocks -= add;
409 last_ext->extLength += add << sb->s_blocksize_bits;
410 }
411
412 if (fake) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200413 udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700414 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700415 count++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800416 } else
Pekka Enberg97e961f2008-10-15 12:29:03 +0200417 udf_write_aext(inode, last_pos, &last_ext->extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800418 last_ext->extLength, 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700419
Jan Kara31170b62007-05-08 00:35:21 -0700420 /* Managed to do everything necessary? */
421 if (!blocks)
422 goto out;
423
424 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
425 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800426 last_ext->extLocation.partitionReferenceNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700427 add = (1 << (30-sb->s_blocksize_bits)) - 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800428 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
429 (add << sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700430
Jan Kara31170b62007-05-08 00:35:21 -0700431 /* Create enough extents to cover the whole hole */
432 while (blocks > add) {
433 blocks -= add;
Pekka Enberg97e961f2008-10-15 12:29:03 +0200434 if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700435 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700436 return -1;
437 count++;
438 }
439 if (blocks) {
440 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700441 (blocks << sb->s_blocksize_bits);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200442 if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700443 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700444 return -1;
445 count++;
446 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700447
448out:
Jan Kara31170b62007-05-08 00:35:21 -0700449 /* Do we have some preallocated blocks saved? */
450 if (prealloc_len) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200451 if (udf_add_aext(inode, last_pos, &prealloc_loc,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800452 prealloc_len, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700453 return -1;
454 last_ext->extLocation = prealloc_loc;
455 last_ext->extLength = prealloc_len;
456 count++;
457 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700458
Jan Kara31170b62007-05-08 00:35:21 -0700459 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800460 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200461 last_pos->offset -= sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800462 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200463 last_pos->offset -= sizeof(struct long_ad);
Jan Kara31170b62007-05-08 00:35:21 -0700464 else
465 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700466
Jan Kara31170b62007-05-08 00:35:21 -0700467 return count;
468}
469
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700470static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800471 int *err, sector_t *phys, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Jan Kara31170b62007-05-08 00:35:21 -0700473 static sector_t last_block;
Jan Karaff116fc2007-05-08 00:35:14 -0700474 struct buffer_head *result = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200475 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700476 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700478 uint32_t elen = 0, tmpelen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200479 struct kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700481 loff_t lbcount = 0, b_off = 0;
482 uint32_t newblocknum, newblock;
483 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800485 struct udf_inode_info *iinfo = UDF_I(inode);
486 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700487 int lastblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Jan Karaff116fc2007-05-08 00:35:14 -0700489 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800490 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700491 prev_epos.bh = NULL;
492 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700493 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700496 alternate between laarr[0] and laarr[1] for locations of the
497 current extent, and the previous extent */
498 do {
499 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700500 brelse(prev_epos.bh);
501 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700502 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700504 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700505 brelse(cur_epos.bh);
506 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700507 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509
510 lbcount += elen;
511
Jan Karaff116fc2007-05-08 00:35:14 -0700512 prev_epos.block = cur_epos.block;
513 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Jan Karaff116fc2007-05-08 00:35:14 -0700515 prev_epos.offset = cur_epos.offset;
516 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Marcin Slusarz4b111112008-02-08 04:20:36 -0800518 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
519 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 break;
521
522 c = !c;
523
524 laarr[c].extLength = (etype << 30) | elen;
525 laarr[c].extLocation = eloc;
526
527 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
528 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700529 ((elen + inode->i_sb->s_blocksize - 1) >>
530 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700532 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 } while (lbcount + elen <= b_off);
534
535 b_off -= lbcount;
536 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700537 /*
538 * Move prev_epos and cur_epos into indirect extent if we are at
539 * the pointer to it
540 */
541 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
542 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700545 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700547 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
548 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700550 ((elen + inode->i_sb->s_blocksize - 1) &
551 ~(inode->i_sb->s_blocksize - 1));
Pekka Enberg97e961f2008-10-15 12:29:03 +0200552 etype = udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700554 brelse(prev_epos.bh);
555 brelse(cur_epos.bh);
556 brelse(next_epos.bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200557 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 *phys = newblock;
559 return NULL;
560 }
561
Jan Kara31170b62007-05-08 00:35:21 -0700562 last_block = block;
563 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700564 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700565 int ret;
566
567 if (count) {
568 if (c)
569 laarr[0] = laarr[1];
570 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700571 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700572 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800573 memset(&laarr[0].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200574 sizeof(struct kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700575 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800576 /* Will udf_extend_file() create real extent from
577 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700578 startnum = (offset > 0);
579 }
580 /* Create extents for the hole between EOF and offset */
581 ret = udf_extend_file(inode, &prev_epos, laarr, offset);
582 if (ret == -1) {
583 brelse(prev_epos.bh);
584 brelse(cur_epos.bh);
585 brelse(next_epos.bh);
586 /* We don't really know the error here so we just make
587 * something up */
588 *err = -ENOSPC;
589 return NULL;
590 }
591 c = 0;
592 offset = 0;
593 count += ret;
594 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800595 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
596 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700597 /* Is there any real extent? - otherwise we overwrite
598 * the fake one... */
599 if (count)
600 c = !c;
601 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700602 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800603 memset(&laarr[c].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200604 sizeof(struct kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700605 count++;
606 endnum++;
Jan Kara31170b62007-05-08 00:35:21 -0700607 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700608 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700610 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 endnum = startnum = ((count > 2) ? 2 : count);
612
Marcin Slusarz4b111112008-02-08 04:20:36 -0800613 /* if the current extent is in position 0,
614 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700615 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700616 laarr[2] = laarr[0];
617 laarr[0] = laarr[1];
618 laarr[1] = laarr[2];
619 c = 1;
620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Marcin Slusarz4b111112008-02-08 04:20:36 -0800622 /* if the current block is located in an extent,
623 read the next extent */
624 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
625 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700626 laarr[c + 1].extLength = (etype << 30) | elen;
627 laarr[c + 1].extLocation = eloc;
628 count++;
629 startnum++;
630 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800631 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 lastblock = 1;
633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700636 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800637 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800639 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800640 if (iinfo->i_next_alloc_block == block)
641 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700643 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800644 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800645 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647
Marcin Slusarz4b111112008-02-08 04:20:36 -0800648 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800649 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800650 goal, err);
651 if (!newblocknum) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700652 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 *err = -ENOSPC;
654 return NULL;
655 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800656 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
Marcin Slusarz4b111112008-02-08 04:20:36 -0800659 /* if the extent the requsted block is located in contains multiple
660 * blocks, split the extent into at most three extents. blocks prior
661 * to requested block, requested block, and blocks after requested
662 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
664
665#ifdef UDF_PREALLOCATE
Jan Kara81056dd2009-07-16 18:02:25 +0200666 /* We preallocate blocks only for regular files. It also makes sense
667 * for directories but there's a problem when to drop the
668 * preallocation. We might use some delayed work for that but I feel
669 * it's overengineering for a filesystem like UDF. */
670 if (S_ISREG(inode->i_mode))
671 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672#endif
673
674 /* merge any continuous blocks in laarr */
675 udf_merge_extents(inode, laarr, &endnum);
676
677 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700678 * of extents is greater than the old number, and deleting extents if
679 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700680 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Jan Kara3bf25cb2007-05-08 00:35:16 -0700682 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Marcin Slusarz4b111112008-02-08 04:20:36 -0800684 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800685 iinfo->i_location.partitionReferenceNum, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800686 if (!newblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 *phys = newblock;
689 *err = 0;
690 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800691 iinfo->i_next_alloc_block = block;
692 iinfo->i_next_alloc_goal = newblocknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 inode->i_ctime = current_fs_time(inode->i_sb);
694
695 if (IS_SYNC(inode))
696 udf_sync_inode(inode);
697 else
698 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return result;
701}
702
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700703static void udf_split_extents(struct inode *inode, int *c, int offset,
704 int newblocknum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200705 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700706 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800708 unsigned long blocksize = inode->i_sb->s_blocksize;
709 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800712 (laarr[*c].extLength >> 30) ==
713 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 int curr = *c;
715 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800716 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 int8_t etype = (laarr[curr].extLength >> 30);
718
Marcin Slusarz4b111112008-02-08 04:20:36 -0800719 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700720 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800721 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700722 laarr[curr + 2] = laarr[curr + 1];
723 laarr[curr + 1] = laarr[curr];
724 } else {
725 laarr[curr + 3] = laarr[curr + 1];
726 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700729 if (offset) {
730 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800731 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200732 &laarr[curr].extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800733 0, offset);
734 laarr[curr].extLength =
735 EXT_NOT_RECORDED_NOT_ALLOCATED |
736 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800738 laarr[curr].extLocation.
739 partitionReferenceNum = 0;
740 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800742 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700743 curr++;
744 (*c)++;
745 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 laarr[curr].extLocation.logicalBlockNum = newblocknum;
749 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
750 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800751 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800753 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700754 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700756 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800758 laarr[curr].extLocation.logicalBlockNum +=
759 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700760 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800761 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700762 curr++;
763 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765 }
766}
767
768static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200769 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700770 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 int start, length = 0, currlength = 0, i;
773
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700774 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (!lastblock)
776 return;
777 else
778 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700779 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800780 if ((laarr[c + 1].extLength >> 30) ==
781 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700782 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800783 length = currlength =
784 (((laarr[c + 1].extLength &
785 UDF_EXTENT_LENGTH_MASK) +
786 inode->i_sb->s_blocksize - 1) >>
787 inode->i_sb->s_blocksize_bits);
788 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 start = c;
790 }
791
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700792 for (i = start + 1; i <= *endnum; i++) {
793 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (lastblock)
795 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800796 } else if ((laarr[i].extLength >> 30) ==
797 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
798 length += (((laarr[i].extLength &
799 UDF_EXTENT_LENGTH_MASK) +
800 inode->i_sb->s_blocksize - 1) >>
801 inode->i_sb->s_blocksize_bits);
802 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 break;
804 }
805
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700806 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700808 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800809 inode->i_sb->s_blocksize - 1) >>
810 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800812 laarr[start].extLocation.partitionReferenceNum,
813 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
814 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
815 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700816 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800817 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800819 (numalloc <<
820 inode->i_sb->s_blocksize_bits);
821 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700822 memmove(&laarr[c + 2], &laarr[c + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200823 sizeof(struct long_ad) * (*endnum - (c + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700824 (*endnum)++;
825 laarr[c + 1].extLocation.logicalBlockNum = next;
826 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800827 laarr[c].extLocation.
828 partitionReferenceNum;
829 laarr[c + 1].extLength =
830 EXT_NOT_RECORDED_ALLOCATED |
831 (numalloc <<
832 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700833 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700836 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800837 int elen = ((laarr[i].extLength &
838 UDF_EXTENT_LENGTH_MASK) +
839 inode->i_sb->s_blocksize - 1) >>
840 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700842 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800844 (numalloc <<
845 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700847 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700849 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800850 memmove(&laarr[i],
851 &laarr[i + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200852 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -0800853 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700854 i--;
855 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800858 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800859 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861 }
862}
863
864static void udf_merge_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200865 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700866 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800869 unsigned long blocksize = inode->i_sb->s_blocksize;
870 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700872 for (i = 0; i < (*endnum - 1); i++) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200873 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
874 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Marcin Slusarz4b111112008-02-08 04:20:36 -0800876 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
877 (((li->extLength >> 30) ==
878 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
879 ((lip1->extLocation.logicalBlockNum -
880 li->extLocation.logicalBlockNum) ==
881 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
882 blocksize - 1) >> blocksize_bits)))) {
883
884 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
885 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
886 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
887 lip1->extLength = (lip1->extLength -
888 (li->extLength &
889 UDF_EXTENT_LENGTH_MASK) +
890 UDF_EXTENT_LENGTH_MASK) &
891 ~(blocksize - 1);
892 li->extLength = (li->extLength &
893 UDF_EXTENT_FLAG_MASK) +
894 (UDF_EXTENT_LENGTH_MASK + 1) -
895 blocksize;
896 lip1->extLocation.logicalBlockNum =
897 li->extLocation.logicalBlockNum +
898 ((li->extLength &
899 UDF_EXTENT_LENGTH_MASK) >>
900 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700901 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800902 li->extLength = lip1->extLength +
903 (((li->extLength &
904 UDF_EXTENT_LENGTH_MASK) +
905 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700906 if (*endnum > (i + 2))
907 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200908 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -0800909 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700910 i--;
911 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800913 } else if (((li->extLength >> 30) ==
914 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
915 ((lip1->extLength >> 30) ==
916 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200917 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800918 ((li->extLength &
919 UDF_EXTENT_LENGTH_MASK) +
920 blocksize - 1) >> blocksize_bits);
921 li->extLocation.logicalBlockNum = 0;
922 li->extLocation.partitionReferenceNum = 0;
923
924 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
925 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
926 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
927 lip1->extLength = (lip1->extLength -
928 (li->extLength &
929 UDF_EXTENT_LENGTH_MASK) +
930 UDF_EXTENT_LENGTH_MASK) &
931 ~(blocksize - 1);
932 li->extLength = (li->extLength &
933 UDF_EXTENT_FLAG_MASK) +
934 (UDF_EXTENT_LENGTH_MASK + 1) -
935 blocksize;
936 } else {
937 li->extLength = lip1->extLength +
938 (((li->extLength &
939 UDF_EXTENT_LENGTH_MASK) +
940 blocksize - 1) & ~(blocksize - 1));
941 if (*endnum > (i + 2))
942 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200943 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -0800944 (*endnum - (i + 2)));
945 i--;
946 (*endnum)--;
947 }
948 } else if ((li->extLength >> 30) ==
949 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
950 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200951 &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800952 ((li->extLength &
953 UDF_EXTENT_LENGTH_MASK) +
954 blocksize - 1) >> blocksize_bits);
955 li->extLocation.logicalBlockNum = 0;
956 li->extLocation.partitionReferenceNum = 0;
957 li->extLength = (li->extLength &
958 UDF_EXTENT_LENGTH_MASK) |
959 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961 }
962}
963
964static void udf_update_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200965 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700966 int startnum, int endnum,
967 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 int start = 0, i;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200970 struct kernel_lb_addr tmploc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 uint32_t tmplen;
972
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700973 if (startnum > endnum) {
974 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -0700975 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700976 laarr[i].extLength);
977 } else if (startnum < endnum) {
978 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -0700979 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700980 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -0700981 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700982 &laarr[i].extLength, 1);
983 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985 }
986
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700987 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -0700988 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200989 udf_write_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700990 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992}
993
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700994struct buffer_head *udf_bread(struct inode *inode, int block,
995 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700997 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 bh = udf_getblk(inode, block, create, err);
1000 if (!bh)
1001 return NULL;
1002
1003 if (buffer_uptodate(bh))
1004 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 ll_rw_block(READ, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 wait_on_buffer(bh);
1009 if (buffer_uptodate(bh))
1010 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 brelse(bh);
1013 *err = -EIO;
1014 return NULL;
1015}
1016
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001017void udf_truncate(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 int offset;
1020 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001021 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001024 S_ISLNK(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return;
1026 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1027 return;
1028
1029 lock_kernel();
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001030 iinfo = UDF_I(inode);
1031 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001032 if (inode->i_sb->s_blocksize <
1033 (udf_file_entry_alloc_offset(inode) +
1034 inode->i_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 udf_expand_file_adinicb(inode, inode->i_size, &err);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001036 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1037 inode->i_size = iinfo->i_lenAlloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 unlock_kernel();
1039 return;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001040 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 udf_truncate_extents(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001042 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001044 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + offset,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001045 0x00, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001046 offset - udf_file_entry_alloc_offset(inode));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001047 iinfo->i_lenAlloc = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001049 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001050 block_truncate_page(inode->i_mapping, inode->i_size,
1051 udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 udf_truncate_extents(inode);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1056 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001057 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 else
1059 mark_inode_dirty(inode);
1060 unlock_kernel();
1061}
1062
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001063static void __udf_read_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 struct buffer_head *bh = NULL;
1066 struct fileEntry *fe;
1067 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001068 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 /*
1071 * Set defaults, but the inode is still incomplete!
1072 * Note: get_new_inode() sets the following on a new inode:
1073 * i_sb = sb
1074 * i_no = ino
1075 * i_flags = sb->s_flags
1076 * i_state = 0
1077 * clean_inode(): zero fills and sets
1078 * i_count = 1
1079 * i_nlink = 1
1080 * i_op = NULL;
1081 */
Pekka Enberg97e961f2008-10-15 12:29:03 +02001082 bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001083 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001085 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 make_bad_inode(inode);
1087 return;
1088 }
1089
1090 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001091 ident != TAG_IDENT_USE) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001092 printk(KERN_ERR "udf: udf_read_inode(ino %ld) "
1093 "failed ident=%d\n", inode->i_ino, ident);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001094 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 make_bad_inode(inode);
1096 return;
1097 }
1098
1099 fe = (struct fileEntry *)bh->b_data;
1100
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001101 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001102 struct buffer_head *ibh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Pekka Enberg97e961f2008-10-15 12:29:03 +02001104 ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001105 &ident);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001106 if (ident == TAG_IDENT_IE && ibh) {
1107 struct buffer_head *nbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001108 struct kernel_lb_addr loc;
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001109 struct indirectEntry *ie;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001110
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001111 ie = (struct indirectEntry *)ibh->b_data;
1112 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001113
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001114 if (ie->indirectICB.extLength &&
Pekka Enberg97e961f2008-10-15 12:29:03 +02001115 (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001116 &ident))) {
1117 if (ident == TAG_IDENT_FE ||
1118 ident == TAG_IDENT_EFE) {
1119 memcpy(&iinfo->i_location,
1120 &loc,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001121 sizeof(struct kernel_lb_addr));
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001122 brelse(bh);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001123 brelse(ibh);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001124 brelse(nbh);
1125 __udf_read_inode(inode);
1126 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001127 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001128 brelse(nbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001130 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001131 brelse(ibh);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001132 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 printk(KERN_ERR "udf: unsupported strategy type: %d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001134 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001135 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 make_bad_inode(inode);
1137 return;
1138 }
1139 udf_fill_inode(inode, bh);
Jan Kara31170b62007-05-08 00:35:21 -07001140
Jan Kara3bf25cb2007-05-08 00:35:16 -07001141 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
1144static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1145{
1146 struct fileEntry *fe;
1147 struct extendedFileEntry *efe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 int offset;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001149 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001150 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 fe = (struct fileEntry *)bh->b_data;
1153 efe = (struct extendedFileEntry *)bh->b_data;
1154
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001155 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001156 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001157 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001158 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001160 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001161 ICBTAG_FLAG_AD_MASK;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001162 iinfo->i_unique = 0;
1163 iinfo->i_lenEAttr = 0;
1164 iinfo->i_lenExtents = 0;
1165 iinfo->i_lenAlloc = 0;
1166 iinfo->i_next_alloc_block = 0;
1167 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001168 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001169 iinfo->i_efe = 1;
1170 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001171 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1172 sizeof(struct extendedFileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001173 make_bad_inode(inode);
1174 return;
1175 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001176 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001177 bh->b_data + sizeof(struct extendedFileEntry),
1178 inode->i_sb->s_blocksize -
1179 sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001180 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001181 iinfo->i_efe = 0;
1182 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001183 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1184 sizeof(struct fileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001185 make_bad_inode(inode);
1186 return;
1187 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001188 memcpy(iinfo->i_ext.i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001189 bh->b_data + sizeof(struct fileEntry),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001190 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001191 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001192 iinfo->i_efe = 0;
1193 iinfo->i_use = 1;
1194 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001195 ((struct unallocSpaceEntry *)bh->b_data)->
1196 lengthAllocDescs);
1197 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1198 sizeof(struct unallocSpaceEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001199 make_bad_inode(inode);
1200 return;
1201 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001202 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001203 bh->b_data + sizeof(struct unallocSpaceEntry),
1204 inode->i_sb->s_blocksize -
1205 sizeof(struct unallocSpaceEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return;
1207 }
1208
1209 inode->i_uid = le32_to_cpu(fe->uid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001210 if (inode->i_uid == -1 ||
1211 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1212 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001213 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 inode->i_gid = le32_to_cpu(fe->gid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001216 if (inode->i_gid == -1 ||
1217 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1218 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001219 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
1222 if (!inode->i_nlink)
1223 inode->i_nlink = 1;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 inode->i_size = le64_to_cpu(fe->informationLength);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001226 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +01001228 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001229 sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +01001230 inode->i_mode = sbi->s_fmode;
1231 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001232 sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +01001233 inode->i_mode = sbi->s_dmode;
1234 else
1235 inode->i_mode = udf_convert_permissions(fe);
1236 inode->i_mode &= ~sbi->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001238 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001240 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Marcin Slusarz56774802008-02-10 11:25:31 +01001242 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001243 inode->i_atime = sbi->s_record_time;
1244
Marcin Slusarz56774802008-02-10 11:25:31 +01001245 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1246 fe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001247 inode->i_mtime = sbi->s_record_time;
1248
Marcin Slusarz56774802008-02-10 11:25:31 +01001249 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001250 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001252 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1253 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1254 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1255 offset = sizeof(struct fileEntry) + iinfo->i_lenEAttr;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001256 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001257 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001258 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Marcin Slusarz56774802008-02-10 11:25:31 +01001260 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001261 inode->i_atime = sbi->s_record_time;
1262
Marcin Slusarz56774802008-02-10 11:25:31 +01001263 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1264 efe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001265 inode->i_mtime = sbi->s_record_time;
1266
Marcin Slusarz56774802008-02-10 11:25:31 +01001267 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001268 iinfo->i_crtime = sbi->s_record_time;
1269
Marcin Slusarz56774802008-02-10 11:25:31 +01001270 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001271 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001273 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1274 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1275 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001276 offset = sizeof(struct extendedFileEntry) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001277 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
1279
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001280 switch (fe->icbTag.fileType) {
1281 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001282 inode->i_op = &udf_dir_inode_operations;
1283 inode->i_fop = &udf_dir_operations;
1284 inode->i_mode |= S_IFDIR;
1285 inc_nlink(inode);
1286 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001287 case ICBTAG_FILE_TYPE_REALTIME:
1288 case ICBTAG_FILE_TYPE_REGULAR:
1289 case ICBTAG_FILE_TYPE_UNDEF:
Jan Kara742e1792008-04-08 01:17:52 +02001290 case ICBTAG_FILE_TYPE_VAT20:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001291 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001292 inode->i_data.a_ops = &udf_adinicb_aops;
1293 else
1294 inode->i_data.a_ops = &udf_aops;
1295 inode->i_op = &udf_file_inode_operations;
1296 inode->i_fop = &udf_file_operations;
1297 inode->i_mode |= S_IFREG;
1298 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001299 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001300 inode->i_mode |= S_IFBLK;
1301 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001302 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001303 inode->i_mode |= S_IFCHR;
1304 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001305 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001306 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1307 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001308 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001309 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1310 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001311 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001312 inode->i_data.a_ops = &udf_symlink_aops;
1313 inode->i_op = &page_symlink_inode_operations;
1314 inode->i_mode = S_IFLNK | S_IRWXUGO;
1315 break;
Jan Karabfb257a2008-04-08 20:37:21 +02001316 case ICBTAG_FILE_TYPE_MAIN:
1317 udf_debug("METADATA FILE-----\n");
1318 break;
1319 case ICBTAG_FILE_TYPE_MIRROR:
1320 udf_debug("METADATA MIRROR FILE-----\n");
1321 break;
1322 case ICBTAG_FILE_TYPE_BITMAP:
1323 udf_debug("METADATA BITMAP FILE-----\n");
1324 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001325 default:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001326 printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
1327 "file type=%d\n", inode->i_ino,
1328 fe->icbTag.fileType);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001329 make_bad_inode(inode);
1330 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001332 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001333 struct deviceSpec *dsea =
1334 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001335 if (dsea) {
1336 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001337 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1338 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001340 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343}
1344
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001345static int udf_alloc_i_data(struct inode *inode, size_t size)
1346{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001347 struct udf_inode_info *iinfo = UDF_I(inode);
1348 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001349
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001350 if (!iinfo->i_ext.i_data) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001351 printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
1352 "no free memory\n", inode->i_ino);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001353 return -ENOMEM;
1354 }
1355
1356 return 0;
1357}
1358
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001359static mode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 mode_t mode;
1362 uint32_t permissions;
1363 uint32_t flags;
1364
1365 permissions = le32_to_cpu(fe->permissions);
1366 flags = le16_to_cpu(fe->icbTag.flags);
1367
Marcin Slusarz4b111112008-02-08 04:20:36 -08001368 mode = ((permissions) & S_IRWXO) |
1369 ((permissions >> 2) & S_IRWXG) |
1370 ((permissions >> 4) & S_IRWXU) |
1371 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1372 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1373 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 return mode;
1376}
1377
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001378int udf_write_inode(struct inode *inode, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 int ret;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 lock_kernel();
1383 ret = udf_update_inode(inode, sync);
1384 unlock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return ret;
1387}
1388
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001389int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 return udf_update_inode(inode, 1);
1392}
1393
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001394static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 struct buffer_head *bh = NULL;
1397 struct fileEntry *fe;
1398 struct extendedFileEntry *efe;
1399 uint32_t udfperms;
1400 uint16_t icbflags;
1401 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001403 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001404 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001405 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Marcin Slusarz4b111112008-02-08 04:20:36 -08001407 bh = udf_tread(inode->i_sb,
1408 udf_get_lb_pblock(inode->i_sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001409 &iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001410 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 udf_debug("bread failure\n");
1412 return -EIO;
1413 }
1414
1415 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
1416
1417 fe = (struct fileEntry *)bh->b_data;
1418 efe = (struct extendedFileEntry *)bh->b_data;
1419
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001420 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001422 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001424 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001425 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001426 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001427 sizeof(struct unallocSpaceEntry));
1428 crclen = sizeof(struct unallocSpaceEntry) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001429 iinfo->i_lenAlloc - sizeof(struct tag);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001430 use->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001431 iinfo->i_location.
Marcin Slusarz4b111112008-02-08 04:20:36 -08001432 logicalBlockNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 use->descTag.descCRCLength = cpu_to_le16(crclen);
Bob Copelandf845fce2008-04-17 09:47:48 +02001434 use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001435 sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001436 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001437 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 mark_buffer_dirty(bh);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001440 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return err;
1442 }
1443
Phillip Susi4d6660e2006-03-07 21:55:24 -08001444 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1445 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001446 else
1447 fe->uid = cpu_to_le32(inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Phillip Susi4d6660e2006-03-07 21:55:24 -08001449 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1450 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001451 else
1452 fe->gid = cpu_to_le32(inode->i_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Marcin Slusarz4b111112008-02-08 04:20:36 -08001454 udfperms = ((inode->i_mode & S_IRWXO)) |
1455 ((inode->i_mode & S_IRWXG) << 2) |
1456 ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Marcin Slusarz4b111112008-02-08 04:20:36 -08001458 udfperms |= (le32_to_cpu(fe->permissions) &
1459 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1460 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1461 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 fe->permissions = cpu_to_le32(udfperms);
1463
1464 if (S_ISDIR(inode->i_mode))
1465 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1466 else
1467 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1468
1469 fe->informationLength = cpu_to_le64(inode->i_size);
1470
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001471 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001472 struct regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001473 struct deviceSpec *dsea =
1474 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001475 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001477 udf_add_extendedattr(inode,
1478 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001479 sizeof(struct regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 dsea->attrType = cpu_to_le32(12);
1481 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001482 dsea->attrLength = cpu_to_le32(
1483 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001484 sizeof(struct regid));
1485 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001487 eid = (struct regid *)dsea->impUse;
1488 memset(eid, 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 strcpy(eid->ident, UDF_ID_DEVELOPER);
1490 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1491 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1492 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1493 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1494 }
1495
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001496 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001497 memcpy(bh->b_data + sizeof(struct fileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001498 iinfo->i_ext.i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001499 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001500 fe->logicalBlocksRecorded = cpu_to_le64(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001501 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1502 (blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Marcin Slusarz56774802008-02-10 11:25:31 +01001504 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1505 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1506 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001507 memset(&(fe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1509 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1510 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001511 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1512 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1513 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1515 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001516 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001517 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001518 iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001519 inode->i_sb->s_blocksize -
1520 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 efe->objectSize = cpu_to_le64(inode->i_size);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001522 efe->logicalBlocksRecorded = cpu_to_le64(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001523 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1524 (blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001526 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1527 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1528 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1529 iinfo->i_crtime = inode->i_atime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001530
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001531 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1532 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1533 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1534 iinfo->i_crtime = inode->i_mtime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001535
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001536 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1537 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1538 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1539 iinfo->i_crtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Marcin Slusarz56774802008-02-10 11:25:31 +01001541 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1542 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1543 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1544 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001546 memset(&(efe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1548 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1549 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001550 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1551 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1552 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1554 crclen = sizeof(struct extendedFileEntry);
1555 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001556 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 fe->icbTag.strategyType = cpu_to_le16(4096);
1558 fe->icbTag.strategyParameter = cpu_to_le16(1);
1559 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001560 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 fe->icbTag.strategyType = cpu_to_le16(4);
1562 fe->icbTag.numEntries = cpu_to_le16(1);
1563 }
1564
1565 if (S_ISDIR(inode->i_mode))
1566 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1567 else if (S_ISREG(inode->i_mode))
1568 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1569 else if (S_ISLNK(inode->i_mode))
1570 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1571 else if (S_ISBLK(inode->i_mode))
1572 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1573 else if (S_ISCHR(inode->i_mode))
1574 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1575 else if (S_ISFIFO(inode->i_mode))
1576 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1577 else if (S_ISSOCK(inode->i_mode))
1578 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1579
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001580 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001581 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1582 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1583 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1584 (le16_to_cpu(fe->icbTag.flags) &
1585 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1586 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001589 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 fe->descTag.descVersion = cpu_to_le16(3);
1591 else
1592 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001593 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001594 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001595 iinfo->i_location.logicalBlockNum);
1596 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc -
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001597 sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001599 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001600 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001601 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 /* write the data blocks */
1604 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001605 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 sync_dirty_buffer(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001607 if (buffer_req(bh) && !buffer_uptodate(bh)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001608 printk(KERN_WARNING "IO error syncing udf inode "
1609 "[%s:%08lx]\n", inode->i_sb->s_id,
1610 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 err = -EIO;
1612 }
1613 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001614 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return err;
1617}
1618
Pekka Enberg97e961f2008-10-15 12:29:03 +02001619struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1622 struct inode *inode = iget_locked(sb, block);
1623
1624 if (!inode)
1625 return NULL;
1626
1627 if (inode->i_state & I_NEW) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001628 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 __udf_read_inode(inode);
1630 unlock_new_inode(inode);
1631 }
1632
1633 if (is_bad_inode(inode))
1634 goto out_iput;
1635
Pekka Enberg97e961f2008-10-15 12:29:03 +02001636 if (ino->logicalBlockNum >= UDF_SB(sb)->
1637 s_partmaps[ino->partitionReferenceNum].s_partition_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 udf_debug("block=%d, partition=%d out of range\n",
Pekka Enberg97e961f2008-10-15 12:29:03 +02001639 ino->logicalBlockNum, ino->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 make_bad_inode(inode);
1641 goto out_iput;
1642 }
1643
1644 return inode;
1645
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001646 out_iput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 iput(inode);
1648 return NULL;
1649}
1650
Marcin Slusarz4b111112008-02-08 04:20:36 -08001651int8_t udf_add_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001652 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001655 struct short_ad *sad = NULL;
1656 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 struct allocExtDesc *aed;
1658 int8_t etype;
1659 uint8_t *ptr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001660 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Jan Karaff116fc2007-05-08 00:35:14 -07001662 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001663 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001664 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001665 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 else
Jan Karaff116fc2007-05-08 00:35:14 -07001667 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001669 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001670 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001671 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001672 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 else
1674 return -1;
1675
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001676 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 char *sptr, *dptr;
1678 struct buffer_head *nbh;
1679 int err, loffset;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001680 struct kernel_lb_addr obloc = epos->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Marcin Slusarz4b111112008-02-08 04:20:36 -08001682 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1683 obloc.partitionReferenceNum,
1684 obloc.logicalBlockNum, &err);
1685 if (!epos->block.logicalBlockNum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return -1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001687 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001688 &epos->block,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001689 0));
1690 if (!nbh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 lock_buffer(nbh);
1693 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1694 set_buffer_uptodate(nbh);
1695 unlock_buffer(nbh);
1696 mark_buffer_dirty_inode(nbh, inode);
1697
1698 aed = (struct allocExtDesc *)(nbh->b_data);
1699 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001700 aed->previousAllocExtLocation =
1701 cpu_to_le32(obloc.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001702 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -07001703 loffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 aed->lengthAllocDescs = cpu_to_le32(adsize);
1705 sptr = ptr - adsize;
1706 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1707 memcpy(dptr, sptr, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -07001708 epos->offset = sizeof(struct allocExtDesc) + adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001709 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001710 loffset = epos->offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 aed->lengthAllocDescs = cpu_to_le32(0);
1712 sptr = ptr;
Jan Karaff116fc2007-05-08 00:35:14 -07001713 epos->offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001715 if (epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001716 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001717 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001718 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001719 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 mark_inode_dirty(inode);
1721 }
1722 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001723 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001725 epos->block.logicalBlockNum, sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 else
1727 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001728 epos->block.logicalBlockNum, sizeof(struct tag));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001729 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001730 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001731 sad = (struct short_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001732 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1733 inode->i_sb->s_blocksize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001734 sad->extPosition =
1735 cpu_to_le32(epos->block.logicalBlockNum);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001736 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001737 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001738 lad = (struct long_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001739 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1740 inode->i_sb->s_blocksize);
1741 lad->extLocation = cpu_to_lelb(epos->block);
1742 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1743 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001745 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001746 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001747 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -07001748 udf_update_tag(epos->bh->b_data, loffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001750 udf_update_tag(epos->bh->b_data,
1751 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001752 mark_buffer_dirty_inode(epos->bh, inode);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001753 brelse(epos->bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001754 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001756 }
Jan Karaff116fc2007-05-08 00:35:14 -07001757 epos->bh = nbh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
1759
Jan Karaff116fc2007-05-08 00:35:14 -07001760 etype = udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001762 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001763 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001765 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001766 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001767 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001768 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1769 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1770 udf_update_tag(epos->bh->b_data,
1771 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001773 udf_update_tag(epos->bh->b_data,
1774 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001775 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
1777
1778 return etype;
1779}
1780
Marcin Slusarz4b111112008-02-08 04:20:36 -08001781int8_t udf_write_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001782 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
1784 int adsize;
1785 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001786 struct short_ad *sad;
1787 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001788 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Jan Karaff116fc2007-05-08 00:35:14 -07001790 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001791 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001792 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001793 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 else
Jan Karaff116fc2007-05-08 00:35:14 -07001795 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001797 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001798 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001799 sad = (struct short_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001800 sad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001801 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001802 adsize = sizeof(struct short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001803 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001804 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001805 lad = (struct long_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001806 lad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001807 lad->extLocation = cpu_to_lelb(*eloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001808 memset(lad->impUse, 0x00, sizeof(lad->impUse));
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001809 adsize = sizeof(struct long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001810 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001811 default:
1812 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
1814
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001815 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001816 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001817 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001818 struct allocExtDesc *aed =
1819 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07001820 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001821 le32_to_cpu(aed->lengthAllocDescs) +
1822 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
Jan Karaff116fc2007-05-08 00:35:14 -07001824 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001825 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07001830 epos->offset += adsize;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return (elen >> 30);
1833}
1834
Marcin Slusarz4b111112008-02-08 04:20:36 -08001835int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001836 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
1838 int8_t etype;
1839
Jan Karaff116fc2007-05-08 00:35:14 -07001840 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001841 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001842 int block;
Jan Karaff116fc2007-05-08 00:35:14 -07001843 epos->block = *eloc;
1844 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001845 brelse(epos->bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001846 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001847 epos->bh = udf_tread(inode->i_sb, block);
1848 if (!epos->bh) {
1849 udf_debug("reading block %d failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return -1;
1851 }
1852 }
1853
1854 return etype;
1855}
1856
Marcin Slusarz4b111112008-02-08 04:20:36 -08001857int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001858 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 int alen;
1861 int8_t etype;
1862 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001863 struct short_ad *sad;
1864 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001865 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001866
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001867 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001868 if (!epos->offset)
1869 epos->offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001870 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001871 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001872 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001873 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001874 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001875 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001876 if (!epos->offset)
1877 epos->offset = sizeof(struct allocExtDesc);
1878 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001879 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08001880 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
1881 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001884 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001885 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001886 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
1887 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001889 etype = le32_to_cpu(sad->extLength) >> 30;
1890 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001891 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001892 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001893 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
1894 break;
1895 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001896 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
1897 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001898 return -1;
1899 etype = le32_to_cpu(lad->extLength) >> 30;
1900 *eloc = lelb_to_cpu(lad->extLocation);
1901 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
1902 break;
1903 default:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001904 udf_debug("alloc_type = %d unsupported\n",
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001905 iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001906 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908
1909 return etype;
1910}
1911
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001912static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001913 struct kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001915 struct kernel_lb_addr oeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 uint32_t oelen;
1917 int8_t etype;
1918
Jan Karaff116fc2007-05-08 00:35:14 -07001919 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001920 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001922 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001923 udf_write_aext(inode, &epos, &neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 neloc = oeloc;
1925 nelen = (etype << 30) | oelen;
1926 }
Pekka Enberg97e961f2008-10-15 12:29:03 +02001927 udf_add_aext(inode, &epos, &neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001928 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return (nelen >> 30);
1931}
1932
Marcin Slusarz4b111112008-02-08 04:20:36 -08001933int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001934 struct kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Jan Karaff116fc2007-05-08 00:35:14 -07001936 struct extent_position oepos;
1937 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 int8_t etype;
1939 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001940 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001942 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001943 get_bh(epos.bh);
1944 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001947 iinfo = UDF_I(inode);
1948 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001949 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001950 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001951 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 else
1953 adsize = 0;
1954
Jan Karaff116fc2007-05-08 00:35:14 -07001955 oepos = epos;
1956 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return -1;
1958
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001959 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001960 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001961 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001962 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07001963 brelse(oepos.bh);
1964 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07001965 oepos.bh = epos.bh;
1966 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
1968 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001969 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 elen = 0;
1971
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001972 if (epos.bh != oepos.bh) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001973 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
1974 udf_write_aext(inode, &oepos, &eloc, elen, 1);
1975 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001976 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001977 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001979 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001980 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001981 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001982 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001983 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08001984 udf_update_tag(oepos.bh->b_data,
1985 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001987 udf_update_tag(oepos.bh->b_data,
1988 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001989 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001991 } else {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001992 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001993 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001994 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001996 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001997 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001998 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001999 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002000 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002001 udf_update_tag(oepos.bh->b_data,
2002 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002004 udf_update_tag(oepos.bh->b_data,
2005 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002006 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
2008 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002009
Jan Kara3bf25cb2007-05-08 00:35:16 -07002010 brelse(epos.bh);
2011 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 return (elen >> 30);
2014}
2015
Marcin Slusarz4b111112008-02-08 04:20:36 -08002016int8_t inode_bmap(struct inode *inode, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002017 struct extent_position *pos, struct kernel_lb_addr *eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002018 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002020 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002021 loff_t lbcount = 0, bcount =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002022 (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002024 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002026 iinfo = UDF_I(inode);
Jan Karaff116fc2007-05-08 00:35:14 -07002027 pos->offset = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002028 pos->block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002029 pos->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 *elen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002032 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002033 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2034 if (etype == -1) {
2035 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002036 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return -1;
2038 }
2039 lbcount += *elen;
2040 } while (lbcount <= bcount);
2041
Marcin Slusarz4b111112008-02-08 04:20:36 -08002042 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044 return etype;
2045}
2046
Jan Kara60448b12007-05-08 00:35:13 -07002047long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002049 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -07002050 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002051 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002052 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 int ret;
2054
2055 lock_kernel();
2056
Marcin Slusarz4b111112008-02-08 04:20:36 -08002057 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2058 (EXT_RECORDED_ALLOCATED >> 30))
Pekka Enberg97e961f2008-10-15 12:29:03 +02002059 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 else
2061 ret = 0;
2062
2063 unlock_kernel();
Jan Kara3bf25cb2007-05-08 00:35:16 -07002064 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2067 return udf_fixed_to_variable(ret);
2068 else
2069 return ret;
2070}