blob: 6b4409f501964a4c29199b0c967431b439909d2b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * inode.c
3 *
4 * PURPOSE
5 * Inode handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * COPYRIGHT
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
12 *
13 * (C) 1998 Dave Boynton
14 * (C) 1998-2004 Ben Fennema
15 * (C) 1999-2000 Stelias Computing Inc
16 *
17 * HISTORY
18 *
19 * 10/04/98 dgb Added rudimentary directory functions
20 * 10/07/98 Fully working udf_block_map! It works!
21 * 11/25/98 bmap altered to better support extents
Marcin Slusarz4b111112008-02-08 04:20:36 -080022 * 12/06/98 blf partition support in udf_iget, udf_block_map
23 * and udf_read_inode
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 12/12/98 rewrote udf_block_map to handle next extents and descs across
25 * block boundaries (which is not actually allowed)
26 * 12/20/98 added support for strategy 4096
27 * 03/07/99 rewrote udf_block_map (again)
28 * New funcs, inode_bmap, udf_next_aext
29 * 04/19/99 Support for writing device EA's for major/minor #
30 */
31
32#include "udfdecl.h"
33#include <linux/mm.h>
34#include <linux/smp_lock.h>
35#include <linux/module.h>
36#include <linux/pagemap.h>
37#include <linux/buffer_head.h>
38#include <linux/writeback.h>
39#include <linux/slab.h>
40
41#include "udf_i.h"
42#include "udf_sb.h"
43
44MODULE_AUTHOR("Ben Fennema");
45MODULE_DESCRIPTION("Universal Disk Format Filesystem");
46MODULE_LICENSE("GPL");
47
48#define EXTENT_MERGE_SIZE 5
49
50static mode_t udf_convert_permissions(struct fileEntry *);
51static int udf_update_inode(struct inode *, int);
52static void udf_fill_inode(struct inode *, struct buffer_head *);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -070053static int udf_alloc_i_data(struct inode *inode, size_t size);
Jan Kara60448b12007-05-08 00:35:13 -070054static struct buffer_head *inode_getblk(struct inode *, sector_t, int *,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070055 long *, int *);
Jan Karaff116fc2007-05-08 00:35:14 -070056static int8_t udf_insert_aext(struct inode *, struct extent_position,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070057 kernel_lb_addr, uint32_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static void udf_split_extents(struct inode *, int *, int, int,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070059 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static void udf_prealloc_extents(struct inode *, int, int,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070061 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void udf_merge_extents(struct inode *,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070063 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static void udf_update_extents(struct inode *,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070065 kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
66 struct extent_position *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
68
69/*
70 * udf_delete_inode
71 *
72 * PURPOSE
73 * Clean-up before the specified inode is destroyed.
74 *
75 * DESCRIPTION
76 * This routine is called when the kernel destroys an inode structure
77 * ie. when iput() finds i_count == 0.
78 *
79 * HISTORY
80 * July 1, 1997 - Andrew E. Mileski
81 * Written, tested, and released.
82 *
83 * Called at the last iput() if i_nlink is zero.
84 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070085void udf_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Mark Fashehfef26652005-09-09 13:01:31 -070087 truncate_inode_pages(&inode->i_data, 0);
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (is_bad_inode(inode))
90 goto no_delete;
91
92 inode->i_size = 0;
93 udf_truncate(inode);
94 lock_kernel();
95
96 udf_update_inode(inode, IS_SYNC(inode));
97 udf_free_inode(inode);
98
99 unlock_kernel();
100 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700101
102no_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 clear_inode(inode);
104}
105
Jan Kara74584ae2007-06-16 10:16:14 -0700106/*
107 * If we are going to release inode from memory, we discard preallocation and
108 * truncate last inode extent to proper length. We could use drop_inode() but
109 * it's called under inode_lock and thus we cannot mark inode dirty there. We
110 * use clear_inode() but we have to make sure to write inode as it's not written
111 * automatically.
112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113void udf_clear_inode(struct inode *inode)
114{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800115 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (!(inode->i_sb->s_flags & MS_RDONLY)) {
117 lock_kernel();
Jan Kara74584ae2007-06-16 10:16:14 -0700118 /* Discard preallocation for directories, symlinks, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 udf_discard_prealloc(inode);
Jan Kara74584ae2007-06-16 10:16:14 -0700120 udf_truncate_tail_extent(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unlock_kernel();
Jan Kara74584ae2007-06-16 10:16:14 -0700122 write_inode_now(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800124 iinfo = UDF_I(inode);
125 kfree(iinfo->i_ext.i_data);
126 iinfo->i_ext.i_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129static int udf_writepage(struct page *page, struct writeback_control *wbc)
130{
131 return block_write_full_page(page, udf_get_block, wbc);
132}
133
134static int udf_readpage(struct file *file, struct page *page)
135{
136 return block_read_full_page(page, udf_get_block);
137}
138
Nick Pigginbe021ee2007-10-16 01:25:20 -0700139static int udf_write_begin(struct file *file, struct address_space *mapping,
140 loff_t pos, unsigned len, unsigned flags,
141 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Nick Pigginbe021ee2007-10-16 01:25:20 -0700143 *pagep = NULL;
144 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
145 udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148static sector_t udf_bmap(struct address_space *mapping, sector_t block)
149{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700150 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700153const struct address_space_operations udf_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700154 .readpage = udf_readpage,
155 .writepage = udf_writepage,
156 .sync_page = block_sync_page,
Nick Pigginbe021ee2007-10-16 01:25:20 -0700157 .write_begin = udf_write_begin,
158 .write_end = generic_write_end,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700159 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700162void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 struct page *page;
165 char *kaddr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800166 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct writeback_control udf_wbc = {
168 .sync_mode = WB_SYNC_NONE,
169 .nr_to_write = 1,
170 };
171
172 /* from now on we have normal address_space methods */
173 inode->i_data.a_ops = &udf_aops;
174
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800175 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800177 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800179 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 mark_inode_dirty(inode);
181 return;
182 }
183
184 page = grab_cache_page(inode->i_mapping, 0);
Matt Mackallcd7619d2005-05-01 08:59:01 -0700185 BUG_ON(!PageLocked(page));
186
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700187 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 kaddr = kmap(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800189 memset(kaddr + iinfo->i_lenAlloc, 0x00,
190 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
191 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
192 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 flush_dcache_page(page);
194 SetPageUptodate(page);
195 kunmap(page);
196 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800197 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
198 iinfo->i_lenAlloc);
199 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800201 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800203 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 inode->i_data.a_ops->writepage(page, &udf_wbc);
206 page_cache_release(page);
207
208 mark_inode_dirty(inode);
209}
210
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700211struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
212 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700215 struct buffer_head *dbh = NULL;
216 kernel_lb_addr eloc;
217 uint32_t elen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700219 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 struct udf_fileident_bh sfibh, dfibh;
222 loff_t f_pos = udf_ext0_offset(inode) >> 2;
223 int size = (udf_ext0_offset(inode) + inode->i_size) >> 2;
224 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800225 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
228 alloctype = ICBTAG_FLAG_AD_SHORT;
229 else
230 alloctype = ICBTAG_FLAG_AD_LONG;
231
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700232 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800233 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 mark_inode_dirty(inode);
235 return NULL;
236 }
237
238 /* alloc block, and copy data to it */
239 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800240 iinfo->i_location.partitionReferenceNum,
241 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (!(*block))
243 return NULL;
244 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800245 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800246 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (!newblock)
248 return NULL;
249 dbh = udf_tgetblk(inode->i_sb, newblock);
250 if (!dbh)
251 return NULL;
252 lock_buffer(dbh);
253 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
254 set_buffer_uptodate(dbh);
255 unlock_buffer(dbh);
256 mark_buffer_dirty_inode(dbh, inode);
257
Marcin Slusarz4b111112008-02-08 04:20:36 -0800258 sfibh.soffset = sfibh.eoffset =
259 (f_pos & ((inode->i_sb->s_blocksize - 1) >> 2)) << 2;
Jan Karaff116fc2007-05-08 00:35:14 -0700260 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 dfibh.soffset = dfibh.eoffset = 0;
262 dfibh.sbh = dfibh.ebh = dbh;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700263 while ((f_pos < size)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800264 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800265 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
266 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700267 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700268 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return NULL;
270 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800271 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 sfi->descTag.tagLocation = cpu_to_le32(*block);
273 dfibh.soffset = dfibh.eoffset;
274 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
275 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
276 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800277 sfi->fileIdent +
278 le16_to_cpu(sfi->lengthOfImpUse))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800279 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700280 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return NULL;
282 }
283 }
284 mark_buffer_dirty_inode(dbh, inode);
285
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800286 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
287 iinfo->i_lenAlloc);
288 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800290 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800291 iinfo->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 elen = inode->i_size;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800293 iinfo->i_lenExtents = elen;
Jan Karaff116fc2007-05-08 00:35:14 -0700294 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800295 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700296 epos.offset = udf_file_entry_alloc_offset(inode);
297 udf_add_aext(inode, &epos, eloc, elen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /* UniqueID stuff */
299
Jan Kara3bf25cb2007-05-08 00:35:16 -0700300 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 mark_inode_dirty(inode);
302 return dbh;
303}
304
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700305static int udf_get_block(struct inode *inode, sector_t block,
306 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 int err, new;
309 struct buffer_head *bh;
310 unsigned long phys;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800311 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700313 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 phys = udf_block_map(inode, block);
315 if (phys)
316 map_bh(bh_result, inode->i_sb, phys);
317 return 0;
318 }
319
320 err = -EIO;
321 new = 0;
322 bh = NULL;
323
324 lock_kernel();
325
326 if (block < 0)
327 goto abort_negative;
328
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800329 iinfo = UDF_I(inode);
330 if (block == iinfo->i_next_alloc_block + 1) {
331 iinfo->i_next_alloc_block++;
332 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334
335 err = 0;
336
337 bh = inode_getblk(inode, block, &err, &phys, &new);
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200338 BUG_ON(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (err)
340 goto abort;
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200341 BUG_ON(!phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 if (new)
344 set_buffer_new(bh_result);
345 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700346
347abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unlock_kernel();
349 return err;
350
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700351abort_negative:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 udf_warning(inode->i_sb, "udf_get_block", "block < 0");
353 goto abort;
354}
355
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700356static struct buffer_head *udf_getblk(struct inode *inode, long block,
357 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700359 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 struct buffer_head dummy;
361
362 dummy.b_state = 0;
363 dummy.b_blocknr = -1000;
364 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700365 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700367 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 lock_buffer(bh);
369 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
370 set_buffer_uptodate(bh);
371 unlock_buffer(bh);
372 mark_buffer_dirty_inode(bh, inode);
373 }
374 return bh;
375 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return NULL;
378}
379
Jan Kara31170b62007-05-08 00:35:21 -0700380/* Extend the file by 'blocks' blocks, return the number of extents added */
381int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800382 kernel_long_ad *last_ext, sector_t blocks)
Jan Kara31170b62007-05-08 00:35:21 -0700383{
384 sector_t add;
385 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
386 struct super_block *sb = inode->i_sb;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700387 kernel_lb_addr prealloc_loc = {};
Jan Kara31170b62007-05-08 00:35:21 -0700388 int prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800389 struct udf_inode_info *iinfo;
Jan Kara31170b62007-05-08 00:35:21 -0700390
391 /* The previous extent is fake and we should not extend by anything
392 * - there's nothing to do... */
393 if (!blocks && fake)
394 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700395
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800396 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700397 /* Round the last extent up to a multiple of block size */
398 if (last_ext->extLength & (sb->s_blocksize - 1)) {
399 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700400 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
401 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
402 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800403 iinfo->i_lenExtents =
404 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700405 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700406 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700407
Jan Kara31170b62007-05-08 00:35:21 -0700408 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800409 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
410 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700411 /* Save the extent so that we can reattach it to the end */
412 prealloc_loc = last_ext->extLocation;
413 prealloc_len = last_ext->extLength;
414 /* Mark the extent as a hole */
415 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700416 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700417 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800418 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700419 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700420
Jan Kara31170b62007-05-08 00:35:21 -0700421 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800422 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
423 EXT_NOT_RECORDED_NOT_ALLOCATED) {
424 add = ((1 << 30) - sb->s_blocksize -
425 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
426 sb->s_blocksize_bits;
Jan Kara31170b62007-05-08 00:35:21 -0700427 if (add > blocks)
428 add = blocks;
429 blocks -= add;
430 last_ext->extLength += add << sb->s_blocksize_bits;
431 }
432
433 if (fake) {
434 udf_add_aext(inode, last_pos, last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700435 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700436 count++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800437 } else
438 udf_write_aext(inode, last_pos, last_ext->extLocation,
439 last_ext->extLength, 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700440
Jan Kara31170b62007-05-08 00:35:21 -0700441 /* Managed to do everything necessary? */
442 if (!blocks)
443 goto out;
444
445 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
446 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800447 last_ext->extLocation.partitionReferenceNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700448 add = (1 << (30-sb->s_blocksize_bits)) - 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800449 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
450 (add << sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700451
Jan Kara31170b62007-05-08 00:35:21 -0700452 /* Create enough extents to cover the whole hole */
453 while (blocks > add) {
454 blocks -= add;
455 if (udf_add_aext(inode, last_pos, last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700456 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700457 return -1;
458 count++;
459 }
460 if (blocks) {
461 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700462 (blocks << sb->s_blocksize_bits);
Jan Kara31170b62007-05-08 00:35:21 -0700463 if (udf_add_aext(inode, last_pos, last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700464 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700465 return -1;
466 count++;
467 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700468
469out:
Jan Kara31170b62007-05-08 00:35:21 -0700470 /* Do we have some preallocated blocks saved? */
471 if (prealloc_len) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800472 if (udf_add_aext(inode, last_pos, prealloc_loc,
473 prealloc_len, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700474 return -1;
475 last_ext->extLocation = prealloc_loc;
476 last_ext->extLength = prealloc_len;
477 count++;
478 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700479
Jan Kara31170b62007-05-08 00:35:21 -0700480 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800481 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Jan Kara31170b62007-05-08 00:35:21 -0700482 last_pos->offset -= sizeof(short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800483 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Jan Kara31170b62007-05-08 00:35:21 -0700484 last_pos->offset -= sizeof(long_ad);
485 else
486 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700487
Jan Kara31170b62007-05-08 00:35:21 -0700488 return count;
489}
490
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700491static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
492 int *err, long *phys, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Jan Kara31170b62007-05-08 00:35:21 -0700494 static sector_t last_block;
Jan Karaff116fc2007-05-08 00:35:14 -0700495 struct buffer_head *result = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700497 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700499 uint32_t elen = 0, tmpelen;
500 kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700502 loff_t lbcount = 0, b_off = 0;
503 uint32_t newblocknum, newblock;
504 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800506 struct udf_inode_info *iinfo = UDF_I(inode);
507 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700508 int lastblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Jan Karaff116fc2007-05-08 00:35:14 -0700510 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800511 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700512 prev_epos.bh = NULL;
513 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700514 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700517 alternate between laarr[0] and laarr[1] for locations of the
518 current extent, and the previous extent */
519 do {
520 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700521 brelse(prev_epos.bh);
522 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700523 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700525 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700526 brelse(cur_epos.bh);
527 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700528 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530
531 lbcount += elen;
532
Jan Karaff116fc2007-05-08 00:35:14 -0700533 prev_epos.block = cur_epos.block;
534 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Jan Karaff116fc2007-05-08 00:35:14 -0700536 prev_epos.offset = cur_epos.offset;
537 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Marcin Slusarz4b111112008-02-08 04:20:36 -0800539 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
540 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 break;
542
543 c = !c;
544
545 laarr[c].extLength = (etype << 30) | elen;
546 laarr[c].extLocation = eloc;
547
548 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
549 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700550 ((elen + inode->i_sb->s_blocksize - 1) >>
551 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700553 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 } while (lbcount + elen <= b_off);
555
556 b_off -= lbcount;
557 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700558 /*
559 * Move prev_epos and cur_epos into indirect extent if we are at
560 * the pointer to it
561 */
562 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
563 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700566 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700568 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
569 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700571 ((elen + inode->i_sb->s_blocksize - 1) &
572 ~(inode->i_sb->s_blocksize - 1));
Jan Karaff116fc2007-05-08 00:35:14 -0700573 etype = udf_write_aext(inode, &cur_epos, eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700575 brelse(prev_epos.bh);
576 brelse(cur_epos.bh);
577 brelse(next_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 newblock = udf_get_lb_pblock(inode->i_sb, eloc, offset);
579 *phys = newblock;
580 return NULL;
581 }
582
Jan Kara31170b62007-05-08 00:35:21 -0700583 last_block = block;
584 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700585 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700586 int ret;
587
588 if (count) {
589 if (c)
590 laarr[0] = laarr[1];
591 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700592 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700593 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800594 memset(&laarr[0].extLocation, 0x00,
595 sizeof(kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700596 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800597 /* Will udf_extend_file() create real extent from
598 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700599 startnum = (offset > 0);
600 }
601 /* Create extents for the hole between EOF and offset */
602 ret = udf_extend_file(inode, &prev_epos, laarr, offset);
603 if (ret == -1) {
604 brelse(prev_epos.bh);
605 brelse(cur_epos.bh);
606 brelse(next_epos.bh);
607 /* We don't really know the error here so we just make
608 * something up */
609 *err = -ENOSPC;
610 return NULL;
611 }
612 c = 0;
613 offset = 0;
614 count += ret;
615 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800616 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
617 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700618 /* Is there any real extent? - otherwise we overwrite
619 * the fake one... */
620 if (count)
621 c = !c;
622 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700623 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800624 memset(&laarr[c].extLocation, 0x00,
625 sizeof(kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700626 count++;
627 endnum++;
Jan Kara31170b62007-05-08 00:35:21 -0700628 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700629 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700631 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 endnum = startnum = ((count > 2) ? 2 : count);
633
Marcin Slusarz4b111112008-02-08 04:20:36 -0800634 /* if the current extent is in position 0,
635 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700636 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700637 laarr[2] = laarr[0];
638 laarr[0] = laarr[1];
639 laarr[1] = laarr[2];
640 c = 1;
641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Marcin Slusarz4b111112008-02-08 04:20:36 -0800643 /* if the current block is located in an extent,
644 read the next extent */
645 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
646 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700647 laarr[c + 1].extLength = (etype << 30) | elen;
648 laarr[c + 1].extLocation = eloc;
649 count++;
650 startnum++;
651 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800652 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 lastblock = 1;
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700657 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800658 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800660 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800661 if (iinfo->i_next_alloc_block == block)
662 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700664 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800665 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800666 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668
Marcin Slusarz4b111112008-02-08 04:20:36 -0800669 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800670 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800671 goal, err);
672 if (!newblocknum) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700673 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 *err = -ENOSPC;
675 return NULL;
676 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800677 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Marcin Slusarz4b111112008-02-08 04:20:36 -0800680 /* if the extent the requsted block is located in contains multiple
681 * blocks, split the extent into at most three extents. blocks prior
682 * to requested block, requested block, and blocks after requested
683 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
685
686#ifdef UDF_PREALLOCATE
687 /* preallocate blocks */
688 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
689#endif
690
691 /* merge any continuous blocks in laarr */
692 udf_merge_extents(inode, laarr, &endnum);
693
694 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700695 * of extents is greater than the old number, and deleting extents if
696 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700697 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Jan Kara3bf25cb2007-05-08 00:35:16 -0700699 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Marcin Slusarz4b111112008-02-08 04:20:36 -0800701 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800702 iinfo->i_location.partitionReferenceNum, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800703 if (!newblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 *phys = newblock;
706 *err = 0;
707 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800708 iinfo->i_next_alloc_block = block;
709 iinfo->i_next_alloc_goal = newblocknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 inode->i_ctime = current_fs_time(inode->i_sb);
711
712 if (IS_SYNC(inode))
713 udf_sync_inode(inode);
714 else
715 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return result;
718}
719
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700720static void udf_split_extents(struct inode *inode, int *c, int offset,
721 int newblocknum,
722 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
723 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800725 unsigned long blocksize = inode->i_sb->s_blocksize;
726 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800729 (laarr[*c].extLength >> 30) ==
730 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 int curr = *c;
732 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800733 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int8_t etype = (laarr[curr].extLength >> 30);
735
Marcin Slusarz4b111112008-02-08 04:20:36 -0800736 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700737 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800738 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700739 laarr[curr + 2] = laarr[curr + 1];
740 laarr[curr + 1] = laarr[curr];
741 } else {
742 laarr[curr + 3] = laarr[curr + 1];
743 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700746 if (offset) {
747 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800748 udf_free_blocks(inode->i_sb, inode,
749 laarr[curr].extLocation,
750 0, offset);
751 laarr[curr].extLength =
752 EXT_NOT_RECORDED_NOT_ALLOCATED |
753 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800755 laarr[curr].extLocation.
756 partitionReferenceNum = 0;
757 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800759 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700760 curr++;
761 (*c)++;
762 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 laarr[curr].extLocation.logicalBlockNum = newblocknum;
766 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
767 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800768 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800770 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700771 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700773 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800775 laarr[curr].extLocation.logicalBlockNum +=
776 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700777 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800778 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700779 curr++;
780 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782 }
783}
784
785static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700786 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
787 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 int start, length = 0, currlength = 0, i;
790
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700791 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (!lastblock)
793 return;
794 else
795 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700796 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800797 if ((laarr[c + 1].extLength >> 30) ==
798 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700799 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800800 length = currlength =
801 (((laarr[c + 1].extLength &
802 UDF_EXTENT_LENGTH_MASK) +
803 inode->i_sb->s_blocksize - 1) >>
804 inode->i_sb->s_blocksize_bits);
805 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 start = c;
807 }
808
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700809 for (i = start + 1; i <= *endnum; i++) {
810 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (lastblock)
812 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800813 } else if ((laarr[i].extLength >> 30) ==
814 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
815 length += (((laarr[i].extLength &
816 UDF_EXTENT_LENGTH_MASK) +
817 inode->i_sb->s_blocksize - 1) >>
818 inode->i_sb->s_blocksize_bits);
819 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 break;
821 }
822
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700823 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700825 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800826 inode->i_sb->s_blocksize - 1) >>
827 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800829 laarr[start].extLocation.partitionReferenceNum,
830 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
831 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
832 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700833 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800834 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800836 (numalloc <<
837 inode->i_sb->s_blocksize_bits);
838 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700839 memmove(&laarr[c + 2], &laarr[c + 1],
840 sizeof(long_ad) * (*endnum - (c + 1)));
841 (*endnum)++;
842 laarr[c + 1].extLocation.logicalBlockNum = next;
843 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800844 laarr[c].extLocation.
845 partitionReferenceNum;
846 laarr[c + 1].extLength =
847 EXT_NOT_RECORDED_ALLOCATED |
848 (numalloc <<
849 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700850 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700853 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800854 int elen = ((laarr[i].extLength &
855 UDF_EXTENT_LENGTH_MASK) +
856 inode->i_sb->s_blocksize - 1) >>
857 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700859 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800861 (numalloc <<
862 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700864 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700866 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800867 memmove(&laarr[i],
868 &laarr[i + 1],
869 sizeof(long_ad) *
870 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700871 i--;
872 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800875 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800876 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878 }
879}
880
881static void udf_merge_extents(struct inode *inode,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700882 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
883 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800886 unsigned long blocksize = inode->i_sb->s_blocksize;
887 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700889 for (i = 0; i < (*endnum - 1); i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800890 kernel_long_ad *li /*l[i]*/ = &laarr[i];
891 kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Marcin Slusarz4b111112008-02-08 04:20:36 -0800893 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
894 (((li->extLength >> 30) ==
895 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
896 ((lip1->extLocation.logicalBlockNum -
897 li->extLocation.logicalBlockNum) ==
898 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
899 blocksize - 1) >> blocksize_bits)))) {
900
901 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
902 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
903 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
904 lip1->extLength = (lip1->extLength -
905 (li->extLength &
906 UDF_EXTENT_LENGTH_MASK) +
907 UDF_EXTENT_LENGTH_MASK) &
908 ~(blocksize - 1);
909 li->extLength = (li->extLength &
910 UDF_EXTENT_FLAG_MASK) +
911 (UDF_EXTENT_LENGTH_MASK + 1) -
912 blocksize;
913 lip1->extLocation.logicalBlockNum =
914 li->extLocation.logicalBlockNum +
915 ((li->extLength &
916 UDF_EXTENT_LENGTH_MASK) >>
917 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700918 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800919 li->extLength = lip1->extLength +
920 (((li->extLength &
921 UDF_EXTENT_LENGTH_MASK) +
922 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700923 if (*endnum > (i + 2))
924 memmove(&laarr[i + 1], &laarr[i + 2],
Marcin Slusarz4b111112008-02-08 04:20:36 -0800925 sizeof(long_ad) *
926 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700927 i--;
928 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800930 } else if (((li->extLength >> 30) ==
931 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
932 ((lip1->extLength >> 30) ==
933 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
934 udf_free_blocks(inode->i_sb, inode, li->extLocation, 0,
935 ((li->extLength &
936 UDF_EXTENT_LENGTH_MASK) +
937 blocksize - 1) >> blocksize_bits);
938 li->extLocation.logicalBlockNum = 0;
939 li->extLocation.partitionReferenceNum = 0;
940
941 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
942 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
943 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
944 lip1->extLength = (lip1->extLength -
945 (li->extLength &
946 UDF_EXTENT_LENGTH_MASK) +
947 UDF_EXTENT_LENGTH_MASK) &
948 ~(blocksize - 1);
949 li->extLength = (li->extLength &
950 UDF_EXTENT_FLAG_MASK) +
951 (UDF_EXTENT_LENGTH_MASK + 1) -
952 blocksize;
953 } else {
954 li->extLength = lip1->extLength +
955 (((li->extLength &
956 UDF_EXTENT_LENGTH_MASK) +
957 blocksize - 1) & ~(blocksize - 1));
958 if (*endnum > (i + 2))
959 memmove(&laarr[i + 1], &laarr[i + 2],
960 sizeof(long_ad) *
961 (*endnum - (i + 2)));
962 i--;
963 (*endnum)--;
964 }
965 } else if ((li->extLength >> 30) ==
966 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
967 udf_free_blocks(inode->i_sb, inode,
968 li->extLocation, 0,
969 ((li->extLength &
970 UDF_EXTENT_LENGTH_MASK) +
971 blocksize - 1) >> blocksize_bits);
972 li->extLocation.logicalBlockNum = 0;
973 li->extLocation.partitionReferenceNum = 0;
974 li->extLength = (li->extLength &
975 UDF_EXTENT_LENGTH_MASK) |
976 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978 }
979}
980
981static void udf_update_extents(struct inode *inode,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700982 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
983 int startnum, int endnum,
984 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 int start = 0, i;
987 kernel_lb_addr tmploc;
988 uint32_t tmplen;
989
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700990 if (startnum > endnum) {
991 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -0700992 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700993 laarr[i].extLength);
994 } else if (startnum < endnum) {
995 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -0700996 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700997 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -0700998 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700999 &laarr[i].extLength, 1);
1000 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
1002 }
1003
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001004 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001005 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1006 udf_write_aext(inode, epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001007 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009}
1010
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001011struct buffer_head *udf_bread(struct inode *inode, int block,
1012 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001014 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 bh = udf_getblk(inode, block, create, err);
1017 if (!bh)
1018 return NULL;
1019
1020 if (buffer_uptodate(bh))
1021 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 ll_rw_block(READ, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 wait_on_buffer(bh);
1026 if (buffer_uptodate(bh))
1027 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 brelse(bh);
1030 *err = -EIO;
1031 return NULL;
1032}
1033
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001034void udf_truncate(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 int offset;
1037 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001038 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001041 S_ISLNK(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return;
1043 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1044 return;
1045
1046 lock_kernel();
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001047 iinfo = UDF_I(inode);
1048 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001049 if (inode->i_sb->s_blocksize <
1050 (udf_file_entry_alloc_offset(inode) +
1051 inode->i_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 udf_expand_file_adinicb(inode, inode->i_size, &err);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001053 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1054 inode->i_size = iinfo->i_lenAlloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 unlock_kernel();
1056 return;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001057 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 udf_truncate_extents(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001059 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001061 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + offset,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001062 0x00, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001063 offset - udf_file_entry_alloc_offset(inode));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001064 iinfo->i_lenAlloc = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001066 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001067 block_truncate_page(inode->i_mapping, inode->i_size,
1068 udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 udf_truncate_extents(inode);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1073 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001074 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 else
1076 mark_inode_dirty(inode);
1077 unlock_kernel();
1078}
1079
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001080static void __udf_read_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
1082 struct buffer_head *bh = NULL;
1083 struct fileEntry *fe;
1084 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001085 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 /*
1088 * Set defaults, but the inode is still incomplete!
1089 * Note: get_new_inode() sets the following on a new inode:
1090 * i_sb = sb
1091 * i_no = ino
1092 * i_flags = sb->s_flags
1093 * i_state = 0
1094 * clean_inode(): zero fills and sets
1095 * i_count = 1
1096 * i_nlink = 1
1097 * i_op = NULL;
1098 */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001099 bh = udf_read_ptagged(inode->i_sb, iinfo->i_location, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001100 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001102 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 make_bad_inode(inode);
1104 return;
1105 }
1106
1107 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001108 ident != TAG_IDENT_USE) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001109 printk(KERN_ERR "udf: udf_read_inode(ino %ld) "
1110 "failed ident=%d\n", inode->i_ino, ident);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001111 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 make_bad_inode(inode);
1113 return;
1114 }
1115
1116 fe = (struct fileEntry *)bh->b_data;
1117
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001118 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 struct buffer_head *ibh = NULL, *nbh = NULL;
1120 struct indirectEntry *ie;
1121
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001122 ibh = udf_read_ptagged(inode->i_sb, iinfo->i_location, 1,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001123 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001124 if (ident == TAG_IDENT_IE) {
1125 if (ibh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 kernel_lb_addr loc;
1127 ie = (struct indirectEntry *)ibh->b_data;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001130
1131 if (ie->indirectICB.extLength &&
Marcin Slusarz4b111112008-02-08 04:20:36 -08001132 (nbh = udf_read_ptagged(inode->i_sb, loc, 0,
1133 &ident))) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001134 if (ident == TAG_IDENT_FE ||
1135 ident == TAG_IDENT_EFE) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001136 memcpy(&iinfo->i_location,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001137 &loc,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001138 sizeof(kernel_lb_addr));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001139 brelse(bh);
1140 brelse(ibh);
1141 brelse(nbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 __udf_read_inode(inode);
1143 return;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001144 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001145 brelse(nbh);
1146 brelse(ibh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001148 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001149 brelse(ibh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001152 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001153 brelse(ibh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001154 }
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001155 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 printk(KERN_ERR "udf: unsupported strategy type: %d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001157 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001158 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 make_bad_inode(inode);
1160 return;
1161 }
1162 udf_fill_inode(inode, bh);
Jan Kara31170b62007-05-08 00:35:21 -07001163
Jan Kara3bf25cb2007-05-08 00:35:16 -07001164 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1168{
1169 struct fileEntry *fe;
1170 struct extendedFileEntry *efe;
1171 time_t convtime;
1172 long convtime_usec;
1173 int offset;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001174 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001175 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 fe = (struct fileEntry *)bh->b_data;
1178 efe = (struct extendedFileEntry *)bh->b_data;
1179
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001180 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001181 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001182 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001183 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001185 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001186 ICBTAG_FLAG_AD_MASK;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001187 iinfo->i_unique = 0;
1188 iinfo->i_lenEAttr = 0;
1189 iinfo->i_lenExtents = 0;
1190 iinfo->i_lenAlloc = 0;
1191 iinfo->i_next_alloc_block = 0;
1192 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001193 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001194 iinfo->i_efe = 1;
1195 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001196 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1197 sizeof(struct extendedFileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001198 make_bad_inode(inode);
1199 return;
1200 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001201 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001202 bh->b_data + sizeof(struct extendedFileEntry),
1203 inode->i_sb->s_blocksize -
1204 sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001205 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001206 iinfo->i_efe = 0;
1207 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001208 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1209 sizeof(struct fileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001210 make_bad_inode(inode);
1211 return;
1212 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001213 memcpy(iinfo->i_ext.i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001214 bh->b_data + sizeof(struct fileEntry),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001215 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001216 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001217 iinfo->i_efe = 0;
1218 iinfo->i_use = 1;
1219 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001220 ((struct unallocSpaceEntry *)bh->b_data)->
1221 lengthAllocDescs);
1222 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1223 sizeof(struct unallocSpaceEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001224 make_bad_inode(inode);
1225 return;
1226 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001227 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001228 bh->b_data + sizeof(struct unallocSpaceEntry),
1229 inode->i_sb->s_blocksize -
1230 sizeof(struct unallocSpaceEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return;
1232 }
1233
1234 inode->i_uid = le32_to_cpu(fe->uid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001235 if (inode->i_uid == -1 ||
1236 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1237 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001238 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 inode->i_gid = le32_to_cpu(fe->gid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001241 if (inode->i_gid == -1 ||
1242 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1243 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001244 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
1247 if (!inode->i_nlink)
1248 inode->i_nlink = 1;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 inode->i_size = le64_to_cpu(fe->informationLength);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001251 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 inode->i_mode = udf_convert_permissions(fe);
1254 inode->i_mode &= ~UDF_SB(inode->i_sb)->s_umask;
1255
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001256 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001258 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001260 if (udf_stamp_to_time(&convtime, &convtime_usec,
1261 lets_to_cpu(fe->accessTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 inode->i_atime.tv_sec = convtime;
1263 inode->i_atime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001264 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001265 inode->i_atime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001268 if (udf_stamp_to_time(&convtime, &convtime_usec,
1269 lets_to_cpu(fe->modificationTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 inode->i_mtime.tv_sec = convtime;
1271 inode->i_mtime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001272 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001273 inode->i_mtime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001276 if (udf_stamp_to_time(&convtime, &convtime_usec,
1277 lets_to_cpu(fe->attrTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 inode->i_ctime.tv_sec = convtime;
1279 inode->i_ctime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001280 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001281 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001284 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1285 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1286 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1287 offset = sizeof(struct fileEntry) + iinfo->i_lenEAttr;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001288 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001289 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001290 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001292 if (udf_stamp_to_time(&convtime, &convtime_usec,
1293 lets_to_cpu(efe->accessTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 inode->i_atime.tv_sec = convtime;
1295 inode->i_atime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001296 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001297 inode->i_atime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001300 if (udf_stamp_to_time(&convtime, &convtime_usec,
1301 lets_to_cpu(efe->modificationTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 inode->i_mtime.tv_sec = convtime;
1303 inode->i_mtime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001304 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001305 inode->i_mtime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001308 if (udf_stamp_to_time(&convtime, &convtime_usec,
1309 lets_to_cpu(efe->createTime))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001310 iinfo->i_crtime.tv_sec = convtime;
1311 iinfo->i_crtime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001312 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001313 iinfo->i_crtime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001316 if (udf_stamp_to_time(&convtime, &convtime_usec,
1317 lets_to_cpu(efe->attrTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 inode->i_ctime.tv_sec = convtime;
1319 inode->i_ctime.tv_nsec = convtime_usec * 1000;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001320 } else {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001321 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001324 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1325 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1326 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001327 offset = sizeof(struct extendedFileEntry) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001328 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
1330
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001331 switch (fe->icbTag.fileType) {
1332 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001333 inode->i_op = &udf_dir_inode_operations;
1334 inode->i_fop = &udf_dir_operations;
1335 inode->i_mode |= S_IFDIR;
1336 inc_nlink(inode);
1337 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001338 case ICBTAG_FILE_TYPE_REALTIME:
1339 case ICBTAG_FILE_TYPE_REGULAR:
1340 case ICBTAG_FILE_TYPE_UNDEF:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001341 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001342 inode->i_data.a_ops = &udf_adinicb_aops;
1343 else
1344 inode->i_data.a_ops = &udf_aops;
1345 inode->i_op = &udf_file_inode_operations;
1346 inode->i_fop = &udf_file_operations;
1347 inode->i_mode |= S_IFREG;
1348 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001349 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001350 inode->i_mode |= S_IFBLK;
1351 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001352 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001353 inode->i_mode |= S_IFCHR;
1354 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001355 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001356 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1357 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001358 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001359 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1360 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001361 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001362 inode->i_data.a_ops = &udf_symlink_aops;
1363 inode->i_op = &page_symlink_inode_operations;
1364 inode->i_mode = S_IFLNK | S_IRWXUGO;
1365 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001366 default:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001367 printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
1368 "file type=%d\n", inode->i_ino,
1369 fe->icbTag.fileType);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001370 make_bad_inode(inode);
1371 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001373 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001374 struct deviceSpec *dsea =
1375 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001376 if (dsea) {
1377 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001378 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1379 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001381 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384}
1385
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001386static int udf_alloc_i_data(struct inode *inode, size_t size)
1387{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001388 struct udf_inode_info *iinfo = UDF_I(inode);
1389 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001390
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001391 if (!iinfo->i_ext.i_data) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001392 printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
1393 "no free memory\n", inode->i_ino);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001394 return -ENOMEM;
1395 }
1396
1397 return 0;
1398}
1399
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001400static mode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 mode_t mode;
1403 uint32_t permissions;
1404 uint32_t flags;
1405
1406 permissions = le32_to_cpu(fe->permissions);
1407 flags = le16_to_cpu(fe->icbTag.flags);
1408
Marcin Slusarz4b111112008-02-08 04:20:36 -08001409 mode = ((permissions) & S_IRWXO) |
1410 ((permissions >> 2) & S_IRWXG) |
1411 ((permissions >> 4) & S_IRWXU) |
1412 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1413 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1414 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 return mode;
1417}
1418
1419/*
1420 * udf_write_inode
1421 *
1422 * PURPOSE
1423 * Write out the specified inode.
1424 *
1425 * DESCRIPTION
1426 * This routine is called whenever an inode is synced.
1427 * Currently this routine is just a placeholder.
1428 *
1429 * HISTORY
1430 * July 1, 1997 - Andrew E. Mileski
1431 * Written, tested, and released.
1432 */
1433
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001434int udf_write_inode(struct inode *inode, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 int ret;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 lock_kernel();
1439 ret = udf_update_inode(inode, sync);
1440 unlock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return ret;
1443}
1444
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001445int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 return udf_update_inode(inode, 1);
1448}
1449
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001450static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
1452 struct buffer_head *bh = NULL;
1453 struct fileEntry *fe;
1454 struct extendedFileEntry *efe;
1455 uint32_t udfperms;
1456 uint16_t icbflags;
1457 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 kernel_timestamp cpu_time;
1459 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001460 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001461 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001462 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Marcin Slusarz4b111112008-02-08 04:20:36 -08001464 bh = udf_tread(inode->i_sb,
1465 udf_get_lb_pblock(inode->i_sb,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001466 iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001467 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 udf_debug("bread failure\n");
1469 return -EIO;
1470 }
1471
1472 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
1473
1474 fe = (struct fileEntry *)bh->b_data;
1475 efe = (struct extendedFileEntry *)bh->b_data;
1476
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001477 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001479 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001481 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001482 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001483 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001484 sizeof(struct unallocSpaceEntry));
1485 crclen = sizeof(struct unallocSpaceEntry) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001486 iinfo->i_lenAlloc - sizeof(tag);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001487 use->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001488 iinfo->i_location.
Marcin Slusarz4b111112008-02-08 04:20:36 -08001489 logicalBlockNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 use->descTag.descCRCLength = cpu_to_le16(crclen);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001491 use->descTag.descCRC = cpu_to_le16(udf_crc((char *)use +
1492 sizeof(tag), crclen,
1493 0));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001494 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 mark_buffer_dirty(bh);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001497 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return err;
1499 }
1500
Phillip Susi4d6660e2006-03-07 21:55:24 -08001501 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1502 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001503 else
1504 fe->uid = cpu_to_le32(inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Phillip Susi4d6660e2006-03-07 21:55:24 -08001506 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1507 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001508 else
1509 fe->gid = cpu_to_le32(inode->i_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Marcin Slusarz4b111112008-02-08 04:20:36 -08001511 udfperms = ((inode->i_mode & S_IRWXO)) |
1512 ((inode->i_mode & S_IRWXG) << 2) |
1513 ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Marcin Slusarz4b111112008-02-08 04:20:36 -08001515 udfperms |= (le32_to_cpu(fe->permissions) &
1516 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1517 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1518 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 fe->permissions = cpu_to_le32(udfperms);
1520
1521 if (S_ISDIR(inode->i_mode))
1522 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1523 else
1524 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1525
1526 fe->informationLength = cpu_to_le64(inode->i_size);
1527
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001528 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001530 struct deviceSpec *dsea =
1531 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001532 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001534 udf_add_extendedattr(inode,
1535 sizeof(struct deviceSpec) +
1536 sizeof(regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 dsea->attrType = cpu_to_le32(12);
1538 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001539 dsea->attrLength = cpu_to_le32(
1540 sizeof(struct deviceSpec) +
1541 sizeof(regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 dsea->impUseLength = cpu_to_le32(sizeof(regid));
1543 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001544 eid = (regid *)dsea->impUse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 memset(eid, 0, sizeof(regid));
1546 strcpy(eid->ident, UDF_ID_DEVELOPER);
1547 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1548 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1549 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1550 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1551 }
1552
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001553 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001554 memcpy(bh->b_data + sizeof(struct fileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001555 iinfo->i_ext.i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001556 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001557 fe->logicalBlocksRecorded = cpu_to_le64(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001558 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1559 (blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1562 fe->accessTime = cpu_to_lets(cpu_time);
1563 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1564 fe->modificationTime = cpu_to_lets(cpu_time);
1565 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1566 fe->attrTime = cpu_to_lets(cpu_time);
1567 memset(&(fe->impIdent), 0, sizeof(regid));
1568 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1569 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1570 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001571 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1572 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1573 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1575 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001576 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001577 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001578 iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001579 inode->i_sb->s_blocksize -
1580 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 efe->objectSize = cpu_to_le64(inode->i_size);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001582 efe->logicalBlocksRecorded = cpu_to_le64(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001583 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1584 (blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001586 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1587 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1588 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1589 iinfo->i_crtime = inode->i_atime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001590
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001591 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1592 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1593 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1594 iinfo->i_crtime = inode->i_mtime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001595
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001596 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1597 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1598 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1599 iinfo->i_crtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1602 efe->accessTime = cpu_to_lets(cpu_time);
1603 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1604 efe->modificationTime = cpu_to_lets(cpu_time);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001605 if (udf_time_to_stamp(&cpu_time, iinfo->i_crtime))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 efe->createTime = cpu_to_lets(cpu_time);
1607 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1608 efe->attrTime = cpu_to_lets(cpu_time);
1609
1610 memset(&(efe->impIdent), 0, sizeof(regid));
1611 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1612 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1613 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001614 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1615 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1616 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1618 crclen = sizeof(struct extendedFileEntry);
1619 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001620 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 fe->icbTag.strategyType = cpu_to_le16(4096);
1622 fe->icbTag.strategyParameter = cpu_to_le16(1);
1623 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001624 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 fe->icbTag.strategyType = cpu_to_le16(4);
1626 fe->icbTag.numEntries = cpu_to_le16(1);
1627 }
1628
1629 if (S_ISDIR(inode->i_mode))
1630 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1631 else if (S_ISREG(inode->i_mode))
1632 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1633 else if (S_ISLNK(inode->i_mode))
1634 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1635 else if (S_ISBLK(inode->i_mode))
1636 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1637 else if (S_ISCHR(inode->i_mode))
1638 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1639 else if (S_ISFIFO(inode->i_mode))
1640 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1641 else if (S_ISSOCK(inode->i_mode))
1642 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1643
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001644 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001645 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1646 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1647 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1648 (le16_to_cpu(fe->icbTag.flags) &
1649 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1650 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001653 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 fe->descTag.descVersion = cpu_to_le16(3);
1655 else
1656 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001657 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001658 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001659 iinfo->i_location.logicalBlockNum);
1660 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc -
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001661 sizeof(tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001663 fe->descTag.descCRC = cpu_to_le16(udf_crc((char *)fe + sizeof(tag),
1664 crclen, 0));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001665 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 /* write the data blocks */
1668 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001669 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 sync_dirty_buffer(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001671 if (buffer_req(bh) && !buffer_uptodate(bh)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001672 printk(KERN_WARNING "IO error syncing udf inode "
1673 "[%s:%08lx]\n", inode->i_sb->s_id,
1674 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 err = -EIO;
1676 }
1677 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001678 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return err;
1681}
1682
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001683struct inode *udf_iget(struct super_block *sb, kernel_lb_addr ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1686 struct inode *inode = iget_locked(sb, block);
1687
1688 if (!inode)
1689 return NULL;
1690
1691 if (inode->i_state & I_NEW) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001692 memcpy(&UDF_I(inode)->i_location, &ino, sizeof(kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 __udf_read_inode(inode);
1694 unlock_new_inode(inode);
1695 }
1696
1697 if (is_bad_inode(inode))
1698 goto out_iput;
1699
Marcin Slusarz4b111112008-02-08 04:20:36 -08001700 if (ino.logicalBlockNum >= UDF_SB(sb)->
1701 s_partmaps[ino.partitionReferenceNum].s_partition_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 udf_debug("block=%d, partition=%d out of range\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001703 ino.logicalBlockNum, ino.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 make_bad_inode(inode);
1705 goto out_iput;
1706 }
1707
1708 return inode;
1709
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001710 out_iput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 iput(inode);
1712 return NULL;
1713}
1714
Marcin Slusarz4b111112008-02-08 04:20:36 -08001715int8_t udf_add_aext(struct inode *inode, struct extent_position *epos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001716 kernel_lb_addr eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
1718 int adsize;
1719 short_ad *sad = NULL;
1720 long_ad *lad = NULL;
1721 struct allocExtDesc *aed;
1722 int8_t etype;
1723 uint8_t *ptr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001724 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Jan Karaff116fc2007-05-08 00:35:14 -07001726 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001727 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001728 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001729 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 else
Jan Karaff116fc2007-05-08 00:35:14 -07001731 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001733 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 adsize = sizeof(short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001735 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 adsize = sizeof(long_ad);
1737 else
1738 return -1;
1739
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001740 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 char *sptr, *dptr;
1742 struct buffer_head *nbh;
1743 int err, loffset;
Jan Karaff116fc2007-05-08 00:35:14 -07001744 kernel_lb_addr obloc = epos->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Marcin Slusarz4b111112008-02-08 04:20:36 -08001746 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1747 obloc.partitionReferenceNum,
1748 obloc.logicalBlockNum, &err);
1749 if (!epos->block.logicalBlockNum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return -1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001751 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
1752 epos->block,
1753 0));
1754 if (!nbh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 lock_buffer(nbh);
1757 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1758 set_buffer_uptodate(nbh);
1759 unlock_buffer(nbh);
1760 mark_buffer_dirty_inode(nbh, inode);
1761
1762 aed = (struct allocExtDesc *)(nbh->b_data);
1763 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001764 aed->previousAllocExtLocation =
1765 cpu_to_le32(obloc.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001766 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -07001767 loffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 aed->lengthAllocDescs = cpu_to_le32(adsize);
1769 sptr = ptr - adsize;
1770 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1771 memcpy(dptr, sptr, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -07001772 epos->offset = sizeof(struct allocExtDesc) + adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001773 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001774 loffset = epos->offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 aed->lengthAllocDescs = cpu_to_le32(0);
1776 sptr = ptr;
Jan Karaff116fc2007-05-08 00:35:14 -07001777 epos->offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001779 if (epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001780 aed = (struct allocExtDesc *)epos->bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 aed->lengthAllocDescs =
Marcin Slusarz4b111112008-02-08 04:20:36 -08001782 cpu_to_le32(le32_to_cpu(
1783 aed->lengthAllocDescs) + adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001784 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001785 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 mark_inode_dirty(inode);
1787 }
1788 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001789 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001791 epos->block.logicalBlockNum, sizeof(tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 else
1793 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001794 epos->block.logicalBlockNum, sizeof(tag));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001795 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001796 case ICBTAG_FLAG_AD_SHORT:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001797 sad = (short_ad *)sptr;
1798 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1799 inode->i_sb->s_blocksize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001800 sad->extPosition =
1801 cpu_to_le32(epos->block.logicalBlockNum);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001802 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001803 case ICBTAG_FLAG_AD_LONG:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001804 lad = (long_ad *)sptr;
1805 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1806 inode->i_sb->s_blocksize);
1807 lad->extLocation = cpu_to_lelb(epos->block);
1808 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1809 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001811 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001812 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001813 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -07001814 udf_update_tag(epos->bh->b_data, loffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001816 udf_update_tag(epos->bh->b_data,
1817 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001818 mark_buffer_dirty_inode(epos->bh, inode);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001819 brelse(epos->bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001820 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001822 }
Jan Karaff116fc2007-05-08 00:35:14 -07001823 epos->bh = nbh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
1825
Jan Karaff116fc2007-05-08 00:35:14 -07001826 etype = udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001828 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001829 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001831 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001832 aed = (struct allocExtDesc *)epos->bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 aed->lengthAllocDescs =
Marcin Slusarz4b111112008-02-08 04:20:36 -08001834 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) +
1835 adsize);
1836 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1837 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1838 udf_update_tag(epos->bh->b_data,
1839 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001841 udf_update_tag(epos->bh->b_data,
1842 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001843 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 }
1845
1846 return etype;
1847}
1848
Marcin Slusarz4b111112008-02-08 04:20:36 -08001849int8_t udf_write_aext(struct inode *inode, struct extent_position *epos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001850 kernel_lb_addr eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
1852 int adsize;
1853 uint8_t *ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001854 short_ad *sad;
1855 long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001856 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Jan Karaff116fc2007-05-08 00:35:14 -07001858 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001859 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001860 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001861 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 else
Jan Karaff116fc2007-05-08 00:35:14 -07001863 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001865 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001866 case ICBTAG_FLAG_AD_SHORT:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001867 sad = (short_ad *)ptr;
1868 sad->extLength = cpu_to_le32(elen);
1869 sad->extPosition = cpu_to_le32(eloc.logicalBlockNum);
1870 adsize = sizeof(short_ad);
1871 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001872 case ICBTAG_FLAG_AD_LONG:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001873 lad = (long_ad *)ptr;
1874 lad->extLength = cpu_to_le32(elen);
1875 lad->extLocation = cpu_to_lelb(eloc);
1876 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1877 adsize = sizeof(long_ad);
1878 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001879 default:
1880 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
1882
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001883 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001884 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001885 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001886 struct allocExtDesc *aed =
1887 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07001888 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001889 le32_to_cpu(aed->lengthAllocDescs) +
1890 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
Jan Karaff116fc2007-05-08 00:35:14 -07001892 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001893 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07001898 epos->offset += adsize;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return (elen >> 30);
1901}
1902
Marcin Slusarz4b111112008-02-08 04:20:36 -08001903int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
1904 kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
1906 int8_t etype;
1907
Jan Karaff116fc2007-05-08 00:35:14 -07001908 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001909 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001910 int block;
Jan Karaff116fc2007-05-08 00:35:14 -07001911 epos->block = *eloc;
1912 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001913 brelse(epos->bh);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001914 block = udf_get_lb_pblock(inode->i_sb, epos->block, 0);
1915 epos->bh = udf_tread(inode->i_sb, block);
1916 if (!epos->bh) {
1917 udf_debug("reading block %d failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return -1;
1919 }
1920 }
1921
1922 return etype;
1923}
1924
Marcin Slusarz4b111112008-02-08 04:20:36 -08001925int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
1926 kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
1928 int alen;
1929 int8_t etype;
1930 uint8_t *ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001931 short_ad *sad;
1932 long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001933 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001934
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001935 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001936 if (!epos->offset)
1937 epos->offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001938 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001939 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001940 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001941 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001942 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001943 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001944 if (!epos->offset)
1945 epos->offset = sizeof(struct allocExtDesc);
1946 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001947 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08001948 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
1949 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001952 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001953 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001954 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
1955 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001957 etype = le32_to_cpu(sad->extLength) >> 30;
1958 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001959 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001960 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001961 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
1962 break;
1963 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001964 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
1965 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001966 return -1;
1967 etype = le32_to_cpu(lad->extLength) >> 30;
1968 *eloc = lelb_to_cpu(lad->extLocation);
1969 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
1970 break;
1971 default:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001972 udf_debug("alloc_type = %d unsupported\n",
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001973 iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001974 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
1976
1977 return etype;
1978}
1979
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001980static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
1981 kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
1983 kernel_lb_addr oeloc;
1984 uint32_t oelen;
1985 int8_t etype;
1986
Jan Karaff116fc2007-05-08 00:35:14 -07001987 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001988 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001990 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Jan Karaff116fc2007-05-08 00:35:14 -07001991 udf_write_aext(inode, &epos, neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 neloc = oeloc;
1993 nelen = (etype << 30) | oelen;
1994 }
Jan Karaff116fc2007-05-08 00:35:14 -07001995 udf_add_aext(inode, &epos, neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001996 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return (nelen >> 30);
1999}
2000
Marcin Slusarz4b111112008-02-08 04:20:36 -08002001int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002002 kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Jan Karaff116fc2007-05-08 00:35:14 -07002004 struct extent_position oepos;
2005 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 int8_t etype;
2007 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002008 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002010 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002011 get_bh(epos.bh);
2012 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
2014
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002015 iinfo = UDF_I(inode);
2016 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 adsize = sizeof(short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002018 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 adsize = sizeof(long_ad);
2020 else
2021 adsize = 0;
2022
Jan Karaff116fc2007-05-08 00:35:14 -07002023 oepos = epos;
2024 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return -1;
2026
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002027 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Jan Karaff116fc2007-05-08 00:35:14 -07002028 udf_write_aext(inode, &oepos, eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002029 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002030 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07002031 brelse(oepos.bh);
2032 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07002033 oepos.bh = epos.bh;
2034 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
2036 }
2037 memset(&eloc, 0x00, sizeof(kernel_lb_addr));
2038 elen = 0;
2039
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002040 if (epos.bh != oepos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002041 udf_free_blocks(inode->i_sb, inode, epos.block, 0, 1);
2042 udf_write_aext(inode, &oepos, eloc, elen, 1);
2043 udf_write_aext(inode, &oepos, eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002044 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002045 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002047 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002048 aed = (struct allocExtDesc *)oepos.bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 aed->lengthAllocDescs =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002050 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) -
2051 (2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002052 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002053 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002054 udf_update_tag(oepos.bh->b_data,
2055 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002057 udf_update_tag(oepos.bh->b_data,
2058 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002059 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002061 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002062 udf_write_aext(inode, &oepos, eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002063 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002064 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002066 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002067 aed = (struct allocExtDesc *)oepos.bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 aed->lengthAllocDescs =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002069 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) -
2070 adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002071 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002072 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002073 udf_update_tag(oepos.bh->b_data,
2074 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002076 udf_update_tag(oepos.bh->b_data,
2077 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002078 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
2080 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002081
Jan Kara3bf25cb2007-05-08 00:35:16 -07002082 brelse(epos.bh);
2083 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 return (elen >> 30);
2086}
2087
Marcin Slusarz4b111112008-02-08 04:20:36 -08002088int8_t inode_bmap(struct inode *inode, sector_t block,
2089 struct extent_position *pos, kernel_lb_addr *eloc,
2090 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002092 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002093 loff_t lbcount = 0, bcount =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002094 (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002096 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002098 if (block < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 printk(KERN_ERR "udf: inode_bmap: block < 0\n");
2100 return -1;
2101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002103 iinfo = UDF_I(inode);
Jan Karaff116fc2007-05-08 00:35:14 -07002104 pos->offset = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002105 pos->block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002106 pos->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 *elen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002109 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002110 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2111 if (etype == -1) {
2112 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002113 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return -1;
2115 }
2116 lbcount += *elen;
2117 } while (lbcount <= bcount);
2118
Marcin Slusarz4b111112008-02-08 04:20:36 -08002119 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 return etype;
2122}
2123
Jan Kara60448b12007-05-08 00:35:13 -07002124long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Jan Karaff116fc2007-05-08 00:35:14 -07002126 kernel_lb_addr eloc;
2127 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002128 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002129 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 int ret;
2131
2132 lock_kernel();
2133
Marcin Slusarz4b111112008-02-08 04:20:36 -08002134 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2135 (EXT_RECORDED_ALLOCATED >> 30))
Jan Kara60448b12007-05-08 00:35:13 -07002136 ret = udf_get_lb_pblock(inode->i_sb, eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 else
2138 ret = 0;
2139
2140 unlock_kernel();
Jan Kara3bf25cb2007-05-08 00:35:16 -07002141 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2144 return udf_fixed_to_variable(ret);
2145 else
2146 return ret;
2147}