blob: 2eb1220e4236d95f91fa69c211d93be4d34fe724 [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 Gorcunov28de7942007-07-21 04:37:18 -0700100
101no_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 clear_inode(inode);
103}
104
Jan Kara74584ae2007-06-16 10:16:14 -0700105/*
106 * If we are going to release inode from memory, we discard preallocation and
107 * truncate last inode extent to proper length. We could use drop_inode() but
108 * it's called under inode_lock and thus we cannot mark inode dirty there. We
109 * use clear_inode() but we have to make sure to write inode as it's not written
110 * automatically.
111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112void udf_clear_inode(struct inode *inode)
113{
114 if (!(inode->i_sb->s_flags & MS_RDONLY)) {
115 lock_kernel();
Jan Kara74584ae2007-06-16 10:16:14 -0700116 /* Discard preallocation for directories, symlinks, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 udf_discard_prealloc(inode);
Jan Kara74584ae2007-06-16 10:16:14 -0700118 udf_truncate_tail_extent(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unlock_kernel();
Jan Kara74584ae2007-06-16 10:16:14 -0700120 write_inode_now(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 kfree(UDF_I_DATA(inode));
123 UDF_I_DATA(inode) = NULL;
124}
125
126static int udf_writepage(struct page *page, struct writeback_control *wbc)
127{
128 return block_write_full_page(page, udf_get_block, wbc);
129}
130
131static int udf_readpage(struct file *file, struct page *page)
132{
133 return block_read_full_page(page, udf_get_block);
134}
135
Nick Pigginbe021ee2007-10-16 01:25:20 -0700136static int udf_write_begin(struct file *file, struct address_space *mapping,
137 loff_t pos, unsigned len, unsigned flags,
138 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Nick Pigginbe021ee2007-10-16 01:25:20 -0700140 *pagep = NULL;
141 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
142 udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145static sector_t udf_bmap(struct address_space *mapping, sector_t block)
146{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700147 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700150const struct address_space_operations udf_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700151 .readpage = udf_readpage,
152 .writepage = udf_writepage,
153 .sync_page = block_sync_page,
Nick Pigginbe021ee2007-10-16 01:25:20 -0700154 .write_begin = udf_write_begin,
155 .write_end = generic_write_end,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700156 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157};
158
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700159void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 struct page *page;
162 char *kaddr;
163 struct writeback_control udf_wbc = {
164 .sync_mode = WB_SYNC_NONE,
165 .nr_to_write = 1,
166 };
167
168 /* from now on we have normal address_space methods */
169 inode->i_data.a_ops = &udf_aops;
170
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700171 if (!UDF_I_LENALLOC(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
173 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_SHORT;
174 else
175 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_LONG;
176 mark_inode_dirty(inode);
177 return;
178 }
179
180 page = grab_cache_page(inode->i_mapping, 0);
Matt Mackallcd7619d2005-05-01 08:59:01 -0700181 BUG_ON(!PageLocked(page));
182
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700183 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 kaddr = kmap(page);
185 memset(kaddr + UDF_I_LENALLOC(inode), 0x00,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700186 PAGE_CACHE_SIZE - UDF_I_LENALLOC(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 memcpy(kaddr, UDF_I_DATA(inode) + UDF_I_LENEATTR(inode),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700188 UDF_I_LENALLOC(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 flush_dcache_page(page);
190 SetPageUptodate(page);
191 kunmap(page);
192 }
193 memset(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode), 0x00,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700194 UDF_I_LENALLOC(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 UDF_I_LENALLOC(inode) = 0;
196 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
197 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_SHORT;
198 else
199 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_LONG;
200
201 inode->i_data.a_ops->writepage(page, &udf_wbc);
202 page_cache_release(page);
203
204 mark_inode_dirty(inode);
205}
206
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700207struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
208 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700211 struct buffer_head *dbh = NULL;
212 kernel_lb_addr eloc;
213 uint32_t elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700215 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 struct udf_fileident_bh sfibh, dfibh;
218 loff_t f_pos = udf_ext0_offset(inode) >> 2;
219 int size = (udf_ext0_offset(inode) + inode->i_size) >> 2;
220 struct fileIdentDesc cfi, *sfi, *dfi;
221
222 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
223 alloctype = ICBTAG_FLAG_AD_SHORT;
224 else
225 alloctype = ICBTAG_FLAG_AD_LONG;
226
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700227 if (!inode->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 UDF_I_ALLOCTYPE(inode) = alloctype;
229 mark_inode_dirty(inode);
230 return NULL;
231 }
232
233 /* alloc block, and copy data to it */
234 *block = udf_new_block(inode->i_sb, inode,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700235 UDF_I_LOCATION(inode).partitionReferenceNum,
236 UDF_I_LOCATION(inode).logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (!(*block))
238 return NULL;
239 newblock = udf_get_pblock(inode->i_sb, *block,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700240 UDF_I_LOCATION(inode).partitionReferenceNum, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (!newblock)
242 return NULL;
243 dbh = udf_tgetblk(inode->i_sb, newblock);
244 if (!dbh)
245 return NULL;
246 lock_buffer(dbh);
247 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
248 set_buffer_uptodate(dbh);
249 unlock_buffer(dbh);
250 mark_buffer_dirty_inode(dbh, inode);
251
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700252 sfibh.soffset = sfibh.eoffset = (f_pos & ((inode->i_sb->s_blocksize - 1) >> 2)) << 2;
Jan Karaff116fc2007-05-08 00:35:14 -0700253 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 dfibh.soffset = dfibh.eoffset = 0;
255 dfibh.sbh = dfibh.ebh = dbh;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700256 while ((f_pos < size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_IN_ICB;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700258 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL, NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700259 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700260 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return NULL;
262 }
263 UDF_I_ALLOCTYPE(inode) = alloctype;
264 sfi->descTag.tagLocation = cpu_to_le32(*block);
265 dfibh.soffset = dfibh.eoffset;
266 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
267 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
268 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700269 sfi->fileIdent + le16_to_cpu(sfi->lengthOfImpUse))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700271 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return NULL;
273 }
274 }
275 mark_buffer_dirty_inode(dbh, inode);
276
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700277 memset(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode), 0, UDF_I_LENALLOC(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 UDF_I_LENALLOC(inode) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 eloc.logicalBlockNum = *block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700280 eloc.partitionReferenceNum = UDF_I_LOCATION(inode).partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 elen = inode->i_size;
282 UDF_I_LENEXTENTS(inode) = elen;
Jan Karaff116fc2007-05-08 00:35:14 -0700283 epos.bh = NULL;
284 epos.block = UDF_I_LOCATION(inode);
285 epos.offset = udf_file_entry_alloc_offset(inode);
286 udf_add_aext(inode, &epos, eloc, elen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* UniqueID stuff */
288
Jan Kara3bf25cb2007-05-08 00:35:16 -0700289 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 mark_inode_dirty(inode);
291 return dbh;
292}
293
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700294static int udf_get_block(struct inode *inode, sector_t block,
295 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 int err, new;
298 struct buffer_head *bh;
299 unsigned long phys;
300
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700301 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 phys = udf_block_map(inode, block);
303 if (phys)
304 map_bh(bh_result, inode->i_sb, phys);
305 return 0;
306 }
307
308 err = -EIO;
309 new = 0;
310 bh = NULL;
311
312 lock_kernel();
313
314 if (block < 0)
315 goto abort_negative;
316
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700317 if (block == UDF_I_NEXT_ALLOC_BLOCK(inode) + 1) {
318 UDF_I_NEXT_ALLOC_BLOCK(inode)++;
319 UDF_I_NEXT_ALLOC_GOAL(inode)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321
322 err = 0;
323
324 bh = inode_getblk(inode, block, &err, &phys, &new);
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200325 BUG_ON(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (err)
327 goto abort;
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200328 BUG_ON(!phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 if (new)
331 set_buffer_new(bh_result);
332 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700333
334abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 unlock_kernel();
336 return err;
337
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700338abort_negative:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 udf_warning(inode->i_sb, "udf_get_block", "block < 0");
340 goto abort;
341}
342
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700343static struct buffer_head *udf_getblk(struct inode *inode, long block,
344 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700346 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct buffer_head dummy;
348
349 dummy.b_state = 0;
350 dummy.b_blocknr = -1000;
351 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700352 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700354 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 lock_buffer(bh);
356 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
357 set_buffer_uptodate(bh);
358 unlock_buffer(bh);
359 mark_buffer_dirty_inode(bh, inode);
360 }
361 return bh;
362 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return NULL;
365}
366
Jan Kara31170b62007-05-08 00:35:21 -0700367/* Extend the file by 'blocks' blocks, return the number of extents added */
368int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700369 kernel_long_ad * last_ext, sector_t blocks)
Jan Kara31170b62007-05-08 00:35:21 -0700370{
371 sector_t add;
372 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
373 struct super_block *sb = inode->i_sb;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700374 kernel_lb_addr prealloc_loc = {};
Jan Kara31170b62007-05-08 00:35:21 -0700375 int prealloc_len = 0;
376
377 /* The previous extent is fake and we should not extend by anything
378 * - there's nothing to do... */
379 if (!blocks && fake)
380 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700381
Jan Kara31170b62007-05-08 00:35:21 -0700382 /* Round the last extent up to a multiple of block size */
383 if (last_ext->extLength & (sb->s_blocksize - 1)) {
384 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700385 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
386 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
387 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Jan Kara31170b62007-05-08 00:35:21 -0700388 UDF_I_LENEXTENTS(inode) =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700389 (UDF_I_LENEXTENTS(inode) + sb->s_blocksize - 1) &
390 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700391 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700392
Jan Kara31170b62007-05-08 00:35:21 -0700393 /* Last extent are just preallocated blocks? */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700394 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) == EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700395 /* Save the extent so that we can reattach it to the end */
396 prealloc_loc = last_ext->extLocation;
397 prealloc_len = last_ext->extLength;
398 /* Mark the extent as a hole */
399 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700400 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700401 last_ext->extLocation.logicalBlockNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700402 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700403 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700404
Jan Kara31170b62007-05-08 00:35:21 -0700405 /* Can we merge with the previous extent? */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700406 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) == EXT_NOT_RECORDED_NOT_ALLOCATED) {
407 add = ((1 << 30) - sb->s_blocksize - (last_ext->extLength &
408 UDF_EXTENT_LENGTH_MASK)) >> sb->s_blocksize_bits;
Jan Kara31170b62007-05-08 00:35:21 -0700409 if (add > blocks)
410 add = blocks;
411 blocks -= add;
412 last_ext->extLength += add << sb->s_blocksize_bits;
413 }
414
415 if (fake) {
416 udf_add_aext(inode, last_pos, last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700417 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700418 count++;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700419 } else {
420 udf_write_aext(inode, last_pos, last_ext->extLocation, last_ext->extLength, 1);
421 }
422
Jan Kara31170b62007-05-08 00:35:21 -0700423 /* Managed to do everything necessary? */
424 if (!blocks)
425 goto out;
426
427 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
428 last_ext->extLocation.logicalBlockNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700429 last_ext->extLocation.partitionReferenceNum = 0;
430 add = (1 << (30-sb->s_blocksize_bits)) - 1;
431 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | (add << sb->s_blocksize_bits);
432
Jan Kara31170b62007-05-08 00:35:21 -0700433 /* Create enough extents to cover the whole hole */
434 while (blocks > add) {
435 blocks -= add;
436 if (udf_add_aext(inode, last_pos, last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700437 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700438 return -1;
439 count++;
440 }
441 if (blocks) {
442 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700443 (blocks << sb->s_blocksize_bits);
Jan Kara31170b62007-05-08 00:35:21 -0700444 if (udf_add_aext(inode, last_pos, last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700445 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700446 return -1;
447 count++;
448 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700449
450out:
Jan Kara31170b62007-05-08 00:35:21 -0700451 /* Do we have some preallocated blocks saved? */
452 if (prealloc_len) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700453 if (udf_add_aext(inode, last_pos, prealloc_loc, prealloc_len, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700454 return -1;
455 last_ext->extLocation = prealloc_loc;
456 last_ext->extLength = prealloc_len;
457 count++;
458 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700459
Jan Kara31170b62007-05-08 00:35:21 -0700460 /* last_pos should point to the last written extent... */
461 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
462 last_pos->offset -= sizeof(short_ad);
463 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
464 last_pos->offset -= sizeof(long_ad);
465 else
466 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700467
Jan Kara31170b62007-05-08 00:35:21 -0700468 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 Gorcunov28de7942007-07-21 04:37:18 -0700493 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700496 alternate between laarr[0] and laarr[1] for locations of the
497 current extent, and the previous extent */
498 do {
499 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700500 brelse(prev_epos.bh);
501 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700502 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700504 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700505 brelse(cur_epos.bh);
506 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700507 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509
510 lbcount += elen;
511
Jan Karaff116fc2007-05-08 00:35:14 -0700512 prev_epos.block = cur_epos.block;
513 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Jan Karaff116fc2007-05-08 00:35:14 -0700515 prev_epos.offset = cur_epos.offset;
516 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700518 if ((etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1)) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 break;
520
521 c = !c;
522
523 laarr[c].extLength = (etype << 30) | elen;
524 laarr[c].extLocation = eloc;
525
526 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
527 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700528 ((elen + inode->i_sb->s_blocksize - 1) >>
529 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700531 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 } while (lbcount + elen <= b_off);
533
534 b_off -= lbcount;
535 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700536 /*
537 * Move prev_epos and cur_epos into indirect extent if we are at
538 * the pointer to it
539 */
540 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
541 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700544 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700546 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
547 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700549 ((elen + inode->i_sb->s_blocksize - 1) &
550 ~(inode->i_sb->s_blocksize - 1));
Jan Karaff116fc2007-05-08 00:35:14 -0700551 etype = udf_write_aext(inode, &cur_epos, eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700553 brelse(prev_epos.bh);
554 brelse(cur_epos.bh);
555 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 newblock = udf_get_lb_pblock(inode->i_sb, eloc, offset);
557 *phys = newblock;
558 return NULL;
559 }
560
Jan Kara31170b62007-05-08 00:35:21 -0700561 last_block = block;
562 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700563 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700564 int ret;
565
566 if (count) {
567 if (c)
568 laarr[0] = laarr[1];
569 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700570 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700571 /* Create a fake extent when there's not one */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700572 memset(&laarr[0].extLocation, 0x00, sizeof(kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700573 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
574 /* Will udf_extend_file() create real extent from a fake one? */
575 startnum = (offset > 0);
576 }
577 /* Create extents for the hole between EOF and offset */
578 ret = udf_extend_file(inode, &prev_epos, laarr, offset);
579 if (ret == -1) {
580 brelse(prev_epos.bh);
581 brelse(cur_epos.bh);
582 brelse(next_epos.bh);
583 /* We don't really know the error here so we just make
584 * something up */
585 *err = -ENOSPC;
586 return NULL;
587 }
588 c = 0;
589 offset = 0;
590 count += ret;
591 /* We are not covered by a preallocated extent? */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700592 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) != EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700593 /* Is there any real extent? - otherwise we overwrite
594 * the fake one... */
595 if (count)
596 c = !c;
597 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700598 inode->i_sb->s_blocksize;
599 memset(&laarr[c].extLocation, 0x00, sizeof(kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700600 count++;
601 endnum++;
Jan Kara31170b62007-05-08 00:35:21 -0700602 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700603 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700605 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 endnum = startnum = ((count > 2) ? 2 : count);
607
Jan Kara31170b62007-05-08 00:35:21 -0700608 /* if the current extent is in position 0, swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700609 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700610 laarr[2] = laarr[0];
611 laarr[0] = laarr[1];
612 laarr[1] = laarr[2];
613 c = 1;
614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Jan Kara31170b62007-05-08 00:35:21 -0700616 /* if the current block is located in an extent, read the next extent */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700617 if ((etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0)) != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700618 laarr[c + 1].extLength = (etype << 30) | elen;
619 laarr[c + 1].extLocation = eloc;
620 count++;
621 startnum++;
622 endnum++;
623 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 lastblock = 1;
Jan Kara31170b62007-05-08 00:35:21 -0700625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700629 * block in the extent corresponding to the requested block */
630 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700632 } else { /* otherwise, allocate a new block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (UDF_I_NEXT_ALLOC_BLOCK(inode) == block)
634 goal = UDF_I_NEXT_ALLOC_GOAL(inode);
635
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700636 if (!goal) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (!(goal = pgoal))
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700638 goal = UDF_I_LOCATION(inode).logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640
641 if (!(newblocknum = udf_new_block(inode->i_sb, inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700642 UDF_I_LOCATION(inode).partitionReferenceNum,
643 goal, err))) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700644 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 *err = -ENOSPC;
646 return NULL;
647 }
648 UDF_I_LENEXTENTS(inode) += inode->i_sb->s_blocksize;
649 }
650
651 /* if the extent the requsted block is located in contains multiple blocks,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700652 * split the extent into at most three extents. blocks prior to requested
653 * block, requested block, and blocks after requested block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
655
656#ifdef UDF_PREALLOCATE
657 /* preallocate blocks */
658 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
659#endif
660
661 /* merge any continuous blocks in laarr */
662 udf_merge_extents(inode, laarr, &endnum);
663
664 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700665 * of extents is greater than the old number, and deleting extents if
666 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700667 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Jan Kara3bf25cb2007-05-08 00:35:16 -0700669 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 if (!(newblock = udf_get_pblock(inode->i_sb, newblocknum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700672 UDF_I_LOCATION(inode).partitionReferenceNum, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return NULL;
674 }
675 *phys = newblock;
676 *err = 0;
677 *new = 1;
678 UDF_I_NEXT_ALLOC_BLOCK(inode) = block;
679 UDF_I_NEXT_ALLOC_GOAL(inode) = newblocknum;
680 inode->i_ctime = current_fs_time(inode->i_sb);
681
682 if (IS_SYNC(inode))
683 udf_sync_inode(inode);
684 else
685 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return result;
688}
689
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700690static void udf_split_extents(struct inode *inode, int *c, int offset,
691 int newblocknum,
692 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
693 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700696 (laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 int curr = *c;
698 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700699 inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int8_t etype = (laarr[curr].extLength >> 30);
701
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700702 if (blen == 1) {
703 ;
704 } else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700705 laarr[curr + 2] = laarr[curr + 1];
706 laarr[curr + 1] = laarr[curr];
707 } else {
708 laarr[curr + 3] = laarr[curr + 1];
709 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700712 if (offset) {
713 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700714 udf_free_blocks(inode->i_sb, inode, laarr[curr].extLocation, 0, offset);
715 laarr[curr].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
716 (offset << inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 laarr[curr].extLocation.logicalBlockNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700718 laarr[curr].extLocation.partitionReferenceNum = 0;
719 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 laarr[curr].extLength = (etype << 30) |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700721 (offset << inode->i_sb->s_blocksize_bits);
722 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700723 curr++;
724 (*c)++;
725 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 laarr[curr].extLocation.logicalBlockNum = newblocknum;
729 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
730 laarr[curr].extLocation.partitionReferenceNum =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700731 UDF_I_LOCATION(inode).partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700733 inode->i_sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700734 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700736 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700738 laarr[curr].extLocation.logicalBlockNum += (offset + 1);
739 laarr[curr].extLength = (etype << 30) |
740 ((blen - (offset + 1)) << inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700741 curr++;
742 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744 }
745}
746
747static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700748 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
749 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 int start, length = 0, currlength = 0, i;
752
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700753 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (!lastblock)
755 return;
756 else
757 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700758 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700759 if ((laarr[c + 1].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700760 start = c + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700761 length = currlength = (((laarr[c + 1].extLength & UDF_EXTENT_LENGTH_MASK) +
762 inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits);
763 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 start = c;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700768 for (i = start + 1; i <= *endnum; i++) {
769 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (lastblock)
771 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700772 } else if ((laarr[i].extLength >> 30) == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
773 length += (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
774 inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits);
775 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700780 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700782 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
783 inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700785 laarr[start].extLocation.partitionReferenceNum,
786 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ? length :
787 UDF_DEFAULT_PREALLOC_BLOCKS) - currlength);
788 if (numalloc) {
789 if (start == (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 laarr[start].extLength +=
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700791 (numalloc << inode->i_sb->s_blocksize_bits);
792 } else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700793 memmove(&laarr[c + 2], &laarr[c + 1],
794 sizeof(long_ad) * (*endnum - (c + 1)));
795 (*endnum)++;
796 laarr[c + 1].extLocation.logicalBlockNum = next;
797 laarr[c + 1].extLocation.partitionReferenceNum =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700798 laarr[c].extLocation.partitionReferenceNum;
799 laarr[c + 1].extLength = EXT_NOT_RECORDED_ALLOCATED |
800 (numalloc << inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700801 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700804 for (i = start + 1; numalloc && i < *endnum; i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700805 int elen = ((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
806 inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700808 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 laarr[i].extLength -=
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700810 (numalloc << inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700812 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700814 if (*endnum > (i + 1))
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700815 memmove(&laarr[i], &laarr[i + 1],
816 sizeof(long_ad) * (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700817 i--;
818 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700821 UDF_I_LENEXTENTS(inode) += numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823 }
824}
825
826static void udf_merge_extents(struct inode *inode,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700827 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
828 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 int i;
831
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700832 for (i = 0; i < (*endnum - 1); i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700833 if ((laarr[i].extLength >> 30) == (laarr[i + 1].extLength >> 30)) {
834 if (((laarr[i].extLength >> 30) == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
835 ((laarr[i + 1].extLocation.logicalBlockNum - laarr[i].extLocation.logicalBlockNum) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700836 (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700837 inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits))) {
838 if (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
839 (laarr[i + 1].extLength & UDF_EXTENT_LENGTH_MASK) +
840 inode->i_sb->s_blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
841 laarr[i + 1].extLength = (laarr[i + 1].extLength -
842 (laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
843 UDF_EXTENT_LENGTH_MASK) & ~(inode->i_sb->s_blocksize - 1);
844 laarr[i].extLength = (laarr[i].extLength & UDF_EXTENT_FLAG_MASK) +
845 (UDF_EXTENT_LENGTH_MASK + 1) - inode->i_sb->s_blocksize;
846 laarr[i + 1].extLocation.logicalBlockNum =
847 laarr[i].extLocation.logicalBlockNum +
848 ((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) >>
849 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700850 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700851 laarr[i].extLength = laarr[i + 1].extLength +
852 (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
853 inode->i_sb->s_blocksize - 1) & ~(inode->i_sb->s_blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700854 if (*endnum > (i + 2))
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700855 memmove(&laarr[i + 1], &laarr[i + 2],
856 sizeof(long_ad) * (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700857 i--;
858 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700861 } else if (((laarr[i].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
862 ((laarr[i + 1].extLength >> 30) == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
863 udf_free_blocks(inode->i_sb, inode, laarr[i].extLocation, 0,
864 ((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
865 inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 laarr[i].extLocation.logicalBlockNum = 0;
867 laarr[i].extLocation.partitionReferenceNum = 0;
868
869 if (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700870 (laarr[i + 1].extLength & UDF_EXTENT_LENGTH_MASK) +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700871 inode->i_sb->s_blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
872 laarr[i + 1].extLength = (laarr[i + 1].extLength -
873 (laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
874 UDF_EXTENT_LENGTH_MASK) & ~(inode->i_sb->s_blocksize - 1);
875 laarr[i].extLength = (laarr[i].extLength & UDF_EXTENT_FLAG_MASK) +
876 (UDF_EXTENT_LENGTH_MASK + 1) - inode->i_sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700877 } else {
878 laarr[i].extLength = laarr[i + 1].extLength +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700879 (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
880 inode->i_sb->s_blocksize - 1) & ~(inode->i_sb->s_blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700881 if (*endnum > (i + 2))
882 memmove(&laarr[i + 1], &laarr[i + 2],
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700883 sizeof(long_ad) * (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700884 i--;
885 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700887 } else if ((laarr[i].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
888 udf_free_blocks(inode->i_sb, inode, laarr[i].extLocation, 0,
889 ((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
890 inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 laarr[i].extLocation.logicalBlockNum = 0;
892 laarr[i].extLocation.partitionReferenceNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700893 laarr[i].extLength = (laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) |
894 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 }
897}
898
899static void udf_update_extents(struct inode *inode,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700900 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
901 int startnum, int endnum,
902 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 int start = 0, i;
905 kernel_lb_addr tmploc;
906 uint32_t tmplen;
907
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700908 if (startnum > endnum) {
909 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -0700910 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700911 laarr[i].extLength);
912 } else if (startnum < endnum) {
913 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -0700914 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700915 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -0700916 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700917 &laarr[i].extLength, 1);
918 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920 }
921
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700922 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -0700923 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
924 udf_write_aext(inode, epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700925 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927}
928
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700929struct buffer_head *udf_bread(struct inode *inode, int block,
930 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700932 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 bh = udf_getblk(inode, block, create, err);
935 if (!bh)
936 return NULL;
937
938 if (buffer_uptodate(bh))
939 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 ll_rw_block(READ, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 wait_on_buffer(bh);
944 if (buffer_uptodate(bh))
945 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 brelse(bh);
948 *err = -EIO;
949 return NULL;
950}
951
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700952void udf_truncate(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 int offset;
955 int err;
956
957 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700958 S_ISLNK(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return;
960 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
961 return;
962
963 lock_kernel();
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700964 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700965 if (inode->i_sb->s_blocksize < (udf_file_entry_alloc_offset(inode) +
966 inode->i_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 udf_expand_file_adinicb(inode, inode->i_size, &err);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700968 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 inode->i_size = UDF_I_LENALLOC(inode);
970 unlock_kernel();
971 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700972 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 udf_truncate_extents(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700974 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700975 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700977 memset(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode) + offset, 0x00,
978 inode->i_sb->s_blocksize - offset - udf_file_entry_alloc_offset(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 UDF_I_LENALLOC(inode) = inode->i_size;
980 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700981 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700982 block_truncate_page(inode->i_mapping, inode->i_size, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 udf_truncate_extents(inode);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
987 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700988 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 else
990 mark_inode_dirty(inode);
991 unlock_kernel();
992}
993
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700994static void __udf_read_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 struct buffer_head *bh = NULL;
997 struct fileEntry *fe;
998 uint16_t ident;
999
1000 /*
1001 * Set defaults, but the inode is still incomplete!
1002 * Note: get_new_inode() sets the following on a new inode:
1003 * i_sb = sb
1004 * i_no = ino
1005 * i_flags = sb->s_flags
1006 * i_state = 0
1007 * clean_inode(): zero fills and sets
1008 * i_count = 1
1009 * i_nlink = 1
1010 * i_op = NULL;
1011 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 bh = udf_read_ptagged(inode->i_sb, UDF_I_LOCATION(inode), 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001013 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001015 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 make_bad_inode(inode);
1017 return;
1018 }
1019
1020 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001021 ident != TAG_IDENT_USE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001022 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed ident=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001023 inode->i_ino, ident);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001024 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 make_bad_inode(inode);
1026 return;
1027 }
1028
1029 fe = (struct fileEntry *)bh->b_data;
1030
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001031 if (le16_to_cpu(fe->icbTag.strategyType) == 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 struct buffer_head *ibh = NULL, *nbh = NULL;
1033 struct indirectEntry *ie;
1034
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001035 ibh = udf_read_ptagged(inode->i_sb, UDF_I_LOCATION(inode), 1, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001036 if (ident == TAG_IDENT_IE) {
1037 if (ibh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 kernel_lb_addr loc;
1039 ie = (struct indirectEntry *)ibh->b_data;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001042
1043 if (ie->indirectICB.extLength &&
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001044 (nbh = udf_read_ptagged(inode->i_sb, loc, 0, &ident))) {
1045 if (ident == TAG_IDENT_FE ||
1046 ident == TAG_IDENT_EFE) {
1047 memcpy(&UDF_I_LOCATION(inode), &loc,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001048 sizeof(kernel_lb_addr));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001049 brelse(bh);
1050 brelse(ibh);
1051 brelse(nbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 __udf_read_inode(inode);
1053 return;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001054 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001055 brelse(nbh);
1056 brelse(ibh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001058 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001059 brelse(ibh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001062 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001063 brelse(ibh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001064 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001065 } else if (le16_to_cpu(fe->icbTag.strategyType) != 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 printk(KERN_ERR "udf: unsupported strategy type: %d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001067 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001068 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 make_bad_inode(inode);
1070 return;
1071 }
1072 udf_fill_inode(inode, bh);
Jan Kara31170b62007-05-08 00:35:21 -07001073
Jan Kara3bf25cb2007-05-08 00:35:16 -07001074 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
1077static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1078{
1079 struct fileEntry *fe;
1080 struct extendedFileEntry *efe;
1081 time_t convtime;
1082 long convtime_usec;
1083 int offset;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001084 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 fe = (struct fileEntry *)bh->b_data;
1087 efe = (struct extendedFileEntry *)bh->b_data;
1088
1089 if (le16_to_cpu(fe->icbTag.strategyType) == 4)
1090 UDF_I_STRAT4096(inode) = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001091 else /* if (le16_to_cpu(fe->icbTag.strategyType) == 4096) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 UDF_I_STRAT4096(inode) = 1;
1093
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001094 UDF_I_ALLOCTYPE(inode) = le16_to_cpu(fe->icbTag.flags) & ICBTAG_FLAG_AD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 UDF_I_UNIQUE(inode) = 0;
1096 UDF_I_LENEATTR(inode) = 0;
1097 UDF_I_LENEXTENTS(inode) = 0;
1098 UDF_I_LENALLOC(inode) = 0;
1099 UDF_I_NEXT_ALLOC_BLOCK(inode) = 0;
1100 UDF_I_NEXT_ALLOC_GOAL(inode) = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001101 if (le16_to_cpu(fe->descTag.tagIdent) == TAG_IDENT_EFE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 UDF_I_EFE(inode) = 1;
1103 UDF_I_USE(inode) = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001104 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize - sizeof(struct extendedFileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001105 make_bad_inode(inode);
1106 return;
1107 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001108 memcpy(UDF_I_DATA(inode), bh->b_data + sizeof(struct extendedFileEntry),
1109 inode->i_sb->s_blocksize - sizeof(struct extendedFileEntry));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001110 } else if (le16_to_cpu(fe->descTag.tagIdent) == TAG_IDENT_FE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 UDF_I_EFE(inode) = 0;
1112 UDF_I_USE(inode) = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001113 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize - sizeof(struct fileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001114 make_bad_inode(inode);
1115 return;
1116 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001117 memcpy(UDF_I_DATA(inode), bh->b_data + sizeof(struct fileEntry),
1118 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1119 } else if (le16_to_cpu(fe->descTag.tagIdent) == TAG_IDENT_USE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 UDF_I_EFE(inode) = 0;
1121 UDF_I_USE(inode) = 1;
1122 UDF_I_LENALLOC(inode) =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001123 le32_to_cpu(((struct unallocSpaceEntry *)bh->b_data)->lengthAllocDescs);
1124 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize - sizeof(struct unallocSpaceEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001125 make_bad_inode(inode);
1126 return;
1127 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001128 memcpy(UDF_I_DATA(inode), bh->b_data + sizeof(struct unallocSpaceEntry),
1129 inode->i_sb->s_blocksize - sizeof(struct unallocSpaceEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return;
1131 }
1132
1133 inode->i_uid = le32_to_cpu(fe->uid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001134 if (inode->i_uid == -1 ||
1135 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1136 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001137 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 inode->i_gid = le32_to_cpu(fe->gid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001140 if (inode->i_gid == -1 ||
1141 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1142 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001143 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
1146 if (!inode->i_nlink)
1147 inode->i_nlink = 1;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 inode->i_size = le64_to_cpu(fe->informationLength);
1150 UDF_I_LENEXTENTS(inode) = inode->i_size;
1151
1152 inode->i_mode = udf_convert_permissions(fe);
1153 inode->i_mode &= ~UDF_SB(inode->i_sb)->s_umask;
1154
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001155 if (UDF_I_EFE(inode) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001157 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001159 if (udf_stamp_to_time(&convtime, &convtime_usec,
1160 lets_to_cpu(fe->accessTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 inode->i_atime.tv_sec = convtime;
1162 inode->i_atime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001163 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001164 inode->i_atime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001167 if (udf_stamp_to_time(&convtime, &convtime_usec,
1168 lets_to_cpu(fe->modificationTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 inode->i_mtime.tv_sec = convtime;
1170 inode->i_mtime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001171 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001172 inode->i_mtime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001175 if (udf_stamp_to_time(&convtime, &convtime_usec,
1176 lets_to_cpu(fe->attrTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 inode->i_ctime.tv_sec = convtime;
1178 inode->i_ctime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001179 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001180 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182
1183 UDF_I_UNIQUE(inode) = le64_to_cpu(fe->uniqueID);
1184 UDF_I_LENEATTR(inode) = le32_to_cpu(fe->lengthExtendedAttr);
1185 UDF_I_LENALLOC(inode) = le32_to_cpu(fe->lengthAllocDescs);
1186 offset = sizeof(struct fileEntry) + UDF_I_LENEATTR(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001187 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001188 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001189 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001191 if (udf_stamp_to_time(&convtime, &convtime_usec,
1192 lets_to_cpu(efe->accessTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 inode->i_atime.tv_sec = convtime;
1194 inode->i_atime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001195 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001196 inode->i_atime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001199 if (udf_stamp_to_time(&convtime, &convtime_usec,
1200 lets_to_cpu(efe->modificationTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 inode->i_mtime.tv_sec = convtime;
1202 inode->i_mtime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001203 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001204 inode->i_mtime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001207 if (udf_stamp_to_time(&convtime, &convtime_usec,
1208 lets_to_cpu(efe->createTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 UDF_I_CRTIME(inode).tv_sec = convtime;
1210 UDF_I_CRTIME(inode).tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001211 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001212 UDF_I_CRTIME(inode) = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001215 if (udf_stamp_to_time(&convtime, &convtime_usec,
1216 lets_to_cpu(efe->attrTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 inode->i_ctime.tv_sec = convtime;
1218 inode->i_ctime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001219 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001220 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222
1223 UDF_I_UNIQUE(inode) = le64_to_cpu(efe->uniqueID);
1224 UDF_I_LENEATTR(inode) = le32_to_cpu(efe->lengthExtendedAttr);
1225 UDF_I_LENALLOC(inode) = le32_to_cpu(efe->lengthAllocDescs);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001226 offset = sizeof(struct extendedFileEntry) + UDF_I_LENEATTR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001229 switch (fe->icbTag.fileType) {
1230 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001231 inode->i_op = &udf_dir_inode_operations;
1232 inode->i_fop = &udf_dir_operations;
1233 inode->i_mode |= S_IFDIR;
1234 inc_nlink(inode);
1235 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001236 case ICBTAG_FILE_TYPE_REALTIME:
1237 case ICBTAG_FILE_TYPE_REGULAR:
1238 case ICBTAG_FILE_TYPE_UNDEF:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001239 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
1240 inode->i_data.a_ops = &udf_adinicb_aops;
1241 else
1242 inode->i_data.a_ops = &udf_aops;
1243 inode->i_op = &udf_file_inode_operations;
1244 inode->i_fop = &udf_file_operations;
1245 inode->i_mode |= S_IFREG;
1246 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001247 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001248 inode->i_mode |= S_IFBLK;
1249 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001250 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001251 inode->i_mode |= S_IFCHR;
1252 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001253 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001254 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1255 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001256 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001257 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1258 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001259 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001260 inode->i_data.a_ops = &udf_symlink_aops;
1261 inode->i_op = &page_symlink_inode_operations;
1262 inode->i_mode = S_IFLNK | S_IRWXUGO;
1263 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001264 default:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001265 printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown file type=%d\n",
1266 inode->i_ino, fe->icbTag.fileType);
1267 make_bad_inode(inode);
1268 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001270 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001271 struct deviceSpec *dsea = (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001272 if (dsea) {
1273 init_special_inode(inode, inode->i_mode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001274 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1275 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 /* Developer ID ??? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001277 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 make_bad_inode(inode);
1279 }
1280 }
1281}
1282
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001283static int udf_alloc_i_data(struct inode *inode, size_t size)
1284{
1285 UDF_I_DATA(inode) = kmalloc(size, GFP_KERNEL);
1286
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001287 if (!UDF_I_DATA(inode)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001288 printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) no free memory\n",
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001289 inode->i_ino);
1290 return -ENOMEM;
1291 }
1292
1293 return 0;
1294}
1295
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001296static mode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
1298 mode_t mode;
1299 uint32_t permissions;
1300 uint32_t flags;
1301
1302 permissions = le32_to_cpu(fe->permissions);
1303 flags = le16_to_cpu(fe->icbTag.flags);
1304
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001305 mode = (( permissions ) & S_IRWXO) |
1306 (( permissions >> 2 ) & S_IRWXG) |
1307 (( permissions >> 4 ) & S_IRWXU) |
1308 (( flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1309 (( flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1310 (( flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 return mode;
1313}
1314
1315/*
1316 * udf_write_inode
1317 *
1318 * PURPOSE
1319 * Write out the specified inode.
1320 *
1321 * DESCRIPTION
1322 * This routine is called whenever an inode is synced.
1323 * Currently this routine is just a placeholder.
1324 *
1325 * HISTORY
1326 * July 1, 1997 - Andrew E. Mileski
1327 * Written, tested, and released.
1328 */
1329
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001330int udf_write_inode(struct inode *inode, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 int ret;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 lock_kernel();
1335 ret = udf_update_inode(inode, sync);
1336 unlock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return ret;
1339}
1340
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001341int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 return udf_update_inode(inode, 1);
1344}
1345
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001346static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 struct buffer_head *bh = NULL;
1349 struct fileEntry *fe;
1350 struct extendedFileEntry *efe;
1351 uint32_t udfperms;
1352 uint16_t icbflags;
1353 uint16_t crclen;
1354 int i;
1355 kernel_timestamp cpu_time;
1356 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001357 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001359 bh = udf_tread(inode->i_sb, udf_get_lb_pblock(inode->i_sb, UDF_I_LOCATION(inode), 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001360 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 udf_debug("bread failure\n");
1362 return -EIO;
1363 }
1364
1365 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
1366
1367 fe = (struct fileEntry *)bh->b_data;
1368 efe = (struct extendedFileEntry *)bh->b_data;
1369
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001370 if (le16_to_cpu(fe->descTag.tagIdent) == TAG_IDENT_USE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001372 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 use->lengthAllocDescs = cpu_to_le32(UDF_I_LENALLOC(inode));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001375 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry), UDF_I_DATA(inode),
1376 inode->i_sb->s_blocksize - sizeof(struct unallocSpaceEntry));
1377 crclen = sizeof(struct unallocSpaceEntry) + UDF_I_LENALLOC(inode) - sizeof(tag);
1378 use->descTag.tagLocation = cpu_to_le32(UDF_I_LOCATION(inode).logicalBlockNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 use->descTag.descCRCLength = cpu_to_le16(crclen);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001380 use->descTag.descCRC = cpu_to_le16(udf_crc((char *)use + sizeof(tag), crclen, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 use->descTag.tagChecksum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001383 for (i = 0; i < 16; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (i != 4)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001385 use->descTag.tagChecksum += ((uint8_t *)&(use->descTag))[i];
1386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 mark_buffer_dirty(bh);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001389 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return err;
1391 }
1392
Phillip Susi4d6660e2006-03-07 21:55:24 -08001393 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1394 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001395 else
1396 fe->uid = cpu_to_le32(inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Phillip Susi4d6660e2006-03-07 21:55:24 -08001398 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1399 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001400 else
1401 fe->gid = cpu_to_le32(inode->i_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001403 udfperms = ((inode->i_mode & S_IRWXO) ) |
1404 ((inode->i_mode & S_IRWXG) << 2) |
1405 ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001407 udfperms |= (le32_to_cpu(fe->permissions) &
1408 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1409 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1410 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 fe->permissions = cpu_to_le32(udfperms);
1412
1413 if (S_ISDIR(inode->i_mode))
1414 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1415 else
1416 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1417
1418 fe->informationLength = cpu_to_le64(inode->i_size);
1419
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001420 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001422 struct deviceSpec *dsea =
1423 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001424 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001426 udf_add_extendedattr(inode,
1427 sizeof(struct deviceSpec) +
1428 sizeof(regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 dsea->attrType = cpu_to_le32(12);
1430 dsea->attrSubtype = 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001431 dsea->attrLength = cpu_to_le32(sizeof(struct deviceSpec) +
1432 sizeof(regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 dsea->impUseLength = cpu_to_le32(sizeof(regid));
1434 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001435 eid = (regid *)dsea->impUse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 memset(eid, 0, sizeof(regid));
1437 strcpy(eid->ident, UDF_ID_DEVELOPER);
1438 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1439 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1440 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1441 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1442 }
1443
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001444 if (UDF_I_EFE(inode) == 0) {
1445 memcpy(bh->b_data + sizeof(struct fileEntry), UDF_I_DATA(inode),
1446 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001447 fe->logicalBlocksRecorded = cpu_to_le64(
1448 (inode->i_blocks + (1 << (inode->i_sb->s_blocksize_bits - 9)) - 1) >>
1449 (inode->i_sb->s_blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1452 fe->accessTime = cpu_to_lets(cpu_time);
1453 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1454 fe->modificationTime = cpu_to_lets(cpu_time);
1455 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1456 fe->attrTime = cpu_to_lets(cpu_time);
1457 memset(&(fe->impIdent), 0, sizeof(regid));
1458 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1459 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1460 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1461 fe->uniqueID = cpu_to_le64(UDF_I_UNIQUE(inode));
1462 fe->lengthExtendedAttr = cpu_to_le32(UDF_I_LENEATTR(inode));
1463 fe->lengthAllocDescs = cpu_to_le32(UDF_I_LENALLOC(inode));
1464 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1465 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001466 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001467 memcpy(bh->b_data + sizeof(struct extendedFileEntry), UDF_I_DATA(inode),
1468 inode->i_sb->s_blocksize - sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 efe->objectSize = cpu_to_le64(inode->i_size);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001470 efe->logicalBlocksRecorded = cpu_to_le64(
1471 (inode->i_blocks + (1 << (inode->i_sb->s_blocksize_bits - 9)) - 1) >>
1472 (inode->i_sb->s_blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 if (UDF_I_CRTIME(inode).tv_sec > inode->i_atime.tv_sec ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001475 (UDF_I_CRTIME(inode).tv_sec == inode->i_atime.tv_sec &&
1476 UDF_I_CRTIME(inode).tv_nsec > inode->i_atime.tv_nsec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 UDF_I_CRTIME(inode) = inode->i_atime;
1478 }
1479 if (UDF_I_CRTIME(inode).tv_sec > inode->i_mtime.tv_sec ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001480 (UDF_I_CRTIME(inode).tv_sec == inode->i_mtime.tv_sec &&
1481 UDF_I_CRTIME(inode).tv_nsec > inode->i_mtime.tv_nsec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 UDF_I_CRTIME(inode) = inode->i_mtime;
1483 }
1484 if (UDF_I_CRTIME(inode).tv_sec > inode->i_ctime.tv_sec ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001485 (UDF_I_CRTIME(inode).tv_sec == inode->i_ctime.tv_sec &&
1486 UDF_I_CRTIME(inode).tv_nsec > inode->i_ctime.tv_nsec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 UDF_I_CRTIME(inode) = inode->i_ctime;
1488 }
1489
1490 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1491 efe->accessTime = cpu_to_lets(cpu_time);
1492 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1493 efe->modificationTime = cpu_to_lets(cpu_time);
1494 if (udf_time_to_stamp(&cpu_time, UDF_I_CRTIME(inode)))
1495 efe->createTime = cpu_to_lets(cpu_time);
1496 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1497 efe->attrTime = cpu_to_lets(cpu_time);
1498
1499 memset(&(efe->impIdent), 0, sizeof(regid));
1500 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1501 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1502 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1503 efe->uniqueID = cpu_to_le64(UDF_I_UNIQUE(inode));
1504 efe->lengthExtendedAttr = cpu_to_le32(UDF_I_LENEATTR(inode));
1505 efe->lengthAllocDescs = cpu_to_le32(UDF_I_LENALLOC(inode));
1506 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1507 crclen = sizeof(struct extendedFileEntry);
1508 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001509 if (UDF_I_STRAT4096(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 fe->icbTag.strategyType = cpu_to_le16(4096);
1511 fe->icbTag.strategyParameter = cpu_to_le16(1);
1512 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001513 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 fe->icbTag.strategyType = cpu_to_le16(4);
1515 fe->icbTag.numEntries = cpu_to_le16(1);
1516 }
1517
1518 if (S_ISDIR(inode->i_mode))
1519 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1520 else if (S_ISREG(inode->i_mode))
1521 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1522 else if (S_ISLNK(inode->i_mode))
1523 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1524 else if (S_ISBLK(inode->i_mode))
1525 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1526 else if (S_ISCHR(inode->i_mode))
1527 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1528 else if (S_ISFIFO(inode->i_mode))
1529 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1530 else if (S_ISSOCK(inode->i_mode))
1531 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1532
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001533 icbflags = UDF_I_ALLOCTYPE(inode) |
1534 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1535 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1536 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1537 (le16_to_cpu(fe->icbTag.flags) &
1538 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1539 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001542 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 fe->descTag.descVersion = cpu_to_le16(3);
1544 else
1545 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001546 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001547 fe->descTag.tagLocation = cpu_to_le32(UDF_I_LOCATION(inode).logicalBlockNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 crclen += UDF_I_LENEATTR(inode) + UDF_I_LENALLOC(inode) - sizeof(tag);
1549 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001550 fe->descTag.descCRC = cpu_to_le16(udf_crc((char *)fe + sizeof(tag), crclen, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 fe->descTag.tagChecksum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001553 for (i = 0; i < 16; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (i != 4)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001555 fe->descTag.tagChecksum += ((uint8_t *)&(fe->descTag))[i];
1556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 /* write the data blocks */
1559 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001560 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 sync_dirty_buffer(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001562 if (buffer_req(bh) && !buffer_uptodate(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 printk("IO error syncing udf inode [%s:%08lx]\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001564 inode->i_sb->s_id, inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 err = -EIO;
1566 }
1567 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001568 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return err;
1571}
1572
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001573struct inode *udf_iget(struct super_block *sb, kernel_lb_addr ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1576 struct inode *inode = iget_locked(sb, block);
1577
1578 if (!inode)
1579 return NULL;
1580
1581 if (inode->i_state & I_NEW) {
1582 memcpy(&UDF_I_LOCATION(inode), &ino, sizeof(kernel_lb_addr));
1583 __udf_read_inode(inode);
1584 unlock_new_inode(inode);
1585 }
1586
1587 if (is_bad_inode(inode))
1588 goto out_iput;
1589
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001590 if (ino.logicalBlockNum >= UDF_SB(sb)->s_partmaps[ino.partitionReferenceNum].s_partition_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 udf_debug("block=%d, partition=%d out of range\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001592 ino.logicalBlockNum, ino.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 make_bad_inode(inode);
1594 goto out_iput;
1595 }
1596
1597 return inode;
1598
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001599 out_iput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 iput(inode);
1601 return NULL;
1602}
1603
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001604int8_t udf_add_aext(struct inode * inode, struct extent_position * epos,
1605 kernel_lb_addr eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
1607 int adsize;
1608 short_ad *sad = NULL;
1609 long_ad *lad = NULL;
1610 struct allocExtDesc *aed;
1611 int8_t etype;
1612 uint8_t *ptr;
1613
Jan Karaff116fc2007-05-08 00:35:14 -07001614 if (!epos->bh)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001615 ptr = UDF_I_DATA(inode) + epos->offset - udf_file_entry_alloc_offset(inode) + UDF_I_LENEATTR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 else
Jan Karaff116fc2007-05-08 00:35:14 -07001617 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
1620 adsize = sizeof(short_ad);
1621 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
1622 adsize = sizeof(long_ad);
1623 else
1624 return -1;
1625
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001626 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 char *sptr, *dptr;
1628 struct buffer_head *nbh;
1629 int err, loffset;
Jan Karaff116fc2007-05-08 00:35:14 -07001630 kernel_lb_addr obloc = epos->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001632 if (!(epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1633 obloc.partitionReferenceNum,
1634 obloc.logicalBlockNum, &err))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return -1;
1636 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001637 if (!(nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
1638 epos->block, 0)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return -1;
1640 }
1641 lock_buffer(nbh);
1642 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1643 set_buffer_uptodate(nbh);
1644 unlock_buffer(nbh);
1645 mark_buffer_dirty_inode(nbh, inode);
1646
1647 aed = (struct allocExtDesc *)(nbh->b_data);
1648 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001649 aed->previousAllocExtLocation = cpu_to_le32(obloc.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001650 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -07001651 loffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 aed->lengthAllocDescs = cpu_to_le32(adsize);
1653 sptr = ptr - adsize;
1654 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1655 memcpy(dptr, sptr, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -07001656 epos->offset = sizeof(struct allocExtDesc) + adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001657 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001658 loffset = epos->offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 aed->lengthAllocDescs = cpu_to_le32(0);
1660 sptr = ptr;
Jan Karaff116fc2007-05-08 00:35:14 -07001661 epos->offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001663 if (epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001664 aed = (struct allocExtDesc *)epos->bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 aed->lengthAllocDescs =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001666 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001667 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 UDF_I_LENALLOC(inode) += adsize;
1669 mark_inode_dirty(inode);
1670 }
1671 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001672 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001674 epos->block.logicalBlockNum, sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 else
1676 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001677 epos->block.logicalBlockNum, sizeof(tag));
1678 switch (UDF_I_ALLOCTYPE(inode)) {
1679 case ICBTAG_FLAG_AD_SHORT:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001680 sad = (short_ad *)sptr;
1681 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1682 inode->i_sb->s_blocksize);
1683 sad->extPosition = cpu_to_le32(epos->block.logicalBlockNum);
1684 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001685 case ICBTAG_FLAG_AD_LONG:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001686 lad = (long_ad *)sptr;
1687 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1688 inode->i_sb->s_blocksize);
1689 lad->extLocation = cpu_to_lelb(epos->block);
1690 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1691 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001693 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001694 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001695 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -07001696 udf_update_tag(epos->bh->b_data, loffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 else
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001698 udf_update_tag(epos->bh->b_data, sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001699 mark_buffer_dirty_inode(epos->bh, inode);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001700 brelse(epos->bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001701 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001703 }
Jan Karaff116fc2007-05-08 00:35:14 -07001704 epos->bh = nbh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
1706
Jan Karaff116fc2007-05-08 00:35:14 -07001707 etype = udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001709 if (!epos->bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 UDF_I_LENALLOC(inode) += adsize;
1711 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001712 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001713 aed = (struct allocExtDesc *)epos->bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 aed->lengthAllocDescs =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001715 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001716 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001717 udf_update_tag(epos->bh->b_data, epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 else
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001719 udf_update_tag(epos->bh->b_data, sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001720 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722
1723 return etype;
1724}
1725
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001726int8_t udf_write_aext(struct inode * inode, struct extent_position * epos,
1727 kernel_lb_addr eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 int adsize;
1730 uint8_t *ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001731 short_ad *sad;
1732 long_ad *lad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Jan Karaff116fc2007-05-08 00:35:14 -07001734 if (!epos->bh)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001735 ptr = UDF_I_DATA(inode) + epos->offset - udf_file_entry_alloc_offset(inode) + UDF_I_LENEATTR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 else
Jan Karaff116fc2007-05-08 00:35:14 -07001737 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001739 switch (UDF_I_ALLOCTYPE(inode)) {
1740 case ICBTAG_FLAG_AD_SHORT:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001741 sad = (short_ad *)ptr;
1742 sad->extLength = cpu_to_le32(elen);
1743 sad->extPosition = cpu_to_le32(eloc.logicalBlockNum);
1744 adsize = sizeof(short_ad);
1745 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001746 case ICBTAG_FLAG_AD_LONG:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001747 lad = (long_ad *)ptr;
1748 lad->extLength = cpu_to_le32(elen);
1749 lad->extLocation = cpu_to_lelb(eloc);
1750 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1751 adsize = sizeof(long_ad);
1752 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001753 default:
1754 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001757 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001758 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001759 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001760 struct allocExtDesc *aed = (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07001761 udf_update_tag(epos->bh->b_data,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001762 le32_to_cpu(aed->lengthAllocDescs) + sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
Jan Karaff116fc2007-05-08 00:35:14 -07001764 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001765 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07001770 epos->offset += adsize;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return (elen >> 30);
1773}
1774
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001775int8_t udf_next_aext(struct inode * inode, struct extent_position * epos,
1776 kernel_lb_addr * eloc, uint32_t * elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 int8_t etype;
1779
Jan Karaff116fc2007-05-08 00:35:14 -07001780 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001781 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Jan Karaff116fc2007-05-08 00:35:14 -07001782 epos->block = *eloc;
1783 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001784 brelse(epos->bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001785 if (!(epos->bh = udf_tread(inode->i_sb, udf_get_lb_pblock(inode->i_sb, epos->block, 0)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 udf_debug("reading block %d failed!\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001787 udf_get_lb_pblock(inode->i_sb, epos->block, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return -1;
1789 }
1790 }
1791
1792 return etype;
1793}
1794
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001795int8_t udf_current_aext(struct inode * inode, struct extent_position * epos,
1796 kernel_lb_addr * eloc, uint32_t * elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
1798 int alen;
1799 int8_t etype;
1800 uint8_t *ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001801 short_ad *sad;
1802 long_ad *lad;
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001805 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001806 if (!epos->offset)
1807 epos->offset = udf_file_entry_alloc_offset(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001808 ptr = UDF_I_DATA(inode) + epos->offset - udf_file_entry_alloc_offset(inode) + UDF_I_LENEATTR(inode);
1809 alen = udf_file_entry_alloc_offset(inode) + UDF_I_LENALLOC(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001810 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001811 if (!epos->offset)
1812 epos->offset = sizeof(struct allocExtDesc);
1813 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001814 alen = sizeof(struct allocExtDesc) +
1815 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001818 switch (UDF_I_ALLOCTYPE(inode)) {
1819 case ICBTAG_FLAG_AD_SHORT:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001820 if (!(sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001822 etype = le32_to_cpu(sad->extLength) >> 30;
1823 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
1824 eloc->partitionReferenceNum = UDF_I_LOCATION(inode).partitionReferenceNum;
1825 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
1826 break;
1827 case ICBTAG_FLAG_AD_LONG:
1828 if (!(lad = udf_get_filelongad(ptr, alen, &epos->offset, inc)))
1829 return -1;
1830 etype = le32_to_cpu(lad->extLength) >> 30;
1831 *eloc = lelb_to_cpu(lad->extLocation);
1832 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
1833 break;
1834 default:
1835 udf_debug("alloc_type = %d unsupported\n", UDF_I_ALLOCTYPE(inode));
1836 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
1838
1839 return etype;
1840}
1841
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001842static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
1843 kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
1845 kernel_lb_addr oeloc;
1846 uint32_t oelen;
1847 int8_t etype;
1848
Jan Karaff116fc2007-05-08 00:35:14 -07001849 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001850 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001852 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Jan Karaff116fc2007-05-08 00:35:14 -07001853 udf_write_aext(inode, &epos, neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 neloc = oeloc;
1855 nelen = (etype << 30) | oelen;
1856 }
Jan Karaff116fc2007-05-08 00:35:14 -07001857 udf_add_aext(inode, &epos, neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001858 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return (nelen >> 30);
1861}
1862
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001863int8_t udf_delete_aext(struct inode * inode, struct extent_position epos,
1864 kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Jan Karaff116fc2007-05-08 00:35:14 -07001866 struct extent_position oepos;
1867 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 int8_t etype;
1869 struct allocExtDesc *aed;
1870
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001871 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001872 get_bh(epos.bh);
1873 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875
1876 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
1877 adsize = sizeof(short_ad);
1878 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
1879 adsize = sizeof(long_ad);
1880 else
1881 adsize = 0;
1882
Jan Karaff116fc2007-05-08 00:35:14 -07001883 oepos = epos;
1884 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return -1;
1886
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001887 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Jan Karaff116fc2007-05-08 00:35:14 -07001888 udf_write_aext(inode, &oepos, eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001889 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001890 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07001891 brelse(oepos.bh);
1892 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07001893 oepos.bh = epos.bh;
1894 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896 }
1897 memset(&eloc, 0x00, sizeof(kernel_lb_addr));
1898 elen = 0;
1899
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001900 if (epos.bh != oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001901 udf_free_blocks(inode->i_sb, inode, epos.block, 0, 1);
1902 udf_write_aext(inode, &oepos, eloc, elen, 1);
1903 udf_write_aext(inode, &oepos, eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001904 if (!oepos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 UDF_I_LENALLOC(inode) -= (adsize * 2);
1906 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001907 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001908 aed = (struct allocExtDesc *)oepos.bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 aed->lengthAllocDescs =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001910 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) - (2 * adsize));
1911 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001912 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001913 udf_update_tag(oepos.bh->b_data, oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 else
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001915 udf_update_tag(oepos.bh->b_data, sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001916 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001918 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001919 udf_write_aext(inode, &oepos, eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001920 if (!oepos.bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 UDF_I_LENALLOC(inode) -= adsize;
1922 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001923 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001924 aed = (struct allocExtDesc *)oepos.bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 aed->lengthAllocDescs =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001926 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) - adsize);
1927 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001928 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001929 udf_update_tag(oepos.bh->b_data, epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 else
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001931 udf_update_tag(oepos.bh->b_data, sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001932 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
1934 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001935
Jan Kara3bf25cb2007-05-08 00:35:16 -07001936 brelse(epos.bh);
1937 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return (elen >> 30);
1940}
1941
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001942int8_t inode_bmap(struct inode * inode, sector_t block,
1943 struct extent_position * pos, kernel_lb_addr * eloc,
1944 uint32_t * elen, sector_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001946 loff_t lbcount = 0, bcount =
1947 (loff_t) block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 int8_t etype;
1949
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001950 if (block < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 printk(KERN_ERR "udf: inode_bmap: block < 0\n");
1952 return -1;
1953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Jan Karaff116fc2007-05-08 00:35:14 -07001955 pos->offset = 0;
1956 pos->block = UDF_I_LOCATION(inode);
1957 pos->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 *elen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001960 do {
1961 if ((etype = udf_next_aext(inode, pos, eloc, elen, 1)) == -1) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001962 *offset = (bcount - lbcount) >> inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 UDF_I_LENEXTENTS(inode) = lbcount;
1964 return -1;
1965 }
1966 lbcount += *elen;
1967 } while (lbcount <= bcount);
1968
Jan Kara60448b12007-05-08 00:35:13 -07001969 *offset = (bcount + *elen - lbcount) >> inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 return etype;
1972}
1973
Jan Kara60448b12007-05-08 00:35:13 -07001974long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
Jan Karaff116fc2007-05-08 00:35:14 -07001976 kernel_lb_addr eloc;
1977 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07001978 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001979 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 int ret;
1981
1982 lock_kernel();
1983
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001984 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30))
Jan Kara60448b12007-05-08 00:35:13 -07001985 ret = udf_get_lb_pblock(inode->i_sb, eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 else
1987 ret = 0;
1988
1989 unlock_kernel();
Jan Kara3bf25cb2007-05-08 00:35:16 -07001990 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
1993 return udf_fixed_to_variable(ret);
1994 else
1995 return ret;
1996}