blob: be6326f449a1a5a8db77d8bb4a30109f42da25d3 [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
22 * 12/06/98 blf partition support in udf_iget, udf_block_map and udf_read_inode
23 * 12/12/98 rewrote udf_block_map to handle next extents and descs across
24 * block boundaries (which is not actually allowed)
25 * 12/20/98 added support for strategy 4096
26 * 03/07/99 rewrote udf_block_map (again)
27 * New funcs, inode_bmap, udf_next_aext
28 * 04/19/99 Support for writing device EA's for major/minor #
29 */
30
31#include "udfdecl.h"
32#include <linux/mm.h>
33#include <linux/smp_lock.h>
34#include <linux/module.h>
35#include <linux/pagemap.h>
36#include <linux/buffer_head.h>
37#include <linux/writeback.h>
38#include <linux/slab.h>
39
40#include "udf_i.h"
41#include "udf_sb.h"
42
43MODULE_AUTHOR("Ben Fennema");
44MODULE_DESCRIPTION("Universal Disk Format Filesystem");
45MODULE_LICENSE("GPL");
46
47#define EXTENT_MERGE_SIZE 5
48
49static mode_t udf_convert_permissions(struct fileEntry *);
50static int udf_update_inode(struct inode *, int);
51static void udf_fill_inode(struct inode *, struct buffer_head *);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -070052static int udf_alloc_i_data(struct inode *inode, size_t size);
Jan Kara60448b12007-05-08 00:35:13 -070053static struct buffer_head *inode_getblk(struct inode *, sector_t, int *,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070054 long *, int *);
Jan Karaff116fc2007-05-08 00:35:14 -070055static int8_t udf_insert_aext(struct inode *, struct extent_position,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070056 kernel_lb_addr, uint32_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static void udf_split_extents(struct inode *, int *, int, int,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070058 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static void udf_prealloc_extents(struct inode *, int, int,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070060 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static void udf_merge_extents(struct inode *,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070062 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static void udf_update_extents(struct inode *,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070064 kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
65 struct extent_position *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
67
68/*
69 * udf_delete_inode
70 *
71 * PURPOSE
72 * Clean-up before the specified inode is destroyed.
73 *
74 * DESCRIPTION
75 * This routine is called when the kernel destroys an inode structure
76 * ie. when iput() finds i_count == 0.
77 *
78 * HISTORY
79 * July 1, 1997 - Andrew E. Mileski
80 * Written, tested, and released.
81 *
82 * Called at the last iput() if i_nlink is zero.
83 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070084void udf_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Mark Fashehfef26652005-09-09 13:01:31 -070086 truncate_inode_pages(&inode->i_data, 0);
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (is_bad_inode(inode))
89 goto no_delete;
90
91 inode->i_size = 0;
92 udf_truncate(inode);
93 lock_kernel();
94
95 udf_update_inode(inode, IS_SYNC(inode));
96 udf_free_inode(inode);
97
98 unlock_kernel();
99 return;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700100 no_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 clear_inode(inode);
102}
103
Jan Kara74584ae2007-06-16 10:16:14 -0700104/*
105 * If we are going to release inode from memory, we discard preallocation and
106 * truncate last inode extent to proper length. We could use drop_inode() but
107 * it's called under inode_lock and thus we cannot mark inode dirty there. We
108 * use clear_inode() but we have to make sure to write inode as it's not written
109 * automatically.
110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111void udf_clear_inode(struct inode *inode)
112{
113 if (!(inode->i_sb->s_flags & MS_RDONLY)) {
114 lock_kernel();
Jan Kara74584ae2007-06-16 10:16:14 -0700115 /* Discard preallocation for directories, symlinks, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 udf_discard_prealloc(inode);
Jan Kara74584ae2007-06-16 10:16:14 -0700117 udf_truncate_tail_extent(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 unlock_kernel();
Jan Kara74584ae2007-06-16 10:16:14 -0700119 write_inode_now(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 kfree(UDF_I_DATA(inode));
122 UDF_I_DATA(inode) = NULL;
123}
124
125static int udf_writepage(struct page *page, struct writeback_control *wbc)
126{
127 return block_write_full_page(page, udf_get_block, wbc);
128}
129
130static int udf_readpage(struct file *file, struct page *page)
131{
132 return block_read_full_page(page, udf_get_block);
133}
134
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700135static int udf_prepare_write(struct file *file, struct page *page,
136 unsigned from, unsigned to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 return block_prepare_write(page, from, to, udf_get_block);
139}
140
141static sector_t udf_bmap(struct address_space *mapping, sector_t block)
142{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700143 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700146const struct address_space_operations udf_aops = {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700147 .readpage = udf_readpage,
148 .writepage = udf_writepage,
149 .sync_page = block_sync_page,
150 .prepare_write = udf_prepare_write,
151 .commit_write = generic_commit_write,
152 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};
154
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700155void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct page *page;
158 char *kaddr;
159 struct writeback_control udf_wbc = {
160 .sync_mode = WB_SYNC_NONE,
161 .nr_to_write = 1,
162 };
163
164 /* from now on we have normal address_space methods */
165 inode->i_data.a_ops = &udf_aops;
166
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700167 if (!UDF_I_LENALLOC(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
169 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_SHORT;
170 else
171 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_LONG;
172 mark_inode_dirty(inode);
173 return;
174 }
175
176 page = grab_cache_page(inode->i_mapping, 0);
Matt Mackallcd7619d2005-05-01 08:59:01 -0700177 BUG_ON(!PageLocked(page));
178
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700179 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 kaddr = kmap(page);
181 memset(kaddr + UDF_I_LENALLOC(inode), 0x00,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700182 PAGE_CACHE_SIZE - UDF_I_LENALLOC(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 memcpy(kaddr, UDF_I_DATA(inode) + UDF_I_LENEATTR(inode),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700184 UDF_I_LENALLOC(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 flush_dcache_page(page);
186 SetPageUptodate(page);
187 kunmap(page);
188 }
189 memset(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode), 0x00,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700190 UDF_I_LENALLOC(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 UDF_I_LENALLOC(inode) = 0;
192 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
193 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_SHORT;
194 else
195 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_LONG;
196
197 inode->i_data.a_ops->writepage(page, &udf_wbc);
198 page_cache_release(page);
199
200 mark_inode_dirty(inode);
201}
202
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700203struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
204 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700207 struct buffer_head *dbh = NULL;
208 kernel_lb_addr eloc;
209 uint32_t elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700211 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 struct udf_fileident_bh sfibh, dfibh;
214 loff_t f_pos = udf_ext0_offset(inode) >> 2;
215 int size = (udf_ext0_offset(inode) + inode->i_size) >> 2;
216 struct fileIdentDesc cfi, *sfi, *dfi;
217
218 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
219 alloctype = ICBTAG_FLAG_AD_SHORT;
220 else
221 alloctype = ICBTAG_FLAG_AD_LONG;
222
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700223 if (!inode->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 UDF_I_ALLOCTYPE(inode) = alloctype;
225 mark_inode_dirty(inode);
226 return NULL;
227 }
228
229 /* alloc block, and copy data to it */
230 *block = udf_new_block(inode->i_sb, inode,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700231 UDF_I_LOCATION(inode).partitionReferenceNum,
232 UDF_I_LOCATION(inode).logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (!(*block))
235 return NULL;
236 newblock = udf_get_pblock(inode->i_sb, *block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700237 UDF_I_LOCATION(inode).partitionReferenceNum,
238 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (!newblock)
240 return NULL;
241 dbh = udf_tgetblk(inode->i_sb, newblock);
242 if (!dbh)
243 return NULL;
244 lock_buffer(dbh);
245 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
246 set_buffer_uptodate(dbh);
247 unlock_buffer(dbh);
248 mark_buffer_dirty_inode(dbh, inode);
249
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700250 sfibh.soffset = sfibh.eoffset =
251 (f_pos & ((inode->i_sb->s_blocksize - 1) >> 2)) << 2;
Jan Karaff116fc2007-05-08 00:35:14 -0700252 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 dfibh.soffset = dfibh.eoffset = 0;
254 dfibh.sbh = dfibh.ebh = dbh;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700255 while ((f_pos < size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_IN_ICB;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700257 sfi =
258 udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL, NULL,
259 NULL, NULL);
260 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700261 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return NULL;
263 }
264 UDF_I_ALLOCTYPE(inode) = alloctype;
265 sfi->descTag.tagLocation = cpu_to_le32(*block);
266 dfibh.soffset = dfibh.eoffset;
267 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
268 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
269 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700270 sfi->fileIdent +
271 le16_to_cpu(sfi->lengthOfImpUse))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700273 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return NULL;
275 }
276 }
277 mark_buffer_dirty_inode(dbh, inode);
278
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700279 memset(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode), 0,
280 UDF_I_LENALLOC(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 UDF_I_LENALLOC(inode) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 eloc.logicalBlockNum = *block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700283 eloc.partitionReferenceNum =
284 UDF_I_LOCATION(inode).partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 elen = inode->i_size;
286 UDF_I_LENEXTENTS(inode) = elen;
Jan Karaff116fc2007-05-08 00:35:14 -0700287 epos.bh = NULL;
288 epos.block = UDF_I_LOCATION(inode);
289 epos.offset = udf_file_entry_alloc_offset(inode);
290 udf_add_aext(inode, &epos, eloc, elen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /* UniqueID stuff */
292
Jan Kara3bf25cb2007-05-08 00:35:16 -0700293 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 mark_inode_dirty(inode);
295 return dbh;
296}
297
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700298static int udf_get_block(struct inode *inode, sector_t block,
299 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 int err, new;
302 struct buffer_head *bh;
303 unsigned long phys;
304
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700305 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 phys = udf_block_map(inode, block);
307 if (phys)
308 map_bh(bh_result, inode->i_sb, phys);
309 return 0;
310 }
311
312 err = -EIO;
313 new = 0;
314 bh = NULL;
315
316 lock_kernel();
317
318 if (block < 0)
319 goto abort_negative;
320
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700321 if (block == UDF_I_NEXT_ALLOC_BLOCK(inode) + 1) {
322 UDF_I_NEXT_ALLOC_BLOCK(inode)++;
323 UDF_I_NEXT_ALLOC_GOAL(inode)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
326 err = 0;
327
328 bh = inode_getblk(inode, block, &err, &phys, &new);
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200329 BUG_ON(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (err)
331 goto abort;
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200332 BUG_ON(!phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 if (new)
335 set_buffer_new(bh_result);
336 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700337 abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 unlock_kernel();
339 return err;
340
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700341 abort_negative:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 udf_warning(inode->i_sb, "udf_get_block", "block < 0");
343 goto abort;
344}
345
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700346static struct buffer_head *udf_getblk(struct inode *inode, long block,
347 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct buffer_head dummy;
350
351 dummy.b_state = 0;
352 dummy.b_blocknr = -1000;
353 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700354 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 struct buffer_head *bh;
356 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700357 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 lock_buffer(bh);
359 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
360 set_buffer_uptodate(bh);
361 unlock_buffer(bh);
362 mark_buffer_dirty_inode(bh, inode);
363 }
364 return bh;
365 }
366 return NULL;
367}
368
Jan Kara31170b62007-05-08 00:35:21 -0700369/* Extend the file by 'blocks' blocks, return the number of extents added */
370int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700371 kernel_long_ad * last_ext, sector_t blocks)
Jan Kara31170b62007-05-08 00:35:21 -0700372{
373 sector_t add;
374 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
375 struct super_block *sb = inode->i_sb;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700376 kernel_lb_addr prealloc_loc = { 0, 0 };
Jan Kara31170b62007-05-08 00:35:21 -0700377 int prealloc_len = 0;
378
379 /* The previous extent is fake and we should not extend by anything
380 * - there's nothing to do... */
381 if (!blocks && fake)
382 return 0;
383 /* Round the last extent up to a multiple of block size */
384 if (last_ext->extLength & (sb->s_blocksize - 1)) {
385 last_ext->extLength =
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700386 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
387 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
388 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Jan Kara31170b62007-05-08 00:35:21 -0700389 UDF_I_LENEXTENTS(inode) =
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700390 (UDF_I_LENEXTENTS(inode) + sb->s_blocksize - 1) &
391 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700392 }
393 /* Last extent are just preallocated blocks? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700394 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 Gorcunovcb00ea32007-07-19 01:47:43 -0700401 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700402 last_ext->extLocation.logicalBlockNum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700403 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700404 }
405 /* Can we merge with the previous extent? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700406 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
407 EXT_NOT_RECORDED_NOT_ALLOCATED) {
408 add =
409 ((1 << 30) - sb->s_blocksize -
410 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >> sb->
411 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++;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700422 } else
423 udf_write_aext(inode, last_pos, last_ext->extLocation,
424 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700425 /* Managed to do everything necessary? */
426 if (!blocks)
427 goto out;
428
429 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
430 last_ext->extLocation.logicalBlockNum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700431 last_ext->extLocation.partitionReferenceNum = 0;
432 add = (1 << (30 - sb->s_blocksize_bits)) - 1;
433 last_ext->extLength =
434 EXT_NOT_RECORDED_NOT_ALLOCATED | (add << sb->s_blocksize_bits);
Jan Kara31170b62007-05-08 00:35:21 -0700435 /* Create enough extents to cover the whole hole */
436 while (blocks > add) {
437 blocks -= add;
438 if (udf_add_aext(inode, last_pos, last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700439 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700440 return -1;
441 count++;
442 }
443 if (blocks) {
444 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700445 (blocks << sb->s_blocksize_bits);
Jan Kara31170b62007-05-08 00:35:21 -0700446 if (udf_add_aext(inode, last_pos, last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700447 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700448 return -1;
449 count++;
450 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700451 out:
Jan Kara31170b62007-05-08 00:35:21 -0700452 /* Do we have some preallocated blocks saved? */
453 if (prealloc_len) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700454 if (udf_add_aext(inode, last_pos, prealloc_loc, prealloc_len, 1)
455 == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700456 return -1;
457 last_ext->extLocation = prealloc_loc;
458 last_ext->extLength = prealloc_len;
459 count++;
460 }
461 /* last_pos should point to the last written extent... */
462 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
463 last_pos->offset -= sizeof(short_ad);
464 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
465 last_pos->offset -= sizeof(long_ad);
466 else
467 return -1;
468 return count;
469}
470
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700471static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
472 int *err, long *phys, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Jan Kara31170b62007-05-08 00:35:21 -0700474 static sector_t last_block;
Jan Karaff116fc2007-05-08 00:35:14 -0700475 struct buffer_head *result = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700477 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700479 uint32_t elen = 0, tmpelen;
480 kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700482 loff_t lbcount = 0, b_off = 0;
483 uint32_t newblocknum, newblock;
484 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int8_t etype;
486 int goal = 0, pgoal = UDF_I_LOCATION(inode).logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700487 int lastblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Jan Karaff116fc2007-05-08 00:35:14 -0700489 prev_epos.offset = udf_file_entry_alloc_offset(inode);
490 prev_epos.block = UDF_I_LOCATION(inode);
491 prev_epos.bh = NULL;
492 cur_epos = next_epos = prev_epos;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700493 b_off = (loff_t) block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700496 alternate between laarr[0] and laarr[1] for locations of the
497 current extent, and the previous extent */
498 do {
499 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700500 brelse(prev_epos.bh);
501 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700502 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700504 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700505 brelse(cur_epos.bh);
506 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700507 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509
510 lbcount += elen;
511
Jan Karaff116fc2007-05-08 00:35:14 -0700512 prev_epos.block = cur_epos.block;
513 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Jan Karaff116fc2007-05-08 00:35:14 -0700515 prev_epos.offset = cur_epos.offset;
516 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700518 if ((etype =
519 udf_next_aext(inode, &next_epos, &eloc, &elen, 1)) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 break;
521
522 c = !c;
523
524 laarr[c].extLength = (etype << 30) | elen;
525 laarr[c].extLocation = eloc;
526
527 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
528 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700529 ((elen + inode->i_sb->s_blocksize - 1) >>
530 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700532 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 } while (lbcount + elen <= b_off);
534
535 b_off -= lbcount;
536 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700537 /*
538 * Move prev_epos and cur_epos into indirect extent if we are at
539 * the pointer to it
540 */
541 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
542 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700545 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700547 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
548 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700550 ((elen + inode->i_sb->s_blocksize - 1) &
551 ~(inode->i_sb->s_blocksize - 1));
Jan Karaff116fc2007-05-08 00:35:14 -0700552 etype = udf_write_aext(inode, &cur_epos, eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700554 brelse(prev_epos.bh);
555 brelse(cur_epos.bh);
556 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 newblock = udf_get_lb_pblock(inode->i_sb, eloc, offset);
558 *phys = newblock;
559 return NULL;
560 }
561
Jan Kara31170b62007-05-08 00:35:21 -0700562 last_block = block;
563 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700564 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700565 int ret;
566
567 if (count) {
568 if (c)
569 laarr[0] = laarr[1];
570 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700571 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700572 /* Create a fake extent when there's not one */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700573 memset(&laarr[0].extLocation, 0x00,
574 sizeof(kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700575 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
576 /* Will udf_extend_file() create real extent from a fake one? */
577 startnum = (offset > 0);
578 }
579 /* Create extents for the hole between EOF and offset */
580 ret = udf_extend_file(inode, &prev_epos, laarr, offset);
581 if (ret == -1) {
582 brelse(prev_epos.bh);
583 brelse(cur_epos.bh);
584 brelse(next_epos.bh);
585 /* We don't really know the error here so we just make
586 * something up */
587 *err = -ENOSPC;
588 return NULL;
589 }
590 c = 0;
591 offset = 0;
592 count += ret;
593 /* We are not covered by a preallocated extent? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700594 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
595 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700596 /* Is there any real extent? - otherwise we overwrite
597 * the fake one... */
598 if (count)
599 c = !c;
600 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700601 inode->i_sb->s_blocksize;
602 memset(&laarr[c].extLocation, 0x00,
603 sizeof(kernel_lb_addr));
604 count++;
605 endnum++;
Jan Kara31170b62007-05-08 00:35:21 -0700606 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700607 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700609 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 endnum = startnum = ((count > 2) ? 2 : count);
611
Jan Kara31170b62007-05-08 00:35:21 -0700612 /* if the current extent is in position 0, swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700613 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700614 laarr[2] = laarr[0];
615 laarr[0] = laarr[1];
616 laarr[1] = laarr[2];
617 c = 1;
618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Jan Kara31170b62007-05-08 00:35:21 -0700620 /* if the current block is located in an extent, read the next extent */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700621 if ((etype =
622 udf_next_aext(inode, &next_epos, &eloc, &elen, 0)) != -1) {
623 laarr[c + 1].extLength = (etype << 30) | elen;
624 laarr[c + 1].extLocation = eloc;
625 count++;
626 startnum++;
627 endnum++;
628 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 lastblock = 1;
Jan Kara31170b62007-05-08 00:35:21 -0700630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700634 block in the extent corresponding to the requested block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
636 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700637 else { /* otherwise, allocate a new block */
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (UDF_I_NEXT_ALLOC_BLOCK(inode) == block)
640 goal = UDF_I_NEXT_ALLOC_GOAL(inode);
641
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700642 if (!goal) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!(goal = pgoal))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700644 goal =
645 UDF_I_LOCATION(inode).logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647
648 if (!(newblocknum = udf_new_block(inode->i_sb, inode,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700649 UDF_I_LOCATION(inode).
650 partitionReferenceNum, goal,
651 err))) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700652 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 *err = -ENOSPC;
654 return NULL;
655 }
656 UDF_I_LENEXTENTS(inode) += inode->i_sb->s_blocksize;
657 }
658
659 /* if the extent the requsted block is located in contains multiple blocks,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700660 split the extent into at most three extents. blocks prior to requested
661 block, requested block, and blocks after requested block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
663
664#ifdef UDF_PREALLOCATE
665 /* preallocate blocks */
666 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
667#endif
668
669 /* merge any continuous blocks in laarr */
670 udf_merge_extents(inode, laarr, &endnum);
671
672 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700673 of extents is greater than the old number, and deleting extents if
674 the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700675 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Jan Kara3bf25cb2007-05-08 00:35:16 -0700677 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 if (!(newblock = udf_get_pblock(inode->i_sb, newblocknum,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700680 UDF_I_LOCATION(inode).
681 partitionReferenceNum, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return NULL;
683 }
684 *phys = newblock;
685 *err = 0;
686 *new = 1;
687 UDF_I_NEXT_ALLOC_BLOCK(inode) = block;
688 UDF_I_NEXT_ALLOC_GOAL(inode) = newblocknum;
689 inode->i_ctime = current_fs_time(inode->i_sb);
690
691 if (IS_SYNC(inode))
692 udf_sync_inode(inode);
693 else
694 mark_inode_dirty(inode);
695 return result;
696}
697
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700698static void udf_split_extents(struct inode *inode, int *c, int offset,
699 int newblocknum,
700 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
701 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700704 (laarr[*c].extLength >> 30) ==
705 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 int curr = *c;
707 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700708 inode->i_sb->s_blocksize -
709 1) >> inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 int8_t etype = (laarr[curr].extLength >> 30);
711
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700712 if (blen == 1) ;
713 else if (!offset || blen == offset + 1) {
714 laarr[curr + 2] = laarr[curr + 1];
715 laarr[curr + 1] = laarr[curr];
716 } else {
717 laarr[curr + 3] = laarr[curr + 1];
718 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700721 if (offset) {
722 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
723 udf_free_blocks(inode->i_sb, inode,
724 laarr[curr].extLocation, 0,
725 offset);
726 laarr[curr].extLength =
727 EXT_NOT_RECORDED_NOT_ALLOCATED | (offset <<
728 inode->
729 i_sb->
730 s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 laarr[curr].extLocation.logicalBlockNum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700732 laarr[curr].extLocation.partitionReferenceNum =
733 0;
734 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 laarr[curr].extLength = (etype << 30) |
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700736 (offset << inode->i_sb->s_blocksize_bits);
737 curr++;
738 (*c)++;
739 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 laarr[curr].extLocation.logicalBlockNum = newblocknum;
743 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
744 laarr[curr].extLocation.partitionReferenceNum =
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700745 UDF_I_LOCATION(inode).partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700747 inode->i_sb->s_blocksize;
748 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700750 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700752 laarr[curr].extLocation.logicalBlockNum +=
753 (offset + 1);
754 laarr[curr].extLength =
755 (etype << 30) | ((blen - (offset + 1)) << inode->
756 i_sb->s_blocksize_bits);
757 curr++;
758 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760 }
761}
762
763static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700764 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
765 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 int start, length = 0, currlength = 0, i;
768
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700769 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (!lastblock)
771 return;
772 else
773 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700774 } else {
775 if ((laarr[c + 1].extLength >> 30) ==
776 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
777 start = c + 1;
778 length = currlength =
779 (((laarr[c + 1].
780 extLength & UDF_EXTENT_LENGTH_MASK) +
781 inode->i_sb->s_blocksize -
782 1) >> inode->i_sb->s_blocksize_bits);
783 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 start = c;
785 }
786
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700787 for (i = start + 1; i <= *endnum; i++) {
788 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (lastblock)
790 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700791 } else if ((laarr[i].extLength >> 30) ==
792 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
793 length +=
794 (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
795 inode->i_sb->s_blocksize -
796 1) >> inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 else
798 break;
799 }
800
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700801 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700803 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
804 inode->i_sb->s_blocksize -
805 1) >> inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700807 laarr[start].extLocation.
808 partitionReferenceNum,
809 next,
810 (UDF_DEFAULT_PREALLOC_BLOCKS
811 >
812 length ? length :
813 UDF_DEFAULT_PREALLOC_BLOCKS)
814 - currlength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700816 if (numalloc) {
817 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 laarr[start].extLength +=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700819 (numalloc << inode->i_sb->s_blocksize_bits);
820 else {
821 memmove(&laarr[c + 2], &laarr[c + 1],
822 sizeof(long_ad) * (*endnum - (c + 1)));
823 (*endnum)++;
824 laarr[c + 1].extLocation.logicalBlockNum = next;
825 laarr[c + 1].extLocation.partitionReferenceNum =
826 laarr[c].extLocation.partitionReferenceNum;
827 laarr[c + 1].extLength =
828 EXT_NOT_RECORDED_ALLOCATED | (numalloc <<
829 inode->i_sb->
830 s_blocksize_bits);
831 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700834 for (i = start + 1; numalloc && i < *endnum; i++) {
835 int elen =
836 ((laarr[i].
837 extLength & UDF_EXTENT_LENGTH_MASK) +
838 inode->i_sb->s_blocksize -
839 1) >> inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700841 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 laarr[i].extLength -=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700843 (numalloc << inode->i_sb->
844 s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700846 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700848 if (*endnum > (i + 1))
849 memmove(&laarr[i],
850 &laarr[i + 1],
851 sizeof(long_ad) *
852 (*endnum - (i + 1)));
853 i--;
854 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700857 UDF_I_LENEXTENTS(inode) +=
858 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860 }
861}
862
863static void udf_merge_extents(struct inode *inode,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700864 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
865 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 int i;
868
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700869 for (i = 0; i < (*endnum - 1); i++) {
870 if ((laarr[i].extLength >> 30) ==
871 (laarr[i + 1].extLength >> 30)) {
872 if (((laarr[i].extLength >> 30) ==
873 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
874 ||
875 ((laarr[i + 1].extLocation.logicalBlockNum -
876 laarr[i].extLocation.logicalBlockNum) ==
877 (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
878 inode->i_sb->s_blocksize -
879 1) >> inode->i_sb->s_blocksize_bits))) {
880 if (((laarr[i].
881 extLength & UDF_EXTENT_LENGTH_MASK) +
882 (laarr[i + 1].
883 extLength & UDF_EXTENT_LENGTH_MASK) +
884 inode->i_sb->s_blocksize -
885 1) & ~UDF_EXTENT_LENGTH_MASK) {
886 laarr[i + 1].extLength =
887 (laarr[i + 1].extLength -
888 (laarr[i].
889 extLength &
890 UDF_EXTENT_LENGTH_MASK) +
891 UDF_EXTENT_LENGTH_MASK) & ~(inode->
892 i_sb->
893 s_blocksize
894 - 1);
895 laarr[i].extLength =
896 (laarr[i].
897 extLength & UDF_EXTENT_FLAG_MASK) +
898 (UDF_EXTENT_LENGTH_MASK + 1) -
899 inode->i_sb->s_blocksize;
900 laarr[i +
901 1].extLocation.logicalBlockNum =
902 laarr[i].extLocation.
903 logicalBlockNum +
904 ((laarr[i].
905 extLength &
906 UDF_EXTENT_LENGTH_MASK) >> inode->
907 i_sb->s_blocksize_bits);
908 } else {
909 laarr[i].extLength =
910 laarr[i + 1].extLength +
911 (((laarr[i].
912 extLength &
913 UDF_EXTENT_LENGTH_MASK) +
914 inode->i_sb->s_blocksize -
915 1) & ~(inode->i_sb->s_blocksize -
916 1));
917 if (*endnum > (i + 2))
918 memmove(&laarr[i + 1],
919 &laarr[i + 2],
920 sizeof(long_ad) *
921 (*endnum - (i + 2)));
922 i--;
923 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
925 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700926 } else
927 if (((laarr[i].extLength >> 30) ==
928 (EXT_NOT_RECORDED_ALLOCATED >> 30))
929 && ((laarr[i + 1].extLength >> 30) ==
930 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
931 udf_free_blocks(inode->i_sb, inode,
932 laarr[i].extLocation, 0,
933 ((laarr[i].
934 extLength & UDF_EXTENT_LENGTH_MASK) +
935 inode->i_sb->s_blocksize -
936 1) >> inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 laarr[i].extLocation.logicalBlockNum = 0;
938 laarr[i].extLocation.partitionReferenceNum = 0;
939
940 if (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700941 (laarr[i + 1].extLength & UDF_EXTENT_LENGTH_MASK) +
942 inode->i_sb->s_blocksize -
943 1) & ~UDF_EXTENT_LENGTH_MASK) {
944 laarr[i + 1].extLength =
945 (laarr[i + 1].extLength -
946 (laarr[i].
947 extLength & UDF_EXTENT_LENGTH_MASK) +
948 UDF_EXTENT_LENGTH_MASK) & ~(inode->i_sb->
949 s_blocksize -
950 1);
951 laarr[i].extLength =
952 (laarr[i].
953 extLength & UDF_EXTENT_FLAG_MASK) +
954 (UDF_EXTENT_LENGTH_MASK + 1) -
955 inode->i_sb->s_blocksize;
956 } else {
957 laarr[i].extLength = laarr[i + 1].extLength +
958 (((laarr[i].
959 extLength & UDF_EXTENT_LENGTH_MASK) +
960 inode->i_sb->s_blocksize -
961 1) & ~(inode->i_sb->s_blocksize - 1));
962 if (*endnum > (i + 2))
963 memmove(&laarr[i + 1], &laarr[i + 2],
964 sizeof(long_ad) * (*endnum -
965 (i + 2)));
966 i--;
967 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700969 } else if ((laarr[i].extLength >> 30) ==
970 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
971 udf_free_blocks(inode->i_sb, inode,
972 laarr[i].extLocation, 0,
973 ((laarr[i].
974 extLength & UDF_EXTENT_LENGTH_MASK) +
975 inode->i_sb->s_blocksize -
976 1) >> inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 laarr[i].extLocation.logicalBlockNum = 0;
978 laarr[i].extLocation.partitionReferenceNum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700979 laarr[i].extLength =
980 (laarr[i].
981 extLength & UDF_EXTENT_LENGTH_MASK) |
982 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984 }
985}
986
987static void udf_update_extents(struct inode *inode,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700988 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
989 int startnum, int endnum,
990 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 int start = 0, i;
993 kernel_lb_addr tmploc;
994 uint32_t tmplen;
995
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700996 if (startnum > endnum) {
997 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -0700998 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700999 laarr[i].extLength);
1000 } else if (startnum < endnum) {
1001 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001002 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001003 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -07001004 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001005 &laarr[i].extLength, 1);
1006 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008 }
1009
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001010 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001011 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1012 udf_write_aext(inode, epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001013 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015}
1016
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001017struct buffer_head *udf_bread(struct inode *inode, int block,
1018 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001020 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 bh = udf_getblk(inode, block, create, err);
1023 if (!bh)
1024 return NULL;
1025
1026 if (buffer_uptodate(bh))
1027 return bh;
1028 ll_rw_block(READ, 1, &bh);
1029 wait_on_buffer(bh);
1030 if (buffer_uptodate(bh))
1031 return bh;
1032 brelse(bh);
1033 *err = -EIO;
1034 return NULL;
1035}
1036
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001037void udf_truncate(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 int offset;
1040 int err;
1041
1042 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001043 S_ISLNK(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return;
1045 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1046 return;
1047
1048 lock_kernel();
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001049 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB) {
1050 if (inode->i_sb->s_blocksize <
1051 (udf_file_entry_alloc_offset(inode) + inode->i_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 udf_expand_file_adinicb(inode, inode->i_size, &err);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001053 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 inode->i_size = UDF_I_LENALLOC(inode);
1055 unlock_kernel();
1056 return;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001057 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 udf_truncate_extents(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001059 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001061 memset(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode) +
1062 offset, 0x00,
1063 inode->i_sb->s_blocksize - offset -
1064 udf_file_entry_alloc_offset(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 UDF_I_LENALLOC(inode) = inode->i_size;
1066 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001067 } else {
1068 block_truncate_page(inode->i_mapping, inode->i_size,
1069 udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 udf_truncate_extents(inode);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1074 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001075 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 else
1077 mark_inode_dirty(inode);
1078 unlock_kernel();
1079}
1080
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001081static void __udf_read_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 struct buffer_head *bh = NULL;
1084 struct fileEntry *fe;
1085 uint16_t ident;
1086
1087 /*
1088 * Set defaults, but the inode is still incomplete!
1089 * Note: get_new_inode() sets the following on a new inode:
1090 * i_sb = sb
1091 * i_no = ino
1092 * i_flags = sb->s_flags
1093 * i_state = 0
1094 * clean_inode(): zero fills and sets
1095 * i_count = 1
1096 * i_nlink = 1
1097 * i_op = NULL;
1098 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 bh = udf_read_ptagged(inode->i_sb, UDF_I_LOCATION(inode), 0, &ident);
1100
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001101 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001103 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 make_bad_inode(inode);
1105 return;
1106 }
1107
1108 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001109 ident != TAG_IDENT_USE) {
1110 printk(KERN_ERR
1111 "udf: udf_read_inode(ino %ld) failed ident=%d\n",
1112 inode->i_ino, ident);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001113 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 make_bad_inode(inode);
1115 return;
1116 }
1117
1118 fe = (struct fileEntry *)bh->b_data;
1119
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001120 if (le16_to_cpu(fe->icbTag.strategyType) == 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 struct buffer_head *ibh = NULL, *nbh = NULL;
1122 struct indirectEntry *ie;
1123
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001124 ibh =
1125 udf_read_ptagged(inode->i_sb, UDF_I_LOCATION(inode), 1,
1126 &ident);
1127 if (ident == TAG_IDENT_IE) {
1128 if (ibh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 kernel_lb_addr loc;
1130 ie = (struct indirectEntry *)ibh->b_data;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001133
1134 if (ie->indirectICB.extLength &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001135 (nbh =
1136 udf_read_ptagged(inode->i_sb, loc, 0,
1137 &ident))) {
1138 if (ident == TAG_IDENT_FE
1139 || ident == TAG_IDENT_EFE) {
1140 memcpy(&UDF_I_LOCATION(inode),
1141 &loc,
1142 sizeof(kernel_lb_addr));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001143 brelse(bh);
1144 brelse(ibh);
1145 brelse(nbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 __udf_read_inode(inode);
1147 return;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001148 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001149 brelse(nbh);
1150 brelse(ibh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001152 } else
Jan Kara3bf25cb2007-05-08 00:35:16 -07001153 brelse(ibh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001155 } else
Jan Kara3bf25cb2007-05-08 00:35:16 -07001156 brelse(ibh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001157 } else if (le16_to_cpu(fe->icbTag.strategyType) != 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 printk(KERN_ERR "udf: unsupported strategy type: %d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001159 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001160 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 make_bad_inode(inode);
1162 return;
1163 }
1164 udf_fill_inode(inode, bh);
Jan Kara31170b62007-05-08 00:35:21 -07001165
Jan Kara3bf25cb2007-05-08 00:35:16 -07001166 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
1169static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1170{
1171 struct fileEntry *fe;
1172 struct extendedFileEntry *efe;
1173 time_t convtime;
1174 long convtime_usec;
1175 int offset;
1176
1177 fe = (struct fileEntry *)bh->b_data;
1178 efe = (struct extendedFileEntry *)bh->b_data;
1179
1180 if (le16_to_cpu(fe->icbTag.strategyType) == 4)
1181 UDF_I_STRAT4096(inode) = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001182 else /* if (le16_to_cpu(fe->icbTag.strategyType) == 4096) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 UDF_I_STRAT4096(inode) = 1;
1184
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001185 UDF_I_ALLOCTYPE(inode) =
1186 le16_to_cpu(fe->icbTag.flags) & ICBTAG_FLAG_AD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 UDF_I_UNIQUE(inode) = 0;
1188 UDF_I_LENEATTR(inode) = 0;
1189 UDF_I_LENEXTENTS(inode) = 0;
1190 UDF_I_LENALLOC(inode) = 0;
1191 UDF_I_NEXT_ALLOC_BLOCK(inode) = 0;
1192 UDF_I_NEXT_ALLOC_GOAL(inode) = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001193 if (le16_to_cpu(fe->descTag.tagIdent) == TAG_IDENT_EFE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 UDF_I_EFE(inode) = 1;
1195 UDF_I_USE(inode) = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001196 if (udf_alloc_i_data
1197 (inode,
1198 inode->i_sb->s_blocksize -
1199 sizeof(struct extendedFileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001200 make_bad_inode(inode);
1201 return;
1202 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001203 memcpy(UDF_I_DATA(inode),
1204 bh->b_data + sizeof(struct extendedFileEntry),
1205 inode->i_sb->s_blocksize -
1206 sizeof(struct extendedFileEntry));
1207 } else if (le16_to_cpu(fe->descTag.tagIdent) == TAG_IDENT_FE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 UDF_I_EFE(inode) = 0;
1209 UDF_I_USE(inode) = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001210 if (udf_alloc_i_data
1211 (inode,
1212 inode->i_sb->s_blocksize - sizeof(struct fileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001213 make_bad_inode(inode);
1214 return;
1215 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001216 memcpy(UDF_I_DATA(inode), bh->b_data + sizeof(struct fileEntry),
1217 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1218 } else if (le16_to_cpu(fe->descTag.tagIdent) == TAG_IDENT_USE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 UDF_I_EFE(inode) = 0;
1220 UDF_I_USE(inode) = 1;
1221 UDF_I_LENALLOC(inode) =
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001222 le32_to_cpu(((struct unallocSpaceEntry *)bh->b_data)->
1223 lengthAllocDescs);
1224 if (udf_alloc_i_data
1225 (inode,
1226 inode->i_sb->s_blocksize -
1227 sizeof(struct unallocSpaceEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001228 make_bad_inode(inode);
1229 return;
1230 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001231 memcpy(UDF_I_DATA(inode),
1232 bh->b_data + sizeof(struct unallocSpaceEntry),
1233 inode->i_sb->s_blocksize -
1234 sizeof(struct unallocSpaceEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return;
1236 }
1237
1238 inode->i_uid = le32_to_cpu(fe->uid);
Phillip Susi4d6660e2006-03-07 21:55:24 -08001239 if (inode->i_uid == -1 || UDF_QUERY_FLAG(inode->i_sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001240 UDF_FLAG_UID_IGNORE))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001241 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 inode->i_gid = le32_to_cpu(fe->gid);
Phillip Susi4d6660e2006-03-07 21:55:24 -08001244 if (inode->i_gid == -1 || UDF_QUERY_FLAG(inode->i_sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001245 UDF_FLAG_GID_IGNORE))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001246 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
1249 if (!inode->i_nlink)
1250 inode->i_nlink = 1;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 inode->i_size = le64_to_cpu(fe->informationLength);
1253 UDF_I_LENEXTENTS(inode) = inode->i_size;
1254
1255 inode->i_mode = udf_convert_permissions(fe);
1256 inode->i_mode &= ~UDF_SB(inode->i_sb)->s_umask;
1257
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001258 if (UDF_I_EFE(inode) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001260 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001262 if (udf_stamp_to_time(&convtime, &convtime_usec,
1263 lets_to_cpu(fe->accessTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 inode->i_atime.tv_sec = convtime;
1265 inode->i_atime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001266 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 inode->i_atime = UDF_SB_RECORDTIME(inode->i_sb);
1268 }
1269
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001270 if (udf_stamp_to_time(&convtime, &convtime_usec,
1271 lets_to_cpu(fe->modificationTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 inode->i_mtime.tv_sec = convtime;
1273 inode->i_mtime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001274 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 inode->i_mtime = UDF_SB_RECORDTIME(inode->i_sb);
1276 }
1277
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001278 if (udf_stamp_to_time(&convtime, &convtime_usec,
1279 lets_to_cpu(fe->attrTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 inode->i_ctime.tv_sec = convtime;
1281 inode->i_ctime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001282 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 inode->i_ctime = UDF_SB_RECORDTIME(inode->i_sb);
1284 }
1285
1286 UDF_I_UNIQUE(inode) = le64_to_cpu(fe->uniqueID);
1287 UDF_I_LENEATTR(inode) = le32_to_cpu(fe->lengthExtendedAttr);
1288 UDF_I_LENALLOC(inode) = le32_to_cpu(fe->lengthAllocDescs);
1289 offset = sizeof(struct fileEntry) + UDF_I_LENEATTR(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001290 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001291 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001292 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001294 if (udf_stamp_to_time(&convtime, &convtime_usec,
1295 lets_to_cpu(efe->accessTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 inode->i_atime.tv_sec = convtime;
1297 inode->i_atime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001298 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 inode->i_atime = UDF_SB_RECORDTIME(inode->i_sb);
1300 }
1301
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001302 if (udf_stamp_to_time(&convtime, &convtime_usec,
1303 lets_to_cpu(efe->modificationTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 inode->i_mtime.tv_sec = convtime;
1305 inode->i_mtime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001306 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 inode->i_mtime = UDF_SB_RECORDTIME(inode->i_sb);
1308 }
1309
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001310 if (udf_stamp_to_time(&convtime, &convtime_usec,
1311 lets_to_cpu(efe->createTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 UDF_I_CRTIME(inode).tv_sec = convtime;
1313 UDF_I_CRTIME(inode).tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001314 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 UDF_I_CRTIME(inode) = UDF_SB_RECORDTIME(inode->i_sb);
1316 }
1317
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001318 if (udf_stamp_to_time(&convtime, &convtime_usec,
1319 lets_to_cpu(efe->attrTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 inode->i_ctime.tv_sec = convtime;
1321 inode->i_ctime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001322 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 inode->i_ctime = UDF_SB_RECORDTIME(inode->i_sb);
1324 }
1325
1326 UDF_I_UNIQUE(inode) = le64_to_cpu(efe->uniqueID);
1327 UDF_I_LENEATTR(inode) = le32_to_cpu(efe->lengthExtendedAttr);
1328 UDF_I_LENALLOC(inode) = le32_to_cpu(efe->lengthAllocDescs);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001329 offset =
1330 sizeof(struct extendedFileEntry) + UDF_I_LENEATTR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001333 switch (fe->icbTag.fileType) {
1334 case ICBTAG_FILE_TYPE_DIRECTORY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 {
1336 inode->i_op = &udf_dir_inode_operations;
1337 inode->i_fop = &udf_dir_operations;
1338 inode->i_mode |= S_IFDIR;
Dave Hansend8c76e62006-09-30 23:29:04 -07001339 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 break;
1341 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001342 case ICBTAG_FILE_TYPE_REALTIME:
1343 case ICBTAG_FILE_TYPE_REGULAR:
1344 case ICBTAG_FILE_TYPE_UNDEF:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 {
1346 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
1347 inode->i_data.a_ops = &udf_adinicb_aops;
1348 else
1349 inode->i_data.a_ops = &udf_aops;
1350 inode->i_op = &udf_file_inode_operations;
1351 inode->i_fop = &udf_file_operations;
1352 inode->i_mode |= S_IFREG;
1353 break;
1354 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001355 case ICBTAG_FILE_TYPE_BLOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 {
1357 inode->i_mode |= S_IFBLK;
1358 break;
1359 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001360 case ICBTAG_FILE_TYPE_CHAR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 {
1362 inode->i_mode |= S_IFCHR;
1363 break;
1364 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001365 case ICBTAG_FILE_TYPE_FIFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 {
1367 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1368 break;
1369 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001370 case ICBTAG_FILE_TYPE_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 {
1372 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1373 break;
1374 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001375 case ICBTAG_FILE_TYPE_SYMLINK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 {
1377 inode->i_data.a_ops = &udf_symlink_aops;
1378 inode->i_op = &page_symlink_inode_operations;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001379 inode->i_mode = S_IFLNK | S_IRWXUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 break;
1381 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001382 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001384 printk(KERN_ERR
1385 "udf: udf_fill_inode(ino %ld) failed unknown file type=%d\n",
1386 inode->i_ino, fe->icbTag.fileType);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 make_bad_inode(inode);
1388 return;
1389 }
1390 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001391 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1392 struct deviceSpec *dsea = (struct deviceSpec *)
1393 udf_get_extendedattr(inode, 12, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001395 if (dsea) {
1396 init_special_inode(inode, inode->i_mode,
1397 MKDEV(le32_to_cpu
1398 (dsea->majorDeviceIdent),
1399 le32_to_cpu(dsea->
1400 minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 /* Developer ID ??? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001402 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 make_bad_inode(inode);
1404 }
1405 }
1406}
1407
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001408static int udf_alloc_i_data(struct inode *inode, size_t size)
1409{
1410 UDF_I_DATA(inode) = kmalloc(size, GFP_KERNEL);
1411
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001412 if (!UDF_I_DATA(inode)) {
1413 printk(KERN_ERR
1414 "udf:udf_alloc_i_data (ino %ld) no free memory\n",
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001415 inode->i_ino);
1416 return -ENOMEM;
1417 }
1418
1419 return 0;
1420}
1421
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001422static mode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 mode_t mode;
1425 uint32_t permissions;
1426 uint32_t flags;
1427
1428 permissions = le32_to_cpu(fe->permissions);
1429 flags = le16_to_cpu(fe->icbTag.flags);
1430
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001431 mode = ((permissions) & S_IRWXO) |
1432 ((permissions >> 2) & S_IRWXG) |
1433 ((permissions >> 4) & S_IRWXU) |
1434 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1435 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1436 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 return mode;
1439}
1440
1441/*
1442 * udf_write_inode
1443 *
1444 * PURPOSE
1445 * Write out the specified inode.
1446 *
1447 * DESCRIPTION
1448 * This routine is called whenever an inode is synced.
1449 * Currently this routine is just a placeholder.
1450 *
1451 * HISTORY
1452 * July 1, 1997 - Andrew E. Mileski
1453 * Written, tested, and released.
1454 */
1455
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001456int udf_write_inode(struct inode *inode, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 int ret;
1459 lock_kernel();
1460 ret = udf_update_inode(inode, sync);
1461 unlock_kernel();
1462 return ret;
1463}
1464
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001465int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 return udf_update_inode(inode, 1);
1468}
1469
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001470static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 struct buffer_head *bh = NULL;
1473 struct fileEntry *fe;
1474 struct extendedFileEntry *efe;
1475 uint32_t udfperms;
1476 uint16_t icbflags;
1477 uint16_t crclen;
1478 int i;
1479 kernel_timestamp cpu_time;
1480 int err = 0;
1481
1482 bh = udf_tread(inode->i_sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001483 udf_get_lb_pblock(inode->i_sb, UDF_I_LOCATION(inode),
1484 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001486 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 udf_debug("bread failure\n");
1488 return -EIO;
1489 }
1490
1491 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
1492
1493 fe = (struct fileEntry *)bh->b_data;
1494 efe = (struct extendedFileEntry *)bh->b_data;
1495
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001496 if (le16_to_cpu(fe->descTag.tagIdent) == TAG_IDENT_USE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 struct unallocSpaceEntry *use =
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001498 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 use->lengthAllocDescs = cpu_to_le32(UDF_I_LENALLOC(inode));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001501 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1502 UDF_I_DATA(inode),
1503 inode->i_sb->s_blocksize -
1504 sizeof(struct unallocSpaceEntry));
1505 crclen =
1506 sizeof(struct unallocSpaceEntry) + UDF_I_LENALLOC(inode) -
1507 sizeof(tag);
1508 use->descTag.tagLocation =
1509 cpu_to_le32(UDF_I_LOCATION(inode).logicalBlockNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 use->descTag.descCRCLength = cpu_to_le16(crclen);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001511 use->descTag.descCRC =
1512 cpu_to_le16(udf_crc((char *)use + sizeof(tag), crclen, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 use->descTag.tagChecksum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001515 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (i != 4)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001517 use->descTag.tagChecksum +=
1518 ((uint8_t *) & (use->descTag))[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 mark_buffer_dirty(bh);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001521 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return err;
1523 }
1524
Phillip Susi4d6660e2006-03-07 21:55:24 -08001525 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1526 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001527 else
1528 fe->uid = cpu_to_le32(inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Phillip Susi4d6660e2006-03-07 21:55:24 -08001530 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1531 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001532 else
1533 fe->gid = cpu_to_le32(inode->i_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001535 udfperms = ((inode->i_mode & S_IRWXO)) |
1536 ((inode->i_mode & S_IRWXG) << 2) | ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001538 udfperms |= (le32_to_cpu(fe->permissions) &
1539 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1540 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1541 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 fe->permissions = cpu_to_le32(udfperms);
1543
1544 if (S_ISDIR(inode->i_mode))
1545 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1546 else
1547 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1548
1549 fe->informationLength = cpu_to_le64(inode->i_size);
1550
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001551 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 regid *eid;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001553 struct deviceSpec *dsea = (struct deviceSpec *)
1554 udf_get_extendedattr(inode, 12, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001556 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 dsea = (struct deviceSpec *)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001558 udf_add_extendedattr(inode,
1559 sizeof(struct deviceSpec) +
1560 sizeof(regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 dsea->attrType = cpu_to_le32(12);
1562 dsea->attrSubtype = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001563 dsea->attrLength =
1564 cpu_to_le32(sizeof(struct deviceSpec) +
1565 sizeof(regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 dsea->impUseLength = cpu_to_le32(sizeof(regid));
1567 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001568 eid = (regid *) dsea->impUse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 memset(eid, 0, sizeof(regid));
1570 strcpy(eid->ident, UDF_ID_DEVELOPER);
1571 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1572 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1573 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1574 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1575 }
1576
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001577 if (UDF_I_EFE(inode) == 0) {
1578 memcpy(bh->b_data + sizeof(struct fileEntry), UDF_I_DATA(inode),
1579 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1580 fe->logicalBlocksRecorded =
1581 cpu_to_le64((inode->i_blocks +
1582 (1 << (inode->i_sb->s_blocksize_bits - 9)) -
1583 1) >> (inode->i_sb->s_blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1586 fe->accessTime = cpu_to_lets(cpu_time);
1587 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1588 fe->modificationTime = cpu_to_lets(cpu_time);
1589 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1590 fe->attrTime = cpu_to_lets(cpu_time);
1591 memset(&(fe->impIdent), 0, sizeof(regid));
1592 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1593 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1594 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1595 fe->uniqueID = cpu_to_le64(UDF_I_UNIQUE(inode));
1596 fe->lengthExtendedAttr = cpu_to_le32(UDF_I_LENEATTR(inode));
1597 fe->lengthAllocDescs = cpu_to_le32(UDF_I_LENALLOC(inode));
1598 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1599 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001600 } else {
1601 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1602 UDF_I_DATA(inode),
1603 inode->i_sb->s_blocksize -
1604 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 efe->objectSize = cpu_to_le64(inode->i_size);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001606 efe->logicalBlocksRecorded = cpu_to_le64((inode->i_blocks +
1607 (1 <<
1608 (inode->i_sb->
1609 s_blocksize_bits -
1610 9)) -
1611 1) >> (inode->i_sb->
1612 s_blocksize_bits
1613 - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 if (UDF_I_CRTIME(inode).tv_sec > inode->i_atime.tv_sec ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001616 (UDF_I_CRTIME(inode).tv_sec == inode->i_atime.tv_sec &&
1617 UDF_I_CRTIME(inode).tv_nsec > inode->i_atime.tv_nsec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 UDF_I_CRTIME(inode) = inode->i_atime;
1619 }
1620 if (UDF_I_CRTIME(inode).tv_sec > inode->i_mtime.tv_sec ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001621 (UDF_I_CRTIME(inode).tv_sec == inode->i_mtime.tv_sec &&
1622 UDF_I_CRTIME(inode).tv_nsec > inode->i_mtime.tv_nsec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 UDF_I_CRTIME(inode) = inode->i_mtime;
1624 }
1625 if (UDF_I_CRTIME(inode).tv_sec > inode->i_ctime.tv_sec ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001626 (UDF_I_CRTIME(inode).tv_sec == inode->i_ctime.tv_sec &&
1627 UDF_I_CRTIME(inode).tv_nsec > inode->i_ctime.tv_nsec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 UDF_I_CRTIME(inode) = inode->i_ctime;
1629 }
1630
1631 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1632 efe->accessTime = cpu_to_lets(cpu_time);
1633 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1634 efe->modificationTime = cpu_to_lets(cpu_time);
1635 if (udf_time_to_stamp(&cpu_time, UDF_I_CRTIME(inode)))
1636 efe->createTime = cpu_to_lets(cpu_time);
1637 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1638 efe->attrTime = cpu_to_lets(cpu_time);
1639
1640 memset(&(efe->impIdent), 0, sizeof(regid));
1641 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1642 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1643 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1644 efe->uniqueID = cpu_to_le64(UDF_I_UNIQUE(inode));
1645 efe->lengthExtendedAttr = cpu_to_le32(UDF_I_LENEATTR(inode));
1646 efe->lengthAllocDescs = cpu_to_le32(UDF_I_LENALLOC(inode));
1647 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1648 crclen = sizeof(struct extendedFileEntry);
1649 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001650 if (UDF_I_STRAT4096(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 fe->icbTag.strategyType = cpu_to_le16(4096);
1652 fe->icbTag.strategyParameter = cpu_to_le16(1);
1653 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001654 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 fe->icbTag.strategyType = cpu_to_le16(4);
1656 fe->icbTag.numEntries = cpu_to_le16(1);
1657 }
1658
1659 if (S_ISDIR(inode->i_mode))
1660 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1661 else if (S_ISREG(inode->i_mode))
1662 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1663 else if (S_ISLNK(inode->i_mode))
1664 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1665 else if (S_ISBLK(inode->i_mode))
1666 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1667 else if (S_ISCHR(inode->i_mode))
1668 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1669 else if (S_ISFIFO(inode->i_mode))
1670 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1671 else if (S_ISSOCK(inode->i_mode))
1672 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1673
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001674 icbflags = UDF_I_ALLOCTYPE(inode) |
1675 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1676 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1677 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1678 (le16_to_cpu(fe->icbTag.flags) &
1679 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1680 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 fe->icbTag.flags = cpu_to_le16(icbflags);
1683 if (UDF_SB_UDFREV(inode->i_sb) >= 0x0200)
1684 fe->descTag.descVersion = cpu_to_le16(3);
1685 else
1686 fe->descTag.descVersion = cpu_to_le16(2);
1687 fe->descTag.tagSerialNum = cpu_to_le16(UDF_SB_SERIALNUM(inode->i_sb));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001688 fe->descTag.tagLocation =
1689 cpu_to_le32(UDF_I_LOCATION(inode).logicalBlockNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 crclen += UDF_I_LENEATTR(inode) + UDF_I_LENALLOC(inode) - sizeof(tag);
1691 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001692 fe->descTag.descCRC =
1693 cpu_to_le16(udf_crc((char *)fe + sizeof(tag), crclen, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 fe->descTag.tagChecksum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001696 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (i != 4)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001698 fe->descTag.tagChecksum +=
1699 ((uint8_t *) & (fe->descTag))[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 /* write the data blocks */
1702 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001703 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 sync_dirty_buffer(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001705 if (buffer_req(bh) && !buffer_uptodate(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 printk("IO error syncing udf inode [%s:%08lx]\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001707 inode->i_sb->s_id, inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 err = -EIO;
1709 }
1710 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001711 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return err;
1713}
1714
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001715struct inode *udf_iget(struct super_block *sb, kernel_lb_addr ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
1717 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1718 struct inode *inode = iget_locked(sb, block);
1719
1720 if (!inode)
1721 return NULL;
1722
1723 if (inode->i_state & I_NEW) {
1724 memcpy(&UDF_I_LOCATION(inode), &ino, sizeof(kernel_lb_addr));
1725 __udf_read_inode(inode);
1726 unlock_new_inode(inode);
1727 }
1728
1729 if (is_bad_inode(inode))
1730 goto out_iput;
1731
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001732 if (ino.logicalBlockNum >=
1733 UDF_SB_PARTLEN(sb, ino.partitionReferenceNum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 udf_debug("block=%d, partition=%d out of range\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001735 ino.logicalBlockNum, ino.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 make_bad_inode(inode);
1737 goto out_iput;
1738 }
1739
1740 return inode;
1741
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001742 out_iput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 iput(inode);
1744 return NULL;
1745}
1746
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001747int8_t udf_add_aext(struct inode * inode, struct extent_position * epos,
1748 kernel_lb_addr eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
1750 int adsize;
1751 short_ad *sad = NULL;
1752 long_ad *lad = NULL;
1753 struct allocExtDesc *aed;
1754 int8_t etype;
1755 uint8_t *ptr;
1756
Jan Karaff116fc2007-05-08 00:35:14 -07001757 if (!epos->bh)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001758 ptr =
1759 UDF_I_DATA(inode) + epos->offset -
1760 udf_file_entry_alloc_offset(inode) + UDF_I_LENEATTR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 else
Jan Karaff116fc2007-05-08 00:35:14 -07001762 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
1765 adsize = sizeof(short_ad);
1766 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
1767 adsize = sizeof(long_ad);
1768 else
1769 return -1;
1770
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001771 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 char *sptr, *dptr;
1773 struct buffer_head *nbh;
1774 int err, loffset;
Jan Karaff116fc2007-05-08 00:35:14 -07001775 kernel_lb_addr obloc = epos->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001777 if (!
1778 (epos->block.logicalBlockNum =
1779 udf_new_block(inode->i_sb, NULL,
1780 obloc.partitionReferenceNum,
1781 obloc.logicalBlockNum, &err))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return -1;
1783 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001784 if (!
1785 (nbh =
1786 udf_tgetblk(inode->i_sb,
1787 udf_get_lb_pblock(inode->i_sb, epos->block,
1788 0)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return -1;
1790 }
1791 lock_buffer(nbh);
1792 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1793 set_buffer_uptodate(nbh);
1794 unlock_buffer(nbh);
1795 mark_buffer_dirty_inode(nbh, inode);
1796
1797 aed = (struct allocExtDesc *)(nbh->b_data);
1798 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001799 aed->previousAllocExtLocation =
1800 cpu_to_le32(obloc.logicalBlockNum);
1801 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -07001802 loffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 aed->lengthAllocDescs = cpu_to_le32(adsize);
1804 sptr = ptr - adsize;
1805 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1806 memcpy(dptr, sptr, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -07001807 epos->offset = sizeof(struct allocExtDesc) + adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001808 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001809 loffset = epos->offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 aed->lengthAllocDescs = cpu_to_le32(0);
1811 sptr = ptr;
Jan Karaff116fc2007-05-08 00:35:14 -07001812 epos->offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001814 if (epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001815 aed = (struct allocExtDesc *)epos->bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 aed->lengthAllocDescs =
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001817 cpu_to_le32(le32_to_cpu
1818 (aed->lengthAllocDescs) +
1819 adsize);
1820 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 UDF_I_LENALLOC(inode) += adsize;
1822 mark_inode_dirty(inode);
1823 }
1824 }
1825 if (UDF_SB_UDFREV(inode->i_sb) >= 0x0200)
1826 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001827 epos->block.logicalBlockNum, sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 else
1829 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001830 epos->block.logicalBlockNum, sizeof(tag));
1831 switch (UDF_I_ALLOCTYPE(inode)) {
1832 case ICBTAG_FLAG_AD_SHORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001834 sad = (short_ad *) sptr;
1835 sad->extLength =
1836 cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1837 inode->i_sb->s_blocksize);
1838 sad->extPosition =
1839 cpu_to_le32(epos->block.logicalBlockNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 break;
1841 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001842 case ICBTAG_FLAG_AD_LONG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001844 lad = (long_ad *) sptr;
1845 lad->extLength =
1846 cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1847 inode->i_sb->s_blocksize);
Jan Karaff116fc2007-05-08 00:35:14 -07001848 lad->extLocation = cpu_to_lelb(epos->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1850 break;
1851 }
1852 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001853 if (epos->bh) {
1854 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT)
1855 || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -07001856 udf_update_tag(epos->bh->b_data, loffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001858 udf_update_tag(epos->bh->b_data,
1859 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001860 mark_buffer_dirty_inode(epos->bh, inode);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001861 brelse(epos->bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001862 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 mark_inode_dirty(inode);
Jan Karaff116fc2007-05-08 00:35:14 -07001864 epos->bh = nbh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 }
1866
Jan Karaff116fc2007-05-08 00:35:14 -07001867 etype = udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001869 if (!epos->bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 UDF_I_LENALLOC(inode) += adsize;
1871 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001872 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001873 aed = (struct allocExtDesc *)epos->bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 aed->lengthAllocDescs =
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001875 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
1876 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT)
1877 || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
1878 udf_update_tag(epos->bh->b_data,
1879 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001881 udf_update_tag(epos->bh->b_data,
1882 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001883 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
1885
1886 return etype;
1887}
1888
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001889int8_t udf_write_aext(struct inode * inode, struct extent_position * epos,
1890 kernel_lb_addr eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
1892 int adsize;
1893 uint8_t *ptr;
1894
Jan Karaff116fc2007-05-08 00:35:14 -07001895 if (!epos->bh)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001896 ptr =
1897 UDF_I_DATA(inode) + epos->offset -
1898 udf_file_entry_alloc_offset(inode) + UDF_I_LENEATTR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 else
Jan Karaff116fc2007-05-08 00:35:14 -07001900 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001902 switch (UDF_I_ALLOCTYPE(inode)) {
1903 case ICBTAG_FLAG_AD_SHORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001905 short_ad *sad = (short_ad *) ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 sad->extLength = cpu_to_le32(elen);
1907 sad->extPosition = cpu_to_le32(eloc.logicalBlockNum);
1908 adsize = sizeof(short_ad);
1909 break;
1910 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001911 case ICBTAG_FLAG_AD_LONG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001913 long_ad *lad = (long_ad *) ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 lad->extLength = cpu_to_le32(elen);
1915 lad->extLocation = cpu_to_lelb(eloc);
1916 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1917 adsize = sizeof(long_ad);
1918 break;
1919 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001920 default:
1921 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
1923
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001924 if (epos->bh) {
1925 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT)
1926 || UDF_SB_UDFREV(inode->i_sb) >= 0x0201) {
1927 struct allocExtDesc *aed =
1928 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07001929 udf_update_tag(epos->bh->b_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001930 le32_to_cpu(aed->lengthAllocDescs) +
1931 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
Jan Karaff116fc2007-05-08 00:35:14 -07001933 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001934 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 mark_inode_dirty(inode);
1936
1937 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07001938 epos->offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return (elen >> 30);
1940}
1941
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001942int8_t udf_next_aext(struct inode * inode, struct extent_position * epos,
1943 kernel_lb_addr * eloc, uint32_t * elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
1945 int8_t etype;
1946
Jan Karaff116fc2007-05-08 00:35:14 -07001947 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001948 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Jan Karaff116fc2007-05-08 00:35:14 -07001949 epos->block = *eloc;
1950 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001951 brelse(epos->bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001952 if (!
1953 (epos->bh =
1954 udf_tread(inode->i_sb,
1955 udf_get_lb_pblock(inode->i_sb, epos->block,
1956 0)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 udf_debug("reading block %d failed!\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001958 udf_get_lb_pblock(inode->i_sb, epos->block,
1959 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return -1;
1961 }
1962 }
1963
1964 return etype;
1965}
1966
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001967int8_t udf_current_aext(struct inode * inode, struct extent_position * epos,
1968 kernel_lb_addr * eloc, uint32_t * elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
1970 int alen;
1971 int8_t etype;
1972 uint8_t *ptr;
1973
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001974 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001975 if (!epos->offset)
1976 epos->offset = udf_file_entry_alloc_offset(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001977 ptr =
1978 UDF_I_DATA(inode) + epos->offset -
1979 udf_file_entry_alloc_offset(inode) + UDF_I_LENEATTR(inode);
1980 alen =
1981 udf_file_entry_alloc_offset(inode) + UDF_I_LENALLOC(inode);
1982 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001983 if (!epos->offset)
1984 epos->offset = sizeof(struct allocExtDesc);
1985 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001986 alen =
1987 sizeof(struct allocExtDesc) +
1988 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
1989 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
1991
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001992 switch (UDF_I_ALLOCTYPE(inode)) {
1993 case ICBTAG_FLAG_AD_SHORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 {
1995 short_ad *sad;
1996
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001997 if (!
1998 (sad =
1999 udf_get_fileshortad(ptr, alen, &epos->offset,
2000 inc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return -1;
2002
2003 etype = le32_to_cpu(sad->extLength) >> 30;
2004 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002005 eloc->partitionReferenceNum =
2006 UDF_I_LOCATION(inode).partitionReferenceNum;
2007 *elen =
2008 le32_to_cpu(sad->
2009 extLength) & UDF_EXTENT_LENGTH_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 break;
2011 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002012 case ICBTAG_FLAG_AD_LONG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 {
2014 long_ad *lad;
2015
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002016 if (!
2017 (lad =
2018 udf_get_filelongad(ptr, alen, &epos->offset, inc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return -1;
2020
2021 etype = le32_to_cpu(lad->extLength) >> 30;
2022 *eloc = lelb_to_cpu(lad->extLocation);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002023 *elen =
2024 le32_to_cpu(lad->
2025 extLength) & UDF_EXTENT_LENGTH_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 break;
2027 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002028 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002030 udf_debug("alloc_type = %d unsupported\n",
2031 UDF_I_ALLOCTYPE(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return -1;
2033 }
2034 }
2035
2036 return etype;
2037}
2038
2039static int8_t
Jan Karaff116fc2007-05-08 00:35:14 -07002040udf_insert_aext(struct inode *inode, struct extent_position epos,
2041 kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 kernel_lb_addr oeloc;
2044 uint32_t oelen;
2045 int8_t etype;
2046
Jan Karaff116fc2007-05-08 00:35:14 -07002047 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07002048 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002050 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Jan Karaff116fc2007-05-08 00:35:14 -07002051 udf_write_aext(inode, &epos, neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 neloc = oeloc;
2054 nelen = (etype << 30) | oelen;
2055 }
Jan Karaff116fc2007-05-08 00:35:14 -07002056 udf_add_aext(inode, &epos, neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002057 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return (nelen >> 30);
2059}
2060
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002061int8_t udf_delete_aext(struct inode * inode, struct extent_position epos,
2062 kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Jan Karaff116fc2007-05-08 00:35:14 -07002064 struct extent_position oepos;
2065 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 int8_t etype;
2067 struct allocExtDesc *aed;
2068
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002069 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002070 get_bh(epos.bh);
2071 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
2073
2074 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
2075 adsize = sizeof(short_ad);
2076 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
2077 adsize = sizeof(long_ad);
2078 else
2079 adsize = 0;
2080
Jan Karaff116fc2007-05-08 00:35:14 -07002081 oepos = epos;
2082 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return -1;
2084
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002085 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Jan Karaff116fc2007-05-08 00:35:14 -07002086 udf_write_aext(inode, &oepos, eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002087 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002088 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07002089 brelse(oepos.bh);
2090 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07002091 oepos.bh = epos.bh;
2092 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
2094 }
2095 memset(&eloc, 0x00, sizeof(kernel_lb_addr));
2096 elen = 0;
2097
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002098 if (epos.bh != oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002099 udf_free_blocks(inode->i_sb, inode, epos.block, 0, 1);
2100 udf_write_aext(inode, &oepos, eloc, elen, 1);
2101 udf_write_aext(inode, &oepos, eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002102 if (!oepos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 UDF_I_LENALLOC(inode) -= (adsize * 2);
2104 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002105 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002106 aed = (struct allocExtDesc *)oepos.bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 aed->lengthAllocDescs =
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002108 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) -
2109 (2 * adsize));
2110 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT)
2111 || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
2112 udf_update_tag(oepos.bh->b_data,
2113 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002115 udf_update_tag(oepos.bh->b_data,
2116 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002117 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002119 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002120 udf_write_aext(inode, &oepos, eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002121 if (!oepos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 UDF_I_LENALLOC(inode) -= adsize;
2123 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002124 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002125 aed = (struct allocExtDesc *)oepos.bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 aed->lengthAllocDescs =
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002127 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) -
2128 adsize);
2129 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT)
2130 || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
2131 udf_update_tag(oepos.bh->b_data,
2132 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002134 udf_update_tag(oepos.bh->b_data,
2135 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002136 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002139
Jan Kara3bf25cb2007-05-08 00:35:16 -07002140 brelse(epos.bh);
2141 brelse(oepos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return (elen >> 30);
2143}
2144
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002145int8_t inode_bmap(struct inode * inode, sector_t block,
2146 struct extent_position * pos, kernel_lb_addr * eloc,
2147 uint32_t * elen, sector_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002149 loff_t lbcount = 0, bcount =
2150 (loff_t) block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 int8_t etype;
2152
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002153 if (block < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 printk(KERN_ERR "udf: inode_bmap: block < 0\n");
2155 return -1;
2156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Jan Karaff116fc2007-05-08 00:35:14 -07002158 pos->offset = 0;
2159 pos->block = UDF_I_LOCATION(inode);
2160 pos->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 *elen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002163 do {
2164 if ((etype = udf_next_aext(inode, pos, eloc, elen, 1)) == -1) {
2165 *offset =
2166 (bcount - lbcount) >> inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 UDF_I_LENEXTENTS(inode) = lbcount;
2168 return -1;
2169 }
2170 lbcount += *elen;
2171 } while (lbcount <= bcount);
2172
Jan Kara60448b12007-05-08 00:35:13 -07002173 *offset = (bcount + *elen - lbcount) >> inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 return etype;
2176}
2177
Jan Kara60448b12007-05-08 00:35:13 -07002178long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Jan Karaff116fc2007-05-08 00:35:14 -07002180 kernel_lb_addr eloc;
2181 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002182 sector_t offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002183 struct extent_position epos = { NULL, 0, {0, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 int ret;
2185
2186 lock_kernel();
2187
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002188 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2189 (EXT_RECORDED_ALLOCATED >> 30))
Jan Kara60448b12007-05-08 00:35:13 -07002190 ret = udf_get_lb_pblock(inode->i_sb, eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 else
2192 ret = 0;
2193
2194 unlock_kernel();
Jan Kara3bf25cb2007-05-08 00:35:16 -07002195 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2198 return udf_fixed_to_variable(ret);
2199 else
2200 return ret;
2201}