blob: ddd7780d1ab1d217a239d76f7842d834a8e1733f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * inode.c
3 *
4 * PURPOSE
5 * Inode handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * COPYRIGHT
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
12 *
13 * (C) 1998 Dave Boynton
14 * (C) 1998-2004 Ben Fennema
15 * (C) 1999-2000 Stelias Computing Inc
16 *
17 * HISTORY
18 *
19 * 10/04/98 dgb Added rudimentary directory functions
20 * 10/07/98 Fully working udf_block_map! It works!
21 * 11/25/98 bmap altered to better support extents
Marcin Slusarz4b111112008-02-08 04:20:36 -080022 * 12/06/98 blf partition support in udf_iget, udf_block_map
23 * and udf_read_inode
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 12/12/98 rewrote udf_block_map to handle next extents and descs across
25 * block boundaries (which is not actually allowed)
26 * 12/20/98 added support for strategy 4096
27 * 03/07/99 rewrote udf_block_map (again)
28 * New funcs, inode_bmap, udf_next_aext
29 * 04/19/99 Support for writing device EA's for major/minor #
30 */
31
32#include "udfdecl.h"
33#include <linux/mm.h>
34#include <linux/smp_lock.h>
35#include <linux/module.h>
36#include <linux/pagemap.h>
37#include <linux/buffer_head.h>
38#include <linux/writeback.h>
39#include <linux/slab.h>
40
41#include "udf_i.h"
42#include "udf_sb.h"
43
44MODULE_AUTHOR("Ben Fennema");
45MODULE_DESCRIPTION("Universal Disk Format Filesystem");
46MODULE_LICENSE("GPL");
47
48#define EXTENT_MERGE_SIZE 5
49
50static mode_t udf_convert_permissions(struct fileEntry *);
51static int udf_update_inode(struct inode *, int);
52static void udf_fill_inode(struct inode *, struct buffer_head *);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -070053static int udf_alloc_i_data(struct inode *inode, size_t size);
Jan Kara60448b12007-05-08 00:35:13 -070054static struct buffer_head *inode_getblk(struct inode *, sector_t, int *,
Marcin Slusarz1ed16172008-02-08 04:20:48 -080055 sector_t *, int *);
Jan Karaff116fc2007-05-08 00:35:14 -070056static int8_t udf_insert_aext(struct inode *, struct extent_position,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070057 kernel_lb_addr, uint32_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static void udf_split_extents(struct inode *, int *, int, int,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070059 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static void udf_prealloc_extents(struct inode *, int, int,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070061 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void udf_merge_extents(struct inode *,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070063 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static void udf_update_extents(struct inode *,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070065 kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
66 struct extent_position *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
68
Christoph Hellwigb1e32122008-02-22 12:38:48 +010069
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070070void udf_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Mark Fashehfef26652005-09-09 13:01:31 -070072 truncate_inode_pages(&inode->i_data, 0);
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (is_bad_inode(inode))
75 goto no_delete;
76
77 inode->i_size = 0;
78 udf_truncate(inode);
79 lock_kernel();
80
81 udf_update_inode(inode, IS_SYNC(inode));
82 udf_free_inode(inode);
83
84 unlock_kernel();
85 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070086
87no_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 clear_inode(inode);
89}
90
Jan Kara74584ae2007-06-16 10:16:14 -070091/*
92 * If we are going to release inode from memory, we discard preallocation and
93 * truncate last inode extent to proper length. We could use drop_inode() but
94 * it's called under inode_lock and thus we cannot mark inode dirty there. We
95 * use clear_inode() but we have to make sure to write inode as it's not written
96 * automatically.
97 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098void udf_clear_inode(struct inode *inode)
99{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800100 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (!(inode->i_sb->s_flags & MS_RDONLY)) {
102 lock_kernel();
Jan Kara74584ae2007-06-16 10:16:14 -0700103 /* Discard preallocation for directories, symlinks, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 udf_discard_prealloc(inode);
Jan Kara74584ae2007-06-16 10:16:14 -0700105 udf_truncate_tail_extent(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 unlock_kernel();
Mike Galbraith32a8f242008-02-08 04:20:49 -0800107 write_inode_now(inode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800109 iinfo = UDF_I(inode);
110 kfree(iinfo->i_ext.i_data);
111 iinfo->i_ext.i_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114static int udf_writepage(struct page *page, struct writeback_control *wbc)
115{
116 return block_write_full_page(page, udf_get_block, wbc);
117}
118
119static int udf_readpage(struct file *file, struct page *page)
120{
121 return block_read_full_page(page, udf_get_block);
122}
123
Nick Pigginbe021ee2007-10-16 01:25:20 -0700124static int udf_write_begin(struct file *file, struct address_space *mapping,
125 loff_t pos, unsigned len, unsigned flags,
126 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Nick Pigginbe021ee2007-10-16 01:25:20 -0700128 *pagep = NULL;
129 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
130 udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133static sector_t udf_bmap(struct address_space *mapping, sector_t block)
134{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700135 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700138const struct address_space_operations udf_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700139 .readpage = udf_readpage,
140 .writepage = udf_writepage,
141 .sync_page = block_sync_page,
Nick Pigginbe021ee2007-10-16 01:25:20 -0700142 .write_begin = udf_write_begin,
143 .write_end = generic_write_end,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700144 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700147void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 struct page *page;
150 char *kaddr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800151 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct writeback_control udf_wbc = {
153 .sync_mode = WB_SYNC_NONE,
154 .nr_to_write = 1,
155 };
156
157 /* from now on we have normal address_space methods */
158 inode->i_data.a_ops = &udf_aops;
159
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800160 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800162 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800164 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 mark_inode_dirty(inode);
166 return;
167 }
168
169 page = grab_cache_page(inode->i_mapping, 0);
Matt Mackallcd7619d2005-05-01 08:59:01 -0700170 BUG_ON(!PageLocked(page));
171
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700172 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 kaddr = kmap(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800174 memset(kaddr + iinfo->i_lenAlloc, 0x00,
175 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
176 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
177 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 flush_dcache_page(page);
179 SetPageUptodate(page);
180 kunmap(page);
181 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800182 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
183 iinfo->i_lenAlloc);
184 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800186 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800188 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 inode->i_data.a_ops->writepage(page, &udf_wbc);
191 page_cache_release(page);
192
193 mark_inode_dirty(inode);
194}
195
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700196struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
197 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700200 struct buffer_head *dbh = NULL;
201 kernel_lb_addr eloc;
202 uint32_t elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700204 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 struct udf_fileident_bh sfibh, dfibh;
Jan Karaaf793292008-02-08 04:20:50 -0800207 loff_t f_pos = udf_ext0_offset(inode);
208 int size = udf_ext0_offset(inode) + inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800210 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
213 alloctype = ICBTAG_FLAG_AD_SHORT;
214 else
215 alloctype = ICBTAG_FLAG_AD_LONG;
216
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700217 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800218 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 mark_inode_dirty(inode);
220 return NULL;
221 }
222
223 /* alloc block, and copy data to it */
224 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800225 iinfo->i_location.partitionReferenceNum,
226 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (!(*block))
228 return NULL;
229 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800230 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800231 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (!newblock)
233 return NULL;
234 dbh = udf_tgetblk(inode->i_sb, newblock);
235 if (!dbh)
236 return NULL;
237 lock_buffer(dbh);
238 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
239 set_buffer_uptodate(dbh);
240 unlock_buffer(dbh);
241 mark_buffer_dirty_inode(dbh, inode);
242
Marcin Slusarz4b111112008-02-08 04:20:36 -0800243 sfibh.soffset = sfibh.eoffset =
Jan Karaaf793292008-02-08 04:20:50 -0800244 f_pos & (inode->i_sb->s_blocksize - 1);
Jan Karaff116fc2007-05-08 00:35:14 -0700245 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 dfibh.soffset = dfibh.eoffset = 0;
247 dfibh.sbh = dfibh.ebh = dbh;
Jan Karaaf793292008-02-08 04:20:50 -0800248 while (f_pos < size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800249 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800250 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
251 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700252 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700253 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return NULL;
255 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800256 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 sfi->descTag.tagLocation = cpu_to_le32(*block);
258 dfibh.soffset = dfibh.eoffset;
259 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
260 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
261 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800262 sfi->fileIdent +
263 le16_to_cpu(sfi->lengthOfImpUse))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800264 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700265 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return NULL;
267 }
268 }
269 mark_buffer_dirty_inode(dbh, inode);
270
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800271 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
272 iinfo->i_lenAlloc);
273 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800275 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800276 iinfo->i_location.partitionReferenceNum;
Jan Kara05343c42008-02-08 04:20:51 -0800277 elen = inode->i_sb->s_blocksize;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800278 iinfo->i_lenExtents = elen;
Jan Karaff116fc2007-05-08 00:35:14 -0700279 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800280 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700281 epos.offset = udf_file_entry_alloc_offset(inode);
282 udf_add_aext(inode, &epos, eloc, elen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* UniqueID stuff */
284
Jan Kara3bf25cb2007-05-08 00:35:16 -0700285 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 mark_inode_dirty(inode);
287 return dbh;
288}
289
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700290static int udf_get_block(struct inode *inode, sector_t block,
291 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 int err, new;
294 struct buffer_head *bh;
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800295 sector_t phys = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800296 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700298 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 phys = udf_block_map(inode, block);
300 if (phys)
301 map_bh(bh_result, inode->i_sb, phys);
302 return 0;
303 }
304
305 err = -EIO;
306 new = 0;
307 bh = NULL;
308
309 lock_kernel();
310
311 if (block < 0)
312 goto abort_negative;
313
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800314 iinfo = UDF_I(inode);
315 if (block == iinfo->i_next_alloc_block + 1) {
316 iinfo->i_next_alloc_block++;
317 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319
320 err = 0;
321
322 bh = inode_getblk(inode, block, &err, &phys, &new);
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200323 BUG_ON(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (err)
325 goto abort;
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200326 BUG_ON(!phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 if (new)
329 set_buffer_new(bh_result);
330 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700331
332abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 unlock_kernel();
334 return err;
335
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700336abort_negative:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 udf_warning(inode->i_sb, "udf_get_block", "block < 0");
338 goto abort;
339}
340
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700341static struct buffer_head *udf_getblk(struct inode *inode, long block,
342 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700344 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct buffer_head dummy;
346
347 dummy.b_state = 0;
348 dummy.b_blocknr = -1000;
349 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700350 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700352 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 lock_buffer(bh);
354 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
355 set_buffer_uptodate(bh);
356 unlock_buffer(bh);
357 mark_buffer_dirty_inode(bh, inode);
358 }
359 return bh;
360 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return NULL;
363}
364
Jan Kara31170b62007-05-08 00:35:21 -0700365/* Extend the file by 'blocks' blocks, return the number of extents added */
366int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800367 kernel_long_ad *last_ext, sector_t blocks)
Jan Kara31170b62007-05-08 00:35:21 -0700368{
369 sector_t add;
370 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
371 struct super_block *sb = inode->i_sb;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700372 kernel_lb_addr prealloc_loc = {};
Jan Kara31170b62007-05-08 00:35:21 -0700373 int prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800374 struct udf_inode_info *iinfo;
Jan Kara31170b62007-05-08 00:35:21 -0700375
376 /* The previous extent is fake and we should not extend by anything
377 * - there's nothing to do... */
378 if (!blocks && fake)
379 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700380
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800381 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700382 /* Round the last extent up to a multiple of block size */
383 if (last_ext->extLength & (sb->s_blocksize - 1)) {
384 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700385 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
386 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
387 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800388 iinfo->i_lenExtents =
389 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700390 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700391 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700392
Jan Kara31170b62007-05-08 00:35:21 -0700393 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800394 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
395 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700396 /* Save the extent so that we can reattach it to the end */
397 prealloc_loc = last_ext->extLocation;
398 prealloc_len = last_ext->extLength;
399 /* Mark the extent as a hole */
400 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700401 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700402 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800403 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700404 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700405
Jan Kara31170b62007-05-08 00:35:21 -0700406 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800407 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
408 EXT_NOT_RECORDED_NOT_ALLOCATED) {
409 add = ((1 << 30) - sb->s_blocksize -
410 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
411 sb->s_blocksize_bits;
Jan Kara31170b62007-05-08 00:35:21 -0700412 if (add > blocks)
413 add = blocks;
414 blocks -= add;
415 last_ext->extLength += add << sb->s_blocksize_bits;
416 }
417
418 if (fake) {
419 udf_add_aext(inode, last_pos, last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700420 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700421 count++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800422 } else
423 udf_write_aext(inode, last_pos, last_ext->extLocation,
424 last_ext->extLength, 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700425
Jan Kara31170b62007-05-08 00:35:21 -0700426 /* Managed to do everything necessary? */
427 if (!blocks)
428 goto out;
429
430 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
431 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800432 last_ext->extLocation.partitionReferenceNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700433 add = (1 << (30-sb->s_blocksize_bits)) - 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800434 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
435 (add << sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700436
Jan Kara31170b62007-05-08 00:35:21 -0700437 /* Create enough extents to cover the whole hole */
438 while (blocks > add) {
439 blocks -= add;
440 if (udf_add_aext(inode, last_pos, last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700441 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700442 return -1;
443 count++;
444 }
445 if (blocks) {
446 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700447 (blocks << sb->s_blocksize_bits);
Jan Kara31170b62007-05-08 00:35:21 -0700448 if (udf_add_aext(inode, last_pos, last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700449 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700450 return -1;
451 count++;
452 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700453
454out:
Jan Kara31170b62007-05-08 00:35:21 -0700455 /* Do we have some preallocated blocks saved? */
456 if (prealloc_len) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800457 if (udf_add_aext(inode, last_pos, prealloc_loc,
458 prealloc_len, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700459 return -1;
460 last_ext->extLocation = prealloc_loc;
461 last_ext->extLength = prealloc_len;
462 count++;
463 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700464
Jan Kara31170b62007-05-08 00:35:21 -0700465 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800466 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Jan Kara31170b62007-05-08 00:35:21 -0700467 last_pos->offset -= sizeof(short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800468 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Jan Kara31170b62007-05-08 00:35:21 -0700469 last_pos->offset -= sizeof(long_ad);
470 else
471 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700472
Jan Kara31170b62007-05-08 00:35:21 -0700473 return count;
474}
475
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700476static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800477 int *err, sector_t *phys, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Jan Kara31170b62007-05-08 00:35:21 -0700479 static sector_t last_block;
Jan Karaff116fc2007-05-08 00:35:14 -0700480 struct buffer_head *result = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700482 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700484 uint32_t elen = 0, tmpelen;
485 kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700487 loff_t lbcount = 0, b_off = 0;
488 uint32_t newblocknum, newblock;
489 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800491 struct udf_inode_info *iinfo = UDF_I(inode);
492 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700493 int lastblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Jan Karaff116fc2007-05-08 00:35:14 -0700495 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800496 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700497 prev_epos.bh = NULL;
498 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700499 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700502 alternate between laarr[0] and laarr[1] for locations of the
503 current extent, and the previous extent */
504 do {
505 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700506 brelse(prev_epos.bh);
507 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700508 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700510 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700511 brelse(cur_epos.bh);
512 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700513 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
516 lbcount += elen;
517
Jan Karaff116fc2007-05-08 00:35:14 -0700518 prev_epos.block = cur_epos.block;
519 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Jan Karaff116fc2007-05-08 00:35:14 -0700521 prev_epos.offset = cur_epos.offset;
522 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Marcin Slusarz4b111112008-02-08 04:20:36 -0800524 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
525 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 break;
527
528 c = !c;
529
530 laarr[c].extLength = (etype << 30) | elen;
531 laarr[c].extLocation = eloc;
532
533 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
534 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700535 ((elen + inode->i_sb->s_blocksize - 1) >>
536 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700538 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 } while (lbcount + elen <= b_off);
540
541 b_off -= lbcount;
542 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700543 /*
544 * Move prev_epos and cur_epos into indirect extent if we are at
545 * the pointer to it
546 */
547 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
548 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700551 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700553 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
554 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700556 ((elen + inode->i_sb->s_blocksize - 1) &
557 ~(inode->i_sb->s_blocksize - 1));
Jan Karaff116fc2007-05-08 00:35:14 -0700558 etype = udf_write_aext(inode, &cur_epos, eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700560 brelse(prev_epos.bh);
561 brelse(cur_epos.bh);
562 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 newblock = udf_get_lb_pblock(inode->i_sb, eloc, offset);
564 *phys = newblock;
565 return NULL;
566 }
567
Jan Kara31170b62007-05-08 00:35:21 -0700568 last_block = block;
569 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700570 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700571 int ret;
572
573 if (count) {
574 if (c)
575 laarr[0] = laarr[1];
576 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700577 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700578 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800579 memset(&laarr[0].extLocation, 0x00,
580 sizeof(kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700581 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800582 /* Will udf_extend_file() create real extent from
583 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700584 startnum = (offset > 0);
585 }
586 /* Create extents for the hole between EOF and offset */
587 ret = udf_extend_file(inode, &prev_epos, laarr, offset);
588 if (ret == -1) {
589 brelse(prev_epos.bh);
590 brelse(cur_epos.bh);
591 brelse(next_epos.bh);
592 /* We don't really know the error here so we just make
593 * something up */
594 *err = -ENOSPC;
595 return NULL;
596 }
597 c = 0;
598 offset = 0;
599 count += ret;
600 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800601 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
602 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700603 /* Is there any real extent? - otherwise we overwrite
604 * the fake one... */
605 if (count)
606 c = !c;
607 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700608 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800609 memset(&laarr[c].extLocation, 0x00,
610 sizeof(kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700611 count++;
612 endnum++;
Jan Kara31170b62007-05-08 00:35:21 -0700613 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700614 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700616 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 endnum = startnum = ((count > 2) ? 2 : count);
618
Marcin Slusarz4b111112008-02-08 04:20:36 -0800619 /* if the current extent is in position 0,
620 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700621 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700622 laarr[2] = laarr[0];
623 laarr[0] = laarr[1];
624 laarr[1] = laarr[2];
625 c = 1;
626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Marcin Slusarz4b111112008-02-08 04:20:36 -0800628 /* if the current block is located in an extent,
629 read the next extent */
630 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
631 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700632 laarr[c + 1].extLength = (etype << 30) | elen;
633 laarr[c + 1].extLocation = eloc;
634 count++;
635 startnum++;
636 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800637 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 lastblock = 1;
639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700642 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800643 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800645 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800646 if (iinfo->i_next_alloc_block == block)
647 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700649 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800650 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800651 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
Marcin Slusarz4b111112008-02-08 04:20:36 -0800654 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800655 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800656 goal, err);
657 if (!newblocknum) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700658 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 *err = -ENOSPC;
660 return NULL;
661 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800662 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
Marcin Slusarz4b111112008-02-08 04:20:36 -0800665 /* if the extent the requsted block is located in contains multiple
666 * blocks, split the extent into at most three extents. blocks prior
667 * to requested block, requested block, and blocks after requested
668 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
670
671#ifdef UDF_PREALLOCATE
672 /* preallocate blocks */
673 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
674#endif
675
676 /* merge any continuous blocks in laarr */
677 udf_merge_extents(inode, laarr, &endnum);
678
679 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700680 * of extents is greater than the old number, and deleting extents if
681 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700682 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Jan Kara3bf25cb2007-05-08 00:35:16 -0700684 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Marcin Slusarz4b111112008-02-08 04:20:36 -0800686 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800687 iinfo->i_location.partitionReferenceNum, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800688 if (!newblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 *phys = newblock;
691 *err = 0;
692 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800693 iinfo->i_next_alloc_block = block;
694 iinfo->i_next_alloc_goal = newblocknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 inode->i_ctime = current_fs_time(inode->i_sb);
696
697 if (IS_SYNC(inode))
698 udf_sync_inode(inode);
699 else
700 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return result;
703}
704
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700705static void udf_split_extents(struct inode *inode, int *c, int offset,
706 int newblocknum,
707 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
708 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800710 unsigned long blocksize = inode->i_sb->s_blocksize;
711 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800714 (laarr[*c].extLength >> 30) ==
715 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 int curr = *c;
717 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800718 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 int8_t etype = (laarr[curr].extLength >> 30);
720
Marcin Slusarz4b111112008-02-08 04:20:36 -0800721 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700722 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800723 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700724 laarr[curr + 2] = laarr[curr + 1];
725 laarr[curr + 1] = laarr[curr];
726 } else {
727 laarr[curr + 3] = laarr[curr + 1];
728 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700731 if (offset) {
732 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800733 udf_free_blocks(inode->i_sb, inode,
734 laarr[curr].extLocation,
735 0, offset);
736 laarr[curr].extLength =
737 EXT_NOT_RECORDED_NOT_ALLOCATED |
738 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800740 laarr[curr].extLocation.
741 partitionReferenceNum = 0;
742 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800744 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700745 curr++;
746 (*c)++;
747 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 laarr[curr].extLocation.logicalBlockNum = newblocknum;
751 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
752 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800753 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800755 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700756 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700758 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800760 laarr[curr].extLocation.logicalBlockNum +=
761 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700762 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800763 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700764 curr++;
765 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767 }
768}
769
770static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700771 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
772 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 int start, length = 0, currlength = 0, i;
775
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700776 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (!lastblock)
778 return;
779 else
780 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700781 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800782 if ((laarr[c + 1].extLength >> 30) ==
783 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700784 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800785 length = currlength =
786 (((laarr[c + 1].extLength &
787 UDF_EXTENT_LENGTH_MASK) +
788 inode->i_sb->s_blocksize - 1) >>
789 inode->i_sb->s_blocksize_bits);
790 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 start = c;
792 }
793
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700794 for (i = start + 1; i <= *endnum; i++) {
795 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (lastblock)
797 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800798 } else if ((laarr[i].extLength >> 30) ==
799 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
800 length += (((laarr[i].extLength &
801 UDF_EXTENT_LENGTH_MASK) +
802 inode->i_sb->s_blocksize - 1) >>
803 inode->i_sb->s_blocksize_bits);
804 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 break;
806 }
807
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700808 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700810 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800811 inode->i_sb->s_blocksize - 1) >>
812 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800814 laarr[start].extLocation.partitionReferenceNum,
815 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
816 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
817 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700818 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800819 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800821 (numalloc <<
822 inode->i_sb->s_blocksize_bits);
823 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700824 memmove(&laarr[c + 2], &laarr[c + 1],
825 sizeof(long_ad) * (*endnum - (c + 1)));
826 (*endnum)++;
827 laarr[c + 1].extLocation.logicalBlockNum = next;
828 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800829 laarr[c].extLocation.
830 partitionReferenceNum;
831 laarr[c + 1].extLength =
832 EXT_NOT_RECORDED_ALLOCATED |
833 (numalloc <<
834 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700835 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700838 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800839 int elen = ((laarr[i].extLength &
840 UDF_EXTENT_LENGTH_MASK) +
841 inode->i_sb->s_blocksize - 1) >>
842 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700844 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800846 (numalloc <<
847 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700849 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700851 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800852 memmove(&laarr[i],
853 &laarr[i + 1],
854 sizeof(long_ad) *
855 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700856 i--;
857 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
859 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800860 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800861 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863 }
864}
865
866static void udf_merge_extents(struct inode *inode,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700867 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
868 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800871 unsigned long blocksize = inode->i_sb->s_blocksize;
872 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700874 for (i = 0; i < (*endnum - 1); i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800875 kernel_long_ad *li /*l[i]*/ = &laarr[i];
876 kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Marcin Slusarz4b111112008-02-08 04:20:36 -0800878 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
879 (((li->extLength >> 30) ==
880 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
881 ((lip1->extLocation.logicalBlockNum -
882 li->extLocation.logicalBlockNum) ==
883 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
884 blocksize - 1) >> blocksize_bits)))) {
885
886 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
887 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
888 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
889 lip1->extLength = (lip1->extLength -
890 (li->extLength &
891 UDF_EXTENT_LENGTH_MASK) +
892 UDF_EXTENT_LENGTH_MASK) &
893 ~(blocksize - 1);
894 li->extLength = (li->extLength &
895 UDF_EXTENT_FLAG_MASK) +
896 (UDF_EXTENT_LENGTH_MASK + 1) -
897 blocksize;
898 lip1->extLocation.logicalBlockNum =
899 li->extLocation.logicalBlockNum +
900 ((li->extLength &
901 UDF_EXTENT_LENGTH_MASK) >>
902 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700903 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800904 li->extLength = lip1->extLength +
905 (((li->extLength &
906 UDF_EXTENT_LENGTH_MASK) +
907 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700908 if (*endnum > (i + 2))
909 memmove(&laarr[i + 1], &laarr[i + 2],
Marcin Slusarz4b111112008-02-08 04:20:36 -0800910 sizeof(long_ad) *
911 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700912 i--;
913 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800915 } else if (((li->extLength >> 30) ==
916 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
917 ((lip1->extLength >> 30) ==
918 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
919 udf_free_blocks(inode->i_sb, inode, li->extLocation, 0,
920 ((li->extLength &
921 UDF_EXTENT_LENGTH_MASK) +
922 blocksize - 1) >> blocksize_bits);
923 li->extLocation.logicalBlockNum = 0;
924 li->extLocation.partitionReferenceNum = 0;
925
926 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
927 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
928 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
929 lip1->extLength = (lip1->extLength -
930 (li->extLength &
931 UDF_EXTENT_LENGTH_MASK) +
932 UDF_EXTENT_LENGTH_MASK) &
933 ~(blocksize - 1);
934 li->extLength = (li->extLength &
935 UDF_EXTENT_FLAG_MASK) +
936 (UDF_EXTENT_LENGTH_MASK + 1) -
937 blocksize;
938 } else {
939 li->extLength = lip1->extLength +
940 (((li->extLength &
941 UDF_EXTENT_LENGTH_MASK) +
942 blocksize - 1) & ~(blocksize - 1));
943 if (*endnum > (i + 2))
944 memmove(&laarr[i + 1], &laarr[i + 2],
945 sizeof(long_ad) *
946 (*endnum - (i + 2)));
947 i--;
948 (*endnum)--;
949 }
950 } else if ((li->extLength >> 30) ==
951 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
952 udf_free_blocks(inode->i_sb, inode,
953 li->extLocation, 0,
954 ((li->extLength &
955 UDF_EXTENT_LENGTH_MASK) +
956 blocksize - 1) >> blocksize_bits);
957 li->extLocation.logicalBlockNum = 0;
958 li->extLocation.partitionReferenceNum = 0;
959 li->extLength = (li->extLength &
960 UDF_EXTENT_LENGTH_MASK) |
961 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963 }
964}
965
966static void udf_update_extents(struct inode *inode,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700967 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
968 int startnum, int endnum,
969 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 int start = 0, i;
972 kernel_lb_addr tmploc;
973 uint32_t tmplen;
974
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700975 if (startnum > endnum) {
976 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -0700977 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700978 laarr[i].extLength);
979 } else if (startnum < endnum) {
980 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -0700981 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700982 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -0700983 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700984 &laarr[i].extLength, 1);
985 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987 }
988
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700989 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -0700990 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
991 udf_write_aext(inode, epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700992 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994}
995
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700996struct buffer_head *udf_bread(struct inode *inode, int block,
997 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700999 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 bh = udf_getblk(inode, block, create, err);
1002 if (!bh)
1003 return NULL;
1004
1005 if (buffer_uptodate(bh))
1006 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 ll_rw_block(READ, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 wait_on_buffer(bh);
1011 if (buffer_uptodate(bh))
1012 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 brelse(bh);
1015 *err = -EIO;
1016 return NULL;
1017}
1018
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001019void udf_truncate(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 int offset;
1022 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001023 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001026 S_ISLNK(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return;
1028 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1029 return;
1030
1031 lock_kernel();
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001032 iinfo = UDF_I(inode);
1033 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001034 if (inode->i_sb->s_blocksize <
1035 (udf_file_entry_alloc_offset(inode) +
1036 inode->i_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 udf_expand_file_adinicb(inode, inode->i_size, &err);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001038 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1039 inode->i_size = iinfo->i_lenAlloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 unlock_kernel();
1041 return;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001042 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 udf_truncate_extents(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001044 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001046 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + offset,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001047 0x00, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001048 offset - udf_file_entry_alloc_offset(inode));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001049 iinfo->i_lenAlloc = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001051 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001052 block_truncate_page(inode->i_mapping, inode->i_size,
1053 udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 udf_truncate_extents(inode);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1058 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001059 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 else
1061 mark_inode_dirty(inode);
1062 unlock_kernel();
1063}
1064
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001065static void __udf_read_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 struct buffer_head *bh = NULL;
1068 struct fileEntry *fe;
1069 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001070 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /*
1073 * Set defaults, but the inode is still incomplete!
1074 * Note: get_new_inode() sets the following on a new inode:
1075 * i_sb = sb
1076 * i_no = ino
1077 * i_flags = sb->s_flags
1078 * i_state = 0
1079 * clean_inode(): zero fills and sets
1080 * i_count = 1
1081 * i_nlink = 1
1082 * i_op = NULL;
1083 */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001084 bh = udf_read_ptagged(inode->i_sb, iinfo->i_location, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001085 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001087 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 make_bad_inode(inode);
1089 return;
1090 }
1091
1092 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001093 ident != TAG_IDENT_USE) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001094 printk(KERN_ERR "udf: udf_read_inode(ino %ld) "
1095 "failed ident=%d\n", inode->i_ino, ident);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001096 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 make_bad_inode(inode);
1098 return;
1099 }
1100
1101 fe = (struct fileEntry *)bh->b_data;
1102
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001103 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001104 struct buffer_head *ibh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001106 ibh = udf_read_ptagged(inode->i_sb, iinfo->i_location, 1,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001107 &ident);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001108 if (ident == TAG_IDENT_IE && ibh) {
1109 struct buffer_head *nbh = NULL;
1110 kernel_lb_addr loc;
1111 struct indirectEntry *ie;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001112
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001113 ie = (struct indirectEntry *)ibh->b_data;
1114 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001115
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001116 if (ie->indirectICB.extLength &&
1117 (nbh = udf_read_ptagged(inode->i_sb, loc, 0,
1118 &ident))) {
1119 if (ident == TAG_IDENT_FE ||
1120 ident == TAG_IDENT_EFE) {
1121 memcpy(&iinfo->i_location,
1122 &loc,
1123 sizeof(kernel_lb_addr));
1124 brelse(bh);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001125 brelse(ibh);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001126 brelse(nbh);
1127 __udf_read_inode(inode);
1128 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001129 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001130 brelse(nbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001132 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001133 brelse(ibh);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001134 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 printk(KERN_ERR "udf: unsupported strategy type: %d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001136 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001137 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 make_bad_inode(inode);
1139 return;
1140 }
1141 udf_fill_inode(inode, bh);
Jan Kara31170b62007-05-08 00:35:21 -07001142
Jan Kara3bf25cb2007-05-08 00:35:16 -07001143 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1147{
1148 struct fileEntry *fe;
1149 struct extendedFileEntry *efe;
1150 time_t convtime;
1151 long convtime_usec;
1152 int offset;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001153 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001154 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 fe = (struct fileEntry *)bh->b_data;
1157 efe = (struct extendedFileEntry *)bh->b_data;
1158
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001159 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001160 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001161 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001162 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001164 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001165 ICBTAG_FLAG_AD_MASK;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001166 iinfo->i_unique = 0;
1167 iinfo->i_lenEAttr = 0;
1168 iinfo->i_lenExtents = 0;
1169 iinfo->i_lenAlloc = 0;
1170 iinfo->i_next_alloc_block = 0;
1171 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001172 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001173 iinfo->i_efe = 1;
1174 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001175 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1176 sizeof(struct extendedFileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001177 make_bad_inode(inode);
1178 return;
1179 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001180 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001181 bh->b_data + sizeof(struct extendedFileEntry),
1182 inode->i_sb->s_blocksize -
1183 sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001184 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001185 iinfo->i_efe = 0;
1186 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001187 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1188 sizeof(struct fileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001189 make_bad_inode(inode);
1190 return;
1191 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001192 memcpy(iinfo->i_ext.i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001193 bh->b_data + sizeof(struct fileEntry),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001194 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001195 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001196 iinfo->i_efe = 0;
1197 iinfo->i_use = 1;
1198 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001199 ((struct unallocSpaceEntry *)bh->b_data)->
1200 lengthAllocDescs);
1201 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1202 sizeof(struct unallocSpaceEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001203 make_bad_inode(inode);
1204 return;
1205 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001206 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001207 bh->b_data + sizeof(struct unallocSpaceEntry),
1208 inode->i_sb->s_blocksize -
1209 sizeof(struct unallocSpaceEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return;
1211 }
1212
1213 inode->i_uid = le32_to_cpu(fe->uid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001214 if (inode->i_uid == -1 ||
1215 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1216 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001217 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 inode->i_gid = le32_to_cpu(fe->gid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001220 if (inode->i_gid == -1 ||
1221 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1222 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001223 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
1226 if (!inode->i_nlink)
1227 inode->i_nlink = 1;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 inode->i_size = le64_to_cpu(fe->informationLength);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001230 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 inode->i_mode = udf_convert_permissions(fe);
1233 inode->i_mode &= ~UDF_SB(inode->i_sb)->s_umask;
1234
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001235 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001237 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001239 if (udf_stamp_to_time(&convtime, &convtime_usec,
1240 lets_to_cpu(fe->accessTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 inode->i_atime.tv_sec = convtime;
1242 inode->i_atime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001243 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001244 inode->i_atime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001247 if (udf_stamp_to_time(&convtime, &convtime_usec,
1248 lets_to_cpu(fe->modificationTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 inode->i_mtime.tv_sec = convtime;
1250 inode->i_mtime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001251 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001252 inode->i_mtime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001255 if (udf_stamp_to_time(&convtime, &convtime_usec,
1256 lets_to_cpu(fe->attrTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 inode->i_ctime.tv_sec = convtime;
1258 inode->i_ctime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001259 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001260 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
1262
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001263 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1264 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1265 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1266 offset = sizeof(struct fileEntry) + iinfo->i_lenEAttr;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001267 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001268 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001269 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001271 if (udf_stamp_to_time(&convtime, &convtime_usec,
1272 lets_to_cpu(efe->accessTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 inode->i_atime.tv_sec = convtime;
1274 inode->i_atime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001275 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001276 inode->i_atime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001279 if (udf_stamp_to_time(&convtime, &convtime_usec,
1280 lets_to_cpu(efe->modificationTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 inode->i_mtime.tv_sec = convtime;
1282 inode->i_mtime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001283 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001284 inode->i_mtime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001287 if (udf_stamp_to_time(&convtime, &convtime_usec,
1288 lets_to_cpu(efe->createTime))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001289 iinfo->i_crtime.tv_sec = convtime;
1290 iinfo->i_crtime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001291 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001292 iinfo->i_crtime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001295 if (udf_stamp_to_time(&convtime, &convtime_usec,
1296 lets_to_cpu(efe->attrTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 inode->i_ctime.tv_sec = convtime;
1298 inode->i_ctime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001299 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001300 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001303 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1304 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1305 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001306 offset = sizeof(struct extendedFileEntry) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001307 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001310 switch (fe->icbTag.fileType) {
1311 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001312 inode->i_op = &udf_dir_inode_operations;
1313 inode->i_fop = &udf_dir_operations;
1314 inode->i_mode |= S_IFDIR;
1315 inc_nlink(inode);
1316 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001317 case ICBTAG_FILE_TYPE_REALTIME:
1318 case ICBTAG_FILE_TYPE_REGULAR:
1319 case ICBTAG_FILE_TYPE_UNDEF:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001320 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001321 inode->i_data.a_ops = &udf_adinicb_aops;
1322 else
1323 inode->i_data.a_ops = &udf_aops;
1324 inode->i_op = &udf_file_inode_operations;
1325 inode->i_fop = &udf_file_operations;
1326 inode->i_mode |= S_IFREG;
1327 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001328 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001329 inode->i_mode |= S_IFBLK;
1330 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001331 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001332 inode->i_mode |= S_IFCHR;
1333 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001334 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001335 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1336 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001337 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001338 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1339 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001340 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001341 inode->i_data.a_ops = &udf_symlink_aops;
1342 inode->i_op = &page_symlink_inode_operations;
1343 inode->i_mode = S_IFLNK | S_IRWXUGO;
1344 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001345 default:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001346 printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
1347 "file type=%d\n", inode->i_ino,
1348 fe->icbTag.fileType);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001349 make_bad_inode(inode);
1350 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001352 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001353 struct deviceSpec *dsea =
1354 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001355 if (dsea) {
1356 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001357 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1358 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001360 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363}
1364
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001365static int udf_alloc_i_data(struct inode *inode, size_t size)
1366{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001367 struct udf_inode_info *iinfo = UDF_I(inode);
1368 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001369
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001370 if (!iinfo->i_ext.i_data) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001371 printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
1372 "no free memory\n", inode->i_ino);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001373 return -ENOMEM;
1374 }
1375
1376 return 0;
1377}
1378
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001379static mode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 mode_t mode;
1382 uint32_t permissions;
1383 uint32_t flags;
1384
1385 permissions = le32_to_cpu(fe->permissions);
1386 flags = le16_to_cpu(fe->icbTag.flags);
1387
Marcin Slusarz4b111112008-02-08 04:20:36 -08001388 mode = ((permissions) & S_IRWXO) |
1389 ((permissions >> 2) & S_IRWXG) |
1390 ((permissions >> 4) & S_IRWXU) |
1391 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1392 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1393 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 return mode;
1396}
1397
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001398int udf_write_inode(struct inode *inode, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400 int ret;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 lock_kernel();
1403 ret = udf_update_inode(inode, sync);
1404 unlock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return ret;
1407}
1408
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001409int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
1411 return udf_update_inode(inode, 1);
1412}
1413
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001414static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 struct buffer_head *bh = NULL;
1417 struct fileEntry *fe;
1418 struct extendedFileEntry *efe;
1419 uint32_t udfperms;
1420 uint16_t icbflags;
1421 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 kernel_timestamp cpu_time;
1423 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001424 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001425 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001426 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Marcin Slusarz4b111112008-02-08 04:20:36 -08001428 bh = udf_tread(inode->i_sb,
1429 udf_get_lb_pblock(inode->i_sb,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001430 iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001431 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 udf_debug("bread failure\n");
1433 return -EIO;
1434 }
1435
1436 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
1437
1438 fe = (struct fileEntry *)bh->b_data;
1439 efe = (struct extendedFileEntry *)bh->b_data;
1440
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001441 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001443 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001445 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001446 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001447 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001448 sizeof(struct unallocSpaceEntry));
1449 crclen = sizeof(struct unallocSpaceEntry) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001450 iinfo->i_lenAlloc - sizeof(tag);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001451 use->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001452 iinfo->i_location.
Marcin Slusarz4b111112008-02-08 04:20:36 -08001453 logicalBlockNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 use->descTag.descCRCLength = cpu_to_le16(crclen);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001455 use->descTag.descCRC = cpu_to_le16(udf_crc((char *)use +
1456 sizeof(tag), crclen,
1457 0));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001458 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 mark_buffer_dirty(bh);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001461 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return err;
1463 }
1464
Phillip Susi4d6660e2006-03-07 21:55:24 -08001465 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1466 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001467 else
1468 fe->uid = cpu_to_le32(inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Phillip Susi4d6660e2006-03-07 21:55:24 -08001470 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1471 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001472 else
1473 fe->gid = cpu_to_le32(inode->i_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Marcin Slusarz4b111112008-02-08 04:20:36 -08001475 udfperms = ((inode->i_mode & S_IRWXO)) |
1476 ((inode->i_mode & S_IRWXG) << 2) |
1477 ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Marcin Slusarz4b111112008-02-08 04:20:36 -08001479 udfperms |= (le32_to_cpu(fe->permissions) &
1480 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1481 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1482 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 fe->permissions = cpu_to_le32(udfperms);
1484
1485 if (S_ISDIR(inode->i_mode))
1486 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1487 else
1488 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1489
1490 fe->informationLength = cpu_to_le64(inode->i_size);
1491
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001492 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001494 struct deviceSpec *dsea =
1495 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001496 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001498 udf_add_extendedattr(inode,
1499 sizeof(struct deviceSpec) +
1500 sizeof(regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 dsea->attrType = cpu_to_le32(12);
1502 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001503 dsea->attrLength = cpu_to_le32(
1504 sizeof(struct deviceSpec) +
1505 sizeof(regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 dsea->impUseLength = cpu_to_le32(sizeof(regid));
1507 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001508 eid = (regid *)dsea->impUse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 memset(eid, 0, sizeof(regid));
1510 strcpy(eid->ident, UDF_ID_DEVELOPER);
1511 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1512 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1513 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1514 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1515 }
1516
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001517 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001518 memcpy(bh->b_data + sizeof(struct fileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001519 iinfo->i_ext.i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001520 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001521 fe->logicalBlocksRecorded = cpu_to_le64(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001522 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1523 (blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1526 fe->accessTime = cpu_to_lets(cpu_time);
1527 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1528 fe->modificationTime = cpu_to_lets(cpu_time);
1529 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1530 fe->attrTime = cpu_to_lets(cpu_time);
1531 memset(&(fe->impIdent), 0, sizeof(regid));
1532 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1533 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1534 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001535 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1536 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1537 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1539 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001540 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001541 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001542 iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001543 inode->i_sb->s_blocksize -
1544 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 efe->objectSize = cpu_to_le64(inode->i_size);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001546 efe->logicalBlocksRecorded = cpu_to_le64(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001547 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1548 (blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001550 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1551 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1552 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1553 iinfo->i_crtime = inode->i_atime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001554
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001555 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1556 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1557 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1558 iinfo->i_crtime = inode->i_mtime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001559
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001560 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1561 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1562 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1563 iinfo->i_crtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1566 efe->accessTime = cpu_to_lets(cpu_time);
1567 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1568 efe->modificationTime = cpu_to_lets(cpu_time);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001569 if (udf_time_to_stamp(&cpu_time, iinfo->i_crtime))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 efe->createTime = cpu_to_lets(cpu_time);
1571 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1572 efe->attrTime = cpu_to_lets(cpu_time);
1573
1574 memset(&(efe->impIdent), 0, sizeof(regid));
1575 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1576 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1577 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001578 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1579 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1580 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1582 crclen = sizeof(struct extendedFileEntry);
1583 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001584 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 fe->icbTag.strategyType = cpu_to_le16(4096);
1586 fe->icbTag.strategyParameter = cpu_to_le16(1);
1587 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001588 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 fe->icbTag.strategyType = cpu_to_le16(4);
1590 fe->icbTag.numEntries = cpu_to_le16(1);
1591 }
1592
1593 if (S_ISDIR(inode->i_mode))
1594 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1595 else if (S_ISREG(inode->i_mode))
1596 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1597 else if (S_ISLNK(inode->i_mode))
1598 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1599 else if (S_ISBLK(inode->i_mode))
1600 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1601 else if (S_ISCHR(inode->i_mode))
1602 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1603 else if (S_ISFIFO(inode->i_mode))
1604 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1605 else if (S_ISSOCK(inode->i_mode))
1606 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1607
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001608 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001609 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1610 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1611 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1612 (le16_to_cpu(fe->icbTag.flags) &
1613 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1614 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001617 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 fe->descTag.descVersion = cpu_to_le16(3);
1619 else
1620 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001621 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001622 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001623 iinfo->i_location.logicalBlockNum);
1624 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc -
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001625 sizeof(tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001627 fe->descTag.descCRC = cpu_to_le16(udf_crc((char *)fe + sizeof(tag),
1628 crclen, 0));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001629 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 /* write the data blocks */
1632 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001633 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 sync_dirty_buffer(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001635 if (buffer_req(bh) && !buffer_uptodate(bh)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001636 printk(KERN_WARNING "IO error syncing udf inode "
1637 "[%s:%08lx]\n", inode->i_sb->s_id,
1638 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 err = -EIO;
1640 }
1641 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001642 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return err;
1645}
1646
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001647struct inode *udf_iget(struct super_block *sb, kernel_lb_addr ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1650 struct inode *inode = iget_locked(sb, block);
1651
1652 if (!inode)
1653 return NULL;
1654
1655 if (inode->i_state & I_NEW) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001656 memcpy(&UDF_I(inode)->i_location, &ino, sizeof(kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 __udf_read_inode(inode);
1658 unlock_new_inode(inode);
1659 }
1660
1661 if (is_bad_inode(inode))
1662 goto out_iput;
1663
Marcin Slusarz4b111112008-02-08 04:20:36 -08001664 if (ino.logicalBlockNum >= UDF_SB(sb)->
1665 s_partmaps[ino.partitionReferenceNum].s_partition_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 udf_debug("block=%d, partition=%d out of range\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001667 ino.logicalBlockNum, ino.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 make_bad_inode(inode);
1669 goto out_iput;
1670 }
1671
1672 return inode;
1673
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001674 out_iput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 iput(inode);
1676 return NULL;
1677}
1678
Marcin Slusarz4b111112008-02-08 04:20:36 -08001679int8_t udf_add_aext(struct inode *inode, struct extent_position *epos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001680 kernel_lb_addr eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 int adsize;
1683 short_ad *sad = NULL;
1684 long_ad *lad = NULL;
1685 struct allocExtDesc *aed;
1686 int8_t etype;
1687 uint8_t *ptr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001688 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Jan Karaff116fc2007-05-08 00:35:14 -07001690 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001691 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001692 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001693 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 else
Jan Karaff116fc2007-05-08 00:35:14 -07001695 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001697 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 adsize = sizeof(short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001699 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 adsize = sizeof(long_ad);
1701 else
1702 return -1;
1703
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001704 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 char *sptr, *dptr;
1706 struct buffer_head *nbh;
1707 int err, loffset;
Jan Karaff116fc2007-05-08 00:35:14 -07001708 kernel_lb_addr obloc = epos->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Marcin Slusarz4b111112008-02-08 04:20:36 -08001710 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1711 obloc.partitionReferenceNum,
1712 obloc.logicalBlockNum, &err);
1713 if (!epos->block.logicalBlockNum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return -1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001715 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
1716 epos->block,
1717 0));
1718 if (!nbh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 lock_buffer(nbh);
1721 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1722 set_buffer_uptodate(nbh);
1723 unlock_buffer(nbh);
1724 mark_buffer_dirty_inode(nbh, inode);
1725
1726 aed = (struct allocExtDesc *)(nbh->b_data);
1727 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001728 aed->previousAllocExtLocation =
1729 cpu_to_le32(obloc.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001730 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -07001731 loffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 aed->lengthAllocDescs = cpu_to_le32(adsize);
1733 sptr = ptr - adsize;
1734 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1735 memcpy(dptr, sptr, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -07001736 epos->offset = sizeof(struct allocExtDesc) + adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001737 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001738 loffset = epos->offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 aed->lengthAllocDescs = cpu_to_le32(0);
1740 sptr = ptr;
Jan Karaff116fc2007-05-08 00:35:14 -07001741 epos->offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001743 if (epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001744 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001745 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001746 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001747 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 mark_inode_dirty(inode);
1749 }
1750 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001751 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001753 epos->block.logicalBlockNum, sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 else
1755 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001756 epos->block.logicalBlockNum, sizeof(tag));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001757 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001758 case ICBTAG_FLAG_AD_SHORT:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001759 sad = (short_ad *)sptr;
1760 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1761 inode->i_sb->s_blocksize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001762 sad->extPosition =
1763 cpu_to_le32(epos->block.logicalBlockNum);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001764 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001765 case ICBTAG_FLAG_AD_LONG:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001766 lad = (long_ad *)sptr;
1767 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1768 inode->i_sb->s_blocksize);
1769 lad->extLocation = cpu_to_lelb(epos->block);
1770 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1771 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001773 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001774 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001775 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -07001776 udf_update_tag(epos->bh->b_data, loffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001778 udf_update_tag(epos->bh->b_data,
1779 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001780 mark_buffer_dirty_inode(epos->bh, inode);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001781 brelse(epos->bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001782 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001784 }
Jan Karaff116fc2007-05-08 00:35:14 -07001785 epos->bh = nbh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787
Jan Karaff116fc2007-05-08 00:35:14 -07001788 etype = udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001790 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001791 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001793 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001794 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001795 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001796 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1797 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1798 udf_update_tag(epos->bh->b_data,
1799 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001801 udf_update_tag(epos->bh->b_data,
1802 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001803 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805
1806 return etype;
1807}
1808
Marcin Slusarz4b111112008-02-08 04:20:36 -08001809int8_t udf_write_aext(struct inode *inode, struct extent_position *epos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001810 kernel_lb_addr eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 int adsize;
1813 uint8_t *ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001814 short_ad *sad;
1815 long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001816 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Jan Karaff116fc2007-05-08 00:35:14 -07001818 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001819 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001820 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001821 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 else
Jan Karaff116fc2007-05-08 00:35:14 -07001823 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001825 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001826 case ICBTAG_FLAG_AD_SHORT:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001827 sad = (short_ad *)ptr;
1828 sad->extLength = cpu_to_le32(elen);
1829 sad->extPosition = cpu_to_le32(eloc.logicalBlockNum);
1830 adsize = sizeof(short_ad);
1831 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001832 case ICBTAG_FLAG_AD_LONG:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001833 lad = (long_ad *)ptr;
1834 lad->extLength = cpu_to_le32(elen);
1835 lad->extLocation = cpu_to_lelb(eloc);
1836 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1837 adsize = sizeof(long_ad);
1838 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001839 default:
1840 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
1842
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001843 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001844 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001845 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001846 struct allocExtDesc *aed =
1847 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07001848 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001849 le32_to_cpu(aed->lengthAllocDescs) +
1850 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
Jan Karaff116fc2007-05-08 00:35:14 -07001852 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001853 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07001858 epos->offset += adsize;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return (elen >> 30);
1861}
1862
Marcin Slusarz4b111112008-02-08 04:20:36 -08001863int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
1864 kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 int8_t etype;
1867
Jan Karaff116fc2007-05-08 00:35:14 -07001868 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001869 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001870 int block;
Jan Karaff116fc2007-05-08 00:35:14 -07001871 epos->block = *eloc;
1872 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001873 brelse(epos->bh);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001874 block = udf_get_lb_pblock(inode->i_sb, epos->block, 0);
1875 epos->bh = udf_tread(inode->i_sb, block);
1876 if (!epos->bh) {
1877 udf_debug("reading block %d failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return -1;
1879 }
1880 }
1881
1882 return etype;
1883}
1884
Marcin Slusarz4b111112008-02-08 04:20:36 -08001885int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
1886 kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
1888 int alen;
1889 int8_t etype;
1890 uint8_t *ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001891 short_ad *sad;
1892 long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001893 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001894
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001895 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001896 if (!epos->offset)
1897 epos->offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001898 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001899 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001900 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001901 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001902 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001903 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001904 if (!epos->offset)
1905 epos->offset = sizeof(struct allocExtDesc);
1906 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001907 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08001908 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
1909 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001912 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001913 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001914 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
1915 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001917 etype = le32_to_cpu(sad->extLength) >> 30;
1918 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001919 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001920 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001921 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
1922 break;
1923 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001924 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
1925 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001926 return -1;
1927 etype = le32_to_cpu(lad->extLength) >> 30;
1928 *eloc = lelb_to_cpu(lad->extLocation);
1929 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
1930 break;
1931 default:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001932 udf_debug("alloc_type = %d unsupported\n",
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001933 iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001934 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 }
1936
1937 return etype;
1938}
1939
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001940static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
1941 kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
1943 kernel_lb_addr oeloc;
1944 uint32_t oelen;
1945 int8_t etype;
1946
Jan Karaff116fc2007-05-08 00:35:14 -07001947 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001948 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001950 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Jan Karaff116fc2007-05-08 00:35:14 -07001951 udf_write_aext(inode, &epos, neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 neloc = oeloc;
1953 nelen = (etype << 30) | oelen;
1954 }
Jan Karaff116fc2007-05-08 00:35:14 -07001955 udf_add_aext(inode, &epos, neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001956 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return (nelen >> 30);
1959}
1960
Marcin Slusarz4b111112008-02-08 04:20:36 -08001961int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001962 kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Jan Karaff116fc2007-05-08 00:35:14 -07001964 struct extent_position oepos;
1965 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 int8_t etype;
1967 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001968 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001970 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001971 get_bh(epos.bh);
1972 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001975 iinfo = UDF_I(inode);
1976 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 adsize = sizeof(short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001978 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 adsize = sizeof(long_ad);
1980 else
1981 adsize = 0;
1982
Jan Karaff116fc2007-05-08 00:35:14 -07001983 oepos = epos;
1984 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return -1;
1986
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001987 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Jan Karaff116fc2007-05-08 00:35:14 -07001988 udf_write_aext(inode, &oepos, eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001989 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001990 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07001991 brelse(oepos.bh);
1992 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07001993 oepos.bh = epos.bh;
1994 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
1996 }
1997 memset(&eloc, 0x00, sizeof(kernel_lb_addr));
1998 elen = 0;
1999
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002000 if (epos.bh != oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002001 udf_free_blocks(inode->i_sb, inode, epos.block, 0, 1);
2002 udf_write_aext(inode, &oepos, eloc, elen, 1);
2003 udf_write_aext(inode, &oepos, eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002004 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002005 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002007 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002008 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002009 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002010 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002011 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002012 udf_update_tag(oepos.bh->b_data,
2013 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002015 udf_update_tag(oepos.bh->b_data,
2016 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002017 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002019 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002020 udf_write_aext(inode, &oepos, eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002021 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002022 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002024 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002025 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002026 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002027 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002028 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002029 udf_update_tag(oepos.bh->b_data,
2030 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002032 udf_update_tag(oepos.bh->b_data,
2033 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002034 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
2036 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002037
Jan Kara3bf25cb2007-05-08 00:35:16 -07002038 brelse(epos.bh);
2039 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return (elen >> 30);
2042}
2043
Marcin Slusarz4b111112008-02-08 04:20:36 -08002044int8_t inode_bmap(struct inode *inode, sector_t block,
2045 struct extent_position *pos, kernel_lb_addr *eloc,
2046 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002048 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002049 loff_t lbcount = 0, bcount =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002050 (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002052 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002054 if (block < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 printk(KERN_ERR "udf: inode_bmap: block < 0\n");
2056 return -1;
2057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002059 iinfo = UDF_I(inode);
Jan Karaff116fc2007-05-08 00:35:14 -07002060 pos->offset = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002061 pos->block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002062 pos->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 *elen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002065 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002066 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2067 if (etype == -1) {
2068 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002069 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return -1;
2071 }
2072 lbcount += *elen;
2073 } while (lbcount <= bcount);
2074
Marcin Slusarz4b111112008-02-08 04:20:36 -08002075 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 return etype;
2078}
2079
Jan Kara60448b12007-05-08 00:35:13 -07002080long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
Jan Karaff116fc2007-05-08 00:35:14 -07002082 kernel_lb_addr eloc;
2083 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002084 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002085 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 int ret;
2087
2088 lock_kernel();
2089
Marcin Slusarz4b111112008-02-08 04:20:36 -08002090 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2091 (EXT_RECORDED_ALLOCATED >> 30))
Jan Kara60448b12007-05-08 00:35:13 -07002092 ret = udf_get_lb_pblock(inode->i_sb, eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 else
2094 ret = 0;
2095
2096 unlock_kernel();
Jan Kara3bf25cb2007-05-08 00:35:16 -07002097 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2100 return udf_fixed_to_variable(ret);
2101 else
2102 return ret;
2103}